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