]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/PropertiesTableAttributesDxe/PropertiesTableAttributesDxe.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Universal / PropertiesTableAttributesDxe / PropertiesTableAttributesDxe.c
CommitLineData
9d58ab09 1/** @file\r
03d486b2
JY
2 This module sets default policy for attributes of EfiACPIMemoryNVS and EfiReservedMemoryType.\r
3\r
4 This module sets EFI_MEMORY_XP for attributes of EfiACPIMemoryNVS and EfiReservedMemoryType\r
5 in UEFI memory map, if and only of PropertiesTable is published and has BIT0 set.\r
6\r
d1102dba 7Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>\r
03d486b2
JY
8This program and the accompanying materials\r
9are licensed and made available under the terms and conditions of the BSD License\r
10which accompanies this distribution. The full text of the license may be found at\r
11http://opensource.org/licenses/bsd-license.php\r
12\r
13THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include <Uefi.h>\r
19#include <Library/DebugLib.h>\r
20#include <Library/UefiBootServicesTableLib.h>\r
21#include <Library/DxeServicesTableLib.h>\r
22#include <Library/UefiLib.h>\r
23#include <Library/MemoryAllocationLib.h>\r
24#include <Guid/EventGroup.h>\r
25#include <Guid/PropertiesTable.h>\r
26\r
27/**\r
28 Converts a number of EFI_PAGEs to a size in bytes.\r
29\r
30 NOTE: Do not use EFI_PAGES_TO_SIZE because it handles UINTN only.\r
31\r
32 @param Pages The number of EFI_PAGES.\r
33\r
34 @return The number of bytes associated with the number of EFI_PAGEs specified\r
35 by Pages.\r
36**/\r
37UINT64\r
38EfiPagesToSize (\r
39 IN UINT64 Pages\r
40 )\r
41{\r
42 return LShiftU64 (Pages, EFI_PAGE_SHIFT);\r
43}\r
44\r
45/**\r
46 Set memory attributes according to default policy.\r
47\r
48 @param MemoryMap A pointer to the buffer in which firmware places the current memory map.\r
49 @param MemoryMapSize Size, in bytes, of the MemoryMap buffer.\r
50 @param DescriptorSize size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.\r
51**/\r
52VOID\r
53SetMemorySpaceAttributesDefault (\r
54 IN EFI_MEMORY_DESCRIPTOR *MemoryMap,\r
55 IN UINTN MemoryMapSize,\r
56 IN UINTN DescriptorSize\r
57 )\r
58{\r
59 EFI_MEMORY_DESCRIPTOR *MemoryMapEntry;\r
60 EFI_MEMORY_DESCRIPTOR *MemoryMapEnd;\r
61 EFI_STATUS Status;\r
62\r
63 DEBUG ((EFI_D_INFO, "SetMemorySpaceAttributesDefault\n"));\r
64\r
65 MemoryMapEntry = MemoryMap;\r
66 MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + MemoryMapSize);\r
67 while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) {\r
68 if (MemoryMapEntry->PhysicalStart < BASE_1MB) {\r
69 //\r
70 // Do not touch memory space below 1MB\r
71 //\r
72 MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
73 continue;\r
74 }\r
75 switch (MemoryMapEntry->Type) {\r
76 case EfiRuntimeServicesCode:\r
77 case EfiRuntimeServicesData:\r
78 //\r
79 // should be handled later;\r
80 //\r
81 break;\r
82 case EfiReservedMemoryType:\r
83 case EfiACPIMemoryNVS:\r
84 //\r
85 // Handle EfiReservedMemoryType and EfiACPIMemoryNVS, because there might be firmware executable there.\r
86 //\r
87 DEBUG ((EFI_D_INFO, "SetMemorySpaceAttributes - %016lx - %016lx (%016lx) ...\n",\r
88 MemoryMapEntry->PhysicalStart,\r
89 MemoryMapEntry->PhysicalStart + EfiPagesToSize (MemoryMapEntry->NumberOfPages),\r
90 MemoryMapEntry->Attribute\r
91 ));\r
92 Status = gDS->SetMemorySpaceCapabilities (\r
93 MemoryMapEntry->PhysicalStart,\r
94 EfiPagesToSize (MemoryMapEntry->NumberOfPages),\r
95 MemoryMapEntry->Attribute | EFI_MEMORY_XP\r
96 );\r
97 DEBUG ((EFI_D_INFO, "SetMemorySpaceCapabilities - %r\n", Status));\r
98 break;\r
99 }\r
100\r
101 MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
102 }\r
103\r
104 return ;\r
105}\r
106\r
107/**\r
108 Update memory attributes according to default policy.\r
109\r
110 @param[in] Event The Event this notify function registered to.\r
111 @param[in] Context Pointer to the context data registered to the Event.\r
112**/\r
113VOID\r
114EFIAPI\r
115UpdateMemoryAttributesDefault (\r
116 EFI_EVENT Event,\r
117 VOID *Context\r
118 )\r
119{\r
120 EFI_STATUS Status;\r
121 EFI_MEMORY_DESCRIPTOR *MemoryMap;\r
122 UINTN MemoryMapSize;\r
123 UINTN MapKey;\r
124 UINTN DescriptorSize;\r
125 UINT32 DescriptorVersion;\r
126 EFI_PROPERTIES_TABLE *PropertiesTable;\r
127\r
128 DEBUG ((EFI_D_INFO, "UpdateMemoryAttributesDefault\n"));\r
fd0618c1 129 Status = EfiGetSystemConfigurationTable (&gEfiPropertiesTableGuid, (VOID **) &PropertiesTable);\r
03d486b2
JY
130 if (EFI_ERROR (Status)) {\r
131 goto Done;\r
132 }\r
d1102dba 133\r
a6451806 134 ASSERT (PropertiesTable != NULL);\r
03d486b2
JY
135\r
136 DEBUG ((EFI_D_INFO, "MemoryProtectionAttribute - 0x%016lx\n", PropertiesTable->MemoryProtectionAttribute));\r
137 if ((PropertiesTable->MemoryProtectionAttribute & EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) == 0) {\r
138 goto Done;\r
139 }\r
140\r
141 //\r
142 // Get the EFI memory map.\r
143 //\r
144 MemoryMapSize = 0;\r
145 MemoryMap = NULL;\r
146 Status = gBS->GetMemoryMap (\r
147 &MemoryMapSize,\r
148 MemoryMap,\r
149 &MapKey,\r
150 &DescriptorSize,\r
151 &DescriptorVersion\r
152 );\r
153 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
154 do {\r
155 MemoryMap = (EFI_MEMORY_DESCRIPTOR *) AllocatePool (MemoryMapSize);\r
156 ASSERT (MemoryMap != NULL);\r
157 Status = gBS->GetMemoryMap (\r
158 &MemoryMapSize,\r
159 MemoryMap,\r
160 &MapKey,\r
161 &DescriptorSize,\r
162 &DescriptorVersion\r
163 );\r
164 if (EFI_ERROR (Status)) {\r
165 FreePool (MemoryMap);\r
166 }\r
167 } while (Status == EFI_BUFFER_TOO_SMALL);\r
168 ASSERT_EFI_ERROR (Status);\r
169\r
170 SetMemorySpaceAttributesDefault (MemoryMap, MemoryMapSize, DescriptorSize);\r
171\r
172Done:\r
173 gBS->CloseEvent (Event);\r
174\r
175 return ;\r
176}\r
177\r
178/**\r
179 The entrypoint of properties table attribute driver.\r
180\r
181 @param ImageHandle The firmware allocated handle for the EFI image.\r
182 @param SystemTable A pointer to the EFI System Table.\r
183\r
184 @retval EFI_SUCCESS It always returns EFI_SUCCESS.\r
185\r
186**/\r
187EFI_STATUS\r
188EFIAPI\r
189InitializePropertiesTableAttributesDxe (\r
190 IN EFI_HANDLE ImageHandle,\r
191 IN EFI_SYSTEM_TABLE *SystemTable\r
192 )\r
193{\r
194 EFI_STATUS Status;\r
195 EFI_EVENT ReadyToBootEvent;\r
196\r
197 Status = gBS->CreateEventEx (\r
198 EVT_NOTIFY_SIGNAL,\r
199 TPL_CALLBACK,\r
200 UpdateMemoryAttributesDefault,\r
201 NULL,\r
202 &gEfiEventReadyToBootGuid,\r
203 &ReadyToBootEvent\r
204 );\r
205 ASSERT_EFI_ERROR (Status);\r
206\r
207 return EFI_SUCCESS;\r
208}\r