2 This driver effectuates OVMF's platform configuration settings and exposes
5 Copyright (C) 2014, Red Hat, Inc.
6 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials are licensed and made available
9 under the terms and conditions of the BSD License which accompanies this
10 distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
14 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/BaseLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/DevicePathLib.h>
21 #include <Library/HiiLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/PrintLib.h>
24 #include <Library/UefiBootServicesTableLib.h>
25 #include <Library/UefiHiiServicesLib.h>
26 #include <Protocol/DevicePath.h>
27 #include <Protocol/GraphicsOutput.h>
28 #include <Protocol/HiiConfigAccess.h>
29 #include <Guid/MdeModuleHii.h>
30 #include <Guid/OvmfPlatformConfig.h>
33 #include "PlatformConfig.h"
36 // The HiiAddPackages() library function requires that any controller (or
37 // image) handle, to be associated with the HII packages under installation, be
38 // "decorated" with a device path. The tradition seems to be a vendor device
41 // We'd like to associate our HII packages with the driver's image handle. The
42 // first idea is to use the driver image's device path. Unfortunately, loaded
43 // images only come with an EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL (not the
44 // usual EFI_DEVICE_PATH_PROTOCOL), ie. a different GUID. In addition, even the
45 // EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL interface may be NULL, if the image
46 // has been loaded from an "unnamed" memory source buffer.
48 // Hence let's just stick with the tradition -- use a dedicated vendor device
49 // path, with the driver's FILE_GUID.
53 VENDOR_DEVICE_PATH VendorDevicePath
;
54 EFI_DEVICE_PATH_PROTOCOL End
;
58 STATIC PKG_DEVICE_PATH mPkgDevicePath
= {
64 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
) ),
65 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
) >> 8)
72 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
74 (UINT8
) (END_DEVICE_PATH_LENGTH
),
75 (UINT8
) (END_DEVICE_PATH_LENGTH
>> 8)
81 // The configuration interface between the HII engine (form display etc) and
84 STATIC EFI_HII_CONFIG_ACCESS_PROTOCOL mConfigAccess
;
87 // The handle representing our list of packages after installation.
89 STATIC EFI_HII_HANDLE mInstalledPackages
;
92 // The arrays below constitute our HII package list. They are auto-generated by
93 // the VFR compiler and linked into the driver image during the build.
95 // - The strings package receives its C identifier from the driver's BASE_NAME,
98 // - The forms package receives its C identifier from the VFR file's basename,
102 extern UINT8 PlatformDxeStrings
[];
103 extern UINT8 PlatformFormsBin
[];
106 // We want to be notified about GOP installations until we find one GOP
107 // interface that lets us populate the form.
109 STATIC EFI_EVENT mGopEvent
;
112 // The registration record underneath this pointer allows us to iterate through
113 // the GOP instances one by one.
115 STATIC VOID
*mGopTracker
;
118 // Cache the resolutions we get from the GOP.
125 STATIC UINTN mNumGopModes
;
126 STATIC GOP_MODE
*mGopModes
;
130 Load the persistent platform configuration and translate it to binary form
133 If the platform configuration is missing, then the function fills in a
136 @param[out] MainFormState Binary form/widget state after translation.
138 @retval EFI_SUCCESS Form/widget state ready.
139 @return Error codes from underlying functions.
144 PlatformConfigToFormState (
145 OUT MAIN_FORM_STATE
*MainFormState
149 PLATFORM_CONFIG PlatformConfig
;
150 UINT64 OptionalElements
;
153 ZeroMem (MainFormState
, sizeof *MainFormState
);
155 Status
= PlatformConfigLoad (&PlatformConfig
, &OptionalElements
);
158 if (OptionalElements
& PLATFORM_CONFIG_F_GRAPHICS_RESOLUTION
) {
160 // Format the preferred resolution as text.
162 UnicodeSPrintAsciiFormat (
163 (CHAR16
*) MainFormState
->CurrentPreferredResolution
,
164 sizeof MainFormState
->CurrentPreferredResolution
,
166 (INT64
) PlatformConfig
.HorizontalResolution
,
167 (INT64
) PlatformConfig
.VerticalResolution
);
170 // Try to locate it in the drop-down list too. This may not succeed, but
173 for (ModeNumber
= 0; ModeNumber
< mNumGopModes
; ++ModeNumber
) {
174 if (mGopModes
[ModeNumber
].X
== PlatformConfig
.HorizontalResolution
&&
175 mGopModes
[ModeNumber
].Y
== PlatformConfig
.VerticalResolution
) {
176 MainFormState
->NextPreferredResolution
= (UINT32
) ModeNumber
;
184 // fall through otherwise
188 UnicodeSPrintAsciiFormat (
189 (CHAR16
*) MainFormState
->CurrentPreferredResolution
,
190 sizeof MainFormState
->CurrentPreferredResolution
,
203 This function is called by the HII machinery when it fetches the form state.
205 See the precise documentation in the UEFI spec.
207 @param[in] This The Config Access Protocol instance.
209 @param[in] Request A <ConfigRequest> format UCS-2 string describing the
212 @param[out] Progress A pointer into Request on output, identifying the query
213 element where processing failed.
215 @param[out] Results A <MultiConfigAltResp> format UCS-2 string that has
216 all values filled in for the names in the Request
219 @retval EFI_SUCCESS Extraction of form state in <MultiConfigAltResp>
221 @return Status codes from underlying functions.
228 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
229 IN CONST EFI_STRING Request
,
230 OUT EFI_STRING
*Progress
,
231 OUT EFI_STRING
*Results
234 MAIN_FORM_STATE MainFormState
;
237 DEBUG ((EFI_D_VERBOSE
, "%a: Request=\"%s\"\n", __FUNCTION__
, Request
));
239 Status
= PlatformConfigToFormState (&MainFormState
);
240 if (EFI_ERROR (Status
)) {
246 // Answer the textual request keying off the binary form state.
248 Status
= gHiiConfigRouting
->BlockToConfig (gHiiConfigRouting
, Request
,
249 (VOID
*) &MainFormState
, sizeof MainFormState
,
251 if (EFI_ERROR (Status
)) {
252 DEBUG ((EFI_D_ERROR
, "%a: BlockToConfig(): %r, Progress=\"%s\"\n",
253 __FUNCTION__
, Status
, (Status
== EFI_DEVICE_ERROR
) ? NULL
: *Progress
));
255 DEBUG ((EFI_D_VERBOSE
, "%a: Results=\"%s\"\n", __FUNCTION__
, *Results
));
262 Interpret the binary form state and save it as persistent platform
265 @param[in] MainFormState Binary form/widget state to verify and save.
267 @retval EFI_SUCCESS Platform configuration saved.
268 @return Error codes from underlying functions.
273 FormStateToPlatformConfig (
274 IN CONST MAIN_FORM_STATE
*MainFormState
278 PLATFORM_CONFIG PlatformConfig
;
279 CONST GOP_MODE
*GopMode
;
282 // There's nothing to do with the textual CurrentPreferredResolution field.
283 // We verify and translate the selection in the drop-down list.
285 if (MainFormState
->NextPreferredResolution
>= mNumGopModes
) {
286 return EFI_INVALID_PARAMETER
;
288 GopMode
= mGopModes
+ MainFormState
->NextPreferredResolution
;
290 ZeroMem (&PlatformConfig
, sizeof PlatformConfig
);
291 PlatformConfig
.HorizontalResolution
= GopMode
->X
;
292 PlatformConfig
.VerticalResolution
= GopMode
->Y
;
294 Status
= PlatformConfigSave (&PlatformConfig
);
300 This function is called by the HII machinery when it wants the driver to
301 interpret and persist the form state.
303 See the precise documentation in the UEFI spec.
305 @param[in] This The Config Access Protocol instance.
307 @param[in] Configuration A <ConfigResp> format UCS-2 string describing the
310 @param[out] Progress A pointer into Configuration on output,
311 identifying the element where processing failed.
313 @retval EFI_SUCCESS Configuration verified, state permanent.
315 @return Status codes from underlying functions.
321 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
322 IN CONST EFI_STRING Configuration
,
323 OUT EFI_STRING
*Progress
326 MAIN_FORM_STATE MainFormState
;
330 DEBUG ((EFI_D_VERBOSE
, "%a: Configuration=\"%s\"\n", __FUNCTION__
,
334 // the "read" step in RMW
336 Status
= PlatformConfigToFormState (&MainFormState
);
337 if (EFI_ERROR (Status
)) {
338 *Progress
= Configuration
;
343 // the "modify" step in RMW
345 // (Update the binary form state. This update may be partial, which is why in
346 // general we must pre-load the form state from the platform config.)
348 BlockSize
= sizeof MainFormState
;
349 Status
= gHiiConfigRouting
->ConfigToBlock (gHiiConfigRouting
, Configuration
,
350 (VOID
*) &MainFormState
, &BlockSize
, Progress
);
351 if (EFI_ERROR (Status
)) {
352 DEBUG ((EFI_D_ERROR
, "%a: ConfigToBlock(): %r, Progress=\"%s\"\n",
353 __FUNCTION__
, Status
,
354 (Status
== EFI_BUFFER_TOO_SMALL
) ? NULL
: *Progress
));
359 // the "write" step in RMW
361 Status
= FormStateToPlatformConfig (&MainFormState
);
362 if (EFI_ERROR (Status
)) {
363 *Progress
= Configuration
;
373 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
374 IN EFI_BROWSER_ACTION Action
,
375 IN EFI_QUESTION_ID QuestionId
,
377 IN OUT EFI_IFR_TYPE_VALUE
*Value
,
378 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
381 DEBUG ((EFI_D_VERBOSE
, "%a: Action=0x%Lx QuestionId=%d Type=%d\n",
382 __FUNCTION__
, (UINT64
) Action
, QuestionId
, Type
));
384 if (Action
!= EFI_BROWSER_ACTION_CHANGED
) {
385 return EFI_UNSUPPORTED
;
388 switch (QuestionId
) {
389 case QUESTION_SAVE_EXIT
:
390 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_SUBMIT_EXIT
;
393 case QUESTION_DISCARD_EXIT
:
394 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT
;
406 Query and save all resolutions supported by the GOP.
408 @param[in] Gop The Graphics Output Protocol instance to query.
410 @param[out] NumGopModes The number of modes supported by the GOP. On output,
411 this parameter will be positive.
413 @param[out] GopModes On output, a dynamically allocated array containing
414 the resolutions returned by the GOP. The caller is
415 responsible for freeing the array after use.
417 @retval EFI_UNSUPPORTED No modes found.
418 @retval EFI_OUT_OF_RESOURCES Failed to allocate GopModes.
419 @return Error codes from Gop->QueryMode().
426 IN EFI_GRAPHICS_OUTPUT_PROTOCOL
*Gop
,
427 OUT UINTN
*NumGopModes
,
428 OUT GOP_MODE
**GopModes
434 if (Gop
->Mode
->MaxMode
== 0) {
435 return EFI_UNSUPPORTED
;
437 *NumGopModes
= Gop
->Mode
->MaxMode
;
439 *GopModes
= AllocatePool (Gop
->Mode
->MaxMode
* sizeof **GopModes
);
440 if (*GopModes
== NULL
) {
441 return EFI_OUT_OF_RESOURCES
;
444 for (ModeNumber
= 0; ModeNumber
< Gop
->Mode
->MaxMode
; ++ModeNumber
) {
445 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
*Info
;
448 Status
= Gop
->QueryMode (Gop
, ModeNumber
, &SizeOfInfo
, &Info
);
449 if (EFI_ERROR (Status
)) {
453 (*GopModes
)[ModeNumber
].X
= Info
->HorizontalResolution
;
454 (*GopModes
)[ModeNumber
].Y
= Info
->VerticalResolution
;
461 FreePool (*GopModes
);
468 Create a set of "one-of-many" (ie. "drop down list") option IFR opcodes,
469 based on available GOP resolutions, to be placed under a "one-of-many" (ie.
470 "drop down list") opcode.
472 @param[in] PackageList The package list with the formset and form for
473 which the drop down options are produced. Option
474 names are added as new strings to PackageList.
476 @param[out] OpCodeBuffer On output, a dynamically allocated opcode buffer
477 with drop down list options corresponding to GOP
478 resolutions. The caller is responsible for freeing
479 OpCodeBuffer with HiiFreeOpCodeHandle() after use.
481 @param[in] NumGopModes Number of entries in GopModes.
483 @param[in] GopModes Array of resolutions retrieved from the GOP.
485 @retval EFI_SUCESS Opcodes have been successfully produced.
487 @return Status codes from underlying functions. PackageList may
488 have been extended with new strings. OpCodeBuffer is
494 CreateResolutionOptions (
495 IN EFI_HII_HANDLE
*PackageList
,
496 OUT VOID
**OpCodeBuffer
,
497 IN UINTN NumGopModes
,
498 IN GOP_MODE
*GopModes
505 OutputBuffer
= HiiAllocateOpCodeHandle ();
506 if (OutputBuffer
== NULL
) {
507 return EFI_OUT_OF_RESOURCES
;
510 for (ModeNumber
= 0; ModeNumber
< NumGopModes
; ++ModeNumber
) {
511 CHAR16 Desc
[MAXSIZE_RES_CUR
];
512 EFI_STRING_ID NewString
;
515 UnicodeSPrintAsciiFormat (Desc
, sizeof Desc
, "%Ldx%Ld",
516 (INT64
) GopModes
[ModeNumber
].X
, (INT64
) GopModes
[ModeNumber
].Y
);
517 NewString
= HiiSetString (PackageList
, 0 /* new string */, Desc
,
518 NULL
/* for all languages */);
519 if (NewString
== 0) {
520 Status
= EFI_OUT_OF_RESOURCES
;
521 goto FreeOutputBuffer
;
523 OpCode
= HiiCreateOneOfOptionOpCode (OutputBuffer
, NewString
,
524 0 /* Flags */, EFI_IFR_NUMERIC_SIZE_4
, ModeNumber
);
525 if (OpCode
== NULL
) {
526 Status
= EFI_OUT_OF_RESOURCES
;
527 goto FreeOutputBuffer
;
531 *OpCodeBuffer
= OutputBuffer
;
535 HiiFreeOpCodeHandle (OutputBuffer
);
542 Populate the form identified by the (PackageList, FormSetGuid, FormId)
545 The drop down list of video resolutions is generated from (NumGopModes,
548 @retval EFI_SUCESS Form successfully updated.
549 @return Status codes from underlying functions.
556 IN EFI_HII_HANDLE
*PackageList
,
557 IN EFI_GUID
*FormSetGuid
,
558 IN EFI_FORM_ID FormId
,
559 IN UINTN NumGopModes
,
560 IN GOP_MODE
*GopModes
566 EFI_IFR_GUID_LABEL
*Anchor
;
569 OpCodeBuffer2
= NULL
;
572 // 1. Allocate an empty opcode buffer.
574 OpCodeBuffer
= HiiAllocateOpCodeHandle ();
575 if (OpCodeBuffer
== NULL
) {
576 return EFI_OUT_OF_RESOURCES
;
580 // 2. Create a label opcode (which is a Tiano extension) inside the buffer.
581 // The label's number must match the "anchor" label in the form.
583 OpCode
= HiiCreateGuidOpCode (OpCodeBuffer
, &gEfiIfrTianoGuid
,
584 NULL
/* optional copy origin */, sizeof *Anchor
);
585 if (OpCode
== NULL
) {
586 Status
= EFI_OUT_OF_RESOURCES
;
587 goto FreeOpCodeBuffer
;
590 Anchor
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
591 Anchor
->Number
= LABEL_RES_NEXT
;
594 // 3. Create the opcodes inside the buffer that are to be inserted into the
597 // 3.1. Get a list of resolutions.
599 Status
= CreateResolutionOptions (PackageList
, &OpCodeBuffer2
,
600 NumGopModes
, GopModes
);
601 if (EFI_ERROR (Status
)) {
602 goto FreeOpCodeBuffer
;
606 // 3.2. Create a one-of-many question with the above options.
608 OpCode
= HiiCreateOneOfOpCode (
609 OpCodeBuffer
, // create opcode inside this
611 QUESTION_RES_NEXT
, // ID of question,
612 FORMSTATEID_MAIN_FORM
, // identifies form state
614 (UINT16
) OFFSET_OF (MAIN_FORM_STATE
, // value of question stored
615 NextPreferredResolution
), // at this offset,
616 STRING_TOKEN (STR_RES_NEXT
), // Prompt,
617 STRING_TOKEN (STR_RES_NEXT_HELP
), // Help,
619 EFI_IFR_NUMERIC_SIZE_4
, // see sizeof
620 // NextPreferredResolution,
621 OpCodeBuffer2
, // buffer with possible
623 NULL
// DEFAULT opcodes
625 if (OpCode
== NULL
) {
626 Status
= EFI_OUT_OF_RESOURCES
;
627 goto FreeOpCodeBuffer2
;
631 // 4. Update the form with the opcode buffer.
633 Status
= HiiUpdateForm (PackageList
, FormSetGuid
, FormId
,
634 OpCodeBuffer
, // buffer with head anchor, and new contents to be
636 NULL
// buffer with tail anchor, for deleting old
641 HiiFreeOpCodeHandle (OpCodeBuffer2
);
644 HiiFreeOpCodeHandle (OpCodeBuffer
);
651 Load and execute the platform configuration.
653 @retval EFI_SUCCESS Configuration loaded and executed.
654 @return Status codes from PlatformConfigLoad().
659 ExecutePlatformConfig (
664 PLATFORM_CONFIG PlatformConfig
;
665 UINT64 OptionalElements
;
666 RETURN_STATUS PcdStatus
;
668 Status
= PlatformConfigLoad (&PlatformConfig
, &OptionalElements
);
669 if (EFI_ERROR (Status
)) {
670 DEBUG (((Status
== EFI_NOT_FOUND
) ? EFI_D_VERBOSE
: EFI_D_ERROR
,
671 "%a: failed to load platform config: %r\n", __FUNCTION__
, Status
));
675 if (OptionalElements
& PLATFORM_CONFIG_F_GRAPHICS_RESOLUTION
) {
677 // Pass the preferred resolution to GraphicsConsoleDxe via dynamic PCDs.
679 PcdStatus
= PcdSet32S (PcdVideoHorizontalResolution
,
680 PlatformConfig
.HorizontalResolution
);
681 ASSERT_RETURN_ERROR (PcdStatus
);
683 PcdStatus
= PcdSet32S (PcdVideoVerticalResolution
,
684 PlatformConfig
.VerticalResolution
);
685 ASSERT_RETURN_ERROR (PcdStatus
);
693 Notification callback for GOP interface installation.
695 @param[in] Event Event whose notification function is being invoked.
697 @param[in] Context The pointer to the notification function's context, which
698 is implementation-dependent.
709 EFI_GRAPHICS_OUTPUT_PROTOCOL
*Gop
;
711 ASSERT (Event
== mGopEvent
);
714 // Check further GOPs.
720 Status
= gBS
->LocateProtocol (&gEfiGraphicsOutputProtocolGuid
, mGopTracker
,
722 if (EFI_ERROR (Status
)) {
726 Status
= QueryGopModes (Gop
, &mNumGopModes
, &mGopModes
);
727 if (EFI_ERROR (Status
)) {
731 Status
= PopulateForm (mInstalledPackages
, &gOvmfPlatformConfigGuid
,
732 FORMID_MAIN_FORM
, mNumGopModes
, mGopModes
);
733 if (EFI_ERROR (Status
)) {
734 FreePool (mGopModes
);
742 // Success -- so uninstall this callback. Closing the event removes all
743 // pending notifications and all protocol registrations.
745 Status
= gBS
->CloseEvent (mGopEvent
);
746 ASSERT_EFI_ERROR (Status
);
753 Entry point for this driver.
755 @param[in] ImageHandle Image handle of this driver.
756 @param[in] SystemTable Pointer to SystemTable.
758 @retval EFI_SUCESS Driver has loaded successfully.
759 @retval EFI_OUT_OF_RESOURCES Failed to install HII packages.
760 @return Error codes from lower level functions.
766 IN EFI_HANDLE ImageHandle
,
767 IN EFI_SYSTEM_TABLE
*SystemTable
772 ExecutePlatformConfig ();
774 mConfigAccess
.ExtractConfig
= &ExtractConfig
;
775 mConfigAccess
.RouteConfig
= &RouteConfig
;
776 mConfigAccess
.Callback
= &Callback
;
779 // Declare ourselves suitable for HII communication.
781 Status
= gBS
->InstallMultipleProtocolInterfaces (&ImageHandle
,
782 &gEfiDevicePathProtocolGuid
, &mPkgDevicePath
,
783 &gEfiHiiConfigAccessProtocolGuid
, &mConfigAccess
,
785 if (EFI_ERROR (Status
)) {
790 // Publish the HII package list to HII Database.
792 mInstalledPackages
= HiiAddPackages (
793 &gEfiCallerIdGuid
, // PackageListGuid
794 ImageHandle
, // associated DeviceHandle
795 PlatformDxeStrings
, // 1st package
796 PlatformFormsBin
, // 2nd package
799 if (mInstalledPackages
== NULL
) {
800 Status
= EFI_OUT_OF_RESOURCES
;
801 goto UninstallProtocols
;
804 Status
= gBS
->CreateEvent (EVT_NOTIFY_SIGNAL
, TPL_CALLBACK
, &GopInstalled
,
805 NULL
/* Context */, &mGopEvent
);
806 if (EFI_ERROR (Status
)) {
810 Status
= gBS
->RegisterProtocolNotify (&gEfiGraphicsOutputProtocolGuid
,
811 mGopEvent
, &mGopTracker
);
812 if (EFI_ERROR (Status
)) {
817 // Check already installed GOPs.
819 Status
= gBS
->SignalEvent (mGopEvent
);
820 ASSERT_EFI_ERROR (Status
);
825 gBS
->CloseEvent (mGopEvent
);
828 HiiRemovePackages (mInstalledPackages
);
831 gBS
->UninstallMultipleProtocolInterfaces (ImageHandle
,
832 &gEfiDevicePathProtocolGuid
, &mPkgDevicePath
,
833 &gEfiHiiConfigAccessProtocolGuid
, &mConfigAccess
,
841 @param[in] ImageHandle Handle that identifies the image to evict.
843 @retval EFI_SUCCESS The image has been unloaded.
848 IN EFI_HANDLE ImageHandle
851 if (mGopEvent
== NULL
) {
853 // The GOP callback ran successfully and unregistered itself. Release the
854 // resources allocated there.
856 ASSERT (mGopModes
!= NULL
);
857 FreePool (mGopModes
);
860 // Otherwise we need to unregister the callback.
862 ASSERT (mGopModes
== NULL
);
863 gBS
->CloseEvent (mGopEvent
);
867 // Release resources allocated by the entry point.
869 HiiRemovePackages (mInstalledPackages
);
870 gBS
->UninstallMultipleProtocolInterfaces (ImageHandle
,
871 &gEfiDevicePathProtocolGuid
, &mPkgDevicePath
,
872 &gEfiHiiConfigAccessProtocolGuid
, &mConfigAccess
,