]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/MemoryAllocationLib.c
Update code to support VS2013 tool chain.
[mirror_edk2.git] / MdeModulePkg / Library / PiSmmCoreMemoryAllocationLib / MemoryAllocationLib.c
CommitLineData
713b7781 1/** @file\r
2 Support routines for memory allocation routines based on SMM Core internal functions.\r
3\r
91403ce9 4 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
cd5ebaa0 5 This program and the accompanying materials \r
713b7781 6 are licensed and made available under the terms and conditions of the BSD License \r
7 which accompanies this distribution. The full text of the license may be found at \r
8 http://opensource.org/licenses/bsd-license.php \r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12\r
13**/\r
14\r
15#include <PiSmm.h>\r
16\r
34401578 17#include <Protocol/SmmAccess2.h>\r
713b7781 18#include <Library/MemoryAllocationLib.h>\r
34401578 19#include <Library/UefiBootServicesTableLib.h>\r
713b7781 20#include <Library/BaseMemoryLib.h>\r
21#include <Library/DebugLib.h>\r
22#include "PiSmmCoreMemoryAllocationServices.h"\r
23\r
34401578
LG
24EFI_SMRAM_DESCRIPTOR *mSmramRanges = NULL;\r
25UINTN mSmramRangeCount = 0;\r
26\r
27/**\r
28 This function gets and caches SMRAM ranges that are present in the system.\r
29 \r
30 It will ASSERT() if SMM Access2 Protocol doesn't exist.\r
31 It will ASSERT() if SMRAM ranges can't be got.\r
32 It will ASSERT() if Resource can't be allocated for cache SMRAM range. \r
33\r
34**/\r
35VOID\r
36EFIAPI\r
37GetSmramRanges (\r
38 VOID\r
39 )\r
40{\r
41 EFI_STATUS Status;\r
42 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;\r
43 UINTN Size;\r
44\r
45 //\r
46 // Locate SMM Access2 Protocol\r
47 //\r
48 Status = gBS->LocateProtocol (\r
49 &gEfiSmmAccess2ProtocolGuid, \r
50 NULL, \r
51 (VOID **)&SmmAccess\r
52 );\r
53 ASSERT_EFI_ERROR (Status);\r
54\r
55 //\r
56 // Get SMRAM range information\r
57 //\r
58 Size = 0;\r
59 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r
60 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
61\r
62 mSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);\r
63 ASSERT (mSmramRanges != NULL);\r
64\r
65 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);\r
66 ASSERT_EFI_ERROR (Status);\r
67\r
68 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
69}\r
70\r
71/**\r
72 Check whether the start address of buffer is within any of the SMRAM ranges.\r
73\r
74 @param[in] Buffer The pointer to the buffer to be checked.\r
75\r
76 @retval TURE The buffer is in SMRAM ranges.\r
77 @retval FALSE The buffer is out of SMRAM ranges.\r
78**/\r
79BOOLEAN\r
80EFIAPI\r
81BufferInSmram (\r
82 IN VOID *Buffer\r
83 )\r
84{\r
85 UINTN Index;\r
86\r
87 if (mSmramRanges == NULL) {\r
88 //\r
89 // SMRAM ranges is not got. Try to get them all.\r
90 //\r
91 GetSmramRanges();\r
92 }\r
93\r
94 for (Index = 0; Index < mSmramRangeCount; Index ++) {\r
95 if (((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer >= mSmramRanges[Index].CpuStart) && \r
96 ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer < (mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize))) {\r
97 return TRUE;\r
98 }\r
99 }\r
100\r
101 return FALSE;\r
102}\r
103\r
713b7781 104/**\r
105 Allocates one or more 4KB pages of a certain memory type.\r
106\r
107 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated\r
108 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.\r
109 If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
110\r
111 @param MemoryType The type of memory to allocate.\r
112 @param Pages The number of 4 KB pages to allocate.\r
113\r
114 @return A pointer to the allocated buffer or NULL if allocation fails.\r
115\r
116**/\r
117VOID *\r
118InternalAllocatePages (\r
119 IN EFI_MEMORY_TYPE MemoryType, \r
120 IN UINTN Pages\r
121 )\r
122{\r
123 EFI_STATUS Status;\r
124 EFI_PHYSICAL_ADDRESS Memory; \r
125\r
126 if (Pages == 0) {\r
127 return NULL;\r
128 }\r
129\r
130 Status = SmmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);\r
131 if (EFI_ERROR (Status)) {\r
132 return NULL;\r
133 }\r
134 return (VOID *) (UINTN) Memory;\r
135}\r
136\r
137/**\r
138 Allocates one or more 4KB pages of type EfiBootServicesData.\r
139\r
140 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the\r
141 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
142 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
143 returned.\r
144\r
145 @param Pages The number of 4 KB pages to allocate.\r
146\r
147 @return A pointer to the allocated buffer or NULL if allocation fails.\r
148\r
149**/\r
150VOID *\r
151EFIAPI\r
152AllocatePages (\r
153 IN UINTN Pages\r
154 )\r
155{\r
156 return InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
157}\r
158\r
159/**\r
160 Allocates one or more 4KB pages of type EfiRuntimeServicesData.\r
161\r
162 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r
163 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
164 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
165 returned.\r
166\r
167 @param Pages The number of 4 KB pages to allocate.\r
168\r
169 @return A pointer to the allocated buffer or NULL if allocation fails.\r
170\r
171**/\r
172VOID *\r
173EFIAPI\r
174AllocateRuntimePages (\r
175 IN UINTN Pages\r
176 )\r
177{\r
178 return InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
179}\r
180\r
181/**\r
182 Allocates one or more 4KB pages of type EfiReservedMemoryType.\r
183\r
184 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the\r
185 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
186 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
187 returned.\r
188\r
189 @param Pages The number of 4 KB pages to allocate.\r
190\r
191 @return A pointer to the allocated buffer or NULL if allocation fails.\r
192\r
193**/\r
194VOID *\r
195EFIAPI\r
196AllocateReservedPages (\r
197 IN UINTN Pages\r
198 )\r
199{\r
200 return NULL;\r
201}\r
202\r
203/**\r
204 Frees one or more 4KB pages that were previously allocated with one of the page allocation\r
205 functions in the Memory Allocation Library.\r
206\r
207 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
208 must have been allocated on a previous call to the page allocation services of the Memory\r
209 Allocation Library. If it is not possible to free allocated pages, then this function will\r
210 perform no actions.\r
211 \r
212 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,\r
213 then ASSERT().\r
214 If Pages is zero, then ASSERT().\r
215 \r
216 @param Buffer Pointer to the buffer of pages to free.\r
217 @param Pages The number of 4 KB pages to free.\r
218\r
219**/\r
220VOID\r
221EFIAPI\r
222FreePages (\r
223 IN VOID *Buffer,\r
224 IN UINTN Pages\r
225 )\r
226{\r
227 EFI_STATUS Status;\r
228\r
229 ASSERT (Pages != 0);\r
34401578
LG
230 if (BufferInSmram (Buffer)) {\r
231 //\r
232 // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePages() service.\r
233 // So, SmmFreePages() service is used to free it.\r
234 //\r
235 Status = SmmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
236 } else {\r
237 //\r
238 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.\r
239 // So, gBS->FreePages() service is used to free it.\r
240 //\r
241 Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
242 }\r
713b7781 243 ASSERT_EFI_ERROR (Status);\r
244}\r
245\r
246/**\r
247 Allocates one or more 4KB pages of a certain memory type at a specified alignment.\r
248\r
249 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment\r
250 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.\r
251 If there is not enough memory at the specified alignment remaining to satisfy the request, then\r
252 NULL is returned.\r
253 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
91403ce9 254 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
713b7781 255\r
256 @param MemoryType The type of memory to allocate.\r
257 @param Pages The number of 4 KB pages to allocate.\r
258 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
259 If Alignment is zero, then byte alignment is used.\r
260\r
261 @return A pointer to the allocated buffer or NULL if allocation fails.\r
262\r
263**/\r
264VOID *\r
265InternalAllocateAlignedPages (\r
266 IN EFI_MEMORY_TYPE MemoryType, \r
267 IN UINTN Pages,\r
268 IN UINTN Alignment\r
269 )\r
270{\r
271 EFI_STATUS Status;\r
272 EFI_PHYSICAL_ADDRESS Memory;\r
273 UINTN AlignedMemory;\r
274 UINTN AlignmentMask;\r
275 UINTN UnalignedPages;\r
276 UINTN RealPages;\r
277\r
278 //\r
279 // Alignment must be a power of two or zero.\r
280 //\r
281 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
282 \r
283 if (Pages == 0) {\r
284 return NULL;\r
285 }\r
286 if (Alignment > EFI_PAGE_SIZE) {\r
287 //\r
288 // Caculate the total number of pages since alignment is larger than page size.\r
289 //\r
290 AlignmentMask = Alignment - 1;\r
291 RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment);\r
292 //\r
293 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
294 //\r
295 ASSERT (RealPages > Pages);\r
296 \r
297 Status = SmmAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);\r
298 if (EFI_ERROR (Status)) {\r
299 return NULL;\r
300 }\r
301 AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;\r
302 UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);\r
303 if (UnalignedPages > 0) {\r
304 //\r
305 // Free first unaligned page(s).\r
306 //\r
307 Status = SmmFreePages (Memory, UnalignedPages);\r
308 ASSERT_EFI_ERROR (Status);\r
309 }\r
310 Memory = (EFI_PHYSICAL_ADDRESS) (AlignedMemory + EFI_PAGES_TO_SIZE (Pages));\r
311 UnalignedPages = RealPages - Pages - UnalignedPages;\r
312 if (UnalignedPages > 0) {\r
313 //\r
314 // Free last unaligned page(s).\r
315 //\r
316 Status = SmmFreePages (Memory, UnalignedPages);\r
317 ASSERT_EFI_ERROR (Status);\r
318 }\r
319 } else {\r
320 //\r
321 // Do not over-allocate pages in this case.\r
322 //\r
323 Status = SmmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);\r
324 if (EFI_ERROR (Status)) {\r
325 return NULL;\r
326 }\r
327 AlignedMemory = (UINTN) Memory;\r
328 }\r
329 return (VOID *) AlignedMemory;\r
330}\r
331\r
332/**\r
333 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r
334\r
335 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r
336 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
337 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
338 request, then NULL is returned.\r
339 \r
340 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
91403ce9 341 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
713b7781 342\r
343 @param Pages The number of 4 KB pages to allocate.\r
344 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
345 If Alignment is zero, then byte alignment is used.\r
346\r
347 @return A pointer to the allocated buffer or NULL if allocation fails.\r
348\r
349**/\r
350VOID *\r
351EFIAPI\r
352AllocateAlignedPages (\r
353 IN UINTN Pages,\r
354 IN UINTN Alignment\r
355 )\r
356{\r
357 return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
358}\r
359\r
360/**\r
361 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.\r
362\r
363 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an\r
364 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
365 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
366 request, then NULL is returned.\r
367 \r
368 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
91403ce9 369 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
713b7781 370\r
371 @param Pages The number of 4 KB pages to allocate.\r
372 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
373 If Alignment is zero, then byte alignment is used.\r
374\r
375 @return A pointer to the allocated buffer or NULL if allocation fails.\r
376\r
377**/\r
378VOID *\r
379EFIAPI\r
380AllocateAlignedRuntimePages (\r
381 IN UINTN Pages,\r
382 IN UINTN Alignment\r
383 )\r
384{\r
385 return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
386}\r
387\r
388/**\r
389 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.\r
390\r
391 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an\r
392 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
393 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
394 request, then NULL is returned.\r
395 \r
396 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
91403ce9 397 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
713b7781 398\r
399 @param Pages The number of 4 KB pages to allocate.\r
400 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
401 If Alignment is zero, then byte alignment is used.\r
402\r
403 @return A pointer to the allocated buffer or NULL if allocation fails.\r
404\r
405**/\r
406VOID *\r
407EFIAPI\r
408AllocateAlignedReservedPages (\r
409 IN UINTN Pages,\r
410 IN UINTN Alignment\r
411 )\r
412{\r
413 return NULL;\r
414}\r
415\r
416/**\r
417 Frees one or more 4KB pages that were previously allocated with one of the aligned page\r
418 allocation functions in the Memory Allocation Library.\r
419\r
420 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
421 must have been allocated on a previous call to the aligned page allocation services of the Memory\r
422 Allocation Library. If it is not possible to free allocated pages, then this function will \r
423 perform no actions.\r
424 \r
425 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation\r
426 Library, then ASSERT().\r
427 If Pages is zero, then ASSERT().\r
428 \r
429 @param Buffer Pointer to the buffer of pages to free.\r
430 @param Pages The number of 4 KB pages to free.\r
431\r
432**/\r
433VOID\r
434EFIAPI\r
435FreeAlignedPages (\r
436 IN VOID *Buffer,\r
437 IN UINTN Pages\r
438 )\r
439{\r
440 EFI_STATUS Status;\r
441\r
442 ASSERT (Pages != 0);\r
34401578
LG
443 if (BufferInSmram (Buffer)) {\r
444 //\r
445 // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePages() service.\r
446 // So, SmmFreePages() service is used to free it.\r
447 //\r
448 Status = SmmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
449 } else {\r
450 //\r
451 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.\r
452 // So, gBS->FreePages() service is used to free it.\r
453 //\r
454 Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
455 }\r
713b7781 456 ASSERT_EFI_ERROR (Status);\r
457}\r
458\r
459/**\r
460 Allocates a buffer of a certain pool type.\r
461\r
462 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a\r
463 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
464 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
465\r
466 @param MemoryType The type of memory to allocate.\r
467 @param AllocationSize The number of bytes to allocate.\r
468\r
469 @return A pointer to the allocated buffer or NULL if allocation fails.\r
470\r
471**/\r
472VOID *\r
473InternalAllocatePool (\r
474 IN EFI_MEMORY_TYPE MemoryType, \r
475 IN UINTN AllocationSize\r
476 )\r
477{\r
478 EFI_STATUS Status;\r
479 VOID *Memory;\r
480\r
481 Status = SmmAllocatePool (MemoryType, AllocationSize, &Memory);\r
482 if (EFI_ERROR (Status)) {\r
483 Memory = NULL;\r
484 }\r
485 return Memory;\r
486}\r
487\r
488/**\r
489 Allocates a buffer of type EfiBootServicesData.\r
490\r
491 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
492 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
493 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
494\r
495 @param AllocationSize The number of bytes to allocate.\r
496\r
497 @return A pointer to the allocated buffer or NULL if allocation fails.\r
498\r
499**/\r
500VOID *\r
501EFIAPI\r
502AllocatePool (\r
503 IN UINTN AllocationSize\r
504 )\r
505{\r
506 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
507}\r
508\r
509/**\r
510 Allocates a buffer of type EfiRuntimeServicesData.\r
511\r
512 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns\r
513 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
514 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
515\r
516 @param AllocationSize The number of bytes to allocate.\r
517\r
518 @return A pointer to the allocated buffer or NULL if allocation fails.\r
519\r
520**/\r
521VOID *\r
522EFIAPI\r
523AllocateRuntimePool (\r
524 IN UINTN AllocationSize\r
525 )\r
526{\r
527 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
528}\r
529\r
530/**\r
531 Allocates a buffer of type EfiReservedMemoryType.\r
532\r
533 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns\r
534 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
535 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
536\r
537 @param AllocationSize The number of bytes to allocate.\r
538\r
539 @return A pointer to the allocated buffer or NULL if allocation fails.\r
540\r
541**/\r
542VOID *\r
543EFIAPI\r
544AllocateReservedPool (\r
545 IN UINTN AllocationSize\r
546 )\r
547{\r
548 return NULL;\r
549}\r
550\r
551/**\r
552 Allocates and zeros a buffer of a certain pool type.\r
553\r
554 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer\r
555 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid\r
556 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,\r
557 then NULL is returned.\r
558\r
559 @param PoolType The type of memory to allocate.\r
560 @param AllocationSize The number of bytes to allocate and zero.\r
561\r
562 @return A pointer to the allocated buffer or NULL if allocation fails.\r
563\r
564**/\r
565VOID *\r
566InternalAllocateZeroPool (\r
567 IN EFI_MEMORY_TYPE PoolType, \r
568 IN UINTN AllocationSize\r
569 ) \r
570{\r
571 VOID *Memory;\r
572\r
573 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
574 if (Memory != NULL) {\r
575 Memory = ZeroMem (Memory, AllocationSize);\r
576 }\r
577 return Memory;\r
578}\r
579\r
580/**\r
581 Allocates and zeros a buffer of type EfiBootServicesData.\r
582\r
583 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
584 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
585 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
586 request, then NULL is returned.\r
587\r
588 @param AllocationSize The number of bytes to allocate and zero.\r
589\r
590 @return A pointer to the allocated buffer or NULL if allocation fails.\r
591\r
592**/\r
593VOID *\r
594EFIAPI\r
595AllocateZeroPool (\r
596 IN UINTN AllocationSize\r
597 )\r
598{\r
599 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
600}\r
601\r
602/**\r
603 Allocates and zeros a buffer of type EfiRuntimeServicesData.\r
604\r
605 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the\r
606 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
607 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
608 request, then NULL is returned.\r
609\r
610 @param AllocationSize The number of bytes to allocate and zero.\r
611\r
612 @return A pointer to the allocated buffer or NULL if allocation fails.\r
613\r
614**/\r
615VOID *\r
616EFIAPI\r
617AllocateRuntimeZeroPool (\r
618 IN UINTN AllocationSize\r
619 )\r
620{\r
621 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
622}\r
623\r
624/**\r
625 Allocates and zeros a buffer of type EfiReservedMemoryType.\r
626\r
627 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the\r
628 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
629 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
630 request, then NULL is returned.\r
631\r
632 @param AllocationSize The number of bytes to allocate and zero.\r
633\r
634 @return A pointer to the allocated buffer or NULL if allocation fails.\r
635\r
636**/\r
637VOID *\r
638EFIAPI\r
639AllocateReservedZeroPool (\r
640 IN UINTN AllocationSize\r
641 )\r
642{\r
643 return NULL;\r
644}\r
645\r
646/**\r
647 Copies a buffer to an allocated buffer of a certain pool type.\r
648\r
649 Allocates the number bytes specified by AllocationSize of a certain pool type, copies\r
650 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
651 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
652 is not enough memory remaining to satisfy the request, then NULL is returned.\r
653 If Buffer is NULL, then ASSERT().\r
654 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
655\r
656 @param PoolType The type of pool to allocate.\r
657 @param AllocationSize The number of bytes to allocate and zero.\r
658 @param Buffer The buffer to copy to the allocated buffer.\r
659\r
660 @return A pointer to the allocated buffer or NULL if allocation fails.\r
661\r
662**/\r
663VOID *\r
664InternalAllocateCopyPool (\r
665 IN EFI_MEMORY_TYPE PoolType, \r
666 IN UINTN AllocationSize,\r
667 IN CONST VOID *Buffer\r
668 ) \r
669{\r
670 VOID *Memory;\r
671\r
672 ASSERT (Buffer != NULL);\r
673 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r
674\r
675 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
676 if (Memory != NULL) {\r
677 Memory = CopyMem (Memory, Buffer, AllocationSize);\r
678 }\r
679 return Memory;\r
680} \r
681\r
682/**\r
683 Copies a buffer to an allocated buffer of type EfiBootServicesData.\r
684\r
685 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies\r
686 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
687 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
688 is not enough memory remaining to satisfy the request, then NULL is returned.\r
689 \r
690 If Buffer is NULL, then ASSERT().\r
691 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
692\r
693 @param AllocationSize The number of bytes to allocate and zero.\r
694 @param Buffer The buffer to copy to the allocated buffer.\r
695\r
696 @return A pointer to the allocated buffer or NULL if allocation fails.\r
697\r
698**/\r
699VOID *\r
700EFIAPI\r
701AllocateCopyPool (\r
702 IN UINTN AllocationSize,\r
703 IN CONST VOID *Buffer\r
704 )\r
705{\r
706 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
707}\r
708\r
709/**\r
710 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.\r
711\r
712 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies\r
713 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
714 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
715 is not enough memory remaining to satisfy the request, then NULL is returned.\r
716 \r
717 If Buffer is NULL, then ASSERT().\r
718 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
719\r
720 @param AllocationSize The number of bytes to allocate and zero.\r
721 @param Buffer The buffer to copy to the allocated buffer.\r
722\r
723 @return A pointer to the allocated buffer or NULL if allocation fails.\r
724\r
725**/\r
726VOID *\r
727EFIAPI\r
728AllocateRuntimeCopyPool (\r
729 IN UINTN AllocationSize,\r
730 IN CONST VOID *Buffer\r
731 )\r
732{\r
733 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
734}\r
735\r
736/**\r
737 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.\r
738\r
739 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies\r
740 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
741 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
742 is not enough memory remaining to satisfy the request, then NULL is returned.\r
743 \r
744 If Buffer is NULL, then ASSERT().\r
745 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
746\r
747 @param AllocationSize The number of bytes to allocate and zero.\r
748 @param Buffer The buffer to copy to the allocated buffer.\r
749\r
750 @return A pointer to the allocated buffer or NULL if allocation fails.\r
751\r
752**/\r
753VOID *\r
754EFIAPI\r
755AllocateReservedCopyPool (\r
756 IN UINTN AllocationSize,\r
757 IN CONST VOID *Buffer\r
758 )\r
759{\r
760 return NULL;\r
761}\r
762\r
763/**\r
764 Reallocates a buffer of a specified memory type.\r
765\r
766 Allocates and zeros the number bytes specified by NewSize from memory of the type\r
767 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and \r
768 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
769 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
770 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
771 enough memory remaining to satisfy the request, then NULL is returned.\r
772 \r
773 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
774 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
775\r
776 @param PoolType The type of pool to allocate.\r
777 @param OldSize The size, in bytes, of OldBuffer.\r
778 @param NewSize The size, in bytes, of the buffer to reallocate.\r
779 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
780 parameter that may be NULL.\r
781\r
782 @return A pointer to the allocated buffer or NULL if allocation fails.\r
783\r
784**/\r
785VOID *\r
786InternalReallocatePool (\r
787 IN EFI_MEMORY_TYPE PoolType, \r
788 IN UINTN OldSize,\r
789 IN UINTN NewSize,\r
790 IN VOID *OldBuffer OPTIONAL\r
791 )\r
792{\r
793 VOID *NewBuffer;\r
794\r
795 NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);\r
796 if (NewBuffer != NULL && OldBuffer != NULL) {\r
797 CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));\r
798 FreePool (OldBuffer);\r
799 }\r
800 return NewBuffer;\r
801}\r
802\r
803/**\r
804 Reallocates a buffer of type EfiBootServicesData.\r
805\r
806 Allocates and zeros the number bytes specified by NewSize from memory of type\r
807 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and \r
808 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
809 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
810 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
811 enough memory remaining to satisfy the request, then NULL is returned.\r
812 \r
813 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
814 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
815\r
816 @param OldSize The size, in bytes, of OldBuffer.\r
817 @param NewSize The size, in bytes, of the buffer to reallocate.\r
818 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
819 parameter that may be NULL.\r
820\r
821 @return A pointer to the allocated buffer or NULL if allocation fails.\r
822\r
823**/\r
824VOID *\r
825EFIAPI\r
826ReallocatePool (\r
827 IN UINTN OldSize,\r
828 IN UINTN NewSize,\r
829 IN VOID *OldBuffer OPTIONAL\r
830 )\r
831{\r
832 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
833}\r
834\r
835/**\r
836 Reallocates a buffer of type EfiRuntimeServicesData.\r
837\r
838 Allocates and zeros the number bytes specified by NewSize from memory of type\r
839 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and \r
840 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
841 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
842 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
843 enough memory remaining to satisfy the request, then NULL is returned.\r
844\r
845 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
846 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
847\r
848 @param OldSize The size, in bytes, of OldBuffer.\r
849 @param NewSize The size, in bytes, of the buffer to reallocate.\r
850 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
851 parameter that may be NULL.\r
852\r
853 @return A pointer to the allocated buffer or NULL if allocation fails.\r
854\r
855**/\r
856VOID *\r
857EFIAPI\r
858ReallocateRuntimePool (\r
859 IN UINTN OldSize,\r
860 IN UINTN NewSize,\r
861 IN VOID *OldBuffer OPTIONAL\r
862 )\r
863{\r
864 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
865}\r
866\r
867/**\r
868 Reallocates a buffer of type EfiReservedMemoryType.\r
869\r
870 Allocates and zeros the number bytes specified by NewSize from memory of type\r
871 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and \r
872 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
873 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
874 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
875 enough memory remaining to satisfy the request, then NULL is returned.\r
876\r
877 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
878 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
879\r
880 @param OldSize The size, in bytes, of OldBuffer.\r
881 @param NewSize The size, in bytes, of the buffer to reallocate.\r
882 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
883 parameter that may be NULL.\r
884\r
885 @return A pointer to the allocated buffer or NULL if allocation fails.\r
886\r
887**/\r
888VOID *\r
889EFIAPI\r
890ReallocateReservedPool (\r
891 IN UINTN OldSize,\r
892 IN UINTN NewSize,\r
893 IN VOID *OldBuffer OPTIONAL\r
894 )\r
895{\r
896 return NULL;\r
897}\r
898\r
899/**\r
900 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
901 Memory Allocation Library.\r
902\r
903 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
904 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
905 resources, then this function will perform no actions.\r
906 \r
907 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
908 then ASSERT().\r
909\r
910 @param Buffer Pointer to the buffer to free.\r
911\r
912**/\r
913VOID\r
914EFIAPI\r
915FreePool (\r
916 IN VOID *Buffer\r
917 )\r
918{\r
919 EFI_STATUS Status;\r
920\r
34401578
LG
921 if (BufferInSmram (Buffer)) {\r
922 //\r
923 // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePool() service.\r
924 // So, SmmFreePool() service is used to free it.\r
925 //\r
926 Status = SmmFreePool (Buffer);\r
927 } else {\r
928 //\r
929 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePool() service.\r
930 // So, gBS->FreePool() service is used to free it.\r
931 //\r
932 Status = gBS->FreePool (Buffer);\r
933 }\r
713b7781 934 ASSERT_EFI_ERROR (Status);\r
935}\r
936\r