]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/PlatformPei/MemTypeInfo.c
OvmfPkg: Apply uncrustify changes
[mirror_edk2.git] / OvmfPkg / PlatformPei / MemTypeInfo.c
index c709236a457a23bbb934c6ec8c12c9811fb33f08..fc5ccfaf113d25f09fc7b462de006cded6f27354 100644 (file)
@@ -1,7 +1,5 @@
 /** @file\r
-  Produce a default memory type information HOB unless we can determine, from\r
-  the existence of the "MemoryTypeInformation" variable, that the DXE IPL PEIM\r
-  will produce the HOB.\r
+  Produce the memory type information HOB.\r
 \r
   Copyright (C) 2017-2020, Red Hat, Inc.\r
 \r
 \r
 #include "Platform.h"\r
 \r
-STATIC EFI_MEMORY_TYPE_INFORMATION mDefaultMemoryTypeInformation[] = {\r
-  { EfiACPIMemoryNVS,       0x004 },\r
-  { EfiACPIReclaimMemory,   0x008 },\r
-  { EfiReservedMemoryType,  0x004 },\r
-  { EfiRuntimeServicesData, 0x024 },\r
-  { EfiRuntimeServicesCode, 0x030 },\r
-  { EfiBootServicesCode,    0x180 },\r
-  { EfiBootServicesData,    0xF00 },\r
-  { EfiMaxMemoryType,       0x000 }\r
+#define MEMORY_TYPE_INFO_DEFAULT(Type) \\r
+  { Type, FixedPcdGet32 (PcdMemoryType ## Type) }\r
+\r
+STATIC EFI_MEMORY_TYPE_INFORMATION  mMemoryTypeInformation[] = {\r
+  MEMORY_TYPE_INFO_DEFAULT (EfiACPIMemoryNVS),\r
+  MEMORY_TYPE_INFO_DEFAULT (EfiACPIReclaimMemory),\r
+  MEMORY_TYPE_INFO_DEFAULT (EfiReservedMemoryType),\r
+  MEMORY_TYPE_INFO_DEFAULT (EfiRuntimeServicesCode),\r
+  MEMORY_TYPE_INFO_DEFAULT (EfiRuntimeServicesData),\r
+  { EfiMaxMemoryType,                               0}\r
 };\r
 \r
 STATIC\r
@@ -38,11 +37,129 @@ BuildMemTypeInfoHob (
 {\r
   BuildGuidDataHob (\r
     &gEfiMemoryTypeInformationGuid,\r
-    mDefaultMemoryTypeInformation,\r
-    sizeof mDefaultMemoryTypeInformation\r
+    mMemoryTypeInformation,\r
+    sizeof mMemoryTypeInformation\r
     );\r
-  DEBUG ((DEBUG_INFO, "%a: default memory type information HOB built\n",\r
-    __FUNCTION__));\r
+}\r
+\r
+/**\r
+  Refresh the mMemoryTypeInformation array (which we'll turn into the\r
+  MemoryTypeInformation HOB) from the MemoryTypeInformation UEFI variable.\r
+\r
+  Normally, the DXE IPL PEIM builds the HOB from the UEFI variable. But it does\r
+  so *transparently*. Instead, we consider the UEFI variable as a list of\r
+  hints, for updating our HOB defaults:\r
+\r
+  - Record types not covered in mMemoryTypeInformation are ignored. In\r
+    particular, this hides record types from the UEFI variable that may lead to\r
+    reboots without benefiting SMM security, such as EfiBootServicesData.\r
+\r
+  - Records that would lower the defaults in mMemoryTypeInformation are also\r
+    ignored.\r
+\r
+  @param[in] ReadOnlyVariable2  The EFI_PEI_READ_ONLY_VARIABLE2_PPI used for\r
+                                retrieving the MemoryTypeInformation UEFI\r
+                                variable.\r
+**/\r
+STATIC\r
+VOID\r
+RefreshMemTypeInfo (\r
+  IN EFI_PEI_READ_ONLY_VARIABLE2_PPI  *ReadOnlyVariable2\r
+  )\r
+{\r
+  UINTN                        DataSize;\r
+  EFI_MEMORY_TYPE_INFORMATION  Entries[EfiMaxMemoryType + 1];\r
+  EFI_STATUS                   Status;\r
+  UINTN                        NumEntries;\r
+  UINTN                        HobRecordIdx;\r
+\r
+  //\r
+  // Read the MemoryTypeInformation UEFI variable from the\r
+  // gEfiMemoryTypeInformationGuid namespace.\r
+  //\r
+  DataSize = sizeof Entries;\r
+  Status   = ReadOnlyVariable2->GetVariable (\r
+                                  ReadOnlyVariable2,\r
+                                  EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
+                                  &gEfiMemoryTypeInformationGuid,\r
+                                  NULL,\r
+                                  &DataSize,\r
+                                  Entries\r
+                                  );\r
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // If the UEFI variable does not exist (EFI_NOT_FOUND), we can't use it for\r
+    // udpating mMemoryTypeInformation.\r
+    //\r
+    // If the UEFI variable exists but Entries is too small to hold it\r
+    // (EFI_BUFFER_TOO_SMALL), then the variable contents are arguably invalid.\r
+    // That's because Entries has room for every distinct EFI_MEMORY_TYPE,\r
+    // including the terminator record with EfiMaxMemoryType. Thus, we can't\r
+    // use the UEFI variable for updating mMemoryTypeInformation.\r
+    //\r
+    // If the UEFI variable couldn't be read for some other reason, we\r
+    // similarly can't use it for udpating mMemoryTypeInformation.\r
+    //\r
+    DEBUG ((DEBUG_ERROR, "%a: GetVariable(): %r\n", __FUNCTION__, Status));\r
+    return;\r
+  }\r
+\r
+  //\r
+  // Sanity-check the UEFI variable size against the record size.\r
+  //\r
+  if (DataSize % sizeof Entries[0] != 0) {\r
+    DEBUG ((\r
+      DEBUG_ERROR,\r
+      "%a: invalid UEFI variable size %Lu\n",\r
+      __FUNCTION__,\r
+      (UINT64)DataSize\r
+      ));\r
+    return;\r
+  }\r
+\r
+  NumEntries = DataSize / sizeof Entries[0];\r
+\r
+  //\r
+  // For each record in mMemoryTypeInformation, except the terminator record,\r
+  // look up the first match (if any) in the UEFI variable, based on the memory\r
+  // type.\r
+  //\r
+  for (HobRecordIdx = 0;\r
+       HobRecordIdx < ARRAY_SIZE (mMemoryTypeInformation) - 1;\r
+       HobRecordIdx++)\r
+  {\r
+    EFI_MEMORY_TYPE_INFORMATION  *HobRecord;\r
+    UINTN                        Idx;\r
+    EFI_MEMORY_TYPE_INFORMATION  *VariableRecord;\r
+\r
+    HobRecord = &mMemoryTypeInformation[HobRecordIdx];\r
+\r
+    for (Idx = 0; Idx < NumEntries; Idx++) {\r
+      VariableRecord = &Entries[Idx];\r
+\r
+      if (VariableRecord->Type == HobRecord->Type) {\r
+        break;\r
+      }\r
+    }\r
+\r
+    //\r
+    // If there is a match, allow the UEFI variable to increase NumberOfPages.\r
+    //\r
+    if ((Idx < NumEntries) &&\r
+        (HobRecord->NumberOfPages < VariableRecord->NumberOfPages))\r
+    {\r
+      DEBUG ((\r
+        DEBUG_VERBOSE,\r
+        "%a: Type 0x%x: NumberOfPages 0x%x -> 0x%x\n",\r
+        __FUNCTION__,\r
+        HobRecord->Type,\r
+        HobRecord->NumberOfPages,\r
+        VariableRecord->NumberOfPages\r
+        ));\r
+\r
+      HobRecord->NumberOfPages = VariableRecord->NumberOfPages;\r
+    }\r
+  }\r
 }\r
 \r
 /**\r
@@ -66,47 +183,10 @@ OnReadOnlyVariable2Available (
   IN VOID                       *Ppi\r
   )\r
 {\r
-  EFI_PEI_READ_ONLY_VARIABLE2_PPI *ReadOnlyVariable2;\r
-  UINTN                           DataSize;\r
-  EFI_STATUS                      Status;\r
-\r
   DEBUG ((DEBUG_VERBOSE, "%a\n", __FUNCTION__));\r
 \r
-  //\r
-  // Check if the "MemoryTypeInformation" variable exists, in the\r
-  // gEfiMemoryTypeInformationGuid namespace.\r
-  //\r
-  ReadOnlyVariable2 = Ppi;\r
-  DataSize = 0;\r
-  Status = ReadOnlyVariable2->GetVariable (\r
-                                ReadOnlyVariable2,\r
-                                EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
-                                &gEfiMemoryTypeInformationGuid,\r
-                                NULL,\r
-                                &DataSize,\r
-                                NULL\r
-                                );\r
-  switch (Status) {\r
-  case EFI_BUFFER_TOO_SMALL:\r
-    //\r
-    // The variable exists; the DXE IPL PEIM will build the HOB from it.\r
-    //\r
-    break;\r
-  case EFI_NOT_FOUND:\r
-    //\r
-    // The variable does not exist; install the default memory type information\r
-    // HOB.\r
-    //\r
-    BuildMemTypeInfoHob ();\r
-    break;\r
-  default:\r
-    DEBUG ((DEBUG_ERROR, "%a: unexpected: GetVariable(): %r\n", __FUNCTION__,\r
-      Status));\r
-    ASSERT (FALSE);\r
-    CpuDeadLoop ();\r
-    break;\r
-  }\r
-\r
+  RefreshMemTypeInfo (Ppi);\r
+  BuildMemTypeInfoHob ();\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -114,7 +194,7 @@ OnReadOnlyVariable2Available (
 // Notification object for registering the callback, for when\r
 // EFI_PEI_READ_ONLY_VARIABLE2_PPI becomes available.\r
 //\r
-STATIC CONST EFI_PEI_NOTIFY_DESCRIPTOR mReadOnlyVariable2Notify = {\r
+STATIC CONST EFI_PEI_NOTIFY_DESCRIPTOR  mReadOnlyVariable2Notify = {\r
   (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH |\r
    EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),  // Flags\r
   &gEfiPeiReadOnlyVariable2PpiGuid,         // Guid\r
@@ -126,7 +206,7 @@ MemTypeInfoInitialization (
   VOID\r
   )\r
 {\r
-  EFI_STATUS Status;\r
+  EFI_STATUS  Status;\r
 \r
   if (!FeaturePcdGet (PcdSmmSmramRequire)) {\r
     //\r
@@ -139,8 +219,12 @@ MemTypeInfoInitialization (
 \r
   Status = PeiServicesNotifyPpi (&mReadOnlyVariable2Notify);\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((DEBUG_ERROR, "%a: failed to set up R/O Variable 2 callback: %r\n",\r
-      __FUNCTION__, Status));\r
+    DEBUG ((\r
+      DEBUG_ERROR,\r
+      "%a: failed to set up R/O Variable 2 callback: %r\n",\r
+      __FUNCTION__,\r
+      Status\r
+      ));\r
     ASSERT (FALSE);\r
     CpuDeadLoop ();\r
   }\r