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 pxe_get_file_size(ulong *sizep)
37 val = from_env("filesize");
41 if (strict_strtoul(val, 16, sizep) < 0)
48 * format_mac_pxe() - obtain a MAC address in the PXE format
50 * This produces a MAC-address string in the format for the current ethernet
53 * 01-aa-bb-cc-dd-ee-ff
55 * where aa-ff is the MAC address in hex
57 * @outbuf: Buffer to write string to
58 * @outbuf_len: length of buffer
59 * @return 1 if OK, -ENOSPC if buffer is too small, -ENOENT is there is no
60 * current ethernet device
62 int format_mac_pxe(char *outbuf, size_t outbuf_len)
66 if (outbuf_len < 21) {
67 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
71 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
74 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
75 ethaddr[0], ethaddr[1], ethaddr[2],
76 ethaddr[3], ethaddr[4], ethaddr[5]);
82 * get_relfile() - read a file relative to the PXE file
84 * As in pxelinux, paths to files referenced from files we retrieve are
85 * relative to the location of bootfile. get_relfile takes such a path and
86 * joins it with the bootfile path to get the full path to the target file. If
87 * the bootfile path is NULL, we use file_path as is.
90 * @file_path: File path to read (relative to the PXE file)
91 * @file_addr: Address to load file to
92 * @filesizep: If not NULL, returns the file size in bytes
93 * Returns 1 for success, or < 0 on error
95 static int get_relfile(struct pxe_context *ctx, const char *file_path,
96 unsigned long file_addr, ulong *filesizep)
99 char relfile[MAX_TFTP_PATH_LEN + 1];
104 if (file_path[0] == '/' && ctx->allow_abs_path)
107 strncpy(relfile, ctx->bootdir, MAX_TFTP_PATH_LEN);
109 path_len = strlen(file_path) + strlen(relfile);
111 if (path_len > MAX_TFTP_PATH_LEN) {
112 printf("Base path too long (%s%s)\n", relfile, file_path);
114 return -ENAMETOOLONG;
117 strcat(relfile, file_path);
119 printf("Retrieving file: %s\n", relfile);
121 sprintf(addr_buf, "%lx", file_addr);
123 ret = ctx->getfile(ctx, relfile, addr_buf, &size);
125 return log_msg_ret("get", ret);
133 * get_pxe_file() - read a file
135 * The file is read and nul-terminated
138 * @file_path: File path to read (relative to the PXE file)
139 * @file_addr: Address to load file to
140 * Returns 1 for success, or < 0 on error
142 int get_pxe_file(struct pxe_context *ctx, const char *file_path,
149 err = get_relfile(ctx, file_path, file_addr, &size);
153 buf = map_sysmem(file_addr + size, 1);
160 #define PXELINUX_DIR "pxelinux.cfg/"
163 * get_pxelinux_path() - Get a file in the pxelinux.cfg/ directory
166 * @file: Filename to process (relative to pxelinux.cfg/)
167 * Returns 1 for success, -ENAMETOOLONG if the resulting path is too long.
168 * or other value < 0 on other error
170 int get_pxelinux_path(struct pxe_context *ctx, const char *file,
171 unsigned long pxefile_addr_r)
173 size_t base_len = strlen(PXELINUX_DIR);
174 char path[MAX_TFTP_PATH_LEN + 1];
176 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
177 printf("path (%s%s) too long, skipping\n",
179 return -ENAMETOOLONG;
182 sprintf(path, PXELINUX_DIR "%s", file);
184 return get_pxe_file(ctx, path, pxefile_addr_r);
188 * get_relfile_envaddr() - read a file to an address in an env var
190 * Wrapper to make it easier to store the file at file_path in the location
191 * specified by envaddr_name. file_path will be joined to the bootfile path,
192 * if any is specified.
195 * @file_path: File path to read (relative to the PXE file)
196 * @envaddr_name: Name of environment variable which contains the address to
198 * @filesizep: Returns the file size in bytes
199 * Returns 1 on success, -ENOENT if @envaddr_name does not exist as an
200 * environment variable, -EINVAL if its format is not valid hex, or other
201 * value < 0 on other error
203 static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
204 const char *envaddr_name, ulong *filesizep)
206 unsigned long file_addr;
209 envaddr = from_env(envaddr_name);
213 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
216 return get_relfile(ctx, file_path, file_addr, filesizep);
220 * label_create() - crate a new PXE label
222 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
223 * result must be free()'d to reclaim the memory.
225 * Returns a pointer to the label, or NULL if out of memory
227 static struct pxe_label *label_create(void)
229 struct pxe_label *label;
231 label = malloc(sizeof(struct pxe_label));
235 memset(label, 0, sizeof(struct pxe_label));
241 * label_destroy() - free the memory used by a pxe_label
243 * This frees @label itself as well as memory used by its name,
244 * kernel, config, append, initrd, fdt, fdtdir and fdtoverlay members, if
247 * So - be sure to only use dynamically allocated memory for the members of
248 * the pxe_label struct, unless you want to clean it up first. These are
249 * currently only created by the pxe file parsing code.
251 * @label: Label to free
253 static void label_destroy(struct pxe_label *label)
262 free(label->fdtoverlays);
267 * label_print() - Print a label and its string members if they're defined
269 * This is passed as a callback to the menu code for displaying each
272 * @data: Label to print (is cast to struct pxe_label *)
274 static void label_print(void *data)
276 struct pxe_label *label = data;
277 const char *c = label->menu ? label->menu : label->name;
279 printf("%s:\t%s\n", label->num, c);
283 * label_localboot() - Boot a label that specified 'localboot'
285 * This requires that the 'localcmd' environment variable is defined. Its
286 * contents will be executed as U-Boot commands. If the label specified an
287 * 'append' line, its contents will be used to overwrite the contents of the
288 * 'bootargs' environment variable prior to running 'localcmd'.
290 * @label: Label to process
291 * Returns 1 on success or < 0 on error
293 static int label_localboot(struct pxe_label *label)
297 localcmd = from_env("localcmd");
302 char bootargs[CONFIG_SYS_CBSIZE];
304 cli_simple_process_macros(label->append, bootargs,
306 env_set("bootargs", bootargs);
309 debug("running: %s\n", localcmd);
311 return run_command_list(localcmd, strlen(localcmd), 0);
315 * label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays'
318 * @label: Label to process
320 #ifdef CONFIG_OF_LIBFDT_OVERLAY
321 static void label_boot_fdtoverlay(struct pxe_context *ctx,
322 struct pxe_label *label)
324 char *fdtoverlay = label->fdtoverlays;
325 struct fdt_header *working_fdt;
326 char *fdtoverlay_addr_env;
327 ulong fdtoverlay_addr;
331 /* Get the main fdt and map it */
332 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
333 working_fdt = map_sysmem(fdt_addr, 0);
334 err = fdt_check_header(working_fdt);
338 /* Get the specific overlay loading address */
339 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
340 if (!fdtoverlay_addr_env) {
341 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
345 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
347 /* Cycle over the overlay files and apply them in order */
349 struct fdt_header *blob;
354 /* Drop leading spaces */
355 while (*fdtoverlay == ' ')
358 /* Copy a single filename if multiple provided */
359 end = strstr(fdtoverlay, " ");
361 len = (int)(end - fdtoverlay);
362 overlayfile = malloc(len + 1);
363 strncpy(overlayfile, fdtoverlay, len);
364 overlayfile[len] = '\0';
366 overlayfile = fdtoverlay;
368 if (!strlen(overlayfile))
371 /* Load overlay file */
372 err = get_relfile_envaddr(ctx, overlayfile, "fdtoverlay_addr_r",
375 printf("Failed loading overlay %s\n", overlayfile);
379 /* Resize main fdt */
380 fdt_shrink_to_minimum(working_fdt, 8192);
382 blob = map_sysmem(fdtoverlay_addr, 0);
383 err = fdt_check_header(blob);
385 printf("Invalid overlay %s, skipping\n",
390 err = fdt_overlay_apply_verbose(working_fdt, blob);
392 printf("Failed to apply overlay %s, skipping\n",
400 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
405 * label_boot() - Boot according to the contents of a pxe_label
407 * If we can't boot for any reason, we return. A successful boot never
410 * The kernel will be stored in the location given by the 'kernel_addr_r'
411 * environment variable.
413 * If the label specifies an initrd file, it will be stored in the location
414 * given by the 'ramdisk_addr_r' environment variable.
416 * If the label specifies an 'append' line, its contents will overwrite that
417 * of the 'bootargs' environment variable.
420 * @label: Label to process
421 * Returns does not return on success, otherwise returns 0 if a localboot
422 * label was processed, or 1 on error
424 static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
426 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
427 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
428 char *kernel_addr = NULL;
429 char *initrd_addr_str = NULL;
430 char initrd_filesize[10];
432 char mac_str[29] = "";
433 char ip_str[68] = "";
434 char *fit_addr = NULL;
443 label->attempted = 1;
445 if (label->localboot) {
446 if (label->localboot_val >= 0)
447 label_localboot(label);
451 if (!label->kernel) {
452 printf("No kernel given, skipping %s\n",
460 if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r",
462 printf("Skipping %s for failure retrieving initrd\n",
467 initrd_addr_str = env_get("ramdisk_addr_r");
468 strcpy(initrd_filesize, simple_xtoa(size));
470 strncpy(initrd_str, initrd_addr_str, 18);
471 strcat(initrd_str, ":");
472 strncat(initrd_str, initrd_filesize, 9);
475 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r",
477 printf("Skipping %s for failure retrieving kernel\n",
482 if (label->ipappend & 0x1) {
483 sprintf(ip_str, " ip=%s:%s:%s:%s",
484 env_get("ipaddr"), env_get("serverip"),
485 env_get("gatewayip"), env_get("netmask"));
488 if (IS_ENABLED(CONFIG_CMD_NET)) {
489 if (label->ipappend & 0x2) {
492 strcpy(mac_str, " BOOTIF=");
493 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
499 if ((label->ipappend & 0x3) || label->append) {
500 char bootargs[CONFIG_SYS_CBSIZE] = "";
501 char finalbootargs[CONFIG_SYS_CBSIZE];
503 if (strlen(label->append ?: "") +
504 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
505 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
506 strlen(label->append ?: ""),
507 strlen(ip_str), strlen(mac_str),
513 strncpy(bootargs, label->append, sizeof(bootargs));
515 strcat(bootargs, ip_str);
516 strcat(bootargs, mac_str);
518 cli_simple_process_macros(bootargs, finalbootargs,
519 sizeof(finalbootargs));
520 env_set("bootargs", finalbootargs);
521 printf("append: %s\n", finalbootargs);
524 kernel_addr = env_get("kernel_addr_r");
526 /* for FIT, append the configuration identifier */
528 int len = strlen(kernel_addr) + strlen(label->config) + 1;
530 fit_addr = malloc(len);
532 printf("malloc fail (FIT address)\n");
535 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
536 kernel_addr = fit_addr;
540 * fdt usage is optional:
541 * It handles the following scenarios.
543 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
544 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
545 * bootm, and adjust argc appropriately.
547 * If retrieve fails and no exact fdt blob is specified in pxe file with
548 * "fdt" label, try Scenario 2.
550 * Scenario 2: If there is an fdt_addr specified, pass it along to
551 * bootm, and adjust argc appropriately.
553 * Scenario 3: fdt blob is not available.
555 bootm_argv[3] = env_get("fdt_addr_r");
557 /* if fdt label is defined then get fdt from server */
559 char *fdtfile = NULL;
560 char *fdtfilefree = NULL;
563 fdtfile = label->fdt;
564 } else if (label->fdtdir) {
565 char *f1, *f2, *f3, *f4, *slash;
567 f1 = env_get("fdtfile");
574 * For complex cases where this code doesn't
575 * generate the correct filename, the board
576 * code should set $fdtfile during early boot,
577 * or the boot scripts should set $fdtfile
578 * before invoking "pxe" or "sysboot".
582 f3 = env_get("board");
594 len = strlen(label->fdtdir);
597 else if (label->fdtdir[len - 1] != '/')
602 len = strlen(label->fdtdir) + strlen(slash) +
603 strlen(f1) + strlen(f2) + strlen(f3) +
605 fdtfilefree = malloc(len);
607 printf("malloc fail (FDT filename)\n");
611 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
612 label->fdtdir, slash, f1, f2, f3, f4);
613 fdtfile = fdtfilefree;
617 int err = get_relfile_envaddr(ctx, fdtfile,
622 bootm_argv[3] = NULL;
625 printf("Skipping %s for failure retrieving FDT\n",
631 #ifdef CONFIG_OF_LIBFDT_OVERLAY
632 if (label->fdtoverlays)
633 label_boot_fdtoverlay(ctx, label);
636 bootm_argv[3] = NULL;
640 bootm_argv[1] = kernel_addr;
641 zboot_argv[1] = kernel_addr;
643 if (initrd_addr_str) {
644 bootm_argv[2] = initrd_str;
647 zboot_argv[3] = initrd_addr_str;
648 zboot_argv[4] = initrd_filesize;
653 bootm_argv[3] = env_get("fdt_addr");
661 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
662 buf = map_sysmem(kernel_addr_r, 0);
663 /* Try bootm for legacy and FIT format image */
664 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
665 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
666 /* Try booting an AArch64 Linux kernel image */
667 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
668 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
669 /* Try booting a Image */
670 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
671 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
672 /* Try booting an x86_64 Linux kernel image */
673 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
674 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
684 /** enum token_type - Tokens for the pxe file parser */
710 /** struct token - token - given by a value and a type */
713 enum token_type type;
716 /* 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 * enum lex_state - lexer state
744 * Since pxe(linux) files don't have a token to identify the start of a
745 * literal, we have to keep track of when we're in a state where a literal is
746 * expected vs when we're in a state a keyword is expected.
755 * get_string() - retrieves a string from *p and stores it as a token in *t.
757 * This is used for scanning both string literals and keywords.
759 * Characters from *p are copied into t-val until a character equal to
760 * delim is found, or a NUL byte is reached. If delim has the special value of
761 * ' ', any whitespace character will be used as a delimiter.
763 * If lower is unequal to 0, uppercase characters will be converted to
764 * lowercase in the result. This is useful to make keywords case
767 * The location of *p is updated to point to the first character after the end
768 * of the token - the ending delimiter.
770 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
773 * @p: Points to a pointer to the current position in the input being processed.
774 * Updated to point at the first character after the current token
775 * @t: Pointers to a token to fill in
776 * @delim: Delimiter character to look for, either newline or space
777 * @lower: true to convert the string to lower case when storing
778 * Returns the new value of t->val, on success, NULL if out of memory
780 static char *get_string(char **p, struct token *t, char delim, int lower)
786 * b and e both start at the beginning of the input stream.
788 * e is incremented until we find the ending delimiter, or a NUL byte
789 * is reached. Then, we take e - b to find the length of the token.
794 if ((delim == ' ' && isspace(*e)) || delim == *e)
802 * Allocate memory to hold the string, and copy it in, converting
803 * characters to lowercase if lower is != 0.
805 t->val = malloc(len + 1);
809 for (i = 0; i < len; i++, b++) {
811 t->val[i] = tolower(*b);
818 /* Update *p so the caller knows where to continue scanning */
826 * get_keyword() - Populate a keyword token with a type and value
828 * Updates the ->type field based on the keyword string in @val
829 * @t: Token to populate
831 static void get_keyword(struct token *t)
835 for (i = 0; keywords[i].val; i++) {
836 if (!strcmp(t->val, keywords[i].val)) {
837 t->type = keywords[i].type;
844 * get_token() - Get the next token
846 * We have to keep track of which state we're in to know if we're looking to get
847 * a string literal or a keyword.
849 * @p: Points to a pointer to the current position in the input being processed.
850 * Updated to point at the first character after the current token
852 static void get_token(char **p, struct token *t, enum lex_state state)
858 /* eat non EOL whitespace */
863 * eat comments. note that string literals can't begin with #, but
864 * can contain a # after their first character.
867 while (*c && *c != '\n')
874 } else if (*c == '\0') {
877 } else if (state == L_SLITERAL) {
878 get_string(&c, t, '\n', 0);
879 } else if (state == L_KEYWORD) {
881 * when we expect a keyword, we first get the next string
882 * token delimited by whitespace, and then check if it
883 * matches a keyword in our keyword list. if it does, it's
884 * converted to a keyword token of the appropriate type, and
885 * if not, it remains a string token.
887 get_string(&c, t, ' ', 1);
895 * eol_or_eof() - Find end of line
897 * Increment *c until we get to the end of the current line, or EOF
899 * @c: Points to a pointer to the current position in the input being processed.
900 * Updated to point at the first character after the current token
902 static void eol_or_eof(char **c)
904 while (**c && **c != '\n')
909 * All of these parse_* functions share some common behavior.
911 * They finish with *c pointing after the token they parse, and return 1 on
912 * success, or < 0 on error.
916 * Parse a string literal and store a pointer it at *dst. String literals
917 * terminate at the end of the line.
919 static int parse_sliteral(char **c, char **dst)
924 get_token(c, &t, L_SLITERAL);
926 if (t.type != T_STRING) {
927 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
937 * Parse a base 10 (unsigned) integer and store it at *dst.
939 static int parse_integer(char **c, int *dst)
944 get_token(c, &t, L_SLITERAL);
945 if (t.type != T_STRING) {
946 printf("Expected string: %.*s\n", (int)(*c - s), s);
950 *dst = simple_strtol(t.val, NULL, 10);
957 static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
958 struct pxe_menu *cfg, int nest_level);
961 * Parse an include statement, and retrieve and parse the file it mentions.
963 * base should point to a location where it's safe to store the file, and
964 * nest_level should indicate how many nested includes have occurred. For this
965 * include, nest_level has already been incremented and doesn't need to be
968 static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
969 struct pxe_menu *cfg, int nest_level)
977 err = parse_sliteral(c, &include_path);
979 printf("Expected include path: %.*s\n", (int)(*c - s), s);
983 err = get_pxe_file(ctx, include_path, base);
985 printf("Couldn't retrieve %s\n", include_path);
989 buf = map_sysmem(base, 0);
990 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
997 * Parse lines that begin with 'menu'.
999 * base and nest are provided to handle the 'menu include' case.
1001 * base should point to a location where it's safe to store the included file.
1003 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1004 * a file it includes, 3 when parsing a file included by that file, and so on.
1006 static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
1007 unsigned long base, int nest_level)
1013 get_token(c, &t, L_KEYWORD);
1017 err = parse_sliteral(c, &cfg->title);
1022 err = handle_include(ctx, c, base, cfg, nest_level + 1);
1026 err = parse_sliteral(c, &cfg->bmp);
1030 printf("Ignoring malformed menu command: %.*s\n",
1042 * Handles parsing a 'menu line' when we're parsing a label.
1044 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1045 struct pxe_label *label)
1052 get_token(c, &t, L_KEYWORD);
1056 if (!cfg->default_label)
1057 cfg->default_label = strdup(label->name);
1059 if (!cfg->default_label)
1064 parse_sliteral(c, &label->menu);
1067 printf("Ignoring malformed menu command: %.*s\n",
1077 * Handles parsing a 'kernel' label.
1078 * expecting "filename" or "<fit_filename>#cfg"
1080 static int parse_label_kernel(char **c, struct pxe_label *label)
1085 err = parse_sliteral(c, &label->kernel);
1089 s = strstr(label->kernel, "#");
1093 label->config = malloc(strlen(s) + 1);
1097 strcpy(label->config, s);
1104 * Parses a label and adds it to the list of labels for a menu.
1106 * A label ends when we either get to the end of a file, or
1107 * get some input we otherwise don't have a handler defined
1111 static int parse_label(char **c, struct pxe_menu *cfg)
1116 struct pxe_label *label;
1119 label = label_create();
1123 err = parse_sliteral(c, &label->name);
1125 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1126 label_destroy(label);
1130 list_add_tail(&label->list, &cfg->labels);
1134 get_token(c, &t, L_KEYWORD);
1139 err = parse_label_menu(c, cfg, label);
1144 err = parse_label_kernel(c, label);
1148 err = parse_sliteral(c, &label->append);
1151 s = strstr(label->append, "initrd=");
1155 len = (int)(strchr(s, ' ') - s);
1156 label->initrd = malloc(len + 1);
1157 strncpy(label->initrd, s, len);
1158 label->initrd[len] = '\0';
1164 err = parse_sliteral(c, &label->initrd);
1169 err = parse_sliteral(c, &label->fdt);
1174 err = parse_sliteral(c, &label->fdtdir);
1178 if (!label->fdtoverlays)
1179 err = parse_sliteral(c, &label->fdtoverlays);
1183 label->localboot = 1;
1184 err = parse_integer(c, &label->localboot_val);
1188 err = parse_integer(c, &label->ipappend);
1196 * put the token back! we don't want it - it's the end
1197 * of a label and whatever token this is, it's
1198 * something for the menu level context to handle.
1210 * This 16 comes from the limit pxelinux imposes on nested includes.
1212 * There is no reason at all we couldn't do more, but some limit helps prevent
1213 * infinite (until crash occurs) recursion if a file tries to include itself.
1215 #define MAX_NEST_LEVEL 16
1218 * Entry point for parsing a menu file. nest_level indicates how many times
1219 * we've nested in includes. It will be 1 for the top level menu file.
1221 * Returns 1 on success, < 0 on error.
1223 static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
1224 struct pxe_menu *cfg, int nest_level)
1227 char *s, *b, *label_name;
1232 if (nest_level > MAX_NEST_LEVEL) {
1233 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1240 get_token(&p, &t, L_KEYWORD);
1246 err = parse_menu(ctx, &p, cfg,
1247 base + ALIGN(strlen(b) + 1, 4),
1252 err = parse_integer(&p, &cfg->timeout);
1256 err = parse_label(&p, cfg);
1261 err = parse_sliteral(&p, &label_name);
1264 if (cfg->default_label)
1265 free(cfg->default_label);
1267 cfg->default_label = label_name;
1273 err = handle_include(ctx, &p,
1274 base + ALIGN(strlen(b), 4), cfg,
1289 printf("Ignoring unknown command: %.*s\n",
1301 void destroy_pxe_menu(struct pxe_menu *cfg)
1303 struct list_head *pos, *n;
1304 struct pxe_label *label;
1307 free(cfg->default_label);
1309 list_for_each_safe(pos, n, &cfg->labels) {
1310 label = list_entry(pos, struct pxe_label, list);
1312 label_destroy(label);
1318 struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
1320 struct pxe_menu *cfg;
1324 cfg = malloc(sizeof(struct pxe_menu));
1328 memset(cfg, 0, sizeof(struct pxe_menu));
1330 INIT_LIST_HEAD(&cfg->labels);
1332 buf = map_sysmem(menucfg, 0);
1333 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
1336 destroy_pxe_menu(cfg);
1344 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1347 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1349 struct pxe_label *label;
1350 struct list_head *pos;
1354 char *default_num = NULL;
1357 * Create a menu and add items for all the labels.
1359 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
1360 cfg->prompt, NULL, label_print, NULL, NULL);
1364 list_for_each(pos, &cfg->labels) {
1365 label = list_entry(pos, struct pxe_label, list);
1367 sprintf(label->num, "%d", i++);
1368 if (menu_item_add(m, label->num, label) != 1) {
1372 if (cfg->default_label &&
1373 (strcmp(label->name, cfg->default_label) == 0))
1374 default_num = label->num;
1378 * After we've created items for each label in the menu, set the
1379 * menu's default label if one was specified.
1382 err = menu_default_set(m, default_num);
1384 if (err != -ENOENT) {
1389 printf("Missing default: %s\n", cfg->default_label);
1397 * Try to boot any labels we have yet to attempt to boot.
1399 static void boot_unattempted_labels(struct pxe_context *ctx,
1400 struct pxe_menu *cfg)
1402 struct list_head *pos;
1403 struct pxe_label *label;
1405 list_for_each(pos, &cfg->labels) {
1406 label = list_entry(pos, struct pxe_label, list);
1408 if (!label->attempted)
1409 label_boot(ctx, label);
1413 void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
1419 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1420 /* display BMP if available */
1422 if (get_relfile(ctx, cfg->bmp, image_load_addr, NULL)) {
1423 if (CONFIG_IS_ENABLED(CMD_CLS))
1424 run_command("cls", 0);
1425 bmp_display(image_load_addr,
1426 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1428 printf("Skipping background bmp %s for failure\n",
1434 m = pxe_menu_to_menu(cfg);
1438 err = menu_get_choice(m, &choice);
1442 * err == 1 means we got a choice back from menu_get_choice.
1444 * err == -ENOENT if the menu was setup to select the default but no
1445 * default was set. in that case, we should continue trying to boot
1446 * labels that haven't been attempted yet.
1448 * otherwise, the user interrupted or there was some other error and
1453 err = label_boot(ctx, choice);
1456 } else if (err != -ENOENT) {
1460 boot_unattempted_labels(ctx, cfg);
1463 int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1464 pxe_getfile_func getfile, void *userdata,
1465 bool allow_abs_path, const char *bootfile)
1467 const char *last_slash;
1468 size_t path_len = 0;
1470 memset(ctx, '\0', sizeof(*ctx));
1472 ctx->getfile = getfile;
1473 ctx->userdata = userdata;
1474 ctx->allow_abs_path = allow_abs_path;
1476 /* figure out the boot directory, if there is one */
1477 if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
1479 ctx->bootdir = strdup(bootfile ? bootfile : "");
1484 last_slash = strrchr(bootfile, '/');
1486 path_len = (last_slash - bootfile) + 1;
1488 ctx->bootdir[path_len] = '\0';
1493 void pxe_destroy_ctx(struct pxe_context *ctx)
1498 int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt)
1500 struct pxe_menu *cfg;
1502 cfg = parse_pxefile(ctx, pxefile_addr_r);
1504 printf("Error parsing config file\n");
1511 handle_pxe_menu(ctx, cfg);
1513 destroy_pxe_menu(cfg);