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