]> git.proxmox.com Git - mirror_edk2.git/blobdiff - DuetPkg/Library/DuetBdsLib/BdsPlatform.c
Fix system memory map issue.
[mirror_edk2.git] / DuetPkg / Library / DuetBdsLib / BdsPlatform.c
index f3fc39675705702589ebe2541c4ca0441eba3bc6..9f9283c99f720bc40a9ca8c62fd0f510cf6bc35a 100644 (file)
@@ -27,6 +27,11 @@ Abstract:
 CHAR16  mFirmwareVendor[] = L"TianoCore.org";\r
 extern BOOLEAN  gConnectAllHappened;\r
 extern USB_CLASS_FORMAT_DEVICE_PATH gUsbClassKeyboardDevicePath;\r
+\r
+EFI_GUID                    *gTableGuidArray[] = {\r
+    &gEfiAcpi20TableGuid, &gEfiAcpiTableGuid, &gEfiSmbiosTableGuid, &gEfiMpsTableGuid\r
+  };\r
+\r
 //\r
 // BDS Platform Functions\r
 //\r
@@ -52,9 +57,6 @@ Returns:
   EFI_PEI_HOB_POINTERS        HobStart;\r
   EFI_PHYSICAL_ADDRESS        *Table;\r
   UINTN                       Index;\r
-  EFI_GUID                    *TableGuidArray[] = {\r
-    &gEfiAcpi20TableGuid, &gEfiAcpiTableGuid, &gEfiSmbiosTableGuid, &gEfiMpsTableGuid\r
-  };\r
 \r
   //\r
   // Get Hob List\r
@@ -63,8 +65,8 @@ Returns:
   //\r
   // Iteratively add ACPI Table, SMBIOS Table, MPS Table to EFI System Table\r
   //\r
-  for (Index = 0; Index < sizeof (TableGuidArray) / sizeof (*TableGuidArray); ++Index) {\r
-    GuidHob.Raw = GetNextGuidHob (TableGuidArray[Index], HobStart.Raw);\r
+  for (Index = 0; Index < sizeof (gTableGuidArray) / sizeof (*gTableGuidArray); ++Index) {\r
+    GuidHob.Raw = GetNextGuidHob (gTableGuidArray[Index], HobStart.Raw);\r
     if (GuidHob.Raw != NULL) {\r
       Table = GET_GUID_HOB_DATA (GuidHob.Guid);\r
       if (Table != NULL) {\r
@@ -73,8 +75,8 @@ Returns:
         // According to UEFI Spec, we should make sure Smbios table, \r
         // ACPI table and Mps tables kept in memory of specified type\r
         //\r
-        ConvertSystemTable(TableGuidArray[Index], (VOID**)&Table);\r
-        gBS->InstallConfigurationTable (TableGuidArray[Index], (VOID *)Table);\r
+        ConvertSystemTable(gTableGuidArray[Index], (VOID**)&Table);\r
+        gBS->InstallConfigurationTable (gTableGuidArray[Index], (VOID *)Table);\r
       }\r
     }\r
   }\r
@@ -145,13 +147,13 @@ UpdateMemoryMap (
   VOID\r
   )\r
 {\r
-  EFI_STATUS                  Status;\r
-  EFI_PEI_HOB_POINTERS        GuidHob;\r
-  VOID                        *Table;\r
-  MEMORY_DESC_HOB             MemoryDescHob;\r
-  UINTN                       Index;\r
-  EFI_PHYSICAL_ADDRESS        Memory;\r
-\r
+  EFI_STATUS                      Status;\r
+  EFI_PEI_HOB_POINTERS            GuidHob;\r
+  VOID                            *Table;\r
+  MEMORY_DESC_HOB                 MemoryDescHob;\r
+  UINTN                           Index;\r
+  EFI_PHYSICAL_ADDRESS            Memory;\r
+  EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;\r
   //\r
   // Get Hob List\r
   //\r
@@ -185,16 +187,62 @@ UpdateMemoryMap (
         (MemoryDescHob.MemDesc[Index].Type == EfiRuntimeServicesCode) ||\r
         (MemoryDescHob.MemDesc[Index].Type == EfiACPIReclaimMemory) ||\r
         (MemoryDescHob.MemDesc[Index].Type == EfiACPIMemoryNVS)) {\r
-      DEBUG ((EFI_D_ERROR, "PhysicalStart - 0x%x, ", MemoryDescHob.MemDesc[Index].PhysicalStart));\r
-      DEBUG ((EFI_D_ERROR, "PageNumber    - 0x%x, ", MemoryDescHob.MemDesc[Index].NumberOfPages));\r
-      DEBUG ((EFI_D_ERROR, "Type          - 0x%x\n", MemoryDescHob.MemDesc[Index].Type));\r
+      DEBUG ((EFI_D_ERROR, "PhysicalStart - 0x%016lx, ", MemoryDescHob.MemDesc[Index].PhysicalStart));\r
+      DEBUG ((EFI_D_ERROR, "PageNumber    - 0x%016lx, ", MemoryDescHob.MemDesc[Index].NumberOfPages));\r
+      DEBUG ((EFI_D_ERROR, "Attribute     - 0x%016lx, ", MemoryDescHob.MemDesc[Index].Attribute));\r
+      DEBUG ((EFI_D_ERROR, "Type          - 0x%08x\n", MemoryDescHob.MemDesc[Index].Type));\r
       if ((MemoryDescHob.MemDesc[Index].Type == EfiRuntimeServicesData) ||\r
           (MemoryDescHob.MemDesc[Index].Type == EfiRuntimeServicesCode)) {\r
         //\r
-        // Skip RuntimeSevicesData and RuntimeServicesCode, they are BFV\r
+        // For RuntimeSevicesData and RuntimeServicesCode, they are BFV or DxeCore.\r
+        // The memory type is assigned in EfiLdr\r
+        //\r
+        Status = gDS->GetMemorySpaceDescriptor (MemoryDescHob.MemDesc[Index].PhysicalStart, &Descriptor);\r
+        if (EFI_ERROR (Status)) {\r
+          continue;\r
+        }\r
+        if (Descriptor.GcdMemoryType != EfiGcdMemoryTypeReserved) {\r
+          //\r
+          // BFV or tested DXE core\r
+          //\r
+          continue;\r
+        }\r
+        //\r
+        // Untested DXE Core region, free and remove\r
+        //\r
+        Status = gDS->FreeMemorySpace (\r
+                        MemoryDescHob.MemDesc[Index].PhysicalStart,\r
+                        LShiftU64 (MemoryDescHob.MemDesc[Index].NumberOfPages, EFI_PAGE_SHIFT)\r
+                        );\r
+        if (EFI_ERROR (Status)) {\r
+          DEBUG ((EFI_D_ERROR, "FreeMemorySpace fail - %r!\n", Status));\r
+          continue;\r
+        }\r
+        Status = gDS->RemoveMemorySpace (\r
+                        MemoryDescHob.MemDesc[Index].PhysicalStart,\r
+                        LShiftU64 (MemoryDescHob.MemDesc[Index].NumberOfPages, EFI_PAGE_SHIFT)\r
+                        );\r
+        if (EFI_ERROR (Status)) {\r
+          DEBUG ((EFI_D_ERROR, "RemoveMemorySpace fail - %r!\n", Status));\r
+          continue;\r
+        }\r
+\r
+        //\r
+        // Convert Runtime type to BootTime type\r
+        //\r
+        if (MemoryDescHob.MemDesc[Index].Type == EfiRuntimeServicesData) {\r
+          MemoryDescHob.MemDesc[Index].Type = EfiBootServicesData;\r
+        } else {\r
+          MemoryDescHob.MemDesc[Index].Type = EfiBootServicesCode;\r
+        }\r
+\r
+        //\r
+        // PassThrough, let below code add and alloate.\r
         //\r
-        continue;\r
       }\r
+      //\r
+      // ACPI or reserved memory\r
+      //\r
       Status = gDS->AddMemorySpace (\r
                       EfiGcdMemoryTypeSystemMemory,\r
                       MemoryDescHob.MemDesc[Index].PhysicalStart,\r
@@ -202,7 +250,7 @@ UpdateMemoryMap (
                       MemoryDescHob.MemDesc[Index].Attribute\r
                       );\r
       if (EFI_ERROR (Status)) {\r
-        DEBUG ((EFI_D_ERROR, "AddMemorySpace fail!\n"));\r
+        DEBUG ((EFI_D_ERROR, "AddMemorySpace fail - %r!\n", Status));\r
         if ((MemoryDescHob.MemDesc[Index].Type == EfiACPIReclaimMemory) ||\r
             (MemoryDescHob.MemDesc[Index].Type == EfiACPIMemoryNVS)) {\r
           //\r
@@ -222,7 +270,7 @@ UpdateMemoryMap (
                       &Memory\r
                       );\r
       if (EFI_ERROR (Status)) {\r
-        DEBUG ((EFI_D_ERROR, "AllocatePages fail!\n"));\r
+        DEBUG ((EFI_D_ERROR, "AllocatePages fail - %r!\n", Status));\r
         //\r
         // For the page added, it must be allocated.\r
         //\r
@@ -347,8 +395,9 @@ Returns:
 \r
 \r
 VOID\r
+EFIAPI\r
 PlatformBdsInit (\r
-  IN EFI_BDS_ARCH_PROTOCOL_INSTANCE  *PrivateData\r
+  VOID\r
   )\r
 /*++\r
 \r
@@ -359,8 +408,6 @@ Routine Description:
 \r
 Arguments:\r
 \r
-  PrivateData  - The EFI_BDS_ARCH_PROTOCOL_INSTANCE instance\r
-\r
 Returns:\r
 \r
   None.\r
@@ -1115,8 +1162,8 @@ Returns:
 }\r
 \r
 VOID\r
+EFIAPI\r
 PlatformBdsPolicyBehavior (\r
-  IN EFI_BDS_ARCH_PROTOCOL_INSTANCE  *PrivateData,\r
   IN OUT LIST_ENTRY              *DriverOptionList,\r
   IN OUT LIST_ENTRY              *BootOptionList\r
   )\r
@@ -1130,8 +1177,6 @@ Routine Description:
   \r
 Arguments:\r
 \r
-  PrivateData      - The EFI_BDS_ARCH_PROTOCOL_INSTANCE instance\r
-  \r
   DriverOptionList - The header of the driver option link list\r
   \r
   BootOptionList   - The header of the boot option link list\r
@@ -1150,11 +1195,12 @@ Returns:
   UINTN                              Index;\r
   EFI_INPUT_KEY                      Key;\r
   EFI_TPL                            OldTpl;\r
+  EFI_BOOT_MODE                      BootMode;\r
 \r
   //\r
   // Init the time out value\r
   //\r
-  Timeout = BdsLibGetTimeout ();\r
+  Timeout = PcdGet16 (PcdPlatformBootTimeOut);\r
 \r
   //\r
   // Load the driver option as the driver option list\r
@@ -1164,14 +1210,14 @@ Returns:
   //\r
   // Get current Boot Mode\r
   //\r
-  Status = BdsLibGetBootMode (&PrivateData->BootMode);\r
-  DEBUG ((EFI_D_ERROR, "Boot Mode:%x\n", PrivateData->BootMode));\r
+  Status = BdsLibGetBootMode (&BootMode);\r
+  DEBUG ((EFI_D_ERROR, "Boot Mode:%x\n", BootMode));\r
 \r
   //\r
   // Go the different platform policy with different boot mode\r
   // Notes: this part code can be change with the table policy\r
   //\r
-  ASSERT (PrivateData->BootMode == BOOT_WITH_FULL_CONFIGURATION);\r
+  ASSERT (BootMode == BOOT_WITH_FULL_CONFIGURATION);\r
   //\r
   // Connect platform console\r
   //\r
@@ -1209,6 +1255,8 @@ Returns:
   // Give one chance to enter the setup if we\r
   // have the time out\r
   //\r
+  // BUGBUG: hard code timeout to 5 second to show logo in graphic mode.\r
+  Timeout = 5;  \r
   if (Timeout != 0) {\r
     PlatformBdsEnterFrontPage (Timeout, FALSE);\r
   }\r
@@ -1283,6 +1331,7 @@ Returns:
 }\r
 \r
 VOID\r
+EFIAPI\r
 PlatformBdsBootSuccess (\r
   IN  BDS_COMMON_OPTION *Option\r
   )\r
@@ -1319,6 +1368,7 @@ Returns:
 }\r
 \r
 VOID\r
+EFIAPI\r
 PlatformBdsBootFail (\r
   IN  BDS_COMMON_OPTION  *Option,\r
   IN  EFI_STATUS         Status,\r
@@ -1458,38 +1508,6 @@ Returns:
   return EFI_UNSUPPORTED;\r
 }  \r
 \r
-UINT8\r
-GetBufferCheckSum (\r
-  IN VOID *      Buffer,\r
-  IN UINTN       Length\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-  Caculate buffer checksum (8-bit)\r
-\r
-Arguments:\r
-  Buffer - Pointer to Buffer that to be caculated\r
-  Length - How many bytes are to be caculated  \r
-\r
-Returns:\r
-  Checksum of the buffer\r
-\r
---*/\r
-{\r
-  UINT8   CheckSum;\r
-  UINT8   *Ptr8;\r
-  \r
-  CheckSum = 0;\r
-  Ptr8 = (UINT8 *) Buffer;\r
-  \r
-  while (Length > 0) {\r
-    CheckSum = (UINT8) (CheckSum + *Ptr8++);\r
-    Length--;\r
-  }\r
-  \r
-  return (UINT8)((0xFF - CheckSum) + 1);\r
-}  \r
 \r
 EFI_STATUS\r
 ConvertAcpiTable (\r
@@ -1624,7 +1642,7 @@ Returns:
   SmbiosTableNew->TableAddress = (UINT32)BufferPtr;\r
   SmbiosTableNew->IntermediateChecksum = 0;\r
   SmbiosTableNew->IntermediateChecksum = \r
-          GetBufferCheckSum ((UINT8*)SmbiosTableNew + 0x10, SmbiosEntryLen -0x10);\r
+          CalculateCheckSum8 ((UINT8*)SmbiosTableNew + 0x10, SmbiosEntryLen -0x10);\r
   //\r
   // Change the SMBIOS pointer\r
   //\r
@@ -1723,10 +1741,10 @@ Returns:
         MpsTableNew->OemTablePointer = (UINT32)(UINTN)OemTableNew;\r
     }\r
     MpsTableNew->Checksum = 0;\r
-    MpsTableNew->Checksum = GetBufferCheckSum (MpsTableNew, MpsTableOri->BaseTableLength);\r
+    MpsTableNew->Checksum = CalculateCheckSum8 ((UINT8*)MpsTableNew, MpsTableOri->BaseTableLength);\r
     MpsFloatingPointerNew->PhysicalAddress = (UINT32)(UINTN)MpsTableNew;\r
     MpsFloatingPointerNew->Checksum = 0;\r
-    MpsFloatingPointerNew->Checksum = GetBufferCheckSum (MpsFloatingPointerNew, FPLength);\r
+    MpsFloatingPointerNew->Checksum = CalculateCheckSum8 ((UINT8*)MpsFloatingPointerNew, FPLength);\r
   }\r
   //\r
   // Change the pointer\r