[BOOTMGFW]:
[reactos.git] / reactos / boot / environ / include / bl.h
1 /*
2 * COPYRIGHT: See COPYING.ARM in the top level directory
3 * PROJECT: ReactOS UEFI Boot Library
4 * FILE: boot/environ/include/bl.h
5 * PURPOSE: Main Boot Library Header
6 * PROGRAMMER: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8
9 #ifndef _BL_H
10 #define _BL_H
11
12 /* INCLUDES ******************************************************************/
13
14 /* C Headers */
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <wchar.h>
18
19 /* NT Base Headers */
20 #include <ntifs.h>
21
22 /* NDK Headers */
23 #include <ntndk.h>
24
25 /* UEFI Headers */
26 #include <Uefi.h>
27 #include <DevicePath.h>
28 #include <LoadedImage.h>
29
30 VOID
31 EarlyPrint(_In_ PWCHAR Format, ...);
32
33 /* DEFINES *******************************************************************/
34
35 #define BL_APPLICATION_FLAG_CONVERTED_FROM_EFI 0x01
36
37 #define BL_APP_ENTRY_SIGNATURE "BTAPENT"
38
39 #define BOOT_APPLICATION_SIGNATURE_1 'TOOB'
40 #define BOOT_APPLICATION_SIGNATURE_2 ' PPA'
41
42 #define BOOT_MEMORY_TRANSLATION_TYPE_PHYSICAL 0
43 #define BOOT_MEMORY_TRANSLATION_TYPE_VIRTUAL 1
44
45 #define BOOT_APPLICATION_VERSION 2
46 #define BL_MEMORY_DATA_VERSION 1
47 #define BL_RETURN_ARGUMENTS_VERSION 1
48 #define BL_FIRMWARE_DESCRIPTOR_VERSION 2
49
50 #define BL_APPLICATION_ENTRY_FLAG_NO_GUID 0x01
51
52 #define BL_CONTEXT_PAGING_ON 1
53 #define BL_CONTEXT_INTERRUPTS_ON 2
54
55 #define BL_MM_FLAG_USE_FIRMWARE_FOR_MEMORY_MAP_BUFFERS 0x01
56 #define BL_MM_FLAG_UNKNOWN 0x02
57
58 #define BL_LIBRARY_FLAG_REINITIALIZE 0x02
59 #define BL_LIBRARY_FLAG_REINITIALIZE_ALL 0x04
60 #define BL_LIBRARY_FLAG_INITIALIZATION_COMPLETED 0x20
61
62 /* ENUMERATIONS **************************************************************/
63
64 typedef enum _BL_TRANSLATION_TYPE
65 {
66 BlNone,
67 BlVirtual,
68 BlPae,
69 BlMax
70 } BL_TRANSLATION_TYPE;
71
72 typedef enum _BL_ARCH_MODE
73 {
74 BlProtectedMode,
75 BlRealMode
76 } BL_ARCH_MODE;
77
78 //
79 // Boot Device Types
80 //
81 typedef enum _BL_DEVICE_TYPE
82 {
83 LocalDevice = 0,
84 PartitionDevice = 2,
85 UdpDevice = 4,
86 HardDiskDevice = 6
87 } BL_DEVICE_TYPE;
88
89 //
90 // Local Device Types
91 //
92 typedef enum _BL_LOCAL_DEVICE_TYPE
93 {
94 FloppyDevice = 1,
95 CdRomDevice = 2,
96 RamDiskDevice = 3,
97 } BL_LOCAL_DEVICE_TYPE;
98
99 //
100 // Partition types
101 //
102 typedef enum _BL_PARTITION_TYPE
103 {
104 GptPartition,
105 MbrPartition,
106 RawPartition,
107 } BL_PARTITION_TYPE;
108
109 //
110 // File Path Types
111 //
112 typedef enum _BL_PATH_TYPE
113 {
114 EfiPath = 4
115 } BL_PATH_TYPE;
116
117 //
118 // Classes of Memory
119 //
120 typedef enum _BL_MEMORY_CLASS
121 {
122 BlLoaderClass = 0xD,
123 BlApplicationClass,
124 BlSystemClass
125 } BL_MEMORY_CLASS;
126
127 //
128 // Types of Memory
129 //
130 typedef enum _BL_MEMORY_TYPE
131 {
132 //
133 // Loader Memory
134 //
135 BlLoaderMemory = 0xD0000002,
136 BlLoaderPageDirectory = 0xD0000006,
137 BlLoaderReferencePage = 0xD0000007,
138 BlLoaderRamDisk = 0xD0000008,
139 BlLoaderData = 0xD000000A,
140 BlLoaderSelfMap = 0xD000000F,
141
142 //
143 // Application Memory
144 //
145 BlApplicationData = 0xE0000004,
146
147 //
148 // System Memory
149 //
150 BlConventionalMemory = 0xF0000001,
151 BlUnusableMemory = 0xF0000002,
152 BlReservedMemory = 0xF0000003,
153 BlEfiBootMemory = 0xF0000004,
154 BlEfiRuntimeMemory = 0xF0000006,
155 BlAcpiReclaimMemory = 0xF0000008,
156 BlAcpiNvsMemory = 0xF0000009,
157 BlDeviceIoMemory = 0xF000000A,
158 BlDevicePortMemory = 0xF000000B,
159 BlPalMemory = 0xF000000C,
160 } BL_MEMORY_TYPE;
161
162 typedef enum _BL_MEMORY_ATTR
163 {
164 BlMemoryUncached = 1,
165 BlMemoryWriteCombined = 2,
166 BlMemoryWriteThrough = 4,
167 BlMemoryWriteBack = 8,
168 BlMemoryUncachedExported = 0x10,
169 BlMemoryWriteProtected = 0x100,
170 BlMemoryReadProtected = 0x200,
171 BlMemoryExecuteProtected = 0x400,
172 BlMemoryRuntime = 0x1000000
173 } BL_MEMORY_ATTR;
174
175 /* DATA STRUCTURES ***********************************************************/
176
177 typedef struct _BL_LIBRARY_PARAMETERS
178 {
179 ULONG LibraryFlags;
180 ULONG TranslationType;
181 ULONG MinimumAllocationCount;
182 ULONG MinimumHeapSize;
183 ULONG HeapAllocationAttributes;
184 PWCHAR ApplicationBaseDirectory;
185 ULONG DescriptorCount;
186 PWCHAR FontBaseDirectory;
187 } BL_LIBRARY_PARAMETERS, *PBL_LIBRARY_PARAMETERS;
188
189 /* This should eventually go into a more public header */
190 typedef struct _BOOT_APPLICATION_PARAMETER_BLOCK
191 {
192 /* This header tells the library what image we're dealing with */
193 ULONG Signature[2];
194 ULONG Version;
195 ULONG Size;
196 ULONG ImageType;
197 ULONG MemoryTranslationType;
198
199 /* Where is the image located */
200 ULONGLONG ImageBase;
201 ULONG ImageSize;
202
203 /* Offset to BL_MEMORY_DATA */
204 ULONG MemoryDataOffset;
205
206 /* Offset to BL_APPLICATION_ENTRY */
207 ULONG AppEntryOffset;
208
209 /* Offset to BL_DEVICE_DESCRPIPTOR */
210 ULONG BootDeviceOffset;
211
212 /* Offset to BL_FIRMWARE_PARAMETERS */
213 ULONG FirmwareParametersOffset;
214
215 /* Offset to BL_RETURN_ARGUMENTS */
216 ULONG ReturnArgumentsOffset;
217 } BOOT_APPLICATION_PARAMETER_BLOCK, *PBOOT_APPLICATION_PARAMETER_BLOCK;
218
219 typedef struct _BL_MEMORY_DATA
220 {
221 ULONG Version;
222 ULONG MdListOffset;
223 ULONG DescriptorCount;
224 ULONG DescriptorSize;
225 ULONG Unknown;
226 } BL_MEMORY_DATA, *PBL_MEMORY_DATA;
227
228 typedef struct _BL_FIRMWARE_DESCRIPTOR
229 {
230 ULONG Version;
231 ULONG Unknown;
232 EFI_HANDLE ImageHandle;
233 EFI_SYSTEM_TABLE *SystemTable;
234 } BL_FIRMWARE_DESCRIPTOR, *PBL_FIRMWARE_DESCRIPTOR;
235
236 typedef struct _BL_RETURN_ARGUMENTS
237 {
238 ULONG Version;
239 ULONG ReturnArgumentData[6];
240 } BL_RETURN_ARGUMENTS, *PBL_RETURN_ARGUMENTS;
241
242 typedef struct _BL_MEMORY_DESCRIPTOR
243 {
244 LIST_ENTRY ListEntry;
245 union
246 {
247 struct
248 {
249 ULONGLONG BasePage;
250 ULONGLONG VirtualPage;
251 };
252 struct
253 {
254 ULONGLONG BaseAddress;
255 ULONGLONG VirtualAddress;
256 };
257 };
258 ULONGLONG PageCount;
259 ULONG Flags;
260 BL_MEMORY_TYPE Type;
261 } BL_MEMORY_DESCRIPTOR, *PBL_MEMORY_DESCRIPTOR;
262
263 typedef struct _BL_BCD_OPTION
264 {
265 ULONG Type;
266 ULONG DataOffset;
267 ULONG DataSize;
268 ULONG ListOffset;
269 ULONG NextEntryOffset;
270 ULONG Empty;
271 } BL_BCD_OPTION, *PBL_BCD_OPTION;
272
273 typedef struct _BL_APPLICATION_ENTRY
274 {
275 CHAR Signature[8];
276 ULONG Flags;
277 GUID Guid;
278 ULONG Unknown[4];
279 BL_BCD_OPTION BcdData;
280 } BL_APPLICATION_ENTRY, *PBL_APPLICATION_ENTRY;
281
282 typedef struct _BL_HARDDISK_DEVICE
283 {
284 ULONG PartitionType;
285 union
286 {
287 struct
288 {
289 ULONG PartitionSignature;
290 } Mbr;
291
292 struct
293 {
294 GUID PartitionSignature;
295 } Gpt;
296
297 struct
298 {
299 ULONG DiskNumber;
300 } Raw;
301 };
302 } BL_HARDDISK_DEVICE;
303
304 typedef struct _BL_LOCAL_DEVICE
305 {
306 ULONG Type;
307 union
308 {
309 struct
310 {
311 ULONG DriveNumber;
312 } FloppyDisk;
313
314 BL_HARDDISK_DEVICE HardDisk;
315
316 struct
317 {
318 PHYSICAL_ADDRESS ImageBase;
319 LARGE_INTEGER ImageSize;
320 ULONG ImageOffset;
321 } RamDisk;
322 };
323 } BL_LOCAL_DEVICE;
324
325 typedef struct _BL_DEVICE_DESCRIPTOR
326 {
327 ULONG Size;
328 ULONG Flags;
329 DEVICE_TYPE DeviceType;
330 ULONG Unknown;
331 union
332 {
333 BL_LOCAL_DEVICE Local;
334
335 struct
336 {
337 ULONG Unknown;
338 } Remote;
339
340 struct
341 {
342 union
343 {
344 ULONG PartitionNumber;
345 } Mbr;
346
347 union
348 {
349 GUID PartitionGuid;
350 } Gpt;
351
352 BL_LOCAL_DEVICE Disk;
353 } Partition;
354 };
355 } BL_DEVICE_DESCRIPTOR, *PBL_DEVICE_DESCRIPTOR;
356
357 typedef struct _BL_FILE_PATH_DESCRIPTOR
358 {
359 ULONG Version;
360 ULONG Length;
361 ULONG PathType;
362 UCHAR Path[ANYSIZE_ARRAY];
363 } BL_FILE_PATH_DESCRIPTOR, *PBL_FILE_PATH_DESCRIPTOR;
364
365 typedef struct _BL_WINDOWS_LOAD_OPTIONS
366 {
367 CHAR Signature[8];
368 ULONG Version;
369 ULONG Length;
370 ULONG OsPathOffset;
371 WCHAR LoadOptions[ANYSIZE_ARRAY];
372 } BL_WINDOWS_LOAD_OPTIONS, *PBL_WINDOWS_LOAD_OPTIONS;
373
374 typedef struct _BL_ARCH_CONTEXT
375 {
376 BL_ARCH_MODE Mode;
377 BL_TRANSLATION_TYPE TranslationType;
378 ULONG ContextFlags;
379 } BL_ARCH_CONTEXT, *PBL_ARCH_CONTEXT;
380
381 typedef struct _BL_MEMORY_DESCRIPTOR_LIST
382 {
383 LIST_ENTRY ListHead;
384 PLIST_ENTRY First;
385 PLIST_ENTRY This;
386 ULONG Type;
387 } BL_MEMORY_DESCRIPTOR_LIST, *PBL_MEMORY_DESCRIPTOR_LIST;
388
389 /* INLINE ROUTINES ***********************************************************/
390
391 FORCEINLINE
392 VOID
393 BlSetupDefaultParameters (
394 _Out_ PBL_LIBRARY_PARAMETERS LibraryParameters
395 )
396 {
397 BL_LIBRARY_PARAMETERS DefaultParameters =
398 {
399 0x20,
400 BlVirtual,
401 1024,
402 2 * 1024 * 1024,
403 0,
404 NULL,
405 0,
406 NULL
407 };
408
409 /* Copy the defaults */
410 RtlCopyMemory(LibraryParameters, &DefaultParameters, sizeof(*LibraryParameters));
411 }
412
413 FORCEINLINE
414 VOID
415 MmMdInitializeListHead (
416 _In_ PBL_MEMORY_DESCRIPTOR_LIST List
417 )
418 {
419 /* Initialize the list */
420 InitializeListHead(&List->ListHead);
421 List->First = &List->ListHead;
422 List->This = NULL;
423 }
424
425 /* INITIALIZATION ROUTINES ***************************************************/
426
427 NTSTATUS
428 BlInitializeLibrary(
429 _In_ PBOOT_APPLICATION_PARAMETER_BLOCK BootAppParameters,
430 _In_ PBL_LIBRARY_PARAMETERS LibraryParameters
431 );
432
433 NTSTATUS
434 BlpArchInitialize (
435 _In_ ULONG Phase
436 );
437
438 NTSTATUS
439 BlpFwInitialize (
440 _In_ ULONG Phase,
441 _In_ PBL_FIRMWARE_DESCRIPTOR FirmwareParameters
442 );
443
444 NTSTATUS
445 BlpMmInitialize (
446 _In_ PBL_MEMORY_DATA MemoryData,
447 _In_ BL_TRANSLATION_TYPE TranslationType,
448 _In_ PBL_LIBRARY_PARAMETERS LibraryParameters
449 );
450
451 /* UTILITY ROUTINES **********************************************************/
452
453 EFI_STATUS
454 EfiGetEfiStatusCode(
455 _In_ NTSTATUS Status
456 );
457
458 NTSTATUS
459 EfiGetNtStatusCode (
460 _In_ EFI_STATUS EfiStatus
461 );
462
463 /* BCD ROUTINES **************************************************************/
464
465 ULONG
466 BlGetBootOptionSize (
467 _In_ PBL_BCD_OPTION BcdOption
468 );
469
470 /* CONTEXT ROUTINES **********************************************************/
471
472 VOID
473 BlpArchSwitchContext (
474 _In_ BL_ARCH_MODE NewMode
475 );
476
477 /* MEMORY MANAGER ROUTINES ***************************************************/
478
479 NTSTATUS
480 MmBaInitialize (
481 VOID
482 );
483
484 NTSTATUS
485 MmPaInitialize (
486 _In_ PBL_MEMORY_DATA MemoryData,
487 _In_ ULONG MinimumPages
488 );
489
490 NTSTATUS
491 MmArchInitialize (
492 _In_ ULONG Phase,
493 _In_ PBL_MEMORY_DATA MemoryData,
494 _In_ BL_TRANSLATION_TYPE TranslationType,
495 _In_ BL_TRANSLATION_TYPE LibraryTranslationType
496 );
497
498 NTSTATUS
499 MmHaInitialize (
500 _In_ ULONG HeapSize,
501 _In_ ULONG HeapAttributes
502 );
503
504 VOID
505 MmMdInitialize (
506 _In_ ULONG Phase,
507 _In_ PBL_LIBRARY_PARAMETERS LibraryParameters
508 );
509
510 NTSTATUS
511 MmFwGetMemoryMap (
512 _Out_ PBL_MEMORY_DESCRIPTOR_LIST MemoryMap,
513 _In_ ULONG Flags
514 );
515
516 VOID
517 MmMdFreeList(
518 _In_ PBL_MEMORY_DESCRIPTOR_LIST MdList
519 );
520
521 extern ULONG MmDescriptorCallTreeCount;
522 extern ULONG BlpApplicationFlags;
523 extern BL_LIBRARY_PARAMETERS BlpLibraryParameters;
524 extern BL_TRANSLATION_TYPE MmTranslationType;
525 extern PBL_ARCH_CONTEXT CurrentExecutionContext;
526
527 #endif