1 /** @file
2   Implementation of the 6 PEI Ffs (FV) APIs in library form.
3 
4   Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5 
6   This program and the accompanying materials
7   are licensed and made available under the terms and conditions of the BSD License
8   which accompanies this distribution.  The full text of the license may be found at
9   http://opensource.org/licenses/bsd-license.php
10 
11   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 
14 **/
15 
16 #include <PiPei.h>
17 
18 #include <Library/BaseLib.h>
19 #include <Library/PrePiLib.h>
20 #include <Library/DebugLib.h>
21 
22 
23 
24 /**
25   Allocates one or more 4KB pages of type EfiBootServicesData.
26 
27   Allocates the number of 4KB pages of MemoryType and returns a pointer to the
28   allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If Pages is 0, then NULL
29   is returned.  If there is not enough memory remaining to satisfy the request, then NULL is
30   returned.
31 
32   @param  Pages                 The number of 4 KB pages to allocate.
33 
34   @return A pointer to the allocated buffer or NULL if allocation fails.
35 
36 **/
37 VOID *
38 EFIAPI
AllocatePages(IN UINTN Pages)39 AllocatePages (
40   IN UINTN            Pages
41   )
42 {
43   EFI_PEI_HOB_POINTERS                    Hob;
44   EFI_PHYSICAL_ADDRESS                    Offset;
45 
46   Hob.Raw = GetHobList ();
47 
48   // Check to see if on 4k boundary
49   Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;
50   if (Offset != 0) {
51     // If not aligned, make the allocation aligned.
52     Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;
53   }
54 
55   //
56   // Verify that there is sufficient memory to satisfy the allocation
57   //
58   if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) {
59     return 0;
60   } else {
61     //
62     // Update the PHIT to reflect the memory usage
63     //
64     Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
65 
66     // This routine used to create a memory allocation HOB a la PEI, but that's not
67     // necessary for us.
68 
69     //
70     // Create a memory allocation HOB.
71     //
72     BuildMemoryAllocationHob (
73         Hob.HandoffInformationTable->EfiFreeMemoryTop,
74         Pages * EFI_PAGE_SIZE,
75         EfiBootServicesData
76         );
77     return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
78   }
79 }
80 
81 
82 /**
83   Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
84 
85   Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
86   alignment specified by Alignment.  The allocated buffer is returned.  If Pages is 0, then NULL is
87   returned.  If there is not enough memory at the specified alignment remaining to satisfy the
88   request, then NULL is returned.
89   If Alignment is not a power of two and Alignment is not zero, then ASSERT().
90 
91   @param  Pages                 The number of 4 KB pages to allocate.
92   @param  Alignment             The requested alignment of the allocation.  Must be a power of two.
93                                 If Alignment is zero, then byte alignment is used.
94 
95   @return A pointer to the allocated buffer or NULL if allocation fails.
96 
97 **/
98 VOID *
99 EFIAPI
AllocateAlignedPages(IN UINTN Pages,IN UINTN Alignment)100 AllocateAlignedPages (
101   IN UINTN  Pages,
102   IN UINTN  Alignment
103   )
104 {
105   VOID    *Memory;
106   UINTN   AlignmentMask;
107 
108   //
109   // Alignment must be a power of two or zero.
110   //
111   ASSERT ((Alignment & (Alignment - 1)) == 0);
112 
113   if (Pages == 0) {
114     return NULL;
115   }
116   //
117   // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
118   //
119   ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));
120   //
121   // We would rather waste some memory to save PEI code size.
122   //
123   Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));
124   if (Alignment == 0) {
125     AlignmentMask = Alignment;
126   } else {
127     AlignmentMask = Alignment - 1;
128   }
129   return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask);
130 }
131 
132 
133 /**
134   Frees one or more 4KB pages that were previously allocated with one of the page allocation
135   functions in the Memory Allocation Library.
136 
137   Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer.  Buffer
138   must have been allocated on a previous call to the page allocation services of the Memory
139   Allocation Library.  If it is not possible to free allocated pages, then this function will
140   perform no actions.
141 
142   If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
143   then ASSERT().
144   If Pages is zero, then ASSERT().
145 
146   @param  Buffer                Pointer to the buffer of pages to free.
147   @param  Pages                 The number of 4 KB pages to free.
148 
149 **/
150 VOID
151 EFIAPI
FreePages(IN VOID * Buffer,IN UINTN Pages)152 FreePages (
153   IN VOID   *Buffer,
154   IN UINTN  Pages
155   )
156 {
157   // For now, we do not support the ability to free pages in the PrePei Memory Allocator.
158   // The allocated memory is lost.
159 }
160 
161 /**
162   Allocates a buffer of type EfiBootServicesData.
163 
164   Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
165   pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
166   returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.
167 
168   @param  AllocationSize        The number of bytes to allocate.
169 
170   @return A pointer to the allocated buffer or NULL if allocation fails.
171 
172 **/
173 VOID *
174 EFIAPI
AllocatePool(IN UINTN AllocationSize)175 AllocatePool (
176   IN UINTN  AllocationSize
177   )
178 {
179   EFI_HOB_MEMORY_POOL      *Hob;
180 
181   Hob = GetHobList ();
182 
183 
184   //
185   // Verify that there is sufficient memory to satisfy the allocation
186   //
187   if (AllocationSize > 0x10000) {
188     // Please call AllcoatePages for big allocations
189     return 0;
190   } else {
191 
192     Hob = (EFI_HOB_MEMORY_POOL *)CreateHob (EFI_HOB_TYPE_MEMORY_POOL, (UINT16)(sizeof (EFI_HOB_TYPE_MEMORY_POOL) + AllocationSize));
193     return (VOID *)(Hob + 1);
194   }
195 }
196 
197 /**
198   Frees a buffer that was previously allocated with one of the pool allocation functions in the
199   Memory Allocation Library.
200 
201   Frees the buffer specified by Buffer.  Buffer must have been allocated on a previous call to the
202   pool allocation services of the Memory Allocation Library.  If it is not possible to free pool
203   resources, then this function will perform no actions.
204 
205   If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
206   then ASSERT().
207 
208   @param  Buffer                Pointer to the buffer to free.
209 
210 **/
211 VOID
212 EFIAPI
FreePool(IN VOID * Buffer)213 FreePool (
214   IN VOID   *Buffer
215   )
216 {
217   // Not implemented yet
218 }
219