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,
41 * Like env_get, but prints an error if envvar isn't defined in the
42 * environment. It always returns what env_get does, so it can be used in
43 * place of env_get without changing error handling otherwise.
45 static char *from_env(const char *envvar)
49 ret = env_get(envvar);
52 printf("missing environment variable: %s\n", envvar);
59 * Convert an ethaddr from the environment to the format used by pxelinux
60 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
61 * the beginning of the ethernet address to indicate a hardware type of
62 * Ethernet. Also converts uppercase hex characters into lowercase, to match
63 * pxelinux's behavior.
65 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
66 * environment, or some other value < 0 on error.
68 static int format_mac_pxe(char *outbuf, size_t outbuf_len)
72 if (outbuf_len < 21) {
73 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
78 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
81 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
82 ethaddr[0], ethaddr[1], ethaddr[2],
83 ethaddr[3], ethaddr[4], ethaddr[5]);
90 * Returns the directory the file specified in the bootfile env variable is
91 * in. If bootfile isn't defined in the environment, return NULL, which should
92 * be interpreted as "don't prepend anything to paths".
94 static int get_bootfile_path(const char *file_path, char *bootfile_path,
95 size_t bootfile_path_size)
97 char *bootfile, *last_slash;
100 /* Only syslinux allows absolute paths */
101 if (file_path[0] == '/' && !is_pxe)
104 bootfile = from_env("bootfile");
109 last_slash = strrchr(bootfile, '/');
111 if (last_slash == NULL)
114 path_len = (last_slash - bootfile) + 1;
116 if (bootfile_path_size < path_len) {
117 printf("bootfile_path too small. (%zd < %zd)\n",
118 bootfile_path_size, path_len);
123 strncpy(bootfile_path, bootfile, path_len);
126 bootfile_path[path_len] = '\0';
131 static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
133 #ifdef CONFIG_CMD_NET
134 static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
136 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
138 tftp_argv[1] = file_addr;
139 tftp_argv[2] = (void *)file_path;
141 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
148 static char *fs_argv[5];
150 static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
152 #ifdef CONFIG_CMD_EXT2
153 fs_argv[0] = "ext2load";
154 fs_argv[3] = file_addr;
155 fs_argv[4] = (void *)file_path;
157 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
163 static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
165 #ifdef CONFIG_CMD_FAT
166 fs_argv[0] = "fatload";
167 fs_argv[3] = file_addr;
168 fs_argv[4] = (void *)file_path;
170 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
176 static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
178 #ifdef CONFIG_CMD_FS_GENERIC
180 fs_argv[3] = file_addr;
181 fs_argv[4] = (void *)file_path;
183 if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
190 * As in pxelinux, paths to files referenced from files we retrieve are
191 * relative to the location of bootfile. get_relfile takes such a path and
192 * joins it with the bootfile path to get the full path to the target file. If
193 * the bootfile path is NULL, we use file_path as is.
195 * Returns 1 for success, or < 0 on error.
197 static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path,
198 unsigned long file_addr)
201 char relfile[MAX_TFTP_PATH_LEN+1];
205 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
210 path_len = strlen(file_path);
211 path_len += strlen(relfile);
213 if (path_len > MAX_TFTP_PATH_LEN) {
214 printf("Base path too long (%s%s)\n",
218 return -ENAMETOOLONG;
221 strcat(relfile, file_path);
223 printf("Retrieving file: %s\n", relfile);
225 sprintf(addr_buf, "%lx", file_addr);
227 return do_getfile(cmdtp, relfile, addr_buf);
231 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
232 * 'bootfile' was specified in the environment, the path to bootfile will be
233 * prepended to 'file_path' and the resulting path will be used.
235 * Returns 1 on success, or < 0 for error.
237 static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path,
238 unsigned long file_addr)
240 unsigned long config_file_size;
245 err = get_relfile(cmdtp, file_path, file_addr);
251 * the file comes without a NUL byte at the end, so find out its size
252 * and add the NUL byte.
254 tftp_filesize = from_env("filesize");
259 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
262 buf = map_sysmem(file_addr + config_file_size, 1);
269 #ifdef CONFIG_CMD_NET
271 #define PXELINUX_DIR "pxelinux.cfg/"
274 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
275 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
276 * from the bootfile path, as described above.
278 * Returns 1 on success or < 0 on error.
280 static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file,
281 unsigned long pxefile_addr_r)
283 size_t base_len = strlen(PXELINUX_DIR);
284 char path[MAX_TFTP_PATH_LEN+1];
286 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
287 printf("path (%s%s) too long, skipping\n",
289 return -ENAMETOOLONG;
292 sprintf(path, PXELINUX_DIR "%s", file);
294 return get_pxe_file(cmdtp, path, pxefile_addr_r);
298 * Looks for a pxe file with a name based on the pxeuuid environment variable.
300 * Returns 1 on success or < 0 on error.
302 static int pxe_uuid_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
306 uuid_str = from_env("pxeuuid");
311 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
315 * Looks for a pxe file with a name based on the 'ethaddr' environment
318 * Returns 1 on success or < 0 on error.
320 static int pxe_mac_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
325 err = format_mac_pxe(mac_str, sizeof(mac_str));
330 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
334 * Looks for pxe files with names based on our IP address. See pxelinux
335 * documentation for details on what these file names look like. We match
338 * Returns 1 on success or < 0 on error.
340 static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
345 sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
347 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
348 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
353 ip_addr[mask_pos] = '\0';
360 * Entry point for the 'pxe get' command.
361 * This Follows pxelinux's rules to download a config file from a tftp server.
362 * The file is stored at the location given by the pxefile_addr_r environment
363 * variable, which must be set.
365 * UUID comes from pxeuuid env variable, if defined
366 * MAC addr comes from ethaddr env variable, if defined
369 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
371 * Returns 0 on success or 1 on error.
374 do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
376 char *pxefile_addr_str;
377 unsigned long pxefile_addr_r;
380 do_getfile = do_get_tftp;
383 return CMD_RET_USAGE;
385 pxefile_addr_str = from_env("pxefile_addr_r");
387 if (!pxefile_addr_str)
390 err = strict_strtoul(pxefile_addr_str, 16,
391 (unsigned long *)&pxefile_addr_r);
396 * Keep trying paths until we successfully get a file we're looking
399 if (pxe_uuid_path(cmdtp, pxefile_addr_r) > 0 ||
400 pxe_mac_path(cmdtp, pxefile_addr_r) > 0 ||
401 pxe_ipaddr_paths(cmdtp, pxefile_addr_r) > 0) {
402 printf("Config file found\n");
407 while (pxe_default_paths[i]) {
408 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
409 pxefile_addr_r) > 0) {
410 printf("Config file found\n");
416 printf("Config file not found\n");
423 * Wrapper to make it easier to store the file at file_path in the location
424 * specified by envaddr_name. file_path will be joined to the bootfile path,
425 * if any is specified.
427 * Returns 1 on success or < 0 on error.
429 static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
431 unsigned long file_addr;
434 envaddr = from_env(envaddr_name);
439 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
442 return get_relfile(cmdtp, file_path, file_addr);
446 * A note on the pxe file parser.
448 * We're parsing files that use syslinux grammar, which has a few quirks.
449 * String literals must be recognized based on context - there is no
450 * quoting or escaping support. There's also nothing to explicitly indicate
451 * when a label section completes. We deal with that by ending a label
452 * section whenever we see a line that doesn't include.
454 * As with the syslinux family, this same file format could be reused in the
455 * future for non pxe purposes. The only action it takes during parsing that
456 * would throw this off is handling of include files. It assumes we're using
457 * pxe, and does a tftp download of a file listed as an include file in the
458 * middle of the parsing operation. That could be handled by refactoring it to
459 * take a 'include file getter' function.
463 * Describes a single label given in a pxe file.
465 * Create these with the 'label_create' function given below.
467 * name - the name of the menu as given on the 'menu label' line.
468 * kernel - the path to the kernel file to use for this label.
469 * append - kernel command line to use when booting this label
470 * initrd - path to the initrd to use for this label.
471 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
472 * localboot - 1 if this label specified 'localboot', 0 otherwise.
473 * list - lets these form a list, which a pxe_menu struct will hold.
489 struct list_head list;
493 * Describes a pxe menu as given via pxe files.
495 * title - the name of the menu as given by a 'menu title' line.
496 * default_label - the name of the default label, if any.
497 * bmp - the bmp file name which is displayed in background
498 * timeout - time in tenths of a second to wait for a user key-press before
499 * booting the default label.
500 * prompt - if 0, don't prompt for a choice unless the timeout period is
501 * interrupted. If 1, always prompt for a choice regardless of
503 * labels - a list of labels defined for the menu.
511 struct list_head labels;
515 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
516 * result must be free()'d to reclaim the memory.
518 * Returns NULL if malloc fails.
520 static struct pxe_label *label_create(void)
522 struct pxe_label *label;
524 label = malloc(sizeof(struct pxe_label));
529 memset(label, 0, sizeof(struct pxe_label));
535 * Free the memory used by a pxe_label, including that used by its name,
536 * kernel, append and initrd members, if they're non NULL.
538 * So - be sure to only use dynamically allocated memory for the members of
539 * the pxe_label struct, unless you want to clean it up first. These are
540 * currently only created by the pxe file parsing code.
542 static void label_destroy(struct pxe_label *label)
569 * Print a label and its string members if they're defined.
571 * This is passed as a callback to the menu code for displaying each
574 static void label_print(void *data)
576 struct pxe_label *label = data;
577 const char *c = label->menu ? label->menu : label->name;
579 printf("%s:\t%s\n", label->num, c);
583 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
584 * environment variable is defined. Its contents will be executed as U-Boot
585 * command. If the label specified an 'append' line, its contents will be
586 * used to overwrite the contents of the 'bootargs' environment variable prior
587 * to running 'localcmd'.
589 * Returns 1 on success or < 0 on error.
591 static int label_localboot(struct pxe_label *label)
595 localcmd = from_env("localcmd");
601 char bootargs[CONFIG_SYS_CBSIZE];
603 cli_simple_process_macros(label->append, bootargs);
604 env_set("bootargs", bootargs);
607 debug("running: %s\n", localcmd);
609 return run_command_list(localcmd, strlen(localcmd), 0);
613 * Boot according to the contents of a pxe_label.
615 * If we can't boot for any reason, we return. A successful boot never
618 * The kernel will be stored in the location given by the 'kernel_addr_r'
619 * environment variable.
621 * If the label specifies an initrd file, it will be stored in the location
622 * given by the 'ramdisk_addr_r' environment variable.
624 * If the label specifies an 'append' line, its contents will overwrite that
625 * of the 'bootargs' environment variable.
627 static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
629 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
631 char mac_str[29] = "";
632 char ip_str[68] = "";
633 char *fit_addr = NULL;
641 label->attempted = 1;
643 if (label->localboot) {
644 if (label->localboot_val >= 0)
645 label_localboot(label);
649 if (label->kernel == NULL) {
650 printf("No kernel given, skipping %s\n",
656 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
657 printf("Skipping %s for failure retrieving initrd\n",
662 bootm_argv[2] = initrd_str;
663 strncpy(bootm_argv[2], env_get("ramdisk_addr_r"), 18);
664 strcat(bootm_argv[2], ":");
665 strncat(bootm_argv[2], env_get("filesize"), 9);
668 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
669 printf("Skipping %s for failure retrieving kernel\n",
674 if (label->ipappend & 0x1) {
675 sprintf(ip_str, " ip=%s:%s:%s:%s",
676 env_get("ipaddr"), env_get("serverip"),
677 env_get("gatewayip"), env_get("netmask"));
680 #ifdef CONFIG_CMD_NET
681 if (label->ipappend & 0x2) {
683 strcpy(mac_str, " BOOTIF=");
684 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
690 if ((label->ipappend & 0x3) || label->append) {
691 char bootargs[CONFIG_SYS_CBSIZE] = "";
692 char finalbootargs[CONFIG_SYS_CBSIZE];
694 if (strlen(label->append ?: "") +
695 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
696 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
697 strlen(label->append ?: ""),
698 strlen(ip_str), strlen(mac_str),
703 strncpy(bootargs, label->append,
705 strcat(bootargs, ip_str);
706 strcat(bootargs, mac_str);
708 cli_simple_process_macros(bootargs, finalbootargs);
709 env_set("bootargs", finalbootargs);
710 printf("append: %s\n", finalbootargs);
714 bootm_argv[1] = env_get("kernel_addr_r");
715 /* for FIT, append the configuration identifier */
717 int len = strlen(bootm_argv[1]) + strlen(label->config) + 1;
719 fit_addr = malloc(len);
721 printf("malloc fail (FIT address)\n");
724 snprintf(fit_addr, len, "%s%s", bootm_argv[1], label->config);
725 bootm_argv[1] = fit_addr;
729 * fdt usage is optional:
730 * It handles the following scenarios. All scenarios are exclusive
732 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
733 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
734 * and adjust argc appropriately.
736 * Scenario 2: If there is an fdt_addr specified, pass it along to
737 * bootm, and adjust argc appropriately.
739 * Scenario 3: fdt blob is not available.
741 bootm_argv[3] = env_get("fdt_addr_r");
743 /* if fdt label is defined then get fdt from server */
745 char *fdtfile = NULL;
746 char *fdtfilefree = NULL;
749 fdtfile = label->fdt;
750 } else if (label->fdtdir) {
751 char *f1, *f2, *f3, *f4, *slash;
753 f1 = env_get("fdtfile");
760 * For complex cases where this code doesn't
761 * generate the correct filename, the board
762 * code should set $fdtfile during early boot,
763 * or the boot scripts should set $fdtfile
764 * before invoking "pxe" or "sysboot".
768 f3 = env_get("board");
772 len = strlen(label->fdtdir);
775 else if (label->fdtdir[len - 1] != '/')
780 len = strlen(label->fdtdir) + strlen(slash) +
781 strlen(f1) + strlen(f2) + strlen(f3) +
783 fdtfilefree = malloc(len);
785 printf("malloc fail (FDT filename)\n");
789 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
790 label->fdtdir, slash, f1, f2, f3, f4);
791 fdtfile = fdtfilefree;
795 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
798 printf("Skipping %s for failure retrieving fdt\n",
803 bootm_argv[3] = NULL;
808 bootm_argv[3] = env_get("fdt_addr");
816 kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
817 buf = map_sysmem(kernel_addr, 0);
818 /* Try bootm for legacy and FIT format image */
819 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
820 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
821 #ifdef CONFIG_CMD_BOOTI
822 /* Try booting an AArch64 Linux kernel image */
824 do_booti(cmdtp, 0, bootm_argc, bootm_argv);
825 #elif defined(CONFIG_CMD_BOOTZ)
826 /* Try booting a Image */
828 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
839 * Tokens for the pxe file parser.
866 * A token - given by a value and a type.
870 enum token_type type;
874 * Keywords recognized.
876 static const struct token keywords[] = {
879 {"timeout", T_TIMEOUT},
880 {"default", T_DEFAULT},
881 {"prompt", T_PROMPT},
883 {"kernel", T_KERNEL},
885 {"localboot", T_LOCALBOOT},
886 {"append", T_APPEND},
887 {"initrd", T_INITRD},
888 {"include", T_INCLUDE},
889 {"devicetree", T_FDT},
891 {"devicetreedir", T_FDTDIR},
892 {"fdtdir", T_FDTDIR},
893 {"ontimeout", T_ONTIMEOUT,},
894 {"ipappend", T_IPAPPEND,},
895 {"background", T_BACKGROUND,},
900 * Since pxe(linux) files don't have a token to identify the start of a
901 * literal, we have to keep track of when we're in a state where a literal is
902 * expected vs when we're in a state a keyword is expected.
911 * get_string retrieves a string from *p and stores it as a token in
914 * get_string used for scanning both string literals and keywords.
916 * Characters from *p are copied into t-val until a character equal to
917 * delim is found, or a NUL byte is reached. If delim has the special value of
918 * ' ', any whitespace character will be used as a delimiter.
920 * If lower is unequal to 0, uppercase characters will be converted to
921 * lowercase in the result. This is useful to make keywords case
924 * The location of *p is updated to point to the first character after the end
925 * of the token - the ending delimiter.
927 * On success, the new value of t->val is returned. Memory for t->val is
928 * allocated using malloc and must be free()'d to reclaim it. If insufficient
929 * memory is available, NULL is returned.
931 static char *get_string(char **p, struct token *t, char delim, int lower)
937 * b and e both start at the beginning of the input stream.
939 * e is incremented until we find the ending delimiter, or a NUL byte
940 * is reached. Then, we take e - b to find the length of the token.
945 if ((delim == ' ' && isspace(*e)) || delim == *e)
953 * Allocate memory to hold the string, and copy it in, converting
954 * characters to lowercase if lower is != 0.
956 t->val = malloc(len + 1);
960 for (i = 0; i < len; i++, b++) {
962 t->val[i] = tolower(*b);
970 * Update *p so the caller knows where to continue scanning.
980 * Populate a keyword token with a type and value.
982 static void get_keyword(struct token *t)
986 for (i = 0; keywords[i].val; i++) {
987 if (!strcmp(t->val, keywords[i].val)) {
988 t->type = keywords[i].type;
995 * Get the next token. We have to keep track of which state we're in to know
996 * if we're looking to get a string literal or a keyword.
998 * *p is updated to point at the first character after the current token.
1000 static void get_token(char **p, struct token *t, enum lex_state state)
1004 t->type = T_INVALID;
1006 /* eat non EOL whitespace */
1011 * eat comments. note that string literals can't begin with #, but
1012 * can contain a # after their first character.
1015 while (*c && *c != '\n')
1022 } else if (*c == '\0') {
1025 } else if (state == L_SLITERAL) {
1026 get_string(&c, t, '\n', 0);
1027 } else if (state == L_KEYWORD) {
1029 * when we expect a keyword, we first get the next string
1030 * token delimited by whitespace, and then check if it
1031 * matches a keyword in our keyword list. if it does, it's
1032 * converted to a keyword token of the appropriate type, and
1033 * if not, it remains a string token.
1035 get_string(&c, t, ' ', 1);
1043 * Increment *c until we get to the end of the current line, or EOF.
1045 static void eol_or_eof(char **c)
1047 while (**c && **c != '\n')
1052 * All of these parse_* functions share some common behavior.
1054 * They finish with *c pointing after the token they parse, and return 1 on
1055 * success, or < 0 on error.
1059 * Parse a string literal and store a pointer it at *dst. String literals
1060 * terminate at the end of the line.
1062 static int parse_sliteral(char **c, char **dst)
1067 get_token(c, &t, L_SLITERAL);
1069 if (t.type != T_STRING) {
1070 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1080 * Parse a base 10 (unsigned) integer and store it at *dst.
1082 static int parse_integer(char **c, int *dst)
1087 get_token(c, &t, L_SLITERAL);
1089 if (t.type != T_STRING) {
1090 printf("Expected string: %.*s\n", (int)(*c - s), s);
1094 *dst = simple_strtol(t.val, NULL, 10);
1101 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
1102 struct pxe_menu *cfg, int nest_level);
1105 * Parse an include statement, and retrieve and parse the file it mentions.
1107 * base should point to a location where it's safe to store the file, and
1108 * nest_level should indicate how many nested includes have occurred. For this
1109 * include, nest_level has already been incremented and doesn't need to be
1112 static int handle_include(cmd_tbl_t *cmdtp, char **c, unsigned long base,
1113 struct pxe_menu *cfg, int nest_level)
1121 err = parse_sliteral(c, &include_path);
1124 printf("Expected include path: %.*s\n",
1129 err = get_pxe_file(cmdtp, include_path, base);
1132 printf("Couldn't retrieve %s\n", include_path);
1136 buf = map_sysmem(base, 0);
1137 ret = parse_pxefile_top(cmdtp, buf, base, cfg, nest_level);
1144 * Parse lines that begin with 'menu'.
1146 * base and nest are provided to handle the 'menu include' case.
1148 * base should point to a location where it's safe to store the included file.
1150 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1151 * a file it includes, 3 when parsing a file included by that file, and so on.
1153 static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg,
1154 unsigned long base, int nest_level)
1160 get_token(c, &t, L_KEYWORD);
1164 err = parse_sliteral(c, &cfg->title);
1169 err = handle_include(cmdtp, c, base, cfg,
1174 err = parse_sliteral(c, &cfg->bmp);
1178 printf("Ignoring malformed menu command: %.*s\n",
1191 * Handles parsing a 'menu line' when we're parsing a label.
1193 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1194 struct pxe_label *label)
1201 get_token(c, &t, L_KEYWORD);
1205 if (!cfg->default_label)
1206 cfg->default_label = strdup(label->name);
1208 if (!cfg->default_label)
1213 parse_sliteral(c, &label->menu);
1216 printf("Ignoring malformed menu command: %.*s\n",
1226 * Handles parsing a 'kernel' label.
1227 * expecting "filename" or "<fit_filename>#cfg"
1229 static int parse_label_kernel(char **c, struct pxe_label *label)
1234 err = parse_sliteral(c, &label->kernel);
1238 s = strstr(label->kernel, "#");
1242 label->config = malloc(strlen(s) + 1);
1246 strcpy(label->config, s);
1253 * Parses a label and adds it to the list of labels for a menu.
1255 * A label ends when we either get to the end of a file, or
1256 * get some input we otherwise don't have a handler defined
1260 static int parse_label(char **c, struct pxe_menu *cfg)
1265 struct pxe_label *label;
1268 label = label_create();
1272 err = parse_sliteral(c, &label->name);
1274 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1275 label_destroy(label);
1279 list_add_tail(&label->list, &cfg->labels);
1283 get_token(c, &t, L_KEYWORD);
1288 err = parse_label_menu(c, cfg, label);
1293 err = parse_label_kernel(c, label);
1297 err = parse_sliteral(c, &label->append);
1300 s = strstr(label->append, "initrd=");
1304 len = (int)(strchr(s, ' ') - s);
1305 label->initrd = malloc(len + 1);
1306 strncpy(label->initrd, s, len);
1307 label->initrd[len] = '\0';
1313 err = parse_sliteral(c, &label->initrd);
1318 err = parse_sliteral(c, &label->fdt);
1323 err = parse_sliteral(c, &label->fdtdir);
1327 label->localboot = 1;
1328 err = parse_integer(c, &label->localboot_val);
1332 err = parse_integer(c, &label->ipappend);
1340 * put the token back! we don't want it - it's the end
1341 * of a label and whatever token this is, it's
1342 * something for the menu level context to handle.
1354 * This 16 comes from the limit pxelinux imposes on nested includes.
1356 * There is no reason at all we couldn't do more, but some limit helps prevent
1357 * infinite (until crash occurs) recursion if a file tries to include itself.
1359 #define MAX_NEST_LEVEL 16
1362 * Entry point for parsing a menu file. nest_level indicates how many times
1363 * we've nested in includes. It will be 1 for the top level menu file.
1365 * Returns 1 on success, < 0 on error.
1367 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
1368 struct pxe_menu *cfg, int nest_level)
1371 char *s, *b, *label_name;
1376 if (nest_level > MAX_NEST_LEVEL) {
1377 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1384 get_token(&p, &t, L_KEYWORD);
1390 err = parse_menu(cmdtp, &p, cfg,
1391 base + ALIGN(strlen(b) + 1, 4),
1396 err = parse_integer(&p, &cfg->timeout);
1400 err = parse_label(&p, cfg);
1405 err = parse_sliteral(&p, &label_name);
1408 if (cfg->default_label)
1409 free(cfg->default_label);
1411 cfg->default_label = label_name;
1417 err = handle_include(cmdtp, &p,
1418 base + ALIGN(strlen(b), 4), cfg,
1433 printf("Ignoring unknown command: %.*s\n",
1444 * Free the memory used by a pxe_menu and its labels.
1446 static void destroy_pxe_menu(struct pxe_menu *cfg)
1448 struct list_head *pos, *n;
1449 struct pxe_label *label;
1454 if (cfg->default_label)
1455 free(cfg->default_label);
1457 list_for_each_safe(pos, n, &cfg->labels) {
1458 label = list_entry(pos, struct pxe_label, list);
1460 label_destroy(label);
1467 * Entry point for parsing a pxe file. This is only used for the top level
1470 * Returns NULL if there is an error, otherwise, returns a pointer to a
1471 * pxe_menu struct populated with the results of parsing the pxe file (and any
1472 * files it includes). The resulting pxe_menu struct can be free()'d by using
1473 * the destroy_pxe_menu() function.
1475 static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, unsigned long menucfg)
1477 struct pxe_menu *cfg;
1481 cfg = malloc(sizeof(struct pxe_menu));
1486 memset(cfg, 0, sizeof(struct pxe_menu));
1488 INIT_LIST_HEAD(&cfg->labels);
1490 buf = map_sysmem(menucfg, 0);
1491 r = parse_pxefile_top(cmdtp, buf, menucfg, cfg, 1);
1495 destroy_pxe_menu(cfg);
1503 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1506 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1508 struct pxe_label *label;
1509 struct list_head *pos;
1513 char *default_num = NULL;
1516 * Create a menu and add items for all the labels.
1518 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
1519 cfg->prompt, label_print, NULL, NULL);
1524 list_for_each(pos, &cfg->labels) {
1525 label = list_entry(pos, struct pxe_label, list);
1527 sprintf(label->num, "%d", i++);
1528 if (menu_item_add(m, label->num, label) != 1) {
1532 if (cfg->default_label &&
1533 (strcmp(label->name, cfg->default_label) == 0))
1534 default_num = label->num;
1539 * After we've created items for each label in the menu, set the
1540 * menu's default label if one was specified.
1543 err = menu_default_set(m, default_num);
1545 if (err != -ENOENT) {
1550 printf("Missing default: %s\n", cfg->default_label);
1558 * Try to boot any labels we have yet to attempt to boot.
1560 static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1562 struct list_head *pos;
1563 struct pxe_label *label;
1565 list_for_each(pos, &cfg->labels) {
1566 label = list_entry(pos, struct pxe_label, list);
1568 if (!label->attempted)
1569 label_boot(cmdtp, label);
1574 * Boot the system as prescribed by a pxe_menu.
1576 * Use the menu system to either get the user's choice or the default, based
1577 * on config or user input. If there is no default or user's choice,
1578 * attempted to boot labels in the order they were given in pxe files.
1579 * If the default or user's choice fails to boot, attempt to boot other
1580 * labels in the order they were given in pxe files.
1582 * If this function returns, there weren't any labels that successfully
1583 * booted, or the user interrupted the menu selection via ctrl+c.
1585 static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1591 #ifdef CONFIG_CMD_BMP
1592 /* display BMP if available */
1594 if (get_relfile(cmdtp, cfg->bmp, load_addr)) {
1595 run_command("cls", 0);
1596 bmp_display(load_addr,
1597 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1599 printf("Skipping background bmp %s for failure\n",
1605 m = pxe_menu_to_menu(cfg);
1609 err = menu_get_choice(m, &choice);
1614 * err == 1 means we got a choice back from menu_get_choice.
1616 * err == -ENOENT if the menu was setup to select the default but no
1617 * default was set. in that case, we should continue trying to boot
1618 * labels that haven't been attempted yet.
1620 * otherwise, the user interrupted or there was some other error and
1625 err = label_boot(cmdtp, choice);
1628 } else if (err != -ENOENT) {
1632 boot_unattempted_labels(cmdtp, cfg);
1635 #ifdef CONFIG_CMD_NET
1637 * Boots a system using a pxe file
1639 * Returns 0 on success, 1 on error.
1642 do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1644 unsigned long pxefile_addr_r;
1645 struct pxe_menu *cfg;
1646 char *pxefile_addr_str;
1648 do_getfile = do_get_tftp;
1651 pxefile_addr_str = from_env("pxefile_addr_r");
1652 if (!pxefile_addr_str)
1655 } else if (argc == 2) {
1656 pxefile_addr_str = argv[1];
1658 return CMD_RET_USAGE;
1661 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1662 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1666 cfg = parse_pxefile(cmdtp, pxefile_addr_r);
1669 printf("Error parsing config file\n");
1673 handle_pxe_menu(cmdtp, cfg);
1675 destroy_pxe_menu(cfg);
1677 copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
1682 static cmd_tbl_t cmd_pxe_sub[] = {
1683 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1684 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1687 static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1692 return CMD_RET_USAGE;
1696 /* drop initial "pxe" arg */
1700 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1703 return cp->cmd(cmdtp, flag, argc, argv);
1705 return CMD_RET_USAGE;
1710 "commands to get and boot from pxe files",
1711 "get - try to retrieve a pxe file using tftp\npxe "
1712 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1717 * Boots a system using a local disk syslinux/extlinux file
1719 * Returns 0 on success, 1 on error.
1721 static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1723 unsigned long pxefile_addr_r;
1724 struct pxe_menu *cfg;
1725 char *pxefile_addr_str;
1731 if (argc > 1 && strstr(argv[1], "-p")) {
1738 return cmd_usage(cmdtp);
1741 pxefile_addr_str = from_env("pxefile_addr_r");
1742 if (!pxefile_addr_str)
1745 pxefile_addr_str = argv[4];
1749 filename = env_get("bootfile");
1752 env_set("bootfile", filename);
1755 if (strstr(argv[3], "ext2"))
1756 do_getfile = do_get_ext2;
1757 else if (strstr(argv[3], "fat"))
1758 do_getfile = do_get_fat;
1759 else if (strstr(argv[3], "any"))
1760 do_getfile = do_get_any;
1762 printf("Invalid filesystem: %s\n", argv[3]);
1765 fs_argv[1] = argv[1];
1766 fs_argv[2] = argv[2];
1768 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1769 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1773 if (get_pxe_file(cmdtp, filename, pxefile_addr_r) < 0) {
1774 printf("Error reading config file\n");
1778 cfg = parse_pxefile(cmdtp, pxefile_addr_r);
1781 printf("Error parsing config file\n");
1788 handle_pxe_menu(cmdtp, cfg);
1790 destroy_pxe_menu(cfg);
1796 sysboot, 7, 1, do_sysboot,
1797 "command to get and boot from syslinux files",
1798 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1799 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1800 " or any filesystem on 'dev' on 'interface' to address 'addr'"