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 = hextoul(env_get("fdt_addr_r"), NULL);
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 = hextoul(fdtoverlay_addr_env, NULL);
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");
600 len = strlen(label->fdtdir);
603 else if (label->fdtdir[len - 1] != '/')
608 len = strlen(label->fdtdir) + strlen(slash) +
609 strlen(f1) + strlen(f2) + strlen(f3) +
611 fdtfilefree = malloc(len);
613 printf("malloc fail (FDT filename)\n");
617 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
618 label->fdtdir, slash, f1, f2, f3, f4);
619 fdtfile = fdtfilefree;
623 int err = get_relfile_envaddr(cmdtp, fdtfile,
628 bootm_argv[3] = NULL;
631 printf("Skipping %s for failure retrieving FDT\n",
637 #ifdef CONFIG_OF_LIBFDT_OVERLAY
638 if (label->fdtoverlays)
639 label_boot_fdtoverlay(cmdtp, label);
642 bootm_argv[3] = NULL;
647 bootm_argv[3] = env_get("fdt_addr");
655 kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
656 buf = map_sysmem(kernel_addr, 0);
657 /* Try bootm for legacy and FIT format image */
658 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
659 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
660 /* Try booting an AArch64 Linux kernel image */
661 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
662 do_booti(cmdtp, 0, bootm_argc, bootm_argv);
663 /* Try booting a Image */
664 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
665 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
666 /* Try booting an x86_64 Linux kernel image */
667 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
668 do_zboot_parent(cmdtp, 0, bootm_argc, bootm_argv, NULL);
679 * Tokens for the pxe file parser.
707 * A token - given by a value and a type.
711 enum token_type type;
715 * Keywords recognized.
717 static const struct token keywords[] = {
720 {"timeout", T_TIMEOUT},
721 {"default", T_DEFAULT},
722 {"prompt", T_PROMPT},
724 {"kernel", T_KERNEL},
726 {"localboot", T_LOCALBOOT},
727 {"append", T_APPEND},
728 {"initrd", T_INITRD},
729 {"include", T_INCLUDE},
730 {"devicetree", T_FDT},
732 {"devicetreedir", T_FDTDIR},
733 {"fdtdir", T_FDTDIR},
734 {"fdtoverlays", T_FDTOVERLAYS},
735 {"ontimeout", T_ONTIMEOUT,},
736 {"ipappend", T_IPAPPEND,},
737 {"background", T_BACKGROUND,},
742 * Since pxe(linux) files don't have a token to identify the start of a
743 * literal, we have to keep track of when we're in a state where a literal is
744 * expected vs when we're in a state a keyword is expected.
753 * get_string retrieves a string from *p and stores it as a token in
756 * get_string used for scanning both string literals and keywords.
758 * Characters from *p are copied into t-val until a character equal to
759 * delim is found, or a NUL byte is reached. If delim has the special value of
760 * ' ', any whitespace character will be used as a delimiter.
762 * If lower is unequal to 0, uppercase characters will be converted to
763 * lowercase in the result. This is useful to make keywords case
766 * The location of *p is updated to point to the first character after the end
767 * of the token - the ending delimiter.
769 * On success, the new value of t->val is returned. Memory for t->val is
770 * allocated using malloc and must be free()'d to reclaim it. If insufficient
771 * memory is available, NULL is returned.
773 static char *get_string(char **p, struct token *t, char delim, int lower)
779 * b and e both start at the beginning of the input stream.
781 * e is incremented until we find the ending delimiter, or a NUL byte
782 * is reached. Then, we take e - b to find the length of the token.
788 if ((delim == ' ' && isspace(*e)) || delim == *e)
796 * Allocate memory to hold the string, and copy it in, converting
797 * characters to lowercase if lower is != 0.
799 t->val = malloc(len + 1);
803 for (i = 0; i < len; i++, b++) {
805 t->val[i] = tolower(*b);
813 * Update *p so the caller knows where to continue scanning.
823 * Populate a keyword token with a type and value.
825 static void get_keyword(struct token *t)
829 for (i = 0; keywords[i].val; i++) {
830 if (!strcmp(t->val, keywords[i].val)) {
831 t->type = keywords[i].type;
838 * Get the next token. We have to keep track of which state we're in to know
839 * if we're looking to get a string literal or a keyword.
841 * *p is updated to point at the first character after the current token.
843 static void get_token(char **p, struct token *t, enum lex_state state)
849 /* eat non EOL whitespace */
854 * eat comments. note that string literals can't begin with #, but
855 * can contain a # after their first character.
858 while (*c && *c != '\n')
865 } else if (*c == '\0') {
868 } else if (state == L_SLITERAL) {
869 get_string(&c, t, '\n', 0);
870 } else if (state == L_KEYWORD) {
872 * when we expect a keyword, we first get the next string
873 * token delimited by whitespace, and then check if it
874 * matches a keyword in our keyword list. if it does, it's
875 * converted to a keyword token of the appropriate type, and
876 * if not, it remains a string token.
878 get_string(&c, t, ' ', 1);
886 * Increment *c until we get to the end of the current line, or EOF.
888 static void eol_or_eof(char **c)
890 while (**c && **c != '\n')
895 * All of these parse_* functions share some common behavior.
897 * They finish with *c pointing after the token they parse, and return 1 on
898 * success, or < 0 on error.
902 * Parse a string literal and store a pointer it at *dst. String literals
903 * terminate at the end of the line.
905 static int parse_sliteral(char **c, char **dst)
910 get_token(c, &t, L_SLITERAL);
912 if (t.type != T_STRING) {
913 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
923 * Parse a base 10 (unsigned) integer and store it at *dst.
925 static int parse_integer(char **c, int *dst)
930 get_token(c, &t, L_SLITERAL);
932 if (t.type != T_STRING) {
933 printf("Expected string: %.*s\n", (int)(*c - s), s);
937 *dst = simple_strtol(t.val, NULL, 10);
944 static int parse_pxefile_top(struct cmd_tbl *cmdtp, char *p, unsigned long base,
945 struct pxe_menu *cfg, int nest_level);
948 * Parse an include statement, and retrieve and parse the file it mentions.
950 * base should point to a location where it's safe to store the file, and
951 * nest_level should indicate how many nested includes have occurred. For this
952 * include, nest_level has already been incremented and doesn't need to be
955 static int handle_include(struct cmd_tbl *cmdtp, char **c, unsigned long base,
956 struct pxe_menu *cfg, int nest_level)
964 err = parse_sliteral(c, &include_path);
967 printf("Expected include path: %.*s\n", (int)(*c - s), s);
971 err = get_pxe_file(cmdtp, include_path, base);
974 printf("Couldn't retrieve %s\n", include_path);
978 buf = map_sysmem(base, 0);
979 ret = parse_pxefile_top(cmdtp, buf, base, cfg, nest_level);
986 * Parse lines that begin with 'menu'.
988 * base and nest are provided to handle the 'menu include' case.
990 * base should point to a location where it's safe to store the included file.
992 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
993 * a file it includes, 3 when parsing a file included by that file, and so on.
995 static int parse_menu(struct cmd_tbl *cmdtp, char **c, struct pxe_menu *cfg,
996 unsigned long base, int nest_level)
1002 get_token(c, &t, L_KEYWORD);
1006 err = parse_sliteral(c, &cfg->title);
1011 err = handle_include(cmdtp, c, base, cfg, nest_level + 1);
1015 err = parse_sliteral(c, &cfg->bmp);
1019 printf("Ignoring malformed menu command: %.*s\n",
1032 * Handles parsing a 'menu line' when we're parsing a label.
1034 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1035 struct pxe_label *label)
1042 get_token(c, &t, L_KEYWORD);
1046 if (!cfg->default_label)
1047 cfg->default_label = strdup(label->name);
1049 if (!cfg->default_label)
1054 parse_sliteral(c, &label->menu);
1057 printf("Ignoring malformed menu command: %.*s\n",
1067 * Handles parsing a 'kernel' label.
1068 * expecting "filename" or "<fit_filename>#cfg"
1070 static int parse_label_kernel(char **c, struct pxe_label *label)
1075 err = parse_sliteral(c, &label->kernel);
1079 s = strstr(label->kernel, "#");
1083 label->config = malloc(strlen(s) + 1);
1087 strcpy(label->config, s);
1094 * Parses a label and adds it to the list of labels for a menu.
1096 * A label ends when we either get to the end of a file, or
1097 * get some input we otherwise don't have a handler defined
1101 static int parse_label(char **c, struct pxe_menu *cfg)
1106 struct pxe_label *label;
1109 label = label_create();
1113 err = parse_sliteral(c, &label->name);
1115 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1116 label_destroy(label);
1120 list_add_tail(&label->list, &cfg->labels);
1124 get_token(c, &t, L_KEYWORD);
1129 err = parse_label_menu(c, cfg, label);
1134 err = parse_label_kernel(c, label);
1138 err = parse_sliteral(c, &label->append);
1141 s = strstr(label->append, "initrd=");
1145 len = (int)(strchr(s, ' ') - s);
1146 label->initrd = malloc(len + 1);
1147 strncpy(label->initrd, s, len);
1148 label->initrd[len] = '\0';
1154 err = parse_sliteral(c, &label->initrd);
1159 err = parse_sliteral(c, &label->fdt);
1164 err = parse_sliteral(c, &label->fdtdir);
1168 if (!label->fdtoverlays)
1169 err = parse_sliteral(c, &label->fdtoverlays);
1173 label->localboot = 1;
1174 err = parse_integer(c, &label->localboot_val);
1178 err = parse_integer(c, &label->ipappend);
1186 * put the token back! we don't want it - it's the end
1187 * of a label and whatever token this is, it's
1188 * something for the menu level context to handle.
1200 * This 16 comes from the limit pxelinux imposes on nested includes.
1202 * There is no reason at all we couldn't do more, but some limit helps prevent
1203 * infinite (until crash occurs) recursion if a file tries to include itself.
1205 #define MAX_NEST_LEVEL 16
1208 * Entry point for parsing a menu file. nest_level indicates how many times
1209 * we've nested in includes. It will be 1 for the top level menu file.
1211 * Returns 1 on success, < 0 on error.
1213 static int parse_pxefile_top(struct cmd_tbl *cmdtp, char *p, unsigned long base,
1214 struct pxe_menu *cfg, int nest_level)
1217 char *s, *b, *label_name;
1222 if (nest_level > MAX_NEST_LEVEL) {
1223 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1230 get_token(&p, &t, L_KEYWORD);
1236 err = parse_menu(cmdtp, &p, cfg,
1237 base + ALIGN(strlen(b) + 1, 4),
1242 err = parse_integer(&p, &cfg->timeout);
1246 err = parse_label(&p, cfg);
1251 err = parse_sliteral(&p, &label_name);
1254 if (cfg->default_label)
1255 free(cfg->default_label);
1257 cfg->default_label = label_name;
1263 err = handle_include(cmdtp, &p,
1264 base + ALIGN(strlen(b), 4), cfg,
1279 printf("Ignoring unknown command: %.*s\n",
1290 * Free the memory used by a pxe_menu and its labels.
1292 void destroy_pxe_menu(struct pxe_menu *cfg)
1294 struct list_head *pos, *n;
1295 struct pxe_label *label;
1300 if (cfg->default_label)
1301 free(cfg->default_label);
1303 list_for_each_safe(pos, n, &cfg->labels) {
1304 label = list_entry(pos, struct pxe_label, list);
1306 label_destroy(label);
1313 * Entry point for parsing a pxe file. This is only used for the top level
1316 * Returns NULL if there is an error, otherwise, returns a pointer to a
1317 * pxe_menu struct populated with the results of parsing the pxe file (and any
1318 * files it includes). The resulting pxe_menu struct can be free()'d by using
1319 * the destroy_pxe_menu() function.
1321 struct pxe_menu *parse_pxefile(struct cmd_tbl *cmdtp, unsigned long menucfg)
1323 struct pxe_menu *cfg;
1327 cfg = malloc(sizeof(struct pxe_menu));
1332 memset(cfg, 0, sizeof(struct pxe_menu));
1334 INIT_LIST_HEAD(&cfg->labels);
1336 buf = map_sysmem(menucfg, 0);
1337 r = parse_pxefile_top(cmdtp, buf, menucfg, cfg, 1);
1341 destroy_pxe_menu(cfg);
1349 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1352 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1354 struct pxe_label *label;
1355 struct list_head *pos;
1359 char *default_num = NULL;
1362 * Create a menu and add items for all the labels.
1364 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
1365 cfg->prompt, NULL, label_print, NULL, NULL);
1370 list_for_each(pos, &cfg->labels) {
1371 label = list_entry(pos, struct pxe_label, list);
1373 sprintf(label->num, "%d", i++);
1374 if (menu_item_add(m, label->num, label) != 1) {
1378 if (cfg->default_label &&
1379 (strcmp(label->name, cfg->default_label) == 0))
1380 default_num = label->num;
1384 * After we've created items for each label in the menu, set the
1385 * menu's default label if one was specified.
1388 err = menu_default_set(m, default_num);
1390 if (err != -ENOENT) {
1395 printf("Missing default: %s\n", cfg->default_label);
1403 * Try to boot any labels we have yet to attempt to boot.
1405 static void boot_unattempted_labels(struct cmd_tbl *cmdtp, struct pxe_menu *cfg)
1407 struct list_head *pos;
1408 struct pxe_label *label;
1410 list_for_each(pos, &cfg->labels) {
1411 label = list_entry(pos, struct pxe_label, list);
1413 if (!label->attempted)
1414 label_boot(cmdtp, label);
1419 * Boot the system as prescribed by a pxe_menu.
1421 * Use the menu system to either get the user's choice or the default, based
1422 * on config or user input. If there is no default or user's choice,
1423 * attempted to boot labels in the order they were given in pxe files.
1424 * If the default or user's choice fails to boot, attempt to boot other
1425 * labels in the order they were given in pxe files.
1427 * If this function returns, there weren't any labels that successfully
1428 * booted, or the user interrupted the menu selection via ctrl+c.
1430 void handle_pxe_menu(struct cmd_tbl *cmdtp, struct pxe_menu *cfg)
1436 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1437 /* display BMP if available */
1439 if (get_relfile(cmdtp, cfg->bmp, image_load_addr)) {
1440 if (CONFIG_IS_ENABLED(CMD_CLS))
1441 run_command("cls", 0);
1442 bmp_display(image_load_addr,
1443 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1445 printf("Skipping background bmp %s for failure\n",
1451 m = pxe_menu_to_menu(cfg);
1455 err = menu_get_choice(m, &choice);
1460 * err == 1 means we got a choice back from menu_get_choice.
1462 * err == -ENOENT if the menu was setup to select the default but no
1463 * default was set. in that case, we should continue trying to boot
1464 * labels that haven't been attempted yet.
1466 * otherwise, the user interrupted or there was some other error and
1471 err = label_boot(cmdtp, choice);
1474 } else if (err != -ENOENT) {
1478 boot_unattempted_labels(cmdtp, cfg);