]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Compatibility/FrameworkHiiOnUefiHiiThunk/ConfigAccess.c
Update HiiConfigAccess.ExtractConfig interface to support NULL request string and...
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / FrameworkHiiOnUefiHiiThunk / ConfigAccess.c
1 /** @file
2 This file implements functions related to Config Access Protocols installed by
3 by HII Thunk Modules. These Config access Protocols are used to thunk UEFI Config
4 Access Callback to Framework HII Callback and EFI Variable Set/Get operations.
5
6 Copyright (c) 2008 - 2010, Intel Corporation
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
15 **/
16
17 #include "HiiDatabase.h"
18 #include "UefiIfrParser.h"
19
20 BOOLEAN mHiiPackageListUpdated = FALSE;
21
22 HII_VENDOR_DEVICE_PATH mUefiHiiVendorDevicePath = {
23 {
24 {
25 {
26 HARDWARE_DEVICE_PATH,
27 HW_VENDOR_DP,
28 {
29 (UINT8) (sizeof (HII_VENDOR_DEVICE_PATH_NODE)),
30 (UINT8) ((sizeof (HII_VENDOR_DEVICE_PATH_NODE)) >> 8)
31 }
32 },
33 EFI_CALLER_ID_GUID
34 },
35 0,
36 0
37 },
38 {
39 END_DEVICE_PATH_TYPE,
40 END_ENTIRE_DEVICE_PATH_SUBTYPE,
41 {
42 (UINT8) (sizeof (EFI_DEVICE_PATH_PROTOCOL)),
43 (UINT8) ((sizeof (EFI_DEVICE_PATH_PROTOCOL)) >> 8)
44 }
45 }
46 };
47
48 CONFIG_ACCESS_PRIVATE gConfigAccessPrivateTempate = {
49 CONFIG_ACCESS_PRIVATE_SIGNATURE,
50 {
51 ThunkExtractConfig,
52 ThunkRouteConfig,
53 ThunkCallback
54 }, //ConfigAccessProtocol
55 NULL, //FormCallbackProtocol
56 NULL
57 };
58
59 /**
60 Get the first EFI_IFR_VARSTORE from the FormSet.
61
62 @param FormSet The Form Set.
63
64 @retval FORMSET_STORAGE * Return the first EFI_IFR_VARSTORE.
65 @retval NULL If the Form Set does not have EFI_IFR_VARSTORE.
66 **/
67 FORMSET_STORAGE *
68 GetFirstStorageOfFormSet (
69 IN CONST FORM_BROWSER_FORMSET * FormSet
70 )
71 {
72 LIST_ENTRY *StorageList;
73 FORMSET_STORAGE *Storage;
74
75 StorageList = GetFirstNode (&FormSet->StorageListHead);
76
77 if (!IsNull (&FormSet->StorageListHead, StorageList)) {
78 Storage = FORMSET_STORAGE_FROM_LINK (StorageList);
79 return Storage;
80 }
81
82 return NULL;
83 }
84
85 /**
86 Get the FORM_BROWSER_STATEMENT that matches the Question's value.
87
88 @param FormSet The Form Set.
89 @param QuestionId QuestionId
90
91 @retval FORM_BROWSER_STATEMENT* FORM_BROWSER_STATEMENT that match Question's value.
92 @retval NULL If the Form Set does not have EFI_IFR_VARSTORE.
93 **/
94 FORM_BROWSER_STATEMENT *
95 GetStorageFromQuestionId (
96 IN CONST FORM_BROWSER_FORMSET * FormSet,
97 IN EFI_QUESTION_ID QuestionId
98 )
99 {
100 LIST_ENTRY *FormList;
101 LIST_ENTRY *StatementList;
102 FORM_BROWSER_FORM *Form;
103 FORM_BROWSER_STATEMENT *Statement;
104
105 FormList = GetFirstNode (&FormSet->FormListHead);
106
107 while (!IsNull (&FormSet->FormListHead, FormList)) {
108 Form = FORM_BROWSER_FORM_FROM_LINK (FormList);
109
110 StatementList = GetFirstNode (&Form->StatementListHead);
111
112 while (!IsNull (&Form->StatementListHead, StatementList)) {
113 Statement = FORM_BROWSER_STATEMENT_FROM_LINK (StatementList);
114 if ((QuestionId == Statement->QuestionId) && (Statement->Storage != NULL)) {
115 //
116 // UEFI Question ID is unique in a FormSet.
117 //
118 ASSERT (Statement->Storage->Type == EFI_HII_VARSTORE_BUFFER);
119 return Statement;
120 }
121 StatementList = GetNextNode (&Form->StatementListHead, StatementList);
122 }
123
124 FormList = GetNextNode (&FormSet->FormListHead, FormList);
125 }
126
127 return NULL;
128 }
129
130 /**
131 Get the EFI_IFR_VARSTORE based the <ConfigHdr> string in a <ConfigRequest>
132 or a <ConfigResp> string.
133
134 @param FormSet The Form Set.
135 @param ConfigString The Configuration String which is defined by UEFI HII.
136
137 @retval FORMSET_STORAGE * The EFI_IFR_VARSTORE where the Question's value is stored.
138 @retval NULL If the Form Set does not have EFI_IFR_VARSTORE with such ID.
139 **/
140 FORMSET_STORAGE *
141 GetStorageFromConfigString (
142 IN CONST FORM_BROWSER_FORMSET *FormSet,
143 IN CONST EFI_STRING ConfigString
144 )
145 {
146 LIST_ENTRY *StorageList;
147 FORMSET_STORAGE *Storage;
148 CHAR16 *Name;
149
150 if (ConfigString == NULL) {
151 return NULL;
152 }
153
154 StorageList = GetFirstNode (&FormSet->StorageListHead);
155
156 while (!IsNull (&FormSet->StorageListHead, StorageList)) {
157 Storage = FORMSET_STORAGE_FROM_LINK (StorageList);
158
159 if ((Storage->VarStoreId == FormSet->DefaultVarStoreId) && (FormSet->OriginalDefaultVarStoreName != NULL)) {
160 Name = FormSet->OriginalDefaultVarStoreName;
161 } else {
162 Name = Storage->Name;
163 }
164
165 if (HiiIsConfigHdrMatch (ConfigString, &Storage->Guid, Name)) {
166 return Storage;
167 }
168
169 StorageList = GetNextNode (&FormSet->StorageListHead, StorageList);
170 }
171
172 return NULL;
173 }
174
175 /**
176 This function installs a EFI_CONFIG_ACCESS_PROTOCOL instance for a form package registered
177 by a module using Framework HII Protocol Interfaces.
178
179 UEFI HII require EFI_HII_CONFIG_ACCESS_PROTOCOL to be installed on a EFI_HANDLE, so
180 that Setup Utility can load the Buffer Storage using this protocol.
181
182 @param Packages The Package List.
183 @param ThunkContext The Thunk Context.
184
185 @retval EFI_SUCCESS The Config Access Protocol is installed successfully.
186 @retval EFI_OUT_RESOURCE There is not enough memory.
187
188 **/
189 EFI_STATUS
190 InstallDefaultConfigAccessProtocol (
191 IN CONST EFI_HII_PACKAGES *Packages,
192 IN OUT HII_THUNK_CONTEXT *ThunkContext
193 )
194 {
195 EFI_STATUS Status;
196 CONFIG_ACCESS_PRIVATE *ConfigAccessInstance;
197 HII_VENDOR_DEVICE_PATH *HiiVendorPath;
198
199 ASSERT (ThunkContext->IfrPackageCount != 0);
200
201 ConfigAccessInstance = AllocateCopyPool (
202 sizeof (CONFIG_ACCESS_PRIVATE),
203 &gConfigAccessPrivateTempate
204 );
205 ASSERT (ConfigAccessInstance != NULL);
206
207 //
208 // Use memory address as unique ID to distinguish from different device paths
209 // This function may be called multi times by the framework HII driver.
210 //
211 HiiVendorPath = AllocateCopyPool (
212 sizeof (HII_VENDOR_DEVICE_PATH),
213 &mUefiHiiVendorDevicePath
214 );
215 ASSERT (HiiVendorPath != NULL);
216
217 HiiVendorPath->Node.UniqueId = (UINT64) ((UINTN) HiiVendorPath);
218
219 Status = gBS->InstallMultipleProtocolInterfaces (
220 &ThunkContext->UefiHiiDriverHandle,
221 &gEfiDevicePathProtocolGuid,
222 HiiVendorPath,
223 &gEfiHiiConfigAccessProtocolGuid,
224 &ConfigAccessInstance->ConfigAccessProtocol,
225 NULL
226 );
227 ASSERT_EFI_ERROR (Status);
228
229 ConfigAccessInstance->ThunkContext = ThunkContext;
230
231 return EFI_SUCCESS;
232 }
233
234 /**
235 This function un-installs the EFI_CONFIG_ACCESS_PROTOCOL instance for a form package registered
236 by a module using Framework HII Protocol Interfaces.
237
238 ASSERT if no Config Access is found for such pakcage list or failed to uninstall the protocol.
239
240 @param ThunkContext The Thunk Context.
241
242 **/
243 VOID
244 UninstallDefaultConfigAccessProtocol (
245 IN HII_THUNK_CONTEXT *ThunkContext
246 )
247 {
248 EFI_STATUS Status;
249 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
250 HII_VENDOR_DEVICE_PATH *HiiVendorPath;
251
252 Status = gBS->HandleProtocol (
253 ThunkContext->UefiHiiDriverHandle,
254 &gEfiHiiConfigAccessProtocolGuid,
255 (VOID **) &ConfigAccess
256 );
257 ASSERT_EFI_ERROR (Status);
258
259 Status = gBS->HandleProtocol (
260 ThunkContext->UefiHiiDriverHandle,
261 &gEfiDevicePathProtocolGuid,
262 (VOID **) &HiiVendorPath
263 );
264 ASSERT_EFI_ERROR (Status);
265
266 Status = gBS->UninstallMultipleProtocolInterfaces (
267 ThunkContext->UefiHiiDriverHandle,
268 &gEfiDevicePathProtocolGuid,
269 HiiVendorPath,
270 &gEfiHiiConfigAccessProtocolGuid,
271 ConfigAccess,
272 NULL
273 );
274 ASSERT_EFI_ERROR (Status);
275
276 }
277
278
279 /**
280 Wrap the EFI_HII_CONFIG_ACCESS_PROTOCOL.ExtractConfig to a call to EFI_FORM_CALLBACK_PROTOCOL.NvRead.
281
282 @param BufferStorage The key with all attributes needed to call EFI_FORM_CALLBACK_PROTOCOL.NvRead.
283 @param FwFormCallBack The EFI_FORM_CALLBACK_PROTOCOL registered by Framework HII module.
284 @param Data The data read.
285 @param DataSize The size of data.
286
287 @retval EFI_STATUS The status returned by the EFI_FORM_CALLBACK_PROTOCOL.NvWrite.
288 @retval EFI_INVALID_PARAMETER If the EFI_FORM_CALLBACK_PROTOCOL.NvRead return the size information of the data
289 does not match what has been recorded early in he BUFFER_STORAGE_ENTRY.
290 **/
291 EFI_STATUS
292 CallFormCallBack (
293 IN FORMSET_STORAGE *BufferStorage,
294 IN EFI_FORM_CALLBACK_PROTOCOL *FwFormCallBack,
295 OUT VOID **Data,
296 OUT UINTN *DataSize
297 )
298 {
299 EFI_STATUS Status;
300
301 *DataSize = 0;
302 *Data = NULL;
303
304 Status = FwFormCallBack->NvRead (
305 FwFormCallBack,
306 BufferStorage->Name,
307 &BufferStorage->Guid,
308 NULL,
309 DataSize,
310 *Data
311 );
312 if (Status == EFI_BUFFER_TOO_SMALL) {
313 if (BufferStorage->Size != *DataSize) {
314 ASSERT (FALSE);
315 return EFI_INVALID_PARAMETER;
316 }
317
318 *Data = AllocateZeroPool (*DataSize);
319 if (*Data == NULL) {
320 return EFI_OUT_OF_RESOURCES;
321 }
322
323 Status = FwFormCallBack->NvRead (
324 FwFormCallBack,
325 BufferStorage->Name,
326 &BufferStorage->Guid,
327 NULL,
328 DataSize,
329 *Data
330 );
331 }
332
333 return Status;
334 }
335
336
337 /**
338 Wrap the EFI_HII_CONFIG_ACCESS_PROTOCOL.ExtractConfig to a call to UEFI Variable Get Service.
339
340 @param BufferStorage The key with all attributes needed to call a UEFI Variable Get Service.
341 @param Data The data read.
342 @param DataSize The size of data.
343
344 If the UEFI Variable Get Service return the size information of the data
345 does not match what has been recorded early in he BUFFER_STORAGE_ENTRY.
346 then ASSERT.
347
348 @retval EFI_STATUS The status returned by the UEFI Variable Get Service.
349 @retval EFI_INVALID_PARAMETER If the UEFI Variable Get Service return the size information of the data
350 does not match what has been recorded early in he BUFFER_STORAGE_ENTRY.
351 **/
352 EFI_STATUS
353 GetUefiVariable (
354 IN FORMSET_STORAGE *BufferStorage,
355 OUT VOID **Data,
356 OUT UINTN *DataSize
357 )
358 {
359 EFI_STATUS Status;
360
361 *DataSize = 0;
362 *Data = NULL;
363 Status = gRT->GetVariable (
364 BufferStorage->Name,
365 &BufferStorage->Guid,
366 NULL,
367 DataSize,
368 *Data
369 );
370 if (Status == EFI_BUFFER_TOO_SMALL) {
371
372 if (BufferStorage->Size != *DataSize) {
373 ASSERT (FALSE);
374 return EFI_INVALID_PARAMETER;
375 }
376
377 *Data = AllocateZeroPool (*DataSize);
378 if (*Data == NULL) {
379 return EFI_OUT_OF_RESOURCES;
380 }
381
382 Status = gRT->GetVariable (
383 BufferStorage->Name,
384 &BufferStorage->Guid,
385 NULL,
386 DataSize,
387 *Data
388 );
389 }
390
391 return Status;
392 }
393
394 /**
395
396 This function implement the EFI_HII_CONFIG_ACCESS_PROTOCOL.ExtractConfig
397 so that data can be read from the data storage such as UEFI Variable or module's
398 customized storage exposed by EFI_FRAMEWORK_CALLBACK.
399
400 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL
401 @param Request A null-terminated Unicode string in <ConfigRequest> format. Note that this
402 includes the routing information as well as the configurable name / value pairs. It is
403 invalid for this string to be in <MultiConfigRequest> format.
404
405 @param Progress On return, points to a character in the Request string. Points to the string's null
406 terminator if request was successful. Points to the most recent '&' before the first
407 failing name / value pair (or the beginning of the string if the failure is in the first
408 name / value pair) if the request was not successful
409 @param Results A null-terminated Unicode string in <ConfigAltResp> format which has all
410 values filled in for the names in the Request string. String to be allocated by the called
411 function.
412
413 @retval EFI_INVALID_PARAMETER If there is no Buffer Storage for this Config Access instance.
414 @retval EFI_SUCCESS The setting is retrived successfully.
415 @retval !EFI_SUCCESS The error returned by UEFI Get Variable or Framework Form Callback Nvread.
416 **/
417 EFI_STATUS
418 EFIAPI
419 ThunkExtractConfig (
420 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
421 IN CONST EFI_STRING Request,
422 OUT EFI_STRING *Progress,
423 OUT EFI_STRING *Results
424 )
425 {
426 EFI_STATUS Status;
427 CONFIG_ACCESS_PRIVATE *ConfigAccess;
428 FORMSET_STORAGE *BufferStorage;
429 VOID *Data;
430 UINTN DataSize;
431 FORM_BROWSER_FORMSET *FormSetContext;
432 CHAR16 *VarStoreName;
433 EFI_STRING ConfigRequestHdr;
434 EFI_STRING ConfigRequest;
435 UINTN Size;
436 BOOLEAN AllocatedRequest;
437 LIST_ENTRY *StorageList;
438 EFI_STRING SingleResult;
439 EFI_STRING FinalResults;
440 EFI_STRING StrPointer;
441
442 if (Progress == NULL || Results == NULL) {
443 return EFI_INVALID_PARAMETER;
444 }
445 *Progress = Request;
446
447 Status = EFI_SUCCESS;
448 Data = NULL;
449 StrPointer = NULL;
450 SingleResult = NULL;
451 FinalResults = NULL;
452 ConfigAccess = CONFIG_ACCESS_PRIVATE_FROM_PROTOCOL (This);
453 FormSetContext = ConfigAccess->ThunkContext->FormSet;
454 if (IsListEmpty (&FormSetContext->StorageListHead)) {
455 //
456 // No VarStorage does exist in this form.
457 //
458 return EFI_NOT_FOUND;
459 }
460 StorageList = GetFirstNode (&FormSetContext->StorageListHead);
461
462 do {
463 if (Request != NULL) {
464 BufferStorage = GetStorageFromConfigString (ConfigAccess->ThunkContext->FormSet, Request);
465 if (BufferStorage == NULL) {
466 return EFI_NOT_FOUND;
467 }
468 } else {
469 if (IsNull (&FormSetContext->StorageListHead, StorageList)) {
470 //
471 // No Storage to be extracted into the results.
472 //
473 break;
474 }
475 BufferStorage = FORMSET_STORAGE_FROM_LINK (StorageList);
476 StorageList = GetNextNode (&FormSetContext->StorageListHead, StorageList);
477 }
478
479 VarStoreName = NULL;
480 ConfigRequestHdr = NULL;
481 ConfigRequest = NULL;
482 Size = 0;
483 AllocatedRequest = FALSE;
484
485 if (ConfigAccess->ThunkContext->NvMapOverride == NULL) {
486 //
487 // NvMapOverride is not used. Get the Storage data from EFI Variable or Framework Form Callback.
488 //
489 if (ConfigAccess->FormCallbackProtocol == NULL ||
490 ConfigAccess->FormCallbackProtocol->NvRead == NULL) {
491 Status = GetUefiVariable (
492 BufferStorage,
493 &Data,
494 &DataSize
495 );
496 } else {
497 Status = CallFormCallBack (
498 BufferStorage,
499 ConfigAccess->FormCallbackProtocol,
500 &Data,
501 &DataSize
502 );
503 }
504 } else {
505 //
506 // Use the NvMapOverride.
507 //
508 DataSize = BufferStorage->Size;
509 Data = AllocateCopyPool (DataSize, ConfigAccess->ThunkContext->NvMapOverride);
510
511 if (Data != NULL) {
512 Status = EFI_SUCCESS;
513 } else {
514 Status = EFI_OUT_OF_RESOURCES;
515 }
516 }
517
518 if (!EFI_ERROR (Status)) {
519 ConfigRequest = Request;
520 if (Request == NULL || (StrStr (Request, L"OFFSET") == NULL)) {
521 //
522 // Request is without any request element, construct full request string.
523 //
524
525 if ((BufferStorage->VarStoreId == FormSetContext->DefaultVarStoreId) && (FormSetContext->OriginalDefaultVarStoreName != NULL)) {
526 VarStoreName = FormSetContext->OriginalDefaultVarStoreName;
527 } else {
528 VarStoreName = BufferStorage->Name;
529 }
530
531 //
532 // First Set ConfigRequestHdr string.
533 //
534 ConfigRequestHdr = HiiConstructConfigHdr (&BufferStorage->Guid, VarStoreName, ConfigAccess->ThunkContext->UefiHiiDriverHandle);
535 ASSERT (ConfigRequestHdr != NULL);
536
537 //
538 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
539 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
540 //
541 Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16);
542 ConfigRequest = AllocateZeroPool (Size);
543 ASSERT (ConfigRequest != NULL);
544 AllocatedRequest = TRUE;
545 UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64)DataSize);
546 FreePool (ConfigRequestHdr);
547 }
548 Status = mHiiConfigRoutingProtocol->BlockToConfig (
549 mHiiConfigRoutingProtocol,
550 ConfigRequest,
551 Data,
552 DataSize,
553 &SingleResult,
554 Progress
555 );
556 //
557 // Free the allocated config request string.
558 //
559 if (AllocatedRequest) {
560 FreePool (ConfigRequest);
561 ConfigRequest = NULL;
562 }
563 }
564 //
565 // Free the allocated Data
566 //
567 if (Data != NULL) {
568 FreePool (Data);
569 }
570 //
571 // Directly return when meet with error
572 //
573 if (EFI_ERROR (Status)) {
574 break;
575 }
576 //
577 // Merge result into the final results.
578 //
579 if (FinalResults == NULL) {
580 FinalResults = SingleResult;
581 SingleResult = NULL;
582 } else {
583 Size = StrLen (FinalResults);
584 Size = Size + 1;
585 Size = Size + StrLen (SingleResult) + 1;
586 StrPointer = AllocateZeroPool (Size * sizeof (CHAR16));
587 ASSERT (StrPointer != NULL);
588 StrCpy (StrPointer, FinalResults);
589 FreePool (FinalResults);
590 FinalResults = StrPointer;
591 StrPointer = StrPointer + StrLen (StrPointer);
592 *StrPointer = L'&';
593 StrCpy (StrPointer + 1, SingleResult);
594 FreePool (SingleResult);
595 }
596 } while (Request == NULL);
597
598 if (!EFI_ERROR (Status)) {
599 *Results = FinalResults;
600 } else {
601 if (FinalResults != NULL) {
602 FreePool (FinalResults);
603 }
604 }
605 //
606 // Set Progress string to the original request string.
607 //
608 if (Request == NULL) {
609 *Progress = NULL;
610 } else if (StrStr (Request, L"OFFSET") == NULL) {
611 *Progress = Request + StrLen (Request);
612 }
613
614 return Status;
615 }
616
617 /**
618 This function implement the EFI_HII_CONFIG_ACCESS_PROTOCOL.RouteConfig
619 so that data can be written to the data storage such as UEFI Variable or module's
620 customized storage exposed by EFI_FRAMEWORK_CALLBACK.
621
622 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL
623 @param Configuration A null-terminated Unicode string in <ConfigResp> format.
624 @param Progress A pointer to a string filled in with the offset of the most recent '&' before the first
625 failing name / value pair (or the beginning of the string if the failure is in the first
626 name / value pair) or the terminating NULL if all was successful.
627
628 @retval EFI_INVALID_PARAMETER If there is no Buffer Storage for this Config Access instance.
629 @retval EFI_SUCCESS The setting is saved successfully.
630 @retval !EFI_SUCCESS The error returned by UEFI Set Variable or Framework Form Callback Nvwrite.
631 **/
632 EFI_STATUS
633 EFIAPI
634 ThunkRouteConfig (
635 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
636 IN CONST EFI_STRING Configuration,
637 OUT EFI_STRING *Progress
638 )
639 {
640 EFI_STATUS Status;
641 CONFIG_ACCESS_PRIVATE *ConfigAccess;
642 FORMSET_STORAGE *BufferStorage;
643 VOID *Data;
644 UINTN DataSize;
645 UINTN DataSize2;
646 BOOLEAN ResetRequired;
647 BOOLEAN DataAllocated;
648
649 if (Configuration == NULL) {
650 return EFI_INVALID_PARAMETER;
651 }
652
653 Data = NULL;
654 ConfigAccess = CONFIG_ACCESS_PRIVATE_FROM_PROTOCOL (This);
655
656 BufferStorage = GetStorageFromConfigString (ConfigAccess->ThunkContext->FormSet, Configuration);
657
658 if (BufferStorage == NULL) {
659 *Progress = Configuration;
660 return EFI_NOT_FOUND;
661 }
662
663 DataSize2 = BufferStorage->Size;
664 if (ConfigAccess->ThunkContext->NvMapOverride == NULL) {
665 DataAllocated = TRUE;
666 if (ConfigAccess->FormCallbackProtocol == NULL ||
667 ConfigAccess->FormCallbackProtocol->NvRead == NULL) {
668 Status = GetUefiVariable (
669 BufferStorage,
670 &Data,
671 &DataSize
672 );
673 } else {
674 Status = CallFormCallBack (
675 BufferStorage,
676 ConfigAccess->FormCallbackProtocol,
677 &Data,
678 &DataSize
679 );
680 }
681 } else {
682 //
683 // ConfigToBlock will convert the Config String and update the NvMapOverride accordingly.
684 //
685 Status = EFI_SUCCESS;
686 Data = ConfigAccess->ThunkContext->NvMapOverride;
687 DataSize = DataSize2;
688 DataAllocated = FALSE;
689 }
690 if (EFI_ERROR (Status) || (DataSize2 != DataSize)) {
691 if (Data == NULL) {
692 Data = AllocateZeroPool (DataSize2);
693 }
694 }
695
696 DataSize = DataSize2;
697 Status = mHiiConfigRoutingProtocol->ConfigToBlock (
698 mHiiConfigRoutingProtocol,
699 Configuration,
700 Data,
701 &DataSize,
702 Progress
703 );
704 if (EFI_ERROR (Status)) {
705 goto Done;
706 }
707
708 if (ConfigAccess->ThunkContext->NvMapOverride == NULL) {
709 if (ConfigAccess->FormCallbackProtocol == NULL ||
710 ConfigAccess->FormCallbackProtocol->NvWrite == NULL) {
711 Status = gRT->SetVariable (
712 BufferStorage->Name,
713 &BufferStorage->Guid,
714 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
715 DataSize2,
716 Data
717 );
718 } else {
719 Status = ConfigAccess->FormCallbackProtocol->NvWrite (
720 ConfigAccess->FormCallbackProtocol,
721 BufferStorage->Name,
722 &BufferStorage->Guid,
723 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
724 DataSize2,
725 Data,
726 &ResetRequired
727 );
728 }
729 }
730
731 Done:
732 if (DataAllocated && (Data != NULL)) {
733 FreePool (Data);
734 }
735
736 return Status;
737 }
738
739 /**
740 Build the EFI_IFR_DATA_ARRAY which will be used to pass to
741 EFI_FORM_CALLBACK_PROTOCOL.Callback. Check definition of EFI_IFR_DATA_ARRAY
742 for details.
743
744 ASSERT if the Question Type is not EFI_IFR_TYPE_NUM_SIZE_* or EFI_IFR_TYPE_STRING.
745
746 @param ConfigAccess Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL
747 @param QuestionId The Question ID.
748 @param Type The Question Type.
749 @param Value The Question Value.
750 @param NvMapAllocated On output indicates if a buffer is allocated for NvMap.
751
752 @return A pointer to EFI_IFR_DATA_ARRAY. The caller must free this buffer allocated.
753 **/
754 EFI_IFR_DATA_ARRAY *
755 CreateIfrDataArray (
756 IN CONFIG_ACCESS_PRIVATE *ConfigAccess,
757 IN EFI_QUESTION_ID QuestionId,
758 IN UINT8 Type,
759 IN EFI_IFR_TYPE_VALUE *Value,
760 OUT BOOLEAN *NvMapAllocated
761 )
762 {
763 EFI_IFR_DATA_ARRAY *IfrDataArray;
764 EFI_IFR_DATA_ENTRY *IfrDataEntry;
765 UINTN BrowserDataSize;
766 FORMSET_STORAGE *BufferStorage;
767 UINTN Size;
768 EFI_STRING String;
769 FORM_BROWSER_STATEMENT *Statement;
770
771 *NvMapAllocated = FALSE;
772
773 String = NULL;
774
775 switch (Type) {
776 case EFI_IFR_TYPE_NUM_SIZE_8:
777 case EFI_IFR_TYPE_NUM_SIZE_16:
778 case EFI_IFR_TYPE_NUM_SIZE_32:
779 case EFI_IFR_TYPE_NUM_SIZE_64:
780 case EFI_IFR_TYPE_BOOLEAN:
781 Size = sizeof (*Value);
782 break;
783
784 case EFI_IFR_TYPE_STRING:
785 if (Value->string == 0) {
786 Size = 0;
787 } else {
788 String = HiiGetString (ConfigAccess->ThunkContext->UefiHiiHandle, Value->string, NULL);
789 ASSERT (String != NULL);
790
791 Size = StrSize (String);
792 }
793 break;
794
795 case EFI_IFR_TYPE_ACTION:
796 Size = 0;
797 break;
798
799 default:
800 ASSERT (FALSE);
801 Size = 0;
802 break;
803 }
804
805 IfrDataArray = AllocateZeroPool (sizeof (EFI_IFR_DATA_ARRAY) + sizeof (EFI_IFR_DATA_ENTRY) + Size);
806 ASSERT (IfrDataArray != NULL);
807 IfrDataArray->EntryCount = 1;
808 IfrDataEntry = (EFI_IFR_DATA_ENTRY *) (IfrDataArray + 1);
809
810 Statement = GetStorageFromQuestionId (ConfigAccess->ThunkContext->FormSet, QuestionId);
811
812 if (Statement == NULL || Statement->Storage == NULL) {
813 //
814 // The QuestionId is not associated with a Buffer Storage.
815 // Try to get the first Buffer Storage then.
816 //
817 BufferStorage = GetFirstStorageOfFormSet (ConfigAccess->ThunkContext->FormSet);
818 } else {
819 BufferStorage = Statement->Storage;
820 IfrDataEntry->OpCode = Statement->Operand;
821 }
822
823 if (BufferStorage != NULL) {
824 BrowserDataSize = BufferStorage->Size;
825 IfrDataEntry->Length = (UINT8) (sizeof (EFI_IFR_DATA_ENTRY) + Size);
826
827 if (ConfigAccess->ThunkContext->NvMapOverride == NULL) {
828 *NvMapAllocated = TRUE;
829 IfrDataArray->NvRamMap = AllocateZeroPool (BrowserDataSize);
830 } else {
831 *NvMapAllocated = FALSE;
832 IfrDataArray->NvRamMap = ConfigAccess->ThunkContext->NvMapOverride;
833 }
834
835 ASSERT (HiiGetBrowserData (&BufferStorage->Guid, BufferStorage->Name, BrowserDataSize, (UINT8 *) IfrDataArray->NvRamMap));
836
837 switch (Type) {
838 case EFI_IFR_TYPE_NUM_SIZE_8:
839 case EFI_IFR_TYPE_NUM_SIZE_16:
840 case EFI_IFR_TYPE_NUM_SIZE_32:
841 case EFI_IFR_TYPE_NUM_SIZE_64:
842 case EFI_IFR_TYPE_BOOLEAN:
843 CopyMem (&IfrDataEntry->Data, &(Value->u8), sizeof (*Value));
844 break;
845
846 case EFI_IFR_TYPE_STRING:
847 if (Size != 0) {
848 ASSERT (String != NULL);
849 StrCpy ((CHAR16 *) &IfrDataEntry->Data, String);
850 FreePool (String);
851 }
852 break;
853
854 case EFI_IFR_TYPE_ACTION:
855 break;
856
857 default:
858 ASSERT (FALSE);
859 break;
860 }
861
862 //
863 // Need to fiil in the information for the rest of field for EFI_IFR_DATA_ENTRY.
864 // It seems that no implementation is found to use other fields. Leave them uninitialized for now.
865 //
866 //UINT8 OpCode; // Likely a string, numeric, or one-of
867 //UINT8 Length; // Length of the EFI_IFR_DATA_ENTRY packet
868 //UINT16 Flags; // Flags settings to determine what behavior is desired from the browser after the callback
869 //VOID *Data; // The data in the form based on the op-code type - this is not a pointer to the data, the data follows immediately
870 // If the OpCode is a OneOf or Numeric type - Data is a UINT16 value
871 // If the OpCode is a String type - Data is a CHAR16[x] type
872 // If the OpCode is a Checkbox type - Data is a UINT8 value
873 // If the OpCode is a NV Access type - Data is a FRAMEWORK_EFI_IFR_NV_DATA structure
874 }
875
876 return IfrDataArray;
877 }
878
879 /**
880 If a NvMapOverride is passed in to EFI_FORM_BROWSER_PROTOCOL.SendForm, the Form Browser
881 needs to be informed when data changed in NvMapOverride. This function will invoke
882 SetBrowserData () to set internal data of Form Browser.
883
884 @param ConfigAccess The Config Access Private Context.
885 @param QuestionId The Question Id that invokes the callback.
886
887
888 **/
889 VOID
890 SyncBrowserDataForNvMapOverride (
891 IN CONST CONFIG_ACCESS_PRIVATE *ConfigAccess,
892 IN EFI_QUESTION_ID QuestionId
893 )
894 {
895 FORMSET_STORAGE *BufferStorage;
896 BOOLEAN CheckFlag;
897 UINTN BrowserDataSize;
898 FORM_BROWSER_STATEMENT *Statement;
899
900 if (ConfigAccess->ThunkContext->NvMapOverride != NULL) {
901
902 Statement = GetStorageFromQuestionId (ConfigAccess->ThunkContext->FormSet, QuestionId);
903
904 if (Statement == NULL || Statement->Storage == NULL) {
905 //
906 // QuestionId is a statement without Storage.
907 // 1) It is a Goto.
908 //
909 //
910 BufferStorage = GetFirstStorageOfFormSet (ConfigAccess->ThunkContext->FormSet);
911 } else {
912 BufferStorage = Statement->Storage;
913 }
914
915 //
916 // If NvMapOverride is not NULL, this Form must have at least one Buffer Type Variable Storage.
917 //
918 ASSERT (BufferStorage != NULL);
919
920 BrowserDataSize = BufferStorage->Size;
921
922 CheckFlag = HiiSetBrowserData (&BufferStorage->Guid, BufferStorage->Name, BrowserDataSize, ConfigAccess->ThunkContext->NvMapOverride, NULL);
923 ASSERT (CheckFlag);
924 }
925
926 }
927
928 /**
929 Free up resource allocated for a EFI_IFR_DATA_ARRAY by CreateIfrDataArray ().
930
931 @param Array The EFI_IFR_DATA_ARRAY allocated.
932 @param NvMapAllocated If the NvRamMap is allocated for EFI_IFR_DATA_ARRAY.
933
934 **/
935 VOID
936 DestroyIfrDataArray (
937 IN EFI_IFR_DATA_ARRAY *Array,
938 IN BOOLEAN NvMapAllocated
939 )
940 {
941 if (Array != NULL) {
942 if (NvMapAllocated) {
943 FreePool (Array->NvRamMap);
944 }
945
946 FreePool (Array);
947 }
948 }
949
950 /**
951 Get the ONE_OF_OPTION_MAP_ENTRY for a QuestionId that invokes the
952 EFI_FORM_CALLBACK_PROTOCOL.Callback. The information is needed as
953 the callback mechanism for EFI_IFR_ONE_OF_OPTION is changed from
954 EFI_IFR_ONE_OF_OPTION in Framework IFR. Check EFI_IFR_GUID_OPTIONKEY
955 for detailed information.
956
957 @param ThunkContext The Thunk Context.
958 @param QuestionId The Question Id.
959 @param Type The Question Type.
960 @param Value The One Of Option's value.
961
962 @return The ONE_OF_OPTION_MAP_ENTRY found.
963 @retval NULL If no entry is found.
964 **/
965 ONE_OF_OPTION_MAP_ENTRY *
966 GetOneOfOptionMapEntry (
967 IN HII_THUNK_CONTEXT *ThunkContext,
968 IN EFI_QUESTION_ID QuestionId,
969 IN UINT8 Type,
970 IN EFI_IFR_TYPE_VALUE *Value
971 )
972 {
973 LIST_ENTRY *Link;
974 LIST_ENTRY *Link2;
975 ONE_OF_OPTION_MAP_ENTRY *OneOfOptionMapEntry;
976 ONE_OF_OPTION_MAP *OneOfOptionMap;
977 FORM_BROWSER_FORMSET *FormSet;
978
979 FormSet = ThunkContext->FormSet;
980
981 Link = GetFirstNode (&FormSet->OneOfOptionMapListHead);
982
983 while (!IsNull (&FormSet->OneOfOptionMapListHead, Link)) {
984 OneOfOptionMap = ONE_OF_OPTION_MAP_FROM_LINK(Link);
985 if (OneOfOptionMap->QuestionId == QuestionId) {
986 ASSERT (OneOfOptionMap->ValueType == Type);
987
988 Link2 = GetFirstNode (&OneOfOptionMap->OneOfOptionMapEntryListHead);
989
990 while (!IsNull (&OneOfOptionMap->OneOfOptionMapEntryListHead, Link2)) {
991 OneOfOptionMapEntry = ONE_OF_OPTION_MAP_ENTRY_FROM_LINK (Link2);
992
993 if (CompareMem (Value, &OneOfOptionMapEntry->Value, sizeof (EFI_IFR_TYPE_VALUE)) == 0) {
994 return OneOfOptionMapEntry;
995 }
996
997 Link2 = GetNextNode (&OneOfOptionMap->OneOfOptionMapEntryListHead, Link2);
998 }
999 }
1000
1001 Link = GetNextNode (&FormSet->OneOfOptionMapListHead, Link);
1002 }
1003
1004
1005 return NULL;
1006 }
1007
1008 /**
1009 Functions which are registered to receive notification of
1010 database events have this prototype. The actual event is encoded
1011 in NotifyType. The following table describes how PackageType,
1012 PackageGuid, Handle, and Package are used for each of the
1013 notification types.
1014
1015 If any Pakcage List in database is updated, mHiiPackageListUpdated
1016 will be set. If mHiiPackageListUpdated is set, Framework ThunkCallback()
1017 will force the UEFI Setup Browser to save the uncommitted data. This
1018 is needed as Framework's Callback function may dynamically update
1019 opcode in a Package List. UEFI Setup Browser will quit itself and reparse
1020 the Package List's IFR and display it. UEFI Config Access's implementation
1021 is required to save the modified (SetBrowserData or directly save the data
1022 to NV storage). But Framework HII Modules is not aware of this rule. Therefore,
1023 we will enforce the rule in ThunkCallback (). The side effect of force saving
1024 of NV data is the NV flag in browser may not flag a update as data has already
1025 been saved to NV storage.
1026
1027 @param PackageType Package type of the notification.
1028
1029 @param PackageGuid If PackageType is
1030 EFI_HII_PACKAGE_TYPE_GUID, then this is
1031 the pointer to the GUID from the Guid
1032 field of EFI_HII_PACKAGE_GUID_HEADER.
1033 Otherwise, it must be NULL.
1034
1035 @param Package Points to the package referred to by the
1036 notification Handle The handle of the package
1037 list which contains the specified package.
1038
1039 @param Handle The HII handle.
1040
1041 @param NotifyType The type of change concerning the
1042 database. See
1043 EFI_HII_DATABASE_NOTIFY_TYPE.
1044
1045 **/
1046 EFI_STATUS
1047 EFIAPI
1048 FormUpdateNotify (
1049 IN UINT8 PackageType,
1050 IN CONST EFI_GUID *PackageGuid,
1051 IN CONST EFI_HII_PACKAGE_HEADER *Package,
1052 IN EFI_HII_HANDLE Handle,
1053 IN EFI_HII_DATABASE_NOTIFY_TYPE NotifyType
1054 )
1055 {
1056 mHiiPackageListUpdated = TRUE;
1057
1058 return EFI_SUCCESS;
1059 }
1060
1061 /**
1062 Wrap the EFI_HII_CONFIG_ACCESS_PROTOCOL.CallBack to EFI_FORM_CALLBACK_PROTOCOL.Callback. Therefor,
1063 the framework HII module willl do no porting and work with a UEFI HII SetupBrowser.
1064
1065 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
1066 @param Action Specifies the type of action taken by the browser. See EFI_BROWSER_ACTION_x.
1067 @param QuestionId A unique value which is sent to the original exporting driver so that it can identify the
1068 type of data to expect. The format of the data tends to vary based on the opcode that
1069 generated the callback.
1070 @param Type The type of value for the question. See EFI_IFR_TYPE_x in
1071 EFI_IFR_ONE_OF_OPTION.
1072 @param Value A pointer to the data being sent to the original exporting driver. The type is specified
1073 by Type. Type EFI_IFR_TYPE_VALUE is defined in
1074 EFI_IFR_ONE_OF_OPTION.
1075 @param ActionRequest On return, points to the action requested by the callback function. Type
1076 EFI_BROWSER_ACTION_REQUEST is specified in SendForm() in the Form
1077 Browser Protocol.
1078
1079 @retval EFI_UNSUPPORTED If the Framework HII module does not register Callback although it specify the opcode under
1080 focuse to be INTERRACTIVE.
1081 @retval EFI_SUCCESS The callback complete successfully.
1082 @retval !EFI_SUCCESS The error code returned by EFI_FORM_CALLBACK_PROTOCOL.Callback.
1083
1084 **/
1085 EFI_STATUS
1086 EFIAPI
1087 ThunkCallback (
1088 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
1089 IN EFI_BROWSER_ACTION Action,
1090 IN EFI_QUESTION_ID QuestionId,
1091 IN UINT8 Type,
1092 IN EFI_IFR_TYPE_VALUE *Value,
1093 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
1094 )
1095 {
1096 EFI_STATUS Status;
1097 CONFIG_ACCESS_PRIVATE *ConfigAccess;
1098 EFI_FORM_CALLBACK_PROTOCOL *FormCallbackProtocol;
1099 EFI_HII_CALLBACK_PACKET *Packet;
1100 EFI_IFR_DATA_ARRAY *Data;
1101 EFI_IFR_DATA_ENTRY *DataEntry;
1102 UINT16 KeyValue;
1103 ONE_OF_OPTION_MAP_ENTRY *OneOfOptionMapEntry;
1104 EFI_HANDLE NotifyHandle;
1105 EFI_INPUT_KEY Key;
1106 BOOLEAN NvMapAllocated;
1107
1108 if ((Action == EFI_BROWSER_ACTION_FORM_OPEN) || (Action == EFI_BROWSER_ACTION_FORM_CLOSE)) {
1109 //
1110 // Ignore UEFI OPEN/CLOSE Action for FrameworkCallback
1111 //
1112 return EFI_SUCCESS;
1113 }
1114
1115 ASSERT (This != NULL);
1116 ASSERT (Value != NULL);
1117 ASSERT (ActionRequest != NULL);
1118
1119 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_NONE;
1120
1121 ConfigAccess = CONFIG_ACCESS_PRIVATE_FROM_PROTOCOL (This);
1122
1123 FormCallbackProtocol = ConfigAccess->FormCallbackProtocol;
1124 if (FormCallbackProtocol == NULL) {
1125 ASSERT (FALSE);
1126 return EFI_UNSUPPORTED;
1127 }
1128
1129 //
1130 // Check if the QuestionId match a OneOfOption.
1131 //
1132 OneOfOptionMapEntry = GetOneOfOptionMapEntry (ConfigAccess->ThunkContext, QuestionId, Type, Value);
1133
1134 if (OneOfOptionMapEntry == NULL) {
1135 //
1136 // This is not a One-Of-Option opcode. QuestionId is the KeyValue
1137 //
1138 KeyValue = QuestionId;
1139 } else {
1140 //
1141 // Otherwise, use the original Key specified in One Of Option in the Framework VFR syntax.
1142 //
1143 KeyValue = OneOfOptionMapEntry->FwKey;
1144 }
1145
1146 //
1147 // Build the EFI_IFR_DATA_ARRAY
1148 //
1149 Data = CreateIfrDataArray (ConfigAccess, QuestionId, Type, Value, &NvMapAllocated);
1150
1151 Status = mHiiDatabase->RegisterPackageNotify (
1152 mHiiDatabase,
1153 EFI_HII_PACKAGE_FORMS,
1154 NULL,
1155 FormUpdateNotify,
1156 EFI_HII_DATABASE_NOTIFY_REMOVE_PACK,
1157 &NotifyHandle
1158 );
1159 //
1160 //Call the Framework Callback function.
1161 //
1162 Packet = NULL;
1163 Status = FormCallbackProtocol->Callback (
1164 FormCallbackProtocol,
1165 KeyValue,
1166 Data,
1167 &Packet
1168 );
1169 SyncBrowserDataForNvMapOverride (ConfigAccess, QuestionId);
1170
1171 //
1172 // Callback require browser to perform action
1173 //
1174 if (EFI_ERROR (Status)) {
1175 if (Packet != NULL) {
1176 do {
1177 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, Packet->String, NULL);
1178 } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);
1179 }
1180 //
1181 // Error Code in Status is discarded.
1182 //
1183 } else {
1184 if (Packet != NULL) {
1185 if (Packet->DataArray.EntryCount == 1 && Packet->DataArray.NvRamMap == NULL) {
1186 DataEntry = (EFI_IFR_DATA_ENTRY *) ((UINT8 *) Packet + sizeof (EFI_IFR_DATA_ARRAY));
1187 if ((DataEntry->Flags & EXIT_REQUIRED) == EXIT_REQUIRED) {
1188 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;
1189 }
1190
1191 if ((DataEntry->Flags & SAVE_REQUIRED) == SAVE_REQUIRED) {
1192 Status = ConfigAccess->ConfigAccessProtocol.RouteConfig (
1193 &ConfigAccess->ConfigAccessProtocol,
1194 NULL,
1195 NULL
1196 );
1197 }
1198 }
1199 FreePool (Packet);
1200 }
1201 }
1202
1203 //
1204 // Unregister notify for Form package update
1205 //
1206 Status = mHiiDatabase->UnregisterPackageNotify (
1207 mHiiDatabase,
1208 NotifyHandle
1209 );
1210 //
1211 // UEFI SetupBrowser behaves differently with Framework SetupBrowser when call back function
1212 // update any forms in HII database. UEFI SetupBrowser will re-parse the displaying form package and load
1213 // the values from variable storages. Framework SetupBrowser will only re-parse the displaying form packages.
1214 // To make sure customer's previous changes is saved and the changing question behaves as expected, we
1215 // issue a EFI_BROWSER_ACTION_REQUEST_SUBMIT to ask UEFI SetupBrowser to save the changes proceed to re-parse
1216 // the form and load all the variable storages.
1217 //
1218 if (*ActionRequest == EFI_BROWSER_ACTION_REQUEST_NONE && mHiiPackageListUpdated) {
1219 mHiiPackageListUpdated= FALSE;
1220 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;
1221 } else {
1222 if (ConfigAccess->ThunkContext->FormSet->SubClass == EFI_FRONT_PAGE_SUBCLASS ||
1223 ConfigAccess->ThunkContext->FormSet->SubClass == EFI_SINGLE_USE_SUBCLASS) {
1224 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;
1225 }
1226 }
1227
1228
1229 //
1230 // Clean up.
1231 //
1232 DestroyIfrDataArray (Data, NvMapAllocated);
1233
1234 return Status;
1235 }
1236