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>
22 #define MAX_TFTP_PATH_LEN 127
24 const char *pxe_default_paths[] = {
26 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
28 "default-" CONFIG_SYS_ARCH,
36 * Like getenv, but prints an error if envvar isn't defined in the
37 * environment. It always returns what getenv does, so it can be used in
38 * place of getenv without changing error handling otherwise.
40 static char *from_env(const char *envvar)
47 printf("missing environment variable: %s\n", envvar);
54 * Convert an ethaddr from the environment to the format used by pxelinux
55 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
56 * the beginning of the ethernet address to indicate a hardware type of
57 * Ethernet. Also converts uppercase hex characters into lowercase, to match
58 * pxelinux's behavior.
60 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
61 * environment, or some other value < 0 on error.
63 static int format_mac_pxe(char *outbuf, size_t outbuf_len)
67 if (outbuf_len < 21) {
68 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
73 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
77 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
78 ethaddr[0], ethaddr[1], ethaddr[2],
79 ethaddr[3], ethaddr[4], ethaddr[5]);
86 * Returns the directory the file specified in the bootfile env variable is
87 * in. If bootfile isn't defined in the environment, return NULL, which should
88 * be interpreted as "don't prepend anything to paths".
90 static int get_bootfile_path(const char *file_path, char *bootfile_path,
91 size_t bootfile_path_size)
93 char *bootfile, *last_slash;
96 /* Only syslinux allows absolute paths */
97 if (file_path[0] == '/' && !is_pxe)
100 bootfile = from_env("bootfile");
105 last_slash = strrchr(bootfile, '/');
107 if (last_slash == NULL)
110 path_len = (last_slash - bootfile) + 1;
112 if (bootfile_path_size < path_len) {
113 printf("bootfile_path too small. (%zd < %zd)\n",
114 bootfile_path_size, path_len);
119 strncpy(bootfile_path, bootfile, path_len);
122 bootfile_path[path_len] = '\0';
127 static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
129 #ifdef CONFIG_CMD_NET
130 static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
132 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
134 tftp_argv[1] = file_addr;
135 tftp_argv[2] = (void *)file_path;
137 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
144 static char *fs_argv[5];
146 static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
148 #ifdef CONFIG_CMD_EXT2
149 fs_argv[0] = "ext2load";
150 fs_argv[3] = file_addr;
151 fs_argv[4] = (void *)file_path;
153 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
159 static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
161 #ifdef CONFIG_CMD_FAT
162 fs_argv[0] = "fatload";
163 fs_argv[3] = file_addr;
164 fs_argv[4] = (void *)file_path;
166 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
172 static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
174 #ifdef CONFIG_CMD_FS_GENERIC
176 fs_argv[3] = file_addr;
177 fs_argv[4] = (void *)file_path;
179 if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
186 * As in pxelinux, paths to files referenced from files we retrieve are
187 * relative to the location of bootfile. get_relfile takes such a path and
188 * joins it with the bootfile path to get the full path to the target file. If
189 * the bootfile path is NULL, we use file_path as is.
191 * Returns 1 for success, or < 0 on error.
193 static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path,
194 unsigned long file_addr)
197 char relfile[MAX_TFTP_PATH_LEN+1];
201 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
206 path_len = strlen(file_path);
207 path_len += strlen(relfile);
209 if (path_len > MAX_TFTP_PATH_LEN) {
210 printf("Base path too long (%s%s)\n",
214 return -ENAMETOOLONG;
217 strcat(relfile, file_path);
219 printf("Retrieving file: %s\n", relfile);
221 sprintf(addr_buf, "%lx", file_addr);
223 return do_getfile(cmdtp, relfile, addr_buf);
227 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
228 * 'bootfile' was specified in the environment, the path to bootfile will be
229 * prepended to 'file_path' and the resulting path will be used.
231 * Returns 1 on success, or < 0 for error.
233 static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path,
234 unsigned long file_addr)
236 unsigned long config_file_size;
241 err = get_relfile(cmdtp, file_path, file_addr);
247 * the file comes without a NUL byte at the end, so find out its size
248 * and add the NUL byte.
250 tftp_filesize = from_env("filesize");
255 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
258 buf = map_sysmem(file_addr + config_file_size, 1);
265 #ifdef CONFIG_CMD_NET
267 #define PXELINUX_DIR "pxelinux.cfg/"
270 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
271 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
272 * from the bootfile path, as described above.
274 * Returns 1 on success or < 0 on error.
276 static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file,
277 unsigned long pxefile_addr_r)
279 size_t base_len = strlen(PXELINUX_DIR);
280 char path[MAX_TFTP_PATH_LEN+1];
282 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
283 printf("path (%s%s) too long, skipping\n",
285 return -ENAMETOOLONG;
288 sprintf(path, PXELINUX_DIR "%s", file);
290 return get_pxe_file(cmdtp, path, pxefile_addr_r);
294 * Looks for a pxe file with a name based on the pxeuuid environment variable.
296 * Returns 1 on success or < 0 on error.
298 static int pxe_uuid_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
302 uuid_str = from_env("pxeuuid");
307 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
311 * Looks for a pxe file with a name based on the 'ethaddr' environment
314 * Returns 1 on success or < 0 on error.
316 static int pxe_mac_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
321 err = format_mac_pxe(mac_str, sizeof(mac_str));
326 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
330 * Looks for pxe files with names based on our IP address. See pxelinux
331 * documentation for details on what these file names look like. We match
334 * Returns 1 on success or < 0 on error.
336 static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
341 sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
343 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
344 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
349 ip_addr[mask_pos] = '\0';
356 * Entry point for the 'pxe get' command.
357 * This Follows pxelinux's rules to download a config file from a tftp server.
358 * The file is stored at the location given by the pxefile_addr_r environment
359 * variable, which must be set.
361 * UUID comes from pxeuuid env variable, if defined
362 * MAC addr comes from ethaddr env variable, if defined
365 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
367 * Returns 0 on success or 1 on error.
370 do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
372 char *pxefile_addr_str;
373 unsigned long pxefile_addr_r;
376 do_getfile = do_get_tftp;
379 return CMD_RET_USAGE;
381 pxefile_addr_str = from_env("pxefile_addr_r");
383 if (!pxefile_addr_str)
386 err = strict_strtoul(pxefile_addr_str, 16,
387 (unsigned long *)&pxefile_addr_r);
392 * Keep trying paths until we successfully get a file we're looking
395 if (pxe_uuid_path(cmdtp, pxefile_addr_r) > 0 ||
396 pxe_mac_path(cmdtp, pxefile_addr_r) > 0 ||
397 pxe_ipaddr_paths(cmdtp, pxefile_addr_r) > 0) {
398 printf("Config file found\n");
403 while (pxe_default_paths[i]) {
404 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
405 pxefile_addr_r) > 0) {
406 printf("Config file found\n");
412 printf("Config file not found\n");
419 * Wrapper to make it easier to store the file at file_path in the location
420 * specified by envaddr_name. file_path will be joined to the bootfile path,
421 * if any is specified.
423 * Returns 1 on success or < 0 on error.
425 static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
427 unsigned long file_addr;
430 envaddr = from_env(envaddr_name);
435 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
438 return get_relfile(cmdtp, file_path, file_addr);
442 * A note on the pxe file parser.
444 * We're parsing files that use syslinux grammar, which has a few quirks.
445 * String literals must be recognized based on context - there is no
446 * quoting or escaping support. There's also nothing to explicitly indicate
447 * when a label section completes. We deal with that by ending a label
448 * section whenever we see a line that doesn't include.
450 * As with the syslinux family, this same file format could be reused in the
451 * future for non pxe purposes. The only action it takes during parsing that
452 * would throw this off is handling of include files. It assumes we're using
453 * pxe, and does a tftp download of a file listed as an include file in the
454 * middle of the parsing operation. That could be handled by refactoring it to
455 * take a 'include file getter' function.
459 * Describes a single label given in a pxe file.
461 * Create these with the 'label_create' function given below.
463 * name - the name of the menu as given on the 'menu label' line.
464 * kernel - the path to the kernel file to use for this label.
465 * append - kernel command line to use when booting this label
466 * initrd - path to the initrd to use for this label.
467 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
468 * localboot - 1 if this label specified 'localboot', 0 otherwise.
469 * list - lets these form a list, which a pxe_menu struct will hold.
484 struct list_head list;
488 * Describes a pxe menu as given via pxe files.
490 * title - the name of the menu as given by a 'menu title' line.
491 * default_label - the name of the default label, if any.
492 * timeout - time in tenths of a second to wait for a user key-press before
493 * booting the default label.
494 * prompt - if 0, don't prompt for a choice unless the timeout period is
495 * interrupted. If 1, always prompt for a choice regardless of
497 * labels - a list of labels defined for the menu.
504 struct list_head labels;
508 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
509 * result must be free()'d to reclaim the memory.
511 * Returns NULL if malloc fails.
513 static struct pxe_label *label_create(void)
515 struct pxe_label *label;
517 label = malloc(sizeof(struct pxe_label));
522 memset(label, 0, sizeof(struct pxe_label));
528 * Free the memory used by a pxe_label, including that used by its name,
529 * kernel, append and initrd members, if they're non NULL.
531 * So - be sure to only use dynamically allocated memory for the members of
532 * the pxe_label struct, unless you want to clean it up first. These are
533 * currently only created by the pxe file parsing code.
535 static void label_destroy(struct pxe_label *label)
559 * Print a label and its string members if they're defined.
561 * This is passed as a callback to the menu code for displaying each
564 static void label_print(void *data)
566 struct pxe_label *label = data;
567 const char *c = label->menu ? label->menu : label->name;
569 printf("%s:\t%s\n", label->num, c);
573 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
574 * environment variable is defined. Its contents will be executed as U-boot
575 * command. If the label specified an 'append' line, its contents will be
576 * used to overwrite the contents of the 'bootargs' environment variable prior
577 * to running 'localcmd'.
579 * Returns 1 on success or < 0 on error.
581 static int label_localboot(struct pxe_label *label)
585 localcmd = from_env("localcmd");
591 char bootargs[CONFIG_SYS_CBSIZE];
593 cli_simple_process_macros(label->append, bootargs);
594 setenv("bootargs", bootargs);
597 debug("running: %s\n", localcmd);
599 return run_command_list(localcmd, strlen(localcmd), 0);
603 * Boot according to the contents of a pxe_label.
605 * If we can't boot for any reason, we return. A successful boot never
608 * The kernel will be stored in the location given by the 'kernel_addr_r'
609 * environment variable.
611 * If the label specifies an initrd file, it will be stored in the location
612 * given by the 'ramdisk_addr_r' environment variable.
614 * If the label specifies an 'append' line, its contents will overwrite that
615 * of the 'bootargs' environment variable.
617 static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
619 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
621 char mac_str[29] = "";
622 char ip_str[68] = "";
630 label->attempted = 1;
632 if (label->localboot) {
633 if (label->localboot_val >= 0)
634 label_localboot(label);
638 if (label->kernel == NULL) {
639 printf("No kernel given, skipping %s\n",
645 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
646 printf("Skipping %s for failure retrieving initrd\n",
651 bootm_argv[2] = initrd_str;
652 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
653 strcat(bootm_argv[2], ":");
654 strcat(bootm_argv[2], getenv("filesize"));
659 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
660 printf("Skipping %s for failure retrieving kernel\n",
665 if (label->ipappend & 0x1) {
666 sprintf(ip_str, " ip=%s:%s:%s:%s",
667 getenv("ipaddr"), getenv("serverip"),
668 getenv("gatewayip"), getenv("netmask"));
671 #ifdef CONFIG_CMD_NET
672 if (label->ipappend & 0x2) {
674 strcpy(mac_str, " BOOTIF=");
675 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
681 if ((label->ipappend & 0x3) || label->append) {
682 char bootargs[CONFIG_SYS_CBSIZE] = "";
683 char finalbootargs[CONFIG_SYS_CBSIZE];
685 if (strlen(label->append ?: "") +
686 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
687 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
688 strlen(label->append ?: ""),
689 strlen(ip_str), strlen(mac_str),
695 strcpy(bootargs, label->append);
696 strcat(bootargs, ip_str);
697 strcat(bootargs, mac_str);
699 cli_simple_process_macros(bootargs, finalbootargs);
700 setenv("bootargs", finalbootargs);
701 printf("append: %s\n", finalbootargs);
704 bootm_argv[1] = getenv("kernel_addr_r");
707 * fdt usage is optional:
708 * It handles the following scenarios. All scenarios are exclusive
710 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
711 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
712 * and adjust argc appropriately.
714 * Scenario 2: If there is an fdt_addr specified, pass it along to
715 * bootm, and adjust argc appropriately.
717 * Scenario 3: fdt blob is not available.
719 bootm_argv[3] = getenv("fdt_addr_r");
721 /* if fdt label is defined then get fdt from server */
723 char *fdtfile = NULL;
724 char *fdtfilefree = NULL;
727 fdtfile = label->fdt;
728 } else if (label->fdtdir) {
729 char *f1, *f2, *f3, *f4, *slash;
731 f1 = getenv("fdtfile");
738 * For complex cases where this code doesn't
739 * generate the correct filename, the board
740 * code should set $fdtfile during early boot,
741 * or the boot scripts should set $fdtfile
742 * before invoking "pxe" or "sysboot".
746 f3 = getenv("board");
750 len = strlen(label->fdtdir);
753 else if (label->fdtdir[len - 1] != '/')
758 len = strlen(label->fdtdir) + strlen(slash) +
759 strlen(f1) + strlen(f2) + strlen(f3) +
761 fdtfilefree = malloc(len);
763 printf("malloc fail (FDT filename)\n");
767 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
768 label->fdtdir, slash, f1, f2, f3, f4);
769 fdtfile = fdtfilefree;
773 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
776 printf("Skipping %s for failure retrieving fdt\n",
781 bootm_argv[3] = NULL;
786 bootm_argv[3] = getenv("fdt_addr");
791 kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
792 buf = map_sysmem(kernel_addr, 0);
793 /* Try bootm for legacy and FIT format image */
794 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
795 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
796 #ifdef CONFIG_CMD_BOOTZ
797 /* Try booting a zImage */
799 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
806 * Tokens for the pxe file parser.
832 * A token - given by a value and a type.
836 enum token_type type;
840 * Keywords recognized.
842 static const struct token keywords[] = {
845 {"timeout", T_TIMEOUT},
846 {"default", T_DEFAULT},
847 {"prompt", T_PROMPT},
849 {"kernel", T_KERNEL},
851 {"localboot", T_LOCALBOOT},
852 {"append", T_APPEND},
853 {"initrd", T_INITRD},
854 {"include", T_INCLUDE},
855 {"devicetree", T_FDT},
857 {"devicetreedir", T_FDTDIR},
858 {"fdtdir", T_FDTDIR},
859 {"ontimeout", T_ONTIMEOUT,},
860 {"ipappend", T_IPAPPEND,},
865 * Since pxe(linux) files don't have a token to identify the start of a
866 * literal, we have to keep track of when we're in a state where a literal is
867 * expected vs when we're in a state a keyword is expected.
876 * get_string retrieves a string from *p and stores it as a token in
879 * get_string used for scanning both string literals and keywords.
881 * Characters from *p are copied into t-val until a character equal to
882 * delim is found, or a NUL byte is reached. If delim has the special value of
883 * ' ', any whitespace character will be used as a delimiter.
885 * If lower is unequal to 0, uppercase characters will be converted to
886 * lowercase in the result. This is useful to make keywords case
889 * The location of *p is updated to point to the first character after the end
890 * of the token - the ending delimiter.
892 * On success, the new value of t->val is returned. Memory for t->val is
893 * allocated using malloc and must be free()'d to reclaim it. If insufficient
894 * memory is available, NULL is returned.
896 static char *get_string(char **p, struct token *t, char delim, int lower)
902 * b and e both start at the beginning of the input stream.
904 * e is incremented until we find the ending delimiter, or a NUL byte
905 * is reached. Then, we take e - b to find the length of the token.
910 if ((delim == ' ' && isspace(*e)) || delim == *e)
918 * Allocate memory to hold the string, and copy it in, converting
919 * characters to lowercase if lower is != 0.
921 t->val = malloc(len + 1);
925 for (i = 0; i < len; i++, b++) {
927 t->val[i] = tolower(*b);
935 * Update *p so the caller knows where to continue scanning.
945 * Populate a keyword token with a type and value.
947 static void get_keyword(struct token *t)
951 for (i = 0; keywords[i].val; i++) {
952 if (!strcmp(t->val, keywords[i].val)) {
953 t->type = keywords[i].type;
960 * Get the next token. We have to keep track of which state we're in to know
961 * if we're looking to get a string literal or a keyword.
963 * *p is updated to point at the first character after the current token.
965 static void get_token(char **p, struct token *t, enum lex_state state)
971 /* eat non EOL whitespace */
976 * eat comments. note that string literals can't begin with #, but
977 * can contain a # after their first character.
980 while (*c && *c != '\n')
987 } else if (*c == '\0') {
990 } else if (state == L_SLITERAL) {
991 get_string(&c, t, '\n', 0);
992 } else if (state == L_KEYWORD) {
994 * when we expect a keyword, we first get the next string
995 * token delimited by whitespace, and then check if it
996 * matches a keyword in our keyword list. if it does, it's
997 * converted to a keyword token of the appropriate type, and
998 * if not, it remains a string token.
1000 get_string(&c, t, ' ', 1);
1008 * Increment *c until we get to the end of the current line, or EOF.
1010 static void eol_or_eof(char **c)
1012 while (**c && **c != '\n')
1017 * All of these parse_* functions share some common behavior.
1019 * They finish with *c pointing after the token they parse, and return 1 on
1020 * success, or < 0 on error.
1024 * Parse a string literal and store a pointer it at *dst. String literals
1025 * terminate at the end of the line.
1027 static int parse_sliteral(char **c, char **dst)
1032 get_token(c, &t, L_SLITERAL);
1034 if (t.type != T_STRING) {
1035 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1045 * Parse a base 10 (unsigned) integer and store it at *dst.
1047 static int parse_integer(char **c, int *dst)
1052 get_token(c, &t, L_SLITERAL);
1054 if (t.type != T_STRING) {
1055 printf("Expected string: %.*s\n", (int)(*c - s), s);
1059 *dst = simple_strtol(t.val, NULL, 10);
1066 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
1067 struct pxe_menu *cfg, int nest_level);
1070 * Parse an include statement, and retrieve and parse the file it mentions.
1072 * base should point to a location where it's safe to store the file, and
1073 * nest_level should indicate how many nested includes have occurred. For this
1074 * include, nest_level has already been incremented and doesn't need to be
1077 static int handle_include(cmd_tbl_t *cmdtp, char **c, unsigned long base,
1078 struct pxe_menu *cfg, int nest_level)
1086 err = parse_sliteral(c, &include_path);
1089 printf("Expected include path: %.*s\n",
1094 err = get_pxe_file(cmdtp, include_path, base);
1097 printf("Couldn't retrieve %s\n", include_path);
1101 buf = map_sysmem(base, 0);
1102 ret = parse_pxefile_top(cmdtp, buf, base, cfg, nest_level);
1109 * Parse lines that begin with 'menu'.
1111 * base and nest are provided to handle the 'menu include' case.
1113 * base should point to a location where it's safe to store the included file.
1115 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1116 * a file it includes, 3 when parsing a file included by that file, and so on.
1118 static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg,
1119 unsigned long base, int nest_level)
1125 get_token(c, &t, L_KEYWORD);
1129 err = parse_sliteral(c, &cfg->title);
1134 err = handle_include(cmdtp, c, base, cfg,
1139 printf("Ignoring malformed menu command: %.*s\n",
1152 * Handles parsing a 'menu line' when we're parsing a label.
1154 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1155 struct pxe_label *label)
1162 get_token(c, &t, L_KEYWORD);
1166 if (!cfg->default_label)
1167 cfg->default_label = strdup(label->name);
1169 if (!cfg->default_label)
1174 parse_sliteral(c, &label->menu);
1177 printf("Ignoring malformed menu command: %.*s\n",
1187 * Parses a label and adds it to the list of labels for a menu.
1189 * A label ends when we either get to the end of a file, or
1190 * get some input we otherwise don't have a handler defined
1194 static int parse_label(char **c, struct pxe_menu *cfg)
1199 struct pxe_label *label;
1202 label = label_create();
1206 err = parse_sliteral(c, &label->name);
1208 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1209 label_destroy(label);
1213 list_add_tail(&label->list, &cfg->labels);
1217 get_token(c, &t, L_KEYWORD);
1222 err = parse_label_menu(c, cfg, label);
1227 err = parse_sliteral(c, &label->kernel);
1231 err = parse_sliteral(c, &label->append);
1234 s = strstr(label->append, "initrd=");
1238 len = (int)(strchr(s, ' ') - s);
1239 label->initrd = malloc(len + 1);
1240 strncpy(label->initrd, s, len);
1241 label->initrd[len] = '\0';
1247 err = parse_sliteral(c, &label->initrd);
1252 err = parse_sliteral(c, &label->fdt);
1257 err = parse_sliteral(c, &label->fdtdir);
1261 label->localboot = 1;
1262 err = parse_integer(c, &label->localboot_val);
1266 err = parse_integer(c, &label->ipappend);
1274 * put the token back! we don't want it - it's the end
1275 * of a label and whatever token this is, it's
1276 * something for the menu level context to handle.
1288 * This 16 comes from the limit pxelinux imposes on nested includes.
1290 * There is no reason at all we couldn't do more, but some limit helps prevent
1291 * infinite (until crash occurs) recursion if a file tries to include itself.
1293 #define MAX_NEST_LEVEL 16
1296 * Entry point for parsing a menu file. nest_level indicates how many times
1297 * we've nested in includes. It will be 1 for the top level menu file.
1299 * Returns 1 on success, < 0 on error.
1301 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
1302 struct pxe_menu *cfg, int nest_level)
1305 char *s, *b, *label_name;
1310 if (nest_level > MAX_NEST_LEVEL) {
1311 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1318 get_token(&p, &t, L_KEYWORD);
1324 err = parse_menu(cmdtp, &p, cfg,
1325 base + ALIGN(strlen(b) + 1, 4),
1330 err = parse_integer(&p, &cfg->timeout);
1334 err = parse_label(&p, cfg);
1339 err = parse_sliteral(&p, &label_name);
1342 if (cfg->default_label)
1343 free(cfg->default_label);
1345 cfg->default_label = label_name;
1351 err = handle_include(cmdtp, &p,
1352 base + ALIGN(strlen(b), 4), cfg,
1367 printf("Ignoring unknown command: %.*s\n",
1378 * Free the memory used by a pxe_menu and its labels.
1380 static void destroy_pxe_menu(struct pxe_menu *cfg)
1382 struct list_head *pos, *n;
1383 struct pxe_label *label;
1388 if (cfg->default_label)
1389 free(cfg->default_label);
1391 list_for_each_safe(pos, n, &cfg->labels) {
1392 label = list_entry(pos, struct pxe_label, list);
1394 label_destroy(label);
1401 * Entry point for parsing a pxe file. This is only used for the top level
1404 * Returns NULL if there is an error, otherwise, returns a pointer to a
1405 * pxe_menu struct populated with the results of parsing the pxe file (and any
1406 * files it includes). The resulting pxe_menu struct can be free()'d by using
1407 * the destroy_pxe_menu() function.
1409 static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, unsigned long menucfg)
1411 struct pxe_menu *cfg;
1415 cfg = malloc(sizeof(struct pxe_menu));
1420 memset(cfg, 0, sizeof(struct pxe_menu));
1422 INIT_LIST_HEAD(&cfg->labels);
1424 buf = map_sysmem(menucfg, 0);
1425 r = parse_pxefile_top(cmdtp, buf, menucfg, cfg, 1);
1429 destroy_pxe_menu(cfg);
1437 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1440 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1442 struct pxe_label *label;
1443 struct list_head *pos;
1447 char *default_num = NULL;
1450 * Create a menu and add items for all the labels.
1452 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1458 list_for_each(pos, &cfg->labels) {
1459 label = list_entry(pos, struct pxe_label, list);
1461 sprintf(label->num, "%d", i++);
1462 if (menu_item_add(m, label->num, label) != 1) {
1466 if (cfg->default_label &&
1467 (strcmp(label->name, cfg->default_label) == 0))
1468 default_num = label->num;
1473 * After we've created items for each label in the menu, set the
1474 * menu's default label if one was specified.
1477 err = menu_default_set(m, default_num);
1479 if (err != -ENOENT) {
1484 printf("Missing default: %s\n", cfg->default_label);
1492 * Try to boot any labels we have yet to attempt to boot.
1494 static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1496 struct list_head *pos;
1497 struct pxe_label *label;
1499 list_for_each(pos, &cfg->labels) {
1500 label = list_entry(pos, struct pxe_label, list);
1502 if (!label->attempted)
1503 label_boot(cmdtp, label);
1508 * Boot the system as prescribed by a pxe_menu.
1510 * Use the menu system to either get the user's choice or the default, based
1511 * on config or user input. If there is no default or user's choice,
1512 * attempted to boot labels in the order they were given in pxe files.
1513 * If the default or user's choice fails to boot, attempt to boot other
1514 * labels in the order they were given in pxe files.
1516 * If this function returns, there weren't any labels that successfully
1517 * booted, or the user interrupted the menu selection via ctrl+c.
1519 static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1525 m = pxe_menu_to_menu(cfg);
1529 err = menu_get_choice(m, &choice);
1534 * err == 1 means we got a choice back from menu_get_choice.
1536 * err == -ENOENT if the menu was setup to select the default but no
1537 * default was set. in that case, we should continue trying to boot
1538 * labels that haven't been attempted yet.
1540 * otherwise, the user interrupted or there was some other error and
1545 err = label_boot(cmdtp, choice);
1548 } else if (err != -ENOENT) {
1552 boot_unattempted_labels(cmdtp, cfg);
1555 #ifdef CONFIG_CMD_NET
1557 * Boots a system using a pxe file
1559 * Returns 0 on success, 1 on error.
1562 do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1564 unsigned long pxefile_addr_r;
1565 struct pxe_menu *cfg;
1566 char *pxefile_addr_str;
1568 do_getfile = do_get_tftp;
1571 pxefile_addr_str = from_env("pxefile_addr_r");
1572 if (!pxefile_addr_str)
1575 } else if (argc == 2) {
1576 pxefile_addr_str = argv[1];
1578 return CMD_RET_USAGE;
1581 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1582 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1586 cfg = parse_pxefile(cmdtp, pxefile_addr_r);
1589 printf("Error parsing config file\n");
1593 handle_pxe_menu(cmdtp, cfg);
1595 destroy_pxe_menu(cfg);
1597 copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
1602 static cmd_tbl_t cmd_pxe_sub[] = {
1603 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1604 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1607 static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1612 return CMD_RET_USAGE;
1616 /* drop initial "pxe" arg */
1620 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1623 return cp->cmd(cmdtp, flag, argc, argv);
1625 return CMD_RET_USAGE;
1630 "commands to get and boot from pxe files",
1631 "get - try to retrieve a pxe file using tftp\npxe "
1632 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1637 * Boots a system using a local disk syslinux/extlinux file
1639 * Returns 0 on success, 1 on error.
1641 static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1643 unsigned long pxefile_addr_r;
1644 struct pxe_menu *cfg;
1645 char *pxefile_addr_str;
1651 if (strstr(argv[1], "-p")) {
1658 return cmd_usage(cmdtp);
1661 pxefile_addr_str = from_env("pxefile_addr_r");
1662 if (!pxefile_addr_str)
1665 pxefile_addr_str = argv[4];
1669 filename = getenv("bootfile");
1672 setenv("bootfile", filename);
1675 if (strstr(argv[3], "ext2"))
1676 do_getfile = do_get_ext2;
1677 else if (strstr(argv[3], "fat"))
1678 do_getfile = do_get_fat;
1679 else if (strstr(argv[3], "any"))
1680 do_getfile = do_get_any;
1682 printf("Invalid filesystem: %s\n", argv[3]);
1685 fs_argv[1] = argv[1];
1686 fs_argv[2] = argv[2];
1688 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1689 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1693 if (get_pxe_file(cmdtp, filename, pxefile_addr_r) < 0) {
1694 printf("Error reading config file\n");
1698 cfg = parse_pxefile(cmdtp, pxefile_addr_r);
1701 printf("Error parsing config file\n");
1708 handle_pxe_menu(cmdtp, cfg);
1710 destroy_pxe_menu(cfg);
1716 sysboot, 7, 1, do_sysboot,
1717 "command to get and boot from syslinux files",
1718 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1719 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1720 " or any filesystem on 'dev' on 'interface' to address 'addr'"