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