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
33 int format_mac_pxe(char *outbuf, size_t outbuf_len)
37 if (outbuf_len < 21) {
38 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
43 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
46 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
47 ethaddr[0], ethaddr[1], ethaddr[2],
48 ethaddr[3], ethaddr[4], ethaddr[5]);
54 * Returns the directory the file specified in the bootfile env variable is
55 * in. If bootfile isn't defined in the environment, return NULL, which should
56 * be interpreted as "don't prepend anything to paths".
58 static int get_bootfile_path(const char *file_path, char *bootfile_path,
59 size_t bootfile_path_size, bool allow_abs_path)
61 char *bootfile, *last_slash;
64 /* Only syslinux allows absolute paths */
65 if (file_path[0] == '/' && allow_abs_path)
68 bootfile = from_env("bootfile");
73 last_slash = strrchr(bootfile, '/');
78 path_len = (last_slash - bootfile) + 1;
80 if (bootfile_path_size < path_len) {
81 printf("bootfile_path too small. (%zd < %zd)\n",
82 bootfile_path_size, path_len);
87 strncpy(bootfile_path, bootfile, path_len);
90 bootfile_path[path_len] = '\0';
96 * As in pxelinux, paths to files referenced from files we retrieve are
97 * relative to the location of bootfile. get_relfile takes such a path and
98 * joins it with the bootfile path to get the full path to the target file. If
99 * the bootfile path is NULL, we use file_path as is.
101 * Returns 1 for success, or < 0 on error.
103 static int get_relfile(struct pxe_context *ctx, const char *file_path,
104 unsigned long file_addr)
107 char relfile[MAX_TFTP_PATH_LEN + 1];
111 err = get_bootfile_path(file_path, relfile, sizeof(relfile),
112 ctx->allow_abs_path);
117 path_len = strlen(file_path);
118 path_len += strlen(relfile);
120 if (path_len > MAX_TFTP_PATH_LEN) {
121 printf("Base path too long (%s%s)\n", relfile, file_path);
123 return -ENAMETOOLONG;
126 strcat(relfile, file_path);
128 printf("Retrieving file: %s\n", relfile);
130 sprintf(addr_buf, "%lx", file_addr);
132 return ctx->getfile(ctx, relfile, addr_buf);
135 int get_pxe_file(struct pxe_context *ctx, const char *file_path,
136 unsigned long file_addr)
138 unsigned long config_file_size;
143 err = get_relfile(ctx, file_path, file_addr);
149 * the file comes without a NUL byte at the end, so find out its size
150 * and add the NUL byte.
152 tftp_filesize = from_env("filesize");
157 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
160 buf = map_sysmem(file_addr + config_file_size, 1);
167 #define PXELINUX_DIR "pxelinux.cfg/"
169 int get_pxelinux_path(struct pxe_context *ctx, const char *file,
170 unsigned long pxefile_addr_r)
172 size_t base_len = strlen(PXELINUX_DIR);
173 char path[MAX_TFTP_PATH_LEN + 1];
175 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
176 printf("path (%s%s) too long, skipping\n",
178 return -ENAMETOOLONG;
181 sprintf(path, PXELINUX_DIR "%s", file);
183 return get_pxe_file(ctx, path, pxefile_addr_r);
187 * Wrapper to make it easier to store the file at file_path in the location
188 * specified by envaddr_name. file_path will be joined to the bootfile path,
189 * if any is specified.
191 * Returns 1 on success or < 0 on error.
193 static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
194 const char *envaddr_name)
196 unsigned long file_addr;
199 envaddr = from_env(envaddr_name);
204 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
207 return get_relfile(ctx, file_path, file_addr);
211 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
212 * result must be free()'d to reclaim the memory.
214 * Returns NULL if malloc fails.
216 static struct pxe_label *label_create(void)
218 struct pxe_label *label;
220 label = malloc(sizeof(struct pxe_label));
225 memset(label, 0, sizeof(struct pxe_label));
231 * Free the memory used by a pxe_label, including that used by its name,
232 * kernel, append and initrd members, if they're non NULL.
234 * So - be sure to only use dynamically allocated memory for the members of
235 * the pxe_label struct, unless you want to clean it up first. These are
236 * currently only created by the pxe file parsing code.
238 static void label_destroy(struct pxe_label *label)
261 if (label->fdtoverlays)
262 free(label->fdtoverlays);
268 * Print a label and its string members if they're defined.
270 * This is passed as a callback to the menu code for displaying each
273 static void label_print(void *data)
275 struct pxe_label *label = data;
276 const char *c = label->menu ? label->menu : label->name;
278 printf("%s:\t%s\n", label->num, c);
282 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
283 * environment variable is defined. Its contents will be executed as U-Boot
284 * command. If the label specified an 'append' line, its contents will be
285 * used to overwrite the contents of the 'bootargs' environment variable prior
286 * to running 'localcmd'.
288 * Returns 1 on success or < 0 on error.
290 static int label_localboot(struct pxe_label *label)
294 localcmd = from_env("localcmd");
300 char bootargs[CONFIG_SYS_CBSIZE];
302 cli_simple_process_macros(label->append, bootargs,
304 env_set("bootargs", bootargs);
307 debug("running: %s\n", localcmd);
309 return run_command_list(localcmd, strlen(localcmd), 0);
313 * Loads fdt overlays specified in 'fdtoverlays'.
315 #ifdef CONFIG_OF_LIBFDT_OVERLAY
316 static void label_boot_fdtoverlay(struct pxe_context *ctx,
317 struct pxe_label *label)
319 char *fdtoverlay = label->fdtoverlays;
320 struct fdt_header *working_fdt;
321 char *fdtoverlay_addr_env;
322 ulong fdtoverlay_addr;
326 /* Get the main fdt and map it */
327 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
328 working_fdt = map_sysmem(fdt_addr, 0);
329 err = fdt_check_header(working_fdt);
333 /* Get the specific overlay loading address */
334 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
335 if (!fdtoverlay_addr_env) {
336 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
340 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
342 /* Cycle over the overlay files and apply them in order */
344 struct fdt_header *blob;
349 /* Drop leading spaces */
350 while (*fdtoverlay == ' ')
353 /* Copy a single filename if multiple provided */
354 end = strstr(fdtoverlay, " ");
356 len = (int)(end - fdtoverlay);
357 overlayfile = malloc(len + 1);
358 strncpy(overlayfile, fdtoverlay, len);
359 overlayfile[len] = '\0';
361 overlayfile = fdtoverlay;
363 if (!strlen(overlayfile))
366 /* Load overlay file */
367 err = get_relfile_envaddr(ctx, overlayfile,
368 "fdtoverlay_addr_r");
370 printf("Failed loading overlay %s\n", overlayfile);
374 /* Resize main fdt */
375 fdt_shrink_to_minimum(working_fdt, 8192);
377 blob = map_sysmem(fdtoverlay_addr, 0);
378 err = fdt_check_header(blob);
380 printf("Invalid overlay %s, skipping\n",
385 err = fdt_overlay_apply_verbose(working_fdt, blob);
387 printf("Failed to apply overlay %s, skipping\n",
395 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
400 * Boot according to the contents of a pxe_label.
402 * If we can't boot for any reason, we return. A successful boot never
405 * The kernel will be stored in the location given by the 'kernel_addr_r'
406 * environment variable.
408 * If the label specifies an initrd file, it will be stored in the location
409 * given by the 'ramdisk_addr_r' environment variable.
411 * If the label specifies an 'append' line, its contents will overwrite that
412 * of the 'bootargs' environment variable.
414 static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
416 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
417 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
418 char *kernel_addr = NULL;
419 char *initrd_addr_str = NULL;
420 char initrd_filesize[10];
422 char mac_str[29] = "";
423 char ip_str[68] = "";
424 char *fit_addr = NULL;
433 label->attempted = 1;
435 if (label->localboot) {
436 if (label->localboot_val >= 0)
437 label_localboot(label);
441 if (!label->kernel) {
442 printf("No kernel given, skipping %s\n",
448 if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r") < 0) {
449 printf("Skipping %s for failure retrieving initrd\n",
454 initrd_addr_str = env_get("ramdisk_addr_r");
455 strncpy(initrd_filesize, env_get("filesize"), 9);
457 strncpy(initrd_str, initrd_addr_str, 18);
458 strcat(initrd_str, ":");
459 strncat(initrd_str, initrd_filesize, 9);
462 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r") < 0) {
463 printf("Skipping %s for failure retrieving kernel\n",
468 if (label->ipappend & 0x1) {
469 sprintf(ip_str, " ip=%s:%s:%s:%s",
470 env_get("ipaddr"), env_get("serverip"),
471 env_get("gatewayip"), env_get("netmask"));
474 if (IS_ENABLED(CONFIG_CMD_NET)) {
475 if (label->ipappend & 0x2) {
478 strcpy(mac_str, " BOOTIF=");
479 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
485 if ((label->ipappend & 0x3) || label->append) {
486 char bootargs[CONFIG_SYS_CBSIZE] = "";
487 char finalbootargs[CONFIG_SYS_CBSIZE];
489 if (strlen(label->append ?: "") +
490 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
491 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
492 strlen(label->append ?: ""),
493 strlen(ip_str), strlen(mac_str),
499 strncpy(bootargs, label->append, sizeof(bootargs));
501 strcat(bootargs, ip_str);
502 strcat(bootargs, mac_str);
504 cli_simple_process_macros(bootargs, finalbootargs,
505 sizeof(finalbootargs));
506 env_set("bootargs", finalbootargs);
507 printf("append: %s\n", finalbootargs);
510 kernel_addr = env_get("kernel_addr_r");
512 /* for FIT, append the configuration identifier */
514 int len = strlen(kernel_addr) + strlen(label->config) + 1;
516 fit_addr = malloc(len);
518 printf("malloc fail (FIT address)\n");
521 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
522 kernel_addr = fit_addr;
526 * fdt usage is optional:
527 * It handles the following scenarios.
529 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
530 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
531 * bootm, and adjust argc appropriately.
533 * If retrieve fails and no exact fdt blob is specified in pxe file with
534 * "fdt" label, try Scenario 2.
536 * Scenario 2: If there is an fdt_addr specified, pass it along to
537 * bootm, and adjust argc appropriately.
539 * Scenario 3: fdt blob is not available.
541 bootm_argv[3] = env_get("fdt_addr_r");
543 /* if fdt label is defined then get fdt from server */
545 char *fdtfile = NULL;
546 char *fdtfilefree = NULL;
549 fdtfile = label->fdt;
550 } else if (label->fdtdir) {
551 char *f1, *f2, *f3, *f4, *slash;
553 f1 = env_get("fdtfile");
560 * For complex cases where this code doesn't
561 * generate the correct filename, the board
562 * code should set $fdtfile during early boot,
563 * or the boot scripts should set $fdtfile
564 * before invoking "pxe" or "sysboot".
568 f3 = env_get("board");
580 len = strlen(label->fdtdir);
583 else if (label->fdtdir[len - 1] != '/')
588 len = strlen(label->fdtdir) + strlen(slash) +
589 strlen(f1) + strlen(f2) + strlen(f3) +
591 fdtfilefree = malloc(len);
593 printf("malloc fail (FDT filename)\n");
597 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
598 label->fdtdir, slash, f1, f2, f3, f4);
599 fdtfile = fdtfilefree;
603 int err = get_relfile_envaddr(ctx, fdtfile,
608 bootm_argv[3] = NULL;
611 printf("Skipping %s for failure retrieving FDT\n",
617 #ifdef CONFIG_OF_LIBFDT_OVERLAY
618 if (label->fdtoverlays)
619 label_boot_fdtoverlay(ctx, label);
622 bootm_argv[3] = NULL;
626 bootm_argv[1] = kernel_addr;
627 zboot_argv[1] = kernel_addr;
629 if (initrd_addr_str) {
630 bootm_argv[2] = initrd_str;
633 zboot_argv[3] = initrd_addr_str;
634 zboot_argv[4] = initrd_filesize;
639 bootm_argv[3] = env_get("fdt_addr");
647 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
648 buf = map_sysmem(kernel_addr_r, 0);
649 /* Try bootm for legacy and FIT format image */
650 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
651 do_bootm(ctx->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(ctx->cmdtp, 0, bootm_argc, bootm_argv);
655 /* Try booting a Image */
656 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
657 do_bootz(ctx->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(ctx->cmdtp, 0, zboot_argc, zboot_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 pxe_context *ctx, char *p, ulong 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 pxe_context *ctx, 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(ctx, include_path, base);
966 printf("Couldn't retrieve %s\n", include_path);
970 buf = map_sysmem(base, 0);
971 ret = parse_pxefile_top(ctx, 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 pxe_context *ctx, 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(ctx, 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 pxe_context *ctx, 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(ctx, &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(ctx, &p,
1256 base + ALIGN(strlen(b), 4), cfg,
1271 printf("Ignoring unknown command: %.*s\n",
1283 void destroy_pxe_menu(struct pxe_menu *cfg)
1285 struct list_head *pos, *n;
1286 struct pxe_label *label;
1291 if (cfg->default_label)
1292 free(cfg->default_label);
1294 list_for_each_safe(pos, n, &cfg->labels) {
1295 label = list_entry(pos, struct pxe_label, list);
1297 label_destroy(label);
1303 struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
1305 struct pxe_menu *cfg;
1309 cfg = malloc(sizeof(struct pxe_menu));
1314 memset(cfg, 0, sizeof(struct pxe_menu));
1316 INIT_LIST_HEAD(&cfg->labels);
1318 buf = map_sysmem(menucfg, 0);
1319 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
1323 destroy_pxe_menu(cfg);
1331 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1334 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1336 struct pxe_label *label;
1337 struct list_head *pos;
1341 char *default_num = NULL;
1344 * Create a menu and add items for all the labels.
1346 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
1347 cfg->prompt, NULL, label_print, NULL, NULL);
1352 list_for_each(pos, &cfg->labels) {
1353 label = list_entry(pos, struct pxe_label, list);
1355 sprintf(label->num, "%d", i++);
1356 if (menu_item_add(m, label->num, label) != 1) {
1360 if (cfg->default_label &&
1361 (strcmp(label->name, cfg->default_label) == 0))
1362 default_num = label->num;
1366 * After we've created items for each label in the menu, set the
1367 * menu's default label if one was specified.
1370 err = menu_default_set(m, default_num);
1372 if (err != -ENOENT) {
1377 printf("Missing default: %s\n", cfg->default_label);
1385 * Try to boot any labels we have yet to attempt to boot.
1387 static void boot_unattempted_labels(struct pxe_context *ctx,
1388 struct pxe_menu *cfg)
1390 struct list_head *pos;
1391 struct pxe_label *label;
1393 list_for_each(pos, &cfg->labels) {
1394 label = list_entry(pos, struct pxe_label, list);
1396 if (!label->attempted)
1397 label_boot(ctx, label);
1401 void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
1407 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1408 /* display BMP if available */
1410 if (get_relfile(ctx, cfg->bmp, image_load_addr)) {
1411 if (CONFIG_IS_ENABLED(CMD_CLS))
1412 run_command("cls", 0);
1413 bmp_display(image_load_addr,
1414 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1416 printf("Skipping background bmp %s for failure\n",
1422 m = pxe_menu_to_menu(cfg);
1426 err = menu_get_choice(m, &choice);
1431 * err == 1 means we got a choice back from menu_get_choice.
1433 * err == -ENOENT if the menu was setup to select the default but no
1434 * default was set. in that case, we should continue trying to boot
1435 * labels that haven't been attempted yet.
1437 * otherwise, the user interrupted or there was some other error and
1442 err = label_boot(ctx, choice);
1445 } else if (err != -ENOENT) {
1449 boot_unattempted_labels(ctx, cfg);
1452 void pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1453 pxe_getfile_func getfile, void *userdata,
1454 bool allow_abs_path)
1457 ctx->getfile = getfile;
1458 ctx->userdata = userdata;
1459 ctx->allow_abs_path = allow_abs_path;