1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2010-2011 Calxeda, Inc.
4 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
13 #include <linux/string.h>
14 #include <linux/ctype.h>
16 #include <linux/list.h>
24 #define MAX_TFTP_PATH_LEN 127
26 const char *pxe_default_paths[] = {
28 #ifdef CONFIG_SYS_BOARD
29 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC "-" CONFIG_SYS_BOARD,
31 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
33 "default-" CONFIG_SYS_ARCH,
42 * Convert an ethaddr from the environment to the format used by pxelinux
43 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
44 * the beginning of the ethernet address to indicate a hardware type of
45 * Ethernet. Also converts uppercase hex characters into lowercase, to match
46 * pxelinux's behavior.
48 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
49 * environment, or some other value < 0 on error.
51 static int format_mac_pxe(char *outbuf, size_t outbuf_len)
55 if (outbuf_len < 21) {
56 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
61 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
64 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
65 ethaddr[0], ethaddr[1], ethaddr[2],
66 ethaddr[3], ethaddr[4], ethaddr[5]);
73 * Returns the directory the file specified in the bootfile env variable is
74 * in. If bootfile isn't defined in the environment, return NULL, which should
75 * be interpreted as "don't prepend anything to paths".
77 static int get_bootfile_path(const char *file_path, char *bootfile_path,
78 size_t bootfile_path_size)
80 char *bootfile, *last_slash;
83 /* Only syslinux allows absolute paths */
84 if (file_path[0] == '/' && !is_pxe)
87 bootfile = from_env("bootfile");
92 last_slash = strrchr(bootfile, '/');
94 if (last_slash == NULL)
97 path_len = (last_slash - bootfile) + 1;
99 if (bootfile_path_size < path_len) {
100 printf("bootfile_path too small. (%zd < %zd)\n",
101 bootfile_path_size, path_len);
106 strncpy(bootfile_path, bootfile, path_len);
109 bootfile_path[path_len] = '\0';
114 static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
116 #ifdef CONFIG_CMD_NET
117 static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
119 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
121 tftp_argv[1] = file_addr;
122 tftp_argv[2] = (void *)file_path;
124 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
131 static char *fs_argv[5];
133 static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
135 #ifdef CONFIG_CMD_EXT2
136 fs_argv[0] = "ext2load";
137 fs_argv[3] = file_addr;
138 fs_argv[4] = (void *)file_path;
140 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
146 static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
148 #ifdef CONFIG_CMD_FAT
149 fs_argv[0] = "fatload";
150 fs_argv[3] = file_addr;
151 fs_argv[4] = (void *)file_path;
153 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
159 static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
161 #ifdef CONFIG_CMD_FS_GENERIC
163 fs_argv[3] = file_addr;
164 fs_argv[4] = (void *)file_path;
166 if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
173 * As in pxelinux, paths to files referenced from files we retrieve are
174 * relative to the location of bootfile. get_relfile takes such a path and
175 * joins it with the bootfile path to get the full path to the target file. If
176 * the bootfile path is NULL, we use file_path as is.
178 * Returns 1 for success, or < 0 on error.
180 static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path,
181 unsigned long file_addr)
184 char relfile[MAX_TFTP_PATH_LEN+1];
188 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
193 path_len = strlen(file_path);
194 path_len += strlen(relfile);
196 if (path_len > MAX_TFTP_PATH_LEN) {
197 printf("Base path too long (%s%s)\n",
201 return -ENAMETOOLONG;
204 strcat(relfile, file_path);
206 printf("Retrieving file: %s\n", relfile);
208 sprintf(addr_buf, "%lx", file_addr);
210 return do_getfile(cmdtp, relfile, addr_buf);
214 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
215 * 'bootfile' was specified in the environment, the path to bootfile will be
216 * prepended to 'file_path' and the resulting path will be used.
218 * Returns 1 on success, or < 0 for error.
220 static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path,
221 unsigned long file_addr)
223 unsigned long config_file_size;
228 err = get_relfile(cmdtp, file_path, file_addr);
234 * the file comes without a NUL byte at the end, so find out its size
235 * and add the NUL byte.
237 tftp_filesize = from_env("filesize");
242 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
245 buf = map_sysmem(file_addr + config_file_size, 1);
252 #ifdef CONFIG_CMD_NET
254 #define PXELINUX_DIR "pxelinux.cfg/"
257 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
258 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
259 * from the bootfile path, as described above.
261 * Returns 1 on success or < 0 on error.
263 static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file,
264 unsigned long pxefile_addr_r)
266 size_t base_len = strlen(PXELINUX_DIR);
267 char path[MAX_TFTP_PATH_LEN+1];
269 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
270 printf("path (%s%s) too long, skipping\n",
272 return -ENAMETOOLONG;
275 sprintf(path, PXELINUX_DIR "%s", file);
277 return get_pxe_file(cmdtp, path, pxefile_addr_r);
281 * Looks for a pxe file with a name based on the pxeuuid environment variable.
283 * Returns 1 on success or < 0 on error.
285 static int pxe_uuid_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
289 uuid_str = from_env("pxeuuid");
294 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
298 * Looks for a pxe file with a name based on the 'ethaddr' environment
301 * Returns 1 on success or < 0 on error.
303 static int pxe_mac_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
308 err = format_mac_pxe(mac_str, sizeof(mac_str));
313 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
317 * Looks for pxe files with names based on our IP address. See pxelinux
318 * documentation for details on what these file names look like. We match
321 * Returns 1 on success or < 0 on error.
323 static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
328 sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
330 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
331 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
336 ip_addr[mask_pos] = '\0';
343 * Entry point for the 'pxe get' command.
344 * This Follows pxelinux's rules to download a config file from a tftp server.
345 * The file is stored at the location given by the pxefile_addr_r environment
346 * variable, which must be set.
348 * UUID comes from pxeuuid env variable, if defined
349 * MAC addr comes from ethaddr env variable, if defined
352 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
354 * Returns 0 on success or 1 on error.
357 do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
359 char *pxefile_addr_str;
360 unsigned long pxefile_addr_r;
363 do_getfile = do_get_tftp;
366 return CMD_RET_USAGE;
368 pxefile_addr_str = from_env("pxefile_addr_r");
370 if (!pxefile_addr_str)
373 err = strict_strtoul(pxefile_addr_str, 16,
374 (unsigned long *)&pxefile_addr_r);
379 * Keep trying paths until we successfully get a file we're looking
382 if (pxe_uuid_path(cmdtp, pxefile_addr_r) > 0 ||
383 pxe_mac_path(cmdtp, pxefile_addr_r) > 0 ||
384 pxe_ipaddr_paths(cmdtp, pxefile_addr_r) > 0) {
385 printf("Config file found\n");
390 while (pxe_default_paths[i]) {
391 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
392 pxefile_addr_r) > 0) {
393 printf("Config file found\n");
399 printf("Config file not found\n");
406 * Wrapper to make it easier to store the file at file_path in the location
407 * specified by envaddr_name. file_path will be joined to the bootfile path,
408 * if any is specified.
410 * Returns 1 on success or < 0 on error.
412 static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
414 unsigned long file_addr;
417 envaddr = from_env(envaddr_name);
422 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
425 return get_relfile(cmdtp, file_path, file_addr);
429 * A note on the pxe file parser.
431 * We're parsing files that use syslinux grammar, which has a few quirks.
432 * String literals must be recognized based on context - there is no
433 * quoting or escaping support. There's also nothing to explicitly indicate
434 * when a label section completes. We deal with that by ending a label
435 * section whenever we see a line that doesn't include.
437 * As with the syslinux family, this same file format could be reused in the
438 * future for non pxe purposes. The only action it takes during parsing that
439 * would throw this off is handling of include files. It assumes we're using
440 * pxe, and does a tftp download of a file listed as an include file in the
441 * middle of the parsing operation. That could be handled by refactoring it to
442 * take a 'include file getter' function.
446 * Describes a single label given in a pxe file.
448 * Create these with the 'label_create' function given below.
450 * name - the name of the menu as given on the 'menu label' line.
451 * kernel - the path to the kernel file to use for this label.
452 * append - kernel command line to use when booting this label
453 * initrd - path to the initrd to use for this label.
454 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
455 * localboot - 1 if this label specified 'localboot', 0 otherwise.
456 * list - lets these form a list, which a pxe_menu struct will hold.
472 struct list_head list;
476 * Describes a pxe menu as given via pxe files.
478 * title - the name of the menu as given by a 'menu title' line.
479 * default_label - the name of the default label, if any.
480 * bmp - the bmp file name which is displayed in background
481 * timeout - time in tenths of a second to wait for a user key-press before
482 * booting the default label.
483 * prompt - if 0, don't prompt for a choice unless the timeout period is
484 * interrupted. If 1, always prompt for a choice regardless of
486 * labels - a list of labels defined for the menu.
494 struct list_head labels;
498 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
499 * result must be free()'d to reclaim the memory.
501 * Returns NULL if malloc fails.
503 static struct pxe_label *label_create(void)
505 struct pxe_label *label;
507 label = malloc(sizeof(struct pxe_label));
512 memset(label, 0, sizeof(struct pxe_label));
518 * Free the memory used by a pxe_label, including that used by its name,
519 * kernel, append and initrd members, if they're non NULL.
521 * So - be sure to only use dynamically allocated memory for the members of
522 * the pxe_label struct, unless you want to clean it up first. These are
523 * currently only created by the pxe file parsing code.
525 static void label_destroy(struct pxe_label *label)
552 * Print a label and its string members if they're defined.
554 * This is passed as a callback to the menu code for displaying each
557 static void label_print(void *data)
559 struct pxe_label *label = data;
560 const char *c = label->menu ? label->menu : label->name;
562 printf("%s:\t%s\n", label->num, c);
566 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
567 * environment variable is defined. Its contents will be executed as U-Boot
568 * command. If the label specified an 'append' line, its contents will be
569 * used to overwrite the contents of the 'bootargs' environment variable prior
570 * to running 'localcmd'.
572 * Returns 1 on success or < 0 on error.
574 static int label_localboot(struct pxe_label *label)
578 localcmd = from_env("localcmd");
584 char bootargs[CONFIG_SYS_CBSIZE];
586 cli_simple_process_macros(label->append, bootargs);
587 env_set("bootargs", bootargs);
590 debug("running: %s\n", localcmd);
592 return run_command_list(localcmd, strlen(localcmd), 0);
596 * Boot according to the contents of a pxe_label.
598 * If we can't boot for any reason, we return. A successful boot never
601 * The kernel will be stored in the location given by the 'kernel_addr_r'
602 * environment variable.
604 * If the label specifies an initrd file, it will be stored in the location
605 * given by the 'ramdisk_addr_r' environment variable.
607 * If the label specifies an 'append' line, its contents will overwrite that
608 * of the 'bootargs' environment variable.
610 static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
612 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
614 char mac_str[29] = "";
615 char ip_str[68] = "";
616 char *fit_addr = NULL;
624 label->attempted = 1;
626 if (label->localboot) {
627 if (label->localboot_val >= 0)
628 label_localboot(label);
632 if (label->kernel == NULL) {
633 printf("No kernel given, skipping %s\n",
639 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
640 printf("Skipping %s for failure retrieving initrd\n",
645 bootm_argv[2] = initrd_str;
646 strncpy(bootm_argv[2], env_get("ramdisk_addr_r"), 18);
647 strcat(bootm_argv[2], ":");
648 strncat(bootm_argv[2], env_get("filesize"), 9);
652 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
653 printf("Skipping %s for failure retrieving kernel\n",
658 if (label->ipappend & 0x1) {
659 sprintf(ip_str, " ip=%s:%s:%s:%s",
660 env_get("ipaddr"), env_get("serverip"),
661 env_get("gatewayip"), env_get("netmask"));
664 #ifdef CONFIG_CMD_NET
665 if (label->ipappend & 0x2) {
667 strcpy(mac_str, " BOOTIF=");
668 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
674 if ((label->ipappend & 0x3) || label->append) {
675 char bootargs[CONFIG_SYS_CBSIZE] = "";
676 char finalbootargs[CONFIG_SYS_CBSIZE];
678 if (strlen(label->append ?: "") +
679 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
680 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
681 strlen(label->append ?: ""),
682 strlen(ip_str), strlen(mac_str),
687 strncpy(bootargs, label->append,
689 strcat(bootargs, ip_str);
690 strcat(bootargs, mac_str);
692 cli_simple_process_macros(bootargs, finalbootargs);
693 env_set("bootargs", finalbootargs);
694 printf("append: %s\n", finalbootargs);
698 bootm_argv[1] = env_get("kernel_addr_r");
699 /* for FIT, append the configuration identifier */
701 int len = strlen(bootm_argv[1]) + strlen(label->config) + 1;
703 fit_addr = malloc(len);
705 printf("malloc fail (FIT address)\n");
708 snprintf(fit_addr, len, "%s%s", bootm_argv[1], label->config);
709 bootm_argv[1] = fit_addr;
713 * fdt usage is optional:
714 * It handles the following scenarios. All scenarios are exclusive
716 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
717 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
718 * and adjust argc appropriately.
720 * Scenario 2: If there is an fdt_addr specified, pass it along to
721 * bootm, and adjust argc appropriately.
723 * Scenario 3: fdt blob is not available.
725 bootm_argv[3] = env_get("fdt_addr_r");
727 /* if fdt label is defined then get fdt from server */
729 char *fdtfile = NULL;
730 char *fdtfilefree = NULL;
733 fdtfile = label->fdt;
734 } else if (label->fdtdir) {
735 char *f1, *f2, *f3, *f4, *slash;
737 f1 = env_get("fdtfile");
744 * For complex cases where this code doesn't
745 * generate the correct filename, the board
746 * code should set $fdtfile during early boot,
747 * or the boot scripts should set $fdtfile
748 * before invoking "pxe" or "sysboot".
752 f3 = env_get("board");
756 len = strlen(label->fdtdir);
759 else if (label->fdtdir[len - 1] != '/')
764 len = strlen(label->fdtdir) + strlen(slash) +
765 strlen(f1) + strlen(f2) + strlen(f3) +
767 fdtfilefree = malloc(len);
769 printf("malloc fail (FDT filename)\n");
773 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
774 label->fdtdir, slash, f1, f2, f3, f4);
775 fdtfile = fdtfilefree;
779 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
782 printf("Skipping %s for failure retrieving fdt\n",
787 bootm_argv[3] = NULL;
792 bootm_argv[3] = env_get("fdt_addr");
800 kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
801 buf = map_sysmem(kernel_addr, 0);
802 /* Try bootm for legacy and FIT format image */
803 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
804 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
805 #ifdef CONFIG_CMD_BOOTI
806 /* Try booting an AArch64 Linux kernel image */
808 do_booti(cmdtp, 0, bootm_argc, bootm_argv);
809 #elif defined(CONFIG_CMD_BOOTZ)
810 /* Try booting a Image */
812 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
823 * Tokens for the pxe file parser.
850 * A token - given by a value and a type.
854 enum token_type type;
858 * Keywords recognized.
860 static const struct token keywords[] = {
863 {"timeout", T_TIMEOUT},
864 {"default", T_DEFAULT},
865 {"prompt", T_PROMPT},
867 {"kernel", T_KERNEL},
869 {"localboot", T_LOCALBOOT},
870 {"append", T_APPEND},
871 {"initrd", T_INITRD},
872 {"include", T_INCLUDE},
873 {"devicetree", T_FDT},
875 {"devicetreedir", T_FDTDIR},
876 {"fdtdir", T_FDTDIR},
877 {"ontimeout", T_ONTIMEOUT,},
878 {"ipappend", T_IPAPPEND,},
879 {"background", T_BACKGROUND,},
884 * Since pxe(linux) files don't have a token to identify the start of a
885 * literal, we have to keep track of when we're in a state where a literal is
886 * expected vs when we're in a state a keyword is expected.
895 * get_string retrieves a string from *p and stores it as a token in
898 * get_string used for scanning both string literals and keywords.
900 * Characters from *p are copied into t-val until a character equal to
901 * delim is found, or a NUL byte is reached. If delim has the special value of
902 * ' ', any whitespace character will be used as a delimiter.
904 * If lower is unequal to 0, uppercase characters will be converted to
905 * lowercase in the result. This is useful to make keywords case
908 * The location of *p is updated to point to the first character after the end
909 * of the token - the ending delimiter.
911 * On success, the new value of t->val is returned. Memory for t->val is
912 * allocated using malloc and must be free()'d to reclaim it. If insufficient
913 * memory is available, NULL is returned.
915 static char *get_string(char **p, struct token *t, char delim, int lower)
921 * b and e both start at the beginning of the input stream.
923 * e is incremented until we find the ending delimiter, or a NUL byte
924 * is reached. Then, we take e - b to find the length of the token.
929 if ((delim == ' ' && isspace(*e)) || delim == *e)
937 * Allocate memory to hold the string, and copy it in, converting
938 * characters to lowercase if lower is != 0.
940 t->val = malloc(len + 1);
944 for (i = 0; i < len; i++, b++) {
946 t->val[i] = tolower(*b);
954 * Update *p so the caller knows where to continue scanning.
964 * Populate a keyword token with a type and value.
966 static void get_keyword(struct token *t)
970 for (i = 0; keywords[i].val; i++) {
971 if (!strcmp(t->val, keywords[i].val)) {
972 t->type = keywords[i].type;
979 * Get the next token. We have to keep track of which state we're in to know
980 * if we're looking to get a string literal or a keyword.
982 * *p is updated to point at the first character after the current token.
984 static void get_token(char **p, struct token *t, enum lex_state state)
990 /* eat non EOL whitespace */
995 * eat comments. note that string literals can't begin with #, but
996 * can contain a # after their first character.
999 while (*c && *c != '\n')
1006 } else if (*c == '\0') {
1009 } else if (state == L_SLITERAL) {
1010 get_string(&c, t, '\n', 0);
1011 } else if (state == L_KEYWORD) {
1013 * when we expect a keyword, we first get the next string
1014 * token delimited by whitespace, and then check if it
1015 * matches a keyword in our keyword list. if it does, it's
1016 * converted to a keyword token of the appropriate type, and
1017 * if not, it remains a string token.
1019 get_string(&c, t, ' ', 1);
1027 * Increment *c until we get to the end of the current line, or EOF.
1029 static void eol_or_eof(char **c)
1031 while (**c && **c != '\n')
1036 * All of these parse_* functions share some common behavior.
1038 * They finish with *c pointing after the token they parse, and return 1 on
1039 * success, or < 0 on error.
1043 * Parse a string literal and store a pointer it at *dst. String literals
1044 * terminate at the end of the line.
1046 static int parse_sliteral(char **c, char **dst)
1051 get_token(c, &t, L_SLITERAL);
1053 if (t.type != T_STRING) {
1054 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1064 * Parse a base 10 (unsigned) integer and store it at *dst.
1066 static int parse_integer(char **c, int *dst)
1071 get_token(c, &t, L_SLITERAL);
1073 if (t.type != T_STRING) {
1074 printf("Expected string: %.*s\n", (int)(*c - s), s);
1078 *dst = simple_strtol(t.val, NULL, 10);
1085 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
1086 struct pxe_menu *cfg, int nest_level);
1089 * Parse an include statement, and retrieve and parse the file it mentions.
1091 * base should point to a location where it's safe to store the file, and
1092 * nest_level should indicate how many nested includes have occurred. For this
1093 * include, nest_level has already been incremented and doesn't need to be
1096 static int handle_include(cmd_tbl_t *cmdtp, char **c, unsigned long base,
1097 struct pxe_menu *cfg, int nest_level)
1105 err = parse_sliteral(c, &include_path);
1108 printf("Expected include path: %.*s\n",
1113 err = get_pxe_file(cmdtp, include_path, base);
1116 printf("Couldn't retrieve %s\n", include_path);
1120 buf = map_sysmem(base, 0);
1121 ret = parse_pxefile_top(cmdtp, buf, base, cfg, nest_level);
1128 * Parse lines that begin with 'menu'.
1130 * base and nest are provided to handle the 'menu include' case.
1132 * base should point to a location where it's safe to store the included file.
1134 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1135 * a file it includes, 3 when parsing a file included by that file, and so on.
1137 static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg,
1138 unsigned long base, int nest_level)
1144 get_token(c, &t, L_KEYWORD);
1148 err = parse_sliteral(c, &cfg->title);
1153 err = handle_include(cmdtp, c, base, cfg,
1158 err = parse_sliteral(c, &cfg->bmp);
1162 printf("Ignoring malformed menu command: %.*s\n",
1175 * Handles parsing a 'menu line' when we're parsing a label.
1177 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1178 struct pxe_label *label)
1185 get_token(c, &t, L_KEYWORD);
1189 if (!cfg->default_label)
1190 cfg->default_label = strdup(label->name);
1192 if (!cfg->default_label)
1197 parse_sliteral(c, &label->menu);
1200 printf("Ignoring malformed menu command: %.*s\n",
1210 * Handles parsing a 'kernel' label.
1211 * expecting "filename" or "<fit_filename>#cfg"
1213 static int parse_label_kernel(char **c, struct pxe_label *label)
1218 err = parse_sliteral(c, &label->kernel);
1222 s = strstr(label->kernel, "#");
1226 label->config = malloc(strlen(s) + 1);
1230 strcpy(label->config, s);
1237 * Parses a label and adds it to the list of labels for a menu.
1239 * A label ends when we either get to the end of a file, or
1240 * get some input we otherwise don't have a handler defined
1244 static int parse_label(char **c, struct pxe_menu *cfg)
1249 struct pxe_label *label;
1252 label = label_create();
1256 err = parse_sliteral(c, &label->name);
1258 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1259 label_destroy(label);
1263 list_add_tail(&label->list, &cfg->labels);
1267 get_token(c, &t, L_KEYWORD);
1272 err = parse_label_menu(c, cfg, label);
1277 err = parse_label_kernel(c, label);
1281 err = parse_sliteral(c, &label->append);
1284 s = strstr(label->append, "initrd=");
1288 len = (int)(strchr(s, ' ') - s);
1289 label->initrd = malloc(len + 1);
1290 strncpy(label->initrd, s, len);
1291 label->initrd[len] = '\0';
1297 err = parse_sliteral(c, &label->initrd);
1302 err = parse_sliteral(c, &label->fdt);
1307 err = parse_sliteral(c, &label->fdtdir);
1311 label->localboot = 1;
1312 err = parse_integer(c, &label->localboot_val);
1316 err = parse_integer(c, &label->ipappend);
1324 * put the token back! we don't want it - it's the end
1325 * of a label and whatever token this is, it's
1326 * something for the menu level context to handle.
1338 * This 16 comes from the limit pxelinux imposes on nested includes.
1340 * There is no reason at all we couldn't do more, but some limit helps prevent
1341 * infinite (until crash occurs) recursion if a file tries to include itself.
1343 #define MAX_NEST_LEVEL 16
1346 * Entry point for parsing a menu file. nest_level indicates how many times
1347 * we've nested in includes. It will be 1 for the top level menu file.
1349 * Returns 1 on success, < 0 on error.
1351 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
1352 struct pxe_menu *cfg, int nest_level)
1355 char *s, *b, *label_name;
1360 if (nest_level > MAX_NEST_LEVEL) {
1361 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1368 get_token(&p, &t, L_KEYWORD);
1374 err = parse_menu(cmdtp, &p, cfg,
1375 base + ALIGN(strlen(b) + 1, 4),
1380 err = parse_integer(&p, &cfg->timeout);
1384 err = parse_label(&p, cfg);
1389 err = parse_sliteral(&p, &label_name);
1392 if (cfg->default_label)
1393 free(cfg->default_label);
1395 cfg->default_label = label_name;
1401 err = handle_include(cmdtp, &p,
1402 base + ALIGN(strlen(b), 4), cfg,
1417 printf("Ignoring unknown command: %.*s\n",
1428 * Free the memory used by a pxe_menu and its labels.
1430 static void destroy_pxe_menu(struct pxe_menu *cfg)
1432 struct list_head *pos, *n;
1433 struct pxe_label *label;
1438 if (cfg->default_label)
1439 free(cfg->default_label);
1441 list_for_each_safe(pos, n, &cfg->labels) {
1442 label = list_entry(pos, struct pxe_label, list);
1444 label_destroy(label);
1451 * Entry point for parsing a pxe file. This is only used for the top level
1454 * Returns NULL if there is an error, otherwise, returns a pointer to a
1455 * pxe_menu struct populated with the results of parsing the pxe file (and any
1456 * files it includes). The resulting pxe_menu struct can be free()'d by using
1457 * the destroy_pxe_menu() function.
1459 static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, unsigned long menucfg)
1461 struct pxe_menu *cfg;
1465 cfg = malloc(sizeof(struct pxe_menu));
1470 memset(cfg, 0, sizeof(struct pxe_menu));
1472 INIT_LIST_HEAD(&cfg->labels);
1474 buf = map_sysmem(menucfg, 0);
1475 r = parse_pxefile_top(cmdtp, buf, menucfg, cfg, 1);
1479 destroy_pxe_menu(cfg);
1487 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1490 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1492 struct pxe_label *label;
1493 struct list_head *pos;
1497 char *default_num = NULL;
1500 * Create a menu and add items for all the labels.
1502 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
1503 cfg->prompt, label_print, NULL, NULL);
1508 list_for_each(pos, &cfg->labels) {
1509 label = list_entry(pos, struct pxe_label, list);
1511 sprintf(label->num, "%d", i++);
1512 if (menu_item_add(m, label->num, label) != 1) {
1516 if (cfg->default_label &&
1517 (strcmp(label->name, cfg->default_label) == 0))
1518 default_num = label->num;
1523 * After we've created items for each label in the menu, set the
1524 * menu's default label if one was specified.
1527 err = menu_default_set(m, default_num);
1529 if (err != -ENOENT) {
1534 printf("Missing default: %s\n", cfg->default_label);
1542 * Try to boot any labels we have yet to attempt to boot.
1544 static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1546 struct list_head *pos;
1547 struct pxe_label *label;
1549 list_for_each(pos, &cfg->labels) {
1550 label = list_entry(pos, struct pxe_label, list);
1552 if (!label->attempted)
1553 label_boot(cmdtp, label);
1558 * Boot the system as prescribed by a pxe_menu.
1560 * Use the menu system to either get the user's choice or the default, based
1561 * on config or user input. If there is no default or user's choice,
1562 * attempted to boot labels in the order they were given in pxe files.
1563 * If the default or user's choice fails to boot, attempt to boot other
1564 * labels in the order they were given in pxe files.
1566 * If this function returns, there weren't any labels that successfully
1567 * booted, or the user interrupted the menu selection via ctrl+c.
1569 static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1575 #ifdef CONFIG_CMD_BMP
1576 /* display BMP if available */
1578 if (get_relfile(cmdtp, cfg->bmp, load_addr)) {
1579 run_command("cls", 0);
1580 bmp_display(load_addr,
1581 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1583 printf("Skipping background bmp %s for failure\n",
1589 m = pxe_menu_to_menu(cfg);
1593 err = menu_get_choice(m, &choice);
1598 * err == 1 means we got a choice back from menu_get_choice.
1600 * err == -ENOENT if the menu was setup to select the default but no
1601 * default was set. in that case, we should continue trying to boot
1602 * labels that haven't been attempted yet.
1604 * otherwise, the user interrupted or there was some other error and
1609 err = label_boot(cmdtp, choice);
1612 } else if (err != -ENOENT) {
1616 boot_unattempted_labels(cmdtp, cfg);
1619 #ifdef CONFIG_CMD_NET
1621 * Boots a system using a pxe file
1623 * Returns 0 on success, 1 on error.
1626 do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1628 unsigned long pxefile_addr_r;
1629 struct pxe_menu *cfg;
1630 char *pxefile_addr_str;
1632 do_getfile = do_get_tftp;
1635 pxefile_addr_str = from_env("pxefile_addr_r");
1636 if (!pxefile_addr_str)
1639 } else if (argc == 2) {
1640 pxefile_addr_str = argv[1];
1642 return CMD_RET_USAGE;
1645 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1646 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1650 cfg = parse_pxefile(cmdtp, pxefile_addr_r);
1653 printf("Error parsing config file\n");
1657 handle_pxe_menu(cmdtp, cfg);
1659 destroy_pxe_menu(cfg);
1661 copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
1666 static cmd_tbl_t cmd_pxe_sub[] = {
1667 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1668 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1671 static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1676 return CMD_RET_USAGE;
1680 /* drop initial "pxe" arg */
1684 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1687 return cp->cmd(cmdtp, flag, argc, argv);
1689 return CMD_RET_USAGE;
1694 "commands to get and boot from pxe files",
1695 "get - try to retrieve a pxe file using tftp\npxe "
1696 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1701 * Boots a system using a local disk syslinux/extlinux file
1703 * Returns 0 on success, 1 on error.
1705 static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1707 unsigned long pxefile_addr_r;
1708 struct pxe_menu *cfg;
1709 char *pxefile_addr_str;
1715 if (argc > 1 && strstr(argv[1], "-p")) {
1722 return cmd_usage(cmdtp);
1725 pxefile_addr_str = from_env("pxefile_addr_r");
1726 if (!pxefile_addr_str)
1729 pxefile_addr_str = argv[4];
1733 filename = env_get("bootfile");
1736 env_set("bootfile", filename);
1739 if (strstr(argv[3], "ext2"))
1740 do_getfile = do_get_ext2;
1741 else if (strstr(argv[3], "fat"))
1742 do_getfile = do_get_fat;
1743 else if (strstr(argv[3], "any"))
1744 do_getfile = do_get_any;
1746 printf("Invalid filesystem: %s\n", argv[3]);
1749 fs_argv[1] = argv[1];
1750 fs_argv[2] = argv[2];
1752 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1753 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1757 if (get_pxe_file(cmdtp, filename, pxefile_addr_r) < 0) {
1758 printf("Error reading config file\n");
1762 cfg = parse_pxefile(cmdtp, pxefile_addr_r);
1765 printf("Error parsing config file\n");
1772 handle_pxe_menu(cmdtp, cfg);
1774 destroy_pxe_menu(cfg);
1780 sysboot, 7, 1, do_sysboot,
1781 "command to get and boot from syslinux files",
1782 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1783 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1784 " or any filesystem on 'dev' on 'interface' to address 'addr'"