1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2010-2011 Calxeda, Inc.
4  * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
5  */
6 
7 #include <command.h>
8 #include <env.h>
9 #include <fs.h>
10 #include <net.h>
11 #include <net6.h>
12 #include <malloc.h>
13 #include <vsprintf.h>
14 
15 #include "pxe_utils.h"
16 
17 const char *pxe_default_paths[] = {
18 #ifdef CONFIG_SYS_SOC
19 #ifdef CONFIG_SYS_BOARD
20 	"default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC "-" CONFIG_SYS_BOARD,
21 #endif
22 	"default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
23 #endif
24 	"default-" CONFIG_SYS_ARCH,
25 	"default",
26 	NULL
27 };
28 
do_get_tftp(struct pxe_context * ctx,const char * file_path,char * file_addr,enum bootflow_img_t type,ulong * sizep)29 static int do_get_tftp(struct pxe_context *ctx, const char *file_path,
30 		       char *file_addr, enum bootflow_img_t type, ulong *sizep)
31 {
32 	char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
33 	int ret;
34 	int num_args;
35 
36 	tftp_argv[1] = file_addr;
37 	tftp_argv[2] = (void *)file_path;
38 	if (ctx->use_ipv6) {
39 		tftp_argv[3] = USE_IP6_CMD_PARAM;
40 		num_args = 4;
41 	} else {
42 		num_args = 3;
43 	}
44 
45 	if (do_tftpb(ctx->cmdtp, 0, num_args, tftp_argv))
46 		return -ENOENT;
47 
48 	ret = pxe_get_file_size(sizep);
49 	if (ret)
50 		return log_msg_ret("tftp", ret);
51 	ctx->pxe_file_size = *sizep;
52 
53 	return 1;
54 }
55 
56 /*
57  * 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  */
pxe_dhcp_option_path(struct pxe_context * ctx,unsigned long pxefile_addr_r)63 static 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 	/* set to NULL to avoid double-free if DHCP is tried again */
69 	pxelinux_configfile = NULL;
70 
71 	return ret;
72 }
73 
74 /*
75  * 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  */
pxe_uuid_path(struct pxe_context * ctx,unsigned long pxefile_addr_r)79 static int pxe_uuid_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)
80 {
81 	char *uuid_str;
82 
83 	uuid_str = from_env("pxeuuid");
84 
85 	if (!uuid_str)
86 		return -ENOENT;
87 
88 	return get_pxelinux_path(ctx, uuid_str, pxefile_addr_r);
89 }
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  */
pxe_mac_path(struct pxe_context * ctx,unsigned long pxefile_addr_r)97 static int pxe_mac_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)
98 {
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 
107 	return get_pxelinux_path(ctx, mac_str, pxefile_addr_r);
108 }
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  */
pxe_ipaddr_paths(struct pxe_context * ctx,unsigned long pxefile_addr_r)117 static int pxe_ipaddr_paths(struct pxe_context *ctx, unsigned long pxefile_addr_r)
118 {
119 	char ip_addr[9];
120 	int mask_pos, err;
121 
122 	sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
123 
124 	for (mask_pos = 7; mask_pos >= 0;  mask_pos--) {
125 		err = get_pxelinux_path(ctx, ip_addr, pxefile_addr_r);
126 
127 		if (err > 0)
128 			return err;
129 
130 		ip_addr[mask_pos] = '\0';
131 	}
132 
133 	return -ENOENT;
134 }
135 
pxe_get(ulong pxefile_addr_r,char ** bootdirp,ulong * sizep,bool use_ipv6)136 int pxe_get(ulong pxefile_addr_r, char **bootdirp, ulong *sizep, bool use_ipv6)
137 {
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,
143 			  env_get("bootfile"), use_ipv6, false))
144 		return -ENOMEM;
145 
146 	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 
154 	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 
162 	/*
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 
179 error_exit:
180 	pxe_destroy_ctx(&ctx);
181 
182 	return -ENOENT;
183 done:
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 
196 /*
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  */
210 static int
do_pxe_get(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])211 do_pxe_get(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
212 {
213 	char *pxefile_addr_str;
214 	ulong pxefile_addr_r;
215 	char *fname;
216 	ulong size;
217 	int ret;
218 	bool use_ipv6 = false;
219 
220 	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 	}
230 
231 	pxefile_addr_str = from_env("pxefile_addr_r");
232 
233 	if (!pxefile_addr_str)
234 		return 1;
235 
236 	ret = strict_strtoul(pxefile_addr_str, 16,
237 			     (unsigned long *)&pxefile_addr_r);
238 	if (ret < 0)
239 		return 1;
240 
241 	ret = pxe_get(pxefile_addr_r, &fname, &size, use_ipv6);
242 	switch (ret) {
243 	case 0:
244 		printf("Config file '%s' found\n", fname);
245 		break;
246 	case -ENOMEM:
247 		printf("Out of memory\n");
248 		return CMD_RET_FAILURE;
249 	default:
250 		printf("Config file not found\n");
251 		return CMD_RET_FAILURE;
252 	}
253 
254 	return 0;
255 }
256 
257 /*
258  * Boots a system using a pxe file
259  *
260  * Returns 0 on success, 1 on error.
261  */
262 static int
do_pxe_boot(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])263 do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
264 {
265 	unsigned long pxefile_addr_r;
266 	char *pxefile_addr_str;
267 	struct pxe_context ctx;
268 	int ret;
269 	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 	}
275 
276 	if (argc == 1 || (argc == 2 && use_ipv6)) {
277 		pxefile_addr_str = from_env("pxefile_addr_r");
278 		if (!pxefile_addr_str)
279 			return 1;
280 
281 	} else if (argc == 2 || (argc == 3 && use_ipv6)) {
282 		pxefile_addr_str = argv[1];
283 	} else {
284 		return CMD_RET_USAGE;
285 	}
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 
292 	if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false,
293 			  env_get("bootfile"), use_ipv6, false)) {
294 		printf("Out of memory\n");
295 		return CMD_RET_FAILURE;
296 	}
297 	ret = pxe_process(&ctx, pxefile_addr_r, false);
298 	pxe_destroy_ctx(&ctx);
299 	if (ret)
300 		return CMD_RET_FAILURE;
301 
302 	copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
303 
304 	return 0;
305 }
306 
307 static struct cmd_tbl cmd_pxe_sub[] = {
308 	U_BOOT_CMD_MKENT(get, 2, 1, do_pxe_get, "", ""),
309 	U_BOOT_CMD_MKENT(boot, 3, 1, do_pxe_boot, "", "")
310 };
311 
do_pxe(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])312 static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
313 {
314 	struct cmd_tbl *cp;
315 
316 	if (argc < 2)
317 		return CMD_RET_USAGE;
318 
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 
328 	return CMD_RET_USAGE;
329 }
330 
331 U_BOOT_CMD(pxe, 4, 1, do_pxe,
332 	   "get and boot from pxe files",
333 	   "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"
335 );
336