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