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