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 | |
| 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 | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 87 | if (!last_slash) |
Patrice Chotard | 2373cba | 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 | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 94 | bootfile_path_size, path_len); |
Patrice Chotard | 2373cba | 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 | 0914011 | 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 | 2373cba | 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 | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 118 | static int get_relfile(struct cmd_tbl *cmdtp, const char *file_path, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 119 | unsigned long file_addr) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 120 | { |
| 121 | size_t path_len; |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 122 | char relfile[MAX_TFTP_PATH_LEN + 1]; |
Patrice Chotard | 2373cba | 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 | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 135 | printf("Base path too long (%s%s)\n", relfile, file_path); |
Patrice Chotard | 2373cba | 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 | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 156 | int get_pxe_file(struct cmd_tbl *cmdtp, const char *file_path, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 157 | unsigned long file_addr) |
Patrice Chotard | 2373cba | 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 | 2373cba | 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 | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 197 | int get_pxelinux_path(struct cmd_tbl *cmdtp, const char *file, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 198 | unsigned long pxefile_addr_r) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 199 | { |
| 200 | size_t base_len = strlen(PXELINUX_DIR); |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 201 | char path[MAX_TFTP_PATH_LEN + 1]; |
Patrice Chotard | 2373cba | 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 | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 205 | PXELINUX_DIR, file); |
Patrice Chotard | 2373cba | 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 | 0914011 | 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 | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 222 | const char *envaddr_name) |
Patrice Chotard | 2373cba | 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 | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 289 | if (label->fdtoverlays) |
| 290 | free(label->fdtoverlays); |
| 291 | |
Patrice Chotard | 2373cba | 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 | 1a62d64 | 2020-11-05 10:33:47 -0700 | [diff] [blame] | 330 | cli_simple_process_macros(label->append, bootargs, |
| 331 | sizeof(bootargs)); |
Patrice Chotard | 2373cba | 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 | } |
| 339 | |
| 340 | /* |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 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 */ |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 354 | fdt_addr = hextoul(env_get("fdt_addr_r"), NULL); |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 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 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 367 | fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL); |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 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 |
| 425 | |
| 426 | /* |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 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 | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 441 | static int label_boot(struct cmd_tbl *cmdtp, struct pxe_label *label) |
Patrice Chotard | 2373cba | 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 | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 463 | if (!label->kernel) { |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 464 | printf("No kernel given, skipping %s\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->initrd) { |
| 470 | if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) { |
| 471 | printf("Skipping %s for failure retrieving initrd\n", |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 472 | label->name); |
Patrice Chotard | 2373cba | 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 | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 485 | label->name); |
Patrice Chotard | 2373cba | 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 | |
Kory Maincent | ff0287e | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 495 | if (IS_ENABLED(CONFIG_CMD_NET)) { |
| 496 | if (label->ipappend & 0x2) { |
| 497 | int err; |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 498 | |
Kory Maincent | ff0287e | 2021-02-02 16:42:28 +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 | } |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 504 | } |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 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 | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 517 | } |
Patrice Chotard | 8cb22a6 | 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 | 1a62d64 | 2020-11-05 10:33:47 -0700 | [diff] [blame] | 525 | cli_simple_process_macros(bootargs, finalbootargs, |
| 526 | sizeof(finalbootargs)); |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 527 | env_set("bootargs", finalbootargs); |
| 528 | printf("append: %s\n", finalbootargs); |
Patrice Chotard | 2373cba | 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 | db36674 | 2019-09-03 10:52:24 +0300 | [diff] [blame] | 547 | * It handles the following scenarios. |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 548 | * |
Anton Leontiev | db36674 | 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 | 2373cba | 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"; |
Dimitri John Ledkov | 75efe7d | 2021-04-21 12:42:01 +0100 | [diff] [blame] | 590 | if (!f1) { |
| 591 | f1 = ""; |
| 592 | f2 = ""; |
| 593 | } |
| 594 | if (!f3) { |
| 595 | f2 = ""; |
| 596 | f3 = ""; |
| 597 | } |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | len = strlen(label->fdtdir); |
| 601 | if (!len) |
| 602 | slash = "./"; |
| 603 | else if (label->fdtdir[len - 1] != '/') |
| 604 | slash = "/"; |
| 605 | else |
| 606 | slash = ""; |
| 607 | |
| 608 | len = strlen(label->fdtdir) + strlen(slash) + |
| 609 | strlen(f1) + strlen(f2) + strlen(f3) + |
| 610 | strlen(f4) + 1; |
| 611 | fdtfilefree = malloc(len); |
| 612 | if (!fdtfilefree) { |
| 613 | printf("malloc fail (FDT filename)\n"); |
| 614 | goto cleanup; |
| 615 | } |
| 616 | |
| 617 | snprintf(fdtfilefree, len, "%s%s%s%s%s%s", |
| 618 | label->fdtdir, slash, f1, f2, f3, f4); |
| 619 | fdtfile = fdtfilefree; |
| 620 | } |
| 621 | |
| 622 | if (fdtfile) { |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 623 | int err = get_relfile_envaddr(cmdtp, fdtfile, |
| 624 | "fdt_addr_r"); |
| 625 | |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 626 | free(fdtfilefree); |
| 627 | if (err < 0) { |
Anton Leontiev | db36674 | 2019-09-03 10:52:24 +0300 | [diff] [blame] | 628 | bootm_argv[3] = NULL; |
| 629 | |
| 630 | if (label->fdt) { |
| 631 | printf("Skipping %s for failure retrieving FDT\n", |
| 632 | label->name); |
| 633 | goto cleanup; |
| 634 | } |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 635 | } |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 636 | |
| 637 | #ifdef CONFIG_OF_LIBFDT_OVERLAY |
| 638 | if (label->fdtoverlays) |
| 639 | label_boot_fdtoverlay(cmdtp, label); |
| 640 | #endif |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 641 | } else { |
| 642 | bootm_argv[3] = NULL; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | if (!bootm_argv[3]) |
| 647 | bootm_argv[3] = env_get("fdt_addr"); |
| 648 | |
| 649 | if (bootm_argv[3]) { |
| 650 | if (!bootm_argv[2]) |
| 651 | bootm_argv[2] = "-"; |
| 652 | bootm_argc = 4; |
| 653 | } |
| 654 | |
| 655 | kernel_addr = genimg_get_kernel_addr(bootm_argv[1]); |
| 656 | buf = map_sysmem(kernel_addr, 0); |
| 657 | /* Try bootm for legacy and FIT format image */ |
| 658 | if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID) |
| 659 | do_bootm(cmdtp, 0, bootm_argc, bootm_argv); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 660 | /* Try booting an AArch64 Linux kernel image */ |
Kory Maincent | ff0287e | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 661 | else if (IS_ENABLED(CONFIG_CMD_BOOTI)) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 662 | do_booti(cmdtp, 0, bootm_argc, bootm_argv); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 663 | /* Try booting a Image */ |
Kory Maincent | ff0287e | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 664 | else if (IS_ENABLED(CONFIG_CMD_BOOTZ)) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 665 | do_bootz(cmdtp, 0, bootm_argc, bootm_argv); |
Kory Maincent | 18c2582 | 2021-02-02 16:42:29 +0100 | [diff] [blame] | 666 | /* Try booting an x86_64 Linux kernel image */ |
| 667 | else if (IS_ENABLED(CONFIG_CMD_ZBOOT)) |
| 668 | do_zboot_parent(cmdtp, 0, bootm_argc, bootm_argv, NULL); |
Kory Maincent | ff0287e | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 669 | |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 670 | unmap_sysmem(buf); |
| 671 | |
| 672 | cleanup: |
| 673 | if (fit_addr) |
| 674 | free(fit_addr); |
| 675 | return 1; |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * Tokens for the pxe file parser. |
| 680 | */ |
| 681 | enum token_type { |
| 682 | T_EOL, |
| 683 | T_STRING, |
| 684 | T_EOF, |
| 685 | T_MENU, |
| 686 | T_TITLE, |
| 687 | T_TIMEOUT, |
| 688 | T_LABEL, |
| 689 | T_KERNEL, |
| 690 | T_LINUX, |
| 691 | T_APPEND, |
| 692 | T_INITRD, |
| 693 | T_LOCALBOOT, |
| 694 | T_DEFAULT, |
| 695 | T_PROMPT, |
| 696 | T_INCLUDE, |
| 697 | T_FDT, |
| 698 | T_FDTDIR, |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 699 | T_FDTOVERLAYS, |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 700 | T_ONTIMEOUT, |
| 701 | T_IPAPPEND, |
| 702 | T_BACKGROUND, |
| 703 | T_INVALID |
| 704 | }; |
| 705 | |
| 706 | /* |
| 707 | * A token - given by a value and a type. |
| 708 | */ |
| 709 | struct token { |
| 710 | char *val; |
| 711 | enum token_type type; |
| 712 | }; |
| 713 | |
| 714 | /* |
| 715 | * Keywords recognized. |
| 716 | */ |
| 717 | static const struct token keywords[] = { |
| 718 | {"menu", T_MENU}, |
| 719 | {"title", T_TITLE}, |
| 720 | {"timeout", T_TIMEOUT}, |
| 721 | {"default", T_DEFAULT}, |
| 722 | {"prompt", T_PROMPT}, |
| 723 | {"label", T_LABEL}, |
| 724 | {"kernel", T_KERNEL}, |
| 725 | {"linux", T_LINUX}, |
| 726 | {"localboot", T_LOCALBOOT}, |
| 727 | {"append", T_APPEND}, |
| 728 | {"initrd", T_INITRD}, |
| 729 | {"include", T_INCLUDE}, |
| 730 | {"devicetree", T_FDT}, |
| 731 | {"fdt", T_FDT}, |
| 732 | {"devicetreedir", T_FDTDIR}, |
| 733 | {"fdtdir", T_FDTDIR}, |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 734 | {"fdtoverlays", T_FDTOVERLAYS}, |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 735 | {"ontimeout", T_ONTIMEOUT,}, |
| 736 | {"ipappend", T_IPAPPEND,}, |
| 737 | {"background", T_BACKGROUND,}, |
| 738 | {NULL, T_INVALID} |
| 739 | }; |
| 740 | |
| 741 | /* |
| 742 | * Since pxe(linux) files don't have a token to identify the start of a |
| 743 | * literal, we have to keep track of when we're in a state where a literal is |
| 744 | * expected vs when we're in a state a keyword is expected. |
| 745 | */ |
| 746 | enum lex_state { |
| 747 | L_NORMAL = 0, |
| 748 | L_KEYWORD, |
| 749 | L_SLITERAL |
| 750 | }; |
| 751 | |
| 752 | /* |
| 753 | * get_string retrieves a string from *p and stores it as a token in |
| 754 | * *t. |
| 755 | * |
| 756 | * get_string used for scanning both string literals and keywords. |
| 757 | * |
| 758 | * Characters from *p are copied into t-val until a character equal to |
| 759 | * delim is found, or a NUL byte is reached. If delim has the special value of |
| 760 | * ' ', any whitespace character will be used as a delimiter. |
| 761 | * |
| 762 | * If lower is unequal to 0, uppercase characters will be converted to |
| 763 | * lowercase in the result. This is useful to make keywords case |
| 764 | * insensitive. |
| 765 | * |
| 766 | * The location of *p is updated to point to the first character after the end |
| 767 | * of the token - the ending delimiter. |
| 768 | * |
| 769 | * On success, the new value of t->val is returned. Memory for t->val is |
| 770 | * allocated using malloc and must be free()'d to reclaim it. If insufficient |
| 771 | * memory is available, NULL is returned. |
| 772 | */ |
| 773 | static char *get_string(char **p, struct token *t, char delim, int lower) |
| 774 | { |
| 775 | char *b, *e; |
| 776 | size_t len, i; |
| 777 | |
| 778 | /* |
| 779 | * b and e both start at the beginning of the input stream. |
| 780 | * |
| 781 | * e is incremented until we find the ending delimiter, or a NUL byte |
| 782 | * is reached. Then, we take e - b to find the length of the token. |
| 783 | */ |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 784 | b = *p; |
| 785 | e = *p; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 786 | |
| 787 | while (*e) { |
| 788 | if ((delim == ' ' && isspace(*e)) || delim == *e) |
| 789 | break; |
| 790 | e++; |
| 791 | } |
| 792 | |
| 793 | len = e - b; |
| 794 | |
| 795 | /* |
| 796 | * Allocate memory to hold the string, and copy it in, converting |
| 797 | * characters to lowercase if lower is != 0. |
| 798 | */ |
| 799 | t->val = malloc(len + 1); |
| 800 | if (!t->val) |
| 801 | return NULL; |
| 802 | |
| 803 | for (i = 0; i < len; i++, b++) { |
| 804 | if (lower) |
| 805 | t->val[i] = tolower(*b); |
| 806 | else |
| 807 | t->val[i] = *b; |
| 808 | } |
| 809 | |
| 810 | t->val[len] = '\0'; |
| 811 | |
| 812 | /* |
| 813 | * Update *p so the caller knows where to continue scanning. |
| 814 | */ |
| 815 | *p = e; |
| 816 | |
| 817 | t->type = T_STRING; |
| 818 | |
| 819 | return t->val; |
| 820 | } |
| 821 | |
| 822 | /* |
| 823 | * Populate a keyword token with a type and value. |
| 824 | */ |
| 825 | static void get_keyword(struct token *t) |
| 826 | { |
| 827 | int i; |
| 828 | |
| 829 | for (i = 0; keywords[i].val; i++) { |
| 830 | if (!strcmp(t->val, keywords[i].val)) { |
| 831 | t->type = keywords[i].type; |
| 832 | break; |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | /* |
| 838 | * Get the next token. We have to keep track of which state we're in to know |
| 839 | * if we're looking to get a string literal or a keyword. |
| 840 | * |
| 841 | * *p is updated to point at the first character after the current token. |
| 842 | */ |
| 843 | static void get_token(char **p, struct token *t, enum lex_state state) |
| 844 | { |
| 845 | char *c = *p; |
| 846 | |
| 847 | t->type = T_INVALID; |
| 848 | |
| 849 | /* eat non EOL whitespace */ |
| 850 | while (isblank(*c)) |
| 851 | c++; |
| 852 | |
| 853 | /* |
| 854 | * eat comments. note that string literals can't begin with #, but |
| 855 | * can contain a # after their first character. |
| 856 | */ |
| 857 | if (*c == '#') { |
| 858 | while (*c && *c != '\n') |
| 859 | c++; |
| 860 | } |
| 861 | |
| 862 | if (*c == '\n') { |
| 863 | t->type = T_EOL; |
| 864 | c++; |
| 865 | } else if (*c == '\0') { |
| 866 | t->type = T_EOF; |
| 867 | c++; |
| 868 | } else if (state == L_SLITERAL) { |
| 869 | get_string(&c, t, '\n', 0); |
| 870 | } else if (state == L_KEYWORD) { |
| 871 | /* |
| 872 | * when we expect a keyword, we first get the next string |
| 873 | * token delimited by whitespace, and then check if it |
| 874 | * matches a keyword in our keyword list. if it does, it's |
| 875 | * converted to a keyword token of the appropriate type, and |
| 876 | * if not, it remains a string token. |
| 877 | */ |
| 878 | get_string(&c, t, ' ', 1); |
| 879 | get_keyword(t); |
| 880 | } |
| 881 | |
| 882 | *p = c; |
| 883 | } |
| 884 | |
| 885 | /* |
| 886 | * Increment *c until we get to the end of the current line, or EOF. |
| 887 | */ |
| 888 | static void eol_or_eof(char **c) |
| 889 | { |
| 890 | while (**c && **c != '\n') |
| 891 | (*c)++; |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | * All of these parse_* functions share some common behavior. |
| 896 | * |
| 897 | * They finish with *c pointing after the token they parse, and return 1 on |
| 898 | * success, or < 0 on error. |
| 899 | */ |
| 900 | |
| 901 | /* |
| 902 | * Parse a string literal and store a pointer it at *dst. String literals |
| 903 | * terminate at the end of the line. |
| 904 | */ |
| 905 | static int parse_sliteral(char **c, char **dst) |
| 906 | { |
| 907 | struct token t; |
| 908 | char *s = *c; |
| 909 | |
| 910 | get_token(c, &t, L_SLITERAL); |
| 911 | |
| 912 | if (t.type != T_STRING) { |
| 913 | printf("Expected string literal: %.*s\n", (int)(*c - s), s); |
| 914 | return -EINVAL; |
| 915 | } |
| 916 | |
| 917 | *dst = t.val; |
| 918 | |
| 919 | return 1; |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * Parse a base 10 (unsigned) integer and store it at *dst. |
| 924 | */ |
| 925 | static int parse_integer(char **c, int *dst) |
| 926 | { |
| 927 | struct token t; |
| 928 | char *s = *c; |
| 929 | |
| 930 | get_token(c, &t, L_SLITERAL); |
| 931 | |
| 932 | if (t.type != T_STRING) { |
| 933 | printf("Expected string: %.*s\n", (int)(*c - s), s); |
| 934 | return -EINVAL; |
| 935 | } |
| 936 | |
| 937 | *dst = simple_strtol(t.val, NULL, 10); |
| 938 | |
| 939 | free(t.val); |
| 940 | |
| 941 | return 1; |
| 942 | } |
| 943 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 944 | static int parse_pxefile_top(struct cmd_tbl *cmdtp, char *p, unsigned long base, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 945 | struct pxe_menu *cfg, int nest_level); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 946 | |
| 947 | /* |
| 948 | * Parse an include statement, and retrieve and parse the file it mentions. |
| 949 | * |
| 950 | * base should point to a location where it's safe to store the file, and |
| 951 | * nest_level should indicate how many nested includes have occurred. For this |
| 952 | * include, nest_level has already been incremented and doesn't need to be |
| 953 | * incremented here. |
| 954 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 955 | static int handle_include(struct cmd_tbl *cmdtp, char **c, unsigned long base, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 956 | struct pxe_menu *cfg, int nest_level) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 957 | { |
| 958 | char *include_path; |
| 959 | char *s = *c; |
| 960 | int err; |
| 961 | char *buf; |
| 962 | int ret; |
| 963 | |
| 964 | err = parse_sliteral(c, &include_path); |
| 965 | |
| 966 | if (err < 0) { |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 967 | printf("Expected include path: %.*s\n", (int)(*c - s), s); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 968 | return err; |
| 969 | } |
| 970 | |
| 971 | err = get_pxe_file(cmdtp, include_path, base); |
| 972 | |
| 973 | if (err < 0) { |
| 974 | printf("Couldn't retrieve %s\n", include_path); |
| 975 | return err; |
| 976 | } |
| 977 | |
| 978 | buf = map_sysmem(base, 0); |
| 979 | ret = parse_pxefile_top(cmdtp, buf, base, cfg, nest_level); |
| 980 | unmap_sysmem(buf); |
| 981 | |
| 982 | return ret; |
| 983 | } |
| 984 | |
| 985 | /* |
| 986 | * Parse lines that begin with 'menu'. |
| 987 | * |
| 988 | * base and nest are provided to handle the 'menu include' case. |
| 989 | * |
| 990 | * base should point to a location where it's safe to store the included file. |
| 991 | * |
| 992 | * nest_level should be 1 when parsing the top level pxe file, 2 when parsing |
| 993 | * a file it includes, 3 when parsing a file included by that file, and so on. |
| 994 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 995 | static int parse_menu(struct cmd_tbl *cmdtp, char **c, struct pxe_menu *cfg, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 996 | unsigned long base, int nest_level) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 997 | { |
| 998 | struct token t; |
| 999 | char *s = *c; |
| 1000 | int err = 0; |
| 1001 | |
| 1002 | get_token(c, &t, L_KEYWORD); |
| 1003 | |
| 1004 | switch (t.type) { |
| 1005 | case T_TITLE: |
| 1006 | err = parse_sliteral(c, &cfg->title); |
| 1007 | |
| 1008 | break; |
| 1009 | |
| 1010 | case T_INCLUDE: |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1011 | err = handle_include(cmdtp, c, base, cfg, nest_level + 1); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1012 | break; |
| 1013 | |
| 1014 | case T_BACKGROUND: |
| 1015 | err = parse_sliteral(c, &cfg->bmp); |
| 1016 | break; |
| 1017 | |
| 1018 | default: |
| 1019 | printf("Ignoring malformed menu command: %.*s\n", |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1020 | (int)(*c - s), s); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | if (err < 0) |
| 1024 | return err; |
| 1025 | |
| 1026 | eol_or_eof(c); |
| 1027 | |
| 1028 | return 1; |
| 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * Handles parsing a 'menu line' when we're parsing a label. |
| 1033 | */ |
| 1034 | static int parse_label_menu(char **c, struct pxe_menu *cfg, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1035 | struct pxe_label *label) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1036 | { |
| 1037 | struct token t; |
| 1038 | char *s; |
| 1039 | |
| 1040 | s = *c; |
| 1041 | |
| 1042 | get_token(c, &t, L_KEYWORD); |
| 1043 | |
| 1044 | switch (t.type) { |
| 1045 | case T_DEFAULT: |
| 1046 | if (!cfg->default_label) |
| 1047 | cfg->default_label = strdup(label->name); |
| 1048 | |
| 1049 | if (!cfg->default_label) |
| 1050 | return -ENOMEM; |
| 1051 | |
| 1052 | break; |
| 1053 | case T_LABEL: |
| 1054 | parse_sliteral(c, &label->menu); |
| 1055 | break; |
| 1056 | default: |
| 1057 | printf("Ignoring malformed menu command: %.*s\n", |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1058 | (int)(*c - s), s); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | eol_or_eof(c); |
| 1062 | |
| 1063 | return 0; |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | * Handles parsing a 'kernel' label. |
| 1068 | * expecting "filename" or "<fit_filename>#cfg" |
| 1069 | */ |
| 1070 | static int parse_label_kernel(char **c, struct pxe_label *label) |
| 1071 | { |
| 1072 | char *s; |
| 1073 | int err; |
| 1074 | |
| 1075 | err = parse_sliteral(c, &label->kernel); |
| 1076 | if (err < 0) |
| 1077 | return err; |
| 1078 | |
| 1079 | s = strstr(label->kernel, "#"); |
| 1080 | if (!s) |
| 1081 | return 1; |
| 1082 | |
| 1083 | label->config = malloc(strlen(s) + 1); |
| 1084 | if (!label->config) |
| 1085 | return -ENOMEM; |
| 1086 | |
| 1087 | strcpy(label->config, s); |
| 1088 | *s = 0; |
| 1089 | |
| 1090 | return 1; |
| 1091 | } |
| 1092 | |
| 1093 | /* |
| 1094 | * Parses a label and adds it to the list of labels for a menu. |
| 1095 | * |
| 1096 | * A label ends when we either get to the end of a file, or |
| 1097 | * get some input we otherwise don't have a handler defined |
| 1098 | * for. |
| 1099 | * |
| 1100 | */ |
| 1101 | static int parse_label(char **c, struct pxe_menu *cfg) |
| 1102 | { |
| 1103 | struct token t; |
| 1104 | int len; |
| 1105 | char *s = *c; |
| 1106 | struct pxe_label *label; |
| 1107 | int err; |
| 1108 | |
| 1109 | label = label_create(); |
| 1110 | if (!label) |
| 1111 | return -ENOMEM; |
| 1112 | |
| 1113 | err = parse_sliteral(c, &label->name); |
| 1114 | if (err < 0) { |
| 1115 | printf("Expected label name: %.*s\n", (int)(*c - s), s); |
| 1116 | label_destroy(label); |
| 1117 | return -EINVAL; |
| 1118 | } |
| 1119 | |
| 1120 | list_add_tail(&label->list, &cfg->labels); |
| 1121 | |
| 1122 | while (1) { |
| 1123 | s = *c; |
| 1124 | get_token(c, &t, L_KEYWORD); |
| 1125 | |
| 1126 | err = 0; |
| 1127 | switch (t.type) { |
| 1128 | case T_MENU: |
| 1129 | err = parse_label_menu(c, cfg, label); |
| 1130 | break; |
| 1131 | |
| 1132 | case T_KERNEL: |
| 1133 | case T_LINUX: |
| 1134 | err = parse_label_kernel(c, label); |
| 1135 | break; |
| 1136 | |
| 1137 | case T_APPEND: |
| 1138 | err = parse_sliteral(c, &label->append); |
| 1139 | if (label->initrd) |
| 1140 | break; |
| 1141 | s = strstr(label->append, "initrd="); |
| 1142 | if (!s) |
| 1143 | break; |
| 1144 | s += 7; |
| 1145 | len = (int)(strchr(s, ' ') - s); |
| 1146 | label->initrd = malloc(len + 1); |
| 1147 | strncpy(label->initrd, s, len); |
| 1148 | label->initrd[len] = '\0'; |
| 1149 | |
| 1150 | break; |
| 1151 | |
| 1152 | case T_INITRD: |
| 1153 | if (!label->initrd) |
| 1154 | err = parse_sliteral(c, &label->initrd); |
| 1155 | break; |
| 1156 | |
| 1157 | case T_FDT: |
| 1158 | if (!label->fdt) |
| 1159 | err = parse_sliteral(c, &label->fdt); |
| 1160 | break; |
| 1161 | |
| 1162 | case T_FDTDIR: |
| 1163 | if (!label->fdtdir) |
| 1164 | err = parse_sliteral(c, &label->fdtdir); |
| 1165 | break; |
| 1166 | |
Neil Armstrong | 69076df | 2021-01-20 09:54:53 +0100 | [diff] [blame] | 1167 | case T_FDTOVERLAYS: |
| 1168 | if (!label->fdtoverlays) |
| 1169 | err = parse_sliteral(c, &label->fdtoverlays); |
| 1170 | break; |
| 1171 | |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1172 | case T_LOCALBOOT: |
| 1173 | label->localboot = 1; |
| 1174 | err = parse_integer(c, &label->localboot_val); |
| 1175 | break; |
| 1176 | |
| 1177 | case T_IPAPPEND: |
| 1178 | err = parse_integer(c, &label->ipappend); |
| 1179 | break; |
| 1180 | |
| 1181 | case T_EOL: |
| 1182 | break; |
| 1183 | |
| 1184 | default: |
| 1185 | /* |
| 1186 | * put the token back! we don't want it - it's the end |
| 1187 | * of a label and whatever token this is, it's |
| 1188 | * something for the menu level context to handle. |
| 1189 | */ |
| 1190 | *c = s; |
| 1191 | return 1; |
| 1192 | } |
| 1193 | |
| 1194 | if (err < 0) |
| 1195 | return err; |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | /* |
| 1200 | * This 16 comes from the limit pxelinux imposes on nested includes. |
| 1201 | * |
| 1202 | * There is no reason at all we couldn't do more, but some limit helps prevent |
| 1203 | * infinite (until crash occurs) recursion if a file tries to include itself. |
| 1204 | */ |
| 1205 | #define MAX_NEST_LEVEL 16 |
| 1206 | |
| 1207 | /* |
| 1208 | * Entry point for parsing a menu file. nest_level indicates how many times |
| 1209 | * we've nested in includes. It will be 1 for the top level menu file. |
| 1210 | * |
| 1211 | * Returns 1 on success, < 0 on error. |
| 1212 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1213 | static int parse_pxefile_top(struct cmd_tbl *cmdtp, char *p, unsigned long base, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1214 | struct pxe_menu *cfg, int nest_level) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1215 | { |
| 1216 | struct token t; |
| 1217 | char *s, *b, *label_name; |
| 1218 | int err; |
| 1219 | |
| 1220 | b = p; |
| 1221 | |
| 1222 | if (nest_level > MAX_NEST_LEVEL) { |
| 1223 | printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL); |
| 1224 | return -EMLINK; |
| 1225 | } |
| 1226 | |
| 1227 | while (1) { |
| 1228 | s = p; |
| 1229 | |
| 1230 | get_token(&p, &t, L_KEYWORD); |
| 1231 | |
| 1232 | err = 0; |
| 1233 | switch (t.type) { |
| 1234 | case T_MENU: |
| 1235 | cfg->prompt = 1; |
| 1236 | err = parse_menu(cmdtp, &p, cfg, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1237 | base + ALIGN(strlen(b) + 1, 4), |
| 1238 | nest_level); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1239 | break; |
| 1240 | |
| 1241 | case T_TIMEOUT: |
| 1242 | err = parse_integer(&p, &cfg->timeout); |
| 1243 | break; |
| 1244 | |
| 1245 | case T_LABEL: |
| 1246 | err = parse_label(&p, cfg); |
| 1247 | break; |
| 1248 | |
| 1249 | case T_DEFAULT: |
| 1250 | case T_ONTIMEOUT: |
| 1251 | err = parse_sliteral(&p, &label_name); |
| 1252 | |
| 1253 | if (label_name) { |
| 1254 | if (cfg->default_label) |
| 1255 | free(cfg->default_label); |
| 1256 | |
| 1257 | cfg->default_label = label_name; |
| 1258 | } |
| 1259 | |
| 1260 | break; |
| 1261 | |
| 1262 | case T_INCLUDE: |
| 1263 | err = handle_include(cmdtp, &p, |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1264 | base + ALIGN(strlen(b), 4), cfg, |
| 1265 | nest_level + 1); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1266 | break; |
| 1267 | |
| 1268 | case T_PROMPT: |
| 1269 | eol_or_eof(&p); |
| 1270 | break; |
| 1271 | |
| 1272 | case T_EOL: |
| 1273 | break; |
| 1274 | |
| 1275 | case T_EOF: |
| 1276 | return 1; |
| 1277 | |
| 1278 | default: |
| 1279 | printf("Ignoring unknown command: %.*s\n", |
Patrice Chotard | 8cb22a6 | 2019-11-25 09:07:39 +0100 | [diff] [blame] | 1280 | (int)(p - s), s); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1281 | eol_or_eof(&p); |
| 1282 | } |
| 1283 | |
| 1284 | if (err < 0) |
| 1285 | return err; |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | /* |
| 1290 | * Free the memory used by a pxe_menu and its labels. |
| 1291 | */ |
| 1292 | void destroy_pxe_menu(struct pxe_menu *cfg) |
| 1293 | { |
| 1294 | struct list_head *pos, *n; |
| 1295 | struct pxe_label *label; |
| 1296 | |
| 1297 | if (cfg->title) |
| 1298 | free(cfg->title); |
| 1299 | |
| 1300 | if (cfg->default_label) |
| 1301 | free(cfg->default_label); |
| 1302 | |
| 1303 | list_for_each_safe(pos, n, &cfg->labels) { |
| 1304 | label = list_entry(pos, struct pxe_label, list); |
| 1305 | |
| 1306 | label_destroy(label); |
| 1307 | } |
| 1308 | |
| 1309 | free(cfg); |
| 1310 | } |
| 1311 | |
| 1312 | /* |
| 1313 | * Entry point for parsing a pxe file. This is only used for the top level |
| 1314 | * file. |
| 1315 | * |
| 1316 | * Returns NULL if there is an error, otherwise, returns a pointer to a |
| 1317 | * pxe_menu struct populated with the results of parsing the pxe file (and any |
| 1318 | * files it includes). The resulting pxe_menu struct can be free()'d by using |
| 1319 | * the destroy_pxe_menu() function. |
| 1320 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1321 | struct pxe_menu *parse_pxefile(struct cmd_tbl *cmdtp, unsigned long menucfg) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1322 | { |
| 1323 | struct pxe_menu *cfg; |
| 1324 | char *buf; |
| 1325 | int r; |
| 1326 | |
| 1327 | cfg = malloc(sizeof(struct pxe_menu)); |
| 1328 | |
| 1329 | if (!cfg) |
| 1330 | return NULL; |
| 1331 | |
| 1332 | memset(cfg, 0, sizeof(struct pxe_menu)); |
| 1333 | |
| 1334 | INIT_LIST_HEAD(&cfg->labels); |
| 1335 | |
| 1336 | buf = map_sysmem(menucfg, 0); |
| 1337 | r = parse_pxefile_top(cmdtp, buf, menucfg, cfg, 1); |
| 1338 | unmap_sysmem(buf); |
| 1339 | |
| 1340 | if (r < 0) { |
| 1341 | destroy_pxe_menu(cfg); |
| 1342 | return NULL; |
| 1343 | } |
| 1344 | |
| 1345 | return cfg; |
| 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic |
| 1350 | * menu code. |
| 1351 | */ |
| 1352 | static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg) |
| 1353 | { |
| 1354 | struct pxe_label *label; |
| 1355 | struct list_head *pos; |
| 1356 | struct menu *m; |
| 1357 | int err; |
| 1358 | int i = 1; |
| 1359 | char *default_num = NULL; |
| 1360 | |
| 1361 | /* |
| 1362 | * Create a menu and add items for all the labels. |
| 1363 | */ |
| 1364 | m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10), |
Thirupathaiah Annapureddy | 5168d7a | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 1365 | cfg->prompt, NULL, label_print, NULL, NULL); |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1366 | |
| 1367 | if (!m) |
| 1368 | return NULL; |
| 1369 | |
| 1370 | list_for_each(pos, &cfg->labels) { |
| 1371 | label = list_entry(pos, struct pxe_label, list); |
| 1372 | |
| 1373 | sprintf(label->num, "%d", i++); |
| 1374 | if (menu_item_add(m, label->num, label) != 1) { |
| 1375 | menu_destroy(m); |
| 1376 | return NULL; |
| 1377 | } |
| 1378 | if (cfg->default_label && |
| 1379 | (strcmp(label->name, cfg->default_label) == 0)) |
| 1380 | default_num = label->num; |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | /* |
| 1384 | * After we've created items for each label in the menu, set the |
| 1385 | * menu's default label if one was specified. |
| 1386 | */ |
| 1387 | if (default_num) { |
| 1388 | err = menu_default_set(m, default_num); |
| 1389 | if (err != 1) { |
| 1390 | if (err != -ENOENT) { |
| 1391 | menu_destroy(m); |
| 1392 | return NULL; |
| 1393 | } |
| 1394 | |
| 1395 | printf("Missing default: %s\n", cfg->default_label); |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | return m; |
| 1400 | } |
| 1401 | |
| 1402 | /* |
| 1403 | * Try to boot any labels we have yet to attempt to boot. |
| 1404 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1405 | static void boot_unattempted_labels(struct cmd_tbl *cmdtp, struct pxe_menu *cfg) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1406 | { |
| 1407 | struct list_head *pos; |
| 1408 | struct pxe_label *label; |
| 1409 | |
| 1410 | list_for_each(pos, &cfg->labels) { |
| 1411 | label = list_entry(pos, struct pxe_label, list); |
| 1412 | |
| 1413 | if (!label->attempted) |
| 1414 | label_boot(cmdtp, label); |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | /* |
| 1419 | * Boot the system as prescribed by a pxe_menu. |
| 1420 | * |
| 1421 | * Use the menu system to either get the user's choice or the default, based |
| 1422 | * on config or user input. If there is no default or user's choice, |
| 1423 | * attempted to boot labels in the order they were given in pxe files. |
| 1424 | * If the default or user's choice fails to boot, attempt to boot other |
| 1425 | * labels in the order they were given in pxe files. |
| 1426 | * |
| 1427 | * If this function returns, there weren't any labels that successfully |
| 1428 | * booted, or the user interrupted the menu selection via ctrl+c. |
| 1429 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1430 | void handle_pxe_menu(struct cmd_tbl *cmdtp, struct pxe_menu *cfg) |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1431 | { |
| 1432 | void *choice; |
| 1433 | struct menu *m; |
| 1434 | int err; |
| 1435 | |
Kory Maincent | ff0287e | 2021-02-02 16:42:28 +0100 | [diff] [blame] | 1436 | if (IS_ENABLED(CONFIG_CMD_BMP)) { |
| 1437 | /* display BMP if available */ |
| 1438 | if (cfg->bmp) { |
| 1439 | if (get_relfile(cmdtp, cfg->bmp, image_load_addr)) { |
| 1440 | if (CONFIG_IS_ENABLED(CMD_CLS)) |
| 1441 | run_command("cls", 0); |
| 1442 | bmp_display(image_load_addr, |
| 1443 | BMP_ALIGN_CENTER, BMP_ALIGN_CENTER); |
| 1444 | } else { |
| 1445 | printf("Skipping background bmp %s for failure\n", |
| 1446 | cfg->bmp); |
| 1447 | } |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1448 | } |
| 1449 | } |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 1450 | |
| 1451 | m = pxe_menu_to_menu(cfg); |
| 1452 | if (!m) |
| 1453 | return; |
| 1454 | |
| 1455 | err = menu_get_choice(m, &choice); |
| 1456 | |
| 1457 | menu_destroy(m); |
| 1458 | |
| 1459 | /* |
| 1460 | * err == 1 means we got a choice back from menu_get_choice. |
| 1461 | * |
| 1462 | * err == -ENOENT if the menu was setup to select the default but no |
| 1463 | * default was set. in that case, we should continue trying to boot |
| 1464 | * labels that haven't been attempted yet. |
| 1465 | * |
| 1466 | * otherwise, the user interrupted or there was some other error and |
| 1467 | * we give up. |
| 1468 | */ |
| 1469 | |
| 1470 | if (err == 1) { |
| 1471 | err = label_boot(cmdtp, choice); |
| 1472 | if (!err) |
| 1473 | return; |
| 1474 | } else if (err != -ENOENT) { |
| 1475 | return; |
| 1476 | } |
| 1477 | |
| 1478 | boot_unattempted_labels(cmdtp, cfg); |
| 1479 | } |