]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
06283a64 JH |
2 | /* |
3 | * Copyright 2010-2011 Calxeda, Inc. | |
1fb7d0e6 | 4 | * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved. |
06283a64 | 5 | */ |
1a459660 | 6 | |
06283a64 JH |
7 | #include <common.h> |
8 | #include <command.h> | |
14449982 | 9 | #include <fs.h> |
77f4e477 | 10 | #include <net.h> |
7d018892 SE |
11 | #include <net6.h> |
12 | #include <malloc.h> | |
06283a64 | 13 | |
2373cba3 | 14 | #include "pxe_utils.h" |
06283a64 | 15 | |
2373cba3 | 16 | #ifdef CONFIG_CMD_NET |
39f98553 | 17 | const char *pxe_default_paths[] = { |
58d9ff93 | 18 | #ifdef CONFIG_SYS_SOC |
2455efa2 MB |
19 | #ifdef CONFIG_SYS_BOARD |
20 | "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC "-" CONFIG_SYS_BOARD, | |
21 | #endif | |
39f98553 | 22 | "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC, |
58d9ff93 | 23 | #endif |
39f98553 RH |
24 | "default-" CONFIG_SYS_ARCH, |
25 | "default", | |
26 | NULL | |
27 | }; | |
28 | ||
b1ead6b9 | 29 | static int do_get_tftp(struct pxe_context *ctx, const char *file_path, |
4d79e884 | 30 | char *file_addr, ulong *sizep) |
669df7e4 RH |
31 | { |
32 | char *tftp_argv[] = {"tftp", NULL, NULL, NULL}; | |
4d79e884 | 33 | int ret; |
7d018892 | 34 | int num_args; |
669df7e4 RH |
35 | |
36 | tftp_argv[1] = file_addr; | |
23b7194e | 37 | tftp_argv[2] = (void *)file_path; |
7d018892 SE |
38 | if (ctx->use_ipv6) { |
39 | tftp_argv[3] = USE_IP6_CMD_PARAM; | |
40 | num_args = 4; | |
41 | } else { | |
42 | num_args = 3; | |
43 | } | |
669df7e4 | 44 | |
7d018892 | 45 | if (do_tftpb(ctx->cmdtp, 0, num_args, tftp_argv)) |
669df7e4 | 46 | return -ENOENT; |
7d018892 | 47 | |
4d79e884 SG |
48 | ret = pxe_get_file_size(sizep); |
49 | if (ret) | |
50 | return log_msg_ret("tftp", ret); | |
51 | ctx->pxe_file_size = *sizep; | |
669df7e4 RH |
52 | |
53 | return 1; | |
54 | } | |
b81fdb04 | 55 | |
7d018892 SE |
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 | */ | |
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 | ||
69 | return ret; | |
70 | } | |
71 | ||
06283a64 JH |
72 | /* |
73 | * 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 | */ | |
fd3fa5c3 | 77 | static int pxe_uuid_path(struct pxe_context *ctx, unsigned long pxefile_addr_r) |
06283a64 JH |
78 | { |
79 | char *uuid_str; | |
80 | ||
81 | uuid_str = from_env("pxeuuid"); | |
82 | ||
83 | if (!uuid_str) | |
84 | return -ENOENT; | |
85 | ||
fd3fa5c3 | 86 | return get_pxelinux_path(ctx, uuid_str, pxefile_addr_r); |
06283a64 JH |
87 | } |
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 | */ | |
fd3fa5c3 | 95 | static int pxe_mac_path(struct pxe_context *ctx, unsigned long pxefile_addr_r) |
06283a64 JH |
96 | { |
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 | ||
fd3fa5c3 | 105 | return get_pxelinux_path(ctx, mac_str, pxefile_addr_r); |
06283a64 JH |
106 | } |
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 | */ | |
fd3fa5c3 | 115 | static int pxe_ipaddr_paths(struct pxe_context *ctx, unsigned long pxefile_addr_r) |
06283a64 JH |
116 | { |
117 | char ip_addr[9]; | |
118 | int mask_pos, err; | |
119 | ||
049a95a7 | 120 | sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr)); |
06283a64 JH |
121 | |
122 | for (mask_pos = 7; mask_pos >= 0; mask_pos--) { | |
fd3fa5c3 | 123 | err = get_pxelinux_path(ctx, ip_addr, pxefile_addr_r); |
06283a64 JH |
124 | |
125 | if (err > 0) | |
126 | return err; | |
127 | ||
128 | ip_addr[mask_pos] = '\0'; | |
129 | } | |
130 | ||
131 | return -ENOENT; | |
132 | } | |
d50244e9 | 133 | |
7d018892 | 134 | int pxe_get(ulong pxefile_addr_r, char **bootdirp, ulong *sizep, bool use_ipv6) |
d50244e9 SG |
135 | { |
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, | |
7d018892 | 141 | env_get("bootfile"), use_ipv6)) |
d50244e9 | 142 | return -ENOMEM; |
7d018892 | 143 | |
91953956 SE |
144 | 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 | ||
7d018892 SE |
152 | 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 | ||
d50244e9 SG |
160 | /* |
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 | ||
7d018892 | 177 | error_exit: |
d50244e9 SG |
178 | pxe_destroy_ctx(&ctx); |
179 | ||
180 | return -ENOENT; | |
181 | done: | |
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 | ||
06283a64 JH |
194 | /* |
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 | */ | |
208 | static int | |
09140113 | 209 | do_pxe_get(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
06283a64 JH |
210 | { |
211 | char *pxefile_addr_str; | |
d50244e9 SG |
212 | ulong pxefile_addr_r; |
213 | char *fname; | |
214 | ulong size; | |
215 | int ret; | |
7d018892 | 216 | bool use_ipv6 = false; |
06283a64 | 217 | |
7d018892 SE |
218 | 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 | } | |
06283a64 | 228 | |
06283a64 JH |
229 | pxefile_addr_str = from_env("pxefile_addr_r"); |
230 | ||
231 | if (!pxefile_addr_str) | |
232 | return 1; | |
233 | ||
d50244e9 | 234 | ret = strict_strtoul(pxefile_addr_str, 16, |
31839dc2 | 235 | (unsigned long *)&pxefile_addr_r); |
d50244e9 | 236 | if (ret < 0) |
06283a64 JH |
237 | return 1; |
238 | ||
7d018892 | 239 | ret = pxe_get(pxefile_addr_r, &fname, &size, use_ipv6); |
d50244e9 SG |
240 | switch (ret) { |
241 | case 0: | |
242 | printf("Config file '%s' found\n", fname); | |
243 | break; | |
244 | case -ENOMEM: | |
12df842e SG |
245 | printf("Out of memory\n"); |
246 | return CMD_RET_FAILURE; | |
d50244e9 SG |
247 | default: |
248 | printf("Config file not found\n"); | |
249 | return CMD_RET_FAILURE; | |
39f98553 RH |
250 | } |
251 | ||
d50244e9 | 252 | return 0; |
06283a64 | 253 | } |
06283a64 | 254 | |
06283a64 JH |
255 | /* |
256 | * Boots a system using a pxe file | |
257 | * | |
258 | * Returns 0 on success, 1 on error. | |
259 | */ | |
260 | static int | |
09140113 | 261 | do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
06283a64 JH |
262 | { |
263 | unsigned long pxefile_addr_r; | |
06283a64 | 264 | char *pxefile_addr_str; |
fd3fa5c3 | 265 | struct pxe_context ctx; |
9e62e7ca | 266 | int ret; |
7d018892 SE |
267 | bool use_ipv6 = false; |
268 | ||
269 | if (IS_ENABLED(CONFIG_IPV6)) { | |
270 | if (!strcmp(argv[argc - 1], USE_IP6_CMD_PARAM)) | |
271 | use_ipv6 = true; | |
272 | } | |
06283a64 | 273 | |
7d018892 | 274 | if (argc == 1 || (argc == 2 && use_ipv6)) { |
06283a64 JH |
275 | pxefile_addr_str = from_env("pxefile_addr_r"); |
276 | if (!pxefile_addr_str) | |
277 | return 1; | |
278 | ||
7d018892 | 279 | } else if (argc == 2 || (argc == 3 && use_ipv6)) { |
06283a64 JH |
280 | pxefile_addr_str = argv[1]; |
281 | } else { | |
4c12eeb8 | 282 | return CMD_RET_USAGE; |
06283a64 JH |
283 | } |
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 | ||
12df842e | 290 | if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false, |
7d018892 | 291 | env_get("bootfile"), use_ipv6)) { |
12df842e SG |
292 | printf("Out of memory\n"); |
293 | return CMD_RET_FAILURE; | |
294 | } | |
9e62e7ca | 295 | ret = pxe_process(&ctx, pxefile_addr_r, false); |
12df842e | 296 | pxe_destroy_ctx(&ctx); |
9e62e7ca SG |
297 | if (ret) |
298 | return CMD_RET_FAILURE; | |
06283a64 | 299 | |
1411157d | 300 | copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name)); |
ded2e20e | 301 | |
06283a64 JH |
302 | return 0; |
303 | } | |
304 | ||
09140113 | 305 | static struct cmd_tbl cmd_pxe_sub[] = { |
7d018892 SE |
306 | U_BOOT_CMD_MKENT(get, 2, 1, do_pxe_get, "", ""), |
307 | U_BOOT_CMD_MKENT(boot, 3, 1, do_pxe_boot, "", "") | |
06283a64 JH |
308 | }; |
309 | ||
09140113 | 310 | static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
06283a64 | 311 | { |
09140113 | 312 | struct cmd_tbl *cp; |
06283a64 JH |
313 | |
314 | if (argc < 2) | |
4c12eeb8 | 315 | return CMD_RET_USAGE; |
06283a64 JH |
316 | |
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 | ||
4c12eeb8 | 326 | return CMD_RET_USAGE; |
06283a64 JH |
327 | } |
328 | ||
7d018892 | 329 | U_BOOT_CMD(pxe, 4, 1, do_pxe, |
eed99ce3 | 330 | "get and boot from pxe files", |
7d018892 SE |
331 | "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" | |
06283a64 | 333 | ); |
7d018892 SE |
334 | |
335 | #endif /* CONFIG_CMD_NET */ |