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