]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockUefiLib.c
SecurityPkg: SecureBootVariableLib: Added unit tests
[mirror_edk2.git] / SecurityPkg / Library / SecureBootVariableLib / UnitTest / MockUefiLib.c
1 /** @file
2 The UEFI Library provides functions and macros that simplify the development of
3 UEFI Drivers and UEFI Applications. These functions and macros help manage EFI
4 events, build simple locks utilizing EFI Task Priority Levels (TPLs), install
5 EFI Driver Model related protocols, manage Unicode string tables for UEFI Drivers,
6 and print messages on the console output and standard error devices.
7
8 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
10
11 **/
12
13 #include <Uefi.h>
14
15 #include <Library/DebugLib.h>
16 #include <Library/MemoryAllocationLib.h>
17 #include <Library/UefiRuntimeServicesTableLib.h>
18
19 /**
20 Returns the status whether get the variable success. The function retrieves
21 variable through the UEFI Runtime Service GetVariable(). The
22 returned buffer is allocated using AllocatePool(). The caller is responsible
23 for freeing this buffer with FreePool().
24
25 If Name is NULL, then ASSERT().
26 If Guid is NULL, then ASSERT().
27 If Value is NULL, then ASSERT().
28
29 @param[in] Name The pointer to a Null-terminated Unicode string.
30 @param[in] Guid The pointer to an EFI_GUID structure
31 @param[out] Value The buffer point saved the variable info.
32 @param[out] Size The buffer size of the variable.
33
34 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.
35 @return EFI_SUCCESS Find the specified variable.
36 @return Others Errors Return errors from call to gRT->GetVariable.
37
38 **/
39 EFI_STATUS
40 EFIAPI
41 GetVariable2 (
42 IN CONST CHAR16 *Name,
43 IN CONST EFI_GUID *Guid,
44 OUT VOID **Value,
45 OUT UINTN *Size OPTIONAL
46 )
47 {
48 EFI_STATUS Status;
49 UINTN BufferSize;
50
51 ASSERT (Name != NULL && Guid != NULL && Value != NULL);
52
53 //
54 // Try to get the variable size.
55 //
56 BufferSize = 0;
57 *Value = NULL;
58 if (Size != NULL) {
59 *Size = 0;
60 }
61
62 Status = gRT->GetVariable ((CHAR16 *)Name, (EFI_GUID *)Guid, NULL, &BufferSize, *Value);
63 if (Status != EFI_BUFFER_TOO_SMALL) {
64 return Status;
65 }
66
67 //
68 // Allocate buffer to get the variable.
69 //
70 *Value = AllocatePool (BufferSize);
71 ASSERT (*Value != NULL);
72 if (*Value == NULL) {
73 return EFI_OUT_OF_RESOURCES;
74 }
75
76 //
77 // Get the variable data.
78 //
79 Status = gRT->GetVariable ((CHAR16 *)Name, (EFI_GUID *)Guid, NULL, &BufferSize, *Value);
80 if (EFI_ERROR (Status)) {
81 FreePool (*Value);
82 *Value = NULL;
83 }
84
85 if (Size != NULL) {
86 *Size = BufferSize;
87 }
88
89 return Status;
90 }
91
92 /** Return the attributes of the variable.
93
94 Returns the status whether get the variable success. The function retrieves
95 variable through the UEFI Runtime Service GetVariable(). The
96 returned buffer is allocated using AllocatePool(). The caller is responsible
97 for freeing this buffer with FreePool(). The attributes are returned if
98 the caller provides a valid Attribute parameter.
99
100 If Name is NULL, then ASSERT().
101 If Guid is NULL, then ASSERT().
102 If Value is NULL, then ASSERT().
103
104 @param[in] Name The pointer to a Null-terminated Unicode string.
105 @param[in] Guid The pointer to an EFI_GUID structure
106 @param[out] Value The buffer point saved the variable info.
107 @param[out] Size The buffer size of the variable.
108 @param[out] Attr The pointer to the variable attributes as found in var store
109
110 @retval EFI_OUT_OF_RESOURCES Allocate buffer failed.
111 @retval EFI_SUCCESS Find the specified variable.
112 @retval Others Errors Return errors from call to gRT->GetVariable.
113
114 **/
115 EFI_STATUS
116 EFIAPI
117 GetVariable3 (
118 IN CONST CHAR16 *Name,
119 IN CONST EFI_GUID *Guid,
120 OUT VOID **Value,
121 OUT UINTN *Size OPTIONAL,
122 OUT UINT32 *Attr OPTIONAL
123 )
124 {
125 EFI_STATUS Status;
126 UINTN BufferSize;
127
128 ASSERT (Name != NULL && Guid != NULL && Value != NULL);
129
130 //
131 // Try to get the variable size.
132 //
133 BufferSize = 0;
134 *Value = NULL;
135 if (Size != NULL) {
136 *Size = 0;
137 }
138
139 if (Attr != NULL) {
140 *Attr = 0;
141 }
142
143 Status = gRT->GetVariable ((CHAR16 *)Name, (EFI_GUID *)Guid, Attr, &BufferSize, *Value);
144 if (Status != EFI_BUFFER_TOO_SMALL) {
145 return Status;
146 }
147
148 //
149 // Allocate buffer to get the variable.
150 //
151 *Value = AllocatePool (BufferSize);
152 ASSERT (*Value != NULL);
153 if (*Value == NULL) {
154 return EFI_OUT_OF_RESOURCES;
155 }
156
157 //
158 // Get the variable data.
159 //
160 Status = gRT->GetVariable ((CHAR16 *)Name, (EFI_GUID *)Guid, Attr, &BufferSize, *Value);
161 if (EFI_ERROR (Status)) {
162 FreePool (*Value);
163 *Value = NULL;
164 }
165
166 if (Size != NULL) {
167 *Size = BufferSize;
168 }
169
170 return Status;
171 }
172
173 /**
174 Returns a pointer to an allocated buffer that contains the contents of a
175 variable retrieved through the UEFI Runtime Service GetVariable(). This
176 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.
177 The returned buffer is allocated using AllocatePool(). The caller is
178 responsible for freeing this buffer with FreePool().
179
180 If Name is NULL, then ASSERT().
181 If Value is NULL, then ASSERT().
182
183 @param[in] Name The pointer to a Null-terminated Unicode string.
184 @param[out] Value The buffer point saved the variable info.
185 @param[out] Size The buffer size of the variable.
186
187 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.
188 @return EFI_SUCCESS Find the specified variable.
189 @return Others Errors Return errors from call to gRT->GetVariable.
190
191 **/
192 EFI_STATUS
193 EFIAPI
194 GetEfiGlobalVariable2 (
195 IN CONST CHAR16 *Name,
196 OUT VOID **Value,
197 OUT UINTN *Size OPTIONAL
198 )
199 {
200 return GetVariable2 (Name, &gEfiGlobalVariableGuid, Value, Size);
201 }