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