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