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