2 Produce the memory type information HOB.
4 Copyright (C) 2017-2020, Red Hat, Inc.
6 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include <Guid/MemoryTypeInformation.h>
10 #include <Library/BaseLib.h>
11 #include <Library/DebugLib.h>
12 #include <Library/HobLib.h>
13 #include <Library/PcdLib.h>
14 #include <Library/PeiServicesLib.h>
15 #include <Ppi/ReadOnlyVariable2.h>
16 #include <Uefi/UefiMultiPhase.h>
20 #define MEMORY_TYPE_INFO_DEFAULT(Type) \
21 { Type, FixedPcdGet32 (PcdMemoryType ## Type) }
23 STATIC EFI_MEMORY_TYPE_INFORMATION mMemoryTypeInformation
[] = {
24 MEMORY_TYPE_INFO_DEFAULT (EfiACPIMemoryNVS
),
25 MEMORY_TYPE_INFO_DEFAULT (EfiACPIReclaimMemory
),
26 MEMORY_TYPE_INFO_DEFAULT (EfiReservedMemoryType
),
27 MEMORY_TYPE_INFO_DEFAULT (EfiRuntimeServicesCode
),
28 MEMORY_TYPE_INFO_DEFAULT (EfiRuntimeServicesData
),
29 { EfiMaxMemoryType
, 0}
39 &gEfiMemoryTypeInformationGuid
,
40 mMemoryTypeInformation
,
41 sizeof mMemoryTypeInformation
46 Refresh the mMemoryTypeInformation array (which we'll turn into the
47 MemoryTypeInformation HOB) from the MemoryTypeInformation UEFI variable.
49 Normally, the DXE IPL PEIM builds the HOB from the UEFI variable. But it does
50 so *transparently*. Instead, we consider the UEFI variable as a list of
51 hints, for updating our HOB defaults:
53 - Record types not covered in mMemoryTypeInformation are ignored. In
54 particular, this hides record types from the UEFI variable that may lead to
55 reboots without benefiting SMM security, such as EfiBootServicesData.
57 - Records that would lower the defaults in mMemoryTypeInformation are also
60 @param[in] ReadOnlyVariable2 The EFI_PEI_READ_ONLY_VARIABLE2_PPI used for
61 retrieving the MemoryTypeInformation UEFI
67 IN EFI_PEI_READ_ONLY_VARIABLE2_PPI
*ReadOnlyVariable2
71 EFI_MEMORY_TYPE_INFORMATION Entries
[EfiMaxMemoryType
+ 1];
77 // Read the MemoryTypeInformation UEFI variable from the
78 // gEfiMemoryTypeInformationGuid namespace.
80 DataSize
= sizeof Entries
;
81 Status
= ReadOnlyVariable2
->GetVariable (
83 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
84 &gEfiMemoryTypeInformationGuid
,
89 if (EFI_ERROR (Status
)) {
91 // If the UEFI variable does not exist (EFI_NOT_FOUND), we can't use it for
92 // udpating mMemoryTypeInformation.
94 // If the UEFI variable exists but Entries is too small to hold it
95 // (EFI_BUFFER_TOO_SMALL), then the variable contents are arguably invalid.
96 // That's because Entries has room for every distinct EFI_MEMORY_TYPE,
97 // including the terminator record with EfiMaxMemoryType. Thus, we can't
98 // use the UEFI variable for updating mMemoryTypeInformation.
100 // If the UEFI variable couldn't be read for some other reason, we
101 // similarly can't use it for udpating mMemoryTypeInformation.
103 DEBUG ((DEBUG_ERROR
, "%a: GetVariable(): %r\n", __FUNCTION__
, Status
));
108 // Sanity-check the UEFI variable size against the record size.
110 if (DataSize
% sizeof Entries
[0] != 0) {
113 "%a: invalid UEFI variable size %Lu\n",
120 NumEntries
= DataSize
/ sizeof Entries
[0];
123 // For each record in mMemoryTypeInformation, except the terminator record,
124 // look up the first match (if any) in the UEFI variable, based on the memory
127 for (HobRecordIdx
= 0;
128 HobRecordIdx
< ARRAY_SIZE (mMemoryTypeInformation
) - 1;
131 EFI_MEMORY_TYPE_INFORMATION
*HobRecord
;
133 EFI_MEMORY_TYPE_INFORMATION
*VariableRecord
;
135 HobRecord
= &mMemoryTypeInformation
[HobRecordIdx
];
137 for (Idx
= 0; Idx
< NumEntries
; Idx
++) {
138 VariableRecord
= &Entries
[Idx
];
140 if (VariableRecord
->Type
== HobRecord
->Type
) {
146 // If there is a match, allow the UEFI variable to increase NumberOfPages.
148 if ((Idx
< NumEntries
) &&
149 (HobRecord
->NumberOfPages
< VariableRecord
->NumberOfPages
))
153 "%a: Type 0x%x: NumberOfPages 0x%x -> 0x%x\n",
156 HobRecord
->NumberOfPages
,
157 VariableRecord
->NumberOfPages
160 HobRecord
->NumberOfPages
= VariableRecord
->NumberOfPages
;
166 Notification function called when EFI_PEI_READ_ONLY_VARIABLE2_PPI becomes
169 @param[in] PeiServices Indirect reference to the PEI Services Table.
170 @param[in] NotifyDescriptor Address of the notification descriptor data
172 @param[in] Ppi Address of the PPI that was installed.
174 @return Status of the notification. The status code returned from this
180 OnReadOnlyVariable2Available (
181 IN EFI_PEI_SERVICES
**PeiServices
,
182 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
186 DEBUG ((DEBUG_VERBOSE
, "%a\n", __FUNCTION__
));
188 RefreshMemTypeInfo (Ppi
);
189 BuildMemTypeInfoHob ();
194 // Notification object for registering the callback, for when
195 // EFI_PEI_READ_ONLY_VARIABLE2_PPI becomes available.
197 STATIC CONST EFI_PEI_NOTIFY_DESCRIPTOR mReadOnlyVariable2Notify
= {
198 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH
|
199 EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
), // Flags
200 &gEfiPeiReadOnlyVariable2PpiGuid
, // Guid
201 OnReadOnlyVariable2Available
// Notify
205 MemTypeInfoInitialization (
211 if (!FeaturePcdGet (PcdSmmSmramRequire
)) {
213 // EFI_PEI_READ_ONLY_VARIABLE2_PPI will never be available; install
214 // the default memory type information HOB right away.
216 BuildMemTypeInfoHob ();
220 Status
= PeiServicesNotifyPpi (&mReadOnlyVariable2Notify
);
221 if (EFI_ERROR (Status
)) {
224 "%a: failed to set up R/O Variable 2 callback: %r\n",