Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010-2011 Calxeda, Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by the Free |
| 6 | * Software Foundation; either version 2 of the License, or (at your option) |
| 7 | * any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #include <common.h> |
| 18 | #include <command.h> |
| 19 | #include <malloc.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/ctype.h> |
| 22 | #include <errno.h> |
| 23 | #include <linux/list.h> |
| 24 | |
| 25 | #include "menu.h" |
Justin L Werner | 502afac | 2012-08-09 14:20:30 -0600 | [diff] [blame] | 26 | #if defined(CONFIG_BOOTP_VENDOREX) && defined(CONFIG_BOOTP_VENDOREX_PXE_SHARED) |
| 27 | #include "../net/magic.h" |
| 28 | #endif |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 29 | |
| 30 | #define MAX_TFTP_PATH_LEN 127 |
| 31 | |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 32 | /* |
| 33 | * Like getenv, but prints an error if envvar isn't defined in the |
| 34 | * environment. It always returns what getenv does, so it can be used in |
| 35 | * place of getenv without changing error handling otherwise. |
| 36 | */ |
| 37 | static char *from_env(char *envvar) |
| 38 | { |
| 39 | char *ret; |
| 40 | |
| 41 | ret = getenv(envvar); |
| 42 | |
| 43 | if (!ret) |
| 44 | printf("missing environment variable: %s\n", envvar); |
| 45 | |
| 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * Convert an ethaddr from the environment to the format used by pxelinux |
| 51 | * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to |
| 52 | * the beginning of the ethernet address to indicate a hardware type of |
| 53 | * Ethernet. Also converts uppercase hex characters into lowercase, to match |
| 54 | * pxelinux's behavior. |
| 55 | * |
| 56 | * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the |
| 57 | * environment, or some other value < 0 on error. |
| 58 | */ |
| 59 | static int format_mac_pxe(char *outbuf, size_t outbuf_len) |
| 60 | { |
| 61 | size_t ethaddr_len; |
| 62 | char *p, *ethaddr; |
| 63 | |
| 64 | ethaddr = from_env("ethaddr"); |
| 65 | |
| 66 | if (!ethaddr) |
John Rigby | 03e97da | 2011-07-13 23:05:19 -0600 | [diff] [blame] | 67 | ethaddr = from_env("usbethaddr"); |
| 68 | |
| 69 | if (!ethaddr) |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 70 | return -ENOENT; |
| 71 | |
| 72 | ethaddr_len = strlen(ethaddr); |
| 73 | |
| 74 | /* |
| 75 | * ethaddr_len + 4 gives room for "01-", ethaddr, and a NUL byte at |
| 76 | * the end. |
| 77 | */ |
| 78 | if (outbuf_len < ethaddr_len + 4) { |
| 79 | printf("outbuf is too small (%d < %d)\n", |
| 80 | outbuf_len, ethaddr_len + 4); |
| 81 | |
| 82 | return -EINVAL; |
| 83 | } |
| 84 | |
| 85 | strcpy(outbuf, "01-"); |
| 86 | |
| 87 | for (p = outbuf + 3; *ethaddr; ethaddr++, p++) { |
| 88 | if (*ethaddr == ':') |
| 89 | *p = '-'; |
| 90 | else |
| 91 | *p = tolower(*ethaddr); |
| 92 | } |
| 93 | |
| 94 | *p = '\0'; |
| 95 | |
| 96 | return 1; |
| 97 | } |
| 98 | |
Justin L Werner | 502afac | 2012-08-09 14:20:30 -0600 | [diff] [blame] | 99 | #if defined(CONFIG_BOOTP_VENDOREX) && defined(CONFIG_BOOTP_VENDOREX_PXE_SHARED) |
| 100 | /* |
| 101 | * Returns the directory the file specified in the bootfile env variable is |
| 102 | * in, unless DHCP MAGIC was received and we have a PathPrefix value, in which |
| 103 | * case the bootenv (if it was specified) is overridden. If bootfile |
| 104 | * isn't defined in the environment or obtained from PathPrefix DHCP |
| 105 | * vendor extension, return NULL, which should be interpreted as |
| 106 | * "don't prepend anything to paths". |
| 107 | */ |
| 108 | #else |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 109 | /* |
| 110 | * Returns the directory the file specified in the bootfile env variable is |
| 111 | * in. If bootfile isn't defined in the environment, return NULL, which should |
| 112 | * be interpreted as "don't prepend anything to paths". |
| 113 | */ |
Justin L Werner | 502afac | 2012-08-09 14:20:30 -0600 | [diff] [blame] | 114 | #endif |
Rob Herring | 90ba7d7 | 2012-03-28 05:51:36 +0000 | [diff] [blame] | 115 | static int get_bootfile_path(const char *file_path, char *bootfile_path, |
| 116 | size_t bootfile_path_size) |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 117 | { |
| 118 | char *bootfile, *last_slash; |
Rob Herring | 90ba7d7 | 2012-03-28 05:51:36 +0000 | [diff] [blame] | 119 | size_t path_len = 0; |
Justin L Werner | 502afac | 2012-08-09 14:20:30 -0600 | [diff] [blame] | 120 | #if defined(CONFIG_BOOTP_VENDOREX) && defined(CONFIG_BOOTP_VENDOREX_PXE_SHARED) |
| 121 | char *path_prefix = getenv(PXE_PATH_PREFIX); |
| 122 | |
| 123 | /* Override the initial environment */ |
| 124 | if (path_prefix) { |
| 125 | setenv("bootfile", path_prefix); |
| 126 | } |
| 127 | #endif |
| 128 | |
| 129 | bootfile = from_env("bootfile"); |
Rob Herring | 90ba7d7 | 2012-03-28 05:51:36 +0000 | [diff] [blame] | 130 | |
| 131 | if (file_path[0] == '/') |
| 132 | goto ret; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 133 | |
| 134 | bootfile = from_env("bootfile"); |
Rob Herring | 90ba7d7 | 2012-03-28 05:51:36 +0000 | [diff] [blame] | 135 | if (!bootfile) |
| 136 | goto ret; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 137 | |
| 138 | last_slash = strrchr(bootfile, '/'); |
| 139 | |
Rob Herring | 90ba7d7 | 2012-03-28 05:51:36 +0000 | [diff] [blame] | 140 | if (last_slash == NULL) |
| 141 | goto ret; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 142 | |
| 143 | path_len = (last_slash - bootfile) + 1; |
| 144 | |
| 145 | if (bootfile_path_size < path_len) { |
| 146 | printf("bootfile_path too small. (%d < %d)\n", |
| 147 | bootfile_path_size, path_len); |
| 148 | |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | strncpy(bootfile_path, bootfile, path_len); |
| 153 | |
Rob Herring | 90ba7d7 | 2012-03-28 05:51:36 +0000 | [diff] [blame] | 154 | ret: |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 155 | bootfile_path[path_len] = '\0'; |
| 156 | |
| 157 | return 1; |
| 158 | } |
| 159 | |
Rob Herring | 669df7e | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 160 | static int (*do_getfile)(char *file_path, char *file_addr); |
| 161 | |
| 162 | static int do_get_tftp(char *file_path, char *file_addr) |
| 163 | { |
| 164 | char *tftp_argv[] = {"tftp", NULL, NULL, NULL}; |
| 165 | |
| 166 | tftp_argv[1] = file_addr; |
| 167 | tftp_argv[2] = file_path; |
| 168 | |
| 169 | if (do_tftpb(NULL, 0, 3, tftp_argv)) |
| 170 | return -ENOENT; |
| 171 | |
| 172 | return 1; |
| 173 | } |
| 174 | |
| 175 | static char *fs_argv[5]; |
| 176 | |
| 177 | static int do_get_ext2(char *file_path, char *file_addr) |
| 178 | { |
| 179 | #ifdef CONFIG_CMD_EXT2 |
| 180 | fs_argv[0] = "ext2load"; |
| 181 | fs_argv[3] = file_addr; |
| 182 | fs_argv[4] = file_path; |
| 183 | |
| 184 | if (!do_ext2load(NULL, 0, 5, fs_argv)) |
| 185 | return 1; |
| 186 | #endif |
| 187 | return -ENOENT; |
| 188 | } |
| 189 | |
| 190 | static int do_get_fat(char *file_path, char *file_addr) |
| 191 | { |
| 192 | #ifdef CONFIG_CMD_FAT |
| 193 | fs_argv[0] = "fatload"; |
| 194 | fs_argv[3] = file_addr; |
| 195 | fs_argv[4] = file_path; |
| 196 | |
| 197 | if (!do_fat_fsload(NULL, 0, 5, fs_argv)) |
| 198 | return 1; |
| 199 | #endif |
| 200 | return -ENOENT; |
| 201 | } |
| 202 | |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 203 | /* |
| 204 | * As in pxelinux, paths to files referenced from files we retrieve are |
| 205 | * relative to the location of bootfile. get_relfile takes such a path and |
| 206 | * joins it with the bootfile path to get the full path to the target file. If |
| 207 | * the bootfile path is NULL, we use file_path as is. |
| 208 | * |
| 209 | * Returns 1 for success, or < 0 on error. |
| 210 | */ |
| 211 | static int get_relfile(char *file_path, void *file_addr) |
| 212 | { |
| 213 | size_t path_len; |
| 214 | char relfile[MAX_TFTP_PATH_LEN+1]; |
| 215 | char addr_buf[10]; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 216 | int err; |
| 217 | |
Rob Herring | 90ba7d7 | 2012-03-28 05:51:36 +0000 | [diff] [blame] | 218 | err = get_bootfile_path(file_path, relfile, sizeof(relfile)); |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 219 | |
| 220 | if (err < 0) |
| 221 | return err; |
| 222 | |
| 223 | path_len = strlen(file_path); |
| 224 | path_len += strlen(relfile); |
| 225 | |
| 226 | if (path_len > MAX_TFTP_PATH_LEN) { |
| 227 | printf("Base path too long (%s%s)\n", |
| 228 | relfile, |
| 229 | file_path); |
| 230 | |
| 231 | return -ENAMETOOLONG; |
| 232 | } |
| 233 | |
| 234 | strcat(relfile, file_path); |
| 235 | |
| 236 | printf("Retrieving file: %s\n", relfile); |
| 237 | |
| 238 | sprintf(addr_buf, "%p", file_addr); |
| 239 | |
Rob Herring | 669df7e | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 240 | return do_getfile(relfile, addr_buf); |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If |
| 245 | * 'bootfile' was specified in the environment, the path to bootfile will be |
| 246 | * prepended to 'file_path' and the resulting path will be used. |
| 247 | * |
| 248 | * Returns 1 on success, or < 0 for error. |
| 249 | */ |
| 250 | static int get_pxe_file(char *file_path, void *file_addr) |
| 251 | { |
| 252 | unsigned long config_file_size; |
| 253 | char *tftp_filesize; |
| 254 | int err; |
| 255 | |
| 256 | err = get_relfile(file_path, file_addr); |
| 257 | |
| 258 | if (err < 0) |
| 259 | return err; |
| 260 | |
| 261 | /* |
| 262 | * the file comes without a NUL byte at the end, so find out its size |
| 263 | * and add the NUL byte. |
| 264 | */ |
| 265 | tftp_filesize = from_env("filesize"); |
| 266 | |
| 267 | if (!tftp_filesize) |
| 268 | return -ENOENT; |
| 269 | |
| 270 | if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0) |
| 271 | return -EINVAL; |
| 272 | |
| 273 | *(char *)(file_addr + config_file_size) = '\0'; |
| 274 | |
| 275 | return 1; |
| 276 | } |
| 277 | |
| 278 | #define PXELINUX_DIR "pxelinux.cfg/" |
| 279 | |
| 280 | /* |
| 281 | * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file |
| 282 | * to do the hard work, the location of the 'pxelinux.cfg' folder is generated |
| 283 | * from the bootfile path, as described above. |
| 284 | * |
| 285 | * Returns 1 on success or < 0 on error. |
| 286 | */ |
| 287 | static int get_pxelinux_path(char *file, void *pxefile_addr_r) |
| 288 | { |
| 289 | size_t base_len = strlen(PXELINUX_DIR); |
| 290 | char path[MAX_TFTP_PATH_LEN+1]; |
| 291 | |
| 292 | if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) { |
| 293 | printf("path (%s%s) too long, skipping\n", |
| 294 | PXELINUX_DIR, file); |
| 295 | return -ENAMETOOLONG; |
| 296 | } |
| 297 | |
| 298 | sprintf(path, PXELINUX_DIR "%s", file); |
| 299 | |
| 300 | return get_pxe_file(path, pxefile_addr_r); |
| 301 | } |
| 302 | |
Justin L Werner | 502afac | 2012-08-09 14:20:30 -0600 | [diff] [blame] | 303 | #if defined(CONFIG_BOOTP_VENDOREX) && defined(CONFIG_BOOTP_VENDOREX_PXE_SHARED) |
| 304 | /* |
| 305 | * Attempt to load a config file obtained from DHCP vendor extension option |
| 306 | * |
| 307 | * Returns 1 on success or < 0 on error. |
| 308 | */ |
| 309 | static int pxe_dhcp_config_path(void *pxefile_addr_r) |
| 310 | { |
| 311 | char *name = getenv(PXE_CONFIG_FILE); |
| 312 | int retval = -ENOENT; |
| 313 | |
| 314 | if (name && *name) { |
| 315 | retval = get_pxelinux_path(name, pxefile_addr_r); |
| 316 | } |
| 317 | |
| 318 | return retval; |
| 319 | } |
| 320 | #endif |
| 321 | |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 322 | /* |
| 323 | * Looks for a pxe file with a name based on the pxeuuid environment variable. |
| 324 | * |
| 325 | * Returns 1 on success or < 0 on error. |
| 326 | */ |
| 327 | static int pxe_uuid_path(void *pxefile_addr_r) |
| 328 | { |
| 329 | char *uuid_str; |
| 330 | |
| 331 | uuid_str = from_env("pxeuuid"); |
| 332 | |
| 333 | if (!uuid_str) |
| 334 | return -ENOENT; |
| 335 | |
| 336 | return get_pxelinux_path(uuid_str, pxefile_addr_r); |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Looks for a pxe file with a name based on the 'ethaddr' environment |
| 341 | * variable. |
| 342 | * |
| 343 | * Returns 1 on success or < 0 on error. |
| 344 | */ |
| 345 | static int pxe_mac_path(void *pxefile_addr_r) |
| 346 | { |
| 347 | char mac_str[21]; |
| 348 | int err; |
| 349 | |
| 350 | err = format_mac_pxe(mac_str, sizeof(mac_str)); |
| 351 | |
| 352 | if (err < 0) |
| 353 | return err; |
| 354 | |
| 355 | return get_pxelinux_path(mac_str, pxefile_addr_r); |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Looks for pxe files with names based on our IP address. See pxelinux |
| 360 | * documentation for details on what these file names look like. We match |
| 361 | * that exactly. |
| 362 | * |
| 363 | * Returns 1 on success or < 0 on error. |
| 364 | */ |
| 365 | static int pxe_ipaddr_paths(void *pxefile_addr_r) |
| 366 | { |
| 367 | char ip_addr[9]; |
| 368 | int mask_pos, err; |
| 369 | |
| 370 | sprintf(ip_addr, "%08X", ntohl(NetOurIP)); |
| 371 | |
| 372 | for (mask_pos = 7; mask_pos >= 0; mask_pos--) { |
| 373 | err = get_pxelinux_path(ip_addr, pxefile_addr_r); |
| 374 | |
| 375 | if (err > 0) |
| 376 | return err; |
| 377 | |
| 378 | ip_addr[mask_pos] = '\0'; |
| 379 | } |
| 380 | |
| 381 | return -ENOENT; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Entry point for the 'pxe get' command. |
| 386 | * This Follows pxelinux's rules to download a config file from a tftp server. |
| 387 | * The file is stored at the location given by the pxefile_addr_r environment |
| 388 | * variable, which must be set. |
| 389 | * |
| 390 | * UUID comes from pxeuuid env variable, if defined |
| 391 | * MAC addr comes from ethaddr env variable, if defined |
| 392 | * IP |
| 393 | * |
| 394 | * see http://syslinux.zytor.com/wiki/index.php/PXELINUX |
| 395 | * |
| 396 | * Returns 0 on success or 1 on error. |
| 397 | */ |
| 398 | static int |
| 399 | do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 400 | { |
| 401 | char *pxefile_addr_str; |
Jason Hobbs | 834c938 | 2012-03-05 08:12:28 +0000 | [diff] [blame] | 402 | unsigned long pxefile_addr_r; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 403 | int err; |
| 404 | |
Rob Herring | 669df7e | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 405 | do_getfile = do_get_tftp; |
| 406 | |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 407 | if (argc != 1) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 408 | return CMD_RET_USAGE; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 409 | |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 410 | pxefile_addr_str = from_env("pxefile_addr_r"); |
| 411 | |
| 412 | if (!pxefile_addr_str) |
| 413 | return 1; |
| 414 | |
| 415 | err = strict_strtoul(pxefile_addr_str, 16, |
| 416 | (unsigned long *)&pxefile_addr_r); |
| 417 | if (err < 0) |
| 418 | return 1; |
| 419 | |
| 420 | /* |
| 421 | * Keep trying paths until we successfully get a file we're looking |
| 422 | * for. |
| 423 | */ |
Justin L Werner | 502afac | 2012-08-09 14:20:30 -0600 | [diff] [blame] | 424 | #if defined(CONFIG_BOOTP_VENDOREX) && defined(CONFIG_BOOTP_VENDOREX_PXE_SHARED) |
| 425 | if (pxe_dhcp_config_path(pxefile_addr_r) > 0 |
| 426 | || pxe_uuid_path(pxefile_addr_r) > 0 |
| 427 | #else |
| 428 | if (pxe_uuid_path(pxefile_addr_r) > 0 |
| 429 | #endif |
Jason Hobbs | 834c938 | 2012-03-05 08:12:28 +0000 | [diff] [blame] | 430 | || pxe_mac_path((void *)pxefile_addr_r) > 0 |
| 431 | || pxe_ipaddr_paths((void *)pxefile_addr_r) > 0 |
| 432 | || get_pxelinux_path("default", (void *)pxefile_addr_r) > 0) { |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 433 | |
| 434 | printf("Config file found\n"); |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | printf("Config file not found\n"); |
| 440 | |
| 441 | return 1; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * Wrapper to make it easier to store the file at file_path in the location |
| 446 | * specified by envaddr_name. file_path will be joined to the bootfile path, |
| 447 | * if any is specified. |
| 448 | * |
| 449 | * Returns 1 on success or < 0 on error. |
| 450 | */ |
| 451 | static int get_relfile_envaddr(char *file_path, char *envaddr_name) |
| 452 | { |
Jason Hobbs | 834c938 | 2012-03-05 08:12:28 +0000 | [diff] [blame] | 453 | unsigned long file_addr; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 454 | char *envaddr; |
| 455 | |
| 456 | envaddr = from_env(envaddr_name); |
| 457 | |
| 458 | if (!envaddr) |
| 459 | return -ENOENT; |
| 460 | |
Jason Hobbs | 834c938 | 2012-03-05 08:12:28 +0000 | [diff] [blame] | 461 | if (strict_strtoul(envaddr, 16, &file_addr) < 0) |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 462 | return -EINVAL; |
| 463 | |
Jason Hobbs | 834c938 | 2012-03-05 08:12:28 +0000 | [diff] [blame] | 464 | return get_relfile(file_path, (void *)file_addr); |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /* |
| 468 | * A note on the pxe file parser. |
| 469 | * |
| 470 | * We're parsing files that use syslinux grammar, which has a few quirks. |
| 471 | * String literals must be recognized based on context - there is no |
| 472 | * quoting or escaping support. There's also nothing to explicitly indicate |
| 473 | * when a label section completes. We deal with that by ending a label |
| 474 | * section whenever we see a line that doesn't include. |
| 475 | * |
| 476 | * As with the syslinux family, this same file format could be reused in the |
| 477 | * future for non pxe purposes. The only action it takes during parsing that |
| 478 | * would throw this off is handling of include files. It assumes we're using |
| 479 | * pxe, and does a tftp download of a file listed as an include file in the |
| 480 | * middle of the parsing operation. That could be handled by refactoring it to |
| 481 | * take a 'include file getter' function. |
| 482 | */ |
| 483 | |
| 484 | /* |
| 485 | * Describes a single label given in a pxe file. |
| 486 | * |
| 487 | * Create these with the 'label_create' function given below. |
| 488 | * |
| 489 | * name - the name of the menu as given on the 'menu label' line. |
| 490 | * kernel - the path to the kernel file to use for this label. |
| 491 | * append - kernel command line to use when booting this label |
| 492 | * initrd - path to the initrd to use for this label. |
| 493 | * attempted - 0 if we haven't tried to boot this label, 1 if we have. |
| 494 | * localboot - 1 if this label specified 'localboot', 0 otherwise. |
| 495 | * list - lets these form a list, which a pxe_menu struct will hold. |
| 496 | */ |
| 497 | struct pxe_label { |
| 498 | char *name; |
Rob Herring | 7815c4e | 2012-03-28 05:51:34 +0000 | [diff] [blame] | 499 | char *menu; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 500 | char *kernel; |
| 501 | char *append; |
| 502 | char *initrd; |
| 503 | int attempted; |
| 504 | int localboot; |
| 505 | struct list_head list; |
| 506 | }; |
| 507 | |
| 508 | /* |
| 509 | * Describes a pxe menu as given via pxe files. |
| 510 | * |
| 511 | * title - the name of the menu as given by a 'menu title' line. |
| 512 | * default_label - the name of the default label, if any. |
| 513 | * timeout - time in tenths of a second to wait for a user key-press before |
| 514 | * booting the default label. |
| 515 | * prompt - if 0, don't prompt for a choice unless the timeout period is |
| 516 | * interrupted. If 1, always prompt for a choice regardless of |
| 517 | * timeout. |
| 518 | * labels - a list of labels defined for the menu. |
| 519 | */ |
| 520 | struct pxe_menu { |
| 521 | char *title; |
| 522 | char *default_label; |
| 523 | int timeout; |
| 524 | int prompt; |
| 525 | struct list_head labels; |
| 526 | }; |
| 527 | |
| 528 | /* |
| 529 | * Allocates memory for and initializes a pxe_label. This uses malloc, so the |
| 530 | * result must be free()'d to reclaim the memory. |
| 531 | * |
| 532 | * Returns NULL if malloc fails. |
| 533 | */ |
| 534 | static struct pxe_label *label_create(void) |
| 535 | { |
| 536 | struct pxe_label *label; |
| 537 | |
| 538 | label = malloc(sizeof(struct pxe_label)); |
| 539 | |
| 540 | if (!label) |
| 541 | return NULL; |
| 542 | |
| 543 | memset(label, 0, sizeof(struct pxe_label)); |
| 544 | |
| 545 | return label; |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * Free the memory used by a pxe_label, including that used by its name, |
| 550 | * kernel, append and initrd members, if they're non NULL. |
| 551 | * |
| 552 | * So - be sure to only use dynamically allocated memory for the members of |
| 553 | * the pxe_label struct, unless you want to clean it up first. These are |
| 554 | * currently only created by the pxe file parsing code. |
| 555 | */ |
| 556 | static void label_destroy(struct pxe_label *label) |
| 557 | { |
| 558 | if (label->name) |
| 559 | free(label->name); |
| 560 | |
| 561 | if (label->kernel) |
| 562 | free(label->kernel); |
| 563 | |
| 564 | if (label->append) |
| 565 | free(label->append); |
| 566 | |
| 567 | if (label->initrd) |
| 568 | free(label->initrd); |
| 569 | |
| 570 | free(label); |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * Print a label and its string members if they're defined. |
| 575 | * |
| 576 | * This is passed as a callback to the menu code for displaying each |
| 577 | * menu entry. |
| 578 | */ |
| 579 | static void label_print(void *data) |
| 580 | { |
| 581 | struct pxe_label *label = data; |
Rob Herring | 7815c4e | 2012-03-28 05:51:34 +0000 | [diff] [blame] | 582 | const char *c = label->menu ? label->menu : label->kernel; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 583 | |
Rob Herring | 7815c4e | 2012-03-28 05:51:34 +0000 | [diff] [blame] | 584 | printf("%s:\t%s\n", label->name, c); |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 585 | |
| 586 | if (label->kernel) |
Rob Herring | 7815c4e | 2012-03-28 05:51:34 +0000 | [diff] [blame] | 587 | printf("\t\tkernel: %s\n", label->kernel); |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 588 | |
| 589 | if (label->append) |
Rob Herring | 7815c4e | 2012-03-28 05:51:34 +0000 | [diff] [blame] | 590 | printf("\t\tappend: %s\n", label->append); |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 591 | |
| 592 | if (label->initrd) |
Rob Herring | 7815c4e | 2012-03-28 05:51:34 +0000 | [diff] [blame] | 593 | printf("\t\tinitrd: %s\n", label->initrd); |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /* |
| 597 | * Boot a label that specified 'localboot'. This requires that the 'localcmd' |
| 598 | * environment variable is defined. Its contents will be executed as U-boot |
| 599 | * command. If the label specified an 'append' line, its contents will be |
| 600 | * used to overwrite the contents of the 'bootargs' environment variable prior |
| 601 | * to running 'localcmd'. |
| 602 | * |
| 603 | * Returns 1 on success or < 0 on error. |
| 604 | */ |
| 605 | static int label_localboot(struct pxe_label *label) |
| 606 | { |
| 607 | char *localcmd, *dupcmd; |
| 608 | int ret; |
| 609 | |
| 610 | localcmd = from_env("localcmd"); |
| 611 | |
| 612 | if (!localcmd) |
| 613 | return -ENOENT; |
| 614 | |
| 615 | /* |
| 616 | * dup the command to avoid any issues with the version of it existing |
| 617 | * in the environment changing during the execution of the command. |
| 618 | */ |
| 619 | dupcmd = strdup(localcmd); |
| 620 | |
| 621 | if (!dupcmd) |
| 622 | return -ENOMEM; |
| 623 | |
| 624 | if (label->append) |
| 625 | setenv("bootargs", label->append); |
| 626 | |
| 627 | printf("running: %s\n", dupcmd); |
| 628 | |
Simon Glass | 009dde1 | 2012-02-14 19:59:20 +0000 | [diff] [blame] | 629 | ret = run_command(dupcmd, 0); |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 630 | |
| 631 | free(dupcmd); |
| 632 | |
| 633 | return ret; |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * Boot according to the contents of a pxe_label. |
| 638 | * |
| 639 | * If we can't boot for any reason, we return. A successful boot never |
| 640 | * returns. |
| 641 | * |
| 642 | * The kernel will be stored in the location given by the 'kernel_addr_r' |
| 643 | * environment variable. |
| 644 | * |
| 645 | * If the label specifies an initrd file, it will be stored in the location |
| 646 | * given by the 'ramdisk_addr_r' environment variable. |
| 647 | * |
| 648 | * If the label specifies an 'append' line, its contents will overwrite that |
| 649 | * of the 'bootargs' environment variable. |
| 650 | */ |
| 651 | static void label_boot(struct pxe_label *label) |
| 652 | { |
| 653 | char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL }; |
| 654 | int bootm_argc = 3; |
| 655 | |
| 656 | label_print(label); |
| 657 | |
| 658 | label->attempted = 1; |
| 659 | |
| 660 | if (label->localboot) { |
| 661 | label_localboot(label); |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | if (label->kernel == NULL) { |
| 666 | printf("No kernel given, skipping %s\n", |
| 667 | label->name); |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | if (label->initrd) { |
| 672 | if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) { |
| 673 | printf("Skipping %s for failure retrieving initrd\n", |
| 674 | label->name); |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | bootm_argv[2] = getenv("ramdisk_addr_r"); |
| 679 | } else { |
| 680 | bootm_argv[2] = "-"; |
| 681 | } |
| 682 | |
| 683 | if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) { |
| 684 | printf("Skipping %s for failure retrieving kernel\n", |
| 685 | label->name); |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | if (label->append) |
| 690 | setenv("bootargs", label->append); |
| 691 | |
| 692 | bootm_argv[1] = getenv("kernel_addr_r"); |
| 693 | |
| 694 | /* |
| 695 | * fdt usage is optional. If there is an fdt_addr specified, we will |
| 696 | * pass it along to bootm, and adjust argc appropriately. |
| 697 | */ |
| 698 | bootm_argv[3] = getenv("fdt_addr"); |
| 699 | |
| 700 | if (bootm_argv[3]) |
| 701 | bootm_argc = 4; |
| 702 | |
| 703 | do_bootm(NULL, 0, bootm_argc, bootm_argv); |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | * Tokens for the pxe file parser. |
| 708 | */ |
| 709 | enum token_type { |
| 710 | T_EOL, |
| 711 | T_STRING, |
| 712 | T_EOF, |
| 713 | T_MENU, |
| 714 | T_TITLE, |
| 715 | T_TIMEOUT, |
| 716 | T_LABEL, |
| 717 | T_KERNEL, |
Rob Herring | beb9f6c | 2012-03-28 05:51:35 +0000 | [diff] [blame] | 718 | T_LINUX, |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 719 | T_APPEND, |
| 720 | T_INITRD, |
| 721 | T_LOCALBOOT, |
| 722 | T_DEFAULT, |
| 723 | T_PROMPT, |
| 724 | T_INCLUDE, |
| 725 | T_INVALID |
| 726 | }; |
| 727 | |
| 728 | /* |
| 729 | * A token - given by a value and a type. |
| 730 | */ |
| 731 | struct token { |
| 732 | char *val; |
| 733 | enum token_type type; |
| 734 | }; |
| 735 | |
| 736 | /* |
| 737 | * Keywords recognized. |
| 738 | */ |
| 739 | static const struct token keywords[] = { |
| 740 | {"menu", T_MENU}, |
| 741 | {"title", T_TITLE}, |
| 742 | {"timeout", T_TIMEOUT}, |
| 743 | {"default", T_DEFAULT}, |
| 744 | {"prompt", T_PROMPT}, |
| 745 | {"label", T_LABEL}, |
| 746 | {"kernel", T_KERNEL}, |
Rob Herring | beb9f6c | 2012-03-28 05:51:35 +0000 | [diff] [blame] | 747 | {"linux", T_LINUX}, |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 748 | {"localboot", T_LOCALBOOT}, |
| 749 | {"append", T_APPEND}, |
| 750 | {"initrd", T_INITRD}, |
| 751 | {"include", T_INCLUDE}, |
| 752 | {NULL, T_INVALID} |
| 753 | }; |
| 754 | |
| 755 | /* |
| 756 | * Since pxe(linux) files don't have a token to identify the start of a |
| 757 | * literal, we have to keep track of when we're in a state where a literal is |
| 758 | * expected vs when we're in a state a keyword is expected. |
| 759 | */ |
| 760 | enum lex_state { |
| 761 | L_NORMAL = 0, |
| 762 | L_KEYWORD, |
| 763 | L_SLITERAL |
| 764 | }; |
| 765 | |
| 766 | /* |
| 767 | * get_string retrieves a string from *p and stores it as a token in |
| 768 | * *t. |
| 769 | * |
| 770 | * get_string used for scanning both string literals and keywords. |
| 771 | * |
| 772 | * Characters from *p are copied into t-val until a character equal to |
| 773 | * delim is found, or a NUL byte is reached. If delim has the special value of |
| 774 | * ' ', any whitespace character will be used as a delimiter. |
| 775 | * |
| 776 | * If lower is unequal to 0, uppercase characters will be converted to |
| 777 | * lowercase in the result. This is useful to make keywords case |
| 778 | * insensitive. |
| 779 | * |
| 780 | * The location of *p is updated to point to the first character after the end |
| 781 | * of the token - the ending delimiter. |
| 782 | * |
| 783 | * On success, the new value of t->val is returned. Memory for t->val is |
| 784 | * allocated using malloc and must be free()'d to reclaim it. If insufficient |
| 785 | * memory is available, NULL is returned. |
| 786 | */ |
| 787 | static char *get_string(char **p, struct token *t, char delim, int lower) |
| 788 | { |
| 789 | char *b, *e; |
| 790 | size_t len, i; |
| 791 | |
| 792 | /* |
| 793 | * b and e both start at the beginning of the input stream. |
| 794 | * |
| 795 | * e is incremented until we find the ending delimiter, or a NUL byte |
| 796 | * is reached. Then, we take e - b to find the length of the token. |
| 797 | */ |
| 798 | b = e = *p; |
| 799 | |
| 800 | while (*e) { |
| 801 | if ((delim == ' ' && isspace(*e)) || delim == *e) |
| 802 | break; |
| 803 | e++; |
| 804 | } |
| 805 | |
| 806 | len = e - b; |
| 807 | |
| 808 | /* |
| 809 | * Allocate memory to hold the string, and copy it in, converting |
| 810 | * characters to lowercase if lower is != 0. |
| 811 | */ |
| 812 | t->val = malloc(len + 1); |
| 813 | if (!t->val) |
| 814 | return NULL; |
| 815 | |
| 816 | for (i = 0; i < len; i++, b++) { |
| 817 | if (lower) |
| 818 | t->val[i] = tolower(*b); |
| 819 | else |
| 820 | t->val[i] = *b; |
| 821 | } |
| 822 | |
| 823 | t->val[len] = '\0'; |
| 824 | |
| 825 | /* |
| 826 | * Update *p so the caller knows where to continue scanning. |
| 827 | */ |
| 828 | *p = e; |
| 829 | |
| 830 | t->type = T_STRING; |
| 831 | |
| 832 | return t->val; |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | * Populate a keyword token with a type and value. |
| 837 | */ |
| 838 | static void get_keyword(struct token *t) |
| 839 | { |
| 840 | int i; |
| 841 | |
| 842 | for (i = 0; keywords[i].val; i++) { |
| 843 | if (!strcmp(t->val, keywords[i].val)) { |
| 844 | t->type = keywords[i].type; |
| 845 | break; |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * Get the next token. We have to keep track of which state we're in to know |
| 852 | * if we're looking to get a string literal or a keyword. |
| 853 | * |
| 854 | * *p is updated to point at the first character after the current token. |
| 855 | */ |
| 856 | static void get_token(char **p, struct token *t, enum lex_state state) |
| 857 | { |
| 858 | char *c = *p; |
| 859 | |
| 860 | t->type = T_INVALID; |
| 861 | |
| 862 | /* eat non EOL whitespace */ |
| 863 | while (isblank(*c)) |
| 864 | c++; |
| 865 | |
| 866 | /* |
| 867 | * eat comments. note that string literals can't begin with #, but |
| 868 | * can contain a # after their first character. |
| 869 | */ |
| 870 | if (*c == '#') { |
| 871 | while (*c && *c != '\n') |
| 872 | c++; |
| 873 | } |
| 874 | |
| 875 | if (*c == '\n') { |
| 876 | t->type = T_EOL; |
| 877 | c++; |
| 878 | } else if (*c == '\0') { |
| 879 | t->type = T_EOF; |
| 880 | c++; |
| 881 | } else if (state == L_SLITERAL) { |
| 882 | get_string(&c, t, '\n', 0); |
| 883 | } else if (state == L_KEYWORD) { |
| 884 | /* |
| 885 | * when we expect a keyword, we first get the next string |
| 886 | * token delimited by whitespace, and then check if it |
| 887 | * matches a keyword in our keyword list. if it does, it's |
| 888 | * converted to a keyword token of the appropriate type, and |
| 889 | * if not, it remains a string token. |
| 890 | */ |
| 891 | get_string(&c, t, ' ', 1); |
| 892 | get_keyword(t); |
| 893 | } |
| 894 | |
| 895 | *p = c; |
| 896 | } |
| 897 | |
| 898 | /* |
| 899 | * Increment *c until we get to the end of the current line, or EOF. |
| 900 | */ |
| 901 | static void eol_or_eof(char **c) |
| 902 | { |
| 903 | while (**c && **c != '\n') |
| 904 | (*c)++; |
| 905 | } |
| 906 | |
| 907 | /* |
| 908 | * All of these parse_* functions share some common behavior. |
| 909 | * |
| 910 | * They finish with *c pointing after the token they parse, and return 1 on |
| 911 | * success, or < 0 on error. |
| 912 | */ |
| 913 | |
| 914 | /* |
| 915 | * Parse a string literal and store a pointer it at *dst. String literals |
| 916 | * terminate at the end of the line. |
| 917 | */ |
| 918 | static int parse_sliteral(char **c, char **dst) |
| 919 | { |
| 920 | struct token t; |
| 921 | char *s = *c; |
| 922 | |
| 923 | get_token(c, &t, L_SLITERAL); |
| 924 | |
| 925 | if (t.type != T_STRING) { |
| 926 | printf("Expected string literal: %.*s\n", (int)(*c - s), s); |
| 927 | return -EINVAL; |
| 928 | } |
| 929 | |
| 930 | *dst = t.val; |
| 931 | |
| 932 | return 1; |
| 933 | } |
| 934 | |
| 935 | /* |
| 936 | * Parse a base 10 (unsigned) integer and store it at *dst. |
| 937 | */ |
| 938 | static int parse_integer(char **c, int *dst) |
| 939 | { |
| 940 | struct token t; |
| 941 | char *s = *c; |
| 942 | unsigned long temp; |
| 943 | |
| 944 | get_token(c, &t, L_SLITERAL); |
| 945 | |
| 946 | if (t.type != T_STRING) { |
| 947 | printf("Expected string: %.*s\n", (int)(*c - s), s); |
| 948 | return -EINVAL; |
| 949 | } |
| 950 | |
| 951 | if (strict_strtoul(t.val, 10, &temp) < 0) { |
| 952 | printf("Expected unsigned integer: %s\n", t.val); |
| 953 | return -EINVAL; |
| 954 | } |
| 955 | |
| 956 | *dst = (int)temp; |
| 957 | |
| 958 | free(t.val); |
| 959 | |
| 960 | return 1; |
| 961 | } |
| 962 | |
| 963 | static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level); |
| 964 | |
| 965 | /* |
| 966 | * Parse an include statement, and retrieve and parse the file it mentions. |
| 967 | * |
| 968 | * base should point to a location where it's safe to store the file, and |
| 969 | * nest_level should indicate how many nested includes have occurred. For this |
| 970 | * include, nest_level has already been incremented and doesn't need to be |
| 971 | * incremented here. |
| 972 | */ |
| 973 | static int handle_include(char **c, char *base, |
| 974 | struct pxe_menu *cfg, int nest_level) |
| 975 | { |
| 976 | char *include_path; |
| 977 | char *s = *c; |
| 978 | int err; |
| 979 | |
| 980 | err = parse_sliteral(c, &include_path); |
| 981 | |
| 982 | if (err < 0) { |
| 983 | printf("Expected include path: %.*s\n", |
| 984 | (int)(*c - s), s); |
| 985 | return err; |
| 986 | } |
| 987 | |
| 988 | err = get_pxe_file(include_path, base); |
| 989 | |
| 990 | if (err < 0) { |
| 991 | printf("Couldn't retrieve %s\n", include_path); |
| 992 | return err; |
| 993 | } |
| 994 | |
| 995 | return parse_pxefile_top(base, cfg, nest_level); |
| 996 | } |
| 997 | |
| 998 | /* |
| 999 | * Parse lines that begin with 'menu'. |
| 1000 | * |
| 1001 | * b and nest are provided to handle the 'menu include' case. |
| 1002 | * |
| 1003 | * b should be the address where the file currently being parsed is stored. |
| 1004 | * |
| 1005 | * nest_level should be 1 when parsing the top level pxe file, 2 when parsing |
| 1006 | * a file it includes, 3 when parsing a file included by that file, and so on. |
| 1007 | */ |
| 1008 | static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level) |
| 1009 | { |
| 1010 | struct token t; |
| 1011 | char *s = *c; |
Heiko Schocher | 43d4a5e | 2011-12-12 20:37:17 +0000 | [diff] [blame] | 1012 | int err = 0; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 1013 | |
| 1014 | get_token(c, &t, L_KEYWORD); |
| 1015 | |
| 1016 | switch (t.type) { |
| 1017 | case T_TITLE: |
| 1018 | err = parse_sliteral(c, &cfg->title); |
| 1019 | |
| 1020 | break; |
| 1021 | |
| 1022 | case T_INCLUDE: |
| 1023 | err = handle_include(c, b + strlen(b) + 1, cfg, |
| 1024 | nest_level + 1); |
| 1025 | break; |
| 1026 | |
| 1027 | default: |
| 1028 | printf("Ignoring malformed menu command: %.*s\n", |
| 1029 | (int)(*c - s), s); |
| 1030 | } |
| 1031 | |
| 1032 | if (err < 0) |
| 1033 | return err; |
| 1034 | |
| 1035 | eol_or_eof(c); |
| 1036 | |
| 1037 | return 1; |
| 1038 | } |
| 1039 | |
| 1040 | /* |
| 1041 | * Handles parsing a 'menu line' when we're parsing a label. |
| 1042 | */ |
| 1043 | static int parse_label_menu(char **c, struct pxe_menu *cfg, |
| 1044 | struct pxe_label *label) |
| 1045 | { |
| 1046 | struct token t; |
| 1047 | char *s; |
| 1048 | |
| 1049 | s = *c; |
| 1050 | |
| 1051 | get_token(c, &t, L_KEYWORD); |
| 1052 | |
| 1053 | switch (t.type) { |
| 1054 | case T_DEFAULT: |
| 1055 | if (cfg->default_label) |
| 1056 | free(cfg->default_label); |
| 1057 | |
| 1058 | cfg->default_label = strdup(label->name); |
| 1059 | |
| 1060 | if (!cfg->default_label) |
| 1061 | return -ENOMEM; |
| 1062 | |
| 1063 | break; |
Rob Herring | 7815c4e | 2012-03-28 05:51:34 +0000 | [diff] [blame] | 1064 | case T_LABEL: |
| 1065 | parse_sliteral(c, &label->menu); |
| 1066 | break; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 1067 | default: |
| 1068 | printf("Ignoring malformed menu command: %.*s\n", |
| 1069 | (int)(*c - s), s); |
| 1070 | } |
| 1071 | |
| 1072 | eol_or_eof(c); |
| 1073 | |
| 1074 | return 0; |
| 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | * Parses a label and adds it to the list of labels for a menu. |
| 1079 | * |
| 1080 | * A label ends when we either get to the end of a file, or |
| 1081 | * get some input we otherwise don't have a handler defined |
| 1082 | * for. |
| 1083 | * |
| 1084 | */ |
| 1085 | static int parse_label(char **c, struct pxe_menu *cfg) |
| 1086 | { |
| 1087 | struct token t; |
Rob Herring | 34bd23e | 2012-03-28 05:51:37 +0000 | [diff] [blame] | 1088 | int len; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 1089 | char *s = *c; |
| 1090 | struct pxe_label *label; |
| 1091 | int err; |
| 1092 | |
| 1093 | label = label_create(); |
| 1094 | if (!label) |
| 1095 | return -ENOMEM; |
| 1096 | |
| 1097 | err = parse_sliteral(c, &label->name); |
| 1098 | if (err < 0) { |
| 1099 | printf("Expected label name: %.*s\n", (int)(*c - s), s); |
| 1100 | label_destroy(label); |
| 1101 | return -EINVAL; |
| 1102 | } |
| 1103 | |
| 1104 | list_add_tail(&label->list, &cfg->labels); |
| 1105 | |
| 1106 | while (1) { |
| 1107 | s = *c; |
| 1108 | get_token(c, &t, L_KEYWORD); |
| 1109 | |
| 1110 | err = 0; |
| 1111 | switch (t.type) { |
| 1112 | case T_MENU: |
| 1113 | err = parse_label_menu(c, cfg, label); |
| 1114 | break; |
| 1115 | |
| 1116 | case T_KERNEL: |
Rob Herring | beb9f6c | 2012-03-28 05:51:35 +0000 | [diff] [blame] | 1117 | case T_LINUX: |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 1118 | err = parse_sliteral(c, &label->kernel); |
| 1119 | break; |
| 1120 | |
| 1121 | case T_APPEND: |
| 1122 | err = parse_sliteral(c, &label->append); |
Rob Herring | 34bd23e | 2012-03-28 05:51:37 +0000 | [diff] [blame] | 1123 | if (label->initrd) |
| 1124 | break; |
| 1125 | s = strstr(label->append, "initrd="); |
| 1126 | if (!s) |
| 1127 | break; |
| 1128 | s += 7; |
| 1129 | len = (int)(strchr(s, ' ') - s); |
| 1130 | label->initrd = malloc(len + 1); |
| 1131 | strncpy(label->initrd, s, len); |
| 1132 | label->initrd[len] = '\0'; |
| 1133 | |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 1134 | break; |
| 1135 | |
| 1136 | case T_INITRD: |
Rob Herring | 34bd23e | 2012-03-28 05:51:37 +0000 | [diff] [blame] | 1137 | if (!label->initrd) |
| 1138 | err = parse_sliteral(c, &label->initrd); |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 1139 | break; |
| 1140 | |
| 1141 | case T_LOCALBOOT: |
| 1142 | err = parse_integer(c, &label->localboot); |
| 1143 | break; |
| 1144 | |
| 1145 | case T_EOL: |
| 1146 | break; |
| 1147 | |
| 1148 | default: |
| 1149 | /* |
| 1150 | * put the token back! we don't want it - it's the end |
| 1151 | * of a label and whatever token this is, it's |
| 1152 | * something for the menu level context to handle. |
| 1153 | */ |
| 1154 | *c = s; |
| 1155 | return 1; |
| 1156 | } |
| 1157 | |
| 1158 | if (err < 0) |
| 1159 | return err; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | /* |
| 1164 | * This 16 comes from the limit pxelinux imposes on nested includes. |
| 1165 | * |
| 1166 | * There is no reason at all we couldn't do more, but some limit helps prevent |
| 1167 | * infinite (until crash occurs) recursion if a file tries to include itself. |
| 1168 | */ |
| 1169 | #define MAX_NEST_LEVEL 16 |
| 1170 | |
| 1171 | /* |
| 1172 | * Entry point for parsing a menu file. nest_level indicates how many times |
| 1173 | * we've nested in includes. It will be 1 for the top level menu file. |
| 1174 | * |
| 1175 | * Returns 1 on success, < 0 on error. |
| 1176 | */ |
| 1177 | static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level) |
| 1178 | { |
| 1179 | struct token t; |
| 1180 | char *s, *b, *label_name; |
| 1181 | int err; |
| 1182 | |
| 1183 | b = p; |
| 1184 | |
| 1185 | if (nest_level > MAX_NEST_LEVEL) { |
| 1186 | printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL); |
| 1187 | return -EMLINK; |
| 1188 | } |
| 1189 | |
| 1190 | while (1) { |
| 1191 | s = p; |
| 1192 | |
| 1193 | get_token(&p, &t, L_KEYWORD); |
| 1194 | |
| 1195 | err = 0; |
| 1196 | switch (t.type) { |
| 1197 | case T_MENU: |
| 1198 | err = parse_menu(&p, cfg, b, nest_level); |
| 1199 | break; |
| 1200 | |
| 1201 | case T_TIMEOUT: |
| 1202 | err = parse_integer(&p, &cfg->timeout); |
| 1203 | break; |
| 1204 | |
| 1205 | case T_LABEL: |
| 1206 | err = parse_label(&p, cfg); |
| 1207 | break; |
| 1208 | |
| 1209 | case T_DEFAULT: |
| 1210 | err = parse_sliteral(&p, &label_name); |
| 1211 | |
| 1212 | if (label_name) { |
| 1213 | if (cfg->default_label) |
| 1214 | free(cfg->default_label); |
| 1215 | |
| 1216 | cfg->default_label = label_name; |
| 1217 | } |
| 1218 | |
| 1219 | break; |
| 1220 | |
Rob Herring | 1e08522 | 2012-05-25 10:43:16 +0000 | [diff] [blame] | 1221 | case T_INCLUDE: |
| 1222 | err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg, |
| 1223 | nest_level + 1); |
| 1224 | break; |
| 1225 | |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 1226 | case T_PROMPT: |
| 1227 | err = parse_integer(&p, &cfg->prompt); |
| 1228 | break; |
| 1229 | |
| 1230 | case T_EOL: |
| 1231 | break; |
| 1232 | |
| 1233 | case T_EOF: |
| 1234 | return 1; |
| 1235 | |
| 1236 | default: |
| 1237 | printf("Ignoring unknown command: %.*s\n", |
| 1238 | (int)(p - s), s); |
| 1239 | eol_or_eof(&p); |
| 1240 | } |
| 1241 | |
| 1242 | if (err < 0) |
| 1243 | return err; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | /* |
| 1248 | * Free the memory used by a pxe_menu and its labels. |
| 1249 | */ |
| 1250 | static void destroy_pxe_menu(struct pxe_menu *cfg) |
| 1251 | { |
| 1252 | struct list_head *pos, *n; |
| 1253 | struct pxe_label *label; |
| 1254 | |
| 1255 | if (cfg->title) |
| 1256 | free(cfg->title); |
| 1257 | |
| 1258 | if (cfg->default_label) |
| 1259 | free(cfg->default_label); |
| 1260 | |
| 1261 | list_for_each_safe(pos, n, &cfg->labels) { |
| 1262 | label = list_entry(pos, struct pxe_label, list); |
| 1263 | |
| 1264 | label_destroy(label); |
| 1265 | } |
| 1266 | |
| 1267 | free(cfg); |
| 1268 | } |
| 1269 | |
| 1270 | /* |
| 1271 | * Entry point for parsing a pxe file. This is only used for the top level |
| 1272 | * file. |
| 1273 | * |
| 1274 | * Returns NULL if there is an error, otherwise, returns a pointer to a |
| 1275 | * pxe_menu struct populated with the results of parsing the pxe file (and any |
| 1276 | * files it includes). The resulting pxe_menu struct can be free()'d by using |
| 1277 | * the destroy_pxe_menu() function. |
| 1278 | */ |
| 1279 | static struct pxe_menu *parse_pxefile(char *menucfg) |
| 1280 | { |
| 1281 | struct pxe_menu *cfg; |
| 1282 | |
| 1283 | cfg = malloc(sizeof(struct pxe_menu)); |
| 1284 | |
| 1285 | if (!cfg) |
| 1286 | return NULL; |
| 1287 | |
| 1288 | memset(cfg, 0, sizeof(struct pxe_menu)); |
| 1289 | |
| 1290 | INIT_LIST_HEAD(&cfg->labels); |
| 1291 | |
| 1292 | if (parse_pxefile_top(menucfg, cfg, 1) < 0) { |
| 1293 | destroy_pxe_menu(cfg); |
| 1294 | return NULL; |
| 1295 | } |
| 1296 | |
| 1297 | return cfg; |
| 1298 | } |
| 1299 | |
| 1300 | /* |
| 1301 | * Converts a pxe_menu struct into a menu struct for use with U-boot's generic |
| 1302 | * menu code. |
| 1303 | */ |
| 1304 | static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg) |
| 1305 | { |
| 1306 | struct pxe_label *label; |
| 1307 | struct list_head *pos; |
| 1308 | struct menu *m; |
| 1309 | int err; |
| 1310 | |
| 1311 | /* |
| 1312 | * Create a menu and add items for all the labels. |
| 1313 | */ |
| 1314 | m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print); |
| 1315 | |
| 1316 | if (!m) |
| 1317 | return NULL; |
| 1318 | |
| 1319 | list_for_each(pos, &cfg->labels) { |
| 1320 | label = list_entry(pos, struct pxe_label, list); |
| 1321 | |
| 1322 | if (menu_item_add(m, label->name, label) != 1) { |
| 1323 | menu_destroy(m); |
| 1324 | return NULL; |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | /* |
| 1329 | * After we've created items for each label in the menu, set the |
| 1330 | * menu's default label if one was specified. |
| 1331 | */ |
| 1332 | if (cfg->default_label) { |
| 1333 | err = menu_default_set(m, cfg->default_label); |
| 1334 | if (err != 1) { |
| 1335 | if (err != -ENOENT) { |
| 1336 | menu_destroy(m); |
| 1337 | return NULL; |
| 1338 | } |
| 1339 | |
| 1340 | printf("Missing default: %s\n", cfg->default_label); |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | return m; |
| 1345 | } |
| 1346 | |
| 1347 | /* |
| 1348 | * Try to boot any labels we have yet to attempt to boot. |
| 1349 | */ |
| 1350 | static void boot_unattempted_labels(struct pxe_menu *cfg) |
| 1351 | { |
| 1352 | struct list_head *pos; |
| 1353 | struct pxe_label *label; |
| 1354 | |
| 1355 | list_for_each(pos, &cfg->labels) { |
| 1356 | label = list_entry(pos, struct pxe_label, list); |
| 1357 | |
| 1358 | if (!label->attempted) |
| 1359 | label_boot(label); |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | /* |
| 1364 | * Boot the system as prescribed by a pxe_menu. |
| 1365 | * |
| 1366 | * Use the menu system to either get the user's choice or the default, based |
| 1367 | * on config or user input. If there is no default or user's choice, |
| 1368 | * attempted to boot labels in the order they were given in pxe files. |
| 1369 | * If the default or user's choice fails to boot, attempt to boot other |
| 1370 | * labels in the order they were given in pxe files. |
| 1371 | * |
| 1372 | * If this function returns, there weren't any labels that successfully |
| 1373 | * booted, or the user interrupted the menu selection via ctrl+c. |
| 1374 | */ |
| 1375 | static void handle_pxe_menu(struct pxe_menu *cfg) |
| 1376 | { |
| 1377 | void *choice; |
| 1378 | struct menu *m; |
| 1379 | int err; |
| 1380 | |
| 1381 | m = pxe_menu_to_menu(cfg); |
| 1382 | if (!m) |
| 1383 | return; |
| 1384 | |
| 1385 | err = menu_get_choice(m, &choice); |
| 1386 | |
| 1387 | menu_destroy(m); |
| 1388 | |
Jason Hobbs | 6f40f27 | 2011-11-07 03:07:15 +0000 | [diff] [blame] | 1389 | /* |
| 1390 | * err == 1 means we got a choice back from menu_get_choice. |
| 1391 | * |
| 1392 | * err == -ENOENT if the menu was setup to select the default but no |
| 1393 | * default was set. in that case, we should continue trying to boot |
| 1394 | * labels that haven't been attempted yet. |
| 1395 | * |
| 1396 | * otherwise, the user interrupted or there was some other error and |
| 1397 | * we give up. |
| 1398 | */ |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 1399 | |
Jason Hobbs | 6f40f27 | 2011-11-07 03:07:15 +0000 | [diff] [blame] | 1400 | if (err == 1) |
| 1401 | label_boot(choice); |
| 1402 | else if (err != -ENOENT) |
| 1403 | return; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 1404 | |
| 1405 | boot_unattempted_labels(cfg); |
| 1406 | } |
| 1407 | |
| 1408 | /* |
| 1409 | * Boots a system using a pxe file |
| 1410 | * |
| 1411 | * Returns 0 on success, 1 on error. |
| 1412 | */ |
| 1413 | static int |
| 1414 | do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 1415 | { |
| 1416 | unsigned long pxefile_addr_r; |
| 1417 | struct pxe_menu *cfg; |
| 1418 | char *pxefile_addr_str; |
| 1419 | |
Rob Herring | 669df7e | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 1420 | do_getfile = do_get_tftp; |
| 1421 | |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 1422 | if (argc == 1) { |
| 1423 | pxefile_addr_str = from_env("pxefile_addr_r"); |
| 1424 | if (!pxefile_addr_str) |
| 1425 | return 1; |
| 1426 | |
| 1427 | } else if (argc == 2) { |
| 1428 | pxefile_addr_str = argv[1]; |
| 1429 | } else { |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1430 | return CMD_RET_USAGE; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) { |
| 1434 | printf("Invalid pxefile address: %s\n", pxefile_addr_str); |
| 1435 | return 1; |
| 1436 | } |
| 1437 | |
| 1438 | cfg = parse_pxefile((char *)(pxefile_addr_r)); |
| 1439 | |
| 1440 | if (cfg == NULL) { |
| 1441 | printf("Error parsing config file\n"); |
| 1442 | return 1; |
| 1443 | } |
| 1444 | |
| 1445 | handle_pxe_menu(cfg); |
| 1446 | |
| 1447 | destroy_pxe_menu(cfg); |
| 1448 | |
| 1449 | return 0; |
| 1450 | } |
| 1451 | |
| 1452 | static cmd_tbl_t cmd_pxe_sub[] = { |
| 1453 | U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""), |
| 1454 | U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "") |
| 1455 | }; |
| 1456 | |
| 1457 | int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 1458 | { |
| 1459 | cmd_tbl_t *cp; |
| 1460 | |
| 1461 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1462 | return CMD_RET_USAGE; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 1463 | |
| 1464 | /* drop initial "pxe" arg */ |
| 1465 | argc--; |
| 1466 | argv++; |
| 1467 | |
| 1468 | cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub)); |
| 1469 | |
| 1470 | if (cp) |
| 1471 | return cp->cmd(cmdtp, flag, argc, argv); |
| 1472 | |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1473 | return CMD_RET_USAGE; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | U_BOOT_CMD( |
| 1477 | pxe, 3, 1, do_pxe, |
| 1478 | "commands to get and boot from pxe files", |
| 1479 | "get - try to retrieve a pxe file using tftp\npxe " |
| 1480 | "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n" |
| 1481 | ); |
Rob Herring | 669df7e | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 1482 | |
| 1483 | /* |
| 1484 | * Boots a system using a local disk syslinux/extlinux file |
| 1485 | * |
| 1486 | * Returns 0 on success, 1 on error. |
| 1487 | */ |
| 1488 | int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 1489 | { |
| 1490 | unsigned long pxefile_addr_r; |
| 1491 | struct pxe_menu *cfg; |
| 1492 | char *pxefile_addr_str; |
| 1493 | char *filename; |
| 1494 | int prompt = 0; |
| 1495 | |
| 1496 | if (strstr(argv[1], "-p")) { |
| 1497 | prompt = 1; |
| 1498 | argc--; |
| 1499 | argv++; |
| 1500 | } |
| 1501 | |
| 1502 | if (argc < 4) |
| 1503 | return cmd_usage(cmdtp); |
| 1504 | |
| 1505 | if (argc < 5) { |
| 1506 | pxefile_addr_str = from_env("pxefile_addr_r"); |
| 1507 | if (!pxefile_addr_str) |
| 1508 | return 1; |
| 1509 | } else { |
| 1510 | pxefile_addr_str = argv[4]; |
| 1511 | } |
| 1512 | |
| 1513 | if (argc < 6) |
| 1514 | filename = getenv("bootfile"); |
| 1515 | else { |
| 1516 | filename = argv[5]; |
| 1517 | setenv("bootfile", filename); |
| 1518 | } |
| 1519 | |
| 1520 | if (strstr(argv[3], "ext2")) |
| 1521 | do_getfile = do_get_ext2; |
| 1522 | else if (strstr(argv[3], "fat")) |
| 1523 | do_getfile = do_get_fat; |
| 1524 | else { |
| 1525 | printf("Invalid filesystem: %s\n", argv[3]); |
| 1526 | return 1; |
| 1527 | } |
| 1528 | fs_argv[1] = argv[1]; |
| 1529 | fs_argv[2] = argv[2]; |
| 1530 | |
| 1531 | if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) { |
| 1532 | printf("Invalid pxefile address: %s\n", pxefile_addr_str); |
| 1533 | return 1; |
| 1534 | } |
| 1535 | |
| 1536 | if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) { |
| 1537 | printf("Error reading config file\n"); |
| 1538 | return 1; |
| 1539 | } |
| 1540 | |
| 1541 | cfg = parse_pxefile((char *)(pxefile_addr_r)); |
| 1542 | |
| 1543 | if (cfg == NULL) { |
| 1544 | printf("Error parsing config file\n"); |
| 1545 | return 1; |
| 1546 | } |
| 1547 | |
| 1548 | if (prompt) |
| 1549 | cfg->prompt = 1; |
| 1550 | |
| 1551 | handle_pxe_menu(cfg); |
| 1552 | |
| 1553 | destroy_pxe_menu(cfg); |
| 1554 | |
| 1555 | return 0; |
| 1556 | } |
| 1557 | |
| 1558 | U_BOOT_CMD( |
| 1559 | sysboot, 7, 1, do_sysboot, |
| 1560 | "command to get and boot from syslinux files", |
| 1561 | "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n" |
| 1562 | " - load and parse syslinux menu file 'filename' from ext2 or fat\n" |
| 1563 | " filesystem on 'dev' on 'interface' to address 'addr'" |
| 1564 | ); |