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