blob: acc6a9b0006b863928a8c4ee42adebaa480a230c [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 {
Rob Herring4b3efae2012-04-14 22:23:21 -0500451 char num[4];
Jason Hobbs06283a62011-08-31 10:37:30 -0500452 char *name;
Rob Herring7815c4e2012-03-28 05:51:34 +0000453 char *menu;
Jason Hobbs06283a62011-08-31 10:37:30 -0500454 char *kernel;
455 char *append;
456 char *initrd;
Chander Kashyapa6559382012-09-06 19:36:31 +0000457 char *fdt;
Jason Hobbs06283a62011-08-31 10:37:30 -0500458 int attempted;
459 int localboot;
Rob Herringc27d5602012-04-12 13:55:20 -0500460 int localboot_val;
Jason Hobbs06283a62011-08-31 10:37:30 -0500461 struct list_head list;
462};
463
464/*
465 * Describes a pxe menu as given via pxe files.
466 *
467 * title - the name of the menu as given by a 'menu title' line.
468 * default_label - the name of the default label, if any.
469 * timeout - time in tenths of a second to wait for a user key-press before
470 * booting the default label.
471 * prompt - if 0, don't prompt for a choice unless the timeout period is
472 * interrupted. If 1, always prompt for a choice regardless of
473 * timeout.
474 * labels - a list of labels defined for the menu.
475 */
476struct pxe_menu {
477 char *title;
478 char *default_label;
479 int timeout;
480 int prompt;
481 struct list_head labels;
482};
483
484/*
485 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
486 * result must be free()'d to reclaim the memory.
487 *
488 * Returns NULL if malloc fails.
489 */
490static struct pxe_label *label_create(void)
491{
492 struct pxe_label *label;
493
494 label = malloc(sizeof(struct pxe_label));
495
496 if (!label)
497 return NULL;
498
499 memset(label, 0, sizeof(struct pxe_label));
500
501 return label;
502}
503
504/*
505 * Free the memory used by a pxe_label, including that used by its name,
506 * kernel, append and initrd members, if they're non NULL.
507 *
508 * So - be sure to only use dynamically allocated memory for the members of
509 * the pxe_label struct, unless you want to clean it up first. These are
510 * currently only created by the pxe file parsing code.
511 */
512static void label_destroy(struct pxe_label *label)
513{
514 if (label->name)
515 free(label->name);
516
517 if (label->kernel)
518 free(label->kernel);
519
520 if (label->append)
521 free(label->append);
522
523 if (label->initrd)
524 free(label->initrd);
525
Chander Kashyapa6559382012-09-06 19:36:31 +0000526 if (label->fdt)
527 free(label->fdt);
528
Jason Hobbs06283a62011-08-31 10:37:30 -0500529 free(label);
530}
531
532/*
533 * Print a label and its string members if they're defined.
534 *
535 * This is passed as a callback to the menu code for displaying each
536 * menu entry.
537 */
538static void label_print(void *data)
539{
540 struct pxe_label *label = data;
Rob Herring4b3efae2012-04-14 22:23:21 -0500541 const char *c = label->menu ? label->menu : label->name;
Jason Hobbs06283a62011-08-31 10:37:30 -0500542
Rob Herring4b3efae2012-04-14 22:23:21 -0500543 printf("%s:\t%s\n", label->num, c);
Jason Hobbs06283a62011-08-31 10:37:30 -0500544}
545
546/*
547 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
548 * environment variable is defined. Its contents will be executed as U-boot
549 * command. If the label specified an 'append' line, its contents will be
550 * used to overwrite the contents of the 'bootargs' environment variable prior
551 * to running 'localcmd'.
552 *
553 * Returns 1 on success or < 0 on error.
554 */
555static int label_localboot(struct pxe_label *label)
556{
Simon Glassd51004a2012-03-30 21:30:55 +0000557 char *localcmd;
Jason Hobbs06283a62011-08-31 10:37:30 -0500558
559 localcmd = from_env("localcmd");
560
561 if (!localcmd)
562 return -ENOENT;
563
Jason Hobbs06283a62011-08-31 10:37:30 -0500564 if (label->append)
565 setenv("bootargs", label->append);
566
Simon Glassd51004a2012-03-30 21:30:55 +0000567 debug("running: %s\n", localcmd);
Jason Hobbs06283a62011-08-31 10:37:30 -0500568
Simon Glassd51004a2012-03-30 21:30:55 +0000569 return run_command_list(localcmd, strlen(localcmd), 0);
Jason Hobbs06283a62011-08-31 10:37:30 -0500570}
571
572/*
573 * Boot according to the contents of a pxe_label.
574 *
575 * If we can't boot for any reason, we return. A successful boot never
576 * returns.
577 *
578 * The kernel will be stored in the location given by the 'kernel_addr_r'
579 * environment variable.
580 *
581 * If the label specifies an initrd file, it will be stored in the location
582 * given by the 'ramdisk_addr_r' environment variable.
583 *
584 * If the label specifies an 'append' line, its contents will overwrite that
585 * of the 'bootargs' environment variable.
586 */
Rob Herringc27d5602012-04-12 13:55:20 -0500587static int label_boot(struct pxe_label *label)
Jason Hobbs06283a62011-08-31 10:37:30 -0500588{
589 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
Rob Herring5af27692012-03-26 13:16:59 -0500590 char initrd_str[22];
Jason Hobbs06283a62011-08-31 10:37:30 -0500591 int bootm_argc = 3;
592
593 label_print(label);
594
595 label->attempted = 1;
596
597 if (label->localboot) {
Rob Herringc27d5602012-04-12 13:55:20 -0500598 if (label->localboot_val >= 0)
599 label_localboot(label);
600 return 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500601 }
602
603 if (label->kernel == NULL) {
604 printf("No kernel given, skipping %s\n",
605 label->name);
Rob Herringc27d5602012-04-12 13:55:20 -0500606 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500607 }
608
609 if (label->initrd) {
610 if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) {
611 printf("Skipping %s for failure retrieving initrd\n",
612 label->name);
Rob Herringc27d5602012-04-12 13:55:20 -0500613 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500614 }
615
Rob Herring5af27692012-03-26 13:16:59 -0500616 bootm_argv[2] = initrd_str;
617 strcpy(bootm_argv[2], getenv("ramdisk_addr_r"));
618 strcat(bootm_argv[2], ":");
619 strcat(bootm_argv[2], getenv("filesize"));
Jason Hobbs06283a62011-08-31 10:37:30 -0500620 } else {
621 bootm_argv[2] = "-";
622 }
623
624 if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) {
625 printf("Skipping %s for failure retrieving kernel\n",
626 label->name);
Rob Herringc27d5602012-04-12 13:55:20 -0500627 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500628 }
629
Rob Herring4b3efae2012-04-14 22:23:21 -0500630 if (label->append) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500631 setenv("bootargs", label->append);
Rob Herring4b3efae2012-04-14 22:23:21 -0500632 printf("append: %s\n", label->append);
633 }
Jason Hobbs06283a62011-08-31 10:37:30 -0500634
635 bootm_argv[1] = getenv("kernel_addr_r");
636
637 /*
Chander Kashyapa6559382012-09-06 19:36:31 +0000638 * fdt usage is optional:
639 * It handles the following scenarios. All scenarios are exclusive
640 *
641 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
642 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
643 * and adjust argc appropriately.
644 *
645 * Scenario 2: If there is an fdt_addr specified, pass it along to
646 * bootm, and adjust argc appropriately.
647 *
648 * Scenario 3: fdt blob is not available.
Jason Hobbs06283a62011-08-31 10:37:30 -0500649 */
Chander Kashyapa6559382012-09-06 19:36:31 +0000650 bootm_argv[3] = getenv("fdt_addr_r");
651
652 /* if fdt label is defined then get fdt from server */
653 if (bootm_argv[3] && label->fdt) {
654 if (get_relfile_envaddr(label->fdt, "fdt_addr_r") < 0) {
655 printf("Skipping %s for failure retrieving fdt\n",
656 label->name);
657 return;
658 }
659 } else
660 bootm_argv[3] = getenv("fdt_addr");
Jason Hobbs06283a62011-08-31 10:37:30 -0500661
662 if (bootm_argv[3])
663 bootm_argc = 4;
664
Rob Herring5af27692012-03-26 13:16:59 -0500665#ifdef CONFIG_CMD_BOOTZ
666 do_bootz(NULL, 0, bootm_argc, bootm_argv);
667#else
Jason Hobbs06283a62011-08-31 10:37:30 -0500668 do_bootm(NULL, 0, bootm_argc, bootm_argv);
Rob Herring5af27692012-03-26 13:16:59 -0500669#endif
Rob Herringc27d5602012-04-12 13:55:20 -0500670 return 1;
Jason Hobbs06283a62011-08-31 10:37:30 -0500671}
672
673/*
674 * Tokens for the pxe file parser.
675 */
676enum token_type {
677 T_EOL,
678 T_STRING,
679 T_EOF,
680 T_MENU,
681 T_TITLE,
682 T_TIMEOUT,
683 T_LABEL,
684 T_KERNEL,
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000685 T_LINUX,
Jason Hobbs06283a62011-08-31 10:37:30 -0500686 T_APPEND,
687 T_INITRD,
688 T_LOCALBOOT,
689 T_DEFAULT,
690 T_PROMPT,
691 T_INCLUDE,
Chander Kashyapa6559382012-09-06 19:36:31 +0000692 T_FDT,
Jason Hobbs06283a62011-08-31 10:37:30 -0500693 T_INVALID
694};
695
696/*
697 * A token - given by a value and a type.
698 */
699struct token {
700 char *val;
701 enum token_type type;
702};
703
704/*
705 * Keywords recognized.
706 */
707static const struct token keywords[] = {
708 {"menu", T_MENU},
709 {"title", T_TITLE},
710 {"timeout", T_TIMEOUT},
711 {"default", T_DEFAULT},
712 {"prompt", T_PROMPT},
713 {"label", T_LABEL},
714 {"kernel", T_KERNEL},
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000715 {"linux", T_LINUX},
Jason Hobbs06283a62011-08-31 10:37:30 -0500716 {"localboot", T_LOCALBOOT},
717 {"append", T_APPEND},
718 {"initrd", T_INITRD},
719 {"include", T_INCLUDE},
Chander Kashyapa6559382012-09-06 19:36:31 +0000720 {"fdt", T_FDT},
Jason Hobbs06283a62011-08-31 10:37:30 -0500721 {NULL, T_INVALID}
722};
723
724/*
725 * Since pxe(linux) files don't have a token to identify the start of a
726 * literal, we have to keep track of when we're in a state where a literal is
727 * expected vs when we're in a state a keyword is expected.
728 */
729enum lex_state {
730 L_NORMAL = 0,
731 L_KEYWORD,
732 L_SLITERAL
733};
734
735/*
736 * get_string retrieves a string from *p and stores it as a token in
737 * *t.
738 *
739 * get_string used for scanning both string literals and keywords.
740 *
741 * Characters from *p are copied into t-val until a character equal to
742 * delim is found, or a NUL byte is reached. If delim has the special value of
743 * ' ', any whitespace character will be used as a delimiter.
744 *
745 * If lower is unequal to 0, uppercase characters will be converted to
746 * lowercase in the result. This is useful to make keywords case
747 * insensitive.
748 *
749 * The location of *p is updated to point to the first character after the end
750 * of the token - the ending delimiter.
751 *
752 * On success, the new value of t->val is returned. Memory for t->val is
753 * allocated using malloc and must be free()'d to reclaim it. If insufficient
754 * memory is available, NULL is returned.
755 */
756static char *get_string(char **p, struct token *t, char delim, int lower)
757{
758 char *b, *e;
759 size_t len, i;
760
761 /*
762 * b and e both start at the beginning of the input stream.
763 *
764 * e is incremented until we find the ending delimiter, or a NUL byte
765 * is reached. Then, we take e - b to find the length of the token.
766 */
767 b = e = *p;
768
769 while (*e) {
770 if ((delim == ' ' && isspace(*e)) || delim == *e)
771 break;
772 e++;
773 }
774
775 len = e - b;
776
777 /*
778 * Allocate memory to hold the string, and copy it in, converting
779 * characters to lowercase if lower is != 0.
780 */
781 t->val = malloc(len + 1);
782 if (!t->val)
783 return NULL;
784
785 for (i = 0; i < len; i++, b++) {
786 if (lower)
787 t->val[i] = tolower(*b);
788 else
789 t->val[i] = *b;
790 }
791
792 t->val[len] = '\0';
793
794 /*
795 * Update *p so the caller knows where to continue scanning.
796 */
797 *p = e;
798
799 t->type = T_STRING;
800
801 return t->val;
802}
803
804/*
805 * Populate a keyword token with a type and value.
806 */
807static void get_keyword(struct token *t)
808{
809 int i;
810
811 for (i = 0; keywords[i].val; i++) {
812 if (!strcmp(t->val, keywords[i].val)) {
813 t->type = keywords[i].type;
814 break;
815 }
816 }
817}
818
819/*
820 * Get the next token. We have to keep track of which state we're in to know
821 * if we're looking to get a string literal or a keyword.
822 *
823 * *p is updated to point at the first character after the current token.
824 */
825static void get_token(char **p, struct token *t, enum lex_state state)
826{
827 char *c = *p;
828
829 t->type = T_INVALID;
830
831 /* eat non EOL whitespace */
832 while (isblank(*c))
833 c++;
834
835 /*
836 * eat comments. note that string literals can't begin with #, but
837 * can contain a # after their first character.
838 */
839 if (*c == '#') {
840 while (*c && *c != '\n')
841 c++;
842 }
843
844 if (*c == '\n') {
845 t->type = T_EOL;
846 c++;
847 } else if (*c == '\0') {
848 t->type = T_EOF;
849 c++;
850 } else if (state == L_SLITERAL) {
851 get_string(&c, t, '\n', 0);
852 } else if (state == L_KEYWORD) {
853 /*
854 * when we expect a keyword, we first get the next string
855 * token delimited by whitespace, and then check if it
856 * matches a keyword in our keyword list. if it does, it's
857 * converted to a keyword token of the appropriate type, and
858 * if not, it remains a string token.
859 */
860 get_string(&c, t, ' ', 1);
861 get_keyword(t);
862 }
863
864 *p = c;
865}
866
867/*
868 * Increment *c until we get to the end of the current line, or EOF.
869 */
870static void eol_or_eof(char **c)
871{
872 while (**c && **c != '\n')
873 (*c)++;
874}
875
876/*
877 * All of these parse_* functions share some common behavior.
878 *
879 * They finish with *c pointing after the token they parse, and return 1 on
880 * success, or < 0 on error.
881 */
882
883/*
884 * Parse a string literal and store a pointer it at *dst. String literals
885 * terminate at the end of the line.
886 */
887static int parse_sliteral(char **c, char **dst)
888{
889 struct token t;
890 char *s = *c;
891
892 get_token(c, &t, L_SLITERAL);
893
894 if (t.type != T_STRING) {
895 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
896 return -EINVAL;
897 }
898
899 *dst = t.val;
900
901 return 1;
902}
903
904/*
905 * Parse a base 10 (unsigned) integer and store it at *dst.
906 */
907static int parse_integer(char **c, int *dst)
908{
909 struct token t;
910 char *s = *c;
911 unsigned long temp;
912
913 get_token(c, &t, L_SLITERAL);
914
915 if (t.type != T_STRING) {
916 printf("Expected string: %.*s\n", (int)(*c - s), s);
917 return -EINVAL;
918 }
919
Rob Herringc27d5602012-04-12 13:55:20 -0500920 *dst = simple_strtol(t.val, &temp, 10);
Jason Hobbs06283a62011-08-31 10:37:30 -0500921
922 free(t.val);
923
924 return 1;
925}
926
927static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
928
929/*
930 * Parse an include statement, and retrieve and parse the file it mentions.
931 *
932 * base should point to a location where it's safe to store the file, and
933 * nest_level should indicate how many nested includes have occurred. For this
934 * include, nest_level has already been incremented and doesn't need to be
935 * incremented here.
936 */
937static int handle_include(char **c, char *base,
938 struct pxe_menu *cfg, int nest_level)
939{
940 char *include_path;
941 char *s = *c;
942 int err;
943
944 err = parse_sliteral(c, &include_path);
945
946 if (err < 0) {
947 printf("Expected include path: %.*s\n",
948 (int)(*c - s), s);
949 return err;
950 }
951
952 err = get_pxe_file(include_path, base);
953
954 if (err < 0) {
955 printf("Couldn't retrieve %s\n", include_path);
956 return err;
957 }
958
959 return parse_pxefile_top(base, cfg, nest_level);
960}
961
962/*
963 * Parse lines that begin with 'menu'.
964 *
965 * b and nest are provided to handle the 'menu include' case.
966 *
967 * b should be the address where the file currently being parsed is stored.
968 *
969 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
970 * a file it includes, 3 when parsing a file included by that file, and so on.
971 */
972static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
973{
974 struct token t;
975 char *s = *c;
Heiko Schocher43d4a5e2011-12-12 20:37:17 +0000976 int err = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500977
978 get_token(c, &t, L_KEYWORD);
979
980 switch (t.type) {
981 case T_TITLE:
982 err = parse_sliteral(c, &cfg->title);
983
984 break;
985
986 case T_INCLUDE:
987 err = handle_include(c, b + strlen(b) + 1, cfg,
988 nest_level + 1);
989 break;
990
991 default:
992 printf("Ignoring malformed menu command: %.*s\n",
993 (int)(*c - s), s);
994 }
995
996 if (err < 0)
997 return err;
998
999 eol_or_eof(c);
1000
1001 return 1;
1002}
1003
1004/*
1005 * Handles parsing a 'menu line' when we're parsing a label.
1006 */
1007static int parse_label_menu(char **c, struct pxe_menu *cfg,
1008 struct pxe_label *label)
1009{
1010 struct token t;
1011 char *s;
1012
1013 s = *c;
1014
1015 get_token(c, &t, L_KEYWORD);
1016
1017 switch (t.type) {
1018 case T_DEFAULT:
1019 if (cfg->default_label)
1020 free(cfg->default_label);
1021
1022 cfg->default_label = strdup(label->name);
1023
1024 if (!cfg->default_label)
1025 return -ENOMEM;
1026
1027 break;
Rob Herring7815c4e2012-03-28 05:51:34 +00001028 case T_LABEL:
1029 parse_sliteral(c, &label->menu);
1030 break;
Jason Hobbs06283a62011-08-31 10:37:30 -05001031 default:
1032 printf("Ignoring malformed menu command: %.*s\n",
1033 (int)(*c - s), s);
1034 }
1035
1036 eol_or_eof(c);
1037
1038 return 0;
1039}
1040
1041/*
1042 * Parses a label and adds it to the list of labels for a menu.
1043 *
1044 * A label ends when we either get to the end of a file, or
1045 * get some input we otherwise don't have a handler defined
1046 * for.
1047 *
1048 */
1049static int parse_label(char **c, struct pxe_menu *cfg)
1050{
1051 struct token t;
Rob Herring34bd23e2012-03-28 05:51:37 +00001052 int len;
Jason Hobbs06283a62011-08-31 10:37:30 -05001053 char *s = *c;
1054 struct pxe_label *label;
1055 int err;
Rob Herringc27d5602012-04-12 13:55:20 -05001056 int localboot;
Jason Hobbs06283a62011-08-31 10:37:30 -05001057
1058 label = label_create();
1059 if (!label)
1060 return -ENOMEM;
1061
1062 err = parse_sliteral(c, &label->name);
1063 if (err < 0) {
1064 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1065 label_destroy(label);
1066 return -EINVAL;
1067 }
1068
1069 list_add_tail(&label->list, &cfg->labels);
1070
1071 while (1) {
1072 s = *c;
1073 get_token(c, &t, L_KEYWORD);
1074
1075 err = 0;
1076 switch (t.type) {
1077 case T_MENU:
1078 err = parse_label_menu(c, cfg, label);
1079 break;
1080
1081 case T_KERNEL:
Rob Herringbeb9f6c2012-03-28 05:51:35 +00001082 case T_LINUX:
Jason Hobbs06283a62011-08-31 10:37:30 -05001083 err = parse_sliteral(c, &label->kernel);
1084 break;
1085
1086 case T_APPEND:
1087 err = parse_sliteral(c, &label->append);
Rob Herring34bd23e2012-03-28 05:51:37 +00001088 if (label->initrd)
1089 break;
1090 s = strstr(label->append, "initrd=");
1091 if (!s)
1092 break;
1093 s += 7;
1094 len = (int)(strchr(s, ' ') - s);
1095 label->initrd = malloc(len + 1);
1096 strncpy(label->initrd, s, len);
1097 label->initrd[len] = '\0';
1098
Jason Hobbs06283a62011-08-31 10:37:30 -05001099 break;
1100
1101 case T_INITRD:
Rob Herring34bd23e2012-03-28 05:51:37 +00001102 if (!label->initrd)
1103 err = parse_sliteral(c, &label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -05001104 break;
1105
Chander Kashyapa6559382012-09-06 19:36:31 +00001106 case T_FDT:
1107 if (!label->fdt)
1108 err = parse_sliteral(c, &label->fdt);
1109 break;
1110
Jason Hobbs06283a62011-08-31 10:37:30 -05001111 case T_LOCALBOOT:
Rob Herringc27d5602012-04-12 13:55:20 -05001112 label->localboot = 1;
1113 err = parse_integer(c, &label->localboot_val);
Jason Hobbs06283a62011-08-31 10:37:30 -05001114 break;
1115
1116 case T_EOL:
1117 break;
1118
1119 default:
1120 /*
1121 * put the token back! we don't want it - it's the end
1122 * of a label and whatever token this is, it's
1123 * something for the menu level context to handle.
1124 */
1125 *c = s;
1126 return 1;
1127 }
1128
1129 if (err < 0)
1130 return err;
1131 }
1132}
1133
1134/*
1135 * This 16 comes from the limit pxelinux imposes on nested includes.
1136 *
1137 * There is no reason at all we couldn't do more, but some limit helps prevent
1138 * infinite (until crash occurs) recursion if a file tries to include itself.
1139 */
1140#define MAX_NEST_LEVEL 16
1141
1142/*
1143 * Entry point for parsing a menu file. nest_level indicates how many times
1144 * we've nested in includes. It will be 1 for the top level menu file.
1145 *
1146 * Returns 1 on success, < 0 on error.
1147 */
1148static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level)
1149{
1150 struct token t;
1151 char *s, *b, *label_name;
1152 int err;
1153
1154 b = p;
1155
1156 if (nest_level > MAX_NEST_LEVEL) {
1157 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1158 return -EMLINK;
1159 }
1160
1161 while (1) {
1162 s = p;
1163
1164 get_token(&p, &t, L_KEYWORD);
1165
1166 err = 0;
1167 switch (t.type) {
1168 case T_MENU:
Rob Herring2619f512012-04-12 13:33:43 -05001169 cfg->prompt = 1;
Jason Hobbs06283a62011-08-31 10:37:30 -05001170 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:
Rob Herring2619f512012-04-12 13:33:43 -05001199 eol_or_eof(&p);
Jason Hobbs06283a62011-08-31 10:37:30 -05001200 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;
Rob Herring4b3efae2012-04-14 22:23:21 -05001282 int i = 1;
Rob Herring0731b7b2012-05-02 18:52:49 -05001283 char *default_num = NULL;
Jason Hobbs06283a62011-08-31 10:37:30 -05001284
1285 /*
1286 * Create a menu and add items for all the labels.
1287 */
1288 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print);
1289
1290 if (!m)
1291 return NULL;
1292
1293 list_for_each(pos, &cfg->labels) {
1294 label = list_entry(pos, struct pxe_label, list);
1295
Rob Herring4b3efae2012-04-14 22:23:21 -05001296 sprintf(label->num, "%d", i++);
1297 if (menu_item_add(m, label->num, label) != 1) {
Jason Hobbs06283a62011-08-31 10:37:30 -05001298 menu_destroy(m);
1299 return NULL;
1300 }
Rob Herring0731b7b2012-05-02 18:52:49 -05001301 if (cfg->default_label &&
1302 (strcmp(label->name, cfg->default_label) == 0))
1303 default_num = label->num;
1304
Jason Hobbs06283a62011-08-31 10:37:30 -05001305 }
1306
1307 /*
1308 * After we've created items for each label in the menu, set the
1309 * menu's default label if one was specified.
1310 */
Rob Herring0731b7b2012-05-02 18:52:49 -05001311 if (default_num) {
1312 err = menu_default_set(m, default_num);
Jason Hobbs06283a62011-08-31 10:37:30 -05001313 if (err != 1) {
1314 if (err != -ENOENT) {
1315 menu_destroy(m);
1316 return NULL;
1317 }
1318
1319 printf("Missing default: %s\n", cfg->default_label);
1320 }
1321 }
1322
1323 return m;
1324}
1325
1326/*
1327 * Try to boot any labels we have yet to attempt to boot.
1328 */
1329static void boot_unattempted_labels(struct pxe_menu *cfg)
1330{
1331 struct list_head *pos;
1332 struct pxe_label *label;
1333
1334 list_for_each(pos, &cfg->labels) {
1335 label = list_entry(pos, struct pxe_label, list);
1336
1337 if (!label->attempted)
1338 label_boot(label);
1339 }
1340}
1341
1342/*
1343 * Boot the system as prescribed by a pxe_menu.
1344 *
1345 * Use the menu system to either get the user's choice or the default, based
1346 * on config or user input. If there is no default or user's choice,
1347 * attempted to boot labels in the order they were given in pxe files.
1348 * If the default or user's choice fails to boot, attempt to boot other
1349 * labels in the order they were given in pxe files.
1350 *
1351 * If this function returns, there weren't any labels that successfully
1352 * booted, or the user interrupted the menu selection via ctrl+c.
1353 */
1354static void handle_pxe_menu(struct pxe_menu *cfg)
1355{
1356 void *choice;
1357 struct menu *m;
1358 int err;
1359
1360 m = pxe_menu_to_menu(cfg);
1361 if (!m)
1362 return;
1363
1364 err = menu_get_choice(m, &choice);
1365
1366 menu_destroy(m);
1367
Jason Hobbs6f40f272011-11-07 03:07:15 +00001368 /*
1369 * err == 1 means we got a choice back from menu_get_choice.
1370 *
1371 * err == -ENOENT if the menu was setup to select the default but no
1372 * default was set. in that case, we should continue trying to boot
1373 * labels that haven't been attempted yet.
1374 *
1375 * otherwise, the user interrupted or there was some other error and
1376 * we give up.
1377 */
Jason Hobbs06283a62011-08-31 10:37:30 -05001378
Rob Herringc27d5602012-04-12 13:55:20 -05001379 if (err == 1) {
1380 err = label_boot(choice);
1381 if (!err)
1382 return;
1383 }
Jason Hobbs6f40f272011-11-07 03:07:15 +00001384 else if (err != -ENOENT)
1385 return;
Jason Hobbs06283a62011-08-31 10:37:30 -05001386
1387 boot_unattempted_labels(cfg);
1388}
1389
1390/*
1391 * Boots a system using a pxe file
1392 *
1393 * Returns 0 on success, 1 on error.
1394 */
1395static int
1396do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1397{
1398 unsigned long pxefile_addr_r;
1399 struct pxe_menu *cfg;
1400 char *pxefile_addr_str;
1401
Rob Herring669df7e2012-05-25 10:47:39 +00001402 do_getfile = do_get_tftp;
1403
Jason Hobbs06283a62011-08-31 10:37:30 -05001404 if (argc == 1) {
1405 pxefile_addr_str = from_env("pxefile_addr_r");
1406 if (!pxefile_addr_str)
1407 return 1;
1408
1409 } else if (argc == 2) {
1410 pxefile_addr_str = argv[1];
1411 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +00001412 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001413 }
1414
1415 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1416 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1417 return 1;
1418 }
1419
1420 cfg = parse_pxefile((char *)(pxefile_addr_r));
1421
1422 if (cfg == NULL) {
1423 printf("Error parsing config file\n");
1424 return 1;
1425 }
1426
1427 handle_pxe_menu(cfg);
1428
1429 destroy_pxe_menu(cfg);
1430
1431 return 0;
1432}
1433
1434static cmd_tbl_t cmd_pxe_sub[] = {
1435 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1436 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1437};
1438
1439int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1440{
1441 cmd_tbl_t *cp;
1442
1443 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001444 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001445
1446 /* drop initial "pxe" arg */
1447 argc--;
1448 argv++;
1449
1450 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1451
1452 if (cp)
1453 return cp->cmd(cmdtp, flag, argc, argv);
1454
Simon Glass4c12eeb2011-12-10 08:44:01 +00001455 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001456}
1457
1458U_BOOT_CMD(
1459 pxe, 3, 1, do_pxe,
1460 "commands to get and boot from pxe files",
1461 "get - try to retrieve a pxe file using tftp\npxe "
1462 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1463);
Rob Herring669df7e2012-05-25 10:47:39 +00001464
1465/*
1466 * Boots a system using a local disk syslinux/extlinux file
1467 *
1468 * Returns 0 on success, 1 on error.
1469 */
1470int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1471{
1472 unsigned long pxefile_addr_r;
1473 struct pxe_menu *cfg;
1474 char *pxefile_addr_str;
1475 char *filename;
1476 int prompt = 0;
1477
1478 if (strstr(argv[1], "-p")) {
1479 prompt = 1;
1480 argc--;
1481 argv++;
1482 }
1483
1484 if (argc < 4)
1485 return cmd_usage(cmdtp);
1486
1487 if (argc < 5) {
1488 pxefile_addr_str = from_env("pxefile_addr_r");
1489 if (!pxefile_addr_str)
1490 return 1;
1491 } else {
1492 pxefile_addr_str = argv[4];
1493 }
1494
1495 if (argc < 6)
1496 filename = getenv("bootfile");
1497 else {
1498 filename = argv[5];
1499 setenv("bootfile", filename);
1500 }
1501
1502 if (strstr(argv[3], "ext2"))
1503 do_getfile = do_get_ext2;
1504 else if (strstr(argv[3], "fat"))
1505 do_getfile = do_get_fat;
1506 else {
1507 printf("Invalid filesystem: %s\n", argv[3]);
1508 return 1;
1509 }
1510 fs_argv[1] = argv[1];
1511 fs_argv[2] = argv[2];
1512
1513 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1514 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1515 return 1;
1516 }
1517
1518 if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) {
1519 printf("Error reading config file\n");
1520 return 1;
1521 }
1522
1523 cfg = parse_pxefile((char *)(pxefile_addr_r));
1524
1525 if (cfg == NULL) {
1526 printf("Error parsing config file\n");
1527 return 1;
1528 }
1529
1530 if (prompt)
1531 cfg->prompt = 1;
1532
1533 handle_pxe_menu(cfg);
1534
1535 destroy_pxe_menu(cfg);
1536
1537 return 0;
1538}
1539
1540U_BOOT_CMD(
1541 sysboot, 7, 1, do_sysboot,
1542 "command to get and boot from syslinux files",
1543 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1544 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1545 " filesystem on 'dev' on 'interface' to address 'addr'"
1546);