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