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