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