]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
ArmPlatformPkg: Remove PcdStandalone from Sec module and Introduce ArmPlatformSecExtr...
[mirror_edk2.git] / EmbeddedPkg / Library / PrePiMemoryAllocationLib / MemoryAllocationLib.c
index 7cfc28e1e7293d42ef4758080f534c9b3ac5d237..85a32b7362052bcb8c02a7876a2c129b2236e981 100644 (file)
-/** @file
-  Implementation of the 6 PEI Ffs (FV) APIs in library form.
-
-  Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
-  
-  This program and the accompanying materials
-  are licensed and made available under the terms and conditions of the BSD License
-  which accompanies this distribution.  The full text of the license may be found at
-  http://opensource.org/licenses/bsd-license.php
-
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-
-**/
-
-#include <PiPei.h>
-
-#include <Library/BaseLib.h>
-#include <Library/PrePiLib.h>
-#include <Library/DebugLib.h>
-
-
-
-/**
-  Allocates one or more 4KB pages of type EfiBootServicesData.
-
-  Allocates the number of 4KB pages of MemoryType and returns a pointer to the
-  allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If Pages is 0, then NULL
-  is returned.  If there is not enough memory remaining to satisfy the request, then NULL is
-  returned.
-
-  @param  Pages                 The number of 4 KB pages to allocate.
-
-  @return A pointer to the allocated buffer or NULL if allocation fails.
-
-**/
-VOID *
-EFIAPI
-AllocatePages (
-  IN UINTN            Pages
-  )
-{
-  EFI_PEI_HOB_POINTERS                    Hob;
-  EFI_PHYSICAL_ADDRESS                    Offset;
-
-  Hob.Raw = GetHobList ();
-
-  // Check to see if on 4k boundary
-  Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;
-  if (Offset != 0) {
-    // If not aligned, make the allocation aligned.
-    Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;
-  }
-
-  //
-  // Verify that there is sufficient memory to satisfy the allocation
-  //
-  if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) {
-    return 0;
-  } else {
-    //
-    // Update the PHIT to reflect the memory usage
-    //
-    Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
-
-    // This routine used to create a memory allocation HOB a la PEI, but that's not
-    // necessary for us.
-
-    //
-    // Create a memory allocation HOB.
-    //
-    BuildMemoryAllocationHob (
-        Hob.HandoffInformationTable->EfiFreeMemoryTop,
-        Pages * EFI_PAGE_SIZE,
-        EfiBootServicesData
-        );
-    return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
-  }
-}
-
-
-/**
-  Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
-
-  Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
-  alignment specified by Alignment.  The allocated buffer is returned.  If Pages is 0, then NULL is
-  returned.  If there is not enough memory at the specified alignment remaining to satisfy the
-  request, then NULL is returned.
-  If Alignment is not a power of two and Alignment is not zero, then ASSERT().
-
-  @param  Pages                 The number of 4 KB pages to allocate.
-  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.
-                                If Alignment is zero, then byte alignment is used.
-
-  @return A pointer to the allocated buffer or NULL if allocation fails.
-
-**/
-VOID *
-EFIAPI
-AllocateAlignedPages (
-  IN UINTN  Pages,
-  IN UINTN  Alignment
-  )
-{
-  VOID    *Memory;
-  UINTN   AlignmentMask;
-
-  //
-  // Alignment must be a power of two or zero.
-  //
-  ASSERT ((Alignment & (Alignment - 1)) == 0);
-
-  if (Pages == 0) {
-    return NULL;
-  }
-  //
-  // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
-  //
-  ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));
-  //
-  // We would rather waste some memory to save PEI code size.
-  //
-  Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));
-  if (Alignment == 0) {
-    AlignmentMask = Alignment;
-  } else {
-    AlignmentMask = Alignment - 1;
-  }
-  return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask);
-}
-
-
-
-
-/**
-  Allocates a buffer of type EfiBootServicesData.
-
-  Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
-  pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
-  returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.
-
-  @param  AllocationSize        The number of bytes to allocate.
-
-  @return A pointer to the allocated buffer or NULL if allocation fails.
-
-**/
-VOID *
-EFIAPI
-AllocatePool (
-  IN UINTN  AllocationSize
-  )
-{
-  EFI_HOB_MEMORY_POOL      *Hob;
-
-  Hob = GetHobList ();
-
-  
-  //
-  // Verify that there is sufficient memory to satisfy the allocation
-  //
-  if (AllocationSize > 0x10000) {
-    // Please call AllcoatePages for big allocations
-    return 0;
-  } else {
-
-    Hob = (EFI_HOB_MEMORY_POOL *)CreateHob (EFI_HOB_TYPE_MEMORY_POOL, (UINT16)(sizeof (EFI_HOB_TYPE_MEMORY_POOL) + AllocationSize));
-    return (VOID *)(Hob + 1);
-  }
-}
-
-/**
-  Frees a buffer that was previously allocated with one of the pool allocation functions in the
-  Memory Allocation Library.
-
-  Frees the buffer specified by Buffer.  Buffer must have been allocated on a previous call to the
-  pool allocation services of the Memory Allocation Library.  If it is not possible to free pool
-  resources, then this function will perform no actions.
-
-  If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
-  then ASSERT().
-
-  @param  Buffer                Pointer to the buffer to free.
-
-**/
-VOID
-EFIAPI
-FreePool (
-  IN VOID   *Buffer
-  )
-{
-  // Not implemented yet
-}
+/** @file\r
+  Implementation of the 6 PEI Ffs (FV) APIs in library form.\r
+\r
+  Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
+  \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 <PiPei.h>\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/PrePiLib.h>\r
+#include <Library/DebugLib.h>\r
+\r
+\r
+\r
+/**\r
+  Allocates one or more 4KB pages of type EfiBootServicesData.\r
+\r
+  Allocates the number of 4KB pages of MemoryType and returns a pointer to the\r
+  allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If Pages is 0, then NULL\r
+  is returned.  If there is not enough memory remaining to satisfy the request, then NULL is\r
+  returned.\r
+\r
+  @param  Pages                 The number of 4 KB pages to allocate.\r
+\r
+  @return A pointer to the allocated buffer or NULL if allocation fails.\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+AllocatePages (\r
+  IN UINTN            Pages\r
+  )\r
+{\r
+  EFI_PEI_HOB_POINTERS                    Hob;\r
+  EFI_PHYSICAL_ADDRESS                    Offset;\r
+\r
+  Hob.Raw = GetHobList ();\r
+\r
+  // Check to see if on 4k boundary\r
+  Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;\r
+  if (Offset != 0) {\r
+    // If not aligned, make the allocation aligned.\r
+    Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;\r
+  }\r
+\r
+  //\r
+  // Verify that there is sufficient memory to satisfy the allocation\r
+  //\r
+  if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) {\r
+    return 0;\r
+  } else {\r
+    //\r
+    // Update the PHIT to reflect the memory usage\r
+    //\r
+    Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;\r
+\r
+    // This routine used to create a memory allocation HOB a la PEI, but that's not\r
+    // necessary for us.\r
+\r
+    //\r
+    // Create a memory allocation HOB.\r
+    //\r
+    BuildMemoryAllocationHob (\r
+        Hob.HandoffInformationTable->EfiFreeMemoryTop,\r
+        Pages * EFI_PAGE_SIZE,\r
+        EfiBootServicesData\r
+        );\r
+    return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;\r
+  }\r
+}\r
+\r
+\r
+/**\r
+  Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r
+\r
+  Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r
+  alignment specified by Alignment.  The allocated buffer is returned.  If Pages is 0, then NULL is\r
+  returned.  If there is not enough memory at the specified alignment remaining to satisfy the\r
+  request, then NULL is returned.\r
+  If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+\r
+  @param  Pages                 The number of 4 KB pages to allocate.\r
+  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.\r
+                                If Alignment is zero, then byte alignment is used.\r
+\r
+  @return A pointer to the allocated buffer or NULL if allocation fails.\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+AllocateAlignedPages (\r
+  IN UINTN  Pages,\r
+  IN UINTN  Alignment\r
+  )\r
+{\r
+  VOID    *Memory;\r
+  UINTN   AlignmentMask;\r
+\r
+  //\r
+  // Alignment must be a power of two or zero.\r
+  //\r
+  ASSERT ((Alignment & (Alignment - 1)) == 0);\r
+\r
+  if (Pages == 0) {\r
+    return NULL;\r
+  }\r
+  //\r
+  // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
+  //\r
+  ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));\r
+  //\r
+  // We would rather waste some memory to save PEI code size.\r
+  //\r
+  Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));\r
+  if (Alignment == 0) {\r
+    AlignmentMask = Alignment;\r
+  } else {\r
+    AlignmentMask = Alignment - 1;\r
+  }\r
+  return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask);\r
+}\r
+\r
+\r
+\r
+\r
+/**\r
+  Allocates a buffer of type EfiBootServicesData.\r
+\r
+  Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
+  pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is\r
+  returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
+\r
+  @param  AllocationSize        The number of bytes to allocate.\r
+\r
+  @return A pointer to the allocated buffer or NULL if allocation fails.\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+AllocatePool (\r
+  IN UINTN  AllocationSize\r
+  )\r
+{\r
+  EFI_HOB_MEMORY_POOL      *Hob;\r
+\r
+  Hob = GetHobList ();\r
+\r
+  \r
+  //\r
+  // Verify that there is sufficient memory to satisfy the allocation\r
+  //\r
+  if (AllocationSize > 0x10000) {\r
+    // Please call AllcoatePages for big allocations\r
+    return 0;\r
+  } else {\r
+\r
+    Hob = (EFI_HOB_MEMORY_POOL *)CreateHob (EFI_HOB_TYPE_MEMORY_POOL, (UINT16)(sizeof (EFI_HOB_TYPE_MEMORY_POOL) + AllocationSize));\r
+    return (VOID *)(Hob + 1);\r
+  }\r
+}\r
+\r
+/**\r
+  Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
+  Memory Allocation Library.\r
+\r
+  Frees the buffer specified by Buffer.  Buffer must have been allocated on a previous call to the\r
+  pool allocation services of the Memory Allocation Library.  If it is not possible to free pool\r
+  resources, then this function will perform no actions.\r
+\r
+  If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
+  then ASSERT().\r
+\r
+  @param  Buffer                Pointer to the buffer to free.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+FreePool (\r
+  IN VOID   *Buffer\r
+  )\r
+{\r
+  // Not implemented yet\r
+}\r