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(const 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)
60 if (outbuf_len < 21) {
61 printf("outbuf is too small (%d < 21)\n", outbuf_len);
66 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
70 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
71 ethaddr[0], ethaddr[1], ethaddr[2],
72 ethaddr[3], ethaddr[4], ethaddr[5]);
78 * Returns the directory the file specified in the bootfile env variable is
79 * in. If bootfile isn't defined in the environment, return NULL, which should
80 * be interpreted as "don't prepend anything to paths".
82 static int get_bootfile_path(const char *file_path, char *bootfile_path,
83 size_t bootfile_path_size)
85 char *bootfile, *last_slash;
88 if (file_path[0] == '/')
91 bootfile = from_env("bootfile");
96 last_slash = strrchr(bootfile, '/');
98 if (last_slash == NULL)
101 path_len = (last_slash - bootfile) + 1;
103 if (bootfile_path_size < path_len) {
104 printf("bootfile_path too small. (%d < %d)\n",
105 bootfile_path_size, path_len);
110 strncpy(bootfile_path, bootfile, path_len);
113 bootfile_path[path_len] = '\0';
118 static int (*do_getfile)(const char *file_path, char *file_addr);
120 static int do_get_tftp(const char *file_path, char *file_addr)
122 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
124 tftp_argv[1] = file_addr;
125 tftp_argv[2] = (void *)file_path;
127 if (do_tftpb(NULL, 0, 3, tftp_argv))
133 static char *fs_argv[5];
135 static int do_get_ext2(const char *file_path, char *file_addr)
137 #ifdef CONFIG_CMD_EXT2
138 fs_argv[0] = "ext2load";
139 fs_argv[3] = file_addr;
140 fs_argv[4] = (void *)file_path;
142 if (!do_ext2load(NULL, 0, 5, fs_argv))
148 static int do_get_fat(const char *file_path, char *file_addr)
150 #ifdef CONFIG_CMD_FAT
151 fs_argv[0] = "fatload";
152 fs_argv[3] = file_addr;
153 fs_argv[4] = (void *)file_path;
155 if (!do_fat_fsload(NULL, 0, 5, fs_argv))
162 * As in pxelinux, paths to files referenced from files we retrieve are
163 * relative to the location of bootfile. get_relfile takes such a path and
164 * joins it with the bootfile path to get the full path to the target file. If
165 * the bootfile path is NULL, we use file_path as is.
167 * Returns 1 for success, or < 0 on error.
169 static int get_relfile(const char *file_path, void *file_addr)
172 char relfile[MAX_TFTP_PATH_LEN+1];
176 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
181 path_len = strlen(file_path);
182 path_len += strlen(relfile);
184 if (path_len > MAX_TFTP_PATH_LEN) {
185 printf("Base path too long (%s%s)\n",
189 return -ENAMETOOLONG;
192 strcat(relfile, file_path);
194 printf("Retrieving file: %s\n", relfile);
196 sprintf(addr_buf, "%p", file_addr);
198 return do_getfile(relfile, addr_buf);
202 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
203 * 'bootfile' was specified in the environment, the path to bootfile will be
204 * prepended to 'file_path' and the resulting path will be used.
206 * Returns 1 on success, or < 0 for error.
208 static int get_pxe_file(const char *file_path, void *file_addr)
210 unsigned long config_file_size;
214 err = get_relfile(file_path, file_addr);
220 * the file comes without a NUL byte at the end, so find out its size
221 * and add the NUL byte.
223 tftp_filesize = from_env("filesize");
228 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
231 *(char *)(file_addr + config_file_size) = '\0';
236 #define PXELINUX_DIR "pxelinux.cfg/"
239 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
240 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
241 * from the bootfile path, as described above.
243 * Returns 1 on success or < 0 on error.
245 static int get_pxelinux_path(const char *file, void *pxefile_addr_r)
247 size_t base_len = strlen(PXELINUX_DIR);
248 char path[MAX_TFTP_PATH_LEN+1];
250 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
251 printf("path (%s%s) too long, skipping\n",
253 return -ENAMETOOLONG;
256 sprintf(path, PXELINUX_DIR "%s", file);
258 return get_pxe_file(path, pxefile_addr_r);
262 * Looks for a pxe file with a name based on the pxeuuid environment variable.
264 * Returns 1 on success or < 0 on error.
266 static int pxe_uuid_path(void *pxefile_addr_r)
270 uuid_str = from_env("pxeuuid");
275 return get_pxelinux_path(uuid_str, pxefile_addr_r);
279 * Looks for a pxe file with a name based on the 'ethaddr' environment
282 * Returns 1 on success or < 0 on error.
284 static int pxe_mac_path(void *pxefile_addr_r)
289 err = format_mac_pxe(mac_str, sizeof(mac_str));
294 return get_pxelinux_path(mac_str, pxefile_addr_r);
298 * Looks for pxe files with names based on our IP address. See pxelinux
299 * documentation for details on what these file names look like. We match
302 * Returns 1 on success or < 0 on error.
304 static int pxe_ipaddr_paths(void *pxefile_addr_r)
309 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
311 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
312 err = get_pxelinux_path(ip_addr, pxefile_addr_r);
317 ip_addr[mask_pos] = '\0';
324 * Entry point for the 'pxe get' command.
325 * This Follows pxelinux's rules to download a config file from a tftp server.
326 * The file is stored at the location given by the pxefile_addr_r environment
327 * variable, which must be set.
329 * UUID comes from pxeuuid env variable, if defined
330 * MAC addr comes from ethaddr env variable, if defined
333 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
335 * Returns 0 on success or 1 on error.
338 do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
340 char *pxefile_addr_str;
341 unsigned long pxefile_addr_r;
344 do_getfile = do_get_tftp;
347 return CMD_RET_USAGE;
349 pxefile_addr_str = from_env("pxefile_addr_r");
351 if (!pxefile_addr_str)
354 err = strict_strtoul(pxefile_addr_str, 16,
355 (unsigned long *)&pxefile_addr_r);
360 * Keep trying paths until we successfully get a file we're looking
363 if (pxe_uuid_path((void *)pxefile_addr_r) > 0
364 || pxe_mac_path((void *)pxefile_addr_r) > 0
365 || pxe_ipaddr_paths((void *)pxefile_addr_r) > 0
366 || get_pxelinux_path("default", (void *)pxefile_addr_r) > 0) {
368 printf("Config file found\n");
373 printf("Config file not found\n");
379 * Wrapper to make it easier to store the file at file_path in the location
380 * specified by envaddr_name. file_path will be joined to the bootfile path,
381 * if any is specified.
383 * Returns 1 on success or < 0 on error.
385 static int get_relfile_envaddr(const char *file_path, const char *envaddr_name)
387 unsigned long file_addr;
390 envaddr = from_env(envaddr_name);
395 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
398 return get_relfile(file_path, (void *)file_addr);
402 * A note on the pxe file parser.
404 * We're parsing files that use syslinux grammar, which has a few quirks.
405 * String literals must be recognized based on context - there is no
406 * quoting or escaping support. There's also nothing to explicitly indicate
407 * when a label section completes. We deal with that by ending a label
408 * section whenever we see a line that doesn't include.
410 * As with the syslinux family, this same file format could be reused in the
411 * future for non pxe purposes. The only action it takes during parsing that
412 * would throw this off is handling of include files. It assumes we're using
413 * pxe, and does a tftp download of a file listed as an include file in the
414 * middle of the parsing operation. That could be handled by refactoring it to
415 * take a 'include file getter' function.
419 * Describes a single label given in a pxe file.
421 * Create these with the 'label_create' function given below.
423 * name - the name of the menu as given on the 'menu label' line.
424 * kernel - the path to the kernel file to use for this label.
425 * append - kernel command line to use when booting this label
426 * initrd - path to the initrd to use for this label.
427 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
428 * localboot - 1 if this label specified 'localboot', 0 otherwise.
429 * list - lets these form a list, which a pxe_menu struct will hold.
442 struct list_head list;
446 * Describes a pxe menu as given via pxe files.
448 * title - the name of the menu as given by a 'menu title' line.
449 * default_label - the name of the default label, if any.
450 * timeout - time in tenths of a second to wait for a user key-press before
451 * booting the default label.
452 * prompt - if 0, don't prompt for a choice unless the timeout period is
453 * interrupted. If 1, always prompt for a choice regardless of
455 * labels - a list of labels defined for the menu.
462 struct list_head labels;
466 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
467 * result must be free()'d to reclaim the memory.
469 * Returns NULL if malloc fails.
471 static struct pxe_label *label_create(void)
473 struct pxe_label *label;
475 label = malloc(sizeof(struct pxe_label));
480 memset(label, 0, sizeof(struct pxe_label));
486 * Free the memory used by a pxe_label, including that used by its name,
487 * kernel, append and initrd members, if they're non NULL.
489 * So - be sure to only use dynamically allocated memory for the members of
490 * the pxe_label struct, unless you want to clean it up first. These are
491 * currently only created by the pxe file parsing code.
493 static void label_destroy(struct pxe_label *label)
514 * Print a label and its string members if they're defined.
516 * This is passed as a callback to the menu code for displaying each
519 static void label_print(void *data)
521 struct pxe_label *label = data;
522 const char *c = label->menu ? label->menu : label->name;
524 printf("%s:\t%s\n", label->num, c);
528 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
529 * environment variable is defined. Its contents will be executed as U-boot
530 * command. If the label specified an 'append' line, its contents will be
531 * used to overwrite the contents of the 'bootargs' environment variable prior
532 * to running 'localcmd'.
534 * Returns 1 on success or < 0 on error.
536 static int label_localboot(struct pxe_label *label)
540 localcmd = from_env("localcmd");
546 setenv("bootargs", label->append);
548 debug("running: %s\n", localcmd);
550 return run_command_list(localcmd, strlen(localcmd), 0);
554 * Boot according to the contents of a pxe_label.
556 * If we can't boot for any reason, we return. A successful boot never
559 * The kernel will be stored in the location given by the 'kernel_addr_r'
560 * environment variable.
562 * If the label specifies an initrd file, it will be stored in the location
563 * given by the 'ramdisk_addr_r' environment variable.
565 * If the label specifies an 'append' line, its contents will overwrite that
566 * of the 'bootargs' environment variable.
568 static int label_boot(struct pxe_label *label)
570 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
576 label->attempted = 1;
578 if (label->localboot) {
579 if (label->localboot_val >= 0)
580 label_localboot(label);
584 if (label->kernel == NULL) {
585 printf("No kernel given, skipping %s\n",
591 if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) {
592 printf("Skipping %s for failure retrieving initrd\n",
597 bootm_argv[2] = initrd_str;
598 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
599 strcat(bootm_argv[2], ":");
600 strcat(bootm_argv[2], getenv("filesize"));
605 if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) {
606 printf("Skipping %s for failure retrieving kernel\n",
612 setenv("bootargs", label->append);
613 printf("append: %s\n", label->append);
616 bootm_argv[1] = getenv("kernel_addr_r");
619 * fdt usage is optional:
620 * It handles the following scenarios. All scenarios are exclusive
622 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
623 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
624 * and adjust argc appropriately.
626 * Scenario 2: If there is an fdt_addr specified, pass it along to
627 * bootm, and adjust argc appropriately.
629 * Scenario 3: fdt blob is not available.
631 bootm_argv[3] = getenv("fdt_addr_r");
633 /* if fdt label is defined then get fdt from server */
634 if (bootm_argv[3] && label->fdt) {
635 if (get_relfile_envaddr(label->fdt, "fdt_addr_r") < 0) {
636 printf("Skipping %s for failure retrieving fdt\n",
641 bootm_argv[3] = getenv("fdt_addr");
646 do_bootm(NULL, 0, bootm_argc, bootm_argv);
648 #ifdef CONFIG_CMD_BOOTZ
649 /* Try booting a zImage if do_bootm returns */
650 do_bootz(NULL, 0, bootm_argc, bootm_argv);
656 * Tokens for the pxe file parser.
680 * A token - given by a value and a type.
684 enum token_type type;
688 * Keywords recognized.
690 static const struct token keywords[] = {
693 {"timeout", T_TIMEOUT},
694 {"default", T_DEFAULT},
695 {"prompt", T_PROMPT},
697 {"kernel", T_KERNEL},
699 {"localboot", T_LOCALBOOT},
700 {"append", T_APPEND},
701 {"initrd", T_INITRD},
702 {"include", T_INCLUDE},
704 {"ontimeout", T_ONTIMEOUT,},
709 * Since pxe(linux) files don't have a token to identify the start of a
710 * literal, we have to keep track of when we're in a state where a literal is
711 * expected vs when we're in a state a keyword is expected.
720 * get_string retrieves a string from *p and stores it as a token in
723 * get_string used for scanning both string literals and keywords.
725 * Characters from *p are copied into t-val until a character equal to
726 * delim is found, or a NUL byte is reached. If delim has the special value of
727 * ' ', any whitespace character will be used as a delimiter.
729 * If lower is unequal to 0, uppercase characters will be converted to
730 * lowercase in the result. This is useful to make keywords case
733 * The location of *p is updated to point to the first character after the end
734 * of the token - the ending delimiter.
736 * On success, the new value of t->val is returned. Memory for t->val is
737 * allocated using malloc and must be free()'d to reclaim it. If insufficient
738 * memory is available, NULL is returned.
740 static char *get_string(char **p, struct token *t, char delim, int lower)
746 * b and e both start at the beginning of the input stream.
748 * e is incremented until we find the ending delimiter, or a NUL byte
749 * is reached. Then, we take e - b to find the length of the token.
754 if ((delim == ' ' && isspace(*e)) || delim == *e)
762 * Allocate memory to hold the string, and copy it in, converting
763 * characters to lowercase if lower is != 0.
765 t->val = malloc(len + 1);
769 for (i = 0; i < len; i++, b++) {
771 t->val[i] = tolower(*b);
779 * Update *p so the caller knows where to continue scanning.
789 * Populate a keyword token with a type and value.
791 static void get_keyword(struct token *t)
795 for (i = 0; keywords[i].val; i++) {
796 if (!strcmp(t->val, keywords[i].val)) {
797 t->type = keywords[i].type;
804 * Get the next token. We have to keep track of which state we're in to know
805 * if we're looking to get a string literal or a keyword.
807 * *p is updated to point at the first character after the current token.
809 static void get_token(char **p, struct token *t, enum lex_state state)
815 /* eat non EOL whitespace */
820 * eat comments. note that string literals can't begin with #, but
821 * can contain a # after their first character.
824 while (*c && *c != '\n')
831 } else if (*c == '\0') {
834 } else if (state == L_SLITERAL) {
835 get_string(&c, t, '\n', 0);
836 } else if (state == L_KEYWORD) {
838 * when we expect a keyword, we first get the next string
839 * token delimited by whitespace, and then check if it
840 * matches a keyword in our keyword list. if it does, it's
841 * converted to a keyword token of the appropriate type, and
842 * if not, it remains a string token.
844 get_string(&c, t, ' ', 1);
852 * Increment *c until we get to the end of the current line, or EOF.
854 static void eol_or_eof(char **c)
856 while (**c && **c != '\n')
861 * All of these parse_* functions share some common behavior.
863 * They finish with *c pointing after the token they parse, and return 1 on
864 * success, or < 0 on error.
868 * Parse a string literal and store a pointer it at *dst. String literals
869 * terminate at the end of the line.
871 static int parse_sliteral(char **c, char **dst)
876 get_token(c, &t, L_SLITERAL);
878 if (t.type != T_STRING) {
879 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
889 * Parse a base 10 (unsigned) integer and store it at *dst.
891 static int parse_integer(char **c, int *dst)
896 get_token(c, &t, L_SLITERAL);
898 if (t.type != T_STRING) {
899 printf("Expected string: %.*s\n", (int)(*c - s), s);
903 *dst = simple_strtol(t.val, NULL, 10);
910 static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
913 * Parse an include statement, and retrieve and parse the file it mentions.
915 * base should point to a location where it's safe to store the file, and
916 * nest_level should indicate how many nested includes have occurred. For this
917 * include, nest_level has already been incremented and doesn't need to be
920 static int handle_include(char **c, char *base,
921 struct pxe_menu *cfg, int nest_level)
927 err = parse_sliteral(c, &include_path);
930 printf("Expected include path: %.*s\n",
935 err = get_pxe_file(include_path, base);
938 printf("Couldn't retrieve %s\n", include_path);
942 return parse_pxefile_top(base, cfg, nest_level);
946 * Parse lines that begin with 'menu'.
948 * b and nest are provided to handle the 'menu include' case.
950 * b should be the address where the file currently being parsed is stored.
952 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
953 * a file it includes, 3 when parsing a file included by that file, and so on.
955 static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
961 get_token(c, &t, L_KEYWORD);
965 err = parse_sliteral(c, &cfg->title);
970 err = handle_include(c, b + strlen(b) + 1, cfg,
975 printf("Ignoring malformed menu command: %.*s\n",
988 * Handles parsing a 'menu line' when we're parsing a label.
990 static int parse_label_menu(char **c, struct pxe_menu *cfg,
991 struct pxe_label *label)
998 get_token(c, &t, L_KEYWORD);
1002 if (!cfg->default_label)
1003 cfg->default_label = strdup(label->name);
1005 if (!cfg->default_label)
1010 parse_sliteral(c, &label->menu);
1013 printf("Ignoring malformed menu command: %.*s\n",
1023 * Parses a label and adds it to the list of labels for a menu.
1025 * A label ends when we either get to the end of a file, or
1026 * get some input we otherwise don't have a handler defined
1030 static int parse_label(char **c, struct pxe_menu *cfg)
1035 struct pxe_label *label;
1038 label = label_create();
1042 err = parse_sliteral(c, &label->name);
1044 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1045 label_destroy(label);
1049 list_add_tail(&label->list, &cfg->labels);
1053 get_token(c, &t, L_KEYWORD);
1058 err = parse_label_menu(c, cfg, label);
1063 err = parse_sliteral(c, &label->kernel);
1067 err = parse_sliteral(c, &label->append);
1070 s = strstr(label->append, "initrd=");
1074 len = (int)(strchr(s, ' ') - s);
1075 label->initrd = malloc(len + 1);
1076 strncpy(label->initrd, s, len);
1077 label->initrd[len] = '\0';
1083 err = parse_sliteral(c, &label->initrd);
1088 err = parse_sliteral(c, &label->fdt);
1092 label->localboot = 1;
1093 err = parse_integer(c, &label->localboot_val);
1101 * put the token back! we don't want it - it's the end
1102 * of a label and whatever token this is, it's
1103 * something for the menu level context to handle.
1115 * This 16 comes from the limit pxelinux imposes on nested includes.
1117 * There is no reason at all we couldn't do more, but some limit helps prevent
1118 * infinite (until crash occurs) recursion if a file tries to include itself.
1120 #define MAX_NEST_LEVEL 16
1123 * Entry point for parsing a menu file. nest_level indicates how many times
1124 * we've nested in includes. It will be 1 for the top level menu file.
1126 * Returns 1 on success, < 0 on error.
1128 static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level)
1131 char *s, *b, *label_name;
1136 if (nest_level > MAX_NEST_LEVEL) {
1137 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1144 get_token(&p, &t, L_KEYWORD);
1150 err = parse_menu(&p, cfg, b, nest_level);
1154 err = parse_integer(&p, &cfg->timeout);
1158 err = parse_label(&p, cfg);
1163 err = parse_sliteral(&p, &label_name);
1166 if (cfg->default_label)
1167 free(cfg->default_label);
1169 cfg->default_label = label_name;
1175 err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg,
1190 printf("Ignoring unknown command: %.*s\n",
1201 * Free the memory used by a pxe_menu and its labels.
1203 static void destroy_pxe_menu(struct pxe_menu *cfg)
1205 struct list_head *pos, *n;
1206 struct pxe_label *label;
1211 if (cfg->default_label)
1212 free(cfg->default_label);
1214 list_for_each_safe(pos, n, &cfg->labels) {
1215 label = list_entry(pos, struct pxe_label, list);
1217 label_destroy(label);
1224 * Entry point for parsing a pxe file. This is only used for the top level
1227 * Returns NULL if there is an error, otherwise, returns a pointer to a
1228 * pxe_menu struct populated with the results of parsing the pxe file (and any
1229 * files it includes). The resulting pxe_menu struct can be free()'d by using
1230 * the destroy_pxe_menu() function.
1232 static struct pxe_menu *parse_pxefile(char *menucfg)
1234 struct pxe_menu *cfg;
1236 cfg = malloc(sizeof(struct pxe_menu));
1241 memset(cfg, 0, sizeof(struct pxe_menu));
1243 INIT_LIST_HEAD(&cfg->labels);
1245 if (parse_pxefile_top(menucfg, cfg, 1) < 0) {
1246 destroy_pxe_menu(cfg);
1254 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1257 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1259 struct pxe_label *label;
1260 struct list_head *pos;
1264 char *default_num = NULL;
1267 * Create a menu and add items for all the labels.
1269 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1275 list_for_each(pos, &cfg->labels) {
1276 label = list_entry(pos, struct pxe_label, list);
1278 sprintf(label->num, "%d", i++);
1279 if (menu_item_add(m, label->num, label) != 1) {
1283 if (cfg->default_label &&
1284 (strcmp(label->name, cfg->default_label) == 0))
1285 default_num = label->num;
1290 * After we've created items for each label in the menu, set the
1291 * menu's default label if one was specified.
1294 err = menu_default_set(m, default_num);
1296 if (err != -ENOENT) {
1301 printf("Missing default: %s\n", cfg->default_label);
1309 * Try to boot any labels we have yet to attempt to boot.
1311 static void boot_unattempted_labels(struct pxe_menu *cfg)
1313 struct list_head *pos;
1314 struct pxe_label *label;
1316 list_for_each(pos, &cfg->labels) {
1317 label = list_entry(pos, struct pxe_label, list);
1319 if (!label->attempted)
1325 * Boot the system as prescribed by a pxe_menu.
1327 * Use the menu system to either get the user's choice or the default, based
1328 * on config or user input. If there is no default or user's choice,
1329 * attempted to boot labels in the order they were given in pxe files.
1330 * If the default or user's choice fails to boot, attempt to boot other
1331 * labels in the order they were given in pxe files.
1333 * If this function returns, there weren't any labels that successfully
1334 * booted, or the user interrupted the menu selection via ctrl+c.
1336 static void handle_pxe_menu(struct pxe_menu *cfg)
1342 m = pxe_menu_to_menu(cfg);
1346 err = menu_get_choice(m, &choice);
1351 * err == 1 means we got a choice back from menu_get_choice.
1353 * err == -ENOENT if the menu was setup to select the default but no
1354 * default was set. in that case, we should continue trying to boot
1355 * labels that haven't been attempted yet.
1357 * otherwise, the user interrupted or there was some other error and
1362 err = label_boot(choice);
1365 } else if (err != -ENOENT) {
1369 boot_unattempted_labels(cfg);
1373 * Boots a system using a pxe file
1375 * Returns 0 on success, 1 on error.
1378 do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1380 unsigned long pxefile_addr_r;
1381 struct pxe_menu *cfg;
1382 char *pxefile_addr_str;
1384 do_getfile = do_get_tftp;
1387 pxefile_addr_str = from_env("pxefile_addr_r");
1388 if (!pxefile_addr_str)
1391 } else if (argc == 2) {
1392 pxefile_addr_str = argv[1];
1394 return CMD_RET_USAGE;
1397 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1398 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1402 cfg = parse_pxefile((char *)(pxefile_addr_r));
1405 printf("Error parsing config file\n");
1409 handle_pxe_menu(cfg);
1411 destroy_pxe_menu(cfg);
1416 static cmd_tbl_t cmd_pxe_sub[] = {
1417 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1418 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1421 int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1426 return CMD_RET_USAGE;
1428 /* drop initial "pxe" arg */
1432 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1435 return cp->cmd(cmdtp, flag, argc, argv);
1437 return CMD_RET_USAGE;
1442 "commands to get and boot from pxe files",
1443 "get - try to retrieve a pxe file using tftp\npxe "
1444 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1448 * Boots a system using a local disk syslinux/extlinux file
1450 * Returns 0 on success, 1 on error.
1452 int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1454 unsigned long pxefile_addr_r;
1455 struct pxe_menu *cfg;
1456 char *pxefile_addr_str;
1460 if (strstr(argv[1], "-p")) {
1467 return cmd_usage(cmdtp);
1470 pxefile_addr_str = from_env("pxefile_addr_r");
1471 if (!pxefile_addr_str)
1474 pxefile_addr_str = argv[4];
1478 filename = getenv("bootfile");
1481 setenv("bootfile", filename);
1484 if (strstr(argv[3], "ext2"))
1485 do_getfile = do_get_ext2;
1486 else if (strstr(argv[3], "fat"))
1487 do_getfile = do_get_fat;
1489 printf("Invalid filesystem: %s\n", argv[3]);
1492 fs_argv[1] = argv[1];
1493 fs_argv[2] = argv[2];
1495 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1496 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1500 if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) {
1501 printf("Error reading config file\n");
1505 cfg = parse_pxefile((char *)(pxefile_addr_r));
1508 printf("Error parsing config file\n");
1515 handle_pxe_menu(cfg);
1517 destroy_pxe_menu(cfg);
1523 sysboot, 7, 1, do_sysboot,
1524 "command to get and boot from syslinux files",
1525 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1526 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1527 " filesystem on 'dev' on 'interface' to address 'addr'"