Newer
Older
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
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);
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
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
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
break;
case T_LOCALBOOT:
err = parse_integer(c, &label->localboot);
break;
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(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(&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;
case T_INCLUDE:
err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg,
nest_level + 1);
break;
1140
1141
1142
1143
1144
1145
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
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
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
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
case T_PROMPT:
err = parse_integer(&p, &cfg->prompt);
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(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(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;
/*
* Create a menu and add items for all the labels.
*/
m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print);
if (!m)
return NULL;
list_for_each(pos, &cfg->labels) {
label = list_entry(pos, struct pxe_label, list);
if (menu_item_add(m, label->name, label) != 1) {
menu_destroy(m);
return NULL;
}
}
/*
* After we've created items for each label in the menu, set the
* menu's default label if one was specified.
*/
if (cfg->default_label) {
err = menu_default_set(m, cfg->default_label);
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(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(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(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.
*/
if (err == 1)
label_boot(choice);
else if (err != -ENOENT)
return;
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
boot_unattempted_labels(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;
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;
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
}
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((char *)(pxefile_addr_r));
if (cfg == NULL) {
printf("Error parsing config file\n");
return 1;
}
handle_pxe_menu(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"
);