]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/VariableAuthenticated/Pei/Variable.c
Add security package to repository.
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / Pei / Variable.c
1 /** @file
2 Implement ReadOnly Variable Services required by PEIM and install PEI
3 ReadOnly Varaiable2 PPI. These services operates the non-volatile
4 storage space.
5
6 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17
18 #include "Variable.h"
19
20 //
21 // Module globals
22 //
23 EFI_PEI_READ_ONLY_VARIABLE2_PPI mVariablePpi = {
24 PeiGetVariable,
25 PeiGetNextVariableName
26 };
27
28 EFI_PEI_PPI_DESCRIPTOR mPpiListVariable = {
29 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
30 &gEfiPeiReadOnlyVariable2PpiGuid,
31 &mVariablePpi
32 };
33
34
35 /**
36 Provide the functionality of the variable services.
37
38 @param FileHandle Handle of the file being invoked.
39 Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextFile().
40 @param PeiServices General purpose services available to every PEIM.
41
42 @retval EFI_SUCCESS If the interface could be successfully installed
43 @retval Others Returned from PeiServicesInstallPpi()
44
45 **/
46 EFI_STATUS
47 EFIAPI
48 PeimInitializeVariableServices (
49 IN EFI_PEI_FILE_HANDLE FileHandle,
50 IN CONST EFI_PEI_SERVICES **PeiServices
51 )
52 {
53 EFI_BOOT_MODE BootMode;
54 EFI_STATUS Status;
55
56 //
57 // Check if this is recovery boot path. If no, publish the variable access capability
58 // to other modules. If yes, the content of variable area is not reliable. Therefore,
59 // in this case we should not provide variable service to other pei modules.
60 //
61 Status = (*PeiServices)->GetBootMode (PeiServices, &BootMode);
62 ASSERT_EFI_ERROR (Status);
63
64 if (BootMode == BOOT_IN_RECOVERY_MODE) {
65 return EFI_UNSUPPORTED;
66 }
67
68 return PeiServicesInstallPpi (&mPpiListVariable);
69
70 }
71
72 /**
73
74 Gets the pointer to the first variable header in given variable store area.
75
76 @param VarStoreHeader Pointer to the Variable Store Header.
77
78 @return Pointer to the first variable header
79
80 **/
81 VARIABLE_HEADER *
82 GetStartPointer (
83 IN VARIABLE_STORE_HEADER *VarStoreHeader
84 )
85 {
86 //
87 // The end of variable store
88 //
89 return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
90 }
91
92
93 /**
94 This code gets the pointer to the last variable memory pointer byte.
95
96 @param VarStoreHeader Pointer to the Variable Store Header.
97
98 @return VARIABLE_HEADER* pointer to last unavailable Variable Header.
99
100 **/
101 VARIABLE_HEADER *
102 GetEndPointer (
103 IN VARIABLE_STORE_HEADER *VarStoreHeader
104 )
105 {
106 //
107 // The end of variable store
108 //
109 return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);
110 }
111
112
113 /**
114 This code checks if variable header is valid or not.
115
116 @param Variable Pointer to the Variable Header.
117
118 @retval TRUE Variable header is valid.
119 @retval FALSE Variable header is not valid.
120
121 **/
122 BOOLEAN
123 IsValidVariableHeader (
124 IN VARIABLE_HEADER *Variable
125 )
126 {
127 if (Variable == NULL || Variable->StartId != VARIABLE_DATA ) {
128 return FALSE;
129 }
130
131 return TRUE;
132 }
133
134
135 /**
136 This code gets the size of name of variable.
137
138 @param Variable Pointer to the Variable Header.
139
140 @return Size of variable in bytes in type UINTN.
141
142 **/
143 UINTN
144 NameSizeOfVariable (
145 IN VARIABLE_HEADER *Variable
146 )
147 {
148 if (Variable->State == (UINT8) (-1) ||
149 Variable->DataSize == (UINT32) (-1) ||
150 Variable->NameSize == (UINT32) (-1) ||
151 Variable->Attributes == (UINT32) (-1)) {
152 return 0;
153 }
154 return (UINTN) Variable->NameSize;
155 }
156
157
158 /**
159 This code gets the size of data of variable.
160
161 @param Variable Pointer to the Variable Header.
162
163 @return Size of variable in bytes in type UINTN.
164
165 **/
166 UINTN
167 DataSizeOfVariable (
168 IN VARIABLE_HEADER *Variable
169 )
170 {
171 if (Variable->State == (UINT8) (-1) ||
172 Variable->DataSize == (UINT32) (-1) ||
173 Variable->NameSize == (UINT32) (-1) ||
174 Variable->Attributes == (UINT32) (-1)) {
175 return 0;
176 }
177 return (UINTN) Variable->DataSize;
178 }
179
180 /**
181 This code gets the pointer to the variable name.
182
183 @param Variable Pointer to the Variable Header.
184
185 @return A CHAR16* pointer to Variable Name.
186
187 **/
188 CHAR16 *
189 GetVariableNamePtr (
190 IN VARIABLE_HEADER *Variable
191 )
192 {
193
194 return (CHAR16 *) (Variable + 1);
195 }
196
197
198 /**
199 This code gets the pointer to the variable data.
200
201 @param Variable Pointer to the Variable Header.
202
203 @return A UINT8* pointer to Variable Data.
204
205 **/
206 UINT8 *
207 GetVariableDataPtr (
208 IN VARIABLE_HEADER *Variable
209 )
210 {
211 UINTN Value;
212
213 //
214 // Be careful about pad size for alignment
215 //
216 Value = (UINTN) GetVariableNamePtr (Variable);
217 Value += NameSizeOfVariable (Variable);
218 Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
219
220 return (UINT8 *) Value;
221 }
222
223
224 /**
225 This code gets the pointer to the next variable header.
226
227 @param Variable Pointer to the Variable Header.
228
229 @return A VARIABLE_HEADER* pointer to next variable header.
230
231 **/
232 VARIABLE_HEADER *
233 GetNextVariablePtr (
234 IN VARIABLE_HEADER *Variable
235 )
236 {
237 UINTN Value;
238
239 if (!IsValidVariableHeader (Variable)) {
240 return NULL;
241 }
242
243 Value = (UINTN) GetVariableDataPtr (Variable);
244 Value += DataSizeOfVariable (Variable);
245 Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
246
247 //
248 // Be careful about pad size for alignment
249 //
250 return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
251 }
252
253 /**
254 This code gets the pointer to the variable name.
255
256 @param VarStoreHeader Pointer to the Variable Store Header.
257
258 @retval EfiRaw Variable store is raw
259 @retval EfiValid Variable store is valid
260 @retval EfiInvalid Variable store is invalid
261
262 **/
263 VARIABLE_STORE_STATUS
264 GetVariableStoreStatus (
265 IN VARIABLE_STORE_HEADER *VarStoreHeader
266 )
267 {
268
269 if (CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) &&
270 VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
271 VarStoreHeader->State == VARIABLE_STORE_HEALTHY
272 ) {
273
274 return EfiValid;
275 }
276
277 if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
278 ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
279 ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
280 ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
281 VarStoreHeader->Size == 0xffffffff &&
282 VarStoreHeader->Format == 0xff &&
283 VarStoreHeader->State == 0xff
284 ) {
285
286 return EfiRaw;
287 } else {
288 return EfiInvalid;
289 }
290 }
291
292
293 /**
294 This function compares a variable with variable entries in database.
295
296 @param Variable Pointer to the variable in our database
297 @param VariableName Name of the variable to compare to 'Variable'
298 @param VendorGuid GUID of the variable to compare to 'Variable'
299 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
300
301 @retval EFI_SUCCESS Found match variable
302 @retval EFI_NOT_FOUND Variable not found
303
304 **/
305 EFI_STATUS
306 CompareWithValidVariable (
307 IN VARIABLE_HEADER *Variable,
308 IN CONST CHAR16 *VariableName,
309 IN CONST EFI_GUID *VendorGuid,
310 OUT VARIABLE_POINTER_TRACK *PtrTrack
311 )
312 {
313 VOID *Point;
314
315 if (VariableName[0] == 0) {
316 PtrTrack->CurrPtr = Variable;
317 return EFI_SUCCESS;
318 } else {
319 //
320 // Don't use CompareGuid function here for performance reasons.
321 // Instead we compare the GUID a UINT32 at a time and branch
322 // on the first failed comparison.
323 //
324 if ((((INT32 *) VendorGuid)[0] == ((INT32 *) &Variable->VendorGuid)[0]) &&
325 (((INT32 *) VendorGuid)[1] == ((INT32 *) &Variable->VendorGuid)[1]) &&
326 (((INT32 *) VendorGuid)[2] == ((INT32 *) &Variable->VendorGuid)[2]) &&
327 (((INT32 *) VendorGuid)[3] == ((INT32 *) &Variable->VendorGuid)[3])
328 ) {
329 ASSERT (NameSizeOfVariable (Variable) != 0);
330 Point = (VOID *) GetVariableNamePtr (Variable);
331 if (CompareMem (VariableName, Point, NameSizeOfVariable (Variable)) == 0) {
332 PtrTrack->CurrPtr = Variable;
333 return EFI_SUCCESS;
334 }
335 }
336 }
337
338 return EFI_NOT_FOUND;
339 }
340
341
342 /**
343 This code finds variable in storage blocks (Non-Volatile).
344
345 @param PeiServices General purpose services available to every PEIM.
346 @param VariableName Name of the variable to be found
347 @param VendorGuid Vendor GUID to be found.
348 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
349
350 @retval EFI_SUCCESS Variable found successfully
351 @retval EFI_NOT_FOUND Variable not found
352 @retval EFI_INVALID_PARAMETER Invalid variable name
353
354 **/
355 EFI_STATUS
356 FindVariable (
357 IN CONST EFI_PEI_SERVICES **PeiServices,
358 IN CONST CHAR16 *VariableName,
359 IN CONST EFI_GUID *VendorGuid,
360 OUT VARIABLE_POINTER_TRACK *PtrTrack
361 )
362 {
363 EFI_HOB_GUID_TYPE *GuidHob;
364 VARIABLE_STORE_HEADER *VariableStoreHeader;
365 VARIABLE_HEADER *Variable;
366 VARIABLE_HEADER *LastVariable;
367 VARIABLE_HEADER *MaxIndex;
368 VARIABLE_INDEX_TABLE *IndexTable;
369 UINT32 Count;
370 UINT32 Offset;
371 UINT8 *VariableBase;
372 BOOLEAN StopRecord;
373
374 if (VariableName[0] != 0 && VendorGuid == NULL) {
375 return EFI_INVALID_PARAMETER;
376 }
377 //
378 // No Variable Address equals zero, so 0 as initial value is safe.
379 //
380 MaxIndex = 0;
381 StopRecord = FALSE;
382
383 GuidHob = GetFirstGuidHob (&gEfiVariableIndexTableGuid);
384 if (GuidHob == NULL) {
385 //
386 // If it's the first time to access variable region in flash, create a guid hob to record
387 // VAR_ADDED type variable info.
388 // Note that as the resource of PEI phase is limited, only store the number of
389 // VARIABLE_INDEX_TABLE_VOLUME of VAR_ADDED type variables to reduce access time.
390 //
391 IndexTable = BuildGuidHob (&gEfiVariableIndexTableGuid, sizeof (VARIABLE_INDEX_TABLE));
392 IndexTable->Length = 0;
393 IndexTable->StartPtr = NULL;
394 IndexTable->EndPtr = NULL;
395 IndexTable->GoneThrough = 0;
396 } else {
397 IndexTable = GET_GUID_HOB_DATA (GuidHob);
398 for (Offset = 0, Count = 0; Count < IndexTable->Length; Count++) {
399 //
400 // traverse the variable info list to look for varible.
401 // The IndexTable->Index[Count] records the distance of two neighbouring VAR_ADDED type variables.
402 //
403 ASSERT (Count < VARIABLE_INDEX_TABLE_VOLUME);
404 Offset += IndexTable->Index[Count];
405 MaxIndex = (VARIABLE_HEADER *)((CHAR8 *)(IndexTable->StartPtr) + Offset);
406 if (CompareWithValidVariable (MaxIndex, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {
407 PtrTrack->StartPtr = IndexTable->StartPtr;
408 PtrTrack->EndPtr = IndexTable->EndPtr;
409
410 return EFI_SUCCESS;
411 }
412 }
413
414 if (IndexTable->GoneThrough != 0) {
415 return EFI_NOT_FOUND;
416 }
417 }
418 //
419 // If not found in HOB, then let's start from the MaxIndex we've found.
420 //
421 if (MaxIndex != NULL) {
422 Variable = GetNextVariablePtr (MaxIndex);
423 LastVariable = MaxIndex;
424 } else {
425 if ((IndexTable->StartPtr != NULL) || (IndexTable->EndPtr != NULL)) {
426 Variable = IndexTable->StartPtr;
427 } else {
428 VariableBase = (UINT8 *) (UINTN) PcdGet64 (PcdFlashNvStorageVariableBase64);
429 if (VariableBase == NULL) {
430 VariableBase = (UINT8 *) (UINTN) PcdGet32 (PcdFlashNvStorageVariableBase);
431 }
432
433 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (VariableBase + \
434 ((EFI_FIRMWARE_VOLUME_HEADER *) (VariableBase)) -> HeaderLength);
435
436 if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {
437 return EFI_UNSUPPORTED;
438 }
439
440 if (~VariableStoreHeader->Size == 0) {
441 return EFI_NOT_FOUND;
442 }
443 //
444 // Find the variable by walk through non-volatile variable store
445 //
446 IndexTable->StartPtr = GetStartPointer (VariableStoreHeader);
447 IndexTable->EndPtr = GetEndPointer (VariableStoreHeader);
448
449 //
450 // Start Pointers for the variable.
451 // Actual Data Pointer where data can be written.
452 //
453 Variable = IndexTable->StartPtr;
454 }
455
456 LastVariable = IndexTable->StartPtr;
457 }
458 //
459 // Find the variable by walk through non-volatile variable store
460 //
461 PtrTrack->StartPtr = IndexTable->StartPtr;
462 PtrTrack->EndPtr = IndexTable->EndPtr;
463
464 while ((Variable < IndexTable->EndPtr) && IsValidVariableHeader (Variable)) {
465 if (Variable->State == VAR_ADDED) {
466 //
467 // Record Variable in VariableIndex HOB
468 //
469 if (IndexTable->Length < VARIABLE_INDEX_TABLE_VOLUME && !StopRecord) {
470 Offset = (UINT32)((UINTN)Variable - (UINTN)LastVariable);
471 //
472 // The distance of two neighbouring VAR_ADDED variable is larger than 2^16,
473 // which is beyond the allowable scope(UINT16) of record. In such case, need not to
474 // record the subsequent VAR_ADDED type variables again.
475 //
476 if ((Offset & 0xFFFF0000UL) != 0) {
477 StopRecord = TRUE;
478 }
479
480 if (!StopRecord) {
481 IndexTable->Index[IndexTable->Length++] = (UINT16) Offset;
482 }
483 LastVariable = Variable;
484 }
485
486 if (CompareWithValidVariable (Variable, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {
487 return EFI_SUCCESS;
488 }
489 }
490
491 Variable = GetNextVariablePtr (Variable);
492 }
493 //
494 // If gone through the VariableStore, that means we never find in Firmware any more.
495 //
496 if ((IndexTable->Length < VARIABLE_INDEX_TABLE_VOLUME) && (!StopRecord)) {
497 IndexTable->GoneThrough = 1;
498 }
499
500 PtrTrack->CurrPtr = NULL;
501
502 return EFI_NOT_FOUND;
503 }
504
505 /**
506 This service retrieves a variable's value using its name and GUID.
507
508 Read the specified variable from the UEFI variable store. If the Data
509 buffer is too small to hold the contents of the variable, the error
510 EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
511 size to obtain the data.
512
513 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
514 @param VariableName A pointer to a null-terminated string that is the variable's name.
515 @param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
516 VariableGuid and VariableName must be unique.
517 @param Attributes If non-NULL, on return, points to the variable's attributes.
518 @param DataSize On entry, points to the size in bytes of the Data buffer.
519 On return, points to the size of the data returned in Data.
520 @param Data Points to the buffer which will hold the returned variable value.
521
522 @retval EFI_SUCCESS The variable was read successfully.
523 @retval EFI_NOT_FOUND The variable could not be found.
524 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
525 DataSize is updated with the size required for
526 the specified variable.
527 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
528 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
529
530 **/
531 EFI_STATUS
532 EFIAPI
533 PeiGetVariable (
534 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
535 IN CONST CHAR16 *VariableName,
536 IN CONST EFI_GUID *VariableGuid,
537 OUT UINT32 *Attributes,
538 IN OUT UINTN *DataSize,
539 OUT VOID *Data
540 )
541 {
542 VARIABLE_POINTER_TRACK Variable;
543 UINTN VarDataSize;
544 EFI_STATUS Status;
545 CONST EFI_PEI_SERVICES **PeiServices;
546
547 PeiServices = GetPeiServicesTablePointer ();
548 if (VariableName == NULL || VariableGuid == NULL || DataSize == NULL) {
549 return EFI_INVALID_PARAMETER;
550 }
551 //
552 // Find existing variable
553 //
554 Status = FindVariable (PeiServices, VariableName, VariableGuid, &Variable);
555 if (Variable.CurrPtr == NULL || Status != EFI_SUCCESS) {
556 return Status;
557 }
558 //
559 // Get data size
560 //
561 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);
562 if (*DataSize >= VarDataSize) {
563 if (Data == NULL) {
564 return EFI_INVALID_PARAMETER;
565 }
566
567 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);
568
569 if (Attributes != NULL) {
570 *Attributes = Variable.CurrPtr->Attributes;
571 }
572
573 *DataSize = VarDataSize;
574 return EFI_SUCCESS;
575 } else {
576 *DataSize = VarDataSize;
577 return EFI_BUFFER_TOO_SMALL;
578 }
579 }
580
581 /**
582 Return the next variable name and GUID.
583
584 This function is called multiple times to retrieve the VariableName
585 and VariableGuid of all variables currently available in the system.
586 On each call, the previous results are passed into the interface,
587 and, on return, the interface returns the data for the next
588 interface. When the entire variable list has been returned,
589 EFI_NOT_FOUND is returned.
590
591 @param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
592
593 @param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
594 On return, the size of the variable name buffer.
595 @param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
596 On return, points to the next variable's null-terminated name string.
597 @param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.
598 On return, a pointer to the next variable's GUID.
599
600 @retval EFI_SUCCESS The variable was read successfully.
601 @retval EFI_NOT_FOUND The variable could not be found.
602 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
603 data. VariableNameSize is updated with the size
604 required for the specified variable.
605 @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
606 VariableNameSize is NULL.
607 @retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
608
609 **/
610 EFI_STATUS
611 EFIAPI
612 PeiGetNextVariableName (
613 IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
614 IN OUT UINTN *VariableNameSize,
615 IN OUT CHAR16 *VariableName,
616 IN OUT EFI_GUID *VariableGuid
617 )
618 {
619 VARIABLE_POINTER_TRACK Variable;
620 UINTN VarNameSize;
621 EFI_STATUS Status;
622 CONST EFI_PEI_SERVICES **PeiServices;
623
624 PeiServices = GetPeiServicesTablePointer ();
625 if (VariableName == NULL || VariableGuid == NULL || VariableNameSize == NULL) {
626 return EFI_INVALID_PARAMETER;
627 }
628
629 Status = FindVariable (PeiServices, VariableName, VariableGuid, &Variable);
630 if (Variable.CurrPtr == NULL || Status != EFI_SUCCESS) {
631 return Status;
632 }
633
634 if (VariableName[0] != 0) {
635 //
636 // If variable name is not NULL, get next variable
637 //
638 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
639 }
640
641 while (!(Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL)) {
642 if (IsValidVariableHeader (Variable.CurrPtr)) {
643 if (Variable.CurrPtr->State == VAR_ADDED) {
644 ASSERT (NameSizeOfVariable (Variable.CurrPtr) != 0);
645
646 VarNameSize = (UINTN) NameSizeOfVariable (Variable.CurrPtr);
647 if (VarNameSize <= *VariableNameSize) {
648 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);
649
650 CopyMem (VariableGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));
651
652 Status = EFI_SUCCESS;
653 } else {
654 Status = EFI_BUFFER_TOO_SMALL;
655 }
656
657 *VariableNameSize = VarNameSize;
658 return Status;
659 //
660 // Variable is found
661 //
662 } else {
663 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
664 }
665 } else {
666 break;
667 }
668 }
669
670 return EFI_NOT_FOUND;
671 }