3 * Copyright (c) 2011, ARM Limited. All rights reserved.
5 * SPDX-License-Identifier: BSD-2-Clause-Patent
12 // The protocols, PPI and GUID definitions for this module
14 #include <Ppi/MasterBootMode.h>
15 #include <Ppi/BootInRecoveryMode.h>
16 #include <Guid/MemoryTypeInformation.h>
18 // The Library classes this module consumes
20 #include <Library/ArmPlatformLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/HobLib.h>
23 #include <Library/PeimEntryPoint.h>
24 #include <Library/PeiServicesLib.h>
25 #include <Library/PcdLib.h>
30 IN EFI_PHYSICAL_ADDRESS UefiMemoryBase
,
31 IN UINT64 UefiMemorySize
34 // May want to put this into a library so you only need the PCD settings if you are using the feature?
36 BuildMemoryTypeInformationHob (
40 EFI_MEMORY_TYPE_INFORMATION Info
[10];
42 Info
[0].Type
= EfiACPIReclaimMemory
;
43 Info
[0].NumberOfPages
= PcdGet32 (PcdMemoryTypeEfiACPIReclaimMemory
);
44 Info
[1].Type
= EfiACPIMemoryNVS
;
45 Info
[1].NumberOfPages
= PcdGet32 (PcdMemoryTypeEfiACPIMemoryNVS
);
46 Info
[2].Type
= EfiReservedMemoryType
;
47 Info
[2].NumberOfPages
= PcdGet32 (PcdMemoryTypeEfiReservedMemoryType
);
48 Info
[3].Type
= EfiRuntimeServicesData
;
49 Info
[3].NumberOfPages
= PcdGet32 (PcdMemoryTypeEfiRuntimeServicesData
);
50 Info
[4].Type
= EfiRuntimeServicesCode
;
51 Info
[4].NumberOfPages
= PcdGet32 (PcdMemoryTypeEfiRuntimeServicesCode
);
52 Info
[5].Type
= EfiBootServicesCode
;
53 Info
[5].NumberOfPages
= PcdGet32 (PcdMemoryTypeEfiBootServicesCode
);
54 Info
[6].Type
= EfiBootServicesData
;
55 Info
[6].NumberOfPages
= PcdGet32 (PcdMemoryTypeEfiBootServicesData
);
56 Info
[7].Type
= EfiLoaderCode
;
57 Info
[7].NumberOfPages
= PcdGet32 (PcdMemoryTypeEfiLoaderCode
);
58 Info
[8].Type
= EfiLoaderData
;
59 Info
[8].NumberOfPages
= PcdGet32 (PcdMemoryTypeEfiLoaderData
);
61 // Terminator for the list
62 Info
[9].Type
= EfiMaxMemoryType
;
63 Info
[9].NumberOfPages
= 0;
65 BuildGuidDataHob (&gEfiMemoryTypeInformationGuid
, &Info
, sizeof (Info
));
76 FileHandle - Handle of the file being invoked.
77 PeiServices - Describes the list of possible PEI Services.
81 Status - EFI_SUCCESS if the boot mode could be set
87 IN EFI_PEI_FILE_HANDLE FileHandle
,
88 IN CONST EFI_PEI_SERVICES
**PeiServices
92 UINTN SystemMemoryBase
;
93 UINT64 SystemMemoryTop
;
98 DEBUG ((EFI_D_LOAD
| EFI_D_INFO
, "Memory Init PEIM Loaded\n"));
100 // Ensure PcdSystemMemorySize has been set
101 ASSERT (PcdGet64 (PcdSystemMemorySize
) != 0);
102 ASSERT (PcdGet64 (PcdSystemMemoryBase
) < (UINT64
)MAX_ALLOC_ADDRESS
);
104 SystemMemoryBase
= (UINTN
)PcdGet64 (PcdSystemMemoryBase
);
105 SystemMemoryTop
= SystemMemoryBase
+ PcdGet64 (PcdSystemMemorySize
);
106 if (SystemMemoryTop
- 1 > MAX_ALLOC_ADDRESS
) {
107 SystemMemoryTop
= (UINT64
)MAX_ALLOC_ADDRESS
+ 1;
109 FdBase
= (UINTN
)PcdGet64 (PcdFdBaseAddress
);
110 FdTop
= FdBase
+ (UINTN
)PcdGet32 (PcdFdSize
);
113 // Declare the UEFI memory to PEI
116 // In case the firmware has been shadowed in the System Memory
117 if ((FdBase
>= SystemMemoryBase
) && (FdTop
<= SystemMemoryTop
)) {
118 // Check if there is enough space between the top of the system memory and the top of the
119 // firmware to place the UEFI memory (for PEI & DXE phases)
120 if (SystemMemoryTop
- FdTop
>= FixedPcdGet32 (PcdSystemMemoryUefiRegionSize
)) {
121 UefiMemoryBase
= SystemMemoryTop
- FixedPcdGet32 (PcdSystemMemoryUefiRegionSize
);
123 // Check there is enough space for the UEFI memory
124 ASSERT (SystemMemoryBase
+ FixedPcdGet32 (PcdSystemMemoryUefiRegionSize
) <= FdBase
);
126 UefiMemoryBase
= FdBase
- FixedPcdGet32 (PcdSystemMemoryUefiRegionSize
);
129 // Check the Firmware does not overlapped with the system memory
130 ASSERT ((FdBase
< SystemMemoryBase
) || (FdBase
>= SystemMemoryTop
));
131 ASSERT ((FdTop
<= SystemMemoryBase
) || (FdTop
> SystemMemoryTop
));
133 UefiMemoryBase
= SystemMemoryTop
- FixedPcdGet32 (PcdSystemMemoryUefiRegionSize
);
136 Status
= PeiServicesInstallPeiMemory (UefiMemoryBase
, FixedPcdGet32 (PcdSystemMemoryUefiRegionSize
));
137 ASSERT_EFI_ERROR (Status
);
139 // Initialize MMU and Memory HOBs (Resource Descriptor HOBs)
140 Status
= MemoryPeim (UefiMemoryBase
, FixedPcdGet32 (PcdSystemMemoryUefiRegionSize
));
141 ASSERT_EFI_ERROR (Status
);