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