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