]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_pxe.c
2 * Copyright 2010-2011 Calxeda, Inc.
3 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <linux/string.h>
12 #include <linux/ctype.h>
14 #include <linux/list.h>
20 #define MAX_TFTP_PATH_LEN 127
22 const char *pxe_default_paths
[] = {
24 "default-" CONFIG_SYS_ARCH
"-" CONFIG_SYS_SOC
,
26 "default-" CONFIG_SYS_ARCH
,
34 * Like getenv, but prints an error if envvar isn't defined in the
35 * environment. It always returns what getenv does, so it can be used in
36 * place of getenv without changing error handling otherwise.
38 static char *from_env(const char *envvar
)
45 printf("missing environment variable: %s\n", envvar
);
52 * Convert an ethaddr from the environment to the format used by pxelinux
53 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
54 * the beginning of the ethernet address to indicate a hardware type of
55 * Ethernet. Also converts uppercase hex characters into lowercase, to match
56 * pxelinux's behavior.
58 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
59 * environment, or some other value < 0 on error.
61 static int format_mac_pxe(char *outbuf
, size_t outbuf_len
)
65 if (outbuf_len
< 21) {
66 printf("outbuf is too small (%zd < 21)\n", outbuf_len
);
71 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
75 sprintf(outbuf
, "01-%02x-%02x-%02x-%02x-%02x-%02x",
76 ethaddr
[0], ethaddr
[1], ethaddr
[2],
77 ethaddr
[3], ethaddr
[4], ethaddr
[5]);
84 * Returns the directory the file specified in the bootfile env variable is
85 * in. If bootfile isn't defined in the environment, return NULL, which should
86 * be interpreted as "don't prepend anything to paths".
88 static int get_bootfile_path(const char *file_path
, char *bootfile_path
,
89 size_t bootfile_path_size
)
91 char *bootfile
, *last_slash
;
94 /* Only syslinux allows absolute paths */
95 if (file_path
[0] == '/' && !is_pxe
)
98 bootfile
= from_env("bootfile");
103 last_slash
= strrchr(bootfile
, '/');
105 if (last_slash
== NULL
)
108 path_len
= (last_slash
- bootfile
) + 1;
110 if (bootfile_path_size
< path_len
) {
111 printf("bootfile_path too small. (%zd < %zd)\n",
112 bootfile_path_size
, path_len
);
117 strncpy(bootfile_path
, bootfile
, path_len
);
120 bootfile_path
[path_len
] = '\0';
125 static int (*do_getfile
)(cmd_tbl_t
*cmdtp
, const char *file_path
, char *file_addr
);
127 #ifdef CONFIG_CMD_NET
128 static int do_get_tftp(cmd_tbl_t
*cmdtp
, const char *file_path
, char *file_addr
)
130 char *tftp_argv
[] = {"tftp", NULL
, NULL
, NULL
};
132 tftp_argv
[1] = file_addr
;
133 tftp_argv
[2] = (void *)file_path
;
135 if (do_tftpb(cmdtp
, 0, 3, tftp_argv
))
142 static char *fs_argv
[5];
144 static int do_get_ext2(cmd_tbl_t
*cmdtp
, const char *file_path
, char *file_addr
)
146 #ifdef CONFIG_CMD_EXT2
147 fs_argv
[0] = "ext2load";
148 fs_argv
[3] = file_addr
;
149 fs_argv
[4] = (void *)file_path
;
151 if (!do_ext2load(cmdtp
, 0, 5, fs_argv
))
157 static int do_get_fat(cmd_tbl_t
*cmdtp
, const char *file_path
, char *file_addr
)
159 #ifdef CONFIG_CMD_FAT
160 fs_argv
[0] = "fatload";
161 fs_argv
[3] = file_addr
;
162 fs_argv
[4] = (void *)file_path
;
164 if (!do_fat_fsload(cmdtp
, 0, 5, fs_argv
))
170 static int do_get_any(cmd_tbl_t
*cmdtp
, const char *file_path
, char *file_addr
)
172 #ifdef CONFIG_CMD_FS_GENERIC
174 fs_argv
[3] = file_addr
;
175 fs_argv
[4] = (void *)file_path
;
177 if (!do_load(cmdtp
, 0, 5, fs_argv
, FS_TYPE_ANY
))
184 * As in pxelinux, paths to files referenced from files we retrieve are
185 * relative to the location of bootfile. get_relfile takes such a path and
186 * joins it with the bootfile path to get the full path to the target file. If
187 * the bootfile path is NULL, we use file_path as is.
189 * Returns 1 for success, or < 0 on error.
191 static int get_relfile(cmd_tbl_t
*cmdtp
, const char *file_path
, void *file_addr
)
194 char relfile
[MAX_TFTP_PATH_LEN
+1];
198 err
= get_bootfile_path(file_path
, relfile
, sizeof(relfile
));
203 path_len
= strlen(file_path
);
204 path_len
+= strlen(relfile
);
206 if (path_len
> MAX_TFTP_PATH_LEN
) {
207 printf("Base path too long (%s%s)\n",
211 return -ENAMETOOLONG
;
214 strcat(relfile
, file_path
);
216 printf("Retrieving file: %s\n", relfile
);
218 sprintf(addr_buf
, "%p", file_addr
);
220 return do_getfile(cmdtp
, relfile
, addr_buf
);
224 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
225 * 'bootfile' was specified in the environment, the path to bootfile will be
226 * prepended to 'file_path' and the resulting path will be used.
228 * Returns 1 on success, or < 0 for error.
230 static int get_pxe_file(cmd_tbl_t
*cmdtp
, const char *file_path
, void *file_addr
)
232 unsigned long config_file_size
;
236 err
= get_relfile(cmdtp
, file_path
, file_addr
);
242 * the file comes without a NUL byte at the end, so find out its size
243 * and add the NUL byte.
245 tftp_filesize
= from_env("filesize");
250 if (strict_strtoul(tftp_filesize
, 16, &config_file_size
) < 0)
253 *(char *)(file_addr
+ config_file_size
) = '\0';
258 #ifdef CONFIG_CMD_NET
260 #define PXELINUX_DIR "pxelinux.cfg/"
263 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
264 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
265 * from the bootfile path, as described above.
267 * Returns 1 on success or < 0 on error.
269 static int get_pxelinux_path(cmd_tbl_t
*cmdtp
, const char *file
, void *pxefile_addr_r
)
271 size_t base_len
= strlen(PXELINUX_DIR
);
272 char path
[MAX_TFTP_PATH_LEN
+1];
274 if (base_len
+ strlen(file
) > MAX_TFTP_PATH_LEN
) {
275 printf("path (%s%s) too long, skipping\n",
277 return -ENAMETOOLONG
;
280 sprintf(path
, PXELINUX_DIR
"%s", file
);
282 return get_pxe_file(cmdtp
, path
, pxefile_addr_r
);
286 * Looks for a pxe file with a name based on the pxeuuid environment variable.
288 * Returns 1 on success or < 0 on error.
290 static int pxe_uuid_path(cmd_tbl_t
*cmdtp
, void *pxefile_addr_r
)
294 uuid_str
= from_env("pxeuuid");
299 return get_pxelinux_path(cmdtp
, uuid_str
, pxefile_addr_r
);
303 * Looks for a pxe file with a name based on the 'ethaddr' environment
306 * Returns 1 on success or < 0 on error.
308 static int pxe_mac_path(cmd_tbl_t
*cmdtp
, void *pxefile_addr_r
)
313 err
= format_mac_pxe(mac_str
, sizeof(mac_str
));
318 return get_pxelinux_path(cmdtp
, mac_str
, pxefile_addr_r
);
322 * Looks for pxe files with names based on our IP address. See pxelinux
323 * documentation for details on what these file names look like. We match
326 * Returns 1 on success or < 0 on error.
328 static int pxe_ipaddr_paths(cmd_tbl_t
*cmdtp
, void *pxefile_addr_r
)
333 sprintf(ip_addr
, "%08X", ntohl(NetOurIP
));
335 for (mask_pos
= 7; mask_pos
>= 0; mask_pos
--) {
336 err
= get_pxelinux_path(cmdtp
, ip_addr
, pxefile_addr_r
);
341 ip_addr
[mask_pos
] = '\0';
348 * Entry point for the 'pxe get' command.
349 * This Follows pxelinux's rules to download a config file from a tftp server.
350 * The file is stored at the location given by the pxefile_addr_r environment
351 * variable, which must be set.
353 * UUID comes from pxeuuid env variable, if defined
354 * MAC addr comes from ethaddr env variable, if defined
357 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
359 * Returns 0 on success or 1 on error.
362 do_pxe_get(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
364 char *pxefile_addr_str
;
365 unsigned long pxefile_addr_r
;
368 do_getfile
= do_get_tftp
;
371 return CMD_RET_USAGE
;
373 pxefile_addr_str
= from_env("pxefile_addr_r");
375 if (!pxefile_addr_str
)
378 err
= strict_strtoul(pxefile_addr_str
, 16,
379 (unsigned long *)&pxefile_addr_r
);
384 * Keep trying paths until we successfully get a file we're looking
387 if (pxe_uuid_path(cmdtp
, (void *)pxefile_addr_r
) > 0 ||
388 pxe_mac_path(cmdtp
, (void *)pxefile_addr_r
) > 0 ||
389 pxe_ipaddr_paths(cmdtp
, (void *)pxefile_addr_r
) > 0) {
390 printf("Config file found\n");
395 while (pxe_default_paths
[i
]) {
396 if (get_pxelinux_path(cmdtp
, pxe_default_paths
[i
],
397 (void *)pxefile_addr_r
) > 0) {
398 printf("Config file found\n");
404 printf("Config file not found\n");
411 * Wrapper to make it easier to store the file at file_path in the location
412 * specified by envaddr_name. file_path will be joined to the bootfile path,
413 * if any is specified.
415 * Returns 1 on success or < 0 on error.
417 static int get_relfile_envaddr(cmd_tbl_t
*cmdtp
, const char *file_path
, const char *envaddr_name
)
419 unsigned long file_addr
;
422 envaddr
= from_env(envaddr_name
);
427 if (strict_strtoul(envaddr
, 16, &file_addr
) < 0)
430 return get_relfile(cmdtp
, file_path
, (void *)file_addr
);
434 * A note on the pxe file parser.
436 * We're parsing files that use syslinux grammar, which has a few quirks.
437 * String literals must be recognized based on context - there is no
438 * quoting or escaping support. There's also nothing to explicitly indicate
439 * when a label section completes. We deal with that by ending a label
440 * section whenever we see a line that doesn't include.
442 * As with the syslinux family, this same file format could be reused in the
443 * future for non pxe purposes. The only action it takes during parsing that
444 * would throw this off is handling of include files. It assumes we're using
445 * pxe, and does a tftp download of a file listed as an include file in the
446 * middle of the parsing operation. That could be handled by refactoring it to
447 * take a 'include file getter' function.
451 * Describes a single label given in a pxe file.
453 * Create these with the 'label_create' function given below.
455 * name - the name of the menu as given on the 'menu label' line.
456 * kernel - the path to the kernel file to use for this label.
457 * append - kernel command line to use when booting this label
458 * initrd - path to the initrd to use for this label.
459 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
460 * localboot - 1 if this label specified 'localboot', 0 otherwise.
461 * list - lets these form a list, which a pxe_menu struct will hold.
476 struct list_head list
;
480 * Describes a pxe menu as given via pxe files.
482 * title - the name of the menu as given by a 'menu title' line.
483 * default_label - the name of the default label, if any.
484 * timeout - time in tenths of a second to wait for a user key-press before
485 * booting the default label.
486 * prompt - if 0, don't prompt for a choice unless the timeout period is
487 * interrupted. If 1, always prompt for a choice regardless of
489 * labels - a list of labels defined for the menu.
496 struct list_head labels
;
500 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
501 * result must be free()'d to reclaim the memory.
503 * Returns NULL if malloc fails.
505 static struct pxe_label
*label_create(void)
507 struct pxe_label
*label
;
509 label
= malloc(sizeof(struct pxe_label
));
514 memset(label
, 0, sizeof(struct pxe_label
));
520 * Free the memory used by a pxe_label, including that used by its name,
521 * kernel, append and initrd members, if they're non NULL.
523 * So - be sure to only use dynamically allocated memory for the members of
524 * the pxe_label struct, unless you want to clean it up first. These are
525 * currently only created by the pxe file parsing code.
527 static void label_destroy(struct pxe_label
*label
)
551 * Print a label and its string members if they're defined.
553 * This is passed as a callback to the menu code for displaying each
556 static void label_print(void *data
)
558 struct pxe_label
*label
= data
;
559 const char *c
= label
->menu
? label
->menu
: label
->name
;
561 printf("%s:\t%s\n", label
->num
, c
);
565 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
566 * environment variable is defined. Its contents will be executed as U-boot
567 * command. If the label specified an 'append' line, its contents will be
568 * used to overwrite the contents of the 'bootargs' environment variable prior
569 * to running 'localcmd'.
571 * Returns 1 on success or < 0 on error.
573 static int label_localboot(struct pxe_label
*label
)
577 localcmd
= from_env("localcmd");
583 char bootargs
[CONFIG_SYS_CBSIZE
];
585 cli_simple_process_macros(label
->append
, bootargs
);
586 setenv("bootargs", bootargs
);
589 debug("running: %s\n", localcmd
);
591 return run_command_list(localcmd
, strlen(localcmd
), 0);
595 * Boot according to the contents of a pxe_label.
597 * If we can't boot for any reason, we return. A successful boot never
600 * The kernel will be stored in the location given by the 'kernel_addr_r'
601 * environment variable.
603 * If the label specifies an initrd file, it will be stored in the location
604 * given by the 'ramdisk_addr_r' environment variable.
606 * If the label specifies an 'append' line, its contents will overwrite that
607 * of the 'bootargs' environment variable.
609 static int label_boot(cmd_tbl_t
*cmdtp
, struct pxe_label
*label
)
611 char *bootm_argv
[] = { "bootm", NULL
, NULL
, NULL
, NULL
};
613 char mac_str
[29] = "";
614 char ip_str
[68] = "";
622 label
->attempted
= 1;
624 if (label
->localboot
) {
625 if (label
->localboot_val
>= 0)
626 label_localboot(label
);
630 if (label
->kernel
== NULL
) {
631 printf("No kernel given, skipping %s\n",
637 if (get_relfile_envaddr(cmdtp
, label
->initrd
, "ramdisk_addr_r") < 0) {
638 printf("Skipping %s for failure retrieving initrd\n",
643 bootm_argv
[2] = initrd_str
;
644 strcpy(bootm_argv
[2], getenv("ramdisk_addr_r"));
645 strcat(bootm_argv
[2], ":");
646 strcat(bootm_argv
[2], getenv("filesize"));
651 if (get_relfile_envaddr(cmdtp
, label
->kernel
, "kernel_addr_r") < 0) {
652 printf("Skipping %s for failure retrieving kernel\n",
657 if (label
->ipappend
& 0x1) {
658 sprintf(ip_str
, " ip=%s:%s:%s:%s",
659 getenv("ipaddr"), getenv("serverip"),
660 getenv("gatewayip"), getenv("netmask"));
663 #ifdef CONFIG_CMD_NET
664 if (label
->ipappend
& 0x2) {
666 strcpy(mac_str
, " BOOTIF=");
667 err
= format_mac_pxe(mac_str
+ 8, sizeof(mac_str
) - 8);
673 if ((label
->ipappend
& 0x3) || label
->append
) {
674 char bootargs
[CONFIG_SYS_CBSIZE
] = "";
675 char finalbootargs
[CONFIG_SYS_CBSIZE
];
677 if (strlen(label
->append
?: "") +
678 strlen(ip_str
) + strlen(mac_str
) + 1 > sizeof(bootargs
)) {
679 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
680 strlen(label
->append
?: ""),
681 strlen(ip_str
), strlen(mac_str
),
687 strcpy(bootargs
, label
->append
);
688 strcat(bootargs
, ip_str
);
689 strcat(bootargs
, mac_str
);
691 cli_simple_process_macros(bootargs
, finalbootargs
);
692 setenv("bootargs", finalbootargs
);
693 printf("append: %s\n", finalbootargs
);
696 bootm_argv
[1] = getenv("kernel_addr_r");
699 * fdt usage is optional:
700 * It handles the following scenarios. All scenarios are exclusive
702 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
703 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
704 * and adjust argc appropriately.
706 * Scenario 2: If there is an fdt_addr specified, pass it along to
707 * bootm, and adjust argc appropriately.
709 * Scenario 3: fdt blob is not available.
711 bootm_argv
[3] = getenv("fdt_addr_r");
713 /* if fdt label is defined then get fdt from server */
715 char *fdtfile
= NULL
;
716 char *fdtfilefree
= NULL
;
719 fdtfile
= label
->fdt
;
720 } else if (label
->fdtdir
) {
721 char *f1
, *f2
, *f3
, *f4
, *slash
;
723 f1
= getenv("fdtfile");
730 * For complex cases where this code doesn't
731 * generate the correct filename, the board
732 * code should set $fdtfile during early boot,
733 * or the boot scripts should set $fdtfile
734 * before invoking "pxe" or "sysboot".
738 f3
= getenv("board");
742 len
= strlen(label
->fdtdir
);
745 else if (label
->fdtdir
[len
- 1] != '/')
750 len
= strlen(label
->fdtdir
) + strlen(slash
) +
751 strlen(f1
) + strlen(f2
) + strlen(f3
) +
753 fdtfilefree
= malloc(len
);
755 printf("malloc fail (FDT filename)\n");
759 snprintf(fdtfilefree
, len
, "%s%s%s%s%s%s",
760 label
->fdtdir
, slash
, f1
, f2
, f3
, f4
);
761 fdtfile
= fdtfilefree
;
765 int err
= get_relfile_envaddr(cmdtp
, fdtfile
, "fdt_addr_r");
768 printf("Skipping %s for failure retrieving fdt\n",
773 bootm_argv
[3] = NULL
;
778 bootm_argv
[3] = getenv("fdt_addr");
783 kernel_addr
= genimg_get_kernel_addr(bootm_argv
[1]);
784 buf
= map_sysmem(kernel_addr
, 0);
785 /* Try bootm for legacy and FIT format image */
786 if (genimg_get_format(buf
) != IMAGE_FORMAT_INVALID
)
787 do_bootm(cmdtp
, 0, bootm_argc
, bootm_argv
);
788 #ifdef CONFIG_CMD_BOOTZ
789 /* Try booting a zImage */
791 do_bootz(cmdtp
, 0, bootm_argc
, bootm_argv
);
797 * Tokens for the pxe file parser.
823 * A token - given by a value and a type.
827 enum token_type type
;
831 * Keywords recognized.
833 static const struct token keywords
[] = {
836 {"timeout", T_TIMEOUT
},
837 {"default", T_DEFAULT
},
838 {"prompt", T_PROMPT
},
840 {"kernel", T_KERNEL
},
842 {"localboot", T_LOCALBOOT
},
843 {"append", T_APPEND
},
844 {"initrd", T_INITRD
},
845 {"include", T_INCLUDE
},
846 {"devicetree", T_FDT
},
848 {"devicetreedir", T_FDTDIR
},
849 {"fdtdir", T_FDTDIR
},
850 {"ontimeout", T_ONTIMEOUT
,},
851 {"ipappend", T_IPAPPEND
,},
856 * Since pxe(linux) files don't have a token to identify the start of a
857 * literal, we have to keep track of when we're in a state where a literal is
858 * expected vs when we're in a state a keyword is expected.
867 * get_string retrieves a string from *p and stores it as a token in
870 * get_string used for scanning both string literals and keywords.
872 * Characters from *p are copied into t-val until a character equal to
873 * delim is found, or a NUL byte is reached. If delim has the special value of
874 * ' ', any whitespace character will be used as a delimiter.
876 * If lower is unequal to 0, uppercase characters will be converted to
877 * lowercase in the result. This is useful to make keywords case
880 * The location of *p is updated to point to the first character after the end
881 * of the token - the ending delimiter.
883 * On success, the new value of t->val is returned. Memory for t->val is
884 * allocated using malloc and must be free()'d to reclaim it. If insufficient
885 * memory is available, NULL is returned.
887 static char *get_string(char **p
, struct token
*t
, char delim
, int lower
)
893 * b and e both start at the beginning of the input stream.
895 * e is incremented until we find the ending delimiter, or a NUL byte
896 * is reached. Then, we take e - b to find the length of the token.
901 if ((delim
== ' ' && isspace(*e
)) || delim
== *e
)
909 * Allocate memory to hold the string, and copy it in, converting
910 * characters to lowercase if lower is != 0.
912 t
->val
= malloc(len
+ 1);
916 for (i
= 0; i
< len
; i
++, b
++) {
918 t
->val
[i
] = tolower(*b
);
926 * Update *p so the caller knows where to continue scanning.
936 * Populate a keyword token with a type and value.
938 static void get_keyword(struct token
*t
)
942 for (i
= 0; keywords
[i
].val
; i
++) {
943 if (!strcmp(t
->val
, keywords
[i
].val
)) {
944 t
->type
= keywords
[i
].type
;
951 * Get the next token. We have to keep track of which state we're in to know
952 * if we're looking to get a string literal or a keyword.
954 * *p is updated to point at the first character after the current token.
956 static void get_token(char **p
, struct token
*t
, enum lex_state state
)
962 /* eat non EOL whitespace */
967 * eat comments. note that string literals can't begin with #, but
968 * can contain a # after their first character.
971 while (*c
&& *c
!= '\n')
978 } else if (*c
== '\0') {
981 } else if (state
== L_SLITERAL
) {
982 get_string(&c
, t
, '\n', 0);
983 } else if (state
== L_KEYWORD
) {
985 * when we expect a keyword, we first get the next string
986 * token delimited by whitespace, and then check if it
987 * matches a keyword in our keyword list. if it does, it's
988 * converted to a keyword token of the appropriate type, and
989 * if not, it remains a string token.
991 get_string(&c
, t
, ' ', 1);
999 * Increment *c until we get to the end of the current line, or EOF.
1001 static void eol_or_eof(char **c
)
1003 while (**c
&& **c
!= '\n')
1008 * All of these parse_* functions share some common behavior.
1010 * They finish with *c pointing after the token they parse, and return 1 on
1011 * success, or < 0 on error.
1015 * Parse a string literal and store a pointer it at *dst. String literals
1016 * terminate at the end of the line.
1018 static int parse_sliteral(char **c
, char **dst
)
1023 get_token(c
, &t
, L_SLITERAL
);
1025 if (t
.type
!= T_STRING
) {
1026 printf("Expected string literal: %.*s\n", (int)(*c
- s
), s
);
1036 * Parse a base 10 (unsigned) integer and store it at *dst.
1038 static int parse_integer(char **c
, int *dst
)
1043 get_token(c
, &t
, L_SLITERAL
);
1045 if (t
.type
!= T_STRING
) {
1046 printf("Expected string: %.*s\n", (int)(*c
- s
), s
);
1050 *dst
= simple_strtol(t
.val
, NULL
, 10);
1057 static int parse_pxefile_top(cmd_tbl_t
*cmdtp
, char *p
, struct pxe_menu
*cfg
, int nest_level
);
1060 * Parse an include statement, and retrieve and parse the file it mentions.
1062 * base should point to a location where it's safe to store the file, and
1063 * nest_level should indicate how many nested includes have occurred. For this
1064 * include, nest_level has already been incremented and doesn't need to be
1067 static int handle_include(cmd_tbl_t
*cmdtp
, char **c
, char *base
,
1068 struct pxe_menu
*cfg
, int nest_level
)
1074 err
= parse_sliteral(c
, &include_path
);
1077 printf("Expected include path: %.*s\n",
1082 err
= get_pxe_file(cmdtp
, include_path
, base
);
1085 printf("Couldn't retrieve %s\n", include_path
);
1089 return parse_pxefile_top(cmdtp
, base
, cfg
, nest_level
);
1093 * Parse lines that begin with 'menu'.
1095 * b and nest are provided to handle the 'menu include' case.
1097 * b should be the address where the file currently being parsed is stored.
1099 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1100 * a file it includes, 3 when parsing a file included by that file, and so on.
1102 static int parse_menu(cmd_tbl_t
*cmdtp
, char **c
, struct pxe_menu
*cfg
, char *b
, int nest_level
)
1108 get_token(c
, &t
, L_KEYWORD
);
1112 err
= parse_sliteral(c
, &cfg
->title
);
1117 err
= handle_include(cmdtp
, c
, b
+ strlen(b
) + 1, cfg
,
1122 printf("Ignoring malformed menu command: %.*s\n",
1135 * Handles parsing a 'menu line' when we're parsing a label.
1137 static int parse_label_menu(char **c
, struct pxe_menu
*cfg
,
1138 struct pxe_label
*label
)
1145 get_token(c
, &t
, L_KEYWORD
);
1149 if (!cfg
->default_label
)
1150 cfg
->default_label
= strdup(label
->name
);
1152 if (!cfg
->default_label
)
1157 parse_sliteral(c
, &label
->menu
);
1160 printf("Ignoring malformed menu command: %.*s\n",
1170 * Parses a label and adds it to the list of labels for a menu.
1172 * A label ends when we either get to the end of a file, or
1173 * get some input we otherwise don't have a handler defined
1177 static int parse_label(char **c
, struct pxe_menu
*cfg
)
1182 struct pxe_label
*label
;
1185 label
= label_create();
1189 err
= parse_sliteral(c
, &label
->name
);
1191 printf("Expected label name: %.*s\n", (int)(*c
- s
), s
);
1192 label_destroy(label
);
1196 list_add_tail(&label
->list
, &cfg
->labels
);
1200 get_token(c
, &t
, L_KEYWORD
);
1205 err
= parse_label_menu(c
, cfg
, label
);
1210 err
= parse_sliteral(c
, &label
->kernel
);
1214 err
= parse_sliteral(c
, &label
->append
);
1217 s
= strstr(label
->append
, "initrd=");
1221 len
= (int)(strchr(s
, ' ') - s
);
1222 label
->initrd
= malloc(len
+ 1);
1223 strncpy(label
->initrd
, s
, len
);
1224 label
->initrd
[len
] = '\0';
1230 err
= parse_sliteral(c
, &label
->initrd
);
1235 err
= parse_sliteral(c
, &label
->fdt
);
1240 err
= parse_sliteral(c
, &label
->fdtdir
);
1244 label
->localboot
= 1;
1245 err
= parse_integer(c
, &label
->localboot_val
);
1249 err
= parse_integer(c
, &label
->ipappend
);
1257 * put the token back! we don't want it - it's the end
1258 * of a label and whatever token this is, it's
1259 * something for the menu level context to handle.
1271 * This 16 comes from the limit pxelinux imposes on nested includes.
1273 * There is no reason at all we couldn't do more, but some limit helps prevent
1274 * infinite (until crash occurs) recursion if a file tries to include itself.
1276 #define MAX_NEST_LEVEL 16
1279 * Entry point for parsing a menu file. nest_level indicates how many times
1280 * we've nested in includes. It will be 1 for the top level menu file.
1282 * Returns 1 on success, < 0 on error.
1284 static int parse_pxefile_top(cmd_tbl_t
*cmdtp
, char *p
, struct pxe_menu
*cfg
, int nest_level
)
1287 char *s
, *b
, *label_name
;
1292 if (nest_level
> MAX_NEST_LEVEL
) {
1293 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL
);
1300 get_token(&p
, &t
, L_KEYWORD
);
1306 err
= parse_menu(cmdtp
, &p
, cfg
, b
, nest_level
);
1310 err
= parse_integer(&p
, &cfg
->timeout
);
1314 err
= parse_label(&p
, cfg
);
1319 err
= parse_sliteral(&p
, &label_name
);
1322 if (cfg
->default_label
)
1323 free(cfg
->default_label
);
1325 cfg
->default_label
= label_name
;
1331 err
= handle_include(cmdtp
, &p
, b
+ ALIGN(strlen(b
), 4), cfg
,
1346 printf("Ignoring unknown command: %.*s\n",
1357 * Free the memory used by a pxe_menu and its labels.
1359 static void destroy_pxe_menu(struct pxe_menu
*cfg
)
1361 struct list_head
*pos
, *n
;
1362 struct pxe_label
*label
;
1367 if (cfg
->default_label
)
1368 free(cfg
->default_label
);
1370 list_for_each_safe(pos
, n
, &cfg
->labels
) {
1371 label
= list_entry(pos
, struct pxe_label
, list
);
1373 label_destroy(label
);
1380 * Entry point for parsing a pxe file. This is only used for the top level
1383 * Returns NULL if there is an error, otherwise, returns a pointer to a
1384 * pxe_menu struct populated with the results of parsing the pxe file (and any
1385 * files it includes). The resulting pxe_menu struct can be free()'d by using
1386 * the destroy_pxe_menu() function.
1388 static struct pxe_menu
*parse_pxefile(cmd_tbl_t
*cmdtp
, char *menucfg
)
1390 struct pxe_menu
*cfg
;
1392 cfg
= malloc(sizeof(struct pxe_menu
));
1397 memset(cfg
, 0, sizeof(struct pxe_menu
));
1399 INIT_LIST_HEAD(&cfg
->labels
);
1401 if (parse_pxefile_top(cmdtp
, menucfg
, cfg
, 1) < 0) {
1402 destroy_pxe_menu(cfg
);
1410 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1413 static struct menu
*pxe_menu_to_menu(struct pxe_menu
*cfg
)
1415 struct pxe_label
*label
;
1416 struct list_head
*pos
;
1420 char *default_num
= NULL
;
1423 * Create a menu and add items for all the labels.
1425 m
= menu_create(cfg
->title
, cfg
->timeout
, cfg
->prompt
, label_print
,
1431 list_for_each(pos
, &cfg
->labels
) {
1432 label
= list_entry(pos
, struct pxe_label
, list
);
1434 sprintf(label
->num
, "%d", i
++);
1435 if (menu_item_add(m
, label
->num
, label
) != 1) {
1439 if (cfg
->default_label
&&
1440 (strcmp(label
->name
, cfg
->default_label
) == 0))
1441 default_num
= label
->num
;
1446 * After we've created items for each label in the menu, set the
1447 * menu's default label if one was specified.
1450 err
= menu_default_set(m
, default_num
);
1452 if (err
!= -ENOENT
) {
1457 printf("Missing default: %s\n", cfg
->default_label
);
1465 * Try to boot any labels we have yet to attempt to boot.
1467 static void boot_unattempted_labels(cmd_tbl_t
*cmdtp
, struct pxe_menu
*cfg
)
1469 struct list_head
*pos
;
1470 struct pxe_label
*label
;
1472 list_for_each(pos
, &cfg
->labels
) {
1473 label
= list_entry(pos
, struct pxe_label
, list
);
1475 if (!label
->attempted
)
1476 label_boot(cmdtp
, label
);
1481 * Boot the system as prescribed by a pxe_menu.
1483 * Use the menu system to either get the user's choice or the default, based
1484 * on config or user input. If there is no default or user's choice,
1485 * attempted to boot labels in the order they were given in pxe files.
1486 * If the default or user's choice fails to boot, attempt to boot other
1487 * labels in the order they were given in pxe files.
1489 * If this function returns, there weren't any labels that successfully
1490 * booted, or the user interrupted the menu selection via ctrl+c.
1492 static void handle_pxe_menu(cmd_tbl_t
*cmdtp
, struct pxe_menu
*cfg
)
1498 m
= pxe_menu_to_menu(cfg
);
1502 err
= menu_get_choice(m
, &choice
);
1507 * err == 1 means we got a choice back from menu_get_choice.
1509 * err == -ENOENT if the menu was setup to select the default but no
1510 * default was set. in that case, we should continue trying to boot
1511 * labels that haven't been attempted yet.
1513 * otherwise, the user interrupted or there was some other error and
1518 err
= label_boot(cmdtp
, choice
);
1521 } else if (err
!= -ENOENT
) {
1525 boot_unattempted_labels(cmdtp
, cfg
);
1528 #ifdef CONFIG_CMD_NET
1530 * Boots a system using a pxe file
1532 * Returns 0 on success, 1 on error.
1535 do_pxe_boot(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
1537 unsigned long pxefile_addr_r
;
1538 struct pxe_menu
*cfg
;
1539 char *pxefile_addr_str
;
1541 do_getfile
= do_get_tftp
;
1544 pxefile_addr_str
= from_env("pxefile_addr_r");
1545 if (!pxefile_addr_str
)
1548 } else if (argc
== 2) {
1549 pxefile_addr_str
= argv
[1];
1551 return CMD_RET_USAGE
;
1554 if (strict_strtoul(pxefile_addr_str
, 16, &pxefile_addr_r
) < 0) {
1555 printf("Invalid pxefile address: %s\n", pxefile_addr_str
);
1559 cfg
= parse_pxefile(cmdtp
, (char *)(pxefile_addr_r
));
1562 printf("Error parsing config file\n");
1566 handle_pxe_menu(cmdtp
, cfg
);
1568 destroy_pxe_menu(cfg
);
1570 copy_filename(BootFile
, "", sizeof(BootFile
));
1575 static cmd_tbl_t cmd_pxe_sub
[] = {
1576 U_BOOT_CMD_MKENT(get
, 1, 1, do_pxe_get
, "", ""),
1577 U_BOOT_CMD_MKENT(boot
, 2, 1, do_pxe_boot
, "", "")
1580 static int do_pxe(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
1585 return CMD_RET_USAGE
;
1589 /* drop initial "pxe" arg */
1593 cp
= find_cmd_tbl(argv
[0], cmd_pxe_sub
, ARRAY_SIZE(cmd_pxe_sub
));
1596 return cp
->cmd(cmdtp
, flag
, argc
, argv
);
1598 return CMD_RET_USAGE
;
1603 "commands to get and boot from pxe files",
1604 "get - try to retrieve a pxe file using tftp\npxe "
1605 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1610 * Boots a system using a local disk syslinux/extlinux file
1612 * Returns 0 on success, 1 on error.
1614 static int do_sysboot(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
1616 unsigned long pxefile_addr_r
;
1617 struct pxe_menu
*cfg
;
1618 char *pxefile_addr_str
;
1624 if (strstr(argv
[1], "-p")) {
1631 return cmd_usage(cmdtp
);
1634 pxefile_addr_str
= from_env("pxefile_addr_r");
1635 if (!pxefile_addr_str
)
1638 pxefile_addr_str
= argv
[4];
1642 filename
= getenv("bootfile");
1645 setenv("bootfile", filename
);
1648 if (strstr(argv
[3], "ext2"))
1649 do_getfile
= do_get_ext2
;
1650 else if (strstr(argv
[3], "fat"))
1651 do_getfile
= do_get_fat
;
1652 else if (strstr(argv
[3], "any"))
1653 do_getfile
= do_get_any
;
1655 printf("Invalid filesystem: %s\n", argv
[3]);
1658 fs_argv
[1] = argv
[1];
1659 fs_argv
[2] = argv
[2];
1661 if (strict_strtoul(pxefile_addr_str
, 16, &pxefile_addr_r
) < 0) {
1662 printf("Invalid pxefile address: %s\n", pxefile_addr_str
);
1666 if (get_pxe_file(cmdtp
, filename
, (void *)pxefile_addr_r
) < 0) {
1667 printf("Error reading config file\n");
1671 cfg
= parse_pxefile(cmdtp
, (char *)(pxefile_addr_r
));
1674 printf("Error parsing config file\n");
1681 handle_pxe_menu(cmdtp
, cfg
);
1683 destroy_pxe_menu(cfg
);
1689 sysboot
, 7, 1, do_sysboot
,
1690 "command to get and boot from syslinux files",
1691 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1692 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1693 " or any filesystem on 'dev' on 'interface' to address 'addr'"