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