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