blob: bf32fb492e6b7498d0a148f7f226b90b6788f4c5 [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 Rigby03e97da2011-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;
456 int attempted;
457 int localboot;
458 struct list_head list;
459};
460
461/*
462 * Describes a pxe menu as given via pxe files.
463 *
464 * title - the name of the menu as given by a 'menu title' line.
465 * default_label - the name of the default label, if any.
466 * timeout - time in tenths of a second to wait for a user key-press before
467 * booting the default label.
468 * prompt - if 0, don't prompt for a choice unless the timeout period is
469 * interrupted. If 1, always prompt for a choice regardless of
470 * timeout.
471 * labels - a list of labels defined for the menu.
472 */
473struct pxe_menu {
474 char *title;
475 char *default_label;
476 int timeout;
477 int prompt;
478 struct list_head labels;
479};
480
481/*
482 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
483 * result must be free()'d to reclaim the memory.
484 *
485 * Returns NULL if malloc fails.
486 */
487static struct pxe_label *label_create(void)
488{
489 struct pxe_label *label;
490
491 label = malloc(sizeof(struct pxe_label));
492
493 if (!label)
494 return NULL;
495
496 memset(label, 0, sizeof(struct pxe_label));
497
498 return label;
499}
500
501/*
502 * Free the memory used by a pxe_label, including that used by its name,
503 * kernel, append and initrd members, if they're non NULL.
504 *
505 * So - be sure to only use dynamically allocated memory for the members of
506 * the pxe_label struct, unless you want to clean it up first. These are
507 * currently only created by the pxe file parsing code.
508 */
509static void label_destroy(struct pxe_label *label)
510{
511 if (label->name)
512 free(label->name);
513
514 if (label->kernel)
515 free(label->kernel);
516
517 if (label->append)
518 free(label->append);
519
520 if (label->initrd)
521 free(label->initrd);
522
523 free(label);
524}
525
526/*
527 * Print a label and its string members if they're defined.
528 *
529 * This is passed as a callback to the menu code for displaying each
530 * menu entry.
531 */
532static void label_print(void *data)
533{
534 struct pxe_label *label = data;
Rob Herring7815c4e2012-03-28 05:51:34 +0000535 const char *c = label->menu ? label->menu : label->kernel;
Jason Hobbs06283a62011-08-31 10:37:30 -0500536
Rob Herring7815c4e2012-03-28 05:51:34 +0000537 printf("%s:\t%s\n", label->name, c);
Jason Hobbs06283a62011-08-31 10:37:30 -0500538
539 if (label->kernel)
Rob Herring7815c4e2012-03-28 05:51:34 +0000540 printf("\t\tkernel: %s\n", label->kernel);
Jason Hobbs06283a62011-08-31 10:37:30 -0500541
542 if (label->append)
Rob Herring7815c4e2012-03-28 05:51:34 +0000543 printf("\t\tappend: %s\n", label->append);
Jason Hobbs06283a62011-08-31 10:37:30 -0500544
545 if (label->initrd)
Rob Herring7815c4e2012-03-28 05:51:34 +0000546 printf("\t\tinitrd: %s\n", label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -0500547}
548
549/*
550 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
551 * environment variable is defined. Its contents will be executed as U-boot
552 * command. If the label specified an 'append' line, its contents will be
553 * used to overwrite the contents of the 'bootargs' environment variable prior
554 * to running 'localcmd'.
555 *
556 * Returns 1 on success or < 0 on error.
557 */
558static int label_localboot(struct pxe_label *label)
559{
560 char *localcmd, *dupcmd;
561 int ret;
562
563 localcmd = from_env("localcmd");
564
565 if (!localcmd)
566 return -ENOENT;
567
568 /*
569 * dup the command to avoid any issues with the version of it existing
570 * in the environment changing during the execution of the command.
571 */
572 dupcmd = strdup(localcmd);
573
574 if (!dupcmd)
575 return -ENOMEM;
576
577 if (label->append)
578 setenv("bootargs", label->append);
579
580 printf("running: %s\n", dupcmd);
581
Simon Glass009dde12012-02-14 19:59:20 +0000582 ret = run_command(dupcmd, 0);
Jason Hobbs06283a62011-08-31 10:37:30 -0500583
584 free(dupcmd);
585
586 return ret;
587}
588
589/*
590 * Boot according to the contents of a pxe_label.
591 *
592 * If we can't boot for any reason, we return. A successful boot never
593 * returns.
594 *
595 * The kernel will be stored in the location given by the 'kernel_addr_r'
596 * environment variable.
597 *
598 * If the label specifies an initrd file, it will be stored in the location
599 * given by the 'ramdisk_addr_r' environment variable.
600 *
601 * If the label specifies an 'append' line, its contents will overwrite that
602 * of the 'bootargs' environment variable.
603 */
604static void label_boot(struct pxe_label *label)
605{
606 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
607 int bootm_argc = 3;
608
609 label_print(label);
610
611 label->attempted = 1;
612
613 if (label->localboot) {
614 label_localboot(label);
615 return;
616 }
617
618 if (label->kernel == NULL) {
619 printf("No kernel given, skipping %s\n",
620 label->name);
621 return;
622 }
623
624 if (label->initrd) {
625 if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) {
626 printf("Skipping %s for failure retrieving initrd\n",
627 label->name);
628 return;
629 }
630
631 bootm_argv[2] = getenv("ramdisk_addr_r");
632 } else {
633 bootm_argv[2] = "-";
634 }
635
636 if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) {
637 printf("Skipping %s for failure retrieving kernel\n",
638 label->name);
639 return;
640 }
641
642 if (label->append)
643 setenv("bootargs", label->append);
644
645 bootm_argv[1] = getenv("kernel_addr_r");
646
647 /*
648 * fdt usage is optional. If there is an fdt_addr specified, we will
649 * pass it along to bootm, and adjust argc appropriately.
650 */
651 bootm_argv[3] = getenv("fdt_addr");
652
653 if (bootm_argv[3])
654 bootm_argc = 4;
655
656 do_bootm(NULL, 0, bootm_argc, bootm_argv);
657}
658
659/*
660 * Tokens for the pxe file parser.
661 */
662enum token_type {
663 T_EOL,
664 T_STRING,
665 T_EOF,
666 T_MENU,
667 T_TITLE,
668 T_TIMEOUT,
669 T_LABEL,
670 T_KERNEL,
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000671 T_LINUX,
Jason Hobbs06283a62011-08-31 10:37:30 -0500672 T_APPEND,
673 T_INITRD,
674 T_LOCALBOOT,
675 T_DEFAULT,
676 T_PROMPT,
677 T_INCLUDE,
678 T_INVALID
679};
680
681/*
682 * A token - given by a value and a type.
683 */
684struct token {
685 char *val;
686 enum token_type type;
687};
688
689/*
690 * Keywords recognized.
691 */
692static const struct token keywords[] = {
693 {"menu", T_MENU},
694 {"title", T_TITLE},
695 {"timeout", T_TIMEOUT},
696 {"default", T_DEFAULT},
697 {"prompt", T_PROMPT},
698 {"label", T_LABEL},
699 {"kernel", T_KERNEL},
Rob Herringbeb9f6c2012-03-28 05:51:35 +0000700 {"linux", T_LINUX},
Jason Hobbs06283a62011-08-31 10:37:30 -0500701 {"localboot", T_LOCALBOOT},
702 {"append", T_APPEND},
703 {"initrd", T_INITRD},
704 {"include", T_INCLUDE},
705 {NULL, T_INVALID}
706};
707
708/*
709 * Since pxe(linux) files don't have a token to identify the start of a
710 * literal, we have to keep track of when we're in a state where a literal is
711 * expected vs when we're in a state a keyword is expected.
712 */
713enum lex_state {
714 L_NORMAL = 0,
715 L_KEYWORD,
716 L_SLITERAL
717};
718
719/*
720 * get_string retrieves a string from *p and stores it as a token in
721 * *t.
722 *
723 * get_string used for scanning both string literals and keywords.
724 *
725 * Characters from *p are copied into t-val until a character equal to
726 * delim is found, or a NUL byte is reached. If delim has the special value of
727 * ' ', any whitespace character will be used as a delimiter.
728 *
729 * If lower is unequal to 0, uppercase characters will be converted to
730 * lowercase in the result. This is useful to make keywords case
731 * insensitive.
732 *
733 * The location of *p is updated to point to the first character after the end
734 * of the token - the ending delimiter.
735 *
736 * On success, the new value of t->val is returned. Memory for t->val is
737 * allocated using malloc and must be free()'d to reclaim it. If insufficient
738 * memory is available, NULL is returned.
739 */
740static char *get_string(char **p, struct token *t, char delim, int lower)
741{
742 char *b, *e;
743 size_t len, i;
744
745 /*
746 * b and e both start at the beginning of the input stream.
747 *
748 * e is incremented until we find the ending delimiter, or a NUL byte
749 * is reached. Then, we take e - b to find the length of the token.
750 */
751 b = e = *p;
752
753 while (*e) {
754 if ((delim == ' ' && isspace(*e)) || delim == *e)
755 break;
756 e++;
757 }
758
759 len = e - b;
760
761 /*
762 * Allocate memory to hold the string, and copy it in, converting
763 * characters to lowercase if lower is != 0.
764 */
765 t->val = malloc(len + 1);
766 if (!t->val)
767 return NULL;
768
769 for (i = 0; i < len; i++, b++) {
770 if (lower)
771 t->val[i] = tolower(*b);
772 else
773 t->val[i] = *b;
774 }
775
776 t->val[len] = '\0';
777
778 /*
779 * Update *p so the caller knows where to continue scanning.
780 */
781 *p = e;
782
783 t->type = T_STRING;
784
785 return t->val;
786}
787
788/*
789 * Populate a keyword token with a type and value.
790 */
791static void get_keyword(struct token *t)
792{
793 int i;
794
795 for (i = 0; keywords[i].val; i++) {
796 if (!strcmp(t->val, keywords[i].val)) {
797 t->type = keywords[i].type;
798 break;
799 }
800 }
801}
802
803/*
804 * Get the next token. We have to keep track of which state we're in to know
805 * if we're looking to get a string literal or a keyword.
806 *
807 * *p is updated to point at the first character after the current token.
808 */
809static void get_token(char **p, struct token *t, enum lex_state state)
810{
811 char *c = *p;
812
813 t->type = T_INVALID;
814
815 /* eat non EOL whitespace */
816 while (isblank(*c))
817 c++;
818
819 /*
820 * eat comments. note that string literals can't begin with #, but
821 * can contain a # after their first character.
822 */
823 if (*c == '#') {
824 while (*c && *c != '\n')
825 c++;
826 }
827
828 if (*c == '\n') {
829 t->type = T_EOL;
830 c++;
831 } else if (*c == '\0') {
832 t->type = T_EOF;
833 c++;
834 } else if (state == L_SLITERAL) {
835 get_string(&c, t, '\n', 0);
836 } else if (state == L_KEYWORD) {
837 /*
838 * when we expect a keyword, we first get the next string
839 * token delimited by whitespace, and then check if it
840 * matches a keyword in our keyword list. if it does, it's
841 * converted to a keyword token of the appropriate type, and
842 * if not, it remains a string token.
843 */
844 get_string(&c, t, ' ', 1);
845 get_keyword(t);
846 }
847
848 *p = c;
849}
850
851/*
852 * Increment *c until we get to the end of the current line, or EOF.
853 */
854static void eol_or_eof(char **c)
855{
856 while (**c && **c != '\n')
857 (*c)++;
858}
859
860/*
861 * All of these parse_* functions share some common behavior.
862 *
863 * They finish with *c pointing after the token they parse, and return 1 on
864 * success, or < 0 on error.
865 */
866
867/*
868 * Parse a string literal and store a pointer it at *dst. String literals
869 * terminate at the end of the line.
870 */
871static int parse_sliteral(char **c, char **dst)
872{
873 struct token t;
874 char *s = *c;
875
876 get_token(c, &t, L_SLITERAL);
877
878 if (t.type != T_STRING) {
879 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
880 return -EINVAL;
881 }
882
883 *dst = t.val;
884
885 return 1;
886}
887
888/*
889 * Parse a base 10 (unsigned) integer and store it at *dst.
890 */
891static int parse_integer(char **c, int *dst)
892{
893 struct token t;
894 char *s = *c;
895 unsigned long temp;
896
897 get_token(c, &t, L_SLITERAL);
898
899 if (t.type != T_STRING) {
900 printf("Expected string: %.*s\n", (int)(*c - s), s);
901 return -EINVAL;
902 }
903
904 if (strict_strtoul(t.val, 10, &temp) < 0) {
905 printf("Expected unsigned integer: %s\n", t.val);
906 return -EINVAL;
907 }
908
909 *dst = (int)temp;
910
911 free(t.val);
912
913 return 1;
914}
915
916static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
917
918/*
919 * Parse an include statement, and retrieve and parse the file it mentions.
920 *
921 * base should point to a location where it's safe to store the file, and
922 * nest_level should indicate how many nested includes have occurred. For this
923 * include, nest_level has already been incremented and doesn't need to be
924 * incremented here.
925 */
926static int handle_include(char **c, char *base,
927 struct pxe_menu *cfg, int nest_level)
928{
929 char *include_path;
930 char *s = *c;
931 int err;
932
933 err = parse_sliteral(c, &include_path);
934
935 if (err < 0) {
936 printf("Expected include path: %.*s\n",
937 (int)(*c - s), s);
938 return err;
939 }
940
941 err = get_pxe_file(include_path, base);
942
943 if (err < 0) {
944 printf("Couldn't retrieve %s\n", include_path);
945 return err;
946 }
947
948 return parse_pxefile_top(base, cfg, nest_level);
949}
950
951/*
952 * Parse lines that begin with 'menu'.
953 *
954 * b and nest are provided to handle the 'menu include' case.
955 *
956 * b should be the address where the file currently being parsed is stored.
957 *
958 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
959 * a file it includes, 3 when parsing a file included by that file, and so on.
960 */
961static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
962{
963 struct token t;
964 char *s = *c;
Heiko Schocher43d4a5e2011-12-12 20:37:17 +0000965 int err = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500966
967 get_token(c, &t, L_KEYWORD);
968
969 switch (t.type) {
970 case T_TITLE:
971 err = parse_sliteral(c, &cfg->title);
972
973 break;
974
975 case T_INCLUDE:
976 err = handle_include(c, b + strlen(b) + 1, cfg,
977 nest_level + 1);
978 break;
979
980 default:
981 printf("Ignoring malformed menu command: %.*s\n",
982 (int)(*c - s), s);
983 }
984
985 if (err < 0)
986 return err;
987
988 eol_or_eof(c);
989
990 return 1;
991}
992
993/*
994 * Handles parsing a 'menu line' when we're parsing a label.
995 */
996static int parse_label_menu(char **c, struct pxe_menu *cfg,
997 struct pxe_label *label)
998{
999 struct token t;
1000 char *s;
1001
1002 s = *c;
1003
1004 get_token(c, &t, L_KEYWORD);
1005
1006 switch (t.type) {
1007 case T_DEFAULT:
1008 if (cfg->default_label)
1009 free(cfg->default_label);
1010
1011 cfg->default_label = strdup(label->name);
1012
1013 if (!cfg->default_label)
1014 return -ENOMEM;
1015
1016 break;
Rob Herring7815c4e2012-03-28 05:51:34 +00001017 case T_LABEL:
1018 parse_sliteral(c, &label->menu);
1019 break;
Jason Hobbs06283a62011-08-31 10:37:30 -05001020 default:
1021 printf("Ignoring malformed menu command: %.*s\n",
1022 (int)(*c - s), s);
1023 }
1024
1025 eol_or_eof(c);
1026
1027 return 0;
1028}
1029
1030/*
1031 * Parses a label and adds it to the list of labels for a menu.
1032 *
1033 * A label ends when we either get to the end of a file, or
1034 * get some input we otherwise don't have a handler defined
1035 * for.
1036 *
1037 */
1038static int parse_label(char **c, struct pxe_menu *cfg)
1039{
1040 struct token t;
Rob Herring34bd23e2012-03-28 05:51:37 +00001041 int len;
Jason Hobbs06283a62011-08-31 10:37:30 -05001042 char *s = *c;
1043 struct pxe_label *label;
1044 int err;
1045
1046 label = label_create();
1047 if (!label)
1048 return -ENOMEM;
1049
1050 err = parse_sliteral(c, &label->name);
1051 if (err < 0) {
1052 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1053 label_destroy(label);
1054 return -EINVAL;
1055 }
1056
1057 list_add_tail(&label->list, &cfg->labels);
1058
1059 while (1) {
1060 s = *c;
1061 get_token(c, &t, L_KEYWORD);
1062
1063 err = 0;
1064 switch (t.type) {
1065 case T_MENU:
1066 err = parse_label_menu(c, cfg, label);
1067 break;
1068
1069 case T_KERNEL:
Rob Herringbeb9f6c2012-03-28 05:51:35 +00001070 case T_LINUX:
Jason Hobbs06283a62011-08-31 10:37:30 -05001071 err = parse_sliteral(c, &label->kernel);
1072 break;
1073
1074 case T_APPEND:
1075 err = parse_sliteral(c, &label->append);
Rob Herring34bd23e2012-03-28 05:51:37 +00001076 if (label->initrd)
1077 break;
1078 s = strstr(label->append, "initrd=");
1079 if (!s)
1080 break;
1081 s += 7;
1082 len = (int)(strchr(s, ' ') - s);
1083 label->initrd = malloc(len + 1);
1084 strncpy(label->initrd, s, len);
1085 label->initrd[len] = '\0';
1086
Jason Hobbs06283a62011-08-31 10:37:30 -05001087 break;
1088
1089 case T_INITRD:
Rob Herring34bd23e2012-03-28 05:51:37 +00001090 if (!label->initrd)
1091 err = parse_sliteral(c, &label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -05001092 break;
1093
1094 case T_LOCALBOOT:
1095 err = parse_integer(c, &label->localboot);
1096 break;
1097
1098 case T_EOL:
1099 break;
1100
1101 default:
1102 /*
1103 * put the token back! we don't want it - it's the end
1104 * of a label and whatever token this is, it's
1105 * something for the menu level context to handle.
1106 */
1107 *c = s;
1108 return 1;
1109 }
1110
1111 if (err < 0)
1112 return err;
1113 }
1114}
1115
1116/*
1117 * This 16 comes from the limit pxelinux imposes on nested includes.
1118 *
1119 * There is no reason at all we couldn't do more, but some limit helps prevent
1120 * infinite (until crash occurs) recursion if a file tries to include itself.
1121 */
1122#define MAX_NEST_LEVEL 16
1123
1124/*
1125 * Entry point for parsing a menu file. nest_level indicates how many times
1126 * we've nested in includes. It will be 1 for the top level menu file.
1127 *
1128 * Returns 1 on success, < 0 on error.
1129 */
1130static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level)
1131{
1132 struct token t;
1133 char *s, *b, *label_name;
1134 int err;
1135
1136 b = p;
1137
1138 if (nest_level > MAX_NEST_LEVEL) {
1139 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1140 return -EMLINK;
1141 }
1142
1143 while (1) {
1144 s = p;
1145
1146 get_token(&p, &t, L_KEYWORD);
1147
1148 err = 0;
1149 switch (t.type) {
1150 case T_MENU:
1151 err = parse_menu(&p, cfg, b, nest_level);
1152 break;
1153
1154 case T_TIMEOUT:
1155 err = parse_integer(&p, &cfg->timeout);
1156 break;
1157
1158 case T_LABEL:
1159 err = parse_label(&p, cfg);
1160 break;
1161
1162 case T_DEFAULT:
1163 err = parse_sliteral(&p, &label_name);
1164
1165 if (label_name) {
1166 if (cfg->default_label)
1167 free(cfg->default_label);
1168
1169 cfg->default_label = label_name;
1170 }
1171
1172 break;
1173
Rob Herring1e085222012-05-25 10:43:16 +00001174 case T_INCLUDE:
1175 err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg,
1176 nest_level + 1);
1177 break;
1178
Jason Hobbs06283a62011-08-31 10:37:30 -05001179 case T_PROMPT:
1180 err = parse_integer(&p, &cfg->prompt);
1181 break;
1182
1183 case T_EOL:
1184 break;
1185
1186 case T_EOF:
1187 return 1;
1188
1189 default:
1190 printf("Ignoring unknown command: %.*s\n",
1191 (int)(p - s), s);
1192 eol_or_eof(&p);
1193 }
1194
1195 if (err < 0)
1196 return err;
1197 }
1198}
1199
1200/*
1201 * Free the memory used by a pxe_menu and its labels.
1202 */
1203static void destroy_pxe_menu(struct pxe_menu *cfg)
1204{
1205 struct list_head *pos, *n;
1206 struct pxe_label *label;
1207
1208 if (cfg->title)
1209 free(cfg->title);
1210
1211 if (cfg->default_label)
1212 free(cfg->default_label);
1213
1214 list_for_each_safe(pos, n, &cfg->labels) {
1215 label = list_entry(pos, struct pxe_label, list);
1216
1217 label_destroy(label);
1218 }
1219
1220 free(cfg);
1221}
1222
1223/*
1224 * Entry point for parsing a pxe file. This is only used for the top level
1225 * file.
1226 *
1227 * Returns NULL if there is an error, otherwise, returns a pointer to a
1228 * pxe_menu struct populated with the results of parsing the pxe file (and any
1229 * files it includes). The resulting pxe_menu struct can be free()'d by using
1230 * the destroy_pxe_menu() function.
1231 */
1232static struct pxe_menu *parse_pxefile(char *menucfg)
1233{
1234 struct pxe_menu *cfg;
1235
1236 cfg = malloc(sizeof(struct pxe_menu));
1237
1238 if (!cfg)
1239 return NULL;
1240
1241 memset(cfg, 0, sizeof(struct pxe_menu));
1242
1243 INIT_LIST_HEAD(&cfg->labels);
1244
1245 if (parse_pxefile_top(menucfg, cfg, 1) < 0) {
1246 destroy_pxe_menu(cfg);
1247 return NULL;
1248 }
1249
1250 return cfg;
1251}
1252
1253/*
1254 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1255 * menu code.
1256 */
1257static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1258{
1259 struct pxe_label *label;
1260 struct list_head *pos;
1261 struct menu *m;
1262 int err;
1263
1264 /*
1265 * Create a menu and add items for all the labels.
1266 */
1267 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print);
1268
1269 if (!m)
1270 return NULL;
1271
1272 list_for_each(pos, &cfg->labels) {
1273 label = list_entry(pos, struct pxe_label, list);
1274
1275 if (menu_item_add(m, label->name, label) != 1) {
1276 menu_destroy(m);
1277 return NULL;
1278 }
1279 }
1280
1281 /*
1282 * After we've created items for each label in the menu, set the
1283 * menu's default label if one was specified.
1284 */
1285 if (cfg->default_label) {
1286 err = menu_default_set(m, cfg->default_label);
1287 if (err != 1) {
1288 if (err != -ENOENT) {
1289 menu_destroy(m);
1290 return NULL;
1291 }
1292
1293 printf("Missing default: %s\n", cfg->default_label);
1294 }
1295 }
1296
1297 return m;
1298}
1299
1300/*
1301 * Try to boot any labels we have yet to attempt to boot.
1302 */
1303static void boot_unattempted_labels(struct pxe_menu *cfg)
1304{
1305 struct list_head *pos;
1306 struct pxe_label *label;
1307
1308 list_for_each(pos, &cfg->labels) {
1309 label = list_entry(pos, struct pxe_label, list);
1310
1311 if (!label->attempted)
1312 label_boot(label);
1313 }
1314}
1315
1316/*
1317 * Boot the system as prescribed by a pxe_menu.
1318 *
1319 * Use the menu system to either get the user's choice or the default, based
1320 * on config or user input. If there is no default or user's choice,
1321 * attempted to boot labels in the order they were given in pxe files.
1322 * If the default or user's choice fails to boot, attempt to boot other
1323 * labels in the order they were given in pxe files.
1324 *
1325 * If this function returns, there weren't any labels that successfully
1326 * booted, or the user interrupted the menu selection via ctrl+c.
1327 */
1328static void handle_pxe_menu(struct pxe_menu *cfg)
1329{
1330 void *choice;
1331 struct menu *m;
1332 int err;
1333
1334 m = pxe_menu_to_menu(cfg);
1335 if (!m)
1336 return;
1337
1338 err = menu_get_choice(m, &choice);
1339
1340 menu_destroy(m);
1341
Jason Hobbs6f40f272011-11-07 03:07:15 +00001342 /*
1343 * err == 1 means we got a choice back from menu_get_choice.
1344 *
1345 * err == -ENOENT if the menu was setup to select the default but no
1346 * default was set. in that case, we should continue trying to boot
1347 * labels that haven't been attempted yet.
1348 *
1349 * otherwise, the user interrupted or there was some other error and
1350 * we give up.
1351 */
Jason Hobbs06283a62011-08-31 10:37:30 -05001352
Jason Hobbs6f40f272011-11-07 03:07:15 +00001353 if (err == 1)
1354 label_boot(choice);
1355 else if (err != -ENOENT)
1356 return;
Jason Hobbs06283a62011-08-31 10:37:30 -05001357
1358 boot_unattempted_labels(cfg);
1359}
1360
1361/*
1362 * Boots a system using a pxe file
1363 *
1364 * Returns 0 on success, 1 on error.
1365 */
1366static int
1367do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1368{
1369 unsigned long pxefile_addr_r;
1370 struct pxe_menu *cfg;
1371 char *pxefile_addr_str;
1372
Rob Herring669df7e2012-05-25 10:47:39 +00001373 do_getfile = do_get_tftp;
1374
Jason Hobbs06283a62011-08-31 10:37:30 -05001375 if (argc == 1) {
1376 pxefile_addr_str = from_env("pxefile_addr_r");
1377 if (!pxefile_addr_str)
1378 return 1;
1379
1380 } else if (argc == 2) {
1381 pxefile_addr_str = argv[1];
1382 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +00001383 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001384 }
1385
1386 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1387 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1388 return 1;
1389 }
1390
1391 cfg = parse_pxefile((char *)(pxefile_addr_r));
1392
1393 if (cfg == NULL) {
1394 printf("Error parsing config file\n");
1395 return 1;
1396 }
1397
1398 handle_pxe_menu(cfg);
1399
1400 destroy_pxe_menu(cfg);
1401
1402 return 0;
1403}
1404
1405static cmd_tbl_t cmd_pxe_sub[] = {
1406 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1407 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1408};
1409
1410int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1411{
1412 cmd_tbl_t *cp;
1413
1414 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001415 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001416
1417 /* drop initial "pxe" arg */
1418 argc--;
1419 argv++;
1420
1421 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1422
1423 if (cp)
1424 return cp->cmd(cmdtp, flag, argc, argv);
1425
Simon Glass4c12eeb2011-12-10 08:44:01 +00001426 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001427}
1428
1429U_BOOT_CMD(
1430 pxe, 3, 1, do_pxe,
1431 "commands to get and boot from pxe files",
1432 "get - try to retrieve a pxe file using tftp\npxe "
1433 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1434);
Rob Herring669df7e2012-05-25 10:47:39 +00001435
1436/*
1437 * Boots a system using a local disk syslinux/extlinux file
1438 *
1439 * Returns 0 on success, 1 on error.
1440 */
1441int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1442{
1443 unsigned long pxefile_addr_r;
1444 struct pxe_menu *cfg;
1445 char *pxefile_addr_str;
1446 char *filename;
1447 int prompt = 0;
1448
1449 if (strstr(argv[1], "-p")) {
1450 prompt = 1;
1451 argc--;
1452 argv++;
1453 }
1454
1455 if (argc < 4)
1456 return cmd_usage(cmdtp);
1457
1458 if (argc < 5) {
1459 pxefile_addr_str = from_env("pxefile_addr_r");
1460 if (!pxefile_addr_str)
1461 return 1;
1462 } else {
1463 pxefile_addr_str = argv[4];
1464 }
1465
1466 if (argc < 6)
1467 filename = getenv("bootfile");
1468 else {
1469 filename = argv[5];
1470 setenv("bootfile", filename);
1471 }
1472
1473 if (strstr(argv[3], "ext2"))
1474 do_getfile = do_get_ext2;
1475 else if (strstr(argv[3], "fat"))
1476 do_getfile = do_get_fat;
1477 else {
1478 printf("Invalid filesystem: %s\n", argv[3]);
1479 return 1;
1480 }
1481 fs_argv[1] = argv[1];
1482 fs_argv[2] = argv[2];
1483
1484 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1485 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1486 return 1;
1487 }
1488
1489 if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) {
1490 printf("Error reading config file\n");
1491 return 1;
1492 }
1493
1494 cfg = parse_pxefile((char *)(pxefile_addr_r));
1495
1496 if (cfg == NULL) {
1497 printf("Error parsing config file\n");
1498 return 1;
1499 }
1500
1501 if (prompt)
1502 cfg->prompt = 1;
1503
1504 handle_pxe_menu(cfg);
1505
1506 destroy_pxe_menu(cfg);
1507
1508 return 0;
1509}
1510
1511U_BOOT_CMD(
1512 sysboot, 7, 1, do_sysboot,
1513 "command to get and boot from syslinux files",
1514 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1515 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1516 " filesystem on 'dev' on 'interface' to address 'addr'"
1517);