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