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