blob: 050874d812e118b293e504884ad746b2f46f2631 [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
29
30/*
31 * Like getenv, but prints an error if envvar isn't defined in the
32 * environment. It always returns what getenv does, so it can be used in
33 * place of getenv without changing error handling otherwise.
34 */
35static char *from_env(char *envvar)
36{
37 char *ret;
38
39 ret = getenv(envvar);
40
41 if (!ret)
42 printf("missing environment variable: %s\n", envvar);
43
44 return ret;
45}
46
47/*
48 * Convert an ethaddr from the environment to the format used by pxelinux
49 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
50 * the beginning of the ethernet address to indicate a hardware type of
51 * Ethernet. Also converts uppercase hex characters into lowercase, to match
52 * pxelinux's behavior.
53 *
54 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
55 * environment, or some other value < 0 on error.
56 */
57static int format_mac_pxe(char *outbuf, size_t outbuf_len)
58{
59 size_t ethaddr_len;
60 char *p, *ethaddr;
61
62 ethaddr = from_env("ethaddr");
63
64 if (!ethaddr)
John Rigbybe99b9e2011-07-13 23:05:19 -060065 ethaddr = from_env("usbethaddr");
66
67 if (!ethaddr)
Jason Hobbs06283a62011-08-31 10:37:30 -050068 return -ENOENT;
69
70 ethaddr_len = strlen(ethaddr);
71
72 /*
73 * ethaddr_len + 4 gives room for "01-", ethaddr, and a NUL byte at
74 * the end.
75 */
76 if (outbuf_len < ethaddr_len + 4) {
77 printf("outbuf is too small (%d < %d)\n",
78 outbuf_len, ethaddr_len + 4);
79
80 return -EINVAL;
81 }
82
83 strcpy(outbuf, "01-");
84
85 for (p = outbuf + 3; *ethaddr; ethaddr++, p++) {
86 if (*ethaddr == ':')
87 *p = '-';
88 else
89 *p = tolower(*ethaddr);
90 }
91
92 *p = '\0';
93
94 return 1;
95}
96
97/*
98 * Returns the directory the file specified in the bootfile env variable is
99 * in. If bootfile isn't defined in the environment, return NULL, which should
100 * be interpreted as "don't prepend anything to paths".
101 */
Rob Herringae97aaf2012-03-28 05:51:36 +0000102static int get_bootfile_path(const char *file_path, char *bootfile_path,
103 size_t bootfile_path_size)
Jason Hobbs06283a62011-08-31 10:37:30 -0500104{
105 char *bootfile, *last_slash;
Rob Herringae97aaf2012-03-28 05:51:36 +0000106 size_t path_len = 0;
107
108 if (file_path[0] == '/')
109 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -0500110
111 bootfile = from_env("bootfile");
112
Rob Herringae97aaf2012-03-28 05:51:36 +0000113 if (!bootfile)
114 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -0500115
116 last_slash = strrchr(bootfile, '/');
117
Rob Herringae97aaf2012-03-28 05:51:36 +0000118 if (last_slash == NULL)
119 goto ret;
Jason Hobbs06283a62011-08-31 10:37:30 -0500120
121 path_len = (last_slash - bootfile) + 1;
122
123 if (bootfile_path_size < path_len) {
124 printf("bootfile_path too small. (%d < %d)\n",
125 bootfile_path_size, path_len);
126
127 return -1;
128 }
129
130 strncpy(bootfile_path, bootfile, path_len);
131
Rob Herringae97aaf2012-03-28 05:51:36 +0000132 ret:
Jason Hobbs06283a62011-08-31 10:37:30 -0500133 bootfile_path[path_len] = '\0';
134
135 return 1;
136}
137
Rob Herringcfb48a32012-05-25 10:47:39 +0000138static int (*do_getfile)(char *file_path, char *file_addr);
139
140static int do_get_tftp(char *file_path, char *file_addr)
141{
142 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
143
144 tftp_argv[1] = file_addr;
145 tftp_argv[2] = file_path;
146
147 if (do_tftpb(NULL, 0, 3, tftp_argv))
148 return -ENOENT;
149
150 return 1;
151}
152
153static char *fs_argv[5];
154
155static int do_get_ext2(char *file_path, char *file_addr)
156{
157#ifdef CONFIG_CMD_EXT2
158 fs_argv[0] = "ext2load";
159 fs_argv[3] = file_addr;
160 fs_argv[4] = file_path;
161
162 if (!do_ext2load(NULL, 0, 5, fs_argv))
163 return 1;
164#endif
165 return -ENOENT;
166}
167
168static int do_get_fat(char *file_path, char *file_addr)
169{
170#ifdef CONFIG_CMD_FAT
171 fs_argv[0] = "fatload";
172 fs_argv[3] = file_addr;
173 fs_argv[4] = file_path;
174
175 if (!do_fat_fsload(NULL, 0, 5, fs_argv))
176 return 1;
177#endif
178 return -ENOENT;
179}
180
181
182
Jason Hobbs06283a62011-08-31 10:37:30 -0500183/*
184 * As in pxelinux, paths to files referenced from files we retrieve are
185 * relative to the location of bootfile. get_relfile takes such a path and
186 * joins it with the bootfile path to get the full path to the target file. If
187 * the bootfile path is NULL, we use file_path as is.
188 *
189 * Returns 1 for success, or < 0 on error.
190 */
191static int get_relfile(char *file_path, void *file_addr)
192{
193 size_t path_len;
194 char relfile[MAX_TFTP_PATH_LEN+1];
195 char addr_buf[10];
Jason Hobbs06283a62011-08-31 10:37:30 -0500196 int err;
197
Rob Herringae97aaf2012-03-28 05:51:36 +0000198 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
Jason Hobbs06283a62011-08-31 10:37:30 -0500199
200 if (err < 0)
201 return err;
202
203 path_len = strlen(file_path);
204 path_len += strlen(relfile);
205
206 if (path_len > MAX_TFTP_PATH_LEN) {
207 printf("Base path too long (%s%s)\n",
208 relfile,
209 file_path);
210
211 return -ENAMETOOLONG;
212 }
213
214 strcat(relfile, file_path);
215
216 printf("Retrieving file: %s\n", relfile);
217
218 sprintf(addr_buf, "%p", file_addr);
219
Rob Herringcfb48a32012-05-25 10:47:39 +0000220 return do_getfile(relfile, addr_buf);
Jason Hobbs06283a62011-08-31 10:37:30 -0500221}
222
223/*
224 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
225 * 'bootfile' was specified in the environment, the path to bootfile will be
226 * prepended to 'file_path' and the resulting path will be used.
227 *
228 * Returns 1 on success, or < 0 for error.
229 */
230static int get_pxe_file(char *file_path, void *file_addr)
231{
232 unsigned long config_file_size;
233 char *tftp_filesize;
234 int err;
235
236 err = get_relfile(file_path, file_addr);
237
238 if (err < 0)
239 return err;
240
241 /*
242 * the file comes without a NUL byte at the end, so find out its size
243 * and add the NUL byte.
244 */
245 tftp_filesize = from_env("filesize");
246
247 if (!tftp_filesize)
248 return -ENOENT;
249
250 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
251 return -EINVAL;
252
253 *(char *)(file_addr + config_file_size) = '\0';
254
255 return 1;
256}
257
258#define PXELINUX_DIR "pxelinux.cfg/"
259
260/*
261 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
262 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
263 * from the bootfile path, as described above.
264 *
265 * Returns 1 on success or < 0 on error.
266 */
267static int get_pxelinux_path(char *file, void *pxefile_addr_r)
268{
269 size_t base_len = strlen(PXELINUX_DIR);
270 char path[MAX_TFTP_PATH_LEN+1];
271
272 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
273 printf("path (%s%s) too long, skipping\n",
274 PXELINUX_DIR, file);
275 return -ENAMETOOLONG;
276 }
277
278 sprintf(path, PXELINUX_DIR "%s", file);
279
280 return get_pxe_file(path, pxefile_addr_r);
281}
282
283/*
284 * Looks for a pxe file with a name based on the pxeuuid environment variable.
285 *
286 * Returns 1 on success or < 0 on error.
287 */
288static int pxe_uuid_path(void *pxefile_addr_r)
289{
290 char *uuid_str;
291
292 uuid_str = from_env("pxeuuid");
293
294 if (!uuid_str)
295 return -ENOENT;
296
297 return get_pxelinux_path(uuid_str, pxefile_addr_r);
298}
299
300/*
301 * Looks for a pxe file with a name based on the 'ethaddr' environment
302 * variable.
303 *
304 * Returns 1 on success or < 0 on error.
305 */
306static int pxe_mac_path(void *pxefile_addr_r)
307{
308 char mac_str[21];
309 int err;
310
311 err = format_mac_pxe(mac_str, sizeof(mac_str));
312
313 if (err < 0)
314 return err;
315
316 return get_pxelinux_path(mac_str, pxefile_addr_r);
317}
318
319/*
320 * Looks for pxe files with names based on our IP address. See pxelinux
321 * documentation for details on what these file names look like. We match
322 * that exactly.
323 *
324 * Returns 1 on success or < 0 on error.
325 */
326static int pxe_ipaddr_paths(void *pxefile_addr_r)
327{
328 char ip_addr[9];
329 int mask_pos, err;
330
331 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
332
333 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
334 err = get_pxelinux_path(ip_addr, pxefile_addr_r);
335
336 if (err > 0)
337 return err;
338
339 ip_addr[mask_pos] = '\0';
340 }
341
342 return -ENOENT;
343}
344
345/*
346 * Entry point for the 'pxe get' command.
347 * This Follows pxelinux's rules to download a config file from a tftp server.
348 * The file is stored at the location given by the pxefile_addr_r environment
349 * variable, which must be set.
350 *
351 * UUID comes from pxeuuid env variable, if defined
352 * MAC addr comes from ethaddr env variable, if defined
353 * IP
354 *
355 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
356 *
357 * Returns 0 on success or 1 on error.
358 */
359static int
360do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
361{
362 char *pxefile_addr_str;
Jason Hobbs834c9382012-03-05 08:12:28 +0000363 unsigned long pxefile_addr_r;
Jason Hobbs06283a62011-08-31 10:37:30 -0500364 int err;
365
Rob Herringcfb48a32012-05-25 10:47:39 +0000366 do_getfile = do_get_tftp;
367
Jason Hobbs06283a62011-08-31 10:37:30 -0500368 if (argc != 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000369 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500370
371
372 pxefile_addr_str = from_env("pxefile_addr_r");
373
374 if (!pxefile_addr_str)
375 return 1;
376
377 err = strict_strtoul(pxefile_addr_str, 16,
378 (unsigned long *)&pxefile_addr_r);
379 if (err < 0)
380 return 1;
381
382 /*
383 * Keep trying paths until we successfully get a file we're looking
384 * for.
385 */
Jason Hobbs834c9382012-03-05 08:12:28 +0000386 if (pxe_uuid_path((void *)pxefile_addr_r) > 0
387 || pxe_mac_path((void *)pxefile_addr_r) > 0
388 || pxe_ipaddr_paths((void *)pxefile_addr_r) > 0
389 || get_pxelinux_path("default", (void *)pxefile_addr_r) > 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500390
391 printf("Config file found\n");
392
393 return 0;
394 }
395
396 printf("Config file not found\n");
397
398 return 1;
399}
400
401/*
402 * Wrapper to make it easier to store the file at file_path in the location
403 * specified by envaddr_name. file_path will be joined to the bootfile path,
404 * if any is specified.
405 *
406 * Returns 1 on success or < 0 on error.
407 */
408static int get_relfile_envaddr(char *file_path, char *envaddr_name)
409{
Jason Hobbs834c9382012-03-05 08:12:28 +0000410 unsigned long file_addr;
Jason Hobbs06283a62011-08-31 10:37:30 -0500411 char *envaddr;
412
413 envaddr = from_env(envaddr_name);
414
415 if (!envaddr)
416 return -ENOENT;
417
Jason Hobbs834c9382012-03-05 08:12:28 +0000418 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
Jason Hobbs06283a62011-08-31 10:37:30 -0500419 return -EINVAL;
420
Jason Hobbs834c9382012-03-05 08:12:28 +0000421 return get_relfile(file_path, (void *)file_addr);
Jason Hobbs06283a62011-08-31 10:37:30 -0500422}
423
424/*
425 * A note on the pxe file parser.
426 *
427 * We're parsing files that use syslinux grammar, which has a few quirks.
428 * String literals must be recognized based on context - there is no
429 * quoting or escaping support. There's also nothing to explicitly indicate
430 * when a label section completes. We deal with that by ending a label
431 * section whenever we see a line that doesn't include.
432 *
433 * As with the syslinux family, this same file format could be reused in the
434 * future for non pxe purposes. The only action it takes during parsing that
435 * would throw this off is handling of include files. It assumes we're using
436 * pxe, and does a tftp download of a file listed as an include file in the
437 * middle of the parsing operation. That could be handled by refactoring it to
438 * take a 'include file getter' function.
439 */
440
441/*
442 * Describes a single label given in a pxe file.
443 *
444 * Create these with the 'label_create' function given below.
445 *
446 * name - the name of the menu as given on the 'menu label' line.
447 * kernel - the path to the kernel file to use for this label.
448 * append - kernel command line to use when booting this label
449 * initrd - path to the initrd to use for this label.
450 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
451 * localboot - 1 if this label specified 'localboot', 0 otherwise.
452 * list - lets these form a list, which a pxe_menu struct will hold.
453 */
454struct pxe_label {
455 char *name;
Rob Herringe539d112012-03-28 05:51:34 +0000456 char *menu;
Jason Hobbs06283a62011-08-31 10:37:30 -0500457 char *kernel;
458 char *append;
459 char *initrd;
460 int attempted;
461 int localboot;
462 struct list_head list;
463};
464
465/*
466 * Describes a pxe menu as given via pxe files.
467 *
468 * title - the name of the menu as given by a 'menu title' line.
469 * default_label - the name of the default label, if any.
470 * timeout - time in tenths of a second to wait for a user key-press before
471 * booting the default label.
472 * prompt - if 0, don't prompt for a choice unless the timeout period is
473 * interrupted. If 1, always prompt for a choice regardless of
474 * timeout.
475 * labels - a list of labels defined for the menu.
476 */
477struct pxe_menu {
478 char *title;
479 char *default_label;
480 int timeout;
481 int prompt;
482 struct list_head labels;
483};
484
485/*
486 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
487 * result must be free()'d to reclaim the memory.
488 *
489 * Returns NULL if malloc fails.
490 */
491static struct pxe_label *label_create(void)
492{
493 struct pxe_label *label;
494
495 label = malloc(sizeof(struct pxe_label));
496
497 if (!label)
498 return NULL;
499
500 memset(label, 0, sizeof(struct pxe_label));
501
502 return label;
503}
504
505/*
506 * Free the memory used by a pxe_label, including that used by its name,
507 * kernel, append and initrd members, if they're non NULL.
508 *
509 * So - be sure to only use dynamically allocated memory for the members of
510 * the pxe_label struct, unless you want to clean it up first. These are
511 * currently only created by the pxe file parsing code.
512 */
513static void label_destroy(struct pxe_label *label)
514{
515 if (label->name)
516 free(label->name);
517
518 if (label->kernel)
519 free(label->kernel);
520
521 if (label->append)
522 free(label->append);
523
524 if (label->initrd)
525 free(label->initrd);
526
527 free(label);
528}
529
530/*
531 * Print a label and its string members if they're defined.
532 *
533 * This is passed as a callback to the menu code for displaying each
534 * menu entry.
535 */
536static void label_print(void *data)
537{
538 struct pxe_label *label = data;
Rob Herringe539d112012-03-28 05:51:34 +0000539 const char *c = label->menu ? label->menu : label->kernel;
Jason Hobbs06283a62011-08-31 10:37:30 -0500540
Rob Herringe539d112012-03-28 05:51:34 +0000541 printf("%s:\t%s\n", label->name, c);
Jason Hobbs06283a62011-08-31 10:37:30 -0500542
543 if (label->kernel)
Rob Herringe539d112012-03-28 05:51:34 +0000544 printf("\t\tkernel: %s\n", label->kernel);
Jason Hobbs06283a62011-08-31 10:37:30 -0500545
546 if (label->append)
Rob Herringe539d112012-03-28 05:51:34 +0000547 printf("\t\tappend: %s\n", label->append);
Jason Hobbs06283a62011-08-31 10:37:30 -0500548
549 if (label->initrd)
Rob Herringe539d112012-03-28 05:51:34 +0000550 printf("\t\tinitrd: %s\n", label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -0500551}
552
553/*
554 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
555 * environment variable is defined. Its contents will be executed as U-boot
556 * command. If the label specified an 'append' line, its contents will be
557 * used to overwrite the contents of the 'bootargs' environment variable prior
558 * to running 'localcmd'.
559 *
560 * Returns 1 on success or < 0 on error.
561 */
562static int label_localboot(struct pxe_label *label)
563{
564 char *localcmd, *dupcmd;
565 int ret;
566
567 localcmd = from_env("localcmd");
568
569 if (!localcmd)
570 return -ENOENT;
571
572 /*
573 * dup the command to avoid any issues with the version of it existing
574 * in the environment changing during the execution of the command.
575 */
576 dupcmd = strdup(localcmd);
577
578 if (!dupcmd)
579 return -ENOMEM;
580
581 if (label->append)
582 setenv("bootargs", label->append);
583
584 printf("running: %s\n", dupcmd);
585
Simon Glass009dde12012-02-14 19:59:20 +0000586 ret = run_command(dupcmd, 0);
Jason Hobbs06283a62011-08-31 10:37:30 -0500587
588 free(dupcmd);
589
590 return ret;
591}
592
593/*
594 * Boot according to the contents of a pxe_label.
595 *
596 * If we can't boot for any reason, we return. A successful boot never
597 * returns.
598 *
599 * The kernel will be stored in the location given by the 'kernel_addr_r'
600 * environment variable.
601 *
602 * If the label specifies an initrd file, it will be stored in the location
603 * given by the 'ramdisk_addr_r' environment variable.
604 *
605 * If the label specifies an 'append' line, its contents will overwrite that
606 * of the 'bootargs' environment variable.
607 */
608static void label_boot(struct pxe_label *label)
609{
610 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
611 int bootm_argc = 3;
612
613 label_print(label);
614
615 label->attempted = 1;
616
617 if (label->localboot) {
618 label_localboot(label);
619 return;
620 }
621
622 if (label->kernel == NULL) {
623 printf("No kernel given, skipping %s\n",
624 label->name);
625 return;
626 }
627
628 if (label->initrd) {
629 if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) {
630 printf("Skipping %s for failure retrieving initrd\n",
631 label->name);
632 return;
633 }
634
635 bootm_argv[2] = getenv("ramdisk_addr_r");
636 } else {
637 bootm_argv[2] = "-";
638 }
639
640 if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) {
641 printf("Skipping %s for failure retrieving kernel\n",
642 label->name);
643 return;
644 }
645
646 if (label->append)
647 setenv("bootargs", label->append);
648
649 bootm_argv[1] = getenv("kernel_addr_r");
650
651 /*
652 * fdt usage is optional. If there is an fdt_addr specified, we will
653 * pass it along to bootm, and adjust argc appropriately.
654 */
655 bootm_argv[3] = getenv("fdt_addr");
656
657 if (bootm_argv[3])
658 bootm_argc = 4;
659
660 do_bootm(NULL, 0, bootm_argc, bootm_argv);
661}
662
663/*
664 * Tokens for the pxe file parser.
665 */
666enum token_type {
667 T_EOL,
668 T_STRING,
669 T_EOF,
670 T_MENU,
671 T_TITLE,
672 T_TIMEOUT,
673 T_LABEL,
674 T_KERNEL,
Rob Herringa0854ad2012-03-28 05:51:35 +0000675 T_LINUX,
Jason Hobbs06283a62011-08-31 10:37:30 -0500676 T_APPEND,
677 T_INITRD,
678 T_LOCALBOOT,
679 T_DEFAULT,
680 T_PROMPT,
681 T_INCLUDE,
682 T_INVALID
683};
684
685/*
686 * A token - given by a value and a type.
687 */
688struct token {
689 char *val;
690 enum token_type type;
691};
692
693/*
694 * Keywords recognized.
695 */
696static const struct token keywords[] = {
697 {"menu", T_MENU},
698 {"title", T_TITLE},
699 {"timeout", T_TIMEOUT},
700 {"default", T_DEFAULT},
701 {"prompt", T_PROMPT},
702 {"label", T_LABEL},
703 {"kernel", T_KERNEL},
Rob Herringa0854ad2012-03-28 05:51:35 +0000704 {"linux", T_LINUX},
Jason Hobbs06283a62011-08-31 10:37:30 -0500705 {"localboot", T_LOCALBOOT},
706 {"append", T_APPEND},
707 {"initrd", T_INITRD},
708 {"include", T_INCLUDE},
709 {NULL, T_INVALID}
710};
711
712/*
713 * Since pxe(linux) files don't have a token to identify the start of a
714 * literal, we have to keep track of when we're in a state where a literal is
715 * expected vs when we're in a state a keyword is expected.
716 */
717enum lex_state {
718 L_NORMAL = 0,
719 L_KEYWORD,
720 L_SLITERAL
721};
722
723/*
724 * get_string retrieves a string from *p and stores it as a token in
725 * *t.
726 *
727 * get_string used for scanning both string literals and keywords.
728 *
729 * Characters from *p are copied into t-val until a character equal to
730 * delim is found, or a NUL byte is reached. If delim has the special value of
731 * ' ', any whitespace character will be used as a delimiter.
732 *
733 * If lower is unequal to 0, uppercase characters will be converted to
734 * lowercase in the result. This is useful to make keywords case
735 * insensitive.
736 *
737 * The location of *p is updated to point to the first character after the end
738 * of the token - the ending delimiter.
739 *
740 * On success, the new value of t->val is returned. Memory for t->val is
741 * allocated using malloc and must be free()'d to reclaim it. If insufficient
742 * memory is available, NULL is returned.
743 */
744static char *get_string(char **p, struct token *t, char delim, int lower)
745{
746 char *b, *e;
747 size_t len, i;
748
749 /*
750 * b and e both start at the beginning of the input stream.
751 *
752 * e is incremented until we find the ending delimiter, or a NUL byte
753 * is reached. Then, we take e - b to find the length of the token.
754 */
755 b = e = *p;
756
757 while (*e) {
758 if ((delim == ' ' && isspace(*e)) || delim == *e)
759 break;
760 e++;
761 }
762
763 len = e - b;
764
765 /*
766 * Allocate memory to hold the string, and copy it in, converting
767 * characters to lowercase if lower is != 0.
768 */
769 t->val = malloc(len + 1);
770 if (!t->val)
771 return NULL;
772
773 for (i = 0; i < len; i++, b++) {
774 if (lower)
775 t->val[i] = tolower(*b);
776 else
777 t->val[i] = *b;
778 }
779
780 t->val[len] = '\0';
781
782 /*
783 * Update *p so the caller knows where to continue scanning.
784 */
785 *p = e;
786
787 t->type = T_STRING;
788
789 return t->val;
790}
791
792/*
793 * Populate a keyword token with a type and value.
794 */
795static void get_keyword(struct token *t)
796{
797 int i;
798
799 for (i = 0; keywords[i].val; i++) {
800 if (!strcmp(t->val, keywords[i].val)) {
801 t->type = keywords[i].type;
802 break;
803 }
804 }
805}
806
807/*
808 * Get the next token. We have to keep track of which state we're in to know
809 * if we're looking to get a string literal or a keyword.
810 *
811 * *p is updated to point at the first character after the current token.
812 */
813static void get_token(char **p, struct token *t, enum lex_state state)
814{
815 char *c = *p;
816
817 t->type = T_INVALID;
818
819 /* eat non EOL whitespace */
820 while (isblank(*c))
821 c++;
822
823 /*
824 * eat comments. note that string literals can't begin with #, but
825 * can contain a # after their first character.
826 */
827 if (*c == '#') {
828 while (*c && *c != '\n')
829 c++;
830 }
831
832 if (*c == '\n') {
833 t->type = T_EOL;
834 c++;
835 } else if (*c == '\0') {
836 t->type = T_EOF;
837 c++;
838 } else if (state == L_SLITERAL) {
839 get_string(&c, t, '\n', 0);
840 } else if (state == L_KEYWORD) {
841 /*
842 * when we expect a keyword, we first get the next string
843 * token delimited by whitespace, and then check if it
844 * matches a keyword in our keyword list. if it does, it's
845 * converted to a keyword token of the appropriate type, and
846 * if not, it remains a string token.
847 */
848 get_string(&c, t, ' ', 1);
849 get_keyword(t);
850 }
851
852 *p = c;
853}
854
855/*
856 * Increment *c until we get to the end of the current line, or EOF.
857 */
858static void eol_or_eof(char **c)
859{
860 while (**c && **c != '\n')
861 (*c)++;
862}
863
864/*
865 * All of these parse_* functions share some common behavior.
866 *
867 * They finish with *c pointing after the token they parse, and return 1 on
868 * success, or < 0 on error.
869 */
870
871/*
872 * Parse a string literal and store a pointer it at *dst. String literals
873 * terminate at the end of the line.
874 */
875static int parse_sliteral(char **c, char **dst)
876{
877 struct token t;
878 char *s = *c;
879
880 get_token(c, &t, L_SLITERAL);
881
882 if (t.type != T_STRING) {
883 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
884 return -EINVAL;
885 }
886
887 *dst = t.val;
888
889 return 1;
890}
891
892/*
893 * Parse a base 10 (unsigned) integer and store it at *dst.
894 */
895static int parse_integer(char **c, int *dst)
896{
897 struct token t;
898 char *s = *c;
899 unsigned long temp;
900
901 get_token(c, &t, L_SLITERAL);
902
903 if (t.type != T_STRING) {
904 printf("Expected string: %.*s\n", (int)(*c - s), s);
905 return -EINVAL;
906 }
907
908 if (strict_strtoul(t.val, 10, &temp) < 0) {
909 printf("Expected unsigned integer: %s\n", t.val);
910 return -EINVAL;
911 }
912
913 *dst = (int)temp;
914
915 free(t.val);
916
917 return 1;
918}
919
920static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
921
922/*
923 * Parse an include statement, and retrieve and parse the file it mentions.
924 *
925 * base should point to a location where it's safe to store the file, and
926 * nest_level should indicate how many nested includes have occurred. For this
927 * include, nest_level has already been incremented and doesn't need to be
928 * incremented here.
929 */
930static int handle_include(char **c, char *base,
931 struct pxe_menu *cfg, int nest_level)
932{
933 char *include_path;
934 char *s = *c;
935 int err;
936
937 err = parse_sliteral(c, &include_path);
938
939 if (err < 0) {
940 printf("Expected include path: %.*s\n",
941 (int)(*c - s), s);
942 return err;
943 }
944
945 err = get_pxe_file(include_path, base);
946
947 if (err < 0) {
948 printf("Couldn't retrieve %s\n", include_path);
949 return err;
950 }
951
952 return parse_pxefile_top(base, cfg, nest_level);
953}
954
955/*
956 * Parse lines that begin with 'menu'.
957 *
958 * b and nest are provided to handle the 'menu include' case.
959 *
960 * b should be the address where the file currently being parsed is stored.
961 *
962 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
963 * a file it includes, 3 when parsing a file included by that file, and so on.
964 */
965static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
966{
967 struct token t;
968 char *s = *c;
Heiko Schocher43d4a5e2011-12-12 20:37:17 +0000969 int err = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500970
971 get_token(c, &t, L_KEYWORD);
972
973 switch (t.type) {
974 case T_TITLE:
975 err = parse_sliteral(c, &cfg->title);
976
977 break;
978
979 case T_INCLUDE:
980 err = handle_include(c, b + strlen(b) + 1, cfg,
981 nest_level + 1);
982 break;
983
984 default:
985 printf("Ignoring malformed menu command: %.*s\n",
986 (int)(*c - s), s);
987 }
988
989 if (err < 0)
990 return err;
991
992 eol_or_eof(c);
993
994 return 1;
995}
996
997/*
998 * Handles parsing a 'menu line' when we're parsing a label.
999 */
1000static int parse_label_menu(char **c, struct pxe_menu *cfg,
1001 struct pxe_label *label)
1002{
1003 struct token t;
1004 char *s;
1005
1006 s = *c;
1007
1008 get_token(c, &t, L_KEYWORD);
1009
1010 switch (t.type) {
1011 case T_DEFAULT:
1012 if (cfg->default_label)
1013 free(cfg->default_label);
1014
1015 cfg->default_label = strdup(label->name);
1016
1017 if (!cfg->default_label)
1018 return -ENOMEM;
1019
1020 break;
Rob Herringe539d112012-03-28 05:51:34 +00001021 case T_LABEL:
1022 parse_sliteral(c, &label->menu);
1023 break;
Jason Hobbs06283a62011-08-31 10:37:30 -05001024 default:
1025 printf("Ignoring malformed menu command: %.*s\n",
1026 (int)(*c - s), s);
1027 }
1028
1029 eol_or_eof(c);
1030
1031 return 0;
1032}
1033
1034/*
1035 * Parses a label and adds it to the list of labels for a menu.
1036 *
1037 * A label ends when we either get to the end of a file, or
1038 * get some input we otherwise don't have a handler defined
1039 * for.
1040 *
1041 */
1042static int parse_label(char **c, struct pxe_menu *cfg)
1043{
1044 struct token t;
Rob Herring36f306c2012-03-28 05:51:37 +00001045 int len;
Jason Hobbs06283a62011-08-31 10:37:30 -05001046 char *s = *c;
1047 struct pxe_label *label;
1048 int err;
1049
1050 label = label_create();
1051 if (!label)
1052 return -ENOMEM;
1053
1054 err = parse_sliteral(c, &label->name);
1055 if (err < 0) {
1056 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1057 label_destroy(label);
1058 return -EINVAL;
1059 }
1060
1061 list_add_tail(&label->list, &cfg->labels);
1062
1063 while (1) {
1064 s = *c;
1065 get_token(c, &t, L_KEYWORD);
1066
1067 err = 0;
1068 switch (t.type) {
1069 case T_MENU:
1070 err = parse_label_menu(c, cfg, label);
1071 break;
1072
1073 case T_KERNEL:
Rob Herringa0854ad2012-03-28 05:51:35 +00001074 case T_LINUX:
Jason Hobbs06283a62011-08-31 10:37:30 -05001075 err = parse_sliteral(c, &label->kernel);
1076 break;
1077
1078 case T_APPEND:
1079 err = parse_sliteral(c, &label->append);
Rob Herring36f306c2012-03-28 05:51:37 +00001080 if (label->initrd)
1081 break;
1082 s = strstr(label->append, "initrd=");
1083 if (!s)
1084 break;
1085 s += 7;
1086 len = (int)(strchr(s, ' ') - s);
1087 label->initrd = malloc(len + 1);
1088 strncpy(label->initrd, s, len);
1089 label->initrd[len] = '\0';
1090
Jason Hobbs06283a62011-08-31 10:37:30 -05001091 break;
1092
1093 case T_INITRD:
Rob Herring36f306c2012-03-28 05:51:37 +00001094 if (!label->initrd)
1095 err = parse_sliteral(c, &label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -05001096 break;
1097
1098 case T_LOCALBOOT:
1099 err = parse_integer(c, &label->localboot);
1100 break;
1101
1102 case T_EOL:
1103 break;
1104
1105 default:
1106 /*
1107 * put the token back! we don't want it - it's the end
1108 * of a label and whatever token this is, it's
1109 * something for the menu level context to handle.
1110 */
1111 *c = s;
1112 return 1;
1113 }
1114
1115 if (err < 0)
1116 return err;
1117 }
1118}
1119
1120/*
1121 * This 16 comes from the limit pxelinux imposes on nested includes.
1122 *
1123 * There is no reason at all we couldn't do more, but some limit helps prevent
1124 * infinite (until crash occurs) recursion if a file tries to include itself.
1125 */
1126#define MAX_NEST_LEVEL 16
1127
1128/*
1129 * Entry point for parsing a menu file. nest_level indicates how many times
1130 * we've nested in includes. It will be 1 for the top level menu file.
1131 *
1132 * Returns 1 on success, < 0 on error.
1133 */
1134static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level)
1135{
1136 struct token t;
1137 char *s, *b, *label_name;
1138 int err;
1139
1140 b = p;
1141
1142 if (nest_level > MAX_NEST_LEVEL) {
1143 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1144 return -EMLINK;
1145 }
1146
1147 while (1) {
1148 s = p;
1149
1150 get_token(&p, &t, L_KEYWORD);
1151
1152 err = 0;
1153 switch (t.type) {
1154 case T_MENU:
1155 err = parse_menu(&p, cfg, b, nest_level);
1156 break;
1157
1158 case T_TIMEOUT:
1159 err = parse_integer(&p, &cfg->timeout);
1160 break;
1161
1162 case T_LABEL:
1163 err = parse_label(&p, cfg);
1164 break;
1165
1166 case T_DEFAULT:
1167 err = parse_sliteral(&p, &label_name);
1168
1169 if (label_name) {
1170 if (cfg->default_label)
1171 free(cfg->default_label);
1172
1173 cfg->default_label = label_name;
1174 }
1175
1176 break;
1177
Rob Herringf13fec52012-05-25 10:43:16 +00001178 case T_INCLUDE:
1179 err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg,
1180 nest_level + 1);
1181 break;
1182
Jason Hobbs06283a62011-08-31 10:37:30 -05001183 case T_PROMPT:
1184 err = parse_integer(&p, &cfg->prompt);
1185 break;
1186
1187 case T_EOL:
1188 break;
1189
1190 case T_EOF:
1191 return 1;
1192
1193 default:
1194 printf("Ignoring unknown command: %.*s\n",
1195 (int)(p - s), s);
1196 eol_or_eof(&p);
1197 }
1198
1199 if (err < 0)
1200 return err;
1201 }
1202}
1203
1204/*
1205 * Free the memory used by a pxe_menu and its labels.
1206 */
1207static void destroy_pxe_menu(struct pxe_menu *cfg)
1208{
1209 struct list_head *pos, *n;
1210 struct pxe_label *label;
1211
1212 if (cfg->title)
1213 free(cfg->title);
1214
1215 if (cfg->default_label)
1216 free(cfg->default_label);
1217
1218 list_for_each_safe(pos, n, &cfg->labels) {
1219 label = list_entry(pos, struct pxe_label, list);
1220
1221 label_destroy(label);
1222 }
1223
1224 free(cfg);
1225}
1226
1227/*
1228 * Entry point for parsing a pxe file. This is only used for the top level
1229 * file.
1230 *
1231 * Returns NULL if there is an error, otherwise, returns a pointer to a
1232 * pxe_menu struct populated with the results of parsing the pxe file (and any
1233 * files it includes). The resulting pxe_menu struct can be free()'d by using
1234 * the destroy_pxe_menu() function.
1235 */
1236static struct pxe_menu *parse_pxefile(char *menucfg)
1237{
1238 struct pxe_menu *cfg;
1239
1240 cfg = malloc(sizeof(struct pxe_menu));
1241
1242 if (!cfg)
1243 return NULL;
1244
1245 memset(cfg, 0, sizeof(struct pxe_menu));
1246
1247 INIT_LIST_HEAD(&cfg->labels);
1248
1249 if (parse_pxefile_top(menucfg, cfg, 1) < 0) {
1250 destroy_pxe_menu(cfg);
1251 return NULL;
1252 }
1253
1254 return cfg;
1255}
1256
1257/*
1258 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1259 * menu code.
1260 */
1261static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1262{
1263 struct pxe_label *label;
1264 struct list_head *pos;
1265 struct menu *m;
1266 int err;
1267
1268 /*
1269 * Create a menu and add items for all the labels.
1270 */
1271 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print);
1272
1273 if (!m)
1274 return NULL;
1275
1276 list_for_each(pos, &cfg->labels) {
1277 label = list_entry(pos, struct pxe_label, list);
1278
1279 if (menu_item_add(m, label->name, label) != 1) {
1280 menu_destroy(m);
1281 return NULL;
1282 }
1283 }
1284
1285 /*
1286 * After we've created items for each label in the menu, set the
1287 * menu's default label if one was specified.
1288 */
1289 if (cfg->default_label) {
1290 err = menu_default_set(m, cfg->default_label);
1291 if (err != 1) {
1292 if (err != -ENOENT) {
1293 menu_destroy(m);
1294 return NULL;
1295 }
1296
1297 printf("Missing default: %s\n", cfg->default_label);
1298 }
1299 }
1300
1301 return m;
1302}
1303
1304/*
1305 * Try to boot any labels we have yet to attempt to boot.
1306 */
1307static void boot_unattempted_labels(struct pxe_menu *cfg)
1308{
1309 struct list_head *pos;
1310 struct pxe_label *label;
1311
1312 list_for_each(pos, &cfg->labels) {
1313 label = list_entry(pos, struct pxe_label, list);
1314
1315 if (!label->attempted)
1316 label_boot(label);
1317 }
1318}
1319
1320/*
1321 * Boot the system as prescribed by a pxe_menu.
1322 *
1323 * Use the menu system to either get the user's choice or the default, based
1324 * on config or user input. If there is no default or user's choice,
1325 * attempted to boot labels in the order they were given in pxe files.
1326 * If the default or user's choice fails to boot, attempt to boot other
1327 * labels in the order they were given in pxe files.
1328 *
1329 * If this function returns, there weren't any labels that successfully
1330 * booted, or the user interrupted the menu selection via ctrl+c.
1331 */
1332static void handle_pxe_menu(struct pxe_menu *cfg)
1333{
1334 void *choice;
1335 struct menu *m;
1336 int err;
1337
1338 m = pxe_menu_to_menu(cfg);
1339 if (!m)
1340 return;
1341
1342 err = menu_get_choice(m, &choice);
1343
1344 menu_destroy(m);
1345
Jason Hobbs6f40f272011-11-07 03:07:15 +00001346 /*
1347 * err == 1 means we got a choice back from menu_get_choice.
1348 *
1349 * err == -ENOENT if the menu was setup to select the default but no
1350 * default was set. in that case, we should continue trying to boot
1351 * labels that haven't been attempted yet.
1352 *
1353 * otherwise, the user interrupted or there was some other error and
1354 * we give up.
1355 */
Jason Hobbs06283a62011-08-31 10:37:30 -05001356
Jason Hobbs6f40f272011-11-07 03:07:15 +00001357 if (err == 1)
1358 label_boot(choice);
1359 else if (err != -ENOENT)
1360 return;
Jason Hobbs06283a62011-08-31 10:37:30 -05001361
1362 boot_unattempted_labels(cfg);
1363}
1364
1365/*
1366 * Boots a system using a pxe file
1367 *
1368 * Returns 0 on success, 1 on error.
1369 */
1370static int
1371do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1372{
1373 unsigned long pxefile_addr_r;
1374 struct pxe_menu *cfg;
1375 char *pxefile_addr_str;
1376
Rob Herringcfb48a32012-05-25 10:47:39 +00001377 do_getfile = do_get_tftp;
1378
Jason Hobbs06283a62011-08-31 10:37:30 -05001379 if (argc == 1) {
1380 pxefile_addr_str = from_env("pxefile_addr_r");
1381 if (!pxefile_addr_str)
1382 return 1;
1383
1384 } else if (argc == 2) {
1385 pxefile_addr_str = argv[1];
1386 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +00001387 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001388 }
1389
1390 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1391 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1392 return 1;
1393 }
1394
1395 cfg = parse_pxefile((char *)(pxefile_addr_r));
1396
1397 if (cfg == NULL) {
1398 printf("Error parsing config file\n");
1399 return 1;
1400 }
1401
1402 handle_pxe_menu(cfg);
1403
1404 destroy_pxe_menu(cfg);
1405
1406 return 0;
1407}
1408
1409static cmd_tbl_t cmd_pxe_sub[] = {
1410 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1411 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1412};
1413
1414int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1415{
1416 cmd_tbl_t *cp;
1417
1418 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001419 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001420
1421 /* drop initial "pxe" arg */
1422 argc--;
1423 argv++;
1424
1425 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1426
1427 if (cp)
1428 return cp->cmd(cmdtp, flag, argc, argv);
1429
Simon Glass4c12eeb2011-12-10 08:44:01 +00001430 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001431}
1432
1433U_BOOT_CMD(
1434 pxe, 3, 1, do_pxe,
1435 "commands to get and boot from pxe files",
1436 "get - try to retrieve a pxe file using tftp\npxe "
1437 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1438);
Rob Herringcfb48a32012-05-25 10:47:39 +00001439
1440/*
1441 * Boots a system using a local disk syslinux/extlinux file
1442 *
1443 * Returns 0 on success, 1 on error.
1444 */
1445int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1446{
1447 unsigned long pxefile_addr_r;
1448 struct pxe_menu *cfg;
1449 char *pxefile_addr_str;
1450 char *filename;
1451 int prompt = 0;
1452
1453 if (strstr(argv[1], "-p")) {
1454 prompt = 1;
1455 argc--;
1456 argv++;
1457 }
1458
1459 if (argc < 4)
1460 return cmd_usage(cmdtp);
1461
1462 if (argc < 5) {
1463 pxefile_addr_str = from_env("pxefile_addr_r");
1464 if (!pxefile_addr_str)
1465 return 1;
1466 } else {
1467 pxefile_addr_str = argv[4];
1468 }
1469
1470 if (argc < 6)
1471 filename = getenv("bootfile");
1472 else {
1473 filename = argv[5];
1474 setenv("bootfile", filename);
1475 }
1476
1477 if (strstr(argv[3], "ext2"))
1478 do_getfile = do_get_ext2;
1479 else if (strstr(argv[3], "fat"))
1480 do_getfile = do_get_fat;
1481 else {
1482 printf("Invalid filesystem: %s\n", argv[3]);
1483 return 1;
1484 }
1485 fs_argv[1] = argv[1];
1486 fs_argv[2] = argv[2];
1487
1488 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1489 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1490 return 1;
1491 }
1492
1493 if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) {
1494 printf("Error reading config file\n");
1495 return 1;
1496 }
1497
1498 cfg = parse_pxefile((char *)(pxefile_addr_r));
1499
1500 if (cfg == NULL) {
1501 printf("Error parsing config file\n");
1502 return 1;
1503 }
1504
1505 if (prompt)
1506 cfg->prompt = 1;
1507
1508 handle_pxe_menu(cfg);
1509
1510 destroy_pxe_menu(cfg);
1511
1512 return 0;
1513}
1514
1515U_BOOT_CMD(
1516 sysboot, 7, 1, do_sysboot,
1517 "command to get and boot from syslinux files",
1518 "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
1519 " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
1520 " filesystem on 'dev' on 'interface' to address 'addr'"
1521);