Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1 | // 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> |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 8 | #include <command.h> |
Patrick Delaunay | e6fe02a | 2022-03-22 17:08:43 +0100 | [diff] [blame] | 9 | #include <dm.h> |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 10 | #include <env.h> |
Simon Glass | 8e8ccfe | 2019-12-28 10:45:03 -0700 | [diff] [blame] | 11 | #include <image.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | #include <mapmem.h> |
| 15 | #include <lcd.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 16 | #include <net.h> |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 17 | #include <fdt_support.h> |
Patrick Delaunay | e6fe02a | 2022-03-22 17:08:43 +0100 | [diff] [blame] | 18 | #include <video.h> |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 19 | #include <linux/libfdt.h> |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 20 | #include <linux/string.h> |
| 21 | #include <linux/ctype.h> |
| 22 | #include <errno.h> |
| 23 | #include <linux/list.h> |
| 24 | |
Zhang Ning | 0290146 | 2022-02-01 08:33:37 +0800 | [diff] [blame] | 25 | #ifdef CONFIG_DM_RNG |
Zhang Ning | 0290146 | 2022-02-01 08:33:37 +0800 | [diff] [blame] | 26 | #include <rng.h> |
| 27 | #endif |
| 28 | |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 29 | #include <splash.h> |
| 30 | #include <asm/io.h> |
| 31 | |
| 32 | #include "menu.h" |
| 33 | #include "cli.h" |
| 34 | |
| 35 | #include "pxe_utils.h" |
| 36 | |
Ben Wolsieffer | 2c4e067 | 2019-11-28 00:07:08 -0500 | [diff] [blame] | 37 | #define MAX_TFTP_PATH_LEN 512 |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 38 | |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 39 | int pxe_get_file_size(ulong *sizep) |
| 40 | { |
| 41 | const char *val; |
| 42 | |
| 43 | val = from_env("filesize"); |
| 44 | if (!val) |
| 45 | return -ENOENT; |
| 46 | |
| 47 | if (strict_strtoul(val, 16, sizep) < 0) |
| 48 | return -EINVAL; |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 53 | /** |
| 54 | * format_mac_pxe() - obtain a MAC address in the PXE format |
| 55 | * |
| 56 | * This produces a MAC-address string in the format for the current ethernet |
| 57 | * device: |
| 58 | * |
| 59 | * 01-aa-bb-cc-dd-ee-ff |
| 60 | * |
| 61 | * where aa-ff is the MAC address in hex |
| 62 | * |
| 63 | * @outbuf: Buffer to write string to |
| 64 | * @outbuf_len: length of buffer |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 65 | * Return: 1 if OK, -ENOSPC if buffer is too small, -ENOENT is there is no |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 66 | * current ethernet device |
| 67 | */ |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 68 | int format_mac_pxe(char *outbuf, size_t outbuf_len) |
| 69 | { |
| 70 | uchar ethaddr[6]; |
| 71 | |
| 72 | if (outbuf_len < 21) { |
| 73 | printf("outbuf is too small (%zd < 21)\n", outbuf_len); |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 74 | return -ENOSPC; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr)) |
| 78 | return -ENOENT; |
| 79 | |
| 80 | sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x", |
| 81 | ethaddr[0], ethaddr[1], ethaddr[2], |
| 82 | ethaddr[3], ethaddr[4], ethaddr[5]); |
| 83 | |
| 84 | return 1; |
| 85 | } |
| 86 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 87 | /** |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 88 | * get_relfile() - read a file relative to the PXE file |
| 89 | * |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 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. |
| 94 | * |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 95 | * @ctx: PXE context |
| 96 | * @file_path: File path to read (relative to the PXE file) |
| 97 | * @file_addr: Address to load file to |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 98 | * @filesizep: If not NULL, returns the file size in bytes |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 99 | * Returns 1 for success, or < 0 on error |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 100 | */ |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 101 | static int get_relfile(struct pxe_context *ctx, const char *file_path, |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 102 | unsigned long file_addr, ulong *filesizep) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 103 | { |
| 104 | size_t path_len; |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 105 | char relfile[MAX_TFTP_PATH_LEN + 1]; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 106 | char addr_buf[18]; |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 107 | ulong size; |
| 108 | int ret; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 109 | |
Simon Glass | 74b7a2b | 2021-10-14 12:48:05 -0600 | [diff] [blame] | 110 | if (file_path[0] == '/' && ctx->allow_abs_path) |
| 111 | *relfile = '\0'; |
| 112 | else |
| 113 | strncpy(relfile, ctx->bootdir, MAX_TFTP_PATH_LEN); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 114 | |
Simon Glass | 74b7a2b | 2021-10-14 12:48:05 -0600 | [diff] [blame] | 115 | path_len = strlen(file_path) + strlen(relfile); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 116 | |
| 117 | if (path_len > MAX_TFTP_PATH_LEN) { |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 118 | printf("Base path too long (%s%s)\n", relfile, file_path); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 119 | |
| 120 | return -ENAMETOOLONG; |
| 121 | } |
| 122 | |
| 123 | strcat(relfile, file_path); |
| 124 | |
| 125 | printf("Retrieving file: %s\n", relfile); |
| 126 | |
| 127 | sprintf(addr_buf, "%lx", file_addr); |
| 128 | |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 129 | ret = ctx->getfile(ctx, relfile, addr_buf, &size); |
| 130 | if (ret < 0) |
| 131 | return log_msg_ret("get", ret); |
| 132 | if (filesizep) |
| 133 | *filesizep = size; |
| 134 | |
| 135 | return 1; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 138 | /** |
| 139 | * get_pxe_file() - read a file |
| 140 | * |
| 141 | * The file is read and nul-terminated |
| 142 | * |
| 143 | * @ctx: PXE context |
| 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 |
| 147 | */ |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 148 | int get_pxe_file(struct pxe_context *ctx, const char *file_path, |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 149 | ulong file_addr) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 150 | { |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 151 | ulong size; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 152 | int err; |
| 153 | char *buf; |
| 154 | |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 155 | err = get_relfile(ctx, file_path, file_addr, &size); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 156 | if (err < 0) |
| 157 | return err; |
| 158 | |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 159 | buf = map_sysmem(file_addr + size, 1); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 160 | *buf = '\0'; |
| 161 | unmap_sysmem(buf); |
| 162 | |
| 163 | return 1; |
| 164 | } |
| 165 | |
| 166 | #define PXELINUX_DIR "pxelinux.cfg/" |
| 167 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 168 | /** |
| 169 | * get_pxelinux_path() - Get a file in the pxelinux.cfg/ directory |
| 170 | * |
| 171 | * @ctx: PXE context |
| 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 |
| 175 | */ |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 176 | int get_pxelinux_path(struct pxe_context *ctx, const char *file, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 177 | unsigned long pxefile_addr_r) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 178 | { |
| 179 | size_t base_len = strlen(PXELINUX_DIR); |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 180 | char path[MAX_TFTP_PATH_LEN + 1]; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 181 | |
| 182 | if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) { |
| 183 | printf("path (%s%s) too long, skipping\n", |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 184 | PXELINUX_DIR, file); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 185 | return -ENAMETOOLONG; |
| 186 | } |
| 187 | |
| 188 | sprintf(path, PXELINUX_DIR "%s", file); |
| 189 | |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 190 | return get_pxe_file(ctx, path, pxefile_addr_r); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 193 | /** |
| 194 | * get_relfile_envaddr() - read a file to an address in an env var |
| 195 | * |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 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. |
| 199 | * |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 200 | * @ctx: PXE context |
| 201 | * @file_path: File path to read (relative to the PXE file) |
| 202 | * @envaddr_name: Name of environment variable which contains the address to |
| 203 | * load to |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 204 | * @filesizep: Returns the file size in bytes |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 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 |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 208 | */ |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 209 | static int get_relfile_envaddr(struct pxe_context *ctx, const char *file_path, |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 210 | const char *envaddr_name, ulong *filesizep) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 211 | { |
| 212 | unsigned long file_addr; |
| 213 | char *envaddr; |
| 214 | |
| 215 | envaddr = from_env(envaddr_name); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 216 | if (!envaddr) |
| 217 | return -ENOENT; |
| 218 | |
| 219 | if (strict_strtoul(envaddr, 16, &file_addr) < 0) |
| 220 | return -EINVAL; |
| 221 | |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 222 | return get_relfile(ctx, file_path, file_addr, filesizep); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 225 | /** |
| 226 | * label_create() - crate a new PXE label |
| 227 | * |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 228 | * Allocates memory for and initializes a pxe_label. This uses malloc, so the |
| 229 | * result must be free()'d to reclaim the memory. |
| 230 | * |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 231 | * Returns a pointer to the label, or NULL if out of memory |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 232 | */ |
| 233 | static struct pxe_label *label_create(void) |
| 234 | { |
| 235 | struct pxe_label *label; |
| 236 | |
| 237 | label = malloc(sizeof(struct pxe_label)); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 238 | if (!label) |
| 239 | return NULL; |
| 240 | |
| 241 | memset(label, 0, sizeof(struct pxe_label)); |
| 242 | |
| 243 | return label; |
| 244 | } |
| 245 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 246 | /** |
| 247 | * label_destroy() - free the memory used by a pxe_label |
| 248 | * |
| 249 | * This frees @label itself as well as memory used by its name, |
| 250 | * kernel, config, append, initrd, fdt, fdtdir and fdtoverlay members, if |
| 251 | * they're non-NULL. |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 252 | * |
| 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. |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 256 | * |
| 257 | * @label: Label to free |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 258 | */ |
| 259 | static void label_destroy(struct pxe_label *label) |
| 260 | { |
Simon Glass | 929860b | 2021-10-14 12:48:02 -0600 | [diff] [blame] | 261 | free(label->name); |
| 262 | free(label->kernel); |
| 263 | free(label->config); |
| 264 | free(label->append); |
| 265 | free(label->initrd); |
| 266 | free(label->fdt); |
| 267 | free(label->fdtdir); |
| 268 | free(label->fdtoverlays); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 269 | free(label); |
| 270 | } |
| 271 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 272 | /** |
| 273 | * label_print() - Print a label and its string members if they're defined |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 274 | * |
| 275 | * This is passed as a callback to the menu code for displaying each |
| 276 | * menu entry. |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 277 | * |
| 278 | * @data: Label to print (is cast to struct pxe_label *) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 279 | */ |
| 280 | static void label_print(void *data) |
| 281 | { |
| 282 | struct pxe_label *label = data; |
| 283 | const char *c = label->menu ? label->menu : label->name; |
| 284 | |
| 285 | printf("%s:\t%s\n", label->num, c); |
| 286 | } |
| 287 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 288 | /** |
| 289 | * label_localboot() - Boot a label that specified 'localboot' |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 290 | * |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 291 | * This requires that the 'localcmd' environment variable is defined. Its |
| 292 | * contents will be executed as U-Boot commands. If the label specified an |
| 293 | * 'append' line, its contents will be used to overwrite the contents of the |
| 294 | * 'bootargs' environment variable prior to running 'localcmd'. |
| 295 | * |
| 296 | * @label: Label to process |
| 297 | * Returns 1 on success or < 0 on error |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 298 | */ |
| 299 | static int label_localboot(struct pxe_label *label) |
| 300 | { |
| 301 | char *localcmd; |
| 302 | |
| 303 | localcmd = from_env("localcmd"); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 304 | if (!localcmd) |
| 305 | return -ENOENT; |
| 306 | |
| 307 | if (label->append) { |
| 308 | char bootargs[CONFIG_SYS_CBSIZE]; |
| 309 | |
Simon Glass | 1a62d64 | 2020-11-05 10:33:47 -0700 | [diff] [blame] | 310 | cli_simple_process_macros(label->append, bootargs, |
| 311 | sizeof(bootargs)); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 312 | env_set("bootargs", bootargs); |
| 313 | } |
| 314 | |
| 315 | debug("running: %s\n", localcmd); |
| 316 | |
| 317 | return run_command_list(localcmd, strlen(localcmd), 0); |
| 318 | } |
| 319 | |
Zhang Ning | 0290146 | 2022-02-01 08:33:37 +0800 | [diff] [blame] | 320 | /* |
| 321 | * label_boot_kaslrseed generate kaslrseed from hw rng |
| 322 | */ |
| 323 | |
| 324 | static void label_boot_kaslrseed(void) |
| 325 | { |
| 326 | #ifdef CONFIG_DM_RNG |
| 327 | ulong fdt_addr; |
| 328 | struct fdt_header *working_fdt; |
| 329 | size_t n = 0x8; |
| 330 | struct udevice *dev; |
| 331 | u64 *buf; |
| 332 | int nodeoffset; |
| 333 | int err; |
| 334 | |
| 335 | /* Get the main fdt and map it */ |
| 336 | fdt_addr = hextoul(env_get("fdt_addr_r"), NULL); |
| 337 | working_fdt = map_sysmem(fdt_addr, 0); |
| 338 | err = fdt_check_header(working_fdt); |
| 339 | if (err) |
| 340 | return; |
| 341 | |
| 342 | /* add extra size for holding kaslr-seed */ |
| 343 | /* err is new fdt size, 0 or negtive */ |
| 344 | err = fdt_shrink_to_minimum(working_fdt, 512); |
| 345 | if (err <= 0) |
| 346 | return; |
| 347 | |
| 348 | if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) { |
| 349 | printf("No RNG device\n"); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | nodeoffset = fdt_find_or_add_subnode(working_fdt, 0, "chosen"); |
| 354 | if (nodeoffset < 0) { |
| 355 | printf("Reading chosen node failed\n"); |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | buf = malloc(n); |
| 360 | if (!buf) { |
| 361 | printf("Out of memory\n"); |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | if (dm_rng_read(dev, buf, n)) { |
| 366 | printf("Reading RNG failed\n"); |
| 367 | goto err; |
| 368 | } |
| 369 | |
| 370 | err = fdt_setprop(working_fdt, nodeoffset, "kaslr-seed", buf, sizeof(buf)); |
| 371 | if (err < 0) { |
| 372 | printf("Unable to set kaslr-seed on chosen node: %s\n", fdt_strerror(err)); |
| 373 | goto err; |
| 374 | } |
| 375 | err: |
| 376 | free(buf); |
| 377 | #endif |
| 378 | return; |
| 379 | } |
| 380 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 381 | /** |
| 382 | * label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays' |
Edoardo Tomelleri | 35821a2 | 2022-09-21 15:26:33 +0200 | [diff] [blame] | 383 | * or 'devicetree-overlay' |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 384 | * |
| 385 | * @ctx: PXE context |
| 386 | * @label: Label to process |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 387 | */ |
| 388 | #ifdef CONFIG_OF_LIBFDT_OVERLAY |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 389 | static void label_boot_fdtoverlay(struct pxe_context *ctx, |
| 390 | struct pxe_label *label) |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 391 | { |
| 392 | char *fdtoverlay = label->fdtoverlays; |
| 393 | struct fdt_header *working_fdt; |
| 394 | char *fdtoverlay_addr_env; |
| 395 | ulong fdtoverlay_addr; |
| 396 | ulong fdt_addr; |
| 397 | int err; |
| 398 | |
| 399 | /* Get the main fdt and map it */ |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 400 | fdt_addr = hextoul(env_get("fdt_addr_r"), NULL); |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 401 | working_fdt = map_sysmem(fdt_addr, 0); |
| 402 | err = fdt_check_header(working_fdt); |
| 403 | if (err) |
| 404 | return; |
| 405 | |
| 406 | /* Get the specific overlay loading address */ |
| 407 | fdtoverlay_addr_env = env_get("fdtoverlay_addr_r"); |
| 408 | if (!fdtoverlay_addr_env) { |
| 409 | printf("Invalid fdtoverlay_addr_r for loading overlays\n"); |
| 410 | return; |
| 411 | } |
| 412 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 413 | fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL); |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 414 | |
| 415 | /* Cycle over the overlay files and apply them in order */ |
| 416 | do { |
| 417 | struct fdt_header *blob; |
| 418 | char *overlayfile; |
| 419 | char *end; |
| 420 | int len; |
| 421 | |
| 422 | /* Drop leading spaces */ |
| 423 | while (*fdtoverlay == ' ') |
| 424 | ++fdtoverlay; |
| 425 | |
| 426 | /* Copy a single filename if multiple provided */ |
| 427 | end = strstr(fdtoverlay, " "); |
| 428 | if (end) { |
| 429 | len = (int)(end - fdtoverlay); |
| 430 | overlayfile = malloc(len + 1); |
| 431 | strncpy(overlayfile, fdtoverlay, len); |
| 432 | overlayfile[len] = '\0'; |
| 433 | } else |
| 434 | overlayfile = fdtoverlay; |
| 435 | |
| 436 | if (!strlen(overlayfile)) |
| 437 | goto skip_overlay; |
| 438 | |
| 439 | /* Load overlay file */ |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 440 | err = get_relfile_envaddr(ctx, overlayfile, "fdtoverlay_addr_r", |
| 441 | NULL); |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 442 | if (err < 0) { |
| 443 | printf("Failed loading overlay %s\n", overlayfile); |
| 444 | goto skip_overlay; |
| 445 | } |
| 446 | |
| 447 | /* Resize main fdt */ |
| 448 | fdt_shrink_to_minimum(working_fdt, 8192); |
| 449 | |
| 450 | blob = map_sysmem(fdtoverlay_addr, 0); |
| 451 | err = fdt_check_header(blob); |
| 452 | if (err) { |
| 453 | printf("Invalid overlay %s, skipping\n", |
| 454 | overlayfile); |
| 455 | goto skip_overlay; |
| 456 | } |
| 457 | |
| 458 | err = fdt_overlay_apply_verbose(working_fdt, blob); |
| 459 | if (err) { |
| 460 | printf("Failed to apply overlay %s, skipping\n", |
| 461 | overlayfile); |
| 462 | goto skip_overlay; |
| 463 | } |
| 464 | |
| 465 | skip_overlay: |
| 466 | if (end) |
| 467 | free(overlayfile); |
| 468 | } while ((fdtoverlay = strstr(fdtoverlay, " "))); |
| 469 | } |
| 470 | #endif |
| 471 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 472 | /** |
| 473 | * label_boot() - Boot according to the contents of a pxe_label |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 474 | * |
| 475 | * If we can't boot for any reason, we return. A successful boot never |
| 476 | * returns. |
| 477 | * |
| 478 | * The kernel will be stored in the location given by the 'kernel_addr_r' |
| 479 | * environment variable. |
| 480 | * |
| 481 | * If the label specifies an initrd file, it will be stored in the location |
| 482 | * given by the 'ramdisk_addr_r' environment variable. |
| 483 | * |
| 484 | * If the label specifies an 'append' line, its contents will overwrite that |
| 485 | * of the 'bootargs' environment variable. |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 486 | * |
| 487 | * @ctx: PXE context |
| 488 | * @label: Label to process |
| 489 | * Returns does not return on success, otherwise returns 0 if a localboot |
| 490 | * label was processed, or 1 on error |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 491 | */ |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 492 | static int label_boot(struct pxe_context *ctx, struct pxe_label *label) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 493 | { |
| 494 | char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL }; |
Zhaofeng Li | 23f3e39 | 2021-10-20 00:18:14 -0700 | [diff] [blame] | 495 | char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL }; |
Zhaofeng Li | c97bd17 | 2021-10-20 00:18:15 -0700 | [diff] [blame] | 496 | char *kernel_addr = NULL; |
| 497 | char *initrd_addr_str = NULL; |
Zhaofeng Li | 23f3e39 | 2021-10-20 00:18:14 -0700 | [diff] [blame] | 498 | char initrd_filesize[10]; |
Zhaofeng Li | c97bd17 | 2021-10-20 00:18:15 -0700 | [diff] [blame] | 499 | char initrd_str[28]; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 500 | char mac_str[29] = ""; |
| 501 | char ip_str[68] = ""; |
| 502 | char *fit_addr = NULL; |
| 503 | int bootm_argc = 2; |
Zhaofeng Li | 23f3e39 | 2021-10-20 00:18:14 -0700 | [diff] [blame] | 504 | int zboot_argc = 3; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 505 | int len = 0; |
Zhaofeng Li | c97bd17 | 2021-10-20 00:18:15 -0700 | [diff] [blame] | 506 | ulong kernel_addr_r; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 507 | void *buf; |
| 508 | |
| 509 | label_print(label); |
| 510 | |
| 511 | label->attempted = 1; |
| 512 | |
| 513 | if (label->localboot) { |
| 514 | if (label->localboot_val >= 0) |
| 515 | label_localboot(label); |
| 516 | return 0; |
| 517 | } |
| 518 | |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 519 | if (!label->kernel) { |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 520 | printf("No kernel given, skipping %s\n", |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 521 | label->name); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 522 | return 1; |
| 523 | } |
| 524 | |
| 525 | if (label->initrd) { |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 526 | ulong size; |
| 527 | |
| 528 | if (get_relfile_envaddr(ctx, label->initrd, "ramdisk_addr_r", |
| 529 | &size) < 0) { |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 530 | printf("Skipping %s for failure retrieving initrd\n", |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 531 | label->name); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 532 | return 1; |
| 533 | } |
| 534 | |
Zhaofeng Li | c97bd17 | 2021-10-20 00:18:15 -0700 | [diff] [blame] | 535 | initrd_addr_str = env_get("ramdisk_addr_r"); |
Heinrich Schuchardt | 085cbda | 2021-11-15 19:26:51 +0100 | [diff] [blame] | 536 | size = snprintf(initrd_str, sizeof(initrd_str), "%s:%lx", |
| 537 | initrd_addr_str, size); |
| 538 | if (size >= sizeof(initrd_str)) |
| 539 | return 1; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 540 | } |
| 541 | |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 542 | if (get_relfile_envaddr(ctx, label->kernel, "kernel_addr_r", |
| 543 | NULL) < 0) { |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 544 | printf("Skipping %s for failure retrieving kernel\n", |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 545 | label->name); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 546 | return 1; |
| 547 | } |
| 548 | |
| 549 | if (label->ipappend & 0x1) { |
| 550 | sprintf(ip_str, " ip=%s:%s:%s:%s", |
| 551 | env_get("ipaddr"), env_get("serverip"), |
| 552 | env_get("gatewayip"), env_get("netmask")); |
| 553 | } |
| 554 | |
Kory Maincent | ff0287e | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 555 | if (IS_ENABLED(CONFIG_CMD_NET)) { |
| 556 | if (label->ipappend & 0x2) { |
| 557 | int err; |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 558 | |
Kory Maincent | ff0287e | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 559 | strcpy(mac_str, " BOOTIF="); |
| 560 | err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8); |
| 561 | if (err < 0) |
| 562 | mac_str[0] = '\0'; |
| 563 | } |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 564 | } |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 565 | |
| 566 | if ((label->ipappend & 0x3) || label->append) { |
| 567 | char bootargs[CONFIG_SYS_CBSIZE] = ""; |
| 568 | char finalbootargs[CONFIG_SYS_CBSIZE]; |
| 569 | |
| 570 | if (strlen(label->append ?: "") + |
| 571 | strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) { |
| 572 | printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n", |
| 573 | strlen(label->append ?: ""), |
| 574 | strlen(ip_str), strlen(mac_str), |
| 575 | sizeof(bootargs)); |
| 576 | return 1; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 577 | } |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 578 | |
| 579 | if (label->append) |
| 580 | strncpy(bootargs, label->append, sizeof(bootargs)); |
| 581 | |
| 582 | strcat(bootargs, ip_str); |
| 583 | strcat(bootargs, mac_str); |
| 584 | |
Simon Glass | 1a62d64 | 2020-11-05 10:33:47 -0700 | [diff] [blame] | 585 | cli_simple_process_macros(bootargs, finalbootargs, |
| 586 | sizeof(finalbootargs)); |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 587 | env_set("bootargs", finalbootargs); |
| 588 | printf("append: %s\n", finalbootargs); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 589 | } |
| 590 | |
Zhaofeng Li | c97bd17 | 2021-10-20 00:18:15 -0700 | [diff] [blame] | 591 | kernel_addr = env_get("kernel_addr_r"); |
Zhaofeng Li | 23f3e39 | 2021-10-20 00:18:14 -0700 | [diff] [blame] | 592 | |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 593 | /* for FIT, append the configuration identifier */ |
| 594 | if (label->config) { |
Zhaofeng Li | c97bd17 | 2021-10-20 00:18:15 -0700 | [diff] [blame] | 595 | int len = strlen(kernel_addr) + strlen(label->config) + 1; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 596 | |
| 597 | fit_addr = malloc(len); |
| 598 | if (!fit_addr) { |
| 599 | printf("malloc fail (FIT address)\n"); |
| 600 | return 1; |
| 601 | } |
Zhaofeng Li | c97bd17 | 2021-10-20 00:18:15 -0700 | [diff] [blame] | 602 | snprintf(fit_addr, len, "%s%s", kernel_addr, label->config); |
| 603 | kernel_addr = fit_addr; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /* |
| 607 | * fdt usage is optional: |
Anton Leontiev | db36674 | 2019-09-03 10:52:24 +0300 | [diff] [blame] | 608 | * It handles the following scenarios. |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 609 | * |
Anton Leontiev | db36674 | 2019-09-03 10:52:24 +0300 | [diff] [blame] | 610 | * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is |
| 611 | * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to |
| 612 | * bootm, and adjust argc appropriately. |
| 613 | * |
| 614 | * If retrieve fails and no exact fdt blob is specified in pxe file with |
| 615 | * "fdt" label, try Scenario 2. |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 616 | * |
| 617 | * Scenario 2: If there is an fdt_addr specified, pass it along to |
| 618 | * bootm, and adjust argc appropriately. |
| 619 | * |
Peter Hoyes | d5ba618 | 2021-10-14 09:40:04 +0100 | [diff] [blame] | 620 | * Scenario 3: If there is an fdtcontroladdr specified, pass it along to |
| 621 | * bootm, and adjust argc appropriately. |
| 622 | * |
| 623 | * Scenario 4: fdt blob is not available. |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 624 | */ |
| 625 | bootm_argv[3] = env_get("fdt_addr_r"); |
| 626 | |
| 627 | /* if fdt label is defined then get fdt from server */ |
| 628 | if (bootm_argv[3]) { |
| 629 | char *fdtfile = NULL; |
| 630 | char *fdtfilefree = NULL; |
| 631 | |
| 632 | if (label->fdt) { |
| 633 | fdtfile = label->fdt; |
| 634 | } else if (label->fdtdir) { |
| 635 | char *f1, *f2, *f3, *f4, *slash; |
| 636 | |
| 637 | f1 = env_get("fdtfile"); |
| 638 | if (f1) { |
| 639 | f2 = ""; |
| 640 | f3 = ""; |
| 641 | f4 = ""; |
| 642 | } else { |
| 643 | /* |
| 644 | * For complex cases where this code doesn't |
| 645 | * generate the correct filename, the board |
| 646 | * code should set $fdtfile during early boot, |
| 647 | * or the boot scripts should set $fdtfile |
| 648 | * before invoking "pxe" or "sysboot". |
| 649 | */ |
| 650 | f1 = env_get("soc"); |
| 651 | f2 = "-"; |
| 652 | f3 = env_get("board"); |
| 653 | f4 = ".dtb"; |
Dimitri John Ledkov | 75efe7d | 2021-04-21 12:42:01 +0100 | [diff] [blame] | 654 | if (!f1) { |
| 655 | f1 = ""; |
| 656 | f2 = ""; |
| 657 | } |
| 658 | if (!f3) { |
| 659 | f2 = ""; |
| 660 | f3 = ""; |
| 661 | } |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | len = strlen(label->fdtdir); |
| 665 | if (!len) |
| 666 | slash = "./"; |
| 667 | else if (label->fdtdir[len - 1] != '/') |
| 668 | slash = "/"; |
| 669 | else |
| 670 | slash = ""; |
| 671 | |
| 672 | len = strlen(label->fdtdir) + strlen(slash) + |
| 673 | strlen(f1) + strlen(f2) + strlen(f3) + |
| 674 | strlen(f4) + 1; |
| 675 | fdtfilefree = malloc(len); |
| 676 | if (!fdtfilefree) { |
| 677 | printf("malloc fail (FDT filename)\n"); |
| 678 | goto cleanup; |
| 679 | } |
| 680 | |
| 681 | snprintf(fdtfilefree, len, "%s%s%s%s%s%s", |
| 682 | label->fdtdir, slash, f1, f2, f3, f4); |
| 683 | fdtfile = fdtfilefree; |
| 684 | } |
| 685 | |
| 686 | if (fdtfile) { |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 687 | int err = get_relfile_envaddr(ctx, fdtfile, |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 688 | "fdt_addr_r", NULL); |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 689 | |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 690 | free(fdtfilefree); |
| 691 | if (err < 0) { |
Anton Leontiev | db36674 | 2019-09-03 10:52:24 +0300 | [diff] [blame] | 692 | bootm_argv[3] = NULL; |
| 693 | |
| 694 | if (label->fdt) { |
| 695 | printf("Skipping %s for failure retrieving FDT\n", |
| 696 | label->name); |
| 697 | goto cleanup; |
| 698 | } |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 699 | } |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 700 | |
Zhang Ning | 0290146 | 2022-02-01 08:33:37 +0800 | [diff] [blame] | 701 | if (label->kaslrseed) |
| 702 | label_boot_kaslrseed(); |
| 703 | |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 704 | #ifdef CONFIG_OF_LIBFDT_OVERLAY |
| 705 | if (label->fdtoverlays) |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 706 | label_boot_fdtoverlay(ctx, label); |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 707 | #endif |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 708 | } else { |
| 709 | bootm_argv[3] = NULL; |
| 710 | } |
| 711 | } |
| 712 | |
Zhaofeng Li | c97bd17 | 2021-10-20 00:18:15 -0700 | [diff] [blame] | 713 | bootm_argv[1] = kernel_addr; |
| 714 | zboot_argv[1] = kernel_addr; |
| 715 | |
| 716 | if (initrd_addr_str) { |
| 717 | bootm_argv[2] = initrd_str; |
| 718 | bootm_argc = 3; |
| 719 | |
| 720 | zboot_argv[3] = initrd_addr_str; |
| 721 | zboot_argv[4] = initrd_filesize; |
| 722 | zboot_argc = 5; |
| 723 | } |
| 724 | |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 725 | if (!bootm_argv[3]) |
| 726 | bootm_argv[3] = env_get("fdt_addr"); |
| 727 | |
Peter Hoyes | d5ba618 | 2021-10-14 09:40:04 +0100 | [diff] [blame] | 728 | if (!bootm_argv[3]) |
| 729 | bootm_argv[3] = env_get("fdtcontroladdr"); |
| 730 | |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 731 | if (bootm_argv[3]) { |
| 732 | if (!bootm_argv[2]) |
| 733 | bootm_argv[2] = "-"; |
| 734 | bootm_argc = 4; |
| 735 | } |
| 736 | |
Zhaofeng Li | c97bd17 | 2021-10-20 00:18:15 -0700 | [diff] [blame] | 737 | kernel_addr_r = genimg_get_kernel_addr(kernel_addr); |
| 738 | buf = map_sysmem(kernel_addr_r, 0); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 739 | /* Try bootm for legacy and FIT format image */ |
John Keeping | be43a35 | 2022-07-28 11:19:15 +0100 | [diff] [blame] | 740 | if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID && |
| 741 | IS_ENABLED(CONFIG_CMD_BOOTM)) |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 742 | do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 743 | /* Try booting an AArch64 Linux kernel image */ |
Kory Maincent | ff0287e | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 744 | else if (IS_ENABLED(CONFIG_CMD_BOOTI)) |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 745 | do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 746 | /* Try booting a Image */ |
Kory Maincent | ff0287e | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 747 | else if (IS_ENABLED(CONFIG_CMD_BOOTZ)) |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 748 | do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv); |
Kory Maincent | 18c2582 | 2021-02-02 16:42:29 +0100 | [diff] [blame] | 749 | /* Try booting an x86_64 Linux kernel image */ |
| 750 | else if (IS_ENABLED(CONFIG_CMD_ZBOOT)) |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 751 | do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL); |
Kory Maincent | ff0287e | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 752 | |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 753 | unmap_sysmem(buf); |
| 754 | |
| 755 | cleanup: |
Simon Glass | 929860b | 2021-10-14 12:48:02 -0600 | [diff] [blame] | 756 | free(fit_addr); |
| 757 | |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 758 | return 1; |
| 759 | } |
| 760 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 761 | /** enum token_type - Tokens for the pxe file parser */ |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 762 | enum token_type { |
| 763 | T_EOL, |
| 764 | T_STRING, |
| 765 | T_EOF, |
| 766 | T_MENU, |
| 767 | T_TITLE, |
| 768 | T_TIMEOUT, |
| 769 | T_LABEL, |
| 770 | T_KERNEL, |
| 771 | T_LINUX, |
| 772 | T_APPEND, |
| 773 | T_INITRD, |
| 774 | T_LOCALBOOT, |
| 775 | T_DEFAULT, |
| 776 | T_PROMPT, |
| 777 | T_INCLUDE, |
| 778 | T_FDT, |
| 779 | T_FDTDIR, |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 780 | T_FDTOVERLAYS, |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 781 | T_ONTIMEOUT, |
| 782 | T_IPAPPEND, |
| 783 | T_BACKGROUND, |
Zhang Ning | 0290146 | 2022-02-01 08:33:37 +0800 | [diff] [blame] | 784 | T_KASLRSEED, |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 785 | T_INVALID |
| 786 | }; |
| 787 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 788 | /** struct token - token - given by a value and a type */ |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 789 | struct token { |
| 790 | char *val; |
| 791 | enum token_type type; |
| 792 | }; |
| 793 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 794 | /* Keywords recognized */ |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 795 | static const struct token keywords[] = { |
| 796 | {"menu", T_MENU}, |
| 797 | {"title", T_TITLE}, |
| 798 | {"timeout", T_TIMEOUT}, |
| 799 | {"default", T_DEFAULT}, |
| 800 | {"prompt", T_PROMPT}, |
| 801 | {"label", T_LABEL}, |
| 802 | {"kernel", T_KERNEL}, |
| 803 | {"linux", T_LINUX}, |
| 804 | {"localboot", T_LOCALBOOT}, |
| 805 | {"append", T_APPEND}, |
| 806 | {"initrd", T_INITRD}, |
| 807 | {"include", T_INCLUDE}, |
| 808 | {"devicetree", T_FDT}, |
| 809 | {"fdt", T_FDT}, |
| 810 | {"devicetreedir", T_FDTDIR}, |
| 811 | {"fdtdir", T_FDTDIR}, |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 812 | {"fdtoverlays", T_FDTOVERLAYS}, |
Edoardo Tomelleri | 35821a2 | 2022-09-21 15:26:33 +0200 | [diff] [blame] | 813 | {"devicetree-overlay", T_FDTOVERLAYS}, |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 814 | {"ontimeout", T_ONTIMEOUT,}, |
| 815 | {"ipappend", T_IPAPPEND,}, |
| 816 | {"background", T_BACKGROUND,}, |
Zhang Ning | 0290146 | 2022-02-01 08:33:37 +0800 | [diff] [blame] | 817 | {"kaslrseed", T_KASLRSEED,}, |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 818 | {NULL, T_INVALID} |
| 819 | }; |
| 820 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 821 | /** |
| 822 | * enum lex_state - lexer state |
| 823 | * |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 824 | * Since pxe(linux) files don't have a token to identify the start of a |
| 825 | * literal, we have to keep track of when we're in a state where a literal is |
| 826 | * expected vs when we're in a state a keyword is expected. |
| 827 | */ |
| 828 | enum lex_state { |
| 829 | L_NORMAL = 0, |
| 830 | L_KEYWORD, |
| 831 | L_SLITERAL |
| 832 | }; |
| 833 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 834 | /** |
| 835 | * get_string() - retrieves a string from *p and stores it as a token in *t. |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 836 | * |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 837 | * This is used for scanning both string literals and keywords. |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 838 | * |
| 839 | * Characters from *p are copied into t-val until a character equal to |
| 840 | * delim is found, or a NUL byte is reached. If delim has the special value of |
| 841 | * ' ', any whitespace character will be used as a delimiter. |
| 842 | * |
| 843 | * If lower is unequal to 0, uppercase characters will be converted to |
| 844 | * lowercase in the result. This is useful to make keywords case |
| 845 | * insensitive. |
| 846 | * |
| 847 | * The location of *p is updated to point to the first character after the end |
| 848 | * of the token - the ending delimiter. |
| 849 | * |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 850 | * Memory for t->val is allocated using malloc and must be free()'d to reclaim |
| 851 | * it. |
| 852 | * |
| 853 | * @p: Points to a pointer to the current position in the input being processed. |
| 854 | * Updated to point at the first character after the current token |
| 855 | * @t: Pointers to a token to fill in |
| 856 | * @delim: Delimiter character to look for, either newline or space |
| 857 | * @lower: true to convert the string to lower case when storing |
| 858 | * Returns the new value of t->val, on success, NULL if out of memory |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 859 | */ |
| 860 | static char *get_string(char **p, struct token *t, char delim, int lower) |
| 861 | { |
| 862 | char *b, *e; |
| 863 | size_t len, i; |
| 864 | |
| 865 | /* |
| 866 | * b and e both start at the beginning of the input stream. |
| 867 | * |
| 868 | * e is incremented until we find the ending delimiter, or a NUL byte |
| 869 | * is reached. Then, we take e - b to find the length of the token. |
| 870 | */ |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 871 | b = *p; |
| 872 | e = *p; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 873 | while (*e) { |
| 874 | if ((delim == ' ' && isspace(*e)) || delim == *e) |
| 875 | break; |
| 876 | e++; |
| 877 | } |
| 878 | |
| 879 | len = e - b; |
| 880 | |
| 881 | /* |
| 882 | * Allocate memory to hold the string, and copy it in, converting |
| 883 | * characters to lowercase if lower is != 0. |
| 884 | */ |
| 885 | t->val = malloc(len + 1); |
| 886 | if (!t->val) |
| 887 | return NULL; |
| 888 | |
| 889 | for (i = 0; i < len; i++, b++) { |
| 890 | if (lower) |
| 891 | t->val[i] = tolower(*b); |
| 892 | else |
| 893 | t->val[i] = *b; |
| 894 | } |
| 895 | |
| 896 | t->val[len] = '\0'; |
| 897 | |
Simon Glass | 929860b | 2021-10-14 12:48:02 -0600 | [diff] [blame] | 898 | /* Update *p so the caller knows where to continue scanning */ |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 899 | *p = e; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 900 | t->type = T_STRING; |
| 901 | |
| 902 | return t->val; |
| 903 | } |
| 904 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 905 | /** |
| 906 | * get_keyword() - Populate a keyword token with a type and value |
| 907 | * |
| 908 | * Updates the ->type field based on the keyword string in @val |
| 909 | * @t: Token to populate |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 910 | */ |
| 911 | static void get_keyword(struct token *t) |
| 912 | { |
| 913 | int i; |
| 914 | |
| 915 | for (i = 0; keywords[i].val; i++) { |
| 916 | if (!strcmp(t->val, keywords[i].val)) { |
| 917 | t->type = keywords[i].type; |
| 918 | break; |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 923 | /** |
| 924 | * get_token() - Get the next token |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 925 | * |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 926 | * We have to keep track of which state we're in to know if we're looking to get |
| 927 | * a string literal or a keyword. |
| 928 | * |
| 929 | * @p: Points to a pointer to the current position in the input being processed. |
| 930 | * Updated to point at the first character after the current token |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 931 | */ |
| 932 | static void get_token(char **p, struct token *t, enum lex_state state) |
| 933 | { |
| 934 | char *c = *p; |
| 935 | |
| 936 | t->type = T_INVALID; |
| 937 | |
| 938 | /* eat non EOL whitespace */ |
| 939 | while (isblank(*c)) |
| 940 | c++; |
| 941 | |
| 942 | /* |
| 943 | * eat comments. note that string literals can't begin with #, but |
| 944 | * can contain a # after their first character. |
| 945 | */ |
| 946 | if (*c == '#') { |
| 947 | while (*c && *c != '\n') |
| 948 | c++; |
| 949 | } |
| 950 | |
| 951 | if (*c == '\n') { |
| 952 | t->type = T_EOL; |
| 953 | c++; |
| 954 | } else if (*c == '\0') { |
| 955 | t->type = T_EOF; |
| 956 | c++; |
| 957 | } else if (state == L_SLITERAL) { |
| 958 | get_string(&c, t, '\n', 0); |
| 959 | } else if (state == L_KEYWORD) { |
| 960 | /* |
| 961 | * when we expect a keyword, we first get the next string |
| 962 | * token delimited by whitespace, and then check if it |
| 963 | * matches a keyword in our keyword list. if it does, it's |
| 964 | * converted to a keyword token of the appropriate type, and |
| 965 | * if not, it remains a string token. |
| 966 | */ |
| 967 | get_string(&c, t, ' ', 1); |
| 968 | get_keyword(t); |
| 969 | } |
| 970 | |
| 971 | *p = c; |
| 972 | } |
| 973 | |
Simon Glass | 18109cc | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 974 | /** |
| 975 | * eol_or_eof() - Find end of line |
| 976 | * |
| 977 | * Increment *c until we get to the end of the current line, or EOF |
| 978 | * |
| 979 | * @c: Points to a pointer to the current position in the input being processed. |
| 980 | * Updated to point at the first character after the current token |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 981 | */ |
| 982 | static void eol_or_eof(char **c) |
| 983 | { |
| 984 | while (**c && **c != '\n') |
| 985 | (*c)++; |
| 986 | } |
| 987 | |
| 988 | /* |
| 989 | * All of these parse_* functions share some common behavior. |
| 990 | * |
| 991 | * They finish with *c pointing after the token they parse, and return 1 on |
| 992 | * success, or < 0 on error. |
| 993 | */ |
| 994 | |
| 995 | /* |
| 996 | * Parse a string literal and store a pointer it at *dst. String literals |
| 997 | * terminate at the end of the line. |
| 998 | */ |
| 999 | static int parse_sliteral(char **c, char **dst) |
| 1000 | { |
| 1001 | struct token t; |
| 1002 | char *s = *c; |
| 1003 | |
| 1004 | get_token(c, &t, L_SLITERAL); |
| 1005 | |
| 1006 | if (t.type != T_STRING) { |
| 1007 | printf("Expected string literal: %.*s\n", (int)(*c - s), s); |
| 1008 | return -EINVAL; |
| 1009 | } |
| 1010 | |
| 1011 | *dst = t.val; |
| 1012 | |
| 1013 | return 1; |
| 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | * Parse a base 10 (unsigned) integer and store it at *dst. |
| 1018 | */ |
| 1019 | static int parse_integer(char **c, int *dst) |
| 1020 | { |
| 1021 | struct token t; |
| 1022 | char *s = *c; |
| 1023 | |
| 1024 | get_token(c, &t, L_SLITERAL); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1025 | if (t.type != T_STRING) { |
| 1026 | printf("Expected string: %.*s\n", (int)(*c - s), s); |
| 1027 | return -EINVAL; |
| 1028 | } |
| 1029 | |
| 1030 | *dst = simple_strtol(t.val, NULL, 10); |
| 1031 | |
| 1032 | free(t.val); |
| 1033 | |
| 1034 | return 1; |
| 1035 | } |
| 1036 | |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1037 | static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1038 | struct pxe_menu *cfg, int nest_level); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1039 | |
| 1040 | /* |
| 1041 | * Parse an include statement, and retrieve and parse the file it mentions. |
| 1042 | * |
| 1043 | * base should point to a location where it's safe to store the file, and |
| 1044 | * nest_level should indicate how many nested includes have occurred. For this |
| 1045 | * include, nest_level has already been incremented and doesn't need to be |
| 1046 | * incremented here. |
| 1047 | */ |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1048 | static int handle_include(struct pxe_context *ctx, char **c, unsigned long base, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1049 | struct pxe_menu *cfg, int nest_level) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1050 | { |
| 1051 | char *include_path; |
| 1052 | char *s = *c; |
| 1053 | int err; |
| 1054 | char *buf; |
| 1055 | int ret; |
| 1056 | |
| 1057 | err = parse_sliteral(c, &include_path); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1058 | if (err < 0) { |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1059 | printf("Expected include path: %.*s\n", (int)(*c - s), s); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1060 | return err; |
| 1061 | } |
| 1062 | |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1063 | err = get_pxe_file(ctx, include_path, base); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1064 | if (err < 0) { |
| 1065 | printf("Couldn't retrieve %s\n", include_path); |
| 1066 | return err; |
| 1067 | } |
| 1068 | |
| 1069 | buf = map_sysmem(base, 0); |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1070 | ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1071 | unmap_sysmem(buf); |
| 1072 | |
| 1073 | return ret; |
| 1074 | } |
| 1075 | |
| 1076 | /* |
| 1077 | * Parse lines that begin with 'menu'. |
| 1078 | * |
| 1079 | * base and nest are provided to handle the 'menu include' case. |
| 1080 | * |
| 1081 | * base should point to a location where it's safe to store the included file. |
| 1082 | * |
| 1083 | * nest_level should be 1 when parsing the top level pxe file, 2 when parsing |
| 1084 | * a file it includes, 3 when parsing a file included by that file, and so on. |
| 1085 | */ |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1086 | static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1087 | unsigned long base, int nest_level) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1088 | { |
| 1089 | struct token t; |
| 1090 | char *s = *c; |
| 1091 | int err = 0; |
| 1092 | |
| 1093 | get_token(c, &t, L_KEYWORD); |
| 1094 | |
| 1095 | switch (t.type) { |
| 1096 | case T_TITLE: |
| 1097 | err = parse_sliteral(c, &cfg->title); |
| 1098 | |
| 1099 | break; |
| 1100 | |
| 1101 | case T_INCLUDE: |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1102 | err = handle_include(ctx, c, base, cfg, nest_level + 1); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1103 | break; |
| 1104 | |
| 1105 | case T_BACKGROUND: |
| 1106 | err = parse_sliteral(c, &cfg->bmp); |
| 1107 | break; |
| 1108 | |
| 1109 | default: |
| 1110 | printf("Ignoring malformed menu command: %.*s\n", |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1111 | (int)(*c - s), s); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1112 | } |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1113 | if (err < 0) |
| 1114 | return err; |
| 1115 | |
| 1116 | eol_or_eof(c); |
| 1117 | |
| 1118 | return 1; |
| 1119 | } |
| 1120 | |
| 1121 | /* |
| 1122 | * Handles parsing a 'menu line' when we're parsing a label. |
| 1123 | */ |
| 1124 | static int parse_label_menu(char **c, struct pxe_menu *cfg, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1125 | struct pxe_label *label) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1126 | { |
| 1127 | struct token t; |
| 1128 | char *s; |
| 1129 | |
| 1130 | s = *c; |
| 1131 | |
| 1132 | get_token(c, &t, L_KEYWORD); |
| 1133 | |
| 1134 | switch (t.type) { |
| 1135 | case T_DEFAULT: |
| 1136 | if (!cfg->default_label) |
| 1137 | cfg->default_label = strdup(label->name); |
| 1138 | |
| 1139 | if (!cfg->default_label) |
| 1140 | return -ENOMEM; |
| 1141 | |
| 1142 | break; |
| 1143 | case T_LABEL: |
| 1144 | parse_sliteral(c, &label->menu); |
| 1145 | break; |
| 1146 | default: |
| 1147 | printf("Ignoring malformed menu command: %.*s\n", |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1148 | (int)(*c - s), s); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | eol_or_eof(c); |
| 1152 | |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * Handles parsing a 'kernel' label. |
| 1158 | * expecting "filename" or "<fit_filename>#cfg" |
| 1159 | */ |
| 1160 | static int parse_label_kernel(char **c, struct pxe_label *label) |
| 1161 | { |
| 1162 | char *s; |
| 1163 | int err; |
| 1164 | |
| 1165 | err = parse_sliteral(c, &label->kernel); |
| 1166 | if (err < 0) |
| 1167 | return err; |
| 1168 | |
| 1169 | s = strstr(label->kernel, "#"); |
| 1170 | if (!s) |
| 1171 | return 1; |
| 1172 | |
| 1173 | label->config = malloc(strlen(s) + 1); |
| 1174 | if (!label->config) |
| 1175 | return -ENOMEM; |
| 1176 | |
| 1177 | strcpy(label->config, s); |
| 1178 | *s = 0; |
| 1179 | |
| 1180 | return 1; |
| 1181 | } |
| 1182 | |
| 1183 | /* |
| 1184 | * Parses a label and adds it to the list of labels for a menu. |
| 1185 | * |
| 1186 | * A label ends when we either get to the end of a file, or |
| 1187 | * get some input we otherwise don't have a handler defined |
| 1188 | * for. |
| 1189 | * |
| 1190 | */ |
| 1191 | static int parse_label(char **c, struct pxe_menu *cfg) |
| 1192 | { |
| 1193 | struct token t; |
| 1194 | int len; |
| 1195 | char *s = *c; |
| 1196 | struct pxe_label *label; |
| 1197 | int err; |
| 1198 | |
| 1199 | label = label_create(); |
| 1200 | if (!label) |
| 1201 | return -ENOMEM; |
| 1202 | |
| 1203 | err = parse_sliteral(c, &label->name); |
| 1204 | if (err < 0) { |
| 1205 | printf("Expected label name: %.*s\n", (int)(*c - s), s); |
| 1206 | label_destroy(label); |
| 1207 | return -EINVAL; |
| 1208 | } |
| 1209 | |
| 1210 | list_add_tail(&label->list, &cfg->labels); |
| 1211 | |
| 1212 | while (1) { |
| 1213 | s = *c; |
| 1214 | get_token(c, &t, L_KEYWORD); |
| 1215 | |
| 1216 | err = 0; |
| 1217 | switch (t.type) { |
| 1218 | case T_MENU: |
| 1219 | err = parse_label_menu(c, cfg, label); |
| 1220 | break; |
| 1221 | |
| 1222 | case T_KERNEL: |
| 1223 | case T_LINUX: |
| 1224 | err = parse_label_kernel(c, label); |
| 1225 | break; |
| 1226 | |
| 1227 | case T_APPEND: |
| 1228 | err = parse_sliteral(c, &label->append); |
| 1229 | if (label->initrd) |
| 1230 | break; |
| 1231 | s = strstr(label->append, "initrd="); |
| 1232 | if (!s) |
| 1233 | break; |
| 1234 | s += 7; |
| 1235 | len = (int)(strchr(s, ' ') - s); |
| 1236 | label->initrd = malloc(len + 1); |
| 1237 | strncpy(label->initrd, s, len); |
| 1238 | label->initrd[len] = '\0'; |
| 1239 | |
| 1240 | break; |
| 1241 | |
| 1242 | case T_INITRD: |
| 1243 | if (!label->initrd) |
| 1244 | err = parse_sliteral(c, &label->initrd); |
| 1245 | break; |
| 1246 | |
| 1247 | case T_FDT: |
| 1248 | if (!label->fdt) |
| 1249 | err = parse_sliteral(c, &label->fdt); |
| 1250 | break; |
| 1251 | |
| 1252 | case T_FDTDIR: |
| 1253 | if (!label->fdtdir) |
| 1254 | err = parse_sliteral(c, &label->fdtdir); |
| 1255 | break; |
| 1256 | |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 1257 | case T_FDTOVERLAYS: |
| 1258 | if (!label->fdtoverlays) |
| 1259 | err = parse_sliteral(c, &label->fdtoverlays); |
| 1260 | break; |
| 1261 | |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1262 | case T_LOCALBOOT: |
| 1263 | label->localboot = 1; |
| 1264 | err = parse_integer(c, &label->localboot_val); |
| 1265 | break; |
| 1266 | |
| 1267 | case T_IPAPPEND: |
| 1268 | err = parse_integer(c, &label->ipappend); |
| 1269 | break; |
| 1270 | |
Zhang Ning | 0290146 | 2022-02-01 08:33:37 +0800 | [diff] [blame] | 1271 | case T_KASLRSEED: |
| 1272 | label->kaslrseed = 1; |
| 1273 | break; |
| 1274 | |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1275 | case T_EOL: |
| 1276 | break; |
| 1277 | |
| 1278 | default: |
| 1279 | /* |
| 1280 | * put the token back! we don't want it - it's the end |
| 1281 | * of a label and whatever token this is, it's |
| 1282 | * something for the menu level context to handle. |
| 1283 | */ |
| 1284 | *c = s; |
| 1285 | return 1; |
| 1286 | } |
| 1287 | |
| 1288 | if (err < 0) |
| 1289 | return err; |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * This 16 comes from the limit pxelinux imposes on nested includes. |
| 1295 | * |
| 1296 | * There is no reason at all we couldn't do more, but some limit helps prevent |
| 1297 | * infinite (until crash occurs) recursion if a file tries to include itself. |
| 1298 | */ |
| 1299 | #define MAX_NEST_LEVEL 16 |
| 1300 | |
| 1301 | /* |
| 1302 | * Entry point for parsing a menu file. nest_level indicates how many times |
| 1303 | * we've nested in includes. It will be 1 for the top level menu file. |
| 1304 | * |
| 1305 | * Returns 1 on success, < 0 on error. |
| 1306 | */ |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1307 | static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1308 | struct pxe_menu *cfg, int nest_level) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1309 | { |
| 1310 | struct token t; |
| 1311 | char *s, *b, *label_name; |
| 1312 | int err; |
| 1313 | |
| 1314 | b = p; |
| 1315 | |
| 1316 | if (nest_level > MAX_NEST_LEVEL) { |
| 1317 | printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL); |
| 1318 | return -EMLINK; |
| 1319 | } |
| 1320 | |
| 1321 | while (1) { |
| 1322 | s = p; |
| 1323 | |
| 1324 | get_token(&p, &t, L_KEYWORD); |
| 1325 | |
| 1326 | err = 0; |
| 1327 | switch (t.type) { |
| 1328 | case T_MENU: |
| 1329 | cfg->prompt = 1; |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1330 | err = parse_menu(ctx, &p, cfg, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1331 | base + ALIGN(strlen(b) + 1, 4), |
| 1332 | nest_level); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1333 | break; |
| 1334 | |
| 1335 | case T_TIMEOUT: |
| 1336 | err = parse_integer(&p, &cfg->timeout); |
| 1337 | break; |
| 1338 | |
| 1339 | case T_LABEL: |
| 1340 | err = parse_label(&p, cfg); |
| 1341 | break; |
| 1342 | |
| 1343 | case T_DEFAULT: |
| 1344 | case T_ONTIMEOUT: |
| 1345 | err = parse_sliteral(&p, &label_name); |
| 1346 | |
| 1347 | if (label_name) { |
| 1348 | if (cfg->default_label) |
| 1349 | free(cfg->default_label); |
| 1350 | |
| 1351 | cfg->default_label = label_name; |
| 1352 | } |
| 1353 | |
| 1354 | break; |
| 1355 | |
| 1356 | case T_INCLUDE: |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1357 | err = handle_include(ctx, &p, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1358 | base + ALIGN(strlen(b), 4), cfg, |
| 1359 | nest_level + 1); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1360 | break; |
| 1361 | |
| 1362 | case T_PROMPT: |
| 1363 | eol_or_eof(&p); |
| 1364 | break; |
| 1365 | |
| 1366 | case T_EOL: |
| 1367 | break; |
| 1368 | |
| 1369 | case T_EOF: |
| 1370 | return 1; |
| 1371 | |
| 1372 | default: |
| 1373 | printf("Ignoring unknown command: %.*s\n", |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1374 | (int)(p - s), s); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1375 | eol_or_eof(&p); |
| 1376 | } |
| 1377 | |
| 1378 | if (err < 0) |
| 1379 | return err; |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | /* |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1384 | */ |
| 1385 | void destroy_pxe_menu(struct pxe_menu *cfg) |
| 1386 | { |
| 1387 | struct list_head *pos, *n; |
| 1388 | struct pxe_label *label; |
| 1389 | |
Simon Glass | 929860b | 2021-10-14 12:48:02 -0600 | [diff] [blame] | 1390 | free(cfg->title); |
| 1391 | free(cfg->default_label); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1392 | |
| 1393 | list_for_each_safe(pos, n, &cfg->labels) { |
| 1394 | label = list_entry(pos, struct pxe_label, list); |
| 1395 | |
| 1396 | label_destroy(label); |
| 1397 | } |
| 1398 | |
| 1399 | free(cfg); |
| 1400 | } |
| 1401 | |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1402 | struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1403 | { |
| 1404 | struct pxe_menu *cfg; |
| 1405 | char *buf; |
| 1406 | int r; |
| 1407 | |
| 1408 | cfg = malloc(sizeof(struct pxe_menu)); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1409 | if (!cfg) |
| 1410 | return NULL; |
| 1411 | |
| 1412 | memset(cfg, 0, sizeof(struct pxe_menu)); |
| 1413 | |
| 1414 | INIT_LIST_HEAD(&cfg->labels); |
| 1415 | |
| 1416 | buf = map_sysmem(menucfg, 0); |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1417 | r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1418 | unmap_sysmem(buf); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1419 | if (r < 0) { |
| 1420 | destroy_pxe_menu(cfg); |
| 1421 | return NULL; |
| 1422 | } |
| 1423 | |
| 1424 | return cfg; |
| 1425 | } |
| 1426 | |
| 1427 | /* |
| 1428 | * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic |
| 1429 | * menu code. |
| 1430 | */ |
| 1431 | static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg) |
| 1432 | { |
| 1433 | struct pxe_label *label; |
| 1434 | struct list_head *pos; |
| 1435 | struct menu *m; |
Amjad Ouled-Ameur | c296979 | 2021-11-13 14:09:20 +0100 | [diff] [blame] | 1436 | char *label_override; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1437 | int err; |
| 1438 | int i = 1; |
| 1439 | char *default_num = NULL; |
Amjad Ouled-Ameur | c296979 | 2021-11-13 14:09:20 +0100 | [diff] [blame] | 1440 | char *override_num = NULL; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1441 | |
| 1442 | /* |
| 1443 | * Create a menu and add items for all the labels. |
| 1444 | */ |
| 1445 | m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10), |
Thirupathaiah Annapureddy | 5168d7a | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 1446 | cfg->prompt, NULL, label_print, NULL, NULL); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1447 | if (!m) |
| 1448 | return NULL; |
| 1449 | |
Amjad Ouled-Ameur | c296979 | 2021-11-13 14:09:20 +0100 | [diff] [blame] | 1450 | label_override = env_get("pxe_label_override"); |
| 1451 | |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1452 | list_for_each(pos, &cfg->labels) { |
| 1453 | label = list_entry(pos, struct pxe_label, list); |
| 1454 | |
| 1455 | sprintf(label->num, "%d", i++); |
| 1456 | if (menu_item_add(m, label->num, label) != 1) { |
| 1457 | menu_destroy(m); |
| 1458 | return NULL; |
| 1459 | } |
| 1460 | if (cfg->default_label && |
| 1461 | (strcmp(label->name, cfg->default_label) == 0)) |
| 1462 | default_num = label->num; |
Amjad Ouled-Ameur | c296979 | 2021-11-13 14:09:20 +0100 | [diff] [blame] | 1463 | if (label_override && !strcmp(label->name, label_override)) |
| 1464 | override_num = label->num; |
| 1465 | } |
| 1466 | |
| 1467 | |
| 1468 | if (label_override) { |
| 1469 | if (override_num) |
| 1470 | default_num = override_num; |
| 1471 | else |
| 1472 | printf("Missing override pxe label: %s\n", |
| 1473 | label_override); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | /* |
| 1477 | * After we've created items for each label in the menu, set the |
| 1478 | * menu's default label if one was specified. |
| 1479 | */ |
| 1480 | if (default_num) { |
| 1481 | err = menu_default_set(m, default_num); |
| 1482 | if (err != 1) { |
| 1483 | if (err != -ENOENT) { |
| 1484 | menu_destroy(m); |
| 1485 | return NULL; |
| 1486 | } |
| 1487 | |
| 1488 | printf("Missing default: %s\n", cfg->default_label); |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | return m; |
| 1493 | } |
| 1494 | |
| 1495 | /* |
| 1496 | * Try to boot any labels we have yet to attempt to boot. |
| 1497 | */ |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1498 | static void boot_unattempted_labels(struct pxe_context *ctx, |
| 1499 | struct pxe_menu *cfg) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1500 | { |
| 1501 | struct list_head *pos; |
| 1502 | struct pxe_label *label; |
| 1503 | |
| 1504 | list_for_each(pos, &cfg->labels) { |
| 1505 | label = list_entry(pos, struct pxe_label, list); |
| 1506 | |
| 1507 | if (!label->attempted) |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1508 | label_boot(ctx, label); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1509 | } |
| 1510 | } |
| 1511 | |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1512 | void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1513 | { |
| 1514 | void *choice; |
| 1515 | struct menu *m; |
| 1516 | int err; |
| 1517 | |
Kory Maincent | ff0287e | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 1518 | if (IS_ENABLED(CONFIG_CMD_BMP)) { |
| 1519 | /* display BMP if available */ |
| 1520 | if (cfg->bmp) { |
Simon Glass | 4d79e88 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 1521 | if (get_relfile(ctx, cfg->bmp, image_load_addr, NULL)) { |
Patrick Delaunay | e6fe02a | 2022-03-22 17:08:43 +0100 | [diff] [blame] | 1522 | #if defined(CONFIG_DM_VIDEO) |
| 1523 | struct udevice *dev; |
| 1524 | |
| 1525 | err = uclass_first_device_err(UCLASS_VIDEO, &dev); |
| 1526 | if (!err) |
| 1527 | video_clear(dev); |
| 1528 | #endif |
Kory Maincent | ff0287e | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 1529 | bmp_display(image_load_addr, |
| 1530 | BMP_ALIGN_CENTER, BMP_ALIGN_CENTER); |
| 1531 | } else { |
| 1532 | printf("Skipping background bmp %s for failure\n", |
| 1533 | cfg->bmp); |
| 1534 | } |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1535 | } |
| 1536 | } |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1537 | |
| 1538 | m = pxe_menu_to_menu(cfg); |
| 1539 | if (!m) |
| 1540 | return; |
| 1541 | |
| 1542 | err = menu_get_choice(m, &choice); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1543 | menu_destroy(m); |
| 1544 | |
| 1545 | /* |
| 1546 | * err == 1 means we got a choice back from menu_get_choice. |
| 1547 | * |
| 1548 | * err == -ENOENT if the menu was setup to select the default but no |
| 1549 | * default was set. in that case, we should continue trying to boot |
| 1550 | * labels that haven't been attempted yet. |
| 1551 | * |
| 1552 | * otherwise, the user interrupted or there was some other error and |
| 1553 | * we give up. |
| 1554 | */ |
| 1555 | |
| 1556 | if (err == 1) { |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1557 | err = label_boot(ctx, choice); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1558 | if (!err) |
| 1559 | return; |
| 1560 | } else if (err != -ENOENT) { |
| 1561 | return; |
| 1562 | } |
| 1563 | |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1564 | boot_unattempted_labels(ctx, cfg); |
| 1565 | } |
| 1566 | |
Simon Glass | 12df842 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 1567 | int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp, |
| 1568 | pxe_getfile_func getfile, void *userdata, |
| 1569 | bool allow_abs_path, const char *bootfile) |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1570 | { |
Simon Glass | 12df842 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 1571 | const char *last_slash; |
| 1572 | size_t path_len = 0; |
| 1573 | |
| 1574 | memset(ctx, '\0', sizeof(*ctx)); |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1575 | ctx->cmdtp = cmdtp; |
Simon Glass | b1ead6b | 2021-10-14 12:47:57 -0600 | [diff] [blame] | 1576 | ctx->getfile = getfile; |
Simon Glass | 4ad5d51 | 2021-10-14 12:47:58 -0600 | [diff] [blame] | 1577 | ctx->userdata = userdata; |
Simon Glass | 8018b9a | 2021-10-14 12:47:59 -0600 | [diff] [blame] | 1578 | ctx->allow_abs_path = allow_abs_path; |
Simon Glass | 12df842 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 1579 | |
| 1580 | /* figure out the boot directory, if there is one */ |
| 1581 | if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN) |
| 1582 | return -ENOSPC; |
| 1583 | ctx->bootdir = strdup(bootfile ? bootfile : ""); |
| 1584 | if (!ctx->bootdir) |
| 1585 | return -ENOMEM; |
| 1586 | |
| 1587 | if (bootfile) { |
| 1588 | last_slash = strrchr(bootfile, '/'); |
| 1589 | if (last_slash) |
| 1590 | path_len = (last_slash - bootfile) + 1; |
| 1591 | } |
| 1592 | ctx->bootdir[path_len] = '\0'; |
| 1593 | |
| 1594 | return 0; |
| 1595 | } |
| 1596 | |
| 1597 | void pxe_destroy_ctx(struct pxe_context *ctx) |
| 1598 | { |
| 1599 | free(ctx->bootdir); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1600 | } |
Simon Glass | 9e62e7c | 2021-10-14 12:48:03 -0600 | [diff] [blame] | 1601 | |
| 1602 | int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt) |
| 1603 | { |
| 1604 | struct pxe_menu *cfg; |
| 1605 | |
| 1606 | cfg = parse_pxefile(ctx, pxefile_addr_r); |
| 1607 | if (!cfg) { |
| 1608 | printf("Error parsing config file\n"); |
| 1609 | return 1; |
| 1610 | } |
| 1611 | |
| 1612 | if (prompt) |
| 1613 | cfg->prompt = 1; |
| 1614 | |
| 1615 | handle_pxe_menu(ctx, cfg); |
| 1616 | |
| 1617 | destroy_pxe_menu(cfg); |
| 1618 | |
| 1619 | return 0; |
| 1620 | } |