2 Sample platform variable cleanup library implementation.
4 Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "PlatVarCleanup.h"
17 VAR_ERROR_FLAG mLastVarErrorFlag
= VAR_ERROR_FLAG_NO_ERROR
;
18 EDKII_VAR_CHECK_PROTOCOL
*mVarCheck
= NULL
;
21 /// The flag to indicate whether the platform has left the DXE phase of execution.
23 BOOLEAN mEndOfDxe
= FALSE
;
25 LIST_ENTRY mUserVariableList
= INITIALIZE_LIST_HEAD_VARIABLE (mUserVariableList
);
26 UINT16 mUserVariableCount
= 0;
27 UINT16 mMarkedUserVariableCount
= 0;
29 EFI_GUID mVariableCleanupHiiGuid
= VARIABLE_CLEANUP_HII_GUID
;
30 CHAR16 mVarStoreName
[] = L
"VariableCleanup";
32 HII_VENDOR_DEVICE_PATH mVarCleanupHiiVendorDevicePath
= {
38 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
)),
39 (UINT8
) ((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
42 VARIABLE_CLEANUP_HII_GUID
46 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
48 (UINT8
) (sizeof (EFI_DEVICE_PATH_PROTOCOL
)),
49 (UINT8
) ((sizeof (EFI_DEVICE_PATH_PROTOCOL
)) >> 8)
55 Internal get variable error flag.
57 @return Variable error flag.
61 InternalGetVarErrorFlag (
67 VAR_ERROR_FLAG ErrorFlag
;
69 Size
= sizeof (ErrorFlag
);
70 Status
= gRT
->GetVariable (
72 &gEdkiiVarErrorFlagGuid
,
77 if (EFI_ERROR (Status
)) {
78 DEBUG ((EFI_D_INFO
, "%s - not found\n", VAR_ERROR_FLAG_NAME
));
79 return VAR_ERROR_FLAG_NO_ERROR
;
87 @param[in] Name Pointer to variable name.
88 @param[in] Guid Pointer to vendor guid.
90 @retval TRUE User variable.
91 @retval FALSE System variable.
101 VAR_CHECK_VARIABLE_PROPERTY Property
;
103 if (mVarCheck
== NULL
) {
104 gBS
->LocateProtocol (
105 &gEdkiiVarCheckProtocolGuid
,
110 ASSERT (mVarCheck
!= NULL
);
112 ZeroMem (&Property
, sizeof (Property
));
113 Status
= mVarCheck
->VariablePropertyGet (
118 if (EFI_ERROR (Status
)) {
120 // No property, it is user variable.
122 DEBUG ((EFI_D_INFO
, "PlatformVarCleanup - User variable: %g:%s\n", Guid
, Name
));
126 // DEBUG ((EFI_D_INFO, "PlatformVarCleanup - Variable Property: %g:%s\n", Guid, Name));
127 // DEBUG ((EFI_D_INFO, " Revision - 0x%04x\n", Property.Revision));
128 // DEBUG ((EFI_D_INFO, " Property - 0x%04x\n", Property.Property));
129 // DEBUG ((EFI_D_INFO, " Attribute - 0x%08x\n", Property.Attributes));
130 // DEBUG ((EFI_D_INFO, " MinSize - 0x%x\n", Property.MinSize));
131 // DEBUG ((EFI_D_INFO, " MaxSize - 0x%x\n", Property.MaxSize));
137 Find user variable node by variable GUID.
139 @param[in] Guid Pointer to vendor guid.
141 @return Pointer to user variable node.
145 FindUserVariableNodeByGuid (
149 USER_VARIABLE_NODE
*UserVariableNode
;
152 for (Link
= mUserVariableList
.ForwardLink
153 ;Link
!= &mUserVariableList
154 ;Link
= Link
->ForwardLink
) {
155 UserVariableNode
= USER_VARIABLE_FROM_LINK (Link
);
157 if (CompareGuid (Guid
, &UserVariableNode
->Guid
)) {
161 return UserVariableNode
;
166 // Create new one if not found.
168 UserVariableNode
= AllocateZeroPool (sizeof (*UserVariableNode
));
169 ASSERT (UserVariableNode
!= NULL
);
170 UserVariableNode
->Signature
= USER_VARIABLE_NODE_SIGNATURE
;
171 CopyGuid (&UserVariableNode
->Guid
, Guid
);
173 // (36 chars of "########-####-####-####-############" + 1 space + 1 terminator) * sizeof (CHAR16).
175 UserVariableNode
->PromptString
= AllocatePool ((36 + 2) * sizeof (CHAR16
));
176 ASSERT (UserVariableNode
->PromptString
!= NULL
);
177 UnicodeSPrint (UserVariableNode
->PromptString
, (36 + 2) * sizeof (CHAR16
), L
" %g", &UserVariableNode
->Guid
);
178 InitializeListHead (&UserVariableNode
->NameLink
);
179 InsertTailList (&mUserVariableList
, &UserVariableNode
->Link
);
180 return UserVariableNode
;
184 Create user variable node.
188 CreateUserVariableNode (
193 EFI_STATUS GetVariableStatus
;
195 UINTN MaxVarNameSize
;
202 USER_VARIABLE_NODE
*UserVariableNode
;
203 USER_VARIABLE_NAME_NODE
*UserVariableNameNode
;
208 // Initialize 128 * sizeof (CHAR16) variable name size.
210 MaxVarNameSize
= 128 * sizeof (CHAR16
);
211 VarName
= AllocateZeroPool (MaxVarNameSize
);
212 ASSERT (VarName
!= NULL
);
215 // Initialize 0x1000 variable data size.
217 MaxDataSize
= 0x1000;
218 Data
= AllocateZeroPool (MaxDataSize
);
219 ASSERT (Data
!= NULL
);
223 VarNameSize
= MaxVarNameSize
;
224 Status
= gRT
->GetNextVariableName (&VarNameSize
, VarName
, &Guid
);
225 if (Status
== EFI_BUFFER_TOO_SMALL
) {
226 VarName
= ReallocatePool (MaxVarNameSize
, VarNameSize
, VarName
);
227 ASSERT (VarName
!= NULL
);
228 MaxVarNameSize
= VarNameSize
;
229 Status
= gRT
->GetNextVariableName (&VarNameSize
, VarName
, &Guid
);
232 if (!EFI_ERROR (Status
)) {
233 if (IsUserVariable (VarName
, &Guid
)) {
234 DataSize
= MaxDataSize
;
235 GetVariableStatus
= gRT
->GetVariable (VarName
, &Guid
, &Attributes
, &DataSize
, Data
);
236 if (GetVariableStatus
== EFI_BUFFER_TOO_SMALL
) {
237 Data
= ReallocatePool (MaxDataSize
, DataSize
, Data
);
238 ASSERT (Data
!= NULL
);
239 MaxDataSize
= DataSize
;
240 GetVariableStatus
= gRT
->GetVariable (VarName
, &Guid
, &Attributes
, &DataSize
, Data
);
242 ASSERT_EFI_ERROR (GetVariableStatus
);
244 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
245 UserVariableNode
= FindUserVariableNodeByGuid (&Guid
);
246 ASSERT (UserVariableNode
!= NULL
);
249 // Different variables that have same variable GUID share same user variable node.
251 UserVariableNameNode
= AllocateZeroPool (sizeof (*UserVariableNameNode
));
252 ASSERT (UserVariableNameNode
!= NULL
);
253 UserVariableNameNode
->Signature
= USER_VARIABLE_NAME_NODE_SIGNATURE
;
254 UserVariableNameNode
->Name
= AllocateCopyPool (VarNameSize
, VarName
);
255 UserVariableNameNode
->Attributes
= Attributes
;
256 UserVariableNameNode
->DataSize
= DataSize
;
257 UserVariableNameNode
->Index
= Index
;
258 UserVariableNameNode
->QuestionId
= (EFI_QUESTION_ID
) (USER_VARIABLE_QUESTION_ID
+ Index
);
260 // 2 space * sizeof (CHAR16) + StrSize.
262 StringSize
= 2 * sizeof (CHAR16
) + StrSize (UserVariableNameNode
->Name
);
263 UserVariableNameNode
->PromptString
= AllocatePool (StringSize
);
264 ASSERT (UserVariableNameNode
->PromptString
!= NULL
);
265 UnicodeSPrint (UserVariableNameNode
->PromptString
, StringSize
, L
" %s", UserVariableNameNode
->Name
);
267 // (33 chars of "Attribtues = 0x and DataSize = 0x" + 1 terminator + (sizeof (UINT32) + sizeof (UINTN)) * 2) * sizeof (CHAR16).
269 StringSize
= (33 + 1 + (sizeof (UINT32
) + sizeof (UINTN
)) * 2) * sizeof (CHAR16
);
270 UserVariableNameNode
->HelpString
= AllocatePool (StringSize
);
271 ASSERT (UserVariableNameNode
->HelpString
!= NULL
);
272 UnicodeSPrint (UserVariableNameNode
->HelpString
, StringSize
, L
"Attribtues = 0x%08x and DataSize = 0x%x", UserVariableNameNode
->Attributes
, UserVariableNameNode
->DataSize
);
273 UserVariableNameNode
->Deleted
= FALSE
;
274 InsertTailList (&UserVariableNode
->NameLink
, &UserVariableNameNode
->Link
);
279 } while (Status
!= EFI_NOT_FOUND
);
281 mUserVariableCount
= Index
;
282 ASSERT (mUserVariableCount
<= MAX_USER_VARIABLE_COUNT
);
283 DEBUG ((EFI_D_INFO
, "PlatformVarCleanup - User variable count: 0x%04x\n", mUserVariableCount
));
290 Destroy user variable nodes.
294 DestroyUserVariableNode (
298 USER_VARIABLE_NODE
*UserVariableNode
;
300 USER_VARIABLE_NAME_NODE
*UserVariableNameNode
;
301 LIST_ENTRY
*NameLink
;
303 while (mUserVariableList
.ForwardLink
!= &mUserVariableList
) {
304 Link
= mUserVariableList
.ForwardLink
;
305 UserVariableNode
= USER_VARIABLE_FROM_LINK (Link
);
307 RemoveEntryList (&UserVariableNode
->Link
);
309 while (UserVariableNode
->NameLink
.ForwardLink
!= &UserVariableNode
->NameLink
) {
310 NameLink
= UserVariableNode
->NameLink
.ForwardLink
;
311 UserVariableNameNode
= USER_VARIABLE_NAME_FROM_LINK (NameLink
);
313 RemoveEntryList (&UserVariableNameNode
->Link
);
315 FreePool (UserVariableNameNode
->Name
);
316 FreePool (UserVariableNameNode
->PromptString
);
317 FreePool (UserVariableNameNode
->HelpString
);
318 FreePool (UserVariableNameNode
);
321 FreePool (UserVariableNode
->PromptString
);
322 FreePool (UserVariableNode
);
327 Create a time based data payload by concatenating the EFI_VARIABLE_AUTHENTICATION_2
328 descriptor with the input data. NO authentication is required in this function.
330 @param[in, out] DataSize On input, the size of Data buffer in bytes.
331 On output, the size of data returned in Data
333 @param[in, out] Data On input, Pointer to data buffer to be wrapped or
334 pointer to NULL to wrap an empty payload.
335 On output, Pointer to the new payload date buffer allocated from pool,
336 it's caller's responsibility to free the memory after using it.
338 @retval EFI_SUCCESS Create time based payload successfully.
339 @retval EFI_OUT_OF_RESOURCES There are not enough memory resourses to create time based payload.
340 @retval EFI_INVALID_PARAMETER The parameter is invalid.
341 @retval Others Unexpected error happens.
345 CreateTimeBasedPayload (
346 IN OUT UINTN
*DataSize
,
354 EFI_VARIABLE_AUTHENTICATION_2
*DescriptorData
;
355 UINTN DescriptorSize
;
358 if (Data
== NULL
|| DataSize
== NULL
) {
359 return EFI_INVALID_PARAMETER
;
363 // At user physical presence, the variable does not need to be signed but the
364 // parameters to the SetVariable() call still need to be prepared as authenticated
365 // variable. So we create EFI_VARIABLE_AUTHENTICATED_2 descriptor without certificate
369 PayloadSize
= *DataSize
;
371 DescriptorSize
= OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
) + OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
);
372 NewData
= (UINT8
*) AllocateZeroPool (DescriptorSize
+ PayloadSize
);
373 if (NewData
== NULL
) {
374 return EFI_OUT_OF_RESOURCES
;
377 if ((Payload
!= NULL
) && (PayloadSize
!= 0)) {
378 CopyMem (NewData
+ DescriptorSize
, Payload
, PayloadSize
);
381 DescriptorData
= (EFI_VARIABLE_AUTHENTICATION_2
*) (NewData
);
383 ZeroMem (&Time
, sizeof (EFI_TIME
));
384 Status
= gRT
->GetTime (&Time
, NULL
);
385 if (EFI_ERROR (Status
)) {
394 CopyMem (&DescriptorData
->TimeStamp
, &Time
, sizeof (EFI_TIME
));
396 DescriptorData
->AuthInfo
.Hdr
.dwLength
= OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
);
397 DescriptorData
->AuthInfo
.Hdr
.wRevision
= 0x0200;
398 DescriptorData
->AuthInfo
.Hdr
.wCertificateType
= WIN_CERT_TYPE_EFI_GUID
;
399 CopyGuid (&DescriptorData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
);
401 if (Payload
!= NULL
) {
405 *DataSize
= DescriptorSize
+ PayloadSize
;
411 Create a counter based data payload by concatenating the EFI_VARIABLE_AUTHENTICATION
412 descriptor with the input data. NO authentication is required in this function.
414 @param[in, out] DataSize On input, the size of Data buffer in bytes.
415 On output, the size of data returned in Data
417 @param[in, out] Data On input, Pointer to data buffer to be wrapped or
418 pointer to NULL to wrap an empty payload.
419 On output, Pointer to the new payload date buffer allocated from pool,
420 it's caller's responsibility to free the memory after using it.
422 @retval EFI_SUCCESS Create counter based payload successfully.
423 @retval EFI_OUT_OF_RESOURCES There are not enough memory resourses to create time based payload.
424 @retval EFI_INVALID_PARAMETER The parameter is invalid.
425 @retval Others Unexpected error happens.
429 CreateCounterBasedPayload (
430 IN OUT UINTN
*DataSize
,
438 EFI_VARIABLE_AUTHENTICATION
*DescriptorData
;
439 UINTN DescriptorSize
;
440 UINT64 MonotonicCount
;
442 if (Data
== NULL
|| DataSize
== NULL
) {
443 return EFI_INVALID_PARAMETER
;
447 // At user physical presence, the variable does not need to be signed but the
448 // parameters to the SetVariable() call still need to be prepared as authenticated
449 // variable. So we create EFI_VARIABLE_AUTHENTICATED descriptor without certificate
453 PayloadSize
= *DataSize
;
455 DescriptorSize
= (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION
, AuthInfo
)) + \
456 (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)) + \
457 sizeof (EFI_CERT_BLOCK_RSA_2048_SHA256
);
458 NewData
= (UINT8
*) AllocateZeroPool (DescriptorSize
+ PayloadSize
);
459 if (NewData
== NULL
) {
460 return EFI_OUT_OF_RESOURCES
;
463 if ((Payload
!= NULL
) && (PayloadSize
!= 0)) {
464 CopyMem (NewData
+ DescriptorSize
, Payload
, PayloadSize
);
467 DescriptorData
= (EFI_VARIABLE_AUTHENTICATION
*) (NewData
);
469 Status
= gBS
->GetNextMonotonicCount (&MonotonicCount
);
470 if (EFI_ERROR (Status
)) {
474 DescriptorData
->MonotonicCount
= MonotonicCount
;
476 DescriptorData
->AuthInfo
.Hdr
.dwLength
= OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
) + sizeof (EFI_CERT_BLOCK_RSA_2048_SHA256
);
477 DescriptorData
->AuthInfo
.Hdr
.wRevision
= 0x0200;
478 DescriptorData
->AuthInfo
.Hdr
.wCertificateType
= WIN_CERT_TYPE_EFI_GUID
;
479 CopyGuid (&DescriptorData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
);
481 if (Payload
!= NULL
) {
485 *DataSize
= DescriptorSize
+ PayloadSize
;
491 Delete user variable.
493 @param[in] DeleteAll Delete all user variables.
494 @param[in] VariableCleanupData Pointer to variable cleanup data.
499 IN BOOLEAN DeleteAll
,
500 IN VARIABLE_CLEANUP_DATA
*VariableCleanupData OPTIONAL
504 USER_VARIABLE_NODE
*UserVariableNode
;
506 USER_VARIABLE_NAME_NODE
*UserVariableNameNode
;
507 LIST_ENTRY
*NameLink
;
511 for (Link
= mUserVariableList
.ForwardLink
512 ;Link
!= &mUserVariableList
513 ;Link
= Link
->ForwardLink
) {
514 UserVariableNode
= USER_VARIABLE_FROM_LINK (Link
);
516 for (NameLink
= UserVariableNode
->NameLink
.ForwardLink
517 ;NameLink
!= &UserVariableNode
->NameLink
518 ;NameLink
= NameLink
->ForwardLink
) {
519 UserVariableNameNode
= USER_VARIABLE_NAME_FROM_LINK (NameLink
);
521 if (!UserVariableNameNode
->Deleted
&& (DeleteAll
|| ((VariableCleanupData
!= NULL
) && (VariableCleanupData
->UserVariable
[UserVariableNameNode
->Index
] == TRUE
)))) {
522 DEBUG ((EFI_D_INFO
, "PlatformVarCleanup - Delete variable: %g:%s\n", &UserVariableNode
->Guid
, UserVariableNameNode
->Name
));
523 if ((UserVariableNameNode
->Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
526 Status
= CreateTimeBasedPayload (&DataSize
, &Data
);
527 if (!EFI_ERROR (Status
)) {
528 Status
= gRT
->SetVariable (UserVariableNameNode
->Name
, &UserVariableNode
->Guid
, UserVariableNameNode
->Attributes
, DataSize
, Data
);
531 } else if ((UserVariableNameNode
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
534 Status
= CreateCounterBasedPayload (&DataSize
, &Data
);
535 if (!EFI_ERROR (Status
)) {
536 Status
= gRT
->SetVariable (UserVariableNameNode
->Name
, &UserVariableNode
->Guid
, UserVariableNameNode
->Attributes
, DataSize
, Data
);
540 Status
= gRT
->SetVariable (UserVariableNameNode
->Name
, &UserVariableNode
->Guid
, 0, 0, NULL
);
542 if (!EFI_ERROR (Status
)) {
543 UserVariableNameNode
->Deleted
= TRUE
;
545 DEBUG ((EFI_D_INFO
, "PlatformVarCleanup - Delete variable fail: %g:%s\n", &UserVariableNode
->Guid
, UserVariableNameNode
->Name
));
553 This function allows a caller to extract the current configuration for one
554 or more named elements from the target driver.
556 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
557 @param[in] Request A null-terminated Unicode string in <ConfigRequest> format.
558 @param[out] Progress On return, points to a character in the Request string.
559 Points to the string's null terminator if request was successful.
560 Points to the most recent '&' before the first failing name/value
561 pair (or the beginning of the string if the failure is in the
562 first name/value pair) if the request was not successful.
563 @param[out] Results A null-terminated Unicode string in <ConfigAltResp> format which
564 has all values filled in for the names in the Request string.
565 String to be allocated by the called function.
567 @retval EFI_SUCCESS The Results is filled with the requested values.
568 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.
569 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.
570 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this driver.
575 VariableCleanupHiiExtractConfig (
576 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
577 IN CONST EFI_STRING Request
,
578 OUT EFI_STRING
*Progress
,
579 OUT EFI_STRING
*Results
583 VARIABLE_CLEANUP_HII_PRIVATE_DATA
*Private
;
585 EFI_STRING ConfigRequestHdr
;
586 EFI_STRING ConfigRequest
;
587 BOOLEAN AllocatedRequest
;
590 if (Progress
== NULL
|| Results
== NULL
) {
591 return EFI_INVALID_PARAMETER
;
595 if ((Request
!= NULL
) && !HiiIsConfigHdrMatch (Request
, &mVariableCleanupHiiGuid
, mVarStoreName
)) {
596 return EFI_NOT_FOUND
;
599 ConfigRequestHdr
= NULL
;
600 ConfigRequest
= NULL
;
601 AllocatedRequest
= FALSE
;
604 Private
= VARIABLE_CLEANUP_HII_PRIVATE_FROM_THIS (This
);
606 // Convert buffer data to <ConfigResp> by helper function BlockToConfig().
608 BufferSize
= sizeof (VARIABLE_CLEANUP_DATA
);
609 ConfigRequest
= Request
;
610 if ((Request
== NULL
) || (StrStr (Request
, L
"OFFSET") == NULL
)) {
612 // Request has no request element, construct full request string.
613 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
614 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator.
616 ConfigRequestHdr
= HiiConstructConfigHdr (&mVariableCleanupHiiGuid
, mVarStoreName
, Private
->HiiHandle
);
617 Size
= (StrLen (ConfigRequestHdr
) + 32 + 1) * sizeof (CHAR16
);
618 ConfigRequest
= AllocateZeroPool (Size
);
619 ASSERT (ConfigRequest
!= NULL
);
620 AllocatedRequest
= TRUE
;
621 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr
, (UINT64
)BufferSize
);
622 FreePool (ConfigRequestHdr
);
625 Status
= Private
->ConfigRouting
->BlockToConfig (
626 Private
->ConfigRouting
,
628 (UINT8
*) &Private
->VariableCleanupData
,
633 ASSERT_EFI_ERROR (Status
);
636 // Free the allocated config request string.
638 if (AllocatedRequest
) {
639 FreePool (ConfigRequest
);
640 ConfigRequest
= NULL
;
643 // Set Progress string to the original request string or the string's null terminator.
645 if (Request
== NULL
) {
647 } else if (StrStr (Request
, L
"OFFSET") == NULL
) {
648 *Progress
= Request
+ StrLen (Request
);
655 Update user variable form.
657 @param[in] Private Points to the VARIABLE_CLEANUP_HII_PRIVATE_DATA.
661 UpdateUserVariableForm (
662 IN VARIABLE_CLEANUP_HII_PRIVATE_DATA
*Private
665 EFI_STRING_ID PromptStringToken
;
666 EFI_STRING_ID HelpStringToken
;
667 VOID
*StartOpCodeHandle
;
668 VOID
*EndOpCodeHandle
;
669 EFI_IFR_GUID_LABEL
*StartLabel
;
670 EFI_IFR_GUID_LABEL
*EndLabel
;
671 USER_VARIABLE_NODE
*UserVariableNode
;
673 USER_VARIABLE_NAME_NODE
*UserVariableNameNode
;
674 LIST_ENTRY
*NameLink
;
678 // Init OpCode Handle.
680 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
681 ASSERT (StartOpCodeHandle
!= NULL
);
683 EndOpCodeHandle
= HiiAllocateOpCodeHandle ();
684 ASSERT (EndOpCodeHandle
!= NULL
);
687 // Create Hii Extend Label OpCode as the start opcode.
689 StartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (StartOpCodeHandle
, &gEfiIfrTianoGuid
, NULL
, sizeof (EFI_IFR_GUID_LABEL
));
690 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
691 StartLabel
->Number
= LABEL_START
;
694 // Create Hii Extend Label OpCode as the end opcode.
696 EndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (EndOpCodeHandle
, &gEfiIfrTianoGuid
, NULL
, sizeof (EFI_IFR_GUID_LABEL
));
697 EndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
698 EndLabel
->Number
= LABEL_END
;
702 &mVariableCleanupHiiGuid
,
703 FORM_ID_VARIABLE_CLEANUP
,
704 StartOpCodeHandle
, // LABEL_START
705 EndOpCodeHandle
// LABEL_END
708 for (Link
= mUserVariableList
.ForwardLink
709 ;Link
!= &mUserVariableList
710 ;Link
= Link
->ForwardLink
) {
711 UserVariableNode
= USER_VARIABLE_FROM_LINK (Link
);
714 // Create checkbox opcode for variables in the same variable GUID space.
717 for (NameLink
= UserVariableNode
->NameLink
.ForwardLink
718 ;NameLink
!= &UserVariableNode
->NameLink
719 ;NameLink
= NameLink
->ForwardLink
) {
720 UserVariableNameNode
= USER_VARIABLE_NAME_FROM_LINK (NameLink
);
722 if (!UserVariableNameNode
->Deleted
) {
725 // Create subtitle opcode for variable GUID.
727 PromptStringToken
= HiiSetString (Private
->HiiHandle
, 0, UserVariableNode
->PromptString
, NULL
);
728 HiiCreateSubTitleOpCode (StartOpCodeHandle
, PromptStringToken
, 0, 0, 0);
733 // Only create opcode for the non-deleted variables.
735 PromptStringToken
= HiiSetString (Private
->HiiHandle
, 0, UserVariableNameNode
->PromptString
, NULL
);
736 HelpStringToken
= HiiSetString (Private
->HiiHandle
, 0, UserVariableNameNode
->HelpString
, NULL
);
737 HiiCreateCheckBoxOpCode (
739 UserVariableNameNode
->QuestionId
,
740 VARIABLE_CLEANUP_VARSTORE_ID
,
741 (UINT16
) (USER_VARIABLE_VAR_OFFSET
+ UserVariableNameNode
->Index
),
744 EFI_IFR_FLAG_CALLBACK
,
745 Private
->VariableCleanupData
.UserVariable
[UserVariableNameNode
->Index
],
752 HiiCreateSubTitleOpCode (
754 STRING_TOKEN (STR_NULL_STRING
),
761 // Create the "Apply changes" and "Discard changes" tags.
763 HiiCreateActionOpCode (
765 SAVE_AND_EXIT_QUESTION_ID
,
766 STRING_TOKEN (STR_SAVE_AND_EXIT
),
767 STRING_TOKEN (STR_NULL_STRING
),
768 EFI_IFR_FLAG_CALLBACK
,
771 HiiCreateActionOpCode (
773 NO_SAVE_AND_EXIT_QUESTION_ID
,
774 STRING_TOKEN (STR_NO_SAVE_AND_EXIT
),
775 STRING_TOKEN (STR_NULL_STRING
),
776 EFI_IFR_FLAG_CALLBACK
,
782 &mVariableCleanupHiiGuid
,
783 FORM_ID_VARIABLE_CLEANUP
,
784 StartOpCodeHandle
, // LABEL_START
785 EndOpCodeHandle
// LABEL_END
788 HiiFreeOpCodeHandle (StartOpCodeHandle
);
789 HiiFreeOpCodeHandle (EndOpCodeHandle
);
793 This function applies changes in a driver's configuration.
794 Input is a Configuration, which has the routing data for this
795 driver followed by name / value configuration pairs. The driver
796 must apply those pairs to its configurable storage. If the
797 driver's configuration is stored in a linear block of data
798 and the driver's name / value pairs are in <BlockConfig>
799 format, it may use the ConfigToBlock helper function (above) to
800 simplify the job. Currently not implemented.
802 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
803 @param[in] Configuration A null-terminated Unicode string in
804 <ConfigString> format.
805 @param[out] Progress A pointer to a string filled in with the
806 offset of the most recent '&' before the
807 first failing name / value pair (or the
808 beginn ing of the string if the failure
809 is in the first name / value pair) or
810 the terminating NULL if all was
813 @retval EFI_SUCCESS The results have been distributed or are
814 awaiting distribution.
815 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
816 parts of the results that must be
817 stored awaiting possible future
819 @retval EFI_INVALID_PARAMETERS Passing in a NULL for the
820 Results parameter would result
821 in this type of error.
822 @retval EFI_NOT_FOUND Target for the specified routing data
828 VariableCleanupHiiRouteConfig (
829 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
830 IN CONST EFI_STRING Configuration
,
831 OUT EFI_STRING
*Progress
835 VARIABLE_CLEANUP_HII_PRIVATE_DATA
*Private
;
838 if (Progress
== NULL
) {
839 return EFI_INVALID_PARAMETER
;
841 *Progress
= Configuration
;
843 if (Configuration
== NULL
) {
844 return EFI_INVALID_PARAMETER
;
848 // Check routing data in <ConfigHdr>.
849 // Note: there is no name for Name/Value storage, only GUID will be checked.
851 if (!HiiIsConfigHdrMatch (Configuration
, &mVariableCleanupHiiGuid
, mVarStoreName
)) {
852 return EFI_NOT_FOUND
;
855 Private
= VARIABLE_CLEANUP_HII_PRIVATE_FROM_THIS (This
);
857 // Get Buffer Storage data.
859 BufferSize
= sizeof (VARIABLE_CLEANUP_DATA
);
861 // Convert <ConfigResp> to buffer data by helper function ConfigToBlock().
863 Status
= Private
->ConfigRouting
->ConfigToBlock (
864 Private
->ConfigRouting
,
866 (UINT8
*) &Private
->VariableCleanupData
,
870 ASSERT_EFI_ERROR (Status
);
872 DeleteUserVariable (FALSE
, &Private
->VariableCleanupData
);
874 // For "F10" hotkey to refresh the form.
876 // UpdateUserVariableForm (Private);
882 This function is called to provide results data to the driver.
883 This data consists of a unique key that is used to identify
884 which data is either being passed back or being asked for.
886 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
887 @param[in] Action Specifies the type of action taken by the browser.
888 @param[in] QuestionId A unique value which is sent to the original
889 exporting driver so that it can identify the type
890 of data to expect. The format of the data tends to
891 vary based on the opcode that generated the callback.
892 @param[in] Type The type of value for the question.
893 @param[in] Value A pointer to the data being sent to the original
895 @param[out] ActionRequest On return, points to the action requested by the
898 @retval EFI_SUCCESS The callback successfully handled the action.
899 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
900 variable and its data.
901 @retval EFI_DEVICE_ERROR The variable could not be saved.
902 @retval EFI_UNSUPPORTED The specified Action is not supported by the
907 VariableCleanupHiiCallback (
908 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
909 IN EFI_BROWSER_ACTION Action
,
910 IN EFI_QUESTION_ID QuestionId
,
912 IN EFI_IFR_TYPE_VALUE
*Value
,
913 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
916 VARIABLE_CLEANUP_HII_PRIVATE_DATA
*Private
;
917 VARIABLE_CLEANUP_DATA
*VariableCleanupData
;
919 Private
= VARIABLE_CLEANUP_HII_PRIVATE_FROM_THIS (This
);
921 if ((Action
!= EFI_BROWSER_ACTION_CHANGING
) && (Action
!= EFI_BROWSER_ACTION_CHANGED
)) {
923 // All other action return unsupported.
925 return EFI_UNSUPPORTED
;
929 // Retrive uncommitted data from Form Browser.
931 VariableCleanupData
= &Private
->VariableCleanupData
;
932 HiiGetBrowserData (&mVariableCleanupHiiGuid
, mVarStoreName
, sizeof (VARIABLE_CLEANUP_DATA
), (UINT8
*) VariableCleanupData
);
933 if (Action
== EFI_BROWSER_ACTION_CHANGING
) {
935 return EFI_INVALID_PARAMETER
;
937 } else if (Action
== EFI_BROWSER_ACTION_CHANGED
) {
938 if ((Value
== NULL
) || (ActionRequest
== NULL
)) {
939 return EFI_INVALID_PARAMETER
;
941 if ((QuestionId
>= USER_VARIABLE_QUESTION_ID
) && (QuestionId
< USER_VARIABLE_QUESTION_ID
+ MAX_USER_VARIABLE_COUNT
)) {
944 // Means one user variable checkbox is marked to delete but not press F10 or "Commit Changes and Exit" menu.
946 mMarkedUserVariableCount
++;
947 ASSERT (mMarkedUserVariableCount
<= mUserVariableCount
);
948 if (mMarkedUserVariableCount
== mUserVariableCount
) {
950 // All user variables have been marked, then also mark the SelectAll checkbox.
952 VariableCleanupData
->SelectAll
= TRUE
;
956 // Means one user variable checkbox is unmarked.
958 mMarkedUserVariableCount
--;
960 // Also unmark the SelectAll checkbox.
962 VariableCleanupData
->SelectAll
= FALSE
;
965 switch (QuestionId
) {
966 case SELECT_ALL_QUESTION_ID
:
969 // Means the SelectAll checkbox is marked to delete all user variables but not press F10 or "Commit Changes and Exit" menu.
971 SetMem (VariableCleanupData
->UserVariable
, sizeof (VariableCleanupData
->UserVariable
), TRUE
);
972 mMarkedUserVariableCount
= mUserVariableCount
;
975 // Means the SelectAll checkbox is unmarked.
977 SetMem (VariableCleanupData
->UserVariable
, sizeof (VariableCleanupData
->UserVariable
), FALSE
);
978 mMarkedUserVariableCount
= 0;
981 case SAVE_AND_EXIT_QUESTION_ID
:
982 DeleteUserVariable (FALSE
, VariableCleanupData
);
983 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_SUBMIT_EXIT
;
986 case NO_SAVE_AND_EXIT_QUESTION_ID
:
988 // Restore local maintain data.
990 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT
;
1000 // Pass changed uncommitted data back to Form Browser.
1002 HiiSetBrowserData (&mVariableCleanupHiiGuid
, mVarStoreName
, sizeof (VARIABLE_CLEANUP_DATA
), (UINT8
*) VariableCleanupData
, NULL
);
1007 Platform variable cleanup.
1009 @param[in] Flag Variable error flag.
1010 @param[in] Type Variable cleanup type.
1011 If it is VarCleanupManually, the interface must be called after console connected.
1013 @retval EFI_SUCCESS No error or error processed.
1014 @retval EFI_UNSUPPORTED The specified Flag or Type is not supported.
1015 For example, system error may be not supported to process and Platform should have mechanism to reset system to manufacture mode.
1016 Another, if system and user variables are wanted to be distinguished to process, the interface must be called after EndOfDxe.
1017 @retval EFI_OUT_OF_RESOURCES Not enough resource to process the error.
1018 @retval EFI_INVALID_PARAMETER The specified Flag or Type is an invalid value.
1019 @retval Others Other failure occurs.
1024 PlatformVarCleanup (
1025 IN VAR_ERROR_FLAG Flag
,
1026 IN VAR_CLEANUP_TYPE Type
1030 EFI_FORM_BROWSER2_PROTOCOL
*FormBrowser2
;
1031 VARIABLE_CLEANUP_HII_PRIVATE_DATA
*Private
;
1035 // This implementation must be called after EndOfDxe.
1037 return EFI_UNSUPPORTED
;
1040 if ((Type
>= VarCleanupMax
) || ((Flag
& ((VAR_ERROR_FLAG
) (VAR_ERROR_FLAG_SYSTEM_ERROR
& VAR_ERROR_FLAG_USER_ERROR
))) == 0)) {
1041 return EFI_INVALID_PARAMETER
;
1044 if (Flag
== VAR_ERROR_FLAG_NO_ERROR
) {
1046 // Just return success if no error.
1051 if ((Flag
& (~((VAR_ERROR_FLAG
) VAR_ERROR_FLAG_SYSTEM_ERROR
))) == 0) {
1053 // This sample does not support system variables cleanup.
1055 DEBUG ((EFI_D_ERROR
, "NOTICE - VAR_ERROR_FLAG_SYSTEM_ERROR\n"));
1056 DEBUG ((EFI_D_ERROR
, "Platform should have mechanism to reset system to manufacture mode\n"));
1057 return EFI_UNSUPPORTED
;
1061 // Continue to process VAR_ERROR_FLAG_USER_ERROR.
1065 // Create user variable nodes for the following processing.
1067 CreateUserVariableNode ();
1071 DeleteUserVariable (TRUE
, NULL
);
1073 // Destroyed the created user variable nodes
1075 DestroyUserVariableNode ();
1079 case VarCleanupManually
:
1081 // Locate FormBrowser2 protocol.
1083 Status
= gBS
->LocateProtocol (&gEfiFormBrowser2ProtocolGuid
, NULL
, (VOID
**) &FormBrowser2
);
1084 if (EFI_ERROR (Status
)) {
1088 Private
= AllocateZeroPool (sizeof (VARIABLE_CLEANUP_HII_PRIVATE_DATA
));
1089 if (Private
== NULL
) {
1090 return EFI_OUT_OF_RESOURCES
;
1093 Private
->Signature
= VARIABLE_CLEANUP_HII_PRIVATE_SIGNATURE
;
1094 Private
->ConfigAccess
.ExtractConfig
= VariableCleanupHiiExtractConfig
;
1095 Private
->ConfigAccess
.RouteConfig
= VariableCleanupHiiRouteConfig
;
1096 Private
->ConfigAccess
.Callback
= VariableCleanupHiiCallback
;
1098 Status
= gBS
->LocateProtocol (
1099 &gEfiHiiConfigRoutingProtocolGuid
,
1101 (VOID
**) &Private
->ConfigRouting
1103 if (EFI_ERROR (Status
)) {
1108 // Install Device Path Protocol and Config Access protocol to driver handle.
1110 Status
= gBS
->InstallMultipleProtocolInterfaces (
1111 &Private
->DriverHandle
,
1112 &gEfiDevicePathProtocolGuid
,
1113 &mVarCleanupHiiVendorDevicePath
,
1114 &gEfiHiiConfigAccessProtocolGuid
,
1115 &Private
->ConfigAccess
,
1118 if (EFI_ERROR (Status
)) {
1123 // Publish our HII data.
1125 Private
->HiiHandle
= HiiAddPackages (
1126 &mVariableCleanupHiiGuid
,
1127 Private
->DriverHandle
,
1128 PlatformVarCleanupLibStrings
,
1132 if (Private
->HiiHandle
== NULL
) {
1133 Status
= EFI_OUT_OF_RESOURCES
;
1137 UpdateUserVariableForm (Private
);
1139 Status
= FormBrowser2
->SendForm (
1141 &Private
->HiiHandle
,
1151 return EFI_UNSUPPORTED
;
1156 if (Private
->DriverHandle
!= NULL
) {
1157 gBS
->UninstallMultipleProtocolInterfaces (
1158 Private
->DriverHandle
,
1159 &gEfiDevicePathProtocolGuid
,
1160 &mVarCleanupHiiVendorDevicePath
,
1161 &gEfiHiiConfigAccessProtocolGuid
,
1162 &Private
->ConfigAccess
,
1166 if (Private
->HiiHandle
!= NULL
) {
1167 HiiRemovePackages (Private
->HiiHandle
);
1173 // Destroyed the created user variable nodes
1175 DestroyUserVariableNode ();
1180 Get last boot variable error flag.
1182 @return Last boot variable error flag.
1187 GetLastBootVarErrorFlag (
1190 return mLastVarErrorFlag
;
1194 Notification function of END_OF_DXE.
1196 This is a notification function registered on END_OF_DXE event.
1198 @param[in] Event Event whose notification function is being invoked.
1199 @param[in] Context Pointer to the notification function's context.
1204 PlatformVarCleanupEndOfDxeEvent (
1213 The constructor function caches the pointer to VarCheck protocol and last boot variable error flag.
1215 The constructor function locates VarCheck protocol from protocol database.
1216 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
1218 @param ImageHandle The firmware allocated handle for the EFI image.
1219 @param SystemTable A pointer to the EFI System Table.
1221 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
1226 PlatformVarCleanupLibConstructor (
1227 IN EFI_HANDLE ImageHandle
,
1228 IN EFI_SYSTEM_TABLE
*SystemTable
1234 mLastVarErrorFlag
= InternalGetVarErrorFlag ();
1235 DEBUG ((EFI_D_INFO
, "mLastVarErrorFlag - 0x%02x\n", mLastVarErrorFlag
));
1238 // Register EFI_END_OF_DXE_EVENT_GROUP_GUID event.
1240 Status
= gBS
->CreateEventEx (
1243 PlatformVarCleanupEndOfDxeEvent
,
1245 &gEfiEndOfDxeEventGroupGuid
,
1248 ASSERT_EFI_ERROR (Status
);