2 * Copyright 2010-2011 Calxeda, Inc.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/string.h>
21 #include <linux/ctype.h>
23 #include <linux/list.h>
27 #define MAX_TFTP_PATH_LEN 127
30 * Like getenv, but prints an error if envvar isn't defined in the
31 * environment. It always returns what getenv does, so it can be used in
32 * place of getenv without changing error handling otherwise.
34 static char *from_env(char *envvar)
41 printf("missing environment variable: %s\n", envvar);
47 * Convert an ethaddr from the environment to the format used by pxelinux
48 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
49 * the beginning of the ethernet address to indicate a hardware type of
50 * Ethernet. Also converts uppercase hex characters into lowercase, to match
51 * pxelinux's behavior.
53 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
54 * environment, or some other value < 0 on error.
56 static int format_mac_pxe(char *outbuf, size_t outbuf_len)
61 ethaddr = from_env("ethaddr");
66 ethaddr_len = strlen(ethaddr);
69 * ethaddr_len + 4 gives room for "01-", ethaddr, and a NUL byte at
72 if (outbuf_len < ethaddr_len + 4) {
73 printf("outbuf is too small (%d < %d)\n",
74 outbuf_len, ethaddr_len + 4);
79 strcpy(outbuf, "01-");
81 for (p = outbuf + 3; *ethaddr; ethaddr++, p++) {
85 *p = tolower(*ethaddr);
94 * Returns the directory the file specified in the bootfile env variable is
95 * in. If bootfile isn't defined in the environment, return NULL, which should
96 * be interpreted as "don't prepend anything to paths".
98 static int get_bootfile_path(const char *file_path, char *bootfile_path,
99 size_t bootfile_path_size)
101 char *bootfile, *last_slash;
104 if (file_path[0] == '/')
107 bootfile = from_env("bootfile");
112 last_slash = strrchr(bootfile, '/');
114 if (last_slash == NULL)
117 path_len = (last_slash - bootfile) + 1;
119 if (bootfile_path_size < path_len) {
120 printf("bootfile_path too small. (%d < %d)\n",
121 bootfile_path_size, path_len);
126 strncpy(bootfile_path, bootfile, path_len);
129 bootfile_path[path_len] = '\0';
134 static int (*do_getfile)(char *file_path, char *file_addr);
136 static int do_get_tftp(char *file_path, char *file_addr)
138 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
140 tftp_argv[1] = file_addr;
141 tftp_argv[2] = file_path;
143 if (do_tftpb(NULL, 0, 3, tftp_argv))
149 static char *fs_argv[5];
151 static int do_get_ext2(char *file_path, char *file_addr)
153 #ifdef CONFIG_CMD_EXT2
154 fs_argv[0] = "ext2load";
155 fs_argv[3] = file_addr;
156 fs_argv[4] = file_path;
158 if (!do_ext2load(NULL, 0, 5, fs_argv))
164 static int do_get_fat(char *file_path, char *file_addr)
166 #ifdef CONFIG_CMD_FAT
167 fs_argv[0] = "fatload";
168 fs_argv[3] = file_addr;
169 fs_argv[4] = file_path;
171 if (!do_fat_fsload(NULL, 0, 5, fs_argv))
178 * As in pxelinux, paths to files referenced from files we retrieve are
179 * relative to the location of bootfile. get_relfile takes such a path and
180 * joins it with the bootfile path to get the full path to the target file. If
181 * the bootfile path is NULL, we use file_path as is.
183 * Returns 1 for success, or < 0 on error.
185 static int get_relfile(char *file_path, void *file_addr)
188 char relfile[MAX_TFTP_PATH_LEN+1];
192 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
197 path_len = strlen(file_path);
198 path_len += strlen(relfile);
200 if (path_len > MAX_TFTP_PATH_LEN) {
201 printf("Base path too long (%s%s)\n",
205 return -ENAMETOOLONG;
208 strcat(relfile, file_path);
210 printf("Retrieving file: %s\n", relfile);
212 sprintf(addr_buf, "%p", file_addr);
214 return do_getfile(relfile, addr_buf);
218 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
219 * 'bootfile' was specified in the environment, the path to bootfile will be
220 * prepended to 'file_path' and the resulting path will be used.
222 * Returns 1 on success, or < 0 for error.
224 static int get_pxe_file(char *file_path, void *file_addr)
226 unsigned long config_file_size;
230 err = get_relfile(file_path, file_addr);
236 * the file comes without a NUL byte at the end, so find out its size
237 * and add the NUL byte.
239 tftp_filesize = from_env("filesize");
244 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
247 *(char *)(file_addr + config_file_size) = '\0';
252 #define PXELINUX_DIR "pxelinux.cfg/"
255 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
256 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
257 * from the bootfile path, as described above.
259 * Returns 1 on success or < 0 on error.
261 static int get_pxelinux_path(char *file, void *pxefile_addr_r)
263 size_t base_len = strlen(PXELINUX_DIR);
264 char path[MAX_TFTP_PATH_LEN+1];
266 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
267 printf("path (%s%s) too long, skipping\n",
269 return -ENAMETOOLONG;
272 sprintf(path, PXELINUX_DIR "%s", file);
274 return get_pxe_file(path, pxefile_addr_r);
278 * Looks for a pxe file with a name based on the pxeuuid environment variable.
280 * Returns 1 on success or < 0 on error.
282 static int pxe_uuid_path(void *pxefile_addr_r)
286 uuid_str = from_env("pxeuuid");
291 return get_pxelinux_path(uuid_str, pxefile_addr_r);
295 * Looks for a pxe file with a name based on the 'ethaddr' environment
298 * Returns 1 on success or < 0 on error.
300 static int pxe_mac_path(void *pxefile_addr_r)
305 err = format_mac_pxe(mac_str, sizeof(mac_str));
310 return get_pxelinux_path(mac_str, pxefile_addr_r);
314 * Looks for pxe files with names based on our IP address. See pxelinux
315 * documentation for details on what these file names look like. We match
318 * Returns 1 on success or < 0 on error.
320 static int pxe_ipaddr_paths(void *pxefile_addr_r)
325 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
327 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
328 err = get_pxelinux_path(ip_addr, pxefile_addr_r);
333 ip_addr[mask_pos] = '\0';
340 * Entry point for the 'pxe get' command.
341 * This Follows pxelinux's rules to download a config file from a tftp server.
342 * The file is stored at the location given by the pxefile_addr_r environment
343 * variable, which must be set.
345 * UUID comes from pxeuuid env variable, if defined
346 * MAC addr comes from ethaddr env variable, if defined
349 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
351 * Returns 0 on success or 1 on error.
354 do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
356 char *pxefile_addr_str;
357 unsigned long pxefile_addr_r;
360 do_getfile = do_get_tftp;
363 return CMD_RET_USAGE;
365 pxefile_addr_str = from_env("pxefile_addr_r");
367 if (!pxefile_addr_str)
370 err = strict_strtoul(pxefile_addr_str, 16,
371 (unsigned long *)&pxefile_addr_r);
376 * Keep trying paths until we successfully get a file we're looking
379 if (pxe_uuid_path((void *)pxefile_addr_r) > 0
380 || pxe_mac_path((void *)pxefile_addr_r) > 0
381 || pxe_ipaddr_paths((void *)pxefile_addr_r) > 0
382 || get_pxelinux_path("default", (void *)pxefile_addr_r) > 0) {
384 printf("Config file found\n");
389 printf("Config file not found\n");
395 * Wrapper to make it easier to store the file at file_path in the location
396 * specified by envaddr_name. file_path will be joined to the bootfile path,
397 * if any is specified.
399 * Returns 1 on success or < 0 on error.
401 static int get_relfile_envaddr(char *file_path, char *envaddr_name)
403 unsigned long file_addr;
406 envaddr = from_env(envaddr_name);
411 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
414 return get_relfile(file_path, (void *)file_addr);
418 * A note on the pxe file parser.
420 * We're parsing files that use syslinux grammar, which has a few quirks.
421 * String literals must be recognized based on context - there is no
422 * quoting or escaping support. There's also nothing to explicitly indicate
423 * when a label section completes. We deal with that by ending a label
424 * section whenever we see a line that doesn't include.
426 * As with the syslinux family, this same file format could be reused in the
427 * future for non pxe purposes. The only action it takes during parsing that
428 * would throw this off is handling of include files. It assumes we're using
429 * pxe, and does a tftp download of a file listed as an include file in the
430 * middle of the parsing operation. That could be handled by refactoring it to
431 * take a 'include file getter' function.
435 * Describes a single label given in a pxe file.
437 * Create these with the 'label_create' function given below.
439 * name - the name of the menu as given on the 'menu label' line.
440 * kernel - the path to the kernel file to use for this label.
441 * append - kernel command line to use when booting this label
442 * initrd - path to the initrd to use for this label.
443 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
444 * localboot - 1 if this label specified 'localboot', 0 otherwise.
445 * list - lets these form a list, which a pxe_menu struct will hold.
456 struct list_head list;
460 * Describes a pxe menu as given via pxe files.
462 * title - the name of the menu as given by a 'menu title' line.
463 * default_label - the name of the default label, if any.
464 * timeout - time in tenths of a second to wait for a user key-press before
465 * booting the default label.
466 * prompt - if 0, don't prompt for a choice unless the timeout period is
467 * interrupted. If 1, always prompt for a choice regardless of
469 * labels - a list of labels defined for the menu.
476 struct list_head labels;
480 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
481 * result must be free()'d to reclaim the memory.
483 * Returns NULL if malloc fails.
485 static struct pxe_label *label_create(void)
487 struct pxe_label *label;
489 label = malloc(sizeof(struct pxe_label));
494 memset(label, 0, sizeof(struct pxe_label));
500 * Free the memory used by a pxe_label, including that used by its name,
501 * kernel, append and initrd members, if they're non NULL.
503 * So - be sure to only use dynamically allocated memory for the members of
504 * the pxe_label struct, unless you want to clean it up first. These are
505 * currently only created by the pxe file parsing code.
507 static void label_destroy(struct pxe_label *label)
528 * Print a label and its string members if they're defined.
530 * This is passed as a callback to the menu code for displaying each
533 static void label_print(void *data)
535 struct pxe_label *label = data;
536 const char *c = label->menu ? label->menu : label->kernel;
538 printf("%s:\t%s\n", label->name, c);
541 printf("\t\tkernel: %s\n", label->kernel);
544 printf("\t\tappend: %s\n", label->append);
547 printf("\t\tinitrd: %s\n", label->initrd);
550 printf("\tfdt: %s\n", label->fdt);
554 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
555 * environment variable is defined. Its contents will be executed as U-boot
556 * command. If the label specified an 'append' line, its contents will be
557 * used to overwrite the contents of the 'bootargs' environment variable prior
558 * to running 'localcmd'.
560 * Returns 1 on success or < 0 on error.
562 static int label_localboot(struct pxe_label *label)
566 localcmd = from_env("localcmd");
572 setenv("bootargs", label->append);
574 debug("running: %s\n", localcmd);
576 return run_command_list(localcmd, strlen(localcmd), 0);
580 * Boot according to the contents of a pxe_label.
582 * If we can't boot for any reason, we return. A successful boot never
585 * The kernel will be stored in the location given by the 'kernel_addr_r'
586 * environment variable.
588 * If the label specifies an initrd file, it will be stored in the location
589 * given by the 'ramdisk_addr_r' environment variable.
591 * If the label specifies an 'append' line, its contents will overwrite that
592 * of the 'bootargs' environment variable.
594 static void label_boot(struct pxe_label *label)
596 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
601 label->attempted = 1;
603 if (label->localboot) {
604 label_localboot(label);
608 if (label->kernel == NULL) {
609 printf("No kernel given, skipping %s\n",
615 if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) {
616 printf("Skipping %s for failure retrieving initrd\n",
621 bootm_argv[2] = getenv("ramdisk_addr_r");
626 if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) {
627 printf("Skipping %s for failure retrieving kernel\n",
633 setenv("bootargs", label->append);
635 bootm_argv[1] = getenv("kernel_addr_r");
638 * fdt usage is optional:
639 * It handles the following scenarios. All scenarios are exclusive
641 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
642 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
643 * and adjust argc appropriately.
645 * Scenario 2: If there is an fdt_addr specified, pass it along to
646 * bootm, and adjust argc appropriately.
648 * Scenario 3: fdt blob is not available.
650 bootm_argv[3] = getenv("fdt_addr_r");
652 /* if fdt label is defined then get fdt from server */
653 if (bootm_argv[3] && label->fdt) {
654 if (get_relfile_envaddr(label->fdt, "fdt_addr_r") < 0) {
655 printf("Skipping %s for failure retrieving fdt\n",
660 bootm_argv[3] = getenv("fdt_addr");
665 do_bootm(NULL, 0, bootm_argc, bootm_argv);
669 * Tokens for the pxe file parser.
692 * A token - given by a value and a type.
696 enum token_type type;
700 * Keywords recognized.
702 static const struct token keywords[] = {
705 {"timeout", T_TIMEOUT},
706 {"default", T_DEFAULT},
707 {"prompt", T_PROMPT},
709 {"kernel", T_KERNEL},
711 {"localboot", T_LOCALBOOT},
712 {"append", T_APPEND},
713 {"initrd", T_INITRD},
714 {"include", T_INCLUDE},
720 * Since pxe(linux) files don't have a token to identify the start of a
721 * literal, we have to keep track of when we're in a state where a literal is
722 * expected vs when we're in a state a keyword is expected.
731 * get_string retrieves a string from *p and stores it as a token in
734 * get_string used for scanning both string literals and keywords.
736 * Characters from *p are copied into t-val until a character equal to
737 * delim is found, or a NUL byte is reached. If delim has the special value of
738 * ' ', any whitespace character will be used as a delimiter.
740 * If lower is unequal to 0, uppercase characters will be converted to
741 * lowercase in the result. This is useful to make keywords case
744 * The location of *p is updated to point to the first character after the end
745 * of the token - the ending delimiter.
747 * On success, the new value of t->val is returned. Memory for t->val is
748 * allocated using malloc and must be free()'d to reclaim it. If insufficient
749 * memory is available, NULL is returned.
751 static char *get_string(char **p, struct token *t, char delim, int lower)
757 * b and e both start at the beginning of the input stream.
759 * e is incremented until we find the ending delimiter, or a NUL byte
760 * is reached. Then, we take e - b to find the length of the token.
765 if ((delim == ' ' && isspace(*e)) || delim == *e)
773 * Allocate memory to hold the string, and copy it in, converting
774 * characters to lowercase if lower is != 0.
776 t->val = malloc(len + 1);
780 for (i = 0; i < len; i++, b++) {
782 t->val[i] = tolower(*b);
790 * Update *p so the caller knows where to continue scanning.
800 * Populate a keyword token with a type and value.
802 static void get_keyword(struct token *t)
806 for (i = 0; keywords[i].val; i++) {
807 if (!strcmp(t->val, keywords[i].val)) {
808 t->type = keywords[i].type;
815 * Get the next token. We have to keep track of which state we're in to know
816 * if we're looking to get a string literal or a keyword.
818 * *p is updated to point at the first character after the current token.
820 static void get_token(char **p, struct token *t, enum lex_state state)
826 /* eat non EOL whitespace */
831 * eat comments. note that string literals can't begin with #, but
832 * can contain a # after their first character.
835 while (*c && *c != '\n')
842 } else if (*c == '\0') {
845 } else if (state == L_SLITERAL) {
846 get_string(&c, t, '\n', 0);
847 } else if (state == L_KEYWORD) {
849 * when we expect a keyword, we first get the next string
850 * token delimited by whitespace, and then check if it
851 * matches a keyword in our keyword list. if it does, it's
852 * converted to a keyword token of the appropriate type, and
853 * if not, it remains a string token.
855 get_string(&c, t, ' ', 1);
863 * Increment *c until we get to the end of the current line, or EOF.
865 static void eol_or_eof(char **c)
867 while (**c && **c != '\n')
872 * All of these parse_* functions share some common behavior.
874 * They finish with *c pointing after the token they parse, and return 1 on
875 * success, or < 0 on error.
879 * Parse a string literal and store a pointer it at *dst. String literals
880 * terminate at the end of the line.
882 static int parse_sliteral(char **c, char **dst)
887 get_token(c, &t, L_SLITERAL);
889 if (t.type != T_STRING) {
890 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
900 * Parse a base 10 (unsigned) integer and store it at *dst.
902 static int parse_integer(char **c, int *dst)
908 get_token(c, &t, L_SLITERAL);
910 if (t.type != T_STRING) {
911 printf("Expected string: %.*s\n", (int)(*c - s), s);
915 if (strict_strtoul(t.val, 10, &temp) < 0) {
916 printf("Expected unsigned integer: %s\n", t.val);
927 static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
930 * Parse an include statement, and retrieve and parse the file it mentions.
932 * base should point to a location where it's safe to store the file, and
933 * nest_level should indicate how many nested includes have occurred. For this
934 * include, nest_level has already been incremented and doesn't need to be
937 static int handle_include(char **c, char *base,
938 struct pxe_menu *cfg, int nest_level)
944 err = parse_sliteral(c, &include_path);
947 printf("Expected include path: %.*s\n",
952 err = get_pxe_file(include_path, base);
955 printf("Couldn't retrieve %s\n", include_path);
959 return parse_pxefile_top(base, cfg, nest_level);
963 * Parse lines that begin with 'menu'.
965 * b and nest are provided to handle the 'menu include' case.
967 * b should be the address where the file currently being parsed is stored.
969 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
970 * a file it includes, 3 when parsing a file included by that file, and so on.
972 static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
978 get_token(c, &t, L_KEYWORD);
982 err = parse_sliteral(c, &cfg->title);
987 err = handle_include(c, b + strlen(b) + 1, cfg,
992 printf("Ignoring malformed menu command: %.*s\n",
1005 * Handles parsing a 'menu line' when we're parsing a label.
1007 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1008 struct pxe_label *label)
1015 get_token(c, &t, L_KEYWORD);
1019 if (cfg->default_label)
1020 free(cfg->default_label);
1022 cfg->default_label = strdup(label->name);
1024 if (!cfg->default_label)
1029 parse_sliteral(c, &label->menu);
1032 printf("Ignoring malformed menu command: %.*s\n",
1042 * Parses a label and adds it to the list of labels for a menu.
1044 * A label ends when we either get to the end of a file, or
1045 * get some input we otherwise don't have a handler defined
1049 static int parse_label(char **c, struct pxe_menu *cfg)
1054 struct pxe_label *label;
1057 label = label_create();
1061 err = parse_sliteral(c, &label->name);
1063 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1064 label_destroy(label);
1068 list_add_tail(&label->list, &cfg->labels);
1072 get_token(c, &t, L_KEYWORD);
1077 err = parse_label_menu(c, cfg, label);
1082 err = parse_sliteral(c, &label->kernel);
1086 err = parse_sliteral(c, &label->append);
1089 s = strstr(label->append, "initrd=");
1093 len = (int)(strchr(s, ' ') - s);
1094 label->initrd = malloc(len + 1);
1095 strncpy(label->initrd, s, len);
1096 label->initrd[len] = '\0';
1102 err = parse_sliteral(c, &label->initrd);
1107 err = parse_sliteral(c, &label->fdt);
1111 err = parse_integer(c, &label->localboot);
1119 * put the token back! we don't want it - it's the end
1120 * of a label and whatever token this is, it's
1121 * something for the menu level context to handle.
1133 * This 16 comes from the limit pxelinux imposes on nested includes.
1135 * There is no reason at all we couldn't do more, but some limit helps prevent
1136 * infinite (until crash occurs) recursion if a file tries to include itself.
1138 #define MAX_NEST_LEVEL 16
1141 * Entry point for parsing a menu file. nest_level indicates how many times
1142 * we've nested in includes. It will be 1 for the top level menu file.
1144 * Returns 1 on success, < 0 on error.
1146 static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level)
1149 char *s, *b, *label_name;
1154 if (nest_level > MAX_NEST_LEVEL) {
1155 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1162 get_token(&p, &t, L_KEYWORD);
1167 err = parse_menu(&p, cfg, b, nest_level);
1171 err = parse_integer(&p, &cfg->timeout);
1175 err = parse_label(&p, cfg);
1179 err = parse_sliteral(&p, &label_name);
1182 if (cfg->default_label)
1183 free(cfg->default_label);
1185 cfg->default_label = label_name;
1191 err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg,
1196 err = parse_integer(&p, &cfg->prompt);
1206 printf("Ignoring unknown command: %.*s\n",
1217 * Free the memory used by a pxe_menu and its labels.
1219 static void destroy_pxe_menu(struct pxe_menu *cfg)
1221 struct list_head *pos, *n;
1222 struct pxe_label *label;
1227 if (cfg->default_label)
1228 free(cfg->default_label);
1230 list_for_each_safe(pos, n, &cfg->labels) {
1231 label = list_entry(pos, struct pxe_label, list);
1233 label_destroy(label);
1240 * Entry point for parsing a pxe file. This is only used for the top level
1243 * Returns NULL if there is an error, otherwise, returns a pointer to a
1244 * pxe_menu struct populated with the results of parsing the pxe file (and any
1245 * files it includes). The resulting pxe_menu struct can be free()'d by using
1246 * the destroy_pxe_menu() function.
1248 static struct pxe_menu *parse_pxefile(char *menucfg)
1250 struct pxe_menu *cfg;
1252 cfg = malloc(sizeof(struct pxe_menu));
1257 memset(cfg, 0, sizeof(struct pxe_menu));
1259 INIT_LIST_HEAD(&cfg->labels);
1261 if (parse_pxefile_top(menucfg, cfg, 1) < 0) {
1262 destroy_pxe_menu(cfg);
1270 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1273 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1275 struct pxe_label *label;
1276 struct list_head *pos;
1281 * Create a menu and add items for all the labels.
1283 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print);
1288 list_for_each(pos, &cfg->labels) {
1289 label = list_entry(pos, struct pxe_label, list);
1291 if (menu_item_add(m, label->name, label) != 1) {
1298 * After we've created items for each label in the menu, set the
1299 * menu's default label if one was specified.
1301 if (cfg->default_label) {
1302 err = menu_default_set(m, cfg->default_label);
1304 if (err != -ENOENT) {
1309 printf("Missing default: %s\n", cfg->default_label);
1317 * Try to boot any labels we have yet to attempt to boot.
1319 static void boot_unattempted_labels(struct pxe_menu *cfg)
1321 struct list_head *pos;
1322 struct pxe_label *label;
1324 list_for_each(pos, &cfg->labels) {
1325 label = list_entry(pos, struct pxe_label, list);
1327 if (!label->attempted)
1333 * Boot the system as prescribed by a pxe_menu.
1335 * Use the menu system to either get the user's choice or the default, based
1336 * on config or user input. If there is no default or user's choice,
1337 * attempted to boot labels in the order they were given in pxe files.
1338 * If the default or user's choice fails to boot, attempt to boot other
1339 * labels in the order they were given in pxe files.
1341 * If this function returns, there weren't any labels that successfully
1342 * booted, or the user interrupted the menu selection via ctrl+c.
1344 static void handle_pxe_menu(struct pxe_menu *cfg)
1350 m = pxe_menu_to_menu(cfg);
1354 err = menu_get_choice(m, &choice);
1359 * err == 1 means we got a choice back from menu_get_choice.
1361 * err == -ENOENT if the menu was setup to select the default but no
1362 * default was set. in that case, we should continue trying to boot
1363 * labels that haven't been attempted yet.
1365 * otherwise, the user interrupted or there was some other error and
1371 else if (err != -ENOENT)
1374 boot_unattempted_labels(cfg);
1378 * Boots a system using a pxe file
1380 * Returns 0 on success, 1 on error.
1383 do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1385 unsigned long pxefile_addr_r;
1386 struct pxe_menu *cfg;
1387 char *pxefile_addr_str;
1389 do_getfile = do_get_tftp;
1392 pxefile_addr_str = from_env("pxefile_addr_r");
1393 if (!pxefile_addr_str)
1396 } else if (argc == 2) {
1397 pxefile_addr_str = argv[1];
1399 return CMD_RET_USAGE;
1402 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1403 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1407 cfg = parse_pxefile((char *)(pxefile_addr_r));
1410 printf("Error parsing config file\n");
1414 handle_pxe_menu(cfg);
1416 destroy_pxe_menu(cfg);
1421 static cmd_tbl_t cmd_pxe_sub[] = {
1422 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1423 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1426 int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1431 return CMD_RET_USAGE;
1433 /* drop initial "pxe" arg */
1437 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1440 return cp->cmd(cmdtp, flag, argc, argv);
1442 return CMD_RET_USAGE;
1447 "commands to get and boot from pxe files",
1448 "get - try to retrieve a pxe file using tftp\npxe "
1449 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1453 * Boots a system using a local disk syslinux/extlinux file
1455 * Returns 0 on success, 1 on error.
1457 int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1459 unsigned long pxefile_addr_r;
1460 struct pxe_menu *cfg;
1461 char *pxefile_addr_str;
1465 if (strstr(argv[1], "-p")) {
1472 return cmd_usage(cmdtp);
1475 pxefile_addr_str = from_env("pxefile_addr_r");
1476 if (!pxefile_addr_str)
1479 pxefile_addr_str = argv[4];
1483 filename = getenv("bootfile");
1486 setenv("bootfile", filename);
1489 if (strstr(argv[3], "ext2"))
1490 do_getfile = do_get_ext2;
1491 else if (strstr(argv[3], "fat"))
1492 do_getfile = do_get_fat;
1494 printf("Invalid filesystem: %s\n", argv[3]);
1497 fs_argv[1] = argv[1];
1498 fs_argv[2] = argv[2];
1500 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1501 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1505 if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) {
1506 printf("Error reading config file\n");
1510 cfg = parse_pxefile((char *)(pxefile_addr_r));
1513 printf("Error parsing config file\n");
1520 handle_pxe_menu(cfg);
1522 destroy_pxe_menu(cfg);
1528 sysboot, 7, 1, do_sysboot,
1529 "command to get and boot from syslinux files",
1530 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1531 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1532 " filesystem on 'dev' on 'interface' to address 'addr'"