blob: ed09d2193958a52d01b2a47679b00e89b97e37c6 [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 };
600 int bootm_argc = 3;
601
602 label_print(label);
603
604 label->attempted = 1;
605
606 if (label->localboot) {
607 label_localboot(label);
608 return;
609 }
610
611 if (label->kernel == NULL) {
612 printf("No kernel given, skipping %s\n",
613 label->name);
614 return;
615 }
616
617 if (label->initrd) {
618 if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) {
619 printf("Skipping %s for failure retrieving initrd\n",
620 label->name);
621 return;
622 }
623
624 bootm_argv[2] = getenv("ramdisk_addr_r");
625 } else {
626 bootm_argv[2] = "-";
627 }
628
629 if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) {
630 printf("Skipping %s for failure retrieving kernel\n",
631 label->name);
632 return;
633 }
634
635 if (label->append)
636 setenv("bootargs", label->append);
637
638 bootm_argv[1] = getenv("kernel_addr_r");
639
640 /*
Chander Kashyapa6559382012-09-06 19:36:31 +0000641 * fdt usage is optional:
642 * It handles the following scenarios. All scenarios are exclusive
643 *
644 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
645 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
646 * and adjust argc appropriately.
647 *
648 * Scenario 2: If there is an fdt_addr specified, pass it along to
649 * bootm, and adjust argc appropriately.
650 *
651 * Scenario 3: fdt blob is not available.
Jason Hobbs06283a62011-08-31 10:37:30 -0500652 */
Chander Kashyapa6559382012-09-06 19:36:31 +0000653 bootm_argv[3] = getenv("fdt_addr_r");
654
655 /* if fdt label is defined then get fdt from server */
656 if (bootm_argv[3] && label->fdt) {
657 if (get_relfile_envaddr(label->fdt, "fdt_addr_r") < 0) {
658 printf("Skipping %s for failure retrieving fdt\n",
659 label->name);
660 return;
661 }
662 } else
663 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs06283a62011-08-31 10:37:30 -0500664
665 if (bootm_argv[3])
666 bootm_argc = 4;
667
668 do_bootm(NULL, 0, bootm_argc, bootm_argv);
669}
670
671/*
672 * Tokens for the pxe file parser.
673 */
674enum token_type {
675 T_EOL,
676 T_STRING,
677 T_EOF,
678 T_MENU,
679 T_TITLE,
680 T_TIMEOUT,
681 T_LABEL,
682 T_KERNEL,
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000683 T_LINUX,
Jason Hobbs06283a62011-08-31 10:37:30 -0500684 T_APPEND,
685 T_INITRD,
686 T_LOCALBOOT,
687 T_DEFAULT,
688 T_PROMPT,
689 T_INCLUDE,
Chander Kashyapa6559382012-09-06 19:36:31 +0000690 T_FDT,
Jason Hobbs06283a62011-08-31 10:37:30 -0500691 T_INVALID
692};
693
694/*
695 * A token - given by a value and a type.
696 */
697struct token {
698 char *val;
699 enum token_type type;
700};
701
702/*
703 * Keywords recognized.
704 */
705static const struct token keywords[] = {
706 {"menu", T_MENU},
707 {"title", T_TITLE},
708 {"timeout", T_TIMEOUT},
709 {"default", T_DEFAULT},
710 {"prompt", T_PROMPT},
711 {"label", T_LABEL},
712 {"kernel", T_KERNEL},
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000713 {"linux", T_LINUX},
Jason Hobbs06283a62011-08-31 10:37:30 -0500714 {"localboot", T_LOCALBOOT},
715 {"append", T_APPEND},
716 {"initrd", T_INITRD},
717 {"include", T_INCLUDE},
Chander Kashyapa6559382012-09-06 19:36:31 +0000718 {"fdt", T_FDT},
Jason Hobbs06283a62011-08-31 10:37:30 -0500719 {NULL, T_INVALID}
720};
721
722/*
723 * Since pxe(linux) files don't have a token to identify the start of a
724 * literal, we have to keep track of when we're in a state where a literal is
725 * expected vs when we're in a state a keyword is expected.
726 */
727enum lex_state {
728 L_NORMAL = 0,
729 L_KEYWORD,
730 L_SLITERAL
731};
732
733/*
734 * get_string retrieves a string from *p and stores it as a token in
735 * *t.
736 *
737 * get_string used for scanning both string literals and keywords.
738 *
739 * Characters from *p are copied into t-val until a character equal to
740 * delim is found, or a NUL byte is reached. If delim has the special value of
741 * ' ', any whitespace character will be used as a delimiter.
742 *
743 * If lower is unequal to 0, uppercase characters will be converted to
744 * lowercase in the result. This is useful to make keywords case
745 * insensitive.
746 *
747 * The location of *p is updated to point to the first character after the end
748 * of the token - the ending delimiter.
749 *
750 * On success, the new value of t->val is returned. Memory for t->val is
751 * allocated using malloc and must be free()'d to reclaim it. If insufficient
752 * memory is available, NULL is returned.
753 */
754static char *get_string(char **p, struct token *t, char delim, int lower)
755{
756 char *b, *e;
757 size_t len, i;
758
759 /*
760 * b and e both start at the beginning of the input stream.
761 *
762 * e is incremented until we find the ending delimiter, or a NUL byte
763 * is reached. Then, we take e - b to find the length of the token.
764 */
765 b = e = *p;
766
767 while (*e) {
768 if ((delim == ' ' && isspace(*e)) || delim == *e)
769 break;
770 e++;
771 }
772
773 len = e - b;
774
775 /*
776 * Allocate memory to hold the string, and copy it in, converting
777 * characters to lowercase if lower is != 0.
778 */
779 t->val = malloc(len + 1);
780 if (!t->val)
781 return NULL;
782
783 for (i = 0; i < len; i++, b++) {
784 if (lower)
785 t->val[i] = tolower(*b);
786 else
787 t->val[i] = *b;
788 }
789
790 t->val[len] = '\0';
791
792 /*
793 * Update *p so the caller knows where to continue scanning.
794 */
795 *p = e;
796
797 t->type = T_STRING;
798
799 return t->val;
800}
801
802/*
803 * Populate a keyword token with a type and value.
804 */
805static void get_keyword(struct token *t)
806{
807 int i;
808
809 for (i = 0; keywords[i].val; i++) {
810 if (!strcmp(t->val, keywords[i].val)) {
811 t->type = keywords[i].type;
812 break;
813 }
814 }
815}
816
817/*
818 * Get the next token. We have to keep track of which state we're in to know
819 * if we're looking to get a string literal or a keyword.
820 *
821 * *p is updated to point at the first character after the current token.
822 */
823static void get_token(char **p, struct token *t, enum lex_state state)
824{
825 char *c = *p;
826
827 t->type = T_INVALID;
828
829 /* eat non EOL whitespace */
830 while (isblank(*c))
831 c++;
832
833 /*
834 * eat comments. note that string literals can't begin with #, but
835 * can contain a # after their first character.
836 */
837 if (*c == '#') {
838 while (*c && *c != '\n')
839 c++;
840 }
841
842 if (*c == '\n') {
843 t->type = T_EOL;
844 c++;
845 } else if (*c == '\0') {
846 t->type = T_EOF;
847 c++;
848 } else if (state == L_SLITERAL) {
849 get_string(&c, t, '\n', 0);
850 } else if (state == L_KEYWORD) {
851 /*
852 * when we expect a keyword, we first get the next string
853 * token delimited by whitespace, and then check if it
854 * matches a keyword in our keyword list. if it does, it's
855 * converted to a keyword token of the appropriate type, and
856 * if not, it remains a string token.
857 */
858 get_string(&c, t, ' ', 1);
859 get_keyword(t);
860 }
861
862 *p = c;
863}
864
865/*
866 * Increment *c until we get to the end of the current line, or EOF.
867 */
868static void eol_or_eof(char **c)
869{
870 while (**c && **c != '\n')
871 (*c)++;
872}
873
874/*
875 * All of these parse_* functions share some common behavior.
876 *
877 * They finish with *c pointing after the token they parse, and return 1 on
878 * success, or < 0 on error.
879 */
880
881/*
882 * Parse a string literal and store a pointer it at *dst. String literals
883 * terminate at the end of the line.
884 */
885static int parse_sliteral(char **c, char **dst)
886{
887 struct token t;
888 char *s = *c;
889
890 get_token(c, &t, L_SLITERAL);
891
892 if (t.type != T_STRING) {
893 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
894 return -EINVAL;
895 }
896
897 *dst = t.val;
898
899 return 1;
900}
901
902/*
903 * Parse a base 10 (unsigned) integer and store it at *dst.
904 */
905static int parse_integer(char **c, int *dst)
906{
907 struct token t;
908 char *s = *c;
909 unsigned long temp;
910
911 get_token(c, &t, L_SLITERAL);
912
913 if (t.type != T_STRING) {
914 printf("Expected string: %.*s\n", (int)(*c - s), s);
915 return -EINVAL;
916 }
917
918 if (strict_strtoul(t.val, 10, &temp) < 0) {
919 printf("Expected unsigned integer: %s\n", t.val);
920 return -EINVAL;
921 }
922
923 *dst = (int)temp;
924
925 free(t.val);
926
927 return 1;
928}
929
930static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
931
932/*
933 * Parse an include statement, and retrieve and parse the file it mentions.
934 *
935 * base should point to a location where it's safe to store the file, and
936 * nest_level should indicate how many nested includes have occurred. For this
937 * include, nest_level has already been incremented and doesn't need to be
938 * incremented here.
939 */
940static int handle_include(char **c, char *base,
941 struct pxe_menu *cfg, int nest_level)
942{
943 char *include_path;
944 char *s = *c;
945 int err;
946
947 err = parse_sliteral(c, &include_path);
948
949 if (err < 0) {
950 printf("Expected include path: %.*s\n",
951 (int)(*c - s), s);
952 return err;
953 }
954
955 err = get_pxe_file(include_path, base);
956
957 if (err < 0) {
958 printf("Couldn't retrieve %s\n", include_path);
959 return err;
960 }
961
962 return parse_pxefile_top(base, cfg, nest_level);
963}
964
965/*
966 * Parse lines that begin with 'menu'.
967 *
968 * b and nest are provided to handle the 'menu include' case.
969 *
970 * b should be the address where the file currently being parsed is stored.
971 *
972 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
973 * a file it includes, 3 when parsing a file included by that file, and so on.
974 */
975static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
976{
977 struct token t;
978 char *s = *c;
Heiko Schocher43d4a5e2011-12-12 20:37:17 +0000979 int err = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500980
981 get_token(c, &t, L_KEYWORD);
982
983 switch (t.type) {
984 case T_TITLE:
985 err = parse_sliteral(c, &cfg->title);
986
987 break;
988
989 case T_INCLUDE:
990 err = handle_include(c, b + strlen(b) + 1, cfg,
991 nest_level + 1);
992 break;
993
994 default:
995 printf("Ignoring malformed menu command: %.*s\n",
996 (int)(*c - s), s);
997 }
998
999 if (err < 0)
1000 return err;
1001
1002 eol_or_eof(c);
1003
1004 return 1;
1005}
1006
1007/*
1008 * Handles parsing a 'menu line' when we're parsing a label.
1009 */
1010static int parse_label_menu(char **c, struct pxe_menu *cfg,
1011 struct pxe_label *label)
1012{
1013 struct token t;
1014 char *s;
1015
1016 s = *c;
1017
1018 get_token(c, &t, L_KEYWORD);
1019
1020 switch (t.type) {
1021 case T_DEFAULT:
1022 if (cfg->default_label)
1023 free(cfg->default_label);
1024
1025 cfg->default_label = strdup(label->name);
1026
1027 if (!cfg->default_label)
1028 return -ENOMEM;
1029
1030 break;
Rob Herring7815c4e2012-03-28 05:51:34 +00001031 case T_LABEL:
1032 parse_sliteral(c, &label->menu);
1033 break;
Jason Hobbs06283a62011-08-31 10:37:30 -05001034 default:
1035 printf("Ignoring malformed menu command: %.*s\n",
1036 (int)(*c - s), s);
1037 }
1038
1039 eol_or_eof(c);
1040
1041 return 0;
1042}
1043
1044/*
1045 * Parses a label and adds it to the list of labels for a menu.
1046 *
1047 * A label ends when we either get to the end of a file, or
1048 * get some input we otherwise don't have a handler defined
1049 * for.
1050 *
1051 */
1052static int parse_label(char **c, struct pxe_menu *cfg)
1053{
1054 struct token t;
Rob Herring34bd23e2012-03-28 05:51:37 +00001055 int len;
Jason Hobbs06283a62011-08-31 10:37:30 -05001056 char *s = *c;
1057 struct pxe_label *label;
1058 int err;
1059
1060 label = label_create();
1061 if (!label)
1062 return -ENOMEM;
1063
1064 err = parse_sliteral(c, &label->name);
1065 if (err < 0) {
1066 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1067 label_destroy(label);
1068 return -EINVAL;
1069 }
1070
1071 list_add_tail(&label->list, &cfg->labels);
1072
1073 while (1) {
1074 s = *c;
1075 get_token(c, &t, L_KEYWORD);
1076
1077 err = 0;
1078 switch (t.type) {
1079 case T_MENU:
1080 err = parse_label_menu(c, cfg, label);
1081 break;
1082
1083 case T_KERNEL:
Rob Herringbeb9f6c2012-03-28 05:51:35 +00001084 case T_LINUX:
Jason Hobbs06283a62011-08-31 10:37:30 -05001085 err = parse_sliteral(c, &label->kernel);
1086 break;
1087
1088 case T_APPEND:
1089 err = parse_sliteral(c, &label->append);
Rob Herring34bd23e2012-03-28 05:51:37 +00001090 if (label->initrd)
1091 break;
1092 s = strstr(label->append, "initrd=");
1093 if (!s)
1094 break;
1095 s += 7;
1096 len = (int)(strchr(s, ' ') - s);
1097 label->initrd = malloc(len + 1);
1098 strncpy(label->initrd, s, len);
1099 label->initrd[len] = '\0';
1100
Jason Hobbs06283a62011-08-31 10:37:30 -05001101 break;
1102
1103 case T_INITRD:
Rob Herring34bd23e2012-03-28 05:51:37 +00001104 if (!label->initrd)
1105 err = parse_sliteral(c, &label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -05001106 break;
1107
Chander Kashyapa6559382012-09-06 19:36:31 +00001108 case T_FDT:
1109 if (!label->fdt)
1110 err = parse_sliteral(c, &label->fdt);
1111 break;
1112
Jason Hobbs06283a62011-08-31 10:37:30 -05001113 case T_LOCALBOOT:
1114 err = parse_integer(c, &label->localboot);
1115 break;
1116
1117 case T_EOL:
1118 break;
1119
1120 default:
1121 /*
1122 * put the token back! we don't want it - it's the end
1123 * of a label and whatever token this is, it's
1124 * something for the menu level context to handle.
1125 */
1126 *c = s;
1127 return 1;
1128 }
1129
1130 if (err < 0)
1131 return err;
1132 }
1133}
1134
1135/*
1136 * This 16 comes from the limit pxelinux imposes on nested includes.
1137 *
1138 * There is no reason at all we couldn't do more, but some limit helps prevent
1139 * infinite (until crash occurs) recursion if a file tries to include itself.
1140 */
1141#define MAX_NEST_LEVEL 16
1142
1143/*
1144 * Entry point for parsing a menu file. nest_level indicates how many times
1145 * we've nested in includes. It will be 1 for the top level menu file.
1146 *
1147 * Returns 1 on success, < 0 on error.
1148 */
1149static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level)
1150{
1151 struct token t;
1152 char *s, *b, *label_name;
1153 int err;
1154
1155 b = p;
1156
1157 if (nest_level > MAX_NEST_LEVEL) {
1158 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1159 return -EMLINK;
1160 }
1161
1162 while (1) {
1163 s = p;
1164
1165 get_token(&p, &t, L_KEYWORD);
1166
1167 err = 0;
1168 switch (t.type) {
1169 case T_MENU:
1170 err = parse_menu(&p, cfg, b, nest_level);
1171 break;
1172
1173 case T_TIMEOUT:
1174 err = parse_integer(&p, &cfg->timeout);
1175 break;
1176
1177 case T_LABEL:
1178 err = parse_label(&p, cfg);
1179 break;
1180
1181 case T_DEFAULT:
1182 err = parse_sliteral(&p, &label_name);
1183
1184 if (label_name) {
1185 if (cfg->default_label)
1186 free(cfg->default_label);
1187
1188 cfg->default_label = label_name;
1189 }
1190
1191 break;
1192
Rob Herring1e085222012-05-25 10:43:16 +00001193 case T_INCLUDE:
1194 err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg,
1195 nest_level + 1);
1196 break;
1197
Jason Hobbs06283a62011-08-31 10:37:30 -05001198 case T_PROMPT:
1199 err = parse_integer(&p, &cfg->prompt);
1200 break;
1201
1202 case T_EOL:
1203 break;
1204
1205 case T_EOF:
1206 return 1;
1207
1208 default:
1209 printf("Ignoring unknown command: %.*s\n",
1210 (int)(p - s), s);
1211 eol_or_eof(&p);
1212 }
1213
1214 if (err < 0)
1215 return err;
1216 }
1217}
1218
1219/*
1220 * Free the memory used by a pxe_menu and its labels.
1221 */
1222static void destroy_pxe_menu(struct pxe_menu *cfg)
1223{
1224 struct list_head *pos, *n;
1225 struct pxe_label *label;
1226
1227 if (cfg->title)
1228 free(cfg->title);
1229
1230 if (cfg->default_label)
1231 free(cfg->default_label);
1232
1233 list_for_each_safe(pos, n, &cfg->labels) {
1234 label = list_entry(pos, struct pxe_label, list);
1235
1236 label_destroy(label);
1237 }
1238
1239 free(cfg);
1240}
1241
1242/*
1243 * Entry point for parsing a pxe file. This is only used for the top level
1244 * file.
1245 *
1246 * Returns NULL if there is an error, otherwise, returns a pointer to a
1247 * pxe_menu struct populated with the results of parsing the pxe file (and any
1248 * files it includes). The resulting pxe_menu struct can be free()'d by using
1249 * the destroy_pxe_menu() function.
1250 */
1251static struct pxe_menu *parse_pxefile(char *menucfg)
1252{
1253 struct pxe_menu *cfg;
1254
1255 cfg = malloc(sizeof(struct pxe_menu));
1256
1257 if (!cfg)
1258 return NULL;
1259
1260 memset(cfg, 0, sizeof(struct pxe_menu));
1261
1262 INIT_LIST_HEAD(&cfg->labels);
1263
1264 if (parse_pxefile_top(menucfg, cfg, 1) < 0) {
1265 destroy_pxe_menu(cfg);
1266 return NULL;
1267 }
1268
1269 return cfg;
1270}
1271
1272/*
1273 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1274 * menu code.
1275 */
1276static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1277{
1278 struct pxe_label *label;
1279 struct list_head *pos;
1280 struct menu *m;
1281 int err;
1282
1283 /*
1284 * Create a menu and add items for all the labels.
1285 */
1286 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print);
1287
1288 if (!m)
1289 return NULL;
1290
1291 list_for_each(pos, &cfg->labels) {
1292 label = list_entry(pos, struct pxe_label, list);
1293
1294 if (menu_item_add(m, label->name, label) != 1) {
1295 menu_destroy(m);
1296 return NULL;
1297 }
1298 }
1299
1300 /*
1301 * After we've created items for each label in the menu, set the
1302 * menu's default label if one was specified.
1303 */
1304 if (cfg->default_label) {
1305 err = menu_default_set(m, cfg->default_label);
1306 if (err != 1) {
1307 if (err != -ENOENT) {
1308 menu_destroy(m);
1309 return NULL;
1310 }
1311
1312 printf("Missing default: %s\n", cfg->default_label);
1313 }
1314 }
1315
1316 return m;
1317}
1318
1319/*
1320 * Try to boot any labels we have yet to attempt to boot.
1321 */
1322static void boot_unattempted_labels(struct pxe_menu *cfg)
1323{
1324 struct list_head *pos;
1325 struct pxe_label *label;
1326
1327 list_for_each(pos, &cfg->labels) {
1328 label = list_entry(pos, struct pxe_label, list);
1329
1330 if (!label->attempted)
1331 label_boot(label);
1332 }
1333}
1334
1335/*
1336 * Boot the system as prescribed by a pxe_menu.
1337 *
1338 * Use the menu system to either get the user's choice or the default, based
1339 * on config or user input. If there is no default or user's choice,
1340 * attempted to boot labels in the order they were given in pxe files.
1341 * If the default or user's choice fails to boot, attempt to boot other
1342 * labels in the order they were given in pxe files.
1343 *
1344 * If this function returns, there weren't any labels that successfully
1345 * booted, or the user interrupted the menu selection via ctrl+c.
1346 */
1347static void handle_pxe_menu(struct pxe_menu *cfg)
1348{
1349 void *choice;
1350 struct menu *m;
1351 int err;
1352
1353 m = pxe_menu_to_menu(cfg);
1354 if (!m)
1355 return;
1356
1357 err = menu_get_choice(m, &choice);
1358
1359 menu_destroy(m);
1360
Jason Hobbs6f40f272011-11-07 03:07:15 +00001361 /*
1362 * err == 1 means we got a choice back from menu_get_choice.
1363 *
1364 * err == -ENOENT if the menu was setup to select the default but no
1365 * default was set. in that case, we should continue trying to boot
1366 * labels that haven't been attempted yet.
1367 *
1368 * otherwise, the user interrupted or there was some other error and
1369 * we give up.
1370 */
Jason Hobbs06283a62011-08-31 10:37:30 -05001371
Jason Hobbs6f40f272011-11-07 03:07:15 +00001372 if (err == 1)
1373 label_boot(choice);
1374 else if (err != -ENOENT)
1375 return;
Jason Hobbs06283a62011-08-31 10:37:30 -05001376
1377 boot_unattempted_labels(cfg);
1378}
1379
1380/*
1381 * Boots a system using a pxe file
1382 *
1383 * Returns 0 on success, 1 on error.
1384 */
1385static int
1386do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1387{
1388 unsigned long pxefile_addr_r;
1389 struct pxe_menu *cfg;
1390 char *pxefile_addr_str;
1391
Rob Herring669df7e2012-05-25 10:47:39 +00001392 do_getfile = do_get_tftp;
1393
Jason Hobbs06283a62011-08-31 10:37:30 -05001394 if (argc == 1) {
1395 pxefile_addr_str = from_env("pxefile_addr_r");
1396 if (!pxefile_addr_str)
1397 return 1;
1398
1399 } else if (argc == 2) {
1400 pxefile_addr_str = argv[1];
1401 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +00001402 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001403 }
1404
1405 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1406 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1407 return 1;
1408 }
1409
1410 cfg = parse_pxefile((char *)(pxefile_addr_r));
1411
1412 if (cfg == NULL) {
1413 printf("Error parsing config file\n");
1414 return 1;
1415 }
1416
1417 handle_pxe_menu(cfg);
1418
1419 destroy_pxe_menu(cfg);
1420
1421 return 0;
1422}
1423
1424static cmd_tbl_t cmd_pxe_sub[] = {
1425 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1426 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1427};
1428
1429int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1430{
1431 cmd_tbl_t *cp;
1432
1433 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001434 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001435
1436 /* drop initial "pxe" arg */
1437 argc--;
1438 argv++;
1439
1440 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1441
1442 if (cp)
1443 return cp->cmd(cmdtp, flag, argc, argv);
1444
Simon Glass4c12eeb2011-12-10 08:44:01 +00001445 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001446}
1447
1448U_BOOT_CMD(
1449 pxe, 3, 1, do_pxe,
1450 "commands to get and boot from pxe files",
1451 "get - try to retrieve a pxe file using tftp\npxe "
1452 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1453);
Rob Herring669df7e2012-05-25 10:47:39 +00001454
1455/*
1456 * Boots a system using a local disk syslinux/extlinux file
1457 *
1458 * Returns 0 on success, 1 on error.
1459 */
1460int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1461{
1462 unsigned long pxefile_addr_r;
1463 struct pxe_menu *cfg;
1464 char *pxefile_addr_str;
1465 char *filename;
1466 int prompt = 0;
1467
1468 if (strstr(argv[1], "-p")) {
1469 prompt = 1;
1470 argc--;
1471 argv++;
1472 }
1473
1474 if (argc < 4)
1475 return cmd_usage(cmdtp);
1476
1477 if (argc < 5) {
1478 pxefile_addr_str = from_env("pxefile_addr_r");
1479 if (!pxefile_addr_str)
1480 return 1;
1481 } else {
1482 pxefile_addr_str = argv[4];
1483 }
1484
1485 if (argc < 6)
1486 filename = getenv("bootfile");
1487 else {
1488 filename = argv[5];
1489 setenv("bootfile", filename);
1490 }
1491
1492 if (strstr(argv[3], "ext2"))
1493 do_getfile = do_get_ext2;
1494 else if (strstr(argv[3], "fat"))
1495 do_getfile = do_get_fat;
1496 else {
1497 printf("Invalid filesystem: %s\n", argv[3]);
1498 return 1;
1499 }
1500 fs_argv[1] = argv[1];
1501 fs_argv[2] = argv[2];
1502
1503 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1504 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1505 return 1;
1506 }
1507
1508 if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) {
1509 printf("Error reading config file\n");
1510 return 1;
1511 }
1512
1513 cfg = parse_pxefile((char *)(pxefile_addr_r));
1514
1515 if (cfg == NULL) {
1516 printf("Error parsing config file\n");
1517 return 1;
1518 }
1519
1520 if (prompt)
1521 cfg->prompt = 1;
1522
1523 handle_pxe_menu(cfg);
1524
1525 destroy_pxe_menu(cfg);
1526
1527 return 0;
1528}
1529
1530U_BOOT_CMD(
1531 sysboot, 7, 1, do_sysboot,
1532 "command to get and boot from syslinux files",
1533 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1534 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1535 " filesystem on 'dev' on 'interface' to address 'addr'"
1536);