]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - common/cmd_pxe.c
Merge branch 'master' of git://git.denx.de/u-boot-mpc85xx
[karo-tx-uboot.git] / common / cmd_pxe.c
1 /*
2  * Copyright 2010-2011 Calxeda, Inc.
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 #include <common.h>
8 #include <command.h>
9 #include <malloc.h>
10 #include <linux/string.h>
11 #include <linux/ctype.h>
12 #include <errno.h>
13 #include <linux/list.h>
14 #include <fs.h>
15
16 #include "menu.h"
17
18 #define MAX_TFTP_PATH_LEN 127
19
20 const char *pxe_default_paths[] = {
21 #ifdef CONFIG_SYS_SOC
22         "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
23 #endif
24         "default-" CONFIG_SYS_ARCH,
25         "default",
26         NULL
27 };
28
29 static bool is_pxe;
30
31 /*
32  * Like getenv, but prints an error if envvar isn't defined in the
33  * environment.  It always returns what getenv does, so it can be used in
34  * place of getenv without changing error handling otherwise.
35  */
36 static char *from_env(const char *envvar)
37 {
38         char *ret;
39
40         ret = getenv(envvar);
41
42         if (!ret)
43                 printf("missing environment variable: %s\n", envvar);
44
45         return ret;
46 }
47
48 /*
49  * Convert an ethaddr from the environment to the format used by pxelinux
50  * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
51  * the beginning of the ethernet address to indicate a hardware type of
52  * Ethernet. Also converts uppercase hex characters into lowercase, to match
53  * pxelinux's behavior.
54  *
55  * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
56  * environment, or some other value < 0 on error.
57  */
58 static int format_mac_pxe(char *outbuf, size_t outbuf_len)
59 {
60         uchar ethaddr[6];
61
62         if (outbuf_len < 21) {
63                 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
64
65                 return -EINVAL;
66         }
67
68         if (!eth_getenv_enetaddr_by_index("eth", eth_get_dev_index(),
69                                           ethaddr))
70                 return -ENOENT;
71
72         sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
73                 ethaddr[0], ethaddr[1], ethaddr[2],
74                 ethaddr[3], ethaddr[4], ethaddr[5]);
75
76         return 1;
77 }
78
79 /*
80  * Returns the directory the file specified in the bootfile env variable is
81  * in. If bootfile isn't defined in the environment, return NULL, which should
82  * be interpreted as "don't prepend anything to paths".
83  */
84 static int get_bootfile_path(const char *file_path, char *bootfile_path,
85                              size_t bootfile_path_size)
86 {
87         char *bootfile, *last_slash;
88         size_t path_len = 0;
89
90         /* Only syslinux allows absolute paths */
91         if (file_path[0] == '/' && !is_pxe)
92                 goto ret;
93
94         bootfile = from_env("bootfile");
95
96         if (!bootfile)
97                 goto ret;
98
99         last_slash = strrchr(bootfile, '/');
100
101         if (last_slash == NULL)
102                 goto ret;
103
104         path_len = (last_slash - bootfile) + 1;
105
106         if (bootfile_path_size < path_len) {
107                 printf("bootfile_path too small. (%zd < %zd)\n",
108                                 bootfile_path_size, path_len);
109
110                 return -1;
111         }
112
113         strncpy(bootfile_path, bootfile, path_len);
114
115  ret:
116         bootfile_path[path_len] = '\0';
117
118         return 1;
119 }
120
121 static int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
122
123 static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
124 {
125         char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
126
127         tftp_argv[1] = file_addr;
128         tftp_argv[2] = (void *)file_path;
129
130         if (do_tftpb(cmdtp, 0, 3, tftp_argv))
131                 return -ENOENT;
132
133         return 1;
134 }
135
136 static char *fs_argv[5];
137
138 static int do_get_ext2(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
139 {
140 #ifdef CONFIG_CMD_EXT2
141         fs_argv[0] = "ext2load";
142         fs_argv[3] = file_addr;
143         fs_argv[4] = (void *)file_path;
144
145         if (!do_ext2load(cmdtp, 0, 5, fs_argv))
146                 return 1;
147 #endif
148         return -ENOENT;
149 }
150
151 static int do_get_fat(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
152 {
153 #ifdef CONFIG_CMD_FAT
154         fs_argv[0] = "fatload";
155         fs_argv[3] = file_addr;
156         fs_argv[4] = (void *)file_path;
157
158         if (!do_fat_fsload(cmdtp, 0, 5, fs_argv))
159                 return 1;
160 #endif
161         return -ENOENT;
162 }
163
164 static int do_get_any(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
165 {
166 #ifdef CONFIG_CMD_FS_GENERIC
167         fs_argv[0] = "load";
168         fs_argv[3] = file_addr;
169         fs_argv[4] = (void *)file_path;
170
171         if (!do_load(cmdtp, 0, 5, fs_argv, FS_TYPE_ANY))
172                 return 1;
173 #endif
174         return -ENOENT;
175 }
176
177 /*
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.
182  *
183  * Returns 1 for success, or < 0 on error.
184  */
185 static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
186 {
187         size_t path_len;
188         char relfile[MAX_TFTP_PATH_LEN+1];
189         char addr_buf[10];
190         int err;
191
192         err = get_bootfile_path(file_path, relfile, sizeof(relfile));
193
194         if (err < 0)
195                 return err;
196
197         path_len = strlen(file_path);
198         path_len += strlen(relfile);
199
200         if (path_len > MAX_TFTP_PATH_LEN) {
201                 printf("Base path too long (%s%s)\n",
202                                         relfile,
203                                         file_path);
204
205                 return -ENAMETOOLONG;
206         }
207
208         strcat(relfile, file_path);
209
210         printf("Retrieving file: %s\n", relfile);
211
212         sprintf(addr_buf, "%p", file_addr);
213
214         return do_getfile(cmdtp, relfile, addr_buf);
215 }
216
217 /*
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.
221  *
222  * Returns 1 on success, or < 0 for error.
223  */
224 static int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path, void *file_addr)
225 {
226         unsigned long config_file_size;
227         char *tftp_filesize;
228         int err;
229
230         err = get_relfile(cmdtp, file_path, file_addr);
231
232         if (err < 0)
233                 return err;
234
235         /*
236          * the file comes without a NUL byte at the end, so find out its size
237          * and add the NUL byte.
238          */
239         tftp_filesize = from_env("filesize");
240
241         if (!tftp_filesize)
242                 return -ENOENT;
243
244         if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
245                 return -EINVAL;
246
247         *(char *)(file_addr + config_file_size) = '\0';
248
249         return 1;
250 }
251
252 #define PXELINUX_DIR "pxelinux.cfg/"
253
254 /*
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.
258  *
259  * Returns 1 on success or < 0 on error.
260  */
261 static int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file, void *pxefile_addr_r)
262 {
263         size_t base_len = strlen(PXELINUX_DIR);
264         char path[MAX_TFTP_PATH_LEN+1];
265
266         if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
267                 printf("path (%s%s) too long, skipping\n",
268                                 PXELINUX_DIR, file);
269                 return -ENAMETOOLONG;
270         }
271
272         sprintf(path, PXELINUX_DIR "%s", file);
273
274         return get_pxe_file(cmdtp, path, pxefile_addr_r);
275 }
276
277 /*
278  * Looks for a pxe file with a name based on the pxeuuid environment variable.
279  *
280  * Returns 1 on success or < 0 on error.
281  */
282 static int pxe_uuid_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
283 {
284         char *uuid_str;
285
286         uuid_str = from_env("pxeuuid");
287
288         if (!uuid_str)
289                 return -ENOENT;
290
291         return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
292 }
293
294 /*
295  * Looks for a pxe file with a name based on the 'ethaddr' environment
296  * variable.
297  *
298  * Returns 1 on success or < 0 on error.
299  */
300 static int pxe_mac_path(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
301 {
302         char mac_str[21];
303         int err;
304
305         err = format_mac_pxe(mac_str, sizeof(mac_str));
306
307         if (err < 0)
308                 return err;
309
310         return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
311 }
312
313 /*
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
316  * that exactly.
317  *
318  * Returns 1 on success or < 0 on error.
319  */
320 static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, void *pxefile_addr_r)
321 {
322         char ip_addr[9];
323         int mask_pos, err;
324
325         sprintf(ip_addr, "%08X", ntohl(NetOurIP));
326
327         for (mask_pos = 7; mask_pos >= 0;  mask_pos--) {
328                 err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
329
330                 if (err > 0)
331                         return err;
332
333                 ip_addr[mask_pos] = '\0';
334         }
335
336         return -ENOENT;
337 }
338
339 /*
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.
344  *
345  * UUID comes from pxeuuid env variable, if defined
346  * MAC addr comes from ethaddr env variable, if defined
347  * IP
348  *
349  * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
350  *
351  * Returns 0 on success or 1 on error.
352  */
353 static int
354 do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
355 {
356         char *pxefile_addr_str;
357         unsigned long pxefile_addr_r;
358         int err, i = 0;
359
360         do_getfile = do_get_tftp;
361
362         if (argc != 1)
363                 return CMD_RET_USAGE;
364
365         pxefile_addr_str = from_env("pxefile_addr_r");
366
367         if (!pxefile_addr_str)
368                 return 1;
369
370         err = strict_strtoul(pxefile_addr_str, 16,
371                                 (unsigned long *)&pxefile_addr_r);
372         if (err < 0)
373                 return 1;
374
375         /*
376          * Keep trying paths until we successfully get a file we're looking
377          * for.
378          */
379         if (pxe_uuid_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
380             pxe_mac_path(cmdtp, (void *)pxefile_addr_r) > 0 ||
381             pxe_ipaddr_paths(cmdtp, (void *)pxefile_addr_r) > 0) {
382                 printf("Config file found\n");
383
384                 return 0;
385         }
386
387         while (pxe_default_paths[i]) {
388                 if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
389                                       (void *)pxefile_addr_r) > 0) {
390                         printf("Config file found\n");
391                         return 0;
392                 }
393                 i++;
394         }
395
396         printf("Config file not found\n");
397
398         return 1;
399 }
400
401 /*
402  * Wrapper to make it easier to store the file at file_path in the location
403  * specified by envaddr_name. file_path will be joined to the bootfile path,
404  * if any is specified.
405  *
406  * Returns 1 on success or < 0 on error.
407  */
408 static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path, const char *envaddr_name)
409 {
410         unsigned long file_addr;
411         char *envaddr;
412
413         envaddr = from_env(envaddr_name);
414
415         if (!envaddr)
416                 return -ENOENT;
417
418         if (strict_strtoul(envaddr, 16, &file_addr) < 0)
419                 return -EINVAL;
420
421         return get_relfile(cmdtp, file_path, (void *)file_addr);
422 }
423
424 /*
425  * A note on the pxe file parser.
426  *
427  * We're parsing files that use syslinux grammar, which has a few quirks.
428  * String literals must be recognized based on context - there is no
429  * quoting or escaping support. There's also nothing to explicitly indicate
430  * when a label section completes. We deal with that by ending a label
431  * section whenever we see a line that doesn't include.
432  *
433  * As with the syslinux family, this same file format could be reused in the
434  * future for non pxe purposes. The only action it takes during parsing that
435  * would throw this off is handling of include files. It assumes we're using
436  * pxe, and does a tftp download of a file listed as an include file in the
437  * middle of the parsing operation. That could be handled by refactoring it to
438  * take a 'include file getter' function.
439  */
440
441 /*
442  * Describes a single label given in a pxe file.
443  *
444  * Create these with the 'label_create' function given below.
445  *
446  * name - the name of the menu as given on the 'menu label' line.
447  * kernel - the path to the kernel file to use for this label.
448  * append - kernel command line to use when booting this label
449  * initrd - path to the initrd to use for this label.
450  * attempted - 0 if we haven't tried to boot this label, 1 if we have.
451  * localboot - 1 if this label specified 'localboot', 0 otherwise.
452  * list - lets these form a list, which a pxe_menu struct will hold.
453  */
454 struct pxe_label {
455         char num[4];
456         char *name;
457         char *menu;
458         char *kernel;
459         char *append;
460         char *initrd;
461         char *fdt;
462         char *fdtdir;
463         int ipappend;
464         int attempted;
465         int localboot;
466         int localboot_val;
467         struct list_head list;
468 };
469
470 /*
471  * Describes a pxe menu as given via pxe files.
472  *
473  * title - the name of the menu as given by a 'menu title' line.
474  * default_label - the name of the default label, if any.
475  * timeout - time in tenths of a second to wait for a user key-press before
476  *           booting the default label.
477  * prompt - if 0, don't prompt for a choice unless the timeout period is
478  *          interrupted.  If 1, always prompt for a choice regardless of
479  *          timeout.
480  * labels - a list of labels defined for the menu.
481  */
482 struct pxe_menu {
483         char *title;
484         char *default_label;
485         int timeout;
486         int prompt;
487         struct list_head labels;
488 };
489
490 /*
491  * Allocates memory for and initializes a pxe_label. This uses malloc, so the
492  * result must be free()'d to reclaim the memory.
493  *
494  * Returns NULL if malloc fails.
495  */
496 static struct pxe_label *label_create(void)
497 {
498         struct pxe_label *label;
499
500         label = malloc(sizeof(struct pxe_label));
501
502         if (!label)
503                 return NULL;
504
505         memset(label, 0, sizeof(struct pxe_label));
506
507         return label;
508 }
509
510 /*
511  * Free the memory used by a pxe_label, including that used by its name,
512  * kernel, append and initrd members, if they're non NULL.
513  *
514  * So - be sure to only use dynamically allocated memory for the members of
515  * the pxe_label struct, unless you want to clean it up first. These are
516  * currently only created by the pxe file parsing code.
517  */
518 static void label_destroy(struct pxe_label *label)
519 {
520         if (label->name)
521                 free(label->name);
522
523         if (label->kernel)
524                 free(label->kernel);
525
526         if (label->append)
527                 free(label->append);
528
529         if (label->initrd)
530                 free(label->initrd);
531
532         if (label->fdt)
533                 free(label->fdt);
534
535         if (label->fdtdir)
536                 free(label->fdtdir);
537
538         free(label);
539 }
540
541 /*
542  * Print a label and its string members if they're defined.
543  *
544  * This is passed as a callback to the menu code for displaying each
545  * menu entry.
546  */
547 static void label_print(void *data)
548 {
549         struct pxe_label *label = data;
550         const char *c = label->menu ? label->menu : label->name;
551
552         printf("%s:\t%s\n", label->num, c);
553 }
554
555 /*
556  * Boot a label that specified 'localboot'. This requires that the 'localcmd'
557  * environment variable is defined. Its contents will be executed as U-boot
558  * command.  If the label specified an 'append' line, its contents will be
559  * used to overwrite the contents of the 'bootargs' environment variable prior
560  * to running 'localcmd'.
561  *
562  * Returns 1 on success or < 0 on error.
563  */
564 static int label_localboot(struct pxe_label *label)
565 {
566         char *localcmd;
567
568         localcmd = from_env("localcmd");
569
570         if (!localcmd)
571                 return -ENOENT;
572
573         if (label->append)
574                 setenv("bootargs", label->append);
575
576         debug("running: %s\n", localcmd);
577
578         return run_command_list(localcmd, strlen(localcmd), 0);
579 }
580
581 /*
582  * Boot according to the contents of a pxe_label.
583  *
584  * If we can't boot for any reason, we return.  A successful boot never
585  * returns.
586  *
587  * The kernel will be stored in the location given by the 'kernel_addr_r'
588  * environment variable.
589  *
590  * If the label specifies an initrd file, it will be stored in the location
591  * given by the 'ramdisk_addr_r' environment variable.
592  *
593  * If the label specifies an 'append' line, its contents will overwrite that
594  * of the 'bootargs' environment variable.
595  */
596 static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
597 {
598         char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
599         char initrd_str[22];
600         char mac_str[29] = "";
601         char ip_str[68] = "";
602         char *bootargs;
603         int bootm_argc = 3;
604         int len = 0;
605
606         label_print(label);
607
608         label->attempted = 1;
609
610         if (label->localboot) {
611                 if (label->localboot_val >= 0)
612                         label_localboot(label);
613                 return 0;
614         }
615
616         if (label->kernel == NULL) {
617                 printf("No kernel given, skipping %s\n",
618                                 label->name);
619                 return 1;
620         }
621
622         if (label->initrd) {
623                 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
624                         printf("Skipping %s for failure retrieving initrd\n",
625                                         label->name);
626                         return 1;
627                 }
628
629                 bootm_argv[2] = initrd_str;
630                 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
631                 strcat(bootm_argv[2], ":");
632                 strcat(bootm_argv[2], getenv("filesize"));
633         } else {
634                 bootm_argv[2] = "-";
635         }
636
637         if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
638                 printf("Skipping %s for failure retrieving kernel\n",
639                                 label->name);
640                 return 1;
641         }
642
643         if (label->ipappend & 0x1) {
644                 sprintf(ip_str, " ip=%s:%s:%s:%s",
645                         getenv("ipaddr"), getenv("serverip"),
646                         getenv("gatewayip"), getenv("netmask"));
647                 len += strlen(ip_str);
648         }
649
650         if (label->ipappend & 0x2) {
651                 int err;
652                 strcpy(mac_str, " BOOTIF=");
653                 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
654                 if (err < 0)
655                         mac_str[0] = '\0';
656                 len += strlen(mac_str);
657         }
658
659         if (label->append)
660                 len += strlen(label->append);
661
662         if (len) {
663                 bootargs = malloc(len + 1);
664                 if (!bootargs)
665                         return 1;
666                 bootargs[0] = '\0';
667                 if (label->append)
668                         strcpy(bootargs, label->append);
669                 strcat(bootargs, ip_str);
670                 strcat(bootargs, mac_str);
671
672                 setenv("bootargs", bootargs);
673                 printf("append: %s\n", bootargs);
674
675                 free(bootargs);
676         }
677
678         bootm_argv[1] = getenv("kernel_addr_r");
679
680         /*
681          * fdt usage is optional:
682          * It handles the following scenarios. All scenarios are exclusive
683          *
684          * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
685          * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
686          * and adjust argc appropriately.
687          *
688          * Scenario 2: If there is an fdt_addr specified, pass it along to
689          * bootm, and adjust argc appropriately.
690          *
691          * Scenario 3: fdt blob is not available.
692          */
693         bootm_argv[3] = getenv("fdt_addr_r");
694
695         /* if fdt label is defined then get fdt from server */
696         if (bootm_argv[3]) {
697                 char *fdtfile = NULL;
698                 char *fdtfilefree = NULL;
699
700                 if (label->fdt) {
701                         fdtfile = label->fdt;
702                 } else if (label->fdtdir) {
703                         char *f1, *f2, *f3, *f4, *slash;
704
705                         f1 = getenv("fdtfile");
706                         if (f1) {
707                                 f2 = "";
708                                 f3 = "";
709                                 f4 = "";
710                         } else {
711                                 /*
712                                  * For complex cases where this code doesn't
713                                  * generate the correct filename, the board
714                                  * code should set $fdtfile during early boot,
715                                  * or the boot scripts should set $fdtfile
716                                  * before invoking "pxe" or "sysboot".
717                                  */
718                                 f1 = getenv("soc");
719                                 f2 = "-";
720                                 f3 = getenv("board");
721                                 f4 = ".dtb";
722                         }
723
724                         len = strlen(label->fdtdir);
725                         if (!len)
726                                 slash = "./";
727                         else if (label->fdtdir[len - 1] != '/')
728                                 slash = "/";
729                         else
730                                 slash = "";
731
732                         len = strlen(label->fdtdir) + strlen(slash) +
733                                 strlen(f1) + strlen(f2) + strlen(f3) +
734                                 strlen(f4) + 1;
735                         fdtfilefree = malloc(len);
736                         if (!fdtfilefree) {
737                                 printf("malloc fail (FDT filename)\n");
738                                 return 1;
739                         }
740
741                         snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
742                                  label->fdtdir, slash, f1, f2, f3, f4);
743                         fdtfile = fdtfilefree;
744                 }
745
746                 if (fdtfile) {
747                         int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
748                         free(fdtfilefree);
749                         if (err < 0) {
750                                 printf("Skipping %s for failure retrieving fdt\n",
751                                                 label->name);
752                                 return 1;
753                         }
754                 } else {
755                         bootm_argv[3] = NULL;
756                 }
757         }
758
759         if (!bootm_argv[3])
760                 bootm_argv[3] = getenv("fdt_addr");
761
762         if (bootm_argv[3])
763                 bootm_argc = 4;
764
765         do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
766
767 #ifdef CONFIG_CMD_BOOTZ
768         /* Try booting a zImage if do_bootm returns */
769         do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
770 #endif
771         return 1;
772 }
773
774 /*
775  * Tokens for the pxe file parser.
776  */
777 enum token_type {
778         T_EOL,
779         T_STRING,
780         T_EOF,
781         T_MENU,
782         T_TITLE,
783         T_TIMEOUT,
784         T_LABEL,
785         T_KERNEL,
786         T_LINUX,
787         T_APPEND,
788         T_INITRD,
789         T_LOCALBOOT,
790         T_DEFAULT,
791         T_PROMPT,
792         T_INCLUDE,
793         T_FDT,
794         T_FDTDIR,
795         T_ONTIMEOUT,
796         T_IPAPPEND,
797         T_INVALID
798 };
799
800 /*
801  * A token - given by a value and a type.
802  */
803 struct token {
804         char *val;
805         enum token_type type;
806 };
807
808 /*
809  * Keywords recognized.
810  */
811 static const struct token keywords[] = {
812         {"menu", T_MENU},
813         {"title", T_TITLE},
814         {"timeout", T_TIMEOUT},
815         {"default", T_DEFAULT},
816         {"prompt", T_PROMPT},
817         {"label", T_LABEL},
818         {"kernel", T_KERNEL},
819         {"linux", T_LINUX},
820         {"localboot", T_LOCALBOOT},
821         {"append", T_APPEND},
822         {"initrd", T_INITRD},
823         {"include", T_INCLUDE},
824         {"devicetree", T_FDT},
825         {"fdt", T_FDT},
826         {"devicetreedir", T_FDTDIR},
827         {"fdtdir", T_FDTDIR},
828         {"ontimeout", T_ONTIMEOUT,},
829         {"ipappend", T_IPAPPEND,},
830         {NULL, T_INVALID}
831 };
832
833 /*
834  * Since pxe(linux) files don't have a token to identify the start of a
835  * literal, we have to keep track of when we're in a state where a literal is
836  * expected vs when we're in a state a keyword is expected.
837  */
838 enum lex_state {
839         L_NORMAL = 0,
840         L_KEYWORD,
841         L_SLITERAL
842 };
843
844 /*
845  * get_string retrieves a string from *p and stores it as a token in
846  * *t.
847  *
848  * get_string used for scanning both string literals and keywords.
849  *
850  * Characters from *p are copied into t-val until a character equal to
851  * delim is found, or a NUL byte is reached. If delim has the special value of
852  * ' ', any whitespace character will be used as a delimiter.
853  *
854  * If lower is unequal to 0, uppercase characters will be converted to
855  * lowercase in the result. This is useful to make keywords case
856  * insensitive.
857  *
858  * The location of *p is updated to point to the first character after the end
859  * of the token - the ending delimiter.
860  *
861  * On success, the new value of t->val is returned. Memory for t->val is
862  * allocated using malloc and must be free()'d to reclaim it.  If insufficient
863  * memory is available, NULL is returned.
864  */
865 static char *get_string(char **p, struct token *t, char delim, int lower)
866 {
867         char *b, *e;
868         size_t len, i;
869
870         /*
871          * b and e both start at the beginning of the input stream.
872          *
873          * e is incremented until we find the ending delimiter, or a NUL byte
874          * is reached. Then, we take e - b to find the length of the token.
875          */
876         b = e = *p;
877
878         while (*e) {
879                 if ((delim == ' ' && isspace(*e)) || delim == *e)
880                         break;
881                 e++;
882         }
883
884         len = e - b;
885
886         /*
887          * Allocate memory to hold the string, and copy it in, converting
888          * characters to lowercase if lower is != 0.
889          */
890         t->val = malloc(len + 1);
891         if (!t->val)
892                 return NULL;
893
894         for (i = 0; i < len; i++, b++) {
895                 if (lower)
896                         t->val[i] = tolower(*b);
897                 else
898                         t->val[i] = *b;
899         }
900
901         t->val[len] = '\0';
902
903         /*
904          * Update *p so the caller knows where to continue scanning.
905          */
906         *p = e;
907
908         t->type = T_STRING;
909
910         return t->val;
911 }
912
913 /*
914  * Populate a keyword token with a type and value.
915  */
916 static void get_keyword(struct token *t)
917 {
918         int i;
919
920         for (i = 0; keywords[i].val; i++) {
921                 if (!strcmp(t->val, keywords[i].val)) {
922                         t->type = keywords[i].type;
923                         break;
924                 }
925         }
926 }
927
928 /*
929  * Get the next token.  We have to keep track of which state we're in to know
930  * if we're looking to get a string literal or a keyword.
931  *
932  * *p is updated to point at the first character after the current token.
933  */
934 static void get_token(char **p, struct token *t, enum lex_state state)
935 {
936         char *c = *p;
937
938         t->type = T_INVALID;
939
940         /* eat non EOL whitespace */
941         while (isblank(*c))
942                 c++;
943
944         /*
945          * eat comments. note that string literals can't begin with #, but
946          * can contain a # after their first character.
947          */
948         if (*c == '#') {
949                 while (*c && *c != '\n')
950                         c++;
951         }
952
953         if (*c == '\n') {
954                 t->type = T_EOL;
955                 c++;
956         } else if (*c == '\0') {
957                 t->type = T_EOF;
958                 c++;
959         } else if (state == L_SLITERAL) {
960                 get_string(&c, t, '\n', 0);
961         } else if (state == L_KEYWORD) {
962                 /*
963                  * when we expect a keyword, we first get the next string
964                  * token delimited by whitespace, and then check if it
965                  * matches a keyword in our keyword list. if it does, it's
966                  * converted to a keyword token of the appropriate type, and
967                  * if not, it remains a string token.
968                  */
969                 get_string(&c, t, ' ', 1);
970                 get_keyword(t);
971         }
972
973         *p = c;
974 }
975
976 /*
977  * Increment *c until we get to the end of the current line, or EOF.
978  */
979 static void eol_or_eof(char **c)
980 {
981         while (**c && **c != '\n')
982                 (*c)++;
983 }
984
985 /*
986  * All of these parse_* functions share some common behavior.
987  *
988  * They finish with *c pointing after the token they parse, and return 1 on
989  * success, or < 0 on error.
990  */
991
992 /*
993  * Parse a string literal and store a pointer it at *dst. String literals
994  * terminate at the end of the line.
995  */
996 static int parse_sliteral(char **c, char **dst)
997 {
998         struct token t;
999         char *s = *c;
1000
1001         get_token(c, &t, L_SLITERAL);
1002
1003         if (t.type != T_STRING) {
1004                 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1005                 return -EINVAL;
1006         }
1007
1008         *dst = t.val;
1009
1010         return 1;
1011 }
1012
1013 /*
1014  * Parse a base 10 (unsigned) integer and store it at *dst.
1015  */
1016 static int parse_integer(char **c, int *dst)
1017 {
1018         struct token t;
1019         char *s = *c;
1020
1021         get_token(c, &t, L_SLITERAL);
1022
1023         if (t.type != T_STRING) {
1024                 printf("Expected string: %.*s\n", (int)(*c - s), s);
1025                 return -EINVAL;
1026         }
1027
1028         *dst = simple_strtol(t.val, NULL, 10);
1029
1030         free(t.val);
1031
1032         return 1;
1033 }
1034
1035 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
1036
1037 /*
1038  * Parse an include statement, and retrieve and parse the file it mentions.
1039  *
1040  * base should point to a location where it's safe to store the file, and
1041  * nest_level should indicate how many nested includes have occurred. For this
1042  * include, nest_level has already been incremented and doesn't need to be
1043  * incremented here.
1044  */
1045 static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
1046                                 struct pxe_menu *cfg, int nest_level)
1047 {
1048         char *include_path;
1049         char *s = *c;
1050         int err;
1051
1052         err = parse_sliteral(c, &include_path);
1053
1054         if (err < 0) {
1055                 printf("Expected include path: %.*s\n",
1056                                  (int)(*c - s), s);
1057                 return err;
1058         }
1059
1060         err = get_pxe_file(cmdtp, include_path, base);
1061
1062         if (err < 0) {
1063                 printf("Couldn't retrieve %s\n", include_path);
1064                 return err;
1065         }
1066
1067         return parse_pxefile_top(cmdtp, base, cfg, nest_level);
1068 }
1069
1070 /*
1071  * Parse lines that begin with 'menu'.
1072  *
1073  * b and nest are provided to handle the 'menu include' case.
1074  *
1075  * b should be the address where the file currently being parsed is stored.
1076  *
1077  * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1078  * a file it includes, 3 when parsing a file included by that file, and so on.
1079  */
1080 static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg, char *b, int nest_level)
1081 {
1082         struct token t;
1083         char *s = *c;
1084         int err = 0;
1085
1086         get_token(c, &t, L_KEYWORD);
1087
1088         switch (t.type) {
1089         case T_TITLE:
1090                 err = parse_sliteral(c, &cfg->title);
1091
1092                 break;
1093
1094         case T_INCLUDE:
1095                 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
1096                                                 nest_level + 1);
1097                 break;
1098
1099         default:
1100                 printf("Ignoring malformed menu command: %.*s\n",
1101                                 (int)(*c - s), s);
1102         }
1103
1104         if (err < 0)
1105                 return err;
1106
1107         eol_or_eof(c);
1108
1109         return 1;
1110 }
1111
1112 /*
1113  * Handles parsing a 'menu line' when we're parsing a label.
1114  */
1115 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1116                                 struct pxe_label *label)
1117 {
1118         struct token t;
1119         char *s;
1120
1121         s = *c;
1122
1123         get_token(c, &t, L_KEYWORD);
1124
1125         switch (t.type) {
1126         case T_DEFAULT:
1127                 if (!cfg->default_label)
1128                         cfg->default_label = strdup(label->name);
1129
1130                 if (!cfg->default_label)
1131                         return -ENOMEM;
1132
1133                 break;
1134         case T_LABEL:
1135                 parse_sliteral(c, &label->menu);
1136                 break;
1137         default:
1138                 printf("Ignoring malformed menu command: %.*s\n",
1139                                 (int)(*c - s), s);
1140         }
1141
1142         eol_or_eof(c);
1143
1144         return 0;
1145 }
1146
1147 /*
1148  * Parses a label and adds it to the list of labels for a menu.
1149  *
1150  * A label ends when we either get to the end of a file, or
1151  * get some input we otherwise don't have a handler defined
1152  * for.
1153  *
1154  */
1155 static int parse_label(char **c, struct pxe_menu *cfg)
1156 {
1157         struct token t;
1158         int len;
1159         char *s = *c;
1160         struct pxe_label *label;
1161         int err;
1162
1163         label = label_create();
1164         if (!label)
1165                 return -ENOMEM;
1166
1167         err = parse_sliteral(c, &label->name);
1168         if (err < 0) {
1169                 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1170                 label_destroy(label);
1171                 return -EINVAL;
1172         }
1173
1174         list_add_tail(&label->list, &cfg->labels);
1175
1176         while (1) {
1177                 s = *c;
1178                 get_token(c, &t, L_KEYWORD);
1179
1180                 err = 0;
1181                 switch (t.type) {
1182                 case T_MENU:
1183                         err = parse_label_menu(c, cfg, label);
1184                         break;
1185
1186                 case T_KERNEL:
1187                 case T_LINUX:
1188                         err = parse_sliteral(c, &label->kernel);
1189                         break;
1190
1191                 case T_APPEND:
1192                         err = parse_sliteral(c, &label->append);
1193                         if (label->initrd)
1194                                 break;
1195                         s = strstr(label->append, "initrd=");
1196                         if (!s)
1197                                 break;
1198                         s += 7;
1199                         len = (int)(strchr(s, ' ') - s);
1200                         label->initrd = malloc(len + 1);
1201                         strncpy(label->initrd, s, len);
1202                         label->initrd[len] = '\0';
1203
1204                         break;
1205
1206                 case T_INITRD:
1207                         if (!label->initrd)
1208                                 err = parse_sliteral(c, &label->initrd);
1209                         break;
1210
1211                 case T_FDT:
1212                         if (!label->fdt)
1213                                 err = parse_sliteral(c, &label->fdt);
1214                         break;
1215
1216                 case T_FDTDIR:
1217                         if (!label->fdtdir)
1218                                 err = parse_sliteral(c, &label->fdtdir);
1219                         break;
1220
1221                 case T_LOCALBOOT:
1222                         label->localboot = 1;
1223                         err = parse_integer(c, &label->localboot_val);
1224                         break;
1225
1226                 case T_IPAPPEND:
1227                         err = parse_integer(c, &label->ipappend);
1228                         break;
1229
1230                 case T_EOL:
1231                         break;
1232
1233                 default:
1234                         /*
1235                          * put the token back! we don't want it - it's the end
1236                          * of a label and whatever token this is, it's
1237                          * something for the menu level context to handle.
1238                          */
1239                         *c = s;
1240                         return 1;
1241                 }
1242
1243                 if (err < 0)
1244                         return err;
1245         }
1246 }
1247
1248 /*
1249  * This 16 comes from the limit pxelinux imposes on nested includes.
1250  *
1251  * There is no reason at all we couldn't do more, but some limit helps prevent
1252  * infinite (until crash occurs) recursion if a file tries to include itself.
1253  */
1254 #define MAX_NEST_LEVEL 16
1255
1256 /*
1257  * Entry point for parsing a menu file. nest_level indicates how many times
1258  * we've nested in includes.  It will be 1 for the top level menu file.
1259  *
1260  * Returns 1 on success, < 0 on error.
1261  */
1262 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
1263 {
1264         struct token t;
1265         char *s, *b, *label_name;
1266         int err;
1267
1268         b = p;
1269
1270         if (nest_level > MAX_NEST_LEVEL) {
1271                 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1272                 return -EMLINK;
1273         }
1274
1275         while (1) {
1276                 s = p;
1277
1278                 get_token(&p, &t, L_KEYWORD);
1279
1280                 err = 0;
1281                 switch (t.type) {
1282                 case T_MENU:
1283                         cfg->prompt = 1;
1284                         err = parse_menu(cmdtp, &p, cfg, b, nest_level);
1285                         break;
1286
1287                 case T_TIMEOUT:
1288                         err = parse_integer(&p, &cfg->timeout);
1289                         break;
1290
1291                 case T_LABEL:
1292                         err = parse_label(&p, cfg);
1293                         break;
1294
1295                 case T_DEFAULT:
1296                 case T_ONTIMEOUT:
1297                         err = parse_sliteral(&p, &label_name);
1298
1299                         if (label_name) {
1300                                 if (cfg->default_label)
1301                                         free(cfg->default_label);
1302
1303                                 cfg->default_label = label_name;
1304                         }
1305
1306                         break;
1307
1308                 case T_INCLUDE:
1309                         err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
1310                                                         nest_level + 1);
1311                         break;
1312
1313                 case T_PROMPT:
1314                         eol_or_eof(&p);
1315                         break;
1316
1317                 case T_EOL:
1318                         break;
1319
1320                 case T_EOF:
1321                         return 1;
1322
1323                 default:
1324                         printf("Ignoring unknown command: %.*s\n",
1325                                                         (int)(p - s), s);
1326                         eol_or_eof(&p);
1327                 }
1328
1329                 if (err < 0)
1330                         return err;
1331         }
1332 }
1333
1334 /*
1335  * Free the memory used by a pxe_menu and its labels.
1336  */
1337 static void destroy_pxe_menu(struct pxe_menu *cfg)
1338 {
1339         struct list_head *pos, *n;
1340         struct pxe_label *label;
1341
1342         if (cfg->title)
1343                 free(cfg->title);
1344
1345         if (cfg->default_label)
1346                 free(cfg->default_label);
1347
1348         list_for_each_safe(pos, n, &cfg->labels) {
1349                 label = list_entry(pos, struct pxe_label, list);
1350
1351                 label_destroy(label);
1352         }
1353
1354         free(cfg);
1355 }
1356
1357 /*
1358  * Entry point for parsing a pxe file. This is only used for the top level
1359  * file.
1360  *
1361  * Returns NULL if there is an error, otherwise, returns a pointer to a
1362  * pxe_menu struct populated with the results of parsing the pxe file (and any
1363  * files it includes). The resulting pxe_menu struct can be free()'d by using
1364  * the destroy_pxe_menu() function.
1365  */
1366 static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
1367 {
1368         struct pxe_menu *cfg;
1369
1370         cfg = malloc(sizeof(struct pxe_menu));
1371
1372         if (!cfg)
1373                 return NULL;
1374
1375         memset(cfg, 0, sizeof(struct pxe_menu));
1376
1377         INIT_LIST_HEAD(&cfg->labels);
1378
1379         if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
1380                 destroy_pxe_menu(cfg);
1381                 return NULL;
1382         }
1383
1384         return cfg;
1385 }
1386
1387 /*
1388  * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1389  * menu code.
1390  */
1391 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1392 {
1393         struct pxe_label *label;
1394         struct list_head *pos;
1395         struct menu *m;
1396         int err;
1397         int i = 1;
1398         char *default_num = NULL;
1399
1400         /*
1401          * Create a menu and add items for all the labels.
1402          */
1403         m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1404                         NULL, NULL);
1405
1406         if (!m)
1407                 return NULL;
1408
1409         list_for_each(pos, &cfg->labels) {
1410                 label = list_entry(pos, struct pxe_label, list);
1411
1412                 sprintf(label->num, "%d", i++);
1413                 if (menu_item_add(m, label->num, label) != 1) {
1414                         menu_destroy(m);
1415                         return NULL;
1416                 }
1417                 if (cfg->default_label &&
1418                     (strcmp(label->name, cfg->default_label) == 0))
1419                         default_num = label->num;
1420
1421         }
1422
1423         /*
1424          * After we've created items for each label in the menu, set the
1425          * menu's default label if one was specified.
1426          */
1427         if (default_num) {
1428                 err = menu_default_set(m, default_num);
1429                 if (err != 1) {
1430                         if (err != -ENOENT) {
1431                                 menu_destroy(m);
1432                                 return NULL;
1433                         }
1434
1435                         printf("Missing default: %s\n", cfg->default_label);
1436                 }
1437         }
1438
1439         return m;
1440 }
1441
1442 /*
1443  * Try to boot any labels we have yet to attempt to boot.
1444  */
1445 static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1446 {
1447         struct list_head *pos;
1448         struct pxe_label *label;
1449
1450         list_for_each(pos, &cfg->labels) {
1451                 label = list_entry(pos, struct pxe_label, list);
1452
1453                 if (!label->attempted)
1454                         label_boot(cmdtp, label);
1455         }
1456 }
1457
1458 /*
1459  * Boot the system as prescribed by a pxe_menu.
1460  *
1461  * Use the menu system to either get the user's choice or the default, based
1462  * on config or user input.  If there is no default or user's choice,
1463  * attempted to boot labels in the order they were given in pxe files.
1464  * If the default or user's choice fails to boot, attempt to boot other
1465  * labels in the order they were given in pxe files.
1466  *
1467  * If this function returns, there weren't any labels that successfully
1468  * booted, or the user interrupted the menu selection via ctrl+c.
1469  */
1470 static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1471 {
1472         void *choice;
1473         struct menu *m;
1474         int err;
1475
1476         m = pxe_menu_to_menu(cfg);
1477         if (!m)
1478                 return;
1479
1480         err = menu_get_choice(m, &choice);
1481
1482         menu_destroy(m);
1483
1484         /*
1485          * err == 1 means we got a choice back from menu_get_choice.
1486          *
1487          * err == -ENOENT if the menu was setup to select the default but no
1488          * default was set. in that case, we should continue trying to boot
1489          * labels that haven't been attempted yet.
1490          *
1491          * otherwise, the user interrupted or there was some other error and
1492          * we give up.
1493          */
1494
1495         if (err == 1) {
1496                 err = label_boot(cmdtp, choice);
1497                 if (!err)
1498                         return;
1499         } else if (err != -ENOENT) {
1500                 return;
1501         }
1502
1503         boot_unattempted_labels(cmdtp, cfg);
1504 }
1505
1506 /*
1507  * Boots a system using a pxe file
1508  *
1509  * Returns 0 on success, 1 on error.
1510  */
1511 static int
1512 do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1513 {
1514         unsigned long pxefile_addr_r;
1515         struct pxe_menu *cfg;
1516         char *pxefile_addr_str;
1517
1518         do_getfile = do_get_tftp;
1519
1520         if (argc == 1) {
1521                 pxefile_addr_str = from_env("pxefile_addr_r");
1522                 if (!pxefile_addr_str)
1523                         return 1;
1524
1525         } else if (argc == 2) {
1526                 pxefile_addr_str = argv[1];
1527         } else {
1528                 return CMD_RET_USAGE;
1529         }
1530
1531         if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1532                 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1533                 return 1;
1534         }
1535
1536         cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
1537
1538         if (cfg == NULL) {
1539                 printf("Error parsing config file\n");
1540                 return 1;
1541         }
1542
1543         handle_pxe_menu(cmdtp, cfg);
1544
1545         destroy_pxe_menu(cfg);
1546
1547         return 0;
1548 }
1549
1550 static cmd_tbl_t cmd_pxe_sub[] = {
1551         U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1552         U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1553 };
1554
1555 int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1556 {
1557         cmd_tbl_t *cp;
1558
1559         if (argc < 2)
1560                 return CMD_RET_USAGE;
1561
1562         is_pxe = true;
1563
1564         /* drop initial "pxe" arg */
1565         argc--;
1566         argv++;
1567
1568         cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1569
1570         if (cp)
1571                 return cp->cmd(cmdtp, flag, argc, argv);
1572
1573         return CMD_RET_USAGE;
1574 }
1575
1576 U_BOOT_CMD(
1577         pxe, 3, 1, do_pxe,
1578         "commands to get and boot from pxe files",
1579         "get - try to retrieve a pxe file using tftp\npxe "
1580         "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1581 );
1582
1583 /*
1584  * Boots a system using a local disk syslinux/extlinux file
1585  *
1586  * Returns 0 on success, 1 on error.
1587  */
1588 int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1589 {
1590         unsigned long pxefile_addr_r;
1591         struct pxe_menu *cfg;
1592         char *pxefile_addr_str;
1593         char *filename;
1594         int prompt = 0;
1595
1596         is_pxe = false;
1597
1598         if (strstr(argv[1], "-p")) {
1599                 prompt = 1;
1600                 argc--;
1601                 argv++;
1602         }
1603
1604         if (argc < 4)
1605                 return cmd_usage(cmdtp);
1606
1607         if (argc < 5) {
1608                 pxefile_addr_str = from_env("pxefile_addr_r");
1609                 if (!pxefile_addr_str)
1610                         return 1;
1611         } else {
1612                 pxefile_addr_str = argv[4];
1613         }
1614
1615         if (argc < 6)
1616                 filename = getenv("bootfile");
1617         else {
1618                 filename = argv[5];
1619                 setenv("bootfile", filename);
1620         }
1621
1622         if (strstr(argv[3], "ext2"))
1623                 do_getfile = do_get_ext2;
1624         else if (strstr(argv[3], "fat"))
1625                 do_getfile = do_get_fat;
1626         else if (strstr(argv[3], "any"))
1627                 do_getfile = do_get_any;
1628         else {
1629                 printf("Invalid filesystem: %s\n", argv[3]);
1630                 return 1;
1631         }
1632         fs_argv[1] = argv[1];
1633         fs_argv[2] = argv[2];
1634
1635         if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1636                 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1637                 return 1;
1638         }
1639
1640         if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
1641                 printf("Error reading config file\n");
1642                 return 1;
1643         }
1644
1645         cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
1646
1647         if (cfg == NULL) {
1648                 printf("Error parsing config file\n");
1649                 return 1;
1650         }
1651
1652         if (prompt)
1653                 cfg->prompt = 1;
1654
1655         handle_pxe_menu(cmdtp, cfg);
1656
1657         destroy_pxe_menu(cfg);
1658
1659         return 0;
1660 }
1661
1662 U_BOOT_CMD(
1663         sysboot, 7, 1, do_sysboot,
1664         "command to get and boot from syslinux files",
1665         "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1666         "    - load and parse syslinux menu file 'filename' from ext2, fat\n"
1667         "      or any filesystem on 'dev' on 'interface' to address 'addr'"
1668 );