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
34 * format_mac_pxe() - obtain a MAC address in the PXE format
36 * This produces a MAC-address string in the format for the current ethernet
39 * 01-aa-bb-cc-dd-ee-ff
41 * where aa-ff is the MAC address in hex
43 * @outbuf: Buffer to write string to
44 * @outbuf_len: length of buffer
45 * @return 1 if OK, -ENOSPC if buffer is too small, -ENOENT is there is no
46 * current ethernet device
48 int format_mac_pxe(char *outbuf, size_t outbuf_len)
52 if (outbuf_len < 21) {
53 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
57 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
60 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
61 ethaddr[0], ethaddr[1], ethaddr[2],
62 ethaddr[3], ethaddr[4], ethaddr[5]);
68 * get_bootfile_path() - Figure out the path of a file to read
70 * Copies the boot directory into the supplied buffer. If there is no boot
71 * directory, set it to ""
74 * @file_path: File path to read (relative to the PXE file)
75 * @bootfile_path: Place to put the bootfile path
76 * @bootfile_path_size: Size of @bootfile_path in bytes
77 * @allow_abs_path: true to allow an absolute path (where @file_path starts with
78 * '/', false to return an empty path (and success) in that case
79 * Returns 1 for success, -ENOSPC if bootfile_path_size is to small to hold the
82 static int get_bootfile_path(struct pxe_context *ctx, const char *file_path,
83 char *bootfile_path, size_t bootfile_path_size,
88 /* Only syslinux allows absolute paths */
89 if (file_path[0] == '/' && allow_abs_path)
92 path_len = strlen(ctx->bootdir);
93 if (bootfile_path_size < path_len + 1) {
94 printf("bootfile_path too small. (%zd < %zd)\n",
95 bootfile_path_size, path_len);
100 strncpy(bootfile_path, ctx->bootdir, path_len);
103 bootfile_path[path_len] = '\0';
109 * get_relfile() - read a file relative to the PXE file
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.
117 * @file_path: File path to read (relative to the PXE file)
118 * @file_addr: Address to load file to
119 * Returns 1 for success, or < 0 on error
121 static int get_relfile(struct pxe_context *ctx, const char *file_path,
122 unsigned long file_addr)
125 char relfile[MAX_TFTP_PATH_LEN + 1];
129 err = get_bootfile_path(ctx, file_path, relfile, sizeof(relfile),
130 ctx->allow_abs_path);
134 path_len = strlen(file_path);
135 path_len += strlen(relfile);
137 if (path_len > MAX_TFTP_PATH_LEN) {
138 printf("Base path too long (%s%s)\n", relfile, file_path);
140 return -ENAMETOOLONG;
143 strcat(relfile, file_path);
145 printf("Retrieving file: %s\n", relfile);
147 sprintf(addr_buf, "%lx", file_addr);
149 return ctx->getfile(ctx, relfile, addr_buf);
153 * get_pxe_file() - read a file
155 * The file is read and nul-terminated
158 * @file_path: File path to read (relative to the PXE file)
159 * @file_addr: Address to load file to
160 * Returns 1 for success, or < 0 on error
162 int get_pxe_file(struct pxe_context *ctx, const char *file_path,
163 unsigned long file_addr)
165 unsigned long config_file_size;
170 err = get_relfile(ctx, file_path, file_addr);
175 * the file comes without a NUL byte at the end, so find out its size
176 * and add the NUL byte.
178 tftp_filesize = from_env("filesize");
182 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
185 buf = map_sysmem(file_addr + config_file_size, 1);
192 #define PXELINUX_DIR "pxelinux.cfg/"
195 * get_pxelinux_path() - Get a file in the pxelinux.cfg/ directory
198 * @file: Filename to process (relative to pxelinux.cfg/)
199 * Returns 1 for success, -ENAMETOOLONG if the resulting path is too long.
200 * or other value < 0 on other error
202 int get_pxelinux_path(struct pxe_context *ctx, const char *file,
203 unsigned long pxefile_addr_r)
205 size_t base_len = strlen(PXELINUX_DIR);
206 char path[MAX_TFTP_PATH_LEN + 1];
208 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
209 printf("path (%s%s) too long, skipping\n",
211 return -ENAMETOOLONG;
214 sprintf(path, PXELINUX_DIR "%s", file);
216 return get_pxe_file(ctx, path, pxefile_addr_r);
220 * get_relfile_envaddr() - read a file to an address in an env var
222 * Wrapper to make it easier to store the file at file_path in the location
223 * specified by envaddr_name. file_path will be joined to the bootfile path,
224 * if any is specified.
227 * @file_path: File path to read (relative to the PXE file)
228 * @envaddr_name: Name of environment variable which contains the address to
230 * Returns 1 on success, -ENOENT if @envaddr_name does not exist as an
231 * environment variable, -EINVAL if its format is not valid hex, or other
232 * value < 0 on other error
234 static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path,
235 const char *envaddr_name)
237 unsigned long file_addr;
240 envaddr = from_env(envaddr_name);
244 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
247 return get_relfile(ctx, file_path, file_addr);
251 * label_create() - crate a new PXE label
253 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
254 * result must be free()'d to reclaim the memory.
256 * Returns a pointer to the label, or NULL if out of memory
258 static struct pxe_label *label_create(void)
260 struct pxe_label *label;
262 label = malloc(sizeof(struct pxe_label));
266 memset(label, 0, sizeof(struct pxe_label));
272 * label_destroy() - free the memory used by a pxe_label
274 * This frees @label itself as well as memory used by its name,
275 * kernel, config, append, initrd, fdt, fdtdir and fdtoverlay members, if
278 * So - be sure to only use dynamically allocated memory for the members of
279 * the pxe_label struct, unless you want to clean it up first. These are
280 * currently only created by the pxe file parsing code.
282 * @label: Label to free
284 static void label_destroy(struct pxe_label *label)
293 free(label->fdtoverlays);
298 * label_print() - Print a label and its string members if they're defined
300 * This is passed as a callback to the menu code for displaying each
303 * @data: Label to print (is cast to struct pxe_label *)
305 static void label_print(void *data)
307 struct pxe_label *label = data;
308 const char *c = label->menu ? label->menu : label->name;
310 printf("%s:\t%s\n", label->num, c);
314 * label_localboot() - Boot a label that specified 'localboot'
316 * This requires that the 'localcmd' environment variable is defined. Its
317 * contents will be executed as U-Boot commands. If the label specified an
318 * 'append' line, its contents will be used to overwrite the contents of the
319 * 'bootargs' environment variable prior to running 'localcmd'.
321 * @label: Label to process
322 * Returns 1 on success or < 0 on error
324 static int label_localboot(struct pxe_label *label)
328 localcmd = from_env("localcmd");
333 char bootargs[CONFIG_SYS_CBSIZE];
335 cli_simple_process_macros(label->append, bootargs,
337 env_set("bootargs", bootargs);
340 debug("running: %s\n", localcmd);
342 return run_command_list(localcmd, strlen(localcmd), 0);
346 * label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays'
349 * @label: Label to process
351 #ifdef CONFIG_OF_LIBFDT_OVERLAY
352 static void label_boot_fdtoverlay(struct pxe_context *ctx,
353 struct pxe_label *label)
355 char *fdtoverlay = label->fdtoverlays;
356 struct fdt_header *working_fdt;
357 char *fdtoverlay_addr_env;
358 ulong fdtoverlay_addr;
362 /* Get the main fdt and map it */
363 fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
364 working_fdt = map_sysmem(fdt_addr, 0);
365 err = fdt_check_header(working_fdt);
369 /* Get the specific overlay loading address */
370 fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
371 if (!fdtoverlay_addr_env) {
372 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
376 fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
378 /* Cycle over the overlay files and apply them in order */
380 struct fdt_header *blob;
385 /* Drop leading spaces */
386 while (*fdtoverlay == ' ')
389 /* Copy a single filename if multiple provided */
390 end = strstr(fdtoverlay, " ");
392 len = (int)(end - fdtoverlay);
393 overlayfile = malloc(len + 1);
394 strncpy(overlayfile, fdtoverlay, len);
395 overlayfile[len] = '\0';
397 overlayfile = fdtoverlay;
399 if (!strlen(overlayfile))
402 /* Load overlay file */
403 err = get_relfile_envaddr(ctx, overlayfile,
404 "fdtoverlay_addr_r");
406 printf("Failed loading overlay %s\n", overlayfile);
410 /* Resize main fdt */
411 fdt_shrink_to_minimum(working_fdt, 8192);
413 blob = map_sysmem(fdtoverlay_addr, 0);
414 err = fdt_check_header(blob);
416 printf("Invalid overlay %s, skipping\n",
421 err = fdt_overlay_apply_verbose(working_fdt, blob);
423 printf("Failed to apply overlay %s, skipping\n",
431 } while ((fdtoverlay = strstr(fdtoverlay, " ")));
436 * label_boot() - Boot according to the contents of a pxe_label
438 * If we can't boot for any reason, we return. A successful boot never
441 * The kernel will be stored in the location given by the 'kernel_addr_r'
442 * environment variable.
444 * If the label specifies an initrd file, it will be stored in the location
445 * given by the 'ramdisk_addr_r' environment variable.
447 * If the label specifies an 'append' line, its contents will overwrite that
448 * of the 'bootargs' environment variable.
451 * @label: Label to process
452 * Returns does not return on success, otherwise returns 0 if a localboot
453 * label was processed, or 1 on error
455 static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
457 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
458 char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL };
459 char *kernel_addr = NULL;
460 char *initrd_addr_str = NULL;
461 char initrd_filesize[10];
463 char mac_str[29] = "";
464 char ip_str[68] = "";
465 char *fit_addr = NULL;
474 label->attempted = 1;
476 if (label->localboot) {
477 if (label->localboot_val >= 0)
478 label_localboot(label);
482 if (!label->kernel) {
483 printf("No kernel given, skipping %s\n",
489 if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r") < 0) {
490 printf("Skipping %s for failure retrieving initrd\n",
495 initrd_addr_str = env_get("ramdisk_addr_r");
496 strncpy(initrd_filesize, env_get("filesize"), 9);
498 strncpy(initrd_str, initrd_addr_str, 18);
499 strcat(initrd_str, ":");
500 strncat(initrd_str, initrd_filesize, 9);
503 if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r") < 0) {
504 printf("Skipping %s for failure retrieving kernel\n",
509 if (label->ipappend & 0x1) {
510 sprintf(ip_str, " ip=%s:%s:%s:%s",
511 env_get("ipaddr"), env_get("serverip"),
512 env_get("gatewayip"), env_get("netmask"));
515 if (IS_ENABLED(CONFIG_CMD_NET)) {
516 if (label->ipappend & 0x2) {
519 strcpy(mac_str, " BOOTIF=");
520 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
526 if ((label->ipappend & 0x3) || label->append) {
527 char bootargs[CONFIG_SYS_CBSIZE] = "";
528 char finalbootargs[CONFIG_SYS_CBSIZE];
530 if (strlen(label->append ?: "") +
531 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
532 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
533 strlen(label->append ?: ""),
534 strlen(ip_str), strlen(mac_str),
540 strncpy(bootargs, label->append, sizeof(bootargs));
542 strcat(bootargs, ip_str);
543 strcat(bootargs, mac_str);
545 cli_simple_process_macros(bootargs, finalbootargs,
546 sizeof(finalbootargs));
547 env_set("bootargs", finalbootargs);
548 printf("append: %s\n", finalbootargs);
551 kernel_addr = env_get("kernel_addr_r");
553 /* for FIT, append the configuration identifier */
555 int len = strlen(kernel_addr) + strlen(label->config) + 1;
557 fit_addr = malloc(len);
559 printf("malloc fail (FIT address)\n");
562 snprintf(fit_addr, len, "%s%s", kernel_addr, label->config);
563 kernel_addr = fit_addr;
567 * fdt usage is optional:
568 * It handles the following scenarios.
570 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
571 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
572 * bootm, and adjust argc appropriately.
574 * If retrieve fails and no exact fdt blob is specified in pxe file with
575 * "fdt" label, try Scenario 2.
577 * Scenario 2: If there is an fdt_addr specified, pass it along to
578 * bootm, and adjust argc appropriately.
580 * Scenario 3: fdt blob is not available.
582 bootm_argv[3] = env_get("fdt_addr_r");
584 /* if fdt label is defined then get fdt from server */
586 char *fdtfile = NULL;
587 char *fdtfilefree = NULL;
590 fdtfile = label->fdt;
591 } else if (label->fdtdir) {
592 char *f1, *f2, *f3, *f4, *slash;
594 f1 = env_get("fdtfile");
601 * For complex cases where this code doesn't
602 * generate the correct filename, the board
603 * code should set $fdtfile during early boot,
604 * or the boot scripts should set $fdtfile
605 * before invoking "pxe" or "sysboot".
609 f3 = env_get("board");
621 len = strlen(label->fdtdir);
624 else if (label->fdtdir[len - 1] != '/')
629 len = strlen(label->fdtdir) + strlen(slash) +
630 strlen(f1) + strlen(f2) + strlen(f3) +
632 fdtfilefree = malloc(len);
634 printf("malloc fail (FDT filename)\n");
638 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
639 label->fdtdir, slash, f1, f2, f3, f4);
640 fdtfile = fdtfilefree;
644 int err = get_relfile_envaddr(ctx, fdtfile,
649 bootm_argv[3] = NULL;
652 printf("Skipping %s for failure retrieving FDT\n",
658 #ifdef CONFIG_OF_LIBFDT_OVERLAY
659 if (label->fdtoverlays)
660 label_boot_fdtoverlay(ctx, label);
663 bootm_argv[3] = NULL;
667 bootm_argv[1] = kernel_addr;
668 zboot_argv[1] = kernel_addr;
670 if (initrd_addr_str) {
671 bootm_argv[2] = initrd_str;
674 zboot_argv[3] = initrd_addr_str;
675 zboot_argv[4] = initrd_filesize;
680 bootm_argv[3] = env_get("fdt_addr");
688 kernel_addr_r = genimg_get_kernel_addr(kernel_addr);
689 buf = map_sysmem(kernel_addr_r, 0);
690 /* Try bootm for legacy and FIT format image */
691 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
692 do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv);
693 /* Try booting an AArch64 Linux kernel image */
694 else if (IS_ENABLED(CONFIG_CMD_BOOTI))
695 do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv);
696 /* Try booting a Image */
697 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
698 do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv);
699 /* Try booting an x86_64 Linux kernel image */
700 else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
701 do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL);
711 /** enum token_type - Tokens for the pxe file parser */
737 /** struct token - token - given by a value and a type */
740 enum token_type type;
743 /* Keywords recognized */
744 static const struct token keywords[] = {
747 {"timeout", T_TIMEOUT},
748 {"default", T_DEFAULT},
749 {"prompt", T_PROMPT},
751 {"kernel", T_KERNEL},
753 {"localboot", T_LOCALBOOT},
754 {"append", T_APPEND},
755 {"initrd", T_INITRD},
756 {"include", T_INCLUDE},
757 {"devicetree", T_FDT},
759 {"devicetreedir", T_FDTDIR},
760 {"fdtdir", T_FDTDIR},
761 {"fdtoverlays", T_FDTOVERLAYS},
762 {"ontimeout", T_ONTIMEOUT,},
763 {"ipappend", T_IPAPPEND,},
764 {"background", T_BACKGROUND,},
769 * enum lex_state - lexer state
771 * Since pxe(linux) files don't have a token to identify the start of a
772 * literal, we have to keep track of when we're in a state where a literal is
773 * expected vs when we're in a state a keyword is expected.
782 * get_string() - retrieves a string from *p and stores it as a token in *t.
784 * This is used for scanning both string literals and keywords.
786 * Characters from *p are copied into t-val until a character equal to
787 * delim is found, or a NUL byte is reached. If delim has the special value of
788 * ' ', any whitespace character will be used as a delimiter.
790 * If lower is unequal to 0, uppercase characters will be converted to
791 * lowercase in the result. This is useful to make keywords case
794 * The location of *p is updated to point to the first character after the end
795 * of the token - the ending delimiter.
797 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
800 * @p: Points to a pointer to the current position in the input being processed.
801 * Updated to point at the first character after the current token
802 * @t: Pointers to a token to fill in
803 * @delim: Delimiter character to look for, either newline or space
804 * @lower: true to convert the string to lower case when storing
805 * Returns the new value of t->val, on success, NULL if out of memory
807 static char *get_string(char **p, struct token *t, char delim, int lower)
813 * b and e both start at the beginning of the input stream.
815 * e is incremented until we find the ending delimiter, or a NUL byte
816 * is reached. Then, we take e - b to find the length of the token.
821 if ((delim == ' ' && isspace(*e)) || delim == *e)
829 * Allocate memory to hold the string, and copy it in, converting
830 * characters to lowercase if lower is != 0.
832 t->val = malloc(len + 1);
836 for (i = 0; i < len; i++, b++) {
838 t->val[i] = tolower(*b);
845 /* Update *p so the caller knows where to continue scanning */
853 * get_keyword() - Populate a keyword token with a type and value
855 * Updates the ->type field based on the keyword string in @val
856 * @t: Token to populate
858 static void get_keyword(struct token *t)
862 for (i = 0; keywords[i].val; i++) {
863 if (!strcmp(t->val, keywords[i].val)) {
864 t->type = keywords[i].type;
871 * get_token() - Get the next token
873 * We have to keep track of which state we're in to know if we're looking to get
874 * a string literal or a keyword.
876 * @p: Points to a pointer to the current position in the input being processed.
877 * Updated to point at the first character after the current token
879 static void get_token(char **p, struct token *t, enum lex_state state)
885 /* eat non EOL whitespace */
890 * eat comments. note that string literals can't begin with #, but
891 * can contain a # after their first character.
894 while (*c && *c != '\n')
901 } else if (*c == '\0') {
904 } else if (state == L_SLITERAL) {
905 get_string(&c, t, '\n', 0);
906 } else if (state == L_KEYWORD) {
908 * when we expect a keyword, we first get the next string
909 * token delimited by whitespace, and then check if it
910 * matches a keyword in our keyword list. if it does, it's
911 * converted to a keyword token of the appropriate type, and
912 * if not, it remains a string token.
914 get_string(&c, t, ' ', 1);
922 * eol_or_eof() - Find end of line
924 * Increment *c until we get to the end of the current line, or EOF
926 * @c: Points to a pointer to the current position in the input being processed.
927 * Updated to point at the first character after the current token
929 static void eol_or_eof(char **c)
931 while (**c && **c != '\n')
936 * All of these parse_* functions share some common behavior.
938 * They finish with *c pointing after the token they parse, and return 1 on
939 * success, or < 0 on error.
943 * Parse a string literal and store a pointer it at *dst. String literals
944 * terminate at the end of the line.
946 static int parse_sliteral(char **c, char **dst)
951 get_token(c, &t, L_SLITERAL);
953 if (t.type != T_STRING) {
954 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
964 * Parse a base 10 (unsigned) integer and store it at *dst.
966 static int parse_integer(char **c, int *dst)
971 get_token(c, &t, L_SLITERAL);
972 if (t.type != T_STRING) {
973 printf("Expected string: %.*s\n", (int)(*c - s), s);
977 *dst = simple_strtol(t.val, NULL, 10);
984 static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base,
985 struct pxe_menu *cfg, int nest_level);
988 * Parse an include statement, and retrieve and parse the file it mentions.
990 * base should point to a location where it's safe to store the file, and
991 * nest_level should indicate how many nested includes have occurred. For this
992 * include, nest_level has already been incremented and doesn't need to be
995 static int handle_include(struct pxe_context *ctx, char **c, unsigned long base,
996 struct pxe_menu *cfg, int nest_level)
1004 err = parse_sliteral(c, &include_path);
1006 printf("Expected include path: %.*s\n", (int)(*c - s), s);
1010 err = get_pxe_file(ctx, include_path, base);
1012 printf("Couldn't retrieve %s\n", include_path);
1016 buf = map_sysmem(base, 0);
1017 ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level);
1024 * Parse lines that begin with 'menu'.
1026 * base and nest are provided to handle the 'menu include' case.
1028 * base should point to a location where it's safe to store the included file.
1030 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1031 * a file it includes, 3 when parsing a file included by that file, and so on.
1033 static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg,
1034 unsigned long base, int nest_level)
1040 get_token(c, &t, L_KEYWORD);
1044 err = parse_sliteral(c, &cfg->title);
1049 err = handle_include(ctx, c, base, cfg, nest_level + 1);
1053 err = parse_sliteral(c, &cfg->bmp);
1057 printf("Ignoring malformed menu command: %.*s\n",
1069 * Handles parsing a 'menu line' when we're parsing a label.
1071 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1072 struct pxe_label *label)
1079 get_token(c, &t, L_KEYWORD);
1083 if (!cfg->default_label)
1084 cfg->default_label = strdup(label->name);
1086 if (!cfg->default_label)
1091 parse_sliteral(c, &label->menu);
1094 printf("Ignoring malformed menu command: %.*s\n",
1104 * Handles parsing a 'kernel' label.
1105 * expecting "filename" or "<fit_filename>#cfg"
1107 static int parse_label_kernel(char **c, struct pxe_label *label)
1112 err = parse_sliteral(c, &label->kernel);
1116 s = strstr(label->kernel, "#");
1120 label->config = malloc(strlen(s) + 1);
1124 strcpy(label->config, s);
1131 * Parses a label and adds it to the list of labels for a menu.
1133 * A label ends when we either get to the end of a file, or
1134 * get some input we otherwise don't have a handler defined
1138 static int parse_label(char **c, struct pxe_menu *cfg)
1143 struct pxe_label *label;
1146 label = label_create();
1150 err = parse_sliteral(c, &label->name);
1152 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1153 label_destroy(label);
1157 list_add_tail(&label->list, &cfg->labels);
1161 get_token(c, &t, L_KEYWORD);
1166 err = parse_label_menu(c, cfg, label);
1171 err = parse_label_kernel(c, label);
1175 err = parse_sliteral(c, &label->append);
1178 s = strstr(label->append, "initrd=");
1182 len = (int)(strchr(s, ' ') - s);
1183 label->initrd = malloc(len + 1);
1184 strncpy(label->initrd, s, len);
1185 label->initrd[len] = '\0';
1191 err = parse_sliteral(c, &label->initrd);
1196 err = parse_sliteral(c, &label->fdt);
1201 err = parse_sliteral(c, &label->fdtdir);
1205 if (!label->fdtoverlays)
1206 err = parse_sliteral(c, &label->fdtoverlays);
1210 label->localboot = 1;
1211 err = parse_integer(c, &label->localboot_val);
1215 err = parse_integer(c, &label->ipappend);
1223 * put the token back! we don't want it - it's the end
1224 * of a label and whatever token this is, it's
1225 * something for the menu level context to handle.
1237 * This 16 comes from the limit pxelinux imposes on nested includes.
1239 * There is no reason at all we couldn't do more, but some limit helps prevent
1240 * infinite (until crash occurs) recursion if a file tries to include itself.
1242 #define MAX_NEST_LEVEL 16
1245 * Entry point for parsing a menu file. nest_level indicates how many times
1246 * we've nested in includes. It will be 1 for the top level menu file.
1248 * Returns 1 on success, < 0 on error.
1250 static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base,
1251 struct pxe_menu *cfg, int nest_level)
1254 char *s, *b, *label_name;
1259 if (nest_level > MAX_NEST_LEVEL) {
1260 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1267 get_token(&p, &t, L_KEYWORD);
1273 err = parse_menu(ctx, &p, cfg,
1274 base + ALIGN(strlen(b) + 1, 4),
1279 err = parse_integer(&p, &cfg->timeout);
1283 err = parse_label(&p, cfg);
1288 err = parse_sliteral(&p, &label_name);
1291 if (cfg->default_label)
1292 free(cfg->default_label);
1294 cfg->default_label = label_name;
1300 err = handle_include(ctx, &p,
1301 base + ALIGN(strlen(b), 4), cfg,
1316 printf("Ignoring unknown command: %.*s\n",
1328 void destroy_pxe_menu(struct pxe_menu *cfg)
1330 struct list_head *pos, *n;
1331 struct pxe_label *label;
1334 free(cfg->default_label);
1336 list_for_each_safe(pos, n, &cfg->labels) {
1337 label = list_entry(pos, struct pxe_label, list);
1339 label_destroy(label);
1345 struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
1347 struct pxe_menu *cfg;
1351 cfg = malloc(sizeof(struct pxe_menu));
1355 memset(cfg, 0, sizeof(struct pxe_menu));
1357 INIT_LIST_HEAD(&cfg->labels);
1359 buf = map_sysmem(menucfg, 0);
1360 r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
1363 destroy_pxe_menu(cfg);
1371 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1374 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1376 struct pxe_label *label;
1377 struct list_head *pos;
1381 char *default_num = NULL;
1384 * Create a menu and add items for all the labels.
1386 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
1387 cfg->prompt, NULL, label_print, NULL, NULL);
1391 list_for_each(pos, &cfg->labels) {
1392 label = list_entry(pos, struct pxe_label, list);
1394 sprintf(label->num, "%d", i++);
1395 if (menu_item_add(m, label->num, label) != 1) {
1399 if (cfg->default_label &&
1400 (strcmp(label->name, cfg->default_label) == 0))
1401 default_num = label->num;
1405 * After we've created items for each label in the menu, set the
1406 * menu's default label if one was specified.
1409 err = menu_default_set(m, default_num);
1411 if (err != -ENOENT) {
1416 printf("Missing default: %s\n", cfg->default_label);
1424 * Try to boot any labels we have yet to attempt to boot.
1426 static void boot_unattempted_labels(struct pxe_context *ctx,
1427 struct pxe_menu *cfg)
1429 struct list_head *pos;
1430 struct pxe_label *label;
1432 list_for_each(pos, &cfg->labels) {
1433 label = list_entry(pos, struct pxe_label, list);
1435 if (!label->attempted)
1436 label_boot(ctx, label);
1440 void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
1446 if (IS_ENABLED(CONFIG_CMD_BMP)) {
1447 /* display BMP if available */
1449 if (get_relfile(ctx, cfg->bmp, image_load_addr)) {
1450 if (CONFIG_IS_ENABLED(CMD_CLS))
1451 run_command("cls", 0);
1452 bmp_display(image_load_addr,
1453 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1455 printf("Skipping background bmp %s for failure\n",
1461 m = pxe_menu_to_menu(cfg);
1465 err = menu_get_choice(m, &choice);
1469 * err == 1 means we got a choice back from menu_get_choice.
1471 * err == -ENOENT if the menu was setup to select the default but no
1472 * default was set. in that case, we should continue trying to boot
1473 * labels that haven't been attempted yet.
1475 * otherwise, the user interrupted or there was some other error and
1480 err = label_boot(ctx, choice);
1483 } else if (err != -ENOENT) {
1487 boot_unattempted_labels(ctx, cfg);
1490 int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
1491 pxe_getfile_func getfile, void *userdata,
1492 bool allow_abs_path, const char *bootfile)
1494 const char *last_slash;
1495 size_t path_len = 0;
1497 memset(ctx, '\0', sizeof(*ctx));
1499 ctx->getfile = getfile;
1500 ctx->userdata = userdata;
1501 ctx->allow_abs_path = allow_abs_path;
1503 /* figure out the boot directory, if there is one */
1504 if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
1506 ctx->bootdir = strdup(bootfile ? bootfile : "");
1511 last_slash = strrchr(bootfile, '/');
1513 path_len = (last_slash - bootfile) + 1;
1515 ctx->bootdir[path_len] = '\0';
1520 void pxe_destroy_ctx(struct pxe_context *ctx)
1525 int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt)
1527 struct pxe_menu *cfg;
1529 cfg = parse_pxefile(ctx, pxefile_addr_r);
1531 printf("Error parsing config file\n");
1538 handle_pxe_menu(ctx, cfg);
1540 destroy_pxe_menu(cfg);