]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/LockBoxLib/LockBoxDxe.c
OvmfPkg: Apply uncrustify changes
[mirror_edk2.git] / OvmfPkg / Library / LockBoxLib / LockBoxDxe.c
1 /** @file
2
3 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
4
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Uefi.h>
10
11 #include <Library/MemoryAllocationLib.h>
12 #include <Library/UefiBootServicesTableLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/DebugLib.h>
15 #include <Protocol/LockBox.h>
16 #include <LockBoxLib.h>
17
18 /**
19 Allocate memory below 4G memory address.
20
21 This function allocates memory below 4G memory address.
22
23 @param MemoryType Memory type of memory to allocate.
24 @param Size Size of memory to allocate.
25
26 @return Allocated address for output.
27
28 **/
29 STATIC
30 VOID *
31 AllocateMemoryBelow4G (
32 IN EFI_MEMORY_TYPE MemoryType,
33 IN UINTN Size
34 )
35 {
36 UINTN Pages;
37 EFI_PHYSICAL_ADDRESS Address;
38 EFI_STATUS Status;
39 VOID *Buffer;
40 UINTN AllocRemaining;
41
42 Pages = EFI_SIZE_TO_PAGES (Size);
43 Address = 0xffffffff;
44
45 //
46 // Since we need to use gBS->AllocatePages to get a buffer below
47 // 4GB, there is a good chance that space will be wasted for very
48 // small allocation. We keep track of unused portions of the page
49 // allocations, and use these to allocate memory for small buffers.
50 //
51 ASSERT (mLockBoxGlobal->Signature == LOCK_BOX_GLOBAL_SIGNATURE);
52 if ((UINTN)mLockBoxGlobal->SubPageRemaining >= Size) {
53 Buffer = (VOID *)(UINTN)mLockBoxGlobal->SubPageBuffer;
54 mLockBoxGlobal->SubPageBuffer += (UINT32)Size;
55 mLockBoxGlobal->SubPageRemaining -= (UINT32)Size;
56 return Buffer;
57 }
58
59 Status = gBS->AllocatePages (
60 AllocateMaxAddress,
61 MemoryType,
62 Pages,
63 &Address
64 );
65 if (EFI_ERROR (Status)) {
66 return NULL;
67 }
68
69 Buffer = (VOID *)(UINTN)Address;
70 ZeroMem (Buffer, EFI_PAGES_TO_SIZE (Pages));
71
72 AllocRemaining = EFI_PAGES_TO_SIZE (Pages) - Size;
73 if (AllocRemaining > (UINTN)mLockBoxGlobal->SubPageRemaining) {
74 mLockBoxGlobal->SubPageBuffer = (UINT32)(Address + Size);
75 mLockBoxGlobal->SubPageRemaining = (UINT32)AllocRemaining;
76 }
77
78 return Buffer;
79 }
80
81 /**
82 Allocates a buffer of type EfiACPIMemoryNVS.
83
84 Allocates the number bytes specified by AllocationSize of type
85 EfiACPIMemoryNVS and returns a pointer to the allocated buffer.
86 If AllocationSize is 0, then a valid buffer of 0 size is
87 returned. If there is not enough memory remaining to satisfy
88 the request, then NULL is returned.
89
90 @param AllocationSize The number of bytes to allocate.
91
92 @return A pointer to the allocated buffer or NULL if allocation fails.
93
94 **/
95 VOID *
96 EFIAPI
97 AllocateAcpiNvsPool (
98 IN UINTN AllocationSize
99 )
100 {
101 return AllocateMemoryBelow4G (EfiACPIMemoryNVS, AllocationSize);
102 }
103
104 EFI_STATUS
105 EFIAPI
106 LockBoxDxeLibInitialize (
107 IN EFI_HANDLE ImageHandle,
108 IN EFI_SYSTEM_TABLE *SystemTable
109 )
110 {
111 EFI_STATUS Status;
112 VOID *Interface;
113
114 Status = LockBoxLibInitialize ();
115 if (!EFI_ERROR (Status)) {
116 if (PcdGetBool (PcdAcpiS3Enable)) {
117 //
118 // When S3 enabled, the first driver run with this library linked will
119 // have this library constructor to install LockBox protocol on the
120 // ImageHandle. As other drivers may have gEfiLockBoxProtocolGuid
121 // dependency, the first driver should run before them.
122 //
123 Status = gBS->LocateProtocol (&gEfiLockBoxProtocolGuid, NULL, &Interface);
124 if (EFI_ERROR (Status)) {
125 Status = gBS->InstallProtocolInterface (
126 &ImageHandle,
127 &gEfiLockBoxProtocolGuid,
128 EFI_NATIVE_INTERFACE,
129 NULL
130 );
131 ASSERT_EFI_ERROR (Status);
132 }
133 }
134 }
135
136 return Status;
137 }