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