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