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