]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - cmd/pxe_utils.c
c244bfb10d670f3790095164568d7747a5167a41
1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2010-2011 Calxeda, Inc.
4 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
13 #include <linux/string.h>
14 #include <linux/ctype.h>
16 #include <linux/list.h>
24 #include "pxe_utils.h"
26 #define MAX_TFTP_PATH_LEN 512
31 * Convert an ethaddr from the environment to the format used by pxelinux
32 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
33 * the beginning of the ethernet address to indicate a hardware type of
34 * Ethernet. Also converts uppercase hex characters into lowercase, to match
35 * pxelinux's behavior.
37 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
38 * environment, or some other value < 0 on error.
40 int format_mac_pxe(char *outbuf
, size_t outbuf_len
)
44 if (outbuf_len
< 21) {
45 printf("outbuf is too small (%zd < 21)\n", outbuf_len
);
50 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr
))
53 sprintf(outbuf
, "01-%02x-%02x-%02x-%02x-%02x-%02x",
54 ethaddr
[0], ethaddr
[1], ethaddr
[2],
55 ethaddr
[3], ethaddr
[4], ethaddr
[5]);
61 * Returns the directory the file specified in the bootfile env variable is
62 * in. If bootfile isn't defined in the environment, return NULL, which should
63 * be interpreted as "don't prepend anything to paths".
65 static int get_bootfile_path(const char *file_path
, char *bootfile_path
,
66 size_t bootfile_path_size
)
68 char *bootfile
, *last_slash
;
71 /* Only syslinux allows absolute paths */
72 if (file_path
[0] == '/' && !is_pxe
)
75 bootfile
= from_env("bootfile");
80 last_slash
= strrchr(bootfile
, '/');
85 path_len
= (last_slash
- bootfile
) + 1;
87 if (bootfile_path_size
< path_len
) {
88 printf("bootfile_path too small. (%zd < %zd)\n",
89 bootfile_path_size
, path_len
);
94 strncpy(bootfile_path
, bootfile
, path_len
);
97 bootfile_path
[path_len
] = '\0';
102 int (*do_getfile
)(cmd_tbl_t
*cmdtp
, const char *file_path
, char *file_addr
);
105 * As in pxelinux, paths to files referenced from files we retrieve are
106 * relative to the location of bootfile. get_relfile takes such a path and
107 * joins it with the bootfile path to get the full path to the target file. If
108 * the bootfile path is NULL, we use file_path as is.
110 * Returns 1 for success, or < 0 on error.
112 static int get_relfile(cmd_tbl_t
*cmdtp
, const char *file_path
,
113 unsigned long file_addr
)
116 char relfile
[MAX_TFTP_PATH_LEN
+ 1];
120 err
= get_bootfile_path(file_path
, relfile
, sizeof(relfile
));
125 path_len
= strlen(file_path
);
126 path_len
+= strlen(relfile
);
128 if (path_len
> MAX_TFTP_PATH_LEN
) {
129 printf("Base path too long (%s%s)\n", relfile
, file_path
);
131 return -ENAMETOOLONG
;
134 strcat(relfile
, file_path
);
136 printf("Retrieving file: %s\n", relfile
);
138 sprintf(addr_buf
, "%lx", file_addr
);
140 return do_getfile(cmdtp
, relfile
, addr_buf
);
144 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
145 * 'bootfile' was specified in the environment, the path to bootfile will be
146 * prepended to 'file_path' and the resulting path will be used.
148 * Returns 1 on success, or < 0 for error.
150 int get_pxe_file(cmd_tbl_t
*cmdtp
, const char *file_path
,
151 unsigned long file_addr
)
153 unsigned long config_file_size
;
158 err
= get_relfile(cmdtp
, file_path
, file_addr
);
164 * the file comes without a NUL byte at the end, so find out its size
165 * and add the NUL byte.
167 tftp_filesize
= from_env("filesize");
172 if (strict_strtoul(tftp_filesize
, 16, &config_file_size
) < 0)
175 buf
= map_sysmem(file_addr
+ config_file_size
, 1);
182 #define PXELINUX_DIR "pxelinux.cfg/"
185 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
186 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
187 * from the bootfile path, as described above.
189 * Returns 1 on success or < 0 on error.
191 int get_pxelinux_path(cmd_tbl_t
*cmdtp
, const char *file
,
192 unsigned long pxefile_addr_r
)
194 size_t base_len
= strlen(PXELINUX_DIR
);
195 char path
[MAX_TFTP_PATH_LEN
+ 1];
197 if (base_len
+ strlen(file
) > MAX_TFTP_PATH_LEN
) {
198 printf("path (%s%s) too long, skipping\n",
200 return -ENAMETOOLONG
;
203 sprintf(path
, PXELINUX_DIR
"%s", file
);
205 return get_pxe_file(cmdtp
, path
, pxefile_addr_r
);
209 * Wrapper to make it easier to store the file at file_path in the location
210 * specified by envaddr_name. file_path will be joined to the bootfile path,
211 * if any is specified.
213 * Returns 1 on success or < 0 on error.
215 static int get_relfile_envaddr(cmd_tbl_t
*cmdtp
, const char *file_path
,
216 const char *envaddr_name
)
218 unsigned long file_addr
;
221 envaddr
= from_env(envaddr_name
);
226 if (strict_strtoul(envaddr
, 16, &file_addr
) < 0)
229 return get_relfile(cmdtp
, file_path
, file_addr
);
233 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
234 * result must be free()'d to reclaim the memory.
236 * Returns NULL if malloc fails.
238 static struct pxe_label
*label_create(void)
240 struct pxe_label
*label
;
242 label
= malloc(sizeof(struct pxe_label
));
247 memset(label
, 0, sizeof(struct pxe_label
));
253 * Free the memory used by a pxe_label, including that used by its name,
254 * kernel, append and initrd members, if they're non NULL.
256 * So - be sure to only use dynamically allocated memory for the members of
257 * the pxe_label struct, unless you want to clean it up first. These are
258 * currently only created by the pxe file parsing code.
260 static void label_destroy(struct pxe_label
*label
)
287 * Print a label and its string members if they're defined.
289 * This is passed as a callback to the menu code for displaying each
292 static void label_print(void *data
)
294 struct pxe_label
*label
= data
;
295 const char *c
= label
->menu
? label
->menu
: label
->name
;
297 printf("%s:\t%s\n", label
->num
, c
);
301 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
302 * environment variable is defined. Its contents will be executed as U-Boot
303 * command. If the label specified an 'append' line, its contents will be
304 * used to overwrite the contents of the 'bootargs' environment variable prior
305 * to running 'localcmd'.
307 * Returns 1 on success or < 0 on error.
309 static int label_localboot(struct pxe_label
*label
)
313 localcmd
= from_env("localcmd");
319 char bootargs
[CONFIG_SYS_CBSIZE
];
321 cli_simple_process_macros(label
->append
, bootargs
);
322 env_set("bootargs", bootargs
);
325 debug("running: %s\n", localcmd
);
327 return run_command_list(localcmd
, strlen(localcmd
), 0);
331 * Boot according to the contents of a pxe_label.
333 * If we can't boot for any reason, we return. A successful boot never
336 * The kernel will be stored in the location given by the 'kernel_addr_r'
337 * environment variable.
339 * If the label specifies an initrd file, it will be stored in the location
340 * given by the 'ramdisk_addr_r' environment variable.
342 * If the label specifies an 'append' line, its contents will overwrite that
343 * of the 'bootargs' environment variable.
345 static int label_boot(cmd_tbl_t
*cmdtp
, struct pxe_label
*label
)
347 char *bootm_argv
[] = { "bootm", NULL
, NULL
, NULL
, NULL
};
349 char mac_str
[29] = "";
350 char ip_str
[68] = "";
351 char *fit_addr
= NULL
;
359 label
->attempted
= 1;
361 if (label
->localboot
) {
362 if (label
->localboot_val
>= 0)
363 label_localboot(label
);
367 if (!label
->kernel
) {
368 printf("No kernel given, skipping %s\n",
374 if (get_relfile_envaddr(cmdtp
, label
->initrd
, "ramdisk_addr_r") < 0) {
375 printf("Skipping %s for failure retrieving initrd\n",
380 bootm_argv
[2] = initrd_str
;
381 strncpy(bootm_argv
[2], env_get("ramdisk_addr_r"), 18);
382 strcat(bootm_argv
[2], ":");
383 strncat(bootm_argv
[2], env_get("filesize"), 9);
387 if (get_relfile_envaddr(cmdtp
, label
->kernel
, "kernel_addr_r") < 0) {
388 printf("Skipping %s for failure retrieving kernel\n",
393 if (label
->ipappend
& 0x1) {
394 sprintf(ip_str
, " ip=%s:%s:%s:%s",
395 env_get("ipaddr"), env_get("serverip"),
396 env_get("gatewayip"), env_get("netmask"));
399 #ifdef CONFIG_CMD_NET
400 if (label
->ipappend
& 0x2) {
403 strcpy(mac_str
, " BOOTIF=");
404 err
= format_mac_pxe(mac_str
+ 8, sizeof(mac_str
) - 8);
410 if ((label
->ipappend
& 0x3) || label
->append
) {
411 char bootargs
[CONFIG_SYS_CBSIZE
] = "";
412 char finalbootargs
[CONFIG_SYS_CBSIZE
];
414 if (strlen(label
->append
?: "") +
415 strlen(ip_str
) + strlen(mac_str
) + 1 > sizeof(bootargs
)) {
416 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
417 strlen(label
->append
?: ""),
418 strlen(ip_str
), strlen(mac_str
),
424 strncpy(bootargs
, label
->append
, sizeof(bootargs
));
426 strcat(bootargs
, ip_str
);
427 strcat(bootargs
, mac_str
);
429 cli_simple_process_macros(bootargs
, finalbootargs
);
430 env_set("bootargs", finalbootargs
);
431 printf("append: %s\n", finalbootargs
);
434 bootm_argv
[1] = env_get("kernel_addr_r");
435 /* for FIT, append the configuration identifier */
437 int len
= strlen(bootm_argv
[1]) + strlen(label
->config
) + 1;
439 fit_addr
= malloc(len
);
441 printf("malloc fail (FIT address)\n");
444 snprintf(fit_addr
, len
, "%s%s", bootm_argv
[1], label
->config
);
445 bootm_argv
[1] = fit_addr
;
449 * fdt usage is optional:
450 * It handles the following scenarios. All scenarios are exclusive
452 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
453 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
454 * and adjust argc appropriately.
456 * Scenario 2: If there is an fdt_addr specified, pass it along to
457 * bootm, and adjust argc appropriately.
459 * Scenario 3: fdt blob is not available.
461 bootm_argv
[3] = env_get("fdt_addr_r");
463 /* if fdt label is defined then get fdt from server */
465 char *fdtfile
= NULL
;
466 char *fdtfilefree
= NULL
;
469 fdtfile
= label
->fdt
;
470 } else if (label
->fdtdir
) {
471 char *f1
, *f2
, *f3
, *f4
, *slash
;
473 f1
= env_get("fdtfile");
480 * For complex cases where this code doesn't
481 * generate the correct filename, the board
482 * code should set $fdtfile during early boot,
483 * or the boot scripts should set $fdtfile
484 * before invoking "pxe" or "sysboot".
488 f3
= env_get("board");
492 len
= strlen(label
->fdtdir
);
495 else if (label
->fdtdir
[len
- 1] != '/')
500 len
= strlen(label
->fdtdir
) + strlen(slash
) +
501 strlen(f1
) + strlen(f2
) + strlen(f3
) +
503 fdtfilefree
= malloc(len
);
505 printf("malloc fail (FDT filename)\n");
509 snprintf(fdtfilefree
, len
, "%s%s%s%s%s%s",
510 label
->fdtdir
, slash
, f1
, f2
, f3
, f4
);
511 fdtfile
= fdtfilefree
;
515 int err
= get_relfile_envaddr(cmdtp
, fdtfile
,
520 printf("Skipping %s for failure retrieving fdt\n",
525 bootm_argv
[3] = NULL
;
530 bootm_argv
[3] = env_get("fdt_addr");
538 kernel_addr
= genimg_get_kernel_addr(bootm_argv
[1]);
539 buf
= map_sysmem(kernel_addr
, 0);
540 /* Try bootm for legacy and FIT format image */
541 if (genimg_get_format(buf
) != IMAGE_FORMAT_INVALID
)
542 do_bootm(cmdtp
, 0, bootm_argc
, bootm_argv
);
543 #ifdef CONFIG_CMD_BOOTI
544 /* Try booting an AArch64 Linux kernel image */
546 do_booti(cmdtp
, 0, bootm_argc
, bootm_argv
);
547 #elif defined(CONFIG_CMD_BOOTZ)
548 /* Try booting a Image */
550 do_bootz(cmdtp
, 0, bootm_argc
, bootm_argv
);
561 * Tokens for the pxe file parser.
588 * A token - given by a value and a type.
592 enum token_type type
;
596 * Keywords recognized.
598 static const struct token keywords
[] = {
601 {"timeout", T_TIMEOUT
},
602 {"default", T_DEFAULT
},
603 {"prompt", T_PROMPT
},
605 {"kernel", T_KERNEL
},
607 {"localboot", T_LOCALBOOT
},
608 {"append", T_APPEND
},
609 {"initrd", T_INITRD
},
610 {"include", T_INCLUDE
},
611 {"devicetree", T_FDT
},
613 {"devicetreedir", T_FDTDIR
},
614 {"fdtdir", T_FDTDIR
},
615 {"ontimeout", T_ONTIMEOUT
,},
616 {"ipappend", T_IPAPPEND
,},
617 {"background", T_BACKGROUND
,},
622 * Since pxe(linux) files don't have a token to identify the start of a
623 * literal, we have to keep track of when we're in a state where a literal is
624 * expected vs when we're in a state a keyword is expected.
633 * get_string retrieves a string from *p and stores it as a token in
636 * get_string used for scanning both string literals and keywords.
638 * Characters from *p are copied into t-val until a character equal to
639 * delim is found, or a NUL byte is reached. If delim has the special value of
640 * ' ', any whitespace character will be used as a delimiter.
642 * If lower is unequal to 0, uppercase characters will be converted to
643 * lowercase in the result. This is useful to make keywords case
646 * The location of *p is updated to point to the first character after the end
647 * of the token - the ending delimiter.
649 * On success, the new value of t->val is returned. Memory for t->val is
650 * allocated using malloc and must be free()'d to reclaim it. If insufficient
651 * memory is available, NULL is returned.
653 static char *get_string(char **p
, struct token
*t
, char delim
, int lower
)
659 * b and e both start at the beginning of the input stream.
661 * e is incremented until we find the ending delimiter, or a NUL byte
662 * is reached. Then, we take e - b to find the length of the token.
668 if ((delim
== ' ' && isspace(*e
)) || delim
== *e
)
676 * Allocate memory to hold the string, and copy it in, converting
677 * characters to lowercase if lower is != 0.
679 t
->val
= malloc(len
+ 1);
683 for (i
= 0; i
< len
; i
++, b
++) {
685 t
->val
[i
] = tolower(*b
);
693 * Update *p so the caller knows where to continue scanning.
703 * Populate a keyword token with a type and value.
705 static void get_keyword(struct token
*t
)
709 for (i
= 0; keywords
[i
].val
; i
++) {
710 if (!strcmp(t
->val
, keywords
[i
].val
)) {
711 t
->type
= keywords
[i
].type
;
718 * Get the next token. We have to keep track of which state we're in to know
719 * if we're looking to get a string literal or a keyword.
721 * *p is updated to point at the first character after the current token.
723 static void get_token(char **p
, struct token
*t
, enum lex_state state
)
729 /* eat non EOL whitespace */
734 * eat comments. note that string literals can't begin with #, but
735 * can contain a # after their first character.
738 while (*c
&& *c
!= '\n')
745 } else if (*c
== '\0') {
748 } else if (state
== L_SLITERAL
) {
749 get_string(&c
, t
, '\n', 0);
750 } else if (state
== L_KEYWORD
) {
752 * when we expect a keyword, we first get the next string
753 * token delimited by whitespace, and then check if it
754 * matches a keyword in our keyword list. if it does, it's
755 * converted to a keyword token of the appropriate type, and
756 * if not, it remains a string token.
758 get_string(&c
, t
, ' ', 1);
766 * Increment *c until we get to the end of the current line, or EOF.
768 static void eol_or_eof(char **c
)
770 while (**c
&& **c
!= '\n')
775 * All of these parse_* functions share some common behavior.
777 * They finish with *c pointing after the token they parse, and return 1 on
778 * success, or < 0 on error.
782 * Parse a string literal and store a pointer it at *dst. String literals
783 * terminate at the end of the line.
785 static int parse_sliteral(char **c
, char **dst
)
790 get_token(c
, &t
, L_SLITERAL
);
792 if (t
.type
!= T_STRING
) {
793 printf("Expected string literal: %.*s\n", (int)(*c
- s
), s
);
803 * Parse a base 10 (unsigned) integer and store it at *dst.
805 static int parse_integer(char **c
, int *dst
)
810 get_token(c
, &t
, L_SLITERAL
);
812 if (t
.type
!= T_STRING
) {
813 printf("Expected string: %.*s\n", (int)(*c
- s
), s
);
817 *dst
= simple_strtol(t
.val
, NULL
, 10);
824 static int parse_pxefile_top(cmd_tbl_t
*cmdtp
, char *p
, unsigned long base
,
825 struct pxe_menu
*cfg
, int nest_level
);
828 * Parse an include statement, and retrieve and parse the file it mentions.
830 * base should point to a location where it's safe to store the file, and
831 * nest_level should indicate how many nested includes have occurred. For this
832 * include, nest_level has already been incremented and doesn't need to be
835 static int handle_include(cmd_tbl_t
*cmdtp
, char **c
, unsigned long base
,
836 struct pxe_menu
*cfg
, int nest_level
)
844 err
= parse_sliteral(c
, &include_path
);
847 printf("Expected include path: %.*s\n", (int)(*c
- s
), s
);
851 err
= get_pxe_file(cmdtp
, include_path
, base
);
854 printf("Couldn't retrieve %s\n", include_path
);
858 buf
= map_sysmem(base
, 0);
859 ret
= parse_pxefile_top(cmdtp
, buf
, base
, cfg
, nest_level
);
866 * Parse lines that begin with 'menu'.
868 * base and nest are provided to handle the 'menu include' case.
870 * base should point to a location where it's safe to store the included file.
872 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
873 * a file it includes, 3 when parsing a file included by that file, and so on.
875 static int parse_menu(cmd_tbl_t
*cmdtp
, char **c
, struct pxe_menu
*cfg
,
876 unsigned long base
, int nest_level
)
882 get_token(c
, &t
, L_KEYWORD
);
886 err
= parse_sliteral(c
, &cfg
->title
);
891 err
= handle_include(cmdtp
, c
, base
, cfg
, nest_level
+ 1);
895 err
= parse_sliteral(c
, &cfg
->bmp
);
899 printf("Ignoring malformed menu command: %.*s\n",
912 * Handles parsing a 'menu line' when we're parsing a label.
914 static int parse_label_menu(char **c
, struct pxe_menu
*cfg
,
915 struct pxe_label
*label
)
922 get_token(c
, &t
, L_KEYWORD
);
926 if (!cfg
->default_label
)
927 cfg
->default_label
= strdup(label
->name
);
929 if (!cfg
->default_label
)
934 parse_sliteral(c
, &label
->menu
);
937 printf("Ignoring malformed menu command: %.*s\n",
947 * Handles parsing a 'kernel' label.
948 * expecting "filename" or "<fit_filename>#cfg"
950 static int parse_label_kernel(char **c
, struct pxe_label
*label
)
955 err
= parse_sliteral(c
, &label
->kernel
);
959 s
= strstr(label
->kernel
, "#");
963 label
->config
= malloc(strlen(s
) + 1);
967 strcpy(label
->config
, s
);
974 * Parses a label and adds it to the list of labels for a menu.
976 * A label ends when we either get to the end of a file, or
977 * get some input we otherwise don't have a handler defined
981 static int parse_label(char **c
, struct pxe_menu
*cfg
)
986 struct pxe_label
*label
;
989 label
= label_create();
993 err
= parse_sliteral(c
, &label
->name
);
995 printf("Expected label name: %.*s\n", (int)(*c
- s
), s
);
996 label_destroy(label
);
1000 list_add_tail(&label
->list
, &cfg
->labels
);
1004 get_token(c
, &t
, L_KEYWORD
);
1009 err
= parse_label_menu(c
, cfg
, label
);
1014 err
= parse_label_kernel(c
, label
);
1018 err
= parse_sliteral(c
, &label
->append
);
1021 s
= strstr(label
->append
, "initrd=");
1025 len
= (int)(strchr(s
, ' ') - s
);
1026 label
->initrd
= malloc(len
+ 1);
1027 strncpy(label
->initrd
, s
, len
);
1028 label
->initrd
[len
] = '\0';
1034 err
= parse_sliteral(c
, &label
->initrd
);
1039 err
= parse_sliteral(c
, &label
->fdt
);
1044 err
= parse_sliteral(c
, &label
->fdtdir
);
1048 label
->localboot
= 1;
1049 err
= parse_integer(c
, &label
->localboot_val
);
1053 err
= parse_integer(c
, &label
->ipappend
);
1061 * put the token back! we don't want it - it's the end
1062 * of a label and whatever token this is, it's
1063 * something for the menu level context to handle.
1075 * This 16 comes from the limit pxelinux imposes on nested includes.
1077 * There is no reason at all we couldn't do more, but some limit helps prevent
1078 * infinite (until crash occurs) recursion if a file tries to include itself.
1080 #define MAX_NEST_LEVEL 16
1083 * Entry point for parsing a menu file. nest_level indicates how many times
1084 * we've nested in includes. It will be 1 for the top level menu file.
1086 * Returns 1 on success, < 0 on error.
1088 static int parse_pxefile_top(cmd_tbl_t
*cmdtp
, char *p
, unsigned long base
,
1089 struct pxe_menu
*cfg
, int nest_level
)
1092 char *s
, *b
, *label_name
;
1097 if (nest_level
> MAX_NEST_LEVEL
) {
1098 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL
);
1105 get_token(&p
, &t
, L_KEYWORD
);
1111 err
= parse_menu(cmdtp
, &p
, cfg
,
1112 base
+ ALIGN(strlen(b
) + 1, 4),
1117 err
= parse_integer(&p
, &cfg
->timeout
);
1121 err
= parse_label(&p
, cfg
);
1126 err
= parse_sliteral(&p
, &label_name
);
1129 if (cfg
->default_label
)
1130 free(cfg
->default_label
);
1132 cfg
->default_label
= label_name
;
1138 err
= handle_include(cmdtp
, &p
,
1139 base
+ ALIGN(strlen(b
), 4), cfg
,
1154 printf("Ignoring unknown command: %.*s\n",
1165 * Free the memory used by a pxe_menu and its labels.
1167 void destroy_pxe_menu(struct pxe_menu
*cfg
)
1169 struct list_head
*pos
, *n
;
1170 struct pxe_label
*label
;
1175 if (cfg
->default_label
)
1176 free(cfg
->default_label
);
1178 list_for_each_safe(pos
, n
, &cfg
->labels
) {
1179 label
= list_entry(pos
, struct pxe_label
, list
);
1181 label_destroy(label
);
1188 * Entry point for parsing a pxe file. This is only used for the top level
1191 * Returns NULL if there is an error, otherwise, returns a pointer to a
1192 * pxe_menu struct populated with the results of parsing the pxe file (and any
1193 * files it includes). The resulting pxe_menu struct can be free()'d by using
1194 * the destroy_pxe_menu() function.
1196 struct pxe_menu
*parse_pxefile(cmd_tbl_t
*cmdtp
, unsigned long menucfg
)
1198 struct pxe_menu
*cfg
;
1202 cfg
= malloc(sizeof(struct pxe_menu
));
1207 memset(cfg
, 0, sizeof(struct pxe_menu
));
1209 INIT_LIST_HEAD(&cfg
->labels
);
1211 buf
= map_sysmem(menucfg
, 0);
1212 r
= parse_pxefile_top(cmdtp
, buf
, menucfg
, cfg
, 1);
1216 destroy_pxe_menu(cfg
);
1224 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1227 static struct menu
*pxe_menu_to_menu(struct pxe_menu
*cfg
)
1229 struct pxe_label
*label
;
1230 struct list_head
*pos
;
1234 char *default_num
= NULL
;
1237 * Create a menu and add items for all the labels.
1239 m
= menu_create(cfg
->title
, DIV_ROUND_UP(cfg
->timeout
, 10),
1240 cfg
->prompt
, NULL
, label_print
, NULL
, NULL
);
1245 list_for_each(pos
, &cfg
->labels
) {
1246 label
= list_entry(pos
, struct pxe_label
, list
);
1248 sprintf(label
->num
, "%d", i
++);
1249 if (menu_item_add(m
, label
->num
, label
) != 1) {
1253 if (cfg
->default_label
&&
1254 (strcmp(label
->name
, cfg
->default_label
) == 0))
1255 default_num
= label
->num
;
1259 * After we've created items for each label in the menu, set the
1260 * menu's default label if one was specified.
1263 err
= menu_default_set(m
, default_num
);
1265 if (err
!= -ENOENT
) {
1270 printf("Missing default: %s\n", cfg
->default_label
);
1278 * Try to boot any labels we have yet to attempt to boot.
1280 static void boot_unattempted_labels(cmd_tbl_t
*cmdtp
, struct pxe_menu
*cfg
)
1282 struct list_head
*pos
;
1283 struct pxe_label
*label
;
1285 list_for_each(pos
, &cfg
->labels
) {
1286 label
= list_entry(pos
, struct pxe_label
, list
);
1288 if (!label
->attempted
)
1289 label_boot(cmdtp
, label
);
1294 * Boot the system as prescribed by a pxe_menu.
1296 * Use the menu system to either get the user's choice or the default, based
1297 * on config or user input. If there is no default or user's choice,
1298 * attempted to boot labels in the order they were given in pxe files.
1299 * If the default or user's choice fails to boot, attempt to boot other
1300 * labels in the order they were given in pxe files.
1302 * If this function returns, there weren't any labels that successfully
1303 * booted, or the user interrupted the menu selection via ctrl+c.
1305 void handle_pxe_menu(cmd_tbl_t
*cmdtp
, struct pxe_menu
*cfg
)
1311 #ifdef CONFIG_CMD_BMP
1312 /* display BMP if available */
1314 if (get_relfile(cmdtp
, cfg
->bmp
, image_load_addr
)) {
1315 if (CONFIG_IS_ENABLED(CMD_CLS
))
1316 run_command("cls", 0);
1317 bmp_display(image_load_addr
,
1318 BMP_ALIGN_CENTER
, BMP_ALIGN_CENTER
);
1320 printf("Skipping background bmp %s for failure\n",
1326 m
= pxe_menu_to_menu(cfg
);
1330 err
= menu_get_choice(m
, &choice
);
1335 * err == 1 means we got a choice back from menu_get_choice.
1337 * err == -ENOENT if the menu was setup to select the default but no
1338 * default was set. in that case, we should continue trying to boot
1339 * labels that haven't been attempted yet.
1341 * otherwise, the user interrupted or there was some other error and
1346 err
= label_boot(cmdtp
, choice
);
1349 } else if (err
!= -ENOENT
) {
1353 boot_unattempted_labels(cmdtp
, cfg
);