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