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