]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Compatibility/FrameworkHiiToUefiHiiThunk/ConfigAccess.c
clean up for IPF ICC tool chain.
[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 Data = NULL;
407 ConfigAccess = CONFIG_ACCESS_PRIVATE_FROM_PROTOCOL (This);
408
409 BufferStorage = GetStorageFromConfigString (ConfigAccess->ThunkContext->FormSet, Request);
410
411 if (ConfigAccess->ThunkContext->NvMapOverride == NULL) {
412 //
413 // NvMapOverride is not used. Get the Storage data from EFI Variable or Framework Form Callback.
414 //
415 if (ConfigAccess->FormCallbackProtocol == NULL ||
416 ConfigAccess->FormCallbackProtocol->NvRead == NULL) {
417 Status = GetUefiVariable (
418 BufferStorage,
419 &Data,
420 &DataSize
421 );
422 } else {
423 Status = CallFormCallBack (
424 BufferStorage,
425 ConfigAccess->FormCallbackProtocol,
426 &Data,
427 &DataSize
428 );
429 }
430 } else {
431 //
432 // Use the NvMapOverride.
433 //
434 DataSize = BufferStorage->Size;
435 Data = AllocateCopyPool (DataSize, ConfigAccess->ThunkContext->NvMapOverride);
436
437 if (Data != NULL) {
438 Status = EFI_SUCCESS;
439 } else {
440 Status = EFI_OUT_OF_RESOURCES;
441 }
442 }
443
444 if (!EFI_ERROR (Status)) {
445 Status = mHiiConfigRoutingProtocol->BlockToConfig (
446 mHiiConfigRoutingProtocol,
447 Request,
448 Data,
449 DataSize,
450 Results,
451 Progress
452 );
453 }
454
455 if (Data != NULL) {
456 FreePool (Data);
457 }
458 return Status;
459 }
460
461 /**
462 This function implement the EFI_HII_CONFIG_ACCESS_PROTOCOL.RouteConfig
463 so that data can be written to the data storage such as UEFI Variable or module's
464 customized storage exposed by EFI_FRAMEWORK_CALLBACK.
465
466 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL
467 @param Configuration A null-terminated Unicode string in <ConfigResp> format.
468 @param Progress A pointer to a string filled in with the offset of the most recent '&' before the first
469 failing name / value pair (or the beginning of the string if the failure is in the first
470 name / value pair) or the terminating NULL if all was successful.
471
472 @retval EFI_INVALID_PARAMETER If there is no Buffer Storage for this Config Access instance.
473 @retval EFI_SUCCESS The setting is saved successfully.
474 @retval !EFI_SUCCESS The error returned by UEFI Set Variable or Framework Form Callback Nvwrite.
475 **/
476 EFI_STATUS
477 EFIAPI
478 ThunkRouteConfig (
479 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
480 IN CONST EFI_STRING Configuration,
481 OUT EFI_STRING *Progress
482 )
483 {
484 EFI_STATUS Status;
485 CONFIG_ACCESS_PRIVATE *ConfigAccess;
486 FORMSET_STORAGE *BufferStorage;
487 VOID *Data;
488 UINTN DataSize;
489 UINTN DataSize2;
490 UINTN LastModifiedByteIndex;
491 BOOLEAN ResetRequired;
492 BOOLEAN DataAllocated;
493
494 Data = NULL;
495 ConfigAccess = CONFIG_ACCESS_PRIVATE_FROM_PROTOCOL (This);
496
497 BufferStorage = GetStorageFromConfigString (ConfigAccess->ThunkContext->FormSet, Configuration);
498
499 DataSize2 = BufferStorage->Size;
500 if (ConfigAccess->ThunkContext->NvMapOverride == NULL) {
501 DataAllocated = TRUE;
502 if (ConfigAccess->FormCallbackProtocol == NULL ||
503 ConfigAccess->FormCallbackProtocol->NvRead == NULL) {
504 Status = GetUefiVariable (
505 BufferStorage,
506 &Data,
507 &DataSize
508 );
509 } else {
510 Status = CallFormCallBack (
511 BufferStorage,
512 ConfigAccess->FormCallbackProtocol,
513 &Data,
514 &DataSize
515 );
516 }
517 } else {
518 //
519 // ConfigToBlock will convert the Config String and update the NvMapOverride accordingly.
520 //
521 Status = EFI_SUCCESS;
522 Data = ConfigAccess->ThunkContext->NvMapOverride;
523 DataSize = DataSize2;
524 DataAllocated = FALSE;
525 }
526 if (EFI_ERROR (Status) || (DataSize2 != DataSize)) {
527 if (Data == NULL) {
528 Data = AllocateZeroPool (DataSize2);
529 }
530 }
531
532 Status = mHiiConfigRoutingProtocol->ConfigToBlock (
533 mHiiConfigRoutingProtocol,
534 Configuration,
535 Data,
536 &LastModifiedByteIndex,
537 Progress
538 );
539 if (EFI_ERROR (Status)) {
540 goto Done;
541 }
542
543 if (ConfigAccess->ThunkContext->NvMapOverride == NULL) {
544 if (ConfigAccess->FormCallbackProtocol == NULL ||
545 ConfigAccess->FormCallbackProtocol->NvWrite == NULL) {
546 Status = gRT->SetVariable (
547 BufferStorage->Name,
548 &BufferStorage->Guid,
549 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
550 DataSize2,
551 Data
552 );
553 } else {
554 Status = ConfigAccess->FormCallbackProtocol->NvWrite (
555 ConfigAccess->FormCallbackProtocol,
556 BufferStorage->Name,
557 &BufferStorage->Guid,
558 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
559 DataSize2,
560 Data,
561 &ResetRequired
562 );
563
564 }
565 }
566
567 Done:
568 if (DataAllocated && (Data != NULL)) {
569 FreePool (Data);
570 }
571
572 return Status;
573 }
574
575 /**
576 Build the FRAMEWORK_EFI_IFR_DATA_ARRAY which will be used to pass to
577 EFI_FORM_CALLBACK_PROTOCOL.Callback. Check definition of FRAMEWORK_EFI_IFR_DATA_ARRAY
578 for details.
579
580 ASSERT if the Question Type is not EFI_IFR_TYPE_NUM_SIZE_* or EFI_IFR_TYPE_STRING.
581
582 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL
583 @param QuestionId The Question ID.
584 @param Type The Question Type.
585 @param Value The Question Value.
586 @param NvMapAllocated On output indicates if a buffer is allocated for NvMap.
587
588 @return A pointer to FRAMEWORK_EFI_IFR_DATA_ARRAY. The caller must free this buffer allocated.
589 **/
590 FRAMEWORK_EFI_IFR_DATA_ARRAY *
591 CreateIfrDataArray (
592 IN CONFIG_ACCESS_PRIVATE *ConfigAccess,
593 IN EFI_QUESTION_ID QuestionId,
594 IN UINT8 Type,
595 IN EFI_IFR_TYPE_VALUE *Value,
596 OUT BOOLEAN *NvMapAllocated
597 )
598 {
599 FRAMEWORK_EFI_IFR_DATA_ARRAY *IfrDataArray;
600 FRAMEWORK_EFI_IFR_DATA_ENTRY *IfrDataEntry;
601 UINTN BrowserDataSize;
602 FORMSET_STORAGE *BufferStorage;
603 EFI_STATUS Status;
604 UINTN Size;
605 UINTN StringSize;
606 EFI_STRING String;
607
608 *NvMapAllocated = FALSE;
609
610 String = NULL;
611
612 switch (Type) {
613 case EFI_IFR_TYPE_NUM_SIZE_8:
614 case EFI_IFR_TYPE_NUM_SIZE_16:
615 case EFI_IFR_TYPE_NUM_SIZE_32:
616 case EFI_IFR_TYPE_NUM_SIZE_64:
617 case EFI_IFR_TYPE_BOOLEAN:
618 Size = sizeof (*Value);
619 break;
620
621 case EFI_IFR_TYPE_STRING:
622 StringSize = 0;
623 Status = HiiLibGetString (ConfigAccess->ThunkContext->UefiHiiHandle, Value->string, String, &StringSize);
624 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
625
626 String = AllocateZeroPool (StringSize);
627 ASSERT (String != NULL);
628
629 Status = HiiLibGetString (ConfigAccess->ThunkContext->UefiHiiHandle, Value->string, String, &StringSize);
630 ASSERT_EFI_ERROR (Status);
631
632 Size = StringSize;
633 break;
634
635 default:
636 ASSERT (FALSE);
637 Size = 0;
638 break;
639 }
640
641 IfrDataArray = AllocateZeroPool (sizeof (FRAMEWORK_EFI_IFR_DATA_ARRAY) + sizeof (FRAMEWORK_EFI_IFR_DATA_ENTRY) + Size);
642 ASSERT (IfrDataArray != NULL);
643
644 BufferStorage = GetStorageFromQuestionId (ConfigAccess->ThunkContext->FormSet, QuestionId);
645
646 if (BufferStorage == NULL) {
647 //
648 // The QuestionId is not associated with a Buffer Storage.
649 // Try to get the first Buffer Storage then.
650 //
651 BufferStorage = GetFirstStorageOfFormSet (ConfigAccess->ThunkContext->FormSet);
652 }
653
654 if (BufferStorage != NULL) {
655 BrowserDataSize = BufferStorage->Size;
656
657 if (ConfigAccess->ThunkContext->NvMapOverride == NULL) {
658 *NvMapAllocated = TRUE;
659 IfrDataArray->NvRamMap = AllocateZeroPool (BrowserDataSize);
660 } else {
661 *NvMapAllocated = FALSE;
662 IfrDataArray->NvRamMap = ConfigAccess->ThunkContext->NvMapOverride;
663 }
664
665 Status = GetBrowserData (&BufferStorage->Guid, BufferStorage->Name, &BrowserDataSize, IfrDataArray->NvRamMap);
666 ASSERT_EFI_ERROR (Status);
667
668 IfrDataEntry = (FRAMEWORK_EFI_IFR_DATA_ENTRY *) (IfrDataArray + 1);
669
670 switch (Type) {
671 case EFI_IFR_TYPE_NUM_SIZE_8:
672 case EFI_IFR_TYPE_NUM_SIZE_16:
673 case EFI_IFR_TYPE_NUM_SIZE_32:
674 case EFI_IFR_TYPE_NUM_SIZE_64:
675 case EFI_IFR_TYPE_BOOLEAN:
676 CopyMem (&IfrDataEntry->Data, &(Value->u8), sizeof (*Value));
677 break;
678
679 case EFI_IFR_TYPE_STRING:
680 ASSERT (String != NULL);
681 StrCpy ((CHAR16 *) &IfrDataEntry->Data, String);
682 FreePool (String);
683 break;
684 default:
685 ASSERT (FALSE);
686 break;
687 }
688
689 //
690 // Need to fiil in the information for the rest of field for FRAMEWORK_EFI_IFR_DATA_ENTRY.
691 // It seems that no implementation is found to use other fields. Leave them uninitialized for now.
692 //
693 //UINT8 OpCode; // Likely a string, numeric, or one-of
694 //UINT8 Length; // Length of the FRAMEWORK_EFI_IFR_DATA_ENTRY packet
695 //UINT16 Flags; // Flags settings to determine what behavior is desired from the browser after the callback
696 //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
697 // If the OpCode is a OneOf or Numeric type - Data is a UINT16 value
698 // If the OpCode is a String type - Data is a CHAR16[x] type
699 // If the OpCode is a Checkbox type - Data is a UINT8 value
700 // If the OpCode is a NV Access type - Data is a FRAMEWORK_EFI_IFR_NV_DATA structure
701 }
702
703 return IfrDataArray;
704 }
705
706 /**
707 If a NvMapOverride is passed in to EFI_FORM_BROWSER_PROTOCOL.SendForm, the Form Browser
708 needs to be informed when data changed in NvMapOverride. This function will invoke
709 SetBrowserData () to set internal data of Form Browser.
710
711 @param ConfigAccess The Config Access Private Context.
712 @param QuestionId The Question Id that invokes the callback.
713
714
715 **/
716 VOID
717 SyncBrowserDataForNvMapOverride (
718 IN CONST CONFIG_ACCESS_PRIVATE *ConfigAccess,
719 IN EFI_QUESTION_ID QuestionId
720 )
721 {
722 FORMSET_STORAGE *BufferStorage;
723 EFI_STATUS Status;
724 UINTN BrowserDataSize;
725
726 if (ConfigAccess->ThunkContext->NvMapOverride != NULL) {
727
728 BufferStorage = GetStorageFromQuestionId (ConfigAccess->ThunkContext->FormSet, QuestionId);
729
730 if (BufferStorage == NULL) {
731 //
732 // QuestionId is a statement without Storage.
733 // 1) It is a Goto.
734 //
735 //
736 BufferStorage = GetFirstStorageOfFormSet (ConfigAccess->ThunkContext->FormSet);
737 }
738
739 //
740 // If NvMapOverride is not NULL, this Form must have at least one Buffer Type Variable Storage.
741 //
742 ASSERT (BufferStorage != NULL);
743
744 BrowserDataSize = BufferStorage->Size;
745
746 Status = SetBrowserData (&BufferStorage->Guid, BufferStorage->Name, BrowserDataSize, ConfigAccess->ThunkContext->NvMapOverride, NULL);
747 ASSERT_EFI_ERROR (Status);
748 }
749
750 }
751
752 /**
753 Free up resource allocated for a FRAMEWORK_EFI_IFR_DATA_ARRAY by CreateIfrDataArray ().
754
755 @param Array The FRAMEWORK_EFI_IFR_DATA_ARRAY allocated.
756 @param NvMapAllocated If the NvRamMap is allocated for FRAMEWORK_EFI_IFR_DATA_ARRAY.
757
758 **/
759 VOID
760 DestroyIfrDataArray (
761 IN FRAMEWORK_EFI_IFR_DATA_ARRAY *Array,
762 IN BOOLEAN NvMapAllocated
763 )
764 {
765 if (Array != NULL) {
766 if (NvMapAllocated) {
767 FreePool (Array->NvRamMap);
768 }
769
770 FreePool (Array);
771 }
772 }
773
774 /**
775 Get the ONE_OF_OPTION_MAP_ENTRY for a QuestionId that invokes the
776 EFI_FORM_CALLBACK_PROTOCOL.Callback. The information is needed as
777 the callback mechanism for EFI_IFR_ONE_OF_OPTION is changed from
778 EFI_IFR_ONE_OF_OPTION in Framework IFR. Check EFI_IFR_GUID_OPTIONKEY
779 for detailed information.
780
781 @param ThunkContext The Thunk Context.
782 @param QuestionId The Question Id.
783 @param Type The Question Type.
784 @param Value The One Of Option's value.
785
786 @return The ONE_OF_OPTION_MAP_ENTRY found.
787 @retval NULL If no entry is found.
788 **/
789 ONE_OF_OPTION_MAP_ENTRY *
790 GetOneOfOptionMapEntry (
791 IN HII_THUNK_CONTEXT *ThunkContext,
792 IN EFI_QUESTION_ID QuestionId,
793 IN UINT8 Type,
794 IN EFI_IFR_TYPE_VALUE *Value
795 )
796 {
797 LIST_ENTRY *Link;
798 LIST_ENTRY *Link2;
799 ONE_OF_OPTION_MAP_ENTRY *OneOfOptionMapEntry;
800 ONE_OF_OPTION_MAP *OneOfOptionMap;
801 FORM_BROWSER_FORMSET *FormSet;
802
803 FormSet = ThunkContext->FormSet;
804
805 Link = GetFirstNode (&FormSet->OneOfOptionMapListHead);
806
807 while (!IsNull (&FormSet->OneOfOptionMapListHead, Link)) {
808 OneOfOptionMap = ONE_OF_OPTION_MAP_FROM_LINK(Link);
809 if (OneOfOptionMap->QuestionId == QuestionId) {
810 ASSERT (OneOfOptionMap->ValueType == Type);
811
812 Link2 = GetFirstNode (&OneOfOptionMap->OneOfOptionMapEntryListHead);
813
814 while (!IsNull (&OneOfOptionMap->OneOfOptionMapEntryListHead, Link2)) {
815 OneOfOptionMapEntry = ONE_OF_OPTION_MAP_ENTRY_FROM_LINK (Link2);
816
817 if (CompareMem (Value, &OneOfOptionMapEntry->Value, sizeof (EFI_IFR_TYPE_VALUE)) == 0) {
818 return OneOfOptionMapEntry;
819 }
820
821 Link2 = GetNextNode (&OneOfOptionMap->OneOfOptionMapEntryListHead, Link2);
822 }
823 }
824
825 Link = GetNextNode (&FormSet->OneOfOptionMapListHead, Link);
826 }
827
828
829 return NULL;
830 }
831
832 /**
833 Functions which are registered to receive notification of
834 database events have this prototype. The actual event is encoded
835 in NotifyType. The following table describes how PackageType,
836 PackageGuid, Handle, and Package are used for each of the
837 notification types.
838
839 If any Pakcage List in database is updated, mHiiPackageListUpdated
840 will be set. If mHiiPackageListUpdated is set, Framework ThunkCallback()
841 will force the UEFI Setup Browser to save the uncommitted data. This
842 is needed as Framework's Callback function may dynamically update
843 opcode in a Package List. UEFI Setup Browser will quit itself and reparse
844 the Package List's IFR and display it. UEFI Config Access's implementation
845 is required to save the modified (SetBrowserData or directly save the data
846 to NV storage). But Framework HII Modules is not aware of this rule. Therefore,
847 we will enforce the rule in ThunkCallback (). The side effect of force saving
848 of NV data is the NV flag in browser may not flag a update as data has already
849 been saved to NV storage.
850
851 @param PackageType Package type of the notification.
852
853 @param PackageGuid If PackageType is
854 EFI_HII_PACKAGE_TYPE_GUID, then this is
855 the pointer to the GUID from the Guid
856 field of EFI_HII_PACKAGE_GUID_HEADER.
857 Otherwise, it must be NULL.
858
859 @param Package Points to the package referred to by the
860 notification Handle The handle of the package
861 list which contains the specified package.
862
863 @param Handle The HII handle.
864
865 @param NotifyType The type of change concerning the
866 database. See
867 EFI_HII_DATABASE_NOTIFY_TYPE.
868
869 **/
870 EFI_STATUS
871 EFIAPI
872 FormUpdateNotify (
873 IN UINT8 PackageType,
874 IN CONST EFI_GUID *PackageGuid,
875 IN CONST EFI_HII_PACKAGE_HEADER *Package,
876 IN EFI_HII_HANDLE Handle,
877 IN EFI_HII_DATABASE_NOTIFY_TYPE NotifyType
878 )
879 {
880 mHiiPackageListUpdated = TRUE;
881
882 return EFI_SUCCESS;
883 }
884
885 /**
886 Wrap the EFI_HII_CONFIG_ACCESS_PROTOCOL.CallBack to EFI_FORM_CALLBACK_PROTOCOL.Callback. Therefor,
887 the framework HII module willl do no porting and work with a UEFI HII SetupBrowser.
888
889 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
890 @param Action Specifies the type of action taken by the browser. See EFI_BROWSER_ACTION_x.
891 @param QuestionId A unique value which is sent to the original exporting driver so that it can identify the
892 type of data to expect. The format of the data tends to vary based on the opcode that
893 generated the callback.
894 @param Type The type of value for the question. See EFI_IFR_TYPE_x in
895 EFI_IFR_ONE_OF_OPTION.
896 @param Value A pointer to the data being sent to the original exporting driver. The type is specified
897 by Type. Type EFI_IFR_TYPE_VALUE is defined in
898 EFI_IFR_ONE_OF_OPTION.
899 @param ActionRequest On return, points to the action requested by the callback function. Type
900 EFI_BROWSER_ACTION_REQUEST is specified in SendForm() in the Form
901 Browser Protocol.
902
903 @retval EFI_UNSUPPORTED If the Framework HII module does not register Callback although it specify the opcode under
904 focuse to be INTERRACTIVE.
905 @retval EFI_SUCCESS The callback complete successfully.
906 @retval !EFI_SUCCESS The error code returned by EFI_FORM_CALLBACK_PROTOCOL.Callback.
907
908 **/
909 EFI_STATUS
910 EFIAPI
911 ThunkCallback (
912 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
913 IN EFI_BROWSER_ACTION Action,
914 IN EFI_QUESTION_ID QuestionId,
915 IN UINT8 Type,
916 IN EFI_IFR_TYPE_VALUE *Value,
917 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
918 )
919 {
920 EFI_STATUS Status;
921 CONFIG_ACCESS_PRIVATE *ConfigAccess;
922 EFI_FORM_CALLBACK_PROTOCOL *FormCallbackProtocol;
923 EFI_HII_CALLBACK_PACKET *Packet;
924 FRAMEWORK_EFI_IFR_DATA_ARRAY *Data;
925 FRAMEWORK_EFI_IFR_DATA_ENTRY *DataEntry;
926 UINT16 KeyValue;
927 ONE_OF_OPTION_MAP_ENTRY *OneOfOptionMapEntry;
928 EFI_HANDLE NotifyHandle;
929 EFI_INPUT_KEY Key;
930 BOOLEAN NvMapAllocated;
931
932 ASSERT (This != NULL);
933 ASSERT (Value != NULL);
934 ASSERT (ActionRequest != NULL);
935
936 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_NONE;
937
938 ConfigAccess = CONFIG_ACCESS_PRIVATE_FROM_PROTOCOL (This);
939
940 FormCallbackProtocol = ConfigAccess->FormCallbackProtocol;
941 if (FormCallbackProtocol == NULL) {
942 ASSERT (FALSE);
943 return EFI_UNSUPPORTED;
944 }
945
946 //
947 // Check if the QuestionId match a OneOfOption.
948 //
949 OneOfOptionMapEntry = GetOneOfOptionMapEntry (ConfigAccess->ThunkContext, QuestionId, Type, Value);
950
951 if (OneOfOptionMapEntry == NULL) {
952 //
953 // This is not a One-Of-Option opcode. QuestionId is the KeyValue
954 //
955 KeyValue = QuestionId;
956 } else {
957 //
958 // Otherwise, use the original Key specified in One Of Option in the Framework VFR syntax.
959 //
960 KeyValue = OneOfOptionMapEntry->FwKey;
961 }
962
963 //
964 // Build the FRAMEWORK_EFI_IFR_DATA_ARRAY
965 //
966 Data = CreateIfrDataArray (ConfigAccess, QuestionId, Type, Value, &NvMapAllocated);
967
968 Status = mHiiDatabase->RegisterPackageNotify (
969 mHiiDatabase,
970 EFI_HII_PACKAGE_FORMS,
971 NULL,
972 FormUpdateNotify,
973 EFI_HII_DATABASE_NOTIFY_REMOVE_PACK,
974 &NotifyHandle
975 );
976 //
977 //Call the Framework Callback function.
978 //
979 Packet = NULL;
980 Status = FormCallbackProtocol->Callback (
981 FormCallbackProtocol,
982 KeyValue,
983 Data,
984 &Packet
985 );
986 SyncBrowserDataForNvMapOverride (ConfigAccess, QuestionId);
987
988 //
989 // Callback require browser to perform action
990 //
991 if (EFI_ERROR (Status)) {
992 if (Packet != NULL) {
993 do {
994 IfrLibCreatePopUp (1, &Key, Packet->String);
995 } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);
996 }
997 //
998 // Error Code in Status is discarded.
999 //
1000 } else {
1001 if (Packet != NULL) {
1002 if (Packet->DataArray.EntryCount == 1 && Packet->DataArray.NvRamMap == NULL) {
1003 DataEntry = (FRAMEWORK_EFI_IFR_DATA_ENTRY *) ((UINT8 *) Packet + sizeof (FRAMEWORK_EFI_IFR_DATA_ARRAY));
1004 if ((DataEntry->Flags & EXIT_REQUIRED) == EXIT_REQUIRED) {
1005 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;
1006 }
1007
1008 if ((DataEntry->Flags & SAVE_REQUIRED) == SAVE_REQUIRED) {
1009 Status = ConfigAccess->ConfigAccessProtocol.RouteConfig (
1010 &ConfigAccess->ConfigAccessProtocol,
1011 NULL,
1012 NULL
1013 );
1014 }
1015 }
1016 FreePool (Packet);
1017 }
1018 }
1019
1020 //
1021 // Unregister notify for Form package update
1022 //
1023 Status = mHiiDatabase->UnregisterPackageNotify (
1024 mHiiDatabase,
1025 NotifyHandle
1026 );
1027 //
1028 // UEFI SetupBrowser behaves differently with Framework SetupBrowser when call back function
1029 // update any forms in HII database. UEFI SetupBrowser will re-parse the displaying form package and load
1030 // the values from variable storages. Framework SetupBrowser will only re-parse the displaying form packages.
1031 // To make sure customer's previous changes is saved and the changing question behaves as expected, we
1032 // issue a EFI_BROWSER_ACTION_REQUEST_SUBMIT to ask UEFI SetupBrowser to save the changes proceed to re-parse
1033 // the form and load all the variable storages.
1034 //
1035 if (*ActionRequest == EFI_BROWSER_ACTION_REQUEST_NONE && mHiiPackageListUpdated) {
1036 mHiiPackageListUpdated= FALSE;
1037 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;
1038 } else {
1039 if (ConfigAccess->ThunkContext->FormSet->SubClass == EFI_FRONT_PAGE_SUBCLASS ||
1040 ConfigAccess->ThunkContext->FormSet->SubClass == EFI_SINGLE_USE_SUBCLASS) {
1041 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;
1042 }
1043 }
1044
1045
1046 //
1047 // Clean up.
1048 //
1049 DestroyIfrDataArray (Data, NvMapAllocated);
1050
1051 return Status;
1052 }
1053