efiboot.c revision 1.3 1 /* $NetBSD: efiboot.c,v 1.3 2017/02/11 10:15:55 nonaka Exp $ */
2
3 /*-
4 * Copyright (c) 2016 Kimihiro Nonaka <nonaka (at) 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 EFI_LOADED_IMAGE *efi_li;
37 uintptr_t efi_main_sp;
38 bool efi_cleanuped;
39
40 static EFI_PHYSICAL_ADDRESS heap_start = EFI_ALLOCATE_MAX_ADDRESS;
41 static UINTN heap_size = 1 * 1024 * 1024; /* 1MB */
42 static struct btinfo_efi btinfo_efi;
43
44 static void efi_heap_init(void);
45
46 EFI_STATUS EFIAPI efi_main(EFI_HANDLE, EFI_SYSTEM_TABLE *);
47 EFI_STATUS EFIAPI
48 efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *systemTable)
49 {
50 EFI_STATUS status;
51 EFI_DEVICE_PATH *dp0, *dp;
52 extern char twiddle_toggle;
53
54 IH = imageHandle;
55 InitializeLib(IH, systemTable);
56
57 efi_main_sp = (uintptr_t)&status;
58 twiddle_toggle = 1; /* no twiddling until we're ready */
59
60 cninit();
61 efi_heap_init();
62 efi_md_init();
63
64 status = uefi_call_wrapper(BS->HandleProtocol, 3, IH,
65 &LoadedImageProtocol, (void **)&efi_li);
66 if (EFI_ERROR(status))
67 Panic(L"HandleProtocol(LoadedImageProtocol): %r", status);
68 status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_li->DeviceHandle,
69 &DevicePathProtocol, (void **)&dp0);
70 if (EFI_ERROR(status))
71 Panic(L"HandleProtocol(DevicePathProtocol): %r", status);
72 for (dp = dp0; !IsDevicePathEnd(dp); dp = NextDevicePathNode(dp)) {
73 if (DevicePathType(dp) == MEDIA_DEVICE_PATH)
74 continue;
75 if (DevicePathSubType(dp) == MEDIA_HARDDRIVE_DP) {
76 boot_biosdev = 0x80;
77 efi_bootdp = dp;
78 break;
79 }
80 break;
81 }
82
83 efi_disk_probe();
84
85 status = uefi_call_wrapper(BS->SetWatchdogTimer, 4, 0, 0, 0, NULL);
86 if (EFI_ERROR(status))
87 Panic(L"SetWatchdogTimer: %r", status);
88
89 boot();
90
91 return EFI_SUCCESS;
92 }
93
94 void
95 efi_cleanup(void)
96 {
97 EFI_STATUS status;
98 EFI_MEMORY_DESCRIPTOR *desc;
99 UINTN NoEntries, MapKey, DescriptorSize;
100 UINT32 DescriptorVersion;
101 struct btinfo_efimemmap *bim;
102 size_t allocsz;
103
104 clearit();
105
106 memset(&btinfo_efi, 0, sizeof(btinfo_efi));
107 btinfo_efi.systblpa = (intptr_t)ST;
108 #ifdef __i386__ /* bootia32.efi */
109 btinfo_efi.flags |= BI_EFI_32BIT;
110 #endif
111 BI_ADD(&btinfo_efi, BTINFO_EFI, sizeof(btinfo_efi));
112
113 NoEntries = 0;
114 desc = efi_memory_get_map(&NoEntries, &MapKey, &DescriptorSize,
115 &DescriptorVersion, true);
116 status = uefi_call_wrapper(BS->ExitBootServices, 2, IH, MapKey);
117 if (EFI_ERROR(status)) {
118 FreePool(desc);
119 desc = efi_memory_get_map(&NoEntries, &MapKey, &DescriptorSize,
120 &DescriptorVersion, true);
121 status = uefi_call_wrapper(BS->ExitBootServices, 2, IH, MapKey);
122 if (EFI_ERROR(status))
123 Panic(L"ExitBootServices failed");
124 }
125 efi_cleanuped = true;
126
127 allocsz = sizeof(struct btinfo_efimemmap) - 1
128 + NoEntries * DescriptorSize;
129 bim = alloc(allocsz);
130 bim->num = NoEntries;
131 bim->version = DescriptorVersion;
132 bim->size = DescriptorSize;
133 memcpy(bim->memmap, desc, NoEntries * DescriptorSize);
134 BI_ADD(bim, BTINFO_EFIMEMMAP, allocsz);
135 }
136
137 static void
138 efi_heap_init(void)
139 {
140 EFI_STATUS status;
141 u_int sz = EFI_SIZE_TO_PAGES(heap_size);
142
143 status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress,
144 EfiLoaderData, sz, &heap_start);
145 if (EFI_ERROR(status))
146 Panic(L"%a: AllocatePages() failed: %d page(s): %r",
147 __func__, sz, status);
148 setheap((void *)(UINTN)heap_start,
149 (void *)(UINTN)(heap_start + heap_size));
150 }
151