]>
Commit | Line | Data |
---|---|---|
d42fdd6f | 1 | /** @file\r |
356b96b3 | 2 | Produce the memory type information HOB.\r |
d42fdd6f LE |
3 | \r |
4 | Copyright (C) 2017-2020, Red Hat, Inc.\r | |
5 | \r | |
6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
7 | **/\r | |
8 | \r | |
9 | #include <Guid/MemoryTypeInformation.h>\r | |
10 | #include <Library/BaseLib.h>\r | |
11 | #include <Library/DebugLib.h>\r | |
12 | #include <Library/HobLib.h>\r | |
13 | #include <Library/PcdLib.h>\r | |
14 | #include <Library/PeiServicesLib.h>\r | |
15 | #include <Ppi/ReadOnlyVariable2.h>\r | |
16 | #include <Uefi/UefiMultiPhase.h>\r | |
17 | \r | |
18 | #include "Platform.h"\r | |
19 | \r | |
8db87f98 LE |
20 | #define MEMORY_TYPE_INFO_DEFAULT(Type) \\r |
21 | { Type, FixedPcdGet32 (PcdMemoryType ## Type) }\r | |
22 | \r | |
ac0a286f | 23 | STATIC EFI_MEMORY_TYPE_INFORMATION mMemoryTypeInformation[] = {\r |
8db87f98 LE |
24 | MEMORY_TYPE_INFO_DEFAULT (EfiACPIMemoryNVS),\r |
25 | MEMORY_TYPE_INFO_DEFAULT (EfiACPIReclaimMemory),\r | |
26 | MEMORY_TYPE_INFO_DEFAULT (EfiReservedMemoryType),\r | |
27 | MEMORY_TYPE_INFO_DEFAULT (EfiRuntimeServicesCode),\r | |
28 | MEMORY_TYPE_INFO_DEFAULT (EfiRuntimeServicesData),\r | |
ac0a286f | 29 | { EfiMaxMemoryType, 0}\r |
d42fdd6f LE |
30 | };\r |
31 | \r | |
32 | STATIC\r | |
33 | VOID\r | |
34 | BuildMemTypeInfoHob (\r | |
35 | VOID\r | |
36 | )\r | |
37 | {\r | |
38 | BuildGuidDataHob (\r | |
39 | &gEfiMemoryTypeInformationGuid,\r | |
356b96b3 LE |
40 | mMemoryTypeInformation,\r |
41 | sizeof mMemoryTypeInformation\r | |
d42fdd6f | 42 | );\r |
d42fdd6f LE |
43 | }\r |
44 | \r | |
45 | /**\r | |
356b96b3 LE |
46 | Refresh the mMemoryTypeInformation array (which we'll turn into the\r |
47 | MemoryTypeInformation HOB) from the MemoryTypeInformation UEFI variable.\r | |
d42fdd6f | 48 | \r |
356b96b3 LE |
49 | Normally, the DXE IPL PEIM builds the HOB from the UEFI variable. But it does\r |
50 | so *transparently*. Instead, we consider the UEFI variable as a list of\r | |
51 | hints, for updating our HOB defaults:\r | |
d42fdd6f | 52 | \r |
356b96b3 LE |
53 | - Record types not covered in mMemoryTypeInformation are ignored. In\r |
54 | particular, this hides record types from the UEFI variable that may lead to\r | |
55 | reboots without benefiting SMM security, such as EfiBootServicesData.\r | |
56 | \r | |
57 | - Records that would lower the defaults in mMemoryTypeInformation are also\r | |
58 | ignored.\r | |
59 | \r | |
60 | @param[in] ReadOnlyVariable2 The EFI_PEI_READ_ONLY_VARIABLE2_PPI used for\r | |
61 | retrieving the MemoryTypeInformation UEFI\r | |
62 | variable.\r | |
d42fdd6f LE |
63 | **/\r |
64 | STATIC\r | |
356b96b3 LE |
65 | VOID\r |
66 | RefreshMemTypeInfo (\r | |
ac0a286f | 67 | IN EFI_PEI_READ_ONLY_VARIABLE2_PPI *ReadOnlyVariable2\r |
d42fdd6f LE |
68 | )\r |
69 | {\r | |
ac0a286f MK |
70 | UINTN DataSize;\r |
71 | EFI_MEMORY_TYPE_INFORMATION Entries[EfiMaxMemoryType + 1];\r | |
72 | EFI_STATUS Status;\r | |
73 | UINTN NumEntries;\r | |
74 | UINTN HobRecordIdx;\r | |
d42fdd6f LE |
75 | \r |
76 | //\r | |
356b96b3 | 77 | // Read the MemoryTypeInformation UEFI variable from the\r |
d42fdd6f LE |
78 | // gEfiMemoryTypeInformationGuid namespace.\r |
79 | //\r | |
356b96b3 | 80 | DataSize = sizeof Entries;\r |
ac0a286f MK |
81 | Status = ReadOnlyVariable2->GetVariable (\r |
82 | ReadOnlyVariable2,\r | |
83 | EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r | |
84 | &gEfiMemoryTypeInformationGuid,\r | |
85 | NULL,\r | |
86 | &DataSize,\r | |
87 | Entries\r | |
88 | );\r | |
356b96b3 | 89 | if (EFI_ERROR (Status)) {\r |
d42fdd6f | 90 | //\r |
356b96b3 LE |
91 | // If the UEFI variable does not exist (EFI_NOT_FOUND), we can't use it for\r |
92 | // udpating mMemoryTypeInformation.\r | |
d42fdd6f | 93 | //\r |
356b96b3 LE |
94 | // If the UEFI variable exists but Entries is too small to hold it\r |
95 | // (EFI_BUFFER_TOO_SMALL), then the variable contents are arguably invalid.\r | |
96 | // That's because Entries has room for every distinct EFI_MEMORY_TYPE,\r | |
97 | // including the terminator record with EfiMaxMemoryType. Thus, we can't\r | |
98 | // use the UEFI variable for updating mMemoryTypeInformation.\r | |
d42fdd6f | 99 | //\r |
356b96b3 LE |
100 | // If the UEFI variable couldn't be read for some other reason, we\r |
101 | // similarly can't use it for udpating mMemoryTypeInformation.\r | |
d42fdd6f | 102 | //\r |
356b96b3 LE |
103 | DEBUG ((DEBUG_ERROR, "%a: GetVariable(): %r\n", __FUNCTION__, Status));\r |
104 | return;\r | |
d42fdd6f LE |
105 | }\r |
106 | \r | |
356b96b3 LE |
107 | //\r |
108 | // Sanity-check the UEFI variable size against the record size.\r | |
109 | //\r | |
110 | if (DataSize % sizeof Entries[0] != 0) {\r | |
ac0a286f MK |
111 | DEBUG ((\r |
112 | DEBUG_ERROR,\r | |
113 | "%a: invalid UEFI variable size %Lu\n",\r | |
114 | __FUNCTION__,\r | |
115 | (UINT64)DataSize\r | |
116 | ));\r | |
356b96b3 LE |
117 | return;\r |
118 | }\r | |
ac0a286f | 119 | \r |
356b96b3 LE |
120 | NumEntries = DataSize / sizeof Entries[0];\r |
121 | \r | |
122 | //\r | |
123 | // For each record in mMemoryTypeInformation, except the terminator record,\r | |
124 | // look up the first match (if any) in the UEFI variable, based on the memory\r | |
125 | // type.\r | |
126 | //\r | |
127 | for (HobRecordIdx = 0;\r | |
128 | HobRecordIdx < ARRAY_SIZE (mMemoryTypeInformation) - 1;\r | |
ac0a286f MK |
129 | HobRecordIdx++)\r |
130 | {\r | |
131 | EFI_MEMORY_TYPE_INFORMATION *HobRecord;\r | |
132 | UINTN Idx;\r | |
133 | EFI_MEMORY_TYPE_INFORMATION *VariableRecord;\r | |
356b96b3 LE |
134 | \r |
135 | HobRecord = &mMemoryTypeInformation[HobRecordIdx];\r | |
136 | \r | |
137 | for (Idx = 0; Idx < NumEntries; Idx++) {\r | |
138 | VariableRecord = &Entries[Idx];\r | |
139 | \r | |
140 | if (VariableRecord->Type == HobRecord->Type) {\r | |
141 | break;\r | |
142 | }\r | |
143 | }\r | |
144 | \r | |
145 | //\r | |
146 | // If there is a match, allow the UEFI variable to increase NumberOfPages.\r | |
147 | //\r | |
ac0a286f MK |
148 | if ((Idx < NumEntries) &&\r |
149 | (HobRecord->NumberOfPages < VariableRecord->NumberOfPages))\r | |
150 | {\r | |
151 | DEBUG ((\r | |
152 | DEBUG_VERBOSE,\r | |
153 | "%a: Type 0x%x: NumberOfPages 0x%x -> 0x%x\n",\r | |
154 | __FUNCTION__,\r | |
155 | HobRecord->Type,\r | |
156 | HobRecord->NumberOfPages,\r | |
157 | VariableRecord->NumberOfPages\r | |
158 | ));\r | |
356b96b3 LE |
159 | \r |
160 | HobRecord->NumberOfPages = VariableRecord->NumberOfPages;\r | |
161 | }\r | |
162 | }\r | |
163 | }\r | |
164 | \r | |
165 | /**\r | |
166 | Notification function called when EFI_PEI_READ_ONLY_VARIABLE2_PPI becomes\r | |
167 | available.\r | |
168 | \r | |
169 | @param[in] PeiServices Indirect reference to the PEI Services Table.\r | |
170 | @param[in] NotifyDescriptor Address of the notification descriptor data\r | |
171 | structure.\r | |
172 | @param[in] Ppi Address of the PPI that was installed.\r | |
173 | \r | |
174 | @return Status of the notification. The status code returned from this\r | |
175 | function is ignored.\r | |
176 | **/\r | |
177 | STATIC\r | |
178 | EFI_STATUS\r | |
179 | EFIAPI\r | |
180 | OnReadOnlyVariable2Available (\r | |
181 | IN EFI_PEI_SERVICES **PeiServices,\r | |
182 | IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,\r | |
183 | IN VOID *Ppi\r | |
184 | )\r | |
185 | {\r | |
186 | DEBUG ((DEBUG_VERBOSE, "%a\n", __FUNCTION__));\r | |
187 | \r | |
188 | RefreshMemTypeInfo (Ppi);\r | |
189 | BuildMemTypeInfoHob ();\r | |
d42fdd6f LE |
190 | return EFI_SUCCESS;\r |
191 | }\r | |
192 | \r | |
193 | //\r | |
194 | // Notification object for registering the callback, for when\r | |
195 | // EFI_PEI_READ_ONLY_VARIABLE2_PPI becomes available.\r | |
196 | //\r | |
ac0a286f | 197 | STATIC CONST EFI_PEI_NOTIFY_DESCRIPTOR mReadOnlyVariable2Notify = {\r |
d42fdd6f LE |
198 | (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH |\r |
199 | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST), // Flags\r | |
200 | &gEfiPeiReadOnlyVariable2PpiGuid, // Guid\r | |
201 | OnReadOnlyVariable2Available // Notify\r | |
202 | };\r | |
203 | \r | |
204 | VOID\r | |
205 | MemTypeInfoInitialization (\r | |
cc6efda7 | 206 | IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r |
d42fdd6f LE |
207 | )\r |
208 | {\r | |
ac0a286f | 209 | EFI_STATUS Status;\r |
d42fdd6f | 210 | \r |
cc6efda7 | 211 | if (!PlatformInfoHob->SmmSmramRequire) {\r |
d42fdd6f LE |
212 | //\r |
213 | // EFI_PEI_READ_ONLY_VARIABLE2_PPI will never be available; install\r | |
214 | // the default memory type information HOB right away.\r | |
215 | //\r | |
216 | BuildMemTypeInfoHob ();\r | |
217 | return;\r | |
218 | }\r | |
219 | \r | |
220 | Status = PeiServicesNotifyPpi (&mReadOnlyVariable2Notify);\r | |
221 | if (EFI_ERROR (Status)) {\r | |
ac0a286f MK |
222 | DEBUG ((\r |
223 | DEBUG_ERROR,\r | |
224 | "%a: failed to set up R/O Variable 2 callback: %r\n",\r | |
225 | __FUNCTION__,\r | |
226 | Status\r | |
227 | ));\r | |
d42fdd6f LE |
228 | ASSERT (FALSE);\r |
229 | CpuDeadLoop ();\r | |
230 | }\r | |
231 | }\r |