blob: 53af04d7dc75bdef091c34f7d94c7d97ace60777 [file] [log] [blame]
Patrice Chotard17e88042019-11-25 09:07:37 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2010-2011 Calxeda, Inc.
4 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
5 */
6
7#include <common.h>
8#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -07009#include <image.h>
Patrice Chotard17e88042019-11-25 09:07:37 +010010#include <malloc.h>
11#include <mapmem.h>
12#include <lcd.h>
13#include <linux/string.h>
14#include <linux/ctype.h>
15#include <errno.h>
16#include <linux/list.h>
17
18#include <splash.h>
19#include <asm/io.h>
20
21#include "menu.h"
22#include "cli.h"
23
24#include "pxe_utils.h"
25
Ben Wolsieffer6273f132019-11-28 00:07:08 -050026#define MAX_TFTP_PATH_LEN 512
Patrice Chotard17e88042019-11-25 09:07:37 +010027
28bool is_pxe;
29
30/*
31 * Convert an ethaddr from the environment to the format used by pxelinux
32 * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
33 * the beginning of the ethernet address to indicate a hardware type of
34 * Ethernet. Also converts uppercase hex characters into lowercase, to match
35 * pxelinux's behavior.
36 *
37 * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
38 * environment, or some other value < 0 on error.
39 */
40int format_mac_pxe(char *outbuf, size_t outbuf_len)
41{
42 uchar ethaddr[6];
43
44 if (outbuf_len < 21) {
45 printf("outbuf is too small (%zd < 21)\n", outbuf_len);
46
47 return -EINVAL;
48 }
49
50 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
51 return -ENOENT;
52
53 sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
54 ethaddr[0], ethaddr[1], ethaddr[2],
55 ethaddr[3], ethaddr[4], ethaddr[5]);
56
57 return 1;
58}
59
60/*
61 * Returns the directory the file specified in the bootfile env variable is
62 * in. If bootfile isn't defined in the environment, return NULL, which should
63 * be interpreted as "don't prepend anything to paths".
64 */
65static int get_bootfile_path(const char *file_path, char *bootfile_path,
66 size_t bootfile_path_size)
67{
68 char *bootfile, *last_slash;
69 size_t path_len = 0;
70
71 /* Only syslinux allows absolute paths */
72 if (file_path[0] == '/' && !is_pxe)
73 goto ret;
74
75 bootfile = from_env("bootfile");
76
77 if (!bootfile)
78 goto ret;
79
80 last_slash = strrchr(bootfile, '/');
81
Patrice Chotard6233de42019-11-25 09:07:39 +010082 if (!last_slash)
Patrice Chotard17e88042019-11-25 09:07:37 +010083 goto ret;
84
85 path_len = (last_slash - bootfile) + 1;
86
87 if (bootfile_path_size < path_len) {
88 printf("bootfile_path too small. (%zd < %zd)\n",
Patrice Chotard6233de42019-11-25 09:07:39 +010089 bootfile_path_size, path_len);
Patrice Chotard17e88042019-11-25 09:07:37 +010090
91 return -1;
92 }
93
94 strncpy(bootfile_path, bootfile, path_len);
95
96 ret:
97 bootfile_path[path_len] = '\0';
98
99 return 1;
100}
101
102int (*do_getfile)(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr);
103
104/*
105 * As in pxelinux, paths to files referenced from files we retrieve are
106 * relative to the location of bootfile. get_relfile takes such a path and
107 * joins it with the bootfile path to get the full path to the target file. If
108 * the bootfile path is NULL, we use file_path as is.
109 *
110 * Returns 1 for success, or < 0 on error.
111 */
112static int get_relfile(cmd_tbl_t *cmdtp, const char *file_path,
Patrice Chotard6233de42019-11-25 09:07:39 +0100113 unsigned long file_addr)
Patrice Chotard17e88042019-11-25 09:07:37 +0100114{
115 size_t path_len;
Patrice Chotard6233de42019-11-25 09:07:39 +0100116 char relfile[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +0100117 char addr_buf[18];
118 int err;
119
120 err = get_bootfile_path(file_path, relfile, sizeof(relfile));
121
122 if (err < 0)
123 return err;
124
125 path_len = strlen(file_path);
126 path_len += strlen(relfile);
127
128 if (path_len > MAX_TFTP_PATH_LEN) {
Patrice Chotard6233de42019-11-25 09:07:39 +0100129 printf("Base path too long (%s%s)\n", relfile, file_path);
Patrice Chotard17e88042019-11-25 09:07:37 +0100130
131 return -ENAMETOOLONG;
132 }
133
134 strcat(relfile, file_path);
135
136 printf("Retrieving file: %s\n", relfile);
137
138 sprintf(addr_buf, "%lx", file_addr);
139
140 return do_getfile(cmdtp, relfile, addr_buf);
141}
142
143/*
144 * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
145 * 'bootfile' was specified in the environment, the path to bootfile will be
146 * prepended to 'file_path' and the resulting path will be used.
147 *
148 * Returns 1 on success, or < 0 for error.
149 */
150int get_pxe_file(cmd_tbl_t *cmdtp, const char *file_path,
Patrice Chotard6233de42019-11-25 09:07:39 +0100151 unsigned long file_addr)
Patrice Chotard17e88042019-11-25 09:07:37 +0100152{
153 unsigned long config_file_size;
154 char *tftp_filesize;
155 int err;
156 char *buf;
157
158 err = get_relfile(cmdtp, file_path, file_addr);
159
160 if (err < 0)
161 return err;
162
163 /*
164 * the file comes without a NUL byte at the end, so find out its size
165 * and add the NUL byte.
166 */
167 tftp_filesize = from_env("filesize");
168
169 if (!tftp_filesize)
170 return -ENOENT;
171
172 if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
173 return -EINVAL;
174
175 buf = map_sysmem(file_addr + config_file_size, 1);
176 *buf = '\0';
177 unmap_sysmem(buf);
178
179 return 1;
180}
181
182#define PXELINUX_DIR "pxelinux.cfg/"
183
Patrice Chotard17e88042019-11-25 09:07:37 +0100184/*
185 * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
186 * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
187 * from the bootfile path, as described above.
188 *
189 * Returns 1 on success or < 0 on error.
190 */
191int get_pxelinux_path(cmd_tbl_t *cmdtp, const char *file,
Patrice Chotard6233de42019-11-25 09:07:39 +0100192 unsigned long pxefile_addr_r)
Patrice Chotard17e88042019-11-25 09:07:37 +0100193{
194 size_t base_len = strlen(PXELINUX_DIR);
Patrice Chotard6233de42019-11-25 09:07:39 +0100195 char path[MAX_TFTP_PATH_LEN + 1];
Patrice Chotard17e88042019-11-25 09:07:37 +0100196
197 if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
198 printf("path (%s%s) too long, skipping\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100199 PXELINUX_DIR, file);
Patrice Chotard17e88042019-11-25 09:07:37 +0100200 return -ENAMETOOLONG;
201 }
202
203 sprintf(path, PXELINUX_DIR "%s", file);
204
205 return get_pxe_file(cmdtp, path, pxefile_addr_r);
206}
207
208/*
209 * Wrapper to make it easier to store the file at file_path in the location
210 * specified by envaddr_name. file_path will be joined to the bootfile path,
211 * if any is specified.
212 *
213 * Returns 1 on success or < 0 on error.
214 */
Patrice Chotard6233de42019-11-25 09:07:39 +0100215static int get_relfile_envaddr(cmd_tbl_t *cmdtp, const char *file_path,
216 const char *envaddr_name)
Patrice Chotard17e88042019-11-25 09:07:37 +0100217{
218 unsigned long file_addr;
219 char *envaddr;
220
221 envaddr = from_env(envaddr_name);
222
223 if (!envaddr)
224 return -ENOENT;
225
226 if (strict_strtoul(envaddr, 16, &file_addr) < 0)
227 return -EINVAL;
228
229 return get_relfile(cmdtp, file_path, file_addr);
230}
231
232/*
233 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
234 * result must be free()'d to reclaim the memory.
235 *
236 * Returns NULL if malloc fails.
237 */
238static struct pxe_label *label_create(void)
239{
240 struct pxe_label *label;
241
242 label = malloc(sizeof(struct pxe_label));
243
244 if (!label)
245 return NULL;
246
247 memset(label, 0, sizeof(struct pxe_label));
248
249 return label;
250}
251
252/*
253 * Free the memory used by a pxe_label, including that used by its name,
254 * kernel, append and initrd members, if they're non NULL.
255 *
256 * So - be sure to only use dynamically allocated memory for the members of
257 * the pxe_label struct, unless you want to clean it up first. These are
258 * currently only created by the pxe file parsing code.
259 */
260static void label_destroy(struct pxe_label *label)
261{
262 if (label->name)
263 free(label->name);
264
265 if (label->kernel)
266 free(label->kernel);
267
268 if (label->config)
269 free(label->config);
270
271 if (label->append)
272 free(label->append);
273
274 if (label->initrd)
275 free(label->initrd);
276
277 if (label->fdt)
278 free(label->fdt);
279
280 if (label->fdtdir)
281 free(label->fdtdir);
282
283 free(label);
284}
285
286/*
287 * Print a label and its string members if they're defined.
288 *
289 * This is passed as a callback to the menu code for displaying each
290 * menu entry.
291 */
292static void label_print(void *data)
293{
294 struct pxe_label *label = data;
295 const char *c = label->menu ? label->menu : label->name;
296
297 printf("%s:\t%s\n", label->num, c);
298}
299
300/*
301 * Boot a label that specified 'localboot'. This requires that the 'localcmd'
302 * environment variable is defined. Its contents will be executed as U-Boot
303 * command. If the label specified an 'append' line, its contents will be
304 * used to overwrite the contents of the 'bootargs' environment variable prior
305 * to running 'localcmd'.
306 *
307 * Returns 1 on success or < 0 on error.
308 */
309static int label_localboot(struct pxe_label *label)
310{
311 char *localcmd;
312
313 localcmd = from_env("localcmd");
314
315 if (!localcmd)
316 return -ENOENT;
317
318 if (label->append) {
319 char bootargs[CONFIG_SYS_CBSIZE];
320
321 cli_simple_process_macros(label->append, bootargs);
322 env_set("bootargs", bootargs);
323 }
324
325 debug("running: %s\n", localcmd);
326
327 return run_command_list(localcmd, strlen(localcmd), 0);
328}
329
330/*
331 * Boot according to the contents of a pxe_label.
332 *
333 * If we can't boot for any reason, we return. A successful boot never
334 * returns.
335 *
336 * The kernel will be stored in the location given by the 'kernel_addr_r'
337 * environment variable.
338 *
339 * If the label specifies an initrd file, it will be stored in the location
340 * given by the 'ramdisk_addr_r' environment variable.
341 *
342 * If the label specifies an 'append' line, its contents will overwrite that
343 * of the 'bootargs' environment variable.
344 */
345static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
346{
347 char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
348 char initrd_str[28];
349 char mac_str[29] = "";
350 char ip_str[68] = "";
351 char *fit_addr = NULL;
352 int bootm_argc = 2;
353 int len = 0;
354 ulong kernel_addr;
355 void *buf;
356
357 label_print(label);
358
359 label->attempted = 1;
360
361 if (label->localboot) {
362 if (label->localboot_val >= 0)
363 label_localboot(label);
364 return 0;
365 }
366
Patrice Chotard6233de42019-11-25 09:07:39 +0100367 if (!label->kernel) {
Patrice Chotard17e88042019-11-25 09:07:37 +0100368 printf("No kernel given, skipping %s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100369 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100370 return 1;
371 }
372
373 if (label->initrd) {
374 if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
375 printf("Skipping %s for failure retrieving initrd\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100376 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100377 return 1;
378 }
379
380 bootm_argv[2] = initrd_str;
381 strncpy(bootm_argv[2], env_get("ramdisk_addr_r"), 18);
382 strcat(bootm_argv[2], ":");
383 strncat(bootm_argv[2], env_get("filesize"), 9);
384 bootm_argc = 3;
385 }
386
387 if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
388 printf("Skipping %s for failure retrieving kernel\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100389 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100390 return 1;
391 }
392
393 if (label->ipappend & 0x1) {
394 sprintf(ip_str, " ip=%s:%s:%s:%s",
395 env_get("ipaddr"), env_get("serverip"),
396 env_get("gatewayip"), env_get("netmask"));
397 }
398
399#ifdef CONFIG_CMD_NET
400 if (label->ipappend & 0x2) {
401 int err;
Patrice Chotard6233de42019-11-25 09:07:39 +0100402
Patrice Chotard17e88042019-11-25 09:07:37 +0100403 strcpy(mac_str, " BOOTIF=");
404 err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
405 if (err < 0)
406 mac_str[0] = '\0';
407 }
408#endif
409
410 if ((label->ipappend & 0x3) || label->append) {
411 char bootargs[CONFIG_SYS_CBSIZE] = "";
412 char finalbootargs[CONFIG_SYS_CBSIZE];
413
414 if (strlen(label->append ?: "") +
415 strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
416 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
417 strlen(label->append ?: ""),
418 strlen(ip_str), strlen(mac_str),
419 sizeof(bootargs));
420 return 1;
Patrice Chotard17e88042019-11-25 09:07:37 +0100421 }
Patrice Chotard6233de42019-11-25 09:07:39 +0100422
423 if (label->append)
424 strncpy(bootargs, label->append, sizeof(bootargs));
425
426 strcat(bootargs, ip_str);
427 strcat(bootargs, mac_str);
428
429 cli_simple_process_macros(bootargs, finalbootargs);
430 env_set("bootargs", finalbootargs);
431 printf("append: %s\n", finalbootargs);
Patrice Chotard17e88042019-11-25 09:07:37 +0100432 }
433
434 bootm_argv[1] = env_get("kernel_addr_r");
435 /* for FIT, append the configuration identifier */
436 if (label->config) {
437 int len = strlen(bootm_argv[1]) + strlen(label->config) + 1;
438
439 fit_addr = malloc(len);
440 if (!fit_addr) {
441 printf("malloc fail (FIT address)\n");
442 return 1;
443 }
444 snprintf(fit_addr, len, "%s%s", bootm_argv[1], label->config);
445 bootm_argv[1] = fit_addr;
446 }
447
448 /*
449 * fdt usage is optional:
450 * It handles the following scenarios. All scenarios are exclusive
451 *
452 * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
453 * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
454 * and adjust argc appropriately.
455 *
456 * Scenario 2: If there is an fdt_addr specified, pass it along to
457 * bootm, and adjust argc appropriately.
458 *
459 * Scenario 3: fdt blob is not available.
460 */
461 bootm_argv[3] = env_get("fdt_addr_r");
462
463 /* if fdt label is defined then get fdt from server */
464 if (bootm_argv[3]) {
465 char *fdtfile = NULL;
466 char *fdtfilefree = NULL;
467
468 if (label->fdt) {
469 fdtfile = label->fdt;
470 } else if (label->fdtdir) {
471 char *f1, *f2, *f3, *f4, *slash;
472
473 f1 = env_get("fdtfile");
474 if (f1) {
475 f2 = "";
476 f3 = "";
477 f4 = "";
478 } else {
479 /*
480 * For complex cases where this code doesn't
481 * generate the correct filename, the board
482 * code should set $fdtfile during early boot,
483 * or the boot scripts should set $fdtfile
484 * before invoking "pxe" or "sysboot".
485 */
486 f1 = env_get("soc");
487 f2 = "-";
488 f3 = env_get("board");
489 f4 = ".dtb";
490 }
491
492 len = strlen(label->fdtdir);
493 if (!len)
494 slash = "./";
495 else if (label->fdtdir[len - 1] != '/')
496 slash = "/";
497 else
498 slash = "";
499
500 len = strlen(label->fdtdir) + strlen(slash) +
501 strlen(f1) + strlen(f2) + strlen(f3) +
502 strlen(f4) + 1;
503 fdtfilefree = malloc(len);
504 if (!fdtfilefree) {
505 printf("malloc fail (FDT filename)\n");
506 goto cleanup;
507 }
508
509 snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
510 label->fdtdir, slash, f1, f2, f3, f4);
511 fdtfile = fdtfilefree;
512 }
513
514 if (fdtfile) {
Patrice Chotard6233de42019-11-25 09:07:39 +0100515 int err = get_relfile_envaddr(cmdtp, fdtfile,
516 "fdt_addr_r");
517
Patrice Chotard17e88042019-11-25 09:07:37 +0100518 free(fdtfilefree);
519 if (err < 0) {
520 printf("Skipping %s for failure retrieving fdt\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100521 label->name);
Patrice Chotard17e88042019-11-25 09:07:37 +0100522 goto cleanup;
523 }
524 } else {
525 bootm_argv[3] = NULL;
526 }
527 }
528
529 if (!bootm_argv[3])
530 bootm_argv[3] = env_get("fdt_addr");
531
532 if (bootm_argv[3]) {
533 if (!bootm_argv[2])
534 bootm_argv[2] = "-";
535 bootm_argc = 4;
536 }
537
538 kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
539 buf = map_sysmem(kernel_addr, 0);
540 /* Try bootm for legacy and FIT format image */
541 if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
542 do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
543#ifdef CONFIG_CMD_BOOTI
544 /* Try booting an AArch64 Linux kernel image */
545 else
546 do_booti(cmdtp, 0, bootm_argc, bootm_argv);
547#elif defined(CONFIG_CMD_BOOTZ)
548 /* Try booting a Image */
549 else
550 do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
551#endif
552 unmap_sysmem(buf);
553
554cleanup:
555 if (fit_addr)
556 free(fit_addr);
557 return 1;
558}
559
560/*
561 * Tokens for the pxe file parser.
562 */
563enum token_type {
564 T_EOL,
565 T_STRING,
566 T_EOF,
567 T_MENU,
568 T_TITLE,
569 T_TIMEOUT,
570 T_LABEL,
571 T_KERNEL,
572 T_LINUX,
573 T_APPEND,
574 T_INITRD,
575 T_LOCALBOOT,
576 T_DEFAULT,
577 T_PROMPT,
578 T_INCLUDE,
579 T_FDT,
580 T_FDTDIR,
581 T_ONTIMEOUT,
582 T_IPAPPEND,
583 T_BACKGROUND,
584 T_INVALID
585};
586
587/*
588 * A token - given by a value and a type.
589 */
590struct token {
591 char *val;
592 enum token_type type;
593};
594
595/*
596 * Keywords recognized.
597 */
598static const struct token keywords[] = {
599 {"menu", T_MENU},
600 {"title", T_TITLE},
601 {"timeout", T_TIMEOUT},
602 {"default", T_DEFAULT},
603 {"prompt", T_PROMPT},
604 {"label", T_LABEL},
605 {"kernel", T_KERNEL},
606 {"linux", T_LINUX},
607 {"localboot", T_LOCALBOOT},
608 {"append", T_APPEND},
609 {"initrd", T_INITRD},
610 {"include", T_INCLUDE},
611 {"devicetree", T_FDT},
612 {"fdt", T_FDT},
613 {"devicetreedir", T_FDTDIR},
614 {"fdtdir", T_FDTDIR},
615 {"ontimeout", T_ONTIMEOUT,},
616 {"ipappend", T_IPAPPEND,},
617 {"background", T_BACKGROUND,},
618 {NULL, T_INVALID}
619};
620
621/*
622 * Since pxe(linux) files don't have a token to identify the start of a
623 * literal, we have to keep track of when we're in a state where a literal is
624 * expected vs when we're in a state a keyword is expected.
625 */
626enum lex_state {
627 L_NORMAL = 0,
628 L_KEYWORD,
629 L_SLITERAL
630};
631
632/*
633 * get_string retrieves a string from *p and stores it as a token in
634 * *t.
635 *
636 * get_string used for scanning both string literals and keywords.
637 *
638 * Characters from *p are copied into t-val until a character equal to
639 * delim is found, or a NUL byte is reached. If delim has the special value of
640 * ' ', any whitespace character will be used as a delimiter.
641 *
642 * If lower is unequal to 0, uppercase characters will be converted to
643 * lowercase in the result. This is useful to make keywords case
644 * insensitive.
645 *
646 * The location of *p is updated to point to the first character after the end
647 * of the token - the ending delimiter.
648 *
649 * On success, the new value of t->val is returned. Memory for t->val is
650 * allocated using malloc and must be free()'d to reclaim it. If insufficient
651 * memory is available, NULL is returned.
652 */
653static char *get_string(char **p, struct token *t, char delim, int lower)
654{
655 char *b, *e;
656 size_t len, i;
657
658 /*
659 * b and e both start at the beginning of the input stream.
660 *
661 * e is incremented until we find the ending delimiter, or a NUL byte
662 * is reached. Then, we take e - b to find the length of the token.
663 */
Patrice Chotard6233de42019-11-25 09:07:39 +0100664 b = *p;
665 e = *p;
Patrice Chotard17e88042019-11-25 09:07:37 +0100666
667 while (*e) {
668 if ((delim == ' ' && isspace(*e)) || delim == *e)
669 break;
670 e++;
671 }
672
673 len = e - b;
674
675 /*
676 * Allocate memory to hold the string, and copy it in, converting
677 * characters to lowercase if lower is != 0.
678 */
679 t->val = malloc(len + 1);
680 if (!t->val)
681 return NULL;
682
683 for (i = 0; i < len; i++, b++) {
684 if (lower)
685 t->val[i] = tolower(*b);
686 else
687 t->val[i] = *b;
688 }
689
690 t->val[len] = '\0';
691
692 /*
693 * Update *p so the caller knows where to continue scanning.
694 */
695 *p = e;
696
697 t->type = T_STRING;
698
699 return t->val;
700}
701
702/*
703 * Populate a keyword token with a type and value.
704 */
705static void get_keyword(struct token *t)
706{
707 int i;
708
709 for (i = 0; keywords[i].val; i++) {
710 if (!strcmp(t->val, keywords[i].val)) {
711 t->type = keywords[i].type;
712 break;
713 }
714 }
715}
716
717/*
718 * Get the next token. We have to keep track of which state we're in to know
719 * if we're looking to get a string literal or a keyword.
720 *
721 * *p is updated to point at the first character after the current token.
722 */
723static void get_token(char **p, struct token *t, enum lex_state state)
724{
725 char *c = *p;
726
727 t->type = T_INVALID;
728
729 /* eat non EOL whitespace */
730 while (isblank(*c))
731 c++;
732
733 /*
734 * eat comments. note that string literals can't begin with #, but
735 * can contain a # after their first character.
736 */
737 if (*c == '#') {
738 while (*c && *c != '\n')
739 c++;
740 }
741
742 if (*c == '\n') {
743 t->type = T_EOL;
744 c++;
745 } else if (*c == '\0') {
746 t->type = T_EOF;
747 c++;
748 } else if (state == L_SLITERAL) {
749 get_string(&c, t, '\n', 0);
750 } else if (state == L_KEYWORD) {
751 /*
752 * when we expect a keyword, we first get the next string
753 * token delimited by whitespace, and then check if it
754 * matches a keyword in our keyword list. if it does, it's
755 * converted to a keyword token of the appropriate type, and
756 * if not, it remains a string token.
757 */
758 get_string(&c, t, ' ', 1);
759 get_keyword(t);
760 }
761
762 *p = c;
763}
764
765/*
766 * Increment *c until we get to the end of the current line, or EOF.
767 */
768static void eol_or_eof(char **c)
769{
770 while (**c && **c != '\n')
771 (*c)++;
772}
773
774/*
775 * All of these parse_* functions share some common behavior.
776 *
777 * They finish with *c pointing after the token they parse, and return 1 on
778 * success, or < 0 on error.
779 */
780
781/*
782 * Parse a string literal and store a pointer it at *dst. String literals
783 * terminate at the end of the line.
784 */
785static int parse_sliteral(char **c, char **dst)
786{
787 struct token t;
788 char *s = *c;
789
790 get_token(c, &t, L_SLITERAL);
791
792 if (t.type != T_STRING) {
793 printf("Expected string literal: %.*s\n", (int)(*c - s), s);
794 return -EINVAL;
795 }
796
797 *dst = t.val;
798
799 return 1;
800}
801
802/*
803 * Parse a base 10 (unsigned) integer and store it at *dst.
804 */
805static int parse_integer(char **c, int *dst)
806{
807 struct token t;
808 char *s = *c;
809
810 get_token(c, &t, L_SLITERAL);
811
812 if (t.type != T_STRING) {
813 printf("Expected string: %.*s\n", (int)(*c - s), s);
814 return -EINVAL;
815 }
816
817 *dst = simple_strtol(t.val, NULL, 10);
818
819 free(t.val);
820
821 return 1;
822}
823
824static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +0100825 struct pxe_menu *cfg, int nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +0100826
827/*
828 * Parse an include statement, and retrieve and parse the file it mentions.
829 *
830 * base should point to a location where it's safe to store the file, and
831 * nest_level should indicate how many nested includes have occurred. For this
832 * include, nest_level has already been incremented and doesn't need to be
833 * incremented here.
834 */
835static int handle_include(cmd_tbl_t *cmdtp, char **c, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +0100836 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +0100837{
838 char *include_path;
839 char *s = *c;
840 int err;
841 char *buf;
842 int ret;
843
844 err = parse_sliteral(c, &include_path);
845
846 if (err < 0) {
Patrice Chotard6233de42019-11-25 09:07:39 +0100847 printf("Expected include path: %.*s\n", (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +0100848 return err;
849 }
850
851 err = get_pxe_file(cmdtp, include_path, base);
852
853 if (err < 0) {
854 printf("Couldn't retrieve %s\n", include_path);
855 return err;
856 }
857
858 buf = map_sysmem(base, 0);
859 ret = parse_pxefile_top(cmdtp, buf, base, cfg, nest_level);
860 unmap_sysmem(buf);
861
862 return ret;
863}
864
865/*
866 * Parse lines that begin with 'menu'.
867 *
868 * base and nest are provided to handle the 'menu include' case.
869 *
870 * base should point to a location where it's safe to store the included file.
871 *
872 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
873 * a file it includes, 3 when parsing a file included by that file, and so on.
874 */
875static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +0100876 unsigned long base, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +0100877{
878 struct token t;
879 char *s = *c;
880 int err = 0;
881
882 get_token(c, &t, L_KEYWORD);
883
884 switch (t.type) {
885 case T_TITLE:
886 err = parse_sliteral(c, &cfg->title);
887
888 break;
889
890 case T_INCLUDE:
Patrice Chotard6233de42019-11-25 09:07:39 +0100891 err = handle_include(cmdtp, c, base, cfg, nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +0100892 break;
893
894 case T_BACKGROUND:
895 err = parse_sliteral(c, &cfg->bmp);
896 break;
897
898 default:
899 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100900 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +0100901 }
902
903 if (err < 0)
904 return err;
905
906 eol_or_eof(c);
907
908 return 1;
909}
910
911/*
912 * Handles parsing a 'menu line' when we're parsing a label.
913 */
914static int parse_label_menu(char **c, struct pxe_menu *cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +0100915 struct pxe_label *label)
Patrice Chotard17e88042019-11-25 09:07:37 +0100916{
917 struct token t;
918 char *s;
919
920 s = *c;
921
922 get_token(c, &t, L_KEYWORD);
923
924 switch (t.type) {
925 case T_DEFAULT:
926 if (!cfg->default_label)
927 cfg->default_label = strdup(label->name);
928
929 if (!cfg->default_label)
930 return -ENOMEM;
931
932 break;
933 case T_LABEL:
934 parse_sliteral(c, &label->menu);
935 break;
936 default:
937 printf("Ignoring malformed menu command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +0100938 (int)(*c - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +0100939 }
940
941 eol_or_eof(c);
942
943 return 0;
944}
945
946/*
947 * Handles parsing a 'kernel' label.
948 * expecting "filename" or "<fit_filename>#cfg"
949 */
950static int parse_label_kernel(char **c, struct pxe_label *label)
951{
952 char *s;
953 int err;
954
955 err = parse_sliteral(c, &label->kernel);
956 if (err < 0)
957 return err;
958
959 s = strstr(label->kernel, "#");
960 if (!s)
961 return 1;
962
963 label->config = malloc(strlen(s) + 1);
964 if (!label->config)
965 return -ENOMEM;
966
967 strcpy(label->config, s);
968 *s = 0;
969
970 return 1;
971}
972
973/*
974 * Parses a label and adds it to the list of labels for a menu.
975 *
976 * A label ends when we either get to the end of a file, or
977 * get some input we otherwise don't have a handler defined
978 * for.
979 *
980 */
981static int parse_label(char **c, struct pxe_menu *cfg)
982{
983 struct token t;
984 int len;
985 char *s = *c;
986 struct pxe_label *label;
987 int err;
988
989 label = label_create();
990 if (!label)
991 return -ENOMEM;
992
993 err = parse_sliteral(c, &label->name);
994 if (err < 0) {
995 printf("Expected label name: %.*s\n", (int)(*c - s), s);
996 label_destroy(label);
997 return -EINVAL;
998 }
999
1000 list_add_tail(&label->list, &cfg->labels);
1001
1002 while (1) {
1003 s = *c;
1004 get_token(c, &t, L_KEYWORD);
1005
1006 err = 0;
1007 switch (t.type) {
1008 case T_MENU:
1009 err = parse_label_menu(c, cfg, label);
1010 break;
1011
1012 case T_KERNEL:
1013 case T_LINUX:
1014 err = parse_label_kernel(c, label);
1015 break;
1016
1017 case T_APPEND:
1018 err = parse_sliteral(c, &label->append);
1019 if (label->initrd)
1020 break;
1021 s = strstr(label->append, "initrd=");
1022 if (!s)
1023 break;
1024 s += 7;
1025 len = (int)(strchr(s, ' ') - s);
1026 label->initrd = malloc(len + 1);
1027 strncpy(label->initrd, s, len);
1028 label->initrd[len] = '\0';
1029
1030 break;
1031
1032 case T_INITRD:
1033 if (!label->initrd)
1034 err = parse_sliteral(c, &label->initrd);
1035 break;
1036
1037 case T_FDT:
1038 if (!label->fdt)
1039 err = parse_sliteral(c, &label->fdt);
1040 break;
1041
1042 case T_FDTDIR:
1043 if (!label->fdtdir)
1044 err = parse_sliteral(c, &label->fdtdir);
1045 break;
1046
1047 case T_LOCALBOOT:
1048 label->localboot = 1;
1049 err = parse_integer(c, &label->localboot_val);
1050 break;
1051
1052 case T_IPAPPEND:
1053 err = parse_integer(c, &label->ipappend);
1054 break;
1055
1056 case T_EOL:
1057 break;
1058
1059 default:
1060 /*
1061 * put the token back! we don't want it - it's the end
1062 * of a label and whatever token this is, it's
1063 * something for the menu level context to handle.
1064 */
1065 *c = s;
1066 return 1;
1067 }
1068
1069 if (err < 0)
1070 return err;
1071 }
1072}
1073
1074/*
1075 * This 16 comes from the limit pxelinux imposes on nested includes.
1076 *
1077 * There is no reason at all we couldn't do more, but some limit helps prevent
1078 * infinite (until crash occurs) recursion if a file tries to include itself.
1079 */
1080#define MAX_NEST_LEVEL 16
1081
1082/*
1083 * Entry point for parsing a menu file. nest_level indicates how many times
1084 * we've nested in includes. It will be 1 for the top level menu file.
1085 *
1086 * Returns 1 on success, < 0 on error.
1087 */
1088static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
Patrice Chotard6233de42019-11-25 09:07:39 +01001089 struct pxe_menu *cfg, int nest_level)
Patrice Chotard17e88042019-11-25 09:07:37 +01001090{
1091 struct token t;
1092 char *s, *b, *label_name;
1093 int err;
1094
1095 b = p;
1096
1097 if (nest_level > MAX_NEST_LEVEL) {
1098 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
1099 return -EMLINK;
1100 }
1101
1102 while (1) {
1103 s = p;
1104
1105 get_token(&p, &t, L_KEYWORD);
1106
1107 err = 0;
1108 switch (t.type) {
1109 case T_MENU:
1110 cfg->prompt = 1;
1111 err = parse_menu(cmdtp, &p, cfg,
Patrice Chotard6233de42019-11-25 09:07:39 +01001112 base + ALIGN(strlen(b) + 1, 4),
1113 nest_level);
Patrice Chotard17e88042019-11-25 09:07:37 +01001114 break;
1115
1116 case T_TIMEOUT:
1117 err = parse_integer(&p, &cfg->timeout);
1118 break;
1119
1120 case T_LABEL:
1121 err = parse_label(&p, cfg);
1122 break;
1123
1124 case T_DEFAULT:
1125 case T_ONTIMEOUT:
1126 err = parse_sliteral(&p, &label_name);
1127
1128 if (label_name) {
1129 if (cfg->default_label)
1130 free(cfg->default_label);
1131
1132 cfg->default_label = label_name;
1133 }
1134
1135 break;
1136
1137 case T_INCLUDE:
1138 err = handle_include(cmdtp, &p,
Patrice Chotard6233de42019-11-25 09:07:39 +01001139 base + ALIGN(strlen(b), 4), cfg,
1140 nest_level + 1);
Patrice Chotard17e88042019-11-25 09:07:37 +01001141 break;
1142
1143 case T_PROMPT:
1144 eol_or_eof(&p);
1145 break;
1146
1147 case T_EOL:
1148 break;
1149
1150 case T_EOF:
1151 return 1;
1152
1153 default:
1154 printf("Ignoring unknown command: %.*s\n",
Patrice Chotard6233de42019-11-25 09:07:39 +01001155 (int)(p - s), s);
Patrice Chotard17e88042019-11-25 09:07:37 +01001156 eol_or_eof(&p);
1157 }
1158
1159 if (err < 0)
1160 return err;
1161 }
1162}
1163
1164/*
1165 * Free the memory used by a pxe_menu and its labels.
1166 */
1167void destroy_pxe_menu(struct pxe_menu *cfg)
1168{
1169 struct list_head *pos, *n;
1170 struct pxe_label *label;
1171
1172 if (cfg->title)
1173 free(cfg->title);
1174
1175 if (cfg->default_label)
1176 free(cfg->default_label);
1177
1178 list_for_each_safe(pos, n, &cfg->labels) {
1179 label = list_entry(pos, struct pxe_label, list);
1180
1181 label_destroy(label);
1182 }
1183
1184 free(cfg);
1185}
1186
1187/*
1188 * Entry point for parsing a pxe file. This is only used for the top level
1189 * file.
1190 *
1191 * Returns NULL if there is an error, otherwise, returns a pointer to a
1192 * pxe_menu struct populated with the results of parsing the pxe file (and any
1193 * files it includes). The resulting pxe_menu struct can be free()'d by using
1194 * the destroy_pxe_menu() function.
1195 */
1196struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, unsigned long menucfg)
1197{
1198 struct pxe_menu *cfg;
1199 char *buf;
1200 int r;
1201
1202 cfg = malloc(sizeof(struct pxe_menu));
1203
1204 if (!cfg)
1205 return NULL;
1206
1207 memset(cfg, 0, sizeof(struct pxe_menu));
1208
1209 INIT_LIST_HEAD(&cfg->labels);
1210
1211 buf = map_sysmem(menucfg, 0);
1212 r = parse_pxefile_top(cmdtp, buf, menucfg, cfg, 1);
1213 unmap_sysmem(buf);
1214
1215 if (r < 0) {
1216 destroy_pxe_menu(cfg);
1217 return NULL;
1218 }
1219
1220 return cfg;
1221}
1222
1223/*
1224 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1225 * menu code.
1226 */
1227static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
1228{
1229 struct pxe_label *label;
1230 struct list_head *pos;
1231 struct menu *m;
1232 int err;
1233 int i = 1;
1234 char *default_num = NULL;
1235
1236 /*
1237 * Create a menu and add items for all the labels.
1238 */
1239 m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
1240 cfg->prompt, label_print, NULL, NULL);
1241
1242 if (!m)
1243 return NULL;
1244
1245 list_for_each(pos, &cfg->labels) {
1246 label = list_entry(pos, struct pxe_label, list);
1247
1248 sprintf(label->num, "%d", i++);
1249 if (menu_item_add(m, label->num, label) != 1) {
1250 menu_destroy(m);
1251 return NULL;
1252 }
1253 if (cfg->default_label &&
1254 (strcmp(label->name, cfg->default_label) == 0))
1255 default_num = label->num;
Patrice Chotard17e88042019-11-25 09:07:37 +01001256 }
1257
1258 /*
1259 * After we've created items for each label in the menu, set the
1260 * menu's default label if one was specified.
1261 */
1262 if (default_num) {
1263 err = menu_default_set(m, default_num);
1264 if (err != 1) {
1265 if (err != -ENOENT) {
1266 menu_destroy(m);
1267 return NULL;
1268 }
1269
1270 printf("Missing default: %s\n", cfg->default_label);
1271 }
1272 }
1273
1274 return m;
1275}
1276
1277/*
1278 * Try to boot any labels we have yet to attempt to boot.
1279 */
1280static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1281{
1282 struct list_head *pos;
1283 struct pxe_label *label;
1284
1285 list_for_each(pos, &cfg->labels) {
1286 label = list_entry(pos, struct pxe_label, list);
1287
1288 if (!label->attempted)
1289 label_boot(cmdtp, label);
1290 }
1291}
1292
1293/*
1294 * Boot the system as prescribed by a pxe_menu.
1295 *
1296 * Use the menu system to either get the user's choice or the default, based
1297 * on config or user input. If there is no default or user's choice,
1298 * attempted to boot labels in the order they were given in pxe files.
1299 * If the default or user's choice fails to boot, attempt to boot other
1300 * labels in the order they were given in pxe files.
1301 *
1302 * If this function returns, there weren't any labels that successfully
1303 * booted, or the user interrupted the menu selection via ctrl+c.
1304 */
1305void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
1306{
1307 void *choice;
1308 struct menu *m;
1309 int err;
1310
1311#ifdef CONFIG_CMD_BMP
1312 /* display BMP if available */
1313 if (cfg->bmp) {
Simon Glass892265d2019-12-28 10:45:02 -07001314 if (get_relfile(cmdtp, cfg->bmp, image_load_addr)) {
Patrick Delaunaya2fe8b22019-12-03 09:38:35 +01001315 if (CONFIG_IS_ENABLED(CMD_CLS))
1316 run_command("cls", 0);
Simon Glass892265d2019-12-28 10:45:02 -07001317 bmp_display(image_load_addr,
Patrice Chotard17e88042019-11-25 09:07:37 +01001318 BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
1319 } else {
1320 printf("Skipping background bmp %s for failure\n",
1321 cfg->bmp);
1322 }
1323 }
1324#endif
1325
1326 m = pxe_menu_to_menu(cfg);
1327 if (!m)
1328 return;
1329
1330 err = menu_get_choice(m, &choice);
1331
1332 menu_destroy(m);
1333
1334 /*
1335 * err == 1 means we got a choice back from menu_get_choice.
1336 *
1337 * err == -ENOENT if the menu was setup to select the default but no
1338 * default was set. in that case, we should continue trying to boot
1339 * labels that haven't been attempted yet.
1340 *
1341 * otherwise, the user interrupted or there was some other error and
1342 * we give up.
1343 */
1344
1345 if (err == 1) {
1346 err = label_boot(cmdtp, choice);
1347 if (!err)
1348 return;
1349 } else if (err != -ENOENT) {
1350 return;
1351 }
1352
1353 boot_unattempted_labels(cmdtp, cfg);
1354}