2 * COPYRIGHT: See COPYING.ARM in the top level directory
3 * PROJECT: ReactOS UEFI Boot Library
4 * FILE: boot/environ/lib/firmware/efi/firmware.c
5 * PURPOSE: Boot Library Firmware Initialization for EFI
6 * PROGRAMMER: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES ******************************************************************/
13 /* DATA VARIABLES ************************************************************/
15 GUID EfiSimpleTextInputExProtocol
= EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID
;
17 PBL_FIRMWARE_DESCRIPTOR EfiFirmwareParameters
;
18 BL_FIRMWARE_DESCRIPTOR EfiFirmwareData
;
19 EFI_HANDLE EfiImageHandle
;
20 EFI_SYSTEM_TABLE
* EfiSystemTable
;
22 EFI_SYSTEM_TABLE
*EfiST
;
23 EFI_BOOT_SERVICES
*EfiBS
;
24 EFI_RUNTIME_SERVICES
*EfiRT
;
25 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*EfiConOut
;
26 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*EfiConIn
;
27 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*EfiConInEx
;
29 /* FUNCTIONS *****************************************************************/
33 _In_ EFI_HANDLE Handle
,
34 _In_ EFI_GUID
*Protocol
,
35 _Out_ PVOID
* Interface
42 /* Are we using virtual memory/ */
43 if (MmTranslationType
!= BlNone
)
45 /* We need complex tracking to make this work */
46 //Status = EfiVmOpenProtocol(Handle, Protocol, Interface);
47 Status
= STATUS_NOT_SUPPORTED
;
51 /* Are we in protected mode? */
52 OldMode
= CurrentExecutionContext
->Mode
;
53 if (OldMode
!= BlRealMode
)
55 /* FIXME: Not yet implemented */
56 return STATUS_NOT_IMPLEMENTED
;
59 /* Are we on legacy 1.02? */
60 if (EfiST
->FirmwareRevision
== EFI_1_02_SYSTEM_TABLE_REVISION
)
62 /* Make the legacy call */
63 EfiStatus
= EfiBS
->HandleProtocol(Handle
, Protocol
, Interface
);
67 /* Use the UEFI version */
68 EfiStatus
= EfiBS
->OpenProtocol(Handle
,
73 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
75 /* Switch back to protected mode if we came from there */
76 if (OldMode
!= BlRealMode
)
78 BlpArchSwitchContext(OldMode
);
82 /* Convert the error to an NTSTATUS */
83 Status
= EfiGetNtStatusCode(EfiStatus
);
86 /* Clear the interface on failure, and return the status */
87 if (!NT_SUCCESS(Status
))
97 _In_ EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*ConInEx
,
98 _In_ EFI_KEY_TOGGLE_STATE
* KeyToggleState
101 BL_ARCH_MODE OldMode
;
102 EFI_STATUS EfiStatus
;
104 /* Are we in protected mode? */
105 OldMode
= CurrentExecutionContext
->Mode
;
106 if (OldMode
!= BlRealMode
)
108 /* FIXME: Not yet implemented */
109 return STATUS_NOT_IMPLEMENTED
;
112 /* Make the EFI call */
113 EfiStatus
= ConInEx
->SetState(ConInEx
, KeyToggleState
);
115 /* Switch back to protected mode if we came from there */
116 if (OldMode
!= BlRealMode
)
118 BlpArchSwitchContext(OldMode
);
121 /* Convert the error to an NTSTATUS */
122 return EfiGetNtStatusCode(EfiStatus
);
126 EfiSetWatchdogTimer (
130 BL_ARCH_MODE OldMode
;
131 EFI_STATUS EfiStatus
;
133 /* Are we in protected mode? */
134 OldMode
= CurrentExecutionContext
->Mode
;
135 if (OldMode
!= BlRealMode
)
137 /* FIXME: Not yet implemented */
138 return STATUS_NOT_IMPLEMENTED
;
141 /* Make the EFI call */
142 EfiStatus
= EfiBS
->SetWatchdogTimer(0, 0, 0, NULL
);
144 /* Switch back to protected mode if we came from there */
145 if (OldMode
!= BlRealMode
)
147 BlpArchSwitchContext(OldMode
);
150 /* Convert the error to an NTSTATUS */
151 return EfiGetNtStatusCode(EfiStatus
);
156 _Out_ UINTN
* MemoryMapSize
,
157 _Inout_ EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
159 _Out_ UINTN
* DescriptorSize
,
160 _Out_ UINTN
* DescriptorVersion
163 BL_ARCH_MODE OldMode
;
164 EFI_STATUS EfiStatus
;
166 /* Are we in protected mode? */
167 OldMode
= CurrentExecutionContext
->Mode
;
168 if (OldMode
!= BlRealMode
)
170 /* FIXME: Not yet implemented */
171 return STATUS_NOT_IMPLEMENTED
;
174 /* Make the EFI call */
175 EfiStatus
= EfiBS
->GetMemoryMap(MemoryMapSize
,
181 /* Switch back to protected mode if we came from there */
182 if (OldMode
!= BlRealMode
)
184 BlpArchSwitchContext(OldMode
);
187 /* Convert the error to an NTSTATUS */
188 return EfiGetNtStatusCode(EfiStatus
);
194 _In_ EFI_PHYSICAL_ADDRESS PhysicalAddress
197 BL_ARCH_MODE OldMode
;
198 EFI_STATUS EfiStatus
;
200 /* Are we in protected mode? */
201 OldMode
= CurrentExecutionContext
->Mode
;
202 if (OldMode
!= BlRealMode
)
204 /* FIXME: Not yet implemented */
205 return STATUS_NOT_IMPLEMENTED
;
208 /* Make the EFI call */
209 EfiStatus
= EfiBS
->FreePages(PhysicalAddress
, Pages
);
211 /* Switch back to protected mode if we came from there */
212 if (OldMode
!= BlRealMode
)
214 BlpArchSwitchContext(OldMode
);
217 /* Convert the error to an NTSTATUS */
218 return EfiGetNtStatusCode(EfiStatus
);
225 _Inout_ EFI_PHYSICAL_ADDRESS
* Memory
228 BL_ARCH_MODE OldMode
;
229 EFI_STATUS EfiStatus
;
231 /* Are we in protected mode? */
232 OldMode
= CurrentExecutionContext
->Mode
;
233 if (OldMode
!= BlRealMode
)
235 /* FIXME: Not yet implemented */
236 return STATUS_NOT_IMPLEMENTED
;
239 /* Make the EFI call */
240 EfiStatus
= EfiBS
->AllocatePages(Type
, EfiLoaderData
, Pages
, Memory
);
242 /* Switch back to protected mode if we came from there */
243 if (OldMode
!= BlRealMode
)
245 BlpArchSwitchContext(OldMode
);
248 /* Convert the error to an NTSTATUS */
249 return EfiGetNtStatusCode(EfiStatus
);
253 MmFwpGetOsAttributeType (
254 _In_ ULONGLONG Attribute
257 BL_MEMORY_ATTR OsAttribute
= 0;
259 if (Attribute
& EFI_MEMORY_UC
)
261 OsAttribute
= BlMemoryUncached
;
264 if (Attribute
& EFI_MEMORY_WC
)
266 OsAttribute
|= BlMemoryWriteCombined
;
269 if (Attribute
& EFI_MEMORY_WT
)
271 OsAttribute
|= BlMemoryWriteThrough
;
274 if (Attribute
& EFI_MEMORY_WB
)
276 OsAttribute
|= BlMemoryWriteBack
;
279 if (Attribute
& EFI_MEMORY_UCE
)
281 OsAttribute
|= BlMemoryUncachedExported
;
284 if (Attribute
& EFI_MEMORY_WP
)
286 OsAttribute
|= BlMemoryWriteProtected
;
289 if (Attribute
& EFI_MEMORY_RP
)
291 OsAttribute
|= BlMemoryReadProtected
;
294 if (Attribute
& EFI_MEMORY_XP
)
296 OsAttribute
|= BlMemoryExecuteProtected
;
299 if (Attribute
& EFI_MEMORY_RUNTIME
)
301 OsAttribute
|= BlMemoryRuntime
;
308 MmFwpGetOsMemoryType (
309 _In_ EFI_MEMORY_TYPE MemoryType
312 BL_MEMORY_TYPE OsType
;
318 OsType
= BlLoaderMemory
;
321 case EfiBootServicesCode
:
322 case EfiBootServicesData
:
323 OsType
= BlEfiBootMemory
;
326 case EfiRuntimeServicesCode
:
327 case EfiRuntimeServicesData
:
328 OsType
= BlEfiRuntimeMemory
;
331 case EfiConventionalMemory
:
332 OsType
= BlConventionalMemory
;
335 case EfiUnusableMemory
:
336 OsType
= BlUnusableMemory
;
339 case EfiACPIReclaimMemory
:
340 OsType
= BlAcpiReclaimMemory
;
343 case EfiACPIMemoryNVS
:
344 OsType
= BlAcpiNvsMemory
;
347 case EfiMemoryMappedIO
:
348 OsType
= BlDeviceIoMemory
;
351 case EfiMemoryMappedIOPortSpace
:
352 OsType
= BlDevicePortMemory
;
356 OsType
= BlPalMemory
;
360 OsType
= BlReservedMemory
;
369 _Out_ PBL_MEMORY_DESCRIPTOR_LIST MemoryMap
,
373 BL_LIBRARY_PARAMETERS LibraryParameters
= BlpLibraryParameters
;
374 BOOLEAN UseEfiBuffer
, HaveRamDisk
;
376 ULONGLONG Pages
, StartPage
, EndPage
;
377 UINTN EfiMemoryMapSize
, MapKey
, DescriptorSize
, DescriptorVersion
;
378 EFI_PHYSICAL_ADDRESS EfiBuffer
;
379 EFI_MEMORY_DESCRIPTOR
* EfiMemoryMap
;
380 EFI_STATUS EfiStatus
;
381 BL_ARCH_MODE OldMode
;
382 EFI_MEMORY_DESCRIPTOR EfiDescriptor
;
383 BL_MEMORY_TYPE MemoryType
;
384 PBL_MEMORY_DESCRIPTOR Descriptor
;
385 BL_MEMORY_ATTR Attribute
;
387 /* Initialize EFI memory map attributes */
388 EfiMemoryMapSize
= MapKey
= DescriptorSize
= DescriptorVersion
= 0;
390 /* Increment the nesting depth */
391 MmDescriptorCallTreeCount
++;
393 /* Determine if we should use EFI or our own allocator at this point */
394 UseEfiBuffer
= Flags
& BL_MM_FLAG_USE_FIRMWARE_FOR_MEMORY_MAP_BUFFERS
;
395 if (!(LibraryParameters
.LibraryFlags
& BL_LIBRARY_FLAG_INITIALIZATION_COMPLETED
))
400 /* Bail out if we don't have a list to use */
401 if (MemoryMap
== NULL
)
403 Status
= STATUS_INVALID_PARAMETER
;
407 /* Free the current descriptor list */
408 MmMdFreeList(MemoryMap
);
410 /* Call into EFI to get the size of the memory map */
411 Status
= EfiGetMemoryMap(&EfiMemoryMapSize
,
416 if (Status
!= STATUS_BUFFER_TOO_SMALL
)
418 /* This should've failed because our buffer was too small, nothing else */
419 EarlyPrint(L
"Got strange EFI status for memory map: %lx\n", Status
);
420 if (NT_SUCCESS(Status
))
422 Status
= STATUS_UNSUCCESSFUL
;
427 /* Add 4 more descriptors just in case things changed */
428 EfiMemoryMapSize
+= (4 * DescriptorSize
);
429 Pages
= BYTES_TO_PAGES(EfiMemoryMapSize
);
430 EarlyPrint(L
"Memory map size: %lx bytes, %d pages\n", EfiMemoryMapSize
, Pages
);
432 /* Should we use EFI to grab memory? */
435 /* Yes -- request one more page to align up correctly */
438 /* Grab the required pages */
439 Status
= EfiAllocatePages(AllocateAnyPages
,
442 if (!NT_SUCCESS(Status
))
444 EarlyPrint(L
"EFI allocation failed: %lx\n", Status
);
448 /* Free the pages for now */
449 Status
= EfiFreePages(Pages
, EfiBuffer
);
450 if (!NT_SUCCESS(Status
))
456 /* Now round to the actual buffer size, removing the extra page */
457 EfiBuffer
= ROUND_TO_PAGES(EfiBuffer
);
459 Status
= EfiAllocatePages(AllocateAddress
,
462 if (!NT_SUCCESS(Status
))
468 /* Get the final aligned size and proper buffer */
469 EfiMemoryMapSize
= EFI_PAGES_TO_SIZE(Pages
);
470 EfiMemoryMap
= (EFI_MEMORY_DESCRIPTOR
*)(ULONG_PTR
)EfiBuffer
;
472 /* Switch to real mode if not already in it */
473 OldMode
= CurrentExecutionContext
->Mode
;
474 if (OldMode
!= BlRealMode
)
476 BlpArchSwitchContext(BlRealMode
);
479 /* Call EFI to get the memory map */
480 EfiStatus
= EfiBS
->GetMemoryMap(&EfiMemoryMapSize
,
486 /* Switch back into the previous mode */
487 if (OldMode
!= BlRealMode
)
489 BlpArchSwitchContext(OldMode
);
492 /* Convert the result code */
493 Status
= EfiGetNtStatusCode(EfiStatus
);
497 /* We don't support this path yet */
498 Status
= STATUS_NOT_IMPLEMENTED
;
501 /* So far so good? */
502 if (!NT_SUCCESS(Status
))
504 EarlyPrint(L
"Failed to get EFI memory map: %lx\n", Status
);
508 /* Did we get correct data from firmware? */
509 if (((EfiMemoryMapSize
% DescriptorSize
)) ||
510 (DescriptorSize
< sizeof(EFI_MEMORY_DESCRIPTOR
)))
512 EarlyPrint(L
"Incorrect descriptor size\n");
513 Status
= STATUS_UNSUCCESSFUL
;
517 /* Did we boot from a RAM disk? */
518 if ((BlpBootDevice
->DeviceType
== LocalDevice
) &&
519 (BlpBootDevice
->Local
.Type
== RamDiskDevice
))
521 /* We don't handle this yet */
522 EarlyPrint(L
"RAM boot not supported\n");
523 Status
= STATUS_NOT_IMPLEMENTED
;
528 /* We didn't, so there won't be any need to find the memory descriptor */
532 /* Loop the EFI memory map */
534 EarlyPrint(L
"UEFI MEMORY MAP\n\n");
535 EarlyPrint(L
"TYPE START END ATTRIBUTES\n");
536 EarlyPrint(L
"===============================================================\n");
538 while (EfiMemoryMapSize
!= 0)
540 /* Check if this is an EFI buffer, but we're not in real mode */
541 if ((UseEfiBuffer
) && (OldMode
!= BlRealMode
))
543 BlpArchSwitchContext(BlRealMode
);
546 /* Capture it so we can go back to protected mode (if possible) */
547 EfiDescriptor
= *EfiMemoryMap
;
549 /* Go back to protected mode, if we had switched */
550 if ((UseEfiBuffer
) && (OldMode
!= BlRealMode
))
552 BlpArchSwitchContext(OldMode
);
555 /* Convert to OS memory type */
556 MemoryType
= MmFwpGetOsMemoryType(EfiDescriptor
.Type
);
558 /* Round up or round down depending on where the memory is coming from */
559 if (MemoryType
== BlConventionalMemory
)
561 StartPage
= BYTES_TO_PAGES(EfiDescriptor
.PhysicalStart
);
565 StartPage
= EfiDescriptor
.PhysicalStart
>> PAGE_SHIFT
;
568 /* Calculate the ending page */
569 EndPage
= StartPage
+ EfiDescriptor
.NumberOfPages
;
571 /* If after rounding, we ended up with 0 pages, skip this */
572 if (StartPage
== EndPage
)
577 EarlyPrint(L
"%08X 0x%016I64X-0x%016I64X 0x%I64X\n",
579 StartPage
<< PAGE_SHIFT
,
580 EndPage
<< PAGE_SHIFT
,
581 EfiDescriptor
.Attribute
);
583 /* Check for any range of memory below 1MB */
584 if (StartPage
< 0x100)
586 /* Does this range actually contain NULL? */
589 /* Manually create a reserved descriptof for this page */
590 Attribute
= MmFwpGetOsAttributeType(EfiDescriptor
.Attribute
);
591 Descriptor
= MmMdInitByteGranularDescriptor(Attribute
,
598 Status
= STATUS_INSUFFICIENT_RESOURCES
;
602 /* Add this descriptor into the list */
603 Status
= MmMdAddDescriptorToList(MemoryMap
,
605 BL_MM_ADD_DESCRIPTOR_TRUNCATE_FLAG
);
606 if (!NT_SUCCESS(Status
))
608 EarlyPrint(L
"Failed to add zero page descriptor: %lx\n", Status
);
612 /* Now handle the rest of the range, unless this was it */
620 /* Does the range go beyond 1MB? */
623 /* Then create the descriptor for everything up until the megabyte */
624 Attribute
= MmFwpGetOsAttributeType(EfiDescriptor
.Attribute
);
625 Descriptor
= MmMdInitByteGranularDescriptor(Attribute
,
632 Status
= STATUS_INSUFFICIENT_RESOURCES
;
636 /* Check if this region is currently free RAM */
637 if (Descriptor
->Type
== BlConventionalMemory
)
639 /* Set an unknown flag on the descriptor */
640 Descriptor
->Flags
|= 0x80000;
643 /* Add this descriptor into the list */
644 Status
= MmMdAddDescriptorToList(MemoryMap
,
646 BL_MM_ADD_DESCRIPTOR_TRUNCATE_FLAG
);
647 if (!NT_SUCCESS(Status
))
649 EarlyPrint(L
"Failed to add 1MB descriptor: %lx\n", Status
);
653 /* Now handle the rest of the range above 1MB */
658 /* Check if we loaded from a RAM disk */
661 /* We don't handle this yet */
662 EarlyPrint(L
"RAM boot not supported\n");
663 Status
= STATUS_NOT_IMPLEMENTED
;
667 /* Create a descriptor for the current range */
668 Attribute
= MmFwpGetOsAttributeType(EfiDescriptor
.Attribute
);
669 Descriptor
= MmMdInitByteGranularDescriptor(Attribute
,
673 EndPage
- StartPage
);
676 Status
= STATUS_INSUFFICIENT_RESOURCES
;
680 /* Check if this region is currently free RAM below 1MB */
681 if ((Descriptor
->Type
== BlConventionalMemory
) && (EndPage
<= 0x100))
683 /* Set an unknown flag on the descriptor */
684 Descriptor
->Flags
|= 0x80000;
687 /* Add the descriptor to the list, requesting coalescing as asked */
688 Status
= MmMdAddDescriptorToList(MemoryMap
,
690 BL_MM_ADD_DESCRIPTOR_TRUNCATE_FLAG
|
691 (Flags
& BL_MM_FLAG_REQUEST_COALESCING
) ?
692 BL_MM_ADD_DESCRIPTOR_COALESCE_FLAG
: 0);
693 if (!NT_SUCCESS(Status
))
695 EarlyPrint(L
"Failed to add full descriptor: %lx\n", Status
);
700 /* Consume this descriptor, and move to the next one */
701 EfiMemoryMapSize
-= DescriptorSize
;
702 EfiMemoryMap
= (PVOID
)((ULONG_PTR
)EfiMemoryMap
+ DescriptorSize
);
705 /* FIXME: @TODO: Mark the EfiBuffer as free, since we're about to free it */
706 /* For now, just "leak" the 1-2 pages... */
709 /* Free the EFI buffer, if we had one */
712 EfiFreePages(Pages
, EfiBuffer
);
715 /* On failure, free the memory map if one was passed in */
716 if (!NT_SUCCESS(Status
) && (MemoryMap
!= NULL
))
718 MmMdFreeList(MemoryMap
);
721 /* Decrement the nesting depth and return */
722 MmDescriptorCallTreeCount
--;
729 _In_ PBL_FIRMWARE_DESCRIPTOR FirmwareData
732 NTSTATUS Status
= STATUS_SUCCESS
;
733 EFI_KEY_TOGGLE_STATE KeyToggleState
;
735 /* Check if we have vaild firmware data */
736 if (!(FirmwareData
) || !(FirmwareData
->Version
))
738 return STATUS_INVALID_PARAMETER
;
741 /* Check which boot phase we're in */
744 /* Memory manager is ready, open the extended input protocol */
745 Status
= EfiOpenProtocol(EfiST
->ConsoleInHandle
,
746 &EfiSimpleTextInputExProtocol
,
747 (PVOID
*)&EfiConInEx
);
748 if (NT_SUCCESS(Status
))
750 /* Set the initial key toggle state */
751 KeyToggleState
= EFI_TOGGLE_STATE_VALID
| 40;
752 EfiConInExSetState(EfiST
->ConsoleInHandle
, &KeyToggleState
);
755 /* Setup the watchdog timer */
756 EfiSetWatchdogTimer();
760 /* Make a copy of the parameters */
761 EfiFirmwareParameters
= &EfiFirmwareData
;
763 /* Check which version we received */
764 if (FirmwareData
->Version
== 1)
766 /* FIXME: Not supported */
767 Status
= STATUS_NOT_SUPPORTED
;
769 else if (FirmwareData
->Version
>= 2)
771 /* Version 2 -- save the data */
772 EfiFirmwareData
= *FirmwareData
;
773 EfiSystemTable
= FirmwareData
->SystemTable
;
774 EfiImageHandle
= FirmwareData
->ImageHandle
;
776 /* Set the EDK-II style variables as well */
777 EfiST
= EfiSystemTable
;
778 EfiBS
= EfiSystemTable
->BootServices
;
779 EfiRT
= EfiSystemTable
->RuntimeServices
;
780 EfiConOut
= EfiSystemTable
->ConOut
;
781 EfiConIn
= EfiSystemTable
->ConIn
;
786 /* Unknown version */
787 Status
= STATUS_NOT_SUPPORTED
;
791 /* Return the initialization state */