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