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