1 /* $NetBSD: efiboot.c,v 1.13 2023/04/20 00:42:24 manu Exp $ */
2
3 /*-
4 * Copyright (c) 2016 Kimihiro Nonaka <nonaka@netbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "efiboot.h"
30
31 #include "bootinfo.h"
32 #include "devopen.h"
33
34 EFI_HANDLE IH;
35 EFI_DEVICE_PATH *efi_bootdp;
36 enum efi_boot_device_type efi_bootdp_type = BOOT_DEVICE_TYPE_HD;
37 EFI_LOADED_IMAGE *efi_li;
38 uintptr_t efi_main_sp;
39 physaddr_t efi_loadaddr, efi_kernel_start, efi_load_start;
40 physaddr_t efi_kernel_reloc = 0;
41 enum efi_reloc_type efi_reloc_type = RELOC_DEFAULT;
42 u_long efi_kernel_size;
43 bool efi_cleanuped;
44 struct btinfo_efimemmap *btinfo_efimemmap = NULL;
45
46 static EFI_PHYSICAL_ADDRESS heap_start = EFI_ALLOCATE_MAX_ADDRESS;
47 static UINTN heap_size = 1 * 1024 * 1024; /* 1MB */
48 static struct btinfo_efi btinfo_efi;
49
50 static void efi_heap_init(void);
51
52 EFI_STATUS EFIAPI efi_main(EFI_HANDLE, EFI_SYSTEM_TABLE *);
53 EFI_STATUS EFIAPI
efi_main(EFI_HANDLE imageHandle,EFI_SYSTEM_TABLE * systemTable)54 efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *systemTable)
55 {
56 EFI_STATUS status;
57 EFI_DEVICE_PATH *dp0, *dp;
58 extern char twiddle_toggle;
59
60 IH = imageHandle;
61 InitializeLib(IH, systemTable);
62
63 efi_main_sp = (uintptr_t)&status;
64 twiddle_toggle = 1; /* no twiddling until we're ready */
65
66 cninit();
67 efi_heap_init();
68 efi_md_init();
69
70 status = uefi_call_wrapper(BS->HandleProtocol, 3, IH,
71 &LoadedImageProtocol, (void **)&efi_li);
72 if (EFI_ERROR(status))
73 panic("HandleProtocol(LoadedImageProtocol): %" PRIxMAX,
74 (uintmax_t)status);
75 status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_li->DeviceHandle,
76 &DevicePathProtocol, (void **)&dp0);
77 if (EFI_ERROR(status))
78 panic("HandleProtocol(DevicePathProtocol): %" PRIxMAX,
79 (uintmax_t)status);
80 efi_bootdp = dp0;
81 for (dp = dp0; !IsDevicePathEnd(dp); dp = NextDevicePathNode(dp)) {
82 if (DevicePathType(dp) == MEDIA_DEVICE_PATH &&
83 DevicePathSubType(dp) == MEDIA_CDROM_DP) {
84 efi_bootdp_type = BOOT_DEVICE_TYPE_CD;
85 break;
86 }
87 if (DevicePathType(dp) == MESSAGING_DEVICE_PATH &&
88 DevicePathSubType(dp) == MSG_MAC_ADDR_DP) {
89 efi_bootdp_type = BOOT_DEVICE_TYPE_NET;
90 break;
91 }
92 }
93
94 efi_memory_probe();
95 efi_disk_probe();
96 efi_pxe_probe();
97 efi_net_probe();
98
99 status = uefi_call_wrapper(BS->SetWatchdogTimer, 4, 0, 0, 0, NULL);
100 if (EFI_ERROR(status))
101 panic("SetWatchdogTimer: %" PRIxMAX, (uintmax_t)status);
102
103 boot();
104
105 return EFI_SUCCESS;
106 }
107
108 void
efi_cleanup(void)109 efi_cleanup(void)
110 {
111 EFI_STATUS status;
112 EFI_MEMORY_DESCRIPTOR *desc;
113 UINTN NoEntries, MapKey, DescriptorSize;
114 UINT32 DescriptorVersion;
115 size_t allocsz;
116
117 memset(&btinfo_efi, 0, sizeof(btinfo_efi));
118 btinfo_efi.systblpa = (intptr_t)ST;
119 #ifdef __i386__ /* bootia32.efi */
120 btinfo_efi.flags |= BI_EFI_32BIT;
121 #endif
122 BI_ADD(&btinfo_efi, BTINFO_EFI, sizeof(btinfo_efi));
123
124 NoEntries = 0;
125 desc = efi_memory_get_map(&NoEntries, &MapKey, &DescriptorSize,
126 &DescriptorVersion, true);
127 status = uefi_call_wrapper(BS->ExitBootServices, 2, IH, MapKey);
128 if (EFI_ERROR(status)) {
129 FreePool(desc);
130 desc = efi_memory_get_map(&NoEntries, &MapKey, &DescriptorSize,
131 &DescriptorVersion, true);
132 status = uefi_call_wrapper(BS->ExitBootServices, 2, IH, MapKey);
133 if (EFI_ERROR(status))
134 panic("ExitBootServices failed");
135 }
136 efi_cleanuped = true;
137
138 efi_memory_compact_map(desc, &NoEntries, DescriptorSize);
139 allocsz = sizeof(struct btinfo_efimemmap) - 1
140 + NoEntries * DescriptorSize;
141 btinfo_efimemmap = alloc(allocsz);
142 btinfo_efimemmap->num = NoEntries;
143 btinfo_efimemmap->version = DescriptorVersion;
144 btinfo_efimemmap->size = DescriptorSize;
145 memcpy(btinfo_efimemmap->memmap, desc, NoEntries * DescriptorSize);
146 BI_ADD(btinfo_efimemmap, BTINFO_EFIMEMMAP, allocsz);
147 }
148
149 static void
efi_heap_init(void)150 efi_heap_init(void)
151 {
152 EFI_STATUS status;
153 u_int sz = EFI_SIZE_TO_PAGES(heap_size);
154
155 status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress,
156 EfiLoaderData, sz, &heap_start);
157 if (EFI_ERROR(status))
158 Panic(L"%a: AllocatePages() failed: %d page(s): %r",
159 __func__, sz, status);
160 setheap((void *)(UINTN)heap_start,
161 (void *)(UINTN)(heap_start + heap_size));
162 }
163