1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2010-2011 Calxeda, Inc.
4 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
17 #include <fdt_support.h>
19 #include <linux/libfdt.h>
20 #include <linux/string.h>
21 #include <linux/ctype.h>
23 #include <linux/list.h>
35 #include "pxe_utils.h"
37 #define MAX_TFTP_PATH_LEN 512
39 int pxe_get_file_size(ulong
*sizep
)
43 val
= from_env("filesize");
47 if (strict_strtoul(val
, 16, sizep
) < 0)
54 * format_mac_pxe() - obtain a MAC address in the PXE format
56 * This produces a MAC-address string in the format for the current ethernet
59 * 01-aa-bb-cc-dd-ee-ff
61 * where aa-ff is the MAC address in hex
63 * @outbuf: Buffer to write string to
64 * @outbuf_len: length of buffer
65 * Return: 1 if OK, -ENOSPC if buffer is too small, -ENOENT is there is no
66 * current ethernet device
68 int format_mac_pxe(char *outbuf
, size_t outbuf_len
)
72 if (outbuf_len
< 21) {
73 printf("outbuf is too small (%zd < 21)\n", outbuf_len
);
77 if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr
))
80 sprintf(outbuf
, "01-%02x-%02x-%02x-%02x-%02x-%02x",
81 ethaddr
[0], ethaddr
[1], ethaddr
[2],
82 ethaddr
[3], ethaddr
[4], ethaddr
[5]);
88 * get_relfile() - read a file relative to the PXE file
90 * As in pxelinux, paths to files referenced from files we retrieve are
91 * relative to the location of bootfile. get_relfile takes such a path and
92 * joins it with the bootfile path to get the full path to the target file. If
93 * the bootfile path is NULL, we use file_path as is.
96 * @file_path: File path to read (relative to the PXE file)
97 * @file_addr: Address to load file to
98 * @filesizep: If not NULL, returns the file size in bytes
99 * Returns 1 for success, or < 0 on error
101 static int get_relfile(struct pxe_context
*ctx
, const char *file_path
,
102 unsigned long file_addr
, ulong
*filesizep
)
105 char relfile
[MAX_TFTP_PATH_LEN
+ 1];
110 if (file_path
[0] == '/' && ctx
->allow_abs_path
)
113 strncpy(relfile
, ctx
->bootdir
, MAX_TFTP_PATH_LEN
);
115 path_len
= strlen(file_path
) + strlen(relfile
);
117 if (path_len
> MAX_TFTP_PATH_LEN
) {
118 printf("Base path too long (%s%s)\n", relfile
, file_path
);
120 return -ENAMETOOLONG
;
123 strcat(relfile
, file_path
);
125 printf("Retrieving file: %s\n", relfile
);
127 sprintf(addr_buf
, "%lx", file_addr
);
129 ret
= ctx
->getfile(ctx
, relfile
, addr_buf
, &size
);
131 return log_msg_ret("get", ret
);
139 * get_pxe_file() - read a file
141 * The file is read and nul-terminated
144 * @file_path: File path to read (relative to the PXE file)
145 * @file_addr: Address to load file to
146 * Returns 1 for success, or < 0 on error
148 int get_pxe_file(struct pxe_context
*ctx
, const char *file_path
,
155 err
= get_relfile(ctx
, file_path
, file_addr
, &size
);
159 buf
= map_sysmem(file_addr
+ size
, 1);
166 #define PXELINUX_DIR "pxelinux.cfg/"
169 * get_pxelinux_path() - Get a file in the pxelinux.cfg/ directory
172 * @file: Filename to process (relative to pxelinux.cfg/)
173 * Returns 1 for success, -ENAMETOOLONG if the resulting path is too long.
174 * or other value < 0 on other error
176 int get_pxelinux_path(struct pxe_context
*ctx
, const char *file
,
177 unsigned long pxefile_addr_r
)
179 size_t base_len
= strlen(PXELINUX_DIR
);
180 char path
[MAX_TFTP_PATH_LEN
+ 1];
182 if (base_len
+ strlen(file
) > MAX_TFTP_PATH_LEN
) {
183 printf("path (%s%s) too long, skipping\n",
185 return -ENAMETOOLONG
;
188 sprintf(path
, PXELINUX_DIR
"%s", file
);
190 return get_pxe_file(ctx
, path
, pxefile_addr_r
);
194 * get_relfile_envaddr() - read a file to an address in an env var
196 * Wrapper to make it easier to store the file at file_path in the location
197 * specified by envaddr_name. file_path will be joined to the bootfile path,
198 * if any is specified.
201 * @file_path: File path to read (relative to the PXE file)
202 * @envaddr_name: Name of environment variable which contains the address to
204 * @filesizep: Returns the file size in bytes
205 * Returns 1 on success, -ENOENT if @envaddr_name does not exist as an
206 * environment variable, -EINVAL if its format is not valid hex, or other
207 * value < 0 on other error
209 static int get_relfile_envaddr(struct pxe_context
*ctx
, const char *file_path
,
210 const char *envaddr_name
, ulong
*filesizep
)
212 unsigned long file_addr
;
215 envaddr
= from_env(envaddr_name
);
219 if (strict_strtoul(envaddr
, 16, &file_addr
) < 0)
222 return get_relfile(ctx
, file_path
, file_addr
, filesizep
);
226 * label_create() - crate a new PXE label
228 * Allocates memory for and initializes a pxe_label. This uses malloc, so the
229 * result must be free()'d to reclaim the memory.
231 * Returns a pointer to the label, or NULL if out of memory
233 static struct pxe_label
*label_create(void)
235 struct pxe_label
*label
;
237 label
= malloc(sizeof(struct pxe_label
));
241 memset(label
, 0, sizeof(struct pxe_label
));
247 * label_destroy() - free the memory used by a pxe_label
249 * This frees @label itself as well as memory used by its name,
250 * kernel, config, append, initrd, fdt, fdtdir and fdtoverlay members, if
253 * So - be sure to only use dynamically allocated memory for the members of
254 * the pxe_label struct, unless you want to clean it up first. These are
255 * currently only created by the pxe file parsing code.
257 * @label: Label to free
259 static void label_destroy(struct pxe_label
*label
)
262 free(label
->kernel_label
);
269 free(label
->fdtoverlays
);
274 * label_print() - Print a label and its string members if they're defined
276 * This is passed as a callback to the menu code for displaying each
279 * @data: Label to print (is cast to struct pxe_label *)
281 static void label_print(void *data
)
283 struct pxe_label
*label
= data
;
284 const char *c
= label
->menu
? label
->menu
: label
->name
;
286 printf("%s:\t%s\n", label
->num
, c
);
290 * label_localboot() - Boot a label that specified 'localboot'
292 * This requires that the 'localcmd' environment variable is defined. Its
293 * contents will be executed as U-Boot commands. If the label specified an
294 * 'append' line, its contents will be used to overwrite the contents of the
295 * 'bootargs' environment variable prior to running 'localcmd'.
297 * @label: Label to process
298 * Returns 1 on success or < 0 on error
300 static int label_localboot(struct pxe_label
*label
)
304 localcmd
= from_env("localcmd");
309 char bootargs
[CONFIG_SYS_CBSIZE
];
311 cli_simple_process_macros(label
->append
, bootargs
,
313 env_set("bootargs", bootargs
);
316 debug("running: %s\n", localcmd
);
318 return run_command_list(localcmd
, strlen(localcmd
), 0);
322 * label_boot_kaslrseed generate kaslrseed from hw rng
325 static void label_boot_kaslrseed(void)
329 struct fdt_header
*working_fdt
;
336 /* Get the main fdt and map it */
337 fdt_addr
= hextoul(env_get("fdt_addr_r"), NULL
);
338 working_fdt
= map_sysmem(fdt_addr
, 0);
339 err
= fdt_check_header(working_fdt
);
343 /* add extra size for holding kaslr-seed */
344 /* err is new fdt size, 0 or negtive */
345 err
= fdt_shrink_to_minimum(working_fdt
, 512);
349 if (uclass_get_device(UCLASS_RNG
, 0, &dev
) || !dev
) {
350 printf("No RNG device\n");
354 nodeoffset
= fdt_find_or_add_subnode(working_fdt
, 0, "chosen");
355 if (nodeoffset
< 0) {
356 printf("Reading chosen node failed\n");
362 printf("Out of memory\n");
366 if (dm_rng_read(dev
, buf
, n
)) {
367 printf("Reading RNG failed\n");
371 err
= fdt_setprop(working_fdt
, nodeoffset
, "kaslr-seed", buf
, sizeof(buf
));
373 printf("Unable to set kaslr-seed on chosen node: %s\n", fdt_strerror(err
));
383 * label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays'
384 * or 'devicetree-overlay'
387 * @label: Label to process
389 #ifdef CONFIG_OF_LIBFDT_OVERLAY
390 static void label_boot_fdtoverlay(struct pxe_context
*ctx
,
391 struct pxe_label
*label
)
393 char *fdtoverlay
= label
->fdtoverlays
;
394 struct fdt_header
*working_fdt
;
395 char *fdtoverlay_addr_env
;
396 ulong fdtoverlay_addr
;
400 /* Get the main fdt and map it */
401 fdt_addr
= hextoul(env_get("fdt_addr_r"), NULL
);
402 working_fdt
= map_sysmem(fdt_addr
, 0);
403 err
= fdt_check_header(working_fdt
);
407 /* Get the specific overlay loading address */
408 fdtoverlay_addr_env
= env_get("fdtoverlay_addr_r");
409 if (!fdtoverlay_addr_env
) {
410 printf("Invalid fdtoverlay_addr_r for loading overlays\n");
414 fdtoverlay_addr
= hextoul(fdtoverlay_addr_env
, NULL
);
416 /* Cycle over the overlay files and apply them in order */
418 struct fdt_header
*blob
;
423 /* Drop leading spaces */
424 while (*fdtoverlay
== ' ')
427 /* Copy a single filename if multiple provided */
428 end
= strstr(fdtoverlay
, " ");
430 len
= (int)(end
- fdtoverlay
);
431 overlayfile
= malloc(len
+ 1);
432 strncpy(overlayfile
, fdtoverlay
, len
);
433 overlayfile
[len
] = '\0';
435 overlayfile
= fdtoverlay
;
437 if (!strlen(overlayfile
))
440 /* Load overlay file */
441 err
= get_relfile_envaddr(ctx
, overlayfile
, "fdtoverlay_addr_r",
444 printf("Failed loading overlay %s\n", overlayfile
);
448 /* Resize main fdt */
449 fdt_shrink_to_minimum(working_fdt
, 8192);
451 blob
= map_sysmem(fdtoverlay_addr
, 0);
452 err
= fdt_check_header(blob
);
454 printf("Invalid overlay %s, skipping\n",
459 err
= fdt_overlay_apply_verbose(working_fdt
, blob
);
461 printf("Failed to apply overlay %s, skipping\n",
469 } while ((fdtoverlay
= strstr(fdtoverlay
, " ")));
474 * calc_fdt_fname() - Figure out the filename to use for the FDT
476 * Determine the path to the FDT filename, based on the "fdtfile" environment
477 * variable. Use <soc>-<board>.dtb as a fallback
479 * @fdtdir: Directory to use for the FDT file
480 * Return: allocated filename (including directory), or NULL if out of memory
482 static char *calc_fdt_fname(const char *fdtdir
)
485 char *f1
, *f2
, *f3
, *f4
, *slash
;
488 f1
= env_get("fdtfile");
495 * For complex cases where this code doesn't generate the
496 * correct filename, the board code should set $fdtfile during
497 * early boot, or the boot scripts should set $fdtfile before
498 * invoking "pxe" or "sysboot".
502 f3
= env_get("board");
514 len
= strlen(fdtdir
);
517 else if (fdtdir
[len
- 1] != '/')
522 len
= strlen(fdtdir
) + strlen(slash
) + strlen(f1
) + strlen(f2
) +
523 strlen(f3
) + strlen(f4
) + 1;
524 fdtfile
= malloc(len
);
526 printf("malloc fail (FDT filename)\n");
530 snprintf(fdtfile
, len
, "%s%s%s%s%s%s", fdtdir
, slash
, f1
, f2
, f3
, f4
);
536 * label_run_boot() - Run the correct boot procedure
538 * fdt usage is optional:
539 * It handles the following scenarios.
541 * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
542 * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
543 * bootm, and adjust argc appropriately.
545 * If retrieve fails and no exact fdt blob is specified in pxe file with
546 * "fdt" label, try Scenario 2.
548 * Scenario 2: If there is an fdt_addr specified, pass it along to
549 * bootm, and adjust argc appropriately.
551 * Scenario 3: If there is an fdtcontroladdr specified, pass it along to
552 * bootm, and adjust argc appropriately, unless the image type is fitImage.
554 * Scenario 4: fdt blob is not available.
557 * @label: Label to process
558 * @kernel_addr: string containing the kernel address / config
559 * @initrd_str: string containing the initrd address / size
560 * @initrd_addr_str: initrd address, or NULL if none
561 * @initrd_filesize: initrd size in bytes; only valid if initrd_addr_str is not
563 * Returns does not return on success, otherwise returns 0 if a localboot
564 * label was processed, or 1 on error
566 static int label_run_boot(struct pxe_context
*ctx
, struct pxe_label
*label
,
567 char *kernel_addr
, char *initrd_str
,
568 char *initrd_addr_str
, char *initrd_filesize
)
570 struct bootm_info bmi
;
571 const char *fdt_addr
;
576 if (IS_ENABLED(CONFIG_BOOTM
))
579 fdt_addr
= env_get("fdt_addr_r");
581 /* For FIT, the label can be identical to kernel one */
582 if (label
->fdt
&& !strcmp(label
->kernel_label
, label
->fdt
)) {
583 fdt_addr
= kernel_addr
;
584 /* if fdt label is defined then get fdt from server */
585 } else if (fdt_addr
) {
586 char *fdtfile
= NULL
;
587 char *fdtfilefree
= NULL
;
590 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS
)) {
591 if (strcmp("-", label
->fdt
))
592 fdtfile
= label
->fdt
;
594 fdtfile
= label
->fdt
;
596 } else if (label
->fdtdir
) {
597 fdtfilefree
= calc_fdt_fname(label
->fdtdir
);
600 fdtfile
= fdtfilefree
;
604 int err
= get_relfile_envaddr(ctx
, fdtfile
,
612 printf("Skipping %s for failure retrieving FDT\n",
618 printf("Skipping fdtdir %s for failure retrieving dts\n",
623 if (label
->kaslrseed
)
624 label_boot_kaslrseed();
626 #ifdef CONFIG_OF_LIBFDT_OVERLAY
627 if (label
->fdtoverlays
)
628 label_boot_fdtoverlay(ctx
, label
);
635 bmi
.addr_img
= kernel_addr
;
638 bmi
.conf_ramdisk
= initrd_str
;
641 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS
)) {
642 if (strcmp("-", label
->fdt
))
643 fdt_addr
= env_get("fdt_addr");
645 fdt_addr
= env_get("fdt_addr");
649 kernel_addr_r
= genimg_get_kernel_addr(kernel_addr
);
650 buf
= map_sysmem(kernel_addr_r
, 0);
652 if (!fdt_addr
&& genimg_get_format(buf
) != IMAGE_FORMAT_FIT
) {
653 if (IS_ENABLED(CONFIG_SUPPORT_PASSING_ATAGS
)) {
654 if (strcmp("-", label
->fdt
))
655 fdt_addr
= env_get("fdtcontroladdr");
657 fdt_addr
= env_get("fdtcontroladdr");
661 bmi
.conf_fdt
= fdt_addr
;
663 /* Try bootm for legacy and FIT format image */
664 if (genimg_get_format(buf
) != IMAGE_FORMAT_INVALID
&&
665 IS_ENABLED(CONFIG_BOOTM
))
666 ret
= bootm_run(&bmi
);
667 /* Try booting an AArch64 Linux kernel image */
668 else if (IS_ENABLED(CONFIG_BOOTM
))
669 ret
= booti_run(&bmi
);
670 /* Try booting a Image */
671 else if (IS_ENABLED(CONFIG_BOOTM
))
672 ret
= bootz_run(&bmi
);
673 /* Try booting an x86_64 Linux kernel image */
674 else if (IS_ENABLED(CONFIG_ZBOOT
))
675 ret
= zboot_run(hextoul(kernel_addr
, NULL
), 0,
677 hextoul(initrd_addr_str
, NULL
) : 0,
679 hextoul(initrd_filesize
, NULL
) : 0,
688 * label_boot() - Boot according to the contents of a pxe_label
690 * If we can't boot for any reason, we return. A successful boot never
693 * The kernel will be stored in the location given by the 'kernel_addr_r'
694 * environment variable.
696 * If the label specifies an initrd file, it will be stored in the location
697 * given by the 'ramdisk_addr_r' environment variable.
699 * If the label specifies an 'append' line, its contents will overwrite that
700 * of the 'bootargs' environment variable.
703 * @label: Label to process
704 * Returns does not return on success, otherwise returns 0 if a localboot
705 * label was processed, or 1 on error
707 static int label_boot(struct pxe_context
*ctx
, struct pxe_label
*label
)
709 char *kernel_addr
= NULL
;
710 char *initrd_addr_str
= NULL
;
711 char initrd_filesize
[10];
713 char mac_str
[29] = "";
714 char ip_str
[68] = "";
715 char *fit_addr
= NULL
;
720 label
->attempted
= 1;
722 if (label
->localboot
) {
723 if (label
->localboot_val
>= 0)
724 label_localboot(label
);
728 if (!label
->kernel
) {
729 printf("No kernel given, skipping %s\n",
734 if (get_relfile_envaddr(ctx
, label
->kernel
, "kernel_addr_r",
736 printf("Skipping %s for failure retrieving kernel\n",
741 kernel_addr
= env_get("kernel_addr_r");
742 /* for FIT, append the configuration identifier */
744 int len
= strlen(kernel_addr
) + strlen(label
->config
) + 1;
746 fit_addr
= malloc(len
);
748 printf("malloc fail (FIT address)\n");
751 snprintf(fit_addr
, len
, "%s%s", kernel_addr
, label
->config
);
752 kernel_addr
= fit_addr
;
755 /* For FIT, the label can be identical to kernel one */
756 if (label
->initrd
&& !strcmp(label
->kernel_label
, label
->initrd
)) {
757 initrd_addr_str
= kernel_addr
;
758 } else if (label
->initrd
) {
761 if (get_relfile_envaddr(ctx
, label
->initrd
, "ramdisk_addr_r",
763 printf("Skipping %s for failure retrieving initrd\n",
767 strcpy(initrd_filesize
, simple_xtoa(size
));
768 initrd_addr_str
= env_get("ramdisk_addr_r");
769 size
= snprintf(initrd_str
, sizeof(initrd_str
), "%s:%lx",
770 initrd_addr_str
, size
);
771 if (size
>= sizeof(initrd_str
))
775 if (label
->ipappend
& 0x1) {
776 sprintf(ip_str
, " ip=%s:%s:%s:%s",
777 env_get("ipaddr"), env_get("serverip"),
778 env_get("gatewayip"), env_get("netmask"));
781 if (IS_ENABLED(CONFIG_CMD_NET
)) {
782 if (label
->ipappend
& 0x2) {
785 strcpy(mac_str
, " BOOTIF=");
786 err
= format_mac_pxe(mac_str
+ 8, sizeof(mac_str
) - 8);
792 if ((label
->ipappend
& 0x3) || label
->append
) {
793 char bootargs
[CONFIG_SYS_CBSIZE
] = "";
794 char finalbootargs
[CONFIG_SYS_CBSIZE
];
796 if (strlen(label
->append
?: "") +
797 strlen(ip_str
) + strlen(mac_str
) + 1 > sizeof(bootargs
)) {
798 printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
799 strlen(label
->append
?: ""),
800 strlen(ip_str
), strlen(mac_str
),
806 strlcpy(bootargs
, label
->append
, sizeof(bootargs
));
808 strcat(bootargs
, ip_str
);
809 strcat(bootargs
, mac_str
);
811 cli_simple_process_macros(bootargs
, finalbootargs
,
812 sizeof(finalbootargs
));
813 env_set("bootargs", finalbootargs
);
814 printf("append: %s\n", finalbootargs
);
817 ret
= label_run_boot(ctx
, label
, kernel_addr
, initrd_str
,
818 initrd_addr_str
, initrd_filesize
);
826 /** enum token_type - Tokens for the pxe file parser */
853 /** struct token - token - given by a value and a type */
856 enum token_type type
;
859 /* Keywords recognized */
860 static const struct token keywords
[] = {
863 {"timeout", T_TIMEOUT
},
864 {"default", T_DEFAULT
},
865 {"prompt", T_PROMPT
},
867 {"kernel", T_KERNEL
},
869 {"localboot", T_LOCALBOOT
},
870 {"append", T_APPEND
},
871 {"initrd", T_INITRD
},
872 {"include", T_INCLUDE
},
873 {"devicetree", T_FDT
},
875 {"devicetreedir", T_FDTDIR
},
876 {"fdtdir", T_FDTDIR
},
877 {"fdtoverlays", T_FDTOVERLAYS
},
878 {"devicetree-overlay", T_FDTOVERLAYS
},
879 {"ontimeout", T_ONTIMEOUT
,},
880 {"ipappend", T_IPAPPEND
,},
881 {"background", T_BACKGROUND
,},
882 {"kaslrseed", T_KASLRSEED
,},
887 * enum lex_state - lexer state
889 * Since pxe(linux) files don't have a token to identify the start of a
890 * literal, we have to keep track of when we're in a state where a literal is
891 * expected vs when we're in a state a keyword is expected.
900 * get_string() - retrieves a string from *p and stores it as a token in *t.
902 * This is used for scanning both string literals and keywords.
904 * Characters from *p are copied into t-val until a character equal to
905 * delim is found, or a NUL byte is reached. If delim has the special value of
906 * ' ', any whitespace character will be used as a delimiter.
908 * If lower is unequal to 0, uppercase characters will be converted to
909 * lowercase in the result. This is useful to make keywords case
912 * The location of *p is updated to point to the first character after the end
913 * of the token - the ending delimiter.
915 * Memory for t->val is allocated using malloc and must be free()'d to reclaim
918 * @p: Points to a pointer to the current position in the input being processed.
919 * Updated to point at the first character after the current token
920 * @t: Pointers to a token to fill in
921 * @delim: Delimiter character to look for, either newline or space
922 * @lower: true to convert the string to lower case when storing
923 * Returns the new value of t->val, on success, NULL if out of memory
925 static char *get_string(char **p
, struct token
*t
, char delim
, int lower
)
931 * b and e both start at the beginning of the input stream.
933 * e is incremented until we find the ending delimiter, or a NUL byte
934 * is reached. Then, we take e - b to find the length of the token.
939 if ((delim
== ' ' && isspace(*e
)) || delim
== *e
)
947 * Allocate memory to hold the string, and copy it in, converting
948 * characters to lowercase if lower is != 0.
950 t
->val
= malloc(len
+ 1);
954 for (i
= 0; i
< len
; i
++, b
++) {
956 t
->val
[i
] = tolower(*b
);
963 /* Update *p so the caller knows where to continue scanning */
971 * get_keyword() - Populate a keyword token with a type and value
973 * Updates the ->type field based on the keyword string in @val
974 * @t: Token to populate
976 static void get_keyword(struct token
*t
)
980 for (i
= 0; keywords
[i
].val
; i
++) {
981 if (!strcmp(t
->val
, keywords
[i
].val
)) {
982 t
->type
= keywords
[i
].type
;
989 * get_token() - Get the next token
991 * We have to keep track of which state we're in to know if we're looking to get
992 * a string literal or a keyword.
994 * @p: Points to a pointer to the current position in the input being processed.
995 * Updated to point at the first character after the current token
997 static void get_token(char **p
, struct token
*t
, enum lex_state state
)
1001 t
->type
= T_INVALID
;
1003 /* eat non EOL whitespace */
1008 * eat comments. note that string literals can't begin with #, but
1009 * can contain a # after their first character.
1012 while (*c
&& *c
!= '\n')
1019 } else if (*c
== '\0') {
1022 } else if (state
== L_SLITERAL
) {
1023 get_string(&c
, t
, '\n', 0);
1024 } else if (state
== L_KEYWORD
) {
1026 * when we expect a keyword, we first get the next string
1027 * token delimited by whitespace, and then check if it
1028 * matches a keyword in our keyword list. if it does, it's
1029 * converted to a keyword token of the appropriate type, and
1030 * if not, it remains a string token.
1032 get_string(&c
, t
, ' ', 1);
1040 * eol_or_eof() - Find end of line
1042 * Increment *c until we get to the end of the current line, or EOF
1044 * @c: Points to a pointer to the current position in the input being processed.
1045 * Updated to point at the first character after the current token
1047 static void eol_or_eof(char **c
)
1049 while (**c
&& **c
!= '\n')
1054 * All of these parse_* functions share some common behavior.
1056 * They finish with *c pointing after the token they parse, and return 1 on
1057 * success, or < 0 on error.
1061 * Parse a string literal and store a pointer it at *dst. String literals
1062 * terminate at the end of the line.
1064 static int parse_sliteral(char **c
, char **dst
)
1069 get_token(c
, &t
, L_SLITERAL
);
1071 if (t
.type
!= T_STRING
) {
1072 printf("Expected string literal: %.*s\n", (int)(*c
- s
), s
);
1082 * Parse a base 10 (unsigned) integer and store it at *dst.
1084 static int parse_integer(char **c
, int *dst
)
1089 get_token(c
, &t
, L_SLITERAL
);
1090 if (t
.type
!= T_STRING
) {
1091 printf("Expected string: %.*s\n", (int)(*c
- s
), s
);
1095 *dst
= simple_strtol(t
.val
, NULL
, 10);
1102 static int parse_pxefile_top(struct pxe_context
*ctx
, char *p
, ulong base
,
1103 struct pxe_menu
*cfg
, int nest_level
);
1106 * Parse an include statement, and retrieve and parse the file it mentions.
1108 * base should point to a location where it's safe to store the file, and
1109 * nest_level should indicate how many nested includes have occurred. For this
1110 * include, nest_level has already been incremented and doesn't need to be
1113 static int handle_include(struct pxe_context
*ctx
, char **c
, unsigned long base
,
1114 struct pxe_menu
*cfg
, int nest_level
)
1122 err
= parse_sliteral(c
, &include_path
);
1124 printf("Expected include path: %.*s\n", (int)(*c
- s
), s
);
1128 err
= get_pxe_file(ctx
, include_path
, base
);
1130 printf("Couldn't retrieve %s\n", include_path
);
1134 buf
= map_sysmem(base
, 0);
1135 ret
= parse_pxefile_top(ctx
, buf
, base
, cfg
, nest_level
);
1142 * Parse lines that begin with 'menu'.
1144 * base and nest are provided to handle the 'menu include' case.
1146 * base should point to a location where it's safe to store the included file.
1148 * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
1149 * a file it includes, 3 when parsing a file included by that file, and so on.
1151 static int parse_menu(struct pxe_context
*ctx
, char **c
, struct pxe_menu
*cfg
,
1152 unsigned long base
, int nest_level
)
1158 get_token(c
, &t
, L_KEYWORD
);
1162 err
= parse_sliteral(c
, &cfg
->title
);
1167 err
= handle_include(ctx
, c
, base
, cfg
, nest_level
+ 1);
1171 err
= parse_sliteral(c
, &cfg
->bmp
);
1175 printf("Ignoring malformed menu command: %.*s\n",
1187 * Handles parsing a 'menu line' when we're parsing a label.
1189 static int parse_label_menu(char **c
, struct pxe_menu
*cfg
,
1190 struct pxe_label
*label
)
1197 get_token(c
, &t
, L_KEYWORD
);
1201 if (!cfg
->default_label
)
1202 cfg
->default_label
= strdup(label
->name
);
1204 if (!cfg
->default_label
)
1209 parse_sliteral(c
, &label
->menu
);
1212 printf("Ignoring malformed menu command: %.*s\n",
1222 * Handles parsing a 'kernel' label.
1223 * expecting "filename" or "<fit_filename>#cfg"
1225 static int parse_label_kernel(char **c
, struct pxe_label
*label
)
1230 err
= parse_sliteral(c
, &label
->kernel
);
1234 /* copy the kernel label to compare with FDT / INITRD when FIT is used */
1235 label
->kernel_label
= strdup(label
->kernel
);
1236 if (!label
->kernel_label
)
1239 s
= strstr(label
->kernel
, "#");
1243 label
->config
= strdup(s
);
1253 * Parses a label and adds it to the list of labels for a menu.
1255 * A label ends when we either get to the end of a file, or
1256 * get some input we otherwise don't have a handler defined
1260 static int parse_label(char **c
, struct pxe_menu
*cfg
)
1265 struct pxe_label
*label
;
1268 label
= label_create();
1272 err
= parse_sliteral(c
, &label
->name
);
1274 printf("Expected label name: %.*s\n", (int)(*c
- s
), s
);
1275 label_destroy(label
);
1279 list_add_tail(&label
->list
, &cfg
->labels
);
1283 get_token(c
, &t
, L_KEYWORD
);
1288 err
= parse_label_menu(c
, cfg
, label
);
1293 err
= parse_label_kernel(c
, label
);
1297 err
= parse_sliteral(c
, &label
->append
);
1300 s
= strstr(label
->append
, "initrd=");
1304 len
= (int)(strchr(s
, ' ') - s
);
1305 label
->initrd
= malloc(len
+ 1);
1306 strncpy(label
->initrd
, s
, len
);
1307 label
->initrd
[len
] = '\0';
1313 err
= parse_sliteral(c
, &label
->initrd
);
1318 err
= parse_sliteral(c
, &label
->fdt
);
1323 err
= parse_sliteral(c
, &label
->fdtdir
);
1327 if (!label
->fdtoverlays
)
1328 err
= parse_sliteral(c
, &label
->fdtoverlays
);
1332 label
->localboot
= 1;
1333 err
= parse_integer(c
, &label
->localboot_val
);
1337 err
= parse_integer(c
, &label
->ipappend
);
1341 label
->kaslrseed
= 1;
1349 * put the token back! we don't want it - it's the end
1350 * of a label and whatever token this is, it's
1351 * something for the menu level context to handle.
1363 * This 16 comes from the limit pxelinux imposes on nested includes.
1365 * There is no reason at all we couldn't do more, but some limit helps prevent
1366 * infinite (until crash occurs) recursion if a file tries to include itself.
1368 #define MAX_NEST_LEVEL 16
1371 * Entry point for parsing a menu file. nest_level indicates how many times
1372 * we've nested in includes. It will be 1 for the top level menu file.
1374 * Returns 1 on success, < 0 on error.
1376 static int parse_pxefile_top(struct pxe_context
*ctx
, char *p
, unsigned long base
,
1377 struct pxe_menu
*cfg
, int nest_level
)
1380 char *s
, *b
, *label_name
;
1385 if (nest_level
> MAX_NEST_LEVEL
) {
1386 printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL
);
1393 get_token(&p
, &t
, L_KEYWORD
);
1399 err
= parse_menu(ctx
, &p
, cfg
,
1400 base
+ ALIGN(strlen(b
) + 1, 4),
1405 err
= parse_integer(&p
, &cfg
->timeout
);
1409 err
= parse_label(&p
, cfg
);
1414 err
= parse_sliteral(&p
, &label_name
);
1417 if (cfg
->default_label
)
1418 free(cfg
->default_label
);
1420 cfg
->default_label
= label_name
;
1426 err
= handle_include(ctx
, &p
,
1427 base
+ ALIGN(strlen(b
), 4), cfg
,
1432 err
= parse_integer(&p
, &cfg
->prompt
);
1433 // Do not fail if prompt configuration is undefined
1445 printf("Ignoring unknown command: %.*s\n",
1457 void destroy_pxe_menu(struct pxe_menu
*cfg
)
1459 struct list_head
*pos
, *n
;
1460 struct pxe_label
*label
;
1463 free(cfg
->default_label
);
1465 list_for_each_safe(pos
, n
, &cfg
->labels
) {
1466 label
= list_entry(pos
, struct pxe_label
, list
);
1468 label_destroy(label
);
1474 struct pxe_menu
*parse_pxefile(struct pxe_context
*ctx
, unsigned long menucfg
)
1476 struct pxe_menu
*cfg
;
1480 cfg
= malloc(sizeof(struct pxe_menu
));
1484 memset(cfg
, 0, sizeof(struct pxe_menu
));
1486 INIT_LIST_HEAD(&cfg
->labels
);
1488 buf
= map_sysmem(menucfg
, 0);
1489 r
= parse_pxefile_top(ctx
, buf
, menucfg
, cfg
, 1);
1492 destroy_pxe_menu(cfg
);
1500 * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
1503 static struct menu
*pxe_menu_to_menu(struct pxe_menu
*cfg
)
1505 struct pxe_label
*label
;
1506 struct list_head
*pos
;
1508 char *label_override
;
1511 char *default_num
= NULL
;
1512 char *override_num
= NULL
;
1515 * Create a menu and add items for all the labels.
1517 m
= menu_create(cfg
->title
, DIV_ROUND_UP(cfg
->timeout
, 10),
1518 cfg
->prompt
, NULL
, label_print
, NULL
, NULL
);
1522 label_override
= env_get("pxe_label_override");
1524 list_for_each(pos
, &cfg
->labels
) {
1525 label
= list_entry(pos
, struct pxe_label
, list
);
1527 sprintf(label
->num
, "%d", i
++);
1528 if (menu_item_add(m
, label
->num
, label
) != 1) {
1532 if (cfg
->default_label
&&
1533 (strcmp(label
->name
, cfg
->default_label
) == 0))
1534 default_num
= label
->num
;
1535 if (label_override
&& !strcmp(label
->name
, label_override
))
1536 override_num
= label
->num
;
1540 if (label_override
) {
1542 default_num
= override_num
;
1544 printf("Missing override pxe label: %s\n",
1549 * After we've created items for each label in the menu, set the
1550 * menu's default label if one was specified.
1553 err
= menu_default_set(m
, default_num
);
1555 if (err
!= -ENOENT
) {
1560 printf("Missing default: %s\n", cfg
->default_label
);
1568 * Try to boot any labels we have yet to attempt to boot.
1570 static void boot_unattempted_labels(struct pxe_context
*ctx
,
1571 struct pxe_menu
*cfg
)
1573 struct list_head
*pos
;
1574 struct pxe_label
*label
;
1576 list_for_each(pos
, &cfg
->labels
) {
1577 label
= list_entry(pos
, struct pxe_label
, list
);
1579 if (!label
->attempted
)
1580 label_boot(ctx
, label
);
1584 void handle_pxe_menu(struct pxe_context
*ctx
, struct pxe_menu
*cfg
)
1590 if (IS_ENABLED(CONFIG_CMD_BMP
)) {
1591 /* display BMP if available */
1593 if (get_relfile(ctx
, cfg
->bmp
, image_load_addr
, NULL
)) {
1594 #if defined(CONFIG_VIDEO)
1595 struct udevice
*dev
;
1597 err
= uclass_first_device_err(UCLASS_VIDEO
, &dev
);
1601 bmp_display(image_load_addr
,
1602 BMP_ALIGN_CENTER
, BMP_ALIGN_CENTER
);
1604 printf("Skipping background bmp %s for failure\n",
1610 m
= pxe_menu_to_menu(cfg
);
1614 err
= menu_get_choice(m
, &choice
);
1618 * err == 1 means we got a choice back from menu_get_choice.
1620 * err == -ENOENT if the menu was setup to select the default but no
1621 * default was set. in that case, we should continue trying to boot
1622 * labels that haven't been attempted yet.
1624 * otherwise, the user interrupted or there was some other error and
1629 err
= label_boot(ctx
, choice
);
1632 } else if (err
!= -ENOENT
) {
1636 boot_unattempted_labels(ctx
, cfg
);
1639 int pxe_setup_ctx(struct pxe_context
*ctx
, struct cmd_tbl
*cmdtp
,
1640 pxe_getfile_func getfile
, void *userdata
,
1641 bool allow_abs_path
, const char *bootfile
, bool use_ipv6
)
1643 const char *last_slash
;
1644 size_t path_len
= 0;
1646 memset(ctx
, '\0', sizeof(*ctx
));
1648 ctx
->getfile
= getfile
;
1649 ctx
->userdata
= userdata
;
1650 ctx
->allow_abs_path
= allow_abs_path
;
1651 ctx
->use_ipv6
= use_ipv6
;
1653 /* figure out the boot directory, if there is one */
1654 if (bootfile
&& strlen(bootfile
) >= MAX_TFTP_PATH_LEN
)
1656 ctx
->bootdir
= strdup(bootfile
? bootfile
: "");
1661 last_slash
= strrchr(bootfile
, '/');
1663 path_len
= (last_slash
- bootfile
) + 1;
1665 ctx
->bootdir
[path_len
] = '\0';
1670 void pxe_destroy_ctx(struct pxe_context
*ctx
)
1675 int pxe_process(struct pxe_context
*ctx
, ulong pxefile_addr_r
, bool prompt
)
1677 struct pxe_menu
*cfg
;
1679 cfg
= parse_pxefile(ctx
, pxefile_addr_r
);
1681 printf("Error parsing config file\n");
1688 handle_pxe_menu(ctx
, cfg
);
1690 destroy_pxe_menu(cfg
);