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