2 * Copyright 2010-2011 Calxeda, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <linux/string.h>
11 #include <linux/ctype.h>
13 #include <linux/list.h>
18 #define MAX_TFTP_PATH_LEN 127
20 const char *pxe_default_paths[] = {
22 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
24 "default-" CONFIG_SYS_ARCH,
32 * Like getenv, but prints an error if envvar isn't defined in the
33 * environment. It always returns what getenv does, so it can be used in
34 * place of getenv without changing error handling otherwise.
36 static char *from_env(const char *envvar)
43 printf("missing environment variable: %s\n", envvar);
49 * Convert an ethaddr from the environment to the format used by pxelinux
50 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
51 * the beginning of the ethernet address to indicate a hardware type of
52 * Ethernet. Also converts uppercase hex characters into lowercase, to match
53 * pxelinux's behavior.
55 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
56 * environment, or some other value < 0 on error.
58 static int format_mac_pxe(char *outbuf, size_t outbuf_len)
62 if (outbuf_len < 21) {
63 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
68 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
72 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
73 ethaddr[0], ethaddr[1], ethaddr[2],
74 ethaddr[3], ethaddr[4], ethaddr[5]);
80 * Returns the directory the file specified in the bootfile env variable is
81 * in. If bootfile isn't defined in the environment, return NULL, which should
82 * be interpreted as "don't prepend anything to paths".
84 static int get_bootfile_path(const char *file_path, char *bootfile_path,
85 size_t bootfile_path_size)
87 char *bootfile, *last_slash;
90 /* Only syslinux allows absolute paths */
91 if (file_path[0] == '/' && !is_pxe)
94 bootfile = from_env("bootfile");
99 last_slash = strrchr(bootfile, '/');
101 if (last_slash == NULL)
104 path_len = (last_slash - bootfile) + 1;
106 if (bootfile_path_size < path_len) {
107 printf("bootfile_path too small. (%zd < %zd)\n",
108 bootfile_path_size, path_len);
113 strncpy(bootfile_path, bootfile, path_len);
116 bootfile_path[path_len] = '\0';
121 static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
123 static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
125 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
127 tftp_argv[1] = file_addr;
128 tftp_argv[2] = (void *)file_path;
130 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
136 static char *fs_argv[5];
138 static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
140 #ifdef CONFIG_CMD_EXT2
141 fs_argv[0] = "ext2load";
142 fs_argv[3] = file_addr;
143 fs_argv[4] = (void *)file_path;
145 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
151 static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
153 #ifdef CONFIG_CMD_FAT
154 fs_argv[0] = "fatload";
155 fs_argv[3] = file_addr;
156 fs_argv[4] = (void *)file_path;
158 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
164 static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
166 #ifdef CONFIG_CMD_FS_GENERIC
168 fs_argv[3] = file_addr;
169 fs_argv[4] = (void *)file_path;
171 if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
178 * As in pxelinux, paths to files referenced from files we retrieve are
179 * relative to the location of bootfile. get_relfile takes such a path and
180 * joins it with the bootfile path to get the full path to the target file. If
181 * the bootfile path is NULL, we use file_path as is.
183 * Returns 1 for success, or < 0 on error.
185 static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
188 char relfile[MAX_TFTP_PATH_LEN+1];
192 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
197 path_len = strlen(file_path);
198 path_len += strlen(relfile);
200 if (path_len > MAX_TFTP_PATH_LEN) {
201 printf("Base path too long (%s%s)\n",
205 return -ENAMETOOLONG;
208 strcat(relfile, file_path);
210 printf("Retrieving file: %s\n", relfile);
212 sprintf(addr_buf, "%p", file_addr);
214 return do_getfile(cmdtp, relfile, addr_buf);
218 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
219 * 'bootfile' was specified in the environment, the path to bootfile will be
220 * prepended to 'file_path' and the resulting path will be used.
222 * Returns 1 on success, or < 0 for error.
224 static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
226 unsigned long config_file_size;
230 err = get_relfile(cmdtp, file_path, file_addr);
236 * the file comes without a NUL byte at the end, so find out its size
237 * and add the NUL byte.
239 tftp_filesize = from_env("filesize");
244 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
247 *(char *)(file_addr + config_file_size) = '\0';
252 #define PXELINUX_DIR "pxelinux.cfg/"
255 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
256 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
257 * from the bootfile path, as described above.
259 * Returns 1 on success or < 0 on error.
261 static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r)
263 size_t base_len = strlen(PXELINUX_DIR);
264 char path[MAX_TFTP_PATH_LEN+1];
266 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
267 printf("path (%s%s) too long, skipping\n",
269 return -ENAMETOOLONG;
272 sprintf(path, PXELINUX_DIR "%s", file);
274 return get_pxe_file(cmdtp, path, pxefile_addr_r);
278 * Looks for a pxe file with a name based on the pxeuuid environment variable.
280 * Returns 1 on success or < 0 on error.
282 static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
286 uuid_str = from_env("pxeuuid");
291 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
295 * Looks for a pxe file with a name based on the 'ethaddr' environment
298 * Returns 1 on success or < 0 on error.
300 static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
305 err = format_mac_pxe(mac_str, sizeof(mac_str));
310 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
314 * Looks for pxe files with names based on our IP address. See pxelinux
315 * documentation for details on what these file names look like. We match
318 * Returns 1 on success or < 0 on error.
320 static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
325 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
327 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
328 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
333 ip_addr[mask_pos] = '\0';
340 * Entry point for the 'pxe get' command.
341 * This Follows pxelinux's rules to download a config file from a tftp server.
342 * The file is stored at the location given by the pxefile_addr_r environment
343 * variable, which must be set.
345 * UUID comes from pxeuuid env variable, if defined
346 * MAC addr comes from ethaddr env variable, if defined
349 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
351 * Returns 0 on success or 1 on error.
354 do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
356 char *pxefile_addr_str;
357 unsigned long pxefile_addr_r;
360 do_getfile = do_get_tftp;
363 return CMD_RET_USAGE;
365 pxefile_addr_str = from_env("pxefile_addr_r");
367 if (!pxefile_addr_str)
370 err = strict_strtoul(pxefile_addr_str, 16,
371 (unsigned long *)&pxefile_addr_r);
376 * Keep trying paths until we successfully get a file we're looking
379 if (pxe_uuid_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
380 pxe_mac_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
381 pxe_ipaddr_paths(cmdtp, (void *)pxefile_addr_r) > 0) {
382 printf("Config file found\n");
387 while (pxe_default_paths[i]) {
388 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
389 (void *)pxefile_addr_r) > 0) {
390 printf("Config file found\n");
396 printf("Config file not found\n");
402 * Wrapper to make it easier to store the file at file_path in the location
403 * specified by envaddr_name. file_path will be joined to the bootfile path,
404 * if any is specified.
406 * Returns 1 on success or < 0 on error.
408 static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
410 unsigned long file_addr;
413 envaddr = from_env(envaddr_name);
418 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
421 return get_relfile(cmdtp, file_path, (void *)file_addr);
425 * A note on the pxe file parser.
427 * We're parsing files that use syslinux grammar, which has a few quirks.
428 * String literals must be recognized based on context - there is no
429 * quoting or escaping support. There's also nothing to explicitly indicate
430 * when a label section completes. We deal with that by ending a label
431 * section whenever we see a line that doesn't include.
433 * As with the syslinux family, this same file format could be reused in the
434 * future for non pxe purposes. The only action it takes during parsing that
435 * would throw this off is handling of include files. It assumes we're using
436 * pxe, and does a tftp download of a file listed as an include file in the
437 * middle of the parsing operation. That could be handled by refactoring it to
438 * take a 'include file getter' function.
442 * Describes a single label given in a pxe file.
444 * Create these with the 'label_create' function given below.
446 * name - the name of the menu as given on the 'menu label' line.
447 * kernel - the path to the kernel file to use for this label.
448 * append - kernel command line to use when booting this label
449 * initrd - path to the initrd to use for this label.
450 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
451 * localboot - 1 if this label specified 'localboot', 0 otherwise.
452 * list - lets these form a list, which a pxe_menu struct will hold.
467 struct list_head list;
471 * Describes a pxe menu as given via pxe files.
473 * title - the name of the menu as given by a 'menu title' line.
474 * default_label - the name of the default label, if any.
475 * timeout - time in tenths of a second to wait for a user key-press before
476 * booting the default label.
477 * prompt - if 0, don't prompt for a choice unless the timeout period is
478 * interrupted. If 1, always prompt for a choice regardless of
480 * labels - a list of labels defined for the menu.
487 struct list_head labels;
491 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
492 * result must be free()'d to reclaim the memory.
494 * Returns NULL if malloc fails.
496 static struct pxe_label *label_create(void)
498 struct pxe_label *label;
500 label = malloc(sizeof(struct pxe_label));
505 memset(label, 0, sizeof(struct pxe_label));
511 * Free the memory used by a pxe_label, including that used by its name,
512 * kernel, append and initrd members, if they're non NULL.
514 * So - be sure to only use dynamically allocated memory for the members of
515 * the pxe_label struct, unless you want to clean it up first. These are
516 * currently only created by the pxe file parsing code.
518 static void label_destroy(struct pxe_label *label)
542 * Print a label and its string members if they're defined.
544 * This is passed as a callback to the menu code for displaying each
547 static void label_print(void *data)
549 struct pxe_label *label = data;
550 const char *c = label->menu ? label->menu : label->name;
552 printf("%s:\t%s\n", label->num, c);
556 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
557 * environment variable is defined. Its contents will be executed as U-boot
558 * command. If the label specified an 'append' line, its contents will be
559 * used to overwrite the contents of the 'bootargs' environment variable prior
560 * to running 'localcmd'.
562 * Returns 1 on success or < 0 on error.
564 static int label_localboot(struct pxe_label *label)
568 localcmd = from_env("localcmd");
574 setenv("bootargs", label->append);
576 debug("running: %s\n", localcmd);
578 return run_command_list(localcmd, strlen(localcmd), 0);
582 * Boot according to the contents of a pxe_label.
584 * If we can't boot for any reason, we return. A successful boot never
587 * The kernel will be stored in the location given by the 'kernel_addr_r'
588 * environment variable.
590 * If the label specifies an initrd file, it will be stored in the location
591 * given by the 'ramdisk_addr_r' environment variable.
593 * If the label specifies an 'append' line, its contents will overwrite that
594 * of the 'bootargs' environment variable.
596 static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
598 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
600 char mac_str[29] = "";
601 char ip_str[68] = "";
608 label->attempted = 1;
610 if (label->localboot) {
611 if (label->localboot_val >= 0)
612 label_localboot(label);
616 if (label->kernel == NULL) {
617 printf("No kernel given, skipping %s\n",
623 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
624 printf("Skipping %s for failure retrieving initrd\n",
629 bootm_argv[2] = initrd_str;
630 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
631 strcat(bootm_argv[2], ":");
632 strcat(bootm_argv[2], getenv("filesize"));
637 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
638 printf("Skipping %s for failure retrieving kernel\n",
643 if (label->ipappend & 0x1) {
644 sprintf(ip_str, " ip=%s:%s:%s:%s",
645 getenv("ipaddr"), getenv("serverip"),
646 getenv("gatewayip"), getenv("netmask"));
647 len += strlen(ip_str);
650 if (label->ipappend & 0x2) {
652 strcpy(mac_str, " BOOTIF=");
653 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
656 len += strlen(mac_str);
660 len += strlen(label->append);
663 bootargs = malloc(len + 1);
668 strcpy(bootargs, label->append);
669 strcat(bootargs, ip_str);
670 strcat(bootargs, mac_str);
672 setenv("bootargs", bootargs);
673 printf("append: %s\n", bootargs);
678 bootm_argv[1] = getenv("kernel_addr_r");
681 * fdt usage is optional:
682 * It handles the following scenarios. All scenarios are exclusive
684 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
685 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
686 * and adjust argc appropriately.
688 * Scenario 2: If there is an fdt_addr specified, pass it along to
689 * bootm, and adjust argc appropriately.
691 * Scenario 3: fdt blob is not available.
693 bootm_argv[3] = getenv("fdt_addr_r");
695 /* if fdt label is defined then get fdt from server */
697 char *fdtfile = NULL;
698 char *fdtfilefree = NULL;
701 fdtfile = label->fdt;
702 } else if (label->fdtdir) {
703 fdtfile = getenv("fdtfile");
705 * For complex cases, it might be worth calling a
706 * board- or SoC-provided function here to provide a
710 * fdtfile = gen_fdtfile();
712 * If this is added, be sure to keep the default below,
713 * or move it to the default weak implementation of
717 char *soc = getenv("soc");
718 char *board = getenv("board");
721 len = strlen(label->fdtdir);
724 else if (label->fdtdir[len - 1] != '/')
729 len = strlen(label->fdtdir) + strlen(slash) +
730 strlen(soc) + 1 + strlen(board) + 5;
731 fdtfilefree = malloc(len);
733 printf("malloc fail (FDT filename)\n");
737 snprintf(fdtfilefree, len, "%s%s%s-%s.dtb",
738 label->fdtdir, slash, soc, board);
739 fdtfile = fdtfilefree;
744 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
747 printf("Skipping %s for failure retrieving fdt\n",
752 bootm_argv[3] = NULL;
757 bootm_argv[3] = getenv("fdt_addr");
762 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
764 #ifdef CONFIG_CMD_BOOTZ
765 /* Try booting a zImage if do_bootm returns */
766 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
772 * Tokens for the pxe file parser.
798 * A token - given by a value and a type.
802 enum token_type type;
806 * Keywords recognized.
808 static const struct token keywords[] = {
811 {"timeout", T_TIMEOUT},
812 {"default", T_DEFAULT},
813 {"prompt", T_PROMPT},
815 {"kernel", T_KERNEL},
817 {"localboot", T_LOCALBOOT},
818 {"append", T_APPEND},
819 {"initrd", T_INITRD},
820 {"include", T_INCLUDE},
821 {"devicetree", T_FDT},
823 {"devicetreedir", T_FDTDIR},
824 {"fdtdir", T_FDTDIR},
825 {"ontimeout", T_ONTIMEOUT,},
826 {"ipappend", T_IPAPPEND,},
831 * Since pxe(linux) files don't have a token to identify the start of a
832 * literal, we have to keep track of when we're in a state where a literal is
833 * expected vs when we're in a state a keyword is expected.
842 * get_string retrieves a string from *p and stores it as a token in
845 * get_string used for scanning both string literals and keywords.
847 * Characters from *p are copied into t-val until a character equal to
848 * delim is found, or a NUL byte is reached. If delim has the special value of
849 * ' ', any whitespace character will be used as a delimiter.
851 * If lower is unequal to 0, uppercase characters will be converted to
852 * lowercase in the result. This is useful to make keywords case
855 * The location of *p is updated to point to the first character after the end
856 * of the token - the ending delimiter.
858 * On success, the new value of t->val is returned. Memory for t->val is
859 * allocated using malloc and must be free()'d to reclaim it. If insufficient
860 * memory is available, NULL is returned.
862 static char *get_string(char **p, struct token *t, char delim, int lower)
868 * b and e both start at the beginning of the input stream.
870 * e is incremented until we find the ending delimiter, or a NUL byte
871 * is reached. Then, we take e - b to find the length of the token.
876 if ((delim == ' ' && isspace(*e)) || delim == *e)
884 * Allocate memory to hold the string, and copy it in, converting
885 * characters to lowercase if lower is != 0.
887 t->val = malloc(len + 1);
891 for (i = 0; i < len; i++, b++) {
893 t->val[i] = tolower(*b);
901 * Update *p so the caller knows where to continue scanning.
911 * Populate a keyword token with a type and value.
913 static void get_keyword(struct token *t)
917 for (i = 0; keywords[i].val; i++) {
918 if (!strcmp(t->val, keywords[i].val)) {
919 t->type = keywords[i].type;
926 * Get the next token. We have to keep track of which state we're in to know
927 * if we're looking to get a string literal or a keyword.
929 * *p is updated to point at the first character after the current token.
931 static void get_token(char **p, struct token *t, enum lex_state state)
937 /* eat non EOL whitespace */
942 * eat comments. note that string literals can't begin with #, but
943 * can contain a # after their first character.
946 while (*c && *c != '\n')
953 } else if (*c == '\0') {
956 } else if (state == L_SLITERAL) {
957 get_string(&c, t, '\n', 0);
958 } else if (state == L_KEYWORD) {
960 * when we expect a keyword, we first get the next string
961 * token delimited by whitespace, and then check if it
962 * matches a keyword in our keyword list. if it does, it's
963 * converted to a keyword token of the appropriate type, and
964 * if not, it remains a string token.
966 get_string(&c, t, ' ', 1);
974 * Increment *c until we get to the end of the current line, or EOF.
976 static void eol_or_eof(char **c)
978 while (**c && **c != '\n')
983 * All of these parse_* functions share some common behavior.
985 * They finish with *c pointing after the token they parse, and return 1 on
986 * success, or < 0 on error.
990 * Parse a string literal and store a pointer it at *dst. String literals
991 * terminate at the end of the line.
993 static int parse_sliteral(char **c, char **dst)
998 get_token(c, &t, L_SLITERAL);
1000 if (t.type != T_STRING) {
1001 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1011 * Parse a base 10 (unsigned) integer and store it at *dst.
1013 static int parse_integer(char **c, int *dst)
1018 get_token(c, &t, L_SLITERAL);
1020 if (t.type != T_STRING) {
1021 printf("Expected string: %.*s\n", (int)(*c - s), s);
1025 *dst = simple_strtol(t.val, NULL, 10);
1032 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
1035 * Parse an include statement, and retrieve and parse the file it mentions.
1037 * base should point to a location where it's safe to store the file, and
1038 * nest_level should indicate how many nested includes have occurred. For this
1039 * include, nest_level has already been incremented and doesn't need to be
1042 static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
1043 struct pxe_menu *cfg, int nest_level)
1049 err = parse_sliteral(c, &include_path);
1052 printf("Expected include path: %.*s\n",
1057 err = get_pxe_file(cmdtp, include_path, base);
1060 printf("Couldn't retrieve %s\n", include_path);
1064 return parse_pxefile_top(cmdtp, base, cfg, nest_level);
1068 * Parse lines that begin with 'menu'.
1070 * b and nest are provided to handle the 'menu include' case.
1072 * b should be the address where the file currently being parsed is stored.
1074 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1075 * a file it includes, 3 when parsing a file included by that file, and so on.
1077 static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg, char *b, int nest_level)
1083 get_token(c, &t, L_KEYWORD);
1087 err = parse_sliteral(c, &cfg->title);
1092 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
1097 printf("Ignoring malformed menu command: %.*s\n",
1110 * Handles parsing a 'menu line' when we're parsing a label.
1112 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1113 struct pxe_label *label)
1120 get_token(c, &t, L_KEYWORD);
1124 if (!cfg->default_label)
1125 cfg->default_label = strdup(label->name);
1127 if (!cfg->default_label)
1132 parse_sliteral(c, &label->menu);
1135 printf("Ignoring malformed menu command: %.*s\n",
1145 * Parses a label and adds it to the list of labels for a menu.
1147 * A label ends when we either get to the end of a file, or
1148 * get some input we otherwise don't have a handler defined
1152 static int parse_label(char **c, struct pxe_menu *cfg)
1157 struct pxe_label *label;
1160 label = label_create();
1164 err = parse_sliteral(c, &label->name);
1166 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1167 label_destroy(label);
1171 list_add_tail(&label->list, &cfg->labels);
1175 get_token(c, &t, L_KEYWORD);
1180 err = parse_label_menu(c, cfg, label);
1185 err = parse_sliteral(c, &label->kernel);
1189 err = parse_sliteral(c, &label->append);
1192 s = strstr(label->append, "initrd=");
1196 len = (int)(strchr(s, ' ') - s);
1197 label->initrd = malloc(len + 1);
1198 strncpy(label->initrd, s, len);
1199 label->initrd[len] = '\0';
1205 err = parse_sliteral(c, &label->initrd);
1210 err = parse_sliteral(c, &label->fdt);
1215 err = parse_sliteral(c, &label->fdtdir);
1219 label->localboot = 1;
1220 err = parse_integer(c, &label->localboot_val);
1224 err = parse_integer(c, &label->ipappend);
1232 * put the token back! we don't want it - it's the end
1233 * of a label and whatever token this is, it's
1234 * something for the menu level context to handle.
1246 * This 16 comes from the limit pxelinux imposes on nested includes.
1248 * There is no reason at all we couldn't do more, but some limit helps prevent
1249 * infinite (until crash occurs) recursion if a file tries to include itself.
1251 #define MAX_NEST_LEVEL 16
1254 * Entry point for parsing a menu file. nest_level indicates how many times
1255 * we've nested in includes. It will be 1 for the top level menu file.
1257 * Returns 1 on success, < 0 on error.
1259 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
1262 char *s, *b, *label_name;
1267 if (nest_level > MAX_NEST_LEVEL) {
1268 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1275 get_token(&p, &t, L_KEYWORD);
1281 err = parse_menu(cmdtp, &p, cfg, b, nest_level);
1285 err = parse_integer(&p, &cfg->timeout);
1289 err = parse_label(&p, cfg);
1294 err = parse_sliteral(&p, &label_name);
1297 if (cfg->default_label)
1298 free(cfg->default_label);
1300 cfg->default_label = label_name;
1306 err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
1321 printf("Ignoring unknown command: %.*s\n",
1332 * Free the memory used by a pxe_menu and its labels.
1334 static void destroy_pxe_menu(struct pxe_menu *cfg)
1336 struct list_head *pos, *n;
1337 struct pxe_label *label;
1342 if (cfg->default_label)
1343 free(cfg->default_label);
1345 list_for_each_safe(pos, n, &cfg->labels) {
1346 label = list_entry(pos, struct pxe_label, list);
1348 label_destroy(label);
1355 * Entry point for parsing a pxe file. This is only used for the top level
1358 * Returns NULL if there is an error, otherwise, returns a pointer to a
1359 * pxe_menu struct populated with the results of parsing the pxe file (and any
1360 * files it includes). The resulting pxe_menu struct can be free()'d by using
1361 * the destroy_pxe_menu() function.
1363 static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
1365 struct pxe_menu *cfg;
1367 cfg = malloc(sizeof(struct pxe_menu));
1372 memset(cfg, 0, sizeof(struct pxe_menu));
1374 INIT_LIST_HEAD(&cfg->labels);
1376 if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
1377 destroy_pxe_menu(cfg);
1385 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1388 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1390 struct pxe_label *label;
1391 struct list_head *pos;
1395 char *default_num = NULL;
1398 * Create a menu and add items for all the labels.
1400 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1406 list_for_each(pos, &cfg->labels) {
1407 label = list_entry(pos, struct pxe_label, list);
1409 sprintf(label->num, "%d", i++);
1410 if (menu_item_add(m, label->num, label) != 1) {
1414 if (cfg->default_label &&
1415 (strcmp(label->name, cfg->default_label) == 0))
1416 default_num = label->num;
1421 * After we've created items for each label in the menu, set the
1422 * menu's default label if one was specified.
1425 err = menu_default_set(m, default_num);
1427 if (err != -ENOENT) {
1432 printf("Missing default: %s\n", cfg->default_label);
1440 * Try to boot any labels we have yet to attempt to boot.
1442 static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1444 struct list_head *pos;
1445 struct pxe_label *label;
1447 list_for_each(pos, &cfg->labels) {
1448 label = list_entry(pos, struct pxe_label, list);
1450 if (!label->attempted)
1451 label_boot(cmdtp, label);
1456 * Boot the system as prescribed by a pxe_menu.
1458 * Use the menu system to either get the user's choice or the default, based
1459 * on config or user input. If there is no default or user's choice,
1460 * attempted to boot labels in the order they were given in pxe files.
1461 * If the default or user's choice fails to boot, attempt to boot other
1462 * labels in the order they were given in pxe files.
1464 * If this function returns, there weren't any labels that successfully
1465 * booted, or the user interrupted the menu selection via ctrl+c.
1467 static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1473 m = pxe_menu_to_menu(cfg);
1477 err = menu_get_choice(m, &choice);
1482 * err == 1 means we got a choice back from menu_get_choice.
1484 * err == -ENOENT if the menu was setup to select the default but no
1485 * default was set. in that case, we should continue trying to boot
1486 * labels that haven't been attempted yet.
1488 * otherwise, the user interrupted or there was some other error and
1493 err = label_boot(cmdtp, choice);
1496 } else if (err != -ENOENT) {
1500 boot_unattempted_labels(cmdtp, cfg);
1504 * Boots a system using a pxe file
1506 * Returns 0 on success, 1 on error.
1509 do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1511 unsigned long pxefile_addr_r;
1512 struct pxe_menu *cfg;
1513 char *pxefile_addr_str;
1515 do_getfile = do_get_tftp;
1518 pxefile_addr_str = from_env("pxefile_addr_r");
1519 if (!pxefile_addr_str)
1522 } else if (argc == 2) {
1523 pxefile_addr_str = argv[1];
1525 return CMD_RET_USAGE;
1528 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1529 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1533 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
1536 printf("Error parsing config file\n");
1540 handle_pxe_menu(cmdtp, cfg);
1542 destroy_pxe_menu(cfg);
1547 static cmd_tbl_t cmd_pxe_sub[] = {
1548 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1549 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1552 int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1557 return CMD_RET_USAGE;
1561 /* drop initial "pxe" arg */
1565 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1568 return cp->cmd(cmdtp, flag, argc, argv);
1570 return CMD_RET_USAGE;
1575 "commands to get and boot from pxe files",
1576 "get - try to retrieve a pxe file using tftp\npxe "
1577 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1581 * Boots a system using a local disk syslinux/extlinux file
1583 * Returns 0 on success, 1 on error.
1585 int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1587 unsigned long pxefile_addr_r;
1588 struct pxe_menu *cfg;
1589 char *pxefile_addr_str;
1595 if (strstr(argv[1], "-p")) {
1602 return cmd_usage(cmdtp);
1605 pxefile_addr_str = from_env("pxefile_addr_r");
1606 if (!pxefile_addr_str)
1609 pxefile_addr_str = argv[4];
1613 filename = getenv("bootfile");
1616 setenv("bootfile", filename);
1619 if (strstr(argv[3], "ext2"))
1620 do_getfile = do_get_ext2;
1621 else if (strstr(argv[3], "fat"))
1622 do_getfile = do_get_fat;
1623 else if (strstr(argv[3], "any"))
1624 do_getfile = do_get_any;
1626 printf("Invalid filesystem: %s\n", argv[3]);
1629 fs_argv[1] = argv[1];
1630 fs_argv[2] = argv[2];
1632 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1633 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1637 if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
1638 printf("Error reading config file\n");
1642 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
1645 printf("Error parsing config file\n");
1652 handle_pxe_menu(cmdtp, cfg);
1654 destroy_pxe_menu(cfg);
1660 sysboot, 7, 1, do_sysboot,
1661 "command to get and boot from syslinux files",
1662 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1663 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1664 " or any filesystem on 'dev' on 'interface' to address 'addr'"