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/DebugLib.h>
19 #include <Library/DevicePathLib.h>
20 #include <Library/HiiLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/PrintLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/UefiHiiServicesLib.h>
25 #include <Protocol/DevicePath.h>
26 #include <Protocol/GraphicsOutput.h>
27 #include <Protocol/HiiConfigAccess.h>
28 #include <Guid/MdeModuleHii.h>
29 #include <Guid/OvmfPlatformConfig.h>
32 #include "PlatformConfig.h"
35 // The HiiAddPackages() library function requires that any controller (or
36 // image) handle, to be associated with the HII packages under installation, be
37 // "decorated" with a device path. The tradition seems to be a vendor device
40 // We'd like to associate our HII packages with the driver's image handle. The
41 // first idea is to use the driver image's device path. Unfortunately, loaded
42 // images only come with an EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL (not the
43 // usual EFI_DEVICE_PATH_PROTOCOL), ie. a different GUID. In addition, even the
44 // EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL interface may be NULL, if the image
45 // has been loaded from an "unnamed" memory source buffer.
47 // Hence let's just stick with the tradition -- use a dedicated vendor device
48 // path, with the driver's FILE_GUID.
52 VENDOR_DEVICE_PATH VendorDevicePath
;
53 EFI_DEVICE_PATH_PROTOCOL End
;
57 STATIC PKG_DEVICE_PATH mPkgDevicePath
= {
63 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
) ),
64 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
) >> 8)
71 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
73 (UINT8
) (END_DEVICE_PATH_LENGTH
),
74 (UINT8
) (END_DEVICE_PATH_LENGTH
>> 8)
80 // The configuration interface between the HII engine (form display etc) and
83 STATIC EFI_HII_CONFIG_ACCESS_PROTOCOL mConfigAccess
;
86 // The handle representing our list of packages after installation.
88 STATIC EFI_HII_HANDLE mInstalledPackages
;
91 // The arrays below constitute our HII package list. They are auto-generated by
92 // the VFR compiler and linked into the driver image during the build.
94 // - The strings package receives its C identifier from the driver's BASE_NAME,
97 // - The forms package receives its C identifier from the VFR file's basename,
101 extern UINT8 PlatformDxeStrings
[];
102 extern UINT8 PlatformFormsBin
[];
105 // We want to be notified about GOP installations until we find one GOP
106 // interface that lets us populate the form.
108 STATIC EFI_EVENT mGopEvent
;
111 // The registration record underneath this pointer allows us to iterate through
112 // the GOP instances one by one.
114 STATIC VOID
*mGopTracker
;
117 // Cache the resolutions we get from the GOP.
124 STATIC UINTN mNumGopModes
;
125 STATIC GOP_MODE
*mGopModes
;
129 This function is called by the HII machinery when it fetches the form state.
131 See the precise documentation in the UEFI spec.
133 @param[in] This The Config Access Protocol instance.
135 @param[in] Request A <ConfigRequest> format UCS-2 string describing the
138 @param[out] Progress A pointer into Request on output, identifying the query
139 element where processing failed.
141 @param[out] Results A <MultiConfigAltResp> format UCS-2 string that has
142 all values filled in for the names in the Request
145 @return Status codes from gHiiConfigRouting->BlockToConfig().
152 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
153 IN CONST EFI_STRING Request
,
154 OUT EFI_STRING
*Progress
,
155 OUT EFI_STRING
*Results
158 MAIN_FORM_STATE MainFormState
;
161 DEBUG ((EFI_D_VERBOSE
, "%a: Request=\"%s\"\n", __FUNCTION__
, Request
));
163 StrnCpy ((CHAR16
*) MainFormState
.CurrentPreferredResolution
,
164 L
"Unset", MAXSIZE_RES_CUR
);
165 MainFormState
.NextPreferredResolution
= 0;
166 Status
= gHiiConfigRouting
->BlockToConfig (gHiiConfigRouting
, Request
,
167 (VOID
*) &MainFormState
, sizeof MainFormState
,
169 if (EFI_ERROR (Status
)) {
170 DEBUG ((EFI_D_ERROR
, "%a: BlockToConfig(): %r, Progress=\"%s\"\n",
171 __FUNCTION__
, Status
, (Status
== EFI_DEVICE_ERROR
) ? NULL
: *Progress
));
173 DEBUG ((EFI_D_VERBOSE
, "%a: Results=\"%s\"\n", __FUNCTION__
, *Results
));
183 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
184 IN CONST EFI_STRING Configuration
,
185 OUT EFI_STRING
*Progress
188 DEBUG ((EFI_D_VERBOSE
, "%a: Configuration=\"%s\"\n", __FUNCTION__
,
198 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
199 IN EFI_BROWSER_ACTION Action
,
200 IN EFI_QUESTION_ID QuestionId
,
202 IN OUT EFI_IFR_TYPE_VALUE
*Value
,
203 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
206 DEBUG ((EFI_D_VERBOSE
, "%a: Action=0x%Lx QuestionId=%d Type=%d\n",
207 __FUNCTION__
, (UINT64
) Action
, QuestionId
, Type
));
209 if (Action
!= EFI_BROWSER_ACTION_CHANGED
) {
210 return EFI_UNSUPPORTED
;
213 switch (QuestionId
) {
214 case QUESTION_SAVE_EXIT
:
215 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_SUBMIT_EXIT
;
218 case QUESTION_DISCARD_EXIT
:
219 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT
;
231 Query and save all resolutions supported by the GOP.
233 @param[in] Gop The Graphics Output Protocol instance to query.
235 @param[out] NumGopModes The number of modes supported by the GOP. On output,
236 this parameter will be positive.
238 @param[out] GopModes On output, a dynamically allocated array containing
239 the resolutions returned by the GOP. The caller is
240 responsible for freeing the array after use.
242 @retval EFI_UNSUPPORTED No modes found.
243 @retval EFI_OUT_OF_RESOURCES Failed to allocate GopModes.
244 @return Error codes from Gop->QueryMode().
251 IN EFI_GRAPHICS_OUTPUT_PROTOCOL
*Gop
,
252 OUT UINTN
*NumGopModes
,
253 OUT GOP_MODE
**GopModes
259 if (Gop
->Mode
->MaxMode
== 0) {
260 return EFI_UNSUPPORTED
;
262 *NumGopModes
= Gop
->Mode
->MaxMode
;
264 *GopModes
= AllocatePool (Gop
->Mode
->MaxMode
* sizeof **GopModes
);
265 if (*GopModes
== NULL
) {
266 return EFI_OUT_OF_RESOURCES
;
269 for (ModeNumber
= 0; ModeNumber
< Gop
->Mode
->MaxMode
; ++ModeNumber
) {
270 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
*Info
;
273 Status
= Gop
->QueryMode (Gop
, ModeNumber
, &SizeOfInfo
, &Info
);
274 if (EFI_ERROR (Status
)) {
278 (*GopModes
)[ModeNumber
].X
= Info
->HorizontalResolution
;
279 (*GopModes
)[ModeNumber
].Y
= Info
->VerticalResolution
;
286 FreePool (*GopModes
);
293 Create a set of "one-of-many" (ie. "drop down list") option IFR opcodes,
294 based on available GOP resolutions, to be placed under a "one-of-many" (ie.
295 "drop down list") opcode.
297 @param[in] PackageList The package list with the formset and form for
298 which the drop down options are produced. Option
299 names are added as new strings to PackageList.
301 @param[out] OpCodeBuffer On output, a dynamically allocated opcode buffer
302 with drop down list options corresponding to GOP
303 resolutions. The caller is responsible for freeing
304 OpCodeBuffer with HiiFreeOpCodeHandle() after use.
306 @param[in] NumGopModes Number of entries in GopModes.
308 @param[in] GopModes Array of resolutions retrieved from the GOP.
310 @retval EFI_SUCESS Opcodes have been successfully produced.
312 @return Status codes from underlying functions. PackageList may
313 have been extended with new strings. OpCodeBuffer is
319 CreateResolutionOptions (
320 IN EFI_HII_HANDLE
*PackageList
,
321 OUT VOID
**OpCodeBuffer
,
322 IN UINTN NumGopModes
,
323 IN GOP_MODE
*GopModes
330 OutputBuffer
= HiiAllocateOpCodeHandle ();
331 if (OutputBuffer
== NULL
) {
332 return EFI_OUT_OF_RESOURCES
;
335 for (ModeNumber
= 0; ModeNumber
< NumGopModes
; ++ModeNumber
) {
336 CHAR16 Desc
[MAXSIZE_RES_CUR
];
337 EFI_STRING_ID NewString
;
340 UnicodeSPrintAsciiFormat (Desc
, sizeof Desc
, "%Ldx%Ld",
341 (INT64
) GopModes
[ModeNumber
].X
, (INT64
) GopModes
[ModeNumber
].Y
);
342 NewString
= HiiSetString (PackageList
, 0 /* new string */, Desc
,
343 NULL
/* for all languages */);
344 if (NewString
== 0) {
345 Status
= EFI_OUT_OF_RESOURCES
;
346 goto FreeOutputBuffer
;
348 OpCode
= HiiCreateOneOfOptionOpCode (OutputBuffer
, NewString
,
349 0 /* Flags */, EFI_IFR_NUMERIC_SIZE_4
, ModeNumber
);
350 if (OpCode
== NULL
) {
351 Status
= EFI_OUT_OF_RESOURCES
;
352 goto FreeOutputBuffer
;
356 *OpCodeBuffer
= OutputBuffer
;
360 HiiFreeOpCodeHandle (OutputBuffer
);
367 Populate the form identified by the (PackageList, FormSetGuid, FormId)
370 The drop down list of video resolutions is generated from (NumGopModes,
373 @retval EFI_SUCESS Form successfully updated.
374 @return Status codes from underlying functions.
381 IN EFI_HII_HANDLE
*PackageList
,
382 IN EFI_GUID
*FormSetGuid
,
383 IN EFI_FORM_ID FormId
,
384 IN UINTN NumGopModes
,
385 IN GOP_MODE
*GopModes
391 EFI_IFR_GUID_LABEL
*Anchor
;
394 OpCodeBuffer2
= NULL
;
397 // 1. Allocate an empty opcode buffer.
399 OpCodeBuffer
= HiiAllocateOpCodeHandle ();
400 if (OpCodeBuffer
== NULL
) {
401 return EFI_OUT_OF_RESOURCES
;
405 // 2. Create a label opcode (which is a Tiano extension) inside the buffer.
406 // The label's number must match the "anchor" label in the form.
408 OpCode
= HiiCreateGuidOpCode (OpCodeBuffer
, &gEfiIfrTianoGuid
,
409 NULL
/* optional copy origin */, sizeof *Anchor
);
410 if (OpCode
== NULL
) {
411 Status
= EFI_OUT_OF_RESOURCES
;
412 goto FreeOpCodeBuffer
;
415 Anchor
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
416 Anchor
->Number
= LABEL_RES_NEXT
;
419 // 3. Create the opcodes inside the buffer that are to be inserted into the
422 // 3.1. Get a list of resolutions.
424 Status
= CreateResolutionOptions (PackageList
, &OpCodeBuffer2
,
425 NumGopModes
, GopModes
);
426 if (EFI_ERROR (Status
)) {
427 goto FreeOpCodeBuffer
;
431 // 3.2. Create a one-of-many question with the above options.
433 OpCode
= HiiCreateOneOfOpCode (
434 OpCodeBuffer
, // create opcode inside this
436 QUESTION_RES_NEXT
, // ID of question,
437 FORMSTATEID_MAIN_FORM
, // identifies form state
439 (UINT16
) OFFSET_OF (MAIN_FORM_STATE
, // value of question stored
440 NextPreferredResolution
), // at this offset,
441 STRING_TOKEN (STR_RES_NEXT
), // Prompt,
442 STRING_TOKEN (STR_RES_NEXT_HELP
), // Help,
444 EFI_IFR_NUMERIC_SIZE_4
, // see sizeof
445 // NextPreferredResolution,
446 OpCodeBuffer2
, // buffer with possible
448 NULL
// DEFAULT opcodes
450 if (OpCode
== NULL
) {
451 Status
= EFI_OUT_OF_RESOURCES
;
452 goto FreeOpCodeBuffer2
;
456 // 4. Update the form with the opcode buffer.
458 Status
= HiiUpdateForm (PackageList
, FormSetGuid
, FormId
,
459 OpCodeBuffer
, // buffer with head anchor, and new contents to be
461 NULL
// buffer with tail anchor, for deleting old
466 HiiFreeOpCodeHandle (OpCodeBuffer2
);
469 HiiFreeOpCodeHandle (OpCodeBuffer
);
476 Load and execute the platform configuration.
478 @retval EFI_SUCCESS Configuration loaded and executed.
479 @return Status codes from PlatformConfigLoad().
484 ExecutePlatformConfig (
489 PLATFORM_CONFIG PlatformConfig
;
490 UINT64 OptionalElements
;
492 Status
= PlatformConfigLoad (&PlatformConfig
, &OptionalElements
);
493 if (EFI_ERROR (Status
)) {
494 DEBUG (((Status
== EFI_NOT_FOUND
) ? EFI_D_VERBOSE
: EFI_D_ERROR
,
495 "%a: failed to load platform config: %r\n", __FUNCTION__
, Status
));
499 if (OptionalElements
& PLATFORM_CONFIG_F_GRAPHICS_RESOLUTION
) {
501 // Pass the preferred resolution to GraphicsConsoleDxe via dynamic PCDs.
503 PcdSet32 (PcdVideoHorizontalResolution
,
504 PlatformConfig
.HorizontalResolution
);
505 PcdSet32 (PcdVideoVerticalResolution
,
506 PlatformConfig
.VerticalResolution
);
514 Notification callback for GOP interface installation.
516 @param[in] Event Event whose notification function is being invoked.
518 @param[in] Context The pointer to the notification function's context, which
519 is implementation-dependent.
530 EFI_GRAPHICS_OUTPUT_PROTOCOL
*Gop
;
532 ASSERT (Event
== mGopEvent
);
535 // Check further GOPs.
541 Status
= gBS
->LocateProtocol (&gEfiGraphicsOutputProtocolGuid
, mGopTracker
,
543 if (EFI_ERROR (Status
)) {
547 Status
= QueryGopModes (Gop
, &mNumGopModes
, &mGopModes
);
548 if (EFI_ERROR (Status
)) {
552 Status
= PopulateForm (mInstalledPackages
, &gOvmfPlatformConfigGuid
,
553 FORMID_MAIN_FORM
, mNumGopModes
, mGopModes
);
554 if (EFI_ERROR (Status
)) {
555 FreePool (mGopModes
);
563 // Success -- so uninstall this callback. Closing the event removes all
564 // pending notifications and all protocol registrations.
566 Status
= gBS
->CloseEvent (mGopEvent
);
567 ASSERT_EFI_ERROR (Status
);
574 Entry point for this driver.
576 @param[in] ImageHandle Image handle of this driver.
577 @param[in] SystemTable Pointer to SystemTable.
579 @retval EFI_SUCESS Driver has loaded successfully.
580 @retval EFI_OUT_OF_RESOURCES Failed to install HII packages.
581 @return Error codes from lower level functions.
587 IN EFI_HANDLE ImageHandle
,
588 IN EFI_SYSTEM_TABLE
*SystemTable
593 ExecutePlatformConfig ();
595 mConfigAccess
.ExtractConfig
= &ExtractConfig
;
596 mConfigAccess
.RouteConfig
= &RouteConfig
;
597 mConfigAccess
.Callback
= &Callback
;
600 // Declare ourselves suitable for HII communication.
602 Status
= gBS
->InstallMultipleProtocolInterfaces (&ImageHandle
,
603 &gEfiDevicePathProtocolGuid
, &mPkgDevicePath
,
604 &gEfiHiiConfigAccessProtocolGuid
, &mConfigAccess
,
606 if (EFI_ERROR (Status
)) {
611 // Publish the HII package list to HII Database.
613 mInstalledPackages
= HiiAddPackages (
614 &gEfiCallerIdGuid
, // PackageListGuid
615 ImageHandle
, // associated DeviceHandle
616 PlatformDxeStrings
, // 1st package
617 PlatformFormsBin
, // 2nd package
620 if (mInstalledPackages
== NULL
) {
621 Status
= EFI_OUT_OF_RESOURCES
;
622 goto UninstallProtocols
;
625 Status
= gBS
->CreateEvent (EVT_NOTIFY_SIGNAL
, TPL_CALLBACK
, &GopInstalled
,
626 NULL
/* Context */, &mGopEvent
);
627 if (EFI_ERROR (Status
)) {
631 Status
= gBS
->RegisterProtocolNotify (&gEfiGraphicsOutputProtocolGuid
,
632 mGopEvent
, &mGopTracker
);
633 if (EFI_ERROR (Status
)) {
638 // Check already installed GOPs.
640 Status
= gBS
->SignalEvent (mGopEvent
);
641 ASSERT_EFI_ERROR (Status
);
646 gBS
->CloseEvent (mGopEvent
);
649 HiiRemovePackages (mInstalledPackages
);
652 gBS
->UninstallMultipleProtocolInterfaces (ImageHandle
,
653 &gEfiDevicePathProtocolGuid
, &mPkgDevicePath
,
654 &gEfiHiiConfigAccessProtocolGuid
, &mConfigAccess
,
662 @param[in] ImageHandle Handle that identifies the image to evict.
664 @retval EFI_SUCCESS The image has been unloaded.
669 IN EFI_HANDLE ImageHandle
672 if (mGopEvent
== NULL
) {
674 // The GOP callback ran successfully and unregistered itself. Release the
675 // resources allocated there.
677 ASSERT (mGopModes
!= NULL
);
678 FreePool (mGopModes
);
681 // Otherwise we need to unregister the callback.
683 ASSERT (mGopModes
== NULL
);
684 gBS
->CloseEvent (mGopEvent
);
688 // Release resources allocated by the entry point.
690 HiiRemovePackages (mInstalledPackages
);
691 gBS
->UninstallMultipleProtocolInterfaces (ImageHandle
,
692 &gEfiDevicePathProtocolGuid
, &mPkgDevicePath
,
693 &gEfiHiiConfigAccessProtocolGuid
, &mConfigAccess
,