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