2 * COPYRIGHT: See COPYING.ARM in the top level directory
3 * PROJECT: ReactOS UEFI Boot Library
4 * FILE: boot/environ/lib/bootlib.c
5 * PURPOSE: Boot Library Initialization
6 * PROGRAMMER: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES ******************************************************************/
13 /* DATA VARIABLES ************************************************************/
15 BL_LIBRARY_PARAMETERS BlpLibraryParameters
;
16 PBL_DEVICE_DESCRIPTOR BlpBootDevice
;
17 PWCHAR BlpApplicationBaseDirectory
;
18 PBOOT_APPLICATION_PARAMETER_BLOCK BlpApplicationParameters
;
19 BL_APPLICATION_ENTRY BlpApplicationEntry
;
20 BOOLEAN BlpLibraryParametersInitialized
;
22 ULONG PdPersistAllocations
;
23 LIST_ENTRY BlBadpListHead
;
25 /* FUNCTIONS *****************************************************************/
28 * @name InitializeLibrary
30 * The InitializeLibrary function initializes the Boot Library.
32 * @param BootParameters
33 * Pointer to the Boot Application Parameter Block.
35 * @param LibraryParameters
36 * Pointer to the Boot Library Parameters.
38 * @return NT_SUCCESS if the boot library was loaded correctly, relevant error
44 _In_ PBOOT_APPLICATION_PARAMETER_BLOCK BootAppParameters
,
45 _In_ PBL_LIBRARY_PARAMETERS LibraryParameters
49 PBL_MEMORY_DATA MemoryData
;
50 PBL_APPLICATION_ENTRY AppEntry
;
51 PBL_FIRMWARE_DESCRIPTOR FirmwareDescriptor
;
52 ULONG_PTR ParamPointer
= (ULONG_PTR
)BootAppParameters
;
54 /* Validate correct Boot Application data */
55 if (!(BootAppParameters
) ||
56 (BootAppParameters
->Signature
[0] != BOOT_APPLICATION_SIGNATURE_1
) ||
57 (BootAppParameters
->Signature
[1] != BOOT_APPLICATION_SIGNATURE_2
) ||
58 (BootAppParameters
->Size
< sizeof(*BootAppParameters
)))
60 Status
= STATUS_INVALID_PARAMETER
;
64 /* Get sub-structures */
65 MemoryData
= (PBL_MEMORY_DATA
)(ParamPointer
+ BootAppParameters
->MemoryDataOffset
);
66 FirmwareDescriptor
= (PBL_FIRMWARE_DESCRIPTOR
)(ParamPointer
+ BootAppParameters
->FirmwareParametersOffset
);
67 AppEntry
= (PBL_APPLICATION_ENTRY
)(ParamPointer
+ BootAppParameters
->AppEntryOffset
);
68 BlpBootDevice
= (PBL_DEVICE_DESCRIPTOR
)(ParamPointer
+ BootAppParameters
->BootDeviceOffset
);
69 BlpApplicationBaseDirectory
= LibraryParameters
->ApplicationBaseDirectory
;
71 /* Initialize the firmware table */
72 Status
= BlpFwInitialize(0, FirmwareDescriptor
);
73 if (!NT_SUCCESS(Status
))
78 /* Find boot application entry */
79 if (strncmp(AppEntry
->Signature
, BL_APP_ENTRY_SIGNATURE
, 7))
81 Status
= STATUS_INVALID_PARAMETER_9
;
86 BlpApplicationParameters
= BootAppParameters
;
87 BlpLibraryParameters
= *LibraryParameters
;
89 /* Save the application entry */
90 if (AppEntry
->Flags
& 2)
92 AppEntry
->Flags
= (AppEntry
->Flags
& ~0x2) | 0x80;
94 BlpApplicationEntry
= *AppEntry
;
96 /* Everything has been captured */
97 BlpLibraryParametersInitialized
= TRUE
;
99 /* Initialize the architecture (PM or RM) switching */
100 Status
= BlpArchInitialize(0);
101 if (!NT_SUCCESS(Status
))
106 /* Initialize the memory manager */
107 Status
= BlpMmInitialize(MemoryData
,
108 BootAppParameters
->MemoryTranslationType
,
110 if (!NT_SUCCESS(Status
))
112 EfiPrintf(L
"MM init failed!\r\n");
116 /* Initialize firmware now that the heap, etc works */
117 Status
= BlpFwInitialize(1, FirmwareDescriptor
);
118 if (!NT_SUCCESS(Status
))
120 /* Destroy memory manager in phase 1 */
122 EfiPrintf(L
"Firmware2 init failed!\r\n");
127 /* Modern systems have an undocumented BCD system for the boot frequency */
128 Status
= BlGetBootOptionInteger(BlpApplicationEntry
.BcdData
,
131 if (NT_SUCCESS(Status
) && (BootFrequency
))
133 /* Use it if present */
134 BlpTimePerformanceFrequency
= BootFrequency
;
139 /* Use the TSC for calibration */
140 Status
= BlpTimeCalibratePerformanceCounter();
141 if (!NT_SUCCESS(Status
))
143 /* Destroy memory manager in phase 1 */
144 EfiPrintf(L
"TSC calibration failed\r\n");
150 /* Now setup the rest of the architecture (IDT, etc) */
151 Status
= BlpArchInitialize(1);
152 if (!NT_SUCCESS(Status
))
154 /* Destroy memory manager in phase 1 */
155 EfiPrintf(L
"Arch2 init failed\r\n");
160 #ifdef BL_TPM_SUPPORT
161 /* Initialize support for Trusted Platform Module v1.2 */
165 #ifdef BL_TPM_SUPPORT
166 /* Initialize the event manager */
167 EnSubsystemInitialized
= 1;
168 InitializeListHead(&EnEventNotificationList
);
171 /* Initialize the I/O Manager */
172 Status
= BlpIoInitialize();
173 if (!NT_SUCCESS(Status
))
175 /* Destroy memory manager in phase 1 and the event manager */
176 EfiPrintf(L
"IO init failed\r\n");
177 #ifdef BL_TPM_SUPPORT
178 if (EnSubsystemInitialized
)
187 #ifdef BL_NET_SUPPORT
188 /* Initialize the network stack */
189 Status
= BlNetInitialize();
190 if (!NT_SUCCESS(Status
))
192 /* Destroy the I/O, event, and memory managers in phase 1 */
194 #ifdef BL_TPM_SUPPORT
195 if (EnSubsystemInitialized
)
205 /* Initialize the utility library */
206 Status
= BlUtlInitialize();
207 if (!NT_SUCCESS(Status
))
209 /* Destroy the network, I/O, event, and memory managers in phase 1 */
210 #ifdef BL_NET_SUPPORT
214 #ifdef BL_TPM_SUPPORT
215 if (EnSubsystemInitialized
)
221 EfiPrintf(L
"Util init failed\r\n");
226 /* Initialize PCI Platform Support */
227 PltInitializePciConfiguration();
230 #ifdef BL_SECURE_BOOT_SUPPORT
231 /* Read the current SecureBoot Policy*/
232 Status
= BlSecureBootSetActivePolicyFromPersistedData();
233 if (!NT_SUCCESS(Status
))
235 /* Destroy everything that we've currently set up */
237 PltDestroyPciConfiguration();
239 #ifdef BL_NET_SUPPORT
243 #ifdef BL_TPM_SUPPORT
244 if (EnSubsystemInitialized
)
254 #ifdef BL_TPM_SUPPORT
255 /* Initialize phase 0 of the security subsystem */
256 SipInitializePhase0();
260 /* Bring up the boot debugger, now that SecureBoot has been processed */
264 #ifdef BL_ETW_SUPPORT
265 /* Initialize internal logging */
269 /* Are graphics enabled? */
270 if (!(LibraryParameters
->LibraryFlags
& BL_LIBRARY_FLAG_NO_DISPLAY
))
272 /* Initialize the graphics library */
273 BlpDisplayInitialize(LibraryParameters
->LibraryFlags
);
276 /* Initialize the boot application persistent data */
277 PdPersistAllocations
= 0;
278 InitializeListHead(&BlBadpListHead
);
280 #ifdef BL_TPM_SUPPORT
281 /* Now setup the security subsystem in phase 1 */
286 /* Setup the text, UI and font resources */
287 Status
= BlpResourceInitialize();
288 if (!NT_SUCCESS(Status
))
290 /* Tear down everything if this failed */
291 if (!(LibraryParameters
->LibraryFlags
& BL_LIBRARY_FLAG_TEXT_MODE
))
293 // BlpDisplayDestroy();
297 PltDestroyPciConfiguration();
299 #ifdef BL_NET_SUPPORT
303 #ifdef BL_TPM_SUPPORT
304 if (EnSubsystemInitialized
)
314 #if BL_BITLOCKER_SUPPORT
315 /* Setup the boot cryptography library */
316 g_pEnvironmentData
= &SymCryptEnvironmentWindowsBootLibrary
;
317 if (SymCryptEnvWindowsBootLibInit
)
319 SymCryptEnvWindowsBootLibInit();
323 /* We are fully initialized, remember this and exit with success */
324 BlpLibraryParameters
.LibraryFlags
|= BL_LIBRARY_FLAG_INITIALIZATION_COMPLETED
;
325 Status
= STATUS_SUCCESS
;
332 * @name BlInitializeLibrary
334 * The BlInitializeLibrary function initializes, or re-initializes, the
337 * @param BootParameters
338 * Pointer to the Boot Application Parameter Block.
340 * @param LibraryParameters
341 * Pointer to the Boot Library Parameters.
343 * @return NT_SUCCESS if the boot library was loaded correctly, relevant error
349 _In_ PBOOT_APPLICATION_PARAMETER_BLOCK BootAppParameters
,
350 _In_ PBL_LIBRARY_PARAMETERS LibraryParameters
355 /* Are we re-initializing the library? */
356 if (LibraryParameters
->LibraryFlags
& BL_LIBRARY_FLAG_REINITIALIZE
)
359 BlpLibraryParameters
= *LibraryParameters
;
360 if (LibraryParameters
->LibraryFlags
& BL_LIBRARY_FLAG_REINITIALIZE_ALL
)
363 /* Initialize all the core modules again */
366 BlMmRemoveBadMemory();
367 BlpMmInitializeConstraints();
369 /* Redraw the graphics console as needed */
370 BlpDisplayInitialize(LibraryParameters
->LibraryFlags
);
371 BlpResourceInitialize();
375 /* Nothing to do, we're done */
376 Status
= STATUS_SUCCESS
;
380 /* Nope, this is first time initialization */
381 Status
= InitializeLibrary(BootAppParameters
, LibraryParameters
);
384 /* Return initialization status */
393 EfiPrintf(L
"Destroy not yet implemented\r\n");