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