1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
#include <linux/list.h>
#include <sys/times.h>
#include <fcntl.h>
#include <stdbool.h>
#include <string.h>
#include <core.h>
#include <fs.h>
#include "cli.h"
#include "console.h"
#include "com32.h"
#include "menu.h"
#include "config.h"
#include "syslinux/adv.h"
#include "syslinux/boot.h"
#include "syslinux/config.h"
#include <sys/module.h>
struct file_ext {
const char *name;
enum kernel_type type;
};
static const struct file_ext file_extensions[] = {
{ ".c32", IMAGE_TYPE_COM32 },
{ ".img", IMAGE_TYPE_FDIMAGE },
{ ".bss", IMAGE_TYPE_BSS },
{ ".bin", IMAGE_TYPE_BOOT },
{ ".bs", IMAGE_TYPE_BOOT },
{ ".0", IMAGE_TYPE_PXE },
{ NULL, 0 },
};
/*
* Return a pointer to one byte after the last character of the
* command.
*/
static inline const char *find_command(const char *str)
{
const char *p;
p = str;
while (*p && !my_isspace(*p))
p++;
return p;
}
__export uint32_t parse_image_type(const char *kernel)
{
const struct file_ext *ext;
const char *p;
int len;
/* Find the end of the command */
p = find_command(kernel);
len = p - kernel;
for (ext = file_extensions; ext->name; ext++) {
int elen = strlen(ext->name);
if (!strncmp(kernel + len - elen, ext->name, elen))
return ext->type;
}
/* use IMAGE_TYPE_KERNEL as default */
return IMAGE_TYPE_KERNEL;
}
/*
* Returns the kernel name with file extension if one wasn't present.
*/
static const char *get_extension(const char *kernel)
{
const struct file_ext *ext;
const char *p;
int len;
/* Find the end of the command */
p = find_command(kernel);
len = p - kernel;
for (ext = file_extensions; ext->name; ext++) {
char *str;
int elen = strlen(ext->name);
FILE *f;
str = malloc(len + elen + 1);
strncpy(str, kernel, len);
strncpy(str + len, ext->name, elen);
str[len + elen] = '\0';
f = findpath(str);
free(str);
if (f) {
fclose(f);
return ext->name;
}
}
return NULL;
}
const char *apply_extension(const char *kernel, const char *ext)
{
const char *p;
char *k;
int len = strlen(kernel);
int elen = strlen(ext);
k = malloc(len + elen + 1);
if (!k)
return NULL;
p = find_command(kernel);
len = p - kernel;
/* Copy just the kernel name */
memcpy(k, kernel, len);
/* Append the extension */
if (strncmp(p - elen, ext, elen)) {
memcpy(k + len, ext, elen);
len += elen;
}
/* Copy the rest of the command line */
strcpy(k + len, p);
k[len + strlen(p)] = '\0';
return k;
}
/*
* Attempt to load a kernel after deciding what type of image it is.
*
* We only return from this function if something went wrong loading
* the the kernel. If we return the caller should call enter_cmdline()
* so that the user can help us out.
*/
__export void load_kernel(const char *command_line)
{
struct menu_entry *me;
const char *cmdline;
const char *kernel;
uint32_t type;
kernel = strdup(command_line);
if (!kernel)
goto bad_kernel;
/* Virtual kernel? */
me = find_label(kernel);
if (me) {
const char *args;
char *cmd;
size_t len = strlen(me->cmdline) + 1;
/* Find the end of the command */
args = find_command(kernel);
while(*args && my_isspace(*args))
args++;
if (strlen(args))
len += strlen(args) + 1; /* +1 for space (' ') */
cmd = malloc(len);
if (!cmd)
goto bad_kernel;
if (strlen(args))
snprintf(cmd, len, "%s %s", me->cmdline, args);
else
strncpy(cmd, me->cmdline, len);
type = parse_image_type(cmd);
execute(cmd, type, false);
/* We shouldn't return */
goto bad_kernel;
}
if (!allowimplicit)
goto bad_implicit;
/* Insert a null character to ignore any user-specified options */
if (!allowoptions) {
char *p = (char *)find_command(kernel);
*p = '\0';
}
type = parse_image_type(kernel);
if (type == IMAGE_TYPE_KERNEL) {
const char *ext;
/*
* Automatically lookup the extension if one wasn't
* supplied by the user.
*/
ext = get_extension(kernel);
if (ext) {
const char *k;
k = apply_extension(kernel, ext);
if (!k)
goto bad_kernel;
free((void *)kernel);
kernel = k;
type = parse_image_type(kernel);
}
}
execute(kernel, type, true);
free((void *)kernel);
bad_implicit:
bad_kernel:
/*
* If we fail to boot the kernel execute the "onerror" command
* line.
*/
if (onerrorlen) {
me = find_label(onerror);
if (me)
rsprintf(&cmdline, "%s %s", me->cmdline, default_cmd);
else
rsprintf(&cmdline, "%s %s", onerror, default_cmd);
type = parse_image_type(cmdline);
execute(cmdline, type, true);
}
}
/*
* If this function returns you must call ldinux_enter_command() to
* preserve the 4.0x behaviour.
*/
void ldlinux_auto_boot(void)
{
if (!defaultlevel) {
if (strlen(ConfigName))
printf("No DEFAULT or UI configuration directive found!\n");
if (noescape)
kaboom();
} else
load_kernel(default_cmd);
}
static void enter_cmdline(void)
{
const char *cmdline;
/* Enter endless command line prompt, should support "exit" */
while (1) {
bool to = false;
if (noescape) {
ldlinux_auto_boot();
continue;
}
cmdline = edit_cmdline("boot:", 1, NULL, cat_help_file, &to);
printf("\n");
/* return if user only press enter or we timed out */
if (!cmdline || cmdline[0] == '\0') {
if (to && ontimeoutlen)
load_kernel(ontimeout);
else
ldlinux_auto_boot();
} else
load_kernel(cmdline);
}
}
void ldlinux_enter_command(void)
{
enter_cmdline();
}
/*
* Undo the work we did in openconsole().
*/
static void __destructor close_console(void)
{
int i;
for (i = 0; i <= 2; i++)
close(i);
}
void ldlinux_console_init(void)
{
openconsole(&dev_stdcon_r, &dev_ansiserial_w);
}
__export int main(int argc __unused, char **argv)
{
const void *adv;
const char *cmdline;
size_t count = 0;
ldlinux_console_init();
parse_configs(&argv[1]);
__syslinux_set_serial_console_info();
adv = syslinux_getadv(ADV_BOOTONCE, &count);
if (adv && count) {
/*
* We apparently have a boot-once set; clear it and
* then execute the boot-once.
*/
char *src, *dst;
size_t i;
src = (char *)adv;
cmdline = dst = malloc(count + 1);
if (!dst) {
printf("Failed to allocate memory for ADV\n");
ldlinux_enter_command();
}
for (i = 0; i < count; i++)
*dst++ = *src++;
*dst = '\0'; /* Null-terminate */
/* Clear the boot-once data from the ADV */
if (!syslinux_setadv(ADV_BOOTONCE, 0, NULL))
syslinux_adv_write();
load_kernel(cmdline); /* Shouldn't return */
ldlinux_enter_command();
}
if (!forceprompt && !shift_is_held())
ldlinux_auto_boot();
if (defaultlevel > 1)
ldlinux_auto_boot();
ldlinux_enter_command();
return 0;
}
|