blob: 83bf67365a910c8ea0149bcc0b14088c0b7bcb53 [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;
Rob Herringc27d5602012-04-12 13:55:20 -0500459 int localboot_val;
Jason Hobbs06283a62011-08-31 10:37:30 -0500460 struct list_head list;
461};
462
463/*
464 * Describes a pxe menu as given via pxe files.
465 *
466 * title - the name of the menu as given by a 'menu title' line.
467 * default_label - the name of the default label, if any.
468 * timeout - time in tenths of a second to wait for a user key-press before
469 * booting the default label.
470 * prompt - if 0, don't prompt for a choice unless the timeout period is
471 * interrupted. If 1, always prompt for a choice regardless of
472 * timeout.
473 * labels - a list of labels defined for the menu.
474 */
475struct pxe_menu {
476 char *title;
477 char *default_label;
478 int timeout;
479 int prompt;
480 struct list_head labels;
481};
482
483/*
484 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
485 * result must be free()'d to reclaim the memory.
486 *
487 * Returns NULL if malloc fails.
488 */
489static struct pxe_label *label_create(void)
490{
491 struct pxe_label *label;
492
493 label = malloc(sizeof(struct pxe_label));
494
495 if (!label)
496 return NULL;
497
498 memset(label, 0, sizeof(struct pxe_label));
499
500 return label;
501}
502
503/*
504 * Free the memory used by a pxe_label, including that used by its name,
505 * kernel, append and initrd members, if they're non NULL.
506 *
507 * So - be sure to only use dynamically allocated memory for the members of
508 * the pxe_label struct, unless you want to clean it up first. These are
509 * currently only created by the pxe file parsing code.
510 */
511static void label_destroy(struct pxe_label *label)
512{
513 if (label->name)
514 free(label->name);
515
516 if (label->kernel)
517 free(label->kernel);
518
519 if (label->append)
520 free(label->append);
521
522 if (label->initrd)
523 free(label->initrd);
524
Chander Kashyapa6559382012-09-06 19:36:31 +0000525 if (label->fdt)
526 free(label->fdt);
527
Jason Hobbs06283a62011-08-31 10:37:30 -0500528 free(label);
529}
530
531/*
532 * Print a label and its string members if they're defined.
533 *
534 * This is passed as a callback to the menu code for displaying each
535 * menu entry.
536 */
537static void label_print(void *data)
538{
539 struct pxe_label *label = data;
Rob Herring7815c4e2012-03-28 05:51:34 +0000540 const char *c = label->menu ? label->menu : label->kernel;
Jason Hobbs06283a62011-08-31 10:37:30 -0500541
Rob Herring7815c4e2012-03-28 05:51:34 +0000542 printf("%s:\t%s\n", label->name, c);
Jason Hobbs06283a62011-08-31 10:37:30 -0500543
544 if (label->kernel)
Rob Herring7815c4e2012-03-28 05:51:34 +0000545 printf("\t\tkernel: %s\n", label->kernel);
Jason Hobbs06283a62011-08-31 10:37:30 -0500546
547 if (label->append)
Rob Herring7815c4e2012-03-28 05:51:34 +0000548 printf("\t\tappend: %s\n", label->append);
Jason Hobbs06283a62011-08-31 10:37:30 -0500549
550 if (label->initrd)
Rob Herring7815c4e2012-03-28 05:51:34 +0000551 printf("\t\tinitrd: %s\n", label->initrd);
Chander Kashyapa6559382012-09-06 19:36:31 +0000552
553 if (label->fdt)
554 printf("\tfdt: %s\n", label->fdt);
Jason Hobbs06283a62011-08-31 10:37:30 -0500555}
556
557/*
558 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
559 * environment variable is defined. Its contents will be executed as U-boot
560 * command. If the label specified an 'append' line, its contents will be
561 * used to overwrite the contents of the 'bootargs' environment variable prior
562 * to running 'localcmd'.
563 *
564 * Returns 1 on success or < 0 on error.
565 */
566static int label_localboot(struct pxe_label *label)
567{
Simon Glassd51004a2012-03-30 21:30:55 +0000568 char *localcmd;
Jason Hobbs06283a62011-08-31 10:37:30 -0500569
570 localcmd = from_env("localcmd");
571
572 if (!localcmd)
573 return -ENOENT;
574
Jason Hobbs06283a62011-08-31 10:37:30 -0500575 if (label->append)
576 setenv("bootargs", label->append);
577
Simon Glassd51004a2012-03-30 21:30:55 +0000578 debug("running: %s\n", localcmd);
Jason Hobbs06283a62011-08-31 10:37:30 -0500579
Simon Glassd51004a2012-03-30 21:30:55 +0000580 return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs06283a62011-08-31 10:37:30 -0500581}
582
583/*
584 * Boot according to the contents of a pxe_label.
585 *
586 * If we can't boot for any reason, we return. A successful boot never
587 * returns.
588 *
589 * The kernel will be stored in the location given by the 'kernel_addr_r'
590 * environment variable.
591 *
592 * If the label specifies an initrd file, it will be stored in the location
593 * given by the 'ramdisk_addr_r' environment variable.
594 *
595 * If the label specifies an 'append' line, its contents will overwrite that
596 * of the 'bootargs' environment variable.
597 */
Rob Herringc27d5602012-04-12 13:55:20 -0500598static int label_boot(struct pxe_label *label)
Jason Hobbs06283a62011-08-31 10:37:30 -0500599{
600 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Rob Herring5af27692012-03-26 13:16:59 -0500601 char initrd_str[22];
Jason Hobbs06283a62011-08-31 10:37:30 -0500602 int bootm_argc = 3;
603
604 label_print(label);
605
606 label->attempted = 1;
607
608 if (label->localboot) {
Rob Herringc27d5602012-04-12 13:55:20 -0500609 if (label->localboot_val >= 0)
610 label_localboot(label);
611 return 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500612 }
613
614 if (label->kernel == NULL) {
615 printf("No kernel given, skipping %s\n",
616 label->name);
Rob Herringc27d5602012-04-12 13:55:20 -0500617 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500618 }
619
620 if (label->initrd) {
621 if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) {
622 printf("Skipping %s for failure retrieving initrd\n",
623 label->name);
Rob Herringc27d5602012-04-12 13:55:20 -0500624 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500625 }
626
Rob Herring5af27692012-03-26 13:16:59 -0500627 bootm_argv[2] = initrd_str;
628 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
629 strcat(bootm_argv[2], ":");
630 strcat(bootm_argv[2], getenv("filesize"));
Jason Hobbs06283a62011-08-31 10:37:30 -0500631 } else {
632 bootm_argv[2] = "-";
633 }
634
635 if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) {
636 printf("Skipping %s for failure retrieving kernel\n",
637 label->name);
Rob Herringc27d5602012-04-12 13:55:20 -0500638 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500639 }
640
641 if (label->append)
642 setenv("bootargs", label->append);
643
644 bootm_argv[1] = getenv("kernel_addr_r");
645
646 /*
Chander Kashyapa6559382012-09-06 19:36:31 +0000647 * fdt usage is optional:
648 * It handles the following scenarios. All scenarios are exclusive
649 *
650 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
651 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
652 * and adjust argc appropriately.
653 *
654 * Scenario 2: If there is an fdt_addr specified, pass it along to
655 * bootm, and adjust argc appropriately.
656 *
657 * Scenario 3: fdt blob is not available.
Jason Hobbs06283a62011-08-31 10:37:30 -0500658 */
Chander Kashyapa6559382012-09-06 19:36:31 +0000659 bootm_argv[3] = getenv("fdt_addr_r");
660
661 /* if fdt label is defined then get fdt from server */
662 if (bootm_argv[3] && label->fdt) {
663 if (get_relfile_envaddr(label->fdt, "fdt_addr_r") < 0) {
664 printf("Skipping %s for failure retrieving fdt\n",
665 label->name);
666 return;
667 }
668 } else
669 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs06283a62011-08-31 10:37:30 -0500670
671 if (bootm_argv[3])
672 bootm_argc = 4;
673
Rob Herring5af27692012-03-26 13:16:59 -0500674#ifdef CONFIG_CMD_BOOTZ
675 do_bootz(NULL, 0, bootm_argc, bootm_argv);
676#else
Jason Hobbs06283a62011-08-31 10:37:30 -0500677 do_bootm(NULL, 0, bootm_argc, bootm_argv);
Rob Herring5af27692012-03-26 13:16:59 -0500678#endif
Rob Herringc27d5602012-04-12 13:55:20 -0500679 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500680}
681
682/*
683 * Tokens for the pxe file parser.
684 */
685enum token_type {
686 T_EOL,
687 T_STRING,
688 T_EOF,
689 T_MENU,
690 T_TITLE,
691 T_TIMEOUT,
692 T_LABEL,
693 T_KERNEL,
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000694 T_LINUX,
Jason Hobbs06283a62011-08-31 10:37:30 -0500695 T_APPEND,
696 T_INITRD,
697 T_LOCALBOOT,
698 T_DEFAULT,
699 T_PROMPT,
700 T_INCLUDE,
Chander Kashyapa6559382012-09-06 19:36:31 +0000701 T_FDT,
Jason Hobbs06283a62011-08-31 10:37:30 -0500702 T_INVALID
703};
704
705/*
706 * A token - given by a value and a type.
707 */
708struct token {
709 char *val;
710 enum token_type type;
711};
712
713/*
714 * Keywords recognized.
715 */
716static const struct token keywords[] = {
717 {"menu", T_MENU},
718 {"title", T_TITLE},
719 {"timeout", T_TIMEOUT},
720 {"default", T_DEFAULT},
721 {"prompt", T_PROMPT},
722 {"label", T_LABEL},
723 {"kernel", T_KERNEL},
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000724 {"linux", T_LINUX},
Jason Hobbs06283a62011-08-31 10:37:30 -0500725 {"localboot", T_LOCALBOOT},
726 {"append", T_APPEND},
727 {"initrd", T_INITRD},
728 {"include", T_INCLUDE},
Chander Kashyapa6559382012-09-06 19:36:31 +0000729 {"fdt", T_FDT},
Jason Hobbs06283a62011-08-31 10:37:30 -0500730 {NULL, T_INVALID}
731};
732
733/*
734 * Since pxe(linux) files don't have a token to identify the start of a
735 * literal, we have to keep track of when we're in a state where a literal is
736 * expected vs when we're in a state a keyword is expected.
737 */
738enum lex_state {
739 L_NORMAL = 0,
740 L_KEYWORD,
741 L_SLITERAL
742};
743
744/*
745 * get_string retrieves a string from *p and stores it as a token in
746 * *t.
747 *
748 * get_string used for scanning both string literals and keywords.
749 *
750 * Characters from *p are copied into t-val until a character equal to
751 * delim is found, or a NUL byte is reached. If delim has the special value of
752 * ' ', any whitespace character will be used as a delimiter.
753 *
754 * If lower is unequal to 0, uppercase characters will be converted to
755 * lowercase in the result. This is useful to make keywords case
756 * insensitive.
757 *
758 * The location of *p is updated to point to the first character after the end
759 * of the token - the ending delimiter.
760 *
761 * On success, the new value of t->val is returned. Memory for t->val is
762 * allocated using malloc and must be free()'d to reclaim it. If insufficient
763 * memory is available, NULL is returned.
764 */
765static char *get_string(char **p, struct token *t, char delim, int lower)
766{
767 char *b, *e;
768 size_t len, i;
769
770 /*
771 * b and e both start at the beginning of the input stream.
772 *
773 * e is incremented until we find the ending delimiter, or a NUL byte
774 * is reached. Then, we take e - b to find the length of the token.
775 */
776 b = e = *p;
777
778 while (*e) {
779 if ((delim == ' ' && isspace(*e)) || delim == *e)
780 break;
781 e++;
782 }
783
784 len = e - b;
785
786 /*
787 * Allocate memory to hold the string, and copy it in, converting
788 * characters to lowercase if lower is != 0.
789 */
790 t->val = malloc(len + 1);
791 if (!t->val)
792 return NULL;
793
794 for (i = 0; i < len; i++, b++) {
795 if (lower)
796 t->val[i] = tolower(*b);
797 else
798 t->val[i] = *b;
799 }
800
801 t->val[len] = '\0';
802
803 /*
804 * Update *p so the caller knows where to continue scanning.
805 */
806 *p = e;
807
808 t->type = T_STRING;
809
810 return t->val;
811}
812
813/*
814 * Populate a keyword token with a type and value.
815 */
816static void get_keyword(struct token *t)
817{
818 int i;
819
820 for (i = 0; keywords[i].val; i++) {
821 if (!strcmp(t->val, keywords[i].val)) {
822 t->type = keywords[i].type;
823 break;
824 }
825 }
826}
827
828/*
829 * Get the next token. We have to keep track of which state we're in to know
830 * if we're looking to get a string literal or a keyword.
831 *
832 * *p is updated to point at the first character after the current token.
833 */
834static void get_token(char **p, struct token *t, enum lex_state state)
835{
836 char *c = *p;
837
838 t->type = T_INVALID;
839
840 /* eat non EOL whitespace */
841 while (isblank(*c))
842 c++;
843
844 /*
845 * eat comments. note that string literals can't begin with #, but
846 * can contain a # after their first character.
847 */
848 if (*c == '#') {
849 while (*c && *c != '\n')
850 c++;
851 }
852
853 if (*c == '\n') {
854 t->type = T_EOL;
855 c++;
856 } else if (*c == '\0') {
857 t->type = T_EOF;
858 c++;
859 } else if (state == L_SLITERAL) {
860 get_string(&c, t, '\n', 0);
861 } else if (state == L_KEYWORD) {
862 /*
863 * when we expect a keyword, we first get the next string
864 * token delimited by whitespace, and then check if it
865 * matches a keyword in our keyword list. if it does, it's
866 * converted to a keyword token of the appropriate type, and
867 * if not, it remains a string token.
868 */
869 get_string(&c, t, ' ', 1);
870 get_keyword(t);
871 }
872
873 *p = c;
874}
875
876/*
877 * Increment *c until we get to the end of the current line, or EOF.
878 */
879static void eol_or_eof(char **c)
880{
881 while (**c && **c != '\n')
882 (*c)++;
883}
884
885/*
886 * All of these parse_* functions share some common behavior.
887 *
888 * They finish with *c pointing after the token they parse, and return 1 on
889 * success, or < 0 on error.
890 */
891
892/*
893 * Parse a string literal and store a pointer it at *dst. String literals
894 * terminate at the end of the line.
895 */
896static int parse_sliteral(char **c, char **dst)
897{
898 struct token t;
899 char *s = *c;
900
901 get_token(c, &t, L_SLITERAL);
902
903 if (t.type != T_STRING) {
904 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
905 return -EINVAL;
906 }
907
908 *dst = t.val;
909
910 return 1;
911}
912
913/*
914 * Parse a base 10 (unsigned) integer and store it at *dst.
915 */
916static int parse_integer(char **c, int *dst)
917{
918 struct token t;
919 char *s = *c;
920 unsigned long temp;
921
922 get_token(c, &t, L_SLITERAL);
923
924 if (t.type != T_STRING) {
925 printf("Expected string: %.*s\n", (int)(*c - s), s);
926 return -EINVAL;
927 }
928
Rob Herringc27d5602012-04-12 13:55:20 -0500929 *dst = simple_strtol(t.val, &temp, 10);
Jason Hobbs06283a62011-08-31 10:37:30 -0500930
931 free(t.val);
932
933 return 1;
934}
935
936static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
937
938/*
939 * Parse an include statement, and retrieve and parse the file it mentions.
940 *
941 * base should point to a location where it's safe to store the file, and
942 * nest_level should indicate how many nested includes have occurred. For this
943 * include, nest_level has already been incremented and doesn't need to be
944 * incremented here.
945 */
946static int handle_include(char **c, char *base,
947 struct pxe_menu *cfg, int nest_level)
948{
949 char *include_path;
950 char *s = *c;
951 int err;
952
953 err = parse_sliteral(c, &include_path);
954
955 if (err < 0) {
956 printf("Expected include path: %.*s\n",
957 (int)(*c - s), s);
958 return err;
959 }
960
961 err = get_pxe_file(include_path, base);
962
963 if (err < 0) {
964 printf("Couldn't retrieve %s\n", include_path);
965 return err;
966 }
967
968 return parse_pxefile_top(base, cfg, nest_level);
969}
970
971/*
972 * Parse lines that begin with 'menu'.
973 *
974 * b and nest are provided to handle the 'menu include' case.
975 *
976 * b should be the address where the file currently being parsed is stored.
977 *
978 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
979 * a file it includes, 3 when parsing a file included by that file, and so on.
980 */
981static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
982{
983 struct token t;
984 char *s = *c;
Heiko Schocher43d4a5e2011-12-12 20:37:17 +0000985 int err = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500986
987 get_token(c, &t, L_KEYWORD);
988
989 switch (t.type) {
990 case T_TITLE:
991 err = parse_sliteral(c, &cfg->title);
992
993 break;
994
995 case T_INCLUDE:
996 err = handle_include(c, b + strlen(b) + 1, cfg,
997 nest_level + 1);
998 break;
999
1000 default:
1001 printf("Ignoring malformed menu command: %.*s\n",
1002 (int)(*c - s), s);
1003 }
1004
1005 if (err < 0)
1006 return err;
1007
1008 eol_or_eof(c);
1009
1010 return 1;
1011}
1012
1013/*
1014 * Handles parsing a 'menu line' when we're parsing a label.
1015 */
1016static int parse_label_menu(char **c, struct pxe_menu *cfg,
1017 struct pxe_label *label)
1018{
1019 struct token t;
1020 char *s;
1021
1022 s = *c;
1023
1024 get_token(c, &t, L_KEYWORD);
1025
1026 switch (t.type) {
1027 case T_DEFAULT:
1028 if (cfg->default_label)
1029 free(cfg->default_label);
1030
1031 cfg->default_label = strdup(label->name);
1032
1033 if (!cfg->default_label)
1034 return -ENOMEM;
1035
1036 break;
Rob Herring7815c4e2012-03-28 05:51:34 +00001037 case T_LABEL:
1038 parse_sliteral(c, &label->menu);
1039 break;
Jason Hobbs06283a62011-08-31 10:37:30 -05001040 default:
1041 printf("Ignoring malformed menu command: %.*s\n",
1042 (int)(*c - s), s);
1043 }
1044
1045 eol_or_eof(c);
1046
1047 return 0;
1048}
1049
1050/*
1051 * Parses a label and adds it to the list of labels for a menu.
1052 *
1053 * A label ends when we either get to the end of a file, or
1054 * get some input we otherwise don't have a handler defined
1055 * for.
1056 *
1057 */
1058static int parse_label(char **c, struct pxe_menu *cfg)
1059{
1060 struct token t;
Rob Herring34bd23e2012-03-28 05:51:37 +00001061 int len;
Jason Hobbs06283a62011-08-31 10:37:30 -05001062 char *s = *c;
1063 struct pxe_label *label;
1064 int err;
Rob Herringc27d5602012-04-12 13:55:20 -05001065 int localboot;
Jason Hobbs06283a62011-08-31 10:37:30 -05001066
1067 label = label_create();
1068 if (!label)
1069 return -ENOMEM;
1070
1071 err = parse_sliteral(c, &label->name);
1072 if (err < 0) {
1073 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1074 label_destroy(label);
1075 return -EINVAL;
1076 }
1077
1078 list_add_tail(&label->list, &cfg->labels);
1079
1080 while (1) {
1081 s = *c;
1082 get_token(c, &t, L_KEYWORD);
1083
1084 err = 0;
1085 switch (t.type) {
1086 case T_MENU:
1087 err = parse_label_menu(c, cfg, label);
1088 break;
1089
1090 case T_KERNEL:
Rob Herringbeb9f6c2012-03-28 05:51:35 +00001091 case T_LINUX:
Jason Hobbs06283a62011-08-31 10:37:30 -05001092 err = parse_sliteral(c, &label->kernel);
1093 break;
1094
1095 case T_APPEND:
1096 err = parse_sliteral(c, &label->append);
Rob Herring34bd23e2012-03-28 05:51:37 +00001097 if (label->initrd)
1098 break;
1099 s = strstr(label->append, "initrd=");
1100 if (!s)
1101 break;
1102 s += 7;
1103 len = (int)(strchr(s, ' ') - s);
1104 label->initrd = malloc(len + 1);
1105 strncpy(label->initrd, s, len);
1106 label->initrd[len] = '\0';
1107
Jason Hobbs06283a62011-08-31 10:37:30 -05001108 break;
1109
1110 case T_INITRD:
Rob Herring34bd23e2012-03-28 05:51:37 +00001111 if (!label->initrd)
1112 err = parse_sliteral(c, &label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -05001113 break;
1114
Chander Kashyapa6559382012-09-06 19:36:31 +00001115 case T_FDT:
1116 if (!label->fdt)
1117 err = parse_sliteral(c, &label->fdt);
1118 break;
1119
Jason Hobbs06283a62011-08-31 10:37:30 -05001120 case T_LOCALBOOT:
Rob Herringc27d5602012-04-12 13:55:20 -05001121 label->localboot = 1;
1122 err = parse_integer(c, &label->localboot_val);
Jason Hobbs06283a62011-08-31 10:37:30 -05001123 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
Rob Herringc27d5602012-04-12 13:55:20 -05001381 if (err == 1) {
1382 err = label_boot(choice);
1383 if (!err)
1384 return;
1385 }
Jason Hobbs6f40f272011-11-07 03:07:15 +00001386 else if (err != -ENOENT)
1387 return;
Jason Hobbs06283a62011-08-31 10:37:30 -05001388
1389 boot_unattempted_labels(cfg);
1390}
1391
1392/*
1393 * Boots a system using a pxe file
1394 *
1395 * Returns 0 on success, 1 on error.
1396 */
1397static int
1398do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1399{
1400 unsigned long pxefile_addr_r;
1401 struct pxe_menu *cfg;
1402 char *pxefile_addr_str;
1403
Rob Herring669df7e2012-05-25 10:47:39 +00001404 do_getfile = do_get_tftp;
1405
Jason Hobbs06283a62011-08-31 10:37:30 -05001406 if (argc == 1) {
1407 pxefile_addr_str = from_env("pxefile_addr_r");
1408 if (!pxefile_addr_str)
1409 return 1;
1410
1411 } else if (argc == 2) {
1412 pxefile_addr_str = argv[1];
1413 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +00001414 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001415 }
1416
1417 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1418 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1419 return 1;
1420 }
1421
1422 cfg = parse_pxefile((char *)(pxefile_addr_r));
1423
1424 if (cfg == NULL) {
1425 printf("Error parsing config file\n");
1426 return 1;
1427 }
1428
1429 handle_pxe_menu(cfg);
1430
1431 destroy_pxe_menu(cfg);
1432
1433 return 0;
1434}
1435
1436static cmd_tbl_t cmd_pxe_sub[] = {
1437 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1438 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1439};
1440
1441int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1442{
1443 cmd_tbl_t *cp;
1444
1445 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001446 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001447
1448 /* drop initial "pxe" arg */
1449 argc--;
1450 argv++;
1451
1452 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1453
1454 if (cp)
1455 return cp->cmd(cmdtp, flag, argc, argv);
1456
Simon Glass4c12eeb2011-12-10 08:44:01 +00001457 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001458}
1459
1460U_BOOT_CMD(
1461 pxe, 3, 1, do_pxe,
1462 "commands to get and boot from pxe files",
1463 "get - try to retrieve a pxe file using tftp\npxe "
1464 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1465);
Rob Herring669df7e2012-05-25 10:47:39 +00001466
1467/*
1468 * Boots a system using a local disk syslinux/extlinux file
1469 *
1470 * Returns 0 on success, 1 on error.
1471 */
1472int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1473{
1474 unsigned long pxefile_addr_r;
1475 struct pxe_menu *cfg;
1476 char *pxefile_addr_str;
1477 char *filename;
1478 int prompt = 0;
1479
1480 if (strstr(argv[1], "-p")) {
1481 prompt = 1;
1482 argc--;
1483 argv++;
1484 }
1485
1486 if (argc < 4)
1487 return cmd_usage(cmdtp);
1488
1489 if (argc < 5) {
1490 pxefile_addr_str = from_env("pxefile_addr_r");
1491 if (!pxefile_addr_str)
1492 return 1;
1493 } else {
1494 pxefile_addr_str = argv[4];
1495 }
1496
1497 if (argc < 6)
1498 filename = getenv("bootfile");
1499 else {
1500 filename = argv[5];
1501 setenv("bootfile", filename);
1502 }
1503
1504 if (strstr(argv[3], "ext2"))
1505 do_getfile = do_get_ext2;
1506 else if (strstr(argv[3], "fat"))
1507 do_getfile = do_get_fat;
1508 else {
1509 printf("Invalid filesystem: %s\n", argv[3]);
1510 return 1;
1511 }
1512 fs_argv[1] = argv[1];
1513 fs_argv[2] = argv[2];
1514
1515 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1516 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1517 return 1;
1518 }
1519
1520 if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) {
1521 printf("Error reading config file\n");
1522 return 1;
1523 }
1524
1525 cfg = parse_pxefile((char *)(pxefile_addr_r));
1526
1527 if (cfg == NULL) {
1528 printf("Error parsing config file\n");
1529 return 1;
1530 }
1531
1532 if (prompt)
1533 cfg->prompt = 1;
1534
1535 handle_pxe_menu(cfg);
1536
1537 destroy_pxe_menu(cfg);
1538
1539 return 0;
1540}
1541
1542U_BOOT_CMD(
1543 sysboot, 7, 1, do_sysboot,
1544 "command to get and boot from syslinux files",
1545 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1546 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1547 " filesystem on 'dev' on 'interface' to address 'addr'"
1548);