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