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
29 const char *pxe_default_paths[] = {
31 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
33 "default-" CONFIG_SYS_ARCH,
39 * Like getenv, but prints an error if envvar isn't defined in the
40 * environment. It always returns what getenv does, so it can be used in
41 * place of getenv without changing error handling otherwise.
43 static char *from_env(const char *envvar)
50 printf("missing environment variable: %s\n", envvar);
56 * Convert an ethaddr from the environment to the format used by pxelinux
57 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
58 * the beginning of the ethernet address to indicate a hardware type of
59 * Ethernet. Also converts uppercase hex characters into lowercase, to match
60 * pxelinux's behavior.
62 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
63 * environment, or some other value < 0 on error.
65 static int format_mac_pxe(char *outbuf, size_t outbuf_len)
69 if (outbuf_len < 21) {
70 printf("outbuf is too small (%d < 21)\n", outbuf_len);
75 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
79 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
80 ethaddr[0], ethaddr[1], ethaddr[2],
81 ethaddr[3], ethaddr[4], ethaddr[5]);
87 * Returns the directory the file specified in the bootfile env variable is
88 * in. If bootfile isn't defined in the environment, return NULL, which should
89 * be interpreted as "don't prepend anything to paths".
91 static int get_bootfile_path(const char *file_path, char *bootfile_path,
92 size_t bootfile_path_size)
94 char *bootfile, *last_slash;
97 if (file_path[0] == '/')
100 bootfile = from_env("bootfile");
105 last_slash = strrchr(bootfile, '/');
107 if (last_slash == NULL)
110 path_len = (last_slash - bootfile) + 1;
112 if (bootfile_path_size < path_len) {
113 printf("bootfile_path too small. (%d < %d)\n",
114 bootfile_path_size, path_len);
119 strncpy(bootfile_path, bootfile, path_len);
122 bootfile_path[path_len] = '\0';
127 static int (*do_getfile)(const char *file_path, char *file_addr);
129 static int do_get_tftp(const char *file_path, char *file_addr)
131 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
133 tftp_argv[1] = file_addr;
134 tftp_argv[2] = (void *)file_path;
136 if (do_tftpb(NULL, 0, 3, tftp_argv))
142 static char *fs_argv[5];
144 static int do_get_ext2(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(NULL, 0, 5, fs_argv))
157 static int do_get_fat(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(NULL, 0, 5, fs_argv))
171 * As in pxelinux, paths to files referenced from files we retrieve are
172 * relative to the location of bootfile. get_relfile takes such a path and
173 * joins it with the bootfile path to get the full path to the target file. If
174 * the bootfile path is NULL, we use file_path as is.
176 * Returns 1 for success, or < 0 on error.
178 static int get_relfile(const char *file_path, void *file_addr)
181 char relfile[MAX_TFTP_PATH_LEN+1];
185 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
190 path_len = strlen(file_path);
191 path_len += strlen(relfile);
193 if (path_len > MAX_TFTP_PATH_LEN) {
194 printf("Base path too long (%s%s)\n",
198 return -ENAMETOOLONG;
201 strcat(relfile, file_path);
203 printf("Retrieving file: %s\n", relfile);
205 sprintf(addr_buf, "%p", file_addr);
207 return do_getfile(relfile, addr_buf);
211 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
212 * 'bootfile' was specified in the environment, the path to bootfile will be
213 * prepended to 'file_path' and the resulting path will be used.
215 * Returns 1 on success, or < 0 for error.
217 static int get_pxe_file(const char *file_path, void *file_addr)
219 unsigned long config_file_size;
223 err = get_relfile(file_path, file_addr);
229 * the file comes without a NUL byte at the end, so find out its size
230 * and add the NUL byte.
232 tftp_filesize = from_env("filesize");
237 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
240 *(char *)(file_addr + config_file_size) = '\0';
245 #define PXELINUX_DIR "pxelinux.cfg/"
248 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
249 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
250 * from the bootfile path, as described above.
252 * Returns 1 on success or < 0 on error.
254 static int get_pxelinux_path(const char *file, void *pxefile_addr_r)
256 size_t base_len = strlen(PXELINUX_DIR);
257 char path[MAX_TFTP_PATH_LEN+1];
259 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
260 printf("path (%s%s) too long, skipping\n",
262 return -ENAMETOOLONG;
265 sprintf(path, PXELINUX_DIR "%s", file);
267 return get_pxe_file(path, pxefile_addr_r);
271 * Looks for a pxe file with a name based on the pxeuuid environment variable.
273 * Returns 1 on success or < 0 on error.
275 static int pxe_uuid_path(void *pxefile_addr_r)
279 uuid_str = from_env("pxeuuid");
284 return get_pxelinux_path(uuid_str, pxefile_addr_r);
288 * Looks for a pxe file with a name based on the 'ethaddr' environment
291 * Returns 1 on success or < 0 on error.
293 static int pxe_mac_path(void *pxefile_addr_r)
298 err = format_mac_pxe(mac_str, sizeof(mac_str));
303 return get_pxelinux_path(mac_str, pxefile_addr_r);
307 * Looks for pxe files with names based on our IP address. See pxelinux
308 * documentation for details on what these file names look like. We match
311 * Returns 1 on success or < 0 on error.
313 static int pxe_ipaddr_paths(void *pxefile_addr_r)
318 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
320 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
321 err = get_pxelinux_path(ip_addr, pxefile_addr_r);
326 ip_addr[mask_pos] = '\0';
333 * Entry point for the 'pxe get' command.
334 * This Follows pxelinux's rules to download a config file from a tftp server.
335 * The file is stored at the location given by the pxefile_addr_r environment
336 * variable, which must be set.
338 * UUID comes from pxeuuid env variable, if defined
339 * MAC addr comes from ethaddr env variable, if defined
342 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
344 * Returns 0 on success or 1 on error.
347 do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
349 char *pxefile_addr_str;
350 unsigned long pxefile_addr_r;
353 do_getfile = do_get_tftp;
356 return CMD_RET_USAGE;
358 pxefile_addr_str = from_env("pxefile_addr_r");
360 if (!pxefile_addr_str)
363 err = strict_strtoul(pxefile_addr_str, 16,
364 (unsigned long *)&pxefile_addr_r);
369 * Keep trying paths until we successfully get a file we're looking
372 if (pxe_uuid_path((void *)pxefile_addr_r) > 0 ||
373 pxe_mac_path((void *)pxefile_addr_r) > 0 ||
374 pxe_ipaddr_paths((void *)pxefile_addr_r) > 0) {
375 printf("Config file found\n");
380 while (pxe_default_paths[i]) {
381 if (get_pxelinux_path(pxe_default_paths[i],
382 (void *)pxefile_addr_r) > 0) {
383 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(const char *file_path, const 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.
459 struct list_head list;
463 * Describes a pxe menu as given via pxe files.
465 * title - the name of the menu as given by a 'menu title' line.
466 * default_label - the name of the default label, if any.
467 * timeout - time in tenths of a second to wait for a user key-press before
468 * booting the default label.
469 * prompt - if 0, don't prompt for a choice unless the timeout period is
470 * interrupted. If 1, always prompt for a choice regardless of
472 * labels - a list of labels defined for the menu.
479 struct list_head labels;
483 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
484 * result must be free()'d to reclaim the memory.
486 * Returns NULL if malloc fails.
488 static struct pxe_label *label_create(void)
490 struct pxe_label *label;
492 label = malloc(sizeof(struct pxe_label));
497 memset(label, 0, sizeof(struct pxe_label));
503 * Free the memory used by a pxe_label, including that used by its name,
504 * kernel, append and initrd members, if they're non NULL.
506 * So - be sure to only use dynamically allocated memory for the members of
507 * the pxe_label struct, unless you want to clean it up first. These are
508 * currently only created by the pxe file parsing code.
510 static void label_destroy(struct pxe_label *label)
531 * Print a label and its string members if they're defined.
533 * This is passed as a callback to the menu code for displaying each
536 static void label_print(void *data)
538 struct pxe_label *label = data;
539 const char *c = label->menu ? label->menu : label->name;
541 printf("%s:\t%s\n", label->num, c);
545 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
546 * environment variable is defined. Its contents will be executed as U-boot
547 * command. If the label specified an 'append' line, its contents will be
548 * used to overwrite the contents of the 'bootargs' environment variable prior
549 * to running 'localcmd'.
551 * Returns 1 on success or < 0 on error.
553 static int label_localboot(struct pxe_label *label)
557 localcmd = from_env("localcmd");
563 setenv("bootargs", label->append);
565 debug("running: %s\n", localcmd);
567 return run_command_list(localcmd, strlen(localcmd), 0);
571 * Boot according to the contents of a pxe_label.
573 * If we can't boot for any reason, we return. A successful boot never
576 * The kernel will be stored in the location given by the 'kernel_addr_r'
577 * environment variable.
579 * If the label specifies an initrd file, it will be stored in the location
580 * given by the 'ramdisk_addr_r' environment variable.
582 * If the label specifies an 'append' line, its contents will overwrite that
583 * of the 'bootargs' environment variable.
585 static int label_boot(struct pxe_label *label)
587 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
589 char mac_str[29] = "";
590 char ip_str[68] = "";
597 label->attempted = 1;
599 if (label->localboot) {
600 if (label->localboot_val >= 0)
601 label_localboot(label);
605 if (label->kernel == NULL) {
606 printf("No kernel given, skipping %s\n",
612 if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) {
613 printf("Skipping %s for failure retrieving initrd\n",
618 bootm_argv[2] = initrd_str;
619 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
620 strcat(bootm_argv[2], ":");
621 strcat(bootm_argv[2], getenv("filesize"));
626 if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) {
627 printf("Skipping %s for failure retrieving kernel\n",
632 if (label->ipappend & 0x1) {
633 sprintf(ip_str, " ip=%s:%s:%s:%s",
634 getenv("ipaddr"), getenv("serverip"),
635 getenv("gatewayip"), getenv("netmask"));
636 len += strlen(ip_str);
639 if (label->ipappend & 0x2) {
641 strcpy(mac_str, " BOOTIF=");
642 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
645 len += strlen(mac_str);
649 len += strlen(label->append);
652 bootargs = malloc(len + 1);
657 strcpy(bootargs, label->append);
658 strcat(bootargs, ip_str);
659 strcat(bootargs, mac_str);
661 setenv("bootargs", bootargs);
662 printf("append: %s\n", bootargs);
667 bootm_argv[1] = getenv("kernel_addr_r");
670 * fdt usage is optional:
671 * It handles the following scenarios. All scenarios are exclusive
673 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
674 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
675 * and adjust argc appropriately.
677 * Scenario 2: If there is an fdt_addr specified, pass it along to
678 * bootm, and adjust argc appropriately.
680 * Scenario 3: fdt blob is not available.
682 bootm_argv[3] = getenv("fdt_addr_r");
684 /* if fdt label is defined then get fdt from server */
685 if (bootm_argv[3] && label->fdt) {
686 if (get_relfile_envaddr(label->fdt, "fdt_addr_r") < 0) {
687 printf("Skipping %s for failure retrieving fdt\n",
692 bootm_argv[3] = getenv("fdt_addr");
697 do_bootm(NULL, 0, bootm_argc, bootm_argv);
699 #ifdef CONFIG_CMD_BOOTZ
700 /* Try booting a zImage if do_bootm returns */
701 do_bootz(NULL, 0, bootm_argc, bootm_argv);
707 * Tokens for the pxe file parser.
732 * A token - given by a value and a type.
736 enum token_type type;
740 * Keywords recognized.
742 static const struct token keywords[] = {
745 {"timeout", T_TIMEOUT},
746 {"default", T_DEFAULT},
747 {"prompt", T_PROMPT},
749 {"kernel", T_KERNEL},
751 {"localboot", T_LOCALBOOT},
752 {"append", T_APPEND},
753 {"initrd", T_INITRD},
754 {"include", T_INCLUDE},
756 {"ontimeout", T_ONTIMEOUT,},
757 {"ipappend", T_IPAPPEND,},
762 * Since pxe(linux) files don't have a token to identify the start of a
763 * literal, we have to keep track of when we're in a state where a literal is
764 * expected vs when we're in a state a keyword is expected.
773 * get_string retrieves a string from *p and stores it as a token in
776 * get_string used for scanning both string literals and keywords.
778 * Characters from *p are copied into t-val until a character equal to
779 * delim is found, or a NUL byte is reached. If delim has the special value of
780 * ' ', any whitespace character will be used as a delimiter.
782 * If lower is unequal to 0, uppercase characters will be converted to
783 * lowercase in the result. This is useful to make keywords case
786 * The location of *p is updated to point to the first character after the end
787 * of the token - the ending delimiter.
789 * On success, the new value of t->val is returned. Memory for t->val is
790 * allocated using malloc and must be free()'d to reclaim it. If insufficient
791 * memory is available, NULL is returned.
793 static char *get_string(char **p, struct token *t, char delim, int lower)
799 * b and e both start at the beginning of the input stream.
801 * e is incremented until we find the ending delimiter, or a NUL byte
802 * is reached. Then, we take e - b to find the length of the token.
807 if ((delim == ' ' && isspace(*e)) || delim == *e)
815 * Allocate memory to hold the string, and copy it in, converting
816 * characters to lowercase if lower is != 0.
818 t->val = malloc(len + 1);
822 for (i = 0; i < len; i++, b++) {
824 t->val[i] = tolower(*b);
832 * Update *p so the caller knows where to continue scanning.
842 * Populate a keyword token with a type and value.
844 static void get_keyword(struct token *t)
848 for (i = 0; keywords[i].val; i++) {
849 if (!strcmp(t->val, keywords[i].val)) {
850 t->type = keywords[i].type;
857 * Get the next token. We have to keep track of which state we're in to know
858 * if we're looking to get a string literal or a keyword.
860 * *p is updated to point at the first character after the current token.
862 static void get_token(char **p, struct token *t, enum lex_state state)
868 /* eat non EOL whitespace */
873 * eat comments. note that string literals can't begin with #, but
874 * can contain a # after their first character.
877 while (*c && *c != '\n')
884 } else if (*c == '\0') {
887 } else if (state == L_SLITERAL) {
888 get_string(&c, t, '\n', 0);
889 } else if (state == L_KEYWORD) {
891 * when we expect a keyword, we first get the next string
892 * token delimited by whitespace, and then check if it
893 * matches a keyword in our keyword list. if it does, it's
894 * converted to a keyword token of the appropriate type, and
895 * if not, it remains a string token.
897 get_string(&c, t, ' ', 1);
905 * Increment *c until we get to the end of the current line, or EOF.
907 static void eol_or_eof(char **c)
909 while (**c && **c != '\n')
914 * All of these parse_* functions share some common behavior.
916 * They finish with *c pointing after the token they parse, and return 1 on
917 * success, or < 0 on error.
921 * Parse a string literal and store a pointer it at *dst. String literals
922 * terminate at the end of the line.
924 static int parse_sliteral(char **c, char **dst)
929 get_token(c, &t, L_SLITERAL);
931 if (t.type != T_STRING) {
932 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
942 * Parse a base 10 (unsigned) integer and store it at *dst.
944 static int parse_integer(char **c, int *dst)
949 get_token(c, &t, L_SLITERAL);
951 if (t.type != T_STRING) {
952 printf("Expected string: %.*s\n", (int)(*c - s), s);
956 *dst = simple_strtol(t.val, NULL, 10);
963 static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
966 * Parse an include statement, and retrieve and parse the file it mentions.
968 * base should point to a location where it's safe to store the file, and
969 * nest_level should indicate how many nested includes have occurred. For this
970 * include, nest_level has already been incremented and doesn't need to be
973 static int handle_include(char **c, char *base,
974 struct pxe_menu *cfg, int nest_level)
980 err = parse_sliteral(c, &include_path);
983 printf("Expected include path: %.*s\n",
988 err = get_pxe_file(include_path, base);
991 printf("Couldn't retrieve %s\n", include_path);
995 return parse_pxefile_top(base, cfg, nest_level);
999 * Parse lines that begin with 'menu'.
1001 * b and nest are provided to handle the 'menu include' case.
1003 * b should be the address where the file currently being parsed is stored.
1005 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1006 * a file it includes, 3 when parsing a file included by that file, and so on.
1008 static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
1014 get_token(c, &t, L_KEYWORD);
1018 err = parse_sliteral(c, &cfg->title);
1023 err = handle_include(c, b + strlen(b) + 1, cfg,
1028 printf("Ignoring malformed menu command: %.*s\n",
1041 * Handles parsing a 'menu line' when we're parsing a label.
1043 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1044 struct pxe_label *label)
1051 get_token(c, &t, L_KEYWORD);
1055 if (!cfg->default_label)
1056 cfg->default_label = strdup(label->name);
1058 if (!cfg->default_label)
1063 parse_sliteral(c, &label->menu);
1066 printf("Ignoring malformed menu command: %.*s\n",
1076 * Parses a label and adds it to the list of labels for a menu.
1078 * A label ends when we either get to the end of a file, or
1079 * get some input we otherwise don't have a handler defined
1083 static int parse_label(char **c, struct pxe_menu *cfg)
1088 struct pxe_label *label;
1091 label = label_create();
1095 err = parse_sliteral(c, &label->name);
1097 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1098 label_destroy(label);
1102 list_add_tail(&label->list, &cfg->labels);
1106 get_token(c, &t, L_KEYWORD);
1111 err = parse_label_menu(c, cfg, label);
1116 err = parse_sliteral(c, &label->kernel);
1120 err = parse_sliteral(c, &label->append);
1123 s = strstr(label->append, "initrd=");
1127 len = (int)(strchr(s, ' ') - s);
1128 label->initrd = malloc(len + 1);
1129 strncpy(label->initrd, s, len);
1130 label->initrd[len] = '\0';
1136 err = parse_sliteral(c, &label->initrd);
1141 err = parse_sliteral(c, &label->fdt);
1145 label->localboot = 1;
1146 err = parse_integer(c, &label->localboot_val);
1150 err = parse_integer(c, &label->ipappend);
1158 * put the token back! we don't want it - it's the end
1159 * of a label and whatever token this is, it's
1160 * something for the menu level context to handle.
1172 * This 16 comes from the limit pxelinux imposes on nested includes.
1174 * There is no reason at all we couldn't do more, but some limit helps prevent
1175 * infinite (until crash occurs) recursion if a file tries to include itself.
1177 #define MAX_NEST_LEVEL 16
1180 * Entry point for parsing a menu file. nest_level indicates how many times
1181 * we've nested in includes. It will be 1 for the top level menu file.
1183 * Returns 1 on success, < 0 on error.
1185 static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level)
1188 char *s, *b, *label_name;
1193 if (nest_level > MAX_NEST_LEVEL) {
1194 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1201 get_token(&p, &t, L_KEYWORD);
1207 err = parse_menu(&p, cfg, b, nest_level);
1211 err = parse_integer(&p, &cfg->timeout);
1215 err = parse_label(&p, cfg);
1220 err = parse_sliteral(&p, &label_name);
1223 if (cfg->default_label)
1224 free(cfg->default_label);
1226 cfg->default_label = label_name;
1232 err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg,
1247 printf("Ignoring unknown command: %.*s\n",
1258 * Free the memory used by a pxe_menu and its labels.
1260 static void destroy_pxe_menu(struct pxe_menu *cfg)
1262 struct list_head *pos, *n;
1263 struct pxe_label *label;
1268 if (cfg->default_label)
1269 free(cfg->default_label);
1271 list_for_each_safe(pos, n, &cfg->labels) {
1272 label = list_entry(pos, struct pxe_label, list);
1274 label_destroy(label);
1281 * Entry point for parsing a pxe file. This is only used for the top level
1284 * Returns NULL if there is an error, otherwise, returns a pointer to a
1285 * pxe_menu struct populated with the results of parsing the pxe file (and any
1286 * files it includes). The resulting pxe_menu struct can be free()'d by using
1287 * the destroy_pxe_menu() function.
1289 static struct pxe_menu *parse_pxefile(char *menucfg)
1291 struct pxe_menu *cfg;
1293 cfg = malloc(sizeof(struct pxe_menu));
1298 memset(cfg, 0, sizeof(struct pxe_menu));
1300 INIT_LIST_HEAD(&cfg->labels);
1302 if (parse_pxefile_top(menucfg, cfg, 1) < 0) {
1303 destroy_pxe_menu(cfg);
1311 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1314 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1316 struct pxe_label *label;
1317 struct list_head *pos;
1321 char *default_num = NULL;
1324 * Create a menu and add items for all the labels.
1326 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1332 list_for_each(pos, &cfg->labels) {
1333 label = list_entry(pos, struct pxe_label, list);
1335 sprintf(label->num, "%d", i++);
1336 if (menu_item_add(m, label->num, label) != 1) {
1340 if (cfg->default_label &&
1341 (strcmp(label->name, cfg->default_label) == 0))
1342 default_num = label->num;
1347 * After we've created items for each label in the menu, set the
1348 * menu's default label if one was specified.
1351 err = menu_default_set(m, default_num);
1353 if (err != -ENOENT) {
1358 printf("Missing default: %s\n", cfg->default_label);
1366 * Try to boot any labels we have yet to attempt to boot.
1368 static void boot_unattempted_labels(struct pxe_menu *cfg)
1370 struct list_head *pos;
1371 struct pxe_label *label;
1373 list_for_each(pos, &cfg->labels) {
1374 label = list_entry(pos, struct pxe_label, list);
1376 if (!label->attempted)
1382 * Boot the system as prescribed by a pxe_menu.
1384 * Use the menu system to either get the user's choice or the default, based
1385 * on config or user input. If there is no default or user's choice,
1386 * attempted to boot labels in the order they were given in pxe files.
1387 * If the default or user's choice fails to boot, attempt to boot other
1388 * labels in the order they were given in pxe files.
1390 * If this function returns, there weren't any labels that successfully
1391 * booted, or the user interrupted the menu selection via ctrl+c.
1393 static void handle_pxe_menu(struct pxe_menu *cfg)
1399 m = pxe_menu_to_menu(cfg);
1403 err = menu_get_choice(m, &choice);
1408 * err == 1 means we got a choice back from menu_get_choice.
1410 * err == -ENOENT if the menu was setup to select the default but no
1411 * default was set. in that case, we should continue trying to boot
1412 * labels that haven't been attempted yet.
1414 * otherwise, the user interrupted or there was some other error and
1419 err = label_boot(choice);
1422 } else if (err != -ENOENT) {
1426 boot_unattempted_labels(cfg);
1430 * Boots a system using a pxe file
1432 * Returns 0 on success, 1 on error.
1435 do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1437 unsigned long pxefile_addr_r;
1438 struct pxe_menu *cfg;
1439 char *pxefile_addr_str;
1441 do_getfile = do_get_tftp;
1444 pxefile_addr_str = from_env("pxefile_addr_r");
1445 if (!pxefile_addr_str)
1448 } else if (argc == 2) {
1449 pxefile_addr_str = argv[1];
1451 return CMD_RET_USAGE;
1454 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1455 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1459 cfg = parse_pxefile((char *)(pxefile_addr_r));
1462 printf("Error parsing config file\n");
1466 handle_pxe_menu(cfg);
1468 destroy_pxe_menu(cfg);
1473 static cmd_tbl_t cmd_pxe_sub[] = {
1474 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1475 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1478 int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1483 return CMD_RET_USAGE;
1485 /* drop initial "pxe" arg */
1489 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1492 return cp->cmd(cmdtp, flag, argc, argv);
1494 return CMD_RET_USAGE;
1499 "commands to get and boot from pxe files",
1500 "get - try to retrieve a pxe file using tftp\npxe "
1501 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1505 * Boots a system using a local disk syslinux/extlinux file
1507 * Returns 0 on success, 1 on error.
1509 int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1511 unsigned long pxefile_addr_r;
1512 struct pxe_menu *cfg;
1513 char *pxefile_addr_str;
1517 if (strstr(argv[1], "-p")) {
1524 return cmd_usage(cmdtp);
1527 pxefile_addr_str = from_env("pxefile_addr_r");
1528 if (!pxefile_addr_str)
1531 pxefile_addr_str = argv[4];
1535 filename = getenv("bootfile");
1538 setenv("bootfile", filename);
1541 if (strstr(argv[3], "ext2"))
1542 do_getfile = do_get_ext2;
1543 else if (strstr(argv[3], "fat"))
1544 do_getfile = do_get_fat;
1546 printf("Invalid filesystem: %s\n", argv[3]);
1549 fs_argv[1] = argv[1];
1550 fs_argv[2] = argv[2];
1552 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1553 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1557 if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) {
1558 printf("Error reading config file\n");
1562 cfg = parse_pxefile((char *)(pxefile_addr_r));
1565 printf("Error parsing config file\n");
1572 handle_pxe_menu(cfg);
1574 destroy_pxe_menu(cfg);
1580 sysboot, 7, 1, do_sysboot,
1581 "command to get and boot from syslinux files",
1582 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1583 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1584 " filesystem on 'dev' on 'interface' to address 'addr'"