]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Compatibility/FrameworkHiiOnUefiHiiThunk/ConfigAccess.c
Correct BlockSize when call ConfigToBlock API.
[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 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 BOOLEAN ResetRequired;
555 BOOLEAN DataAllocated;
556
557 if (Configuration == NULL) {
558 return EFI_INVALID_PARAMETER;
559 }
560
561 Data = NULL;
562 ConfigAccess = CONFIG_ACCESS_PRIVATE_FROM_PROTOCOL (This);
563
564 BufferStorage = GetStorageFromConfigString (ConfigAccess->ThunkContext->FormSet, Configuration);
565
566 if (BufferStorage == NULL) {
567 *Progress = Configuration;
568 return EFI_NOT_FOUND;
569 }
570
571 DataSize2 = BufferStorage->Size;
572 if (ConfigAccess->ThunkContext->NvMapOverride == NULL) {
573 DataAllocated = TRUE;
574 if (ConfigAccess->FormCallbackProtocol == NULL ||
575 ConfigAccess->FormCallbackProtocol->NvRead == NULL) {
576 Status = GetUefiVariable (
577 BufferStorage,
578 &Data,
579 &DataSize
580 );
581 } else {
582 Status = CallFormCallBack (
583 BufferStorage,
584 ConfigAccess->FormCallbackProtocol,
585 &Data,
586 &DataSize
587 );
588 }
589 } else {
590 //
591 // ConfigToBlock will convert the Config String and update the NvMapOverride accordingly.
592 //
593 Status = EFI_SUCCESS;
594 Data = ConfigAccess->ThunkContext->NvMapOverride;
595 DataSize = DataSize2;
596 DataAllocated = FALSE;
597 }
598 if (EFI_ERROR (Status) || (DataSize2 != DataSize)) {
599 if (Data == NULL) {
600 Data = AllocateZeroPool (DataSize2);
601 }
602 }
603
604 DataSize = DataSize2;
605 Status = mHiiConfigRoutingProtocol->ConfigToBlock (
606 mHiiConfigRoutingProtocol,
607 Configuration,
608 Data,
609 &DataSize,
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 Done:
640 if (DataAllocated && (Data != NULL)) {
641 FreePool (Data);
642 }
643
644 return Status;
645 }
646
647 /**
648 Build the EFI_IFR_DATA_ARRAY which will be used to pass to
649 EFI_FORM_CALLBACK_PROTOCOL.Callback. Check definition of EFI_IFR_DATA_ARRAY
650 for details.
651
652 ASSERT if the Question Type is not EFI_IFR_TYPE_NUM_SIZE_* or EFI_IFR_TYPE_STRING.
653
654 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL
655 @param QuestionId The Question ID.
656 @param Type The Question Type.
657 @param Value The Question Value.
658 @param NvMapAllocated On output indicates if a buffer is allocated for NvMap.
659
660 @return A pointer to EFI_IFR_DATA_ARRAY. The caller must free this buffer allocated.
661 **/
662 EFI_IFR_DATA_ARRAY *
663 CreateIfrDataArray (
664 IN CONFIG_ACCESS_PRIVATE *ConfigAccess,
665 IN EFI_QUESTION_ID QuestionId,
666 IN UINT8 Type,
667 IN EFI_IFR_TYPE_VALUE *Value,
668 OUT BOOLEAN *NvMapAllocated
669 )
670 {
671 EFI_IFR_DATA_ARRAY *IfrDataArray;
672 EFI_IFR_DATA_ENTRY *IfrDataEntry;
673 UINTN BrowserDataSize;
674 FORMSET_STORAGE *BufferStorage;
675 UINTN Size;
676 EFI_STRING String;
677 FORM_BROWSER_STATEMENT *Statement;
678
679 *NvMapAllocated = FALSE;
680
681 String = NULL;
682
683 switch (Type) {
684 case EFI_IFR_TYPE_NUM_SIZE_8:
685 case EFI_IFR_TYPE_NUM_SIZE_16:
686 case EFI_IFR_TYPE_NUM_SIZE_32:
687 case EFI_IFR_TYPE_NUM_SIZE_64:
688 case EFI_IFR_TYPE_BOOLEAN:
689 Size = sizeof (*Value);
690 break;
691
692 case EFI_IFR_TYPE_STRING:
693 if (Value->string == 0) {
694 Size = 0;
695 } else {
696 String = HiiGetString (ConfigAccess->ThunkContext->UefiHiiHandle, Value->string, NULL);
697 ASSERT (String != NULL);
698
699 Size = StrSize (String);
700 }
701 break;
702
703 case EFI_IFR_TYPE_ACTION:
704 Size = 0;
705 break;
706
707 default:
708 ASSERT (FALSE);
709 Size = 0;
710 break;
711 }
712
713 IfrDataArray = AllocateZeroPool (sizeof (EFI_IFR_DATA_ARRAY) + sizeof (EFI_IFR_DATA_ENTRY) + Size);
714 ASSERT (IfrDataArray != NULL);
715 IfrDataArray->EntryCount = 1;
716 IfrDataEntry = (EFI_IFR_DATA_ENTRY *) (IfrDataArray + 1);
717
718 Statement = GetStorageFromQuestionId (ConfigAccess->ThunkContext->FormSet, QuestionId);
719
720 if (Statement == NULL || Statement->Storage == NULL) {
721 //
722 // The QuestionId is not associated with a Buffer Storage.
723 // Try to get the first Buffer Storage then.
724 //
725 BufferStorage = GetFirstStorageOfFormSet (ConfigAccess->ThunkContext->FormSet);
726 } else {
727 BufferStorage = Statement->Storage;
728 IfrDataEntry->OpCode = Statement->Operand;
729 }
730
731 if (BufferStorage != NULL) {
732 BrowserDataSize = BufferStorage->Size;
733 IfrDataEntry->Length = (UINT8) (sizeof (EFI_IFR_DATA_ENTRY) + Size);
734
735 if (ConfigAccess->ThunkContext->NvMapOverride == NULL) {
736 *NvMapAllocated = TRUE;
737 IfrDataArray->NvRamMap = AllocateZeroPool (BrowserDataSize);
738 } else {
739 *NvMapAllocated = FALSE;
740 IfrDataArray->NvRamMap = ConfigAccess->ThunkContext->NvMapOverride;
741 }
742
743 ASSERT (HiiGetBrowserData (&BufferStorage->Guid, BufferStorage->Name, BrowserDataSize, (UINT8 *) IfrDataArray->NvRamMap));
744
745 switch (Type) {
746 case EFI_IFR_TYPE_NUM_SIZE_8:
747 case EFI_IFR_TYPE_NUM_SIZE_16:
748 case EFI_IFR_TYPE_NUM_SIZE_32:
749 case EFI_IFR_TYPE_NUM_SIZE_64:
750 case EFI_IFR_TYPE_BOOLEAN:
751 CopyMem (&IfrDataEntry->Data, &(Value->u8), sizeof (*Value));
752 break;
753
754 case EFI_IFR_TYPE_STRING:
755 if (Size != 0) {
756 ASSERT (String != NULL);
757 StrCpy ((CHAR16 *) &IfrDataEntry->Data, String);
758 FreePool (String);
759 }
760 break;
761
762 case EFI_IFR_TYPE_ACTION:
763 break;
764
765 default:
766 ASSERT (FALSE);
767 break;
768 }
769
770 //
771 // Need to fiil in the information for the rest of field for EFI_IFR_DATA_ENTRY.
772 // It seems that no implementation is found to use other fields. Leave them uninitialized for now.
773 //
774 //UINT8 OpCode; // Likely a string, numeric, or one-of
775 //UINT8 Length; // Length of the EFI_IFR_DATA_ENTRY packet
776 //UINT16 Flags; // Flags settings to determine what behavior is desired from the browser after the callback
777 //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
778 // If the OpCode is a OneOf or Numeric type - Data is a UINT16 value
779 // If the OpCode is a String type - Data is a CHAR16[x] type
780 // If the OpCode is a Checkbox type - Data is a UINT8 value
781 // If the OpCode is a NV Access type - Data is a FRAMEWORK_EFI_IFR_NV_DATA structure
782 }
783
784 return IfrDataArray;
785 }
786
787 /**
788 If a NvMapOverride is passed in to EFI_FORM_BROWSER_PROTOCOL.SendForm, the Form Browser
789 needs to be informed when data changed in NvMapOverride. This function will invoke
790 SetBrowserData () to set internal data of Form Browser.
791
792 @param ConfigAccess The Config Access Private Context.
793 @param QuestionId The Question Id that invokes the callback.
794
795
796 **/
797 VOID
798 SyncBrowserDataForNvMapOverride (
799 IN CONST CONFIG_ACCESS_PRIVATE *ConfigAccess,
800 IN EFI_QUESTION_ID QuestionId
801 )
802 {
803 FORMSET_STORAGE *BufferStorage;
804 BOOLEAN CheckFlag;
805 UINTN BrowserDataSize;
806 FORM_BROWSER_STATEMENT *Statement;
807
808 if (ConfigAccess->ThunkContext->NvMapOverride != NULL) {
809
810 Statement = GetStorageFromQuestionId (ConfigAccess->ThunkContext->FormSet, QuestionId);
811
812 if (Statement == NULL || Statement->Storage == NULL) {
813 //
814 // QuestionId is a statement without Storage.
815 // 1) It is a Goto.
816 //
817 //
818 BufferStorage = GetFirstStorageOfFormSet (ConfigAccess->ThunkContext->FormSet);
819 } else {
820 BufferStorage = Statement->Storage;
821 }
822
823 //
824 // If NvMapOverride is not NULL, this Form must have at least one Buffer Type Variable Storage.
825 //
826 ASSERT (BufferStorage != NULL);
827
828 BrowserDataSize = BufferStorage->Size;
829
830 CheckFlag = HiiSetBrowserData (&BufferStorage->Guid, BufferStorage->Name, BrowserDataSize, ConfigAccess->ThunkContext->NvMapOverride, NULL);
831 ASSERT (CheckFlag);
832 }
833
834 }
835
836 /**
837 Free up resource allocated for a EFI_IFR_DATA_ARRAY by CreateIfrDataArray ().
838
839 @param Array The EFI_IFR_DATA_ARRAY allocated.
840 @param NvMapAllocated If the NvRamMap is allocated for EFI_IFR_DATA_ARRAY.
841
842 **/
843 VOID
844 DestroyIfrDataArray (
845 IN EFI_IFR_DATA_ARRAY *Array,
846 IN BOOLEAN NvMapAllocated
847 )
848 {
849 if (Array != NULL) {
850 if (NvMapAllocated) {
851 FreePool (Array->NvRamMap);
852 }
853
854 FreePool (Array);
855 }
856 }
857
858 /**
859 Get the ONE_OF_OPTION_MAP_ENTRY for a QuestionId that invokes the
860 EFI_FORM_CALLBACK_PROTOCOL.Callback. The information is needed as
861 the callback mechanism for EFI_IFR_ONE_OF_OPTION is changed from
862 EFI_IFR_ONE_OF_OPTION in Framework IFR. Check EFI_IFR_GUID_OPTIONKEY
863 for detailed information.
864
865 @param ThunkContext The Thunk Context.
866 @param QuestionId The Question Id.
867 @param Type The Question Type.
868 @param Value The One Of Option's value.
869
870 @return The ONE_OF_OPTION_MAP_ENTRY found.
871 @retval NULL If no entry is found.
872 **/
873 ONE_OF_OPTION_MAP_ENTRY *
874 GetOneOfOptionMapEntry (
875 IN HII_THUNK_CONTEXT *ThunkContext,
876 IN EFI_QUESTION_ID QuestionId,
877 IN UINT8 Type,
878 IN EFI_IFR_TYPE_VALUE *Value
879 )
880 {
881 LIST_ENTRY *Link;
882 LIST_ENTRY *Link2;
883 ONE_OF_OPTION_MAP_ENTRY *OneOfOptionMapEntry;
884 ONE_OF_OPTION_MAP *OneOfOptionMap;
885 FORM_BROWSER_FORMSET *FormSet;
886
887 FormSet = ThunkContext->FormSet;
888
889 Link = GetFirstNode (&FormSet->OneOfOptionMapListHead);
890
891 while (!IsNull (&FormSet->OneOfOptionMapListHead, Link)) {
892 OneOfOptionMap = ONE_OF_OPTION_MAP_FROM_LINK(Link);
893 if (OneOfOptionMap->QuestionId == QuestionId) {
894 ASSERT (OneOfOptionMap->ValueType == Type);
895
896 Link2 = GetFirstNode (&OneOfOptionMap->OneOfOptionMapEntryListHead);
897
898 while (!IsNull (&OneOfOptionMap->OneOfOptionMapEntryListHead, Link2)) {
899 OneOfOptionMapEntry = ONE_OF_OPTION_MAP_ENTRY_FROM_LINK (Link2);
900
901 if (CompareMem (Value, &OneOfOptionMapEntry->Value, sizeof (EFI_IFR_TYPE_VALUE)) == 0) {
902 return OneOfOptionMapEntry;
903 }
904
905 Link2 = GetNextNode (&OneOfOptionMap->OneOfOptionMapEntryListHead, Link2);
906 }
907 }
908
909 Link = GetNextNode (&FormSet->OneOfOptionMapListHead, Link);
910 }
911
912
913 return NULL;
914 }
915
916 /**
917 Functions which are registered to receive notification of
918 database events have this prototype. The actual event is encoded
919 in NotifyType. The following table describes how PackageType,
920 PackageGuid, Handle, and Package are used for each of the
921 notification types.
922
923 If any Pakcage List in database is updated, mHiiPackageListUpdated
924 will be set. If mHiiPackageListUpdated is set, Framework ThunkCallback()
925 will force the UEFI Setup Browser to save the uncommitted data. This
926 is needed as Framework's Callback function may dynamically update
927 opcode in a Package List. UEFI Setup Browser will quit itself and reparse
928 the Package List's IFR and display it. UEFI Config Access's implementation
929 is required to save the modified (SetBrowserData or directly save the data
930 to NV storage). But Framework HII Modules is not aware of this rule. Therefore,
931 we will enforce the rule in ThunkCallback (). The side effect of force saving
932 of NV data is the NV flag in browser may not flag a update as data has already
933 been saved to NV storage.
934
935 @param PackageType Package type of the notification.
936
937 @param PackageGuid If PackageType is
938 EFI_HII_PACKAGE_TYPE_GUID, then this is
939 the pointer to the GUID from the Guid
940 field of EFI_HII_PACKAGE_GUID_HEADER.
941 Otherwise, it must be NULL.
942
943 @param Package Points to the package referred to by the
944 notification Handle The handle of the package
945 list which contains the specified package.
946
947 @param Handle The HII handle.
948
949 @param NotifyType The type of change concerning the
950 database. See
951 EFI_HII_DATABASE_NOTIFY_TYPE.
952
953 **/
954 EFI_STATUS
955 EFIAPI
956 FormUpdateNotify (
957 IN UINT8 PackageType,
958 IN CONST EFI_GUID *PackageGuid,
959 IN CONST EFI_HII_PACKAGE_HEADER *Package,
960 IN EFI_HII_HANDLE Handle,
961 IN EFI_HII_DATABASE_NOTIFY_TYPE NotifyType
962 )
963 {
964 mHiiPackageListUpdated = TRUE;
965
966 return EFI_SUCCESS;
967 }
968
969 /**
970 Wrap the EFI_HII_CONFIG_ACCESS_PROTOCOL.CallBack to EFI_FORM_CALLBACK_PROTOCOL.Callback. Therefor,
971 the framework HII module willl do no porting and work with a UEFI HII SetupBrowser.
972
973 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
974 @param Action Specifies the type of action taken by the browser. See EFI_BROWSER_ACTION_x.
975 @param QuestionId A unique value which is sent to the original exporting driver so that it can identify the
976 type of data to expect. The format of the data tends to vary based on the opcode that
977 generated the callback.
978 @param Type The type of value for the question. See EFI_IFR_TYPE_x in
979 EFI_IFR_ONE_OF_OPTION.
980 @param Value A pointer to the data being sent to the original exporting driver. The type is specified
981 by Type. Type EFI_IFR_TYPE_VALUE is defined in
982 EFI_IFR_ONE_OF_OPTION.
983 @param ActionRequest On return, points to the action requested by the callback function. Type
984 EFI_BROWSER_ACTION_REQUEST is specified in SendForm() in the Form
985 Browser Protocol.
986
987 @retval EFI_UNSUPPORTED If the Framework HII module does not register Callback although it specify the opcode under
988 focuse to be INTERRACTIVE.
989 @retval EFI_SUCCESS The callback complete successfully.
990 @retval !EFI_SUCCESS The error code returned by EFI_FORM_CALLBACK_PROTOCOL.Callback.
991
992 **/
993 EFI_STATUS
994 EFIAPI
995 ThunkCallback (
996 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
997 IN EFI_BROWSER_ACTION Action,
998 IN EFI_QUESTION_ID QuestionId,
999 IN UINT8 Type,
1000 IN EFI_IFR_TYPE_VALUE *Value,
1001 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
1002 )
1003 {
1004 EFI_STATUS Status;
1005 CONFIG_ACCESS_PRIVATE *ConfigAccess;
1006 EFI_FORM_CALLBACK_PROTOCOL *FormCallbackProtocol;
1007 EFI_HII_CALLBACK_PACKET *Packet;
1008 EFI_IFR_DATA_ARRAY *Data;
1009 EFI_IFR_DATA_ENTRY *DataEntry;
1010 UINT16 KeyValue;
1011 ONE_OF_OPTION_MAP_ENTRY *OneOfOptionMapEntry;
1012 EFI_HANDLE NotifyHandle;
1013 EFI_INPUT_KEY Key;
1014 BOOLEAN NvMapAllocated;
1015
1016 if ((Action == EFI_BROWSER_ACTION_FORM_OPEN) || (Action == EFI_BROWSER_ACTION_FORM_CLOSE)) {
1017 //
1018 // Ignore UEFI OPEN/CLOSE Action for FrameworkCallback
1019 //
1020 return EFI_SUCCESS;
1021 }
1022
1023 ASSERT (This != NULL);
1024 ASSERT (Value != NULL);
1025 ASSERT (ActionRequest != NULL);
1026
1027 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_NONE;
1028
1029 ConfigAccess = CONFIG_ACCESS_PRIVATE_FROM_PROTOCOL (This);
1030
1031 FormCallbackProtocol = ConfigAccess->FormCallbackProtocol;
1032 if (FormCallbackProtocol == NULL) {
1033 ASSERT (FALSE);
1034 return EFI_UNSUPPORTED;
1035 }
1036
1037 //
1038 // Check if the QuestionId match a OneOfOption.
1039 //
1040 OneOfOptionMapEntry = GetOneOfOptionMapEntry (ConfigAccess->ThunkContext, QuestionId, Type, Value);
1041
1042 if (OneOfOptionMapEntry == NULL) {
1043 //
1044 // This is not a One-Of-Option opcode. QuestionId is the KeyValue
1045 //
1046 KeyValue = QuestionId;
1047 } else {
1048 //
1049 // Otherwise, use the original Key specified in One Of Option in the Framework VFR syntax.
1050 //
1051 KeyValue = OneOfOptionMapEntry->FwKey;
1052 }
1053
1054 //
1055 // Build the EFI_IFR_DATA_ARRAY
1056 //
1057 Data = CreateIfrDataArray (ConfigAccess, QuestionId, Type, Value, &NvMapAllocated);
1058
1059 Status = mHiiDatabase->RegisterPackageNotify (
1060 mHiiDatabase,
1061 EFI_HII_PACKAGE_FORMS,
1062 NULL,
1063 FormUpdateNotify,
1064 EFI_HII_DATABASE_NOTIFY_REMOVE_PACK,
1065 &NotifyHandle
1066 );
1067 //
1068 //Call the Framework Callback function.
1069 //
1070 Packet = NULL;
1071 Status = FormCallbackProtocol->Callback (
1072 FormCallbackProtocol,
1073 KeyValue,
1074 Data,
1075 &Packet
1076 );
1077 SyncBrowserDataForNvMapOverride (ConfigAccess, QuestionId);
1078
1079 //
1080 // Callback require browser to perform action
1081 //
1082 if (EFI_ERROR (Status)) {
1083 if (Packet != NULL) {
1084 do {
1085 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, Packet->String, NULL);
1086 } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);
1087 }
1088 //
1089 // Error Code in Status is discarded.
1090 //
1091 } else {
1092 if (Packet != NULL) {
1093 if (Packet->DataArray.EntryCount == 1 && Packet->DataArray.NvRamMap == NULL) {
1094 DataEntry = (EFI_IFR_DATA_ENTRY *) ((UINT8 *) Packet + sizeof (EFI_IFR_DATA_ARRAY));
1095 if ((DataEntry->Flags & EXIT_REQUIRED) == EXIT_REQUIRED) {
1096 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;
1097 }
1098
1099 if ((DataEntry->Flags & SAVE_REQUIRED) == SAVE_REQUIRED) {
1100 Status = ConfigAccess->ConfigAccessProtocol.RouteConfig (
1101 &ConfigAccess->ConfigAccessProtocol,
1102 NULL,
1103 NULL
1104 );
1105 }
1106 }
1107 FreePool (Packet);
1108 }
1109 }
1110
1111 //
1112 // Unregister notify for Form package update
1113 //
1114 Status = mHiiDatabase->UnregisterPackageNotify (
1115 mHiiDatabase,
1116 NotifyHandle
1117 );
1118 //
1119 // UEFI SetupBrowser behaves differently with Framework SetupBrowser when call back function
1120 // update any forms in HII database. UEFI SetupBrowser will re-parse the displaying form package and load
1121 // the values from variable storages. Framework SetupBrowser will only re-parse the displaying form packages.
1122 // To make sure customer's previous changes is saved and the changing question behaves as expected, we
1123 // issue a EFI_BROWSER_ACTION_REQUEST_SUBMIT to ask UEFI SetupBrowser to save the changes proceed to re-parse
1124 // the form and load all the variable storages.
1125 //
1126 if (*ActionRequest == EFI_BROWSER_ACTION_REQUEST_NONE && mHiiPackageListUpdated) {
1127 mHiiPackageListUpdated= FALSE;
1128 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;
1129 } else {
1130 if (ConfigAccess->ThunkContext->FormSet->SubClass == EFI_FRONT_PAGE_SUBCLASS ||
1131 ConfigAccess->ThunkContext->FormSet->SubClass == EFI_SINGLE_USE_SUBCLASS) {
1132 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;
1133 }
1134 }
1135
1136
1137 //
1138 // Clean up.
1139 //
1140 DestroyIfrDataArray (Data, NvMapAllocated);
1141
1142 return Status;
1143 }
1144