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