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
;
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
;
385 /* Increment the nesting depth */
386 MmDescriptorCallTreeCount
++;
388 /* Determine if we should use EFI or our own allocator at this point */
389 UseEfiBuffer
= Flags
& BL_MM_FLAG_USE_FIRMWARE_FOR_MEMORY_MAP_BUFFERS
;
390 if (!(LibraryParameters
.LibraryFlags
& BL_LIBRARY_FLAG_INITIALIZATION_COMPLETED
))
395 /* Bail out if we don't have a list to use */
396 if (MemoryMap
== NULL
)
398 Status
= STATUS_INVALID_PARAMETER
;
402 /* Free the current descriptor list */
403 MmMdFreeList(MemoryMap
);
405 /* Call into EFI to get the size of the memory map */
406 Status
= EfiGetMemoryMap(&EfiMemoryMapSize
,
411 if (Status
!= STATUS_BUFFER_TOO_SMALL
)
413 /* This should've failed because our buffer was too small, nothing else */
414 EarlyPrint(L
"Got strange EFI status for memory map: %lx\n", Status
);
415 if (NT_SUCCESS(Status
))
417 Status
= STATUS_UNSUCCESSFUL
;
422 /* Add 4 more descriptors just in case things changed */
423 EfiMemoryMapSize
+= (4 * DescriptorSize
);
424 Pages
= BYTES_TO_PAGES(EfiMemoryMapSize
);
425 EarlyPrint(L
"Memory map size: %lx bytes, %d pages\n", EfiMemoryMapSize
, Pages
);
427 /* Should we use EFI to grab memory? */
430 /* Yes -- request one more page to align up correctly */
433 /* Grab the required pages */
434 Status
= EfiAllocatePages(AllocateAnyPages
,
437 if (!NT_SUCCESS(Status
))
439 EarlyPrint(L
"EFI allocation failed: %lx\n", Status
);
443 /* Free the pages for now */
444 Status
= EfiFreePages(Pages
, EfiBuffer
);
445 if (!NT_SUCCESS(Status
))
451 /* Now round to the actual buffer size, removing the extra page */
452 EfiBuffer
= ROUND_TO_PAGES(EfiBuffer
);
454 Status
= EfiAllocatePages(AllocateAddress
,
457 if (!NT_SUCCESS(Status
))
463 /* Get the final aligned size and proper buffer */
464 EfiMemoryMapSize
= EFI_PAGES_TO_SIZE(Pages
);
465 EfiMemoryMap
= (EFI_MEMORY_DESCRIPTOR
*)(ULONG_PTR
)EfiBuffer
;
467 /* Switch to real mode if not already in it */
468 OldMode
= CurrentExecutionContext
->Mode
;
469 if (OldMode
!= BlRealMode
)
471 BlpArchSwitchContext(BlRealMode
);
474 /* Call EFI to get the memory map */
475 EfiStatus
= EfiBS
->GetMemoryMap(&EfiMemoryMapSize
,
481 /* Switch back into the previous mode */
482 if (OldMode
!= BlRealMode
)
484 BlpArchSwitchContext(OldMode
);
487 /* Convert the result code */
488 Status
= EfiGetNtStatusCode(EfiStatus
);
492 /* We don't support this path yet */
493 Status
= STATUS_NOT_IMPLEMENTED
;
496 /* So far so good? */
497 if (!NT_SUCCESS(Status
))
499 EarlyPrint(L
"Failed to get EFI memory map: %lx\n", Status
);
503 /* Did we get correct data from firmware? */
504 if (((EfiMemoryMapSize
% DescriptorSize
)) ||
505 (DescriptorSize
< sizeof(EFI_MEMORY_DESCRIPTOR
)))
507 EarlyPrint(L
"Incorrect descriptor size\n");
508 Status
= STATUS_UNSUCCESSFUL
;
512 /* Loop the EFI memory map */
513 EarlyPrint(L
"UEFI MEMORY MAP\n\n");
514 EarlyPrint(L
"TYPE START END ATTRIBUTES\n");
515 EarlyPrint(L
"===============================================================\n");
516 while (EfiMemoryMapSize
!= 0)
518 /* Check if this is an EFI buffer, but we're not in real mode */
519 if ((UseEfiBuffer
) && (OldMode
!= BlRealMode
))
521 BlpArchSwitchContext(BlRealMode
);
524 /* Capture it so we can go back to protected mode (if possible) */
525 EfiDescriptor
= *EfiMemoryMap
;
527 /* Go back to protected mode, if we had switched */
528 if ((UseEfiBuffer
) && (OldMode
!= BlRealMode
))
530 BlpArchSwitchContext(OldMode
);
533 /* Convert to OS memory type */
534 MemoryType
= MmFwpGetOsMemoryType(EfiDescriptor
.Type
);
536 /* Round up or round down depending on where the memory is coming from */
537 if (MemoryType
== BlConventionalMemory
)
539 StartPage
= BYTES_TO_PAGES(EfiDescriptor
.PhysicalStart
);
543 StartPage
= EfiDescriptor
.PhysicalStart
>> PAGE_SHIFT
;
546 /* Calculate the ending page */
547 EndPage
= StartPage
+ EfiDescriptor
.NumberOfPages
;
549 /* If after rounding, we ended up with 0 pages, skip this */
550 if (StartPage
== EndPage
)
555 EarlyPrint(L
"%08X 0x%016I64X-0x%016I64X 0x%X\n",
557 StartPage
<< PAGE_SHIFT
,
558 EndPage
<< PAGE_SHIFT
,
559 EfiDescriptor
.Attribute
);
561 /* Consume this descriptor, and move to the next one */
563 EfiMemoryMapSize
-= DescriptorSize
;
564 EfiMemoryMap
= (PVOID
)((ULONG_PTR
)EfiMemoryMap
+ DescriptorSize
);
568 /* Free the EFI buffer, if we had one */
571 EfiFreePages(Pages
, EfiBuffer
);
574 /* On failure, free the memory map if one was passed in */
575 if (!NT_SUCCESS(Status
) && (MemoryMap
!= NULL
))
577 MmMdFreeList(MemoryMap
);
580 /* Decrement the nesting depth and return */
581 MmDescriptorCallTreeCount
--;
588 _In_ PBL_FIRMWARE_DESCRIPTOR FirmwareData
591 NTSTATUS Status
= STATUS_SUCCESS
;
592 EFI_KEY_TOGGLE_STATE KeyToggleState
;
594 /* Check if we have vaild firmware data */
595 if (!(FirmwareData
) || !(FirmwareData
->Version
))
597 return STATUS_INVALID_PARAMETER
;
600 /* Check which boot phase we're in */
603 /* Memory manager is ready, open the extended input protocol */
604 Status
= EfiOpenProtocol(EfiST
->ConsoleInHandle
,
605 &EfiSimpleTextInputExProtocol
,
606 (PVOID
*)&EfiConInEx
);
607 if (NT_SUCCESS(Status
))
609 /* Set the initial key toggle state */
610 KeyToggleState
= EFI_TOGGLE_STATE_VALID
| 40;
611 EfiConInExSetState(EfiST
->ConsoleInHandle
, &KeyToggleState
);
614 /* Setup the watchdog timer */
615 EfiSetWatchdogTimer();
619 /* Make a copy of the parameters */
620 EfiFirmwareParameters
= &EfiFirmwareData
;
622 /* Check which version we received */
623 if (FirmwareData
->Version
== 1)
625 /* FIXME: Not supported */
626 Status
= STATUS_NOT_SUPPORTED
;
628 else if (FirmwareData
->Version
>= 2)
630 /* Version 2 -- save the data */
631 EfiFirmwareData
= *FirmwareData
;
632 EfiSystemTable
= FirmwareData
->SystemTable
;
633 EfiImageHandle
= FirmwareData
->ImageHandle
;
635 /* Set the EDK-II style variables as well */
636 EfiST
= EfiSystemTable
;
637 EfiBS
= EfiSystemTable
->BootServices
;
638 EfiRT
= EfiSystemTable
->RuntimeServices
;
639 EfiConOut
= EfiSystemTable
->ConOut
;
640 EfiConIn
= EfiSystemTable
->ConIn
;
645 /* Unknown version */
646 Status
= STATUS_NOT_SUPPORTED
;
650 /* Return the initialization state */