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 env_get, but prints an error if envvar isn't defined in the
37 * environment. It always returns what env_get does, so it can be used in
38 * place of env_get without changing error handling otherwise.
40 static char *from_env(const char *envvar)
44 ret = env_get(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_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
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,
193 unsigned long file_addr)
196 char relfile[MAX_TFTP_PATH_LEN+1];
200 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
205 path_len = strlen(file_path);
206 path_len += strlen(relfile);
208 if (path_len > MAX_TFTP_PATH_LEN) {
209 printf("Base path too long (%s%s)\n",
213 return -ENAMETOOLONG;
216 strcat(relfile, file_path);
218 printf("Retrieving file: %s\n", relfile);
220 sprintf(addr_buf, "%lx", file_addr);
222 return do_getfile(cmdtp, relfile, addr_buf);
226 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
227 * 'bootfile' was specified in the environment, the path to bootfile will be
228 * prepended to 'file_path' and the resulting path will be used.
230 * Returns 1 on success, or < 0 for error.
232 static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path,
233 unsigned long file_addr)
235 unsigned long config_file_size;
240 err = get_relfile(cmdtp, file_path, file_addr);
246 * the file comes without a NUL byte at the end, so find out its size
247 * and add the NUL byte.
249 tftp_filesize = from_env("filesize");
254 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
257 buf = map_sysmem(file_addr + config_file_size, 1);
264 #ifdef CONFIG_CMD_NET
266 #define PXELINUX_DIR "pxelinux.cfg/"
269 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
270 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
271 * from the bootfile path, as described above.
273 * Returns 1 on success or < 0 on error.
275 static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file,
276 unsigned long pxefile_addr_r)
278 size_t base_len = strlen(PXELINUX_DIR);
279 char path[MAX_TFTP_PATH_LEN+1];
281 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
282 printf("path (%s%s) too long, skipping\n",
284 return -ENAMETOOLONG;
287 sprintf(path, PXELINUX_DIR "%s", file);
289 return get_pxe_file(cmdtp, path, pxefile_addr_r);
293 * Looks for a pxe file with a name based on the pxeuuid environment variable.
295 * Returns 1 on success or < 0 on error.
297 static int pxe_uuid_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
301 uuid_str = from_env("pxeuuid");
306 return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
310 * Looks for a pxe file with a name based on the 'ethaddr' environment
313 * Returns 1 on success or < 0 on error.
315 static int pxe_mac_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
320 err = format_mac_pxe(mac_str, sizeof(mac_str));
325 return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
329 * Looks for pxe files with names based on our IP address. See pxelinux
330 * documentation for details on what these file names look like. We match
333 * Returns 1 on success or < 0 on error.
335 static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
340 sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
342 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
343 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
348 ip_addr[mask_pos] = '\0';
355 * Entry point for the 'pxe get' command.
356 * This Follows pxelinux's rules to download a config file from a tftp server.
357 * The file is stored at the location given by the pxefile_addr_r environment
358 * variable, which must be set.
360 * UUID comes from pxeuuid env variable, if defined
361 * MAC addr comes from ethaddr env variable, if defined
364 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
366 * Returns 0 on success or 1 on error.
369 do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
371 char *pxefile_addr_str;
372 unsigned long pxefile_addr_r;
375 do_getfile = do_get_tftp;
378 return CMD_RET_USAGE;
380 pxefile_addr_str = from_env("pxefile_addr_r");
382 if (!pxefile_addr_str)
385 err = strict_strtoul(pxefile_addr_str, 16,
386 (unsigned long *)&pxefile_addr_r);
391 * Keep trying paths until we successfully get a file we're looking
394 if (pxe_uuid_path(cmdtp, pxefile_addr_r) > 0 ||
395 pxe_mac_path(cmdtp, pxefile_addr_r) > 0 ||
396 pxe_ipaddr_paths(cmdtp, pxefile_addr_r) > 0) {
397 printf("Config file found\n");
402 while (pxe_default_paths[i]) {
403 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
404 pxefile_addr_r) > 0) {
405 printf("Config file found\n");
411 printf("Config file not found\n");
418 * Wrapper to make it easier to store the file at file_path in the location
419 * specified by envaddr_name. file_path will be joined to the bootfile path,
420 * if any is specified.
422 * Returns 1 on success or < 0 on error.
424 static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
426 unsigned long file_addr;
429 envaddr = from_env(envaddr_name);
434 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
437 return get_relfile(cmdtp, file_path, file_addr);
441 * A note on the pxe file parser.
443 * We're parsing files that use syslinux grammar, which has a few quirks.
444 * String literals must be recognized based on context - there is no
445 * quoting or escaping support. There's also nothing to explicitly indicate
446 * when a label section completes. We deal with that by ending a label
447 * section whenever we see a line that doesn't include.
449 * As with the syslinux family, this same file format could be reused in the
450 * future for non pxe purposes. The only action it takes during parsing that
451 * would throw this off is handling of include files. It assumes we're using
452 * pxe, and does a tftp download of a file listed as an include file in the
453 * middle of the parsing operation. That could be handled by refactoring it to
454 * take a 'include file getter' function.
458 * Describes a single label given in a pxe file.
460 * Create these with the 'label_create' function given below.
462 * name - the name of the menu as given on the 'menu label' line.
463 * kernel - the path to the kernel file to use for this label.
464 * append - kernel command line to use when booting this label
465 * initrd - path to the initrd to use for this label.
466 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
467 * localboot - 1 if this label specified 'localboot', 0 otherwise.
468 * list - lets these form a list, which a pxe_menu struct will hold.
483 struct list_head list;
487 * Describes a pxe menu as given via pxe files.
489 * title - the name of the menu as given by a 'menu title' line.
490 * default_label - the name of the default label, if any.
491 * timeout - time in tenths of a second to wait for a user key-press before
492 * booting the default label.
493 * prompt - if 0, don't prompt for a choice unless the timeout period is
494 * interrupted. If 1, always prompt for a choice regardless of
496 * labels - a list of labels defined for the menu.
503 struct list_head labels;
507 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
508 * result must be free()'d to reclaim the memory.
510 * Returns NULL if malloc fails.
512 static struct pxe_label *label_create(void)
514 struct pxe_label *label;
516 label = malloc(sizeof(struct pxe_label));
521 memset(label, 0, sizeof(struct pxe_label));
527 * Free the memory used by a pxe_label, including that used by its name,
528 * kernel, append and initrd members, if they're non NULL.
530 * So - be sure to only use dynamically allocated memory for the members of
531 * the pxe_label struct, unless you want to clean it up first. These are
532 * currently only created by the pxe file parsing code.
534 static void label_destroy(struct pxe_label *label)
558 * Print a label and its string members if they're defined.
560 * This is passed as a callback to the menu code for displaying each
563 static void label_print(void *data)
565 struct pxe_label *label = data;
566 const char *c = label->menu ? label->menu : label->name;
568 printf("%s:\t%s\n", label->num, c);
572 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
573 * environment variable is defined. Its contents will be executed as U-Boot
574 * command. If the label specified an 'append' line, its contents will be
575 * used to overwrite the contents of the 'bootargs' environment variable prior
576 * to running 'localcmd'.
578 * Returns 1 on success or < 0 on error.
580 static int label_localboot(struct pxe_label *label)
584 localcmd = from_env("localcmd");
590 char bootargs[CONFIG_SYS_CBSIZE];
592 cli_simple_process_macros(label->append, bootargs);
593 env_set("bootargs", bootargs);
596 debug("running: %s\n", localcmd);
598 return run_command_list(localcmd, strlen(localcmd), 0);
602 * Boot according to the contents of a pxe_label.
604 * If we can't boot for any reason, we return. A successful boot never
607 * The kernel will be stored in the location given by the 'kernel_addr_r'
608 * environment variable.
610 * If the label specifies an initrd file, it will be stored in the location
611 * given by the 'ramdisk_addr_r' environment variable.
613 * If the label specifies an 'append' line, its contents will overwrite that
614 * of the 'bootargs' environment variable.
616 static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
618 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
620 char mac_str[29] = "";
621 char ip_str[68] = "";
629 label->attempted = 1;
631 if (label->localboot) {
632 if (label->localboot_val >= 0)
633 label_localboot(label);
637 if (label->kernel == NULL) {
638 printf("No kernel given, skipping %s\n",
644 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
645 printf("Skipping %s for failure retrieving initrd\n",
650 bootm_argv[2] = initrd_str;
651 strncpy(bootm_argv[2], env_get("ramdisk_addr_r"), 18);
652 strcat(bootm_argv[2], ":");
653 strncat(bootm_argv[2], env_get("filesize"), 9);
656 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
657 printf("Skipping %s for failure retrieving kernel\n",
662 if (label->ipappend & 0x1) {
663 sprintf(ip_str, " ip=%s:%s:%s:%s",
664 env_get("ipaddr"), env_get("serverip"),
665 env_get("gatewayip"), env_get("netmask"));
668 #ifdef CONFIG_CMD_NET
669 if (label->ipappend & 0x2) {
671 strcpy(mac_str, " BOOTIF=");
672 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
678 if ((label->ipappend & 0x3) || label->append) {
679 char bootargs[CONFIG_SYS_CBSIZE] = "";
680 char finalbootargs[CONFIG_SYS_CBSIZE];
682 if (strlen(label->append ?: "") +
683 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
684 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
685 strlen(label->append ?: ""),
686 strlen(ip_str), strlen(mac_str),
691 strncpy(bootargs, label->append,
693 strcat(bootargs, ip_str);
694 strcat(bootargs, mac_str);
696 cli_simple_process_macros(bootargs, finalbootargs);
697 env_set("bootargs", finalbootargs);
698 printf("append: %s\n", finalbootargs);
702 bootm_argv[1] = env_get("kernel_addr_r");
705 * fdt usage is optional:
706 * It handles the following scenarios. All scenarios are exclusive
708 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
709 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
710 * and adjust argc appropriately.
712 * Scenario 2: If there is an fdt_addr specified, pass it along to
713 * bootm, and adjust argc appropriately.
715 * Scenario 3: fdt blob is not available.
717 bootm_argv[3] = env_get("fdt_addr_r");
719 /* if fdt label is defined then get fdt from server */
721 char *fdtfile = NULL;
722 char *fdtfilefree = NULL;
725 fdtfile = label->fdt;
726 } else if (label->fdtdir) {
727 char *f1, *f2, *f3, *f4, *slash;
729 f1 = env_get("fdtfile");
736 * For complex cases where this code doesn't
737 * generate the correct filename, the board
738 * code should set $fdtfile during early boot,
739 * or the boot scripts should set $fdtfile
740 * before invoking "pxe" or "sysboot".
744 f3 = env_get("board");
748 len = strlen(label->fdtdir);
751 else if (label->fdtdir[len - 1] != '/')
756 len = strlen(label->fdtdir) + strlen(slash) +
757 strlen(f1) + strlen(f2) + strlen(f3) +
759 fdtfilefree = malloc(len);
761 printf("malloc fail (FDT filename)\n");
765 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
766 label->fdtdir, slash, f1, f2, f3, f4);
767 fdtfile = fdtfilefree;
771 int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
774 printf("Skipping %s for failure retrieving fdt\n",
779 bootm_argv[3] = NULL;
784 bootm_argv[3] = env_get("fdt_addr");
792 kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
793 buf = map_sysmem(kernel_addr, 0);
794 /* Try bootm for legacy and FIT format image */
795 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
796 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
797 #ifdef CONFIG_CMD_BOOTI
798 /* Try booting an AArch64 Linux kernel image */
800 do_booti(cmdtp, 0, bootm_argc, bootm_argv);
801 #elif defined(CONFIG_CMD_BOOTZ)
802 /* Try booting a Image */
804 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
811 * Tokens for the pxe file parser.
837 * A token - given by a value and a type.
841 enum token_type type;
845 * Keywords recognized.
847 static const struct token keywords[] = {
850 {"timeout", T_TIMEOUT},
851 {"default", T_DEFAULT},
852 {"prompt", T_PROMPT},
854 {"kernel", T_KERNEL},
856 {"localboot", T_LOCALBOOT},
857 {"append", T_APPEND},
858 {"initrd", T_INITRD},
859 {"include", T_INCLUDE},
860 {"devicetree", T_FDT},
862 {"devicetreedir", T_FDTDIR},
863 {"fdtdir", T_FDTDIR},
864 {"ontimeout", T_ONTIMEOUT,},
865 {"ipappend", T_IPAPPEND,},
870 * Since pxe(linux) files don't have a token to identify the start of a
871 * literal, we have to keep track of when we're in a state where a literal is
872 * expected vs when we're in a state a keyword is expected.
881 * get_string retrieves a string from *p and stores it as a token in
884 * get_string used for scanning both string literals and keywords.
886 * Characters from *p are copied into t-val until a character equal to
887 * delim is found, or a NUL byte is reached. If delim has the special value of
888 * ' ', any whitespace character will be used as a delimiter.
890 * If lower is unequal to 0, uppercase characters will be converted to
891 * lowercase in the result. This is useful to make keywords case
894 * The location of *p is updated to point to the first character after the end
895 * of the token - the ending delimiter.
897 * On success, the new value of t->val is returned. Memory for t->val is
898 * allocated using malloc and must be free()'d to reclaim it. If insufficient
899 * memory is available, NULL is returned.
901 static char *get_string(char **p, struct token *t, char delim, int lower)
907 * b and e both start at the beginning of the input stream.
909 * e is incremented until we find the ending delimiter, or a NUL byte
910 * is reached. Then, we take e - b to find the length of the token.
915 if ((delim == ' ' && isspace(*e)) || delim == *e)
923 * Allocate memory to hold the string, and copy it in, converting
924 * characters to lowercase if lower is != 0.
926 t->val = malloc(len + 1);
930 for (i = 0; i < len; i++, b++) {
932 t->val[i] = tolower(*b);
940 * Update *p so the caller knows where to continue scanning.
950 * Populate a keyword token with a type and value.
952 static void get_keyword(struct token *t)
956 for (i = 0; keywords[i].val; i++) {
957 if (!strcmp(t->val, keywords[i].val)) {
958 t->type = keywords[i].type;
965 * Get the next token. We have to keep track of which state we're in to know
966 * if we're looking to get a string literal or a keyword.
968 * *p is updated to point at the first character after the current token.
970 static void get_token(char **p, struct token *t, enum lex_state state)
976 /* eat non EOL whitespace */
981 * eat comments. note that string literals can't begin with #, but
982 * can contain a # after their first character.
985 while (*c && *c != '\n')
992 } else if (*c == '\0') {
995 } else if (state == L_SLITERAL) {
996 get_string(&c, t, '\n', 0);
997 } else if (state == L_KEYWORD) {
999 * when we expect a keyword, we first get the next string
1000 * token delimited by whitespace, and then check if it
1001 * matches a keyword in our keyword list. if it does, it's
1002 * converted to a keyword token of the appropriate type, and
1003 * if not, it remains a string token.
1005 get_string(&c, t, ' ', 1);
1013 * Increment *c until we get to the end of the current line, or EOF.
1015 static void eol_or_eof(char **c)
1017 while (**c && **c != '\n')
1022 * All of these parse_* functions share some common behavior.
1024 * They finish with *c pointing after the token they parse, and return 1 on
1025 * success, or < 0 on error.
1029 * Parse a string literal and store a pointer it at *dst. String literals
1030 * terminate at the end of the line.
1032 static int parse_sliteral(char **c, char **dst)
1037 get_token(c, &t, L_SLITERAL);
1039 if (t.type != T_STRING) {
1040 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1050 * Parse a base 10 (unsigned) integer and store it at *dst.
1052 static int parse_integer(char **c, int *dst)
1057 get_token(c, &t, L_SLITERAL);
1059 if (t.type != T_STRING) {
1060 printf("Expected string: %.*s\n", (int)(*c - s), s);
1064 *dst = simple_strtol(t.val, NULL, 10);
1071 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
1072 struct pxe_menu *cfg, int nest_level);
1075 * Parse an include statement, and retrieve and parse the file it mentions.
1077 * base should point to a location where it's safe to store the file, and
1078 * nest_level should indicate how many nested includes have occurred. For this
1079 * include, nest_level has already been incremented and doesn't need to be
1082 static int handle_include(cmd_tbl_t *cmdtp, char **c, unsigned long base,
1083 struct pxe_menu *cfg, int nest_level)
1091 err = parse_sliteral(c, &include_path);
1094 printf("Expected include path: %.*s\n",
1099 err = get_pxe_file(cmdtp, include_path, base);
1102 printf("Couldn't retrieve %s\n", include_path);
1106 buf = map_sysmem(base, 0);
1107 ret = parse_pxefile_top(cmdtp, buf, base, cfg, nest_level);
1114 * Parse lines that begin with 'menu'.
1116 * base and nest are provided to handle the 'menu include' case.
1118 * base should point to a location where it's safe to store the included file.
1120 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1121 * a file it includes, 3 when parsing a file included by that file, and so on.
1123 static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg,
1124 unsigned long base, int nest_level)
1130 get_token(c, &t, L_KEYWORD);
1134 err = parse_sliteral(c, &cfg->title);
1139 err = handle_include(cmdtp, c, base, cfg,
1144 printf("Ignoring malformed menu command: %.*s\n",
1157 * Handles parsing a 'menu line' when we're parsing a label.
1159 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1160 struct pxe_label *label)
1167 get_token(c, &t, L_KEYWORD);
1171 if (!cfg->default_label)
1172 cfg->default_label = strdup(label->name);
1174 if (!cfg->default_label)
1179 parse_sliteral(c, &label->menu);
1182 printf("Ignoring malformed menu command: %.*s\n",
1192 * Parses a label and adds it to the list of labels for a menu.
1194 * A label ends when we either get to the end of a file, or
1195 * get some input we otherwise don't have a handler defined
1199 static int parse_label(char **c, struct pxe_menu *cfg)
1204 struct pxe_label *label;
1207 label = label_create();
1211 err = parse_sliteral(c, &label->name);
1213 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1214 label_destroy(label);
1218 list_add_tail(&label->list, &cfg->labels);
1222 get_token(c, &t, L_KEYWORD);
1227 err = parse_label_menu(c, cfg, label);
1232 err = parse_sliteral(c, &label->kernel);
1236 err = parse_sliteral(c, &label->append);
1239 s = strstr(label->append, "initrd=");
1243 len = (int)(strchr(s, ' ') - s);
1244 label->initrd = malloc(len + 1);
1245 strncpy(label->initrd, s, len);
1246 label->initrd[len] = '\0';
1252 err = parse_sliteral(c, &label->initrd);
1257 err = parse_sliteral(c, &label->fdt);
1262 err = parse_sliteral(c, &label->fdtdir);
1266 label->localboot = 1;
1267 err = parse_integer(c, &label->localboot_val);
1271 err = parse_integer(c, &label->ipappend);
1279 * put the token back! we don't want it - it's the end
1280 * of a label and whatever token this is, it's
1281 * something for the menu level context to handle.
1293 * This 16 comes from the limit pxelinux imposes on nested includes.
1295 * There is no reason at all we couldn't do more, but some limit helps prevent
1296 * infinite (until crash occurs) recursion if a file tries to include itself.
1298 #define MAX_NEST_LEVEL 16
1301 * Entry point for parsing a menu file. nest_level indicates how many times
1302 * we've nested in includes. It will be 1 for the top level menu file.
1304 * Returns 1 on success, < 0 on error.
1306 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
1307 struct pxe_menu *cfg, int nest_level)
1310 char *s, *b, *label_name;
1315 if (nest_level > MAX_NEST_LEVEL) {
1316 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1323 get_token(&p, &t, L_KEYWORD);
1329 err = parse_menu(cmdtp, &p, cfg,
1330 base + ALIGN(strlen(b) + 1, 4),
1335 err = parse_integer(&p, &cfg->timeout);
1339 err = parse_label(&p, cfg);
1344 err = parse_sliteral(&p, &label_name);
1347 if (cfg->default_label)
1348 free(cfg->default_label);
1350 cfg->default_label = label_name;
1356 err = handle_include(cmdtp, &p,
1357 base + ALIGN(strlen(b), 4), cfg,
1372 printf("Ignoring unknown command: %.*s\n",
1383 * Free the memory used by a pxe_menu and its labels.
1385 static void destroy_pxe_menu(struct pxe_menu *cfg)
1387 struct list_head *pos, *n;
1388 struct pxe_label *label;
1393 if (cfg->default_label)
1394 free(cfg->default_label);
1396 list_for_each_safe(pos, n, &cfg->labels) {
1397 label = list_entry(pos, struct pxe_label, list);
1399 label_destroy(label);
1406 * Entry point for parsing a pxe file. This is only used for the top level
1409 * Returns NULL if there is an error, otherwise, returns a pointer to a
1410 * pxe_menu struct populated with the results of parsing the pxe file (and any
1411 * files it includes). The resulting pxe_menu struct can be free()'d by using
1412 * the destroy_pxe_menu() function.
1414 static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, unsigned long menucfg)
1416 struct pxe_menu *cfg;
1420 cfg = malloc(sizeof(struct pxe_menu));
1425 memset(cfg, 0, sizeof(struct pxe_menu));
1427 INIT_LIST_HEAD(&cfg->labels);
1429 buf = map_sysmem(menucfg, 0);
1430 r = parse_pxefile_top(cmdtp, buf, menucfg, cfg, 1);
1434 destroy_pxe_menu(cfg);
1442 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1445 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1447 struct pxe_label *label;
1448 struct list_head *pos;
1452 char *default_num = NULL;
1455 * Create a menu and add items for all the labels.
1457 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1463 list_for_each(pos, &cfg->labels) {
1464 label = list_entry(pos, struct pxe_label, list);
1466 sprintf(label->num, "%d", i++);
1467 if (menu_item_add(m, label->num, label) != 1) {
1471 if (cfg->default_label &&
1472 (strcmp(label->name, cfg->default_label) == 0))
1473 default_num = label->num;
1478 * After we've created items for each label in the menu, set the
1479 * menu's default label if one was specified.
1482 err = menu_default_set(m, default_num);
1484 if (err != -ENOENT) {
1489 printf("Missing default: %s\n", cfg->default_label);
1497 * Try to boot any labels we have yet to attempt to boot.
1499 static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1501 struct list_head *pos;
1502 struct pxe_label *label;
1504 list_for_each(pos, &cfg->labels) {
1505 label = list_entry(pos, struct pxe_label, list);
1507 if (!label->attempted)
1508 label_boot(cmdtp, label);
1513 * Boot the system as prescribed by a pxe_menu.
1515 * Use the menu system to either get the user's choice or the default, based
1516 * on config or user input. If there is no default or user's choice,
1517 * attempted to boot labels in the order they were given in pxe files.
1518 * If the default or user's choice fails to boot, attempt to boot other
1519 * labels in the order they were given in pxe files.
1521 * If this function returns, there weren't any labels that successfully
1522 * booted, or the user interrupted the menu selection via ctrl+c.
1524 static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1530 m = pxe_menu_to_menu(cfg);
1534 err = menu_get_choice(m, &choice);
1539 * err == 1 means we got a choice back from menu_get_choice.
1541 * err == -ENOENT if the menu was setup to select the default but no
1542 * default was set. in that case, we should continue trying to boot
1543 * labels that haven't been attempted yet.
1545 * otherwise, the user interrupted or there was some other error and
1550 err = label_boot(cmdtp, choice);
1553 } else if (err != -ENOENT) {
1557 boot_unattempted_labels(cmdtp, cfg);
1560 #ifdef CONFIG_CMD_NET
1562 * Boots a system using a pxe file
1564 * Returns 0 on success, 1 on error.
1567 do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1569 unsigned long pxefile_addr_r;
1570 struct pxe_menu *cfg;
1571 char *pxefile_addr_str;
1573 do_getfile = do_get_tftp;
1576 pxefile_addr_str = from_env("pxefile_addr_r");
1577 if (!pxefile_addr_str)
1580 } else if (argc == 2) {
1581 pxefile_addr_str = argv[1];
1583 return CMD_RET_USAGE;
1586 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1587 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1591 cfg = parse_pxefile(cmdtp, pxefile_addr_r);
1594 printf("Error parsing config file\n");
1598 handle_pxe_menu(cmdtp, cfg);
1600 destroy_pxe_menu(cfg);
1602 copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
1607 static cmd_tbl_t cmd_pxe_sub[] = {
1608 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1609 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1612 static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1617 return CMD_RET_USAGE;
1621 /* drop initial "pxe" arg */
1625 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1628 return cp->cmd(cmdtp, flag, argc, argv);
1630 return CMD_RET_USAGE;
1635 "commands to get and boot from pxe files",
1636 "get - try to retrieve a pxe file using tftp\npxe "
1637 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1642 * Boots a system using a local disk syslinux/extlinux file
1644 * Returns 0 on success, 1 on error.
1646 static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1648 unsigned long pxefile_addr_r;
1649 struct pxe_menu *cfg;
1650 char *pxefile_addr_str;
1656 if (argc > 1 && strstr(argv[1], "-p")) {
1663 return cmd_usage(cmdtp);
1666 pxefile_addr_str = from_env("pxefile_addr_r");
1667 if (!pxefile_addr_str)
1670 pxefile_addr_str = argv[4];
1674 filename = env_get("bootfile");
1677 env_set("bootfile", filename);
1680 if (strstr(argv[3], "ext2"))
1681 do_getfile = do_get_ext2;
1682 else if (strstr(argv[3], "fat"))
1683 do_getfile = do_get_fat;
1684 else if (strstr(argv[3], "any"))
1685 do_getfile = do_get_any;
1687 printf("Invalid filesystem: %s\n", argv[3]);
1690 fs_argv[1] = argv[1];
1691 fs_argv[2] = argv[2];
1693 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1694 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1698 if (get_pxe_file(cmdtp, filename, pxefile_addr_r) < 0) {
1699 printf("Error reading config file\n");
1703 cfg = parse_pxefile(cmdtp, pxefile_addr_r);
1706 printf("Error parsing config file\n");
1713 handle_pxe_menu(cmdtp, cfg);
1715 destroy_pxe_menu(cfg);
1721 sysboot, 7, 1, do_sysboot,
1722 "command to get and boot from syslinux files",
1723 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1724 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1725 " or any filesystem on 'dev' on 'interface' to address 'addr'"