blob: 0e9f8a4bfc6b1537837154d9ce25799e5aa3c6c2 [file] [log] [blame]
Jason Hobbs06283a62011-08-31 10:37:30 -05001/*
2 * Copyright 2010-2011 Calxeda, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17#include <common.h>
18#include <command.h>
19#include <malloc.h>
20#include <linux/string.h>
21#include <linux/ctype.h>
22#include <errno.h>
23#include <linux/list.h>
24
25#include "menu.h"
26
27#define MAX_TFTP_PATH_LEN 127
28
Jason Hobbs06283a62011-08-31 10:37:30 -050029/*
30 * Like getenv, but prints an error if envvar isn't defined in the
31 * environment. It always returns what getenv does, so it can be used in
32 * place of getenv without changing error handling otherwise.
33 */
34static char *from_env(char *envvar)
35{
36 char *ret;
37
38 ret = getenv(envvar);
39
40 if (!ret)
41 printf("missing environment variable: %s\n", envvar);
42
43 return ret;
44}
45
46/*
47 * Convert an ethaddr from the environment to the format used by pxelinux
48 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
49 * the beginning of the ethernet address to indicate a hardware type of
50 * Ethernet. Also converts uppercase hex characters into lowercase, to match
51 * pxelinux's behavior.
52 *
53 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
54 * environment, or some other value < 0 on error.
55 */
56static int format_mac_pxe(char *outbuf, size_t outbuf_len)
57{
58 size_t ethaddr_len;
59 char *p, *ethaddr;
60
61 ethaddr = from_env("ethaddr");
62
63 if (!ethaddr)
John Rigby5943fe42011-07-13 23:05:19 -060064 ethaddr = from_env("usbethaddr");
65
66 if (!ethaddr)
Jason Hobbs06283a62011-08-31 10:37:30 -050067 return -ENOENT;
68
69 ethaddr_len = strlen(ethaddr);
70
71 /*
72 * ethaddr_len + 4 gives room for "01-", ethaddr, and a NUL byte at
73 * the end.
74 */
75 if (outbuf_len < ethaddr_len + 4) {
76 printf("outbuf is too small (%d < %d)\n",
77 outbuf_len, ethaddr_len + 4);
78
79 return -EINVAL;
80 }
81
82 strcpy(outbuf, "01-");
83
84 for (p = outbuf + 3; *ethaddr; ethaddr++, p++) {
85 if (*ethaddr == ':')
86 *p = '-';
87 else
88 *p = tolower(*ethaddr);
89 }
90
91 *p = '\0';
92
93 return 1;
94}
95
96/*
97 * Returns the directory the file specified in the bootfile env variable is
98 * in. If bootfile isn't defined in the environment, return NULL, which should
99 * be interpreted as "don't prepend anything to paths".
100 */
Rob Herring90ba7d72012-03-28 05:51:36 +0000101static int get_bootfile_path(const char *file_path, char *bootfile_path,
102 size_t bootfile_path_size)
Jason Hobbs06283a62011-08-31 10:37:30 -0500103{
104 char *bootfile, *last_slash;
Rob Herring90ba7d72012-03-28 05:51:36 +0000105 size_t path_len = 0;
106
107 if (file_path[0] == '/')
108 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -0500109
110 bootfile = from_env("bootfile");
111
Rob Herring90ba7d72012-03-28 05:51:36 +0000112 if (!bootfile)
113 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -0500114
115 last_slash = strrchr(bootfile, '/');
116
Rob Herring90ba7d72012-03-28 05:51:36 +0000117 if (last_slash == NULL)
118 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -0500119
120 path_len = (last_slash - bootfile) + 1;
121
122 if (bootfile_path_size < path_len) {
123 printf("bootfile_path too small. (%d < %d)\n",
124 bootfile_path_size, path_len);
125
126 return -1;
127 }
128
129 strncpy(bootfile_path, bootfile, path_len);
130
Rob Herring90ba7d72012-03-28 05:51:36 +0000131 ret:
Jason Hobbs06283a62011-08-31 10:37:30 -0500132 bootfile_path[path_len] = '\0';
133
134 return 1;
135}
136
Rob Herring669df7e2012-05-25 10:47:39 +0000137static int (*do_getfile)(char *file_path, char *file_addr);
138
139static int do_get_tftp(char *file_path, char *file_addr)
140{
141 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
142
143 tftp_argv[1] = file_addr;
144 tftp_argv[2] = file_path;
145
146 if (do_tftpb(NULL, 0, 3, tftp_argv))
147 return -ENOENT;
148
149 return 1;
150}
151
152static char *fs_argv[5];
153
154static int do_get_ext2(char *file_path, char *file_addr)
155{
156#ifdef CONFIG_CMD_EXT2
157 fs_argv[0] = "ext2load";
158 fs_argv[3] = file_addr;
159 fs_argv[4] = file_path;
160
161 if (!do_ext2load(NULL, 0, 5, fs_argv))
162 return 1;
163#endif
164 return -ENOENT;
165}
166
167static int do_get_fat(char *file_path, char *file_addr)
168{
169#ifdef CONFIG_CMD_FAT
170 fs_argv[0] = "fatload";
171 fs_argv[3] = file_addr;
172 fs_argv[4] = file_path;
173
174 if (!do_fat_fsload(NULL, 0, 5, fs_argv))
175 return 1;
176#endif
177 return -ENOENT;
178}
179
Jason Hobbs06283a62011-08-31 10:37:30 -0500180/*
181 * As in pxelinux, paths to files referenced from files we retrieve are
182 * relative to the location of bootfile. get_relfile takes such a path and
183 * joins it with the bootfile path to get the full path to the target file. If
184 * the bootfile path is NULL, we use file_path as is.
185 *
186 * Returns 1 for success, or < 0 on error.
187 */
188static int get_relfile(char *file_path, void *file_addr)
189{
190 size_t path_len;
191 char relfile[MAX_TFTP_PATH_LEN+1];
192 char addr_buf[10];
Jason Hobbs06283a62011-08-31 10:37:30 -0500193 int err;
194
Rob Herring90ba7d72012-03-28 05:51:36 +0000195 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
Jason Hobbs06283a62011-08-31 10:37:30 -0500196
197 if (err < 0)
198 return err;
199
200 path_len = strlen(file_path);
201 path_len += strlen(relfile);
202
203 if (path_len > MAX_TFTP_PATH_LEN) {
204 printf("Base path too long (%s%s)\n",
205 relfile,
206 file_path);
207
208 return -ENAMETOOLONG;
209 }
210
211 strcat(relfile, file_path);
212
213 printf("Retrieving file: %s\n", relfile);
214
215 sprintf(addr_buf, "%p", file_addr);
216
Rob Herring669df7e2012-05-25 10:47:39 +0000217 return do_getfile(relfile, addr_buf);
Jason Hobbs06283a62011-08-31 10:37:30 -0500218}
219
220/*
221 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
222 * 'bootfile' was specified in the environment, the path to bootfile will be
223 * prepended to 'file_path' and the resulting path will be used.
224 *
225 * Returns 1 on success, or < 0 for error.
226 */
227static int get_pxe_file(char *file_path, void *file_addr)
228{
229 unsigned long config_file_size;
230 char *tftp_filesize;
231 int err;
232
233 err = get_relfile(file_path, file_addr);
234
235 if (err < 0)
236 return err;
237
238 /*
239 * the file comes without a NUL byte at the end, so find out its size
240 * and add the NUL byte.
241 */
242 tftp_filesize = from_env("filesize");
243
244 if (!tftp_filesize)
245 return -ENOENT;
246
247 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
248 return -EINVAL;
249
250 *(char *)(file_addr + config_file_size) = '\0';
251
252 return 1;
253}
254
255#define PXELINUX_DIR "pxelinux.cfg/"
256
257/*
258 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
259 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
260 * from the bootfile path, as described above.
261 *
262 * Returns 1 on success or < 0 on error.
263 */
264static int get_pxelinux_path(char *file, void *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(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 */
285static int pxe_uuid_path(void *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(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 */
303static int pxe_mac_path(void *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(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 */
323static int pxe_ipaddr_paths(void *pxefile_addr_r)
324{
325 char ip_addr[9];
326 int mask_pos, err;
327
328 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
329
330 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
331 err = get_pxelinux_path(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 */
356static int
357do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
358{
359 char *pxefile_addr_str;
Jason Hobbs834c9382012-03-05 08:12:28 +0000360 unsigned long pxefile_addr_r;
Jason Hobbs06283a62011-08-31 10:37:30 -0500361 int err;
362
Rob Herring669df7e2012-05-25 10:47:39 +0000363 do_getfile = do_get_tftp;
364
Jason Hobbs06283a62011-08-31 10:37:30 -0500365 if (argc != 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000366 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500367
Jason Hobbs06283a62011-08-31 10:37:30 -0500368 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 */
Jason Hobbs834c9382012-03-05 08:12:28 +0000382 if (pxe_uuid_path((void *)pxefile_addr_r) > 0
383 || pxe_mac_path((void *)pxefile_addr_r) > 0
384 || pxe_ipaddr_paths((void *)pxefile_addr_r) > 0
385 || get_pxelinux_path("default", (void *)pxefile_addr_r) > 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500386
387 printf("Config file found\n");
388
389 return 0;
390 }
391
392 printf("Config file not found\n");
393
394 return 1;
395}
396
397/*
398 * Wrapper to make it easier to store the file at file_path in the location
399 * specified by envaddr_name. file_path will be joined to the bootfile path,
400 * if any is specified.
401 *
402 * Returns 1 on success or < 0 on error.
403 */
404static int get_relfile_envaddr(char *file_path, char *envaddr_name)
405{
Jason Hobbs834c9382012-03-05 08:12:28 +0000406 unsigned long file_addr;
Jason Hobbs06283a62011-08-31 10:37:30 -0500407 char *envaddr;
408
409 envaddr = from_env(envaddr_name);
410
411 if (!envaddr)
412 return -ENOENT;
413
Jason Hobbs834c9382012-03-05 08:12:28 +0000414 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
Jason Hobbs06283a62011-08-31 10:37:30 -0500415 return -EINVAL;
416
Jason Hobbs834c9382012-03-05 08:12:28 +0000417 return get_relfile(file_path, (void *)file_addr);
Jason Hobbs06283a62011-08-31 10:37:30 -0500418}
419
420/*
421 * A note on the pxe file parser.
422 *
423 * We're parsing files that use syslinux grammar, which has a few quirks.
424 * String literals must be recognized based on context - there is no
425 * quoting or escaping support. There's also nothing to explicitly indicate
426 * when a label section completes. We deal with that by ending a label
427 * section whenever we see a line that doesn't include.
428 *
429 * As with the syslinux family, this same file format could be reused in the
430 * future for non pxe purposes. The only action it takes during parsing that
431 * would throw this off is handling of include files. It assumes we're using
432 * pxe, and does a tftp download of a file listed as an include file in the
433 * middle of the parsing operation. That could be handled by refactoring it to
434 * take a 'include file getter' function.
435 */
436
437/*
438 * Describes a single label given in a pxe file.
439 *
440 * Create these with the 'label_create' function given below.
441 *
442 * name - the name of the menu as given on the 'menu label' line.
443 * kernel - the path to the kernel file to use for this label.
444 * append - kernel command line to use when booting this label
445 * initrd - path to the initrd to use for this label.
446 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
447 * localboot - 1 if this label specified 'localboot', 0 otherwise.
448 * list - lets these form a list, which a pxe_menu struct will hold.
449 */
450struct pxe_label {
451 char *name;
Rob Herring7815c4e2012-03-28 05:51:34 +0000452 char *menu;
Jason Hobbs06283a62011-08-31 10:37:30 -0500453 char *kernel;
454 char *append;
455 char *initrd;
Chander Kashyapa6559382012-09-06 19:36:31 +0000456 char *fdt;
Jason Hobbs06283a62011-08-31 10:37:30 -0500457 int attempted;
458 int localboot;
459 struct list_head list;
460};
461
462/*
463 * Describes a pxe menu as given via pxe files.
464 *
465 * title - the name of the menu as given by a 'menu title' line.
466 * default_label - the name of the default label, if any.
467 * timeout - time in tenths of a second to wait for a user key-press before
468 * booting the default label.
469 * prompt - if 0, don't prompt for a choice unless the timeout period is
470 * interrupted. If 1, always prompt for a choice regardless of
471 * timeout.
472 * labels - a list of labels defined for the menu.
473 */
474struct pxe_menu {
475 char *title;
476 char *default_label;
477 int timeout;
478 int prompt;
479 struct list_head labels;
480};
481
482/*
483 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
484 * result must be free()'d to reclaim the memory.
485 *
486 * Returns NULL if malloc fails.
487 */
488static struct pxe_label *label_create(void)
489{
490 struct pxe_label *label;
491
492 label = malloc(sizeof(struct pxe_label));
493
494 if (!label)
495 return NULL;
496
497 memset(label, 0, sizeof(struct pxe_label));
498
499 return label;
500}
501
502/*
503 * Free the memory used by a pxe_label, including that used by its name,
504 * kernel, append and initrd members, if they're non NULL.
505 *
506 * So - be sure to only use dynamically allocated memory for the members of
507 * the pxe_label struct, unless you want to clean it up first. These are
508 * currently only created by the pxe file parsing code.
509 */
510static void label_destroy(struct pxe_label *label)
511{
512 if (label->name)
513 free(label->name);
514
515 if (label->kernel)
516 free(label->kernel);
517
518 if (label->append)
519 free(label->append);
520
521 if (label->initrd)
522 free(label->initrd);
523
Chander Kashyapa6559382012-09-06 19:36:31 +0000524 if (label->fdt)
525 free(label->fdt);
526
Jason Hobbs06283a62011-08-31 10:37:30 -0500527 free(label);
528}
529
530/*
531 * Print a label and its string members if they're defined.
532 *
533 * This is passed as a callback to the menu code for displaying each
534 * menu entry.
535 */
536static void label_print(void *data)
537{
538 struct pxe_label *label = data;
Rob Herring7815c4e2012-03-28 05:51:34 +0000539 const char *c = label->menu ? label->menu : label->kernel;
Jason Hobbs06283a62011-08-31 10:37:30 -0500540
Rob Herring7815c4e2012-03-28 05:51:34 +0000541 printf("%s:\t%s\n", label->name, c);
Jason Hobbs06283a62011-08-31 10:37:30 -0500542
543 if (label->kernel)
Rob Herring7815c4e2012-03-28 05:51:34 +0000544 printf("\t\tkernel: %s\n", label->kernel);
Jason Hobbs06283a62011-08-31 10:37:30 -0500545
546 if (label->append)
Rob Herring7815c4e2012-03-28 05:51:34 +0000547 printf("\t\tappend: %s\n", label->append);
Jason Hobbs06283a62011-08-31 10:37:30 -0500548
549 if (label->initrd)
Rob Herring7815c4e2012-03-28 05:51:34 +0000550 printf("\t\tinitrd: %s\n", label->initrd);
Chander Kashyapa6559382012-09-06 19:36:31 +0000551
552 if (label->fdt)
553 printf("\tfdt: %s\n", label->fdt);
Jason Hobbs06283a62011-08-31 10:37:30 -0500554}
555
556/*
557 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
558 * environment variable is defined. Its contents will be executed as U-boot
559 * command. If the label specified an 'append' line, its contents will be
560 * used to overwrite the contents of the 'bootargs' environment variable prior
561 * to running 'localcmd'.
562 *
563 * Returns 1 on success or < 0 on error.
564 */
565static int label_localboot(struct pxe_label *label)
566{
Simon Glassd51004a2012-03-30 21:30:55 +0000567 char *localcmd;
Jason Hobbs06283a62011-08-31 10:37:30 -0500568
569 localcmd = from_env("localcmd");
570
571 if (!localcmd)
572 return -ENOENT;
573
Jason Hobbs06283a62011-08-31 10:37:30 -0500574 if (label->append)
575 setenv("bootargs", label->append);
576
Simon Glassd51004a2012-03-30 21:30:55 +0000577 debug("running: %s\n", localcmd);
Jason Hobbs06283a62011-08-31 10:37:30 -0500578
Simon Glassd51004a2012-03-30 21:30:55 +0000579 return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs06283a62011-08-31 10:37:30 -0500580}
581
582/*
583 * Boot according to the contents of a pxe_label.
584 *
585 * If we can't boot for any reason, we return. A successful boot never
586 * returns.
587 *
588 * The kernel will be stored in the location given by the 'kernel_addr_r'
589 * environment variable.
590 *
591 * If the label specifies an initrd file, it will be stored in the location
592 * given by the 'ramdisk_addr_r' environment variable.
593 *
594 * If the label specifies an 'append' line, its contents will overwrite that
595 * of the 'bootargs' environment variable.
596 */
597static void label_boot(struct pxe_label *label)
598{
599 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Rob Herring5af27692012-03-26 13:16:59 -0500600 char initrd_str[22];
Jason Hobbs06283a62011-08-31 10:37:30 -0500601 int bootm_argc = 3;
602
603 label_print(label);
604
605 label->attempted = 1;
606
607 if (label->localboot) {
608 label_localboot(label);
609 return;
610 }
611
612 if (label->kernel == NULL) {
613 printf("No kernel given, skipping %s\n",
614 label->name);
615 return;
616 }
617
618 if (label->initrd) {
619 if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) {
620 printf("Skipping %s for failure retrieving initrd\n",
621 label->name);
622 return;
623 }
624
Rob Herring5af27692012-03-26 13:16:59 -0500625 bootm_argv[2] = initrd_str;
626 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
627 strcat(bootm_argv[2], ":");
628 strcat(bootm_argv[2], getenv("filesize"));
Jason Hobbs06283a62011-08-31 10:37:30 -0500629 } else {
630 bootm_argv[2] = "-";
631 }
632
633 if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) {
634 printf("Skipping %s for failure retrieving kernel\n",
635 label->name);
636 return;
637 }
638
639 if (label->append)
640 setenv("bootargs", label->append);
641
642 bootm_argv[1] = getenv("kernel_addr_r");
643
644 /*
Chander Kashyapa6559382012-09-06 19:36:31 +0000645 * fdt usage is optional:
646 * It handles the following scenarios. All scenarios are exclusive
647 *
648 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
649 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
650 * and adjust argc appropriately.
651 *
652 * Scenario 2: If there is an fdt_addr specified, pass it along to
653 * bootm, and adjust argc appropriately.
654 *
655 * Scenario 3: fdt blob is not available.
Jason Hobbs06283a62011-08-31 10:37:30 -0500656 */
Chander Kashyapa6559382012-09-06 19:36:31 +0000657 bootm_argv[3] = getenv("fdt_addr_r");
658
659 /* if fdt label is defined then get fdt from server */
660 if (bootm_argv[3] && label->fdt) {
661 if (get_relfile_envaddr(label->fdt, "fdt_addr_r") < 0) {
662 printf("Skipping %s for failure retrieving fdt\n",
663 label->name);
664 return;
665 }
666 } else
667 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs06283a62011-08-31 10:37:30 -0500668
669 if (bootm_argv[3])
670 bootm_argc = 4;
671
Rob Herring5af27692012-03-26 13:16:59 -0500672#ifdef CONFIG_CMD_BOOTZ
673 do_bootz(NULL, 0, bootm_argc, bootm_argv);
674#else
Jason Hobbs06283a62011-08-31 10:37:30 -0500675 do_bootm(NULL, 0, bootm_argc, bootm_argv);
Rob Herring5af27692012-03-26 13:16:59 -0500676#endif
Jason Hobbs06283a62011-08-31 10:37:30 -0500677}
678
679/*
680 * Tokens for the pxe file parser.
681 */
682enum token_type {
683 T_EOL,
684 T_STRING,
685 T_EOF,
686 T_MENU,
687 T_TITLE,
688 T_TIMEOUT,
689 T_LABEL,
690 T_KERNEL,
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000691 T_LINUX,
Jason Hobbs06283a62011-08-31 10:37:30 -0500692 T_APPEND,
693 T_INITRD,
694 T_LOCALBOOT,
695 T_DEFAULT,
696 T_PROMPT,
697 T_INCLUDE,
Chander Kashyapa6559382012-09-06 19:36:31 +0000698 T_FDT,
Jason Hobbs06283a62011-08-31 10:37:30 -0500699 T_INVALID
700};
701
702/*
703 * A token - given by a value and a type.
704 */
705struct token {
706 char *val;
707 enum token_type type;
708};
709
710/*
711 * Keywords recognized.
712 */
713static const struct token keywords[] = {
714 {"menu", T_MENU},
715 {"title", T_TITLE},
716 {"timeout", T_TIMEOUT},
717 {"default", T_DEFAULT},
718 {"prompt", T_PROMPT},
719 {"label", T_LABEL},
720 {"kernel", T_KERNEL},
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000721 {"linux", T_LINUX},
Jason Hobbs06283a62011-08-31 10:37:30 -0500722 {"localboot", T_LOCALBOOT},
723 {"append", T_APPEND},
724 {"initrd", T_INITRD},
725 {"include", T_INCLUDE},
Chander Kashyapa6559382012-09-06 19:36:31 +0000726 {"fdt", T_FDT},
Jason Hobbs06283a62011-08-31 10:37:30 -0500727 {NULL, T_INVALID}
728};
729
730/*
731 * Since pxe(linux) files don't have a token to identify the start of a
732 * literal, we have to keep track of when we're in a state where a literal is
733 * expected vs when we're in a state a keyword is expected.
734 */
735enum lex_state {
736 L_NORMAL = 0,
737 L_KEYWORD,
738 L_SLITERAL
739};
740
741/*
742 * get_string retrieves a string from *p and stores it as a token in
743 * *t.
744 *
745 * get_string used for scanning both string literals and keywords.
746 *
747 * Characters from *p are copied into t-val until a character equal to
748 * delim is found, or a NUL byte is reached. If delim has the special value of
749 * ' ', any whitespace character will be used as a delimiter.
750 *
751 * If lower is unequal to 0, uppercase characters will be converted to
752 * lowercase in the result. This is useful to make keywords case
753 * insensitive.
754 *
755 * The location of *p is updated to point to the first character after the end
756 * of the token - the ending delimiter.
757 *
758 * On success, the new value of t->val is returned. Memory for t->val is
759 * allocated using malloc and must be free()'d to reclaim it. If insufficient
760 * memory is available, NULL is returned.
761 */
762static char *get_string(char **p, struct token *t, char delim, int lower)
763{
764 char *b, *e;
765 size_t len, i;
766
767 /*
768 * b and e both start at the beginning of the input stream.
769 *
770 * e is incremented until we find the ending delimiter, or a NUL byte
771 * is reached. Then, we take e - b to find the length of the token.
772 */
773 b = e = *p;
774
775 while (*e) {
776 if ((delim == ' ' && isspace(*e)) || delim == *e)
777 break;
778 e++;
779 }
780
781 len = e - b;
782
783 /*
784 * Allocate memory to hold the string, and copy it in, converting
785 * characters to lowercase if lower is != 0.
786 */
787 t->val = malloc(len + 1);
788 if (!t->val)
789 return NULL;
790
791 for (i = 0; i < len; i++, b++) {
792 if (lower)
793 t->val[i] = tolower(*b);
794 else
795 t->val[i] = *b;
796 }
797
798 t->val[len] = '\0';
799
800 /*
801 * Update *p so the caller knows where to continue scanning.
802 */
803 *p = e;
804
805 t->type = T_STRING;
806
807 return t->val;
808}
809
810/*
811 * Populate a keyword token with a type and value.
812 */
813static void get_keyword(struct token *t)
814{
815 int i;
816
817 for (i = 0; keywords[i].val; i++) {
818 if (!strcmp(t->val, keywords[i].val)) {
819 t->type = keywords[i].type;
820 break;
821 }
822 }
823}
824
825/*
826 * Get the next token. We have to keep track of which state we're in to know
827 * if we're looking to get a string literal or a keyword.
828 *
829 * *p is updated to point at the first character after the current token.
830 */
831static void get_token(char **p, struct token *t, enum lex_state state)
832{
833 char *c = *p;
834
835 t->type = T_INVALID;
836
837 /* eat non EOL whitespace */
838 while (isblank(*c))
839 c++;
840
841 /*
842 * eat comments. note that string literals can't begin with #, but
843 * can contain a # after their first character.
844 */
845 if (*c == '#') {
846 while (*c && *c != '\n')
847 c++;
848 }
849
850 if (*c == '\n') {
851 t->type = T_EOL;
852 c++;
853 } else if (*c == '\0') {
854 t->type = T_EOF;
855 c++;
856 } else if (state == L_SLITERAL) {
857 get_string(&c, t, '\n', 0);
858 } else if (state == L_KEYWORD) {
859 /*
860 * when we expect a keyword, we first get the next string
861 * token delimited by whitespace, and then check if it
862 * matches a keyword in our keyword list. if it does, it's
863 * converted to a keyword token of the appropriate type, and
864 * if not, it remains a string token.
865 */
866 get_string(&c, t, ' ', 1);
867 get_keyword(t);
868 }
869
870 *p = c;
871}
872
873/*
874 * Increment *c until we get to the end of the current line, or EOF.
875 */
876static void eol_or_eof(char **c)
877{
878 while (**c && **c != '\n')
879 (*c)++;
880}
881
882/*
883 * All of these parse_* functions share some common behavior.
884 *
885 * They finish with *c pointing after the token they parse, and return 1 on
886 * success, or < 0 on error.
887 */
888
889/*
890 * Parse a string literal and store a pointer it at *dst. String literals
891 * terminate at the end of the line.
892 */
893static int parse_sliteral(char **c, char **dst)
894{
895 struct token t;
896 char *s = *c;
897
898 get_token(c, &t, L_SLITERAL);
899
900 if (t.type != T_STRING) {
901 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
902 return -EINVAL;
903 }
904
905 *dst = t.val;
906
907 return 1;
908}
909
910/*
911 * Parse a base 10 (unsigned) integer and store it at *dst.
912 */
913static int parse_integer(char **c, int *dst)
914{
915 struct token t;
916 char *s = *c;
917 unsigned long temp;
918
919 get_token(c, &t, L_SLITERAL);
920
921 if (t.type != T_STRING) {
922 printf("Expected string: %.*s\n", (int)(*c - s), s);
923 return -EINVAL;
924 }
925
926 if (strict_strtoul(t.val, 10, &temp) < 0) {
927 printf("Expected unsigned integer: %s\n", t.val);
928 return -EINVAL;
929 }
930
931 *dst = (int)temp;
932
933 free(t.val);
934
935 return 1;
936}
937
938static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
939
940/*
941 * Parse an include statement, and retrieve and parse the file it mentions.
942 *
943 * base should point to a location where it's safe to store the file, and
944 * nest_level should indicate how many nested includes have occurred. For this
945 * include, nest_level has already been incremented and doesn't need to be
946 * incremented here.
947 */
948static int handle_include(char **c, char *base,
949 struct pxe_menu *cfg, int nest_level)
950{
951 char *include_path;
952 char *s = *c;
953 int err;
954
955 err = parse_sliteral(c, &include_path);
956
957 if (err < 0) {
958 printf("Expected include path: %.*s\n",
959 (int)(*c - s), s);
960 return err;
961 }
962
963 err = get_pxe_file(include_path, base);
964
965 if (err < 0) {
966 printf("Couldn't retrieve %s\n", include_path);
967 return err;
968 }
969
970 return parse_pxefile_top(base, cfg, nest_level);
971}
972
973/*
974 * Parse lines that begin with 'menu'.
975 *
976 * b and nest are provided to handle the 'menu include' case.
977 *
978 * b should be the address where the file currently being parsed is stored.
979 *
980 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
981 * a file it includes, 3 when parsing a file included by that file, and so on.
982 */
983static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
984{
985 struct token t;
986 char *s = *c;
Heiko Schocher43d4a5e2011-12-12 20:37:17 +0000987 int err = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500988
989 get_token(c, &t, L_KEYWORD);
990
991 switch (t.type) {
992 case T_TITLE:
993 err = parse_sliteral(c, &cfg->title);
994
995 break;
996
997 case T_INCLUDE:
998 err = handle_include(c, b + strlen(b) + 1, cfg,
999 nest_level + 1);
1000 break;
1001
1002 default:
1003 printf("Ignoring malformed menu command: %.*s\n",
1004 (int)(*c - s), s);
1005 }
1006
1007 if (err < 0)
1008 return err;
1009
1010 eol_or_eof(c);
1011
1012 return 1;
1013}
1014
1015/*
1016 * Handles parsing a 'menu line' when we're parsing a label.
1017 */
1018static int parse_label_menu(char **c, struct pxe_menu *cfg,
1019 struct pxe_label *label)
1020{
1021 struct token t;
1022 char *s;
1023
1024 s = *c;
1025
1026 get_token(c, &t, L_KEYWORD);
1027
1028 switch (t.type) {
1029 case T_DEFAULT:
1030 if (cfg->default_label)
1031 free(cfg->default_label);
1032
1033 cfg->default_label = strdup(label->name);
1034
1035 if (!cfg->default_label)
1036 return -ENOMEM;
1037
1038 break;
Rob Herring7815c4e2012-03-28 05:51:34 +00001039 case T_LABEL:
1040 parse_sliteral(c, &label->menu);
1041 break;
Jason Hobbs06283a62011-08-31 10:37:30 -05001042 default:
1043 printf("Ignoring malformed menu command: %.*s\n",
1044 (int)(*c - s), s);
1045 }
1046
1047 eol_or_eof(c);
1048
1049 return 0;
1050}
1051
1052/*
1053 * Parses a label and adds it to the list of labels for a menu.
1054 *
1055 * A label ends when we either get to the end of a file, or
1056 * get some input we otherwise don't have a handler defined
1057 * for.
1058 *
1059 */
1060static int parse_label(char **c, struct pxe_menu *cfg)
1061{
1062 struct token t;
Rob Herring34bd23e2012-03-28 05:51:37 +00001063 int len;
Jason Hobbs06283a62011-08-31 10:37:30 -05001064 char *s = *c;
1065 struct pxe_label *label;
1066 int err;
1067
1068 label = label_create();
1069 if (!label)
1070 return -ENOMEM;
1071
1072 err = parse_sliteral(c, &label->name);
1073 if (err < 0) {
1074 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1075 label_destroy(label);
1076 return -EINVAL;
1077 }
1078
1079 list_add_tail(&label->list, &cfg->labels);
1080
1081 while (1) {
1082 s = *c;
1083 get_token(c, &t, L_KEYWORD);
1084
1085 err = 0;
1086 switch (t.type) {
1087 case T_MENU:
1088 err = parse_label_menu(c, cfg, label);
1089 break;
1090
1091 case T_KERNEL:
Rob Herringbeb9f6c2012-03-28 05:51:35 +00001092 case T_LINUX:
Jason Hobbs06283a62011-08-31 10:37:30 -05001093 err = parse_sliteral(c, &label->kernel);
1094 break;
1095
1096 case T_APPEND:
1097 err = parse_sliteral(c, &label->append);
Rob Herring34bd23e2012-03-28 05:51:37 +00001098 if (label->initrd)
1099 break;
1100 s = strstr(label->append, "initrd=");
1101 if (!s)
1102 break;
1103 s += 7;
1104 len = (int)(strchr(s, ' ') - s);
1105 label->initrd = malloc(len + 1);
1106 strncpy(label->initrd, s, len);
1107 label->initrd[len] = '\0';
1108
Jason Hobbs06283a62011-08-31 10:37:30 -05001109 break;
1110
1111 case T_INITRD:
Rob Herring34bd23e2012-03-28 05:51:37 +00001112 if (!label->initrd)
1113 err = parse_sliteral(c, &label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -05001114 break;
1115
Chander Kashyapa6559382012-09-06 19:36:31 +00001116 case T_FDT:
1117 if (!label->fdt)
1118 err = parse_sliteral(c, &label->fdt);
1119 break;
1120
Jason Hobbs06283a62011-08-31 10:37:30 -05001121 case T_LOCALBOOT:
1122 err = parse_integer(c, &label->localboot);
1123 break;
1124
1125 case T_EOL:
1126 break;
1127
1128 default:
1129 /*
1130 * put the token back! we don't want it - it's the end
1131 * of a label and whatever token this is, it's
1132 * something for the menu level context to handle.
1133 */
1134 *c = s;
1135 return 1;
1136 }
1137
1138 if (err < 0)
1139 return err;
1140 }
1141}
1142
1143/*
1144 * This 16 comes from the limit pxelinux imposes on nested includes.
1145 *
1146 * There is no reason at all we couldn't do more, but some limit helps prevent
1147 * infinite (until crash occurs) recursion if a file tries to include itself.
1148 */
1149#define MAX_NEST_LEVEL 16
1150
1151/*
1152 * Entry point for parsing a menu file. nest_level indicates how many times
1153 * we've nested in includes. It will be 1 for the top level menu file.
1154 *
1155 * Returns 1 on success, < 0 on error.
1156 */
1157static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level)
1158{
1159 struct token t;
1160 char *s, *b, *label_name;
1161 int err;
1162
1163 b = p;
1164
1165 if (nest_level > MAX_NEST_LEVEL) {
1166 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1167 return -EMLINK;
1168 }
1169
1170 while (1) {
1171 s = p;
1172
1173 get_token(&p, &t, L_KEYWORD);
1174
1175 err = 0;
1176 switch (t.type) {
1177 case T_MENU:
Rob Herring2619f512012-04-12 13:33:43 -05001178 cfg->prompt = 1;
Jason Hobbs06283a62011-08-31 10:37:30 -05001179 err = parse_menu(&p, cfg, b, nest_level);
1180 break;
1181
1182 case T_TIMEOUT:
1183 err = parse_integer(&p, &cfg->timeout);
1184 break;
1185
1186 case T_LABEL:
1187 err = parse_label(&p, cfg);
1188 break;
1189
1190 case T_DEFAULT:
1191 err = parse_sliteral(&p, &label_name);
1192
1193 if (label_name) {
1194 if (cfg->default_label)
1195 free(cfg->default_label);
1196
1197 cfg->default_label = label_name;
1198 }
1199
1200 break;
1201
Rob Herring1e085222012-05-25 10:43:16 +00001202 case T_INCLUDE:
1203 err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg,
1204 nest_level + 1);
1205 break;
1206
Jason Hobbs06283a62011-08-31 10:37:30 -05001207 case T_PROMPT:
Rob Herring2619f512012-04-12 13:33:43 -05001208 eol_or_eof(&p);
Jason Hobbs06283a62011-08-31 10:37:30 -05001209 break;
1210
1211 case T_EOL:
1212 break;
1213
1214 case T_EOF:
1215 return 1;
1216
1217 default:
1218 printf("Ignoring unknown command: %.*s\n",
1219 (int)(p - s), s);
1220 eol_or_eof(&p);
1221 }
1222
1223 if (err < 0)
1224 return err;
1225 }
1226}
1227
1228/*
1229 * Free the memory used by a pxe_menu and its labels.
1230 */
1231static void destroy_pxe_menu(struct pxe_menu *cfg)
1232{
1233 struct list_head *pos, *n;
1234 struct pxe_label *label;
1235
1236 if (cfg->title)
1237 free(cfg->title);
1238
1239 if (cfg->default_label)
1240 free(cfg->default_label);
1241
1242 list_for_each_safe(pos, n, &cfg->labels) {
1243 label = list_entry(pos, struct pxe_label, list);
1244
1245 label_destroy(label);
1246 }
1247
1248 free(cfg);
1249}
1250
1251/*
1252 * Entry point for parsing a pxe file. This is only used for the top level
1253 * file.
1254 *
1255 * Returns NULL if there is an error, otherwise, returns a pointer to a
1256 * pxe_menu struct populated with the results of parsing the pxe file (and any
1257 * files it includes). The resulting pxe_menu struct can be free()'d by using
1258 * the destroy_pxe_menu() function.
1259 */
1260static struct pxe_menu *parse_pxefile(char *menucfg)
1261{
1262 struct pxe_menu *cfg;
1263
1264 cfg = malloc(sizeof(struct pxe_menu));
1265
1266 if (!cfg)
1267 return NULL;
1268
1269 memset(cfg, 0, sizeof(struct pxe_menu));
1270
1271 INIT_LIST_HEAD(&cfg->labels);
1272
1273 if (parse_pxefile_top(menucfg, cfg, 1) < 0) {
1274 destroy_pxe_menu(cfg);
1275 return NULL;
1276 }
1277
1278 return cfg;
1279}
1280
1281/*
1282 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1283 * menu code.
1284 */
1285static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1286{
1287 struct pxe_label *label;
1288 struct list_head *pos;
1289 struct menu *m;
1290 int err;
1291
1292 /*
1293 * Create a menu and add items for all the labels.
1294 */
1295 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print);
1296
1297 if (!m)
1298 return NULL;
1299
1300 list_for_each(pos, &cfg->labels) {
1301 label = list_entry(pos, struct pxe_label, list);
1302
1303 if (menu_item_add(m, label->name, label) != 1) {
1304 menu_destroy(m);
1305 return NULL;
1306 }
1307 }
1308
1309 /*
1310 * After we've created items for each label in the menu, set the
1311 * menu's default label if one was specified.
1312 */
1313 if (cfg->default_label) {
1314 err = menu_default_set(m, cfg->default_label);
1315 if (err != 1) {
1316 if (err != -ENOENT) {
1317 menu_destroy(m);
1318 return NULL;
1319 }
1320
1321 printf("Missing default: %s\n", cfg->default_label);
1322 }
1323 }
1324
1325 return m;
1326}
1327
1328/*
1329 * Try to boot any labels we have yet to attempt to boot.
1330 */
1331static void boot_unattempted_labels(struct pxe_menu *cfg)
1332{
1333 struct list_head *pos;
1334 struct pxe_label *label;
1335
1336 list_for_each(pos, &cfg->labels) {
1337 label = list_entry(pos, struct pxe_label, list);
1338
1339 if (!label->attempted)
1340 label_boot(label);
1341 }
1342}
1343
1344/*
1345 * Boot the system as prescribed by a pxe_menu.
1346 *
1347 * Use the menu system to either get the user's choice or the default, based
1348 * on config or user input. If there is no default or user's choice,
1349 * attempted to boot labels in the order they were given in pxe files.
1350 * If the default or user's choice fails to boot, attempt to boot other
1351 * labels in the order they were given in pxe files.
1352 *
1353 * If this function returns, there weren't any labels that successfully
1354 * booted, or the user interrupted the menu selection via ctrl+c.
1355 */
1356static void handle_pxe_menu(struct pxe_menu *cfg)
1357{
1358 void *choice;
1359 struct menu *m;
1360 int err;
1361
1362 m = pxe_menu_to_menu(cfg);
1363 if (!m)
1364 return;
1365
1366 err = menu_get_choice(m, &choice);
1367
1368 menu_destroy(m);
1369
Jason Hobbs6f40f272011-11-07 03:07:15 +00001370 /*
1371 * err == 1 means we got a choice back from menu_get_choice.
1372 *
1373 * err == -ENOENT if the menu was setup to select the default but no
1374 * default was set. in that case, we should continue trying to boot
1375 * labels that haven't been attempted yet.
1376 *
1377 * otherwise, the user interrupted or there was some other error and
1378 * we give up.
1379 */
Jason Hobbs06283a62011-08-31 10:37:30 -05001380
Jason Hobbs6f40f272011-11-07 03:07:15 +00001381 if (err == 1)
1382 label_boot(choice);
1383 else if (err != -ENOENT)
1384 return;
Jason Hobbs06283a62011-08-31 10:37:30 -05001385
1386 boot_unattempted_labels(cfg);
1387}
1388
1389/*
1390 * Boots a system using a pxe file
1391 *
1392 * Returns 0 on success, 1 on error.
1393 */
1394static int
1395do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1396{
1397 unsigned long pxefile_addr_r;
1398 struct pxe_menu *cfg;
1399 char *pxefile_addr_str;
1400
Rob Herring669df7e2012-05-25 10:47:39 +00001401 do_getfile = do_get_tftp;
1402
Jason Hobbs06283a62011-08-31 10:37:30 -05001403 if (argc == 1) {
1404 pxefile_addr_str = from_env("pxefile_addr_r");
1405 if (!pxefile_addr_str)
1406 return 1;
1407
1408 } else if (argc == 2) {
1409 pxefile_addr_str = argv[1];
1410 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +00001411 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001412 }
1413
1414 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1415 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1416 return 1;
1417 }
1418
1419 cfg = parse_pxefile((char *)(pxefile_addr_r));
1420
1421 if (cfg == NULL) {
1422 printf("Error parsing config file\n");
1423 return 1;
1424 }
1425
1426 handle_pxe_menu(cfg);
1427
1428 destroy_pxe_menu(cfg);
1429
1430 return 0;
1431}
1432
1433static cmd_tbl_t cmd_pxe_sub[] = {
1434 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1435 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1436};
1437
1438int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1439{
1440 cmd_tbl_t *cp;
1441
1442 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001443 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001444
1445 /* drop initial "pxe" arg */
1446 argc--;
1447 argv++;
1448
1449 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1450
1451 if (cp)
1452 return cp->cmd(cmdtp, flag, argc, argv);
1453
Simon Glass4c12eeb2011-12-10 08:44:01 +00001454 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001455}
1456
1457U_BOOT_CMD(
1458 pxe, 3, 1, do_pxe,
1459 "commands to get and boot from pxe files",
1460 "get - try to retrieve a pxe file using tftp\npxe "
1461 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1462);
Rob Herring669df7e2012-05-25 10:47:39 +00001463
1464/*
1465 * Boots a system using a local disk syslinux/extlinux file
1466 *
1467 * Returns 0 on success, 1 on error.
1468 */
1469int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1470{
1471 unsigned long pxefile_addr_r;
1472 struct pxe_menu *cfg;
1473 char *pxefile_addr_str;
1474 char *filename;
1475 int prompt = 0;
1476
1477 if (strstr(argv[1], "-p")) {
1478 prompt = 1;
1479 argc--;
1480 argv++;
1481 }
1482
1483 if (argc < 4)
1484 return cmd_usage(cmdtp);
1485
1486 if (argc < 5) {
1487 pxefile_addr_str = from_env("pxefile_addr_r");
1488 if (!pxefile_addr_str)
1489 return 1;
1490 } else {
1491 pxefile_addr_str = argv[4];
1492 }
1493
1494 if (argc < 6)
1495 filename = getenv("bootfile");
1496 else {
1497 filename = argv[5];
1498 setenv("bootfile", filename);
1499 }
1500
1501 if (strstr(argv[3], "ext2"))
1502 do_getfile = do_get_ext2;
1503 else if (strstr(argv[3], "fat"))
1504 do_getfile = do_get_fat;
1505 else {
1506 printf("Invalid filesystem: %s\n", argv[3]);
1507 return 1;
1508 }
1509 fs_argv[1] = argv[1];
1510 fs_argv[2] = argv[2];
1511
1512 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1513 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1514 return 1;
1515 }
1516
1517 if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) {
1518 printf("Error reading config file\n");
1519 return 1;
1520 }
1521
1522 cfg = parse_pxefile((char *)(pxefile_addr_r));
1523
1524 if (cfg == NULL) {
1525 printf("Error parsing config file\n");
1526 return 1;
1527 }
1528
1529 if (prompt)
1530 cfg->prompt = 1;
1531
1532 handle_pxe_menu(cfg);
1533
1534 destroy_pxe_menu(cfg);
1535
1536 return 0;
1537}
1538
1539U_BOOT_CMD(
1540 sysboot, 7, 1, do_sysboot,
1541 "command to get and boot from syslinux files",
1542 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1543 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1544 " filesystem on 'dev' on 'interface' to address 'addr'"
1545);