]>
Commit | Line | Data |
---|---|---|
2ef2b01e A |
1 | /** @file |
2 | Implementation of the 6 PEI Ffs (FV) APIs in library form. | |
3 | ||
60274cca | 4 | Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR> |
2ef2b01e | 5 | |
60274cca | 6 | This program and the accompanying materials |
2ef2b01e A |
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 <PrePi.h> | |
17 | ||
18 | ||
19 | ||
20 | /** | |
21 | Allocates one or more 4KB pages of type EfiBootServicesData. | |
22 | ||
23 | Allocates the number of 4KB pages of MemoryType and returns a pointer to the | |
24 | allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL | |
25 | is returned. If there is not enough memory remaining to satisfy the request, then NULL is | |
26 | returned. | |
27 | ||
28 | @param Pages The number of 4 KB pages to allocate. | |
29 | ||
30 | @return A pointer to the allocated buffer or NULL if allocation fails. | |
31 | ||
32 | **/ | |
33 | VOID * | |
34 | EFIAPI | |
35 | AllocatePages ( | |
36 | IN UINTN Pages | |
37 | ) | |
38 | { | |
39 | EFI_PEI_HOB_POINTERS Hob; | |
40 | EFI_PHYSICAL_ADDRESS Offset; | |
41 | ||
42 | Hob.Raw = GetHobList (); | |
43 | ||
44 | // Check to see if on 4k boundary | |
45 | Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF; | |
46 | if (Offset != 0) { | |
47 | // If not aligned, make the allocation aligned. | |
48 | Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset; | |
49 | } | |
50 | ||
51 | // | |
52 | // Verify that there is sufficient memory to satisfy the allocation | |
53 | // | |
54 | if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) { | |
55 | return 0; | |
56 | } else { | |
57 | // | |
58 | // Update the PHIT to reflect the memory usage | |
59 | // | |
60 | Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE; | |
61 | ||
62 | // This routine used to create a memory allocation HOB a la PEI, but that's not | |
63 | // necessary for us. | |
64 | ||
65 | return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop; | |
66 | } | |
67 | } | |
68 | ||
69 | ||
70 | /** | |
71 | Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment. | |
72 | ||
73 | Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an | |
74 | alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is | |
75 | returned. If there is not enough memory at the specified alignment remaining to satisfy the | |
76 | request, then NULL is returned. | |
77 | If Alignment is not a power of two and Alignment is not zero, then ASSERT(). | |
78 | ||
79 | @param Pages The number of 4 KB pages to allocate. | |
80 | @param Alignment The requested alignment of the allocation. Must be a power of two. | |
81 | If Alignment is zero, then byte alignment is used. | |
82 | ||
83 | @return A pointer to the allocated buffer or NULL if allocation fails. | |
84 | ||
85 | **/ | |
86 | VOID * | |
87 | EFIAPI | |
88 | AllocateAlignedPages ( | |
89 | IN UINTN Pages, | |
90 | IN UINTN Alignment | |
91 | ) | |
92 | { | |
93 | VOID *Memory; | |
94 | UINTN AlignmentMask; | |
95 | ||
96 | // | |
97 | // Alignment must be a power of two or zero. | |
98 | // | |
99 | ASSERT ((Alignment & (Alignment - 1)) == 0); | |
100 | ||
101 | if (Pages == 0) { | |
102 | return NULL; | |
103 | } | |
104 | // | |
105 | // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow. | |
106 | // | |
107 | ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment))); | |
108 | // | |
109 | // We would rather waste some memory to save PEI code size. | |
110 | // | |
111 | Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment)); | |
112 | if (Alignment == 0) { | |
113 | AlignmentMask = Alignment; | |
114 | } else { | |
115 | AlignmentMask = Alignment - 1; | |
116 | } | |
117 | return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask); | |
118 | } | |
119 | ||
120 | ||
121 | ||
122 | ||
123 | /** | |
124 | Allocates a buffer of type EfiBootServicesData. | |
125 | ||
126 | Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a | |
127 | pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is | |
128 | returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. | |
129 | ||
130 | @param AllocationSize The number of bytes to allocate. | |
131 | ||
132 | @return A pointer to the allocated buffer or NULL if allocation fails. | |
133 | ||
134 | **/ | |
135 | VOID * | |
136 | EFIAPI | |
137 | AllocatePool ( | |
138 | IN UINTN AllocationSize | |
139 | ) | |
140 | { | |
141 | EFI_HOB_MEMORY_POOL *Hob; | |
142 | ||
143 | Hob = GetHobList (); | |
144 | ||
145 | ||
146 | // | |
147 | // Verify that there is sufficient memory to satisfy the allocation | |
148 | // | |
149 | if (AllocationSize > 0x10000) { | |
150 | // Please call AllcoatePages for big allocations | |
151 | return 0; | |
152 | } else { | |
153 | ||
154 | Hob = (EFI_HOB_MEMORY_POOL *)CreateHob (EFI_HOB_TYPE_MEMORY_POOL, (UINT16)(sizeof (EFI_HOB_TYPE_MEMORY_POOL) + AllocationSize)); | |
155 | return (VOID *)(Hob + 1); | |
156 | } | |
157 | } | |
158 | ||
159 | ||
160 |