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