]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/MemoryAllocationLib.c
1. Add EFI_COMPONENT_NAME2_PROTOCOL.GetControllerName() support.
[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
cd5ebaa0
HT
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
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
254\r
255 @param MemoryType The type of memory to allocate.\r
256 @param Pages The number of 4 KB pages to allocate.\r
257 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
258 If Alignment is zero, then byte alignment is used.\r
259\r
260 @return A pointer to the allocated buffer or NULL if allocation fails.\r
261\r
262**/\r
263VOID *\r
264InternalAllocateAlignedPages (\r
265 IN EFI_MEMORY_TYPE MemoryType, \r
266 IN UINTN Pages,\r
267 IN UINTN Alignment\r
268 )\r
269{\r
270 EFI_STATUS Status;\r
271 EFI_PHYSICAL_ADDRESS Memory;\r
272 UINTN AlignedMemory;\r
273 UINTN AlignmentMask;\r
274 UINTN UnalignedPages;\r
275 UINTN RealPages;\r
276\r
277 //\r
278 // Alignment must be a power of two or zero.\r
279 //\r
280 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
281 \r
282 if (Pages == 0) {\r
283 return NULL;\r
284 }\r
285 if (Alignment > EFI_PAGE_SIZE) {\r
286 //\r
287 // Caculate the total number of pages since alignment is larger than page size.\r
288 //\r
289 AlignmentMask = Alignment - 1;\r
290 RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment);\r
291 //\r
292 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
293 //\r
294 ASSERT (RealPages > Pages);\r
295 \r
296 Status = SmmAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);\r
297 if (EFI_ERROR (Status)) {\r
298 return NULL;\r
299 }\r
300 AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;\r
301 UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);\r
302 if (UnalignedPages > 0) {\r
303 //\r
304 // Free first unaligned page(s).\r
305 //\r
306 Status = SmmFreePages (Memory, UnalignedPages);\r
307 ASSERT_EFI_ERROR (Status);\r
308 }\r
309 Memory = (EFI_PHYSICAL_ADDRESS) (AlignedMemory + EFI_PAGES_TO_SIZE (Pages));\r
310 UnalignedPages = RealPages - Pages - UnalignedPages;\r
311 if (UnalignedPages > 0) {\r
312 //\r
313 // Free last unaligned page(s).\r
314 //\r
315 Status = SmmFreePages (Memory, UnalignedPages);\r
316 ASSERT_EFI_ERROR (Status);\r
317 }\r
318 } else {\r
319 //\r
320 // Do not over-allocate pages in this case.\r
321 //\r
322 Status = SmmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);\r
323 if (EFI_ERROR (Status)) {\r
324 return NULL;\r
325 }\r
326 AlignedMemory = (UINTN) Memory;\r
327 }\r
328 return (VOID *) AlignedMemory;\r
329}\r
330\r
331/**\r
332 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r
333\r
334 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r
335 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
336 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
337 request, then NULL is returned.\r
338 \r
339 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
340\r
341 @param Pages The number of 4 KB pages to allocate.\r
342 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
343 If Alignment is zero, then byte alignment is used.\r
344\r
345 @return A pointer to the allocated buffer or NULL if allocation fails.\r
346\r
347**/\r
348VOID *\r
349EFIAPI\r
350AllocateAlignedPages (\r
351 IN UINTN Pages,\r
352 IN UINTN Alignment\r
353 )\r
354{\r
355 return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
356}\r
357\r
358/**\r
359 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.\r
360\r
361 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an\r
362 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
363 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
364 request, then NULL is returned.\r
365 \r
366 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
367\r
368 @param Pages The number of 4 KB pages to allocate.\r
369 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
370 If Alignment is zero, then byte alignment is used.\r
371\r
372 @return A pointer to the allocated buffer or NULL if allocation fails.\r
373\r
374**/\r
375VOID *\r
376EFIAPI\r
377AllocateAlignedRuntimePages (\r
378 IN UINTN Pages,\r
379 IN UINTN Alignment\r
380 )\r
381{\r
382 return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
383}\r
384\r
385/**\r
386 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.\r
387\r
388 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an\r
389 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
390 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
391 request, then NULL is returned.\r
392 \r
393 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
394\r
395 @param Pages The number of 4 KB pages to allocate.\r
396 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
397 If Alignment is zero, then byte alignment is used.\r
398\r
399 @return A pointer to the allocated buffer or NULL if allocation fails.\r
400\r
401**/\r
402VOID *\r
403EFIAPI\r
404AllocateAlignedReservedPages (\r
405 IN UINTN Pages,\r
406 IN UINTN Alignment\r
407 )\r
408{\r
409 return NULL;\r
410}\r
411\r
412/**\r
413 Frees one or more 4KB pages that were previously allocated with one of the aligned page\r
414 allocation functions in the Memory Allocation Library.\r
415\r
416 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
417 must have been allocated on a previous call to the aligned page allocation services of the Memory\r
418 Allocation Library. If it is not possible to free allocated pages, then this function will \r
419 perform no actions.\r
420 \r
421 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation\r
422 Library, then ASSERT().\r
423 If Pages is zero, then ASSERT().\r
424 \r
425 @param Buffer Pointer to the buffer of pages to free.\r
426 @param Pages The number of 4 KB pages to free.\r
427\r
428**/\r
429VOID\r
430EFIAPI\r
431FreeAlignedPages (\r
432 IN VOID *Buffer,\r
433 IN UINTN Pages\r
434 )\r
435{\r
436 EFI_STATUS Status;\r
437\r
438 ASSERT (Pages != 0);\r
34401578
LG
439 if (BufferInSmram (Buffer)) {\r
440 //\r
441 // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePages() service.\r
442 // So, SmmFreePages() service is used to free it.\r
443 //\r
444 Status = SmmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
445 } else {\r
446 //\r
447 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.\r
448 // So, gBS->FreePages() service is used to free it.\r
449 //\r
450 Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
451 }\r
713b7781 452 ASSERT_EFI_ERROR (Status);\r
453}\r
454\r
455/**\r
456 Allocates a buffer of a certain pool type.\r
457\r
458 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a\r
459 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
460 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
461\r
462 @param MemoryType The type of memory to allocate.\r
463 @param AllocationSize The number of bytes to allocate.\r
464\r
465 @return A pointer to the allocated buffer or NULL if allocation fails.\r
466\r
467**/\r
468VOID *\r
469InternalAllocatePool (\r
470 IN EFI_MEMORY_TYPE MemoryType, \r
471 IN UINTN AllocationSize\r
472 )\r
473{\r
474 EFI_STATUS Status;\r
475 VOID *Memory;\r
476\r
477 Status = SmmAllocatePool (MemoryType, AllocationSize, &Memory);\r
478 if (EFI_ERROR (Status)) {\r
479 Memory = NULL;\r
480 }\r
481 return Memory;\r
482}\r
483\r
484/**\r
485 Allocates a buffer of type EfiBootServicesData.\r
486\r
487 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
488 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
489 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
490\r
491 @param AllocationSize The number of bytes to allocate.\r
492\r
493 @return A pointer to the allocated buffer or NULL if allocation fails.\r
494\r
495**/\r
496VOID *\r
497EFIAPI\r
498AllocatePool (\r
499 IN UINTN AllocationSize\r
500 )\r
501{\r
502 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
503}\r
504\r
505/**\r
506 Allocates a buffer of type EfiRuntimeServicesData.\r
507\r
508 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns\r
509 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
510 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
511\r
512 @param AllocationSize The number of bytes to allocate.\r
513\r
514 @return A pointer to the allocated buffer or NULL if allocation fails.\r
515\r
516**/\r
517VOID *\r
518EFIAPI\r
519AllocateRuntimePool (\r
520 IN UINTN AllocationSize\r
521 )\r
522{\r
523 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
524}\r
525\r
526/**\r
527 Allocates a buffer of type EfiReservedMemoryType.\r
528\r
529 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns\r
530 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
531 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
532\r
533 @param AllocationSize The number of bytes to allocate.\r
534\r
535 @return A pointer to the allocated buffer or NULL if allocation fails.\r
536\r
537**/\r
538VOID *\r
539EFIAPI\r
540AllocateReservedPool (\r
541 IN UINTN AllocationSize\r
542 )\r
543{\r
544 return NULL;\r
545}\r
546\r
547/**\r
548 Allocates and zeros a buffer of a certain pool type.\r
549\r
550 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer\r
551 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid\r
552 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,\r
553 then NULL is returned.\r
554\r
555 @param PoolType The type of memory to allocate.\r
556 @param AllocationSize The number of bytes to allocate and zero.\r
557\r
558 @return A pointer to the allocated buffer or NULL if allocation fails.\r
559\r
560**/\r
561VOID *\r
562InternalAllocateZeroPool (\r
563 IN EFI_MEMORY_TYPE PoolType, \r
564 IN UINTN AllocationSize\r
565 ) \r
566{\r
567 VOID *Memory;\r
568\r
569 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
570 if (Memory != NULL) {\r
571 Memory = ZeroMem (Memory, AllocationSize);\r
572 }\r
573 return Memory;\r
574}\r
575\r
576/**\r
577 Allocates and zeros a buffer of type EfiBootServicesData.\r
578\r
579 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
580 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
581 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
582 request, then NULL is returned.\r
583\r
584 @param AllocationSize The number of bytes to allocate and zero.\r
585\r
586 @return A pointer to the allocated buffer or NULL if allocation fails.\r
587\r
588**/\r
589VOID *\r
590EFIAPI\r
591AllocateZeroPool (\r
592 IN UINTN AllocationSize\r
593 )\r
594{\r
595 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
596}\r
597\r
598/**\r
599 Allocates and zeros a buffer of type EfiRuntimeServicesData.\r
600\r
601 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the\r
602 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
603 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
604 request, then NULL is returned.\r
605\r
606 @param AllocationSize The number of bytes to allocate and zero.\r
607\r
608 @return A pointer to the allocated buffer or NULL if allocation fails.\r
609\r
610**/\r
611VOID *\r
612EFIAPI\r
613AllocateRuntimeZeroPool (\r
614 IN UINTN AllocationSize\r
615 )\r
616{\r
617 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
618}\r
619\r
620/**\r
621 Allocates and zeros a buffer of type EfiReservedMemoryType.\r
622\r
623 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the\r
624 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
625 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
626 request, then NULL is returned.\r
627\r
628 @param AllocationSize The number of bytes to allocate and zero.\r
629\r
630 @return A pointer to the allocated buffer or NULL if allocation fails.\r
631\r
632**/\r
633VOID *\r
634EFIAPI\r
635AllocateReservedZeroPool (\r
636 IN UINTN AllocationSize\r
637 )\r
638{\r
639 return NULL;\r
640}\r
641\r
642/**\r
643 Copies a buffer to an allocated buffer of a certain pool type.\r
644\r
645 Allocates the number bytes specified by AllocationSize of a certain pool type, copies\r
646 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
647 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
648 is not enough memory remaining to satisfy the request, then NULL is returned.\r
649 If Buffer is NULL, then ASSERT().\r
650 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
651\r
652 @param PoolType The type of pool to allocate.\r
653 @param AllocationSize The number of bytes to allocate and zero.\r
654 @param Buffer The buffer to copy to the allocated buffer.\r
655\r
656 @return A pointer to the allocated buffer or NULL if allocation fails.\r
657\r
658**/\r
659VOID *\r
660InternalAllocateCopyPool (\r
661 IN EFI_MEMORY_TYPE PoolType, \r
662 IN UINTN AllocationSize,\r
663 IN CONST VOID *Buffer\r
664 ) \r
665{\r
666 VOID *Memory;\r
667\r
668 ASSERT (Buffer != NULL);\r
669 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r
670\r
671 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
672 if (Memory != NULL) {\r
673 Memory = CopyMem (Memory, Buffer, AllocationSize);\r
674 }\r
675 return Memory;\r
676} \r
677\r
678/**\r
679 Copies a buffer to an allocated buffer of type EfiBootServicesData.\r
680\r
681 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies\r
682 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
683 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
684 is not enough memory remaining to satisfy the request, then NULL is returned.\r
685 \r
686 If Buffer is NULL, then ASSERT().\r
687 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
688\r
689 @param AllocationSize The number of bytes to allocate and zero.\r
690 @param Buffer The buffer to copy to the allocated buffer.\r
691\r
692 @return A pointer to the allocated buffer or NULL if allocation fails.\r
693\r
694**/\r
695VOID *\r
696EFIAPI\r
697AllocateCopyPool (\r
698 IN UINTN AllocationSize,\r
699 IN CONST VOID *Buffer\r
700 )\r
701{\r
702 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
703}\r
704\r
705/**\r
706 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.\r
707\r
708 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies\r
709 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
710 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
711 is not enough memory remaining to satisfy the request, then NULL is returned.\r
712 \r
713 If Buffer is NULL, then ASSERT().\r
714 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
715\r
716 @param AllocationSize The number of bytes to allocate and zero.\r
717 @param Buffer The buffer to copy to the allocated buffer.\r
718\r
719 @return A pointer to the allocated buffer or NULL if allocation fails.\r
720\r
721**/\r
722VOID *\r
723EFIAPI\r
724AllocateRuntimeCopyPool (\r
725 IN UINTN AllocationSize,\r
726 IN CONST VOID *Buffer\r
727 )\r
728{\r
729 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
730}\r
731\r
732/**\r
733 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.\r
734\r
735 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies\r
736 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
737 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
738 is not enough memory remaining to satisfy the request, then NULL is returned.\r
739 \r
740 If Buffer is NULL, then ASSERT().\r
741 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
742\r
743 @param AllocationSize The number of bytes to allocate and zero.\r
744 @param Buffer The buffer to copy to the allocated buffer.\r
745\r
746 @return A pointer to the allocated buffer or NULL if allocation fails.\r
747\r
748**/\r
749VOID *\r
750EFIAPI\r
751AllocateReservedCopyPool (\r
752 IN UINTN AllocationSize,\r
753 IN CONST VOID *Buffer\r
754 )\r
755{\r
756 return NULL;\r
757}\r
758\r
759/**\r
760 Reallocates a buffer of a specified memory type.\r
761\r
762 Allocates and zeros the number bytes specified by NewSize from memory of the type\r
763 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and \r
764 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
765 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
766 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
767 enough memory remaining to satisfy the request, then NULL is returned.\r
768 \r
769 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
770 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
771\r
772 @param PoolType The type of pool to allocate.\r
773 @param OldSize The size, in bytes, of OldBuffer.\r
774 @param NewSize The size, in bytes, of the buffer to reallocate.\r
775 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
776 parameter that may be NULL.\r
777\r
778 @return A pointer to the allocated buffer or NULL if allocation fails.\r
779\r
780**/\r
781VOID *\r
782InternalReallocatePool (\r
783 IN EFI_MEMORY_TYPE PoolType, \r
784 IN UINTN OldSize,\r
785 IN UINTN NewSize,\r
786 IN VOID *OldBuffer OPTIONAL\r
787 )\r
788{\r
789 VOID *NewBuffer;\r
790\r
791 NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);\r
792 if (NewBuffer != NULL && OldBuffer != NULL) {\r
793 CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));\r
794 FreePool (OldBuffer);\r
795 }\r
796 return NewBuffer;\r
797}\r
798\r
799/**\r
800 Reallocates a buffer of type EfiBootServicesData.\r
801\r
802 Allocates and zeros the number bytes specified by NewSize from memory of type\r
803 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and \r
804 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
805 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
806 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
807 enough memory remaining to satisfy the request, then NULL is returned.\r
808 \r
809 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
810 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
811\r
812 @param OldSize The size, in bytes, of OldBuffer.\r
813 @param NewSize The size, in bytes, of the buffer to reallocate.\r
814 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
815 parameter that may be NULL.\r
816\r
817 @return A pointer to the allocated buffer or NULL if allocation fails.\r
818\r
819**/\r
820VOID *\r
821EFIAPI\r
822ReallocatePool (\r
823 IN UINTN OldSize,\r
824 IN UINTN NewSize,\r
825 IN VOID *OldBuffer OPTIONAL\r
826 )\r
827{\r
828 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
829}\r
830\r
831/**\r
832 Reallocates a buffer of type EfiRuntimeServicesData.\r
833\r
834 Allocates and zeros the number bytes specified by NewSize from memory of type\r
835 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and \r
836 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
837 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
838 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
839 enough memory remaining to satisfy the request, then NULL is returned.\r
840\r
841 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
842 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
843\r
844 @param OldSize The size, in bytes, of OldBuffer.\r
845 @param NewSize The size, in bytes, of the buffer to reallocate.\r
846 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
847 parameter that may be NULL.\r
848\r
849 @return A pointer to the allocated buffer or NULL if allocation fails.\r
850\r
851**/\r
852VOID *\r
853EFIAPI\r
854ReallocateRuntimePool (\r
855 IN UINTN OldSize,\r
856 IN UINTN NewSize,\r
857 IN VOID *OldBuffer OPTIONAL\r
858 )\r
859{\r
860 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
861}\r
862\r
863/**\r
864 Reallocates a buffer of type EfiReservedMemoryType.\r
865\r
866 Allocates and zeros the number bytes specified by NewSize from memory of type\r
867 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and \r
868 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
869 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
870 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
871 enough memory remaining to satisfy the request, then NULL is returned.\r
872\r
873 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
874 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
875\r
876 @param OldSize The size, in bytes, of OldBuffer.\r
877 @param NewSize The size, in bytes, of the buffer to reallocate.\r
878 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
879 parameter that may be NULL.\r
880\r
881 @return A pointer to the allocated buffer or NULL if allocation fails.\r
882\r
883**/\r
884VOID *\r
885EFIAPI\r
886ReallocateReservedPool (\r
887 IN UINTN OldSize,\r
888 IN UINTN NewSize,\r
889 IN VOID *OldBuffer OPTIONAL\r
890 )\r
891{\r
892 return NULL;\r
893}\r
894\r
895/**\r
896 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
897 Memory Allocation Library.\r
898\r
899 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
900 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
901 resources, then this function will perform no actions.\r
902 \r
903 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
904 then ASSERT().\r
905\r
906 @param Buffer Pointer to the buffer to free.\r
907\r
908**/\r
909VOID\r
910EFIAPI\r
911FreePool (\r
912 IN VOID *Buffer\r
913 )\r
914{\r
915 EFI_STATUS Status;\r
916\r
34401578
LG
917 if (BufferInSmram (Buffer)) {\r
918 //\r
919 // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePool() service.\r
920 // So, SmmFreePool() service is used to free it.\r
921 //\r
922 Status = SmmFreePool (Buffer);\r
923 } else {\r
924 //\r
925 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePool() service.\r
926 // So, gBS->FreePool() service is used to free it.\r
927 //\r
928 Status = gBS->FreePool (Buffer);\r
929 }\r
713b7781 930 ASSERT_EFI_ERROR (Status);\r
931}\r
932\r