efiboot.c revision 1.8 1 /* $NetBSD: efiboot.c,v 1.8 2018/09/15 17:06:32 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
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 #include "efifile.h"
31 #include "efiblock.h"
32 #include "efifdt.h"
33
34 #include <sys/reboot.h>
35
36 EFI_HANDLE IH;
37 EFI_DEVICE_PATH *efi_bootdp;
38 EFI_LOADED_IMAGE *efi_li;
39
40 int howto = 0;
41
42 static EFI_PHYSICAL_ADDRESS heap_start;
43 static UINTN heap_size = 1 * 1024 * 1024;
44 static EFI_EVENT delay_ev = 0;
45
46 EFI_STATUS EFIAPI efi_main(EFI_HANDLE, EFI_SYSTEM_TABLE *);
47
48 EFI_STATUS EFIAPI
49 efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *systemTable)
50 {
51 EFI_STATUS status;
52 u_int sz = EFI_SIZE_TO_PAGES(heap_size);
53
54 IH = imageHandle;
55
56 InitializeLib(imageHandle, systemTable);
57
58 (void)uefi_call_wrapper(ST->ConOut->Reset, 2, ST->ConOut, FALSE);
59 if (ST->ConOut->ClearScreen)
60 (void)uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
61
62 status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData, sz, &heap_start);
63 if (EFI_ERROR(status))
64 return status;
65 setheap((void *)heap_start, (void *)(heap_start + heap_size));
66
67 status = uefi_call_wrapper(BS->HandleProtocol, 3, imageHandle, &LoadedImageProtocol, (void **)&efi_li);
68 if (EFI_ERROR(status))
69 return status;
70 status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_li->DeviceHandle, &DevicePathProtocol, (void **)&efi_bootdp);
71 if (EFI_ERROR(status))
72 efi_bootdp = NULL;
73
74 #ifdef EFIBOOT_DEBUG
75 Print(L"Loaded image : 0x%lX\n", efi_li);
76 Print(L"FilePath : 0x%lX\n", efi_li->FilePath);
77 Print(L"ImageBase : 0x%lX\n", efi_li->ImageBase);
78 Print(L"ImageSize : 0x%lX\n", efi_li->ImageSize);
79 Print(L"Image file : %s\n", DevicePathToStr(efi_li->FilePath));
80 #endif
81
82 efi_fdt_probe();
83 efi_file_system_probe();
84 efi_block_probe();
85 efi_pxe_probe();
86 efi_net_probe();
87
88 boot();
89
90 return EFI_SUCCESS;
91 }
92
93 void
94 efi_cleanup(void)
95 {
96 EFI_STATUS status;
97 UINTN nentries, mapkey, descsize;
98 UINT32 descver;
99
100 LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
101
102 status = uefi_call_wrapper(BS->ExitBootServices, 2, IH, mapkey);
103 if (EFI_ERROR(status))
104 printf("WARNING: ExitBootServices failed\n");
105 }
106
107 void
108 efi_exit(void)
109 {
110 EFI_STATUS status;
111
112 status = uefi_call_wrapper(BS->Exit, 4, IH, EFI_ABORTED, 0, NULL);
113 if (EFI_ERROR(status))
114 printf("WARNING: Exit failed\n");
115 }
116
117 void
118 efi_reboot(void)
119 {
120 uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS, 0, NULL);
121
122 printf("WARNING: Reset failed\n");
123 }
124
125 void
126 efi_delay(int us)
127 {
128 EFI_STATUS status;
129 UINTN val;
130
131 if (delay_ev == 0) {
132 status = uefi_call_wrapper(BS->CreateEvent, 5, EVT_TIMER, TPL_APPLICATION, 0, 0, &delay_ev);
133 if (EFI_ERROR(status))
134 return;
135 }
136
137 uefi_call_wrapper(BS->SetTimer, 3, delay_ev, TimerRelative, us * 10);
138 uefi_call_wrapper(BS->WaitForEvent, 3, 1, &delay_ev, &val);
139 }
140
141 void
142 efi_progress(const char *fmt, ...)
143 {
144 va_list ap;
145
146 if ((howto & AB_SILENT) != 0)
147 return;
148
149 va_start(ap, fmt);
150 vprintf(fmt, ap);
151 va_end(ap);
152 }
153