1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2010-2011 Calxeda, Inc.
4 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
16 #include <fdt_support.h>
17 #include <linux/libfdt.h>
18 #include <linux/string.h>
19 #include <linux/ctype.h>
21 #include <linux/list.h>
29 #include "pxe_utils.h"
31 #define MAX_TFTP_PATH_LEN 512
36 * Convert an ethaddr from the environment to the format used by pxelinux
37 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
38 * the beginning of the ethernet address to indicate a hardware type of
39 * Ethernet. Also converts uppercase hex characters into lowercase, to match
40 * pxelinux's behavior.
42 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
43 * environment, or some other value < 0 on error.
45 int format_mac_pxe(char *outbuf, size_t outbuf_len)
49 if (outbuf_len < 21) {
50 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
55 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
58 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
59 ethaddr[0], ethaddr[1], ethaddr[2],
60 ethaddr[3], ethaddr[4], ethaddr[5]);
66 * Returns the directory the file specified in the bootfile env variable is
67 * in. If bootfile isn't defined in the environment, return NULL, which should
68 * be interpreted as "don't prepend anything to paths".
70 static int get_bootfile_path(const char *file_path, char *bootfile_path,
71 size_t bootfile_path_size)
73 char *bootfile, *last_slash;
76 /* Only syslinux allows absolute paths */
77 if (file_path[0] == '/' && !is_pxe)
80 bootfile = from_env("bootfile");
85 last_slash = strrchr(bootfile, '/');
90 path_len = (last_slash - bootfile) + 1;
92 if (bootfile_path_size < path_len) {
93 printf("bootfile_path too small. (%zd < %zd)\n",
94 bootfile_path_size, path_len);
99 strncpy(bootfile_path, bootfile, path_len);
102 bootfile_path[path_len] = '\0';
107 int (*do_getfile)(struct cmd_tbl *cmdtp, const char *file_path,
111 * As in pxelinux, paths to files referenced from files we retrieve are
112 * relative to the location of bootfile. get_relfile takes such a path and
113 * joins it with the bootfile path to get the full path to the target file. If
114 * the bootfile path is NULL, we use file_path as is.
116 * Returns 1 for success, or < 0 on error.
118 static int get_relfile(struct cmd_tbl *cmdtp, const char *file_path,
119 unsigned long file_addr)
122 char relfile[MAX_TFTP_PATH_LEN + 1];
126 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
131 path_len = strlen(file_path);
132 path_len += strlen(relfile);
134 if (path_len > MAX_TFTP_PATH_LEN) {
135 printf("Base path too long (%s%s)\n", relfile, file_path);
137 return -ENAMETOOLONG;
140 strcat(relfile, file_path);
142 printf("Retrieving file: %s\n", relfile);
144 sprintf(addr_buf, "%lx", file_addr);
146 return do_getfile(cmdtp, relfile, addr_buf);
150 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
151 * 'bootfile' was specified in the environment, the path to bootfile will be
152 * prepended to 'file_path' and the resulting path will be used.
154 * Returns 1 on success, or < 0 for error.
156 int get_pxe_file(struct cmd_tbl *cmdtp, const char *file_path,
157 unsigned long file_addr)
159 unsigned long config_file_size;
164 err = get_relfile(cmdtp, file_path, file_addr);
170 * the file comes without a NUL byte at the end, so find out its size
171 * and add the NUL byte.
173 tftp_filesize = from_env("filesize");
178 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
181 buf = map_sysmem(file_addr + config_file_size, 1);
188 #define PXELINUX_DIR "pxelinux.cfg/"
191 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
192 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
193 * from the bootfile path, as described above.
195 * Returns 1 on success or < 0 on error.
197 int get_pxelinux_path(struct cmd_tbl *cmdtp, const char *file,
198 unsigned long pxefile_addr_r)
200 size_t base_len = strlen(PXELINUX_DIR);
201 char path[MAX_TFTP_PATH_LEN + 1];
203 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
204 printf("path (%s%s) too long, skipping\n",
206 return -ENAMETOOLONG;
209 sprintf(path, PXELINUX_DIR "%s", file);
211 return get_pxe_file(cmdtp, path, pxefile_addr_r);
215 * Wrapper to make it easier to store the file at file_path in the location
216 * specified by envaddr_name. file_path will be joined to the bootfile path,
217 * if any is specified.
219 * Returns 1 on success or < 0 on error.
221 static int get_relfile_envaddr(struct cmd_tbl *cmdtp, const char *file_path,
222 const char *envaddr_name)
224 unsigned long file_addr;
227 envaddr = from_env(envaddr_name);
232 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
235 return get_relfile(cmdtp, file_path, file_addr);
239 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
240 * result must be free()'d to reclaim the memory.
242 * Returns NULL if malloc fails.
244 static struct pxe_label *label_create(void)
246 struct pxe_label *label;
248 label = malloc(sizeof(struct pxe_label));
253 memset(label, 0, sizeof(struct pxe_label));
259 * Free the memory used by a pxe_label, including that used by its name,
260 * kernel, append and initrd members, if they're non NULL.
262 * So - be sure to only use dynamically allocated memory for the members of
263 * the pxe_label struct, unless you want to clean it up first. These are
264 * currently only created by the pxe file parsing code.
266 static void label_destroy(struct pxe_label *label)
289 if (label->fdtoverlays)
290 free(label->fdtoverlays);
296 * Print a label and its string members if they're defined.
298 * This is passed as a callback to the menu code for displaying each
301 static void label_print(void *data)
303 struct pxe_label *label = data;
304 const char *c = label->menu ? label->menu : label->name;
306 printf("%s:\t%s\n", label->num, c);
310 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
311 * environment variable is defined. Its contents will be executed as U-Boot
312 * command. If the label specified an 'append' line, its contents will be
313 * used to overwrite the contents of the 'bootargs' environment variable prior
314 * to running 'localcmd'.
316 * Returns 1 on success or < 0 on error.
318 static int label_localboot(struct pxe_label *label)
322 localcmd = from_env("localcmd");
328 char bootargs[CONFIG_SYS_CBSIZE];
330 cli_simple_process_macros(label->append, bootargs,
332 env_set("bootargs", bootargs);
335 debug("running: %s\n", localcmd);
337 return run_command_list(localcmd, strlen(localcmd), 0);
341 * Loads fdt overlays specified in 'fdtoverlays'.
343 #ifdef CONFIG_OF_LIBFDT_OVERLAY
344 static void label_boot_fdtoverlay(struct cmd_tbl *cmdtp, struct pxe_label *label)
346 char *fdtoverlay = label->fdtoverlays;
347 struct fdt_header *working_fdt;
348 char *fdtoverlay_addr_env;
349 ulong fdtoverlay_addr;
353 /* Get the main fdt and map it */
354 fdt_addr = simple_strtoul(env_get("fdt_addr_r"), NULL, 16);
355 working_fdt = map_sysmem(fdt_addr, 0);
356 err = fdt_check_header(working_fdt);
360 /* Get the specific overlay loading address */
361 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
362 if (!fdtoverlay_addr_env) {
363 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
367 fdtoverlay_addr = simple_strtoul(fdtoverlay_addr_env, NULL, 16);
369 /* Cycle over the overlay files and apply them in order */
371 struct fdt_header *blob;
376 /* Drop leading spaces */
377 while (*fdtoverlay == ' ')
380 /* Copy a single filename if multiple provided */
381 end = strstr(fdtoverlay, " ");
383 len = (int)(end - fdtoverlay);
384 overlayfile = malloc(len + 1);
385 strncpy(overlayfile, fdtoverlay, len);
386 overlayfile[len] = '\0';
388 overlayfile = fdtoverlay;
390 if (!strlen(overlayfile))
393 /* Load overlay file */
394 err = get_relfile_envaddr(cmdtp, overlayfile,
395 "fdtoverlay_addr_r");
397 printf("Failed loading overlay %s\n", overlayfile);
401 /* Resize main fdt */
402 fdt_shrink_to_minimum(working_fdt, 8192);
404 blob = map_sysmem(fdtoverlay_addr, 0);
405 err = fdt_check_header(blob);
407 printf("Invalid overlay %s, skipping\n",
412 err = fdt_overlay_apply_verbose(working_fdt, blob);
414 printf("Failed to apply overlay %s, skipping\n",
422 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
427 * Boot according to the contents of a pxe_label.
429 * If we can't boot for any reason, we return. A successful boot never
432 * The kernel will be stored in the location given by the 'kernel_addr_r'
433 * environment variable.
435 * If the label specifies an initrd file, it will be stored in the location
436 * given by the 'ramdisk_addr_r' environment variable.
438 * If the label specifies an 'append' line, its contents will overwrite that
439 * of the 'bootargs' environment variable.
441 static int label_boot(struct cmd_tbl *cmdtp, struct pxe_label *label)
443 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
445 char mac_str[29] = "";
446 char ip_str[68] = "";
447 char *fit_addr = NULL;
455 label->attempted = 1;
457 if (label->localboot) {
458 if (label->localboot_val >= 0)
459 label_localboot(label);
463 if (!label->kernel) {
464 printf("No kernel given, skipping %s\n",
470 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
471 printf("Skipping %s for failure retrieving initrd\n",
476 bootm_argv[2] = initrd_str;
477 strncpy(bootm_argv[2], env_get("ramdisk_addr_r"), 18);
478 strcat(bootm_argv[2], ":");
479 strncat(bootm_argv[2], env_get("filesize"), 9);
483 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
484 printf("Skipping %s for failure retrieving kernel\n",
489 if (label->ipappend & 0x1) {
490 sprintf(ip_str, " ip=%s:%s:%s:%s",
491 env_get("ipaddr"), env_get("serverip"),
492 env_get("gatewayip"), env_get("netmask"));
495 if (IS_ENABLED(CONFIG_CMD_NET)) {
496 if (label->ipappend & 0x2) {
499 strcpy(mac_str, " BOOTIF=");
500 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
506 if ((label->ipappend & 0x3) || label->append) {
507 char bootargs[CONFIG_SYS_CBSIZE] = "";
508 char finalbootargs[CONFIG_SYS_CBSIZE];
510 if (strlen(label->append ?: "") +
511 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
512 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
513 strlen(label->append ?: ""),
514 strlen(ip_str), strlen(mac_str),
520 strncpy(bootargs, label->append, sizeof(bootargs));
522 strcat(bootargs, ip_str);
523 strcat(bootargs, mac_str);
525 cli_simple_process_macros(bootargs, finalbootargs,
526 sizeof(finalbootargs));
527 env_set("bootargs", finalbootargs);
528 printf("append: %s\n", finalbootargs);
531 bootm_argv[1] = env_get("kernel_addr_r");
532 /* for FIT, append the configuration identifier */
534 int len = strlen(bootm_argv[1]) + strlen(label->config) + 1;
536 fit_addr = malloc(len);
538 printf("malloc fail (FIT address)\n");
541 snprintf(fit_addr, len, "%s%s", bootm_argv[1], label->config);
542 bootm_argv[1] = fit_addr;
546 * fdt usage is optional:
547 * It handles the following scenarios.
549 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
550 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
551 * bootm, and adjust argc appropriately.
553 * If retrieve fails and no exact fdt blob is specified in pxe file with
554 * "fdt" label, try Scenario 2.
556 * Scenario 2: If there is an fdt_addr specified, pass it along to
557 * bootm, and adjust argc appropriately.
559 * Scenario 3: fdt blob is not available.
561 bootm_argv[3] = env_get("fdt_addr_r");
563 /* if fdt label is defined then get fdt from server */
565 char *fdtfile = NULL;
566 char *fdtfilefree = NULL;
569 fdtfile = label->fdt;
570 } else if (label->fdtdir) {
571 char *f1, *f2, *f3, *f4, *slash;
573 f1 = env_get("fdtfile");
580 * For complex cases where this code doesn't
581 * generate the correct filename, the board
582 * code should set $fdtfile during early boot,
583 * or the boot scripts should set $fdtfile
584 * before invoking "pxe" or "sysboot".
588 f3 = env_get("board");
592 len = strlen(label->fdtdir);
595 else if (label->fdtdir[len - 1] != '/')
600 len = strlen(label->fdtdir) + strlen(slash) +
601 strlen(f1) + strlen(f2) + strlen(f3) +
603 fdtfilefree = malloc(len);
605 printf("malloc fail (FDT filename)\n");
609 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
610 label->fdtdir, slash, f1, f2, f3, f4);
611 fdtfile = fdtfilefree;
615 int err = get_relfile_envaddr(cmdtp, fdtfile,
620 bootm_argv[3] = NULL;
623 printf("Skipping %s for failure retrieving FDT\n",
629 #ifdef CONFIG_OF_LIBFDT_OVERLAY
630 if (label->fdtoverlays)
631 label_boot_fdtoverlay(cmdtp, label);
634 bootm_argv[3] = NULL;
639 bootm_argv[3] = env_get("fdt_addr");
647 kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
648 buf = map_sysmem(kernel_addr, 0);
649 /* Try bootm for legacy and FIT format image */
650 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
651 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
652 /* Try booting an AArch64 Linux kernel image */
653 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
654 do_booti(cmdtp, 0, bootm_argc, bootm_argv);
655 /* Try booting a Image */
656 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
657 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
658 /* Try booting an x86_64 Linux kernel image */
659 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
660 do_zboot_parent(cmdtp, 0, bootm_argc, bootm_argv, NULL);
671 * Tokens for the pxe file parser.
699 * A token - given by a value and a type.
703 enum token_type type;
707 * Keywords recognized.
709 static const struct token keywords[] = {
712 {"timeout", T_TIMEOUT},
713 {"default", T_DEFAULT},
714 {"prompt", T_PROMPT},
716 {"kernel", T_KERNEL},
718 {"localboot", T_LOCALBOOT},
719 {"append", T_APPEND},
720 {"initrd", T_INITRD},
721 {"include", T_INCLUDE},
722 {"devicetree", T_FDT},
724 {"devicetreedir", T_FDTDIR},
725 {"fdtdir", T_FDTDIR},
726 {"fdtoverlays", T_FDTOVERLAYS},
727 {"ontimeout", T_ONTIMEOUT,},
728 {"ipappend", T_IPAPPEND,},
729 {"background", T_BACKGROUND,},
734 * Since pxe(linux) files don't have a token to identify the start of a
735 * literal, we have to keep track of when we're in a state where a literal is
736 * expected vs when we're in a state a keyword is expected.
745 * get_string retrieves a string from *p and stores it as a token in
748 * get_string used for scanning both string literals and keywords.
750 * Characters from *p are copied into t-val until a character equal to
751 * delim is found, or a NUL byte is reached. If delim has the special value of
752 * ' ', any whitespace character will be used as a delimiter.
754 * If lower is unequal to 0, uppercase characters will be converted to
755 * lowercase in the result. This is useful to make keywords case
758 * The location of *p is updated to point to the first character after the end
759 * of the token - the ending delimiter.
761 * On success, the new value of t->val is returned. Memory for t->val is
762 * allocated using malloc and must be free()'d to reclaim it. If insufficient
763 * memory is available, NULL is returned.
765 static char *get_string(char **p, struct token *t, char delim, int lower)
771 * b and e both start at the beginning of the input stream.
773 * e is incremented until we find the ending delimiter, or a NUL byte
774 * is reached. Then, we take e - b to find the length of the token.
780 if ((delim == ' ' && isspace(*e)) || delim == *e)
788 * Allocate memory to hold the string, and copy it in, converting
789 * characters to lowercase if lower is != 0.
791 t->val = malloc(len + 1);
795 for (i = 0; i < len; i++, b++) {
797 t->val[i] = tolower(*b);
805 * Update *p so the caller knows where to continue scanning.
815 * Populate a keyword token with a type and value.
817 static void get_keyword(struct token *t)
821 for (i = 0; keywords[i].val; i++) {
822 if (!strcmp(t->val, keywords[i].val)) {
823 t->type = keywords[i].type;
830 * Get the next token. We have to keep track of which state we're in to know
831 * if we're looking to get a string literal or a keyword.
833 * *p is updated to point at the first character after the current token.
835 static void get_token(char **p, struct token *t, enum lex_state state)
841 /* eat non EOL whitespace */
846 * eat comments. note that string literals can't begin with #, but
847 * can contain a # after their first character.
850 while (*c && *c != '\n')
857 } else if (*c == '\0') {
860 } else if (state == L_SLITERAL) {
861 get_string(&c, t, '\n', 0);
862 } else if (state == L_KEYWORD) {
864 * when we expect a keyword, we first get the next string
865 * token delimited by whitespace, and then check if it
866 * matches a keyword in our keyword list. if it does, it's
867 * converted to a keyword token of the appropriate type, and
868 * if not, it remains a string token.
870 get_string(&c, t, ' ', 1);
878 * Increment *c until we get to the end of the current line, or EOF.
880 static void eol_or_eof(char **c)
882 while (**c && **c != '\n')
887 * All of these parse_* functions share some common behavior.
889 * They finish with *c pointing after the token they parse, and return 1 on
890 * success, or < 0 on error.
894 * Parse a string literal and store a pointer it at *dst. String literals
895 * terminate at the end of the line.
897 static int parse_sliteral(char **c, char **dst)
902 get_token(c, &t, L_SLITERAL);
904 if (t.type != T_STRING) {
905 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
915 * Parse a base 10 (unsigned) integer and store it at *dst.
917 static int parse_integer(char **c, int *dst)
922 get_token(c, &t, L_SLITERAL);
924 if (t.type != T_STRING) {
925 printf("Expected string: %.*s\n", (int)(*c - s), s);
929 *dst = simple_strtol(t.val, NULL, 10);
936 static int parse_pxefile_top(struct cmd_tbl *cmdtp, char *p, unsigned long base,
937 struct pxe_menu *cfg, int nest_level);
940 * Parse an include statement, and retrieve and parse the file it mentions.
942 * base should point to a location where it's safe to store the file, and
943 * nest_level should indicate how many nested includes have occurred. For this
944 * include, nest_level has already been incremented and doesn't need to be
947 static int handle_include(struct cmd_tbl *cmdtp, char **c, unsigned long base,
948 struct pxe_menu *cfg, int nest_level)
956 err = parse_sliteral(c, &include_path);
959 printf("Expected include path: %.*s\n", (int)(*c - s), s);
963 err = get_pxe_file(cmdtp, include_path, base);
966 printf("Couldn't retrieve %s\n", include_path);
970 buf = map_sysmem(base, 0);
971 ret = parse_pxefile_top(cmdtp, buf, base, cfg, nest_level);
978 * Parse lines that begin with 'menu'.
980 * base and nest are provided to handle the 'menu include' case.
982 * base should point to a location where it's safe to store the included file.
984 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
985 * a file it includes, 3 when parsing a file included by that file, and so on.
987 static int parse_menu(struct cmd_tbl *cmdtp, char **c, struct pxe_menu *cfg,
988 unsigned long base, int nest_level)
994 get_token(c, &t, L_KEYWORD);
998 err = parse_sliteral(c, &cfg->title);
1003 err = handle_include(cmdtp, c, base, cfg, nest_level + 1);
1007 err = parse_sliteral(c, &cfg->bmp);
1011 printf("Ignoring malformed menu command: %.*s\n",
1024 * Handles parsing a 'menu line' when we're parsing a label.
1026 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1027 struct pxe_label *label)
1034 get_token(c, &t, L_KEYWORD);
1038 if (!cfg->default_label)
1039 cfg->default_label = strdup(label->name);
1041 if (!cfg->default_label)
1046 parse_sliteral(c, &label->menu);
1049 printf("Ignoring malformed menu command: %.*s\n",
1059 * Handles parsing a 'kernel' label.
1060 * expecting "filename" or "<fit_filename>#cfg"
1062 static int parse_label_kernel(char **c, struct pxe_label *label)
1067 err = parse_sliteral(c, &label->kernel);
1071 s = strstr(label->kernel, "#");
1075 label->config = malloc(strlen(s) + 1);
1079 strcpy(label->config, s);
1086 * Parses a label and adds it to the list of labels for a menu.
1088 * A label ends when we either get to the end of a file, or
1089 * get some input we otherwise don't have a handler defined
1093 static int parse_label(char **c, struct pxe_menu *cfg)
1098 struct pxe_label *label;
1101 label = label_create();
1105 err = parse_sliteral(c, &label->name);
1107 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1108 label_destroy(label);
1112 list_add_tail(&label->list, &cfg->labels);
1116 get_token(c, &t, L_KEYWORD);
1121 err = parse_label_menu(c, cfg, label);
1126 err = parse_label_kernel(c, label);
1130 err = parse_sliteral(c, &label->append);
1133 s = strstr(label->append, "initrd=");
1137 len = (int)(strchr(s, ' ') - s);
1138 label->initrd = malloc(len + 1);
1139 strncpy(label->initrd, s, len);
1140 label->initrd[len] = '\0';
1146 err = parse_sliteral(c, &label->initrd);
1151 err = parse_sliteral(c, &label->fdt);
1156 err = parse_sliteral(c, &label->fdtdir);
1160 if (!label->fdtoverlays)
1161 err = parse_sliteral(c, &label->fdtoverlays);
1165 label->localboot = 1;
1166 err = parse_integer(c, &label->localboot_val);
1170 err = parse_integer(c, &label->ipappend);
1178 * put the token back! we don't want it - it's the end
1179 * of a label and whatever token this is, it's
1180 * something for the menu level context to handle.
1192 * This 16 comes from the limit pxelinux imposes on nested includes.
1194 * There is no reason at all we couldn't do more, but some limit helps prevent
1195 * infinite (until crash occurs) recursion if a file tries to include itself.
1197 #define MAX_NEST_LEVEL 16
1200 * Entry point for parsing a menu file. nest_level indicates how many times
1201 * we've nested in includes. It will be 1 for the top level menu file.
1203 * Returns 1 on success, < 0 on error.
1205 static int parse_pxefile_top(struct cmd_tbl *cmdtp, char *p, unsigned long base,
1206 struct pxe_menu *cfg, int nest_level)
1209 char *s, *b, *label_name;
1214 if (nest_level > MAX_NEST_LEVEL) {
1215 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1222 get_token(&p, &t, L_KEYWORD);
1228 err = parse_menu(cmdtp, &p, cfg,
1229 base + ALIGN(strlen(b) + 1, 4),
1234 err = parse_integer(&p, &cfg->timeout);
1238 err = parse_label(&p, cfg);
1243 err = parse_sliteral(&p, &label_name);
1246 if (cfg->default_label)
1247 free(cfg->default_label);
1249 cfg->default_label = label_name;
1255 err = handle_include(cmdtp, &p,
1256 base + ALIGN(strlen(b), 4), cfg,
1271 printf("Ignoring unknown command: %.*s\n",
1282 * Free the memory used by a pxe_menu and its labels.
1284 void destroy_pxe_menu(struct pxe_menu *cfg)
1286 struct list_head *pos, *n;
1287 struct pxe_label *label;
1292 if (cfg->default_label)
1293 free(cfg->default_label);
1295 list_for_each_safe(pos, n, &cfg->labels) {
1296 label = list_entry(pos, struct pxe_label, list);
1298 label_destroy(label);
1305 * Entry point for parsing a pxe file. This is only used for the top level
1308 * Returns NULL if there is an error, otherwise, returns a pointer to a
1309 * pxe_menu struct populated with the results of parsing the pxe file (and any
1310 * files it includes). The resulting pxe_menu struct can be free()'d by using
1311 * the destroy_pxe_menu() function.
1313 struct pxe_menu *parse_pxefile(struct cmd_tbl *cmdtp, unsigned long menucfg)
1315 struct pxe_menu *cfg;
1319 cfg = malloc(sizeof(struct pxe_menu));
1324 memset(cfg, 0, sizeof(struct pxe_menu));
1326 INIT_LIST_HEAD(&cfg->labels);
1328 buf = map_sysmem(menucfg, 0);
1329 r = parse_pxefile_top(cmdtp, buf, menucfg, cfg, 1);
1333 destroy_pxe_menu(cfg);
1341 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1344 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1346 struct pxe_label *label;
1347 struct list_head *pos;
1351 char *default_num = NULL;
1354 * Create a menu and add items for all the labels.
1356 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
1357 cfg->prompt, NULL, label_print, NULL, NULL);
1362 list_for_each(pos, &cfg->labels) {
1363 label = list_entry(pos, struct pxe_label, list);
1365 sprintf(label->num, "%d", i++);
1366 if (menu_item_add(m, label->num, label) != 1) {
1370 if (cfg->default_label &&
1371 (strcmp(label->name, cfg->default_label) == 0))
1372 default_num = label->num;
1376 * After we've created items for each label in the menu, set the
1377 * menu's default label if one was specified.
1380 err = menu_default_set(m, default_num);
1382 if (err != -ENOENT) {
1387 printf("Missing default: %s\n", cfg->default_label);
1395 * Try to boot any labels we have yet to attempt to boot.
1397 static void boot_unattempted_labels(struct cmd_tbl *cmdtp, struct pxe_menu *cfg)
1399 struct list_head *pos;
1400 struct pxe_label *label;
1402 list_for_each(pos, &cfg->labels) {
1403 label = list_entry(pos, struct pxe_label, list);
1405 if (!label->attempted)
1406 label_boot(cmdtp, label);
1411 * Boot the system as prescribed by a pxe_menu.
1413 * Use the menu system to either get the user's choice or the default, based
1414 * on config or user input. If there is no default or user's choice,
1415 * attempted to boot labels in the order they were given in pxe files.
1416 * If the default or user's choice fails to boot, attempt to boot other
1417 * labels in the order they were given in pxe files.
1419 * If this function returns, there weren't any labels that successfully
1420 * booted, or the user interrupted the menu selection via ctrl+c.
1422 void handle_pxe_menu(struct cmd_tbl *cmdtp, struct pxe_menu *cfg)
1428 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1429 /* display BMP if available */
1431 if (get_relfile(cmdtp, cfg->bmp, image_load_addr)) {
1432 if (CONFIG_IS_ENABLED(CMD_CLS))
1433 run_command("cls", 0);
1434 bmp_display(image_load_addr,
1435 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1437 printf("Skipping background bmp %s for failure\n",
1443 m = pxe_menu_to_menu(cfg);
1447 err = menu_get_choice(m, &choice);
1452 * err == 1 means we got a choice back from menu_get_choice.
1454 * err == -ENOENT if the menu was setup to select the default but no
1455 * default was set. in that case, we should continue trying to boot
1456 * labels that haven't been attempted yet.
1458 * otherwise, the user interrupted or there was some other error and
1463 err = label_boot(cmdtp, choice);
1466 } else if (err != -ENOENT) {
1470 boot_unattempted_labels(cmdtp, cfg);