Commit | Line | Data |
---|---|---|
acfa9bdf SG |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* | |
3 | * Bootmethod for distro boot via EFI | |
4 | * | |
5 | * Copyright 2021 Google LLC | |
6 | * Written by Simon Glass <sjg@chromium.org> | |
7 | */ | |
8 | ||
9 | #define LOG_CATEGORY UCLASS_BOOTSTD | |
10 | ||
11 | #include <common.h> | |
12 | #include <bootdev.h> | |
13 | #include <bootflow.h> | |
14 | #include <bootmeth.h> | |
15 | #include <command.h> | |
16 | #include <dm.h> | |
17 | #include <efi_loader.h> | |
18 | #include <fs.h> | |
19 | #include <malloc.h> | |
20 | #include <mapmem.h> | |
21 | #include <mmc.h> | |
3e18860e | 22 | #include <net.h> |
acfa9bdf | 23 | #include <pxe_utils.h> |
2984d21a | 24 | #include <linux/sizes.h> |
acfa9bdf SG |
25 | |
26 | #define EFI_DIRNAME "efi/boot/" | |
27 | ||
28 | /** | |
29 | * get_efi_leafname() - Get the leaf name for the EFI file we expect | |
30 | * | |
31 | * @str: Place to put leaf name for this architecture, e.g. "bootaa64.efi". | |
32 | * Must have at least 16 bytes of space | |
33 | * @max_len: Length of @str, must be >=16 | |
34 | */ | |
35 | static int get_efi_leafname(char *str, int max_len) | |
36 | { | |
37 | const char *base; | |
38 | ||
39 | if (max_len < 16) | |
40 | return log_msg_ret("spc", -ENOSPC); | |
41 | if (IS_ENABLED(CONFIG_ARM64)) | |
42 | base = "bootaa64"; | |
43 | else if (IS_ENABLED(CONFIG_ARM)) | |
44 | base = "bootarm"; | |
45 | else if (IS_ENABLED(CONFIG_X86_RUN_32BIT)) | |
46 | base = "bootia32"; | |
47 | else if (IS_ENABLED(CONFIG_X86_RUN_64BIT)) | |
48 | base = "bootx64"; | |
49 | else if (IS_ENABLED(CONFIG_ARCH_RV32I)) | |
50 | base = "bootriscv32"; | |
51 | else if (IS_ENABLED(CONFIG_ARCH_RV64I)) | |
52 | base = "bootriscv64"; | |
53 | else if (IS_ENABLED(CONFIG_SANDBOX)) | |
54 | base = "bootsbox"; | |
55 | else | |
56 | return -EINVAL; | |
57 | ||
58 | strcpy(str, base); | |
59 | strcat(str, ".efi"); | |
60 | ||
61 | return 0; | |
62 | } | |
63 | ||
3e18860e SG |
64 | static int get_efi_pxe_arch(void) |
65 | { | |
66 | /* http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xml */ | |
67 | if (IS_ENABLED(CONFIG_ARM64)) | |
68 | return 0xb; | |
69 | else if (IS_ENABLED(CONFIG_ARM)) | |
70 | return 0xa; | |
71 | else if (IS_ENABLED(CONFIG_X86_64)) | |
72 | return 0x6; | |
73 | else if (IS_ENABLED(CONFIG_X86)) | |
74 | return 0x7; | |
75 | else if (IS_ENABLED(CONFIG_ARCH_RV32I)) | |
76 | return 0x19; | |
77 | else if (IS_ENABLED(CONFIG_ARCH_RV64I)) | |
78 | return 0x1b; | |
79 | else if (IS_ENABLED(CONFIG_SANDBOX)) | |
80 | return 0; /* not used */ | |
81 | ||
82 | return -EINVAL; | |
83 | } | |
84 | ||
85 | static int get_efi_pxe_vci(char *str, int max_len) | |
86 | { | |
87 | int ret; | |
88 | ||
89 | ret = get_efi_pxe_arch(); | |
90 | if (ret < 0) | |
91 | return ret; | |
92 | ||
93 | snprintf(str, max_len, "PXEClient:Arch:%05x:UNDI:003000", ret); | |
94 | ||
95 | return 0; | |
96 | } | |
97 | ||
146242cc SG |
98 | /** |
99 | * bootmeth_uses_network() - check if the media device is Ethernet | |
100 | * | |
101 | * @bflow: Bootflow to check | |
102 | * Returns: true if the media device is Ethernet, else false | |
103 | */ | |
104 | static bool bootmeth_uses_network(struct bootflow *bflow) | |
105 | { | |
106 | const struct udevice *media = dev_get_parent(bflow->dev); | |
107 | ||
108 | return IS_ENABLED(CONFIG_CMD_DHCP) && | |
109 | device_get_uclass_id(media) == UCLASS_ETH; | |
110 | } | |
111 | ||
d80bb4f9 | 112 | static void set_efi_bootdev(struct blk_desc *desc, struct bootflow *bflow) |
acfa9bdf SG |
113 | { |
114 | const struct udevice *media_dev; | |
115 | int size = bflow->size; | |
116 | const char *dev_name; | |
117 | char devnum_str[9]; | |
118 | char dirname[200]; | |
119 | char *last_slash; | |
acfa9bdf SG |
120 | |
121 | /* | |
122 | * This is a horrible hack to tell EFI about this boot device. Once we | |
123 | * unify EFI with the rest of U-Boot we can clean this up. The same hack | |
124 | * exists in multiple places, e.g. in the fs, tftp and load commands. | |
125 | * | |
126 | * Once we can clean up the EFI code to make proper use of driver model, | |
127 | * this can go away. | |
128 | */ | |
129 | media_dev = dev_get_parent(bflow->dev); | |
484e701d MM |
130 | snprintf(devnum_str, sizeof(devnum_str), "%x:%x", |
131 | desc ? desc->devnum : dev_seq(media_dev), | |
d80bb4f9 | 132 | bflow->part); |
acfa9bdf SG |
133 | |
134 | strlcpy(dirname, bflow->fname, sizeof(dirname)); | |
135 | last_slash = strrchr(dirname, '/'); | |
136 | if (last_slash) | |
137 | *last_slash = '\0'; | |
138 | ||
139 | log_debug("setting bootdev %s, %s, %s, %p, %x\n", | |
140 | dev_get_uclass_name(media_dev), devnum_str, bflow->fname, | |
141 | bflow->buf, size); | |
142 | dev_name = device_get_uclass_id(media_dev) == UCLASS_MASS_STORAGE ? | |
143 | "usb" : dev_get_uclass_name(media_dev); | |
144 | efi_set_bootdev(dev_name, devnum_str, bflow->fname, bflow->buf, size); | |
d80bb4f9 SG |
145 | } |
146 | ||
6a8c2f97 | 147 | static int efiload_read_file(struct bootflow *bflow, ulong addr) |
d80bb4f9 | 148 | { |
6a8c2f97 SG |
149 | struct blk_desc *desc = NULL; |
150 | loff_t bytes_read; | |
d80bb4f9 SG |
151 | int ret; |
152 | ||
6a8c2f97 SG |
153 | if (bflow->blk) |
154 | desc = dev_get_uclass_plat(bflow->blk); | |
155 | ret = bootmeth_setup_fs(bflow, desc); | |
156 | if (ret) | |
157 | return log_msg_ret("set", ret); | |
158 | ||
159 | ret = fs_read(bflow->fname, addr, 0, bflow->size, &bytes_read); | |
d80bb4f9 SG |
160 | if (ret) |
161 | return log_msg_ret("read", ret); | |
6a8c2f97 SG |
162 | bflow->buf = map_sysmem(addr, bflow->size); |
163 | ||
164 | set_efi_bootdev(desc, bflow); | |
acfa9bdf SG |
165 | |
166 | return 0; | |
167 | } | |
168 | ||
169 | static int distro_efi_check(struct udevice *dev, struct bootflow_iter *iter) | |
170 | { | |
3e18860e SG |
171 | /* This only works on block and network devices */ |
172 | if (bootflow_iter_check_blk(iter) && bootflow_iter_check_net(iter)) | |
173 | return log_msg_ret("blk", -ENOTSUPP); | |
acfa9bdf | 174 | |
d9f48579 SG |
175 | /* This works on block devices and network devices */ |
176 | if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY) | |
177 | return log_msg_ret("pxe", -ENOTSUPP); | |
178 | ||
acfa9bdf SG |
179 | return 0; |
180 | } | |
181 | ||
47dd6b4d SG |
182 | /** |
183 | * distro_efi_get_fdt_name() - Get the filename for reading the .dtb file | |
184 | * | |
185 | * @fname: Place to put filename | |
186 | * @size: Max size of filename | |
187 | * @seq: Sequence number, to cycle through options (0=first) | |
188 | * Returns: 0 on success, -ENOENT if the "fdtfile" env var does not exist, | |
189 | * -EINVAL if there are no more options, -EALREADY if the control FDT should be | |
190 | * used | |
191 | */ | |
192 | static int distro_efi_get_fdt_name(char *fname, int size, int seq) | |
e890e8c4 SG |
193 | { |
194 | const char *fdt_fname; | |
47dd6b4d SG |
195 | const char *prefix; |
196 | ||
197 | /* select the prefix */ | |
198 | switch (seq) { | |
199 | case 0: | |
200 | /* this is the default */ | |
201 | prefix = "/dtb"; | |
202 | break; | |
203 | case 1: | |
204 | prefix = ""; | |
205 | break; | |
206 | case 2: | |
207 | prefix = "/dtb/current"; | |
208 | break; | |
209 | default: | |
210 | return log_msg_ret("pref", -EINVAL); | |
211 | } | |
e890e8c4 SG |
212 | |
213 | fdt_fname = env_get("fdtfile"); | |
214 | if (fdt_fname) { | |
47dd6b4d | 215 | snprintf(fname, size, "%s/%s", prefix, fdt_fname); |
e890e8c4 | 216 | log_debug("Using device tree: %s\n", fname); |
47dd6b4d SG |
217 | } else if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE)) { |
218 | strcpy(fname, "<prior>"); | |
219 | return log_msg_ret("pref", -EALREADY); | |
220 | /* Use this fallback only for 32-bit ARM */ | |
221 | } else if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_ARM64)) { | |
e890e8c4 SG |
222 | const char *soc = env_get("soc"); |
223 | const char *board = env_get("board"); | |
224 | const char *boardver = env_get("boardver"); | |
225 | ||
226 | /* cf the code in label_boot() which seems very complex */ | |
47dd6b4d | 227 | snprintf(fname, size, "%s/%s%s%s%s.dtb", prefix, |
e890e8c4 SG |
228 | soc ? soc : "", soc ? "-" : "", board ? board : "", |
229 | boardver ? boardver : ""); | |
230 | log_debug("Using default device tree: %s\n", fname); | |
47dd6b4d SG |
231 | } else { |
232 | return log_msg_ret("env", -ENOENT); | |
e890e8c4 | 233 | } |
47dd6b4d SG |
234 | |
235 | return 0; | |
e890e8c4 SG |
236 | } |
237 | ||
6a8c2f97 SG |
238 | /* |
239 | * distro_efi_try_bootflow_files() - Check that files are present | |
240 | * | |
241 | * This reads any FDT file and checks whether the bootflow file is present, for | |
242 | * later reading. We avoid reading the bootflow now, since it is likely large, | |
243 | * it may take a long time and we want to avoid needing to allocate memory for | |
244 | * it | |
245 | * | |
246 | * @dev: bootmeth device to use | |
247 | * @bflow: bootflow to update | |
248 | */ | |
249 | static int distro_efi_try_bootflow_files(struct udevice *dev, | |
3e18860e | 250 | struct bootflow *bflow) |
acfa9bdf SG |
251 | { |
252 | struct blk_desc *desc = NULL; | |
e890e8c4 | 253 | ulong fdt_addr, size; |
3e18860e | 254 | char fname[256]; |
47dd6b4d | 255 | int ret, seq; |
acfa9bdf SG |
256 | |
257 | /* We require a partition table */ | |
258 | if (!bflow->part) | |
259 | return -ENOENT; | |
260 | ||
261 | strcpy(fname, EFI_DIRNAME); | |
262 | ret = get_efi_leafname(fname + strlen(fname), | |
263 | sizeof(fname) - strlen(fname)); | |
264 | if (ret) | |
265 | return log_msg_ret("leaf", ret); | |
266 | ||
267 | if (bflow->blk) | |
268 | desc = dev_get_uclass_plat(bflow->blk); | |
269 | ret = bootmeth_try_file(bflow, desc, NULL, fname); | |
270 | if (ret) | |
271 | return log_msg_ret("try", ret); | |
272 | ||
6a8c2f97 SG |
273 | /* Since we can access the file, let's call it ready */ |
274 | bflow->state = BOOTFLOWST_READY; | |
acfa9bdf | 275 | |
47dd6b4d SG |
276 | fdt_addr = env_get_hex("fdt_addr_r", 0); |
277 | ||
278 | /* try the various available names */ | |
279 | ret = -ENOENT; | |
9f835165 SG |
280 | *fname = '\0'; |
281 | for (seq = 0; ret == -ENOENT; seq++) { | |
47dd6b4d | 282 | ret = distro_efi_get_fdt_name(fname, sizeof(fname), seq); |
9f835165 | 283 | if (ret == -EALREADY) |
47dd6b4d | 284 | bflow->flags = BOOTFLOWF_USE_PRIOR_FDT; |
2984d21a SG |
285 | if (!ret) { |
286 | /* Limit FDT files to 4MB */ | |
287 | size = SZ_4M; | |
9f835165 SG |
288 | ret = bootmeth_common_read_file(dev, bflow, fname, |
289 | fdt_addr, &size); | |
2984d21a | 290 | } |
47dd6b4d SG |
291 | } |
292 | ||
9f835165 SG |
293 | if (*fname) { |
294 | bflow->fdt_fname = strdup(fname); | |
295 | if (!bflow->fdt_fname) | |
296 | return log_msg_ret("fil", -ENOMEM); | |
297 | } | |
e890e8c4 | 298 | |
e890e8c4 SG |
299 | if (!ret) { |
300 | bflow->fdt_size = size; | |
301 | bflow->fdt_addr = fdt_addr; | |
302 | ||
303 | /* | |
304 | * TODO: Apply extension overlay | |
305 | * | |
306 | * Here we need to load and apply the extension overlay. This is | |
307 | * not implemented. See do_extension_apply(). The extension | |
308 | * stuff needs an implementation in boot/extension.c so it is | |
309 | * separate from the command code. Really the extension stuff | |
310 | * should use the device tree and a uclass / driver interface | |
311 | * rather than implementing its own list | |
312 | */ | |
313 | } else { | |
314 | log_debug("No device tree available\n"); | |
184fc037 | 315 | bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT; |
e890e8c4 SG |
316 | } |
317 | ||
acfa9bdf SG |
318 | return 0; |
319 | } | |
320 | ||
3e18860e SG |
321 | static int distro_efi_read_bootflow_net(struct bootflow *bflow) |
322 | { | |
e890e8c4 SG |
323 | char file_addr[17], fname[256]; |
324 | char *tftp_argv[] = {"tftp", file_addr, fname, NULL}; | |
325 | struct cmd_tbl cmdtp = {}; /* dummy */ | |
e31317e1 | 326 | const char *addr_str, *fdt_addr_str, *bootfile_name; |
3e18860e | 327 | int ret, arch, size; |
e890e8c4 | 328 | ulong addr, fdt_addr; |
3e18860e | 329 | char str[36]; |
3e18860e SG |
330 | |
331 | ret = get_efi_pxe_vci(str, sizeof(str)); | |
332 | if (ret) | |
333 | return log_msg_ret("vci", ret); | |
334 | ret = get_efi_pxe_arch(); | |
335 | if (ret < 0) | |
336 | return log_msg_ret("arc", ret); | |
337 | arch = ret; | |
338 | ||
339 | ret = env_set("bootp_vci", str); | |
340 | if (ret) | |
341 | return log_msg_ret("vcs", ret); | |
c5440a6a | 342 | ret = env_set_hex("bootp_arch", arch); |
3e18860e SG |
343 | if (ret) |
344 | return log_msg_ret("ars", ret); | |
345 | ||
346 | /* figure out the load address */ | |
347 | addr_str = env_get("kernel_addr_r"); | |
348 | addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr; | |
349 | ||
350 | /* clear any previous bootfile */ | |
351 | env_set("bootfile", NULL); | |
352 | ||
353 | /* read the kernel */ | |
354 | ret = dhcp_run(addr, NULL, true); | |
355 | if (ret) | |
356 | return log_msg_ret("dhc", ret); | |
357 | ||
358 | size = env_get_hex("filesize", -1); | |
359 | if (size <= 0) | |
360 | return log_msg_ret("sz", -EINVAL); | |
361 | bflow->size = size; | |
362 | ||
e31317e1 SR |
363 | /* bootfile should be setup by dhcp*/ |
364 | bootfile_name = env_get("bootfile"); | |
365 | if (!bootfile_name) | |
366 | return log_msg_ret("bootfile_name", ret); | |
367 | bflow->fname = strdup(bootfile_name); | |
368 | ||
3e18860e SG |
369 | /* do the hideous EFI hack */ |
370 | efi_set_bootdev("Net", "", bflow->fname, map_sysmem(addr, 0), | |
371 | bflow->size); | |
372 | ||
e890e8c4 SG |
373 | /* read the DT file also */ |
374 | fdt_addr_str = env_get("fdt_addr_r"); | |
375 | if (!fdt_addr_str) | |
376 | return log_msg_ret("fdt", -EINVAL); | |
377 | fdt_addr = hextoul(fdt_addr_str, NULL); | |
378 | sprintf(file_addr, "%lx", fdt_addr); | |
379 | ||
47dd6b4d SG |
380 | /* We only allow the first prefix with PXE */ |
381 | ret = distro_efi_get_fdt_name(fname, sizeof(fname), 0); | |
382 | if (ret) | |
383 | return log_msg_ret("nam", ret); | |
384 | ||
e890e8c4 SG |
385 | bflow->fdt_fname = strdup(fname); |
386 | if (!bflow->fdt_fname) | |
387 | return log_msg_ret("fil", -ENOMEM); | |
388 | ||
389 | if (!do_tftpb(&cmdtp, 0, 3, tftp_argv)) { | |
390 | bflow->fdt_size = env_get_hex("filesize", 0); | |
391 | bflow->fdt_addr = fdt_addr; | |
392 | } else { | |
393 | log_debug("No device tree available\n"); | |
184fc037 | 394 | bflow->flags |= BOOTFLOWF_USE_BUILTIN_FDT; |
e890e8c4 SG |
395 | } |
396 | ||
3e18860e SG |
397 | bflow->state = BOOTFLOWST_READY; |
398 | ||
399 | return 0; | |
400 | } | |
401 | ||
402 | static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow) | |
403 | { | |
3e18860e SG |
404 | int ret; |
405 | ||
37503b0c SR |
406 | /* |
407 | * bootmeth_efi doesn't allocate any buffer neither for blk nor net device | |
408 | * set flag to avoid freeing static buffer. | |
409 | */ | |
410 | bflow->flags |= BOOTFLOWF_STATIC_BUF; | |
411 | ||
146242cc | 412 | if (bootmeth_uses_network(bflow)) { |
3e18860e SG |
413 | /* we only support reading from one device, so ignore 'dev' */ |
414 | ret = distro_efi_read_bootflow_net(bflow); | |
415 | if (ret) | |
416 | return log_msg_ret("net", ret); | |
417 | } else { | |
6a8c2f97 | 418 | ret = distro_efi_try_bootflow_files(dev, bflow); |
3e18860e SG |
419 | if (ret) |
420 | return log_msg_ret("blk", ret); | |
421 | } | |
422 | ||
423 | return 0; | |
424 | } | |
425 | ||
6072703d | 426 | static int distro_efi_boot(struct udevice *dev, struct bootflow *bflow) |
acfa9bdf | 427 | { |
e890e8c4 | 428 | ulong kernel, fdt; |
6a8c2f97 | 429 | int ret; |
acfa9bdf | 430 | |
6a8c2f97 | 431 | kernel = env_get_hex("kernel_addr_r", 0); |
146242cc | 432 | if (!bootmeth_uses_network(bflow)) { |
6a8c2f97 SG |
433 | ret = efiload_read_file(bflow, kernel); |
434 | if (ret) | |
435 | return log_msg_ret("read", ret); | |
e890e8c4 SG |
436 | |
437 | /* | |
184fc037 | 438 | * use the provided device tree if not using the built-in fdt |
e890e8c4 | 439 | */ |
184fc037 | 440 | if (bflow->flags & ~BOOTFLOWF_USE_BUILTIN_FDT) |
e890e8c4 | 441 | fdt = bflow->fdt_addr; |
184fc037 | 442 | |
e890e8c4 SG |
443 | } else { |
444 | /* | |
445 | * This doesn't actually work for network devices: | |
446 | * | |
447 | * do_bootefi_image() No UEFI binary known at 0x02080000 | |
448 | * | |
449 | * But this is the same behaviour for distro boot, so it can be | |
450 | * fixed here. | |
451 | */ | |
e890e8c4 SG |
452 | fdt = env_get_hex("fdt_addr_r", 0); |
453 | } | |
454 | ||
184fc037 SR |
455 | if (bflow->flags & BOOTFLOWF_USE_BUILTIN_FDT) { |
456 | log_debug("Booting with built-in fdt\n"); | |
1373ffde TR |
457 | if (efi_binary_run(map_sysmem(kernel, 0), 0, |
458 | EFI_FDT_USE_INTERNAL)) | |
459 | return log_msg_ret("run", -EINVAL); | |
184fc037 SR |
460 | } else { |
461 | log_debug("Booting with external fdt\n"); | |
1373ffde TR |
462 | if (efi_binary_run(map_sysmem(kernel, 0), 0, |
463 | map_sysmem(fdt, 0))) | |
464 | return log_msg_ret("run", -EINVAL); | |
184fc037 | 465 | } |
acfa9bdf SG |
466 | |
467 | return 0; | |
468 | } | |
469 | ||
470 | static int distro_bootmeth_efi_bind(struct udevice *dev) | |
471 | { | |
472 | struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); | |
473 | ||
474 | plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ? | |
475 | "EFI boot from an .efi file" : "EFI"; | |
476 | ||
477 | return 0; | |
478 | } | |
479 | ||
480 | static struct bootmeth_ops distro_efi_bootmeth_ops = { | |
481 | .check = distro_efi_check, | |
482 | .read_bootflow = distro_efi_read_bootflow, | |
483 | .read_file = bootmeth_common_read_file, | |
484 | .boot = distro_efi_boot, | |
485 | }; | |
486 | ||
487 | static const struct udevice_id distro_efi_bootmeth_ids[] = { | |
488 | { .compatible = "u-boot,distro-efi" }, | |
489 | { } | |
490 | }; | |
491 | ||
492 | U_BOOT_DRIVER(bootmeth_efi) = { | |
493 | .name = "bootmeth_efi", | |
494 | .id = UCLASS_BOOTMETH, | |
495 | .of_match = distro_efi_bootmeth_ids, | |
496 | .ops = &distro_efi_bootmeth_ops, | |
497 | .bind = distro_bootmeth_efi_bind, | |
498 | }; |