]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_pxe.c
Merge branch 'master' of git://git.denx.de/u-boot-tegra
[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 (label->append)
678                         strcpy(bootargs, label->append);
679                 strcat(bootargs, ip_str);
680                 strcat(bootargs, mac_str);
681
682                 cli_simple_process_macros(bootargs, finalbootargs);
683                 setenv("bootargs", finalbootargs);
684                 printf("append: %s\n", finalbootargs);
685         }
686
687         bootm_argv[1] = getenv("kernel_addr_r");
688
689         /*
690          * fdt usage is optional:
691          * It handles the following scenarios. All scenarios are exclusive
692          *
693          * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
694          * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
695          * and adjust argc appropriately.
696          *
697          * Scenario 2: If there is an fdt_addr specified, pass it along to
698          * bootm, and adjust argc appropriately.
699          *
700          * Scenario 3: fdt blob is not available.
701          */
702         bootm_argv[3] = getenv("fdt_addr_r");
703
704         /* if fdt label is defined then get fdt from server */
705         if (bootm_argv[3]) {
706                 char *fdtfile = NULL;
707                 char *fdtfilefree = NULL;
708
709                 if (label->fdt) {
710                         fdtfile = label->fdt;
711                 } else if (label->fdtdir) {
712                         char *f1, *f2, *f3, *f4, *slash;
713
714                         f1 = getenv("fdtfile");
715                         if (f1) {
716                                 f2 = "";
717                                 f3 = "";
718                                 f4 = "";
719                         } else {
720                                 /*
721                                  * For complex cases where this code doesn't
722                                  * generate the correct filename, the board
723                                  * code should set $fdtfile during early boot,
724                                  * or the boot scripts should set $fdtfile
725                                  * before invoking "pxe" or "sysboot".
726                                  */
727                                 f1 = getenv("soc");
728                                 f2 = "-";
729                                 f3 = getenv("board");
730                                 f4 = ".dtb";
731                         }
732
733                         len = strlen(label->fdtdir);
734                         if (!len)
735                                 slash = "./";
736                         else if (label->fdtdir[len - 1] != '/')
737                                 slash = "/";
738                         else
739                                 slash = "";
740
741                         len = strlen(label->fdtdir) + strlen(slash) +
742                                 strlen(f1) + strlen(f2) + strlen(f3) +
743                                 strlen(f4) + 1;
744                         fdtfilefree = malloc(len);
745                         if (!fdtfilefree) {
746                                 printf("malloc fail (FDT filename)\n");
747                                 return 1;
748                         }
749
750                         snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
751                                  label->fdtdir, slash, f1, f2, f3, f4);
752                         fdtfile = fdtfilefree;
753                 }
754
755                 if (fdtfile) {
756                         int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
757                         free(fdtfilefree);
758                         if (err < 0) {
759                                 printf("Skipping %s for failure retrieving fdt\n",
760                                                 label->name);
761                                 return 1;
762                         }
763                 } else {
764                         bootm_argv[3] = NULL;
765                 }
766         }
767
768         if (!bootm_argv[3])
769                 bootm_argv[3] = getenv("fdt_addr");
770
771         if (bootm_argv[3])
772                 bootm_argc = 4;
773
774         kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
775         buf = map_sysmem(kernel_addr, 0);
776         /* Try bootm for legacy and FIT format image */
777         if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
778                 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
779 #ifdef CONFIG_CMD_BOOTZ
780         /* Try booting a zImage */
781         else
782                 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
783 #endif
784         return 1;
785 }
786
787 /*
788  * Tokens for the pxe file parser.
789  */
790 enum token_type {
791         T_EOL,
792         T_STRING,
793         T_EOF,
794         T_MENU,
795         T_TITLE,
796         T_TIMEOUT,
797         T_LABEL,
798         T_KERNEL,
799         T_LINUX,
800         T_APPEND,
801         T_INITRD,
802         T_LOCALBOOT,
803         T_DEFAULT,
804         T_PROMPT,
805         T_INCLUDE,
806         T_FDT,
807         T_FDTDIR,
808         T_ONTIMEOUT,
809         T_IPAPPEND,
810         T_INVALID
811 };
812
813 /*
814  * A token - given by a value and a type.
815  */
816 struct token {
817         char *val;
818         enum token_type type;
819 };
820
821 /*
822  * Keywords recognized.
823  */
824 static const struct token keywords[] = {
825         {"menu", T_MENU},
826         {"title", T_TITLE},
827         {"timeout", T_TIMEOUT},
828         {"default", T_DEFAULT},
829         {"prompt", T_PROMPT},
830         {"label", T_LABEL},
831         {"kernel", T_KERNEL},
832         {"linux", T_LINUX},
833         {"localboot", T_LOCALBOOT},
834         {"append", T_APPEND},
835         {"initrd", T_INITRD},
836         {"include", T_INCLUDE},
837         {"devicetree", T_FDT},
838         {"fdt", T_FDT},
839         {"devicetreedir", T_FDTDIR},
840         {"fdtdir", T_FDTDIR},
841         {"ontimeout", T_ONTIMEOUT,},
842         {"ipappend", T_IPAPPEND,},
843         {NULL, T_INVALID}
844 };
845
846 /*
847  * Since pxe(linux) files don't have a token to identify the start of a
848  * literal, we have to keep track of when we're in a state where a literal is
849  * expected vs when we're in a state a keyword is expected.
850  */
851 enum lex_state {
852         L_NORMAL = 0,
853         L_KEYWORD,
854         L_SLITERAL
855 };
856
857 /*
858  * get_string retrieves a string from *p and stores it as a token in
859  * *t.
860  *
861  * get_string used for scanning both string literals and keywords.
862  *
863  * Characters from *p are copied into t-val until a character equal to
864  * delim is found, or a NUL byte is reached. If delim has the special value of
865  * ' ', any whitespace character will be used as a delimiter.
866  *
867  * If lower is unequal to 0, uppercase characters will be converted to
868  * lowercase in the result. This is useful to make keywords case
869  * insensitive.
870  *
871  * The location of *p is updated to point to the first character after the end
872  * of the token - the ending delimiter.
873  *
874  * On success, the new value of t->val is returned. Memory for t->val is
875  * allocated using malloc and must be free()'d to reclaim it.  If insufficient
876  * memory is available, NULL is returned.
877  */
878 static char *get_string(char **p, struct token *t, char delim, int lower)
879 {
880         char *b, *e;
881         size_t len, i;
882
883         /*
884          * b and e both start at the beginning of the input stream.
885          *
886          * e is incremented until we find the ending delimiter, or a NUL byte
887          * is reached. Then, we take e - b to find the length of the token.
888          */
889         b = e = *p;
890
891         while (*e) {
892                 if ((delim == ' ' && isspace(*e)) || delim == *e)
893                         break;
894                 e++;
895         }
896
897         len = e - b;
898
899         /*
900          * Allocate memory to hold the string, and copy it in, converting
901          * characters to lowercase if lower is != 0.
902          */
903         t->val = malloc(len + 1);
904         if (!t->val)
905                 return NULL;
906
907         for (i = 0; i < len; i++, b++) {
908                 if (lower)
909                         t->val[i] = tolower(*b);
910                 else
911                         t->val[i] = *b;
912         }
913
914         t->val[len] = '\0';
915
916         /*
917          * Update *p so the caller knows where to continue scanning.
918          */
919         *p = e;
920
921         t->type = T_STRING;
922
923         return t->val;
924 }
925
926 /*
927  * Populate a keyword token with a type and value.
928  */
929 static void get_keyword(struct token *t)
930 {
931         int i;
932
933         for (i = 0; keywords[i].val; i++) {
934                 if (!strcmp(t->val, keywords[i].val)) {
935                         t->type = keywords[i].type;
936                         break;
937                 }
938         }
939 }
940
941 /*
942  * Get the next token.  We have to keep track of which state we're in to know
943  * if we're looking to get a string literal or a keyword.
944  *
945  * *p is updated to point at the first character after the current token.
946  */
947 static void get_token(char **p, struct token *t, enum lex_state state)
948 {
949         char *c = *p;
950
951         t->type = T_INVALID;
952
953         /* eat non EOL whitespace */
954         while (isblank(*c))
955                 c++;
956
957         /*
958          * eat comments. note that string literals can't begin with #, but
959          * can contain a # after their first character.
960          */
961         if (*c == '#') {
962                 while (*c && *c != '\n')
963                         c++;
964         }
965
966         if (*c == '\n') {
967                 t->type = T_EOL;
968                 c++;
969         } else if (*c == '\0') {
970                 t->type = T_EOF;
971                 c++;
972         } else if (state == L_SLITERAL) {
973                 get_string(&c, t, '\n', 0);
974         } else if (state == L_KEYWORD) {
975                 /*
976                  * when we expect a keyword, we first get the next string
977                  * token delimited by whitespace, and then check if it
978                  * matches a keyword in our keyword list. if it does, it's
979                  * converted to a keyword token of the appropriate type, and
980                  * if not, it remains a string token.
981                  */
982                 get_string(&c, t, ' ', 1);
983                 get_keyword(t);
984         }
985
986         *p = c;
987 }
988
989 /*
990  * Increment *c until we get to the end of the current line, or EOF.
991  */
992 static void eol_or_eof(char **c)
993 {
994         while (**c && **c != '\n')
995                 (*c)++;
996 }
997
998 /*
999  * All of these parse_* functions share some common behavior.
1000  *
1001  * They finish with *c pointing after the token they parse, and return 1 on
1002  * success, or < 0 on error.
1003  */
1004
1005 /*
1006  * Parse a string literal and store a pointer it at *dst. String literals
1007  * terminate at the end of the line.
1008  */
1009 static int parse_sliteral(char **c, char **dst)
1010 {
1011         struct token t;
1012         char *s = *c;
1013
1014         get_token(c, &t, L_SLITERAL);
1015
1016         if (t.type != T_STRING) {
1017                 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
1018                 return -EINVAL;
1019         }
1020
1021         *dst = t.val;
1022
1023         return 1;
1024 }
1025
1026 /*
1027  * Parse a base 10 (unsigned) integer and store it at *dst.
1028  */
1029 static int parse_integer(char **c, int *dst)
1030 {
1031         struct token t;
1032         char *s = *c;
1033
1034         get_token(c, &t, L_SLITERAL);
1035
1036         if (t.type != T_STRING) {
1037                 printf("Expected string: %.*s\n", (int)(*c - s), s);
1038                 return -EINVAL;
1039         }
1040
1041         *dst = simple_strtol(t.val, NULL, 10);
1042
1043         free(t.val);
1044
1045         return 1;
1046 }
1047
1048 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level);
1049
1050 /*
1051  * Parse an include statement, and retrieve and parse the file it mentions.
1052  *
1053  * base should point to a location where it's safe to store the file, and
1054  * nest_level should indicate how many nested includes have occurred. For this
1055  * include, nest_level has already been incremented and doesn't need to be
1056  * incremented here.
1057  */
1058 static int handle_include(cmd_tbl_t *cmdtp, char **c, char *base,
1059                                 struct pxe_menu *cfg, int nest_level)
1060 {
1061         char *include_path;
1062         char *s = *c;
1063         int err;
1064
1065         err = parse_sliteral(c, &include_path);
1066
1067         if (err < 0) {
1068                 printf("Expected include path: %.*s\n",
1069                                  (int)(*c - s), s);
1070                 return err;
1071         }
1072
1073         err = get_pxe_file(cmdtp, include_path, base);
1074
1075         if (err < 0) {
1076                 printf("Couldn't retrieve %s\n", include_path);
1077                 return err;
1078         }
1079
1080         return parse_pxefile_top(cmdtp, base, cfg, nest_level);
1081 }
1082
1083 /*
1084  * Parse lines that begin with 'menu'.
1085  *
1086  * b and nest are provided to handle the 'menu include' case.
1087  *
1088  * b should be the address where the file currently being parsed is stored.
1089  *
1090  * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1091  * a file it includes, 3 when parsing a file included by that file, and so on.
1092  */
1093 static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg, char *b, int nest_level)
1094 {
1095         struct token t;
1096         char *s = *c;
1097         int err = 0;
1098
1099         get_token(c, &t, L_KEYWORD);
1100
1101         switch (t.type) {
1102         case T_TITLE:
1103                 err = parse_sliteral(c, &cfg->title);
1104
1105                 break;
1106
1107         case T_INCLUDE:
1108                 err = handle_include(cmdtp, c, b + strlen(b) + 1, cfg,
1109                                                 nest_level + 1);
1110                 break;
1111
1112         default:
1113                 printf("Ignoring malformed menu command: %.*s\n",
1114                                 (int)(*c - s), s);
1115         }
1116
1117         if (err < 0)
1118                 return err;
1119
1120         eol_or_eof(c);
1121
1122         return 1;
1123 }
1124
1125 /*
1126  * Handles parsing a 'menu line' when we're parsing a label.
1127  */
1128 static int parse_label_menu(char **c, struct pxe_menu *cfg,
1129                                 struct pxe_label *label)
1130 {
1131         struct token t;
1132         char *s;
1133
1134         s = *c;
1135
1136         get_token(c, &t, L_KEYWORD);
1137
1138         switch (t.type) {
1139         case T_DEFAULT:
1140                 if (!cfg->default_label)
1141                         cfg->default_label = strdup(label->name);
1142
1143                 if (!cfg->default_label)
1144                         return -ENOMEM;
1145
1146                 break;
1147         case T_LABEL:
1148                 parse_sliteral(c, &label->menu);
1149                 break;
1150         default:
1151                 printf("Ignoring malformed menu command: %.*s\n",
1152                                 (int)(*c - s), s);
1153         }
1154
1155         eol_or_eof(c);
1156
1157         return 0;
1158 }
1159
1160 /*
1161  * Parses a label and adds it to the list of labels for a menu.
1162  *
1163  * A label ends when we either get to the end of a file, or
1164  * get some input we otherwise don't have a handler defined
1165  * for.
1166  *
1167  */
1168 static int parse_label(char **c, struct pxe_menu *cfg)
1169 {
1170         struct token t;
1171         int len;
1172         char *s = *c;
1173         struct pxe_label *label;
1174         int err;
1175
1176         label = label_create();
1177         if (!label)
1178                 return -ENOMEM;
1179
1180         err = parse_sliteral(c, &label->name);
1181         if (err < 0) {
1182                 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1183                 label_destroy(label);
1184                 return -EINVAL;
1185         }
1186
1187         list_add_tail(&label->list, &cfg->labels);
1188
1189         while (1) {
1190                 s = *c;
1191                 get_token(c, &t, L_KEYWORD);
1192
1193                 err = 0;
1194                 switch (t.type) {
1195                 case T_MENU:
1196                         err = parse_label_menu(c, cfg, label);
1197                         break;
1198
1199                 case T_KERNEL:
1200                 case T_LINUX:
1201                         err = parse_sliteral(c, &label->kernel);
1202                         break;
1203
1204                 case T_APPEND:
1205                         err = parse_sliteral(c, &label->append);
1206                         if (label->initrd)
1207                                 break;
1208                         s = strstr(label->append, "initrd=");
1209                         if (!s)
1210                                 break;
1211                         s += 7;
1212                         len = (int)(strchr(s, ' ') - s);
1213                         label->initrd = malloc(len + 1);
1214                         strncpy(label->initrd, s, len);
1215                         label->initrd[len] = '\0';
1216
1217                         break;
1218
1219                 case T_INITRD:
1220                         if (!label->initrd)
1221                                 err = parse_sliteral(c, &label->initrd);
1222                         break;
1223
1224                 case T_FDT:
1225                         if (!label->fdt)
1226                                 err = parse_sliteral(c, &label->fdt);
1227                         break;
1228
1229                 case T_FDTDIR:
1230                         if (!label->fdtdir)
1231                                 err = parse_sliteral(c, &label->fdtdir);
1232                         break;
1233
1234                 case T_LOCALBOOT:
1235                         label->localboot = 1;
1236                         err = parse_integer(c, &label->localboot_val);
1237                         break;
1238
1239                 case T_IPAPPEND:
1240                         err = parse_integer(c, &label->ipappend);
1241                         break;
1242
1243                 case T_EOL:
1244                         break;
1245
1246                 default:
1247                         /*
1248                          * put the token back! we don't want it - it's the end
1249                          * of a label and whatever token this is, it's
1250                          * something for the menu level context to handle.
1251                          */
1252                         *c = s;
1253                         return 1;
1254                 }
1255
1256                 if (err < 0)
1257                         return err;
1258         }
1259 }
1260
1261 /*
1262  * This 16 comes from the limit pxelinux imposes on nested includes.
1263  *
1264  * There is no reason at all we couldn't do more, but some limit helps prevent
1265  * infinite (until crash occurs) recursion if a file tries to include itself.
1266  */
1267 #define MAX_NEST_LEVEL 16
1268
1269 /*
1270  * Entry point for parsing a menu file. nest_level indicates how many times
1271  * we've nested in includes.  It will be 1 for the top level menu file.
1272  *
1273  * Returns 1 on success, < 0 on error.
1274  */
1275 static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, struct pxe_menu *cfg, int nest_level)
1276 {
1277         struct token t;
1278         char *s, *b, *label_name;
1279         int err;
1280
1281         b = p;
1282
1283         if (nest_level > MAX_NEST_LEVEL) {
1284                 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1285                 return -EMLINK;
1286         }
1287
1288         while (1) {
1289                 s = p;
1290
1291                 get_token(&p, &t, L_KEYWORD);
1292
1293                 err = 0;
1294                 switch (t.type) {
1295                 case T_MENU:
1296                         cfg->prompt = 1;
1297                         err = parse_menu(cmdtp, &p, cfg, b, nest_level);
1298                         break;
1299
1300                 case T_TIMEOUT:
1301                         err = parse_integer(&p, &cfg->timeout);
1302                         break;
1303
1304                 case T_LABEL:
1305                         err = parse_label(&p, cfg);
1306                         break;
1307
1308                 case T_DEFAULT:
1309                 case T_ONTIMEOUT:
1310                         err = parse_sliteral(&p, &label_name);
1311
1312                         if (label_name) {
1313                                 if (cfg->default_label)
1314                                         free(cfg->default_label);
1315
1316                                 cfg->default_label = label_name;
1317                         }
1318
1319                         break;
1320
1321                 case T_INCLUDE:
1322                         err = handle_include(cmdtp, &p, b + ALIGN(strlen(b), 4), cfg,
1323                                                         nest_level + 1);
1324                         break;
1325
1326                 case T_PROMPT:
1327                         eol_or_eof(&p);
1328                         break;
1329
1330                 case T_EOL:
1331                         break;
1332
1333                 case T_EOF:
1334                         return 1;
1335
1336                 default:
1337                         printf("Ignoring unknown command: %.*s\n",
1338                                                         (int)(p - s), s);
1339                         eol_or_eof(&p);
1340                 }
1341
1342                 if (err < 0)
1343                         return err;
1344         }
1345 }
1346
1347 /*
1348  * Free the memory used by a pxe_menu and its labels.
1349  */
1350 static void destroy_pxe_menu(struct pxe_menu *cfg)
1351 {
1352         struct list_head *pos, *n;
1353         struct pxe_label *label;
1354
1355         if (cfg->title)
1356                 free(cfg->title);
1357
1358         if (cfg->default_label)
1359                 free(cfg->default_label);
1360
1361         list_for_each_safe(pos, n, &cfg->labels) {
1362                 label = list_entry(pos, struct pxe_label, list);
1363
1364                 label_destroy(label);
1365         }
1366
1367         free(cfg);
1368 }
1369
1370 /*
1371  * Entry point for parsing a pxe file. This is only used for the top level
1372  * file.
1373  *
1374  * Returns NULL if there is an error, otherwise, returns a pointer to a
1375  * pxe_menu struct populated with the results of parsing the pxe file (and any
1376  * files it includes). The resulting pxe_menu struct can be free()'d by using
1377  * the destroy_pxe_menu() function.
1378  */
1379 static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, char *menucfg)
1380 {
1381         struct pxe_menu *cfg;
1382
1383         cfg = malloc(sizeof(struct pxe_menu));
1384
1385         if (!cfg)
1386                 return NULL;
1387
1388         memset(cfg, 0, sizeof(struct pxe_menu));
1389
1390         INIT_LIST_HEAD(&cfg->labels);
1391
1392         if (parse_pxefile_top(cmdtp, menucfg, cfg, 1) < 0) {
1393                 destroy_pxe_menu(cfg);
1394                 return NULL;
1395         }
1396
1397         return cfg;
1398 }
1399
1400 /*
1401  * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1402  * menu code.
1403  */
1404 static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1405 {
1406         struct pxe_label *label;
1407         struct list_head *pos;
1408         struct menu *m;
1409         int err;
1410         int i = 1;
1411         char *default_num = NULL;
1412
1413         /*
1414          * Create a menu and add items for all the labels.
1415          */
1416         m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print,
1417                         NULL, NULL);
1418
1419         if (!m)
1420                 return NULL;
1421
1422         list_for_each(pos, &cfg->labels) {
1423                 label = list_entry(pos, struct pxe_label, list);
1424
1425                 sprintf(label->num, "%d", i++);
1426                 if (menu_item_add(m, label->num, label) != 1) {
1427                         menu_destroy(m);
1428                         return NULL;
1429                 }
1430                 if (cfg->default_label &&
1431                     (strcmp(label->name, cfg->default_label) == 0))
1432                         default_num = label->num;
1433
1434         }
1435
1436         /*
1437          * After we've created items for each label in the menu, set the
1438          * menu's default label if one was specified.
1439          */
1440         if (default_num) {
1441                 err = menu_default_set(m, default_num);
1442                 if (err != 1) {
1443                         if (err != -ENOENT) {
1444                                 menu_destroy(m);
1445                                 return NULL;
1446                         }
1447
1448                         printf("Missing default: %s\n", cfg->default_label);
1449                 }
1450         }
1451
1452         return m;
1453 }
1454
1455 /*
1456  * Try to boot any labels we have yet to attempt to boot.
1457  */
1458 static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1459 {
1460         struct list_head *pos;
1461         struct pxe_label *label;
1462
1463         list_for_each(pos, &cfg->labels) {
1464                 label = list_entry(pos, struct pxe_label, list);
1465
1466                 if (!label->attempted)
1467                         label_boot(cmdtp, label);
1468         }
1469 }
1470
1471 /*
1472  * Boot the system as prescribed by a pxe_menu.
1473  *
1474  * Use the menu system to either get the user's choice or the default, based
1475  * on config or user input.  If there is no default or user's choice,
1476  * attempted to boot labels in the order they were given in pxe files.
1477  * If the default or user's choice fails to boot, attempt to boot other
1478  * labels in the order they were given in pxe files.
1479  *
1480  * If this function returns, there weren't any labels that successfully
1481  * booted, or the user interrupted the menu selection via ctrl+c.
1482  */
1483 static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1484 {
1485         void *choice;
1486         struct menu *m;
1487         int err;
1488
1489         m = pxe_menu_to_menu(cfg);
1490         if (!m)
1491                 return;
1492
1493         err = menu_get_choice(m, &choice);
1494
1495         menu_destroy(m);
1496
1497         /*
1498          * err == 1 means we got a choice back from menu_get_choice.
1499          *
1500          * err == -ENOENT if the menu was setup to select the default but no
1501          * default was set. in that case, we should continue trying to boot
1502          * labels that haven't been attempted yet.
1503          *
1504          * otherwise, the user interrupted or there was some other error and
1505          * we give up.
1506          */
1507
1508         if (err == 1) {
1509                 err = label_boot(cmdtp, choice);
1510                 if (!err)
1511                         return;
1512         } else if (err != -ENOENT) {
1513                 return;
1514         }
1515
1516         boot_unattempted_labels(cmdtp, cfg);
1517 }
1518
1519 #ifdef CONFIG_CMD_NET
1520 /*
1521  * Boots a system using a pxe file
1522  *
1523  * Returns 0 on success, 1 on error.
1524  */
1525 static int
1526 do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1527 {
1528         unsigned long pxefile_addr_r;
1529         struct pxe_menu *cfg;
1530         char *pxefile_addr_str;
1531
1532         do_getfile = do_get_tftp;
1533
1534         if (argc == 1) {
1535                 pxefile_addr_str = from_env("pxefile_addr_r");
1536                 if (!pxefile_addr_str)
1537                         return 1;
1538
1539         } else if (argc == 2) {
1540                 pxefile_addr_str = argv[1];
1541         } else {
1542                 return CMD_RET_USAGE;
1543         }
1544
1545         if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1546                 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1547                 return 1;
1548         }
1549
1550         cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
1551
1552         if (cfg == NULL) {
1553                 printf("Error parsing config file\n");
1554                 return 1;
1555         }
1556
1557         handle_pxe_menu(cmdtp, cfg);
1558
1559         destroy_pxe_menu(cfg);
1560
1561         copy_filename(BootFile, "", sizeof(BootFile));
1562
1563         return 0;
1564 }
1565
1566 static cmd_tbl_t cmd_pxe_sub[] = {
1567         U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1568         U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1569 };
1570
1571 static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1572 {
1573         cmd_tbl_t *cp;
1574
1575         if (argc < 2)
1576                 return CMD_RET_USAGE;
1577
1578         is_pxe = true;
1579
1580         /* drop initial "pxe" arg */
1581         argc--;
1582         argv++;
1583
1584         cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1585
1586         if (cp)
1587                 return cp->cmd(cmdtp, flag, argc, argv);
1588
1589         return CMD_RET_USAGE;
1590 }
1591
1592 U_BOOT_CMD(
1593         pxe, 3, 1, do_pxe,
1594         "commands to get and boot from pxe files",
1595         "get - try to retrieve a pxe file using tftp\npxe "
1596         "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1597 );
1598 #endif
1599
1600 /*
1601  * Boots a system using a local disk syslinux/extlinux file
1602  *
1603  * Returns 0 on success, 1 on error.
1604  */
1605 static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1606 {
1607         unsigned long pxefile_addr_r;
1608         struct pxe_menu *cfg;
1609         char *pxefile_addr_str;
1610         char *filename;
1611         int prompt = 0;
1612
1613         is_pxe = false;
1614
1615         if (strstr(argv[1], "-p")) {
1616                 prompt = 1;
1617                 argc--;
1618                 argv++;
1619         }
1620
1621         if (argc < 4)
1622                 return cmd_usage(cmdtp);
1623
1624         if (argc < 5) {
1625                 pxefile_addr_str = from_env("pxefile_addr_r");
1626                 if (!pxefile_addr_str)
1627                         return 1;
1628         } else {
1629                 pxefile_addr_str = argv[4];
1630         }
1631
1632         if (argc < 6)
1633                 filename = getenv("bootfile");
1634         else {
1635                 filename = argv[5];
1636                 setenv("bootfile", filename);
1637         }
1638
1639         if (strstr(argv[3], "ext2"))
1640                 do_getfile = do_get_ext2;
1641         else if (strstr(argv[3], "fat"))
1642                 do_getfile = do_get_fat;
1643         else if (strstr(argv[3], "any"))
1644                 do_getfile = do_get_any;
1645         else {
1646                 printf("Invalid filesystem: %s\n", argv[3]);
1647                 return 1;
1648         }
1649         fs_argv[1] = argv[1];
1650         fs_argv[2] = argv[2];
1651
1652         if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1653                 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1654                 return 1;
1655         }
1656
1657         if (get_pxe_file(cmdtp, filename, (void *)pxefile_addr_r) < 0) {
1658                 printf("Error reading config file\n");
1659                 return 1;
1660         }
1661
1662         cfg = parse_pxefile(cmdtp, (char *)(pxefile_addr_r));
1663
1664         if (cfg == NULL) {
1665                 printf("Error parsing config file\n");
1666                 return 1;
1667         }
1668
1669         if (prompt)
1670                 cfg->prompt = 1;
1671
1672         handle_pxe_menu(cmdtp, cfg);
1673
1674         destroy_pxe_menu(cfg);
1675
1676         return 0;
1677 }
1678
1679 U_BOOT_CMD(
1680         sysboot, 7, 1, do_sysboot,
1681         "command to get and boot from syslinux files",
1682         "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
1683         "    - load and parse syslinux menu file 'filename' from ext2, fat\n"
1684         "      or any filesystem on 'dev' on 'interface' to address 'addr'"
1685 );