blob: ae02c28c07503763290b073702d6589e2f2fbeb1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jason Hobbs06283a62011-08-31 10:37:30 -05002/*
3 * Copyright 2010-2011 Calxeda, Inc.
Bryan Wu1fb7d0e2014-07-31 17:39:59 -07004 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
Jason Hobbs06283a62011-08-31 10:37:30 -05005 */
Wolfgang Denk1a459662013-07-08 09:37:19 +02006
Jason Hobbs06283a62011-08-31 10:37:30 -05007#include <command.h>
Simon Glass14449982019-12-28 10:44:44 -07008#include <fs.h>
Simon Glass77f4e472019-12-28 10:44:43 -07009#include <net.h>
Sean Edmond7d018892023-04-11 10:48:47 -070010#include <net6.h>
11#include <malloc.h>
Tom Rini03de3052024-05-20 13:35:03 -060012#include <vsprintf.h>
Jason Hobbs06283a62011-08-31 10:37:30 -050013
Patrice Chotard2373cba2019-11-25 09:07:37 +010014#include "pxe_utils.h"
Jason Hobbs06283a62011-08-31 10:37:30 -050015
Patrice Chotard2373cba2019-11-25 09:07:37 +010016#ifdef CONFIG_CMD_NET
Rob Herring39f98552012-12-02 21:00:28 -060017const char *pxe_default_paths[] = {
Joe Hershberger58d9ff92013-06-24 17:21:04 -050018#ifdef CONFIG_SYS_SOC
Marek BehĂșn2455efa2019-05-02 15:29:12 +020019#ifdef CONFIG_SYS_BOARD
20 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC "-" CONFIG_SYS_BOARD,
21#endif
Rob Herring39f98552012-12-02 21:00:28 -060022 "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
Joe Hershberger58d9ff92013-06-24 17:21:04 -050023#endif
Rob Herring39f98552012-12-02 21:00:28 -060024 "default-" CONFIG_SYS_ARCH,
25 "default",
26 NULL
27};
28
Simon Glassb1ead6b2021-10-14 12:47:57 -060029static int do_get_tftp(struct pxe_context *ctx, const char *file_path,
Simon Glass4d79e882021-10-14 12:48:08 -060030 char *file_addr, ulong *sizep)
Rob Herring669df7e2012-05-25 10:47:39 +000031{
32 char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
Simon Glass4d79e882021-10-14 12:48:08 -060033 int ret;
Sean Edmond7d018892023-04-11 10:48:47 -070034 int num_args;
Rob Herring669df7e2012-05-25 10:47:39 +000035
36 tftp_argv[1] = file_addr;
Rob Herring23b71942012-12-02 21:00:21 -060037 tftp_argv[2] = (void *)file_path;
Sean Edmond7d018892023-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 Herring669df7e2012-05-25 10:47:39 +000044
Sean Edmond7d018892023-04-11 10:48:47 -070045 if (do_tftpb(ctx->cmdtp, 0, num_args, tftp_argv))
Rob Herring669df7e2012-05-25 10:47:39 +000046 return -ENOENT;
Sean Edmond7d018892023-04-11 10:48:47 -070047
Simon Glass4d79e882021-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 Herring669df7e2012-05-25 10:47:39 +000052
53 return 1;
54}
Stephen Warrenb81fdb02014-02-05 20:49:20 -070055
Jason Hobbs06283a62011-08-31 10:37:30 -050056/*
Sean Edmond7d018892023-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);
68
69 return ret;
70}
71
72/*
Jason Hobbs06283a62011-08-31 10:37:30 -050073 * Looks for a pxe file with a name based on the pxeuuid environment variable.
74 *
75 * Returns 1 on success or < 0 on error.
76 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -060077static int pxe_uuid_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -050078{
79 char *uuid_str;
80
81 uuid_str = from_env("pxeuuid");
82
83 if (!uuid_str)
84 return -ENOENT;
85
Simon Glassfd3fa5c2021-10-14 12:47:56 -060086 return get_pxelinux_path(ctx, uuid_str, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -050087}
88
89/*
90 * Looks for a pxe file with a name based on the 'ethaddr' environment
91 * variable.
92 *
93 * Returns 1 on success or < 0 on error.
94 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -060095static int pxe_mac_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -050096{
97 char mac_str[21];
98 int err;
99
100 err = format_mac_pxe(mac_str, sizeof(mac_str));
101
102 if (err < 0)
103 return err;
104
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600105 return get_pxelinux_path(ctx, mac_str, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -0500106}
107
108/*
109 * Looks for pxe files with names based on our IP address. See pxelinux
110 * documentation for details on what these file names look like. We match
111 * that exactly.
112 *
113 * Returns 1 on success or < 0 on error.
114 */
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600115static int pxe_ipaddr_paths(struct pxe_context *ctx, unsigned long pxefile_addr_r)
Jason Hobbs06283a62011-08-31 10:37:30 -0500116{
117 char ip_addr[9];
118 int mask_pos, err;
119
Joe Hershberger049a95a2015-04-08 01:41:01 -0500120 sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
Jason Hobbs06283a62011-08-31 10:37:30 -0500121
122 for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600123 err = get_pxelinux_path(ctx, ip_addr, pxefile_addr_r);
Jason Hobbs06283a62011-08-31 10:37:30 -0500124
125 if (err > 0)
126 return err;
127
128 ip_addr[mask_pos] = '\0';
129 }
130
131 return -ENOENT;
132}
Simon Glassd50244e2021-10-14 12:48:11 -0600133
Sean Edmond7d018892023-04-11 10:48:47 -0700134int pxe_get(ulong pxefile_addr_r, char **bootdirp, ulong *sizep, bool use_ipv6)
Simon Glassd50244e2021-10-14 12:48:11 -0600135{
136 struct cmd_tbl cmdtp[] = {}; /* dummy */
137 struct pxe_context ctx;
138 int i;
139
140 if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false,
Sean Edmond7d018892023-04-11 10:48:47 -0700141 env_get("bootfile"), use_ipv6))
Simon Glassd50244e2021-10-14 12:48:11 -0600142 return -ENOMEM;
Sean Edmond7d018892023-04-11 10:48:47 -0700143
Sean Edmond91953952023-07-25 16:20:30 -0700144 if (IS_ENABLED(CONFIG_BOOTP_PXE_DHCP_OPTION) &&
145 pxelinux_configfile && !use_ipv6) {
146 if (pxe_dhcp_option_path(&ctx, pxefile_addr_r) > 0)
147 goto done;
148
149 goto error_exit;
150 }
151
Sean Edmond7d018892023-04-11 10:48:47 -0700152 if (IS_ENABLED(CONFIG_DHCP6_PXE_DHCP_OPTION) &&
153 pxelinux_configfile && use_ipv6) {
154 if (pxe_dhcp_option_path(&ctx, pxefile_addr_r) > 0)
155 goto done;
156
157 goto error_exit;
158 }
159
Simon Glassd50244e2021-10-14 12:48:11 -0600160 /*
161 * Keep trying paths until we successfully get a file we're looking
162 * for.
163 */
164 if (pxe_uuid_path(&ctx, pxefile_addr_r) > 0 ||
165 pxe_mac_path(&ctx, pxefile_addr_r) > 0 ||
166 pxe_ipaddr_paths(&ctx, pxefile_addr_r) > 0)
167 goto done;
168
169 i = 0;
170 while (pxe_default_paths[i]) {
171 if (get_pxelinux_path(&ctx, pxe_default_paths[i],
172 pxefile_addr_r) > 0)
173 goto done;
174 i++;
175 }
176
Sean Edmond7d018892023-04-11 10:48:47 -0700177error_exit:
Simon Glassd50244e2021-10-14 12:48:11 -0600178 pxe_destroy_ctx(&ctx);
179
180 return -ENOENT;
181done:
182 *bootdirp = env_get("bootfile");
183
184 /*
185 * The PXE file size is returned but not the name. It is probably not
186 * that useful.
187 */
188 *sizep = ctx.pxe_file_size;
189 pxe_destroy_ctx(&ctx);
190
191 return 0;
192}
193
Jason Hobbs06283a62011-08-31 10:37:30 -0500194/*
195 * Entry point for the 'pxe get' command.
196 * This Follows pxelinux's rules to download a config file from a tftp server.
197 * The file is stored at the location given by the pxefile_addr_r environment
198 * variable, which must be set.
199 *
200 * UUID comes from pxeuuid env variable, if defined
201 * MAC addr comes from ethaddr env variable, if defined
202 * IP
203 *
204 * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
205 *
206 * Returns 0 on success or 1 on error.
207 */
208static int
Simon Glass09140112020-05-10 11:40:03 -0600209do_pxe_get(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Jason Hobbs06283a62011-08-31 10:37:30 -0500210{
211 char *pxefile_addr_str;
Simon Glassd50244e2021-10-14 12:48:11 -0600212 ulong pxefile_addr_r;
213 char *fname;
214 ulong size;
215 int ret;
Sean Edmond7d018892023-04-11 10:48:47 -0700216 bool use_ipv6 = false;
Jason Hobbs06283a62011-08-31 10:37:30 -0500217
Sean Edmond7d018892023-04-11 10:48:47 -0700218 if (IS_ENABLED(CONFIG_IPV6)) {
219 if (!strcmp(argv[argc - 1], USE_IP6_CMD_PARAM))
220 use_ipv6 = true;
221
222 if (!(argc == 1 || (argc == 2 && use_ipv6)))
223 return CMD_RET_USAGE;
224 } else {
225 if (argc != 1)
226 return CMD_RET_USAGE;
227 }
Jason Hobbs06283a62011-08-31 10:37:30 -0500228
Jason Hobbs06283a62011-08-31 10:37:30 -0500229 pxefile_addr_str = from_env("pxefile_addr_r");
230
231 if (!pxefile_addr_str)
232 return 1;
233
Simon Glassd50244e2021-10-14 12:48:11 -0600234 ret = strict_strtoul(pxefile_addr_str, 16,
Patrice Chotard31839dc2019-11-25 09:07:41 +0100235 (unsigned long *)&pxefile_addr_r);
Simon Glassd50244e2021-10-14 12:48:11 -0600236 if (ret < 0)
Jason Hobbs06283a62011-08-31 10:37:30 -0500237 return 1;
238
Sean Edmond7d018892023-04-11 10:48:47 -0700239 ret = pxe_get(pxefile_addr_r, &fname, &size, use_ipv6);
Simon Glassd50244e2021-10-14 12:48:11 -0600240 switch (ret) {
241 case 0:
242 printf("Config file '%s' found\n", fname);
243 break;
244 case -ENOMEM:
Simon Glass12df8422021-10-14 12:48:04 -0600245 printf("Out of memory\n");
246 return CMD_RET_FAILURE;
Simon Glassd50244e2021-10-14 12:48:11 -0600247 default:
248 printf("Config file not found\n");
249 return CMD_RET_FAILURE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500250 }
251
Simon Glassd50244e2021-10-14 12:48:11 -0600252 return 0;
Jason Hobbs06283a62011-08-31 10:37:30 -0500253}
Jason Hobbs06283a62011-08-31 10:37:30 -0500254
255/*
256 * Boots a system using a pxe file
257 *
258 * Returns 0 on success, 1 on error.
259 */
260static int
Simon Glass09140112020-05-10 11:40:03 -0600261do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Jason Hobbs06283a62011-08-31 10:37:30 -0500262{
263 unsigned long pxefile_addr_r;
Jason Hobbs06283a62011-08-31 10:37:30 -0500264 char *pxefile_addr_str;
Simon Glassfd3fa5c2021-10-14 12:47:56 -0600265 struct pxe_context ctx;
Simon Glass9e62e7c2021-10-14 12:48:03 -0600266 int ret;
Sean Edmond7d018892023-04-11 10:48:47 -0700267 bool use_ipv6 = false;
Jason Hobbs06283a62011-08-31 10:37:30 -0500268
Sean Edmond7d018892023-04-11 10:48:47 -0700269 if (IS_ENABLED(CONFIG_IPV6)) {
270 if (!strcmp(argv[argc - 1], USE_IP6_CMD_PARAM))
271 use_ipv6 = true;
272 }
273
274 if (argc == 1 || (argc == 2 && use_ipv6)) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500275 pxefile_addr_str = from_env("pxefile_addr_r");
276 if (!pxefile_addr_str)
277 return 1;
278
Sean Edmond7d018892023-04-11 10:48:47 -0700279 } else if (argc == 2 || (argc == 3 && use_ipv6)) {
Jason Hobbs06283a62011-08-31 10:37:30 -0500280 pxefile_addr_str = argv[1];
281 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000282 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500283 }
284
285 if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
286 printf("Invalid pxefile address: %s\n", pxefile_addr_str);
287 return 1;
288 }
289
Simon Glass12df8422021-10-14 12:48:04 -0600290 if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false,
Sean Edmond7d018892023-04-11 10:48:47 -0700291 env_get("bootfile"), use_ipv6)) {
Simon Glass12df8422021-10-14 12:48:04 -0600292 printf("Out of memory\n");
293 return CMD_RET_FAILURE;
294 }
Simon Glass9e62e7c2021-10-14 12:48:03 -0600295 ret = pxe_process(&ctx, pxefile_addr_r, false);
Simon Glass12df8422021-10-14 12:48:04 -0600296 pxe_destroy_ctx(&ctx);
Simon Glass9e62e7c2021-10-14 12:48:03 -0600297 if (ret)
298 return CMD_RET_FAILURE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500299
Joe Hershberger14111572015-04-08 01:41:02 -0500300 copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
Stephen Warrended2e202014-07-22 18:06:46 -0600301
Jason Hobbs06283a62011-08-31 10:37:30 -0500302 return 0;
303}
304
Simon Glass09140112020-05-10 11:40:03 -0600305static struct cmd_tbl cmd_pxe_sub[] = {
Sean Edmond7d018892023-04-11 10:48:47 -0700306 U_BOOT_CMD_MKENT(get, 2, 1, do_pxe_get, "", ""),
307 U_BOOT_CMD_MKENT(boot, 3, 1, do_pxe_boot, "", "")
Jason Hobbs06283a62011-08-31 10:37:30 -0500308};
309
Simon Glass09140112020-05-10 11:40:03 -0600310static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Jason Hobbs06283a62011-08-31 10:37:30 -0500311{
Simon Glass09140112020-05-10 11:40:03 -0600312 struct cmd_tbl *cp;
Jason Hobbs06283a62011-08-31 10:37:30 -0500313
314 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000315 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500316
317 /* drop initial "pxe" arg */
318 argc--;
319 argv++;
320
321 cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
322
323 if (cp)
324 return cp->cmd(cmdtp, flag, argc, argv);
325
Simon Glass4c12eeb2011-12-10 08:44:01 +0000326 return CMD_RET_USAGE;
Jason Hobbs06283a62011-08-31 10:37:30 -0500327}
328
Sean Edmond7d018892023-04-11 10:48:47 -0700329U_BOOT_CMD(pxe, 4, 1, do_pxe,
Bin Mengeed99ce2023-07-31 16:33:23 +0800330 "get and boot from pxe files",
Sean Edmond7d018892023-04-11 10:48:47 -0700331 "get [" USE_IP6_CMD_PARAM "] - try to retrieve a pxe file using tftp\n"
332 "pxe boot [pxefile_addr_r] [-ipv6] - boot from the pxe file at pxefile_addr_r\n"
Jason Hobbs06283a62011-08-31 10:37:30 -0500333);
Sean Edmond7d018892023-04-11 10:48:47 -0700334
335#endif /* CONFIG_CMD_NET */