]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - cmd/pxe.c
1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2010-2011 Calxeda, Inc.
4 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
11 #include <linux/string.h>
12 #include <linux/ctype.h>
14 #include <linux/list.h>
21 #define MAX_TFTP_PATH_LEN 127
23 const char *pxe_default_paths
[] = {
25 "default-" CONFIG_SYS_ARCH
"-" CONFIG_SYS_SOC
,
27 "default-" CONFIG_SYS_ARCH
,
35 * Like env_get, but prints an error if envvar isn't defined in the
36 * environment. It always returns what env_get does, so it can be used in
37 * place of env_get without changing error handling otherwise.
39 static char *from_env(const char *envvar
)
43 ret
= env_get(envvar
);
46 printf("missing environment variable: %s\n", envvar
);
53 * Convert an ethaddr from the environment to the format used by pxelinux
54 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
55 * the beginning of the ethernet address to indicate a hardware type of
56 * Ethernet. Also converts uppercase hex characters into lowercase, to match
57 * pxelinux's behavior.
59 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
60 * environment, or some other value < 0 on error.
62 static int format_mac_pxe(char *outbuf
, size_t outbuf_len
)
66 if (outbuf_len
< 21) {
67 printf("outbuf is too small (%zd < 21)\n", outbuf_len
);
72 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr
))
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
,
192 unsigned long file_addr
)
195 char relfile
[MAX_TFTP_PATH_LEN
+1];
199 err
= get_bootfile_path(file_path
, relfile
, sizeof(relfile
));
204 path_len
= strlen(file_path
);
205 path_len
+= strlen(relfile
);
207 if (path_len
> MAX_TFTP_PATH_LEN
) {
208 printf("Base path too long (%s%s)\n",
212 return -ENAMETOOLONG
;
215 strcat(relfile
, file_path
);
217 printf("Retrieving file: %s\n", relfile
);
219 sprintf(addr_buf
, "%lx", file_addr
);
221 return do_getfile(cmdtp
, relfile
, addr_buf
);
225 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
226 * 'bootfile' was specified in the environment, the path to bootfile will be
227 * prepended to 'file_path' and the resulting path will be used.
229 * Returns 1 on success, or < 0 for error.
231 static int get_pxe_file(cmd_tbl_t
*cmdtp
, const char *file_path
,
232 unsigned long file_addr
)
234 unsigned long config_file_size
;
239 err
= get_relfile(cmdtp
, file_path
, file_addr
);
245 * the file comes without a NUL byte at the end, so find out its size
246 * and add the NUL byte.
248 tftp_filesize
= from_env("filesize");
253 if (strict_strtoul(tftp_filesize
, 16, &config_file_size
) < 0)
256 buf
= map_sysmem(file_addr
+ config_file_size
, 1);
263 #ifdef CONFIG_CMD_NET
265 #define PXELINUX_DIR "pxelinux.cfg/"
268 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
269 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
270 * from the bootfile path, as described above.
272 * Returns 1 on success or < 0 on error.
274 static int get_pxelinux_path(cmd_tbl_t
*cmdtp
, const char *file
,
275 unsigned long pxefile_addr_r
)
277 size_t base_len
= strlen(PXELINUX_DIR
);
278 char path
[MAX_TFTP_PATH_LEN
+1];
280 if (base_len
+ strlen(file
) > MAX_TFTP_PATH_LEN
) {
281 printf("path (%s%s) too long, skipping\n",
283 return -ENAMETOOLONG
;
286 sprintf(path
, PXELINUX_DIR
"%s", file
);
288 return get_pxe_file(cmdtp
, path
, pxefile_addr_r
);
292 * Looks for a pxe file with a name based on the pxeuuid environment variable.
294 * Returns 1 on success or < 0 on error.
296 static int pxe_uuid_path(cmd_tbl_t
*cmdtp
, unsigned long pxefile_addr_r
)
300 uuid_str
= from_env("pxeuuid");
305 return get_pxelinux_path(cmdtp
, uuid_str
, pxefile_addr_r
);
309 * Looks for a pxe file with a name based on the 'ethaddr' environment
312 * Returns 1 on success or < 0 on error.
314 static int pxe_mac_path(cmd_tbl_t
*cmdtp
, unsigned long pxefile_addr_r
)
319 err
= format_mac_pxe(mac_str
, sizeof(mac_str
));
324 return get_pxelinux_path(cmdtp
, mac_str
, pxefile_addr_r
);
328 * Looks for pxe files with names based on our IP address. See pxelinux
329 * documentation for details on what these file names look like. We match
332 * Returns 1 on success or < 0 on error.
334 static int pxe_ipaddr_paths(cmd_tbl_t
*cmdtp
, unsigned long pxefile_addr_r
)
339 sprintf(ip_addr
, "%08X", ntohl(net_ip
.s_addr
));
341 for (mask_pos
= 7; mask_pos
>= 0; mask_pos
--) {
342 err
= get_pxelinux_path(cmdtp
, ip_addr
, pxefile_addr_r
);
347 ip_addr
[mask_pos
] = '\0';
354 * Entry point for the 'pxe get' command.
355 * This Follows pxelinux's rules to download a config file from a tftp server.
356 * The file is stored at the location given by the pxefile_addr_r environment
357 * variable, which must be set.
359 * UUID comes from pxeuuid env variable, if defined
360 * MAC addr comes from ethaddr env variable, if defined
363 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
365 * Returns 0 on success or 1 on error.
368 do_pxe_get(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
370 char *pxefile_addr_str
;
371 unsigned long pxefile_addr_r
;
374 do_getfile
= do_get_tftp
;
377 return CMD_RET_USAGE
;
379 pxefile_addr_str
= from_env("pxefile_addr_r");
381 if (!pxefile_addr_str
)
384 err
= strict_strtoul(pxefile_addr_str
, 16,
385 (unsigned long *)&pxefile_addr_r
);
390 * Keep trying paths until we successfully get a file we're looking
393 if (pxe_uuid_path(cmdtp
, pxefile_addr_r
) > 0 ||
394 pxe_mac_path(cmdtp
, pxefile_addr_r
) > 0 ||
395 pxe_ipaddr_paths(cmdtp
, pxefile_addr_r
) > 0) {
396 printf("Config file found\n");
401 while (pxe_default_paths
[i
]) {
402 if (get_pxelinux_path(cmdtp
, pxe_default_paths
[i
],
403 pxefile_addr_r
) > 0) {
404 printf("Config file found\n");
410 printf("Config file not found\n");
417 * Wrapper to make it easier to store the file at file_path in the location
418 * specified by envaddr_name. file_path will be joined to the bootfile path,
419 * if any is specified.
421 * Returns 1 on success or < 0 on error.
423 static int get_relfile_envaddr(cmd_tbl_t
*cmdtp
, const char *file_path
, const char *envaddr_name
)
425 unsigned long file_addr
;
428 envaddr
= from_env(envaddr_name
);
433 if (strict_strtoul(envaddr
, 16, &file_addr
) < 0)
436 return get_relfile(cmdtp
, file_path
, file_addr
);
440 * A note on the pxe file parser.
442 * We're parsing files that use syslinux grammar, which has a few quirks.
443 * String literals must be recognized based on context - there is no
444 * quoting or escaping support. There's also nothing to explicitly indicate
445 * when a label section completes. We deal with that by ending a label
446 * section whenever we see a line that doesn't include.
448 * As with the syslinux family, this same file format could be reused in the
449 * future for non pxe purposes. The only action it takes during parsing that
450 * would throw this off is handling of include files. It assumes we're using
451 * pxe, and does a tftp download of a file listed as an include file in the
452 * middle of the parsing operation. That could be handled by refactoring it to
453 * take a 'include file getter' function.
457 * Describes a single label given in a pxe file.
459 * Create these with the 'label_create' function given below.
461 * name - the name of the menu as given on the 'menu label' line.
462 * kernel - the path to the kernel file to use for this label.
463 * append - kernel command line to use when booting this label
464 * initrd - path to the initrd to use for this label.
465 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
466 * localboot - 1 if this label specified 'localboot', 0 otherwise.
467 * list - lets these form a list, which a pxe_menu struct will hold.
482 struct list_head list
;
486 * Describes a pxe menu as given via pxe files.
488 * title - the name of the menu as given by a 'menu title' line.
489 * default_label - the name of the default label, if any.
490 * timeout - time in tenths of a second to wait for a user key-press before
491 * booting the default label.
492 * prompt - if 0, don't prompt for a choice unless the timeout period is
493 * interrupted. If 1, always prompt for a choice regardless of
495 * labels - a list of labels defined for the menu.
502 struct list_head labels
;
506 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
507 * result must be free()'d to reclaim the memory.
509 * Returns NULL if malloc fails.
511 static struct pxe_label
*label_create(void)
513 struct pxe_label
*label
;
515 label
= malloc(sizeof(struct pxe_label
));
520 memset(label
, 0, sizeof(struct pxe_label
));
526 * Free the memory used by a pxe_label, including that used by its name,
527 * kernel, append and initrd members, if they're non NULL.
529 * So - be sure to only use dynamically allocated memory for the members of
530 * the pxe_label struct, unless you want to clean it up first. These are
531 * currently only created by the pxe file parsing code.
533 static void label_destroy(struct pxe_label
*label
)
557 * Print a label and its string members if they're defined.
559 * This is passed as a callback to the menu code for displaying each
562 static void label_print(void *data
)
564 struct pxe_label
*label
= data
;
565 const char *c
= label
->menu
? label
->menu
: label
->name
;
567 printf("%s:\t%s\n", label
->num
, c
);
571 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
572 * environment variable is defined. Its contents will be executed as U-Boot
573 * command. If the label specified an 'append' line, its contents will be
574 * used to overwrite the contents of the 'bootargs' environment variable prior
575 * to running 'localcmd'.
577 * Returns 1 on success or < 0 on error.
579 static int label_localboot(struct pxe_label
*label
)
583 localcmd
= from_env("localcmd");
589 char bootargs
[CONFIG_SYS_CBSIZE
];
591 cli_simple_process_macros(label
->append
, bootargs
);
592 env_set("bootargs", bootargs
);
595 debug("running: %s\n", localcmd
);
597 return run_command_list(localcmd
, strlen(localcmd
), 0);
601 * Boot according to the contents of a pxe_label.
603 * If we can't boot for any reason, we return. A successful boot never
606 * The kernel will be stored in the location given by the 'kernel_addr_r'
607 * environment variable.
609 * If the label specifies an initrd file, it will be stored in the location
610 * given by the 'ramdisk_addr_r' environment variable.
612 * If the label specifies an 'append' line, its contents will overwrite that
613 * of the 'bootargs' environment variable.
615 static int label_boot(cmd_tbl_t
*cmdtp
, struct pxe_label
*label
)
617 char *bootm_argv
[] = { "bootm", NULL
, NULL
, NULL
, NULL
};
619 char mac_str
[29] = "";
620 char ip_str
[68] = "";
628 label
->attempted
= 1;
630 if (label
->localboot
) {
631 if (label
->localboot_val
>= 0)
632 label_localboot(label
);
636 if (label
->kernel
== NULL
) {
637 printf("No kernel given, skipping %s\n",
643 if (get_relfile_envaddr(cmdtp
, label
->initrd
, "ramdisk_addr_r") < 0) {
644 printf("Skipping %s for failure retrieving initrd\n",
649 bootm_argv
[2] = initrd_str
;
650 strncpy(bootm_argv
[2], env_get("ramdisk_addr_r"), 18);
651 strcat(bootm_argv
[2], ":");
652 strncat(bootm_argv
[2], env_get("filesize"), 9);
655 if (get_relfile_envaddr(cmdtp
, label
->kernel
, "kernel_addr_r") < 0) {
656 printf("Skipping %s for failure retrieving kernel\n",
661 if (label
->ipappend
& 0x1) {
662 sprintf(ip_str
, " ip=%s:%s:%s:%s",
663 env_get("ipaddr"), env_get("serverip"),
664 env_get("gatewayip"), env_get("netmask"));
667 #ifdef CONFIG_CMD_NET
668 if (label
->ipappend
& 0x2) {
670 strcpy(mac_str
, " BOOTIF=");
671 err
= format_mac_pxe(mac_str
+ 8, sizeof(mac_str
) - 8);
677 if ((label
->ipappend
& 0x3) || label
->append
) {
678 char bootargs
[CONFIG_SYS_CBSIZE
] = "";
679 char finalbootargs
[CONFIG_SYS_CBSIZE
];
681 if (strlen(label
->append
?: "") +
682 strlen(ip_str
) + strlen(mac_str
) + 1 > sizeof(bootargs
)) {
683 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
684 strlen(label
->append
?: ""),
685 strlen(ip_str
), strlen(mac_str
),
690 strncpy(bootargs
, label
->append
,
692 strcat(bootargs
, ip_str
);
693 strcat(bootargs
, mac_str
);
695 cli_simple_process_macros(bootargs
, finalbootargs
);
696 env_set("bootargs", finalbootargs
);
697 printf("append: %s\n", finalbootargs
);
701 bootm_argv
[1] = env_get("kernel_addr_r");
704 * fdt usage is optional:
705 * It handles the following scenarios. All scenarios are exclusive
707 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
708 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
709 * and adjust argc appropriately.
711 * Scenario 2: If there is an fdt_addr specified, pass it along to
712 * bootm, and adjust argc appropriately.
714 * Scenario 3: fdt blob is not available.
716 bootm_argv
[3] = env_get("fdt_addr_r");
718 /* if fdt label is defined then get fdt from server */
720 char *fdtfile
= NULL
;
721 char *fdtfilefree
= NULL
;
724 fdtfile
= label
->fdt
;
725 } else if (label
->fdtdir
) {
726 char *f1
, *f2
, *f3
, *f4
, *slash
;
728 f1
= env_get("fdtfile");
735 * For complex cases where this code doesn't
736 * generate the correct filename, the board
737 * code should set $fdtfile during early boot,
738 * or the boot scripts should set $fdtfile
739 * before invoking "pxe" or "sysboot".
743 f3
= env_get("board");
747 len
= strlen(label
->fdtdir
);
750 else if (label
->fdtdir
[len
- 1] != '/')
755 len
= strlen(label
->fdtdir
) + strlen(slash
) +
756 strlen(f1
) + strlen(f2
) + strlen(f3
) +
758 fdtfilefree
= malloc(len
);
760 printf("malloc fail (FDT filename)\n");
764 snprintf(fdtfilefree
, len
, "%s%s%s%s%s%s",
765 label
->fdtdir
, slash
, f1
, f2
, f3
, f4
);
766 fdtfile
= fdtfilefree
;
770 int err
= get_relfile_envaddr(cmdtp
, fdtfile
, "fdt_addr_r");
773 printf("Skipping %s for failure retrieving fdt\n",
778 bootm_argv
[3] = NULL
;
783 bootm_argv
[3] = env_get("fdt_addr");
791 kernel_addr
= genimg_get_kernel_addr(bootm_argv
[1]);
792 buf
= map_sysmem(kernel_addr
, 0);
793 /* Try bootm for legacy and FIT format image */
794 if (genimg_get_format(buf
) != IMAGE_FORMAT_INVALID
)
795 do_bootm(cmdtp
, 0, bootm_argc
, bootm_argv
);
796 #ifdef CONFIG_CMD_BOOTI
797 /* Try booting an AArch64 Linux kernel image */
799 do_booti(cmdtp
, 0, bootm_argc
, bootm_argv
);
800 #elif defined(CONFIG_CMD_BOOTZ)
801 /* Try booting a Image */
803 do_bootz(cmdtp
, 0, bootm_argc
, bootm_argv
);
810 * Tokens for the pxe file parser.
836 * A token - given by a value and a type.
840 enum token_type type
;
844 * Keywords recognized.
846 static const struct token keywords
[] = {
849 {"timeout", T_TIMEOUT
},
850 {"default", T_DEFAULT
},
851 {"prompt", T_PROMPT
},
853 {"kernel", T_KERNEL
},
855 {"localboot", T_LOCALBOOT
},
856 {"append", T_APPEND
},
857 {"initrd", T_INITRD
},
858 {"include", T_INCLUDE
},
859 {"devicetree", T_FDT
},
861 {"devicetreedir", T_FDTDIR
},
862 {"fdtdir", T_FDTDIR
},
863 {"ontimeout", T_ONTIMEOUT
,},
864 {"ipappend", T_IPAPPEND
,},
869 * Since pxe(linux) files don't have a token to identify the start of a
870 * literal, we have to keep track of when we're in a state where a literal is
871 * expected vs when we're in a state a keyword is expected.
880 * get_string retrieves a string from *p and stores it as a token in
883 * get_string used for scanning both string literals and keywords.
885 * Characters from *p are copied into t-val until a character equal to
886 * delim is found, or a NUL byte is reached. If delim has the special value of
887 * ' ', any whitespace character will be used as a delimiter.
889 * If lower is unequal to 0, uppercase characters will be converted to
890 * lowercase in the result. This is useful to make keywords case
893 * The location of *p is updated to point to the first character after the end
894 * of the token - the ending delimiter.
896 * On success, the new value of t->val is returned. Memory for t->val is
897 * allocated using malloc and must be free()'d to reclaim it. If insufficient
898 * memory is available, NULL is returned.
900 static char *get_string(char **p
, struct token
*t
, char delim
, int lower
)
906 * b and e both start at the beginning of the input stream.
908 * e is incremented until we find the ending delimiter, or a NUL byte
909 * is reached. Then, we take e - b to find the length of the token.
914 if ((delim
== ' ' && isspace(*e
)) || delim
== *e
)
922 * Allocate memory to hold the string, and copy it in, converting
923 * characters to lowercase if lower is != 0.
925 t
->val
= malloc(len
+ 1);
929 for (i
= 0; i
< len
; i
++, b
++) {
931 t
->val
[i
] = tolower(*b
);
939 * Update *p so the caller knows where to continue scanning.
949 * Populate a keyword token with a type and value.
951 static void get_keyword(struct token
*t
)
955 for (i
= 0; keywords
[i
].val
; i
++) {
956 if (!strcmp(t
->val
, keywords
[i
].val
)) {
957 t
->type
= keywords
[i
].type
;
964 * Get the next token. We have to keep track of which state we're in to know
965 * if we're looking to get a string literal or a keyword.
967 * *p is updated to point at the first character after the current token.
969 static void get_token(char **p
, struct token
*t
, enum lex_state state
)
975 /* eat non EOL whitespace */
980 * eat comments. note that string literals can't begin with #, but
981 * can contain a # after their first character.
984 while (*c
&& *c
!= '\n')
991 } else if (*c
== '\0') {
994 } else if (state
== L_SLITERAL
) {
995 get_string(&c
, t
, '\n', 0);
996 } else if (state
== L_KEYWORD
) {
998 * when we expect a keyword, we first get the next string
999 * token delimited by whitespace, and then check if it
1000 * matches a keyword in our keyword list. if it does, it's
1001 * converted to a keyword token of the appropriate type, and
1002 * if not, it remains a string token.
1004 get_string(&c
, t
, ' ', 1);
1012 * Increment *c until we get to the end of the current line, or EOF.
1014 static void eol_or_eof(char **c
)
1016 while (**c
&& **c
!= '\n')
1021 * All of these parse_* functions share some common behavior.
1023 * They finish with *c pointing after the token they parse, and return 1 on
1024 * success, or < 0 on error.
1028 * Parse a string literal and store a pointer it at *dst. String literals
1029 * terminate at the end of the line.
1031 static int parse_sliteral(char **c
, char **dst
)
1036 get_token(c
, &t
, L_SLITERAL
);
1038 if (t
.type
!= T_STRING
) {
1039 printf("Expected string literal: %.*s\n", (int)(*c
- s
), s
);
1049 * Parse a base 10 (unsigned) integer and store it at *dst.
1051 static int parse_integer(char **c
, int *dst
)
1056 get_token(c
, &t
, L_SLITERAL
);
1058 if (t
.type
!= T_STRING
) {
1059 printf("Expected string: %.*s\n", (int)(*c
- s
), s
);
1063 *dst
= simple_strtol(t
.val
, NULL
, 10);
1070 static int parse_pxefile_top(cmd_tbl_t
*cmdtp
, char *p
, unsigned long base
,
1071 struct pxe_menu
*cfg
, int nest_level
);
1074 * Parse an include statement, and retrieve and parse the file it mentions.
1076 * base should point to a location where it's safe to store the file, and
1077 * nest_level should indicate how many nested includes have occurred. For this
1078 * include, nest_level has already been incremented and doesn't need to be
1081 static int handle_include(cmd_tbl_t
*cmdtp
, char **c
, unsigned long base
,
1082 struct pxe_menu
*cfg
, int nest_level
)
1090 err
= parse_sliteral(c
, &include_path
);
1093 printf("Expected include path: %.*s\n",
1098 err
= get_pxe_file(cmdtp
, include_path
, base
);
1101 printf("Couldn't retrieve %s\n", include_path
);
1105 buf
= map_sysmem(base
, 0);
1106 ret
= parse_pxefile_top(cmdtp
, buf
, base
, cfg
, nest_level
);
1113 * Parse lines that begin with 'menu'.
1115 * base and nest are provided to handle the 'menu include' case.
1117 * base should point to a location where it's safe to store the included file.
1119 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1120 * a file it includes, 3 when parsing a file included by that file, and so on.
1122 static int parse_menu(cmd_tbl_t
*cmdtp
, char **c
, struct pxe_menu
*cfg
,
1123 unsigned long base
, int nest_level
)
1129 get_token(c
, &t
, L_KEYWORD
);
1133 err
= parse_sliteral(c
, &cfg
->title
);
1138 err
= handle_include(cmdtp
, c
, base
, cfg
,
1143 printf("Ignoring malformed menu command: %.*s\n",
1156 * Handles parsing a 'menu line' when we're parsing a label.
1158 static int parse_label_menu(char **c
, struct pxe_menu
*cfg
,
1159 struct pxe_label
*label
)
1166 get_token(c
, &t
, L_KEYWORD
);
1170 if (!cfg
->default_label
)
1171 cfg
->default_label
= strdup(label
->name
);
1173 if (!cfg
->default_label
)
1178 parse_sliteral(c
, &label
->menu
);
1181 printf("Ignoring malformed menu command: %.*s\n",
1191 * Parses a label and adds it to the list of labels for a menu.
1193 * A label ends when we either get to the end of a file, or
1194 * get some input we otherwise don't have a handler defined
1198 static int parse_label(char **c
, struct pxe_menu
*cfg
)
1203 struct pxe_label
*label
;
1206 label
= label_create();
1210 err
= parse_sliteral(c
, &label
->name
);
1212 printf("Expected label name: %.*s\n", (int)(*c
- s
), s
);
1213 label_destroy(label
);
1217 list_add_tail(&label
->list
, &cfg
->labels
);
1221 get_token(c
, &t
, L_KEYWORD
);
1226 err
= parse_label_menu(c
, cfg
, label
);
1231 err
= parse_sliteral(c
, &label
->kernel
);
1235 err
= parse_sliteral(c
, &label
->append
);
1238 s
= strstr(label
->append
, "initrd=");
1242 len
= (int)(strchr(s
, ' ') - s
);
1243 label
->initrd
= malloc(len
+ 1);
1244 strncpy(label
->initrd
, s
, len
);
1245 label
->initrd
[len
] = '\0';
1251 err
= parse_sliteral(c
, &label
->initrd
);
1256 err
= parse_sliteral(c
, &label
->fdt
);
1261 err
= parse_sliteral(c
, &label
->fdtdir
);
1265 label
->localboot
= 1;
1266 err
= parse_integer(c
, &label
->localboot_val
);
1270 err
= parse_integer(c
, &label
->ipappend
);
1278 * put the token back! we don't want it - it's the end
1279 * of a label and whatever token this is, it's
1280 * something for the menu level context to handle.
1292 * This 16 comes from the limit pxelinux imposes on nested includes.
1294 * There is no reason at all we couldn't do more, but some limit helps prevent
1295 * infinite (until crash occurs) recursion if a file tries to include itself.
1297 #define MAX_NEST_LEVEL 16
1300 * Entry point for parsing a menu file. nest_level indicates how many times
1301 * we've nested in includes. It will be 1 for the top level menu file.
1303 * Returns 1 on success, < 0 on error.
1305 static int parse_pxefile_top(cmd_tbl_t
*cmdtp
, char *p
, unsigned long base
,
1306 struct pxe_menu
*cfg
, int nest_level
)
1309 char *s
, *b
, *label_name
;
1314 if (nest_level
> MAX_NEST_LEVEL
) {
1315 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL
);
1322 get_token(&p
, &t
, L_KEYWORD
);
1328 err
= parse_menu(cmdtp
, &p
, cfg
,
1329 base
+ ALIGN(strlen(b
) + 1, 4),
1334 err
= parse_integer(&p
, &cfg
->timeout
);
1338 err
= parse_label(&p
, cfg
);
1343 err
= parse_sliteral(&p
, &label_name
);
1346 if (cfg
->default_label
)
1347 free(cfg
->default_label
);
1349 cfg
->default_label
= label_name
;
1355 err
= handle_include(cmdtp
, &p
,
1356 base
+ ALIGN(strlen(b
), 4), cfg
,
1371 printf("Ignoring unknown command: %.*s\n",
1382 * Free the memory used by a pxe_menu and its labels.
1384 static void destroy_pxe_menu(struct pxe_menu
*cfg
)
1386 struct list_head
*pos
, *n
;
1387 struct pxe_label
*label
;
1392 if (cfg
->default_label
)
1393 free(cfg
->default_label
);
1395 list_for_each_safe(pos
, n
, &cfg
->labels
) {
1396 label
= list_entry(pos
, struct pxe_label
, list
);
1398 label_destroy(label
);
1405 * Entry point for parsing a pxe file. This is only used for the top level
1408 * Returns NULL if there is an error, otherwise, returns a pointer to a
1409 * pxe_menu struct populated with the results of parsing the pxe file (and any
1410 * files it includes). The resulting pxe_menu struct can be free()'d by using
1411 * the destroy_pxe_menu() function.
1413 static struct pxe_menu
*parse_pxefile(cmd_tbl_t
*cmdtp
, unsigned long menucfg
)
1415 struct pxe_menu
*cfg
;
1419 cfg
= malloc(sizeof(struct pxe_menu
));
1424 memset(cfg
, 0, sizeof(struct pxe_menu
));
1426 INIT_LIST_HEAD(&cfg
->labels
);
1428 buf
= map_sysmem(menucfg
, 0);
1429 r
= parse_pxefile_top(cmdtp
, buf
, menucfg
, cfg
, 1);
1433 destroy_pxe_menu(cfg
);
1441 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1444 static struct menu
*pxe_menu_to_menu(struct pxe_menu
*cfg
)
1446 struct pxe_label
*label
;
1447 struct list_head
*pos
;
1451 char *default_num
= NULL
;
1454 * Create a menu and add items for all the labels.
1456 m
= menu_create(cfg
->title
, DIV_ROUND_UP(cfg
->timeout
, 10),
1457 cfg
->prompt
, label_print
, NULL
, NULL
);
1462 list_for_each(pos
, &cfg
->labels
) {
1463 label
= list_entry(pos
, struct pxe_label
, list
);
1465 sprintf(label
->num
, "%d", i
++);
1466 if (menu_item_add(m
, label
->num
, label
) != 1) {
1470 if (cfg
->default_label
&&
1471 (strcmp(label
->name
, cfg
->default_label
) == 0))
1472 default_num
= label
->num
;
1477 * After we've created items for each label in the menu, set the
1478 * menu's default label if one was specified.
1481 err
= menu_default_set(m
, default_num
);
1483 if (err
!= -ENOENT
) {
1488 printf("Missing default: %s\n", cfg
->default_label
);
1496 * Try to boot any labels we have yet to attempt to boot.
1498 static void boot_unattempted_labels(cmd_tbl_t
*cmdtp
, struct pxe_menu
*cfg
)
1500 struct list_head
*pos
;
1501 struct pxe_label
*label
;
1503 list_for_each(pos
, &cfg
->labels
) {
1504 label
= list_entry(pos
, struct pxe_label
, list
);
1506 if (!label
->attempted
)
1507 label_boot(cmdtp
, label
);
1512 * Boot the system as prescribed by a pxe_menu.
1514 * Use the menu system to either get the user's choice or the default, based
1515 * on config or user input. If there is no default or user's choice,
1516 * attempted to boot labels in the order they were given in pxe files.
1517 * If the default or user's choice fails to boot, attempt to boot other
1518 * labels in the order they were given in pxe files.
1520 * If this function returns, there weren't any labels that successfully
1521 * booted, or the user interrupted the menu selection via ctrl+c.
1523 static void handle_pxe_menu(cmd_tbl_t
*cmdtp
, struct pxe_menu
*cfg
)
1529 m
= pxe_menu_to_menu(cfg
);
1533 err
= menu_get_choice(m
, &choice
);
1538 * err == 1 means we got a choice back from menu_get_choice.
1540 * err == -ENOENT if the menu was setup to select the default but no
1541 * default was set. in that case, we should continue trying to boot
1542 * labels that haven't been attempted yet.
1544 * otherwise, the user interrupted or there was some other error and
1549 err
= label_boot(cmdtp
, choice
);
1552 } else if (err
!= -ENOENT
) {
1556 boot_unattempted_labels(cmdtp
, cfg
);
1559 #ifdef CONFIG_CMD_NET
1561 * Boots a system using a pxe file
1563 * Returns 0 on success, 1 on error.
1566 do_pxe_boot(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
1568 unsigned long pxefile_addr_r
;
1569 struct pxe_menu
*cfg
;
1570 char *pxefile_addr_str
;
1572 do_getfile
= do_get_tftp
;
1575 pxefile_addr_str
= from_env("pxefile_addr_r");
1576 if (!pxefile_addr_str
)
1579 } else if (argc
== 2) {
1580 pxefile_addr_str
= argv
[1];
1582 return CMD_RET_USAGE
;
1585 if (strict_strtoul(pxefile_addr_str
, 16, &pxefile_addr_r
) < 0) {
1586 printf("Invalid pxefile address: %s\n", pxefile_addr_str
);
1590 cfg
= parse_pxefile(cmdtp
, pxefile_addr_r
);
1593 printf("Error parsing config file\n");
1597 handle_pxe_menu(cmdtp
, cfg
);
1599 destroy_pxe_menu(cfg
);
1601 copy_filename(net_boot_file_name
, "", sizeof(net_boot_file_name
));
1606 static cmd_tbl_t cmd_pxe_sub
[] = {
1607 U_BOOT_CMD_MKENT(get
, 1, 1, do_pxe_get
, "", ""),
1608 U_BOOT_CMD_MKENT(boot
, 2, 1, do_pxe_boot
, "", "")
1611 static int do_pxe(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
1616 return CMD_RET_USAGE
;
1620 /* drop initial "pxe" arg */
1624 cp
= find_cmd_tbl(argv
[0], cmd_pxe_sub
, ARRAY_SIZE(cmd_pxe_sub
));
1627 return cp
->cmd(cmdtp
, flag
, argc
, argv
);
1629 return CMD_RET_USAGE
;
1634 "commands to get and boot from pxe files",
1635 "get - try to retrieve a pxe file using tftp\npxe "
1636 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1641 * Boots a system using a local disk syslinux/extlinux file
1643 * Returns 0 on success, 1 on error.
1645 static int do_sysboot(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
1647 unsigned long pxefile_addr_r
;
1648 struct pxe_menu
*cfg
;
1649 char *pxefile_addr_str
;
1655 if (argc
> 1 && strstr(argv
[1], "-p")) {
1662 return cmd_usage(cmdtp
);
1665 pxefile_addr_str
= from_env("pxefile_addr_r");
1666 if (!pxefile_addr_str
)
1669 pxefile_addr_str
= argv
[4];
1673 filename
= env_get("bootfile");
1676 env_set("bootfile", filename
);
1679 if (strstr(argv
[3], "ext2"))
1680 do_getfile
= do_get_ext2
;
1681 else if (strstr(argv
[3], "fat"))
1682 do_getfile
= do_get_fat
;
1683 else if (strstr(argv
[3], "any"))
1684 do_getfile
= do_get_any
;
1686 printf("Invalid filesystem: %s\n", argv
[3]);
1689 fs_argv
[1] = argv
[1];
1690 fs_argv
[2] = argv
[2];
1692 if (strict_strtoul(pxefile_addr_str
, 16, &pxefile_addr_r
) < 0) {
1693 printf("Invalid pxefile address: %s\n", pxefile_addr_str
);
1697 if (get_pxe_file(cmdtp
, filename
, pxefile_addr_r
) < 0) {
1698 printf("Error reading config file\n");
1702 cfg
= parse_pxefile(cmdtp
, pxefile_addr_r
);
1705 printf("Error parsing config file\n");
1712 handle_pxe_menu(cmdtp
, cfg
);
1714 destroy_pxe_menu(cfg
);
1720 sysboot
, 7, 1, do_sysboot
,
1721 "command to get and boot from syslinux files",
1722 "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1723 " - load and parse syslinux menu file 'filename' from ext2, fat\n"
1724 " or any filesystem on 'dev' on 'interface' to address 'addr'"