efifdt.c revision 1.37 1 /* $NetBSD: efifdt.c,v 1.37 2024/08/15 06:15:16 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2019 Jason R. Thorpe
5 * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include "efiboot.h"
31 #include "efifdt.h"
32 #include "efiblock.h"
33 #include "overlay.h"
34 #include "module.h"
35
36 #ifdef EFIBOOT_ACPI
37 #include "efiacpi.h"
38 #endif
39
40 #include <libfdt.h>
41
42 #define FDT_TABLE_GUID \
43 { 0xb1b621d5, 0xf19c, 0x41a5, { 0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0 } }
44 static EFI_GUID FdtTableGuid = FDT_TABLE_GUID;
45
46 #define FDT_MEMORY_NODE_PATH "/memory"
47 #define FDT_MEMORY_NODE_NAME "memory"
48 #define FDT_CHOSEN_NODE_PATH "/chosen"
49 #define FDT_CHOSEN_NODE_NAME "chosen"
50
51 #define FDT_MEMORY_USABLE(_md) \
52 ((_md)->Type == EfiLoaderCode || (_md)->Type == EfiLoaderData || \
53 (_md)->Type == EfiBootServicesCode || (_md)->Type == EfiBootServicesData || \
54 (_md)->Type == EfiConventionalMemory)
55
56 #define FDT_SPACE (4 * 1024 * 1024)
57 #define FDT_ALIGN (2 * 1024 * 1024)
58
59 #ifdef _LP64
60 #define PRIdUINTN "ld"
61 #define PRIxUINTN "lx"
62 #else
63 #define PRIdUINTN "d"
64 #define PRIxUINTN "x"
65 #endif
66 static void *fdt_data = NULL;
67 static size_t fdt_data_size = 512*1024;
68
69 static EFI_PHYSICAL_ADDRESS initrd_addr, dtb_addr, rndseed_addr;
70 static u_long initrd_size = 0, dtb_size = 0, rndseed_size = 0;
71
72 /* exec.c */
73 extern EFI_PHYSICAL_ADDRESS efirng_addr;
74 extern u_long efirng_size;
75
76 #ifdef EFIBOOT_ACPI
77 #define ACPI_FDT_SIZE (128 * 1024)
78 static int efi_fdt_create_acpifdt(void);
79 #endif
80
81 int
82 efi_fdt_probe(void)
83 {
84 EFI_STATUS status;
85
86 status = LibGetSystemConfigurationTable(&FdtTableGuid, &fdt_data);
87 if (EFI_ERROR(status))
88 return EIO;
89
90 if (fdt_check_header(fdt_data) != 0) {
91 fdt_data = NULL;
92 return EINVAL;
93 }
94
95 return 0;
96 }
97
98 int
99 efi_fdt_set_data(void *data)
100 {
101 int err;
102
103 if (fdt_check_header(data) != 0)
104 return EINVAL;
105
106 fdt_data = alloc(fdt_data_size);
107 if (fdt_data == NULL)
108 return ENOMEM;
109 memset(fdt_data, 0, fdt_data_size);
110
111 err = fdt_open_into(data, fdt_data, fdt_data_size);
112 if (err != 0) {
113 dealloc(fdt_data, fdt_data_size);
114 fdt_data = NULL;
115 return ENXIO;
116 }
117
118 return 0;
119 }
120
121 void *
122 efi_fdt_data(void)
123 {
124 return fdt_data;
125 }
126
127 int
128 efi_fdt_size(void)
129 {
130 return fdt_data == NULL ? 0 : fdt_totalsize(fdt_data);
131 }
132
133 bool
134 efi_fdt_overlay_is_compatible(void *dtbo)
135 {
136 const int system_root = fdt_path_offset(fdt_data, "/");
137 const int overlay_root = fdt_path_offset(dtbo, "/");
138
139 if (system_root < 0 || overlay_root < 0)
140 return false;
141
142 const int system_ncompat = fdt_stringlist_count(fdt_data, system_root,
143 "compatible");
144 const int overlay_ncompat = fdt_stringlist_count(dtbo, overlay_root,
145 "compatible");
146
147 if (system_ncompat <= 0 || overlay_ncompat <= 0)
148 return false;
149
150 const char *system_compatible, *overlay_compatible;
151 int si, oi;
152
153 for (si = 0; si < system_ncompat; si++) {
154 system_compatible = fdt_stringlist_get(fdt_data,
155 system_root, "compatible", si, NULL);
156 if (system_compatible == NULL)
157 continue;
158 for (oi = 0; oi < overlay_ncompat; oi++) {
159 overlay_compatible = fdt_stringlist_get(dtbo,
160 overlay_root, "compatible", oi, NULL);
161 if (overlay_compatible == NULL)
162 continue;
163 if (strcmp(system_compatible, overlay_compatible) == 0)
164 return true;
165 }
166 }
167
168 return false;
169 }
170
171 int
172 efi_fdt_overlay_apply(void *dtbo, int *fdterr)
173 {
174 int err = fdt_overlay_apply(fdt_data, dtbo);
175 if (fdterr)
176 *fdterr = err;
177 return err == 0 ? 0 : EIO;
178 }
179
180 void
181 efi_fdt_init(u_long addr, u_long len)
182 {
183 int error;
184
185 error = fdt_open_into(fdt_data, (void *)addr, len);
186 if (error < 0)
187 panic("fdt_open_into failed: %d", error);
188
189 fdt_data = (void *)addr;
190 }
191
192 void
193 efi_fdt_fini(void)
194 {
195 int error;
196
197 error = fdt_pack(fdt_data);
198 if (error < 0)
199 panic("fdt_pack failed: %d", error);
200 }
201
202 void
203 efi_fdt_show(void)
204 {
205 const char *model, *compat;
206 int n, ncompat;
207
208 if (fdt_data == NULL) {
209 return;
210 }
211
212 model = fdt_getprop(fdt_data, fdt_path_offset(fdt_data, "/"), "model", NULL);
213 if (model) {
214 command_printtab("FDT", "%s [", model);
215 }
216 ncompat = fdt_stringlist_count(fdt_data, fdt_path_offset(fdt_data, "/"), "compatible");
217 for (n = 0; n < ncompat; n++) {
218 compat = fdt_stringlist_get(fdt_data, fdt_path_offset(fdt_data, "/"),
219 "compatible", n, NULL);
220 printf("%s%s", n == 0 ? "" : ", ", compat);
221 }
222 printf("]\n");
223 }
224
225 static int
226 efi_fdt_chosen(void)
227 {
228 int chosen;
229
230 chosen = fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH);
231 if (chosen < 0)
232 chosen = fdt_add_subnode(fdt_data,
233 fdt_path_offset(fdt_data, "/"),
234 FDT_CHOSEN_NODE_NAME);
235 if (chosen < 0)
236 panic("FDT: Failed to create " FDT_CHOSEN_NODE_PATH " node");
237
238 return chosen;
239 }
240
241 void
242 efi_fdt_system_table(void)
243 {
244 #ifdef EFIBOOT_RUNTIME_ADDRESS
245 int chosen;
246
247 chosen = efi_fdt_chosen();
248
249 fdt_setprop_u64(fdt_data, chosen, "netbsd,uefi-system-table", (uint64_t)(uintptr_t)ST);
250 #endif
251 }
252
253 void
254 efi_fdt_memory_map(void)
255 {
256 UINTN nentries = 0, mapkey, descsize;
257 EFI_MEMORY_DESCRIPTOR *md, *memmap;
258 UINT32 descver;
259 UINT64 phys_start, phys_size;
260 int n, memory;
261
262 memory = fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH);
263 if (memory < 0)
264 memory = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_MEMORY_NODE_NAME);
265 if (memory < 0)
266 panic("FDT: Failed to create " FDT_MEMORY_NODE_PATH " node");
267
268 fdt_delprop(fdt_data, memory, "reg");
269
270 const int address_cells = fdt_address_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
271 const int size_cells = fdt_size_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
272
273 memmap = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
274 for (n = 0, md = memmap; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
275 /*
276 * create / find the chosen node for each iteration as it might have changed
277 * when adding to the memory node
278 */
279 int chosen = efi_fdt_chosen();
280 fdt_appendprop_u32(fdt_data, chosen, "netbsd,uefi-memmap", md->Type);
281 fdt_appendprop_u64(fdt_data, chosen, "netbsd,uefi-memmap", md->PhysicalStart);
282 fdt_appendprop_u64(fdt_data, chosen, "netbsd,uefi-memmap", md->NumberOfPages);
283 fdt_appendprop_u64(fdt_data, chosen, "netbsd,uefi-memmap", md->Attribute);
284
285 if ((md->Attribute & EFI_MEMORY_RUNTIME) != 0)
286 continue;
287
288 if ((md->Attribute & EFI_MEMORY_WB) == 0)
289 continue;
290 if (!FDT_MEMORY_USABLE(md))
291 continue;
292 if ((address_cells == 1 || size_cells == 1) && md->PhysicalStart + (md->NumberOfPages * EFI_PAGE_SIZE) > 0xffffffff)
293 continue;
294 if (md->NumberOfPages <= 1)
295 continue;
296
297 phys_start = md->PhysicalStart;
298 phys_size = md->NumberOfPages * EFI_PAGE_SIZE;
299
300 if (phys_start & EFI_PAGE_MASK) {
301 /*
302 * UEFI spec says these should be 4KB aligned, but
303 * U-Boot doesn't always, so round up to the next
304 * page.
305 */
306 phys_start = (phys_start + EFI_PAGE_SIZE) & ~EFI_PAGE_MASK;
307 phys_size -= (EFI_PAGE_SIZE * 2);
308 if (phys_size == 0)
309 continue;
310 }
311
312 memory = fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH);
313 if (address_cells == 1)
314 fdt_appendprop_u32(fdt_data, memory, "reg",
315 (uint32_t)phys_start);
316 else
317 fdt_appendprop_u64(fdt_data, memory, "reg",
318 phys_start);
319
320 if (size_cells == 1)
321 fdt_appendprop_u32(fdt_data, memory, "reg",
322 (uint32_t)phys_size);
323 else
324 fdt_appendprop_u64(fdt_data, memory, "reg",
325 phys_size);
326 }
327 }
328
329 void
330 efi_fdt_gop(void)
331 {
332 EFI_STATUS status;
333 EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
334 EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *mode;
335 EFI_HANDLE *gop_handle;
336 UINTN ngop_handle, n;
337 char buf[48];
338 int fb, chosen;
339
340 status = LibLocateHandle(ByProtocol, &GraphicsOutputProtocol, NULL, &ngop_handle, &gop_handle);
341 if (EFI_ERROR(status) || ngop_handle == 0)
342 return;
343
344 for (n = 0; n < ngop_handle; n++) {
345 status = uefi_call_wrapper(BS->HandleProtocol, 3, gop_handle[n], &GraphicsOutputProtocol, (void **)&gop);
346 if (EFI_ERROR(status))
347 continue;
348
349 mode = gop->Mode;
350 if (mode == NULL)
351 continue;
352
353 #ifdef EFIBOOT_DEBUG
354 printf("GOP: FB @ 0x%" PRIx64 " size 0x%" PRIxUINTN "\n", mode->FrameBufferBase, mode->FrameBufferSize);
355 printf("GOP: Version %d\n", mode->Info->Version);
356 printf("GOP: HRes %d VRes %d\n", mode->Info->HorizontalResolution, mode->Info->VerticalResolution);
357 printf("GOP: PixelFormat %d\n", mode->Info->PixelFormat);
358 printf("GOP: PixelBitmask R 0x%x G 0x%x B 0x%x Res 0x%x\n",
359 mode->Info->PixelInformation.RedMask,
360 mode->Info->PixelInformation.GreenMask,
361 mode->Info->PixelInformation.BlueMask,
362 mode->Info->PixelInformation.ReservedMask);
363 printf("GOP: Pixels per scanline %d\n", mode->Info->PixelsPerScanLine);
364 #endif
365
366 if (mode->Info->PixelFormat == PixelBltOnly) {
367 printf("GOP: PixelBltOnly pixel format not supported\n");
368 continue;
369 }
370
371 chosen = efi_fdt_chosen();
372 fdt_setprop_u32(fdt_data, chosen, "#address-cells", 2);
373 fdt_setprop_u32(fdt_data, chosen, "#size-cells", 2);
374 fdt_setprop_empty(fdt_data, chosen, "ranges");
375
376 snprintf(buf, sizeof(buf), "framebuffer@%" PRIx64, mode->FrameBufferBase);
377 fb = fdt_add_subnode(fdt_data, chosen, buf);
378 if (fb < 0) {
379 /* Framebuffer node already exists. No need to create a new one! */
380 return;
381 }
382
383 fdt_appendprop_string(fdt_data, fb, "compatible", "simple-framebuffer");
384 fdt_appendprop_string(fdt_data, fb, "status", "okay");
385 fdt_appendprop_u64(fdt_data, fb, "reg", mode->FrameBufferBase);
386 fdt_appendprop_u64(fdt_data, fb, "reg", mode->FrameBufferSize);
387 fdt_appendprop_u32(fdt_data, fb, "width", mode->Info->HorizontalResolution);
388 fdt_appendprop_u32(fdt_data, fb, "height", mode->Info->VerticalResolution);
389 fdt_appendprop_u32(fdt_data, fb, "stride", mode->Info->PixelsPerScanLine * 4); /* XXX */
390 fdt_appendprop_string(fdt_data, fb, "format", "a8b8g8r8");
391
392 #ifdef EFIBOOT_ACPI
393 /*
394 * In ACPI mode, use GOP as console.
395 */
396 if (efi_acpi_available()) {
397 snprintf(buf, sizeof(buf), "/chosen/framebuffer@%" PRIx64, mode->FrameBufferBase);
398 fdt_setprop_string(fdt_data, chosen, "stdout-path", buf);
399 }
400 #endif
401
402 return;
403 }
404 }
405
406 void
407 efi_fdt_bootargs(const char *bootargs)
408 {
409 struct efi_block_part *bpart = efi_block_boot_part();
410 uint8_t macaddr[6];
411 int chosen;
412
413 chosen = efi_fdt_chosen();
414
415 if (*bootargs)
416 fdt_setprop_string(fdt_data, chosen, "bootargs", bootargs);
417
418 if (bpart) {
419 switch (bpart->type) {
420 case EFI_BLOCK_PART_DISKLABEL:
421 fdt_setprop(fdt_data, chosen, "netbsd,mbr",
422 bpart->hash, sizeof(bpart->hash));
423 fdt_setprop_u32(fdt_data, chosen, "netbsd,partition",
424 bpart->index);
425 break;
426 case EFI_BLOCK_PART_GPT:
427 if (bpart->gpt.ent.ent_name[0] == 0x0000) {
428 fdt_setprop(fdt_data, chosen, "netbsd,gpt-guid",
429 bpart->hash, sizeof(bpart->hash));
430 } else {
431 char *label = NULL;
432 int rv = ucs2_to_utf8(bpart->gpt.ent.ent_name, &label);
433 if (rv == 0) {
434 fdt_setprop_string(fdt_data, chosen, "netbsd,gpt-label", label);
435 FreePool(label);
436 }
437 }
438 break;
439 default:
440 break;
441 }
442 } else if (efi_net_get_booted_macaddr(macaddr) == 0) {
443 fdt_setprop(fdt_data, chosen, "netbsd,booted-mac-address", macaddr, sizeof(macaddr));
444 }
445 }
446
447 static void
448 efi_fdt_userconf_addprop(const char *cmd)
449 {
450 const int chosen = efi_fdt_chosen();
451
452 fdt_appendprop_string(fdt_data, chosen, "netbsd,userconf", cmd);
453 }
454
455 void
456 efi_fdt_userconf(void)
457 {
458 userconf_foreach(efi_fdt_userconf_addprop);
459 }
460
461 void
462 efi_fdt_initrd(u_long initrd_addr, u_long initrd_size)
463 {
464 int chosen;
465
466 if (initrd_size == 0)
467 return;
468
469 chosen = efi_fdt_chosen();
470 fdt_setprop_u64(fdt_data, chosen, "linux,initrd-start", initrd_addr);
471 fdt_setprop_u64(fdt_data, chosen, "linux,initrd-end", initrd_addr + initrd_size);
472 }
473
474 /* pass in the NetBSD on-disk random seed */
475 void
476 efi_fdt_rndseed(u_long addr, u_long size)
477 {
478 int chosen;
479
480 if (size == 0)
481 return;
482
483 chosen = efi_fdt_chosen();
484 fdt_setprop_u64(fdt_data, chosen, "netbsd,rndseed-start", addr);
485 fdt_setprop_u64(fdt_data, chosen, "netbsd,rndseed-end", addr + size);
486 }
487
488 /* pass in output from the EFI firmware's RNG from some unknown source */
489 void
490 efi_fdt_efirng(u_long efirng_addr, u_long efirng_size)
491 {
492 int chosen;
493
494 if (efirng_size == 0)
495 return;
496
497 chosen = efi_fdt_chosen();
498 fdt_setprop_u64(fdt_data, chosen, "netbsd,efirng-start",
499 efirng_addr);
500 fdt_setprop_u64(fdt_data, chosen, "netbsd,efirng-end",
501 efirng_addr + efirng_size);
502 }
503
504 /* pass in module information */
505 void
506 efi_fdt_module(const char *module_name, u_long module_addr, u_long module_size)
507 {
508 int chosen;
509
510 if (module_size == 0)
511 return;
512
513 chosen = efi_fdt_chosen();
514 fdt_appendprop_string(fdt_data, chosen, "netbsd,module-names", module_name);
515 fdt_appendprop_u64(fdt_data, chosen, "netbsd,modules", module_addr);
516 fdt_appendprop_u64(fdt_data, chosen, "netbsd,modules", module_size);
517 }
518
519 static void
520 apply_overlay(const char *path, void *dtbo)
521 {
522
523 if (!efi_fdt_overlay_is_compatible(dtbo)) {
524 printf("boot: %s: incompatible overlay\n", path);
525 return;
526 }
527
528 int fdterr;
529
530 if (efi_fdt_overlay_apply(dtbo, &fdterr) != 0) {
531 printf("boot: %s: error %d applying overlay\n", path, fdterr);
532 }
533 }
534
535 static void
536 apply_overlay_file(const char *path)
537 {
538 EFI_PHYSICAL_ADDRESS dtbo_addr;
539 u_long dtbo_size;
540
541 if (strlen(path) == 0)
542 return;
543
544 if (load_file(path, 0, false, &dtbo_addr, &dtbo_size) != 0 ||
545 dtbo_addr == 0) {
546 /* Error messages have already been displayed. */
547 goto out;
548 }
549
550 apply_overlay(path, (void *)(uintptr_t)dtbo_addr);
551
552 out:
553 if (dtbo_addr) {
554 uefi_call_wrapper(BS->FreePages, 2, dtbo_addr,
555 EFI_SIZE_TO_PAGES(dtbo_size));
556 }
557 }
558
559 static void
560 load_fdt_overlays(void)
561 {
562 if (!dtoverlay_enabled)
563 return;
564
565 dtoverlay_foreach(apply_overlay_file);
566 }
567
568 static void
569 load_module(const char *module_name)
570 {
571 EFI_PHYSICAL_ADDRESS addr;
572 u_long size;
573 char path[PATH_MAX];
574
575 snprintf(path, sizeof(path), "%s/%s/%s.kmod", module_prefix,
576 module_name, module_name);
577
578 if (load_file(path, 0, false, &addr, &size) != 0 || addr == 0 || size == 0)
579 return;
580
581 efi_fdt_module(module_name, (u_long)addr, size);
582 }
583
584 static void
585 load_modules(const char *kernel_name)
586 {
587 if (!module_enabled)
588 return;
589
590 module_init(kernel_name);
591 module_foreach(load_module);
592 }
593
594
595 /*
596 * Prepare kernel arguments and shutdown boot services.
597 */
598 int
599 efi_fdt_prepare_boot(const char *fname, const char *args, u_long *marks)
600 {
601 int error;
602
603 load_file(get_initrd_path(), 0, false, &initrd_addr, &initrd_size);
604 load_file(get_dtb_path(), 0, false, &dtb_addr, &dtb_size);
605
606 error = efi_md_prepare_boot(fname, args, marks);
607 if (error) {
608 return error;
609 }
610 #ifdef EFIBOOT_ACPI
611 /* ACPI support only works for little endian kernels */
612 if (efi_acpi_available() && netbsd_elf_data == ELFDATA2LSB) {
613 error = efi_fdt_create_acpifdt();
614 if (error != 0) {
615 return error;
616 }
617 } else
618 #endif
619 if (dtb_addr && efi_fdt_set_data((void *)(uintptr_t)dtb_addr) != 0) {
620 return EINVAL;
621 }
622
623 if (efi_fdt_size() > 0) {
624 /*
625 * Load the rndseed as late as possible -- after we
626 * have committed to using fdt and executing this
627 * kernel -- so that it doesn't hang around in memory
628 * if we have to bail or the kernel won't use it.
629 */
630 load_file(get_rndseed_path(), 0, false,
631 &rndseed_addr, &rndseed_size);
632
633 efi_fdt_init((marks[MARK_END] + FDT_ALIGN - 1) & -FDT_ALIGN, FDT_ALIGN);
634 load_modules(fname);
635 load_fdt_overlays();
636 efi_fdt_initrd(initrd_addr, initrd_size);
637 efi_fdt_rndseed(rndseed_addr, rndseed_size);
638 efi_fdt_efirng(efirng_addr, efirng_size);
639 efi_fdt_bootargs(args);
640 efi_fdt_userconf();
641 efi_fdt_system_table();
642 efi_fdt_gop();
643 efi_fdt_memory_map();
644 }
645
646 efi_cleanup();
647
648 if (efi_fdt_size() > 0) {
649 efi_fdt_fini();
650 }
651
652 return 0;
653 }
654
655 /*
656 * Free memory after a failed boot.
657 */
658 void
659 efi_fdt_cleanup_boot(void)
660 {
661 if (rndseed_addr) {
662 uefi_call_wrapper(BS->FreePages, 2, rndseed_addr, EFI_SIZE_TO_PAGES(rndseed_size));
663 rndseed_addr = 0;
664 rndseed_size = 0;
665 }
666 if (initrd_addr) {
667 uefi_call_wrapper(BS->FreePages, 2, initrd_addr, EFI_SIZE_TO_PAGES(initrd_size));
668 initrd_addr = 0;
669 initrd_size = 0;
670 }
671 if (dtb_addr) {
672 uefi_call_wrapper(BS->FreePages, 2, dtb_addr, EFI_SIZE_TO_PAGES(dtb_size));
673 dtb_addr = 0;
674 dtb_size = 0;
675 }
676 }
677
678 size_t
679 efi_fdt_alloc_size(void)
680 {
681 return FDT_SPACE;
682 }
683
684 #ifdef EFIBOOT_ACPI
685 int
686 efi_fdt_create_acpifdt(void)
687 {
688 void *acpi_root = efi_acpi_root();
689 void *smbios_table = efi_acpi_smbios();
690 void *fdt;
691 int error;
692
693 if (acpi_root == NULL)
694 return EINVAL;
695
696 fdt = AllocatePool(ACPI_FDT_SIZE);
697 if (fdt == NULL)
698 return ENOMEM;
699
700 error = fdt_create_empty_tree(fdt, ACPI_FDT_SIZE);
701 if (error)
702 return EIO;
703
704 const char *model = efi_acpi_get_model();
705
706 fdt_setprop_string(fdt, fdt_path_offset(fdt, "/"), "compatible", "netbsd,generic-acpi");
707 fdt_setprop_string(fdt, fdt_path_offset(fdt, "/"), "model", model);
708 fdt_setprop_cell(fdt, fdt_path_offset(fdt, "/"), "#address-cells", 2);
709 fdt_setprop_cell(fdt, fdt_path_offset(fdt, "/"), "#size-cells", 2);
710
711 fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), "chosen");
712 fdt_setprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,acpi-root-table", (uint64_t)(uintptr_t)acpi_root);
713 if (smbios_table)
714 fdt_setprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,smbios-table", (uint64_t)(uintptr_t)smbios_table);
715
716 fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), "acpi");
717 fdt_setprop_string(fdt, fdt_path_offset(fdt, "/acpi"), "compatible", "netbsd,acpi");
718
719 return efi_fdt_set_data(fdt);
720 }
721 #endif
722
723 #ifdef EFIBOOT_RUNTIME_ADDRESS
724 static uint64_t
725 efi_fdt_runtime_alloc_va(uint64_t npages)
726 {
727 static uint64_t va = EFIBOOT_RUNTIME_ADDRESS;
728 static uint64_t sz = EFIBOOT_RUNTIME_SIZE;
729 uint64_t nva;
730
731 if (sz < (npages * EFI_PAGE_SIZE)) {
732 panic("efi_acpi_alloc_va: couldn't allocate %" PRIu64 " pages",
733 npages);
734 }
735
736 nva = va;
737 va += (npages * EFI_PAGE_SIZE);
738 sz -= (npages * EFI_PAGE_SIZE);
739
740 return nva;
741 }
742
743 void
744 efi_fdt_set_virtual_address_map(EFI_MEMORY_DESCRIPTOR *memmap, UINTN nentries,
745 UINTN mapkey, UINTN descsize, UINT32 descver)
746 {
747 EFI_MEMORY_DESCRIPTOR *md, *vmd, *vmemmap;
748 EFI_STATUS status;
749 int n, nrt;
750 void *fdt;
751
752 fdt = efi_fdt_data();
753
754 vmemmap = alloc(nentries * descsize);
755 if (vmemmap == NULL)
756 panic("FATAL: couldn't allocate virtual memory map");
757
758 for (n = 0, nrt = 0, vmd = vmemmap, md = memmap;
759 n < nentries;
760 n++, md = NextMemoryDescriptor(md, descsize)) {
761
762 if ((md->Attribute & EFI_MEMORY_RUNTIME) == 0) {
763 continue;
764 }
765
766 md->VirtualStart = efi_fdt_runtime_alloc_va(md->NumberOfPages);
767
768 switch (md->Type) {
769 case EfiRuntimeServicesCode:
770 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
771 "netbsd,uefi-runtime-code", md->PhysicalStart);
772 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
773 "netbsd,uefi-runtime-code", md->VirtualStart);
774 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
775 "netbsd,uefi-runtime-code",
776 md->NumberOfPages * EFI_PAGE_SIZE);
777 break;
778 case EfiRuntimeServicesData:
779 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
780 "netbsd,uefi-runtime-data", md->PhysicalStart);
781 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
782 "netbsd,uefi-runtime-data", md->VirtualStart);
783 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
784 "netbsd,uefi-runtime-data",
785 md->NumberOfPages * EFI_PAGE_SIZE);
786 break;
787 case EfiMemoryMappedIO:
788 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
789 "netbsd,uefi-runtime-mmio", md->PhysicalStart);
790 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
791 "netbsd,uefi-runtime-mmio", md->VirtualStart);
792 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
793 "netbsd,uefi-runtime-mmio",
794 md->NumberOfPages * EFI_PAGE_SIZE);
795 break;
796 default:
797 break;
798 }
799
800 *vmd = *md;
801 vmd = NextMemoryDescriptor(vmd, descsize);
802 ++nrt;
803 }
804
805 status = uefi_call_wrapper(RT->SetVirtualAddressMap, 4, nrt * descsize,
806 descsize, descver, vmemmap);
807 if (EFI_ERROR(status)) {
808 printf("WARNING: SetVirtualAddressMap failed\n");
809 return;
810 }
811 }
812 #endif
813