]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_pxe.c
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>
17 #define MAX_TFTP_PATH_LEN 127
19 const char *pxe_default_paths
[] = {
21 "default-" CONFIG_SYS_ARCH
"-" CONFIG_SYS_SOC
,
23 "default-" CONFIG_SYS_ARCH
,
31 * Like getenv, but prints an error if envvar isn't defined in the
32 * environment. It always returns what getenv does, so it can be used in
33 * place of getenv without changing error handling otherwise.
35 static char *from_env(const char *envvar
)
42 printf("missing environment variable: %s\n", envvar
);
48 * Convert an ethaddr from the environment to the format used by pxelinux
49 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
50 * the beginning of the ethernet address to indicate a hardware type of
51 * Ethernet. Also converts uppercase hex characters into lowercase, to match
52 * pxelinux's behavior.
54 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
55 * environment, or some other value < 0 on error.
57 static int format_mac_pxe(char *outbuf
, size_t outbuf_len
)
61 if (outbuf_len
< 21) {
62 printf("outbuf is too small (%zd < 21)\n", outbuf_len
);
67 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
71 sprintf(outbuf
, "01-%02x-%02x-%02x-%02x-%02x-%02x",
72 ethaddr
[0], ethaddr
[1], ethaddr
[2],
73 ethaddr
[3], ethaddr
[4], ethaddr
[5]);
79 * Returns the directory the file specified in the bootfile env variable is
80 * in. If bootfile isn't defined in the environment, return NULL, which should
81 * be interpreted as "don't prepend anything to paths".
83 static int get_bootfile_path(const char *file_path
, char *bootfile_path
,
84 size_t bootfile_path_size
)
86 char *bootfile
, *last_slash
;
89 /* Only syslinux allows absolute paths */
90 if (file_path
[0] == '/' && !is_pxe
)
93 bootfile
= from_env("bootfile");
98 last_slash
= strrchr(bootfile
, '/');
100 if (last_slash
== NULL
)
103 path_len
= (last_slash
- bootfile
) + 1;
105 if (bootfile_path_size
< path_len
) {
106 printf("bootfile_path too small. (%zd < %zd)\n",
107 bootfile_path_size
, path_len
);
112 strncpy(bootfile_path
, bootfile
, path_len
);
115 bootfile_path
[path_len
] = '\0';
120 static int (*do_getfile
)(cmd_tbl_t
*cmdtp
, const char *file_path
, char *file_addr
);
122 static int do_get_tftp(cmd_tbl_t
*cmdtp
, const char *file_path
, char *file_addr
)
124 char *tftp_argv
[] = {"tftp", NULL
, NULL
, NULL
};
126 tftp_argv
[1] = file_addr
;
127 tftp_argv
[2] = (void *)file_path
;
129 if (do_tftpb(cmdtp
, 0, 3, tftp_argv
))
135 static char *fs_argv
[5];
137 static int do_get_ext2(cmd_tbl_t
*cmdtp
, const char *file_path
, char *file_addr
)
139 #ifdef CONFIG_CMD_EXT2
140 fs_argv
[0] = "ext2load";
141 fs_argv
[3] = file_addr
;
142 fs_argv
[4] = (void *)file_path
;
144 if (!do_ext2load(cmdtp
, 0, 5, fs_argv
))
150 static int do_get_fat(cmd_tbl_t
*cmdtp
, const char *file_path
, char *file_addr
)
152 #ifdef CONFIG_CMD_FAT
153 fs_argv
[0] = "fatload";
154 fs_argv
[3] = file_addr
;
155 fs_argv
[4] = (void *)file_path
;
157 if (!do_fat_fsload(cmdtp
, 0, 5, fs_argv
))
164 * As in pxelinux, paths to files referenced from files we retrieve are
165 * relative to the location of bootfile. get_relfile takes such a path and
166 * joins it with the bootfile path to get the full path to the target file. If
167 * the bootfile path is NULL, we use file_path as is.
169 * Returns 1 for success, or < 0 on error.
171 static int get_relfile(cmd_tbl_t
*cmdtp
, const char *file_path
, void *file_addr
)
174 char relfile
[MAX_TFTP_PATH_LEN
+1];
178 err
= get_bootfile_path(file_path
, relfile
, sizeof(relfile
));
183 path_len
= strlen(file_path
);
184 path_len
+= strlen(relfile
);
186 if (path_len
> MAX_TFTP_PATH_LEN
) {
187 printf("Base path too long (%s%s)\n",
191 return -ENAMETOOLONG
;
194 strcat(relfile
, file_path
);
196 printf("Retrieving file: %s\n", relfile
);
198 sprintf(addr_buf
, "%p", file_addr
);
200 return do_getfile(cmdtp
, relfile
, addr_buf
);
204 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
205 * 'bootfile' was specified in the environment, the path to bootfile will be
206 * prepended to 'file_path' and the resulting path will be used.
208 * Returns 1 on success, or < 0 for error.
210 static int get_pxe_file(cmd_tbl_t
*cmdtp
, const char *file_path
, void *file_addr
)
212 unsigned long config_file_size
;
216 err
= get_relfile(cmdtp
, file_path
, file_addr
);
222 * the file comes without a NUL byte at the end, so find out its size
223 * and add the NUL byte.
225 tftp_filesize
= from_env("filesize");
230 if (strict_strtoul(tftp_filesize
, 16, &config_file_size
) < 0)
233 *(char *)(file_addr
+ config_file_size
) = '\0';
238 #define PXELINUX_DIR "pxelinux.cfg/"
241 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
242 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
243 * from the bootfile path, as described above.
245 * Returns 1 on success or < 0 on error.
247 static int get_pxelinux_path(cmd_tbl_t
*cmdtp
, const char *file
, void *pxefile_addr_r
)
249 size_t base_len
= strlen(PXELINUX_DIR
);
250 char path
[MAX_TFTP_PATH_LEN
+1];
252 if (base_len
+ strlen(file
) > MAX_TFTP_PATH_LEN
) {
253 printf("path (%s%s) too long, skipping\n",
255 return -ENAMETOOLONG
;
258 sprintf(path
, PXELINUX_DIR
"%s", file
);
260 return get_pxe_file(cmdtp
, path
, pxefile_addr_r
);
264 * Looks for a pxe file with a name based on the pxeuuid environment variable.
266 * Returns 1 on success or < 0 on error.
268 static int pxe_uuid_path(cmd_tbl_t
*cmdtp
, void *pxefile_addr_r
)
272 uuid_str
= from_env("pxeuuid");
277 return get_pxelinux_path(cmdtp
, uuid_str
, pxefile_addr_r
);
281 * Looks for a pxe file with a name based on the 'ethaddr' environment
284 * Returns 1 on success or < 0 on error.
286 static int pxe_mac_path(cmd_tbl_t
*cmdtp
, void *pxefile_addr_r
)
291 err
= format_mac_pxe(mac_str
, sizeof(mac_str
));
296 return get_pxelinux_path(cmdtp
, mac_str
, pxefile_addr_r
);
300 * Looks for pxe files with names based on our IP address. See pxelinux
301 * documentation for details on what these file names look like. We match
304 * Returns 1 on success or < 0 on error.
306 static int pxe_ipaddr_paths(cmd_tbl_t
*cmdtp
, void *pxefile_addr_r
)
311 sprintf(ip_addr
, "%08X", ntohl(NetOurIP
));
313 for (mask_pos
= 7; mask_pos
>= 0; mask_pos
--) {
314 err
= get_pxelinux_path(cmdtp
, ip_addr
, pxefile_addr_r
);
319 ip_addr
[mask_pos
] = '\0';
326 * Entry point for the 'pxe get' command.
327 * This Follows pxelinux's rules to download a config file from a tftp server.
328 * The file is stored at the location given by the pxefile_addr_r environment
329 * variable, which must be set.
331 * UUID comes from pxeuuid env variable, if defined
332 * MAC addr comes from ethaddr env variable, if defined
335 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
337 * Returns 0 on success or 1 on error.
340 do_pxe_get(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
342 char *pxefile_addr_str
;
343 unsigned long pxefile_addr_r
;
346 do_getfile
= do_get_tftp
;
349 return CMD_RET_USAGE
;
351 pxefile_addr_str
= from_env("pxefile_addr_r");
353 if (!pxefile_addr_str
)
356 err
= strict_strtoul(pxefile_addr_str
, 16,
357 (unsigned long *)&pxefile_addr_r
);
362 * Keep trying paths until we successfully get a file we're looking
365 if (pxe_uuid_path(cmdtp
, (void *)pxefile_addr_r
) > 0 ||
366 pxe_mac_path(cmdtp
, (void *)pxefile_addr_r
) > 0 ||
367 pxe_ipaddr_paths(cmdtp
, (void *)pxefile_addr_r
) > 0) {
368 printf("Config file found\n");
373 while (pxe_default_paths
[i
]) {
374 if (get_pxelinux_path(cmdtp
, pxe_default_paths
[i
],
375 (void *)pxefile_addr_r
) > 0) {
376 printf("Config file found\n");
382 printf("Config file not found\n");
388 * Wrapper to make it easier to store the file at file_path in the location
389 * specified by envaddr_name. file_path will be joined to the bootfile path,
390 * if any is specified.
392 * Returns 1 on success or < 0 on error.
394 static int get_relfile_envaddr(cmd_tbl_t
*cmdtp
, const char *file_path
, const char *envaddr_name
)
396 unsigned long file_addr
;
399 envaddr
= from_env(envaddr_name
);
404 if (strict_strtoul(envaddr
, 16, &file_addr
) < 0)
407 return get_relfile(cmdtp
, file_path
, (void *)file_addr
);
411 * A note on the pxe file parser.
413 * We're parsing files that use syslinux grammar, which has a few quirks.
414 * String literals must be recognized based on context - there is no
415 * quoting or escaping support. There's also nothing to explicitly indicate
416 * when a label section completes. We deal with that by ending a label
417 * section whenever we see a line that doesn't include.
419 * As with the syslinux family, this same file format could be reused in the
420 * future for non pxe purposes. The only action it takes during parsing that
421 * would throw this off is handling of include files. It assumes we're using
422 * pxe, and does a tftp download of a file listed as an include file in the
423 * middle of the parsing operation. That could be handled by refactoring it to
424 * take a 'include file getter' function.
428 * Describes a single label given in a pxe file.
430 * Create these with the 'label_create' function given below.
432 * name - the name of the menu as given on the 'menu label' line.
433 * kernel - the path to the kernel file to use for this label.
434 * append - kernel command line to use when booting this label
435 * initrd - path to the initrd to use for this label.
436 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
437 * localboot - 1 if this label specified 'localboot', 0 otherwise.
438 * list - lets these form a list, which a pxe_menu struct will hold.
452 struct list_head list
;
456 * Describes a pxe menu as given via pxe files.
458 * title - the name of the menu as given by a 'menu title' line.
459 * default_label - the name of the default label, if any.
460 * timeout - time in tenths of a second to wait for a user key-press before
461 * booting the default label.
462 * prompt - if 0, don't prompt for a choice unless the timeout period is
463 * interrupted. If 1, always prompt for a choice regardless of
465 * labels - a list of labels defined for the menu.
472 struct list_head labels
;
476 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
477 * result must be free()'d to reclaim the memory.
479 * Returns NULL if malloc fails.
481 static struct pxe_label
*label_create(void)
483 struct pxe_label
*label
;
485 label
= malloc(sizeof(struct pxe_label
));
490 memset(label
, 0, sizeof(struct pxe_label
));
496 * Free the memory used by a pxe_label, including that used by its name,
497 * kernel, append and initrd members, if they're non NULL.
499 * So - be sure to only use dynamically allocated memory for the members of
500 * the pxe_label struct, unless you want to clean it up first. These are
501 * currently only created by the pxe file parsing code.
503 static void label_destroy(struct pxe_label
*label
)
524 * Print a label and its string members if they're defined.
526 * This is passed as a callback to the menu code for displaying each
529 static void label_print(void *data
)
531 struct pxe_label
*label
= data
;
532 const char *c
= label
->menu
? label
->menu
: label
->name
;
534 printf("%s:\t%s\n", label
->num
, c
);
538 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
539 * environment variable is defined. Its contents will be executed as U-boot
540 * command. If the label specified an 'append' line, its contents will be
541 * used to overwrite the contents of the 'bootargs' environment variable prior
542 * to running 'localcmd'.
544 * Returns 1 on success or < 0 on error.
546 static int label_localboot(struct pxe_label
*label
)
550 localcmd
= from_env("localcmd");
556 setenv("bootargs", label
->append
);
558 debug("running: %s\n", localcmd
);
560 return run_command_list(localcmd
, strlen(localcmd
), 0);
564 * Boot according to the contents of a pxe_label.
566 * If we can't boot for any reason, we return. A successful boot never
569 * The kernel will be stored in the location given by the 'kernel_addr_r'
570 * environment variable.
572 * If the label specifies an initrd file, it will be stored in the location
573 * given by the 'ramdisk_addr_r' environment variable.
575 * If the label specifies an 'append' line, its contents will overwrite that
576 * of the 'bootargs' environment variable.
578 static int label_boot(cmd_tbl_t
*cmdtp
, struct pxe_label
*label
)
580 char *bootm_argv
[] = { "bootm", NULL
, NULL
, NULL
, NULL
};
582 char mac_str
[29] = "";
583 char ip_str
[68] = "";
590 label
->attempted
= 1;
592 if (label
->localboot
) {
593 if (label
->localboot_val
>= 0)
594 label_localboot(label
);
598 if (label
->kernel
== NULL
) {
599 printf("No kernel given, skipping %s\n",
605 if (get_relfile_envaddr(cmdtp
, label
->initrd
, "ramdisk_addr_r") < 0) {
606 printf("Skipping %s for failure retrieving initrd\n",
611 bootm_argv
[2] = initrd_str
;
612 strcpy(bootm_argv
[2], getenv("ramdisk_addr_r"));
613 strcat(bootm_argv
[2], ":");
614 strcat(bootm_argv
[2], getenv("filesize"));
619 if (get_relfile_envaddr(cmdtp
, label
->kernel
, "kernel_addr_r") < 0) {
620 printf("Skipping %s for failure retrieving kernel\n",
625 if (label
->ipappend
& 0x1) {
626 sprintf(ip_str
, " ip=%s:%s:%s:%s",
627 getenv("ipaddr"), getenv("serverip"),
628 getenv("gatewayip"), getenv("netmask"));
629 len
+= strlen(ip_str
);
632 if (label
->ipappend
& 0x2) {
634 strcpy(mac_str
, " BOOTIF=");
635 err
= format_mac_pxe(mac_str
+ 8, sizeof(mac_str
) - 8);
638 len
+= strlen(mac_str
);
642 len
+= strlen(label
->append
);
645 bootargs
= malloc(len
+ 1);
650 strcpy(bootargs
, label
->append
);
651 strcat(bootargs
, ip_str
);
652 strcat(bootargs
, mac_str
);
654 setenv("bootargs", bootargs
);
655 printf("append: %s\n", bootargs
);
660 bootm_argv
[1] = getenv("kernel_addr_r");
663 * fdt usage is optional:
664 * It handles the following scenarios. All scenarios are exclusive
666 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
667 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
668 * and adjust argc appropriately.
670 * Scenario 2: If there is an fdt_addr specified, pass it along to
671 * bootm, and adjust argc appropriately.
673 * Scenario 3: fdt blob is not available.
675 bootm_argv
[3] = getenv("fdt_addr_r");
677 /* if fdt label is defined then get fdt from server */
678 if (bootm_argv
[3] && label
->fdt
) {
679 if (get_relfile_envaddr(cmdtp
, label
->fdt
, "fdt_addr_r") < 0) {
680 printf("Skipping %s for failure retrieving fdt\n",
685 bootm_argv
[3] = getenv("fdt_addr");
690 do_bootm(cmdtp
, 0, bootm_argc
, bootm_argv
);
692 #ifdef CONFIG_CMD_BOOTZ
693 /* Try booting a zImage if do_bootm returns */
694 do_bootz(cmdtp
, 0, bootm_argc
, bootm_argv
);
700 * Tokens for the pxe file parser.
725 * A token - given by a value and a type.
729 enum token_type type
;
733 * Keywords recognized.
735 static const struct token keywords
[] = {
738 {"timeout", T_TIMEOUT
},
739 {"default", T_DEFAULT
},
740 {"prompt", T_PROMPT
},
742 {"kernel", T_KERNEL
},
744 {"localboot", T_LOCALBOOT
},
745 {"append", T_APPEND
},
746 {"initrd", T_INITRD
},
747 {"include", T_INCLUDE
},
749 {"ontimeout", T_ONTIMEOUT
,},
750 {"ipappend", T_IPAPPEND
,},
755 * Since pxe(linux) files don't have a token to identify the start of a
756 * literal, we have to keep track of when we're in a state where a literal is
757 * expected vs when we're in a state a keyword is expected.
766 * get_string retrieves a string from *p and stores it as a token in
769 * get_string used for scanning both string literals and keywords.
771 * Characters from *p are copied into t-val until a character equal to
772 * delim is found, or a NUL byte is reached. If delim has the special value of
773 * ' ', any whitespace character will be used as a delimiter.
775 * If lower is unequal to 0, uppercase characters will be converted to
776 * lowercase in the result. This is useful to make keywords case
779 * The location of *p is updated to point to the first character after the end
780 * of the token - the ending delimiter.
782 * On success, the new value of t->val is returned. Memory for t->val is
783 * allocated using malloc and must be free()'d to reclaim it. If insufficient
784 * memory is available, NULL is returned.
786 static char *get_string(char **p
, struct token
*t
, char delim
, int lower
)
792 * b and e both start at the beginning of the input stream.
794 * e is incremented until we find the ending delimiter, or a NUL byte
795 * is reached. Then, we take e - b to find the length of the token.
800 if ((delim
== ' ' && isspace(*e
)) || delim
== *e
)
808 * Allocate memory to hold the string, and copy it in, converting
809 * characters to lowercase if lower is != 0.
811 t
->val
= malloc(len
+ 1);
815 for (i
= 0; i
< len
; i
++, b
++) {
817 t
->val
[i
] = tolower(*b
);
825 * Update *p so the caller knows where to continue scanning.
835 * Populate a keyword token with a type and value.
837 static void get_keyword(struct token
*t
)
841 for (i
= 0; keywords
[i
].val
; i
++) {
842 if (!strcmp(t
->val
, keywords
[i
].val
)) {
843 t
->type
= keywords
[i
].type
;
850 * Get the next token. We have to keep track of which state we're in to know
851 * if we're looking to get a string literal or a keyword.
853 * *p is updated to point at the first character after the current token.
855 static void get_token(char **p
, struct token
*t
, enum lex_state state
)
861 /* eat non EOL whitespace */
866 * eat comments. note that string literals can't begin with #, but
867 * can contain a # after their first character.
870 while (*c
&& *c
!= '\n')
877 } else if (*c
== '\0') {
880 } else if (state
== L_SLITERAL
) {
881 get_string(&c
, t
, '\n', 0);
882 } else if (state
== L_KEYWORD
) {
884 * when we expect a keyword, we first get the next string
885 * token delimited by whitespace, and then check if it
886 * matches a keyword in our keyword list. if it does, it's
887 * converted to a keyword token of the appropriate type, and
888 * if not, it remains a string token.
890 get_string(&c
, t
, ' ', 1);
898 * Increment *c until we get to the end of the current line, or EOF.
900 static void eol_or_eof(char **c
)
902 while (**c
&& **c
!= '\n')
907 * All of these parse_* functions share some common behavior.
909 * They finish with *c pointing after the token they parse, and return 1 on
910 * success, or < 0 on error.
914 * Parse a string literal and store a pointer it at *dst. String literals
915 * terminate at the end of the line.
917 static int parse_sliteral(char **c
, char **dst
)
922 get_token(c
, &t
, L_SLITERAL
);
924 if (t
.type
!= T_STRING
) {
925 printf("Expected string literal: %.*s\n", (int)(*c
- s
), s
);
935 * Parse a base 10 (unsigned) integer and store it at *dst.
937 static int parse_integer(char **c
, int *dst
)
942 get_token(c
, &t
, L_SLITERAL
);
944 if (t
.type
!= T_STRING
) {
945 printf("Expected string: %.*s\n", (int)(*c
- s
), s
);
949 *dst
= simple_strtol(t
.val
, NULL
, 10);
956 static int parse_pxefile_top(cmd_tbl_t
*cmdtp
, char *p
, struct pxe_menu
*cfg
, int nest_level
);
959 * Parse an include statement, and retrieve and parse the file it mentions.
961 * base should point to a location where it's safe to store the file, and
962 * nest_level should indicate how many nested includes have occurred. For this
963 * include, nest_level has already been incremented and doesn't need to be
966 static int handle_include(cmd_tbl_t
*cmdtp
, char **c
, char *base
,
967 struct pxe_menu
*cfg
, int nest_level
)
973 err
= parse_sliteral(c
, &include_path
);
976 printf("Expected include path: %.*s\n",
981 err
= get_pxe_file(cmdtp
, include_path
, base
);
984 printf("Couldn't retrieve %s\n", include_path
);
988 return parse_pxefile_top(cmdtp
, base
, cfg
, nest_level
);
992 * Parse lines that begin with 'menu'.
994 * b and nest are provided to handle the 'menu include' case.
996 * b should be the address where the file currently being parsed is stored.
998 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
999 * a file it includes, 3 when parsing a file included by that file, and so on.
1001 static int parse_menu(cmd_tbl_t
*cmdtp
, char **c
, struct pxe_menu
*cfg
, char *b
, int nest_level
)
1007 get_token(c
, &t
, L_KEYWORD
);
1011 err
= parse_sliteral(c
, &cfg
->title
);
1016 err
= handle_include(cmdtp
, c
, b
+ strlen(b
) + 1, cfg
,
1021 printf("Ignoring malformed menu command: %.*s\n",
1034 * Handles parsing a 'menu line' when we're parsing a label.
1036 static int parse_label_menu(char **c
, struct pxe_menu
*cfg
,
1037 struct pxe_label
*label
)
1044 get_token(c
, &t
, L_KEYWORD
);
1048 if (!cfg
->default_label
)
1049 cfg
->default_label
= strdup(label
->name
);
1051 if (!cfg
->default_label
)
1056 parse_sliteral(c
, &label
->menu
);
1059 printf("Ignoring malformed menu command: %.*s\n",
1069 * Parses a label and adds it to the list of labels for a menu.
1071 * A label ends when we either get to the end of a file, or
1072 * get some input we otherwise don't have a handler defined
1076 static int parse_label(char **c
, struct pxe_menu
*cfg
)
1081 struct pxe_label
*label
;
1084 label
= label_create();
1088 err
= parse_sliteral(c
, &label
->name
);
1090 printf("Expected label name: %.*s\n", (int)(*c
- s
), s
);
1091 label_destroy(label
);
1095 list_add_tail(&label
->list
, &cfg
->labels
);
1099 get_token(c
, &t
, L_KEYWORD
);
1104 err
= parse_label_menu(c
, cfg
, label
);
1109 err
= parse_sliteral(c
, &label
->kernel
);
1113 err
= parse_sliteral(c
, &label
->append
);
1116 s
= strstr(label
->append
, "initrd=");
1120 len
= (int)(strchr(s
, ' ') - s
);
1121 label
->initrd
= malloc(len
+ 1);
1122 strncpy(label
->initrd
, s
, len
);
1123 label
->initrd
[len
] = '\0';
1129 err
= parse_sliteral(c
, &label
->initrd
);
1134 err
= parse_sliteral(c
, &label
->fdt
);
1138 label
->localboot
= 1;
1139 err
= parse_integer(c
, &label
->localboot_val
);
1143 err
= parse_integer(c
, &label
->ipappend
);
1151 * put the token back! we don't want it - it's the end
1152 * of a label and whatever token this is, it's
1153 * something for the menu level context to handle.
1165 * This 16 comes from the limit pxelinux imposes on nested includes.
1167 * There is no reason at all we couldn't do more, but some limit helps prevent
1168 * infinite (until crash occurs) recursion if a file tries to include itself.
1170 #define MAX_NEST_LEVEL 16
1173 * Entry point for parsing a menu file. nest_level indicates how many times
1174 * we've nested in includes. It will be 1 for the top level menu file.
1176 * Returns 1 on success, < 0 on error.
1178 static int parse_pxefile_top(cmd_tbl_t
*cmdtp
, char *p
, struct pxe_menu
*cfg
, int nest_level
)
1181 char *s
, *b
, *label_name
;
1186 if (nest_level
> MAX_NEST_LEVEL
) {
1187 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL
);
1194 get_token(&p
, &t
, L_KEYWORD
);
1200 err
= parse_menu(cmdtp
, &p
, cfg
, b
, nest_level
);
1204 err
= parse_integer(&p
, &cfg
->timeout
);
1208 err
= parse_label(&p
, cfg
);
1213 err
= parse_sliteral(&p
, &label_name
);
1216 if (cfg
->default_label
)
1217 free(cfg
->default_label
);
1219 cfg
->default_label
= label_name
;
1225 err
= handle_include(cmdtp
, &p
, b
+ ALIGN(strlen(b
), 4), cfg
,
1240 printf("Ignoring unknown command: %.*s\n",
1251 * Free the memory used by a pxe_menu and its labels.
1253 static void destroy_pxe_menu(struct pxe_menu
*cfg
)
1255 struct list_head
*pos
, *n
;
1256 struct pxe_label
*label
;
1261 if (cfg
->default_label
)
1262 free(cfg
->default_label
);
1264 list_for_each_safe(pos
, n
, &cfg
->labels
) {
1265 label
= list_entry(pos
, struct pxe_label
, list
);
1267 label_destroy(label
);
1274 * Entry point for parsing a pxe file. This is only used for the top level
1277 * Returns NULL if there is an error, otherwise, returns a pointer to a
1278 * pxe_menu struct populated with the results of parsing the pxe file (and any
1279 * files it includes). The resulting pxe_menu struct can be free()'d by using
1280 * the destroy_pxe_menu() function.
1282 static struct pxe_menu
*parse_pxefile(cmd_tbl_t
*cmdtp
, char *menucfg
)
1284 struct pxe_menu
*cfg
;
1286 cfg
= malloc(sizeof(struct pxe_menu
));
1291 memset(cfg
, 0, sizeof(struct pxe_menu
));
1293 INIT_LIST_HEAD(&cfg
->labels
);
1295 if (parse_pxefile_top(cmdtp
, menucfg
, cfg
, 1) < 0) {
1296 destroy_pxe_menu(cfg
);
1304 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1307 static struct menu
*pxe_menu_to_menu(struct pxe_menu
*cfg
)
1309 struct pxe_label
*label
;
1310 struct list_head
*pos
;
1314 char *default_num
= NULL
;
1317 * Create a menu and add items for all the labels.
1319 m
= menu_create(cfg
->title
, cfg
->timeout
, cfg
->prompt
, label_print
,
1325 list_for_each(pos
, &cfg
->labels
) {
1326 label
= list_entry(pos
, struct pxe_label
, list
);
1328 sprintf(label
->num
, "%d", i
++);
1329 if (menu_item_add(m
, label
->num
, label
) != 1) {
1333 if (cfg
->default_label
&&
1334 (strcmp(label
->name
, cfg
->default_label
) == 0))
1335 default_num
= label
->num
;
1340 * After we've created items for each label in the menu, set the
1341 * menu's default label if one was specified.
1344 err
= menu_default_set(m
, default_num
);
1346 if (err
!= -ENOENT
) {
1351 printf("Missing default: %s\n", cfg
->default_label
);
1359 * Try to boot any labels we have yet to attempt to boot.
1361 static void boot_unattempted_labels(cmd_tbl_t
*cmdtp
, struct pxe_menu
*cfg
)
1363 struct list_head
*pos
;
1364 struct pxe_label
*label
;
1366 list_for_each(pos
, &cfg
->labels
) {
1367 label
= list_entry(pos
, struct pxe_label
, list
);
1369 if (!label
->attempted
)
1370 label_boot(cmdtp
, label
);
1375 * Boot the system as prescribed by a pxe_menu.
1377 * Use the menu system to either get the user's choice or the default, based
1378 * on config or user input. If there is no default or user's choice,
1379 * attempted to boot labels in the order they were given in pxe files.
1380 * If the default or user's choice fails to boot, attempt to boot other
1381 * labels in the order they were given in pxe files.
1383 * If this function returns, there weren't any labels that successfully
1384 * booted, or the user interrupted the menu selection via ctrl+c.
1386 static void handle_pxe_menu(cmd_tbl_t
*cmdtp
, struct pxe_menu
*cfg
)
1392 m
= pxe_menu_to_menu(cfg
);
1396 err
= menu_get_choice(m
, &choice
);
1401 * err == 1 means we got a choice back from menu_get_choice.
1403 * err == -ENOENT if the menu was setup to select the default but no
1404 * default was set. in that case, we should continue trying to boot
1405 * labels that haven't been attempted yet.
1407 * otherwise, the user interrupted or there was some other error and
1412 err
= label_boot(cmdtp
, choice
);
1415 } else if (err
!= -ENOENT
) {
1419 boot_unattempted_labels(cmdtp
, cfg
);
1423 * Boots a system using a pxe file
1425 * Returns 0 on success, 1 on error.
1428 do_pxe_boot(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
1430 unsigned long pxefile_addr_r
;
1431 struct pxe_menu
*cfg
;
1432 char *pxefile_addr_str
;
1434 do_getfile
= do_get_tftp
;
1437 pxefile_addr_str
= from_env("pxefile_addr_r");
1438 if (!pxefile_addr_str
)
1441 } else if (argc
== 2) {
1442 pxefile_addr_str
= argv
[1];
1444 return CMD_RET_USAGE
;
1447 if (strict_strtoul(pxefile_addr_str
, 16, &pxefile_addr_r
) < 0) {
1448 printf("Invalid pxefile address: %s\n", pxefile_addr_str
);
1452 cfg
= parse_pxefile(cmdtp
, (char *)(pxefile_addr_r
));
1455 printf("Error parsing config file\n");
1459 handle_pxe_menu(cmdtp
, cfg
);
1461 destroy_pxe_menu(cfg
);
1466 static cmd_tbl_t cmd_pxe_sub
[] = {
1467 U_BOOT_CMD_MKENT(get
, 1, 1, do_pxe_get
, "", ""),
1468 U_BOOT_CMD_MKENT(boot
, 2, 1, do_pxe_boot
, "", "")
1471 int do_pxe(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
1476 return CMD_RET_USAGE
;
1480 /* drop initial "pxe" arg */
1484 cp
= find_cmd_tbl(argv
[0], cmd_pxe_sub
, ARRAY_SIZE(cmd_pxe_sub
));
1487 return cp
->cmd(cmdtp
, flag
, argc
, argv
);
1489 return CMD_RET_USAGE
;
1494 "commands to get and boot from pxe files",
1495 "get - try to retrieve a pxe file using tftp\npxe "
1496 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1500 * Boots a system using a local disk syslinux/extlinux file
1502 * Returns 0 on success, 1 on error.
1504 int do_sysboot(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
1506 unsigned long pxefile_addr_r
;
1507 struct pxe_menu
*cfg
;
1508 char *pxefile_addr_str
;
1514 if (strstr(argv
[1], "-p")) {
1521 return cmd_usage(cmdtp
);
1524 pxefile_addr_str
= from_env("pxefile_addr_r");
1525 if (!pxefile_addr_str
)
1528 pxefile_addr_str
= argv
[4];
1532 filename
= getenv("bootfile");
1535 setenv("bootfile", filename
);
1538 if (strstr(argv
[3], "ext2"))
1539 do_getfile
= do_get_ext2
;
1540 else if (strstr(argv
[3], "fat"))
1541 do_getfile
= do_get_fat
;
1543 printf("Invalid filesystem: %s\n", argv
[3]);
1546 fs_argv
[1] = argv
[1];
1547 fs_argv
[2] = argv
[2];
1549 if (strict_strtoul(pxefile_addr_str
, 16, &pxefile_addr_r
) < 0) {
1550 printf("Invalid pxefile address: %s\n", pxefile_addr_str
);
1554 if (get_pxe_file(cmdtp
, filename
, (void *)pxefile_addr_r
) < 0) {
1555 printf("Error reading config file\n");
1559 cfg
= parse_pxefile(cmdtp
, (char *)(pxefile_addr_r
));
1562 printf("Error parsing config file\n");
1569 handle_pxe_menu(cmdtp
, cfg
);
1571 destroy_pxe_menu(cfg
);
1577 sysboot
, 7, 1, do_sysboot
,
1578 "command to get and boot from syslinux files",
1579 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1580 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1581 " filesystem on 'dev' on 'interface' to address 'addr'"