Change IPF version AuthVariable driver to support multiple-platform feature.
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / EsalVariableDxeSal / Variable.h
CommitLineData
0c18794e 1/** @file\r
2 Internal header file for Extended SAL variable service module.\r
3\r
4Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials \r
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#ifndef _VARIABLE_H_\r
16#define _VARIABLE_H_\r
17\r
18#include <PiDxe.h>\r
19\r
20#include <Protocol/VariableWrite.h>\r
21#include <Protocol/FaultTolerantWrite.h>\r
22#include <Protocol/FirmwareVolumeBlock.h>\r
23#include <Protocol/Variable.h>\r
24#include <Protocol/ExtendedSalBootService.h>\r
25#include <Protocol/ExtendedSalServiceClasses.h>\r
26\r
27#include <Guid/GlobalVariable.h>\r
28#include <Guid/AuthenticatedVariableFormat.h>\r
29#include <Guid/ImageAuthentication.h>\r
30#include <Guid/EventGroup.h>\r
31\r
32#include <Library/PcdLib.h>\r
4f8ef5ce 33#include <Library/HobLib.h>\r
0c18794e 34#include <Library/UefiDriverEntryPoint.h>\r
35#include <Library/DxeServicesTableLib.h>\r
36#include <Library/UefiRuntimeLib.h>\r
37#include <Library/DebugLib.h>\r
38#include <Library/BaseMemoryLib.h>\r
39#include <Library/UefiBootServicesTableLib.h>\r
40#include <Library/UefiLib.h>\r
41#include <Library/BaseLib.h>\r
42#include <Library/SynchronizationLib.h>\r
43#include <Library/MemoryAllocationLib.h>\r
44#include <Library/ExtendedSalLib.h>\r
45#include <Library/BaseCryptLib.h>\r
46\r
47#define MAX_NAME_SIZE 0x100\r
48#define NUM_VAR_NAME 9 // Number of pre-defined variable name to be referenced\r
49#define VAR_PLATFORM_LANG_CODES 0 // Index of "PlatformLangCodes" variable\r
50#define VAR_LANG_CODES 1 // Index of "LangCodes" variable\r
51#define VAR_PLATFORM_LANG 2 // Index of "PlatformLang" variable\r
52#define VAR_LANG 3 // Index of "Lang" variable\r
53#define VAR_HW_ERR_REC 4 // Index of "HwErrRecXXXX" variable\r
54#define VAR_AUTH_KEY_DB 5 // Index of "AuthVarKeyDatabase" variable\r
55#define VAR_SETUP_MODE 6 // Index of "SetupMode" variable\r
56#define VAR_PLATFORM_KEY 7 // Index of "PK" variable\r
57#define VAR_KEY_EXCHANGE_KEY 8 // Index of "KEK" variable\r
58\r
59///\r
60/// "AuthVarKeyDatabase" variable for the Public Key store.\r
61///\r
62#define AUTHVAR_KEYDB_NAME L"AuthVarKeyDatabase"\r
63#define AUTHVAR_KEYDB_NAME_SIZE 38\r
64\r
65///\r
66/// The maximum size of the public key database, restricted by maximum individal EFI \r
67/// varible size, and excluding the variable header and name size.\r
68///\r
69#define MAX_KEYDB_SIZE (FixedPcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER) - AUTHVAR_KEYDB_NAME_SIZE)\r
70#define MAX_KEY_NUM (MAX_KEYDB_SIZE / EFI_CERT_TYPE_RSA2048_SIZE)\r
71\r
72///\r
73/// The size of a 3 character ISO639 language code.\r
74///\r
75#define ISO_639_2_ENTRY_SIZE 3\r
76\r
77typedef enum {\r
78 Physical,\r
79 Virtual\r
80} VARIABLE_POINTER_TYPE;\r
81\r
82typedef struct {\r
83 EFI_PHYSICAL_ADDRESS CurrPtr;\r
84 EFI_PHYSICAL_ADDRESS EndPtr;\r
85 EFI_PHYSICAL_ADDRESS StartPtr;\r
86 BOOLEAN Volatile;\r
87} VARIABLE_POINTER_TRACK;\r
88\r
89typedef struct {\r
90 EFI_PHYSICAL_ADDRESS VolatileVariableBase;\r
91 EFI_PHYSICAL_ADDRESS NonVolatileVariableBase;\r
92 EFI_LOCK VariableServicesLock;\r
93} VARIABLE_GLOBAL;\r
94\r
95typedef struct {\r
96 VARIABLE_GLOBAL VariableGlobal[2];\r
97 CHAR16 *VariableName[2][NUM_VAR_NAME];\r
98 EFI_GUID *GlobalVariableGuid[2];\r
99 UINTN VolatileLastVariableOffset;\r
100 UINTN NonVolatileLastVariableOffset;\r
101 UINTN CommonVariableTotalSize;\r
102 UINTN HwErrVariableTotalSize;\r
103 CHAR8 *PlatformLangCodes[2];\r
104 CHAR8 *LangCodes[2];\r
105 CHAR8 *PlatformLang[2];\r
106 CHAR8 Lang[ISO_639_2_ENTRY_SIZE + 1];\r
107 UINT32 FvbInstance;\r
108 UINT32 ReentrantState;\r
109 EFI_GUID *AuthenticatedVariableGuid[2];\r
110 EFI_GUID *CertRsa2048Sha256Guid[2];\r
111 EFI_GUID *ImageSecurityDatabaseGuid[2];\r
112 VOID *HashContext[2]; // Hash context pointer\r
113 UINT8 KeyList[MAX_KEYDB_SIZE]; // Cached Platform Key list\r
114 UINT8 PubKeyStore[MAX_KEYDB_SIZE]; // Cached Public Key list\r
115} ESAL_VARIABLE_GLOBAL;\r
116\r
117typedef struct {\r
118 EFI_GUID *Guid;\r
119 CHAR16 *Name;\r
120 UINT32 Attributes;\r
121 UINTN DataSize;\r
122 VOID *Data;\r
123} VARIABLE_CACHE_ENTRY;\r
124\r
125\r
126extern ESAL_VARIABLE_GLOBAL *mVariableModuleGlobal;\r
127\r
128//\r
129// Functions\r
130//\r
131\r
132/**\r
133 Initializes variable store area for non-volatile and volatile variable.\r
134\r
135 This function allocates and initializes memory space for global context of ESAL\r
136 variable service and variable store area for non-volatile and volatile variable.\r
137\r
138 @param[in] ImageHandle The Image handle of this driver.\r
139 @param[in] SystemTable The pointer of EFI_SYSTEM_TABLE.\r
140\r
141 @retval EFI_SUCCESS Function successfully executed.\r
142 @retval EFI_OUT_OF_RESOURCES Failed to allocate enough memory resource.\r
143\r
144**/\r
145EFI_STATUS\r
146VariableCommonInitialize (\r
147 IN EFI_HANDLE ImageHandle,\r
148 IN EFI_SYSTEM_TABLE *SystemTable\r
149 );\r
150\r
151/**\r
152 Entry point of Extended SAL Variable service module.\r
153\r
154 This function is the entry point of Extended SAL Variable service module.\r
155 It registers all functions of Extended SAL Variable class, initializes\r
156 variable store for non-volatile and volatile variables, and registers\r
157 notification function for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
158\r
159 @param[in] ImageHandle The Image handle of this driver.\r
160 @param[in] SystemTable The pointer of EFI_SYSTEM_TABLE.\r
161\r
162 @retval EFI_SUCCESS Extended SAL Variable Services Class successfully registered.\r
163\r
164**/\r
165EFI_STATUS\r
166EFIAPI\r
167VariableServiceInitialize (\r
168 IN EFI_HANDLE ImageHandle,\r
169 IN EFI_SYSTEM_TABLE *SystemTable\r
170 );\r
171\r
172/**\r
173 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
174\r
175 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
176 It convers pointer to new virtual address.\r
177\r
178 @param[in] Event The event whose notification function is being invoked.\r
179 @param[in] Context The pointer to the notification function's context.\r
180\r
181**/\r
182VOID\r
183EFIAPI\r
184VariableClassAddressChangeEvent (\r
185 IN EFI_EVENT Event,\r
186 IN VOID *Context\r
187 );\r
188\r
189/**\r
190 Implements EsalGetVariable function of Extended SAL Variable Services Class.\r
191\r
192 This function implements EsalGetVariable function of Extended SAL Variable Services Class.\r
193 It is equivalent in functionality to the EFI Runtime Service GetVariable().\r
194 \r
195 @param[in] VariableName A Null-terminated Unicode string that is the name of\r
196 the vendor's variable.\r
197 @param[in] VendorGuid A unique identifier for the vendor.\r
198 @param[out] Attributes If not NULL, a pointer to the memory location to return the \r
199 attributes bitmask for the variable.\r
200 @param[in, out] DataSize Size of Data found. If size is less than the\r
201 data, this value contains the required size.\r
202 @param[out] Data On input, the size in bytes of the return Data buffer. \r
203 On output, the size of data returned in Data.\r
204 @param[in] VirtualMode Current calling mode for this function.\r
205 @param[in] Global Context of this Extended SAL Variable Services Class call.\r
206\r
207 @retval EFI_SUCCESS The function completed successfully. \r
208 @retval EFI_NOT_FOUND The variable was not found.\r
209 @retval EFI_BUFFER_TOO_SMALL DataSize is too small for the result. DataSize has \r
210 been updated with the size needed to complete the request.\r
211 @retval EFI_INVALID_PARAMETER VariableName is NULL.\r
212 @retval EFI_INVALID_PARAMETER VendorGuid is NULL.\r
213 @retval EFI_INVALID_PARAMETER DataSize is NULL.\r
214 @retval EFI_INVALID_PARAMETER DataSize is not too small and Data is NULL.\r
215 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.\r
216 @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due to an authentication failure.\r
217\r
218**/\r
219EFI_STATUS\r
220EFIAPI\r
221EsalGetVariable (\r
222 IN CHAR16 *VariableName,\r
223 IN EFI_GUID *VendorGuid,\r
224 OUT UINT32 *Attributes OPTIONAL,\r
225 IN OUT UINTN *DataSize,\r
226 OUT VOID *Data,\r
227 IN BOOLEAN VirtualMode,\r
228 IN ESAL_VARIABLE_GLOBAL *Global\r
229 );\r
230\r
231/**\r
232 Implements EsalGetNextVariableName function of Extended SAL Variable Services Class.\r
233\r
234 This function implements EsalGetNextVariableName function of Extended SAL Variable Services Class.\r
235 It is equivalent in functionality to the EFI Runtime Service GetNextVariableName().\r
236 \r
237 @param[in, out] VariableNameSize Size of the variable\r
238 @param[in, out] VariableName On input, supplies the last VariableName that was returned by GetNextVariableName().\r
239 On output, returns the Null-terminated Unicode string of the current variable.\r
240 @param[in, out] VendorGuid On input, supplies the last VendorGuid that was returned by GetNextVariableName().\r
241 On output, returns the VendorGuid of the current variable. \r
242 @param[in] VirtualMode Current calling mode for this function.\r
243 @param[in] Global Context of this Extended SAL Variable Services Class call.\r
244\r
245 @retval EFI_SUCCESS The function completed successfully. \r
246 @retval EFI_NOT_FOUND The next variable was not found.\r
247 @retval EFI_BUFFER_TOO_SMALL VariableNameSize is too small for the result. \r
248 VariableNameSize has been updated with the size needed to complete the request.\r
249 @retval EFI_INVALID_PARAMETER VariableNameSize is NULL.\r
250 @retval EFI_INVALID_PARAMETER VariableName is NULL.\r
251 @retval EFI_INVALID_PARAMETER VendorGuid is NULL.\r
252 @retval EFI_DEVICE_ERROR The variable name could not be retrieved due to a hardware error.\r
253\r
254**/\r
255EFI_STATUS\r
256EFIAPI\r
257EsalGetNextVariableName (\r
258 IN OUT UINTN *VariableNameSize,\r
259 IN OUT CHAR16 *VariableName,\r
260 IN OUT EFI_GUID *VendorGuid,\r
261 IN BOOLEAN VirtualMode,\r
262 IN ESAL_VARIABLE_GLOBAL *Global\r
263 );\r
264\r
265/**\r
266 Implements EsalSetVariable function of Extended SAL Variable Services Class.\r
267\r
268 This function implements EsalSetVariable function of Extended SAL Variable Services Class.\r
269 It is equivalent in functionality to the EFI Runtime Service SetVariable().\r
270 \r
271 @param[in] VariableName A Null-terminated Unicode string that is the name of the vendor's\r
272 variable. Each VariableName is unique for each \r
273 VendorGuid. VariableName must contain 1 or more \r
274 Unicode characters. If VariableName is an empty Unicode \r
275 string, then EFI_INVALID_PARAMETER is returned.\r
276 @param[in] VendorGuid A unique identifier for the vendor.\r
277 @param[in] Attributes Attributes bitmask to set for the variable.\r
278 @param[in] DataSize The size in bytes of the Data buffer. A size of zero causes the\r
279 variable to be deleted.\r
280 @param[in] Data The contents for the variable.\r
281 @param[in] VirtualMode Current calling mode for this function.\r
282 @param[in] Global Context of this Extended SAL Variable Services Class call.\r
283\r
284 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as \r
285 defined by the Attributes.\r
286 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied, or the \r
287 DataSize exceeds the maximum allowed.\r
288 @retval EFI_INVALID_PARAMETER VariableName is an empty Unicode string.\r
289 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.\r
290 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.\r
291 @retval EFI_WRITE_PROTECTED The variable in question is read-only.\r
292 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.\r
293 @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due to an authentication failure.\r
294 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.\r
295\r
296**/\r
297EFI_STATUS\r
298EFIAPI\r
299EsalSetVariable (\r
300 IN CHAR16 *VariableName,\r
301 IN EFI_GUID *VendorGuid,\r
302 IN UINT32 Attributes,\r
303 IN UINTN DataSize,\r
304 IN VOID *Data,\r
305 IN BOOLEAN VirtualMode,\r
306 IN ESAL_VARIABLE_GLOBAL *Global\r
307 );\r
308\r
309/**\r
310 Implements EsalQueryVariableInfo function of Extended SAL Variable Services Class.\r
311\r
312 This function implements EsalQueryVariableInfo function of Extended SAL Variable Services Class.\r
313 It is equivalent in functionality to the EFI Runtime Service QueryVariableInfo().\r
314\r
315 @param[in] Attributes Attributes bitmask to specify the type of variables\r
316 on which to return information.\r
317 @param[out] MaximumVariableStorageSize On output the maximum size of the storage space available for \r
318 the EFI variables associated with the attributes specified. \r
319 @param[out] RemainingVariableStorageSize Returns the remaining size of the storage space available for EFI \r
320 variables associated with the attributes specified.\r
321 @param[out] MaximumVariableSize Returns the maximum size of an individual EFI variable \r
322 associated with the attributes specified.\r
323 @param[in] VirtualMode Current calling mode for this function\r
324 @param[in] Global Context of this Extended SAL Variable Services Class call\r
325\r
326 @retval EFI_SUCCESS Valid answer returned.\r
327 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
328 @retval EFI_UNSUPPORTED The attribute is not supported on this platform, and the \r
329 MaximumVariableStorageSize, RemainingVariableStorageSize, \r
330 MaximumVariableSize are undefined.\r
331**/\r
332EFI_STATUS\r
333EFIAPI\r
334EsalQueryVariableInfo (\r
335 IN UINT32 Attributes,\r
336 OUT UINT64 *MaximumVariableStorageSize,\r
337 OUT UINT64 *RemainingVariableStorageSize,\r
338 OUT UINT64 *MaximumVariableSize,\r
339 IN BOOLEAN VirtualMode,\r
340 IN ESAL_VARIABLE_GLOBAL *Global\r
341 );\r
342\r
343/**\r
344 Writes a buffer to variable storage space.\r
345\r
346 This function writes a buffer to variable storage space into firmware\r
347 volume block device. The destination is specified by parameter\r
348 VariableBase. Fault Tolerant Write protocol is used for writing.\r
349\r
350 @param[in] VariableBase The base address of the variable to write.\r
351 @param[in] Buffer Points to the data buffer.\r
352 @param[in] BufferSize The number of bytes of the data Buffer.\r
353\r
354 @retval EFI_SUCCESS The function completed successfully.\r
355 @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.\r
356 @retval Other The function could not complete successfully.\r
357\r
358**/\r
359EFI_STATUS\r
360FtwVariableSpace (\r
361 IN EFI_PHYSICAL_ADDRESS VariableBase,\r
362 IN UINT8 *Buffer,\r
363 IN UINTN BufferSize\r
364 );\r
365\r
366/**\r
367 Finds variable in volatile and non-volatile storage areas.\r
368\r
369 This code finds variable in volatile and non-volatile storage areas.\r
370 If VariableName is an empty string, then we just return the first\r
371 qualified variable without comparing VariableName and VendorGuid.\r
372 Otherwise, VariableName and VendorGuid are compared.\r
373\r
374 @param[in] VariableName Name of the variable to be found.\r
375 @param[in] VendorGuid Vendor GUID to be found.\r
376 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,\r
377 including the range searched and the target position.\r
378 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including\r
379 base of volatile variable storage area, base of\r
380 NV variable storage area, and a lock.\r
381 @param[in] Instance Instance of FV Block services.\r
382\r
383 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while\r
384 VendorGuid is NULL.\r
385 @retval EFI_SUCCESS Variable successfully found.\r
386 @retval EFI_INVALID_PARAMETER Variable not found.\r
387\r
388**/\r
389EFI_STATUS\r
390FindVariable (\r
391 IN CHAR16 *VariableName,\r
392 IN EFI_GUID *VendorGuid,\r
393 OUT VARIABLE_POINTER_TRACK *PtrTrack,\r
394 IN VARIABLE_GLOBAL *Global,\r
395 IN UINTN Instance\r
396 );\r
397\r
398/**\r
399 Gets the pointer to variable data area.\r
400\r
401 This function gets the pointer to variable data area.\r
402 The variable is specified by its variable header.\r
403\r
404 @param[in] VariableAddress Start address of variable header.\r
405 @param[in] Volatile TRUE - Variable is volatile.\r
406 FALSE - Variable is non-volatile.\r
407 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.\r
408 @param[in] Instance Instance of FV Block services.\r
409 @param[out] VariableData Buffer to hold variable data for output.\r
410\r
411**/\r
412VOID\r
413GetVariableDataPtr (\r
414 IN EFI_PHYSICAL_ADDRESS VariableAddress,\r
415 IN BOOLEAN Volatile,\r
416 IN VARIABLE_GLOBAL *Global,\r
417 IN UINTN Instance,\r
418 OUT CHAR16 *VariableData\r
419 );\r
420\r
421/**\r
422 Gets the size of variable data area.\r
423\r
424 This function gets the size of variable data area.\r
425 The variable is specified by its variable header.\r
426 If variable header contains raw data, just return 0.\r
427\r
428 @param[in] Variable Pointer to the variable header.\r
429\r
430 @return Size of variable data area in bytes.\r
431\r
432**/\r
433UINTN\r
434DataSizeOfVariable (\r
435 IN VARIABLE_HEADER *Variable\r
436 );\r
437\r
438/**\r
439 Update the variable region with Variable information. These are the same \r
440 arguments as the EFI Variable services.\r
441\r
442 @param[in] VariableName Name of variable.\r
443 @param[in] VendorGuid Guid of variable.\r
444 @param[in] Data Variable data.\r
445 @param[in] DataSize Size of data. 0 means delete.\r
446 @param[in] Attributes Attributes of the variable.\r
447 @param[in] KeyIndex Index of associated public key.\r
448 @param[in] MonotonicCount Value of associated monotonic count. \r
449 @param[in] VirtualMode Current calling mode for this function.\r
450 @param[in] Global Context of this Extended SAL Variable Services Class call.\r
451 @param[in] Variable The variable information which is used to keep track of variable usage.\r
452\r
453 @retval EFI_SUCCESS The update operation is success.\r
454 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
455\r
456**/\r
457EFI_STATUS\r
458EFIAPI\r
459UpdateVariable (\r
460 IN CHAR16 *VariableName,\r
461 IN EFI_GUID *VendorGuid,\r
462 IN VOID *Data,\r
463 IN UINTN DataSize,\r
464 IN UINT32 Attributes OPTIONAL, \r
465 IN UINT32 KeyIndex OPTIONAL,\r
466 IN UINT64 MonotonicCount OPTIONAL,\r
467 IN BOOLEAN VirtualMode,\r
468 IN ESAL_VARIABLE_GLOBAL *Global,\r
469 IN VARIABLE_POINTER_TRACK *Variable\r
470 );\r
471\r
472/**\r
473 Checks variable header.\r
474\r
475 This function checks if variable header is valid or not.\r
476\r
477 @param[in] VariableAddress Start address of variable header.\r
478 @param[in] Volatile TRUE - Variable is volatile.\r
479 FALSE - Variable is non-volatile.\r
480 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.\r
481 @param[in] Instance Instance of FV Block services.\r
482 @param[out] VariableHeader Pointer to VARIABLE_HEADER for output.\r
483\r
484 @retval TRUE Variable header is valid.\r
485 @retval FALSE Variable header is not valid.\r
486\r
487**/\r
488BOOLEAN\r
489IsValidVariableHeader (\r
490 IN EFI_PHYSICAL_ADDRESS VariableAddress,\r
491 IN BOOLEAN Volatile,\r
492 IN VARIABLE_GLOBAL *Global,\r
493 IN UINTN Instance,\r
494 OUT VARIABLE_HEADER *VariableHeader OPTIONAL\r
495 );\r
496\r
4f8ef5ce
RN
497/**\r
498 Flush the HOB variable to NV variable storage.\r
499**/\r
500VOID\r
501FlushHob2Nv (\r
502 VOID\r
503 );\r
504\r
0c18794e 505#endif\r