]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/PropertiesTableAttributesDxe/PropertiesTableAttributesDxe.c
Add UEFI 2.5 properties table support in DXE core.
[mirror_edk2.git] / MdeModulePkg / Universal / PropertiesTableAttributesDxe / PropertiesTableAttributesDxe.c
CommitLineData
03d486b2
JY
1/**@file\r
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
7Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
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
129 Status = EfiGetSystemConfigurationTable (&gEfiPropertiesTableGuid, &PropertiesTable);\r
130 if (EFI_ERROR (Status)) {\r
131 goto Done;\r
132 }\r
133\r
134 DEBUG ((EFI_D_INFO, "MemoryProtectionAttribute - 0x%016lx\n", PropertiesTable->MemoryProtectionAttribute));\r
135 if ((PropertiesTable->MemoryProtectionAttribute & EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) == 0) {\r
136 goto Done;\r
137 }\r
138\r
139 //\r
140 // Get the EFI memory map.\r
141 //\r
142 MemoryMapSize = 0;\r
143 MemoryMap = NULL;\r
144 Status = gBS->GetMemoryMap (\r
145 &MemoryMapSize,\r
146 MemoryMap,\r
147 &MapKey,\r
148 &DescriptorSize,\r
149 &DescriptorVersion\r
150 );\r
151 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
152 do {\r
153 MemoryMap = (EFI_MEMORY_DESCRIPTOR *) AllocatePool (MemoryMapSize);\r
154 ASSERT (MemoryMap != NULL);\r
155 Status = gBS->GetMemoryMap (\r
156 &MemoryMapSize,\r
157 MemoryMap,\r
158 &MapKey,\r
159 &DescriptorSize,\r
160 &DescriptorVersion\r
161 );\r
162 if (EFI_ERROR (Status)) {\r
163 FreePool (MemoryMap);\r
164 }\r
165 } while (Status == EFI_BUFFER_TOO_SMALL);\r
166 ASSERT_EFI_ERROR (Status);\r
167\r
168 SetMemorySpaceAttributesDefault (MemoryMap, MemoryMapSize, DescriptorSize);\r
169\r
170Done:\r
171 gBS->CloseEvent (Event);\r
172\r
173 return ;\r
174}\r
175\r
176/**\r
177 The entrypoint of properties table attribute driver.\r
178\r
179 @param ImageHandle The firmware allocated handle for the EFI image.\r
180 @param SystemTable A pointer to the EFI System Table.\r
181\r
182 @retval EFI_SUCCESS It always returns EFI_SUCCESS.\r
183\r
184**/\r
185EFI_STATUS\r
186EFIAPI\r
187InitializePropertiesTableAttributesDxe (\r
188 IN EFI_HANDLE ImageHandle,\r
189 IN EFI_SYSTEM_TABLE *SystemTable\r
190 )\r
191{\r
192 EFI_STATUS Status;\r
193 EFI_EVENT ReadyToBootEvent;\r
194\r
195 Status = gBS->CreateEventEx (\r
196 EVT_NOTIFY_SIGNAL,\r
197 TPL_CALLBACK,\r
198 UpdateMemoryAttributesDefault,\r
199 NULL,\r
200 &gEfiEventReadyToBootGuid,\r
201 &ReadyToBootEvent\r
202 );\r
203 ASSERT_EFI_ERROR (Status);\r
204\r
205 return EFI_SUCCESS;\r
206}\r