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: If there is an fdtcontroladdr specified, pass it along to
554 * bootm, and adjust argc appropriately.
556 * Scenario 4: fdt blob is not available.
558 bootm_argv[3] = env_get("fdt_addr_r");
560 /* if fdt label is defined then get fdt from server */
562 char *fdtfile = NULL;
563 char *fdtfilefree = NULL;
566 fdtfile = label->fdt;
567 } else if (label->fdtdir) {
568 char *f1, *f2, *f3, *f4, *slash;
570 f1 = env_get("fdtfile");
577 * For complex cases where this code doesn't
578 * generate the correct filename, the board
579 * code should set $fdtfile during early boot,
580 * or the boot scripts should set $fdtfile
581 * before invoking "pxe" or "sysboot".
585 f3 = env_get("board");
597 len = strlen(label->fdtdir);
600 else if (label->fdtdir[len - 1] != '/')
605 len = strlen(label->fdtdir) + strlen(slash) +
606 strlen(f1) + strlen(f2) + strlen(f3) +
608 fdtfilefree = malloc(len);
610 printf("malloc fail (FDT filename)\n");
614 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
615 label->fdtdir, slash, f1, f2, f3, f4);
616 fdtfile = fdtfilefree;
620 int err = get_relfile_envaddr(ctx, fdtfile,
625 bootm_argv[3] = NULL;
628 printf("Skipping %s for failure retrieving FDT\n",
634 #ifdef CONFIG_OF_LIBFDT_OVERLAY
635 if (label->fdtoverlays)
636 label_boot_fdtoverlay(ctx, label);
639 bootm_argv[3] = NULL;
643 bootm_argv[1] = kernel_addr;
644 zboot_argv[1] = kernel_addr;
646 if (initrd_addr_str) {
647 bootm_argv[2] = initrd_str;
650 zboot_argv[3] = initrd_addr_str;
651 zboot_argv[4] = initrd_filesize;
656 bootm_argv[3] = env_get("fdt_addr");
659 bootm_argv[3] = env_get("fdtcontroladdr");
667 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
668 buf = map_sysmem(kernel_addr_r, 0);
669 /* Try bootm for legacy and FIT format image */
670 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
671 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
672 /* Try booting an AArch64 Linux kernel image */
673 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
674 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
675 /* Try booting a Image */
676 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
677 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
678 /* Try booting an x86_64 Linux kernel image */
679 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
680 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
690 /** enum token_type - Tokens for the pxe file parser */
716 /** struct token - token - given by a value and a type */
719 enum token_type type;
722 /* Keywords recognized */
723 static const struct token keywords[] = {
726 {"timeout", T_TIMEOUT},
727 {"default", T_DEFAULT},
728 {"prompt", T_PROMPT},
730 {"kernel", T_KERNEL},
732 {"localboot", T_LOCALBOOT},
733 {"append", T_APPEND},
734 {"initrd", T_INITRD},
735 {"include", T_INCLUDE},
736 {"devicetree", T_FDT},
738 {"devicetreedir", T_FDTDIR},
739 {"fdtdir", T_FDTDIR},
740 {"fdtoverlays", T_FDTOVERLAYS},
741 {"ontimeout", T_ONTIMEOUT,},
742 {"ipappend", T_IPAPPEND,},
743 {"background", T_BACKGROUND,},
748 * enum lex_state - lexer state
750 * Since pxe(linux) files don't have a token to identify the start of a
751 * literal, we have to keep track of when we're in a state where a literal is
752 * expected vs when we're in a state a keyword is expected.
761 * get_string() - retrieves a string from *p and stores it as a token in *t.
763 * This is used for scanning both string literals and keywords.
765 * Characters from *p are copied into t-val until a character equal to
766 * delim is found, or a NUL byte is reached. If delim has the special value of
767 * ' ', any whitespace character will be used as a delimiter.
769 * If lower is unequal to 0, uppercase characters will be converted to
770 * lowercase in the result. This is useful to make keywords case
773 * The location of *p is updated to point to the first character after the end
774 * of the token - the ending delimiter.
776 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
779 * @p: Points to a pointer to the current position in the input being processed.
780 * Updated to point at the first character after the current token
781 * @t: Pointers to a token to fill in
782 * @delim: Delimiter character to look for, either newline or space
783 * @lower: true to convert the string to lower case when storing
784 * Returns the new value of t->val, on success, NULL if out of memory
786 static char *get_string(char **p, struct token *t, char delim, int lower)
792 * b and e both start at the beginning of the input stream.
794 * e is incremented until we find the ending delimiter, or a NUL byte
795 * is reached. Then, we take e - b to find the length of the token.
800 if ((delim == ' ' && isspace(*e)) || delim == *e)
808 * Allocate memory to hold the string, and copy it in, converting
809 * characters to lowercase if lower is != 0.
811 t->val = malloc(len + 1);
815 for (i = 0; i < len; i++, b++) {
817 t->val[i] = tolower(*b);
824 /* Update *p so the caller knows where to continue scanning */
832 * get_keyword() - Populate a keyword token with a type and value
834 * Updates the ->type field based on the keyword string in @val
835 * @t: Token to populate
837 static void get_keyword(struct token *t)
841 for (i = 0; keywords[i].val; i++) {
842 if (!strcmp(t->val, keywords[i].val)) {
843 t->type = keywords[i].type;
850 * get_token() - Get the next token
852 * We have to keep track of which state we're in to know if we're looking to get
853 * a string literal or a keyword.
855 * @p: Points to a pointer to the current position in the input being processed.
856 * Updated to point at the first character after the current token
858 static void get_token(char **p, struct token *t, enum lex_state state)
864 /* eat non EOL whitespace */
869 * eat comments. note that string literals can't begin with #, but
870 * can contain a # after their first character.
873 while (*c && *c != '\n')
880 } else if (*c == '\0') {
883 } else if (state == L_SLITERAL) {
884 get_string(&c, t, '\n', 0);
885 } else if (state == L_KEYWORD) {
887 * when we expect a keyword, we first get the next string
888 * token delimited by whitespace, and then check if it
889 * matches a keyword in our keyword list. if it does, it's
890 * converted to a keyword token of the appropriate type, and
891 * if not, it remains a string token.
893 get_string(&c, t, ' ', 1);
901 * eol_or_eof() - Find end of line
903 * Increment *c until we get to the end of the current line, or EOF
905 * @c: Points to a pointer to the current position in the input being processed.
906 * Updated to point at the first character after the current token
908 static void eol_or_eof(char **c)
910 while (**c && **c != '\n')
915 * All of these parse_* functions share some common behavior.
917 * They finish with *c pointing after the token they parse, and return 1 on
918 * success, or < 0 on error.
922 * Parse a string literal and store a pointer it at *dst. String literals
923 * terminate at the end of the line.
925 static int parse_sliteral(char **c, char **dst)
930 get_token(c, &t, L_SLITERAL);
932 if (t.type != T_STRING) {
933 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
943 * Parse a base 10 (unsigned) integer and store it at *dst.
945 static int parse_integer(char **c, int *dst)
950 get_token(c, &t, L_SLITERAL);
951 if (t.type != T_STRING) {
952 printf("Expected string: %.*s\n", (int)(*c - s), s);
956 *dst = simple_strtol(t.val, NULL, 10);
963 static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
964 struct pxe_menu *cfg, int nest_level);
967 * Parse an include statement, and retrieve and parse the file it mentions.
969 * base should point to a location where it's safe to store the file, and
970 * nest_level should indicate how many nested includes have occurred. For this
971 * include, nest_level has already been incremented and doesn't need to be
974 static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
975 struct pxe_menu *cfg, int nest_level)
983 err = parse_sliteral(c, &include_path);
985 printf("Expected include path: %.*s\n", (int)(*c - s), s);
989 err = get_pxe_file(ctx, include_path, base);
991 printf("Couldn't retrieve %s\n", include_path);
995 buf = map_sysmem(base, 0);
996 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
1003 * Parse lines that begin with 'menu'.
1005 * base and nest are provided to handle the 'menu include' case.
1007 * base should point to a location where it's safe to store the included file.
1009 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1010 * a file it includes, 3 when parsing a file included by that file, and so on.
1012 static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
1013 unsigned long base, int nest_level)
1019 get_token(c, &t, L_KEYWORD);
1023 err = parse_sliteral(c, &cfg->title);
1028 err = handle_include(ctx, c, base, cfg, nest_level + 1);
1032 err = parse_sliteral(c, &cfg->bmp);
1036 printf("Ignoring malformed menu command: %.*s\n",
1048 * Handles parsing a 'menu line' when we're parsing a label.
1050 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1051 struct pxe_label *label)
1058 get_token(c, &t, L_KEYWORD);
1062 if (!cfg->default_label)
1063 cfg->default_label = strdup(label->name);
1065 if (!cfg->default_label)
1070 parse_sliteral(c, &label->menu);
1073 printf("Ignoring malformed menu command: %.*s\n",
1083 * Handles parsing a 'kernel' label.
1084 * expecting "filename" or "<fit_filename>#cfg"
1086 static int parse_label_kernel(char **c, struct pxe_label *label)
1091 err = parse_sliteral(c, &label->kernel);
1095 s = strstr(label->kernel, "#");
1099 label->config = malloc(strlen(s) + 1);
1103 strcpy(label->config, s);
1110 * Parses a label and adds it to the list of labels for a menu.
1112 * A label ends when we either get to the end of a file, or
1113 * get some input we otherwise don't have a handler defined
1117 static int parse_label(char **c, struct pxe_menu *cfg)
1122 struct pxe_label *label;
1125 label = label_create();
1129 err = parse_sliteral(c, &label->name);
1131 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1132 label_destroy(label);
1136 list_add_tail(&label->list, &cfg->labels);
1140 get_token(c, &t, L_KEYWORD);
1145 err = parse_label_menu(c, cfg, label);
1150 err = parse_label_kernel(c, label);
1154 err = parse_sliteral(c, &label->append);
1157 s = strstr(label->append, "initrd=");
1161 len = (int)(strchr(s, ' ') - s);
1162 label->initrd = malloc(len + 1);
1163 strncpy(label->initrd, s, len);
1164 label->initrd[len] = '\0';
1170 err = parse_sliteral(c, &label->initrd);
1175 err = parse_sliteral(c, &label->fdt);
1180 err = parse_sliteral(c, &label->fdtdir);
1184 if (!label->fdtoverlays)
1185 err = parse_sliteral(c, &label->fdtoverlays);
1189 label->localboot = 1;
1190 err = parse_integer(c, &label->localboot_val);
1194 err = parse_integer(c, &label->ipappend);
1202 * put the token back! we don't want it - it's the end
1203 * of a label and whatever token this is, it's
1204 * something for the menu level context to handle.
1216 * This 16 comes from the limit pxelinux imposes on nested includes.
1218 * There is no reason at all we couldn't do more, but some limit helps prevent
1219 * infinite (until crash occurs) recursion if a file tries to include itself.
1221 #define MAX_NEST_LEVEL 16
1224 * Entry point for parsing a menu file. nest_level indicates how many times
1225 * we've nested in includes. It will be 1 for the top level menu file.
1227 * Returns 1 on success, < 0 on error.
1229 static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
1230 struct pxe_menu *cfg, int nest_level)
1233 char *s, *b, *label_name;
1238 if (nest_level > MAX_NEST_LEVEL) {
1239 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1246 get_token(&p, &t, L_KEYWORD);
1252 err = parse_menu(ctx, &p, cfg,
1253 base + ALIGN(strlen(b) + 1, 4),
1258 err = parse_integer(&p, &cfg->timeout);
1262 err = parse_label(&p, cfg);
1267 err = parse_sliteral(&p, &label_name);
1270 if (cfg->default_label)
1271 free(cfg->default_label);
1273 cfg->default_label = label_name;
1279 err = handle_include(ctx, &p,
1280 base + ALIGN(strlen(b), 4), cfg,
1295 printf("Ignoring unknown command: %.*s\n",
1307 void destroy_pxe_menu(struct pxe_menu *cfg)
1309 struct list_head *pos, *n;
1310 struct pxe_label *label;
1313 free(cfg->default_label);
1315 list_for_each_safe(pos, n, &cfg->labels) {
1316 label = list_entry(pos, struct pxe_label, list);
1318 label_destroy(label);
1324 struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
1326 struct pxe_menu *cfg;
1330 cfg = malloc(sizeof(struct pxe_menu));
1334 memset(cfg, 0, sizeof(struct pxe_menu));
1336 INIT_LIST_HEAD(&cfg->labels);
1338 buf = map_sysmem(menucfg, 0);
1339 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
1342 destroy_pxe_menu(cfg);
1350 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1353 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1355 struct pxe_label *label;
1356 struct list_head *pos;
1358 char *label_override;
1361 char *default_num = NULL;
1362 char *override_num = NULL;
1365 * Create a menu and add items for all the labels.
1367 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
1368 cfg->prompt, NULL, label_print, NULL, NULL);
1372 label_override = env_get("pxe_label_override");
1374 list_for_each(pos, &cfg->labels) {
1375 label = list_entry(pos, struct pxe_label, list);
1377 sprintf(label->num, "%d", i++);
1378 if (menu_item_add(m, label->num, label) != 1) {
1382 if (cfg->default_label &&
1383 (strcmp(label->name, cfg->default_label) == 0))
1384 default_num = label->num;
1385 if (label_override && !strcmp(label->name, label_override))
1386 override_num = label->num;
1390 if (label_override) {
1392 default_num = override_num;
1394 printf("Missing override pxe label: %s\n",
1399 * After we've created items for each label in the menu, set the
1400 * menu's default label if one was specified.
1403 err = menu_default_set(m, default_num);
1405 if (err != -ENOENT) {
1410 printf("Missing default: %s\n", cfg->default_label);
1418 * Try to boot any labels we have yet to attempt to boot.
1420 static void boot_unattempted_labels(struct pxe_context *ctx,
1421 struct pxe_menu *cfg)
1423 struct list_head *pos;
1424 struct pxe_label *label;
1426 list_for_each(pos, &cfg->labels) {
1427 label = list_entry(pos, struct pxe_label, list);
1429 if (!label->attempted)
1430 label_boot(ctx, label);
1434 void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
1440 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1441 /* display BMP if available */
1443 if (get_relfile(ctx, cfg->bmp, image_load_addr, NULL)) {
1444 if (CONFIG_IS_ENABLED(CMD_CLS))
1445 run_command("cls", 0);
1446 bmp_display(image_load_addr,
1447 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1449 printf("Skipping background bmp %s for failure\n",
1455 m = pxe_menu_to_menu(cfg);
1459 err = menu_get_choice(m, &choice);
1463 * err == 1 means we got a choice back from menu_get_choice.
1465 * err == -ENOENT if the menu was setup to select the default but no
1466 * default was set. in that case, we should continue trying to boot
1467 * labels that haven't been attempted yet.
1469 * otherwise, the user interrupted or there was some other error and
1474 err = label_boot(ctx, choice);
1477 } else if (err != -ENOENT) {
1481 boot_unattempted_labels(ctx, cfg);
1484 int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1485 pxe_getfile_func getfile, void *userdata,
1486 bool allow_abs_path, const char *bootfile)
1488 const char *last_slash;
1489 size_t path_len = 0;
1491 memset(ctx, '\0', sizeof(*ctx));
1493 ctx->getfile = getfile;
1494 ctx->userdata = userdata;
1495 ctx->allow_abs_path = allow_abs_path;
1497 /* figure out the boot directory, if there is one */
1498 if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
1500 ctx->bootdir = strdup(bootfile ? bootfile : "");
1505 last_slash = strrchr(bootfile, '/');
1507 path_len = (last_slash - bootfile) + 1;
1509 ctx->bootdir[path_len] = '\0';
1514 void pxe_destroy_ctx(struct pxe_context *ctx)
1519 int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt)
1521 struct pxe_menu *cfg;
1523 cfg = parse_pxefile(ctx, pxefile_addr_r);
1525 printf("Error parsing config file\n");
1532 handle_pxe_menu(ctx, cfg);
1534 destroy_pxe_menu(cfg);