blob: 04094aa7b0c7d9b24ac2285761f2ba2b3ecad4db [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 */
102static int get_bootfile_path(char *bootfile_path, size_t bootfile_path_size)
103{
104 char *bootfile, *last_slash;
105 size_t path_len;
106
107 bootfile = from_env("bootfile");
108
109 if (!bootfile) {
110 bootfile_path[0] = '\0';
111 return 1;
112 }
113
114 last_slash = strrchr(bootfile, '/');
115
116 if (last_slash == NULL) {
117 bootfile_path[0] = '\0';
118 return 1;
119 }
120
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
132 bootfile_path[path_len] = '\0';
133
134 return 1;
135}
136
137/*
138 * As in pxelinux, paths to files referenced from files we retrieve are
139 * relative to the location of bootfile. get_relfile takes such a path and
140 * joins it with the bootfile path to get the full path to the target file. If
141 * the bootfile path is NULL, we use file_path as is.
142 *
143 * Returns 1 for success, or < 0 on error.
144 */
145static int get_relfile(char *file_path, void *file_addr)
146{
147 size_t path_len;
148 char relfile[MAX_TFTP_PATH_LEN+1];
149 char addr_buf[10];
150 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
151 int err;
152
153 err = get_bootfile_path(relfile, sizeof(relfile));
154
155 if (err < 0)
156 return err;
157
158 path_len = strlen(file_path);
159 path_len += strlen(relfile);
160
161 if (path_len > MAX_TFTP_PATH_LEN) {
162 printf("Base path too long (%s%s)\n",
163 relfile,
164 file_path);
165
166 return -ENAMETOOLONG;
167 }
168
169 strcat(relfile, file_path);
170
171 printf("Retrieving file: %s\n", relfile);
172
173 sprintf(addr_buf, "%p", file_addr);
174
175 tftp_argv[1] = addr_buf;
176 tftp_argv[2] = relfile;
177
178 if (do_tftpb(NULL, 0, 3, tftp_argv))
179 return -ENOENT;
180
181 return 1;
182}
183
184/*
185 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
186 * 'bootfile' was specified in the environment, the path to bootfile will be
187 * prepended to 'file_path' and the resulting path will be used.
188 *
189 * Returns 1 on success, or < 0 for error.
190 */
191static int get_pxe_file(char *file_path, void *file_addr)
192{
193 unsigned long config_file_size;
194 char *tftp_filesize;
195 int err;
196
197 err = get_relfile(file_path, file_addr);
198
199 if (err < 0)
200 return err;
201
202 /*
203 * the file comes without a NUL byte at the end, so find out its size
204 * and add the NUL byte.
205 */
206 tftp_filesize = from_env("filesize");
207
208 if (!tftp_filesize)
209 return -ENOENT;
210
211 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
212 return -EINVAL;
213
214 *(char *)(file_addr + config_file_size) = '\0';
215
216 return 1;
217}
218
219#define PXELINUX_DIR "pxelinux.cfg/"
220
221/*
222 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
223 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
224 * from the bootfile path, as described above.
225 *
226 * Returns 1 on success or < 0 on error.
227 */
228static int get_pxelinux_path(char *file, void *pxefile_addr_r)
229{
230 size_t base_len = strlen(PXELINUX_DIR);
231 char path[MAX_TFTP_PATH_LEN+1];
232
233 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
234 printf("path (%s%s) too long, skipping\n",
235 PXELINUX_DIR, file);
236 return -ENAMETOOLONG;
237 }
238
239 sprintf(path, PXELINUX_DIR "%s", file);
240
241 return get_pxe_file(path, pxefile_addr_r);
242}
243
244/*
245 * Looks for a pxe file with a name based on the pxeuuid environment variable.
246 *
247 * Returns 1 on success or < 0 on error.
248 */
249static int pxe_uuid_path(void *pxefile_addr_r)
250{
251 char *uuid_str;
252
253 uuid_str = from_env("pxeuuid");
254
255 if (!uuid_str)
256 return -ENOENT;
257
258 return get_pxelinux_path(uuid_str, pxefile_addr_r);
259}
260
261/*
262 * Looks for a pxe file with a name based on the 'ethaddr' environment
263 * variable.
264 *
265 * Returns 1 on success or < 0 on error.
266 */
267static int pxe_mac_path(void *pxefile_addr_r)
268{
269 char mac_str[21];
270 int err;
271
272 err = format_mac_pxe(mac_str, sizeof(mac_str));
273
274 if (err < 0)
275 return err;
276
277 return get_pxelinux_path(mac_str, pxefile_addr_r);
278}
279
280/*
281 * Looks for pxe files with names based on our IP address. See pxelinux
282 * documentation for details on what these file names look like. We match
283 * that exactly.
284 *
285 * Returns 1 on success or < 0 on error.
286 */
287static int pxe_ipaddr_paths(void *pxefile_addr_r)
288{
289 char ip_addr[9];
290 int mask_pos, err;
291
292 sprintf(ip_addr, "%08X", ntohl(NetOurIP));
293
294 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
295 err = get_pxelinux_path(ip_addr, pxefile_addr_r);
296
297 if (err > 0)
298 return err;
299
300 ip_addr[mask_pos] = '\0';
301 }
302
303 return -ENOENT;
304}
305
306/*
307 * Entry point for the 'pxe get' command.
308 * This Follows pxelinux's rules to download a config file from a tftp server.
309 * The file is stored at the location given by the pxefile_addr_r environment
310 * variable, which must be set.
311 *
312 * UUID comes from pxeuuid env variable, if defined
313 * MAC addr comes from ethaddr env variable, if defined
314 * IP
315 *
316 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
317 *
318 * Returns 0 on success or 1 on error.
319 */
320static int
321do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
322{
323 char *pxefile_addr_str;
Jason Hobbs834c9382012-03-05 08:12:28 +0000324 unsigned long pxefile_addr_r;
Jason Hobbs06283a62011-08-31 10:37:30 -0500325 int err;
326
327 if (argc != 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000328 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500329
330
331 pxefile_addr_str = from_env("pxefile_addr_r");
332
333 if (!pxefile_addr_str)
334 return 1;
335
336 err = strict_strtoul(pxefile_addr_str, 16,
337 (unsigned long *)&pxefile_addr_r);
338 if (err < 0)
339 return 1;
340
341 /*
342 * Keep trying paths until we successfully get a file we're looking
343 * for.
344 */
Jason Hobbs834c9382012-03-05 08:12:28 +0000345 if (pxe_uuid_path((void *)pxefile_addr_r) > 0
346 || pxe_mac_path((void *)pxefile_addr_r) > 0
347 || pxe_ipaddr_paths((void *)pxefile_addr_r) > 0
348 || get_pxelinux_path("default", (void *)pxefile_addr_r) > 0) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500349
350 printf("Config file found\n");
351
352 return 0;
353 }
354
355 printf("Config file not found\n");
356
357 return 1;
358}
359
360/*
361 * Wrapper to make it easier to store the file at file_path in the location
362 * specified by envaddr_name. file_path will be joined to the bootfile path,
363 * if any is specified.
364 *
365 * Returns 1 on success or < 0 on error.
366 */
367static int get_relfile_envaddr(char *file_path, char *envaddr_name)
368{
Jason Hobbs834c9382012-03-05 08:12:28 +0000369 unsigned long file_addr;
Jason Hobbs06283a62011-08-31 10:37:30 -0500370 char *envaddr;
371
372 envaddr = from_env(envaddr_name);
373
374 if (!envaddr)
375 return -ENOENT;
376
Jason Hobbs834c9382012-03-05 08:12:28 +0000377 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
Jason Hobbs06283a62011-08-31 10:37:30 -0500378 return -EINVAL;
379
Jason Hobbs834c9382012-03-05 08:12:28 +0000380 return get_relfile(file_path, (void *)file_addr);
Jason Hobbs06283a62011-08-31 10:37:30 -0500381}
382
383/*
384 * A note on the pxe file parser.
385 *
386 * We're parsing files that use syslinux grammar, which has a few quirks.
387 * String literals must be recognized based on context - there is no
388 * quoting or escaping support. There's also nothing to explicitly indicate
389 * when a label section completes. We deal with that by ending a label
390 * section whenever we see a line that doesn't include.
391 *
392 * As with the syslinux family, this same file format could be reused in the
393 * future for non pxe purposes. The only action it takes during parsing that
394 * would throw this off is handling of include files. It assumes we're using
395 * pxe, and does a tftp download of a file listed as an include file in the
396 * middle of the parsing operation. That could be handled by refactoring it to
397 * take a 'include file getter' function.
398 */
399
400/*
401 * Describes a single label given in a pxe file.
402 *
403 * Create these with the 'label_create' function given below.
404 *
405 * name - the name of the menu as given on the 'menu label' line.
406 * kernel - the path to the kernel file to use for this label.
407 * append - kernel command line to use when booting this label
408 * initrd - path to the initrd to use for this label.
409 * attempted - 0 if we haven't tried to boot this label, 1 if we have.
410 * localboot - 1 if this label specified 'localboot', 0 otherwise.
411 * list - lets these form a list, which a pxe_menu struct will hold.
412 */
413struct pxe_label {
414 char *name;
Rob Herringe539d112012-03-28 05:51:34 +0000415 char *menu;
Jason Hobbs06283a62011-08-31 10:37:30 -0500416 char *kernel;
417 char *append;
418 char *initrd;
419 int attempted;
420 int localboot;
421 struct list_head list;
422};
423
424/*
425 * Describes a pxe menu as given via pxe files.
426 *
427 * title - the name of the menu as given by a 'menu title' line.
428 * default_label - the name of the default label, if any.
429 * timeout - time in tenths of a second to wait for a user key-press before
430 * booting the default label.
431 * prompt - if 0, don't prompt for a choice unless the timeout period is
432 * interrupted. If 1, always prompt for a choice regardless of
433 * timeout.
434 * labels - a list of labels defined for the menu.
435 */
436struct pxe_menu {
437 char *title;
438 char *default_label;
439 int timeout;
440 int prompt;
441 struct list_head labels;
442};
443
444/*
445 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
446 * result must be free()'d to reclaim the memory.
447 *
448 * Returns NULL if malloc fails.
449 */
450static struct pxe_label *label_create(void)
451{
452 struct pxe_label *label;
453
454 label = malloc(sizeof(struct pxe_label));
455
456 if (!label)
457 return NULL;
458
459 memset(label, 0, sizeof(struct pxe_label));
460
461 return label;
462}
463
464/*
465 * Free the memory used by a pxe_label, including that used by its name,
466 * kernel, append and initrd members, if they're non NULL.
467 *
468 * So - be sure to only use dynamically allocated memory for the members of
469 * the pxe_label struct, unless you want to clean it up first. These are
470 * currently only created by the pxe file parsing code.
471 */
472static void label_destroy(struct pxe_label *label)
473{
474 if (label->name)
475 free(label->name);
476
477 if (label->kernel)
478 free(label->kernel);
479
480 if (label->append)
481 free(label->append);
482
483 if (label->initrd)
484 free(label->initrd);
485
486 free(label);
487}
488
489/*
490 * Print a label and its string members if they're defined.
491 *
492 * This is passed as a callback to the menu code for displaying each
493 * menu entry.
494 */
495static void label_print(void *data)
496{
497 struct pxe_label *label = data;
Rob Herringe539d112012-03-28 05:51:34 +0000498 const char *c = label->menu ? label->menu : label->kernel;
Jason Hobbs06283a62011-08-31 10:37:30 -0500499
Rob Herringe539d112012-03-28 05:51:34 +0000500 printf("%s:\t%s\n", label->name, c);
Jason Hobbs06283a62011-08-31 10:37:30 -0500501
502 if (label->kernel)
Rob Herringe539d112012-03-28 05:51:34 +0000503 printf("\t\tkernel: %s\n", label->kernel);
Jason Hobbs06283a62011-08-31 10:37:30 -0500504
505 if (label->append)
Rob Herringe539d112012-03-28 05:51:34 +0000506 printf("\t\tappend: %s\n", label->append);
Jason Hobbs06283a62011-08-31 10:37:30 -0500507
508 if (label->initrd)
Rob Herringe539d112012-03-28 05:51:34 +0000509 printf("\t\tinitrd: %s\n", label->initrd);
Jason Hobbs06283a62011-08-31 10:37:30 -0500510}
511
512/*
513 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
514 * environment variable is defined. Its contents will be executed as U-boot
515 * command. If the label specified an 'append' line, its contents will be
516 * used to overwrite the contents of the 'bootargs' environment variable prior
517 * to running 'localcmd'.
518 *
519 * Returns 1 on success or < 0 on error.
520 */
521static int label_localboot(struct pxe_label *label)
522{
523 char *localcmd, *dupcmd;
524 int ret;
525
526 localcmd = from_env("localcmd");
527
528 if (!localcmd)
529 return -ENOENT;
530
531 /*
532 * dup the command to avoid any issues with the version of it existing
533 * in the environment changing during the execution of the command.
534 */
535 dupcmd = strdup(localcmd);
536
537 if (!dupcmd)
538 return -ENOMEM;
539
540 if (label->append)
541 setenv("bootargs", label->append);
542
543 printf("running: %s\n", dupcmd);
544
Simon Glass009dde12012-02-14 19:59:20 +0000545 ret = run_command(dupcmd, 0);
Jason Hobbs06283a62011-08-31 10:37:30 -0500546
547 free(dupcmd);
548
549 return ret;
550}
551
552/*
553 * Boot according to the contents of a pxe_label.
554 *
555 * If we can't boot for any reason, we return. A successful boot never
556 * returns.
557 *
558 * The kernel will be stored in the location given by the 'kernel_addr_r'
559 * environment variable.
560 *
561 * If the label specifies an initrd file, it will be stored in the location
562 * given by the 'ramdisk_addr_r' environment variable.
563 *
564 * If the label specifies an 'append' line, its contents will overwrite that
565 * of the 'bootargs' environment variable.
566 */
567static void label_boot(struct pxe_label *label)
568{
569 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
570 int bootm_argc = 3;
571
572 label_print(label);
573
574 label->attempted = 1;
575
576 if (label->localboot) {
577 label_localboot(label);
578 return;
579 }
580
581 if (label->kernel == NULL) {
582 printf("No kernel given, skipping %s\n",
583 label->name);
584 return;
585 }
586
587 if (label->initrd) {
588 if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) {
589 printf("Skipping %s for failure retrieving initrd\n",
590 label->name);
591 return;
592 }
593
594 bootm_argv[2] = getenv("ramdisk_addr_r");
595 } else {
596 bootm_argv[2] = "-";
597 }
598
599 if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) {
600 printf("Skipping %s for failure retrieving kernel\n",
601 label->name);
602 return;
603 }
604
605 if (label->append)
606 setenv("bootargs", label->append);
607
608 bootm_argv[1] = getenv("kernel_addr_r");
609
610 /*
611 * fdt usage is optional. If there is an fdt_addr specified, we will
612 * pass it along to bootm, and adjust argc appropriately.
613 */
614 bootm_argv[3] = getenv("fdt_addr");
615
616 if (bootm_argv[3])
617 bootm_argc = 4;
618
619 do_bootm(NULL, 0, bootm_argc, bootm_argv);
620}
621
622/*
623 * Tokens for the pxe file parser.
624 */
625enum token_type {
626 T_EOL,
627 T_STRING,
628 T_EOF,
629 T_MENU,
630 T_TITLE,
631 T_TIMEOUT,
632 T_LABEL,
633 T_KERNEL,
634 T_APPEND,
635 T_INITRD,
636 T_LOCALBOOT,
637 T_DEFAULT,
638 T_PROMPT,
639 T_INCLUDE,
640 T_INVALID
641};
642
643/*
644 * A token - given by a value and a type.
645 */
646struct token {
647 char *val;
648 enum token_type type;
649};
650
651/*
652 * Keywords recognized.
653 */
654static const struct token keywords[] = {
655 {"menu", T_MENU},
656 {"title", T_TITLE},
657 {"timeout", T_TIMEOUT},
658 {"default", T_DEFAULT},
659 {"prompt", T_PROMPT},
660 {"label", T_LABEL},
661 {"kernel", T_KERNEL},
662 {"localboot", T_LOCALBOOT},
663 {"append", T_APPEND},
664 {"initrd", T_INITRD},
665 {"include", T_INCLUDE},
666 {NULL, T_INVALID}
667};
668
669/*
670 * Since pxe(linux) files don't have a token to identify the start of a
671 * literal, we have to keep track of when we're in a state where a literal is
672 * expected vs when we're in a state a keyword is expected.
673 */
674enum lex_state {
675 L_NORMAL = 0,
676 L_KEYWORD,
677 L_SLITERAL
678};
679
680/*
681 * get_string retrieves a string from *p and stores it as a token in
682 * *t.
683 *
684 * get_string used for scanning both string literals and keywords.
685 *
686 * Characters from *p are copied into t-val until a character equal to
687 * delim is found, or a NUL byte is reached. If delim has the special value of
688 * ' ', any whitespace character will be used as a delimiter.
689 *
690 * If lower is unequal to 0, uppercase characters will be converted to
691 * lowercase in the result. This is useful to make keywords case
692 * insensitive.
693 *
694 * The location of *p is updated to point to the first character after the end
695 * of the token - the ending delimiter.
696 *
697 * On success, the new value of t->val is returned. Memory for t->val is
698 * allocated using malloc and must be free()'d to reclaim it. If insufficient
699 * memory is available, NULL is returned.
700 */
701static char *get_string(char **p, struct token *t, char delim, int lower)
702{
703 char *b, *e;
704 size_t len, i;
705
706 /*
707 * b and e both start at the beginning of the input stream.
708 *
709 * e is incremented until we find the ending delimiter, or a NUL byte
710 * is reached. Then, we take e - b to find the length of the token.
711 */
712 b = e = *p;
713
714 while (*e) {
715 if ((delim == ' ' && isspace(*e)) || delim == *e)
716 break;
717 e++;
718 }
719
720 len = e - b;
721
722 /*
723 * Allocate memory to hold the string, and copy it in, converting
724 * characters to lowercase if lower is != 0.
725 */
726 t->val = malloc(len + 1);
727 if (!t->val)
728 return NULL;
729
730 for (i = 0; i < len; i++, b++) {
731 if (lower)
732 t->val[i] = tolower(*b);
733 else
734 t->val[i] = *b;
735 }
736
737 t->val[len] = '\0';
738
739 /*
740 * Update *p so the caller knows where to continue scanning.
741 */
742 *p = e;
743
744 t->type = T_STRING;
745
746 return t->val;
747}
748
749/*
750 * Populate a keyword token with a type and value.
751 */
752static void get_keyword(struct token *t)
753{
754 int i;
755
756 for (i = 0; keywords[i].val; i++) {
757 if (!strcmp(t->val, keywords[i].val)) {
758 t->type = keywords[i].type;
759 break;
760 }
761 }
762}
763
764/*
765 * Get the next token. We have to keep track of which state we're in to know
766 * if we're looking to get a string literal or a keyword.
767 *
768 * *p is updated to point at the first character after the current token.
769 */
770static void get_token(char **p, struct token *t, enum lex_state state)
771{
772 char *c = *p;
773
774 t->type = T_INVALID;
775
776 /* eat non EOL whitespace */
777 while (isblank(*c))
778 c++;
779
780 /*
781 * eat comments. note that string literals can't begin with #, but
782 * can contain a # after their first character.
783 */
784 if (*c == '#') {
785 while (*c && *c != '\n')
786 c++;
787 }
788
789 if (*c == '\n') {
790 t->type = T_EOL;
791 c++;
792 } else if (*c == '\0') {
793 t->type = T_EOF;
794 c++;
795 } else if (state == L_SLITERAL) {
796 get_string(&c, t, '\n', 0);
797 } else if (state == L_KEYWORD) {
798 /*
799 * when we expect a keyword, we first get the next string
800 * token delimited by whitespace, and then check if it
801 * matches a keyword in our keyword list. if it does, it's
802 * converted to a keyword token of the appropriate type, and
803 * if not, it remains a string token.
804 */
805 get_string(&c, t, ' ', 1);
806 get_keyword(t);
807 }
808
809 *p = c;
810}
811
812/*
813 * Increment *c until we get to the end of the current line, or EOF.
814 */
815static void eol_or_eof(char **c)
816{
817 while (**c && **c != '\n')
818 (*c)++;
819}
820
821/*
822 * All of these parse_* functions share some common behavior.
823 *
824 * They finish with *c pointing after the token they parse, and return 1 on
825 * success, or < 0 on error.
826 */
827
828/*
829 * Parse a string literal and store a pointer it at *dst. String literals
830 * terminate at the end of the line.
831 */
832static int parse_sliteral(char **c, char **dst)
833{
834 struct token t;
835 char *s = *c;
836
837 get_token(c, &t, L_SLITERAL);
838
839 if (t.type != T_STRING) {
840 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
841 return -EINVAL;
842 }
843
844 *dst = t.val;
845
846 return 1;
847}
848
849/*
850 * Parse a base 10 (unsigned) integer and store it at *dst.
851 */
852static int parse_integer(char **c, int *dst)
853{
854 struct token t;
855 char *s = *c;
856 unsigned long temp;
857
858 get_token(c, &t, L_SLITERAL);
859
860 if (t.type != T_STRING) {
861 printf("Expected string: %.*s\n", (int)(*c - s), s);
862 return -EINVAL;
863 }
864
865 if (strict_strtoul(t.val, 10, &temp) < 0) {
866 printf("Expected unsigned integer: %s\n", t.val);
867 return -EINVAL;
868 }
869
870 *dst = (int)temp;
871
872 free(t.val);
873
874 return 1;
875}
876
877static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
878
879/*
880 * Parse an include statement, and retrieve and parse the file it mentions.
881 *
882 * base should point to a location where it's safe to store the file, and
883 * nest_level should indicate how many nested includes have occurred. For this
884 * include, nest_level has already been incremented and doesn't need to be
885 * incremented here.
886 */
887static int handle_include(char **c, char *base,
888 struct pxe_menu *cfg, int nest_level)
889{
890 char *include_path;
891 char *s = *c;
892 int err;
893
894 err = parse_sliteral(c, &include_path);
895
896 if (err < 0) {
897 printf("Expected include path: %.*s\n",
898 (int)(*c - s), s);
899 return err;
900 }
901
902 err = get_pxe_file(include_path, base);
903
904 if (err < 0) {
905 printf("Couldn't retrieve %s\n", include_path);
906 return err;
907 }
908
909 return parse_pxefile_top(base, cfg, nest_level);
910}
911
912/*
913 * Parse lines that begin with 'menu'.
914 *
915 * b and nest are provided to handle the 'menu include' case.
916 *
917 * b should be the address where the file currently being parsed is stored.
918 *
919 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
920 * a file it includes, 3 when parsing a file included by that file, and so on.
921 */
922static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
923{
924 struct token t;
925 char *s = *c;
Heiko Schocher43d4a5e2011-12-12 20:37:17 +0000926 int err = 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500927
928 get_token(c, &t, L_KEYWORD);
929
930 switch (t.type) {
931 case T_TITLE:
932 err = parse_sliteral(c, &cfg->title);
933
934 break;
935
936 case T_INCLUDE:
937 err = handle_include(c, b + strlen(b) + 1, cfg,
938 nest_level + 1);
939 break;
940
941 default:
942 printf("Ignoring malformed menu command: %.*s\n",
943 (int)(*c - s), s);
944 }
945
946 if (err < 0)
947 return err;
948
949 eol_or_eof(c);
950
951 return 1;
952}
953
954/*
955 * Handles parsing a 'menu line' when we're parsing a label.
956 */
957static int parse_label_menu(char **c, struct pxe_menu *cfg,
958 struct pxe_label *label)
959{
960 struct token t;
961 char *s;
962
963 s = *c;
964
965 get_token(c, &t, L_KEYWORD);
966
967 switch (t.type) {
968 case T_DEFAULT:
969 if (cfg->default_label)
970 free(cfg->default_label);
971
972 cfg->default_label = strdup(label->name);
973
974 if (!cfg->default_label)
975 return -ENOMEM;
976
977 break;
Rob Herringe539d112012-03-28 05:51:34 +0000978 case T_LABEL:
979 parse_sliteral(c, &label->menu);
980 break;
Jason Hobbs06283a62011-08-31 10:37:30 -0500981 default:
982 printf("Ignoring malformed menu command: %.*s\n",
983 (int)(*c - s), s);
984 }
985
986 eol_or_eof(c);
987
988 return 0;
989}
990
991/*
992 * Parses a label and adds it to the list of labels for a menu.
993 *
994 * A label ends when we either get to the end of a file, or
995 * get some input we otherwise don't have a handler defined
996 * for.
997 *
998 */
999static int parse_label(char **c, struct pxe_menu *cfg)
1000{
1001 struct token t;
1002 char *s = *c;
1003 struct pxe_label *label;
1004 int err;
1005
1006 label = label_create();
1007 if (!label)
1008 return -ENOMEM;
1009
1010 err = parse_sliteral(c, &label->name);
1011 if (err < 0) {
1012 printf("Expected label name: %.*s\n", (int)(*c - s), s);
1013 label_destroy(label);
1014 return -EINVAL;
1015 }
1016
1017 list_add_tail(&label->list, &cfg->labels);
1018
1019 while (1) {
1020 s = *c;
1021 get_token(c, &t, L_KEYWORD);
1022
1023 err = 0;
1024 switch (t.type) {
1025 case T_MENU:
1026 err = parse_label_menu(c, cfg, label);
1027 break;
1028
1029 case T_KERNEL:
1030 err = parse_sliteral(c, &label->kernel);
1031 break;
1032
1033 case T_APPEND:
1034 err = parse_sliteral(c, &label->append);
1035 break;
1036
1037 case T_INITRD:
1038 err = parse_sliteral(c, &label->initrd);
1039 break;
1040
1041 case T_LOCALBOOT:
1042 err = parse_integer(c, &label->localboot);
1043 break;
1044
1045 case T_EOL:
1046 break;
1047
1048 default:
1049 /*
1050 * put the token back! we don't want it - it's the end
1051 * of a label and whatever token this is, it's
1052 * something for the menu level context to handle.
1053 */
1054 *c = s;
1055 return 1;
1056 }
1057
1058 if (err < 0)
1059 return err;
1060 }
1061}
1062
1063/*
1064 * This 16 comes from the limit pxelinux imposes on nested includes.
1065 *
1066 * There is no reason at all we couldn't do more, but some limit helps prevent
1067 * infinite (until crash occurs) recursion if a file tries to include itself.
1068 */
1069#define MAX_NEST_LEVEL 16
1070
1071/*
1072 * Entry point for parsing a menu file. nest_level indicates how many times
1073 * we've nested in includes. It will be 1 for the top level menu file.
1074 *
1075 * Returns 1 on success, < 0 on error.
1076 */
1077static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level)
1078{
1079 struct token t;
1080 char *s, *b, *label_name;
1081 int err;
1082
1083 b = p;
1084
1085 if (nest_level > MAX_NEST_LEVEL) {
1086 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1087 return -EMLINK;
1088 }
1089
1090 while (1) {
1091 s = p;
1092
1093 get_token(&p, &t, L_KEYWORD);
1094
1095 err = 0;
1096 switch (t.type) {
1097 case T_MENU:
1098 err = parse_menu(&p, cfg, b, nest_level);
1099 break;
1100
1101 case T_TIMEOUT:
1102 err = parse_integer(&p, &cfg->timeout);
1103 break;
1104
1105 case T_LABEL:
1106 err = parse_label(&p, cfg);
1107 break;
1108
1109 case T_DEFAULT:
1110 err = parse_sliteral(&p, &label_name);
1111
1112 if (label_name) {
1113 if (cfg->default_label)
1114 free(cfg->default_label);
1115
1116 cfg->default_label = label_name;
1117 }
1118
1119 break;
1120
Rob Herringf13fec52012-05-25 10:43:16 +00001121 case T_INCLUDE:
1122 err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg,
1123 nest_level + 1);
1124 break;
1125
Jason Hobbs06283a62011-08-31 10:37:30 -05001126 case T_PROMPT:
1127 err = parse_integer(&p, &cfg->prompt);
1128 break;
1129
1130 case T_EOL:
1131 break;
1132
1133 case T_EOF:
1134 return 1;
1135
1136 default:
1137 printf("Ignoring unknown command: %.*s\n",
1138 (int)(p - s), s);
1139 eol_or_eof(&p);
1140 }
1141
1142 if (err < 0)
1143 return err;
1144 }
1145}
1146
1147/*
1148 * Free the memory used by a pxe_menu and its labels.
1149 */
1150static void destroy_pxe_menu(struct pxe_menu *cfg)
1151{
1152 struct list_head *pos, *n;
1153 struct pxe_label *label;
1154
1155 if (cfg->title)
1156 free(cfg->title);
1157
1158 if (cfg->default_label)
1159 free(cfg->default_label);
1160
1161 list_for_each_safe(pos, n, &cfg->labels) {
1162 label = list_entry(pos, struct pxe_label, list);
1163
1164 label_destroy(label);
1165 }
1166
1167 free(cfg);
1168}
1169
1170/*
1171 * Entry point for parsing a pxe file. This is only used for the top level
1172 * file.
1173 *
1174 * Returns NULL if there is an error, otherwise, returns a pointer to a
1175 * pxe_menu struct populated with the results of parsing the pxe file (and any
1176 * files it includes). The resulting pxe_menu struct can be free()'d by using
1177 * the destroy_pxe_menu() function.
1178 */
1179static struct pxe_menu *parse_pxefile(char *menucfg)
1180{
1181 struct pxe_menu *cfg;
1182
1183 cfg = malloc(sizeof(struct pxe_menu));
1184
1185 if (!cfg)
1186 return NULL;
1187
1188 memset(cfg, 0, sizeof(struct pxe_menu));
1189
1190 INIT_LIST_HEAD(&cfg->labels);
1191
1192 if (parse_pxefile_top(menucfg, cfg, 1) < 0) {
1193 destroy_pxe_menu(cfg);
1194 return NULL;
1195 }
1196
1197 return cfg;
1198}
1199
1200/*
1201 * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
1202 * menu code.
1203 */
1204static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1205{
1206 struct pxe_label *label;
1207 struct list_head *pos;
1208 struct menu *m;
1209 int err;
1210
1211 /*
1212 * Create a menu and add items for all the labels.
1213 */
1214 m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print);
1215
1216 if (!m)
1217 return NULL;
1218
1219 list_for_each(pos, &cfg->labels) {
1220 label = list_entry(pos, struct pxe_label, list);
1221
1222 if (menu_item_add(m, label->name, label) != 1) {
1223 menu_destroy(m);
1224 return NULL;
1225 }
1226 }
1227
1228 /*
1229 * After we've created items for each label in the menu, set the
1230 * menu's default label if one was specified.
1231 */
1232 if (cfg->default_label) {
1233 err = menu_default_set(m, cfg->default_label);
1234 if (err != 1) {
1235 if (err != -ENOENT) {
1236 menu_destroy(m);
1237 return NULL;
1238 }
1239
1240 printf("Missing default: %s\n", cfg->default_label);
1241 }
1242 }
1243
1244 return m;
1245}
1246
1247/*
1248 * Try to boot any labels we have yet to attempt to boot.
1249 */
1250static void boot_unattempted_labels(struct pxe_menu *cfg)
1251{
1252 struct list_head *pos;
1253 struct pxe_label *label;
1254
1255 list_for_each(pos, &cfg->labels) {
1256 label = list_entry(pos, struct pxe_label, list);
1257
1258 if (!label->attempted)
1259 label_boot(label);
1260 }
1261}
1262
1263/*
1264 * Boot the system as prescribed by a pxe_menu.
1265 *
1266 * Use the menu system to either get the user's choice or the default, based
1267 * on config or user input. If there is no default or user's choice,
1268 * attempted to boot labels in the order they were given in pxe files.
1269 * If the default or user's choice fails to boot, attempt to boot other
1270 * labels in the order they were given in pxe files.
1271 *
1272 * If this function returns, there weren't any labels that successfully
1273 * booted, or the user interrupted the menu selection via ctrl+c.
1274 */
1275static void handle_pxe_menu(struct pxe_menu *cfg)
1276{
1277 void *choice;
1278 struct menu *m;
1279 int err;
1280
1281 m = pxe_menu_to_menu(cfg);
1282 if (!m)
1283 return;
1284
1285 err = menu_get_choice(m, &choice);
1286
1287 menu_destroy(m);
1288
Jason Hobbs6f40f272011-11-07 03:07:15 +00001289 /*
1290 * err == 1 means we got a choice back from menu_get_choice.
1291 *
1292 * err == -ENOENT if the menu was setup to select the default but no
1293 * default was set. in that case, we should continue trying to boot
1294 * labels that haven't been attempted yet.
1295 *
1296 * otherwise, the user interrupted or there was some other error and
1297 * we give up.
1298 */
Jason Hobbs06283a62011-08-31 10:37:30 -05001299
Jason Hobbs6f40f272011-11-07 03:07:15 +00001300 if (err == 1)
1301 label_boot(choice);
1302 else if (err != -ENOENT)
1303 return;
Jason Hobbs06283a62011-08-31 10:37:30 -05001304
1305 boot_unattempted_labels(cfg);
1306}
1307
1308/*
1309 * Boots a system using a pxe file
1310 *
1311 * Returns 0 on success, 1 on error.
1312 */
1313static int
1314do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1315{
1316 unsigned long pxefile_addr_r;
1317 struct pxe_menu *cfg;
1318 char *pxefile_addr_str;
1319
1320 if (argc == 1) {
1321 pxefile_addr_str = from_env("pxefile_addr_r");
1322 if (!pxefile_addr_str)
1323 return 1;
1324
1325 } else if (argc == 2) {
1326 pxefile_addr_str = argv[1];
1327 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +00001328 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001329 }
1330
1331 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
1332 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
1333 return 1;
1334 }
1335
1336 cfg = parse_pxefile((char *)(pxefile_addr_r));
1337
1338 if (cfg == NULL) {
1339 printf("Error parsing config file\n");
1340 return 1;
1341 }
1342
1343 handle_pxe_menu(cfg);
1344
1345 destroy_pxe_menu(cfg);
1346
1347 return 0;
1348}
1349
1350static cmd_tbl_t cmd_pxe_sub[] = {
1351 U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
1352 U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
1353};
1354
1355int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1356{
1357 cmd_tbl_t *cp;
1358
1359 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001360 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001361
1362 /* drop initial "pxe" arg */
1363 argc--;
1364 argv++;
1365
1366 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
1367
1368 if (cp)
1369 return cp->cmd(cmdtp, flag, argc, argv);
1370
Simon Glass4c12eeb2011-12-10 08:44:01 +00001371 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -05001372}
1373
1374U_BOOT_CMD(
1375 pxe, 3, 1, do_pxe,
1376 "commands to get and boot from pxe files",
1377 "get - try to retrieve a pxe file using tftp\npxe "
1378 "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
1379);