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