2 * Copyright 2010-2011 Calxeda, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <linux/string.h>
11 #include <linux/ctype.h>
13 #include <linux/list.h>
18 #define MAX_TFTP_PATH_LEN 127
20 const char *pxe_default_paths[] = {
22 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
24 "default-" CONFIG_SYS_ARCH,
32 * Like getenv, but prints an error if envvar isn't defined in the
33 * environment. It always returns what getenv does, so it can be used in
34 * place of getenv without changing error handling otherwise.
36 static char *from_env(const char *envvar)
43 printf("missing environment variable: %s\n", envvar);
49 * Convert an ethaddr from the environment to the format used by pxelinux
50 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
51 * the beginning of the ethernet address to indicate a hardware type of
52 * Ethernet. Also converts uppercase hex characters into lowercase, to match
53 * pxelinux's behavior.
55 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
56 * environment, or some other value < 0 on error.
58 static int format_mac_pxe(char *outbuf, size_t outbuf_len)
62 if (outbuf_len < 21) {
63 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
68 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
72 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
73 ethaddr[0], ethaddr[1], ethaddr[2],
74 ethaddr[3], ethaddr[4], ethaddr[5]);
80 * Returns the directory the file specified in the bootfile env variable is
81 * in. If bootfile isn't defined in the environment, return NULL, which should
82 * be interpreted as "don't prepend anything to paths".
84 static int get_bootfile_path(const char *file_path, char *bootfile_path,
85 size_t bootfile_path_size)
87 char *bootfile, *last_slash;
90 /* Only syslinux allows absolute paths */
91 if (file_path[0] == '/' && !is_pxe)
94 bootfile = from_env("bootfile");
99 last_slash = strrchr(bootfile, '/');
101 if (last_slash == NULL)
104 path_len = (last_slash - bootfile) + 1;
106 if (bootfile_path_size < path_len) {
107 printf("bootfile_path too small. (%zd < %zd)\n",
108 bootfile_path_size, path_len);
113 strncpy(bootfile_path, bootfile, path_len);
116 bootfile_path[path_len] = '\0';
121 static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
123 static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
125 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
127 tftp_argv[1] = file_addr;
128 tftp_argv[2] = (void *)file_path;
130 if (do_tftpb(cmdtp, 0, 3, tftp_argv))
136 static char *fs_argv[5];
138 static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
140 #ifdef CONFIG_CMD_EXT2
141 fs_argv[0] = "ext2load";
142 fs_argv[3] = file_addr;
143 fs_argv[4] = (void *)file_path;
145 if (!do_ext2load(cmdtp, 0, 5, fs_argv))
151 static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
153 #ifdef CONFIG_CMD_FAT
154 fs_argv[0] = "fatload";
155 fs_argv[3] = file_addr;
156 fs_argv[4] = (void *)file_path;
158 if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
164 static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
166 #ifdef CONFIG_CMD_FS_GENERIC
168 fs_argv[3] = file_addr;
169 fs_argv[4] = (void *)file_path;
171 if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
178 * As in pxelinux, paths to files referenced from files we retrieve are
179 * relative to the location of bootfile. get_relfile takes such a path and
180 * joins it with the bootfile path to get the full path to the target file. If
181 * the bootfile path is NULL, we use file_path as is.
183 * Returns 1 for success, or < 0 on error.
185 static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
188 char relfile[MAX_TFTP_PATH_LEN+1];
192 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
197 path_len = strlen(file_path);
198 path_len += strlen(relfile);
200 if (path_len > MAX_TFTP_PATH_LEN) {
201 printf("Base path too long (%s%s)\n",
205 return -ENAMETOOLONG;
208 strcat(relfile, file_path);
210 printf("Retrieving file: %s\n", relfile);
212 sprintf(addr_buf, "%p", file_addr);
214 return do_getfile(cmdtp, relfile, addr_buf);
218 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
219 * 'bootfile' was specified in the environment, the path to bootfile will be
220 * prepended to 'file_path' and the resulting path will be used.
222 * Returns 1 on success, or < 0 for error.
224 static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
226 unsigned long config_file_size;
230 err = get_relfile(cmdtp, file_path, file_addr);
236 * the file comes without a NUL byte at the end, so find out its size
237 * and add the NUL byte.
239 tftp_filesize = from_env("filesize");
244 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
247 *(char *)(file_addr + config_file_size) = '\0';
252 #define PXELINUX_DIR "pxelinux.cfg/"
255 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
256 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
257 * from the bootfile path, as described above.
259 * Returns 1 on success or < 0 on error.
261 static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r)
263 size_t base_len = strlen(PXELINUX_DIR);
264 char path[MAX_TFTP_PATH_LEN+1];
266 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
267 printf("path (%s%s) too long, skipping\n",
269 return -ENAMETOOLONG;
272 sprintf(path, PXELINUX_DIR "%s", file);
274 return get_pxe_file(cmdtp, path, pxefile_addr_r);
278 * Looks for a pxe file with a name based on the pxeuuid environment variable.
280 * Returns 1 on success or < 0 on error.
282 static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
286 uuid_str = from_env("pxeuuid");
291 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
295 * Looks for a pxe file with a name based on the 'ethaddr' environment
298 * Returns 1 on success or < 0 on error.
300 static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
305 err = format_mac_pxe(mac_str, sizeof(mac_str));
310 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
314 * Looks for pxe files with names based on our IP address. See pxelinux
315 * documentation for details on what these file names look like. We match
318 * Returns 1 on success or < 0 on error.
320 static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
325 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
327 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
328 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
333 ip_addr[mask_pos] = '\0';
340 * Entry point for the 'pxe get' command.
341 * This Follows pxelinux's rules to download a config file from a tftp server.
342 * The file is stored at the location given by the pxefile_addr_r environment
343 * variable, which must be set.
345 * UUID comes from pxeuuid env variable, if defined
346 * MAC addr comes from ethaddr env variable, if defined
349 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
351 * Returns 0 on success or 1 on error.
354 do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
356 char *pxefile_addr_str;
357 unsigned long pxefile_addr_r;
360 do_getfile = do_get_tftp;
363 return CMD_RET_USAGE;
365 pxefile_addr_str = from_env("pxefile_addr_r");
367 if (!pxefile_addr_str)
370 err = strict_strtoul(pxefile_addr_str, 16,
371 (unsigned long *)&pxefile_addr_r);
376 * Keep trying paths until we successfully get a file we're looking
379 if (pxe_uuid_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
380 pxe_mac_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
381 pxe_ipaddr_paths(cmdtp, (void *)pxefile_addr_r) > 0) {
382 printf("Config file found\n");
387 while (pxe_default_paths[i]) {
388 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
389 (void *)pxefile_addr_r) > 0) {
390 printf("Config file found\n");
396 printf("Config file not found\n");
402 * Wrapper to make it easier to store the file at file_path in the location
403 * specified by envaddr_name. file_path will be joined to the bootfile path,
404 * if any is specified.
406 * Returns 1 on success or < 0 on error.
408 static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
410 unsigned long file_addr;
413 envaddr = from_env(envaddr_name);
418 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
421 return get_relfile(cmdtp, file_path, (void *)file_addr);
425 * A note on the pxe file parser.
427 * We're parsing files that use syslinux grammar, which has a few quirks.
428 * String literals must be recognized based on context - there is no
429 * quoting or escaping support. There's also nothing to explicitly indicate
430 * when a label section completes. We deal with that by ending a label
431 * section whenever we see a line that doesn't include.
433 * As with the syslinux family, this same file format could be reused in the
434 * future for non pxe purposes. The only action it takes during parsing that
435 * would throw this off is handling of include files. It assumes we're using
436 * pxe, and does a tftp download of a file listed as an include file in the
437 * middle of the parsing operation. That could be handled by refactoring it to
438 * take a 'include file getter' function.
442 * Describes a single label given in a pxe file.
444 * Create these with the 'label_create' function given below.
446 * name - the name of the menu as given on the 'menu label' line.
447 * kernel - the path to the kernel file to use for this label.
448 * append - kernel command line to use when booting this label
449 * initrd - path to the initrd to use for this label.
450 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
451 * localboot - 1 if this label specified 'localboot', 0 otherwise.
452 * list - lets these form a list, which a pxe_menu struct will hold.
467 struct list_head list;
471 * Describes a pxe menu as given via pxe files.
473 * title - the name of the menu as given by a 'menu title' line.
474 * default_label - the name of the default label, if any.
475 * timeout - time in tenths of a second to wait for a user key-press before
476 * booting the default label.
477 * prompt - if 0, don't prompt for a choice unless the timeout period is
478 * interrupted. If 1, always prompt for a choice regardless of
480 * labels - a list of labels defined for the menu.
487 struct list_head labels;
491 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
492 * result must be free()'d to reclaim the memory.
494 * Returns NULL if malloc fails.
496 static struct pxe_label *label_create(void)
498 struct pxe_label *label;
500 label = malloc(sizeof(struct pxe_label));
505 memset(label, 0, sizeof(struct pxe_label));
511 * Free the memory used by a pxe_label, including that used by its name,
512 * kernel, append and initrd members, if they're non NULL.
514 * So - be sure to only use dynamically allocated memory for the members of
515 * the pxe_label struct, unless you want to clean it up first. These are
516 * currently only created by the pxe file parsing code.
518 static void label_destroy(struct pxe_label *label)
542 * Print a label and its string members if they're defined.
544 * This is passed as a callback to the menu code for displaying each
547 static void label_print(void *data)
549 struct pxe_label *label = data;
550 const char *c = label->menu ? label->menu : label->name;
552 printf("%s:\t%s\n", label->num, c);
556 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
557 * environment variable is defined. Its contents will be executed as U-boot
558 * command. If the label specified an 'append' line, its contents will be
559 * used to overwrite the contents of the 'bootargs' environment variable prior
560 * to running 'localcmd'.
562 * Returns 1 on success or < 0 on error.
564 static int label_localboot(struct pxe_label *label)
568 localcmd = from_env("localcmd");
574 setenv("bootargs", label->append);
576 debug("running: %s\n", localcmd);
578 return run_command_list(localcmd, strlen(localcmd), 0);
582 * Boot according to the contents of a pxe_label.
584 * If we can't boot for any reason, we return. A successful boot never
587 * The kernel will be stored in the location given by the 'kernel_addr_r'
588 * environment variable.
590 * If the label specifies an initrd file, it will be stored in the location
591 * given by the 'ramdisk_addr_r' environment variable.
593 * If the label specifies an 'append' line, its contents will overwrite that
594 * of the 'bootargs' environment variable.
596 static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
598 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
600 char mac_str[29] = "";
601 char ip_str[68] = "";
608 label->attempted = 1;
610 if (label->localboot) {
611 if (label->localboot_val >= 0)
612 label_localboot(label);
616 if (label->kernel == NULL) {
617 printf("No kernel given, skipping %s\n",
623 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
624 printf("Skipping %s for failure retrieving initrd\n",
629 bootm_argv[2] = initrd_str;
630 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
631 strcat(bootm_argv[2], ":");
632 strcat(bootm_argv[2], getenv("filesize"));
637 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
638 printf("Skipping %s for failure retrieving kernel\n",
643 if (label->ipappend & 0x1) {
644 sprintf(ip_str, " ip=%s:%s:%s:%s",
645 getenv("ipaddr"), getenv("serverip"),
646 getenv("gatewayip"), getenv("netmask"));
647 len += strlen(ip_str);
650 if (label->ipappend & 0x2) {
652 strcpy(mac_str, " BOOTIF=");
653 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
656 len += strlen(mac_str);
660 len += strlen(label->append);
663 bootargs = malloc(len + 1);
668 strcpy(bootargs, label->append);
669 strcat(bootargs, ip_str);
670 strcat(bootargs, mac_str);
672 setenv("bootargs", bootargs);
673 printf("append: %s\n", bootargs);
678 bootm_argv[1] = getenv("kernel_addr_r");
681 * fdt usage is optional:
682 * It handles the following scenarios. All scenarios are exclusive
684 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
685 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
686 * and adjust argc appropriately.
688 * Scenario 2: If there is an fdt_addr specified, pass it along to
689 * bootm, and adjust argc appropriately.
691 * Scenario 3: fdt blob is not available.
693 bootm_argv[3] = getenv("fdt_addr_r");
695 /* if fdt label is defined then get fdt from server */
697 char *fdtfile = NULL;
698 char *fdtfilefree = NULL;
701 fdtfile = label->fdt;
702 } else if (label->fdtdir) {
703 char *f1, *f2, *f3, *f4, *slash;
705 f1 = getenv("fdtfile");
712 * For complex cases where this code doesn't
713 * generate the correct filename, the board
714 * code should set $fdtfile during early boot,
715 * or the boot scripts should set $fdtfile
716 * before invoking "pxe" or "sysboot".
720 f3 = getenv("board");
724 len = strlen(label->fdtdir);
727 else if (label->fdtdir[len - 1] != '/')
732 len = strlen(label->fdtdir) + strlen(slash) +
733 strlen(f1) + strlen(f2) + strlen(f3) +
735 fdtfilefree = malloc(len);
737 printf("malloc fail (FDT filename)\n");
741 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
742 label->fdtdir, slash, f1, f2, f3, f4);
743 fdtfile = fdtfilefree;
747 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
750 printf("Skipping %s for failure retrieving fdt\n",
755 bootm_argv[3] = NULL;
760 bootm_argv[3] = getenv("fdt_addr");
765 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
767 #ifdef CONFIG_CMD_BOOTZ
768 /* Try booting a zImage if do_bootm returns */
769 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
775 * Tokens for the pxe file parser.
801 * A token - given by a value and a type.
805 enum token_type type;
809 * Keywords recognized.
811 static const struct token keywords[] = {
814 {"timeout", T_TIMEOUT},
815 {"default", T_DEFAULT},
816 {"prompt", T_PROMPT},
818 {"kernel", T_KERNEL},
820 {"localboot", T_LOCALBOOT},
821 {"append", T_APPEND},
822 {"initrd", T_INITRD},
823 {"include", T_INCLUDE},
824 {"devicetree", T_FDT},
826 {"devicetreedir", T_FDTDIR},
827 {"fdtdir", T_FDTDIR},
828 {"ontimeout", T_ONTIMEOUT,},
829 {"ipappend", T_IPAPPEND,},
834 * Since pxe(linux) files don't have a token to identify the start of a
835 * literal, we have to keep track of when we're in a state where a literal is
836 * expected vs when we're in a state a keyword is expected.
845 * get_string retrieves a string from *p and stores it as a token in
848 * get_string used for scanning both string literals and keywords.
850 * Characters from *p are copied into t-val until a character equal to
851 * delim is found, or a NUL byte is reached. If delim has the special value of
852 * ' ', any whitespace character will be used as a delimiter.
854 * If lower is unequal to 0, uppercase characters will be converted to
855 * lowercase in the result. This is useful to make keywords case
858 * The location of *p is updated to point to the first character after the end
859 * of the token - the ending delimiter.
861 * On success, the new value of t->val is returned. Memory for t->val is
862 * allocated using malloc and must be free()'d to reclaim it. If insufficient
863 * memory is available, NULL is returned.
865 static char *get_string(char **p, struct token *t, char delim, int lower)
871 * b and e both start at the beginning of the input stream.
873 * e is incremented until we find the ending delimiter, or a NUL byte
874 * is reached. Then, we take e - b to find the length of the token.
879 if ((delim == ' ' && isspace(*e)) || delim == *e)
887 * Allocate memory to hold the string, and copy it in, converting
888 * characters to lowercase if lower is != 0.
890 t->val = malloc(len + 1);
894 for (i = 0; i < len; i++, b++) {
896 t->val[i] = tolower(*b);
904 * Update *p so the caller knows where to continue scanning.
914 * Populate a keyword token with a type and value.
916 static void get_keyword(struct token *t)
920 for (i = 0; keywords[i].val; i++) {
921 if (!strcmp(t->val, keywords[i].val)) {
922 t->type = keywords[i].type;
929 * Get the next token. We have to keep track of which state we're in to know
930 * if we're looking to get a string literal or a keyword.
932 * *p is updated to point at the first character after the current token.
934 static void get_token(char **p, struct token *t, enum lex_state state)
940 /* eat non EOL whitespace */
945 * eat comments. note that string literals can't begin with #, but
946 * can contain a # after their first character.
949 while (*c && *c != '\n')
956 } else if (*c == '\0') {
959 } else if (state == L_SLITERAL) {
960 get_string(&c, t, '\n', 0);
961 } else if (state == L_KEYWORD) {
963 * when we expect a keyword, we first get the next string
964 * token delimited by whitespace, and then check if it
965 * matches a keyword in our keyword list. if it does, it's
966 * converted to a keyword token of the appropriate type, and
967 * if not, it remains a string token.
969 get_string(&c, t, ' ', 1);
977 * Increment *c until we get to the end of the current line, or EOF.
979 static void eol_or_eof(char **c)
981 while (**c && **c != '\n')
986 * All of these parse_* functions share some common behavior.
988 * They finish with *c pointing after the token they parse, and return 1 on
989 * success, or < 0 on error.
993 * Parse a string literal and store a pointer it at *dst. String literals
994 * terminate at the end of the line.
996 static int parse_sliteral(char **c, char **dst)
1001 get_token(c, &t, L_SLITERAL);
1003 if (t.type != T_STRING) {
1004 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1014 * Parse a base 10 (unsigned) integer and store it at *dst.
1016 static int parse_integer(char **c, int *dst)
1021 get_token(c, &t, L_SLITERAL);
1023 if (t.type != T_STRING) {
1024 printf("Expected string: %.*s\n", (int)(*c - s), s);
1028 *dst = simple_strtol(t.val, NULL, 10);
1035 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
1038 * Parse an include statement, and retrieve and parse the file it mentions.
1040 * base should point to a location where it's safe to store the file, and
1041 * nest_level should indicate how many nested includes have occurred. For this
1042 * include, nest_level has already been incremented and doesn't need to be
1045 static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
1046 struct pxe_menu *cfg, int nest_level)
1052 err = parse_sliteral(c, &include_path);
1055 printf("Expected include path: %.*s\n",
1060 err = get_pxe_file(cmdtp, include_path, base);
1063 printf("Couldn't retrieve %s\n", include_path);
1067 return parse_pxefile_top(cmdtp, base, cfg, nest_level);
1071 * Parse lines that begin with 'menu'.
1073 * b and nest are provided to handle the 'menu include' case.
1075 * b should be the address where the file currently being parsed is stored.
1077 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1078 * a file it includes, 3 when parsing a file included by that file, and so on.
1080 static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg, char *b, int nest_level)
1086 get_token(c, &t, L_KEYWORD);
1090 err = parse_sliteral(c, &cfg->title);
1095 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
1100 printf("Ignoring malformed menu command: %.*s\n",
1113 * Handles parsing a 'menu line' when we're parsing a label.
1115 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1116 struct pxe_label *label)
1123 get_token(c, &t, L_KEYWORD);
1127 if (!cfg->default_label)
1128 cfg->default_label = strdup(label->name);
1130 if (!cfg->default_label)
1135 parse_sliteral(c, &label->menu);
1138 printf("Ignoring malformed menu command: %.*s\n",
1148 * Parses a label and adds it to the list of labels for a menu.
1150 * A label ends when we either get to the end of a file, or
1151 * get some input we otherwise don't have a handler defined
1155 static int parse_label(char **c, struct pxe_menu *cfg)
1160 struct pxe_label *label;
1163 label = label_create();
1167 err = parse_sliteral(c, &label->name);
1169 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1170 label_destroy(label);
1174 list_add_tail(&label->list, &cfg->labels);
1178 get_token(c, &t, L_KEYWORD);
1183 err = parse_label_menu(c, cfg, label);
1188 err = parse_sliteral(c, &label->kernel);
1192 err = parse_sliteral(c, &label->append);
1195 s = strstr(label->append, "initrd=");
1199 len = (int)(strchr(s, ' ') - s);
1200 label->initrd = malloc(len + 1);
1201 strncpy(label->initrd, s, len);
1202 label->initrd[len] = '\0';
1208 err = parse_sliteral(c, &label->initrd);
1213 err = parse_sliteral(c, &label->fdt);
1218 err = parse_sliteral(c, &label->fdtdir);
1222 label->localboot = 1;
1223 err = parse_integer(c, &label->localboot_val);
1227 err = parse_integer(c, &label->ipappend);
1235 * put the token back! we don't want it - it's the end
1236 * of a label and whatever token this is, it's
1237 * something for the menu level context to handle.
1249 * This 16 comes from the limit pxelinux imposes on nested includes.
1251 * There is no reason at all we couldn't do more, but some limit helps prevent
1252 * infinite (until crash occurs) recursion if a file tries to include itself.
1254 #define MAX_NEST_LEVEL 16
1257 * Entry point for parsing a menu file. nest_level indicates how many times
1258 * we've nested in includes. It will be 1 for the top level menu file.
1260 * Returns 1 on success, < 0 on error.
1262 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
1265 char *s, *b, *label_name;
1270 if (nest_level > MAX_NEST_LEVEL) {
1271 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1278 get_token(&p, &t, L_KEYWORD);
1284 err = parse_menu(cmdtp, &p, cfg, b, nest_level);
1288 err = parse_integer(&p, &cfg->timeout);
1292 err = parse_label(&p, cfg);
1297 err = parse_sliteral(&p, &label_name);
1300 if (cfg->default_label)
1301 free(cfg->default_label);
1303 cfg->default_label = label_name;
1309 err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
1324 printf("Ignoring unknown command: %.*s\n",
1335 * Free the memory used by a pxe_menu and its labels.
1337 static void destroy_pxe_menu(struct pxe_menu *cfg)
1339 struct list_head *pos, *n;
1340 struct pxe_label *label;
1345 if (cfg->default_label)
1346 free(cfg->default_label);
1348 list_for_each_safe(pos, n, &cfg->labels) {
1349 label = list_entry(pos, struct pxe_label, list);
1351 label_destroy(label);
1358 * Entry point for parsing a pxe file. This is only used for the top level
1361 * Returns NULL if there is an error, otherwise, returns a pointer to a
1362 * pxe_menu struct populated with the results of parsing the pxe file (and any
1363 * files it includes). The resulting pxe_menu struct can be free()'d by using
1364 * the destroy_pxe_menu() function.
1366 static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
1368 struct pxe_menu *cfg;
1370 cfg = malloc(sizeof(struct pxe_menu));
1375 memset(cfg, 0, sizeof(struct pxe_menu));
1377 INIT_LIST_HEAD(&cfg->labels);
1379 if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
1380 destroy_pxe_menu(cfg);
1388 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1391 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1393 struct pxe_label *label;
1394 struct list_head *pos;
1398 char *default_num = NULL;
1401 * Create a menu and add items for all the labels.
1403 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1409 list_for_each(pos, &cfg->labels) {
1410 label = list_entry(pos, struct pxe_label, list);
1412 sprintf(label->num, "%d", i++);
1413 if (menu_item_add(m, label->num, label) != 1) {
1417 if (cfg->default_label &&
1418 (strcmp(label->name, cfg->default_label) == 0))
1419 default_num = label->num;
1424 * After we've created items for each label in the menu, set the
1425 * menu's default label if one was specified.
1428 err = menu_default_set(m, default_num);
1430 if (err != -ENOENT) {
1435 printf("Missing default: %s\n", cfg->default_label);
1443 * Try to boot any labels we have yet to attempt to boot.
1445 static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1447 struct list_head *pos;
1448 struct pxe_label *label;
1450 list_for_each(pos, &cfg->labels) {
1451 label = list_entry(pos, struct pxe_label, list);
1453 if (!label->attempted)
1454 label_boot(cmdtp, label);
1459 * Boot the system as prescribed by a pxe_menu.
1461 * Use the menu system to either get the user's choice or the default, based
1462 * on config or user input. If there is no default or user's choice,
1463 * attempted to boot labels in the order they were given in pxe files.
1464 * If the default or user's choice fails to boot, attempt to boot other
1465 * labels in the order they were given in pxe files.
1467 * If this function returns, there weren't any labels that successfully
1468 * booted, or the user interrupted the menu selection via ctrl+c.
1470 static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1476 m = pxe_menu_to_menu(cfg);
1480 err = menu_get_choice(m, &choice);
1485 * err == 1 means we got a choice back from menu_get_choice.
1487 * err == -ENOENT if the menu was setup to select the default but no
1488 * default was set. in that case, we should continue trying to boot
1489 * labels that haven't been attempted yet.
1491 * otherwise, the user interrupted or there was some other error and
1496 err = label_boot(cmdtp, choice);
1499 } else if (err != -ENOENT) {
1503 boot_unattempted_labels(cmdtp, cfg);
1507 * Boots a system using a pxe file
1509 * Returns 0 on success, 1 on error.
1512 do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1514 unsigned long pxefile_addr_r;
1515 struct pxe_menu *cfg;
1516 char *pxefile_addr_str;
1518 do_getfile = do_get_tftp;
1521 pxefile_addr_str = from_env("pxefile_addr_r");
1522 if (!pxefile_addr_str)
1525 } else if (argc == 2) {
1526 pxefile_addr_str = argv[1];
1528 return CMD_RET_USAGE;
1531 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1532 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1536 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
1539 printf("Error parsing config file\n");
1543 handle_pxe_menu(cmdtp, cfg);
1545 destroy_pxe_menu(cfg);
1550 static cmd_tbl_t cmd_pxe_sub[] = {
1551 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1552 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1555 int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1560 return CMD_RET_USAGE;
1564 /* drop initial "pxe" arg */
1568 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1571 return cp->cmd(cmdtp, flag, argc, argv);
1573 return CMD_RET_USAGE;
1578 "commands to get and boot from pxe files",
1579 "get - try to retrieve a pxe file using tftp\npxe "
1580 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1584 * Boots a system using a local disk syslinux/extlinux file
1586 * Returns 0 on success, 1 on error.
1588 int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1590 unsigned long pxefile_addr_r;
1591 struct pxe_menu *cfg;
1592 char *pxefile_addr_str;
1598 if (strstr(argv[1], "-p")) {
1605 return cmd_usage(cmdtp);
1608 pxefile_addr_str = from_env("pxefile_addr_r");
1609 if (!pxefile_addr_str)
1612 pxefile_addr_str = argv[4];
1616 filename = getenv("bootfile");
1619 setenv("bootfile", filename);
1622 if (strstr(argv[3], "ext2"))
1623 do_getfile = do_get_ext2;
1624 else if (strstr(argv[3], "fat"))
1625 do_getfile = do_get_fat;
1626 else if (strstr(argv[3], "any"))
1627 do_getfile = do_get_any;
1629 printf("Invalid filesystem: %s\n", argv[3]);
1632 fs_argv[1] = argv[1];
1633 fs_argv[2] = argv[2];
1635 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1636 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1640 if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
1641 printf("Error reading config file\n");
1645 cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
1648 printf("Error parsing config file\n");
1655 handle_pxe_menu(cmdtp, cfg);
1657 destroy_pxe_menu(cfg);
1663 sysboot, 7, 1, do_sysboot,
1664 "command to get and boot from syslinux files",
1665 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1666 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1667 " or any filesystem on 'dev' on 'interface' to address 'addr'"