]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Mem/Pool.c
Enhance the error handling for AllocatePool and AllocatePages function.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Mem / Pool.c
index c60761a0455c375de063eeb300e8e6c5c8b7ff98..a435b9760c0e3e9dd812c7b75498efa4af2ad130 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   UEFI Memory pool management functions.\r
 \r
-Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2012, 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
@@ -13,8 +13,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 **/\r
 \r
 #include "DxeMain.h"\r
+#include "Imem.h"\r
 \r
-#define POOL_FREE_SIGNATURE   EFI_SIGNATURE_32('p','f','r','0')\r
+#define POOL_FREE_SIGNATURE   SIGNATURE_32('p','f','r','0')\r
 typedef struct {\r
   UINT32          Signature;\r
   UINT32          Index;\r
@@ -22,7 +23,7 @@ typedef struct {
 } POOL_FREE;\r
 \r
 \r
-#define POOL_HEAD_SIGNATURE   EFI_SIGNATURE_32('p','h','d','0')\r
+#define POOL_HEAD_SIGNATURE   SIGNATURE_32('p','h','d','0')\r
 typedef struct {\r
   UINT32          Signature;\r
   UINT32          Size;\r
@@ -31,9 +32,9 @@ typedef struct {
   CHAR8           Data[1];\r
 } POOL_HEAD;\r
 \r
-#define SIZE_OF_POOL_HEAD EFI_FIELD_OFFSET(POOL_HEAD,Data)\r
+#define SIZE_OF_POOL_HEAD OFFSET_OF(POOL_HEAD,Data)\r
 \r
-#define POOL_TAIL_SIGNATURE   EFI_SIGNATURE_32('p','t','a','l')\r
+#define POOL_TAIL_SIGNATURE   SIGNATURE_32('p','t','a','l')\r
 typedef struct {\r
   UINT32      Signature;\r
   UINT32      Size;\r
@@ -59,7 +60,7 @@ typedef struct {
 // Globals\r
 //\r
 \r
-#define POOL_SIGNATURE  EFI_SIGNATURE_32('p','l','s','t')\r
+#define POOL_SIGNATURE  SIGNATURE_32('p','l','s','t')\r
 typedef struct {\r
     INTN             Signature;\r
     UINTN            Used;\r
@@ -76,7 +77,7 @@ POOL            mPoolHead[EfiMaxMemoryType];
 //\r
 // List of pool header to search for the appropriate memory type.\r
 //\r
-LIST_ENTRY      mPoolHeadList;\r
+LIST_ENTRY      mPoolHeadList = INITIALIZE_LIST_HEAD_VARIABLE (mPoolHeadList);\r
 \r
 \r
 /**\r
@@ -99,7 +100,6 @@ CoreInitializePool (
         InitializeListHead (&mPoolHead[Type].FreeList[Index]);\r
     }\r
   }\r
-  InitializeListHead (&mPoolHeadList);\r
 }\r
 \r
 \r
@@ -124,7 +124,11 @@ LookupPoolHead (
     return &mPoolHead[MemoryType];\r
   }\r
 \r
-  if (MemoryType < 0) {\r
+  //\r
+  // MemoryType values in the range 0x80000000..0xFFFFFFFF are reserved for use by UEFI \r
+  // OS loaders that are provided by operating system vendors\r
+  //\r
+  if (MemoryType >= (INT32)0x80000000 && MemoryType <= (INT32)0xffffffff) {\r
 \r
     for (Link = mPoolHeadList.ForwardLink; Link != &mPoolHeadList; Link = Link->ForwardLink) {\r
       Pool = CR(Link, POOL, Link, POOL_SIGNATURE);\r
@@ -163,7 +167,7 @@ LookupPoolHead (
   @param  Buffer                 The address to return a pointer to the allocated\r
                                  pool\r
 \r
-  @retval EFI_INVALID_PARAMETER  PoolType not valid\r
+  @retval EFI_INVALID_PARAMETER  PoolType not valid or Buffer is NULL. \r
   @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.\r
   @retval EFI_SUCCESS            Pool successfully allocated.\r
 \r
@@ -186,6 +190,10 @@ CoreAllocatePool (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (Buffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   *Buffer = NULL;\r
 \r
   //\r
@@ -333,10 +341,10 @@ Done:
 \r
     DEBUG ((\r
       DEBUG_POOL,\r
-      "AllocatePoolI: Type %x, Addr %x (len %x) %,d\n", PoolType,\r
+      "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType,\r
       Buffer,\r
-      Size - POOL_OVERHEAD,\r
-      Pool->Used\r
+      (UINT64)(Size - POOL_OVERHEAD),\r
+      (UINT64) Pool->Used\r
       ));\r
 \r
     //\r
@@ -345,7 +353,7 @@ Done:
     Pool->Used += Size;\r
 \r
   } else {\r
-    DEBUG ((DEBUG_ERROR | DEBUG_POOL, "AllocatePool: failed to allocate %d bytes\n", Size));\r
+    DEBUG ((DEBUG_ERROR | DEBUG_POOL, "AllocatePool: failed to allocate %ld bytes\n", (UINT64) Size));\r
   }\r
 \r
   return Buffer;\r
@@ -409,19 +417,19 @@ CoreFreePoolI (
   UINTN       Offset;\r
   BOOLEAN     AllFree;\r
 \r
-  ASSERT(NULL != Buffer);\r
+  ASSERT(Buffer != NULL);\r
   //\r
   // Get the head & tail of the pool entry\r
   //\r
   Head = CR (Buffer, POOL_HEAD, Data, POOL_HEAD_SIGNATURE);\r
-  ASSERT(NULL != Head);\r
+  ASSERT(Head != NULL);\r
 \r
   if (Head->Signature != POOL_HEAD_SIGNATURE) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   Tail = HEAD_TO_TAIL (Head);\r
-  ASSERT(NULL != Tail);\r
+  ASSERT(Tail != NULL);\r
 \r
   //\r
   // Debug\r
@@ -447,7 +455,7 @@ CoreFreePoolI (
     return EFI_INVALID_PARAMETER;\r
   }\r
   Pool->Used -= Size;\r
-  DEBUG ((DEBUG_POOL, "FreePool: %x (len %x) %,d\n", Head->Data, Head->Size - POOL_OVERHEAD, Pool->Used));\r
+  DEBUG ((DEBUG_POOL, "FreePool: %p (len %lx) %,ld\n", Head->Data, (UINT64)(Head->Size - POOL_OVERHEAD), (UINT64) Pool->Used));\r
 \r
   //\r
   // Determine the pool list\r
@@ -473,7 +481,7 @@ CoreFreePoolI (
     // Put the pool entry onto the free pool list\r
     //\r
     Free = (POOL_FREE *) Head;\r
-    ASSERT(NULL != Free);\r
+    ASSERT(Free != NULL);\r
     Free->Signature = POOL_FREE_SIGNATURE;\r
     Free->Index     = (UINT32)Index;\r
     InsertHeadList (&Pool->FreeList[Index], &Free->Link);\r
@@ -497,7 +505,7 @@ CoreFreePoolI (
         FSize = LIST_TO_SIZE(Index);\r
         while (Offset + FSize <= DEFAULT_PAGE_ALLOCATION) {\r
           Free = (POOL_FREE *) &NewPage[Offset];\r
-          ASSERT(NULL != Free);\r
+          ASSERT(Free != NULL);\r
           if (Free->Signature != POOL_FREE_SIGNATURE) {\r
             AllFree = FALSE;\r
           }\r
@@ -514,7 +522,7 @@ CoreFreePoolI (
         // Remove all of these pool entries from the free loop lists.\r
         //\r
         Free = (POOL_FREE *) &NewPage[0];\r
-        ASSERT(NULL != Free);\r
+        ASSERT(Free != NULL);\r
         Index = Free->Index;\r
         Offset = 0;\r
 \r
@@ -522,7 +530,7 @@ CoreFreePoolI (
           FSize = LIST_TO_SIZE(Index);\r
           while (Offset + FSize <= DEFAULT_PAGE_ALLOCATION) {\r
             Free = (POOL_FREE *) &NewPage[Offset];\r
-            ASSERT(NULL != Free);\r
+            ASSERT(Free != NULL);\r
             RemoveEntryList (&Free->Link);\r
             Offset += FSize;\r
           }\r