OpenCore  1.0.6
OpenCore Bootloader
Loading...
Searching...
No Matches
UserBootServices.c
Go to the documentation of this file.
1
6#include <UserBootServices.h>
7#include <UserEvent.h>
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <time.h>
12
13EFI_BOOT_SERVICES mBootServices = {
14 .LocateProtocol = DummyLocateProtocol,
15 .AllocatePages = DummyAllocatePages,
16 .InstallConfigurationTable = DummyInstallConfigurationTable,
17 .CalculateCrc32 = DummyCalculateCrc32,
18
19 .CreateEventEx = UserCreateEventEx,
20 .CreateEvent = UserCreateEvent,
21 .CloseEvent = UserCloseEvent,
22 .CheckEvent = UserCheckEvent,
23 .WaitForEvent = UserWaitForEvent,
24 .SignalEvent = UserSignalEvent,
25 .SetTimer = UserSetTimer,
26 .RaiseTPL = UserRaiseTPL,
27 .RestoreTPL = UserRestoreTPL
28};
29
30EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL mConOut = {
31 .OutputString = NullTextOutputString
32};
33
34EFI_SYSTEM_TABLE mSystemTable = {
35 .BootServices = &mBootServices,
36 .ConOut = &mConOut
37};
38
39EFI_RUNTIME_SERVICES mRuntimeServices = {
40 .Hdr = { 0 },
41 .GetTime = DummyGetTime,
42};
43
44EFI_SYSTEM_TABLE *gST = &mSystemTable;
45EFI_BOOT_SERVICES *gBS = &mBootServices;
46
47EFI_HANDLE gImageHandle = (EFI_HANDLE)(UINTN)0xDEADBABEULL;
48
49BOOLEAN mPostEBS = FALSE;
50EFI_SYSTEM_TABLE *mDebugST = &mSystemTable;
51
52EFI_RUNTIME_SERVICES *gRT = &mRuntimeServices;
53
54#define CONFIG_TABLE_SIZE_INCREASED 0x10
56
57EFI_TPL
58EFIAPI
60 IN EFI_TPL NewTpl
61 )
62{
63 return 0;
64}
65
66VOID
67EFIAPI
69 IN EFI_TPL NewTpl
70 )
71{
72 return;
73}
74
75EFI_STATUS
76EFIAPI
78 IN EFI_GUID *Protocol,
79 IN VOID *Registration, OPTIONAL
80 OUT VOID **Interface
81 )
82{
83 return EFI_NOT_FOUND;
84}
85
86EFI_STATUS
87EFIAPI
89 IN EFI_ALLOCATE_TYPE Type,
90 IN EFI_MEMORY_TYPE MemoryType,
91 IN UINTN Pages,
92 IN OUT EFI_PHYSICAL_ADDRESS *Memory
93 )
94{
95 *Memory = (UINTN)AllocatePages (Pages);
96
97 return Memory != NULL ? EFI_SUCCESS : EFI_NOT_FOUND;
98}
99
100EFI_STATUS
101EFIAPI
103 IN EFI_GUID *Guid,
104 IN VOID *Table
105 )
106{
107 //
108 // NOTE: This code is adapted from original EDK II implementations.
109 //
110
111 UINTN Index;
112 EFI_CONFIGURATION_TABLE *EfiConfigurationTable;
113 EFI_CONFIGURATION_TABLE *OldTable;
114
115 //
116 // If Guid is NULL, then this operation cannot be performed
117 //
118 if (Guid == NULL) {
119 return EFI_INVALID_PARAMETER;
120 }
121
122 EfiConfigurationTable = gST->ConfigurationTable;
123
124 //
125 // Search all the table for an entry that matches Guid
126 //
127 for (Index = 0; Index < gST->NumberOfTableEntries; Index++) {
128 if (CompareGuid (Guid, &(gST->ConfigurationTable[Index].VendorGuid))) {
129 break;
130 }
131 }
132
133 if (Index < gST->NumberOfTableEntries) {
134 //
135 // A match was found, so this is either a modify or a delete operation
136 //
137 if (Table != NULL) {
138 //
139 // If Table is not NULL, then this is a modify operation.
140 // Modify the table entry and return.
141 //
142 gST->ConfigurationTable[Index].VendorTable = Table;
143
144 return EFI_SUCCESS;
145 }
146
147 //
148 // A match was found and Table is NULL, so this is a delete operation.
149 //
150 gST->NumberOfTableEntries--;
151
152 //
153 // Copy over deleted entry
154 //
155 CopyMem (
156 &(EfiConfigurationTable[Index]),
157 &(gST->ConfigurationTable[Index + 1]),
158 (gST->NumberOfTableEntries - Index) * sizeof (EFI_CONFIGURATION_TABLE)
159 );
160 } else {
161 //
162 // No matching GUIDs were found, so this is an add operation.
163 //
164 if (Table == NULL) {
165 //
166 // If Table is NULL on an add operation, then return an error.
167 //
168 return EFI_NOT_FOUND;
169 }
170
171 //
172 // Assume that Index == gST->NumberOfTableEntries
173 //
174 if ((Index * sizeof (EFI_CONFIGURATION_TABLE)) >= mSystemTableAllocateSize) {
175 //
176 // Allocate a table with one additional entry.
177 //
178 mSystemTableAllocateSize += (CONFIG_TABLE_SIZE_INCREASED * sizeof (EFI_CONFIGURATION_TABLE));
179 EfiConfigurationTable = AllocatePool (mSystemTableAllocateSize);
180 if (EfiConfigurationTable == NULL) {
181 //
182 // If a new table could not be allocated, then return an error.
183 //
184 return EFI_OUT_OF_RESOURCES;
185 }
186
187 if (gST->ConfigurationTable != NULL) {
188 //
189 // Copy the old table to the new table.
190 //
191 CopyMem (
192 EfiConfigurationTable,
193 gST->ConfigurationTable,
194 Index * sizeof (EFI_CONFIGURATION_TABLE)
195 );
196
197 //
198 // Record the old table pointer.
199 //
200 OldTable = gST->ConfigurationTable;
201
202 //
203 // As the CoreInstallConfigurationTable() may be re-entered by CoreFreePool()
204 // in its calling stack, updating System table to the new table pointer must
205 // be done before calling CoreFreePool() to free the old table.
206 // It can make sure the gST->ConfigurationTable point to the new table
207 // and avoid the errors of use-after-free to the old table by the reenter of
208 // CoreInstallConfigurationTable() in CoreFreePool()'s calling stack.
209 //
210 gST->ConfigurationTable = EfiConfigurationTable;
211
212 //
213 // Free the old table after updating System Table to the new table pointer.
214 //
215 FreePool (OldTable);
216 } else {
217 //
218 // Update System Table
219 //
220 gST->ConfigurationTable = EfiConfigurationTable;
221 }
222 }
223
224 //
225 // Fill in the new entry
226 //
227 CopyGuid ((VOID *)&EfiConfigurationTable[Index].VendorGuid, Guid);
228 EfiConfigurationTable[Index].VendorTable = Table;
229
230 //
231 // This is an add operation, so increment the number of table entries
232 //
233 gST->NumberOfTableEntries++;
234 }
235
236 return EFI_SUCCESS;
237}
238
239EFI_STATUS
240EFIAPI
242 IN VOID *Data,
243 IN UINTN DataSize,
244 OUT UINT32 *CrcOut
245 )
246{
247 if ((Data == NULL) || (DataSize == 0) || (CrcOut == NULL)) {
248 return EFI_INVALID_PARAMETER;
249 }
250
251 *CrcOut = CalculateCrc32 (Data, DataSize);
252 return EFI_SUCCESS;
253}
254
255EFI_STATUS
256EFIAPI
258 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
259 IN CHAR16 *String
260 )
261{
262 while (*String != '\0') {
263 if (*String != '\r') {
264 //
265 // Cast string to CHAR8 to truncate unicode symbols.
266 //
267 putchar ((CHAR8)*String);
268 }
269
270 ++String;
271 }
272
273 return EFI_SUCCESS;
274}
275
276EFI_STATUS
277EFIAPI
279 OUT EFI_TIME *Time,
280 OUT EFI_TIME_CAPABILITIES *Capabilities
281 )
282{
283 time_t EpochTime;
284 struct tm *TimeInfo;
285
286 if (Time == NULL) {
287 return EFI_INVALID_PARAMETER;
288 }
289
290 EpochTime = time (NULL);
291 TimeInfo = localtime (&EpochTime);
292
293 if (TimeInfo == NULL) {
294 return EFI_DEVICE_ERROR;
295 }
296
297 Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;
298 Time->Daylight = 0;
299 Time->Second = (UINT8)TimeInfo->tm_sec;
300 Time->Minute = (UINT8)TimeInfo->tm_min;
301 Time->Hour = (UINT8)TimeInfo->tm_hour;
302 Time->Day = (UINT8)TimeInfo->tm_mday;
303 //
304 // The EFI_TIME Month field count months from 1 to 12,
305 // while tm_mon counts from 0 to 11
306 //
307 Time->Month = (UINT8)(TimeInfo->tm_mon + 1);
308 //
309 // According ISO/IEC 9899:1999 7.23.1 the tm_year field
310 // contains number of years since 1900
311 //
312 Time->Year = (UINT16)(TimeInfo->tm_year + 1900);
313
314 if (Capabilities) {
315 Capabilities->Accuracy = 0;
316 Capabilities->Resolution = 1;
317 Capabilities->SetsToZero = FALSE;
318 }
319
320 return EFI_SUCCESS;
321}
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
GUID *EFIAPI CopyGuid(OUT GUID *DestinationGuid, IN CONST GUID *SourceGuid)
EFI_TPL EFIAPI DummyRaiseTPL(IN EFI_TPL NewTpl)
EFI_STATUS EFIAPI NullTextOutputString(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, IN CHAR16 *String)
EFI_SYSTEM_TABLE * gST
UINTN mSystemTableAllocateSize
#define CONFIG_TABLE_SIZE_INCREASED
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL mConOut
EFI_BOOT_SERVICES mBootServices
EFI_STATUS EFIAPI DummyCalculateCrc32(IN VOID *Data, IN UINTN DataSize, OUT UINT32 *CrcOut)
EFI_RUNTIME_SERVICES * gRT
EFI_SYSTEM_TABLE * mDebugST
EFI_HANDLE gImageHandle
EFI_STATUS EFIAPI DummyInstallConfigurationTable(IN EFI_GUID *Guid, IN VOID *Table)
EFI_STATUS EFIAPI DummyAllocatePages(IN EFI_ALLOCATE_TYPE Type, IN EFI_MEMORY_TYPE MemoryType, IN UINTN Pages, IN OUT EFI_PHYSICAL_ADDRESS *Memory)
VOID EFIAPI DummyRestoreTPL(IN EFI_TPL NewTpl)
EFI_SYSTEM_TABLE mSystemTable
EFI_RUNTIME_SERVICES mRuntimeServices
EFI_STATUS EFIAPI DummyGetTime(OUT EFI_TIME *Time, OUT EFI_TIME_CAPABILITIES *Capabilities)
EFI_STATUS EFIAPI DummyLocateProtocol(IN EFI_GUID *Protocol, IN VOID *Registration, OPTIONAL OUT VOID **Interface)
BOOLEAN mPostEBS
EFI_BOOT_SERVICES * gBS
EFI_STATUS EFIAPI UserWaitForEvent(IN UINTN NumberOfEvents, IN EFI_EVENT *Events, OUT UINTN *Index)
Definition UserEvent.c:481
EFI_STATUS EFIAPI UserCreateEvent(IN UINT32 Type, IN EFI_TPL NotifyTpl, IN EFI_EVENT_NOTIFY NotifyFunction OPTIONAL, IN VOID *NotifyContext OPTIONAL, OUT EFI_EVENT *Event)
Definition UserEvent.c:337
EFI_STATUS EFIAPI UserCheckEvent(IN EFI_EVENT Event)
Definition UserEvent.c:425
EFI_STATUS EFIAPI UserCreateEventEx(IN UINT32 Type, IN EFI_TPL NotifyTpl, IN EFI_EVENT_NOTIFY NotifyFunction OPTIONAL, IN CONST VOID *NotifyContext OPTIONAL, IN CONST EFI_GUID *EventGroup OPTIONAL, OUT EFI_EVENT *Event)
Definition UserEvent.c:248
EFI_STATUS EFIAPI UserSignalEvent(IN EFI_EVENT Event)
Definition UserEvent.c:392
EFI_STATUS EFIAPI UserCloseEvent(IN EFI_EVENT Event)
Definition UserEvent.c:364
EFI_STATUS EFIAPI UserSetTimer(IN EFI_EVENT Event, IN EFI_TIMER_DELAY Type, IN UINT64 TriggerTime)
Definition UserEvent.c:556
VOID EFIAPI UserRestoreTPL(IN EFI_TPL OldTpl)
Definition UserEvent.c:636
EFI_TPL EFIAPI UserRaiseTPL(IN EFI_TPL NewTpl)
Definition UserEvent.c:611