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 | 1b3f75f | 2019-12-28 10:44:44 -0700 | [diff] [blame] | 9 | #include <fs.h> |
Simon Glass | 6e51ee1 | 2019-12-28 10:44:43 -0700 | [diff] [blame] | 10 | #include <net.h> |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 11 | |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 12 | #include "pxe_utils.h" |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 13 | |
Patrice Chotard | 17e8804 | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 14 | #ifdef CONFIG_CMD_NET |
Rob Herring | e26e8d6 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 15 | const char *pxe_default_paths[] = { |
Joe Hershberger | 921a71d | 2013-06-24 17:21:04 -0500 | [diff] [blame] | 16 | #ifdef CONFIG_SYS_SOC |
Marek BehĂșn | 016ed13 | 2019-05-02 15:29:12 +0200 | [diff] [blame] | 17 | #ifdef CONFIG_SYS_BOARD |
| 18 | "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC "-" CONFIG_SYS_BOARD, |
| 19 | #endif |
Rob Herring | e26e8d6 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 20 | "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC, |
Joe Hershberger | 921a71d | 2013-06-24 17:21:04 -0500 | [diff] [blame] | 21 | #endif |
Rob Herring | e26e8d6 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 22 | "default-" CONFIG_SYS_ARCH, |
| 23 | "default", |
| 24 | NULL |
| 25 | }; |
| 26 | |
Simon Glass | 44a20ef | 2021-10-14 12:47:57 -0600 | [diff] [blame] | 27 | static int do_get_tftp(struct pxe_context *ctx, const char *file_path, |
Simon Glass | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 28 | char *file_addr, ulong *sizep) |
Rob Herring | eee675f | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 29 | { |
| 30 | char *tftp_argv[] = {"tftp", NULL, NULL, NULL}; |
Simon Glass | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 31 | int ret; |
Rob Herring | eee675f | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 32 | |
| 33 | tftp_argv[1] = file_addr; |
Rob Herring | 824901c | 2012-12-02 21:00:21 -0600 | [diff] [blame] | 34 | tftp_argv[2] = (void *)file_path; |
Rob Herring | eee675f | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 35 | |
Simon Glass | 44a20ef | 2021-10-14 12:47:57 -0600 | [diff] [blame] | 36 | if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv)) |
Rob Herring | eee675f | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 37 | return -ENOENT; |
Simon Glass | a9401b9 | 2021-10-14 12:48:08 -0600 | [diff] [blame] | 38 | ret = pxe_get_file_size(sizep); |
| 39 | if (ret) |
| 40 | return log_msg_ret("tftp", ret); |
| 41 | ctx->pxe_file_size = *sizep; |
Rob Herring | eee675f | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 42 | |
| 43 | return 1; |
| 44 | } |
Stephen Warren | 857291b | 2014-02-05 20:49:20 -0700 | [diff] [blame] | 45 | |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 46 | /* |
| 47 | * Looks for a pxe file with a name based on the pxeuuid environment variable. |
| 48 | * |
| 49 | * Returns 1 on success or < 0 on error. |
| 50 | */ |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 51 | static int pxe_uuid_path(struct pxe_context *ctx, unsigned long pxefile_addr_r) |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 52 | { |
| 53 | char *uuid_str; |
| 54 | |
| 55 | uuid_str = from_env("pxeuuid"); |
| 56 | |
| 57 | if (!uuid_str) |
| 58 | return -ENOENT; |
| 59 | |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 60 | return get_pxelinux_path(ctx, uuid_str, pxefile_addr_r); |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Looks for a pxe file with a name based on the 'ethaddr' environment |
| 65 | * variable. |
| 66 | * |
| 67 | * Returns 1 on success or < 0 on error. |
| 68 | */ |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 69 | static int pxe_mac_path(struct pxe_context *ctx, unsigned long pxefile_addr_r) |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 70 | { |
| 71 | char mac_str[21]; |
| 72 | int err; |
| 73 | |
| 74 | err = format_mac_pxe(mac_str, sizeof(mac_str)); |
| 75 | |
| 76 | if (err < 0) |
| 77 | return err; |
| 78 | |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 79 | return get_pxelinux_path(ctx, mac_str, pxefile_addr_r); |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Looks for pxe files with names based on our IP address. See pxelinux |
| 84 | * documentation for details on what these file names look like. We match |
| 85 | * that exactly. |
| 86 | * |
| 87 | * Returns 1 on success or < 0 on error. |
| 88 | */ |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 89 | static int pxe_ipaddr_paths(struct pxe_context *ctx, unsigned long pxefile_addr_r) |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 90 | { |
| 91 | char ip_addr[9]; |
| 92 | int mask_pos, err; |
| 93 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 94 | sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr)); |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 95 | |
| 96 | for (mask_pos = 7; mask_pos >= 0; mask_pos--) { |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 97 | err = get_pxelinux_path(ctx, ip_addr, pxefile_addr_r); |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 98 | |
| 99 | if (err > 0) |
| 100 | return err; |
| 101 | |
| 102 | ip_addr[mask_pos] = '\0'; |
| 103 | } |
| 104 | |
| 105 | return -ENOENT; |
| 106 | } |
Simon Glass | 1dacc6f | 2021-10-14 12:48:11 -0600 | [diff] [blame] | 107 | |
| 108 | int pxe_get(ulong pxefile_addr_r, char **bootdirp, ulong *sizep) |
| 109 | { |
| 110 | struct cmd_tbl cmdtp[] = {}; /* dummy */ |
| 111 | struct pxe_context ctx; |
| 112 | int i; |
| 113 | |
| 114 | if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false, |
| 115 | env_get("bootfile"))) |
| 116 | return -ENOMEM; |
| 117 | /* |
| 118 | * Keep trying paths until we successfully get a file we're looking |
| 119 | * for. |
| 120 | */ |
| 121 | if (pxe_uuid_path(&ctx, pxefile_addr_r) > 0 || |
| 122 | pxe_mac_path(&ctx, pxefile_addr_r) > 0 || |
| 123 | pxe_ipaddr_paths(&ctx, pxefile_addr_r) > 0) |
| 124 | goto done; |
| 125 | |
| 126 | i = 0; |
| 127 | while (pxe_default_paths[i]) { |
| 128 | if (get_pxelinux_path(&ctx, pxe_default_paths[i], |
| 129 | pxefile_addr_r) > 0) |
| 130 | goto done; |
| 131 | i++; |
| 132 | } |
| 133 | |
| 134 | pxe_destroy_ctx(&ctx); |
| 135 | |
| 136 | return -ENOENT; |
| 137 | done: |
| 138 | *bootdirp = env_get("bootfile"); |
| 139 | |
| 140 | /* |
| 141 | * The PXE file size is returned but not the name. It is probably not |
| 142 | * that useful. |
| 143 | */ |
| 144 | *sizep = ctx.pxe_file_size; |
| 145 | pxe_destroy_ctx(&ctx); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 150 | /* |
| 151 | * Entry point for the 'pxe get' command. |
| 152 | * This Follows pxelinux's rules to download a config file from a tftp server. |
| 153 | * The file is stored at the location given by the pxefile_addr_r environment |
| 154 | * variable, which must be set. |
| 155 | * |
| 156 | * UUID comes from pxeuuid env variable, if defined |
| 157 | * MAC addr comes from ethaddr env variable, if defined |
| 158 | * IP |
| 159 | * |
| 160 | * see http://syslinux.zytor.com/wiki/index.php/PXELINUX |
| 161 | * |
| 162 | * Returns 0 on success or 1 on error. |
| 163 | */ |
| 164 | static int |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 165 | do_pxe_get(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 166 | { |
| 167 | char *pxefile_addr_str; |
Simon Glass | 1dacc6f | 2021-10-14 12:48:11 -0600 | [diff] [blame] | 168 | ulong pxefile_addr_r; |
| 169 | char *fname; |
| 170 | ulong size; |
| 171 | int ret; |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 172 | |
| 173 | if (argc != 1) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 174 | return CMD_RET_USAGE; |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 175 | |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 176 | pxefile_addr_str = from_env("pxefile_addr_r"); |
| 177 | |
| 178 | if (!pxefile_addr_str) |
| 179 | return 1; |
| 180 | |
Simon Glass | 1dacc6f | 2021-10-14 12:48:11 -0600 | [diff] [blame] | 181 | ret = strict_strtoul(pxefile_addr_str, 16, |
Patrice Chotard | 7f87165 | 2019-11-25 09:07:41 +0100 | [diff] [blame] | 182 | (unsigned long *)&pxefile_addr_r); |
Simon Glass | 1dacc6f | 2021-10-14 12:48:11 -0600 | [diff] [blame] | 183 | if (ret < 0) |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 184 | return 1; |
| 185 | |
Simon Glass | 1dacc6f | 2021-10-14 12:48:11 -0600 | [diff] [blame] | 186 | ret = pxe_get(pxefile_addr_r, &fname, &size); |
| 187 | switch (ret) { |
| 188 | case 0: |
| 189 | printf("Config file '%s' found\n", fname); |
| 190 | break; |
| 191 | case -ENOMEM: |
Simon Glass | e719fe0 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 192 | printf("Out of memory\n"); |
| 193 | return CMD_RET_FAILURE; |
Simon Glass | 1dacc6f | 2021-10-14 12:48:11 -0600 | [diff] [blame] | 194 | default: |
| 195 | printf("Config file not found\n"); |
| 196 | return CMD_RET_FAILURE; |
Rob Herring | e26e8d6 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 197 | } |
| 198 | |
Simon Glass | 1dacc6f | 2021-10-14 12:48:11 -0600 | [diff] [blame] | 199 | return 0; |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 203 | * Boots a system using a pxe file |
| 204 | * |
| 205 | * Returns 0 on success, 1 on error. |
| 206 | */ |
| 207 | static int |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 208 | do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 209 | { |
| 210 | unsigned long pxefile_addr_r; |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 211 | char *pxefile_addr_str; |
Simon Glass | b0d08db | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 212 | struct pxe_context ctx; |
Simon Glass | 791bbfe | 2021-10-14 12:48:03 -0600 | [diff] [blame] | 213 | int ret; |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 214 | |
| 215 | if (argc == 1) { |
| 216 | pxefile_addr_str = from_env("pxefile_addr_r"); |
| 217 | if (!pxefile_addr_str) |
| 218 | return 1; |
| 219 | |
| 220 | } else if (argc == 2) { |
| 221 | pxefile_addr_str = argv[1]; |
| 222 | } else { |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 223 | return CMD_RET_USAGE; |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) { |
| 227 | printf("Invalid pxefile address: %s\n", pxefile_addr_str); |
| 228 | return 1; |
| 229 | } |
| 230 | |
Simon Glass | e719fe0 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 231 | if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false, |
| 232 | env_get("bootfile"))) { |
| 233 | printf("Out of memory\n"); |
| 234 | return CMD_RET_FAILURE; |
| 235 | } |
Simon Glass | 791bbfe | 2021-10-14 12:48:03 -0600 | [diff] [blame] | 236 | ret = pxe_process(&ctx, pxefile_addr_r, false); |
Simon Glass | e719fe0 | 2021-10-14 12:48:04 -0600 | [diff] [blame] | 237 | pxe_destroy_ctx(&ctx); |
Simon Glass | 791bbfe | 2021-10-14 12:48:03 -0600 | [diff] [blame] | 238 | if (ret) |
| 239 | return CMD_RET_FAILURE; |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 240 | |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 241 | copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name)); |
Stephen Warren | c24cbc2 | 2014-07-22 18:06:46 -0600 | [diff] [blame] | 242 | |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 246 | static struct cmd_tbl cmd_pxe_sub[] = { |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 247 | U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""), |
| 248 | U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "") |
| 249 | }; |
| 250 | |
T Karthik Reddy | 37923d9 | 2020-09-16 01:13:56 -0600 | [diff] [blame] | 251 | static void __maybe_unused pxe_reloc(void) |
| 252 | { |
| 253 | static int relocated_pxe; |
| 254 | |
| 255 | if (!relocated_pxe) { |
| 256 | fixup_cmdtable(cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub)); |
| 257 | relocated_pxe = 1; |
| 258 | } |
| 259 | } |
| 260 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 261 | static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 262 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 263 | struct cmd_tbl *cp; |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 264 | |
T Karthik Reddy | 37923d9 | 2020-09-16 01:13:56 -0600 | [diff] [blame] | 265 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 266 | pxe_reloc(); |
| 267 | #endif |
| 268 | |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 269 | if (argc < 2) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 270 | return CMD_RET_USAGE; |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 271 | |
| 272 | /* drop initial "pxe" arg */ |
| 273 | argc--; |
| 274 | argv++; |
| 275 | |
| 276 | cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub)); |
| 277 | |
| 278 | if (cp) |
| 279 | return cp->cmd(cmdtp, flag, argc, argv); |
| 280 | |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 281 | return CMD_RET_USAGE; |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 282 | } |
| 283 | |
Patrice Chotard | 7f87165 | 2019-11-25 09:07:41 +0100 | [diff] [blame] | 284 | U_BOOT_CMD(pxe, 3, 1, do_pxe, |
| 285 | "commands to get and boot from pxe files", |
| 286 | "get - try to retrieve a pxe file using tftp\n" |
| 287 | "pxe boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n" |
Jason Hobbs | 0e3a593 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 288 | ); |
Stephen Warren | 857291b | 2014-02-05 20:49:20 -0700 | [diff] [blame] | 289 | #endif |