Newer
Older
static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg, char *b, int nest_level)
get_token(c, &t, L_KEYWORD);
switch (t.type) {
case T_TITLE:
err = parse_sliteral(c, &cfg->title);
break;
case T_INCLUDE:
err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
nest_level + 1);
break;
default:
printf("Ignoring malformed menu command: %.*s\n",
(int)(*c - s), s);
}
if (err < 0)
return err;
eol_or_eof(c);
return 1;
}
/*
* Handles parsing a 'menu line' when we're parsing a label.
*/
static int parse_label_menu(char **c, struct pxe_menu *cfg,
struct pxe_label *label)
{
struct token t;
char *s;
s = *c;
get_token(c, &t, L_KEYWORD);
switch (t.type) {
case T_DEFAULT:
if (!cfg->default_label)
cfg->default_label = strdup(label->name);
break;
case T_LABEL:
parse_sliteral(c, &label->menu);
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
break;
default:
printf("Ignoring malformed menu command: %.*s\n",
(int)(*c - s), s);
}
eol_or_eof(c);
return 0;
}
/*
* Parses a label and adds it to the list of labels for a menu.
*
* A label ends when we either get to the end of a file, or
* get some input we otherwise don't have a handler defined
* for.
*
*/
static int parse_label(char **c, struct pxe_menu *cfg)
{
struct token t;
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
char *s = *c;
struct pxe_label *label;
int err;
label = label_create();
if (!label)
return -ENOMEM;
err = parse_sliteral(c, &label->name);
if (err < 0) {
printf("Expected label name: %.*s\n", (int)(*c - s), s);
label_destroy(label);
return -EINVAL;
}
list_add_tail(&label->list, &cfg->labels);
while (1) {
s = *c;
get_token(c, &t, L_KEYWORD);
err = 0;
switch (t.type) {
case T_MENU:
err = parse_label_menu(c, cfg, label);
break;
case T_KERNEL:
err = parse_sliteral(c, &label->kernel);
break;
case T_APPEND:
err = parse_sliteral(c, &label->append);
if (label->initrd)
break;
s = strstr(label->append, "initrd=");
if (!s)
break;
s += 7;
len = (int)(strchr(s, ' ') - s);
label->initrd = malloc(len + 1);
strncpy(label->initrd, s, len);
label->initrd[len] = '\0';
if (!label->initrd)
err = parse_sliteral(c, &label->initrd);
case T_FDT:
if (!label->fdt)
err = parse_sliteral(c, &label->fdt);
break;
label->localboot = 1;
err = parse_integer(c, &label->localboot_val);
case T_IPAPPEND:
err = parse_integer(c, &label->ipappend);
break;
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
case T_EOL:
break;
default:
/*
* put the token back! we don't want it - it's the end
* of a label and whatever token this is, it's
* something for the menu level context to handle.
*/
*c = s;
return 1;
}
if (err < 0)
return err;
}
}
/*
* This 16 comes from the limit pxelinux imposes on nested includes.
*
* There is no reason at all we couldn't do more, but some limit helps prevent
* infinite (until crash occurs) recursion if a file tries to include itself.
*/
#define MAX_NEST_LEVEL 16
/*
* Entry point for parsing a menu file. nest_level indicates how many times
* we've nested in includes. It will be 1 for the top level menu file.
*
* Returns 1 on success, < 0 on error.
*/
static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
{
struct token t;
char *s, *b, *label_name;
int err;
b = p;
if (nest_level > MAX_NEST_LEVEL) {
printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
return -EMLINK;
}
while (1) {
s = p;
get_token(&p, &t, L_KEYWORD);
err = 0;
switch (t.type) {
case T_MENU:
err = parse_menu(cmdtp, &p, cfg, b, nest_level);
break;
case T_TIMEOUT:
err = parse_integer(&p, &cfg->timeout);
break;
case T_LABEL:
err = parse_label(&p, cfg);
break;
case T_DEFAULT:
err = parse_sliteral(&p, &label_name);
if (label_name) {
if (cfg->default_label)
free(cfg->default_label);
cfg->default_label = label_name;
}
break;
err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
nest_level + 1);
break;
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
break;
case T_EOL:
break;
case T_EOF:
return 1;
default:
printf("Ignoring unknown command: %.*s\n",
(int)(p - s), s);
eol_or_eof(&p);
}
if (err < 0)
return err;
}
}
/*
* Free the memory used by a pxe_menu and its labels.
*/
static void destroy_pxe_menu(struct pxe_menu *cfg)
{
struct list_head *pos, *n;
struct pxe_label *label;
if (cfg->title)
free(cfg->title);
if (cfg->default_label)
free(cfg->default_label);
list_for_each_safe(pos, n, &cfg->labels) {
label = list_entry(pos, struct pxe_label, list);
label_destroy(label);
}
free(cfg);
}
/*
* Entry point for parsing a pxe file. This is only used for the top level
* file.
*
* Returns NULL if there is an error, otherwise, returns a pointer to a
* pxe_menu struct populated with the results of parsing the pxe file (and any
* files it includes). The resulting pxe_menu struct can be free()'d by using
* the destroy_pxe_menu() function.
*/
static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
{
struct pxe_menu *cfg;
cfg = malloc(sizeof(struct pxe_menu));
if (!cfg)
return NULL;
memset(cfg, 0, sizeof(struct pxe_menu));
INIT_LIST_HEAD(&cfg->labels);
if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
destroy_pxe_menu(cfg);
return NULL;
}
return cfg;
}
/*
* Converts a pxe_menu struct into a menu struct for use with U-boot's generic
* menu code.
*/
static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
{
struct pxe_label *label;
struct list_head *pos;
struct menu *m;
int err;
int i = 1;
char *default_num = NULL;
/*
* Create a menu and add items for all the labels.
*/
m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
NULL, NULL);
if (!m)
return NULL;
list_for_each(pos, &cfg->labels) {
label = list_entry(pos, struct pxe_label, list);
sprintf(label->num, "%d", i++);
if (menu_item_add(m, label->num, label) != 1) {
(strcmp(label->name, cfg->default_label) == 0))
}
/*
* After we've created items for each label in the menu, set the
* menu's default label if one was specified.
*/
if (default_num) {
err = menu_default_set(m, default_num);
if (err != 1) {
if (err != -ENOENT) {
menu_destroy(m);
return NULL;
}
printf("Missing default: %s\n", cfg->default_label);
}
}
return m;
}
/*
* Try to boot any labels we have yet to attempt to boot.
*/
static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
{
struct list_head *pos;
struct pxe_label *label;
list_for_each(pos, &cfg->labels) {
label = list_entry(pos, struct pxe_label, list);
if (!label->attempted)
label_boot(cmdtp, label);
}
}
/*
* Boot the system as prescribed by a pxe_menu.
*
* Use the menu system to either get the user's choice or the default, based
* on config or user input. If there is no default or user's choice,
* attempted to boot labels in the order they were given in pxe files.
* If the default or user's choice fails to boot, attempt to boot other
* labels in the order they were given in pxe files.
*
* If this function returns, there weren't any labels that successfully
* booted, or the user interrupted the menu selection via ctrl+c.
*/
static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
{
void *choice;
struct menu *m;
int err;
m = pxe_menu_to_menu(cfg);
if (!m)
return;
err = menu_get_choice(m, &choice);
menu_destroy(m);
/*
* err == 1 means we got a choice back from menu_get_choice.
*
* err == -ENOENT if the menu was setup to select the default but no
* default was set. in that case, we should continue trying to boot
* labels that haven't been attempted yet.
*
* otherwise, the user interrupted or there was some other error and
* we give up.
*/
err = label_boot(cmdtp, choice);
if (!err)
return;
} else if (err != -ENOENT) {
boot_unattempted_labels(cmdtp, cfg);
}
/*
* Boots a system using a pxe file
*
* Returns 0 on success, 1 on error.
*/
static int
do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
unsigned long pxefile_addr_r;
struct pxe_menu *cfg;
char *pxefile_addr_str;
do_getfile = do_get_tftp;
if (argc == 1) {
pxefile_addr_str = from_env("pxefile_addr_r");
if (!pxefile_addr_str)
return 1;
} else if (argc == 2) {
pxefile_addr_str = argv[1];
} else {
return CMD_RET_USAGE;
}
if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
printf("Invalid pxefile address: %s\n", pxefile_addr_str);
return 1;
}
cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
if (cfg == NULL) {
printf("Error parsing config file\n");
return 1;
}
handle_pxe_menu(cmdtp, cfg);
destroy_pxe_menu(cfg);
return 0;
}
static cmd_tbl_t cmd_pxe_sub[] = {
U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
};
int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
cmd_tbl_t *cp;
if (argc < 2)
return CMD_RET_USAGE;
/* drop initial "pxe" arg */
argc--;
argv++;
cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
if (cp)
return cp->cmd(cmdtp, flag, argc, argv);
return CMD_RET_USAGE;
}
U_BOOT_CMD(
pxe, 3, 1, do_pxe,
"commands to get and boot from pxe files",
"get - try to retrieve a pxe file using tftp\npxe "
"boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
);
/*
* Boots a system using a local disk syslinux/extlinux file
*
* Returns 0 on success, 1 on error.
*/
int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
unsigned long pxefile_addr_r;
struct pxe_menu *cfg;
char *pxefile_addr_str;
char *filename;
int prompt = 0;
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
if (strstr(argv[1], "-p")) {
prompt = 1;
argc--;
argv++;
}
if (argc < 4)
return cmd_usage(cmdtp);
if (argc < 5) {
pxefile_addr_str = from_env("pxefile_addr_r");
if (!pxefile_addr_str)
return 1;
} else {
pxefile_addr_str = argv[4];
}
if (argc < 6)
filename = getenv("bootfile");
else {
filename = argv[5];
setenv("bootfile", filename);
}
if (strstr(argv[3], "ext2"))
do_getfile = do_get_ext2;
else if (strstr(argv[3], "fat"))
do_getfile = do_get_fat;
else {
printf("Invalid filesystem: %s\n", argv[3]);
return 1;
}
fs_argv[1] = argv[1];
fs_argv[2] = argv[2];
if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
printf("Invalid pxefile address: %s\n", pxefile_addr_str);
return 1;
}
if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
printf("Error reading config file\n");
return 1;
}
cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
if (cfg == NULL) {
printf("Error parsing config file\n");
return 1;
}
if (prompt)
cfg->prompt = 1;
handle_pxe_menu(cmdtp, cfg);
destroy_pxe_menu(cfg);
return 0;
}
U_BOOT_CMD(
sysboot, 7, 1, do_sysboot,
"command to get and boot from syslinux files",
"[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
" - load and parse syslinux menu file 'filename' from ext2 or fat\n"
" filesystem on 'dev' on 'interface' to address 'addr'"
);