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