2 * Copyright 2010-2011 Calxeda, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <linux/string.h>
11 #include <linux/ctype.h>
13 #include <linux/list.h>
17 #define MAX_TFTP_PATH_LEN 127
19 const char *pxe_default_paths[] = {
21 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
23 "default-" CONFIG_SYS_ARCH,
29 * Like getenv, but prints an error if envvar isn't defined in the
30 * environment. It always returns what getenv does, so it can be used in
31 * place of getenv without changing error handling otherwise.
33 static char *from_env(const char *envvar)
40 printf("missing environment variable: %s\n", envvar);
46 * Convert an ethaddr from the environment to the format used by pxelinux
47 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
48 * the beginning of the ethernet address to indicate a hardware type of
49 * Ethernet. Also converts uppercase hex characters into lowercase, to match
50 * pxelinux's behavior.
52 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
53 * environment, or some other value < 0 on error.
55 static int format_mac_pxe(char *outbuf, size_t outbuf_len)
59 if (outbuf_len < 21) {
60 printf("outbuf is too small (%d < 21)\n", outbuf_len);
65 if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
69 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
70 ethaddr[0], ethaddr[1], ethaddr[2],
71 ethaddr[3], ethaddr[4], ethaddr[5]);
77 * Returns the directory the file specified in the bootfile env variable is
78 * in. If bootfile isn't defined in the environment, return NULL, which should
79 * be interpreted as "don't prepend anything to paths".
81 static int get_bootfile_path(const char *file_path, char *bootfile_path,
82 size_t bootfile_path_size)
84 char *bootfile, *last_slash;
87 if (file_path[0] == '/')
90 bootfile = from_env("bootfile");
95 last_slash = strrchr(bootfile, '/');
97 if (last_slash == NULL)
100 path_len = (last_slash - bootfile) + 1;
102 if (bootfile_path_size < path_len) {
103 printf("bootfile_path too small. (%d < %d)\n",
104 bootfile_path_size, path_len);
109 strncpy(bootfile_path, bootfile, path_len);
112 bootfile_path[path_len] = '\0';
117 static int (*do_getfile)(const char *file_path, char *file_addr);
119 static int do_get_tftp(const char *file_path, char *file_addr)
121 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
123 tftp_argv[1] = file_addr;
124 tftp_argv[2] = (void *)file_path;
126 if (do_tftpb(NULL, 0, 3, tftp_argv))
132 static char *fs_argv[5];
134 static int do_get_ext2(const char *file_path, char *file_addr)
136 #ifdef CONFIG_CMD_EXT2
137 fs_argv[0] = "ext2load";
138 fs_argv[3] = file_addr;
139 fs_argv[4] = (void *)file_path;
141 if (!do_ext2load(NULL, 0, 5, fs_argv))
147 static int do_get_fat(const char *file_path, char *file_addr)
149 #ifdef CONFIG_CMD_FAT
150 fs_argv[0] = "fatload";
151 fs_argv[3] = file_addr;
152 fs_argv[4] = (void *)file_path;
154 if (!do_fat_fsload(NULL, 0, 5, fs_argv))
161 * As in pxelinux, paths to files referenced from files we retrieve are
162 * relative to the location of bootfile. get_relfile takes such a path and
163 * joins it with the bootfile path to get the full path to the target file. If
164 * the bootfile path is NULL, we use file_path as is.
166 * Returns 1 for success, or < 0 on error.
168 static int get_relfile(const char *file_path, void *file_addr)
171 char relfile[MAX_TFTP_PATH_LEN+1];
175 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
180 path_len = strlen(file_path);
181 path_len += strlen(relfile);
183 if (path_len > MAX_TFTP_PATH_LEN) {
184 printf("Base path too long (%s%s)\n",
188 return -ENAMETOOLONG;
191 strcat(relfile, file_path);
193 printf("Retrieving file: %s\n", relfile);
195 sprintf(addr_buf, "%p", file_addr);
197 return do_getfile(relfile, addr_buf);
201 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
202 * 'bootfile' was specified in the environment, the path to bootfile will be
203 * prepended to 'file_path' and the resulting path will be used.
205 * Returns 1 on success, or < 0 for error.
207 static int get_pxe_file(const char *file_path, void *file_addr)
209 unsigned long config_file_size;
213 err = get_relfile(file_path, file_addr);
219 * the file comes without a NUL byte at the end, so find out its size
220 * and add the NUL byte.
222 tftp_filesize = from_env("filesize");
227 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
230 *(char *)(file_addr + config_file_size) = '\0';
235 #define PXELINUX_DIR "pxelinux.cfg/"
238 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
239 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
240 * from the bootfile path, as described above.
242 * Returns 1 on success or < 0 on error.
244 static int get_pxelinux_path(const char *file, void *pxefile_addr_r)
246 size_t base_len = strlen(PXELINUX_DIR);
247 char path[MAX_TFTP_PATH_LEN+1];
249 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
250 printf("path (%s%s) too long, skipping\n",
252 return -ENAMETOOLONG;
255 sprintf(path, PXELINUX_DIR "%s", file);
257 return get_pxe_file(path, pxefile_addr_r);
261 * Looks for a pxe file with a name based on the pxeuuid environment variable.
263 * Returns 1 on success or < 0 on error.
265 static int pxe_uuid_path(void *pxefile_addr_r)
269 uuid_str = from_env("pxeuuid");
274 return get_pxelinux_path(uuid_str, pxefile_addr_r);
278 * Looks for a pxe file with a name based on the 'ethaddr' environment
281 * Returns 1 on success or < 0 on error.
283 static int pxe_mac_path(void *pxefile_addr_r)
288 err = format_mac_pxe(mac_str, sizeof(mac_str));
293 return get_pxelinux_path(mac_str, pxefile_addr_r);
297 * Looks for pxe files with names based on our IP address. See pxelinux
298 * documentation for details on what these file names look like. We match
301 * Returns 1 on success or < 0 on error.
303 static int pxe_ipaddr_paths(void *pxefile_addr_r)
308 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
310 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
311 err = get_pxelinux_path(ip_addr, pxefile_addr_r);
316 ip_addr[mask_pos] = '\0';
323 * Entry point for the 'pxe get' command.
324 * This Follows pxelinux's rules to download a config file from a tftp server.
325 * The file is stored at the location given by the pxefile_addr_r environment
326 * variable, which must be set.
328 * UUID comes from pxeuuid env variable, if defined
329 * MAC addr comes from ethaddr env variable, if defined
332 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
334 * Returns 0 on success or 1 on error.
337 do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
339 char *pxefile_addr_str;
340 unsigned long pxefile_addr_r;
343 do_getfile = do_get_tftp;
346 return CMD_RET_USAGE;
348 pxefile_addr_str = from_env("pxefile_addr_r");
350 if (!pxefile_addr_str)
353 err = strict_strtoul(pxefile_addr_str, 16,
354 (unsigned long *)&pxefile_addr_r);
359 * Keep trying paths until we successfully get a file we're looking
362 if (pxe_uuid_path((void *)pxefile_addr_r) > 0 ||
363 pxe_mac_path((void *)pxefile_addr_r) > 0 ||
364 pxe_ipaddr_paths((void *)pxefile_addr_r) > 0) {
365 printf("Config file found\n");
370 while (pxe_default_paths[i]) {
371 if (get_pxelinux_path(pxe_default_paths[i],
372 (void *)pxefile_addr_r) > 0) {
373 printf("Config file found\n");
379 printf("Config file not found\n");
385 * Wrapper to make it easier to store the file at file_path in the location
386 * specified by envaddr_name. file_path will be joined to the bootfile path,
387 * if any is specified.
389 * Returns 1 on success or < 0 on error.
391 static int get_relfile_envaddr(const char *file_path, const char *envaddr_name)
393 unsigned long file_addr;
396 envaddr = from_env(envaddr_name);
401 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
404 return get_relfile(file_path, (void *)file_addr);
408 * A note on the pxe file parser.
410 * We're parsing files that use syslinux grammar, which has a few quirks.
411 * String literals must be recognized based on context - there is no
412 * quoting or escaping support. There's also nothing to explicitly indicate
413 * when a label section completes. We deal with that by ending a label
414 * section whenever we see a line that doesn't include.
416 * As with the syslinux family, this same file format could be reused in the
417 * future for non pxe purposes. The only action it takes during parsing that
418 * would throw this off is handling of include files. It assumes we're using
419 * pxe, and does a tftp download of a file listed as an include file in the
420 * middle of the parsing operation. That could be handled by refactoring it to
421 * take a 'include file getter' function.
425 * Describes a single label given in a pxe file.
427 * Create these with the 'label_create' function given below.
429 * name - the name of the menu as given on the 'menu label' line.
430 * kernel - the path to the kernel file to use for this label.
431 * append - kernel command line to use when booting this label
432 * initrd - path to the initrd to use for this label.
433 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
434 * localboot - 1 if this label specified 'localboot', 0 otherwise.
435 * list - lets these form a list, which a pxe_menu struct will hold.
449 struct list_head list;
453 * Describes a pxe menu as given via pxe files.
455 * title - the name of the menu as given by a 'menu title' line.
456 * default_label - the name of the default label, if any.
457 * timeout - time in tenths of a second to wait for a user key-press before
458 * booting the default label.
459 * prompt - if 0, don't prompt for a choice unless the timeout period is
460 * interrupted. If 1, always prompt for a choice regardless of
462 * labels - a list of labels defined for the menu.
469 struct list_head labels;
473 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
474 * result must be free()'d to reclaim the memory.
476 * Returns NULL if malloc fails.
478 static struct pxe_label *label_create(void)
480 struct pxe_label *label;
482 label = malloc(sizeof(struct pxe_label));
487 memset(label, 0, sizeof(struct pxe_label));
493 * Free the memory used by a pxe_label, including that used by its name,
494 * kernel, append and initrd members, if they're non NULL.
496 * So - be sure to only use dynamically allocated memory for the members of
497 * the pxe_label struct, unless you want to clean it up first. These are
498 * currently only created by the pxe file parsing code.
500 static void label_destroy(struct pxe_label *label)
521 * Print a label and its string members if they're defined.
523 * This is passed as a callback to the menu code for displaying each
526 static void label_print(void *data)
528 struct pxe_label *label = data;
529 const char *c = label->menu ? label->menu : label->name;
531 printf("%s:\t%s\n", label->num, c);
535 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
536 * environment variable is defined. Its contents will be executed as U-boot
537 * command. If the label specified an 'append' line, its contents will be
538 * used to overwrite the contents of the 'bootargs' environment variable prior
539 * to running 'localcmd'.
541 * Returns 1 on success or < 0 on error.
543 static int label_localboot(struct pxe_label *label)
547 localcmd = from_env("localcmd");
553 setenv("bootargs", label->append);
555 debug("running: %s\n", localcmd);
557 return run_command_list(localcmd, strlen(localcmd), 0);
561 * Boot according to the contents of a pxe_label.
563 * If we can't boot for any reason, we return. A successful boot never
566 * The kernel will be stored in the location given by the 'kernel_addr_r'
567 * environment variable.
569 * If the label specifies an initrd file, it will be stored in the location
570 * given by the 'ramdisk_addr_r' environment variable.
572 * If the label specifies an 'append' line, its contents will overwrite that
573 * of the 'bootargs' environment variable.
575 static int label_boot(struct pxe_label *label)
577 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
579 char mac_str[29] = "";
580 char ip_str[68] = "";
587 label->attempted = 1;
589 if (label->localboot) {
590 if (label->localboot_val >= 0)
591 label_localboot(label);
595 if (label->kernel == NULL) {
596 printf("No kernel given, skipping %s\n",
602 if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) {
603 printf("Skipping %s for failure retrieving initrd\n",
608 bootm_argv[2] = initrd_str;
609 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
610 strcat(bootm_argv[2], ":");
611 strcat(bootm_argv[2], getenv("filesize"));
616 if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) {
617 printf("Skipping %s for failure retrieving kernel\n",
622 if (label->ipappend & 0x1) {
623 sprintf(ip_str, " ip=%s:%s:%s:%s",
624 getenv("ipaddr"), getenv("serverip"),
625 getenv("gatewayip"), getenv("netmask"));
626 len += strlen(ip_str);
629 if (label->ipappend & 0x2) {
631 strcpy(mac_str, " BOOTIF=");
632 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
635 len += strlen(mac_str);
639 len += strlen(label->append);
642 bootargs = malloc(len + 1);
647 strcpy(bootargs, label->append);
648 strcat(bootargs, ip_str);
649 strcat(bootargs, mac_str);
651 setenv("bootargs", bootargs);
652 printf("append: %s\n", bootargs);
657 bootm_argv[1] = getenv("kernel_addr_r");
660 * fdt usage is optional:
661 * It handles the following scenarios. All scenarios are exclusive
663 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
664 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
665 * and adjust argc appropriately.
667 * Scenario 2: If there is an fdt_addr specified, pass it along to
668 * bootm, and adjust argc appropriately.
670 * Scenario 3: fdt blob is not available.
672 bootm_argv[3] = getenv("fdt_addr_r");
674 /* if fdt label is defined then get fdt from server */
675 if (bootm_argv[3] && label->fdt) {
676 if (get_relfile_envaddr(label->fdt, "fdt_addr_r") < 0) {
677 printf("Skipping %s for failure retrieving fdt\n",
682 bootm_argv[3] = getenv("fdt_addr");
687 do_bootm(NULL, 0, bootm_argc, bootm_argv);
689 #ifdef CONFIG_CMD_BOOTZ
690 /* Try booting a zImage if do_bootm returns */
691 do_bootz(NULL, 0, bootm_argc, bootm_argv);
697 * Tokens for the pxe file parser.
722 * A token - given by a value and a type.
726 enum token_type type;
730 * Keywords recognized.
732 static const struct token keywords[] = {
735 {"timeout", T_TIMEOUT},
736 {"default", T_DEFAULT},
737 {"prompt", T_PROMPT},
739 {"kernel", T_KERNEL},
741 {"localboot", T_LOCALBOOT},
742 {"append", T_APPEND},
743 {"initrd", T_INITRD},
744 {"include", T_INCLUDE},
746 {"ontimeout", T_ONTIMEOUT,},
747 {"ipappend", T_IPAPPEND,},
752 * Since pxe(linux) files don't have a token to identify the start of a
753 * literal, we have to keep track of when we're in a state where a literal is
754 * expected vs when we're in a state a keyword is expected.
763 * get_string retrieves a string from *p and stores it as a token in
766 * get_string used for scanning both string literals and keywords.
768 * Characters from *p are copied into t-val until a character equal to
769 * delim is found, or a NUL byte is reached. If delim has the special value of
770 * ' ', any whitespace character will be used as a delimiter.
772 * If lower is unequal to 0, uppercase characters will be converted to
773 * lowercase in the result. This is useful to make keywords case
776 * The location of *p is updated to point to the first character after the end
777 * of the token - the ending delimiter.
779 * On success, the new value of t->val is returned. Memory for t->val is
780 * allocated using malloc and must be free()'d to reclaim it. If insufficient
781 * memory is available, NULL is returned.
783 static char *get_string(char **p, struct token *t, char delim, int lower)
789 * b and e both start at the beginning of the input stream.
791 * e is incremented until we find the ending delimiter, or a NUL byte
792 * is reached. Then, we take e - b to find the length of the token.
797 if ((delim == ' ' && isspace(*e)) || delim == *e)
805 * Allocate memory to hold the string, and copy it in, converting
806 * characters to lowercase if lower is != 0.
808 t->val = malloc(len + 1);
812 for (i = 0; i < len; i++, b++) {
814 t->val[i] = tolower(*b);
822 * Update *p so the caller knows where to continue scanning.
832 * Populate a keyword token with a type and value.
834 static void get_keyword(struct token *t)
838 for (i = 0; keywords[i].val; i++) {
839 if (!strcmp(t->val, keywords[i].val)) {
840 t->type = keywords[i].type;
847 * Get the next token. We have to keep track of which state we're in to know
848 * if we're looking to get a string literal or a keyword.
850 * *p is updated to point at the first character after the current token.
852 static void get_token(char **p, struct token *t, enum lex_state state)
858 /* eat non EOL whitespace */
863 * eat comments. note that string literals can't begin with #, but
864 * can contain a # after their first character.
867 while (*c && *c != '\n')
874 } else if (*c == '\0') {
877 } else if (state == L_SLITERAL) {
878 get_string(&c, t, '\n', 0);
879 } else if (state == L_KEYWORD) {
881 * when we expect a keyword, we first get the next string
882 * token delimited by whitespace, and then check if it
883 * matches a keyword in our keyword list. if it does, it's
884 * converted to a keyword token of the appropriate type, and
885 * if not, it remains a string token.
887 get_string(&c, t, ' ', 1);
895 * Increment *c until we get to the end of the current line, or EOF.
897 static void eol_or_eof(char **c)
899 while (**c && **c != '\n')
904 * All of these parse_* functions share some common behavior.
906 * They finish with *c pointing after the token they parse, and return 1 on
907 * success, or < 0 on error.
911 * Parse a string literal and store a pointer it at *dst. String literals
912 * terminate at the end of the line.
914 static int parse_sliteral(char **c, char **dst)
919 get_token(c, &t, L_SLITERAL);
921 if (t.type != T_STRING) {
922 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
932 * Parse a base 10 (unsigned) integer and store it at *dst.
934 static int parse_integer(char **c, int *dst)
939 get_token(c, &t, L_SLITERAL);
941 if (t.type != T_STRING) {
942 printf("Expected string: %.*s\n", (int)(*c - s), s);
946 *dst = simple_strtol(t.val, NULL, 10);
953 static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
956 * Parse an include statement, and retrieve and parse the file it mentions.
958 * base should point to a location where it's safe to store the file, and
959 * nest_level should indicate how many nested includes have occurred. For this
960 * include, nest_level has already been incremented and doesn't need to be
963 static int handle_include(char **c, char *base,
964 struct pxe_menu *cfg, int nest_level)
970 err = parse_sliteral(c, &include_path);
973 printf("Expected include path: %.*s\n",
978 err = get_pxe_file(include_path, base);
981 printf("Couldn't retrieve %s\n", include_path);
985 return parse_pxefile_top(base, cfg, nest_level);
989 * Parse lines that begin with 'menu'.
991 * b and nest are provided to handle the 'menu include' case.
993 * b should be the address where the file currently being parsed is stored.
995 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
996 * a file it includes, 3 when parsing a file included by that file, and so on.
998 static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
1004 get_token(c, &t, L_KEYWORD);
1008 err = parse_sliteral(c, &cfg->title);
1013 err = handle_include(c, b + strlen(b) + 1, cfg,
1018 printf("Ignoring malformed menu command: %.*s\n",
1031 * Handles parsing a 'menu line' when we're parsing a label.
1033 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1034 struct pxe_label *label)
1041 get_token(c, &t, L_KEYWORD);
1045 if (!cfg->default_label)
1046 cfg->default_label = strdup(label->name);
1048 if (!cfg->default_label)
1053 parse_sliteral(c, &label->menu);
1056 printf("Ignoring malformed menu command: %.*s\n",
1066 * Parses a label and adds it to the list of labels for a menu.
1068 * A label ends when we either get to the end of a file, or
1069 * get some input we otherwise don't have a handler defined
1073 static int parse_label(char **c, struct pxe_menu *cfg)
1078 struct pxe_label *label;
1081 label = label_create();
1085 err = parse_sliteral(c, &label->name);
1087 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1088 label_destroy(label);
1092 list_add_tail(&label->list, &cfg->labels);
1096 get_token(c, &t, L_KEYWORD);
1101 err = parse_label_menu(c, cfg, label);
1106 err = parse_sliteral(c, &label->kernel);
1110 err = parse_sliteral(c, &label->append);
1113 s = strstr(label->append, "initrd=");
1117 len = (int)(strchr(s, ' ') - s);
1118 label->initrd = malloc(len + 1);
1119 strncpy(label->initrd, s, len);
1120 label->initrd[len] = '\0';
1126 err = parse_sliteral(c, &label->initrd);
1131 err = parse_sliteral(c, &label->fdt);
1135 label->localboot = 1;
1136 err = parse_integer(c, &label->localboot_val);
1140 err = parse_integer(c, &label->ipappend);
1148 * put the token back! we don't want it - it's the end
1149 * of a label and whatever token this is, it's
1150 * something for the menu level context to handle.
1162 * This 16 comes from the limit pxelinux imposes on nested includes.
1164 * There is no reason at all we couldn't do more, but some limit helps prevent
1165 * infinite (until crash occurs) recursion if a file tries to include itself.
1167 #define MAX_NEST_LEVEL 16
1170 * Entry point for parsing a menu file. nest_level indicates how many times
1171 * we've nested in includes. It will be 1 for the top level menu file.
1173 * Returns 1 on success, < 0 on error.
1175 static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level)
1178 char *s, *b, *label_name;
1183 if (nest_level > MAX_NEST_LEVEL) {
1184 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1191 get_token(&p, &t, L_KEYWORD);
1197 err = parse_menu(&p, cfg, b, nest_level);
1201 err = parse_integer(&p, &cfg->timeout);
1205 err = parse_label(&p, cfg);
1210 err = parse_sliteral(&p, &label_name);
1213 if (cfg->default_label)
1214 free(cfg->default_label);
1216 cfg->default_label = label_name;
1222 err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg,
1237 printf("Ignoring unknown command: %.*s\n",
1248 * Free the memory used by a pxe_menu and its labels.
1250 static void destroy_pxe_menu(struct pxe_menu *cfg)
1252 struct list_head *pos, *n;
1253 struct pxe_label *label;
1258 if (cfg->default_label)
1259 free(cfg->default_label);
1261 list_for_each_safe(pos, n, &cfg->labels) {
1262 label = list_entry(pos, struct pxe_label, list);
1264 label_destroy(label);
1271 * Entry point for parsing a pxe file. This is only used for the top level
1274 * Returns NULL if there is an error, otherwise, returns a pointer to a
1275 * pxe_menu struct populated with the results of parsing the pxe file (and any
1276 * files it includes). The resulting pxe_menu struct can be free()'d by using
1277 * the destroy_pxe_menu() function.
1279 static struct pxe_menu *parse_pxefile(char *menucfg)
1281 struct pxe_menu *cfg;
1283 cfg = malloc(sizeof(struct pxe_menu));
1288 memset(cfg, 0, sizeof(struct pxe_menu));
1290 INIT_LIST_HEAD(&cfg->labels);
1292 if (parse_pxefile_top(menucfg, cfg, 1) < 0) {
1293 destroy_pxe_menu(cfg);
1301 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1304 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1306 struct pxe_label *label;
1307 struct list_head *pos;
1311 char *default_num = NULL;
1314 * Create a menu and add items for all the labels.
1316 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1322 list_for_each(pos, &cfg->labels) {
1323 label = list_entry(pos, struct pxe_label, list);
1325 sprintf(label->num, "%d", i++);
1326 if (menu_item_add(m, label->num, label) != 1) {
1330 if (cfg->default_label &&
1331 (strcmp(label->name, cfg->default_label) == 0))
1332 default_num = label->num;
1337 * After we've created items for each label in the menu, set the
1338 * menu's default label if one was specified.
1341 err = menu_default_set(m, default_num);
1343 if (err != -ENOENT) {
1348 printf("Missing default: %s\n", cfg->default_label);
1356 * Try to boot any labels we have yet to attempt to boot.
1358 static void boot_unattempted_labels(struct pxe_menu *cfg)
1360 struct list_head *pos;
1361 struct pxe_label *label;
1363 list_for_each(pos, &cfg->labels) {
1364 label = list_entry(pos, struct pxe_label, list);
1366 if (!label->attempted)
1372 * Boot the system as prescribed by a pxe_menu.
1374 * Use the menu system to either get the user's choice or the default, based
1375 * on config or user input. If there is no default or user's choice,
1376 * attempted to boot labels in the order they were given in pxe files.
1377 * If the default or user's choice fails to boot, attempt to boot other
1378 * labels in the order they were given in pxe files.
1380 * If this function returns, there weren't any labels that successfully
1381 * booted, or the user interrupted the menu selection via ctrl+c.
1383 static void handle_pxe_menu(struct pxe_menu *cfg)
1389 m = pxe_menu_to_menu(cfg);
1393 err = menu_get_choice(m, &choice);
1398 * err == 1 means we got a choice back from menu_get_choice.
1400 * err == -ENOENT if the menu was setup to select the default but no
1401 * default was set. in that case, we should continue trying to boot
1402 * labels that haven't been attempted yet.
1404 * otherwise, the user interrupted or there was some other error and
1409 err = label_boot(choice);
1412 } else if (err != -ENOENT) {
1416 boot_unattempted_labels(cfg);
1420 * Boots a system using a pxe file
1422 * Returns 0 on success, 1 on error.
1425 do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1427 unsigned long pxefile_addr_r;
1428 struct pxe_menu *cfg;
1429 char *pxefile_addr_str;
1431 do_getfile = do_get_tftp;
1434 pxefile_addr_str = from_env("pxefile_addr_r");
1435 if (!pxefile_addr_str)
1438 } else if (argc == 2) {
1439 pxefile_addr_str = argv[1];
1441 return CMD_RET_USAGE;
1444 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1445 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1449 cfg = parse_pxefile((char *)(pxefile_addr_r));
1452 printf("Error parsing config file\n");
1456 handle_pxe_menu(cfg);
1458 destroy_pxe_menu(cfg);
1463 static cmd_tbl_t cmd_pxe_sub[] = {
1464 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1465 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1468 int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1473 return CMD_RET_USAGE;
1475 /* drop initial "pxe" arg */
1479 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1482 return cp->cmd(cmdtp, flag, argc, argv);
1484 return CMD_RET_USAGE;
1489 "commands to get and boot from pxe files",
1490 "get - try to retrieve a pxe file using tftp\npxe "
1491 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1495 * Boots a system using a local disk syslinux/extlinux file
1497 * Returns 0 on success, 1 on error.
1499 int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1501 unsigned long pxefile_addr_r;
1502 struct pxe_menu *cfg;
1503 char *pxefile_addr_str;
1507 if (strstr(argv[1], "-p")) {
1514 return cmd_usage(cmdtp);
1517 pxefile_addr_str = from_env("pxefile_addr_r");
1518 if (!pxefile_addr_str)
1521 pxefile_addr_str = argv[4];
1525 filename = getenv("bootfile");
1528 setenv("bootfile", filename);
1531 if (strstr(argv[3], "ext2"))
1532 do_getfile = do_get_ext2;
1533 else if (strstr(argv[3], "fat"))
1534 do_getfile = do_get_fat;
1536 printf("Invalid filesystem: %s\n", argv[3]);
1539 fs_argv[1] = argv[1];
1540 fs_argv[2] = argv[2];
1542 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1543 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1547 if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) {
1548 printf("Error reading config file\n");
1552 cfg = parse_pxefile((char *)(pxefile_addr_r));
1555 printf("Error parsing config file\n");
1562 handle_pxe_menu(cfg);
1564 destroy_pxe_menu(cfg);
1570 sysboot, 7, 1, do_sysboot,
1571 "command to get and boot from syslinux files",
1572 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1573 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1574 " filesystem on 'dev' on 'interface' to address 'addr'"