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