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