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