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