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