blob: 9404f4451870ec5fcf0faecd92ec4f1e5e8c9b37 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jason Hobbs0e3a5932011-08-31 10:37:30 -05002/*
3 * Copyright 2010-2011 Calxeda, Inc.
Bryan Wua1d715b2014-07-31 17:39:59 -07004 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
Jason Hobbs0e3a5932011-08-31 10:37:30 -05005 */
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006
Jason Hobbs0e3a5932011-08-31 10:37:30 -05007#include <common.h>
8#include <command.h>
Simon Glass1b3f75f2019-12-28 10:44:44 -07009#include <fs.h>
Simon Glass6e51ee12019-12-28 10:44:43 -070010#include <net.h>
Sean Edmondba802862023-04-11 10:48:47 -070011#include <net6.h>
12#include <malloc.h>
Jason Hobbs0e3a5932011-08-31 10:37:30 -050013
Patrice Chotard17e88042019-11-25 09:07:37 +010014#include "pxe_utils.h"
Jason Hobbs0e3a5932011-08-31 10:37:30 -050015
Patrice Chotard17e88042019-11-25 09:07:37 +010016#ifdef CONFIG_CMD_NET
Rob Herringe26e8d62012-12-02 21:00:28 -060017const char *pxe_default_paths[] = {
Joe Hershberger921a71d2013-06-24 17:21:04 -050018#ifdef CONFIG_SYS_SOC
Marek BehĂșn016ed132019-05-02 15:29:12 +020019#ifdef CONFIG_SYS_BOARD
20 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC "-" CONFIG_SYS_BOARD,
21#endif
Rob Herringe26e8d62012-12-02 21:00:28 -060022 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
Joe Hershberger921a71d2013-06-24 17:21:04 -050023#endif
Rob Herringe26e8d62012-12-02 21:00:28 -060024 "default-" CONFIG_SYS_ARCH,
25 "default",
26 NULL
27};
28
Simon Glass44a20ef2021-10-14 12:47:57 -060029static int do_get_tftp(struct pxe_context *ctx, const char *file_path,
Simon Glassa9401b92021-10-14 12:48:08 -060030 char *file_addr, ulong *sizep)
Rob Herringeee675f2012-05-25 10:47:39 +000031{
32 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
Simon Glassa9401b92021-10-14 12:48:08 -060033 int ret;
Sean Edmondba802862023-04-11 10:48:47 -070034 int num_args;
Rob Herringeee675f2012-05-25 10:47:39 +000035
36 tftp_argv[1] = file_addr;
Rob Herring824901c2012-12-02 21:00:21 -060037 tftp_argv[2] = (void *)file_path;
Sean Edmondba802862023-04-11 10:48:47 -070038 if (ctx->use_ipv6) {
39 tftp_argv[3] = USE_IP6_CMD_PARAM;
40 num_args = 4;
41 } else {
42 num_args = 3;
43 }
Rob Herringeee675f2012-05-25 10:47:39 +000044
Sean Edmondba802862023-04-11 10:48:47 -070045 if (do_tftpb(ctx->cmdtp, 0, num_args, tftp_argv))
Rob Herringeee675f2012-05-25 10:47:39 +000046 return -ENOENT;
Sean Edmondba802862023-04-11 10:48:47 -070047
Simon Glassa9401b92021-10-14 12:48:08 -060048 ret = pxe_get_file_size(sizep);
49 if (ret)
50 return log_msg_ret("tftp", ret);
51 ctx->pxe_file_size = *sizep;
Rob Herringeee675f2012-05-25 10:47:39 +000052
53 return 1;
54}
Stephen Warren857291b2014-02-05 20:49:20 -070055
Jason Hobbs0e3a5932011-08-31 10:37:30 -050056/*
Sean Edmondba802862023-04-11 10:48:47 -070057 * Looks for a pxe file with specified config file name,
58 * which is received from DHCPv4 option 209 or
59 * DHCPv6 option 60.
60 *
61 * Returns 1 on success or < 0 on error.
62 */
63static int pxe_dhcp_option_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)
64{
65 int ret = get_pxe_file(ctx, pxelinux_configfile, pxefile_addr_r);
66
67 free(pxelinux_configfile);
Sean Edmond0760efe2024-05-08 19:39:01 -070068 /* set to NULL to avoid double-free if DHCP is tried again */
69 pxelinux_configfile = NULL;
Sean Edmondba802862023-04-11 10:48:47 -070070
71 return ret;
72}
73
74/*
Jason Hobbs0e3a5932011-08-31 10:37:30 -050075 * Looks for a pxe file with a name based on the pxeuuid environment variable.
76 *
77 * Returns 1 on success or < 0 on error.
78 */
Simon Glassb0d08db2021-10-14 12:47:56 -060079static int pxe_uuid_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -050080{
81 char *uuid_str;
82
83 uuid_str = from_env("pxeuuid");
84
85 if (!uuid_str)
86 return -ENOENT;
87
Simon Glassb0d08db2021-10-14 12:47:56 -060088 return get_pxelinux_path(ctx, uuid_str, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -050089}
90
91/*
92 * Looks for a pxe file with a name based on the 'ethaddr' environment
93 * variable.
94 *
95 * Returns 1 on success or < 0 on error.
96 */
Simon Glassb0d08db2021-10-14 12:47:56 -060097static int pxe_mac_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -050098{
99 char mac_str[21];
100 int err;
101
102 err = format_mac_pxe(mac_str, sizeof(mac_str));
103
104 if (err < 0)
105 return err;
106
Simon Glassb0d08db2021-10-14 12:47:56 -0600107 return get_pxelinux_path(ctx, mac_str, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500108}
109
110/*
111 * Looks for pxe files with names based on our IP address. See pxelinux
112 * documentation for details on what these file names look like. We match
113 * that exactly.
114 *
115 * Returns 1 on success or < 0 on error.
116 */
Simon Glassb0d08db2021-10-14 12:47:56 -0600117static int pxe_ipaddr_paths(struct pxe_context *ctx, unsigned long pxefile_addr_r)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500118{
119 char ip_addr[9];
120 int mask_pos, err;
121
Joe Hershberger5874dec2015-04-08 01:41:01 -0500122 sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500123
124 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
Simon Glassb0d08db2021-10-14 12:47:56 -0600125 err = get_pxelinux_path(ctx, ip_addr, pxefile_addr_r);
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500126
127 if (err > 0)
128 return err;
129
130 ip_addr[mask_pos] = '\0';
131 }
132
133 return -ENOENT;
134}
Simon Glass1dacc6f2021-10-14 12:48:11 -0600135
Sean Edmondba802862023-04-11 10:48:47 -0700136int pxe_get(ulong pxefile_addr_r, char **bootdirp, ulong *sizep, bool use_ipv6)
Simon Glass1dacc6f2021-10-14 12:48:11 -0600137{
138 struct cmd_tbl cmdtp[] = {}; /* dummy */
139 struct pxe_context ctx;
140 int i;
141
142 if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false,
Sean Edmondba802862023-04-11 10:48:47 -0700143 env_get("bootfile"), use_ipv6))
Simon Glass1dacc6f2021-10-14 12:48:11 -0600144 return -ENOMEM;
Sean Edmondba802862023-04-11 10:48:47 -0700145
Sean Edmond57867112023-07-25 16:20:30 -0700146 if (IS_ENABLED(CONFIG_BOOTP_PXE_DHCP_OPTION) &&
147 pxelinux_configfile && !use_ipv6) {
148 if (pxe_dhcp_option_path(&ctx, pxefile_addr_r) > 0)
149 goto done;
150
151 goto error_exit;
152 }
153
Sean Edmondba802862023-04-11 10:48:47 -0700154 if (IS_ENABLED(CONFIG_DHCP6_PXE_DHCP_OPTION) &&
155 pxelinux_configfile && use_ipv6) {
156 if (pxe_dhcp_option_path(&ctx, pxefile_addr_r) > 0)
157 goto done;
158
159 goto error_exit;
160 }
161
Simon Glass1dacc6f2021-10-14 12:48:11 -0600162 /*
163 * Keep trying paths until we successfully get a file we're looking
164 * for.
165 */
166 if (pxe_uuid_path(&ctx, pxefile_addr_r) > 0 ||
167 pxe_mac_path(&ctx, pxefile_addr_r) > 0 ||
168 pxe_ipaddr_paths(&ctx, pxefile_addr_r) > 0)
169 goto done;
170
171 i = 0;
172 while (pxe_default_paths[i]) {
173 if (get_pxelinux_path(&ctx, pxe_default_paths[i],
174 pxefile_addr_r) > 0)
175 goto done;
176 i++;
177 }
178
Sean Edmondba802862023-04-11 10:48:47 -0700179error_exit:
Simon Glass1dacc6f2021-10-14 12:48:11 -0600180 pxe_destroy_ctx(&ctx);
181
182 return -ENOENT;
183done:
184 *bootdirp = env_get("bootfile");
185
186 /*
187 * The PXE file size is returned but not the name. It is probably not
188 * that useful.
189 */
190 *sizep = ctx.pxe_file_size;
191 pxe_destroy_ctx(&ctx);
192
193 return 0;
194}
195
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500196/*
197 * Entry point for the 'pxe get' command.
198 * This Follows pxelinux's rules to download a config file from a tftp server.
199 * The file is stored at the location given by the pxefile_addr_r environment
200 * variable, which must be set.
201 *
202 * UUID comes from pxeuuid env variable, if defined
203 * MAC addr comes from ethaddr env variable, if defined
204 * IP
205 *
206 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
207 *
208 * Returns 0 on success or 1 on error.
209 */
210static int
Simon Glassed38aef2020-05-10 11:40:03 -0600211do_pxe_get(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500212{
213 char *pxefile_addr_str;
Simon Glass1dacc6f2021-10-14 12:48:11 -0600214 ulong pxefile_addr_r;
215 char *fname;
216 ulong size;
217 int ret;
Sean Edmondba802862023-04-11 10:48:47 -0700218 bool use_ipv6 = false;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500219
Sean Edmondba802862023-04-11 10:48:47 -0700220 if (IS_ENABLED(CONFIG_IPV6)) {
221 if (!strcmp(argv[argc - 1], USE_IP6_CMD_PARAM))
222 use_ipv6 = true;
223
224 if (!(argc == 1 || (argc == 2 && use_ipv6)))
225 return CMD_RET_USAGE;
226 } else {
227 if (argc != 1)
228 return CMD_RET_USAGE;
229 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500230
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500231 pxefile_addr_str = from_env("pxefile_addr_r");
232
233 if (!pxefile_addr_str)
234 return 1;
235
Simon Glass1dacc6f2021-10-14 12:48:11 -0600236 ret = strict_strtoul(pxefile_addr_str, 16,
Patrice Chotard7f871652019-11-25 09:07:41 +0100237 (unsigned long *)&pxefile_addr_r);
Simon Glass1dacc6f2021-10-14 12:48:11 -0600238 if (ret < 0)
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500239 return 1;
240
Sean Edmondba802862023-04-11 10:48:47 -0700241 ret = pxe_get(pxefile_addr_r, &fname, &size, use_ipv6);
Simon Glass1dacc6f2021-10-14 12:48:11 -0600242 switch (ret) {
243 case 0:
244 printf("Config file '%s' found\n", fname);
245 break;
246 case -ENOMEM:
Simon Glasse719fe02021-10-14 12:48:04 -0600247 printf("Out of memory\n");
248 return CMD_RET_FAILURE;
Simon Glass1dacc6f2021-10-14 12:48:11 -0600249 default:
250 printf("Config file not found\n");
251 return CMD_RET_FAILURE;
Rob Herringe26e8d62012-12-02 21:00:28 -0600252 }
253
Simon Glass1dacc6f2021-10-14 12:48:11 -0600254 return 0;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500255}
256
257/*
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500258 * Boots a system using a pxe file
259 *
260 * Returns 0 on success, 1 on error.
261 */
262static int
Simon Glassed38aef2020-05-10 11:40:03 -0600263do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500264{
265 unsigned long pxefile_addr_r;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500266 char *pxefile_addr_str;
Simon Glassb0d08db2021-10-14 12:47:56 -0600267 struct pxe_context ctx;
Simon Glass791bbfe2021-10-14 12:48:03 -0600268 int ret;
Sean Edmondba802862023-04-11 10:48:47 -0700269 bool use_ipv6 = false;
270
271 if (IS_ENABLED(CONFIG_IPV6)) {
272 if (!strcmp(argv[argc - 1], USE_IP6_CMD_PARAM))
273 use_ipv6 = true;
274 }
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500275
Sean Edmondba802862023-04-11 10:48:47 -0700276 if (argc == 1 || (argc == 2 && use_ipv6)) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500277 pxefile_addr_str = from_env("pxefile_addr_r");
278 if (!pxefile_addr_str)
279 return 1;
280
Sean Edmondba802862023-04-11 10:48:47 -0700281 } else if (argc == 2 || (argc == 3 && use_ipv6)) {
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500282 pxefile_addr_str = argv[1];
283 } else {
Simon Glassa06dfc72011-12-10 08:44:01 +0000284 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500285 }
286
287 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
288 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
289 return 1;
290 }
291
Simon Glasse719fe02021-10-14 12:48:04 -0600292 if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false,
Sean Edmondba802862023-04-11 10:48:47 -0700293 env_get("bootfile"), use_ipv6)) {
Simon Glasse719fe02021-10-14 12:48:04 -0600294 printf("Out of memory\n");
295 return CMD_RET_FAILURE;
296 }
Simon Glass791bbfe2021-10-14 12:48:03 -0600297 ret = pxe_process(&ctx, pxefile_addr_r, false);
Simon Glasse719fe02021-10-14 12:48:04 -0600298 pxe_destroy_ctx(&ctx);
Simon Glass791bbfe2021-10-14 12:48:03 -0600299 if (ret)
300 return CMD_RET_FAILURE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500301
Joe Hershberger290c8992015-04-08 01:41:02 -0500302 copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
Stephen Warrenc24cbc22014-07-22 18:06:46 -0600303
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500304 return 0;
305}
306
Simon Glassed38aef2020-05-10 11:40:03 -0600307static struct cmd_tbl cmd_pxe_sub[] = {
Sean Edmondba802862023-04-11 10:48:47 -0700308 U_BOOT_CMD_MKENT(get, 2, 1, do_pxe_get, "", ""),
309 U_BOOT_CMD_MKENT(boot, 3, 1, do_pxe_boot, "", "")
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500310};
311
Simon Glassed38aef2020-05-10 11:40:03 -0600312static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500313{
Simon Glassed38aef2020-05-10 11:40:03 -0600314 struct cmd_tbl *cp;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500315
316 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000317 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500318
319 /* drop initial "pxe" arg */
320 argc--;
321 argv++;
322
323 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
324
325 if (cp)
326 return cp->cmd(cmdtp, flag, argc, argv);
327
Simon Glassa06dfc72011-12-10 08:44:01 +0000328 return CMD_RET_USAGE;
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500329}
330
Sean Edmondba802862023-04-11 10:48:47 -0700331U_BOOT_CMD(pxe, 4, 1, do_pxe,
Bin Mengc44cfba2023-07-31 16:33:23 +0800332 "get and boot from pxe files",
Sean Edmondba802862023-04-11 10:48:47 -0700333 "get [" USE_IP6_CMD_PARAM "] - try to retrieve a pxe file using tftp\n"
334 "pxe boot [pxefile_addr_r] [-ipv6] - boot from the pxe file at pxefile_addr_r\n"
Jason Hobbs0e3a5932011-08-31 10:37:30 -0500335);
Sean Edmondba802862023-04-11 10:48:47 -0700336
337#endif /* CONFIG_CMD_NET */