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
);
50 * Convert an ethaddr from the environment to the format used by pxelinux
51 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
52 * the beginning of the ethernet address to indicate a hardware type of
53 * Ethernet. Also converts uppercase hex characters into lowercase, to match
54 * pxelinux's behavior.
56 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
57 * environment, or some other value < 0 on error.
59 static int format_mac_pxe(char *outbuf
, size_t outbuf_len
)
63 if (outbuf_len
< 21) {
64 printf("outbuf is too small (%zd < 21)\n", outbuf_len
);
69 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
73 sprintf(outbuf
, "01-%02x-%02x-%02x-%02x-%02x-%02x",
74 ethaddr
[0], ethaddr
[1], ethaddr
[2],
75 ethaddr
[3], ethaddr
[4], ethaddr
[5]);
82 * Returns the directory the file specified in the bootfile env variable is
83 * in. If bootfile isn't defined in the environment, return NULL, which should
84 * be interpreted as "don't prepend anything to paths".
86 static int get_bootfile_path(const char *file_path
, char *bootfile_path
,
87 size_t bootfile_path_size
)
89 char *bootfile
, *last_slash
;
92 /* Only syslinux allows absolute paths */
93 if (file_path
[0] == '/' && !is_pxe
)
96 bootfile
= from_env("bootfile");
101 last_slash
= strrchr(bootfile
, '/');
103 if (last_slash
== NULL
)
106 path_len
= (last_slash
- bootfile
) + 1;
108 if (bootfile_path_size
< path_len
) {
109 printf("bootfile_path too small. (%zd < %zd)\n",
110 bootfile_path_size
, path_len
);
115 strncpy(bootfile_path
, bootfile
, path_len
);
118 bootfile_path
[path_len
] = '\0';
123 static int (*do_getfile
)(cmd_tbl_t
*cmdtp
, const char *file_path
, char *file_addr
);
125 #ifdef CONFIG_CMD_NET
126 static int do_get_tftp(cmd_tbl_t
*cmdtp
, const char *file_path
, char *file_addr
)
128 char *tftp_argv
[] = {"tftp", NULL
, NULL
, NULL
};
130 tftp_argv
[1] = file_addr
;
131 tftp_argv
[2] = (void *)file_path
;
133 if (do_tftpb(cmdtp
, 0, 3, tftp_argv
))
140 static char *fs_argv
[5];
142 static int do_get_ext2(cmd_tbl_t
*cmdtp
, const char *file_path
, char *file_addr
)
144 #ifdef CONFIG_CMD_EXT2
145 fs_argv
[0] = "ext2load";
146 fs_argv
[3] = file_addr
;
147 fs_argv
[4] = (void *)file_path
;
149 if (!do_ext2load(cmdtp
, 0, 5, fs_argv
))
155 static int do_get_fat(cmd_tbl_t
*cmdtp
, const char *file_path
, char *file_addr
)
157 #ifdef CONFIG_CMD_FAT
158 fs_argv
[0] = "fatload";
159 fs_argv
[3] = file_addr
;
160 fs_argv
[4] = (void *)file_path
;
162 if (!do_fat_fsload(cmdtp
, 0, 5, fs_argv
))
168 static int do_get_any(cmd_tbl_t
*cmdtp
, const char *file_path
, char *file_addr
)
170 #ifdef CONFIG_CMD_FS_GENERIC
172 fs_argv
[3] = file_addr
;
173 fs_argv
[4] = (void *)file_path
;
175 if (!do_load(cmdtp
, 0, 5, fs_argv
, FS_TYPE_ANY
))
182 * As in pxelinux, paths to files referenced from files we retrieve are
183 * relative to the location of bootfile. get_relfile takes such a path and
184 * joins it with the bootfile path to get the full path to the target file. If
185 * the bootfile path is NULL, we use file_path as is.
187 * Returns 1 for success, or < 0 on error.
189 static int get_relfile(cmd_tbl_t
*cmdtp
, const char *file_path
, void *file_addr
)
192 char relfile
[MAX_TFTP_PATH_LEN
+1];
196 err
= get_bootfile_path(file_path
, relfile
, sizeof(relfile
));
201 path_len
= strlen(file_path
);
202 path_len
+= strlen(relfile
);
204 if (path_len
> MAX_TFTP_PATH_LEN
) {
205 printf("Base path too long (%s%s)\n",
209 return -ENAMETOOLONG
;
212 strcat(relfile
, file_path
);
214 printf("Retrieving file: %s\n", relfile
);
216 sprintf(addr_buf
, "%p", file_addr
);
218 return do_getfile(cmdtp
, relfile
, addr_buf
);
222 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
223 * 'bootfile' was specified in the environment, the path to bootfile will be
224 * prepended to 'file_path' and the resulting path will be used.
226 * Returns 1 on success, or < 0 for error.
228 static int get_pxe_file(cmd_tbl_t
*cmdtp
, const char *file_path
, void *file_addr
)
230 unsigned long config_file_size
;
234 err
= get_relfile(cmdtp
, file_path
, file_addr
);
240 * the file comes without a NUL byte at the end, so find out its size
241 * and add the NUL byte.
243 tftp_filesize
= from_env("filesize");
248 if (strict_strtoul(tftp_filesize
, 16, &config_file_size
) < 0)
251 *(char *)(file_addr
+ config_file_size
) = '\0';
256 #ifdef CONFIG_CMD_NET
258 #define PXELINUX_DIR "pxelinux.cfg/"
261 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
262 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
263 * from the bootfile path, as described above.
265 * Returns 1 on success or < 0 on error.
267 static int get_pxelinux_path(cmd_tbl_t
*cmdtp
, const char *file
, void *pxefile_addr_r
)
269 size_t base_len
= strlen(PXELINUX_DIR
);
270 char path
[MAX_TFTP_PATH_LEN
+1];
272 if (base_len
+ strlen(file
) > MAX_TFTP_PATH_LEN
) {
273 printf("path (%s%s) too long, skipping\n",
275 return -ENAMETOOLONG
;
278 sprintf(path
, PXELINUX_DIR
"%s", file
);
280 return get_pxe_file(cmdtp
, path
, pxefile_addr_r
);
284 * Looks for a pxe file with a name based on the pxeuuid environment variable.
286 * Returns 1 on success or < 0 on error.
288 static int pxe_uuid_path(cmd_tbl_t
*cmdtp
, void *pxefile_addr_r
)
292 uuid_str
= from_env("pxeuuid");
297 return get_pxelinux_path(cmdtp
, uuid_str
, pxefile_addr_r
);
301 * Looks for a pxe file with a name based on the 'ethaddr' environment
304 * Returns 1 on success or < 0 on error.
306 static int pxe_mac_path(cmd_tbl_t
*cmdtp
, void *pxefile_addr_r
)
311 err
= format_mac_pxe(mac_str
, sizeof(mac_str
));
316 return get_pxelinux_path(cmdtp
, mac_str
, pxefile_addr_r
);
320 * Looks for pxe files with names based on our IP address. See pxelinux
321 * documentation for details on what these file names look like. We match
324 * Returns 1 on success or < 0 on error.
326 static int pxe_ipaddr_paths(cmd_tbl_t
*cmdtp
, void *pxefile_addr_r
)
331 sprintf(ip_addr
, "%08X", ntohl(NetOurIP
));
333 for (mask_pos
= 7; mask_pos
>= 0; mask_pos
--) {
334 err
= get_pxelinux_path(cmdtp
, ip_addr
, pxefile_addr_r
);
339 ip_addr
[mask_pos
] = '\0';
346 * Entry point for the 'pxe get' command.
347 * This Follows pxelinux's rules to download a config file from a tftp server.
348 * The file is stored at the location given by the pxefile_addr_r environment
349 * variable, which must be set.
351 * UUID comes from pxeuuid env variable, if defined
352 * MAC addr comes from ethaddr env variable, if defined
355 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
357 * Returns 0 on success or 1 on error.
360 do_pxe_get(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
362 char *pxefile_addr_str
;
363 unsigned long pxefile_addr_r
;
366 do_getfile
= do_get_tftp
;
369 return CMD_RET_USAGE
;
371 pxefile_addr_str
= from_env("pxefile_addr_r");
373 if (!pxefile_addr_str
)
376 err
= strict_strtoul(pxefile_addr_str
, 16,
377 (unsigned long *)&pxefile_addr_r
);
382 * Keep trying paths until we successfully get a file we're looking
385 if (pxe_uuid_path(cmdtp
, (void *)pxefile_addr_r
) > 0 ||
386 pxe_mac_path(cmdtp
, (void *)pxefile_addr_r
) > 0 ||
387 pxe_ipaddr_paths(cmdtp
, (void *)pxefile_addr_r
) > 0) {
388 printf("Config file found\n");
393 while (pxe_default_paths
[i
]) {
394 if (get_pxelinux_path(cmdtp
, pxe_default_paths
[i
],
395 (void *)pxefile_addr_r
) > 0) {
396 printf("Config file found\n");
402 printf("Config file not found\n");
409 * Wrapper to make it easier to store the file at file_path in the location
410 * specified by envaddr_name. file_path will be joined to the bootfile path,
411 * if any is specified.
413 * Returns 1 on success or < 0 on error.
415 static int get_relfile_envaddr(cmd_tbl_t
*cmdtp
, const char *file_path
, const char *envaddr_name
)
417 unsigned long file_addr
;
420 envaddr
= from_env(envaddr_name
);
425 if (strict_strtoul(envaddr
, 16, &file_addr
) < 0)
428 return get_relfile(cmdtp
, file_path
, (void *)file_addr
);
432 * A note on the pxe file parser.
434 * We're parsing files that use syslinux grammar, which has a few quirks.
435 * String literals must be recognized based on context - there is no
436 * quoting or escaping support. There's also nothing to explicitly indicate
437 * when a label section completes. We deal with that by ending a label
438 * section whenever we see a line that doesn't include.
440 * As with the syslinux family, this same file format could be reused in the
441 * future for non pxe purposes. The only action it takes during parsing that
442 * would throw this off is handling of include files. It assumes we're using
443 * pxe, and does a tftp download of a file listed as an include file in the
444 * middle of the parsing operation. That could be handled by refactoring it to
445 * take a 'include file getter' function.
449 * Describes a single label given in a pxe file.
451 * Create these with the 'label_create' function given below.
453 * name - the name of the menu as given on the 'menu label' line.
454 * kernel - the path to the kernel file to use for this label.
455 * append - kernel command line to use when booting this label
456 * initrd - path to the initrd to use for this label.
457 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
458 * localboot - 1 if this label specified 'localboot', 0 otherwise.
459 * list - lets these form a list, which a pxe_menu struct will hold.
474 struct list_head list
;
478 * Describes a pxe menu as given via pxe files.
480 * title - the name of the menu as given by a 'menu title' line.
481 * default_label - the name of the default label, if any.
482 * timeout - time in tenths of a second to wait for a user key-press before
483 * booting the default label.
484 * prompt - if 0, don't prompt for a choice unless the timeout period is
485 * interrupted. If 1, always prompt for a choice regardless of
487 * labels - a list of labels defined for the menu.
494 struct list_head labels
;
498 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
499 * result must be free()'d to reclaim the memory.
501 * Returns NULL if malloc fails.
503 static struct pxe_label
*label_create(void)
505 struct pxe_label
*label
;
507 label
= malloc(sizeof(struct pxe_label
));
512 memset(label
, 0, sizeof(struct pxe_label
));
518 * Free the memory used by a pxe_label, including that used by its name,
519 * kernel, append and initrd members, if they're non NULL.
521 * So - be sure to only use dynamically allocated memory for the members of
522 * the pxe_label struct, unless you want to clean it up first. These are
523 * currently only created by the pxe file parsing code.
525 static void label_destroy(struct pxe_label
*label
)
549 * Print a label and its string members if they're defined.
551 * This is passed as a callback to the menu code for displaying each
554 static void label_print(void *data
)
556 struct pxe_label
*label
= data
;
557 const char *c
= label
->menu
? label
->menu
: label
->name
;
559 printf("%s:\t%s\n", label
->num
, c
);
563 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
564 * environment variable is defined. Its contents will be executed as U-boot
565 * command. If the label specified an 'append' line, its contents will be
566 * used to overwrite the contents of the 'bootargs' environment variable prior
567 * to running 'localcmd'.
569 * Returns 1 on success or < 0 on error.
571 static int label_localboot(struct pxe_label
*label
)
575 localcmd
= from_env("localcmd");
581 setenv("bootargs", label
->append
);
583 debug("running: %s\n", localcmd
);
585 return run_command_list(localcmd
, strlen(localcmd
), 0);
589 * Boot according to the contents of a pxe_label.
591 * If we can't boot for any reason, we return. A successful boot never
594 * The kernel will be stored in the location given by the 'kernel_addr_r'
595 * environment variable.
597 * If the label specifies an initrd file, it will be stored in the location
598 * given by the 'ramdisk_addr_r' environment variable.
600 * If the label specifies an 'append' line, its contents will overwrite that
601 * of the 'bootargs' environment variable.
603 static int label_boot(cmd_tbl_t
*cmdtp
, struct pxe_label
*label
)
605 char *bootm_argv
[] = { "bootm", NULL
, NULL
, NULL
, NULL
};
607 char mac_str
[29] = "";
608 char ip_str
[68] = "";
615 label
->attempted
= 1;
617 if (label
->localboot
) {
618 if (label
->localboot_val
>= 0)
619 label_localboot(label
);
623 if (label
->kernel
== NULL
) {
624 printf("No kernel given, skipping %s\n",
630 if (get_relfile_envaddr(cmdtp
, label
->initrd
, "ramdisk_addr_r") < 0) {
631 printf("Skipping %s for failure retrieving initrd\n",
636 bootm_argv
[2] = initrd_str
;
637 strcpy(bootm_argv
[2], getenv("ramdisk_addr_r"));
638 strcat(bootm_argv
[2], ":");
639 strcat(bootm_argv
[2], getenv("filesize"));
644 if (get_relfile_envaddr(cmdtp
, label
->kernel
, "kernel_addr_r") < 0) {
645 printf("Skipping %s for failure retrieving kernel\n",
650 if (label
->ipappend
& 0x1) {
651 sprintf(ip_str
, " ip=%s:%s:%s:%s",
652 getenv("ipaddr"), getenv("serverip"),
653 getenv("gatewayip"), getenv("netmask"));
654 len
+= strlen(ip_str
);
657 #ifdef CONFIG_CMD_NET
658 if (label
->ipappend
& 0x2) {
660 strcpy(mac_str
, " BOOTIF=");
661 err
= format_mac_pxe(mac_str
+ 8, sizeof(mac_str
) - 8);
664 len
+= strlen(mac_str
);
669 len
+= strlen(label
->append
);
672 bootargs
= malloc(len
+ 1);
677 strcpy(bootargs
, label
->append
);
678 strcat(bootargs
, ip_str
);
679 strcat(bootargs
, mac_str
);
681 setenv("bootargs", bootargs
);
682 printf("append: %s\n", bootargs
);
687 bootm_argv
[1] = getenv("kernel_addr_r");
690 * fdt usage is optional:
691 * It handles the following scenarios. All scenarios are exclusive
693 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
694 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
695 * and adjust argc appropriately.
697 * Scenario 2: If there is an fdt_addr specified, pass it along to
698 * bootm, and adjust argc appropriately.
700 * Scenario 3: fdt blob is not available.
702 bootm_argv
[3] = getenv("fdt_addr_r");
704 /* if fdt label is defined then get fdt from server */
706 char *fdtfile
= NULL
;
707 char *fdtfilefree
= NULL
;
710 fdtfile
= label
->fdt
;
711 } else if (label
->fdtdir
) {
712 char *f1
, *f2
, *f3
, *f4
, *slash
;
714 f1
= getenv("fdtfile");
721 * For complex cases where this code doesn't
722 * generate the correct filename, the board
723 * code should set $fdtfile during early boot,
724 * or the boot scripts should set $fdtfile
725 * before invoking "pxe" or "sysboot".
729 f3
= getenv("board");
733 len
= strlen(label
->fdtdir
);
736 else if (label
->fdtdir
[len
- 1] != '/')
741 len
= strlen(label
->fdtdir
) + strlen(slash
) +
742 strlen(f1
) + strlen(f2
) + strlen(f3
) +
744 fdtfilefree
= malloc(len
);
746 printf("malloc fail (FDT filename)\n");
750 snprintf(fdtfilefree
, len
, "%s%s%s%s%s%s",
751 label
->fdtdir
, slash
, f1
, f2
, f3
, f4
);
752 fdtfile
= fdtfilefree
;
756 int err
= get_relfile_envaddr(cmdtp
, fdtfile
, "fdt_addr_r");
759 printf("Skipping %s for failure retrieving fdt\n",
764 bootm_argv
[3] = NULL
;
769 bootm_argv
[3] = getenv("fdt_addr");
774 do_bootm(cmdtp
, 0, bootm_argc
, bootm_argv
);
776 #ifdef CONFIG_CMD_BOOTZ
777 /* Try booting a zImage if do_bootm returns */
778 do_bootz(cmdtp
, 0, bootm_argc
, bootm_argv
);
784 * Tokens for the pxe file parser.
810 * A token - given by a value and a type.
814 enum token_type type
;
818 * Keywords recognized.
820 static const struct token keywords
[] = {
823 {"timeout", T_TIMEOUT
},
824 {"default", T_DEFAULT
},
825 {"prompt", T_PROMPT
},
827 {"kernel", T_KERNEL
},
829 {"localboot", T_LOCALBOOT
},
830 {"append", T_APPEND
},
831 {"initrd", T_INITRD
},
832 {"include", T_INCLUDE
},
833 {"devicetree", T_FDT
},
835 {"devicetreedir", T_FDTDIR
},
836 {"fdtdir", T_FDTDIR
},
837 {"ontimeout", T_ONTIMEOUT
,},
838 {"ipappend", T_IPAPPEND
,},
843 * Since pxe(linux) files don't have a token to identify the start of a
844 * literal, we have to keep track of when we're in a state where a literal is
845 * expected vs when we're in a state a keyword is expected.
854 * get_string retrieves a string from *p and stores it as a token in
857 * get_string used for scanning both string literals and keywords.
859 * Characters from *p are copied into t-val until a character equal to
860 * delim is found, or a NUL byte is reached. If delim has the special value of
861 * ' ', any whitespace character will be used as a delimiter.
863 * If lower is unequal to 0, uppercase characters will be converted to
864 * lowercase in the result. This is useful to make keywords case
867 * The location of *p is updated to point to the first character after the end
868 * of the token - the ending delimiter.
870 * On success, the new value of t->val is returned. Memory for t->val is
871 * allocated using malloc and must be free()'d to reclaim it. If insufficient
872 * memory is available, NULL is returned.
874 static char *get_string(char **p
, struct token
*t
, char delim
, int lower
)
880 * b and e both start at the beginning of the input stream.
882 * e is incremented until we find the ending delimiter, or a NUL byte
883 * is reached. Then, we take e - b to find the length of the token.
888 if ((delim
== ' ' && isspace(*e
)) || delim
== *e
)
896 * Allocate memory to hold the string, and copy it in, converting
897 * characters to lowercase if lower is != 0.
899 t
->val
= malloc(len
+ 1);
903 for (i
= 0; i
< len
; i
++, b
++) {
905 t
->val
[i
] = tolower(*b
);
913 * Update *p so the caller knows where to continue scanning.
923 * Populate a keyword token with a type and value.
925 static void get_keyword(struct token
*t
)
929 for (i
= 0; keywords
[i
].val
; i
++) {
930 if (!strcmp(t
->val
, keywords
[i
].val
)) {
931 t
->type
= keywords
[i
].type
;
938 * Get the next token. We have to keep track of which state we're in to know
939 * if we're looking to get a string literal or a keyword.
941 * *p is updated to point at the first character after the current token.
943 static void get_token(char **p
, struct token
*t
, enum lex_state state
)
949 /* eat non EOL whitespace */
954 * eat comments. note that string literals can't begin with #, but
955 * can contain a # after their first character.
958 while (*c
&& *c
!= '\n')
965 } else if (*c
== '\0') {
968 } else if (state
== L_SLITERAL
) {
969 get_string(&c
, t
, '\n', 0);
970 } else if (state
== L_KEYWORD
) {
972 * when we expect a keyword, we first get the next string
973 * token delimited by whitespace, and then check if it
974 * matches a keyword in our keyword list. if it does, it's
975 * converted to a keyword token of the appropriate type, and
976 * if not, it remains a string token.
978 get_string(&c
, t
, ' ', 1);
986 * Increment *c until we get to the end of the current line, or EOF.
988 static void eol_or_eof(char **c
)
990 while (**c
&& **c
!= '\n')
995 * All of these parse_* functions share some common behavior.
997 * They finish with *c pointing after the token they parse, and return 1 on
998 * success, or < 0 on error.
1002 * Parse a string literal and store a pointer it at *dst. String literals
1003 * terminate at the end of the line.
1005 static int parse_sliteral(char **c
, char **dst
)
1010 get_token(c
, &t
, L_SLITERAL
);
1012 if (t
.type
!= T_STRING
) {
1013 printf("Expected string literal: %.*s\n", (int)(*c
- s
), s
);
1023 * Parse a base 10 (unsigned) integer and store it at *dst.
1025 static int parse_integer(char **c
, int *dst
)
1030 get_token(c
, &t
, L_SLITERAL
);
1032 if (t
.type
!= T_STRING
) {
1033 printf("Expected string: %.*s\n", (int)(*c
- s
), s
);
1037 *dst
= simple_strtol(t
.val
, NULL
, 10);
1044 static int parse_pxefile_top(cmd_tbl_t
*cmdtp
, char *p
, struct pxe_menu
*cfg
, int nest_level
);
1047 * Parse an include statement, and retrieve and parse the file it mentions.
1049 * base should point to a location where it's safe to store the file, and
1050 * nest_level should indicate how many nested includes have occurred. For this
1051 * include, nest_level has already been incremented and doesn't need to be
1054 static int handle_include(cmd_tbl_t
*cmdtp
, char **c
, char *base
,
1055 struct pxe_menu
*cfg
, int nest_level
)
1061 err
= parse_sliteral(c
, &include_path
);
1064 printf("Expected include path: %.*s\n",
1069 err
= get_pxe_file(cmdtp
, include_path
, base
);
1072 printf("Couldn't retrieve %s\n", include_path
);
1076 return parse_pxefile_top(cmdtp
, base
, cfg
, nest_level
);
1080 * Parse lines that begin with 'menu'.
1082 * b and nest are provided to handle the 'menu include' case.
1084 * b should be the address where the file currently being parsed is stored.
1086 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1087 * a file it includes, 3 when parsing a file included by that file, and so on.
1089 static int parse_menu(cmd_tbl_t
*cmdtp
, char **c
, struct pxe_menu
*cfg
, char *b
, int nest_level
)
1095 get_token(c
, &t
, L_KEYWORD
);
1099 err
= parse_sliteral(c
, &cfg
->title
);
1104 err
= handle_include(cmdtp
, c
, b
+ strlen(b
) + 1, cfg
,
1109 printf("Ignoring malformed menu command: %.*s\n",
1122 * Handles parsing a 'menu line' when we're parsing a label.
1124 static int parse_label_menu(char **c
, struct pxe_menu
*cfg
,
1125 struct pxe_label
*label
)
1132 get_token(c
, &t
, L_KEYWORD
);
1136 if (!cfg
->default_label
)
1137 cfg
->default_label
= strdup(label
->name
);
1139 if (!cfg
->default_label
)
1144 parse_sliteral(c
, &label
->menu
);
1147 printf("Ignoring malformed menu command: %.*s\n",
1157 * Parses a label and adds it to the list of labels for a menu.
1159 * A label ends when we either get to the end of a file, or
1160 * get some input we otherwise don't have a handler defined
1164 static int parse_label(char **c
, struct pxe_menu
*cfg
)
1169 struct pxe_label
*label
;
1172 label
= label_create();
1176 err
= parse_sliteral(c
, &label
->name
);
1178 printf("Expected label name: %.*s\n", (int)(*c
- s
), s
);
1179 label_destroy(label
);
1183 list_add_tail(&label
->list
, &cfg
->labels
);
1187 get_token(c
, &t
, L_KEYWORD
);
1192 err
= parse_label_menu(c
, cfg
, label
);
1197 err
= parse_sliteral(c
, &label
->kernel
);
1201 err
= parse_sliteral(c
, &label
->append
);
1204 s
= strstr(label
->append
, "initrd=");
1208 len
= (int)(strchr(s
, ' ') - s
);
1209 label
->initrd
= malloc(len
+ 1);
1210 strncpy(label
->initrd
, s
, len
);
1211 label
->initrd
[len
] = '\0';
1217 err
= parse_sliteral(c
, &label
->initrd
);
1222 err
= parse_sliteral(c
, &label
->fdt
);
1227 err
= parse_sliteral(c
, &label
->fdtdir
);
1231 label
->localboot
= 1;
1232 err
= parse_integer(c
, &label
->localboot_val
);
1236 err
= parse_integer(c
, &label
->ipappend
);
1244 * put the token back! we don't want it - it's the end
1245 * of a label and whatever token this is, it's
1246 * something for the menu level context to handle.
1258 * This 16 comes from the limit pxelinux imposes on nested includes.
1260 * There is no reason at all we couldn't do more, but some limit helps prevent
1261 * infinite (until crash occurs) recursion if a file tries to include itself.
1263 #define MAX_NEST_LEVEL 16
1266 * Entry point for parsing a menu file. nest_level indicates how many times
1267 * we've nested in includes. It will be 1 for the top level menu file.
1269 * Returns 1 on success, < 0 on error.
1271 static int parse_pxefile_top(cmd_tbl_t
*cmdtp
, char *p
, struct pxe_menu
*cfg
, int nest_level
)
1274 char *s
, *b
, *label_name
;
1279 if (nest_level
> MAX_NEST_LEVEL
) {
1280 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL
);
1287 get_token(&p
, &t
, L_KEYWORD
);
1293 err
= parse_menu(cmdtp
, &p
, cfg
, b
, nest_level
);
1297 err
= parse_integer(&p
, &cfg
->timeout
);
1301 err
= parse_label(&p
, cfg
);
1306 err
= parse_sliteral(&p
, &label_name
);
1309 if (cfg
->default_label
)
1310 free(cfg
->default_label
);
1312 cfg
->default_label
= label_name
;
1318 err
= handle_include(cmdtp
, &p
, b
+ ALIGN(strlen(b
), 4), cfg
,
1333 printf("Ignoring unknown command: %.*s\n",
1344 * Free the memory used by a pxe_menu and its labels.
1346 static void destroy_pxe_menu(struct pxe_menu
*cfg
)
1348 struct list_head
*pos
, *n
;
1349 struct pxe_label
*label
;
1354 if (cfg
->default_label
)
1355 free(cfg
->default_label
);
1357 list_for_each_safe(pos
, n
, &cfg
->labels
) {
1358 label
= list_entry(pos
, struct pxe_label
, list
);
1360 label_destroy(label
);
1367 * Entry point for parsing a pxe file. This is only used for the top level
1370 * Returns NULL if there is an error, otherwise, returns a pointer to a
1371 * pxe_menu struct populated with the results of parsing the pxe file (and any
1372 * files it includes). The resulting pxe_menu struct can be free()'d by using
1373 * the destroy_pxe_menu() function.
1375 static struct pxe_menu
*parse_pxefile(cmd_tbl_t
*cmdtp
, char *menucfg
)
1377 struct pxe_menu
*cfg
;
1379 cfg
= malloc(sizeof(struct pxe_menu
));
1384 memset(cfg
, 0, sizeof(struct pxe_menu
));
1386 INIT_LIST_HEAD(&cfg
->labels
);
1388 if (parse_pxefile_top(cmdtp
, menucfg
, cfg
, 1) < 0) {
1389 destroy_pxe_menu(cfg
);
1397 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1400 static struct menu
*pxe_menu_to_menu(struct pxe_menu
*cfg
)
1402 struct pxe_label
*label
;
1403 struct list_head
*pos
;
1407 char *default_num
= NULL
;
1410 * Create a menu and add items for all the labels.
1412 m
= menu_create(cfg
->title
, cfg
->timeout
, cfg
->prompt
, label_print
,
1418 list_for_each(pos
, &cfg
->labels
) {
1419 label
= list_entry(pos
, struct pxe_label
, list
);
1421 sprintf(label
->num
, "%d", i
++);
1422 if (menu_item_add(m
, label
->num
, label
) != 1) {
1426 if (cfg
->default_label
&&
1427 (strcmp(label
->name
, cfg
->default_label
) == 0))
1428 default_num
= label
->num
;
1433 * After we've created items for each label in the menu, set the
1434 * menu's default label if one was specified.
1437 err
= menu_default_set(m
, default_num
);
1439 if (err
!= -ENOENT
) {
1444 printf("Missing default: %s\n", cfg
->default_label
);
1452 * Try to boot any labels we have yet to attempt to boot.
1454 static void boot_unattempted_labels(cmd_tbl_t
*cmdtp
, struct pxe_menu
*cfg
)
1456 struct list_head
*pos
;
1457 struct pxe_label
*label
;
1459 list_for_each(pos
, &cfg
->labels
) {
1460 label
= list_entry(pos
, struct pxe_label
, list
);
1462 if (!label
->attempted
)
1463 label_boot(cmdtp
, label
);
1468 * Boot the system as prescribed by a pxe_menu.
1470 * Use the menu system to either get the user's choice or the default, based
1471 * on config or user input. If there is no default or user's choice,
1472 * attempted to boot labels in the order they were given in pxe files.
1473 * If the default or user's choice fails to boot, attempt to boot other
1474 * labels in the order they were given in pxe files.
1476 * If this function returns, there weren't any labels that successfully
1477 * booted, or the user interrupted the menu selection via ctrl+c.
1479 static void handle_pxe_menu(cmd_tbl_t
*cmdtp
, struct pxe_menu
*cfg
)
1485 m
= pxe_menu_to_menu(cfg
);
1489 err
= menu_get_choice(m
, &choice
);
1494 * err == 1 means we got a choice back from menu_get_choice.
1496 * err == -ENOENT if the menu was setup to select the default but no
1497 * default was set. in that case, we should continue trying to boot
1498 * labels that haven't been attempted yet.
1500 * otherwise, the user interrupted or there was some other error and
1505 err
= label_boot(cmdtp
, choice
);
1508 } else if (err
!= -ENOENT
) {
1512 boot_unattempted_labels(cmdtp
, cfg
);
1515 #ifdef CONFIG_CMD_NET
1517 * Boots a system using a pxe file
1519 * Returns 0 on success, 1 on error.
1522 do_pxe_boot(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
1524 unsigned long pxefile_addr_r
;
1525 struct pxe_menu
*cfg
;
1526 char *pxefile_addr_str
;
1528 do_getfile
= do_get_tftp
;
1531 pxefile_addr_str
= from_env("pxefile_addr_r");
1532 if (!pxefile_addr_str
)
1535 } else if (argc
== 2) {
1536 pxefile_addr_str
= argv
[1];
1538 return CMD_RET_USAGE
;
1541 if (strict_strtoul(pxefile_addr_str
, 16, &pxefile_addr_r
) < 0) {
1542 printf("Invalid pxefile address: %s\n", pxefile_addr_str
);
1546 cfg
= parse_pxefile(cmdtp
, (char *)(pxefile_addr_r
));
1549 printf("Error parsing config file\n");
1553 handle_pxe_menu(cmdtp
, cfg
);
1555 destroy_pxe_menu(cfg
);
1560 static cmd_tbl_t cmd_pxe_sub
[] = {
1561 U_BOOT_CMD_MKENT(get
, 1, 1, do_pxe_get
, "", ""),
1562 U_BOOT_CMD_MKENT(boot
, 2, 1, do_pxe_boot
, "", "")
1565 int do_pxe(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
1570 return CMD_RET_USAGE
;
1574 /* drop initial "pxe" arg */
1578 cp
= find_cmd_tbl(argv
[0], cmd_pxe_sub
, ARRAY_SIZE(cmd_pxe_sub
));
1581 return cp
->cmd(cmdtp
, flag
, argc
, argv
);
1583 return CMD_RET_USAGE
;
1588 "commands to get and boot from pxe files",
1589 "get - try to retrieve a pxe file using tftp\npxe "
1590 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1595 * Boots a system using a local disk syslinux/extlinux file
1597 * Returns 0 on success, 1 on error.
1599 int do_sysboot(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
1601 unsigned long pxefile_addr_r
;
1602 struct pxe_menu
*cfg
;
1603 char *pxefile_addr_str
;
1609 if (strstr(argv
[1], "-p")) {
1616 return cmd_usage(cmdtp
);
1619 pxefile_addr_str
= from_env("pxefile_addr_r");
1620 if (!pxefile_addr_str
)
1623 pxefile_addr_str
= argv
[4];
1627 filename
= getenv("bootfile");
1630 setenv("bootfile", filename
);
1633 if (strstr(argv
[3], "ext2"))
1634 do_getfile
= do_get_ext2
;
1635 else if (strstr(argv
[3], "fat"))
1636 do_getfile
= do_get_fat
;
1637 else if (strstr(argv
[3], "any"))
1638 do_getfile
= do_get_any
;
1640 printf("Invalid filesystem: %s\n", argv
[3]);
1643 fs_argv
[1] = argv
[1];
1644 fs_argv
[2] = argv
[2];
1646 if (strict_strtoul(pxefile_addr_str
, 16, &pxefile_addr_r
) < 0) {
1647 printf("Invalid pxefile address: %s\n", pxefile_addr_str
);
1651 if (get_pxe_file(cmdtp
, filename
, (void *)pxefile_addr_r
) < 0) {
1652 printf("Error reading config file\n");
1656 cfg
= parse_pxefile(cmdtp
, (char *)(pxefile_addr_r
));
1659 printf("Error parsing config file\n");
1666 handle_pxe_menu(cmdtp
, cfg
);
1668 destroy_pxe_menu(cfg
);
1674 sysboot
, 7, 1, do_sysboot
,
1675 "command to get and boot from syslinux files",
1676 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1677 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1678 " or any filesystem on 'dev' on 'interface' to address 'addr'"