2 * Copyright 2010-2011 Calxeda, Inc.
3 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
5 * SPDX-License-Identifier: GPL-2.0+
12 #include <linux/string.h>
13 #include <linux/ctype.h>
15 #include <linux/list.h>
21 #define MAX_TFTP_PATH_LEN 127
23 const char *pxe_default_paths[] = {
25 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
27 "default-" CONFIG_SYS_ARCH,
35 * Like getenv, but prints an error if envvar isn't defined in the
36 * environment. It always returns what getenv does, so it can be used in
37 * place of getenv without changing error handling otherwise.
39 static char *from_env(const char *envvar)
46 printf("missing environment variable: %s\n", envvar);
53 * Convert an ethaddr from the environment to the format used by pxelinux
54 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
55 * the beginning of the ethernet address to indicate a hardware type of
56 * Ethernet. Also converts uppercase hex characters into lowercase, to match
57 * pxelinux's behavior.
59 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
60 * environment, or some other value < 0 on error.
62 static int format_mac_pxe(char *outbuf, size_t outbuf_len)
66 if (outbuf_len < 21) {
67 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
72 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
76 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
77 ethaddr[0], ethaddr[1], ethaddr[2],
78 ethaddr[3], ethaddr[4], ethaddr[5]);
85 * Returns the directory the file specified in the bootfile env variable is
86 * in. If bootfile isn't defined in the environment, return NULL, which should
87 * be interpreted as "don't prepend anything to paths".
89 static int get_bootfile_path(const char *file_path, char *bootfile_path,
90 size_t bootfile_path_size)
92 char *bootfile, *last_slash;
95 /* Only syslinux allows absolute paths */
96 if (file_path[0] == '/' && !is_pxe)
99 bootfile = from_env("bootfile");
104 last_slash = strrchr(bootfile, '/');
106 if (last_slash == NULL)
109 path_len = (last_slash - bootfile) + 1;
111 if (bootfile_path_size < path_len) {
112 printf("bootfile_path too small. (%zd < %zd)\n",
113 bootfile_path_size, path_len);
118 strncpy(bootfile_path, bootfile, path_len);
121 bootfile_path[path_len] = '\0';
126 static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
128 #ifdef CONFIG_CMD_NET
129 static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
131 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
133 tftp_argv[1] = file_addr;
134 tftp_argv[2] = (void *)file_path;
136 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
143 static char *fs_argv[5];
145 static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
147 #ifdef CONFIG_CMD_EXT2
148 fs_argv[0] = "ext2load";
149 fs_argv[3] = file_addr;
150 fs_argv[4] = (void *)file_path;
152 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
158 static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
160 #ifdef CONFIG_CMD_FAT
161 fs_argv[0] = "fatload";
162 fs_argv[3] = file_addr;
163 fs_argv[4] = (void *)file_path;
165 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
171 static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
173 #ifdef CONFIG_CMD_FS_GENERIC
175 fs_argv[3] = file_addr;
176 fs_argv[4] = (void *)file_path;
178 if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
185 * As in pxelinux, paths to files referenced from files we retrieve are
186 * relative to the location of bootfile. get_relfile takes such a path and
187 * joins it with the bootfile path to get the full path to the target file. If
188 * the bootfile path is NULL, we use file_path as is.
190 * Returns 1 for success, or < 0 on error.
192 static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
195 char relfile[MAX_TFTP_PATH_LEN+1];
199 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
204 path_len = strlen(file_path);
205 path_len += strlen(relfile);
207 if (path_len > MAX_TFTP_PATH_LEN) {
208 printf("Base path too long (%s%s)\n",
212 return -ENAMETOOLONG;
215 strcat(relfile, file_path);
217 printf("Retrieving file: %s\n", relfile);
219 sprintf(addr_buf, "%p", file_addr);
221 return do_getfile(cmdtp, relfile, addr_buf);
225 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
226 * 'bootfile' was specified in the environment, the path to bootfile will be
227 * prepended to 'file_path' and the resulting path will be used.
229 * Returns 1 on success, or < 0 for error.
231 static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
233 unsigned long config_file_size;
237 err = get_relfile(cmdtp, file_path, file_addr);
243 * the file comes without a NUL byte at the end, so find out its size
244 * and add the NUL byte.
246 tftp_filesize = from_env("filesize");
251 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
254 *(char *)(file_addr + config_file_size) = '\0';
259 #ifdef CONFIG_CMD_NET
261 #define PXELINUX_DIR "pxelinux.cfg/"
264 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
265 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
266 * from the bootfile path, as described above.
268 * Returns 1 on success or < 0 on error.
270 static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r)
272 size_t base_len = strlen(PXELINUX_DIR);
273 char path[MAX_TFTP_PATH_LEN+1];
275 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
276 printf("path (%s%s) too long, skipping\n",
278 return -ENAMETOOLONG;
281 sprintf(path, PXELINUX_DIR "%s", file);
283 return get_pxe_file(cmdtp, path, pxefile_addr_r);
287 * Looks for a pxe file with a name based on the pxeuuid environment variable.
289 * Returns 1 on success or < 0 on error.
291 static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
295 uuid_str = from_env("pxeuuid");
300 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
304 * Looks for a pxe file with a name based on the 'ethaddr' environment
307 * Returns 1 on success or < 0 on error.
309 static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
314 err = format_mac_pxe(mac_str, sizeof(mac_str));
319 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
323 * Looks for pxe files with names based on our IP address. See pxelinux
324 * documentation for details on what these file names look like. We match
327 * Returns 1 on success or < 0 on error.
329 static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
334 sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
336 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
337 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
342 ip_addr[mask_pos] = '\0';
349 * Entry point for the 'pxe get' command.
350 * This Follows pxelinux's rules to download a config file from a tftp server.
351 * The file is stored at the location given by the pxefile_addr_r environment
352 * variable, which must be set.
354 * UUID comes from pxeuuid env variable, if defined
355 * MAC addr comes from ethaddr env variable, if defined
358 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
360 * Returns 0 on success or 1 on error.
363 do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
365 char *pxefile_addr_str;
366 unsigned long pxefile_addr_r;
369 do_getfile = do_get_tftp;
372 return CMD_RET_USAGE;
374 pxefile_addr_str = from_env("pxefile_addr_r");
376 if (!pxefile_addr_str)
379 err = strict_strtoul(pxefile_addr_str, 16,
380 (unsigned long *)&pxefile_addr_r);
385 * Keep trying paths until we successfully get a file we're looking
388 if (pxe_uuid_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
389 pxe_mac_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
390 pxe_ipaddr_paths(cmdtp, (void *)pxefile_addr_r) > 0) {
391 printf("Config file found\n");
396 while (pxe_default_paths[i]) {
397 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
398 (void *)pxefile_addr_r) > 0) {
399 printf("Config file found\n");
405 printf("Config file not found\n");
412 * Wrapper to make it easier to store the file at file_path in the location
413 * specified by envaddr_name. file_path will be joined to the bootfile path,
414 * if any is specified.
416 * Returns 1 on success or < 0 on error.
418 static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
420 unsigned long file_addr;
423 envaddr = from_env(envaddr_name);
428 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
431 return get_relfile(cmdtp, file_path, (void *)file_addr);
435 * A note on the pxe file parser.
437 * We're parsing files that use syslinux grammar, which has a few quirks.
438 * String literals must be recognized based on context - there is no
439 * quoting or escaping support. There's also nothing to explicitly indicate
440 * when a label section completes. We deal with that by ending a label
441 * section whenever we see a line that doesn't include.
443 * As with the syslinux family, this same file format could be reused in the
444 * future for non pxe purposes. The only action it takes during parsing that
445 * would throw this off is handling of include files. It assumes we're using
446 * pxe, and does a tftp download of a file listed as an include file in the
447 * middle of the parsing operation. That could be handled by refactoring it to
448 * take a 'include file getter' function.
452 * Describes a single label given in a pxe file.
454 * Create these with the 'label_create' function given below.
456 * name - the name of the menu as given on the 'menu label' line.
457 * kernel - the path to the kernel file to use for this label.
458 * append - kernel command line to use when booting this label
459 * initrd - path to the initrd to use for this label.
460 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
461 * localboot - 1 if this label specified 'localboot', 0 otherwise.
462 * list - lets these form a list, which a pxe_menu struct will hold.
477 struct list_head list;
481 * Describes a pxe menu as given via pxe files.
483 * title - the name of the menu as given by a 'menu title' line.
484 * default_label - the name of the default label, if any.
485 * timeout - time in tenths of a second to wait for a user key-press before
486 * booting the default label.
487 * prompt - if 0, don't prompt for a choice unless the timeout period is
488 * interrupted. If 1, always prompt for a choice regardless of
490 * labels - a list of labels defined for the menu.
497 struct list_head labels;
501 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
502 * result must be free()'d to reclaim the memory.
504 * Returns NULL if malloc fails.
506 static struct pxe_label *label_create(void)
508 struct pxe_label *label;
510 label = malloc(sizeof(struct pxe_label));
515 memset(label, 0, sizeof(struct pxe_label));
521 * Free the memory used by a pxe_label, including that used by its name,
522 * kernel, append and initrd members, if they're non NULL.
524 * So - be sure to only use dynamically allocated memory for the members of
525 * the pxe_label struct, unless you want to clean it up first. These are
526 * currently only created by the pxe file parsing code.
528 static void label_destroy(struct pxe_label *label)
552 * Print a label and its string members if they're defined.
554 * This is passed as a callback to the menu code for displaying each
557 static void label_print(void *data)
559 struct pxe_label *label = data;
560 const char *c = label->menu ? label->menu : label->name;
562 printf("%s:\t%s\n", label->num, c);
566 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
567 * environment variable is defined. Its contents will be executed as U-boot
568 * command. If the label specified an 'append' line, its contents will be
569 * used to overwrite the contents of the 'bootargs' environment variable prior
570 * to running 'localcmd'.
572 * Returns 1 on success or < 0 on error.
574 static int label_localboot(struct pxe_label *label)
578 localcmd = from_env("localcmd");
584 char bootargs[CONFIG_SYS_CBSIZE];
586 cli_simple_process_macros(label->append, bootargs);
587 setenv("bootargs", bootargs);
590 debug("running: %s\n", localcmd);
592 return run_command_list(localcmd, strlen(localcmd), 0);
596 * Boot according to the contents of a pxe_label.
598 * If we can't boot for any reason, we return. A successful boot never
601 * The kernel will be stored in the location given by the 'kernel_addr_r'
602 * environment variable.
604 * If the label specifies an initrd file, it will be stored in the location
605 * given by the 'ramdisk_addr_r' environment variable.
607 * If the label specifies an 'append' line, its contents will overwrite that
608 * of the 'bootargs' environment variable.
610 static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
612 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
614 char mac_str[29] = "";
615 char ip_str[68] = "";
623 label->attempted = 1;
625 if (label->localboot) {
626 if (label->localboot_val >= 0)
627 label_localboot(label);
631 if (label->kernel == NULL) {
632 printf("No kernel given, skipping %s\n",
638 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
639 printf("Skipping %s for failure retrieving initrd\n",
644 bootm_argv[2] = initrd_str;
645 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
646 strcat(bootm_argv[2], ":");
647 strcat(bootm_argv[2], getenv("filesize"));
652 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
653 printf("Skipping %s for failure retrieving kernel\n",
658 if (label->ipappend & 0x1) {
659 sprintf(ip_str, " ip=%s:%s:%s:%s",
660 getenv("ipaddr"), getenv("serverip"),
661 getenv("gatewayip"), getenv("netmask"));
664 #ifdef CONFIG_CMD_NET
665 if (label->ipappend & 0x2) {
667 strcpy(mac_str, " BOOTIF=");
668 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
674 if ((label->ipappend & 0x3) || label->append) {
675 char bootargs[CONFIG_SYS_CBSIZE] = "";
676 char finalbootargs[CONFIG_SYS_CBSIZE];
678 if (strlen(label->append ?: "") +
679 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
680 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
681 strlen(label->append ?: ""),
682 strlen(ip_str), strlen(mac_str),
688 strcpy(bootargs, label->append);
689 strcat(bootargs, ip_str);
690 strcat(bootargs, mac_str);
692 cli_simple_process_macros(bootargs, finalbootargs);
693 setenv("bootargs", finalbootargs);
694 printf("append: %s\n", finalbootargs);
697 bootm_argv[1] = getenv("kernel_addr_r");
700 * fdt usage is optional:
701 * It handles the following scenarios. All scenarios are exclusive
703 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
704 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
705 * and adjust argc appropriately.
707 * Scenario 2: If there is an fdt_addr specified, pass it along to
708 * bootm, and adjust argc appropriately.
710 * Scenario 3: fdt blob is not available.
712 bootm_argv[3] = getenv("fdt_addr_r");
714 /* if fdt label is defined then get fdt from server */
716 char *fdtfile = NULL;
717 char *fdtfilefree = NULL;
720 fdtfile = label->fdt;
721 } else if (label->fdtdir) {
722 char *f1, *f2, *f3, *f4, *slash;
724 f1 = getenv("fdtfile");
731 * For complex cases where this code doesn't
732 * generate the correct filename, the board
733 * code should set $fdtfile during early boot,
734 * or the boot scripts should set $fdtfile
735 * before invoking "pxe" or "sysboot".
739 f3 = getenv("board");
743 len = strlen(label->fdtdir);
746 else if (label->fdtdir[len - 1] != '/')
751 len = strlen(label->fdtdir) + strlen(slash) +
752 strlen(f1) + strlen(f2) + strlen(f3) +
754 fdtfilefree = malloc(len);
756 printf("malloc fail (FDT filename)\n");
760 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
761 label->fdtdir, slash, f1, f2, f3, f4);
762 fdtfile = fdtfilefree;
766 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
769 printf("Skipping %s for failure retrieving fdt\n",
774 bootm_argv[3] = NULL;
779 bootm_argv[3] = getenv("fdt_addr");
784 kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
785 buf = map_sysmem(kernel_addr, 0);
786 /* Try bootm for legacy and FIT format image */
787 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
788 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
789 #ifdef CONFIG_CMD_BOOTZ
790 /* Try booting a zImage */
792 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
798 * Tokens for the pxe file parser.
824 * A token - given by a value and a type.
828 enum token_type type;
832 * Keywords recognized.
834 static const struct token keywords[] = {
837 {"timeout", T_TIMEOUT},
838 {"default", T_DEFAULT},
839 {"prompt", T_PROMPT},
841 {"kernel", T_KERNEL},
843 {"localboot", T_LOCALBOOT},
844 {"append", T_APPEND},
845 {"initrd", T_INITRD},
846 {"include", T_INCLUDE},
847 {"devicetree", T_FDT},
849 {"devicetreedir", T_FDTDIR},
850 {"fdtdir", T_FDTDIR},
851 {"ontimeout", T_ONTIMEOUT,},
852 {"ipappend", T_IPAPPEND,},
857 * Since pxe(linux) files don't have a token to identify the start of a
858 * literal, we have to keep track of when we're in a state where a literal is
859 * expected vs when we're in a state a keyword is expected.
868 * get_string retrieves a string from *p and stores it as a token in
871 * get_string used for scanning both string literals and keywords.
873 * Characters from *p are copied into t-val until a character equal to
874 * delim is found, or a NUL byte is reached. If delim has the special value of
875 * ' ', any whitespace character will be used as a delimiter.
877 * If lower is unequal to 0, uppercase characters will be converted to
878 * lowercase in the result. This is useful to make keywords case
881 * The location of *p is updated to point to the first character after the end
882 * of the token - the ending delimiter.
884 * On success, the new value of t->val is returned. Memory for t->val is
885 * allocated using malloc and must be free()'d to reclaim it. If insufficient
886 * memory is available, NULL is returned.
888 static char *get_string(char **p, struct token *t, char delim, int lower)
894 * b and e both start at the beginning of the input stream.
896 * e is incremented until we find the ending delimiter, or a NUL byte
897 * is reached. Then, we take e - b to find the length of the token.
902 if ((delim == ' ' && isspace(*e)) || delim == *e)
910 * Allocate memory to hold the string, and copy it in, converting
911 * characters to lowercase if lower is != 0.
913 t->val = malloc(len + 1);
917 for (i = 0; i < len; i++, b++) {
919 t->val[i] = tolower(*b);
927 * Update *p so the caller knows where to continue scanning.
937 * Populate a keyword token with a type and value.
939 static void get_keyword(struct token *t)
943 for (i = 0; keywords[i].val; i++) {
944 if (!strcmp(t->val, keywords[i].val)) {
945 t->type = keywords[i].type;
952 * Get the next token. We have to keep track of which state we're in to know
953 * if we're looking to get a string literal or a keyword.
955 * *p is updated to point at the first character after the current token.
957 static void get_token(char **p, struct token *t, enum lex_state state)
963 /* eat non EOL whitespace */
968 * eat comments. note that string literals can't begin with #, but
969 * can contain a # after their first character.
972 while (*c && *c != '\n')
979 } else if (*c == '\0') {
982 } else if (state == L_SLITERAL) {
983 get_string(&c, t, '\n', 0);
984 } else if (state == L_KEYWORD) {
986 * when we expect a keyword, we first get the next string
987 * token delimited by whitespace, and then check if it
988 * matches a keyword in our keyword list. if it does, it's
989 * converted to a keyword token of the appropriate type, and
990 * if not, it remains a string token.
992 get_string(&c, t, ' ', 1);
1000 * Increment *c until we get to the end of the current line, or EOF.
1002 static void eol_or_eof(char **c)
1004 while (**c && **c != '\n')
1009 * All of these parse_* functions share some common behavior.
1011 * They finish with *c pointing after the token they parse, and return 1 on
1012 * success, or < 0 on error.
1016 * Parse a string literal and store a pointer it at *dst. String literals
1017 * terminate at the end of the line.
1019 static int parse_sliteral(char **c, char **dst)
1024 get_token(c, &t, L_SLITERAL);
1026 if (t.type != T_STRING) {
1027 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1037 * Parse a base 10 (unsigned) integer and store it at *dst.
1039 static int parse_integer(char **c, int *dst)
1044 get_token(c, &t, L_SLITERAL);
1046 if (t.type != T_STRING) {
1047 printf("Expected string: %.*s\n", (int)(*c - s), s);
1051 *dst = simple_strtol(t.val, NULL, 10);
1058 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
1061 * Parse an include statement, and retrieve and parse the file it mentions.
1063 * base should point to a location where it's safe to store the file, and
1064 * nest_level should indicate how many nested includes have occurred. For this
1065 * include, nest_level has already been incremented and doesn't need to be
1068 static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
1069 struct pxe_menu *cfg, int nest_level)
1075 err = parse_sliteral(c, &include_path);
1078 printf("Expected include path: %.*s\n",
1083 err = get_pxe_file(cmdtp, include_path, base);
1086 printf("Couldn't retrieve %s\n", include_path);
1090 return parse_pxefile_top(cmdtp, base, cfg, nest_level);
1094 * Parse lines that begin with 'menu'.
1096 * b and nest are provided to handle the 'menu include' case.
1098 * b should be the address where the file currently being parsed is stored.
1100 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1101 * a file it includes, 3 when parsing a file included by that file, and so on.
1103 static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg, char *b, int nest_level)
1109 get_token(c, &t, L_KEYWORD);
1113 err = parse_sliteral(c, &cfg->title);
1118 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
1123 printf("Ignoring malformed menu command: %.*s\n",
1136 * Handles parsing a 'menu line' when we're parsing a label.
1138 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1139 struct pxe_label *label)
1146 get_token(c, &t, L_KEYWORD);
1150 if (!cfg->default_label)
1151 cfg->default_label = strdup(label->name);
1153 if (!cfg->default_label)
1158 parse_sliteral(c, &label->menu);
1161 printf("Ignoring malformed menu command: %.*s\n",
1171 * Parses a label and adds it to the list of labels for a menu.
1173 * A label ends when we either get to the end of a file, or
1174 * get some input we otherwise don't have a handler defined
1178 static int parse_label(char **c, struct pxe_menu *cfg)
1183 struct pxe_label *label;
1186 label = label_create();
1190 err = parse_sliteral(c, &label->name);
1192 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1193 label_destroy(label);
1197 list_add_tail(&label->list, &cfg->labels);
1201 get_token(c, &t, L_KEYWORD);
1206 err = parse_label_menu(c, cfg, label);
1211 err = parse_sliteral(c, &label->kernel);
1215 err = parse_sliteral(c, &label->append);
1218 s = strstr(label->append, "initrd=");
1222 len = (int)(strchr(s, ' ') - s);
1223 label->initrd = malloc(len + 1);
1224 strncpy(label->initrd, s, len);
1225 label->initrd[len] = '\0';
1231 err = parse_sliteral(c, &label->initrd);
1236 err = parse_sliteral(c, &label->fdt);
1241 err = parse_sliteral(c, &label->fdtdir);
1245 label->localboot = 1;
1246 err = parse_integer(c, &label->localboot_val);
1250 err = parse_integer(c, &label->ipappend);
1258 * put the token back! we don't want it - it's the end
1259 * of a label and whatever token this is, it's
1260 * something for the menu level context to handle.
1272 * This 16 comes from the limit pxelinux imposes on nested includes.
1274 * There is no reason at all we couldn't do more, but some limit helps prevent
1275 * infinite (until crash occurs) recursion if a file tries to include itself.
1277 #define MAX_NEST_LEVEL 16
1280 * Entry point for parsing a menu file. nest_level indicates how many times
1281 * we've nested in includes. It will be 1 for the top level menu file.
1283 * Returns 1 on success, < 0 on error.
1285 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
1288 char *s, *b, *label_name;
1293 if (nest_level > MAX_NEST_LEVEL) {
1294 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1301 get_token(&p, &t, L_KEYWORD);
1307 err = parse_menu(cmdtp, &p, cfg, b, nest_level);
1311 err = parse_integer(&p, &cfg->timeout);
1315 err = parse_label(&p, cfg);
1320 err = parse_sliteral(&p, &label_name);
1323 if (cfg->default_label)
1324 free(cfg->default_label);
1326 cfg->default_label = label_name;
1332 err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
1347 printf("Ignoring unknown command: %.*s\n",
1358 * Free the memory used by a pxe_menu and its labels.
1360 static void destroy_pxe_menu(struct pxe_menu *cfg)
1362 struct list_head *pos, *n;
1363 struct pxe_label *label;
1368 if (cfg->default_label)
1369 free(cfg->default_label);
1371 list_for_each_safe(pos, n, &cfg->labels) {
1372 label = list_entry(pos, struct pxe_label, list);
1374 label_destroy(label);
1381 * Entry point for parsing a pxe file. This is only used for the top level
1384 * Returns NULL if there is an error, otherwise, returns a pointer to a
1385 * pxe_menu struct populated with the results of parsing the pxe file (and any
1386 * files it includes). The resulting pxe_menu struct can be free()'d by using
1387 * the destroy_pxe_menu() function.
1389 static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
1391 struct pxe_menu *cfg;
1393 cfg = malloc(sizeof(struct pxe_menu));
1398 memset(cfg, 0, sizeof(struct pxe_menu));
1400 INIT_LIST_HEAD(&cfg->labels);
1402 if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
1403 destroy_pxe_menu(cfg);
1411 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1414 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1416 struct pxe_label *label;
1417 struct list_head *pos;
1421 char *default_num = NULL;
1424 * Create a menu and add items for all the labels.
1426 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1432 list_for_each(pos, &cfg->labels) {
1433 label = list_entry(pos, struct pxe_label, list);
1435 sprintf(label->num, "%d", i++);
1436 if (menu_item_add(m, label->num, label) != 1) {
1440 if (cfg->default_label &&
1441 (strcmp(label->name, cfg->default_label) == 0))
1442 default_num = label->num;
1447 * After we've created items for each label in the menu, set the
1448 * menu's default label if one was specified.
1451 err = menu_default_set(m, default_num);
1453 if (err != -ENOENT) {
1458 printf("Missing default: %s\n", cfg->default_label);
1466 * Try to boot any labels we have yet to attempt to boot.
1468 static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1470 struct list_head *pos;
1471 struct pxe_label *label;
1473 list_for_each(pos, &cfg->labels) {
1474 label = list_entry(pos, struct pxe_label, list);
1476 if (!label->attempted)
1477 label_boot(cmdtp, label);
1482 * Boot the system as prescribed by a pxe_menu.
1484 * Use the menu system to either get the user's choice or the default, based
1485 * on config or user input. If there is no default or user's choice,
1486 * attempted to boot labels in the order they were given in pxe files.
1487 * If the default or user's choice fails to boot, attempt to boot other
1488 * labels in the order they were given in pxe files.
1490 * If this function returns, there weren't any labels that successfully
1491 * booted, or the user interrupted the menu selection via ctrl+c.
1493 static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1499 m = pxe_menu_to_menu(cfg);
1503 err = menu_get_choice(m, &choice);
1508 * err == 1 means we got a choice back from menu_get_choice.
1510 * err == -ENOENT if the menu was setup to select the default but no
1511 * default was set. in that case, we should continue trying to boot
1512 * labels that haven't been attempted yet.
1514 * otherwise, the user interrupted or there was some other error and
1519 err = label_boot(cmdtp, choice);
1522 } else if (err != -ENOENT) {
1526 boot_unattempted_labels(cmdtp, cfg);
1529 #ifdef CONFIG_CMD_NET
1531 * Boots a system using a pxe file
1533 * Returns 0 on success, 1 on error.
1536 do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1538 unsigned long pxefile_addr_r;
1539 struct pxe_menu *cfg;
1540 char *pxefile_addr_str;
1542 do_getfile = do_get_tftp;
1545 pxefile_addr_str = from_env("pxefile_addr_r");
1546 if (!pxefile_addr_str)
1549 } else if (argc == 2) {
1550 pxefile_addr_str = argv[1];
1552 return CMD_RET_USAGE;
1555 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1556 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1560 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
1563 printf("Error parsing config file\n");
1567 handle_pxe_menu(cmdtp, cfg);
1569 destroy_pxe_menu(cfg);
1571 copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
1576 static cmd_tbl_t cmd_pxe_sub[] = {
1577 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1578 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1581 static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1586 return CMD_RET_USAGE;
1590 /* drop initial "pxe" arg */
1594 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1597 return cp->cmd(cmdtp, flag, argc, argv);
1599 return CMD_RET_USAGE;
1604 "commands to get and boot from pxe files",
1605 "get - try to retrieve a pxe file using tftp\npxe "
1606 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1611 * Boots a system using a local disk syslinux/extlinux file
1613 * Returns 0 on success, 1 on error.
1615 static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1617 unsigned long pxefile_addr_r;
1618 struct pxe_menu *cfg;
1619 char *pxefile_addr_str;
1625 if (strstr(argv[1], "-p")) {
1632 return cmd_usage(cmdtp);
1635 pxefile_addr_str = from_env("pxefile_addr_r");
1636 if (!pxefile_addr_str)
1639 pxefile_addr_str = argv[4];
1643 filename = getenv("bootfile");
1646 setenv("bootfile", filename);
1649 if (strstr(argv[3], "ext2"))
1650 do_getfile = do_get_ext2;
1651 else if (strstr(argv[3], "fat"))
1652 do_getfile = do_get_fat;
1653 else if (strstr(argv[3], "any"))
1654 do_getfile = do_get_any;
1656 printf("Invalid filesystem: %s\n", argv[3]);
1659 fs_argv[1] = argv[1];
1660 fs_argv[2] = argv[2];
1662 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1663 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1667 if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
1668 printf("Error reading config file\n");
1672 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
1675 printf("Error parsing config file\n");
1682 handle_pxe_menu(cmdtp, cfg);
1684 destroy_pxe_menu(cfg);
1690 sysboot, 7, 1, do_sysboot,
1691 "command to get and boot from syslinux files",
1692 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1693 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1694 " or any filesystem on 'dev' on 'interface' to address 'addr'"