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