2 Sample platform variable cleanup library implementation.
4 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "PlatVarCleanup.h"
11 VAR_ERROR_FLAG mLastVarErrorFlag
= VAR_ERROR_FLAG_NO_ERROR
;
12 EDKII_VAR_CHECK_PROTOCOL
*mVarCheck
= NULL
;
15 /// The flag to indicate whether the platform has left the DXE phase of execution.
17 BOOLEAN mEndOfDxe
= FALSE
;
19 EFI_EVENT mPlatVarCleanupLibEndOfDxeEvent
= NULL
;
21 LIST_ENTRY mUserVariableList
= INITIALIZE_LIST_HEAD_VARIABLE (mUserVariableList
);
22 UINT16 mUserVariableCount
= 0;
23 UINT16 mMarkedUserVariableCount
= 0;
25 EFI_GUID mVariableCleanupHiiGuid
= VARIABLE_CLEANUP_HII_GUID
;
26 CHAR16 mVarStoreName
[] = L
"VariableCleanup";
28 HII_VENDOR_DEVICE_PATH mVarCleanupHiiVendorDevicePath
= {
34 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
)),
35 (UINT8
) ((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
38 VARIABLE_CLEANUP_HII_GUID
42 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
44 (UINT8
) (sizeof (EFI_DEVICE_PATH_PROTOCOL
)),
45 (UINT8
) ((sizeof (EFI_DEVICE_PATH_PROTOCOL
)) >> 8)
51 Internal get variable error flag.
53 @return Variable error flag.
57 InternalGetVarErrorFlag (
63 VAR_ERROR_FLAG ErrorFlag
;
65 Size
= sizeof (ErrorFlag
);
66 Status
= gRT
->GetVariable (
68 &gEdkiiVarErrorFlagGuid
,
73 if (EFI_ERROR (Status
)) {
74 DEBUG ((EFI_D_INFO
, "%s - not found\n", VAR_ERROR_FLAG_NAME
));
75 return VAR_ERROR_FLAG_NO_ERROR
;
83 @param[in] Name Pointer to variable name.
84 @param[in] Guid Pointer to vendor guid.
86 @retval TRUE User variable.
87 @retval FALSE System variable.
97 VAR_CHECK_VARIABLE_PROPERTY Property
;
99 if (mVarCheck
== NULL
) {
100 gBS
->LocateProtocol (
101 &gEdkiiVarCheckProtocolGuid
,
106 ASSERT (mVarCheck
!= NULL
);
108 ZeroMem (&Property
, sizeof (Property
));
109 Status
= mVarCheck
->VariablePropertyGet (
114 if (EFI_ERROR (Status
)) {
116 // No property, it is user variable.
118 DEBUG ((EFI_D_INFO
, "PlatformVarCleanup - User variable: %g:%s\n", Guid
, Name
));
122 // DEBUG ((EFI_D_INFO, "PlatformVarCleanup - Variable Property: %g:%s\n", Guid, Name));
123 // DEBUG ((EFI_D_INFO, " Revision - 0x%04x\n", Property.Revision));
124 // DEBUG ((EFI_D_INFO, " Property - 0x%04x\n", Property.Property));
125 // DEBUG ((EFI_D_INFO, " Attribute - 0x%08x\n", Property.Attributes));
126 // DEBUG ((EFI_D_INFO, " MinSize - 0x%x\n", Property.MinSize));
127 // DEBUG ((EFI_D_INFO, " MaxSize - 0x%x\n", Property.MaxSize));
133 Find user variable node by variable GUID.
135 @param[in] Guid Pointer to vendor guid.
137 @return Pointer to user variable node.
141 FindUserVariableNodeByGuid (
145 USER_VARIABLE_NODE
*UserVariableNode
;
148 for (Link
= mUserVariableList
.ForwardLink
149 ;Link
!= &mUserVariableList
150 ;Link
= Link
->ForwardLink
) {
151 UserVariableNode
= USER_VARIABLE_FROM_LINK (Link
);
153 if (CompareGuid (Guid
, &UserVariableNode
->Guid
)) {
157 return UserVariableNode
;
162 // Create new one if not found.
164 UserVariableNode
= AllocateZeroPool (sizeof (*UserVariableNode
));
165 ASSERT (UserVariableNode
!= NULL
);
166 UserVariableNode
->Signature
= USER_VARIABLE_NODE_SIGNATURE
;
167 CopyGuid (&UserVariableNode
->Guid
, Guid
);
169 // (36 chars of "########-####-####-####-############" + 1 space + 1 terminator) * sizeof (CHAR16).
171 UserVariableNode
->PromptString
= AllocatePool ((36 + 2) * sizeof (CHAR16
));
172 ASSERT (UserVariableNode
->PromptString
!= NULL
);
173 UnicodeSPrint (UserVariableNode
->PromptString
, (36 + 2) * sizeof (CHAR16
), L
" %g", &UserVariableNode
->Guid
);
174 InitializeListHead (&UserVariableNode
->NameLink
);
175 InsertTailList (&mUserVariableList
, &UserVariableNode
->Link
);
176 return UserVariableNode
;
180 Create user variable node.
184 CreateUserVariableNode (
189 EFI_STATUS GetVariableStatus
;
191 UINTN MaxVarNameSize
;
198 USER_VARIABLE_NODE
*UserVariableNode
;
199 USER_VARIABLE_NAME_NODE
*UserVariableNameNode
;
204 // Initialize 128 * sizeof (CHAR16) variable name size.
206 MaxVarNameSize
= 128 * sizeof (CHAR16
);
207 VarName
= AllocateZeroPool (MaxVarNameSize
);
208 ASSERT (VarName
!= NULL
);
211 // Initialize 0x1000 variable data size.
213 MaxDataSize
= 0x1000;
214 Data
= AllocateZeroPool (MaxDataSize
);
215 ASSERT (Data
!= NULL
);
219 VarNameSize
= MaxVarNameSize
;
220 Status
= gRT
->GetNextVariableName (&VarNameSize
, VarName
, &Guid
);
221 if (Status
== EFI_BUFFER_TOO_SMALL
) {
222 VarName
= ReallocatePool (MaxVarNameSize
, VarNameSize
, VarName
);
223 ASSERT (VarName
!= NULL
);
224 MaxVarNameSize
= VarNameSize
;
225 Status
= gRT
->GetNextVariableName (&VarNameSize
, VarName
, &Guid
);
228 if (!EFI_ERROR (Status
)) {
229 if (IsUserVariable (VarName
, &Guid
)) {
230 DataSize
= MaxDataSize
;
231 GetVariableStatus
= gRT
->GetVariable (VarName
, &Guid
, &Attributes
, &DataSize
, Data
);
232 if (GetVariableStatus
== EFI_BUFFER_TOO_SMALL
) {
233 Data
= ReallocatePool (MaxDataSize
, DataSize
, Data
);
234 ASSERT (Data
!= NULL
);
235 MaxDataSize
= DataSize
;
236 GetVariableStatus
= gRT
->GetVariable (VarName
, &Guid
, &Attributes
, &DataSize
, Data
);
238 ASSERT_EFI_ERROR (GetVariableStatus
);
240 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
241 UserVariableNode
= FindUserVariableNodeByGuid (&Guid
);
242 ASSERT (UserVariableNode
!= NULL
);
245 // Different variables that have same variable GUID share same user variable node.
247 UserVariableNameNode
= AllocateZeroPool (sizeof (*UserVariableNameNode
));
248 ASSERT (UserVariableNameNode
!= NULL
);
249 UserVariableNameNode
->Signature
= USER_VARIABLE_NAME_NODE_SIGNATURE
;
250 UserVariableNameNode
->Name
= AllocateCopyPool (VarNameSize
, VarName
);
251 UserVariableNameNode
->Attributes
= Attributes
;
252 UserVariableNameNode
->DataSize
= DataSize
;
253 UserVariableNameNode
->Index
= Index
;
254 UserVariableNameNode
->QuestionId
= (EFI_QUESTION_ID
) (USER_VARIABLE_QUESTION_ID
+ Index
);
256 // 2 space * sizeof (CHAR16) + StrSize.
258 StringSize
= 2 * sizeof (CHAR16
) + StrSize (UserVariableNameNode
->Name
);
259 UserVariableNameNode
->PromptString
= AllocatePool (StringSize
);
260 ASSERT (UserVariableNameNode
->PromptString
!= NULL
);
261 UnicodeSPrint (UserVariableNameNode
->PromptString
, StringSize
, L
" %s", UserVariableNameNode
->Name
);
263 // (33 chars of "Attribtues = 0x and DataSize = 0x" + 1 terminator + (sizeof (UINT32) + sizeof (UINTN)) * 2) * sizeof (CHAR16).
265 StringSize
= (33 + 1 + (sizeof (UINT32
) + sizeof (UINTN
)) * 2) * sizeof (CHAR16
);
266 UserVariableNameNode
->HelpString
= AllocatePool (StringSize
);
267 ASSERT (UserVariableNameNode
->HelpString
!= NULL
);
268 UnicodeSPrint (UserVariableNameNode
->HelpString
, StringSize
, L
"Attribtues = 0x%08x and DataSize = 0x%x", UserVariableNameNode
->Attributes
, UserVariableNameNode
->DataSize
);
269 UserVariableNameNode
->Deleted
= FALSE
;
270 InsertTailList (&UserVariableNode
->NameLink
, &UserVariableNameNode
->Link
);
275 } while (Status
!= EFI_NOT_FOUND
);
277 mUserVariableCount
= Index
;
278 ASSERT (mUserVariableCount
<= MAX_USER_VARIABLE_COUNT
);
279 DEBUG ((EFI_D_INFO
, "PlatformVarCleanup - User variable count: 0x%04x\n", mUserVariableCount
));
286 Destroy user variable nodes.
290 DestroyUserVariableNode (
294 USER_VARIABLE_NODE
*UserVariableNode
;
296 USER_VARIABLE_NAME_NODE
*UserVariableNameNode
;
297 LIST_ENTRY
*NameLink
;
299 while (mUserVariableList
.ForwardLink
!= &mUserVariableList
) {
300 Link
= mUserVariableList
.ForwardLink
;
301 UserVariableNode
= USER_VARIABLE_FROM_LINK (Link
);
303 RemoveEntryList (&UserVariableNode
->Link
);
305 while (UserVariableNode
->NameLink
.ForwardLink
!= &UserVariableNode
->NameLink
) {
306 NameLink
= UserVariableNode
->NameLink
.ForwardLink
;
307 UserVariableNameNode
= USER_VARIABLE_NAME_FROM_LINK (NameLink
);
309 RemoveEntryList (&UserVariableNameNode
->Link
);
311 FreePool (UserVariableNameNode
->Name
);
312 FreePool (UserVariableNameNode
->PromptString
);
313 FreePool (UserVariableNameNode
->HelpString
);
314 FreePool (UserVariableNameNode
);
317 FreePool (UserVariableNode
->PromptString
);
318 FreePool (UserVariableNode
);
323 Create a time based data payload by concatenating the EFI_VARIABLE_AUTHENTICATION_2
324 descriptor with the input data. NO authentication is required in this function.
326 @param[in, out] DataSize On input, the size of Data buffer in bytes.
327 On output, the size of data returned in Data
329 @param[in, out] Data On input, Pointer to data buffer to be wrapped or
330 pointer to NULL to wrap an empty payload.
331 On output, Pointer to the new payload date buffer allocated from pool,
332 it's caller's responsibility to free the memory after using it.
334 @retval EFI_SUCCESS Create time based payload successfully.
335 @retval EFI_OUT_OF_RESOURCES There are not enough memory resourses to create time based payload.
336 @retval EFI_INVALID_PARAMETER The parameter is invalid.
337 @retval Others Unexpected error happens.
341 CreateTimeBasedPayload (
342 IN OUT UINTN
*DataSize
,
350 EFI_VARIABLE_AUTHENTICATION_2
*DescriptorData
;
351 UINTN DescriptorSize
;
354 if (Data
== NULL
|| DataSize
== NULL
) {
355 return EFI_INVALID_PARAMETER
;
359 // At user physical presence, the variable does not need to be signed but the
360 // parameters to the SetVariable() call still need to be prepared as authenticated
361 // variable. So we create EFI_VARIABLE_AUTHENTICATED_2 descriptor without certificate
365 PayloadSize
= *DataSize
;
367 DescriptorSize
= OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
) + OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
);
368 NewData
= (UINT8
*) AllocateZeroPool (DescriptorSize
+ PayloadSize
);
369 if (NewData
== NULL
) {
370 return EFI_OUT_OF_RESOURCES
;
373 if ((Payload
!= NULL
) && (PayloadSize
!= 0)) {
374 CopyMem (NewData
+ DescriptorSize
, Payload
, PayloadSize
);
377 DescriptorData
= (EFI_VARIABLE_AUTHENTICATION_2
*) (NewData
);
379 ZeroMem (&Time
, sizeof (EFI_TIME
));
380 Status
= gRT
->GetTime (&Time
, NULL
);
381 if (EFI_ERROR (Status
)) {
390 CopyMem (&DescriptorData
->TimeStamp
, &Time
, sizeof (EFI_TIME
));
392 DescriptorData
->AuthInfo
.Hdr
.dwLength
= OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
);
393 DescriptorData
->AuthInfo
.Hdr
.wRevision
= 0x0200;
394 DescriptorData
->AuthInfo
.Hdr
.wCertificateType
= WIN_CERT_TYPE_EFI_GUID
;
395 CopyGuid (&DescriptorData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
);
397 if (Payload
!= NULL
) {
401 *DataSize
= DescriptorSize
+ PayloadSize
;
407 Create a counter based data payload by concatenating the EFI_VARIABLE_AUTHENTICATION
408 descriptor with the input data. NO authentication is required in this function.
410 @param[in, out] DataSize On input, the size of Data buffer in bytes.
411 On output, the size of data returned in Data
413 @param[in, out] Data On input, Pointer to data buffer to be wrapped or
414 pointer to NULL to wrap an empty payload.
415 On output, Pointer to the new payload date buffer allocated from pool,
416 it's caller's responsibility to free the memory after using it.
418 @retval EFI_SUCCESS Create counter based payload successfully.
419 @retval EFI_OUT_OF_RESOURCES There are not enough memory resourses to create time based payload.
420 @retval EFI_INVALID_PARAMETER The parameter is invalid.
421 @retval Others Unexpected error happens.
425 CreateCounterBasedPayload (
426 IN OUT UINTN
*DataSize
,
434 EFI_VARIABLE_AUTHENTICATION
*DescriptorData
;
435 UINTN DescriptorSize
;
436 UINT64 MonotonicCount
;
438 if (Data
== NULL
|| DataSize
== NULL
) {
439 return EFI_INVALID_PARAMETER
;
443 // At user physical presence, the variable does not need to be signed but the
444 // parameters to the SetVariable() call still need to be prepared as authenticated
445 // variable. So we create EFI_VARIABLE_AUTHENTICATED descriptor without certificate
449 PayloadSize
= *DataSize
;
451 DescriptorSize
= (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION
, AuthInfo
)) + \
452 (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)) + \
453 sizeof (EFI_CERT_BLOCK_RSA_2048_SHA256
);
454 NewData
= (UINT8
*) AllocateZeroPool (DescriptorSize
+ PayloadSize
);
455 if (NewData
== NULL
) {
456 return EFI_OUT_OF_RESOURCES
;
459 if ((Payload
!= NULL
) && (PayloadSize
!= 0)) {
460 CopyMem (NewData
+ DescriptorSize
, Payload
, PayloadSize
);
463 DescriptorData
= (EFI_VARIABLE_AUTHENTICATION
*) (NewData
);
465 Status
= gBS
->GetNextMonotonicCount (&MonotonicCount
);
466 if (EFI_ERROR (Status
)) {
470 DescriptorData
->MonotonicCount
= MonotonicCount
;
472 DescriptorData
->AuthInfo
.Hdr
.dwLength
= OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
) + sizeof (EFI_CERT_BLOCK_RSA_2048_SHA256
);
473 DescriptorData
->AuthInfo
.Hdr
.wRevision
= 0x0200;
474 DescriptorData
->AuthInfo
.Hdr
.wCertificateType
= WIN_CERT_TYPE_EFI_GUID
;
475 CopyGuid (&DescriptorData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
);
477 if (Payload
!= NULL
) {
481 *DataSize
= DescriptorSize
+ PayloadSize
;
487 Delete user variable.
489 @param[in] DeleteAll Delete all user variables.
490 @param[in] VariableCleanupData Pointer to variable cleanup data.
495 IN BOOLEAN DeleteAll
,
496 IN VARIABLE_CLEANUP_DATA
*VariableCleanupData OPTIONAL
500 USER_VARIABLE_NODE
*UserVariableNode
;
502 USER_VARIABLE_NAME_NODE
*UserVariableNameNode
;
503 LIST_ENTRY
*NameLink
;
507 for (Link
= mUserVariableList
.ForwardLink
508 ;Link
!= &mUserVariableList
509 ;Link
= Link
->ForwardLink
) {
510 UserVariableNode
= USER_VARIABLE_FROM_LINK (Link
);
512 for (NameLink
= UserVariableNode
->NameLink
.ForwardLink
513 ;NameLink
!= &UserVariableNode
->NameLink
514 ;NameLink
= NameLink
->ForwardLink
) {
515 UserVariableNameNode
= USER_VARIABLE_NAME_FROM_LINK (NameLink
);
517 if (!UserVariableNameNode
->Deleted
&& (DeleteAll
|| ((VariableCleanupData
!= NULL
) && (VariableCleanupData
->UserVariable
[UserVariableNameNode
->Index
] == TRUE
)))) {
518 DEBUG ((EFI_D_INFO
, "PlatformVarCleanup - Delete variable: %g:%s\n", &UserVariableNode
->Guid
, UserVariableNameNode
->Name
));
519 if ((UserVariableNameNode
->Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
522 Status
= CreateTimeBasedPayload (&DataSize
, &Data
);
523 if (!EFI_ERROR (Status
)) {
524 Status
= gRT
->SetVariable (UserVariableNameNode
->Name
, &UserVariableNode
->Guid
, UserVariableNameNode
->Attributes
, DataSize
, Data
);
527 } else if ((UserVariableNameNode
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
530 Status
= CreateCounterBasedPayload (&DataSize
, &Data
);
531 if (!EFI_ERROR (Status
)) {
532 Status
= gRT
->SetVariable (UserVariableNameNode
->Name
, &UserVariableNode
->Guid
, UserVariableNameNode
->Attributes
, DataSize
, Data
);
536 Status
= gRT
->SetVariable (UserVariableNameNode
->Name
, &UserVariableNode
->Guid
, 0, 0, NULL
);
538 if (!EFI_ERROR (Status
)) {
539 UserVariableNameNode
->Deleted
= TRUE
;
541 DEBUG ((EFI_D_INFO
, "PlatformVarCleanup - Delete variable fail: %g:%s\n", &UserVariableNode
->Guid
, UserVariableNameNode
->Name
));
549 This function allows a caller to extract the current configuration for one
550 or more named elements from the target driver.
552 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
553 @param[in] Request A null-terminated Unicode string in <ConfigRequest> format.
554 @param[out] Progress On return, points to a character in the Request string.
555 Points to the string's null terminator if request was successful.
556 Points to the most recent '&' before the first failing name/value
557 pair (or the beginning of the string if the failure is in the
558 first name/value pair) if the request was not successful.
559 @param[out] Results A null-terminated Unicode string in <ConfigAltResp> format which
560 has all values filled in for the names in the Request string.
561 String to be allocated by the called function.
563 @retval EFI_SUCCESS The Results is filled with the requested values.
564 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.
565 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.
566 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this driver.
571 VariableCleanupHiiExtractConfig (
572 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
573 IN CONST EFI_STRING Request
,
574 OUT EFI_STRING
*Progress
,
575 OUT EFI_STRING
*Results
579 VARIABLE_CLEANUP_HII_PRIVATE_DATA
*Private
;
581 EFI_STRING ConfigRequestHdr
;
582 EFI_STRING ConfigRequest
;
583 BOOLEAN AllocatedRequest
;
586 if (Progress
== NULL
|| Results
== NULL
) {
587 return EFI_INVALID_PARAMETER
;
591 if ((Request
!= NULL
) && !HiiIsConfigHdrMatch (Request
, &mVariableCleanupHiiGuid
, mVarStoreName
)) {
592 return EFI_NOT_FOUND
;
595 ConfigRequestHdr
= NULL
;
596 ConfigRequest
= NULL
;
597 AllocatedRequest
= FALSE
;
600 Private
= VARIABLE_CLEANUP_HII_PRIVATE_FROM_THIS (This
);
602 // Convert buffer data to <ConfigResp> by helper function BlockToConfig().
604 BufferSize
= sizeof (VARIABLE_CLEANUP_DATA
);
605 ConfigRequest
= Request
;
606 if ((Request
== NULL
) || (StrStr (Request
, L
"OFFSET") == NULL
)) {
608 // Request has no request element, construct full request string.
609 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
610 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator.
612 ConfigRequestHdr
= HiiConstructConfigHdr (
613 &mVariableCleanupHiiGuid
,
615 Private
->DriverHandle
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 // Retrieve 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 (
1191 return mLastVarErrorFlag
;
1195 Notification function of END_OF_DXE.
1197 This is a notification function registered on END_OF_DXE event.
1199 @param[in] Event Event whose notification function is being invoked.
1200 @param[in] Context Pointer to the notification function's context.
1205 PlatformVarCleanupEndOfDxeEvent (
1214 The constructor function caches the pointer to VarCheck protocol and last boot variable error flag.
1216 The constructor function locates VarCheck protocol from protocol database.
1217 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
1219 @param ImageHandle The firmware allocated handle for the EFI image.
1220 @param SystemTable A pointer to the EFI System Table.
1222 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
1227 PlatformVarCleanupLibConstructor (
1228 IN EFI_HANDLE ImageHandle
,
1229 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
,
1246 &mPlatVarCleanupLibEndOfDxeEvent
1248 ASSERT_EFI_ERROR (Status
);
1254 The destructor function closes the End of DXE event.
1256 @param ImageHandle The firmware allocated handle for the EFI image.
1257 @param SystemTable A pointer to the EFI System Table.
1259 @retval EFI_SUCCESS The destructor completed successfully.
1264 PlatformVarCleanupLibDestructor (
1265 IN EFI_HANDLE ImageHandle
,
1266 IN EFI_SYSTEM_TABLE
*SystemTable
1272 // Close the End of DXE event.
1274 Status
= gBS
->CloseEvent (mPlatVarCleanupLibEndOfDxeEvent
);
1275 ASSERT_EFI_ERROR (Status
);