Patrice Chotard | 17e8804 | 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 | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 8 | #include <command.h> |
Patrick Delaunay | 6ec8cb9 | 2022-03-22 17:08:43 +0100 | [diff] [blame] | 9 | #include <dm.h> |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 10 | #include <env.h> |
Simon Glass | 85f1378 | 2019-12-28 10:45:03 -0700 | [diff] [blame] | 11 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | #include <mapmem.h> |
| 15 | #include <lcd.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 16 | #include <net.h> |
Neil Armstrong | c77a1a3 | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 17 | #include <fdt_support.h> |
Patrick Delaunay | 6ec8cb9 | 2022-03-22 17:08:43 +0100 | [diff] [blame] | 18 | #include <video.h> |
Neil Armstrong | c77a1a3 | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 19 | #include <linux/libfdt.h> |
Patrice Chotard | 17e8804 | 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 | 9c1d9c5 | 2022-02-01 08:33:37 +0800 | [diff] [blame] | 25 | #ifdef CONFIG_DM_RNG |
Zhang Ning | 9c1d9c5 | 2022-02-01 08:33:37 +0800 | [diff] [blame] | 26 | #include <rng.h> |
| 27 | #endif |
| 28 | |
Patrice Chotard | 17e8804 | 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 | 6273f13 | 2019-11-28 00:07:08 -0500 | [diff] [blame] | 37 | #define MAX_TFTP_PATH_LEN 512 |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 38 | |
Simon Glass | a9401b9 | 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 | 0030244 | 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 | 47b4c02 | 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 | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 66 | * current ethernet device |
| 67 | */ |
Patrice Chotard | 17e8804 | 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 | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 74 | return -ENOSPC; |
Patrice Chotard | 17e8804 | 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 | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 87 | /** |
Simon Glass | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 88 | * get_relfile() - read a file relative to the PXE file |
| 89 | * |
Patrice Chotard | 17e8804 | 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 | 0030244 | 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 | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 98 | * @filesizep: If not NULL, returns the file size in bytes |
Simon Glass | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 99 | * Returns 1 for success, or < 0 on error |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 100 | */ |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 101 | static int get_relfile(struct pxe_context *ctx, const char *file_path, |
Simon Glass | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 102 | unsigned long file_addr, ulong *filesizep) |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 103 | { |
| 104 | size_t path_len; |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 105 | char relfile[MAX_TFTP_PATH_LEN + 1]; |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 106 | char addr_buf[18]; |
Simon Glass | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 107 | ulong size; |
| 108 | int ret; |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 109 | |
Simon Glass | 5e3e39a | 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 | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 114 | |
Simon Glass | 5e3e39a | 2021-10-14 12:48:05 -0600 | [diff] [blame] | 115 | path_len = strlen(file_path) + strlen(relfile); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 116 | |
| 117 | if (path_len > MAX_TFTP_PATH_LEN) { |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 118 | printf("Base path too long (%s%s)\n", relfile, file_path); |
Patrice Chotard | 17e8804 | 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 | a9401b9 | 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 | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Simon Glass | 0030244 | 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 | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 148 | int get_pxe_file(struct pxe_context *ctx, const char *file_path, |
Simon Glass | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 149 | ulong file_addr) |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 150 | { |
Simon Glass | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 151 | ulong size; |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 152 | int err; |
| 153 | char *buf; |
| 154 | |
Simon Glass | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 155 | err = get_relfile(ctx, file_path, file_addr, &size); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 156 | if (err < 0) |
| 157 | return err; |
| 158 | |
Simon Glass | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 159 | buf = map_sysmem(file_addr + size, 1); |
Patrice Chotard | 17e8804 | 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 | 0030244 | 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 | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 176 | int get_pxelinux_path(struct pxe_context *ctx, const char *file, |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 177 | unsigned long pxefile_addr_r) |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 178 | { |
| 179 | size_t base_len = strlen(PXELINUX_DIR); |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 180 | char path[MAX_TFTP_PATH_LEN + 1]; |
Patrice Chotard | 17e8804 | 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 | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 184 | PXELINUX_DIR, file); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 185 | return -ENAMETOOLONG; |
| 186 | } |
| 187 | |
| 188 | sprintf(path, PXELINUX_DIR "%s", file); |
| 189 | |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 190 | return get_pxe_file(ctx, path, pxefile_addr_r); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Simon Glass | 0030244 | 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 | 17e8804 | 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 | 0030244 | 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 | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 204 | * @filesizep: Returns the file size in bytes |
Simon Glass | 0030244 | 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 | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 208 | */ |
Simon Glass | b0d08db | 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 | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 210 | const char *envaddr_name, ulong *filesizep) |
Patrice Chotard | 17e8804 | 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 | 17e8804 | 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 | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 222 | return get_relfile(ctx, file_path, file_addr, filesizep); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Simon Glass | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 225 | /** |
| 226 | * label_create() - crate a new PXE label |
| 227 | * |
Patrice Chotard | 17e8804 | 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 | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 231 | * Returns a pointer to the label, or NULL if out of memory |
Patrice Chotard | 17e8804 | 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 | 17e8804 | 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 | 0030244 | 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 | 17e8804 | 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 | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 256 | * |
| 257 | * @label: Label to free |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 258 | */ |
| 259 | static void label_destroy(struct pxe_label *label) |
| 260 | { |
Simon Glass | 764d0c0 | 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 | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 269 | free(label); |
| 270 | } |
| 271 | |
Simon Glass | 0030244 | 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 | 17e8804 | 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 | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 277 | * |
| 278 | * @data: Label to print (is cast to struct pxe_label *) |
Patrice Chotard | 17e8804 | 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 | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 288 | /** |
| 289 | * label_localboot() - Boot a label that specified 'localboot' |
| 290 | * |
| 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'. |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 295 | * |
Simon Glass | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 296 | * @label: Label to process |
| 297 | * Returns 1 on success or < 0 on error |
Patrice Chotard | 17e8804 | 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 | 17e8804 | 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 | c7b03e8 | 2020-11-05 10:33:47 -0700 | [diff] [blame] | 310 | cli_simple_process_macros(label->append, bootargs, |
| 311 | sizeof(bootargs)); |
Patrice Chotard | 17e8804 | 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 | } |
Neil Armstrong | c77a1a3 | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 319 | |
Zhang Ning | 9c1d9c5 | 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 | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 381 | /** |
| 382 | * label_boot_fdtoverlay() - Loads fdt overlays specified in 'fdtoverlays' |
| 383 | * |
| 384 | * @ctx: PXE context |
| 385 | * @label: Label to process |
Neil Armstrong | c77a1a3 | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 386 | */ |
| 387 | #ifdef CONFIG_OF_LIBFDT_OVERLAY |
Simon Glass | b0d08db | 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 | c77a1a3 | 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 | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 399 | fdt_addr = hextoul(env_get("fdt_addr_r"), NULL); |
Neil Armstrong | c77a1a3 | 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 | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 412 | fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL); |
Neil Armstrong | c77a1a3 | 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 | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 439 | err = get_relfile_envaddr(ctx, overlayfile, "fdtoverlay_addr_r", |
| 440 | NULL); |
Neil Armstrong | c77a1a3 | 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 |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 470 | |
Simon Glass | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 471 | /** |
| 472 | * label_boot() - Boot according to the contents of a pxe_label |
Patrice Chotard | 17e8804 | 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 | 0030244 | 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 | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 490 | */ |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 491 | static int label_boot(struct pxe_context *ctx, struct pxe_label *label) |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 492 | { |
| 493 | char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL }; |
Zhaofeng Li | dbf84cc | 2021-10-20 00:18:14 -0700 | [diff] [blame] | 494 | char *zboot_argv[] = { "zboot", NULL, "0", NULL, NULL }; |
Zhaofeng Li | bc14b93 | 2021-10-20 00:18:15 -0700 | [diff] [blame] | 495 | char *kernel_addr = NULL; |
| 496 | char *initrd_addr_str = NULL; |
Zhaofeng Li | dbf84cc | 2021-10-20 00:18:14 -0700 | [diff] [blame] | 497 | char initrd_filesize[10]; |
Zhaofeng Li | bc14b93 | 2021-10-20 00:18:15 -0700 | [diff] [blame] | 498 | char initrd_str[28]; |
Patrice Chotard | 17e8804 | 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 | dbf84cc | 2021-10-20 00:18:14 -0700 | [diff] [blame] | 503 | int zboot_argc = 3; |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 504 | int len = 0; |
Zhaofeng Li | bc14b93 | 2021-10-20 00:18:15 -0700 | [diff] [blame] | 505 | ulong kernel_addr_r; |
Patrice Chotard | 17e8804 | 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 | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 518 | if (!label->kernel) { |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 519 | printf("No kernel given, skipping %s\n", |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 520 | label->name); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 521 | return 1; |
| 522 | } |
| 523 | |
| 524 | if (label->initrd) { |
Simon Glass | a9401b9 | 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 | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 529 | printf("Skipping %s for failure retrieving initrd\n", |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 530 | label->name); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 531 | return 1; |
| 532 | } |
| 533 | |
Zhaofeng Li | bc14b93 | 2021-10-20 00:18:15 -0700 | [diff] [blame] | 534 | initrd_addr_str = env_get("ramdisk_addr_r"); |
Heinrich Schuchardt | a8d6aed | 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 | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 539 | } |
| 540 | |
Simon Glass | a9401b9 | 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 | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 543 | printf("Skipping %s for failure retrieving kernel\n", |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 544 | label->name); |
Patrice Chotard | 17e8804 | 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 | caabd24 | 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 | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 557 | |
Kory Maincent | caabd24 | 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 | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 563 | } |
Patrice Chotard | 17e8804 | 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 | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 576 | } |
Patrice Chotard | 6233de4 | 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 | c7b03e8 | 2020-11-05 10:33:47 -0700 | [diff] [blame] | 584 | cli_simple_process_macros(bootargs, finalbootargs, |
| 585 | sizeof(finalbootargs)); |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 586 | env_set("bootargs", finalbootargs); |
| 587 | printf("append: %s\n", finalbootargs); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 588 | } |
| 589 | |
Zhaofeng Li | bc14b93 | 2021-10-20 00:18:15 -0700 | [diff] [blame] | 590 | kernel_addr = env_get("kernel_addr_r"); |
Zhaofeng Li | dbf84cc | 2021-10-20 00:18:14 -0700 | [diff] [blame] | 591 | |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 592 | /* for FIT, append the configuration identifier */ |
| 593 | if (label->config) { |
Zhaofeng Li | bc14b93 | 2021-10-20 00:18:15 -0700 | [diff] [blame] | 594 | int len = strlen(kernel_addr) + strlen(label->config) + 1; |
Patrice Chotard | 17e8804 | 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 | bc14b93 | 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 | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* |
| 606 | * fdt usage is optional: |
Anton Leontiev | feb7db8 | 2019-09-03 10:52:24 +0300 | [diff] [blame] | 607 | * It handles the following scenarios. |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 608 | * |
Anton Leontiev | feb7db8 | 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 | 17e8804 | 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 | db2f387 | 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 | 17e8804 | 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 | 5ee984a | 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 | 17e8804 | 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 | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 686 | int err = get_relfile_envaddr(ctx, fdtfile, |
Simon Glass | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 687 | "fdt_addr_r", NULL); |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 688 | |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 689 | free(fdtfilefree); |
| 690 | if (err < 0) { |
Anton Leontiev | feb7db8 | 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 | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 698 | } |
Neil Armstrong | c77a1a3 | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 699 | |
Zhang Ning | 9c1d9c5 | 2022-02-01 08:33:37 +0800 | [diff] [blame] | 700 | if (label->kaslrseed) |
| 701 | label_boot_kaslrseed(); |
| 702 | |
Neil Armstrong | c77a1a3 | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 703 | #ifdef CONFIG_OF_LIBFDT_OVERLAY |
| 704 | if (label->fdtoverlays) |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 705 | label_boot_fdtoverlay(ctx, label); |
Neil Armstrong | c77a1a3 | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 706 | #endif |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 707 | } else { |
| 708 | bootm_argv[3] = NULL; |
| 709 | } |
| 710 | } |
| 711 | |
Zhaofeng Li | bc14b93 | 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 | 17e8804 | 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 | db2f387 | 2021-10-14 09:40:04 +0100 | [diff] [blame] | 727 | if (!bootm_argv[3]) |
| 728 | bootm_argv[3] = env_get("fdtcontroladdr"); |
| 729 | |
Patrice Chotard | 17e8804 | 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 | bc14b93 | 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 | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 738 | /* Try bootm for legacy and FIT format image */ |
| 739 | if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID) |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 740 | do_bootm(ctx->cmdtp, 0, bootm_argc, bootm_argv); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 741 | /* Try booting an AArch64 Linux kernel image */ |
Kory Maincent | caabd24 | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 742 | else if (IS_ENABLED(CONFIG_CMD_BOOTI)) |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 743 | do_booti(ctx->cmdtp, 0, bootm_argc, bootm_argv); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 744 | /* Try booting a Image */ |
Kory Maincent | caabd24 | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 745 | else if (IS_ENABLED(CONFIG_CMD_BOOTZ)) |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 746 | do_bootz(ctx->cmdtp, 0, bootm_argc, bootm_argv); |
Kory Maincent | b2187d0 | 2021-02-02 16:42:29 +0100 | [diff] [blame] | 747 | /* Try booting an x86_64 Linux kernel image */ |
| 748 | else if (IS_ENABLED(CONFIG_CMD_ZBOOT)) |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 749 | do_zboot_parent(ctx->cmdtp, 0, zboot_argc, zboot_argv, NULL); |
Kory Maincent | caabd24 | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 750 | |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 751 | unmap_sysmem(buf); |
| 752 | |
| 753 | cleanup: |
Simon Glass | 764d0c0 | 2021-10-14 12:48:02 -0600 | [diff] [blame] | 754 | free(fit_addr); |
| 755 | |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 756 | return 1; |
| 757 | } |
| 758 | |
Simon Glass | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 759 | /** enum token_type - Tokens for the pxe file parser */ |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 760 | enum token_type { |
| 761 | T_EOL, |
| 762 | T_STRING, |
| 763 | T_EOF, |
| 764 | T_MENU, |
| 765 | T_TITLE, |
| 766 | T_TIMEOUT, |
| 767 | T_LABEL, |
| 768 | T_KERNEL, |
| 769 | T_LINUX, |
| 770 | T_APPEND, |
| 771 | T_INITRD, |
| 772 | T_LOCALBOOT, |
| 773 | T_DEFAULT, |
| 774 | T_PROMPT, |
| 775 | T_INCLUDE, |
| 776 | T_FDT, |
| 777 | T_FDTDIR, |
Neil Armstrong | c77a1a3 | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 778 | T_FDTOVERLAYS, |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 779 | T_ONTIMEOUT, |
| 780 | T_IPAPPEND, |
| 781 | T_BACKGROUND, |
Zhang Ning | 9c1d9c5 | 2022-02-01 08:33:37 +0800 | [diff] [blame] | 782 | T_KASLRSEED, |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 783 | T_INVALID |
| 784 | }; |
| 785 | |
Simon Glass | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 786 | /** struct token - token - given by a value and a type */ |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 787 | struct token { |
| 788 | char *val; |
| 789 | enum token_type type; |
| 790 | }; |
| 791 | |
Simon Glass | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 792 | /* Keywords recognized */ |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 793 | static const struct token keywords[] = { |
| 794 | {"menu", T_MENU}, |
| 795 | {"title", T_TITLE}, |
| 796 | {"timeout", T_TIMEOUT}, |
| 797 | {"default", T_DEFAULT}, |
| 798 | {"prompt", T_PROMPT}, |
| 799 | {"label", T_LABEL}, |
| 800 | {"kernel", T_KERNEL}, |
| 801 | {"linux", T_LINUX}, |
| 802 | {"localboot", T_LOCALBOOT}, |
| 803 | {"append", T_APPEND}, |
| 804 | {"initrd", T_INITRD}, |
| 805 | {"include", T_INCLUDE}, |
| 806 | {"devicetree", T_FDT}, |
| 807 | {"fdt", T_FDT}, |
| 808 | {"devicetreedir", T_FDTDIR}, |
| 809 | {"fdtdir", T_FDTDIR}, |
Neil Armstrong | c77a1a3 | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 810 | {"fdtoverlays", T_FDTOVERLAYS}, |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 811 | {"ontimeout", T_ONTIMEOUT,}, |
| 812 | {"ipappend", T_IPAPPEND,}, |
| 813 | {"background", T_BACKGROUND,}, |
Zhang Ning | 9c1d9c5 | 2022-02-01 08:33:37 +0800 | [diff] [blame] | 814 | {"kaslrseed", T_KASLRSEED,}, |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 815 | {NULL, T_INVALID} |
| 816 | }; |
| 817 | |
Simon Glass | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 818 | /** |
| 819 | * enum lex_state - lexer state |
| 820 | * |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 821 | * Since pxe(linux) files don't have a token to identify the start of a |
| 822 | * literal, we have to keep track of when we're in a state where a literal is |
| 823 | * expected vs when we're in a state a keyword is expected. |
| 824 | */ |
| 825 | enum lex_state { |
| 826 | L_NORMAL = 0, |
| 827 | L_KEYWORD, |
| 828 | L_SLITERAL |
| 829 | }; |
| 830 | |
Simon Glass | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 831 | /** |
| 832 | * get_string() - retrieves a string from *p and stores it as a token in *t. |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 833 | * |
Simon Glass | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 834 | * This is used for scanning both string literals and keywords. |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 835 | * |
| 836 | * Characters from *p are copied into t-val until a character equal to |
| 837 | * delim is found, or a NUL byte is reached. If delim has the special value of |
| 838 | * ' ', any whitespace character will be used as a delimiter. |
| 839 | * |
| 840 | * If lower is unequal to 0, uppercase characters will be converted to |
| 841 | * lowercase in the result. This is useful to make keywords case |
| 842 | * insensitive. |
| 843 | * |
| 844 | * The location of *p is updated to point to the first character after the end |
| 845 | * of the token - the ending delimiter. |
| 846 | * |
Simon Glass | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 847 | * Memory for t->val is allocated using malloc and must be free()'d to reclaim |
| 848 | * it. |
| 849 | * |
| 850 | * @p: Points to a pointer to the current position in the input being processed. |
| 851 | * Updated to point at the first character after the current token |
| 852 | * @t: Pointers to a token to fill in |
| 853 | * @delim: Delimiter character to look for, either newline or space |
| 854 | * @lower: true to convert the string to lower case when storing |
| 855 | * Returns the new value of t->val, on success, NULL if out of memory |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 856 | */ |
| 857 | static char *get_string(char **p, struct token *t, char delim, int lower) |
| 858 | { |
| 859 | char *b, *e; |
| 860 | size_t len, i; |
| 861 | |
| 862 | /* |
| 863 | * b and e both start at the beginning of the input stream. |
| 864 | * |
| 865 | * e is incremented until we find the ending delimiter, or a NUL byte |
| 866 | * is reached. Then, we take e - b to find the length of the token. |
| 867 | */ |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 868 | b = *p; |
| 869 | e = *p; |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 870 | while (*e) { |
| 871 | if ((delim == ' ' && isspace(*e)) || delim == *e) |
| 872 | break; |
| 873 | e++; |
| 874 | } |
| 875 | |
| 876 | len = e - b; |
| 877 | |
| 878 | /* |
| 879 | * Allocate memory to hold the string, and copy it in, converting |
| 880 | * characters to lowercase if lower is != 0. |
| 881 | */ |
| 882 | t->val = malloc(len + 1); |
| 883 | if (!t->val) |
| 884 | return NULL; |
| 885 | |
| 886 | for (i = 0; i < len; i++, b++) { |
| 887 | if (lower) |
| 888 | t->val[i] = tolower(*b); |
| 889 | else |
| 890 | t->val[i] = *b; |
| 891 | } |
| 892 | |
| 893 | t->val[len] = '\0'; |
| 894 | |
Simon Glass | 764d0c0 | 2021-10-14 12:48:02 -0600 | [diff] [blame] | 895 | /* Update *p so the caller knows where to continue scanning */ |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 896 | *p = e; |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 897 | t->type = T_STRING; |
| 898 | |
| 899 | return t->val; |
| 900 | } |
| 901 | |
Simon Glass | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 902 | /** |
| 903 | * get_keyword() - Populate a keyword token with a type and value |
| 904 | * |
| 905 | * Updates the ->type field based on the keyword string in @val |
| 906 | * @t: Token to populate |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 907 | */ |
| 908 | static void get_keyword(struct token *t) |
| 909 | { |
| 910 | int i; |
| 911 | |
| 912 | for (i = 0; keywords[i].val; i++) { |
| 913 | if (!strcmp(t->val, keywords[i].val)) { |
| 914 | t->type = keywords[i].type; |
| 915 | break; |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | |
Simon Glass | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 920 | /** |
| 921 | * get_token() - Get the next token |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 922 | * |
Simon Glass | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 923 | * We have to keep track of which state we're in to know if we're looking to get |
| 924 | * a string literal or a keyword. |
| 925 | * |
| 926 | * @p: Points to a pointer to the current position in the input being processed. |
| 927 | * Updated to point at the first character after the current token |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 928 | */ |
| 929 | static void get_token(char **p, struct token *t, enum lex_state state) |
| 930 | { |
| 931 | char *c = *p; |
| 932 | |
| 933 | t->type = T_INVALID; |
| 934 | |
| 935 | /* eat non EOL whitespace */ |
| 936 | while (isblank(*c)) |
| 937 | c++; |
| 938 | |
| 939 | /* |
| 940 | * eat comments. note that string literals can't begin with #, but |
| 941 | * can contain a # after their first character. |
| 942 | */ |
| 943 | if (*c == '#') { |
| 944 | while (*c && *c != '\n') |
| 945 | c++; |
| 946 | } |
| 947 | |
| 948 | if (*c == '\n') { |
| 949 | t->type = T_EOL; |
| 950 | c++; |
| 951 | } else if (*c == '\0') { |
| 952 | t->type = T_EOF; |
| 953 | c++; |
| 954 | } else if (state == L_SLITERAL) { |
| 955 | get_string(&c, t, '\n', 0); |
| 956 | } else if (state == L_KEYWORD) { |
| 957 | /* |
| 958 | * when we expect a keyword, we first get the next string |
| 959 | * token delimited by whitespace, and then check if it |
| 960 | * matches a keyword in our keyword list. if it does, it's |
| 961 | * converted to a keyword token of the appropriate type, and |
| 962 | * if not, it remains a string token. |
| 963 | */ |
| 964 | get_string(&c, t, ' ', 1); |
| 965 | get_keyword(t); |
| 966 | } |
| 967 | |
| 968 | *p = c; |
| 969 | } |
| 970 | |
Simon Glass | 0030244 | 2021-10-14 12:48:01 -0600 | [diff] [blame] | 971 | /** |
| 972 | * eol_or_eof() - Find end of line |
| 973 | * |
| 974 | * Increment *c until we get to the end of the current line, or EOF |
| 975 | * |
| 976 | * @c: Points to a pointer to the current position in the input being processed. |
| 977 | * Updated to point at the first character after the current token |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 978 | */ |
| 979 | static void eol_or_eof(char **c) |
| 980 | { |
| 981 | while (**c && **c != '\n') |
| 982 | (*c)++; |
| 983 | } |
| 984 | |
| 985 | /* |
| 986 | * All of these parse_* functions share some common behavior. |
| 987 | * |
| 988 | * They finish with *c pointing after the token they parse, and return 1 on |
| 989 | * success, or < 0 on error. |
| 990 | */ |
| 991 | |
| 992 | /* |
| 993 | * Parse a string literal and store a pointer it at *dst. String literals |
| 994 | * terminate at the end of the line. |
| 995 | */ |
| 996 | static int parse_sliteral(char **c, char **dst) |
| 997 | { |
| 998 | struct token t; |
| 999 | char *s = *c; |
| 1000 | |
| 1001 | get_token(c, &t, L_SLITERAL); |
| 1002 | |
| 1003 | if (t.type != T_STRING) { |
| 1004 | printf("Expected string literal: %.*s\n", (int)(*c - s), s); |
| 1005 | return -EINVAL; |
| 1006 | } |
| 1007 | |
| 1008 | *dst = t.val; |
| 1009 | |
| 1010 | return 1; |
| 1011 | } |
| 1012 | |
| 1013 | /* |
| 1014 | * Parse a base 10 (unsigned) integer and store it at *dst. |
| 1015 | */ |
| 1016 | static int parse_integer(char **c, int *dst) |
| 1017 | { |
| 1018 | struct token t; |
| 1019 | char *s = *c; |
| 1020 | |
| 1021 | get_token(c, &t, L_SLITERAL); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1022 | if (t.type != T_STRING) { |
| 1023 | printf("Expected string: %.*s\n", (int)(*c - s), s); |
| 1024 | return -EINVAL; |
| 1025 | } |
| 1026 | |
| 1027 | *dst = simple_strtol(t.val, NULL, 10); |
| 1028 | |
| 1029 | free(t.val); |
| 1030 | |
| 1031 | return 1; |
| 1032 | } |
| 1033 | |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1034 | static int parse_pxefile_top(struct pxe_context *ctx, char *p, ulong base, |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1035 | struct pxe_menu *cfg, int nest_level); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1036 | |
| 1037 | /* |
| 1038 | * Parse an include statement, and retrieve and parse the file it mentions. |
| 1039 | * |
| 1040 | * base should point to a location where it's safe to store the file, and |
| 1041 | * nest_level should indicate how many nested includes have occurred. For this |
| 1042 | * include, nest_level has already been incremented and doesn't need to be |
| 1043 | * incremented here. |
| 1044 | */ |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1045 | static int handle_include(struct pxe_context *ctx, char **c, unsigned long base, |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1046 | struct pxe_menu *cfg, int nest_level) |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1047 | { |
| 1048 | char *include_path; |
| 1049 | char *s = *c; |
| 1050 | int err; |
| 1051 | char *buf; |
| 1052 | int ret; |
| 1053 | |
| 1054 | err = parse_sliteral(c, &include_path); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1055 | if (err < 0) { |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1056 | printf("Expected include path: %.*s\n", (int)(*c - s), s); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1057 | return err; |
| 1058 | } |
| 1059 | |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1060 | err = get_pxe_file(ctx, include_path, base); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1061 | if (err < 0) { |
| 1062 | printf("Couldn't retrieve %s\n", include_path); |
| 1063 | return err; |
| 1064 | } |
| 1065 | |
| 1066 | buf = map_sysmem(base, 0); |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1067 | ret = parse_pxefile_top(ctx, buf, base, cfg, nest_level); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1068 | unmap_sysmem(buf); |
| 1069 | |
| 1070 | return ret; |
| 1071 | } |
| 1072 | |
| 1073 | /* |
| 1074 | * Parse lines that begin with 'menu'. |
| 1075 | * |
| 1076 | * base and nest are provided to handle the 'menu include' case. |
| 1077 | * |
| 1078 | * base should point to a location where it's safe to store the included file. |
| 1079 | * |
| 1080 | * nest_level should be 1 when parsing the top level pxe file, 2 when parsing |
| 1081 | * a file it includes, 3 when parsing a file included by that file, and so on. |
| 1082 | */ |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1083 | static int parse_menu(struct pxe_context *ctx, char **c, struct pxe_menu *cfg, |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1084 | unsigned long base, int nest_level) |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1085 | { |
| 1086 | struct token t; |
| 1087 | char *s = *c; |
| 1088 | int err = 0; |
| 1089 | |
| 1090 | get_token(c, &t, L_KEYWORD); |
| 1091 | |
| 1092 | switch (t.type) { |
| 1093 | case T_TITLE: |
| 1094 | err = parse_sliteral(c, &cfg->title); |
| 1095 | |
| 1096 | break; |
| 1097 | |
| 1098 | case T_INCLUDE: |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1099 | err = handle_include(ctx, c, base, cfg, nest_level + 1); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1100 | break; |
| 1101 | |
| 1102 | case T_BACKGROUND: |
| 1103 | err = parse_sliteral(c, &cfg->bmp); |
| 1104 | break; |
| 1105 | |
| 1106 | default: |
| 1107 | printf("Ignoring malformed menu command: %.*s\n", |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1108 | (int)(*c - s), s); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1109 | } |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1110 | if (err < 0) |
| 1111 | return err; |
| 1112 | |
| 1113 | eol_or_eof(c); |
| 1114 | |
| 1115 | return 1; |
| 1116 | } |
| 1117 | |
| 1118 | /* |
| 1119 | * Handles parsing a 'menu line' when we're parsing a label. |
| 1120 | */ |
| 1121 | static int parse_label_menu(char **c, struct pxe_menu *cfg, |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1122 | struct pxe_label *label) |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1123 | { |
| 1124 | struct token t; |
| 1125 | char *s; |
| 1126 | |
| 1127 | s = *c; |
| 1128 | |
| 1129 | get_token(c, &t, L_KEYWORD); |
| 1130 | |
| 1131 | switch (t.type) { |
| 1132 | case T_DEFAULT: |
| 1133 | if (!cfg->default_label) |
| 1134 | cfg->default_label = strdup(label->name); |
| 1135 | |
| 1136 | if (!cfg->default_label) |
| 1137 | return -ENOMEM; |
| 1138 | |
| 1139 | break; |
| 1140 | case T_LABEL: |
| 1141 | parse_sliteral(c, &label->menu); |
| 1142 | break; |
| 1143 | default: |
| 1144 | printf("Ignoring malformed menu command: %.*s\n", |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1145 | (int)(*c - s), s); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | eol_or_eof(c); |
| 1149 | |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | /* |
| 1154 | * Handles parsing a 'kernel' label. |
| 1155 | * expecting "filename" or "<fit_filename>#cfg" |
| 1156 | */ |
| 1157 | static int parse_label_kernel(char **c, struct pxe_label *label) |
| 1158 | { |
| 1159 | char *s; |
| 1160 | int err; |
| 1161 | |
| 1162 | err = parse_sliteral(c, &label->kernel); |
| 1163 | if (err < 0) |
| 1164 | return err; |
| 1165 | |
| 1166 | s = strstr(label->kernel, "#"); |
| 1167 | if (!s) |
| 1168 | return 1; |
| 1169 | |
| 1170 | label->config = malloc(strlen(s) + 1); |
| 1171 | if (!label->config) |
| 1172 | return -ENOMEM; |
| 1173 | |
| 1174 | strcpy(label->config, s); |
| 1175 | *s = 0; |
| 1176 | |
| 1177 | return 1; |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | * Parses a label and adds it to the list of labels for a menu. |
| 1182 | * |
| 1183 | * A label ends when we either get to the end of a file, or |
| 1184 | * get some input we otherwise don't have a handler defined |
| 1185 | * for. |
| 1186 | * |
| 1187 | */ |
| 1188 | static int parse_label(char **c, struct pxe_menu *cfg) |
| 1189 | { |
| 1190 | struct token t; |
| 1191 | int len; |
| 1192 | char *s = *c; |
| 1193 | struct pxe_label *label; |
| 1194 | int err; |
| 1195 | |
| 1196 | label = label_create(); |
| 1197 | if (!label) |
| 1198 | return -ENOMEM; |
| 1199 | |
| 1200 | err = parse_sliteral(c, &label->name); |
| 1201 | if (err < 0) { |
| 1202 | printf("Expected label name: %.*s\n", (int)(*c - s), s); |
| 1203 | label_destroy(label); |
| 1204 | return -EINVAL; |
| 1205 | } |
| 1206 | |
| 1207 | list_add_tail(&label->list, &cfg->labels); |
| 1208 | |
| 1209 | while (1) { |
| 1210 | s = *c; |
| 1211 | get_token(c, &t, L_KEYWORD); |
| 1212 | |
| 1213 | err = 0; |
| 1214 | switch (t.type) { |
| 1215 | case T_MENU: |
| 1216 | err = parse_label_menu(c, cfg, label); |
| 1217 | break; |
| 1218 | |
| 1219 | case T_KERNEL: |
| 1220 | case T_LINUX: |
| 1221 | err = parse_label_kernel(c, label); |
| 1222 | break; |
| 1223 | |
| 1224 | case T_APPEND: |
| 1225 | err = parse_sliteral(c, &label->append); |
| 1226 | if (label->initrd) |
| 1227 | break; |
| 1228 | s = strstr(label->append, "initrd="); |
| 1229 | if (!s) |
| 1230 | break; |
| 1231 | s += 7; |
| 1232 | len = (int)(strchr(s, ' ') - s); |
| 1233 | label->initrd = malloc(len + 1); |
| 1234 | strncpy(label->initrd, s, len); |
| 1235 | label->initrd[len] = '\0'; |
| 1236 | |
| 1237 | break; |
| 1238 | |
| 1239 | case T_INITRD: |
| 1240 | if (!label->initrd) |
| 1241 | err = parse_sliteral(c, &label->initrd); |
| 1242 | break; |
| 1243 | |
| 1244 | case T_FDT: |
| 1245 | if (!label->fdt) |
| 1246 | err = parse_sliteral(c, &label->fdt); |
| 1247 | break; |
| 1248 | |
| 1249 | case T_FDTDIR: |
| 1250 | if (!label->fdtdir) |
| 1251 | err = parse_sliteral(c, &label->fdtdir); |
| 1252 | break; |
| 1253 | |
Neil Armstrong | c77a1a3 | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 1254 | case T_FDTOVERLAYS: |
| 1255 | if (!label->fdtoverlays) |
| 1256 | err = parse_sliteral(c, &label->fdtoverlays); |
| 1257 | break; |
| 1258 | |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1259 | case T_LOCALBOOT: |
| 1260 | label->localboot = 1; |
| 1261 | err = parse_integer(c, &label->localboot_val); |
| 1262 | break; |
| 1263 | |
| 1264 | case T_IPAPPEND: |
| 1265 | err = parse_integer(c, &label->ipappend); |
| 1266 | break; |
| 1267 | |
Zhang Ning | 9c1d9c5 | 2022-02-01 08:33:37 +0800 | [diff] [blame] | 1268 | case T_KASLRSEED: |
| 1269 | label->kaslrseed = 1; |
| 1270 | break; |
| 1271 | |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1272 | case T_EOL: |
| 1273 | break; |
| 1274 | |
| 1275 | default: |
| 1276 | /* |
| 1277 | * put the token back! we don't want it - it's the end |
| 1278 | * of a label and whatever token this is, it's |
| 1279 | * something for the menu level context to handle. |
| 1280 | */ |
| 1281 | *c = s; |
| 1282 | return 1; |
| 1283 | } |
| 1284 | |
| 1285 | if (err < 0) |
| 1286 | return err; |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | /* |
| 1291 | * This 16 comes from the limit pxelinux imposes on nested includes. |
| 1292 | * |
| 1293 | * There is no reason at all we couldn't do more, but some limit helps prevent |
| 1294 | * infinite (until crash occurs) recursion if a file tries to include itself. |
| 1295 | */ |
| 1296 | #define MAX_NEST_LEVEL 16 |
| 1297 | |
| 1298 | /* |
| 1299 | * Entry point for parsing a menu file. nest_level indicates how many times |
| 1300 | * we've nested in includes. It will be 1 for the top level menu file. |
| 1301 | * |
| 1302 | * Returns 1 on success, < 0 on error. |
| 1303 | */ |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1304 | static int parse_pxefile_top(struct pxe_context *ctx, char *p, unsigned long base, |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1305 | struct pxe_menu *cfg, int nest_level) |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1306 | { |
| 1307 | struct token t; |
| 1308 | char *s, *b, *label_name; |
| 1309 | int err; |
| 1310 | |
| 1311 | b = p; |
| 1312 | |
| 1313 | if (nest_level > MAX_NEST_LEVEL) { |
| 1314 | printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL); |
| 1315 | return -EMLINK; |
| 1316 | } |
| 1317 | |
| 1318 | while (1) { |
| 1319 | s = p; |
| 1320 | |
| 1321 | get_token(&p, &t, L_KEYWORD); |
| 1322 | |
| 1323 | err = 0; |
| 1324 | switch (t.type) { |
| 1325 | case T_MENU: |
| 1326 | cfg->prompt = 1; |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1327 | err = parse_menu(ctx, &p, cfg, |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1328 | base + ALIGN(strlen(b) + 1, 4), |
| 1329 | nest_level); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1330 | break; |
| 1331 | |
| 1332 | case T_TIMEOUT: |
| 1333 | err = parse_integer(&p, &cfg->timeout); |
| 1334 | break; |
| 1335 | |
| 1336 | case T_LABEL: |
| 1337 | err = parse_label(&p, cfg); |
| 1338 | break; |
| 1339 | |
| 1340 | case T_DEFAULT: |
| 1341 | case T_ONTIMEOUT: |
| 1342 | err = parse_sliteral(&p, &label_name); |
| 1343 | |
| 1344 | if (label_name) { |
| 1345 | if (cfg->default_label) |
| 1346 | free(cfg->default_label); |
| 1347 | |
| 1348 | cfg->default_label = label_name; |
| 1349 | } |
| 1350 | |
| 1351 | break; |
| 1352 | |
| 1353 | case T_INCLUDE: |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1354 | err = handle_include(ctx, &p, |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1355 | base + ALIGN(strlen(b), 4), cfg, |
| 1356 | nest_level + 1); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1357 | break; |
| 1358 | |
| 1359 | case T_PROMPT: |
| 1360 | eol_or_eof(&p); |
| 1361 | break; |
| 1362 | |
| 1363 | case T_EOL: |
| 1364 | break; |
| 1365 | |
| 1366 | case T_EOF: |
| 1367 | return 1; |
| 1368 | |
| 1369 | default: |
| 1370 | printf("Ignoring unknown command: %.*s\n", |
Patrice Chotard | 6233de4 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1371 | (int)(p - s), s); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1372 | eol_or_eof(&p); |
| 1373 | } |
| 1374 | |
| 1375 | if (err < 0) |
| 1376 | return err; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | /* |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1381 | */ |
| 1382 | void destroy_pxe_menu(struct pxe_menu *cfg) |
| 1383 | { |
| 1384 | struct list_head *pos, *n; |
| 1385 | struct pxe_label *label; |
| 1386 | |
Simon Glass | 764d0c0 | 2021-10-14 12:48:02 -0600 | [diff] [blame] | 1387 | free(cfg->title); |
| 1388 | free(cfg->default_label); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1389 | |
| 1390 | list_for_each_safe(pos, n, &cfg->labels) { |
| 1391 | label = list_entry(pos, struct pxe_label, list); |
| 1392 | |
| 1393 | label_destroy(label); |
| 1394 | } |
| 1395 | |
| 1396 | free(cfg); |
| 1397 | } |
| 1398 | |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1399 | struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg) |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1400 | { |
| 1401 | struct pxe_menu *cfg; |
| 1402 | char *buf; |
| 1403 | int r; |
| 1404 | |
| 1405 | cfg = malloc(sizeof(struct pxe_menu)); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1406 | if (!cfg) |
| 1407 | return NULL; |
| 1408 | |
| 1409 | memset(cfg, 0, sizeof(struct pxe_menu)); |
| 1410 | |
| 1411 | INIT_LIST_HEAD(&cfg->labels); |
| 1412 | |
| 1413 | buf = map_sysmem(menucfg, 0); |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1414 | r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1415 | unmap_sysmem(buf); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1416 | if (r < 0) { |
| 1417 | destroy_pxe_menu(cfg); |
| 1418 | return NULL; |
| 1419 | } |
| 1420 | |
| 1421 | return cfg; |
| 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic |
| 1426 | * menu code. |
| 1427 | */ |
| 1428 | static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg) |
| 1429 | { |
| 1430 | struct pxe_label *label; |
| 1431 | struct list_head *pos; |
| 1432 | struct menu *m; |
Amjad Ouled-Ameur | 1d564a9 | 2021-11-13 14:09:20 +0100 | [diff] [blame] | 1433 | char *label_override; |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1434 | int err; |
| 1435 | int i = 1; |
| 1436 | char *default_num = NULL; |
Amjad Ouled-Ameur | 1d564a9 | 2021-11-13 14:09:20 +0100 | [diff] [blame] | 1437 | char *override_num = NULL; |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1438 | |
| 1439 | /* |
| 1440 | * Create a menu and add items for all the labels. |
| 1441 | */ |
| 1442 | m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10), |
Thirupathaiah Annapureddy | d6b9f6b | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 1443 | cfg->prompt, NULL, label_print, NULL, NULL); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1444 | if (!m) |
| 1445 | return NULL; |
| 1446 | |
Amjad Ouled-Ameur | 1d564a9 | 2021-11-13 14:09:20 +0100 | [diff] [blame] | 1447 | label_override = env_get("pxe_label_override"); |
| 1448 | |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1449 | list_for_each(pos, &cfg->labels) { |
| 1450 | label = list_entry(pos, struct pxe_label, list); |
| 1451 | |
| 1452 | sprintf(label->num, "%d", i++); |
| 1453 | if (menu_item_add(m, label->num, label) != 1) { |
| 1454 | menu_destroy(m); |
| 1455 | return NULL; |
| 1456 | } |
| 1457 | if (cfg->default_label && |
| 1458 | (strcmp(label->name, cfg->default_label) == 0)) |
| 1459 | default_num = label->num; |
Amjad Ouled-Ameur | 1d564a9 | 2021-11-13 14:09:20 +0100 | [diff] [blame] | 1460 | if (label_override && !strcmp(label->name, label_override)) |
| 1461 | override_num = label->num; |
| 1462 | } |
| 1463 | |
| 1464 | |
| 1465 | if (label_override) { |
| 1466 | if (override_num) |
| 1467 | default_num = override_num; |
| 1468 | else |
| 1469 | printf("Missing override pxe label: %s\n", |
| 1470 | label_override); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | /* |
| 1474 | * After we've created items for each label in the menu, set the |
| 1475 | * menu's default label if one was specified. |
| 1476 | */ |
| 1477 | if (default_num) { |
| 1478 | err = menu_default_set(m, default_num); |
| 1479 | if (err != 1) { |
| 1480 | if (err != -ENOENT) { |
| 1481 | menu_destroy(m); |
| 1482 | return NULL; |
| 1483 | } |
| 1484 | |
| 1485 | printf("Missing default: %s\n", cfg->default_label); |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | return m; |
| 1490 | } |
| 1491 | |
| 1492 | /* |
| 1493 | * Try to boot any labels we have yet to attempt to boot. |
| 1494 | */ |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1495 | static void boot_unattempted_labels(struct pxe_context *ctx, |
| 1496 | struct pxe_menu *cfg) |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1497 | { |
| 1498 | struct list_head *pos; |
| 1499 | struct pxe_label *label; |
| 1500 | |
| 1501 | list_for_each(pos, &cfg->labels) { |
| 1502 | label = list_entry(pos, struct pxe_label, list); |
| 1503 | |
| 1504 | if (!label->attempted) |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1505 | label_boot(ctx, label); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1506 | } |
| 1507 | } |
| 1508 | |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1509 | void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg) |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1510 | { |
| 1511 | void *choice; |
| 1512 | struct menu *m; |
| 1513 | int err; |
| 1514 | |
Kory Maincent | caabd24 | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 1515 | if (IS_ENABLED(CONFIG_CMD_BMP)) { |
| 1516 | /* display BMP if available */ |
| 1517 | if (cfg->bmp) { |
Simon Glass | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 1518 | if (get_relfile(ctx, cfg->bmp, image_load_addr, NULL)) { |
Patrick Delaunay | 6ec8cb9 | 2022-03-22 17:08:43 +0100 | [diff] [blame] | 1519 | #if defined(CONFIG_DM_VIDEO) |
| 1520 | struct udevice *dev; |
| 1521 | |
| 1522 | err = uclass_first_device_err(UCLASS_VIDEO, &dev); |
| 1523 | if (!err) |
| 1524 | video_clear(dev); |
| 1525 | #endif |
Kory Maincent | caabd24 | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 1526 | bmp_display(image_load_addr, |
| 1527 | BMP_ALIGN_CENTER, BMP_ALIGN_CENTER); |
| 1528 | } else { |
| 1529 | printf("Skipping background bmp %s for failure\n", |
| 1530 | cfg->bmp); |
| 1531 | } |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1532 | } |
| 1533 | } |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1534 | |
| 1535 | m = pxe_menu_to_menu(cfg); |
| 1536 | if (!m) |
| 1537 | return; |
| 1538 | |
| 1539 | err = menu_get_choice(m, &choice); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1540 | menu_destroy(m); |
| 1541 | |
| 1542 | /* |
| 1543 | * err == 1 means we got a choice back from menu_get_choice. |
| 1544 | * |
| 1545 | * err == -ENOENT if the menu was setup to select the default but no |
| 1546 | * default was set. in that case, we should continue trying to boot |
| 1547 | * labels that haven't been attempted yet. |
| 1548 | * |
| 1549 | * otherwise, the user interrupted or there was some other error and |
| 1550 | * we give up. |
| 1551 | */ |
| 1552 | |
| 1553 | if (err == 1) { |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1554 | err = label_boot(ctx, choice); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1555 | if (!err) |
| 1556 | return; |
| 1557 | } else if (err != -ENOENT) { |
| 1558 | return; |
| 1559 | } |
| 1560 | |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1561 | boot_unattempted_labels(ctx, cfg); |
| 1562 | } |
| 1563 | |
Simon Glass | e719fe0 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 1564 | int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp, |
| 1565 | pxe_getfile_func getfile, void *userdata, |
| 1566 | bool allow_abs_path, const char *bootfile) |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1567 | { |
Simon Glass | e719fe0 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 1568 | const char *last_slash; |
| 1569 | size_t path_len = 0; |
| 1570 | |
| 1571 | memset(ctx, '\0', sizeof(*ctx)); |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 1572 | ctx->cmdtp = cmdtp; |
Simon Glass | 44a20ef | 2021-10-14 12:47:57 -0600 | [diff] [blame] | 1573 | ctx->getfile = getfile; |
Simon Glass | 121e131 | 2021-10-14 12:47:58 -0600 | [diff] [blame] | 1574 | ctx->userdata = userdata; |
Simon Glass | 3ae416a | 2021-10-14 12:47:59 -0600 | [diff] [blame] | 1575 | ctx->allow_abs_path = allow_abs_path; |
Simon Glass | e719fe0 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 1576 | |
| 1577 | /* figure out the boot directory, if there is one */ |
| 1578 | if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN) |
| 1579 | return -ENOSPC; |
| 1580 | ctx->bootdir = strdup(bootfile ? bootfile : ""); |
| 1581 | if (!ctx->bootdir) |
| 1582 | return -ENOMEM; |
| 1583 | |
| 1584 | if (bootfile) { |
| 1585 | last_slash = strrchr(bootfile, '/'); |
| 1586 | if (last_slash) |
| 1587 | path_len = (last_slash - bootfile) + 1; |
| 1588 | } |
| 1589 | ctx->bootdir[path_len] = '\0'; |
| 1590 | |
| 1591 | return 0; |
| 1592 | } |
| 1593 | |
| 1594 | void pxe_destroy_ctx(struct pxe_context *ctx) |
| 1595 | { |
| 1596 | free(ctx->bootdir); |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1597 | } |
Simon Glass | 791bbfe | 2021-10-14 12:48:03 -0600 | [diff] [blame] | 1598 | |
| 1599 | int pxe_process(struct pxe_context *ctx, ulong pxefile_addr_r, bool prompt) |
| 1600 | { |
| 1601 | struct pxe_menu *cfg; |
| 1602 | |
| 1603 | cfg = parse_pxefile(ctx, pxefile_addr_r); |
| 1604 | if (!cfg) { |
| 1605 | printf("Error parsing config file\n"); |
| 1606 | return 1; |
| 1607 | } |
| 1608 | |
| 1609 | if (prompt) |
| 1610 | cfg->prompt = 1; |
| 1611 | |
| 1612 | handle_pxe_menu(ctx, cfg); |
| 1613 | |
| 1614 | destroy_pxe_menu(cfg); |
| 1615 | |
| 1616 | return 0; |
| 1617 | } |