]>
Commit | Line | Data |
---|---|---|
9071550e | 1 | /*++\r |
2 | \r | |
b1f700a8 HT |
3 | Copyright (c) 2006, Intel Corporation. All rights reserved.<BR>\r |
4 | This program and the accompanying materials \r | |
9071550e | 5 | are licensed and made available under the terms and conditions of the BSD License \r |
6 | which accompanies this distribution. The full text of the license may be found at \r | |
7 | http://opensource.org/licenses/bsd-license.php \r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
11 | \r | |
12 | Module Name:\r | |
13 | Support.c\r | |
14 | \r | |
15 | Abstract:\r | |
16 | \r | |
17 | Revision History:\r | |
18 | \r | |
19 | --*/\r | |
20 | #include "EfiLdr.h"\r | |
21 | \r | |
22 | EFI_STATUS\r | |
23 | EfiAddMemoryDescriptor(\r | |
24 | UINTN *NoDesc,\r | |
25 | EFI_MEMORY_DESCRIPTOR *Desc,\r | |
26 | EFI_MEMORY_TYPE Type,\r | |
27 | EFI_PHYSICAL_ADDRESS BaseAddress,\r | |
28 | UINT64 NoPages,\r | |
29 | UINT64 Attribute\r | |
30 | )\r | |
31 | {\r | |
32 | UINTN NumberOfDesc;\r | |
33 | UINT64 Temp;\r | |
34 | UINTN Index;\r | |
35 | \r | |
36 | if (NoPages == 0) {\r | |
37 | return EFI_SUCCESS;\r | |
38 | }\r | |
39 | \r | |
40 | //\r | |
41 | // See if the new memory descriptor needs to be carved out of an existing memory descriptor\r | |
42 | //\r | |
43 | \r | |
44 | NumberOfDesc = *NoDesc;\r | |
45 | for (Index = 0; Index < NumberOfDesc; Index++) {\r | |
46 | \r | |
47 | if (Desc[Index].Type == EfiConventionalMemory) {\r | |
48 | \r | |
49 | Temp = DivU64x32 ((BaseAddress - Desc[Index].PhysicalStart), EFI_PAGE_SIZE) + NoPages;\r | |
50 | \r | |
51 | if ((Desc[Index].PhysicalStart < BaseAddress) && (Desc[Index].NumberOfPages >= Temp)) {\r | |
52 | if (Desc[Index].NumberOfPages > Temp) {\r | |
53 | Desc[*NoDesc].Type = EfiConventionalMemory;\r | |
54 | Desc[*NoDesc].PhysicalStart = BaseAddress + MultU64x32 (NoPages, EFI_PAGE_SIZE);\r | |
55 | Desc[*NoDesc].NumberOfPages = Desc[Index].NumberOfPages - Temp;\r | |
56 | Desc[*NoDesc].VirtualStart = 0;\r | |
57 | Desc[*NoDesc].Attribute = Desc[Index].Attribute;\r | |
58 | *NoDesc = *NoDesc + 1;\r | |
59 | }\r | |
60 | Desc[Index].NumberOfPages = Temp - NoPages;\r | |
61 | }\r | |
62 | \r | |
63 | if ((Desc[Index].PhysicalStart == BaseAddress) && (Desc[Index].NumberOfPages == NoPages)) {\r | |
64 | Desc[Index].Type = Type;\r | |
65 | Desc[Index].Attribute = Attribute;\r | |
66 | return EFI_SUCCESS;\r | |
67 | }\r | |
68 | \r | |
69 | if ((Desc[Index].PhysicalStart == BaseAddress) && (Desc[Index].NumberOfPages > NoPages)) {\r | |
70 | Desc[Index].NumberOfPages -= NoPages;\r | |
71 | Desc[Index].PhysicalStart += MultU64x32 (NoPages, EFI_PAGE_SIZE);\r | |
72 | }\r | |
73 | }\r | |
74 | }\r | |
75 | \r | |
76 | //\r | |
77 | // Add the new memory descriptor\r | |
78 | //\r | |
79 | \r | |
80 | Desc[*NoDesc].Type = Type;\r | |
81 | Desc[*NoDesc].PhysicalStart = BaseAddress;\r | |
82 | Desc[*NoDesc].NumberOfPages = NoPages;\r | |
83 | Desc[*NoDesc].VirtualStart = 0;\r | |
84 | Desc[*NoDesc].Attribute = Attribute;\r | |
85 | *NoDesc = *NoDesc + 1;\r | |
86 | \r | |
87 | return EFI_SUCCESS;\r | |
88 | }\r | |
89 | \r | |
90 | UINTN\r | |
91 | FindSpace (\r | |
92 | UINTN NoPages,\r | |
93 | IN UINTN *NumberOfMemoryMapEntries,\r | |
94 | IN EFI_MEMORY_DESCRIPTOR *EfiMemoryDescriptor,\r | |
95 | EFI_MEMORY_TYPE Type,\r | |
96 | UINT64 Attribute\r | |
97 | )\r | |
98 | {\r | |
99 | EFI_PHYSICAL_ADDRESS MaxPhysicalStart;\r | |
100 | UINT64 MaxNoPages;\r | |
101 | UINTN Index;\r | |
102 | EFI_MEMORY_DESCRIPTOR *CurrentMemoryDescriptor;\r | |
103 | \r | |
104 | MaxPhysicalStart = 0;\r | |
105 | MaxNoPages = 0;\r | |
106 | CurrentMemoryDescriptor = NULL;\r | |
107 | for (Index = 0; Index < *NumberOfMemoryMapEntries; Index++) {\r | |
108 | if (EfiMemoryDescriptor[Index].PhysicalStart + LShiftU64(EfiMemoryDescriptor[Index].NumberOfPages, EFI_PAGE_SHIFT) <= 0x100000) {\r | |
109 | continue;\r | |
110 | }\r | |
111 | if ((EfiMemoryDescriptor[Index].Type == EfiConventionalMemory) && \r | |
112 | (EfiMemoryDescriptor[Index].NumberOfPages >= NoPages)) {\r | |
113 | if (EfiMemoryDescriptor[Index].PhysicalStart > MaxPhysicalStart) {\r | |
ffdc1867 | 114 | if (EfiMemoryDescriptor[Index].PhysicalStart + LShiftU64(EfiMemoryDescriptor[Index].NumberOfPages, EFI_PAGE_SHIFT) <= 0x100000000ULL) {\r |
9071550e | 115 | MaxPhysicalStart = EfiMemoryDescriptor[Index].PhysicalStart;\r |
116 | MaxNoPages = EfiMemoryDescriptor[Index].NumberOfPages;\r | |
117 | CurrentMemoryDescriptor = &EfiMemoryDescriptor[Index];\r | |
118 | }\r | |
119 | }\r | |
120 | }\r | |
121 | if ((EfiMemoryDescriptor[Index].Type == EfiReservedMemoryType) ||\r | |
122 | (EfiMemoryDescriptor[Index].Type >= EfiACPIReclaimMemory) ) {\r | |
123 | continue;\r | |
124 | }\r | |
125 | if ((EfiMemoryDescriptor[Index].Type == EfiRuntimeServicesCode) ||\r | |
126 | (EfiMemoryDescriptor[Index].Type == EfiRuntimeServicesData)) {\r | |
127 | break;\r | |
128 | }\r | |
129 | }\r | |
130 | \r | |
131 | if (MaxPhysicalStart == 0) {\r | |
132 | return 0;\r | |
133 | }\r | |
134 | \r | |
135 | if (MaxNoPages != NoPages) {\r | |
136 | CurrentMemoryDescriptor->NumberOfPages = MaxNoPages - NoPages;\r | |
137 | EfiMemoryDescriptor[*NumberOfMemoryMapEntries].Type = Type;\r | |
138 | EfiMemoryDescriptor[*NumberOfMemoryMapEntries].PhysicalStart = MaxPhysicalStart + LShiftU64(MaxNoPages - NoPages, EFI_PAGE_SHIFT);\r | |
139 | EfiMemoryDescriptor[*NumberOfMemoryMapEntries].NumberOfPages = NoPages;\r | |
140 | EfiMemoryDescriptor[*NumberOfMemoryMapEntries].VirtualStart = 0;\r | |
141 | EfiMemoryDescriptor[*NumberOfMemoryMapEntries].Attribute = Attribute;\r | |
142 | *NumberOfMemoryMapEntries = *NumberOfMemoryMapEntries + 1;\r | |
143 | } else {\r | |
144 | CurrentMemoryDescriptor->Type = Type;\r | |
145 | CurrentMemoryDescriptor->Attribute = Attribute;\r | |
146 | }\r | |
147 | \r | |
148 | return (UINTN)(MaxPhysicalStart + LShiftU64(MaxNoPages - NoPages, EFI_PAGE_SHIFT));\r | |
149 | }\r | |
150 | \r | |
151 | VOID\r | |
152 | GenMemoryMap (\r | |
153 | UINTN *NumberOfMemoryMapEntries,\r | |
154 | EFI_MEMORY_DESCRIPTOR *EfiMemoryDescriptor,\r | |
155 | BIOS_MEMORY_MAP *BiosMemoryMap\r | |
156 | )\r | |
157 | {\r | |
158 | UINT64 BaseAddress;\r | |
159 | UINT64 Length;\r | |
160 | EFI_MEMORY_TYPE Type;\r | |
161 | UINTN Index;\r | |
162 | UINTN Attr;\r | |
163 | UINT64 Ceiling;\r | |
164 | \r | |
165 | Ceiling = 0xFFFFFFFF;\r | |
166 | for (Index = 0; Index < BiosMemoryMap->MemoryMapSize / sizeof(BIOS_MEMORY_MAP_ENTRY); Index++) {\r | |
167 | \r | |
168 | switch (BiosMemoryMap->MemoryMapEntry[Index].Type) { \r | |
169 | case (INT15_E820_AddressRangeMemory):\r | |
170 | Type = EfiConventionalMemory;\r | |
171 | Attr = EFI_MEMORY_WB;\r | |
172 | break;\r | |
173 | case (INT15_E820_AddressRangeReserved):\r | |
174 | Type = EfiReservedMemoryType;\r | |
175 | Attr = EFI_MEMORY_UC;\r | |
176 | break;\r | |
177 | case (INT15_E820_AddressRangeACPI):\r | |
178 | Type = EfiACPIReclaimMemory;\r | |
179 | Attr = EFI_MEMORY_WB;\r | |
180 | break;\r | |
181 | case (INT15_E820_AddressRangeNVS):\r | |
182 | Type = EfiACPIMemoryNVS;\r | |
183 | Attr = EFI_MEMORY_UC;\r | |
184 | break;\r | |
185 | default:\r | |
186 | // We should not get here, according to ACPI 2.0 Spec.\r | |
187 | // BIOS behaviour of the Int15h, E820h\r | |
188 | Type = EfiReservedMemoryType;\r | |
189 | Attr = EFI_MEMORY_UC;\r | |
190 | break;\r | |
191 | }\r | |
192 | if (Type == EfiConventionalMemory) {\r | |
193 | BaseAddress = BiosMemoryMap->MemoryMapEntry[Index].BaseAddress;\r | |
194 | Length = BiosMemoryMap->MemoryMapEntry[Index].Length;\r | |
195 | if (BaseAddress & EFI_PAGE_MASK) {\r | |
196 | Length = Length + (BaseAddress & EFI_PAGE_MASK) - EFI_PAGE_SIZE;\r | |
197 | BaseAddress = LShiftU64 (RShiftU64 (BaseAddress, EFI_PAGE_SHIFT) + 1, EFI_PAGE_SHIFT);\r | |
198 | }\r | |
199 | } else {\r | |
200 | BaseAddress = BiosMemoryMap->MemoryMapEntry[Index].BaseAddress;\r | |
201 | Length = BiosMemoryMap->MemoryMapEntry[Index].Length + (BaseAddress & EFI_PAGE_MASK);\r | |
202 | BaseAddress = LShiftU64 (RShiftU64 (BaseAddress, EFI_PAGE_SHIFT), EFI_PAGE_SHIFT);\r | |
203 | if (Length & EFI_PAGE_MASK) {\r | |
204 | Length = LShiftU64 (RShiftU64 (Length, EFI_PAGE_SHIFT) + 1, EFI_PAGE_SHIFT);\r | |
205 | }\r | |
206 | //\r | |
207 | // Update Memory Ceiling\r | |
208 | //\r | |
ffdc1867 | 209 | if ((BaseAddress >= 0x100000) && (BaseAddress < 0x100000000ULL)) {\r |
9071550e | 210 | if (Ceiling > BaseAddress) {\r |
211 | Ceiling = BaseAddress;\r | |
212 | }\r | |
213 | }\r | |
214 | }\r | |
215 | EfiAddMemoryDescriptor (\r | |
216 | NumberOfMemoryMapEntries,\r | |
217 | EfiMemoryDescriptor,\r | |
218 | Type,\r | |
219 | (EFI_PHYSICAL_ADDRESS)BaseAddress,\r | |
220 | RShiftU64 (Length, EFI_PAGE_SHIFT),\r | |
221 | Attr\r | |
222 | );\r | |
223 | }\r | |
224 | \r | |
225 | //\r | |
226 | // Update MemoryMap according to Ceiling\r | |
227 | //\r | |
228 | for (Index = 0; Index < *NumberOfMemoryMapEntries; Index++) {\r | |
229 | if ((EfiMemoryDescriptor[Index].Type == EfiConventionalMemory) &&\r | |
230 | (EfiMemoryDescriptor[Index].PhysicalStart > 0x100000) && \r | |
ffdc1867 | 231 | (EfiMemoryDescriptor[Index].PhysicalStart < 0x100000000ULL)) {\r |
9071550e | 232 | if (EfiMemoryDescriptor[Index].PhysicalStart >= Ceiling) {\r |
233 | EfiMemoryDescriptor[Index].Type = EfiReservedMemoryType;\r | |
234 | }\r | |
235 | }\r | |
236 | }\r | |
237 | }\r |