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