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