Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2010-2011 Calxeda, Inc. |
Bryan Wu | 1fb7d0e | 2014-07-31 17:39:59 -0700 | [diff] [blame] | 4 | * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved. |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 5 | */ |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 7 | #include <common.h> |
| 8 | #include <command.h> |
Simon Glass | 1444998 | 2019-12-28 10:44:44 -0700 | [diff] [blame] | 9 | #include <fs.h> |
Simon Glass | 77f4e47 | 2019-12-28 10:44:43 -0700 | [diff] [blame] | 10 | #include <net.h> |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 11 | |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 12 | #include "pxe_utils.h" |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 13 | |
Patrice Chotard | 2373cba | 2019-11-25 09:07:37 +0100 | [diff] [blame] | 14 | #ifdef CONFIG_CMD_NET |
Rob Herring | 39f9855 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 15 | const char *pxe_default_paths[] = { |
Joe Hershberger | 58d9ff9 | 2013-06-24 17:21:04 -0500 | [diff] [blame] | 16 | #ifdef CONFIG_SYS_SOC |
Marek BehĂșn | 2455efa | 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 | 39f9855 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 20 | "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC, |
Joe Hershberger | 58d9ff9 | 2013-06-24 17:21:04 -0500 | [diff] [blame] | 21 | #endif |
Rob Herring | 39f9855 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 22 | "default-" CONFIG_SYS_ARCH, |
| 23 | "default", |
| 24 | NULL |
| 25 | }; |
| 26 | |
Simon Glass | b1ead6b | 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 | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 28 | char *file_addr) |
Rob Herring | 669df7e | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 29 | { |
| 30 | char *tftp_argv[] = {"tftp", NULL, NULL, NULL}; |
| 31 | |
| 32 | tftp_argv[1] = file_addr; |
Rob Herring | 23b7194 | 2012-12-02 21:00:21 -0600 | [diff] [blame] | 33 | tftp_argv[2] = (void *)file_path; |
Rob Herring | 669df7e | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 34 | |
Simon Glass | b1ead6b | 2021-10-14 12:47:57 -0600 | [diff] [blame] | 35 | if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv)) |
Rob Herring | 669df7e | 2012-05-25 10:47:39 +0000 | [diff] [blame] | 36 | return -ENOENT; |
| 37 | |
| 38 | return 1; |
| 39 | } |
Stephen Warren | b81fdb0 | 2014-02-05 20:49:20 -0700 | [diff] [blame] | 40 | |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 41 | /* |
| 42 | * Looks for a pxe file with a name based on the pxeuuid environment variable. |
| 43 | * |
| 44 | * Returns 1 on success or < 0 on error. |
| 45 | */ |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 46 | static int pxe_uuid_path(struct pxe_context *ctx, unsigned long pxefile_addr_r) |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 47 | { |
| 48 | char *uuid_str; |
| 49 | |
| 50 | uuid_str = from_env("pxeuuid"); |
| 51 | |
| 52 | if (!uuid_str) |
| 53 | return -ENOENT; |
| 54 | |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 55 | return get_pxelinux_path(ctx, uuid_str, pxefile_addr_r); |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* |
| 59 | * Looks for a pxe file with a name based on the 'ethaddr' environment |
| 60 | * variable. |
| 61 | * |
| 62 | * Returns 1 on success or < 0 on error. |
| 63 | */ |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 64 | static int pxe_mac_path(struct pxe_context *ctx, unsigned long pxefile_addr_r) |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 65 | { |
| 66 | char mac_str[21]; |
| 67 | int err; |
| 68 | |
| 69 | err = format_mac_pxe(mac_str, sizeof(mac_str)); |
| 70 | |
| 71 | if (err < 0) |
| 72 | return err; |
| 73 | |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 74 | return get_pxelinux_path(ctx, mac_str, pxefile_addr_r); |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Looks for pxe files with names based on our IP address. See pxelinux |
| 79 | * documentation for details on what these file names look like. We match |
| 80 | * that exactly. |
| 81 | * |
| 82 | * Returns 1 on success or < 0 on error. |
| 83 | */ |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 84 | static int pxe_ipaddr_paths(struct pxe_context *ctx, unsigned long pxefile_addr_r) |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 85 | { |
| 86 | char ip_addr[9]; |
| 87 | int mask_pos, err; |
| 88 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 89 | sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr)); |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 90 | |
| 91 | for (mask_pos = 7; mask_pos >= 0; mask_pos--) { |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 92 | err = get_pxelinux_path(ctx, ip_addr, pxefile_addr_r); |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 93 | |
| 94 | if (err > 0) |
| 95 | return err; |
| 96 | |
| 97 | ip_addr[mask_pos] = '\0'; |
| 98 | } |
| 99 | |
| 100 | return -ENOENT; |
| 101 | } |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 102 | /* |
| 103 | * Entry point for the 'pxe get' command. |
| 104 | * This Follows pxelinux's rules to download a config file from a tftp server. |
| 105 | * The file is stored at the location given by the pxefile_addr_r environment |
| 106 | * variable, which must be set. |
| 107 | * |
| 108 | * UUID comes from pxeuuid env variable, if defined |
| 109 | * MAC addr comes from ethaddr env variable, if defined |
| 110 | * IP |
| 111 | * |
| 112 | * see http://syslinux.zytor.com/wiki/index.php/PXELINUX |
| 113 | * |
| 114 | * Returns 0 on success or 1 on error. |
| 115 | */ |
| 116 | static int |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 117 | do_pxe_get(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 118 | { |
| 119 | char *pxefile_addr_str; |
Jason Hobbs | 834c938 | 2012-03-05 08:12:28 +0000 | [diff] [blame] | 120 | unsigned long pxefile_addr_r; |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 121 | struct pxe_context ctx; |
Rob Herring | 39f9855 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 122 | int err, i = 0; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 123 | |
| 124 | if (argc != 1) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 125 | return CMD_RET_USAGE; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 126 | |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 127 | pxefile_addr_str = from_env("pxefile_addr_r"); |
| 128 | |
| 129 | if (!pxefile_addr_str) |
| 130 | return 1; |
| 131 | |
| 132 | err = strict_strtoul(pxefile_addr_str, 16, |
Patrice Chotard | 31839dc | 2019-11-25 09:07:41 +0100 | [diff] [blame] | 133 | (unsigned long *)&pxefile_addr_r); |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 134 | if (err < 0) |
| 135 | return 1; |
| 136 | |
Simon Glass | 12df842 | 2021-10-14 12:48:04 -0600 | [diff] [blame^] | 137 | if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false, |
| 138 | env_get("bootfile"))) { |
| 139 | printf("Out of memory\n"); |
| 140 | return CMD_RET_FAILURE; |
| 141 | } |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 142 | /* |
| 143 | * Keep trying paths until we successfully get a file we're looking |
| 144 | * for. |
| 145 | */ |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 146 | if (pxe_uuid_path(&ctx, pxefile_addr_r) > 0 || |
| 147 | pxe_mac_path(&ctx, pxefile_addr_r) > 0 || |
| 148 | pxe_ipaddr_paths(&ctx, pxefile_addr_r) > 0) { |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 149 | printf("Config file found\n"); |
Simon Glass | 12df842 | 2021-10-14 12:48:04 -0600 | [diff] [blame^] | 150 | pxe_destroy_ctx(&ctx); |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
Rob Herring | 39f9855 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 155 | while (pxe_default_paths[i]) { |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 156 | if (get_pxelinux_path(&ctx, pxe_default_paths[i], |
Sjoerd Simons | 4a0bd10 | 2015-04-13 22:54:25 +0200 | [diff] [blame] | 157 | pxefile_addr_r) > 0) { |
Rob Herring | 39f9855 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 158 | printf("Config file found\n"); |
Simon Glass | 12df842 | 2021-10-14 12:48:04 -0600 | [diff] [blame^] | 159 | pxe_destroy_ctx(&ctx); |
Rob Herring | 39f9855 | 2012-12-02 21:00:28 -0600 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | i++; |
| 163 | } |
| 164 | |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 165 | printf("Config file not found\n"); |
Simon Glass | 12df842 | 2021-10-14 12:48:04 -0600 | [diff] [blame^] | 166 | pxe_destroy_ctx(&ctx); |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 167 | |
| 168 | return 1; |
| 169 | } |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * Boots a system using a pxe file |
| 173 | * |
| 174 | * Returns 0 on success, 1 on error. |
| 175 | */ |
| 176 | static int |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 177 | do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 178 | { |
| 179 | unsigned long pxefile_addr_r; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 180 | char *pxefile_addr_str; |
Simon Glass | fd3fa5c | 2021-10-14 12:47:56 -0600 | [diff] [blame] | 181 | struct pxe_context ctx; |
Simon Glass | 9e62e7c | 2021-10-14 12:48:03 -0600 | [diff] [blame] | 182 | int ret; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 183 | |
| 184 | if (argc == 1) { |
| 185 | pxefile_addr_str = from_env("pxefile_addr_r"); |
| 186 | if (!pxefile_addr_str) |
| 187 | return 1; |
| 188 | |
| 189 | } else if (argc == 2) { |
| 190 | pxefile_addr_str = argv[1]; |
| 191 | } else { |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 192 | return CMD_RET_USAGE; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) { |
| 196 | printf("Invalid pxefile address: %s\n", pxefile_addr_str); |
| 197 | return 1; |
| 198 | } |
| 199 | |
Simon Glass | 12df842 | 2021-10-14 12:48:04 -0600 | [diff] [blame^] | 200 | if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false, |
| 201 | env_get("bootfile"))) { |
| 202 | printf("Out of memory\n"); |
| 203 | return CMD_RET_FAILURE; |
| 204 | } |
Simon Glass | 9e62e7c | 2021-10-14 12:48:03 -0600 | [diff] [blame] | 205 | ret = pxe_process(&ctx, pxefile_addr_r, false); |
Simon Glass | 12df842 | 2021-10-14 12:48:04 -0600 | [diff] [blame^] | 206 | pxe_destroy_ctx(&ctx); |
Simon Glass | 9e62e7c | 2021-10-14 12:48:03 -0600 | [diff] [blame] | 207 | if (ret) |
| 208 | return CMD_RET_FAILURE; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 209 | |
Joe Hershberger | 1411157 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 210 | copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name)); |
Stephen Warren | ded2e20 | 2014-07-22 18:06:46 -0600 | [diff] [blame] | 211 | |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 212 | return 0; |
| 213 | } |
| 214 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 215 | static struct cmd_tbl cmd_pxe_sub[] = { |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 216 | U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""), |
| 217 | U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "") |
| 218 | }; |
| 219 | |
T Karthik Reddy | 504d179 | 2020-09-16 01:13:56 -0600 | [diff] [blame] | 220 | static void __maybe_unused pxe_reloc(void) |
| 221 | { |
| 222 | static int relocated_pxe; |
| 223 | |
| 224 | if (!relocated_pxe) { |
| 225 | fixup_cmdtable(cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub)); |
| 226 | relocated_pxe = 1; |
| 227 | } |
| 228 | } |
| 229 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 230 | static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 231 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 232 | struct cmd_tbl *cp; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 233 | |
T Karthik Reddy | 504d179 | 2020-09-16 01:13:56 -0600 | [diff] [blame] | 234 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 235 | pxe_reloc(); |
| 236 | #endif |
| 237 | |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 238 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 239 | return CMD_RET_USAGE; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 240 | |
| 241 | /* drop initial "pxe" arg */ |
| 242 | argc--; |
| 243 | argv++; |
| 244 | |
| 245 | cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub)); |
| 246 | |
| 247 | if (cp) |
| 248 | return cp->cmd(cmdtp, flag, argc, argv); |
| 249 | |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 250 | return CMD_RET_USAGE; |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 251 | } |
| 252 | |
Patrice Chotard | 31839dc | 2019-11-25 09:07:41 +0100 | [diff] [blame] | 253 | U_BOOT_CMD(pxe, 3, 1, do_pxe, |
| 254 | "commands to get and boot from pxe files", |
| 255 | "get - try to retrieve a pxe file using tftp\n" |
| 256 | "pxe boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n" |
Jason Hobbs | 06283a6 | 2011-08-31 10:37:30 -0500 | [diff] [blame] | 257 | ); |
Stephen Warren | b81fdb0 | 2014-02-05 20:49:20 -0700 | [diff] [blame] | 258 | #endif |