]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/PropertiesTableAttributesDxe/PropertiesTableAttributesDxe.c
Add UEFI 2.5 properties table support in DXE core.
[mirror_edk2.git] / MdeModulePkg / Universal / PropertiesTableAttributesDxe / PropertiesTableAttributesDxe.c
diff --git a/MdeModulePkg/Universal/PropertiesTableAttributesDxe/PropertiesTableAttributesDxe.c b/MdeModulePkg/Universal/PropertiesTableAttributesDxe/PropertiesTableAttributesDxe.c
new file mode 100644 (file)
index 0000000..12e4566
--- /dev/null
@@ -0,0 +1,206 @@
+/**@file\r
+  This module sets default policy for attributes of EfiACPIMemoryNVS and EfiReservedMemoryType.\r
+\r
+  This module sets EFI_MEMORY_XP for attributes of EfiACPIMemoryNVS and EfiReservedMemoryType\r
+  in UEFI memory map, if and only of PropertiesTable is published and has BIT0 set.\r
+\r
+Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <Uefi.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/DxeServicesTableLib.h>\r
+#include <Library/UefiLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Guid/EventGroup.h>\r
+#include <Guid/PropertiesTable.h>\r
+\r
+/**\r
+  Converts a number of EFI_PAGEs to a size in bytes.\r
+\r
+  NOTE: Do not use EFI_PAGES_TO_SIZE because it handles UINTN only.\r
+\r
+  @param  Pages     The number of EFI_PAGES.\r
+\r
+  @return  The number of bytes associated with the number of EFI_PAGEs specified\r
+           by Pages.\r
+**/\r
+UINT64\r
+EfiPagesToSize (\r
+  IN UINT64 Pages\r
+  )\r
+{\r
+  return LShiftU64 (Pages, EFI_PAGE_SHIFT);\r
+}\r
+\r
+/**\r
+  Set memory attributes according to default policy.\r
+\r
+  @param  MemoryMap        A pointer to the buffer in which firmware places the current memory map.\r
+  @param  MemoryMapSize    Size, in bytes, of the MemoryMap buffer.\r
+  @param  DescriptorSize   size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.\r
+**/\r
+VOID\r
+SetMemorySpaceAttributesDefault (\r
+  IN EFI_MEMORY_DESCRIPTOR  *MemoryMap,\r
+  IN UINTN                  MemoryMapSize,\r
+  IN UINTN                  DescriptorSize\r
+  )\r
+{\r
+  EFI_MEMORY_DESCRIPTOR       *MemoryMapEntry;\r
+  EFI_MEMORY_DESCRIPTOR       *MemoryMapEnd;\r
+  EFI_STATUS                  Status;\r
+\r
+  DEBUG ((EFI_D_INFO, "SetMemorySpaceAttributesDefault\n"));\r
+\r
+  MemoryMapEntry = MemoryMap;\r
+  MemoryMapEnd   = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + MemoryMapSize);\r
+  while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) {\r
+    if (MemoryMapEntry->PhysicalStart < BASE_1MB) {\r
+      //\r
+      // Do not touch memory space below 1MB\r
+      //\r
+      MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
+      continue;\r
+    }\r
+    switch (MemoryMapEntry->Type) {\r
+    case EfiRuntimeServicesCode:\r
+    case EfiRuntimeServicesData:\r
+      //\r
+      // should be handled later;\r
+      //\r
+      break;\r
+    case EfiReservedMemoryType:\r
+    case EfiACPIMemoryNVS:\r
+      //\r
+      // Handle EfiReservedMemoryType and EfiACPIMemoryNVS, because there might be firmware executable there.\r
+      //\r
+      DEBUG ((EFI_D_INFO, "SetMemorySpaceAttributes - %016lx - %016lx (%016lx) ...\n",\r
+        MemoryMapEntry->PhysicalStart,\r
+        MemoryMapEntry->PhysicalStart + EfiPagesToSize (MemoryMapEntry->NumberOfPages),\r
+        MemoryMapEntry->Attribute\r
+        ));\r
+      Status = gDS->SetMemorySpaceCapabilities (\r
+                      MemoryMapEntry->PhysicalStart,\r
+                      EfiPagesToSize (MemoryMapEntry->NumberOfPages),\r
+                      MemoryMapEntry->Attribute | EFI_MEMORY_XP\r
+                      );\r
+      DEBUG ((EFI_D_INFO, "SetMemorySpaceCapabilities - %r\n", Status));\r
+      break;\r
+    }\r
+\r
+    MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
+  }\r
+\r
+  return ;\r
+}\r
+\r
+/**\r
+  Update memory attributes according to default policy.\r
+\r
+  @param[in]  Event     The Event this notify function registered to.\r
+  @param[in]  Context   Pointer to the context data registered to the Event.\r
+**/\r
+VOID\r
+EFIAPI\r
+UpdateMemoryAttributesDefault (\r
+  EFI_EVENT                               Event,\r
+  VOID                                    *Context\r
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  EFI_MEMORY_DESCRIPTOR       *MemoryMap;\r
+  UINTN                       MemoryMapSize;\r
+  UINTN                       MapKey;\r
+  UINTN                       DescriptorSize;\r
+  UINT32                      DescriptorVersion;\r
+  EFI_PROPERTIES_TABLE        *PropertiesTable;\r
+\r
+  DEBUG ((EFI_D_INFO, "UpdateMemoryAttributesDefault\n"));\r
+  Status = EfiGetSystemConfigurationTable (&gEfiPropertiesTableGuid, &PropertiesTable);\r
+  if (EFI_ERROR (Status)) {\r
+    goto Done;\r
+  }\r
+\r
+  DEBUG ((EFI_D_INFO, "MemoryProtectionAttribute - 0x%016lx\n", PropertiesTable->MemoryProtectionAttribute));\r
+  if ((PropertiesTable->MemoryProtectionAttribute & EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) == 0) {\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Get the EFI memory map.\r
+  //\r
+  MemoryMapSize  = 0;\r
+  MemoryMap      = NULL;\r
+  Status = gBS->GetMemoryMap (\r
+                  &MemoryMapSize,\r
+                  MemoryMap,\r
+                  &MapKey,\r
+                  &DescriptorSize,\r
+                  &DescriptorVersion\r
+                  );\r
+  ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
+  do {\r
+    MemoryMap = (EFI_MEMORY_DESCRIPTOR *) AllocatePool (MemoryMapSize);\r
+    ASSERT (MemoryMap != NULL);\r
+    Status = gBS->GetMemoryMap (\r
+                    &MemoryMapSize,\r
+                    MemoryMap,\r
+                    &MapKey,\r
+                    &DescriptorSize,\r
+                    &DescriptorVersion\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      FreePool (MemoryMap);\r
+    }\r
+  } while (Status == EFI_BUFFER_TOO_SMALL);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  SetMemorySpaceAttributesDefault (MemoryMap, MemoryMapSize, DescriptorSize);\r
+\r
+Done:\r
+  gBS->CloseEvent (Event);\r
+\r
+  return ;\r
+}\r
+\r
+/**\r
+  The entrypoint of properties table attribute driver.\r
+\r
+  @param  ImageHandle    The firmware allocated handle for the EFI image.\r
+  @param  SystemTable    A pointer to the EFI System Table.\r
+\r
+  @retval EFI_SUCCESS    It always returns EFI_SUCCESS.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+InitializePropertiesTableAttributesDxe (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  EFI_EVENT   ReadyToBootEvent;\r
+\r
+  Status = gBS->CreateEventEx (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_CALLBACK,\r
+                  UpdateMemoryAttributesDefault,\r
+                  NULL,\r
+                  &gEfiEventReadyToBootGuid,\r
+                  &ReadyToBootEvent\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return EFI_SUCCESS;\r
+}\r