1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2010-2011 Calxeda, Inc.
4 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
12 #include <linux/string.h>
13 #include <linux/ctype.h>
15 #include <linux/list.h>
23 #define MAX_TFTP_PATH_LEN 127
25 const char *pxe_default_paths[] = {
27 #ifdef CONFIG_SYS_BOARD
28 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC "-" CONFIG_SYS_BOARD,
30 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
32 "default-" CONFIG_SYS_ARCH,
40 * Like env_get, but prints an error if envvar isn't defined in the
41 * environment. It always returns what env_get does, so it can be used in
42 * place of env_get without changing error handling otherwise.
44 static char *from_env(const char *envvar)
48 ret = env_get(envvar);
51 printf("missing environment variable: %s\n", envvar);
58 * Convert an ethaddr from the environment to the format used by pxelinux
59 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
60 * the beginning of the ethernet address to indicate a hardware type of
61 * Ethernet. Also converts uppercase hex characters into lowercase, to match
62 * pxelinux's behavior.
64 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
65 * environment, or some other value < 0 on error.
67 static int format_mac_pxe(char *outbuf, size_t outbuf_len)
71 if (outbuf_len < 21) {
72 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
77 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
80 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
81 ethaddr[0], ethaddr[1], ethaddr[2],
82 ethaddr[3], ethaddr[4], ethaddr[5]);
89 * Returns the directory the file specified in the bootfile env variable is
90 * in. If bootfile isn't defined in the environment, return NULL, which should
91 * be interpreted as "don't prepend anything to paths".
93 static int get_bootfile_path(const char *file_path, char *bootfile_path,
94 size_t bootfile_path_size)
96 char *bootfile, *last_slash;
99 /* Only syslinux allows absolute paths */
100 if (file_path[0] == '/' && !is_pxe)
103 bootfile = from_env("bootfile");
108 last_slash = strrchr(bootfile, '/');
110 if (last_slash == NULL)
113 path_len = (last_slash - bootfile) + 1;
115 if (bootfile_path_size < path_len) {
116 printf("bootfile_path too small. (%zd < %zd)\n",
117 bootfile_path_size, path_len);
122 strncpy(bootfile_path, bootfile, path_len);
125 bootfile_path[path_len] = '\0';
130 static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
132 #ifdef CONFIG_CMD_NET
133 static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
135 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
137 tftp_argv[1] = file_addr;
138 tftp_argv[2] = (void *)file_path;
140 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
147 static char *fs_argv[5];
149 static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
151 #ifdef CONFIG_CMD_EXT2
152 fs_argv[0] = "ext2load";
153 fs_argv[3] = file_addr;
154 fs_argv[4] = (void *)file_path;
156 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
162 static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
164 #ifdef CONFIG_CMD_FAT
165 fs_argv[0] = "fatload";
166 fs_argv[3] = file_addr;
167 fs_argv[4] = (void *)file_path;
169 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
175 static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
177 #ifdef CONFIG_CMD_FS_GENERIC
179 fs_argv[3] = file_addr;
180 fs_argv[4] = (void *)file_path;
182 if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
189 * As in pxelinux, paths to files referenced from files we retrieve are
190 * relative to the location of bootfile. get_relfile takes such a path and
191 * joins it with the bootfile path to get the full path to the target file. If
192 * the bootfile path is NULL, we use file_path as is.
194 * Returns 1 for success, or < 0 on error.
196 static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path,
197 unsigned long file_addr)
200 char relfile[MAX_TFTP_PATH_LEN+1];
204 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
209 path_len = strlen(file_path);
210 path_len += strlen(relfile);
212 if (path_len > MAX_TFTP_PATH_LEN) {
213 printf("Base path too long (%s%s)\n",
217 return -ENAMETOOLONG;
220 strcat(relfile, file_path);
222 printf("Retrieving file: %s\n", relfile);
224 sprintf(addr_buf, "%lx", file_addr);
226 return do_getfile(cmdtp, relfile, addr_buf);
230 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
231 * 'bootfile' was specified in the environment, the path to bootfile will be
232 * prepended to 'file_path' and the resulting path will be used.
234 * Returns 1 on success, or < 0 for error.
236 static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path,
237 unsigned long file_addr)
239 unsigned long config_file_size;
244 err = get_relfile(cmdtp, file_path, file_addr);
250 * the file comes without a NUL byte at the end, so find out its size
251 * and add the NUL byte.
253 tftp_filesize = from_env("filesize");
258 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
261 buf = map_sysmem(file_addr + config_file_size, 1);
268 #ifdef CONFIG_CMD_NET
270 #define PXELINUX_DIR "pxelinux.cfg/"
273 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
274 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
275 * from the bootfile path, as described above.
277 * Returns 1 on success or < 0 on error.
279 static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file,
280 unsigned long pxefile_addr_r)
282 size_t base_len = strlen(PXELINUX_DIR);
283 char path[MAX_TFTP_PATH_LEN+1];
285 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
286 printf("path (%s%s) too long, skipping\n",
288 return -ENAMETOOLONG;
291 sprintf(path, PXELINUX_DIR "%s", file);
293 return get_pxe_file(cmdtp, path, pxefile_addr_r);
297 * Looks for a pxe file with a name based on the pxeuuid environment variable.
299 * Returns 1 on success or < 0 on error.
301 static int pxe_uuid_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
305 uuid_str = from_env("pxeuuid");
310 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
314 * Looks for a pxe file with a name based on the 'ethaddr' environment
317 * Returns 1 on success or < 0 on error.
319 static int pxe_mac_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
324 err = format_mac_pxe(mac_str, sizeof(mac_str));
329 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
333 * Looks for pxe files with names based on our IP address. See pxelinux
334 * documentation for details on what these file names look like. We match
337 * Returns 1 on success or < 0 on error.
339 static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
344 sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
346 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
347 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
352 ip_addr[mask_pos] = '\0';
359 * Entry point for the 'pxe get' command.
360 * This Follows pxelinux's rules to download a config file from a tftp server.
361 * The file is stored at the location given by the pxefile_addr_r environment
362 * variable, which must be set.
364 * UUID comes from pxeuuid env variable, if defined
365 * MAC addr comes from ethaddr env variable, if defined
368 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
370 * Returns 0 on success or 1 on error.
373 do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
375 char *pxefile_addr_str;
376 unsigned long pxefile_addr_r;
379 do_getfile = do_get_tftp;
382 return CMD_RET_USAGE;
384 pxefile_addr_str = from_env("pxefile_addr_r");
386 if (!pxefile_addr_str)
389 err = strict_strtoul(pxefile_addr_str, 16,
390 (unsigned long *)&pxefile_addr_r);
395 * Keep trying paths until we successfully get a file we're looking
398 if (pxe_uuid_path(cmdtp, pxefile_addr_r) > 0 ||
399 pxe_mac_path(cmdtp, pxefile_addr_r) > 0 ||
400 pxe_ipaddr_paths(cmdtp, pxefile_addr_r) > 0) {
401 printf("Config file found\n");
406 while (pxe_default_paths[i]) {
407 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
408 pxefile_addr_r) > 0) {
409 printf("Config file found\n");
415 printf("Config file not found\n");
422 * Wrapper to make it easier to store the file at file_path in the location
423 * specified by envaddr_name. file_path will be joined to the bootfile path,
424 * if any is specified.
426 * Returns 1 on success or < 0 on error.
428 static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
430 unsigned long file_addr;
433 envaddr = from_env(envaddr_name);
438 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
441 return get_relfile(cmdtp, file_path, file_addr);
445 * A note on the pxe file parser.
447 * We're parsing files that use syslinux grammar, which has a few quirks.
448 * String literals must be recognized based on context - there is no
449 * quoting or escaping support. There's also nothing to explicitly indicate
450 * when a label section completes. We deal with that by ending a label
451 * section whenever we see a line that doesn't include.
453 * As with the syslinux family, this same file format could be reused in the
454 * future for non pxe purposes. The only action it takes during parsing that
455 * would throw this off is handling of include files. It assumes we're using
456 * pxe, and does a tftp download of a file listed as an include file in the
457 * middle of the parsing operation. That could be handled by refactoring it to
458 * take a 'include file getter' function.
462 * Describes a single label given in a pxe file.
464 * Create these with the 'label_create' function given below.
466 * name - the name of the menu as given on the 'menu label' line.
467 * kernel - the path to the kernel file to use for this label.
468 * append - kernel command line to use when booting this label
469 * initrd - path to the initrd to use for this label.
470 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
471 * localboot - 1 if this label specified 'localboot', 0 otherwise.
472 * list - lets these form a list, which a pxe_menu struct will hold.
488 struct list_head list;
492 * Describes a pxe menu as given via pxe files.
494 * title - the name of the menu as given by a 'menu title' line.
495 * default_label - the name of the default label, if any.
496 * bmp - the bmp file name which is displayed in background
497 * timeout - time in tenths of a second to wait for a user key-press before
498 * booting the default label.
499 * prompt - if 0, don't prompt for a choice unless the timeout period is
500 * interrupted. If 1, always prompt for a choice regardless of
502 * labels - a list of labels defined for the menu.
510 struct list_head labels;
514 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
515 * result must be free()'d to reclaim the memory.
517 * Returns NULL if malloc fails.
519 static struct pxe_label *label_create(void)
521 struct pxe_label *label;
523 label = malloc(sizeof(struct pxe_label));
528 memset(label, 0, sizeof(struct pxe_label));
534 * Free the memory used by a pxe_label, including that used by its name,
535 * kernel, append and initrd members, if they're non NULL.
537 * So - be sure to only use dynamically allocated memory for the members of
538 * the pxe_label struct, unless you want to clean it up first. These are
539 * currently only created by the pxe file parsing code.
541 static void label_destroy(struct pxe_label *label)
568 * Print a label and its string members if they're defined.
570 * This is passed as a callback to the menu code for displaying each
573 static void label_print(void *data)
575 struct pxe_label *label = data;
576 const char *c = label->menu ? label->menu : label->name;
578 printf("%s:\t%s\n", label->num, c);
582 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
583 * environment variable is defined. Its contents will be executed as U-Boot
584 * command. If the label specified an 'append' line, its contents will be
585 * used to overwrite the contents of the 'bootargs' environment variable prior
586 * to running 'localcmd'.
588 * Returns 1 on success or < 0 on error.
590 static int label_localboot(struct pxe_label *label)
594 localcmd = from_env("localcmd");
600 char bootargs[CONFIG_SYS_CBSIZE];
602 cli_simple_process_macros(label->append, bootargs);
603 env_set("bootargs", bootargs);
606 debug("running: %s\n", localcmd);
608 return run_command_list(localcmd, strlen(localcmd), 0);
612 * Boot according to the contents of a pxe_label.
614 * If we can't boot for any reason, we return. A successful boot never
617 * The kernel will be stored in the location given by the 'kernel_addr_r'
618 * environment variable.
620 * If the label specifies an initrd file, it will be stored in the location
621 * given by the 'ramdisk_addr_r' environment variable.
623 * If the label specifies an 'append' line, its contents will overwrite that
624 * of the 'bootargs' environment variable.
626 static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
628 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
630 char mac_str[29] = "";
631 char ip_str[68] = "";
632 char *fit_addr = NULL;
640 label->attempted = 1;
642 if (label->localboot) {
643 if (label->localboot_val >= 0)
644 label_localboot(label);
648 if (label->kernel == NULL) {
649 printf("No kernel given, skipping %s\n",
655 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
656 printf("Skipping %s for failure retrieving initrd\n",
661 bootm_argv[2] = initrd_str;
662 strncpy(bootm_argv[2], env_get("ramdisk_addr_r"), 18);
663 strcat(bootm_argv[2], ":");
664 strncat(bootm_argv[2], env_get("filesize"), 9);
667 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
668 printf("Skipping %s for failure retrieving kernel\n",
673 if (label->ipappend & 0x1) {
674 sprintf(ip_str, " ip=%s:%s:%s:%s",
675 env_get("ipaddr"), env_get("serverip"),
676 env_get("gatewayip"), env_get("netmask"));
679 #ifdef CONFIG_CMD_NET
680 if (label->ipappend & 0x2) {
682 strcpy(mac_str, " BOOTIF=");
683 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
689 if ((label->ipappend & 0x3) || label->append) {
690 char bootargs[CONFIG_SYS_CBSIZE] = "";
691 char finalbootargs[CONFIG_SYS_CBSIZE];
693 if (strlen(label->append ?: "") +
694 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
695 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
696 strlen(label->append ?: ""),
697 strlen(ip_str), strlen(mac_str),
702 strncpy(bootargs, label->append,
704 strcat(bootargs, ip_str);
705 strcat(bootargs, mac_str);
707 cli_simple_process_macros(bootargs, finalbootargs);
708 env_set("bootargs", finalbootargs);
709 printf("append: %s\n", finalbootargs);
713 bootm_argv[1] = env_get("kernel_addr_r");
714 /* for FIT, append the configuration identifier */
716 int len = strlen(bootm_argv[1]) + strlen(label->config) + 1;
718 fit_addr = malloc(len);
720 printf("malloc fail (FIT address)\n");
723 snprintf(fit_addr, len, "%s%s", bootm_argv[1], label->config);
724 bootm_argv[1] = fit_addr;
728 * fdt usage is optional:
729 * It handles the following scenarios. All scenarios are exclusive
731 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
732 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
733 * and adjust argc appropriately.
735 * Scenario 2: If there is an fdt_addr specified, pass it along to
736 * bootm, and adjust argc appropriately.
738 * Scenario 3: fdt blob is not available.
740 bootm_argv[3] = env_get("fdt_addr_r");
742 /* if fdt label is defined then get fdt from server */
744 char *fdtfile = NULL;
745 char *fdtfilefree = NULL;
748 fdtfile = label->fdt;
749 } else if (label->fdtdir) {
750 char *f1, *f2, *f3, *f4, *slash;
752 f1 = env_get("fdtfile");
759 * For complex cases where this code doesn't
760 * generate the correct filename, the board
761 * code should set $fdtfile during early boot,
762 * or the boot scripts should set $fdtfile
763 * before invoking "pxe" or "sysboot".
767 f3 = env_get("board");
771 len = strlen(label->fdtdir);
774 else if (label->fdtdir[len - 1] != '/')
779 len = strlen(label->fdtdir) + strlen(slash) +
780 strlen(f1) + strlen(f2) + strlen(f3) +
782 fdtfilefree = malloc(len);
784 printf("malloc fail (FDT filename)\n");
788 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
789 label->fdtdir, slash, f1, f2, f3, f4);
790 fdtfile = fdtfilefree;
794 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
797 printf("Skipping %s for failure retrieving fdt\n",
802 bootm_argv[3] = NULL;
807 bootm_argv[3] = env_get("fdt_addr");
815 kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
816 buf = map_sysmem(kernel_addr, 0);
817 /* Try bootm for legacy and FIT format image */
818 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
819 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
820 #ifdef CONFIG_CMD_BOOTI
821 /* Try booting an AArch64 Linux kernel image */
823 do_booti(cmdtp, 0, bootm_argc, bootm_argv);
824 #elif defined(CONFIG_CMD_BOOTZ)
825 /* Try booting a Image */
827 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
838 * Tokens for the pxe file parser.
865 * A token - given by a value and a type.
869 enum token_type type;
873 * Keywords recognized.
875 static const struct token keywords[] = {
878 {"timeout", T_TIMEOUT},
879 {"default", T_DEFAULT},
880 {"prompt", T_PROMPT},
882 {"kernel", T_KERNEL},
884 {"localboot", T_LOCALBOOT},
885 {"append", T_APPEND},
886 {"initrd", T_INITRD},
887 {"include", T_INCLUDE},
888 {"devicetree", T_FDT},
890 {"devicetreedir", T_FDTDIR},
891 {"fdtdir", T_FDTDIR},
892 {"ontimeout", T_ONTIMEOUT,},
893 {"ipappend", T_IPAPPEND,},
894 {"background", T_BACKGROUND,},
899 * Since pxe(linux) files don't have a token to identify the start of a
900 * literal, we have to keep track of when we're in a state where a literal is
901 * expected vs when we're in a state a keyword is expected.
910 * get_string retrieves a string from *p and stores it as a token in
913 * get_string used for scanning both string literals and keywords.
915 * Characters from *p are copied into t-val until a character equal to
916 * delim is found, or a NUL byte is reached. If delim has the special value of
917 * ' ', any whitespace character will be used as a delimiter.
919 * If lower is unequal to 0, uppercase characters will be converted to
920 * lowercase in the result. This is useful to make keywords case
923 * The location of *p is updated to point to the first character after the end
924 * of the token - the ending delimiter.
926 * On success, the new value of t->val is returned. Memory for t->val is
927 * allocated using malloc and must be free()'d to reclaim it. If insufficient
928 * memory is available, NULL is returned.
930 static char *get_string(char **p, struct token *t, char delim, int lower)
936 * b and e both start at the beginning of the input stream.
938 * e is incremented until we find the ending delimiter, or a NUL byte
939 * is reached. Then, we take e - b to find the length of the token.
944 if ((delim == ' ' && isspace(*e)) || delim == *e)
952 * Allocate memory to hold the string, and copy it in, converting
953 * characters to lowercase if lower is != 0.
955 t->val = malloc(len + 1);
959 for (i = 0; i < len; i++, b++) {
961 t->val[i] = tolower(*b);
969 * Update *p so the caller knows where to continue scanning.
979 * Populate a keyword token with a type and value.
981 static void get_keyword(struct token *t)
985 for (i = 0; keywords[i].val; i++) {
986 if (!strcmp(t->val, keywords[i].val)) {
987 t->type = keywords[i].type;
994 * Get the next token. We have to keep track of which state we're in to know
995 * if we're looking to get a string literal or a keyword.
997 * *p is updated to point at the first character after the current token.
999 static void get_token(char **p, struct token *t, enum lex_state state)
1003 t->type = T_INVALID;
1005 /* eat non EOL whitespace */
1010 * eat comments. note that string literals can't begin with #, but
1011 * can contain a # after their first character.
1014 while (*c && *c != '\n')
1021 } else if (*c == '\0') {
1024 } else if (state == L_SLITERAL) {
1025 get_string(&c, t, '\n', 0);
1026 } else if (state == L_KEYWORD) {
1028 * when we expect a keyword, we first get the next string
1029 * token delimited by whitespace, and then check if it
1030 * matches a keyword in our keyword list. if it does, it's
1031 * converted to a keyword token of the appropriate type, and
1032 * if not, it remains a string token.
1034 get_string(&c, t, ' ', 1);
1042 * Increment *c until we get to the end of the current line, or EOF.
1044 static void eol_or_eof(char **c)
1046 while (**c && **c != '\n')
1051 * All of these parse_* functions share some common behavior.
1053 * They finish with *c pointing after the token they parse, and return 1 on
1054 * success, or < 0 on error.
1058 * Parse a string literal and store a pointer it at *dst. String literals
1059 * terminate at the end of the line.
1061 static int parse_sliteral(char **c, char **dst)
1066 get_token(c, &t, L_SLITERAL);
1068 if (t.type != T_STRING) {
1069 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1079 * Parse a base 10 (unsigned) integer and store it at *dst.
1081 static int parse_integer(char **c, int *dst)
1086 get_token(c, &t, L_SLITERAL);
1088 if (t.type != T_STRING) {
1089 printf("Expected string: %.*s\n", (int)(*c - s), s);
1093 *dst = simple_strtol(t.val, NULL, 10);
1100 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
1101 struct pxe_menu *cfg, int nest_level);
1104 * Parse an include statement, and retrieve and parse the file it mentions.
1106 * base should point to a location where it's safe to store the file, and
1107 * nest_level should indicate how many nested includes have occurred. For this
1108 * include, nest_level has already been incremented and doesn't need to be
1111 static int handle_include(cmd_tbl_t *cmdtp, char **c, unsigned long base,
1112 struct pxe_menu *cfg, int nest_level)
1120 err = parse_sliteral(c, &include_path);
1123 printf("Expected include path: %.*s\n",
1128 err = get_pxe_file(cmdtp, include_path, base);
1131 printf("Couldn't retrieve %s\n", include_path);
1135 buf = map_sysmem(base, 0);
1136 ret = parse_pxefile_top(cmdtp, buf, base, cfg, nest_level);
1143 * Parse lines that begin with 'menu'.
1145 * base and nest are provided to handle the 'menu include' case.
1147 * base should point to a location where it's safe to store the included file.
1149 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1150 * a file it includes, 3 when parsing a file included by that file, and so on.
1152 static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg,
1153 unsigned long base, int nest_level)
1159 get_token(c, &t, L_KEYWORD);
1163 err = parse_sliteral(c, &cfg->title);
1168 err = handle_include(cmdtp, c, base, cfg,
1173 err = parse_sliteral(c, &cfg->bmp);
1177 printf("Ignoring malformed menu command: %.*s\n",
1190 * Handles parsing a 'menu line' when we're parsing a label.
1192 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1193 struct pxe_label *label)
1200 get_token(c, &t, L_KEYWORD);
1204 if (!cfg->default_label)
1205 cfg->default_label = strdup(label->name);
1207 if (!cfg->default_label)
1212 parse_sliteral(c, &label->menu);
1215 printf("Ignoring malformed menu command: %.*s\n",
1225 * Handles parsing a 'kernel' label.
1226 * expecting "filename" or "<fit_filename>#cfg"
1228 static int parse_label_kernel(char **c, struct pxe_label *label)
1233 err = parse_sliteral(c, &label->kernel);
1237 s = strstr(label->kernel, "#");
1241 label->config = malloc(strlen(s) + 1);
1245 strcpy(label->config, s);
1252 * Parses a label and adds it to the list of labels for a menu.
1254 * A label ends when we either get to the end of a file, or
1255 * get some input we otherwise don't have a handler defined
1259 static int parse_label(char **c, struct pxe_menu *cfg)
1264 struct pxe_label *label;
1267 label = label_create();
1271 err = parse_sliteral(c, &label->name);
1273 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1274 label_destroy(label);
1278 list_add_tail(&label->list, &cfg->labels);
1282 get_token(c, &t, L_KEYWORD);
1287 err = parse_label_menu(c, cfg, label);
1292 err = parse_label_kernel(c, label);
1296 err = parse_sliteral(c, &label->append);
1299 s = strstr(label->append, "initrd=");
1303 len = (int)(strchr(s, ' ') - s);
1304 label->initrd = malloc(len + 1);
1305 strncpy(label->initrd, s, len);
1306 label->initrd[len] = '\0';
1312 err = parse_sliteral(c, &label->initrd);
1317 err = parse_sliteral(c, &label->fdt);
1322 err = parse_sliteral(c, &label->fdtdir);
1326 label->localboot = 1;
1327 err = parse_integer(c, &label->localboot_val);
1331 err = parse_integer(c, &label->ipappend);
1339 * put the token back! we don't want it - it's the end
1340 * of a label and whatever token this is, it's
1341 * something for the menu level context to handle.
1353 * This 16 comes from the limit pxelinux imposes on nested includes.
1355 * There is no reason at all we couldn't do more, but some limit helps prevent
1356 * infinite (until crash occurs) recursion if a file tries to include itself.
1358 #define MAX_NEST_LEVEL 16
1361 * Entry point for parsing a menu file. nest_level indicates how many times
1362 * we've nested in includes. It will be 1 for the top level menu file.
1364 * Returns 1 on success, < 0 on error.
1366 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
1367 struct pxe_menu *cfg, int nest_level)
1370 char *s, *b, *label_name;
1375 if (nest_level > MAX_NEST_LEVEL) {
1376 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1383 get_token(&p, &t, L_KEYWORD);
1389 err = parse_menu(cmdtp, &p, cfg,
1390 base + ALIGN(strlen(b) + 1, 4),
1395 err = parse_integer(&p, &cfg->timeout);
1399 err = parse_label(&p, cfg);
1404 err = parse_sliteral(&p, &label_name);
1407 if (cfg->default_label)
1408 free(cfg->default_label);
1410 cfg->default_label = label_name;
1416 err = handle_include(cmdtp, &p,
1417 base + ALIGN(strlen(b), 4), cfg,
1432 printf("Ignoring unknown command: %.*s\n",
1443 * Free the memory used by a pxe_menu and its labels.
1445 static void destroy_pxe_menu(struct pxe_menu *cfg)
1447 struct list_head *pos, *n;
1448 struct pxe_label *label;
1453 if (cfg->default_label)
1454 free(cfg->default_label);
1456 list_for_each_safe(pos, n, &cfg->labels) {
1457 label = list_entry(pos, struct pxe_label, list);
1459 label_destroy(label);
1466 * Entry point for parsing a pxe file. This is only used for the top level
1469 * Returns NULL if there is an error, otherwise, returns a pointer to a
1470 * pxe_menu struct populated with the results of parsing the pxe file (and any
1471 * files it includes). The resulting pxe_menu struct can be free()'d by using
1472 * the destroy_pxe_menu() function.
1474 static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, unsigned long menucfg)
1476 struct pxe_menu *cfg;
1480 cfg = malloc(sizeof(struct pxe_menu));
1485 memset(cfg, 0, sizeof(struct pxe_menu));
1487 INIT_LIST_HEAD(&cfg->labels);
1489 buf = map_sysmem(menucfg, 0);
1490 r = parse_pxefile_top(cmdtp, buf, menucfg, cfg, 1);
1494 destroy_pxe_menu(cfg);
1502 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1505 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1507 struct pxe_label *label;
1508 struct list_head *pos;
1512 char *default_num = NULL;
1515 * Create a menu and add items for all the labels.
1517 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
1518 cfg->prompt, label_print, NULL, NULL);
1523 list_for_each(pos, &cfg->labels) {
1524 label = list_entry(pos, struct pxe_label, list);
1526 sprintf(label->num, "%d", i++);
1527 if (menu_item_add(m, label->num, label) != 1) {
1531 if (cfg->default_label &&
1532 (strcmp(label->name, cfg->default_label) == 0))
1533 default_num = label->num;
1538 * After we've created items for each label in the menu, set the
1539 * menu's default label if one was specified.
1542 err = menu_default_set(m, default_num);
1544 if (err != -ENOENT) {
1549 printf("Missing default: %s\n", cfg->default_label);
1557 * Try to boot any labels we have yet to attempt to boot.
1559 static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1561 struct list_head *pos;
1562 struct pxe_label *label;
1564 list_for_each(pos, &cfg->labels) {
1565 label = list_entry(pos, struct pxe_label, list);
1567 if (!label->attempted)
1568 label_boot(cmdtp, label);
1573 * Boot the system as prescribed by a pxe_menu.
1575 * Use the menu system to either get the user's choice or the default, based
1576 * on config or user input. If there is no default or user's choice,
1577 * attempted to boot labels in the order they were given in pxe files.
1578 * If the default or user's choice fails to boot, attempt to boot other
1579 * labels in the order they were given in pxe files.
1581 * If this function returns, there weren't any labels that successfully
1582 * booted, or the user interrupted the menu selection via ctrl+c.
1584 static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1590 #ifdef CONFIG_CMD_BMP
1591 /* display BMP if available */
1593 if (get_relfile(cmdtp, cfg->bmp, load_addr)) {
1594 run_command("cls", 0);
1595 bmp_display(load_addr,
1596 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1598 printf("Skipping background bmp %s for failure\n",
1604 m = pxe_menu_to_menu(cfg);
1608 err = menu_get_choice(m, &choice);
1613 * err == 1 means we got a choice back from menu_get_choice.
1615 * err == -ENOENT if the menu was setup to select the default but no
1616 * default was set. in that case, we should continue trying to boot
1617 * labels that haven't been attempted yet.
1619 * otherwise, the user interrupted or there was some other error and
1624 err = label_boot(cmdtp, choice);
1627 } else if (err != -ENOENT) {
1631 boot_unattempted_labels(cmdtp, cfg);
1634 #ifdef CONFIG_CMD_NET
1636 * Boots a system using a pxe file
1638 * Returns 0 on success, 1 on error.
1641 do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1643 unsigned long pxefile_addr_r;
1644 struct pxe_menu *cfg;
1645 char *pxefile_addr_str;
1647 do_getfile = do_get_tftp;
1650 pxefile_addr_str = from_env("pxefile_addr_r");
1651 if (!pxefile_addr_str)
1654 } else if (argc == 2) {
1655 pxefile_addr_str = argv[1];
1657 return CMD_RET_USAGE;
1660 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1661 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1665 cfg = parse_pxefile(cmdtp, pxefile_addr_r);
1668 printf("Error parsing config file\n");
1672 handle_pxe_menu(cmdtp, cfg);
1674 destroy_pxe_menu(cfg);
1676 copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
1681 static cmd_tbl_t cmd_pxe_sub[] = {
1682 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1683 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1686 static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1691 return CMD_RET_USAGE;
1695 /* drop initial "pxe" arg */
1699 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1702 return cp->cmd(cmdtp, flag, argc, argv);
1704 return CMD_RET_USAGE;
1709 "commands to get and boot from pxe files",
1710 "get - try to retrieve a pxe file using tftp\npxe "
1711 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1716 * Boots a system using a local disk syslinux/extlinux file
1718 * Returns 0 on success, 1 on error.
1720 static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1722 unsigned long pxefile_addr_r;
1723 struct pxe_menu *cfg;
1724 char *pxefile_addr_str;
1730 if (argc > 1 && strstr(argv[1], "-p")) {
1737 return cmd_usage(cmdtp);
1740 pxefile_addr_str = from_env("pxefile_addr_r");
1741 if (!pxefile_addr_str)
1744 pxefile_addr_str = argv[4];
1748 filename = env_get("bootfile");
1751 env_set("bootfile", filename);
1754 if (strstr(argv[3], "ext2"))
1755 do_getfile = do_get_ext2;
1756 else if (strstr(argv[3], "fat"))
1757 do_getfile = do_get_fat;
1758 else if (strstr(argv[3], "any"))
1759 do_getfile = do_get_any;
1761 printf("Invalid filesystem: %s\n", argv[3]);
1764 fs_argv[1] = argv[1];
1765 fs_argv[2] = argv[2];
1767 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1768 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1772 if (get_pxe_file(cmdtp, filename, pxefile_addr_r) < 0) {
1773 printf("Error reading config file\n");
1777 cfg = parse_pxefile(cmdtp, pxefile_addr_r);
1780 printf("Error parsing config file\n");
1787 handle_pxe_menu(cmdtp, cfg);
1789 destroy_pxe_menu(cfg);
1795 sysboot, 7, 1, do_sysboot,
1796 "command to get and boot from syslinux files",
1797 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1798 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1799 " or any filesystem on 'dev' on 'interface' to address 'addr'"