2 * Copyright 2010-2011 Calxeda, Inc.
3 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <linux/string.h>
12 #include <linux/ctype.h>
14 #include <linux/list.h>
19 #define MAX_TFTP_PATH_LEN 127
21 const char *pxe_default_paths[] = {
23 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
25 "default-" CONFIG_SYS_ARCH,
33 * Like getenv, but prints an error if envvar isn't defined in the
34 * environment. It always returns what getenv does, so it can be used in
35 * place of getenv without changing error handling otherwise.
37 static char *from_env(const char *envvar)
44 printf("missing environment variable: %s\n", envvar);
51 * Convert an ethaddr from the environment to the format used by pxelinux
52 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
53 * the beginning of the ethernet address to indicate a hardware type of
54 * Ethernet. Also converts uppercase hex characters into lowercase, to match
55 * pxelinux's behavior.
57 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
58 * environment, or some other value < 0 on error.
60 static int format_mac_pxe(char *outbuf, size_t outbuf_len)
64 if (outbuf_len < 21) {
65 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
70 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
74 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
75 ethaddr[0], ethaddr[1], ethaddr[2],
76 ethaddr[3], ethaddr[4], ethaddr[5]);
83 * Returns the directory the file specified in the bootfile env variable is
84 * in. If bootfile isn't defined in the environment, return NULL, which should
85 * be interpreted as "don't prepend anything to paths".
87 static int get_bootfile_path(const char *file_path, char *bootfile_path,
88 size_t bootfile_path_size)
90 char *bootfile, *last_slash;
93 /* Only syslinux allows absolute paths */
94 if (file_path[0] == '/' && !is_pxe)
97 bootfile = from_env("bootfile");
102 last_slash = strrchr(bootfile, '/');
104 if (last_slash == NULL)
107 path_len = (last_slash - bootfile) + 1;
109 if (bootfile_path_size < path_len) {
110 printf("bootfile_path too small. (%zd < %zd)\n",
111 bootfile_path_size, path_len);
116 strncpy(bootfile_path, bootfile, path_len);
119 bootfile_path[path_len] = '\0';
124 static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
126 #ifdef CONFIG_CMD_NET
127 static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
129 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
131 tftp_argv[1] = file_addr;
132 tftp_argv[2] = (void *)file_path;
134 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
141 static char *fs_argv[5];
143 static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
145 #ifdef CONFIG_CMD_EXT2
146 fs_argv[0] = "ext2load";
147 fs_argv[3] = file_addr;
148 fs_argv[4] = (void *)file_path;
150 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
156 static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
158 #ifdef CONFIG_CMD_FAT
159 fs_argv[0] = "fatload";
160 fs_argv[3] = file_addr;
161 fs_argv[4] = (void *)file_path;
163 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
169 static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
171 #ifdef CONFIG_CMD_FS_GENERIC
173 fs_argv[3] = file_addr;
174 fs_argv[4] = (void *)file_path;
176 if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
183 * As in pxelinux, paths to files referenced from files we retrieve are
184 * relative to the location of bootfile. get_relfile takes such a path and
185 * joins it with the bootfile path to get the full path to the target file. If
186 * the bootfile path is NULL, we use file_path as is.
188 * Returns 1 for success, or < 0 on error.
190 static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
193 char relfile[MAX_TFTP_PATH_LEN+1];
197 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
202 path_len = strlen(file_path);
203 path_len += strlen(relfile);
205 if (path_len > MAX_TFTP_PATH_LEN) {
206 printf("Base path too long (%s%s)\n",
210 return -ENAMETOOLONG;
213 strcat(relfile, file_path);
215 printf("Retrieving file: %s\n", relfile);
217 sprintf(addr_buf, "%p", file_addr);
219 return do_getfile(cmdtp, relfile, addr_buf);
223 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
224 * 'bootfile' was specified in the environment, the path to bootfile will be
225 * prepended to 'file_path' and the resulting path will be used.
227 * Returns 1 on success, or < 0 for error.
229 static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
231 unsigned long config_file_size;
235 err = get_relfile(cmdtp, file_path, file_addr);
241 * the file comes without a NUL byte at the end, so find out its size
242 * and add the NUL byte.
244 tftp_filesize = from_env("filesize");
249 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
252 *(char *)(file_addr + config_file_size) = '\0';
257 #ifdef CONFIG_CMD_NET
259 #define PXELINUX_DIR "pxelinux.cfg/"
262 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
263 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
264 * from the bootfile path, as described above.
266 * Returns 1 on success or < 0 on error.
268 static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r)
270 size_t base_len = strlen(PXELINUX_DIR);
271 char path[MAX_TFTP_PATH_LEN+1];
273 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
274 printf("path (%s%s) too long, skipping\n",
276 return -ENAMETOOLONG;
279 sprintf(path, PXELINUX_DIR "%s", file);
281 return get_pxe_file(cmdtp, path, pxefile_addr_r);
285 * Looks for a pxe file with a name based on the pxeuuid environment variable.
287 * Returns 1 on success or < 0 on error.
289 static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
293 uuid_str = from_env("pxeuuid");
298 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
302 * Looks for a pxe file with a name based on the 'ethaddr' environment
305 * Returns 1 on success or < 0 on error.
307 static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
312 err = format_mac_pxe(mac_str, sizeof(mac_str));
317 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
321 * Looks for pxe files with names based on our IP address. See pxelinux
322 * documentation for details on what these file names look like. We match
325 * Returns 1 on success or < 0 on error.
327 static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
332 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
334 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
335 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
340 ip_addr[mask_pos] = '\0';
347 * Entry point for the 'pxe get' command.
348 * This Follows pxelinux's rules to download a config file from a tftp server.
349 * The file is stored at the location given by the pxefile_addr_r environment
350 * variable, which must be set.
352 * UUID comes from pxeuuid env variable, if defined
353 * MAC addr comes from ethaddr env variable, if defined
356 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
358 * Returns 0 on success or 1 on error.
361 do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
363 char *pxefile_addr_str;
364 unsigned long pxefile_addr_r;
367 do_getfile = do_get_tftp;
370 return CMD_RET_USAGE;
372 pxefile_addr_str = from_env("pxefile_addr_r");
374 if (!pxefile_addr_str)
377 err = strict_strtoul(pxefile_addr_str, 16,
378 (unsigned long *)&pxefile_addr_r);
383 * Keep trying paths until we successfully get a file we're looking
386 if (pxe_uuid_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
387 pxe_mac_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
388 pxe_ipaddr_paths(cmdtp, (void *)pxefile_addr_r) > 0) {
389 printf("Config file found\n");
394 while (pxe_default_paths[i]) {
395 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
396 (void *)pxefile_addr_r) > 0) {
397 printf("Config file found\n");
403 printf("Config file not found\n");
410 * Wrapper to make it easier to store the file at file_path in the location
411 * specified by envaddr_name. file_path will be joined to the bootfile path,
412 * if any is specified.
414 * Returns 1 on success or < 0 on error.
416 static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
418 unsigned long file_addr;
421 envaddr = from_env(envaddr_name);
426 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
429 return get_relfile(cmdtp, file_path, (void *)file_addr);
433 * A note on the pxe file parser.
435 * We're parsing files that use syslinux grammar, which has a few quirks.
436 * String literals must be recognized based on context - there is no
437 * quoting or escaping support. There's also nothing to explicitly indicate
438 * when a label section completes. We deal with that by ending a label
439 * section whenever we see a line that doesn't include.
441 * As with the syslinux family, this same file format could be reused in the
442 * future for non pxe purposes. The only action it takes during parsing that
443 * would throw this off is handling of include files. It assumes we're using
444 * pxe, and does a tftp download of a file listed as an include file in the
445 * middle of the parsing operation. That could be handled by refactoring it to
446 * take a 'include file getter' function.
450 * Describes a single label given in a pxe file.
452 * Create these with the 'label_create' function given below.
454 * name - the name of the menu as given on the 'menu label' line.
455 * kernel - the path to the kernel file to use for this label.
456 * append - kernel command line to use when booting this label
457 * initrd - path to the initrd to use for this label.
458 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
459 * localboot - 1 if this label specified 'localboot', 0 otherwise.
460 * list - lets these form a list, which a pxe_menu struct will hold.
475 struct list_head list;
479 * Describes a pxe menu as given via pxe files.
481 * title - the name of the menu as given by a 'menu title' line.
482 * default_label - the name of the default label, if any.
483 * timeout - time in tenths of a second to wait for a user key-press before
484 * booting the default label.
485 * prompt - if 0, don't prompt for a choice unless the timeout period is
486 * interrupted. If 1, always prompt for a choice regardless of
488 * labels - a list of labels defined for the menu.
495 struct list_head labels;
499 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
500 * result must be free()'d to reclaim the memory.
502 * Returns NULL if malloc fails.
504 static struct pxe_label *label_create(void)
506 struct pxe_label *label;
508 label = malloc(sizeof(struct pxe_label));
513 memset(label, 0, sizeof(struct pxe_label));
519 * Free the memory used by a pxe_label, including that used by its name,
520 * kernel, append and initrd members, if they're non NULL.
522 * So - be sure to only use dynamically allocated memory for the members of
523 * the pxe_label struct, unless you want to clean it up first. These are
524 * currently only created by the pxe file parsing code.
526 static void label_destroy(struct pxe_label *label)
550 * Print a label and its string members if they're defined.
552 * This is passed as a callback to the menu code for displaying each
555 static void label_print(void *data)
557 struct pxe_label *label = data;
558 const char *c = label->menu ? label->menu : label->name;
560 printf("%s:\t%s\n", label->num, c);
564 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
565 * environment variable is defined. Its contents will be executed as U-boot
566 * command. If the label specified an 'append' line, its contents will be
567 * used to overwrite the contents of the 'bootargs' environment variable prior
568 * to running 'localcmd'.
570 * Returns 1 on success or < 0 on error.
572 static int label_localboot(struct pxe_label *label)
576 localcmd = from_env("localcmd");
582 setenv("bootargs", label->append);
584 debug("running: %s\n", localcmd);
586 return run_command_list(localcmd, strlen(localcmd), 0);
590 * Boot according to the contents of a pxe_label.
592 * If we can't boot for any reason, we return. A successful boot never
595 * The kernel will be stored in the location given by the 'kernel_addr_r'
596 * environment variable.
598 * If the label specifies an initrd file, it will be stored in the location
599 * given by the 'ramdisk_addr_r' environment variable.
601 * If the label specifies an 'append' line, its contents will overwrite that
602 * of the 'bootargs' environment variable.
604 static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
606 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
608 char mac_str[29] = "";
609 char ip_str[68] = "";
618 label->attempted = 1;
620 if (label->localboot) {
621 if (label->localboot_val >= 0)
622 label_localboot(label);
626 if (label->kernel == NULL) {
627 printf("No kernel given, skipping %s\n",
633 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
634 printf("Skipping %s for failure retrieving initrd\n",
639 bootm_argv[2] = initrd_str;
640 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
641 strcat(bootm_argv[2], ":");
642 strcat(bootm_argv[2], getenv("filesize"));
647 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
648 printf("Skipping %s for failure retrieving kernel\n",
653 if (label->ipappend & 0x1) {
654 sprintf(ip_str, " ip=%s:%s:%s:%s",
655 getenv("ipaddr"), getenv("serverip"),
656 getenv("gatewayip"), getenv("netmask"));
657 len += strlen(ip_str);
660 #ifdef CONFIG_CMD_NET
661 if (label->ipappend & 0x2) {
663 strcpy(mac_str, " BOOTIF=");
664 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
667 len += strlen(mac_str);
672 len += strlen(label->append);
675 bootargs = malloc(len + 1);
680 strcpy(bootargs, label->append);
681 strcat(bootargs, ip_str);
682 strcat(bootargs, mac_str);
684 setenv("bootargs", bootargs);
685 printf("append: %s\n", bootargs);
690 bootm_argv[1] = getenv("kernel_addr_r");
693 * fdt usage is optional:
694 * It handles the following scenarios. All scenarios are exclusive
696 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
697 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
698 * and adjust argc appropriately.
700 * Scenario 2: If there is an fdt_addr specified, pass it along to
701 * bootm, and adjust argc appropriately.
703 * Scenario 3: fdt blob is not available.
705 bootm_argv[3] = getenv("fdt_addr_r");
707 /* if fdt label is defined then get fdt from server */
709 char *fdtfile = NULL;
710 char *fdtfilefree = NULL;
713 fdtfile = label->fdt;
714 } else if (label->fdtdir) {
715 char *f1, *f2, *f3, *f4, *slash;
717 f1 = getenv("fdtfile");
724 * For complex cases where this code doesn't
725 * generate the correct filename, the board
726 * code should set $fdtfile during early boot,
727 * or the boot scripts should set $fdtfile
728 * before invoking "pxe" or "sysboot".
732 f3 = getenv("board");
736 len = strlen(label->fdtdir);
739 else if (label->fdtdir[len - 1] != '/')
744 len = strlen(label->fdtdir) + strlen(slash) +
745 strlen(f1) + strlen(f2) + strlen(f3) +
747 fdtfilefree = malloc(len);
749 printf("malloc fail (FDT filename)\n");
753 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
754 label->fdtdir, slash, f1, f2, f3, f4);
755 fdtfile = fdtfilefree;
759 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
762 printf("Skipping %s for failure retrieving fdt\n",
767 bootm_argv[3] = NULL;
772 bootm_argv[3] = getenv("fdt_addr");
777 kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
778 buf = map_sysmem(kernel_addr, 0);
779 /* Try bootm for legacy and FIT format image */
780 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
781 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
782 #ifdef CONFIG_CMD_BOOTZ
783 /* Try booting a zImage */
785 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
791 * Tokens for the pxe file parser.
817 * A token - given by a value and a type.
821 enum token_type type;
825 * Keywords recognized.
827 static const struct token keywords[] = {
830 {"timeout", T_TIMEOUT},
831 {"default", T_DEFAULT},
832 {"prompt", T_PROMPT},
834 {"kernel", T_KERNEL},
836 {"localboot", T_LOCALBOOT},
837 {"append", T_APPEND},
838 {"initrd", T_INITRD},
839 {"include", T_INCLUDE},
840 {"devicetree", T_FDT},
842 {"devicetreedir", T_FDTDIR},
843 {"fdtdir", T_FDTDIR},
844 {"ontimeout", T_ONTIMEOUT,},
845 {"ipappend", T_IPAPPEND,},
850 * Since pxe(linux) files don't have a token to identify the start of a
851 * literal, we have to keep track of when we're in a state where a literal is
852 * expected vs when we're in a state a keyword is expected.
861 * get_string retrieves a string from *p and stores it as a token in
864 * get_string used for scanning both string literals and keywords.
866 * Characters from *p are copied into t-val until a character equal to
867 * delim is found, or a NUL byte is reached. If delim has the special value of
868 * ' ', any whitespace character will be used as a delimiter.
870 * If lower is unequal to 0, uppercase characters will be converted to
871 * lowercase in the result. This is useful to make keywords case
874 * The location of *p is updated to point to the first character after the end
875 * of the token - the ending delimiter.
877 * On success, the new value of t->val is returned. Memory for t->val is
878 * allocated using malloc and must be free()'d to reclaim it. If insufficient
879 * memory is available, NULL is returned.
881 static char *get_string(char **p, struct token *t, char delim, int lower)
887 * b and e both start at the beginning of the input stream.
889 * e is incremented until we find the ending delimiter, or a NUL byte
890 * is reached. Then, we take e - b to find the length of the token.
895 if ((delim == ' ' && isspace(*e)) || delim == *e)
903 * Allocate memory to hold the string, and copy it in, converting
904 * characters to lowercase if lower is != 0.
906 t->val = malloc(len + 1);
910 for (i = 0; i < len; i++, b++) {
912 t->val[i] = tolower(*b);
920 * Update *p so the caller knows where to continue scanning.
930 * Populate a keyword token with a type and value.
932 static void get_keyword(struct token *t)
936 for (i = 0; keywords[i].val; i++) {
937 if (!strcmp(t->val, keywords[i].val)) {
938 t->type = keywords[i].type;
945 * Get the next token. We have to keep track of which state we're in to know
946 * if we're looking to get a string literal or a keyword.
948 * *p is updated to point at the first character after the current token.
950 static void get_token(char **p, struct token *t, enum lex_state state)
956 /* eat non EOL whitespace */
961 * eat comments. note that string literals can't begin with #, but
962 * can contain a # after their first character.
965 while (*c && *c != '\n')
972 } else if (*c == '\0') {
975 } else if (state == L_SLITERAL) {
976 get_string(&c, t, '\n', 0);
977 } else if (state == L_KEYWORD) {
979 * when we expect a keyword, we first get the next string
980 * token delimited by whitespace, and then check if it
981 * matches a keyword in our keyword list. if it does, it's
982 * converted to a keyword token of the appropriate type, and
983 * if not, it remains a string token.
985 get_string(&c, t, ' ', 1);
993 * Increment *c until we get to the end of the current line, or EOF.
995 static void eol_or_eof(char **c)
997 while (**c && **c != '\n')
1002 * All of these parse_* functions share some common behavior.
1004 * They finish with *c pointing after the token they parse, and return 1 on
1005 * success, or < 0 on error.
1009 * Parse a string literal and store a pointer it at *dst. String literals
1010 * terminate at the end of the line.
1012 static int parse_sliteral(char **c, char **dst)
1017 get_token(c, &t, L_SLITERAL);
1019 if (t.type != T_STRING) {
1020 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1030 * Parse a base 10 (unsigned) integer and store it at *dst.
1032 static int parse_integer(char **c, int *dst)
1037 get_token(c, &t, L_SLITERAL);
1039 if (t.type != T_STRING) {
1040 printf("Expected string: %.*s\n", (int)(*c - s), s);
1044 *dst = simple_strtol(t.val, NULL, 10);
1051 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
1054 * Parse an include statement, and retrieve and parse the file it mentions.
1056 * base should point to a location where it's safe to store the file, and
1057 * nest_level should indicate how many nested includes have occurred. For this
1058 * include, nest_level has already been incremented and doesn't need to be
1061 static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
1062 struct pxe_menu *cfg, int nest_level)
1068 err = parse_sliteral(c, &include_path);
1071 printf("Expected include path: %.*s\n",
1076 err = get_pxe_file(cmdtp, include_path, base);
1079 printf("Couldn't retrieve %s\n", include_path);
1083 return parse_pxefile_top(cmdtp, base, cfg, nest_level);
1087 * Parse lines that begin with 'menu'.
1089 * b and nest are provided to handle the 'menu include' case.
1091 * b should be the address where the file currently being parsed is stored.
1093 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1094 * a file it includes, 3 when parsing a file included by that file, and so on.
1096 static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg, char *b, int nest_level)
1102 get_token(c, &t, L_KEYWORD);
1106 err = parse_sliteral(c, &cfg->title);
1111 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
1116 printf("Ignoring malformed menu command: %.*s\n",
1129 * Handles parsing a 'menu line' when we're parsing a label.
1131 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1132 struct pxe_label *label)
1139 get_token(c, &t, L_KEYWORD);
1143 if (!cfg->default_label)
1144 cfg->default_label = strdup(label->name);
1146 if (!cfg->default_label)
1151 parse_sliteral(c, &label->menu);
1154 printf("Ignoring malformed menu command: %.*s\n",
1164 * Parses a label and adds it to the list of labels for a menu.
1166 * A label ends when we either get to the end of a file, or
1167 * get some input we otherwise don't have a handler defined
1171 static int parse_label(char **c, struct pxe_menu *cfg)
1176 struct pxe_label *label;
1179 label = label_create();
1183 err = parse_sliteral(c, &label->name);
1185 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1186 label_destroy(label);
1190 list_add_tail(&label->list, &cfg->labels);
1194 get_token(c, &t, L_KEYWORD);
1199 err = parse_label_menu(c, cfg, label);
1204 err = parse_sliteral(c, &label->kernel);
1208 err = parse_sliteral(c, &label->append);
1211 s = strstr(label->append, "initrd=");
1215 len = (int)(strchr(s, ' ') - s);
1216 label->initrd = malloc(len + 1);
1217 strncpy(label->initrd, s, len);
1218 label->initrd[len] = '\0';
1224 err = parse_sliteral(c, &label->initrd);
1229 err = parse_sliteral(c, &label->fdt);
1234 err = parse_sliteral(c, &label->fdtdir);
1238 label->localboot = 1;
1239 err = parse_integer(c, &label->localboot_val);
1243 err = parse_integer(c, &label->ipappend);
1251 * put the token back! we don't want it - it's the end
1252 * of a label and whatever token this is, it's
1253 * something for the menu level context to handle.
1265 * This 16 comes from the limit pxelinux imposes on nested includes.
1267 * There is no reason at all we couldn't do more, but some limit helps prevent
1268 * infinite (until crash occurs) recursion if a file tries to include itself.
1270 #define MAX_NEST_LEVEL 16
1273 * Entry point for parsing a menu file. nest_level indicates how many times
1274 * we've nested in includes. It will be 1 for the top level menu file.
1276 * Returns 1 on success, < 0 on error.
1278 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
1281 char *s, *b, *label_name;
1286 if (nest_level > MAX_NEST_LEVEL) {
1287 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1294 get_token(&p, &t, L_KEYWORD);
1300 err = parse_menu(cmdtp, &p, cfg, b, nest_level);
1304 err = parse_integer(&p, &cfg->timeout);
1308 err = parse_label(&p, cfg);
1313 err = parse_sliteral(&p, &label_name);
1316 if (cfg->default_label)
1317 free(cfg->default_label);
1319 cfg->default_label = label_name;
1325 err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
1340 printf("Ignoring unknown command: %.*s\n",
1351 * Free the memory used by a pxe_menu and its labels.
1353 static void destroy_pxe_menu(struct pxe_menu *cfg)
1355 struct list_head *pos, *n;
1356 struct pxe_label *label;
1361 if (cfg->default_label)
1362 free(cfg->default_label);
1364 list_for_each_safe(pos, n, &cfg->labels) {
1365 label = list_entry(pos, struct pxe_label, list);
1367 label_destroy(label);
1374 * Entry point for parsing a pxe file. This is only used for the top level
1377 * Returns NULL if there is an error, otherwise, returns a pointer to a
1378 * pxe_menu struct populated with the results of parsing the pxe file (and any
1379 * files it includes). The resulting pxe_menu struct can be free()'d by using
1380 * the destroy_pxe_menu() function.
1382 static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
1384 struct pxe_menu *cfg;
1386 cfg = malloc(sizeof(struct pxe_menu));
1391 memset(cfg, 0, sizeof(struct pxe_menu));
1393 INIT_LIST_HEAD(&cfg->labels);
1395 if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
1396 destroy_pxe_menu(cfg);
1404 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1407 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1409 struct pxe_label *label;
1410 struct list_head *pos;
1414 char *default_num = NULL;
1417 * Create a menu and add items for all the labels.
1419 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1425 list_for_each(pos, &cfg->labels) {
1426 label = list_entry(pos, struct pxe_label, list);
1428 sprintf(label->num, "%d", i++);
1429 if (menu_item_add(m, label->num, label) != 1) {
1433 if (cfg->default_label &&
1434 (strcmp(label->name, cfg->default_label) == 0))
1435 default_num = label->num;
1440 * After we've created items for each label in the menu, set the
1441 * menu's default label if one was specified.
1444 err = menu_default_set(m, default_num);
1446 if (err != -ENOENT) {
1451 printf("Missing default: %s\n", cfg->default_label);
1459 * Try to boot any labels we have yet to attempt to boot.
1461 static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1463 struct list_head *pos;
1464 struct pxe_label *label;
1466 list_for_each(pos, &cfg->labels) {
1467 label = list_entry(pos, struct pxe_label, list);
1469 if (!label->attempted)
1470 label_boot(cmdtp, label);
1475 * Boot the system as prescribed by a pxe_menu.
1477 * Use the menu system to either get the user's choice or the default, based
1478 * on config or user input. If there is no default or user's choice,
1479 * attempted to boot labels in the order they were given in pxe files.
1480 * If the default or user's choice fails to boot, attempt to boot other
1481 * labels in the order they were given in pxe files.
1483 * If this function returns, there weren't any labels that successfully
1484 * booted, or the user interrupted the menu selection via ctrl+c.
1486 static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1492 m = pxe_menu_to_menu(cfg);
1496 err = menu_get_choice(m, &choice);
1501 * err == 1 means we got a choice back from menu_get_choice.
1503 * err == -ENOENT if the menu was setup to select the default but no
1504 * default was set. in that case, we should continue trying to boot
1505 * labels that haven't been attempted yet.
1507 * otherwise, the user interrupted or there was some other error and
1512 err = label_boot(cmdtp, choice);
1515 } else if (err != -ENOENT) {
1519 boot_unattempted_labels(cmdtp, cfg);
1522 #ifdef CONFIG_CMD_NET
1524 * Boots a system using a pxe file
1526 * Returns 0 on success, 1 on error.
1529 do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1531 unsigned long pxefile_addr_r;
1532 struct pxe_menu *cfg;
1533 char *pxefile_addr_str;
1535 do_getfile = do_get_tftp;
1538 pxefile_addr_str = from_env("pxefile_addr_r");
1539 if (!pxefile_addr_str)
1542 } else if (argc == 2) {
1543 pxefile_addr_str = argv[1];
1545 return CMD_RET_USAGE;
1548 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1549 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1553 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
1556 printf("Error parsing config file\n");
1560 handle_pxe_menu(cmdtp, cfg);
1562 destroy_pxe_menu(cfg);
1564 copy_filename(BootFile, "", sizeof(BootFile));
1569 static cmd_tbl_t cmd_pxe_sub[] = {
1570 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1571 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1574 static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1579 return CMD_RET_USAGE;
1583 /* drop initial "pxe" arg */
1587 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1590 return cp->cmd(cmdtp, flag, argc, argv);
1592 return CMD_RET_USAGE;
1597 "commands to get and boot from pxe files",
1598 "get - try to retrieve a pxe file using tftp\npxe "
1599 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1604 * Boots a system using a local disk syslinux/extlinux file
1606 * Returns 0 on success, 1 on error.
1608 static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1610 unsigned long pxefile_addr_r;
1611 struct pxe_menu *cfg;
1612 char *pxefile_addr_str;
1618 if (strstr(argv[1], "-p")) {
1625 return cmd_usage(cmdtp);
1628 pxefile_addr_str = from_env("pxefile_addr_r");
1629 if (!pxefile_addr_str)
1632 pxefile_addr_str = argv[4];
1636 filename = getenv("bootfile");
1639 setenv("bootfile", filename);
1642 if (strstr(argv[3], "ext2"))
1643 do_getfile = do_get_ext2;
1644 else if (strstr(argv[3], "fat"))
1645 do_getfile = do_get_fat;
1646 else if (strstr(argv[3], "any"))
1647 do_getfile = do_get_any;
1649 printf("Invalid filesystem: %s\n", argv[3]);
1652 fs_argv[1] = argv[1];
1653 fs_argv[2] = argv[2];
1655 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1656 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1660 if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
1661 printf("Error reading config file\n");
1665 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
1668 printf("Error parsing config file\n");
1675 handle_pxe_menu(cmdtp, cfg);
1677 destroy_pxe_menu(cfg);
1683 sysboot, 7, 1, do_sysboot,
1684 "command to get and boot from syslinux files",
1685 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1686 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1687 " or any filesystem on 'dev' on 'interface' to address 'addr'"