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