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