[BOOTMGFW]
[reactos.git] / reactos / boot / environ / lib / bootlib.c
1 /*
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)
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include "bl.h"
12
13 /* DATA VARIABLES ************************************************************/
14
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;
21
22 /* FUNCTIONS *****************************************************************/
23
24 /* HACKKKYYY */
25 EFI_SYSTEM_TABLE* g_SystemTable;
26
27 VOID
28 EarlyPrint(_In_ PWCHAR Format, ...)
29 {
30 WCHAR buffer[1024];
31 va_list args;
32
33 va_start(args, Format);
34
35 vswprintf(buffer, Format, args);
36
37 g_SystemTable->ConOut->OutputString(g_SystemTable->ConOut, L"\r");
38 g_SystemTable->ConOut->OutputString(g_SystemTable->ConOut, buffer);
39
40 g_SystemTable->BootServices->Stall(1000000);
41
42 va_end(args);
43 }
44 /* END HACKKKYYY */
45
46 /*++
47 * @name InitializeLibrary
48 *
49 * The InitializeLibrary function initializes the Boot Library.
50 *
51 * @param BootParameters
52 * Pointer to the Boot Application Parameter Block.
53 *
54 * @param LibraryParameters
55 * Pointer to the Boot Library Parameters.
56 *
57 * @return NT_SUCCESS if the boot library was loaded correctly, relevant error
58 * otherwise.
59 *
60 *--*/
61 NTSTATUS
62 InitializeLibrary (
63 _In_ PBOOT_APPLICATION_PARAMETER_BLOCK BootAppParameters,
64 _In_ PBL_LIBRARY_PARAMETERS LibraryParameters
65 )
66 {
67 NTSTATUS Status;
68 PBL_MEMORY_DATA MemoryData;
69 PBL_APPLICATION_ENTRY AppEntry;
70 PBL_FIRMWARE_DESCRIPTOR FirmwareDescriptor;
71 ULONG_PTR ParamPointer = (ULONG_PTR)BootAppParameters;
72
73 /* Validate correct Boot Application data */
74 if (!(BootAppParameters) ||
75 (BootAppParameters->Signature[0] != BOOT_APPLICATION_SIGNATURE_1) ||
76 (BootAppParameters->Signature[1] != BOOT_APPLICATION_SIGNATURE_2) ||
77 (BootAppParameters->Size < sizeof(*BootAppParameters)))
78 {
79 Status = STATUS_INVALID_PARAMETER;
80 goto Quickie;
81 }
82
83 /* Get sub-structures */
84 MemoryData = (PBL_MEMORY_DATA)(ParamPointer + BootAppParameters->MemoryDataOffset);
85 FirmwareDescriptor = (PBL_FIRMWARE_DESCRIPTOR)(ParamPointer + BootAppParameters->FirmwareParametersOffset);
86 AppEntry = (PBL_APPLICATION_ENTRY)(ParamPointer + BootAppParameters->AppEntryOffset);
87 BlpBootDevice = (PBL_DEVICE_DESCRIPTOR)(ParamPointer + BootAppParameters->BootDeviceOffset);
88 BlpApplicationBaseDirectory = LibraryParameters->ApplicationBaseDirectory;
89
90 /* Initialize the firmware table */
91 Status = BlpFwInitialize(0, FirmwareDescriptor);
92 if (!NT_SUCCESS(Status))
93 {
94 goto Quickie;
95 }
96
97 /* Find boot application entry */
98 if (strncmp(AppEntry->Signature, BL_APP_ENTRY_SIGNATURE, 7))
99 {
100 Status = STATUS_INVALID_PARAMETER_9;
101 goto Quickie;
102 }
103
104 /* Read parameters */
105 BlpApplicationParameters = BootAppParameters;
106 BlpLibraryParameters = *LibraryParameters;
107
108 /* Save the application entry */
109 if (AppEntry->Flags & 2)
110 {
111 AppEntry->Flags = (AppEntry->Flags & ~0x2) | 0x80;
112 }
113 BlpApplicationEntry = *AppEntry;
114
115 /* Everything has been captured */
116 BlpLibraryParametersInitialized = TRUE;
117
118 /* Initialize the architecture (PM or RM) switching */
119 Status = BlpArchInitialize(0);
120 if (!NT_SUCCESS(Status))
121 {
122 goto Quickie;
123 }
124
125 /* Initialize the memory manager */
126 Status = BlpMmInitialize(MemoryData,
127 BootAppParameters->MemoryTranslationType,
128 LibraryParameters);
129 if (!NT_SUCCESS(Status))
130 {
131 EarlyPrint(L"MM init failed!\n");
132 goto Quickie;
133 }
134
135 EarlyPrint(L"TODO!\n");
136 Status = STATUS_NOT_IMPLEMENTED;
137
138 Quickie:
139 EarlyPrint(L"Exiting init: %lx\n", Status);
140 return Status;
141 }
142
143 /*++
144 * @name BlInitializeLibrary
145 *
146 * The BlInitializeLibrary function initializes, or re-initializes, the
147 * Boot Library.
148 *
149 * @param BootParameters
150 * Pointer to the Boot Application Parameter Block.
151 *
152 * @param LibraryParameters
153 * Pointer to the Boot Library Parameters.
154 *
155 * @return NT_SUCCESS if the boot library was loaded correctly, relevant error
156 * otherwise.
157 *
158 *--*/
159 NTSTATUS
160 BlInitializeLibrary(
161 _In_ PBOOT_APPLICATION_PARAMETER_BLOCK BootAppParameters,
162 _In_ PBL_LIBRARY_PARAMETERS LibraryParameters
163 )
164 {
165 NTSTATUS Status;
166
167 /* Are we re-initializing the library? */
168 if (LibraryParameters->LibraryFlags & 2)
169 {
170 /* From scratch? */
171 BlpLibraryParameters = *LibraryParameters;
172 if (LibraryParameters->LibraryFlags & 4)
173 {
174 #if 0
175 /* Initialize all the core modules again */
176 BlpSiInitialize(1);
177 BlBdInitialize();
178 BlMmRemoveBadMemory();
179 BlpMmInitializeConstraints();
180
181 /* Redraw the graphics console as needed */
182 BlpDisplayInitialize(LibraryParameters->LibraryFlags);
183 BlpResourceInitialize();
184 #endif
185 }
186
187 /* Nothing to do, we're done */
188 Status = STATUS_SUCCESS;
189 }
190 else
191 {
192 /* Nope, this is first time initialization */
193 Status = InitializeLibrary(BootAppParameters, LibraryParameters);
194 }
195
196 /* Return initialization status */
197 return Status;
198 }
199