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>
20 #define MAX_TFTP_PATH_LEN 127
22 const char *pxe_default_paths[] = {
24 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
26 "default-" CONFIG_SYS_ARCH,
34 * Like getenv, but prints an error if envvar isn't defined in the
35 * environment. It always returns what getenv does, so it can be used in
36 * place of getenv without changing error handling otherwise.
38 static char *from_env(const char *envvar)
45 printf("missing environment variable: %s\n", envvar);
52 * Convert an ethaddr from the environment to the format used by pxelinux
53 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
54 * the beginning of the ethernet address to indicate a hardware type of
55 * Ethernet. Also converts uppercase hex characters into lowercase, to match
56 * pxelinux's behavior.
58 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
59 * environment, or some other value < 0 on error.
61 static int format_mac_pxe(char *outbuf, size_t outbuf_len)
65 if (outbuf_len < 21) {
66 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
71 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
75 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
76 ethaddr[0], ethaddr[1], ethaddr[2],
77 ethaddr[3], ethaddr[4], ethaddr[5]);
84 * Returns the directory the file specified in the bootfile env variable is
85 * in. If bootfile isn't defined in the environment, return NULL, which should
86 * be interpreted as "don't prepend anything to paths".
88 static int get_bootfile_path(const char *file_path, char *bootfile_path,
89 size_t bootfile_path_size)
91 char *bootfile, *last_slash;
94 /* Only syslinux allows absolute paths */
95 if (file_path[0] == '/' && !is_pxe)
98 bootfile = from_env("bootfile");
103 last_slash = strrchr(bootfile, '/');
105 if (last_slash == NULL)
108 path_len = (last_slash - bootfile) + 1;
110 if (bootfile_path_size < path_len) {
111 printf("bootfile_path too small. (%zd < %zd)\n",
112 bootfile_path_size, path_len);
117 strncpy(bootfile_path, bootfile, path_len);
120 bootfile_path[path_len] = '\0';
125 static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
127 #ifdef CONFIG_CMD_NET
128 static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
130 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
132 tftp_argv[1] = file_addr;
133 tftp_argv[2] = (void *)file_path;
135 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
142 static char *fs_argv[5];
144 static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
146 #ifdef CONFIG_CMD_EXT2
147 fs_argv[0] = "ext2load";
148 fs_argv[3] = file_addr;
149 fs_argv[4] = (void *)file_path;
151 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
157 static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
159 #ifdef CONFIG_CMD_FAT
160 fs_argv[0] = "fatload";
161 fs_argv[3] = file_addr;
162 fs_argv[4] = (void *)file_path;
164 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
170 static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
172 #ifdef CONFIG_CMD_FS_GENERIC
174 fs_argv[3] = file_addr;
175 fs_argv[4] = (void *)file_path;
177 if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
184 * As in pxelinux, paths to files referenced from files we retrieve are
185 * relative to the location of bootfile. get_relfile takes such a path and
186 * joins it with the bootfile path to get the full path to the target file. If
187 * the bootfile path is NULL, we use file_path as is.
189 * Returns 1 for success, or < 0 on error.
191 static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
194 char relfile[MAX_TFTP_PATH_LEN+1];
198 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
203 path_len = strlen(file_path);
204 path_len += strlen(relfile);
206 if (path_len > MAX_TFTP_PATH_LEN) {
207 printf("Base path too long (%s%s)\n",
211 return -ENAMETOOLONG;
214 strcat(relfile, file_path);
216 printf("Retrieving file: %s\n", relfile);
218 sprintf(addr_buf, "%p", file_addr);
220 return do_getfile(cmdtp, relfile, addr_buf);
224 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
225 * 'bootfile' was specified in the environment, the path to bootfile will be
226 * prepended to 'file_path' and the resulting path will be used.
228 * Returns 1 on success, or < 0 for error.
230 static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
232 unsigned long config_file_size;
236 err = get_relfile(cmdtp, file_path, file_addr);
242 * the file comes without a NUL byte at the end, so find out its size
243 * and add the NUL byte.
245 tftp_filesize = from_env("filesize");
250 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
253 *(char *)(file_addr + config_file_size) = '\0';
258 #ifdef CONFIG_CMD_NET
260 #define PXELINUX_DIR "pxelinux.cfg/"
263 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
264 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
265 * from the bootfile path, as described above.
267 * Returns 1 on success or < 0 on error.
269 static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r)
271 size_t base_len = strlen(PXELINUX_DIR);
272 char path[MAX_TFTP_PATH_LEN+1];
274 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
275 printf("path (%s%s) too long, skipping\n",
277 return -ENAMETOOLONG;
280 sprintf(path, PXELINUX_DIR "%s", file);
282 return get_pxe_file(cmdtp, path, pxefile_addr_r);
286 * Looks for a pxe file with a name based on the pxeuuid environment variable.
288 * Returns 1 on success or < 0 on error.
290 static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
294 uuid_str = from_env("pxeuuid");
299 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
303 * Looks for a pxe file with a name based on the 'ethaddr' environment
306 * Returns 1 on success or < 0 on error.
308 static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
313 err = format_mac_pxe(mac_str, sizeof(mac_str));
318 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
322 * Looks for pxe files with names based on our IP address. See pxelinux
323 * documentation for details on what these file names look like. We match
326 * Returns 1 on success or < 0 on error.
328 static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
333 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
335 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
336 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
341 ip_addr[mask_pos] = '\0';
348 * Entry point for the 'pxe get' command.
349 * This Follows pxelinux's rules to download a config file from a tftp server.
350 * The file is stored at the location given by the pxefile_addr_r environment
351 * variable, which must be set.
353 * UUID comes from pxeuuid env variable, if defined
354 * MAC addr comes from ethaddr env variable, if defined
357 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
359 * Returns 0 on success or 1 on error.
362 do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
364 char *pxefile_addr_str;
365 unsigned long pxefile_addr_r;
368 do_getfile = do_get_tftp;
371 return CMD_RET_USAGE;
373 pxefile_addr_str = from_env("pxefile_addr_r");
375 if (!pxefile_addr_str)
378 err = strict_strtoul(pxefile_addr_str, 16,
379 (unsigned long *)&pxefile_addr_r);
384 * Keep trying paths until we successfully get a file we're looking
387 if (pxe_uuid_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
388 pxe_mac_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
389 pxe_ipaddr_paths(cmdtp, (void *)pxefile_addr_r) > 0) {
390 printf("Config file found\n");
395 while (pxe_default_paths[i]) {
396 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
397 (void *)pxefile_addr_r) > 0) {
398 printf("Config file found\n");
404 printf("Config file not found\n");
411 * Wrapper to make it easier to store the file at file_path in the location
412 * specified by envaddr_name. file_path will be joined to the bootfile path,
413 * if any is specified.
415 * Returns 1 on success or < 0 on error.
417 static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
419 unsigned long file_addr;
422 envaddr = from_env(envaddr_name);
427 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
430 return get_relfile(cmdtp, file_path, (void *)file_addr);
434 * A note on the pxe file parser.
436 * We're parsing files that use syslinux grammar, which has a few quirks.
437 * String literals must be recognized based on context - there is no
438 * quoting or escaping support. There's also nothing to explicitly indicate
439 * when a label section completes. We deal with that by ending a label
440 * section whenever we see a line that doesn't include.
442 * As with the syslinux family, this same file format could be reused in the
443 * future for non pxe purposes. The only action it takes during parsing that
444 * would throw this off is handling of include files. It assumes we're using
445 * pxe, and does a tftp download of a file listed as an include file in the
446 * middle of the parsing operation. That could be handled by refactoring it to
447 * take a 'include file getter' function.
451 * Describes a single label given in a pxe file.
453 * Create these with the 'label_create' function given below.
455 * name - the name of the menu as given on the 'menu label' line.
456 * kernel - the path to the kernel file to use for this label.
457 * append - kernel command line to use when booting this label
458 * initrd - path to the initrd to use for this label.
459 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
460 * localboot - 1 if this label specified 'localboot', 0 otherwise.
461 * list - lets these form a list, which a pxe_menu struct will hold.
476 struct list_head list;
480 * Describes a pxe menu as given via pxe files.
482 * title - the name of the menu as given by a 'menu title' line.
483 * default_label - the name of the default label, if any.
484 * timeout - time in tenths of a second to wait for a user key-press before
485 * booting the default label.
486 * prompt - if 0, don't prompt for a choice unless the timeout period is
487 * interrupted. If 1, always prompt for a choice regardless of
489 * labels - a list of labels defined for the menu.
496 struct list_head labels;
500 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
501 * result must be free()'d to reclaim the memory.
503 * Returns NULL if malloc fails.
505 static struct pxe_label *label_create(void)
507 struct pxe_label *label;
509 label = malloc(sizeof(struct pxe_label));
514 memset(label, 0, sizeof(struct pxe_label));
520 * Free the memory used by a pxe_label, including that used by its name,
521 * kernel, append and initrd members, if they're non NULL.
523 * So - be sure to only use dynamically allocated memory for the members of
524 * the pxe_label struct, unless you want to clean it up first. These are
525 * currently only created by the pxe file parsing code.
527 static void label_destroy(struct pxe_label *label)
551 * Print a label and its string members if they're defined.
553 * This is passed as a callback to the menu code for displaying each
556 static void label_print(void *data)
558 struct pxe_label *label = data;
559 const char *c = label->menu ? label->menu : label->name;
561 printf("%s:\t%s\n", label->num, c);
565 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
566 * environment variable is defined. Its contents will be executed as U-boot
567 * command. If the label specified an 'append' line, its contents will be
568 * used to overwrite the contents of the 'bootargs' environment variable prior
569 * to running 'localcmd'.
571 * Returns 1 on success or < 0 on error.
573 static int label_localboot(struct pxe_label *label)
577 localcmd = from_env("localcmd");
583 char bootargs[CONFIG_SYS_CBSIZE];
585 cli_simple_process_macros(label->append, bootargs);
586 setenv("bootargs", bootargs);
589 debug("running: %s\n", localcmd);
591 return run_command_list(localcmd, strlen(localcmd), 0);
595 * Boot according to the contents of a pxe_label.
597 * If we can't boot for any reason, we return. A successful boot never
600 * The kernel will be stored in the location given by the 'kernel_addr_r'
601 * environment variable.
603 * If the label specifies an initrd file, it will be stored in the location
604 * given by the 'ramdisk_addr_r' environment variable.
606 * If the label specifies an 'append' line, its contents will overwrite that
607 * of the 'bootargs' environment variable.
609 static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
611 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
613 char mac_str[29] = "";
614 char ip_str[68] = "";
622 label->attempted = 1;
624 if (label->localboot) {
625 if (label->localboot_val >= 0)
626 label_localboot(label);
630 if (label->kernel == NULL) {
631 printf("No kernel given, skipping %s\n",
637 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
638 printf("Skipping %s for failure retrieving initrd\n",
643 bootm_argv[2] = initrd_str;
644 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
645 strcat(bootm_argv[2], ":");
646 strcat(bootm_argv[2], getenv("filesize"));
651 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
652 printf("Skipping %s for failure retrieving kernel\n",
657 if (label->ipappend & 0x1) {
658 sprintf(ip_str, " ip=%s:%s:%s:%s",
659 getenv("ipaddr"), getenv("serverip"),
660 getenv("gatewayip"), getenv("netmask"));
663 #ifdef CONFIG_CMD_NET
664 if (label->ipappend & 0x2) {
666 strcpy(mac_str, " BOOTIF=");
667 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
673 if ((label->ipappend & 0x3) || label->append) {
674 char bootargs[CONFIG_SYS_CBSIZE] = "";
675 char finalbootargs[CONFIG_SYS_CBSIZE];
678 strcpy(bootargs, label->append);
679 strcat(bootargs, ip_str);
680 strcat(bootargs, mac_str);
682 cli_simple_process_macros(bootargs, finalbootargs);
683 setenv("bootargs", finalbootargs);
684 printf("append: %s\n", finalbootargs);
687 bootm_argv[1] = getenv("kernel_addr_r");
690 * fdt usage is optional:
691 * It handles the following scenarios. All scenarios are exclusive
693 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
694 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
695 * and adjust argc appropriately.
697 * Scenario 2: If there is an fdt_addr specified, pass it along to
698 * bootm, and adjust argc appropriately.
700 * Scenario 3: fdt blob is not available.
702 bootm_argv[3] = getenv("fdt_addr_r");
704 /* if fdt label is defined then get fdt from server */
706 char *fdtfile = NULL;
707 char *fdtfilefree = NULL;
710 fdtfile = label->fdt;
711 } else if (label->fdtdir) {
712 char *f1, *f2, *f3, *f4, *slash;
714 f1 = getenv("fdtfile");
721 * For complex cases where this code doesn't
722 * generate the correct filename, the board
723 * code should set $fdtfile during early boot,
724 * or the boot scripts should set $fdtfile
725 * before invoking "pxe" or "sysboot".
729 f3 = getenv("board");
733 len = strlen(label->fdtdir);
736 else if (label->fdtdir[len - 1] != '/')
741 len = strlen(label->fdtdir) + strlen(slash) +
742 strlen(f1) + strlen(f2) + strlen(f3) +
744 fdtfilefree = malloc(len);
746 printf("malloc fail (FDT filename)\n");
750 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
751 label->fdtdir, slash, f1, f2, f3, f4);
752 fdtfile = fdtfilefree;
756 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
759 printf("Skipping %s for failure retrieving fdt\n",
764 bootm_argv[3] = NULL;
769 bootm_argv[3] = getenv("fdt_addr");
774 kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
775 buf = map_sysmem(kernel_addr, 0);
776 /* Try bootm for legacy and FIT format image */
777 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
778 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
779 #ifdef CONFIG_CMD_BOOTZ
780 /* Try booting a zImage */
782 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
788 * Tokens for the pxe file parser.
814 * A token - given by a value and a type.
818 enum token_type type;
822 * Keywords recognized.
824 static const struct token keywords[] = {
827 {"timeout", T_TIMEOUT},
828 {"default", T_DEFAULT},
829 {"prompt", T_PROMPT},
831 {"kernel", T_KERNEL},
833 {"localboot", T_LOCALBOOT},
834 {"append", T_APPEND},
835 {"initrd", T_INITRD},
836 {"include", T_INCLUDE},
837 {"devicetree", T_FDT},
839 {"devicetreedir", T_FDTDIR},
840 {"fdtdir", T_FDTDIR},
841 {"ontimeout", T_ONTIMEOUT,},
842 {"ipappend", T_IPAPPEND,},
847 * Since pxe(linux) files don't have a token to identify the start of a
848 * literal, we have to keep track of when we're in a state where a literal is
849 * expected vs when we're in a state a keyword is expected.
858 * get_string retrieves a string from *p and stores it as a token in
861 * get_string used for scanning both string literals and keywords.
863 * Characters from *p are copied into t-val until a character equal to
864 * delim is found, or a NUL byte is reached. If delim has the special value of
865 * ' ', any whitespace character will be used as a delimiter.
867 * If lower is unequal to 0, uppercase characters will be converted to
868 * lowercase in the result. This is useful to make keywords case
871 * The location of *p is updated to point to the first character after the end
872 * of the token - the ending delimiter.
874 * On success, the new value of t->val is returned. Memory for t->val is
875 * allocated using malloc and must be free()'d to reclaim it. If insufficient
876 * memory is available, NULL is returned.
878 static char *get_string(char **p, struct token *t, char delim, int lower)
884 * b and e both start at the beginning of the input stream.
886 * e is incremented until we find the ending delimiter, or a NUL byte
887 * is reached. Then, we take e - b to find the length of the token.
892 if ((delim == ' ' && isspace(*e)) || delim == *e)
900 * Allocate memory to hold the string, and copy it in, converting
901 * characters to lowercase if lower is != 0.
903 t->val = malloc(len + 1);
907 for (i = 0; i < len; i++, b++) {
909 t->val[i] = tolower(*b);
917 * Update *p so the caller knows where to continue scanning.
927 * Populate a keyword token with a type and value.
929 static void get_keyword(struct token *t)
933 for (i = 0; keywords[i].val; i++) {
934 if (!strcmp(t->val, keywords[i].val)) {
935 t->type = keywords[i].type;
942 * Get the next token. We have to keep track of which state we're in to know
943 * if we're looking to get a string literal or a keyword.
945 * *p is updated to point at the first character after the current token.
947 static void get_token(char **p, struct token *t, enum lex_state state)
953 /* eat non EOL whitespace */
958 * eat comments. note that string literals can't begin with #, but
959 * can contain a # after their first character.
962 while (*c && *c != '\n')
969 } else if (*c == '\0') {
972 } else if (state == L_SLITERAL) {
973 get_string(&c, t, '\n', 0);
974 } else if (state == L_KEYWORD) {
976 * when we expect a keyword, we first get the next string
977 * token delimited by whitespace, and then check if it
978 * matches a keyword in our keyword list. if it does, it's
979 * converted to a keyword token of the appropriate type, and
980 * if not, it remains a string token.
982 get_string(&c, t, ' ', 1);
990 * Increment *c until we get to the end of the current line, or EOF.
992 static void eol_or_eof(char **c)
994 while (**c && **c != '\n')
999 * All of these parse_* functions share some common behavior.
1001 * They finish with *c pointing after the token they parse, and return 1 on
1002 * success, or < 0 on error.
1006 * Parse a string literal and store a pointer it at *dst. String literals
1007 * terminate at the end of the line.
1009 static int parse_sliteral(char **c, char **dst)
1014 get_token(c, &t, L_SLITERAL);
1016 if (t.type != T_STRING) {
1017 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1027 * Parse a base 10 (unsigned) integer and store it at *dst.
1029 static int parse_integer(char **c, int *dst)
1034 get_token(c, &t, L_SLITERAL);
1036 if (t.type != T_STRING) {
1037 printf("Expected string: %.*s\n", (int)(*c - s), s);
1041 *dst = simple_strtol(t.val, NULL, 10);
1048 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
1051 * Parse an include statement, and retrieve and parse the file it mentions.
1053 * base should point to a location where it's safe to store the file, and
1054 * nest_level should indicate how many nested includes have occurred. For this
1055 * include, nest_level has already been incremented and doesn't need to be
1058 static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
1059 struct pxe_menu *cfg, int nest_level)
1065 err = parse_sliteral(c, &include_path);
1068 printf("Expected include path: %.*s\n",
1073 err = get_pxe_file(cmdtp, include_path, base);
1076 printf("Couldn't retrieve %s\n", include_path);
1080 return parse_pxefile_top(cmdtp, base, cfg, nest_level);
1084 * Parse lines that begin with 'menu'.
1086 * b and nest are provided to handle the 'menu include' case.
1088 * b should be the address where the file currently being parsed is stored.
1090 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1091 * a file it includes, 3 when parsing a file included by that file, and so on.
1093 static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg, char *b, int nest_level)
1099 get_token(c, &t, L_KEYWORD);
1103 err = parse_sliteral(c, &cfg->title);
1108 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
1113 printf("Ignoring malformed menu command: %.*s\n",
1126 * Handles parsing a 'menu line' when we're parsing a label.
1128 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1129 struct pxe_label *label)
1136 get_token(c, &t, L_KEYWORD);
1140 if (!cfg->default_label)
1141 cfg->default_label = strdup(label->name);
1143 if (!cfg->default_label)
1148 parse_sliteral(c, &label->menu);
1151 printf("Ignoring malformed menu command: %.*s\n",
1161 * Parses a label and adds it to the list of labels for a menu.
1163 * A label ends when we either get to the end of a file, or
1164 * get some input we otherwise don't have a handler defined
1168 static int parse_label(char **c, struct pxe_menu *cfg)
1173 struct pxe_label *label;
1176 label = label_create();
1180 err = parse_sliteral(c, &label->name);
1182 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1183 label_destroy(label);
1187 list_add_tail(&label->list, &cfg->labels);
1191 get_token(c, &t, L_KEYWORD);
1196 err = parse_label_menu(c, cfg, label);
1201 err = parse_sliteral(c, &label->kernel);
1205 err = parse_sliteral(c, &label->append);
1208 s = strstr(label->append, "initrd=");
1212 len = (int)(strchr(s, ' ') - s);
1213 label->initrd = malloc(len + 1);
1214 strncpy(label->initrd, s, len);
1215 label->initrd[len] = '\0';
1221 err = parse_sliteral(c, &label->initrd);
1226 err = parse_sliteral(c, &label->fdt);
1231 err = parse_sliteral(c, &label->fdtdir);
1235 label->localboot = 1;
1236 err = parse_integer(c, &label->localboot_val);
1240 err = parse_integer(c, &label->ipappend);
1248 * put the token back! we don't want it - it's the end
1249 * of a label and whatever token this is, it's
1250 * something for the menu level context to handle.
1262 * This 16 comes from the limit pxelinux imposes on nested includes.
1264 * There is no reason at all we couldn't do more, but some limit helps prevent
1265 * infinite (until crash occurs) recursion if a file tries to include itself.
1267 #define MAX_NEST_LEVEL 16
1270 * Entry point for parsing a menu file. nest_level indicates how many times
1271 * we've nested in includes. It will be 1 for the top level menu file.
1273 * Returns 1 on success, < 0 on error.
1275 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
1278 char *s, *b, *label_name;
1283 if (nest_level > MAX_NEST_LEVEL) {
1284 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1291 get_token(&p, &t, L_KEYWORD);
1297 err = parse_menu(cmdtp, &p, cfg, b, nest_level);
1301 err = parse_integer(&p, &cfg->timeout);
1305 err = parse_label(&p, cfg);
1310 err = parse_sliteral(&p, &label_name);
1313 if (cfg->default_label)
1314 free(cfg->default_label);
1316 cfg->default_label = label_name;
1322 err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
1337 printf("Ignoring unknown command: %.*s\n",
1348 * Free the memory used by a pxe_menu and its labels.
1350 static void destroy_pxe_menu(struct pxe_menu *cfg)
1352 struct list_head *pos, *n;
1353 struct pxe_label *label;
1358 if (cfg->default_label)
1359 free(cfg->default_label);
1361 list_for_each_safe(pos, n, &cfg->labels) {
1362 label = list_entry(pos, struct pxe_label, list);
1364 label_destroy(label);
1371 * Entry point for parsing a pxe file. This is only used for the top level
1374 * Returns NULL if there is an error, otherwise, returns a pointer to a
1375 * pxe_menu struct populated with the results of parsing the pxe file (and any
1376 * files it includes). The resulting pxe_menu struct can be free()'d by using
1377 * the destroy_pxe_menu() function.
1379 static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
1381 struct pxe_menu *cfg;
1383 cfg = malloc(sizeof(struct pxe_menu));
1388 memset(cfg, 0, sizeof(struct pxe_menu));
1390 INIT_LIST_HEAD(&cfg->labels);
1392 if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
1393 destroy_pxe_menu(cfg);
1401 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1404 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1406 struct pxe_label *label;
1407 struct list_head *pos;
1411 char *default_num = NULL;
1414 * Create a menu and add items for all the labels.
1416 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1422 list_for_each(pos, &cfg->labels) {
1423 label = list_entry(pos, struct pxe_label, list);
1425 sprintf(label->num, "%d", i++);
1426 if (menu_item_add(m, label->num, label) != 1) {
1430 if (cfg->default_label &&
1431 (strcmp(label->name, cfg->default_label) == 0))
1432 default_num = label->num;
1437 * After we've created items for each label in the menu, set the
1438 * menu's default label if one was specified.
1441 err = menu_default_set(m, default_num);
1443 if (err != -ENOENT) {
1448 printf("Missing default: %s\n", cfg->default_label);
1456 * Try to boot any labels we have yet to attempt to boot.
1458 static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1460 struct list_head *pos;
1461 struct pxe_label *label;
1463 list_for_each(pos, &cfg->labels) {
1464 label = list_entry(pos, struct pxe_label, list);
1466 if (!label->attempted)
1467 label_boot(cmdtp, label);
1472 * Boot the system as prescribed by a pxe_menu.
1474 * Use the menu system to either get the user's choice or the default, based
1475 * on config or user input. If there is no default or user's choice,
1476 * attempted to boot labels in the order they were given in pxe files.
1477 * If the default or user's choice fails to boot, attempt to boot other
1478 * labels in the order they were given in pxe files.
1480 * If this function returns, there weren't any labels that successfully
1481 * booted, or the user interrupted the menu selection via ctrl+c.
1483 static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1489 m = pxe_menu_to_menu(cfg);
1493 err = menu_get_choice(m, &choice);
1498 * err == 1 means we got a choice back from menu_get_choice.
1500 * err == -ENOENT if the menu was setup to select the default but no
1501 * default was set. in that case, we should continue trying to boot
1502 * labels that haven't been attempted yet.
1504 * otherwise, the user interrupted or there was some other error and
1509 err = label_boot(cmdtp, choice);
1512 } else if (err != -ENOENT) {
1516 boot_unattempted_labels(cmdtp, cfg);
1519 #ifdef CONFIG_CMD_NET
1521 * Boots a system using a pxe file
1523 * Returns 0 on success, 1 on error.
1526 do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1528 unsigned long pxefile_addr_r;
1529 struct pxe_menu *cfg;
1530 char *pxefile_addr_str;
1532 do_getfile = do_get_tftp;
1535 pxefile_addr_str = from_env("pxefile_addr_r");
1536 if (!pxefile_addr_str)
1539 } else if (argc == 2) {
1540 pxefile_addr_str = argv[1];
1542 return CMD_RET_USAGE;
1545 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1546 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1550 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
1553 printf("Error parsing config file\n");
1557 handle_pxe_menu(cmdtp, cfg);
1559 destroy_pxe_menu(cfg);
1561 copy_filename(BootFile, "", sizeof(BootFile));
1566 static cmd_tbl_t cmd_pxe_sub[] = {
1567 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1568 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1571 static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1576 return CMD_RET_USAGE;
1580 /* drop initial "pxe" arg */
1584 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1587 return cp->cmd(cmdtp, flag, argc, argv);
1589 return CMD_RET_USAGE;
1594 "commands to get and boot from pxe files",
1595 "get - try to retrieve a pxe file using tftp\npxe "
1596 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1601 * Boots a system using a local disk syslinux/extlinux file
1603 * Returns 0 on success, 1 on error.
1605 static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1607 unsigned long pxefile_addr_r;
1608 struct pxe_menu *cfg;
1609 char *pxefile_addr_str;
1615 if (strstr(argv[1], "-p")) {
1622 return cmd_usage(cmdtp);
1625 pxefile_addr_str = from_env("pxefile_addr_r");
1626 if (!pxefile_addr_str)
1629 pxefile_addr_str = argv[4];
1633 filename = getenv("bootfile");
1636 setenv("bootfile", filename);
1639 if (strstr(argv[3], "ext2"))
1640 do_getfile = do_get_ext2;
1641 else if (strstr(argv[3], "fat"))
1642 do_getfile = do_get_fat;
1643 else if (strstr(argv[3], "any"))
1644 do_getfile = do_get_any;
1646 printf("Invalid filesystem: %s\n", argv[3]);
1649 fs_argv[1] = argv[1];
1650 fs_argv[2] = argv[2];
1652 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1653 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1657 if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
1658 printf("Error reading config file\n");
1662 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
1665 printf("Error parsing config file\n");
1672 handle_pxe_menu(cmdtp, cfg);
1674 destroy_pxe_menu(cfg);
1680 sysboot, 7, 1, do_sysboot,
1681 "command to get and boot from syslinux files",
1682 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1683 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1684 " or any filesystem on 'dev' on 'interface' to address 'addr'"