]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Pei/Memory/MemoryServices.c
Refine code to make it more safely.
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Memory / MemoryServices.c
index 02e83a18fc8543af6f393c9f4519991beeeb8b48..10f21d5628b2d833d8fb585e2706eb99af12cbbf 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   EFI PEI Core memory services\r
   \r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
+Copyright (c) 2006 - 2014, 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
@@ -52,9 +52,9 @@ InitializeMemoryServices (
       );\r
 \r
     //\r
-    // Set PS to point to ServiceTableShadow in Cache\r
+    // Set Ps to point to ServiceTableShadow in Cache\r
     //\r
-    PrivateData->PS = &(PrivateData->ServiceTableShadow);\r
+    PrivateData->Ps = &(PrivateData->ServiceTableShadow);\r
   }\r
   \r
   return;\r
@@ -110,20 +110,20 @@ PeiInstallPeiMemory (
 }\r
 \r
 /**\r
+  The purpose of the service is to publish an interface that allows \r
+  PEIMs to allocate memory ranges that are managed by the PEI Foundation.\r
 \r
-  Memory allocation service on permanent memory,\r
-  not usable prior to the memory installation.\r
+  @param  PeiServices      An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
+  @param  MemoryType       The type of memory to allocate.\r
+  @param  Pages            The number of contiguous 4 KB pages to allocate.\r
+  @param  Memory           Pointer to a physical address. On output, the address is set to the base \r
+                           of the page range that was allocated.\r
 \r
-\r
-  @param PeiServices     An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
-  @param MemoryType      Type of memory to allocate.\r
-  @param Pages           Number of pages to allocate.\r
-  @param Memory          Pointer of memory allocated.\r
-\r
-  @retval EFI_SUCCESS              The allocation was successful\r
-  @retval EFI_NOT_AVAILABLE_YET    Called with permanent memory not available\r
-  @retval EFI_OUT_OF_RESOURCES     There is not enough HOB heap to satisfy the requirement\r
-                                   to allocate the number of pages.\r
+  @retval EFI_SUCCESS           The memory range was successfully allocated.\r
+  @retval EFI_OUT_OF_RESOURCES  The pages could not be allocated.\r
+  @retval EFI_INVALID_PARAMETER Type is not equal to EfiLoaderCode, EfiLoaderData, EfiRuntimeServicesCode, \r
+                                EfiRuntimeServicesData, EfiBootServicesCode, EfiBootServicesData,\r
+                                EfiACPIReclaimMemory, or EfiACPIMemoryNVS.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -141,6 +141,17 @@ PeiAllocatePages (
   EFI_PHYSICAL_ADDRESS                    *FreeMemoryBottom;\r
   UINTN                                   RemainingPages;\r
 \r
+  if ((MemoryType != EfiLoaderCode) &&\r
+      (MemoryType != EfiLoaderData) &&\r
+      (MemoryType != EfiRuntimeServicesCode) &&\r
+      (MemoryType != EfiRuntimeServicesData) &&\r
+      (MemoryType != EfiBootServicesCode) &&\r
+      (MemoryType != EfiBootServicesData) &&\r
+      (MemoryType != EfiACPIReclaimMemory) &&\r
+      (MemoryType != EfiACPIMemoryNVS)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
   Hob.Raw     = PrivateData->HobList.Raw;\r
   \r
@@ -149,7 +160,7 @@ PeiAllocatePages (
   //\r
   if (!PrivateData->PeiMemoryInstalled) {\r
     //\r
-    // When PeiInstallMemory is called but CAR has *not* been moved to temporary memory,\r
+    // When PeiInstallMemory is called but temporary memory has *not* been moved to temporary memory,\r
     // the AllocatePage will depend on the field of PEI_CORE_INSTANCE structure.\r
     //\r
     if (!PrivateData->SwitchStackSignal) {\r
@@ -171,7 +182,7 @@ PeiAllocatePages (
   //\r
   // Verify that there is sufficient memory to satisfy the allocation\r
   //\r
-  RemainingPages = EFI_SIZE_TO_PAGES ((UINTN) (*FreeMemoryTop - *FreeMemoryBottom));\r
+  RemainingPages = (UINTN)(*FreeMemoryTop - *FreeMemoryBottom) >> EFI_PAGE_SHIFT;\r
   //\r
   // For page allocation, the overhead sizeof (EFI_HOB_MEMORY_ALLOCATION) needs one extra page.\r
   // So the number of remaining pages needs to be greater than that of the request pages.\r
@@ -207,7 +218,7 @@ PeiAllocatePages (
 /**\r
 \r
   Pool allocation service. Before permenent memory is discoveried, the pool will \r
-  be allocated the heap in the CAR. Genenrally, the size of heap in temporary \r
+  be allocated the heap in the temporary memory. Genenrally, the size of heap in temporary \r
   memory does not exceed to 64K, so the biggest pool size could be allocated is \r
   64K.\r
 \r
@@ -238,14 +249,18 @@ PeiAllocatePool (
   \r
   //\r
   // Generally, the size of heap in temporary memory does not exceed to 64K,\r
-  // so the maxmium size of pool is 0x10000 - sizeof (EFI_HOB_MEMORY_POOL)\r
+  // HobLength is multiples of 8 bytes, so the maxmium size of pool is 0xFFF8 - sizeof (EFI_HOB_MEMORY_POOL)\r
   //\r
-  ASSERT (Size < 0x10000 - sizeof (EFI_HOB_MEMORY_POOL));\r
+  if (Size > (0xFFF8 - sizeof (EFI_HOB_MEMORY_POOL))) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  \r
   Status = PeiServicesCreateHob (\r
              EFI_HOB_TYPE_MEMORY_POOL,\r
              (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + Size),\r
              (VOID **)&Hob\r
              );\r
+  ASSERT_EFI_ERROR (Status);\r
   *Buffer = Hob+1;  \r
 \r
   return Status;\r