]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - OvmfPkg/PlatformDxe/Platform.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / OvmfPkg / PlatformDxe / Platform.c
... / ...
CommitLineData
1/** @file\r
2 This driver effectuates OVMF's platform configuration settings and exposes\r
3 them via HII.\r
4\r
5 Copyright (C) 2014, Red Hat, Inc.\r
6 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
7\r
8 SPDX-License-Identifier: BSD-2-Clause-Patent\r
9**/\r
10\r
11#include <Library/BaseLib.h>\r
12#include <Library/BaseMemoryLib.h>\r
13#include <Library/DebugLib.h>\r
14#include <Library/DevicePathLib.h>\r
15#include <Library/HiiLib.h>\r
16#include <Library/MemoryAllocationLib.h>\r
17#include <Library/PrintLib.h>\r
18#include <Library/UefiBootServicesTableLib.h>\r
19#include <Library/UefiHiiServicesLib.h>\r
20#include <Protocol/DevicePath.h>\r
21#include <Protocol/GraphicsOutput.h>\r
22#include <Protocol/HiiConfigAccess.h>\r
23#include <Guid/MdeModuleHii.h>\r
24#include <Guid/OvmfPlatformConfig.h>\r
25\r
26#include "Platform.h"\r
27#include "PlatformConfig.h"\r
28\r
29//\r
30// The HiiAddPackages() library function requires that any controller (or\r
31// image) handle, to be associated with the HII packages under installation, be\r
32// "decorated" with a device path. The tradition seems to be a vendor device\r
33// path.\r
34//\r
35// We'd like to associate our HII packages with the driver's image handle. The\r
36// first idea is to use the driver image's device path. Unfortunately, loaded\r
37// images only come with an EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL (not the\r
38// usual EFI_DEVICE_PATH_PROTOCOL), ie. a different GUID. In addition, even the\r
39// EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL interface may be NULL, if the image\r
40// has been loaded from an "unnamed" memory source buffer.\r
41//\r
42// Hence let's just stick with the tradition -- use a dedicated vendor device\r
43// path, with the driver's FILE_GUID.\r
44//\r
45#pragma pack(1)\r
46typedef struct {\r
47 VENDOR_DEVICE_PATH VendorDevicePath;\r
48 EFI_DEVICE_PATH_PROTOCOL End;\r
49} PKG_DEVICE_PATH;\r
50#pragma pack()\r
51\r
52STATIC PKG_DEVICE_PATH mPkgDevicePath = {\r
53 {\r
54 {\r
55 HARDWARE_DEVICE_PATH,\r
56 HW_VENDOR_DP,\r
57 {\r
58 (UINT8)(sizeof (VENDOR_DEVICE_PATH)),\r
59 (UINT8)(sizeof (VENDOR_DEVICE_PATH) >> 8)\r
60 }\r
61 },\r
62 EFI_CALLER_ID_GUID\r
63 },\r
64 {\r
65 END_DEVICE_PATH_TYPE,\r
66 END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
67 {\r
68 (UINT8)(END_DEVICE_PATH_LENGTH),\r
69 (UINT8)(END_DEVICE_PATH_LENGTH >> 8)\r
70 }\r
71 }\r
72};\r
73\r
74//\r
75// The configuration interface between the HII engine (form display etc) and\r
76// this driver.\r
77//\r
78STATIC EFI_HII_CONFIG_ACCESS_PROTOCOL mConfigAccess;\r
79\r
80//\r
81// The handle representing our list of packages after installation.\r
82//\r
83STATIC EFI_HII_HANDLE mInstalledPackages;\r
84\r
85//\r
86// The arrays below constitute our HII package list. They are auto-generated by\r
87// the VFR compiler and linked into the driver image during the build.\r
88//\r
89// - The strings package receives its C identifier from the driver's BASE_NAME,\r
90// plus "Strings".\r
91//\r
92// - The forms package receives its C identifier from the VFR file's basename,\r
93// plus "Bin".\r
94//\r
95//\r
96extern UINT8 PlatformDxeStrings[];\r
97extern UINT8 PlatformFormsBin[];\r
98\r
99//\r
100// We want to be notified about GOP installations until we find one GOP\r
101// interface that lets us populate the form.\r
102//\r
103STATIC EFI_EVENT mGopEvent;\r
104\r
105//\r
106// The registration record underneath this pointer allows us to iterate through\r
107// the GOP instances one by one.\r
108//\r
109STATIC VOID *mGopTracker;\r
110\r
111//\r
112// Cache the resolutions we get from the GOP.\r
113//\r
114typedef struct {\r
115 UINT32 X;\r
116 UINT32 Y;\r
117} GOP_MODE;\r
118\r
119STATIC UINTN mNumGopModes;\r
120STATIC GOP_MODE *mGopModes;\r
121\r
122/**\r
123 Load the persistent platform configuration and translate it to binary form\r
124 state.\r
125\r
126 If the platform configuration is missing, then the function fills in a\r
127 default state.\r
128\r
129 @param[out] MainFormState Binary form/widget state after translation.\r
130\r
131 @retval EFI_SUCCESS Form/widget state ready.\r
132 @return Error codes from underlying functions.\r
133**/\r
134STATIC\r
135EFI_STATUS\r
136EFIAPI\r
137PlatformConfigToFormState (\r
138 OUT MAIN_FORM_STATE *MainFormState\r
139 )\r
140{\r
141 EFI_STATUS Status;\r
142 PLATFORM_CONFIG PlatformConfig;\r
143 UINT64 OptionalElements;\r
144 UINTN ModeNumber;\r
145\r
146 ZeroMem (MainFormState, sizeof *MainFormState);\r
147\r
148 Status = PlatformConfigLoad (&PlatformConfig, &OptionalElements);\r
149 switch (Status) {\r
150 case EFI_SUCCESS:\r
151 if (OptionalElements & PLATFORM_CONFIG_F_GRAPHICS_RESOLUTION) {\r
152 //\r
153 // Format the preferred resolution as text.\r
154 //\r
155 UnicodeSPrintAsciiFormat (\r
156 (CHAR16 *)MainFormState->CurrentPreferredResolution,\r
157 sizeof MainFormState->CurrentPreferredResolution,\r
158 "%Ldx%Ld",\r
159 (INT64)PlatformConfig.HorizontalResolution,\r
160 (INT64)PlatformConfig.VerticalResolution\r
161 );\r
162\r
163 //\r
164 // Try to locate it in the drop-down list too. This may not succeed, but\r
165 // that's fine.\r
166 //\r
167 for (ModeNumber = 0; ModeNumber < mNumGopModes; ++ModeNumber) {\r
168 if ((mGopModes[ModeNumber].X == PlatformConfig.HorizontalResolution) &&\r
169 (mGopModes[ModeNumber].Y == PlatformConfig.VerticalResolution))\r
170 {\r
171 MainFormState->NextPreferredResolution = (UINT32)ModeNumber;\r
172 break;\r
173 }\r
174 }\r
175\r
176 break;\r
177 }\r
178\r
179 //\r
180 // fall through otherwise\r
181 //\r
182\r
183 case EFI_NOT_FOUND:\r
184 UnicodeSPrintAsciiFormat (\r
185 (CHAR16 *)MainFormState->CurrentPreferredResolution,\r
186 sizeof MainFormState->CurrentPreferredResolution,\r
187 "Unset"\r
188 );\r
189 break;\r
190\r
191 default:\r
192 return Status;\r
193 }\r
194\r
195 return EFI_SUCCESS;\r
196}\r
197\r
198/**\r
199 This function is called by the HII machinery when it fetches the form state.\r
200\r
201 See the precise documentation in the UEFI spec.\r
202\r
203 @param[in] This The Config Access Protocol instance.\r
204\r
205 @param[in] Request A <ConfigRequest> format UCS-2 string describing the\r
206 query.\r
207\r
208 @param[out] Progress A pointer into Request on output, identifying the query\r
209 element where processing failed.\r
210\r
211 @param[out] Results A <MultiConfigAltResp> format UCS-2 string that has\r
212 all values filled in for the names in the Request\r
213 string.\r
214\r
215 @retval EFI_SUCCESS Extraction of form state in <MultiConfigAltResp>\r
216 encoding successful.\r
217 @return Status codes from underlying functions.\r
218\r
219**/\r
220STATIC\r
221EFI_STATUS\r
222EFIAPI\r
223ExtractConfig (\r
224 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r
225 IN CONST EFI_STRING Request,\r
226 OUT EFI_STRING *Progress,\r
227 OUT EFI_STRING *Results\r
228 )\r
229{\r
230 MAIN_FORM_STATE MainFormState;\r
231 EFI_STATUS Status;\r
232\r
233 DEBUG ((DEBUG_VERBOSE, "%a: Request=\"%s\"\n", __FUNCTION__, Request));\r
234\r
235 Status = PlatformConfigToFormState (&MainFormState);\r
236 if (EFI_ERROR (Status)) {\r
237 *Progress = Request;\r
238 return Status;\r
239 }\r
240\r
241 //\r
242 // Answer the textual request keying off the binary form state.\r
243 //\r
244 Status = gHiiConfigRouting->BlockToConfig (\r
245 gHiiConfigRouting,\r
246 Request,\r
247 (VOID *)&MainFormState,\r
248 sizeof MainFormState,\r
249 Results,\r
250 Progress\r
251 );\r
252 if (EFI_ERROR (Status)) {\r
253 DEBUG ((\r
254 DEBUG_ERROR,\r
255 "%a: BlockToConfig(): %r, Progress=\"%s\"\n",\r
256 __FUNCTION__,\r
257 Status,\r
258 (Status == EFI_DEVICE_ERROR) ? NULL : *Progress\r
259 ));\r
260 } else {\r
261 DEBUG ((DEBUG_VERBOSE, "%a: Results=\"%s\"\n", __FUNCTION__, *Results));\r
262 }\r
263\r
264 return Status;\r
265}\r
266\r
267/**\r
268 Interpret the binary form state and save it as persistent platform\r
269 configuration.\r
270\r
271 @param[in] MainFormState Binary form/widget state to verify and save.\r
272\r
273 @retval EFI_SUCCESS Platform configuration saved.\r
274 @return Error codes from underlying functions.\r
275**/\r
276STATIC\r
277EFI_STATUS\r
278EFIAPI\r
279FormStateToPlatformConfig (\r
280 IN CONST MAIN_FORM_STATE *MainFormState\r
281 )\r
282{\r
283 EFI_STATUS Status;\r
284 PLATFORM_CONFIG PlatformConfig;\r
285 CONST GOP_MODE *GopMode;\r
286\r
287 //\r
288 // There's nothing to do with the textual CurrentPreferredResolution field.\r
289 // We verify and translate the selection in the drop-down list.\r
290 //\r
291 if (MainFormState->NextPreferredResolution >= mNumGopModes) {\r
292 return EFI_INVALID_PARAMETER;\r
293 }\r
294\r
295 GopMode = mGopModes + MainFormState->NextPreferredResolution;\r
296\r
297 ZeroMem (&PlatformConfig, sizeof PlatformConfig);\r
298 PlatformConfig.HorizontalResolution = GopMode->X;\r
299 PlatformConfig.VerticalResolution = GopMode->Y;\r
300\r
301 Status = PlatformConfigSave (&PlatformConfig);\r
302 return Status;\r
303}\r
304\r
305/**\r
306 This function is called by the HII machinery when it wants the driver to\r
307 interpret and persist the form state.\r
308\r
309 See the precise documentation in the UEFI spec.\r
310\r
311 @param[in] This The Config Access Protocol instance.\r
312\r
313 @param[in] Configuration A <ConfigResp> format UCS-2 string describing the\r
314 form state.\r
315\r
316 @param[out] Progress A pointer into Configuration on output,\r
317 identifying the element where processing failed.\r
318\r
319 @retval EFI_SUCCESS Configuration verified, state permanent.\r
320\r
321 @return Status codes from underlying functions.\r
322**/\r
323STATIC\r
324EFI_STATUS\r
325EFIAPI\r
326RouteConfig (\r
327 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r
328 IN CONST EFI_STRING Configuration,\r
329 OUT EFI_STRING *Progress\r
330 )\r
331{\r
332 MAIN_FORM_STATE MainFormState;\r
333 UINTN BlockSize;\r
334 EFI_STATUS Status;\r
335\r
336 DEBUG ((\r
337 DEBUG_VERBOSE,\r
338 "%a: Configuration=\"%s\"\n",\r
339 __FUNCTION__,\r
340 Configuration\r
341 ));\r
342\r
343 //\r
344 // the "read" step in RMW\r
345 //\r
346 Status = PlatformConfigToFormState (&MainFormState);\r
347 if (EFI_ERROR (Status)) {\r
348 *Progress = Configuration;\r
349 return Status;\r
350 }\r
351\r
352 //\r
353 // the "modify" step in RMW\r
354 //\r
355 // (Update the binary form state. This update may be partial, which is why in\r
356 // general we must pre-load the form state from the platform config.)\r
357 //\r
358 BlockSize = sizeof MainFormState;\r
359 Status = gHiiConfigRouting->ConfigToBlock (\r
360 gHiiConfigRouting,\r
361 Configuration,\r
362 (VOID *)&MainFormState,\r
363 &BlockSize,\r
364 Progress\r
365 );\r
366 if (EFI_ERROR (Status)) {\r
367 DEBUG ((\r
368 DEBUG_ERROR,\r
369 "%a: ConfigToBlock(): %r, Progress=\"%s\"\n",\r
370 __FUNCTION__,\r
371 Status,\r
372 (Status == EFI_BUFFER_TOO_SMALL) ? NULL : *Progress\r
373 ));\r
374 return Status;\r
375 }\r
376\r
377 //\r
378 // the "write" step in RMW\r
379 //\r
380 Status = FormStateToPlatformConfig (&MainFormState);\r
381 if (EFI_ERROR (Status)) {\r
382 *Progress = Configuration;\r
383 }\r
384\r
385 return Status;\r
386}\r
387\r
388STATIC\r
389EFI_STATUS\r
390EFIAPI\r
391Callback (\r
392 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r
393 IN EFI_BROWSER_ACTION Action,\r
394 IN EFI_QUESTION_ID QuestionId,\r
395 IN UINT8 Type,\r
396 IN OUT EFI_IFR_TYPE_VALUE *Value,\r
397 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest\r
398 )\r
399{\r
400 DEBUG ((\r
401 DEBUG_VERBOSE,\r
402 "%a: Action=0x%Lx QuestionId=%d Type=%d\n",\r
403 __FUNCTION__,\r
404 (UINT64)Action,\r
405 QuestionId,\r
406 Type\r
407 ));\r
408\r
409 if (Action != EFI_BROWSER_ACTION_CHANGED) {\r
410 return EFI_UNSUPPORTED;\r
411 }\r
412\r
413 switch (QuestionId) {\r
414 case QUESTION_SAVE_EXIT:\r
415 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_SUBMIT_EXIT;\r
416 break;\r
417\r
418 case QUESTION_DISCARD_EXIT:\r
419 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT;\r
420 break;\r
421\r
422 default:\r
423 break;\r
424 }\r
425\r
426 return EFI_SUCCESS;\r
427}\r
428\r
429/**\r
430 Query and save all resolutions supported by the GOP.\r
431\r
432 @param[in] Gop The Graphics Output Protocol instance to query.\r
433\r
434 @param[out] NumGopModes The number of modes supported by the GOP. On output,\r
435 this parameter will be positive.\r
436\r
437 @param[out] GopModes On output, a dynamically allocated array containing\r
438 the resolutions returned by the GOP. The caller is\r
439 responsible for freeing the array after use.\r
440\r
441 @retval EFI_UNSUPPORTED No modes found.\r
442 @retval EFI_OUT_OF_RESOURCES Failed to allocate GopModes.\r
443 @return Error codes from Gop->QueryMode().\r
444\r
445**/\r
446STATIC\r
447EFI_STATUS\r
448EFIAPI\r
449QueryGopModes (\r
450 IN EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop,\r
451 OUT UINTN *NumGopModes,\r
452 OUT GOP_MODE **GopModes\r
453 )\r
454{\r
455 EFI_STATUS Status;\r
456 UINT32 ModeNumber;\r
457\r
458 if (Gop->Mode->MaxMode == 0) {\r
459 return EFI_UNSUPPORTED;\r
460 }\r
461\r
462 *NumGopModes = Gop->Mode->MaxMode;\r
463\r
464 *GopModes = AllocatePool (Gop->Mode->MaxMode * sizeof **GopModes);\r
465 if (*GopModes == NULL) {\r
466 return EFI_OUT_OF_RESOURCES;\r
467 }\r
468\r
469 for (ModeNumber = 0; ModeNumber < Gop->Mode->MaxMode; ++ModeNumber) {\r
470 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;\r
471 UINTN SizeOfInfo;\r
472\r
473 Status = Gop->QueryMode (Gop, ModeNumber, &SizeOfInfo, &Info);\r
474 if (EFI_ERROR (Status)) {\r
475 goto FreeGopModes;\r
476 }\r
477\r
478 (*GopModes)[ModeNumber].X = Info->HorizontalResolution;\r
479 (*GopModes)[ModeNumber].Y = Info->VerticalResolution;\r
480 FreePool (Info);\r
481 }\r
482\r
483 return EFI_SUCCESS;\r
484\r
485FreeGopModes:\r
486 FreePool (*GopModes);\r
487\r
488 return Status;\r
489}\r
490\r
491/**\r
492 Create a set of "one-of-many" (ie. "drop down list") option IFR opcodes,\r
493 based on available GOP resolutions, to be placed under a "one-of-many" (ie.\r
494 "drop down list") opcode.\r
495\r
496 @param[in] PackageList The package list with the formset and form for\r
497 which the drop down options are produced. Option\r
498 names are added as new strings to PackageList.\r
499\r
500 @param[out] OpCodeBuffer On output, a dynamically allocated opcode buffer\r
501 with drop down list options corresponding to GOP\r
502 resolutions. The caller is responsible for freeing\r
503 OpCodeBuffer with HiiFreeOpCodeHandle() after use.\r
504\r
505 @param[in] NumGopModes Number of entries in GopModes.\r
506\r
507 @param[in] GopModes Array of resolutions retrieved from the GOP.\r
508\r
509 @retval EFI_SUCESS Opcodes have been successfully produced.\r
510\r
511 @return Status codes from underlying functions. PackageList may\r
512 have been extended with new strings. OpCodeBuffer is\r
513 unchanged.\r
514**/\r
515STATIC\r
516EFI_STATUS\r
517EFIAPI\r
518CreateResolutionOptions (\r
519 IN EFI_HII_HANDLE PackageList,\r
520 OUT VOID **OpCodeBuffer,\r
521 IN UINTN NumGopModes,\r
522 IN GOP_MODE *GopModes\r
523 )\r
524{\r
525 EFI_STATUS Status;\r
526 VOID *OutputBuffer;\r
527 UINTN ModeNumber;\r
528\r
529 OutputBuffer = HiiAllocateOpCodeHandle ();\r
530 if (OutputBuffer == NULL) {\r
531 return EFI_OUT_OF_RESOURCES;\r
532 }\r
533\r
534 for (ModeNumber = 0; ModeNumber < NumGopModes; ++ModeNumber) {\r
535 CHAR16 Desc[MAXSIZE_RES_CUR];\r
536 EFI_STRING_ID NewString;\r
537 VOID *OpCode;\r
538\r
539 UnicodeSPrintAsciiFormat (\r
540 Desc,\r
541 sizeof Desc,\r
542 "%Ldx%Ld",\r
543 (INT64)GopModes[ModeNumber].X,\r
544 (INT64)GopModes[ModeNumber].Y\r
545 );\r
546 NewString = HiiSetString (\r
547 PackageList,\r
548 0 /* new string */,\r
549 Desc,\r
550 NULL /* for all languages */\r
551 );\r
552 if (NewString == 0) {\r
553 Status = EFI_OUT_OF_RESOURCES;\r
554 goto FreeOutputBuffer;\r
555 }\r
556\r
557 OpCode = HiiCreateOneOfOptionOpCode (\r
558 OutputBuffer,\r
559 NewString,\r
560 0 /* Flags */,\r
561 EFI_IFR_NUMERIC_SIZE_4,\r
562 ModeNumber\r
563 );\r
564 if (OpCode == NULL) {\r
565 Status = EFI_OUT_OF_RESOURCES;\r
566 goto FreeOutputBuffer;\r
567 }\r
568 }\r
569\r
570 *OpCodeBuffer = OutputBuffer;\r
571 return EFI_SUCCESS;\r
572\r
573FreeOutputBuffer:\r
574 HiiFreeOpCodeHandle (OutputBuffer);\r
575\r
576 return Status;\r
577}\r
578\r
579/**\r
580 Populate the form identified by the (PackageList, FormSetGuid, FormId)\r
581 triplet.\r
582\r
583 The drop down list of video resolutions is generated from (NumGopModes,\r
584 GopModes).\r
585\r
586 @retval EFI_SUCESS Form successfully updated.\r
587 @return Status codes from underlying functions.\r
588\r
589**/\r
590STATIC\r
591EFI_STATUS\r
592EFIAPI\r
593PopulateForm (\r
594 IN EFI_HII_HANDLE PackageList,\r
595 IN EFI_GUID *FormSetGuid,\r
596 IN EFI_FORM_ID FormId,\r
597 IN UINTN NumGopModes,\r
598 IN GOP_MODE *GopModes\r
599 )\r
600{\r
601 EFI_STATUS Status;\r
602 VOID *OpCodeBuffer;\r
603 VOID *OpCode;\r
604 EFI_IFR_GUID_LABEL *Anchor;\r
605 VOID *OpCodeBuffer2;\r
606\r
607 OpCodeBuffer2 = NULL;\r
608\r
609 //\r
610 // 1. Allocate an empty opcode buffer.\r
611 //\r
612 OpCodeBuffer = HiiAllocateOpCodeHandle ();\r
613 if (OpCodeBuffer == NULL) {\r
614 return EFI_OUT_OF_RESOURCES;\r
615 }\r
616\r
617 //\r
618 // 2. Create a label opcode (which is a Tiano extension) inside the buffer.\r
619 // The label's number must match the "anchor" label in the form.\r
620 //\r
621 OpCode = HiiCreateGuidOpCode (\r
622 OpCodeBuffer,\r
623 &gEfiIfrTianoGuid,\r
624 NULL /* optional copy origin */,\r
625 sizeof *Anchor\r
626 );\r
627 if (OpCode == NULL) {\r
628 Status = EFI_OUT_OF_RESOURCES;\r
629 goto FreeOpCodeBuffer;\r
630 }\r
631\r
632 Anchor = OpCode;\r
633 Anchor->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
634 Anchor->Number = LABEL_RES_NEXT;\r
635\r
636 //\r
637 // 3. Create the opcodes inside the buffer that are to be inserted into the\r
638 // form.\r
639 //\r
640 // 3.1. Get a list of resolutions.\r
641 //\r
642 Status = CreateResolutionOptions (\r
643 PackageList,\r
644 &OpCodeBuffer2,\r
645 NumGopModes,\r
646 GopModes\r
647 );\r
648 if (EFI_ERROR (Status)) {\r
649 goto FreeOpCodeBuffer;\r
650 }\r
651\r
652 //\r
653 // 3.2. Create a one-of-many question with the above options.\r
654 //\r
655 OpCode = HiiCreateOneOfOpCode (\r
656 OpCodeBuffer, // create opcode inside this\r
657 // opcode buffer,\r
658 QUESTION_RES_NEXT, // ID of question,\r
659 FORMSTATEID_MAIN_FORM, // identifies form state\r
660 // storage,\r
661 (UINT16)OFFSET_OF (\r
662 MAIN_FORM_STATE, // value of question stored\r
663 NextPreferredResolution\r
664 ), // at this offset,\r
665 STRING_TOKEN (STR_RES_NEXT), // Prompt,\r
666 STRING_TOKEN (STR_RES_NEXT_HELP), // Help,\r
667 0, // QuestionFlags,\r
668 EFI_IFR_NUMERIC_SIZE_4, // see sizeof\r
669 // NextPreferredResolution,\r
670 OpCodeBuffer2, // buffer with possible\r
671 // choices,\r
672 NULL // DEFAULT opcodes\r
673 );\r
674 if (OpCode == NULL) {\r
675 Status = EFI_OUT_OF_RESOURCES;\r
676 goto FreeOpCodeBuffer2;\r
677 }\r
678\r
679 //\r
680 // 4. Update the form with the opcode buffer.\r
681 //\r
682 Status = HiiUpdateForm (\r
683 PackageList,\r
684 FormSetGuid,\r
685 FormId,\r
686 OpCodeBuffer, // buffer with head anchor, and new contents to be\r
687 // inserted at it\r
688 NULL // buffer with tail anchor, for deleting old\r
689 // contents up to it\r
690 );\r
691\r
692FreeOpCodeBuffer2:\r
693 HiiFreeOpCodeHandle (OpCodeBuffer2);\r
694\r
695FreeOpCodeBuffer:\r
696 HiiFreeOpCodeHandle (OpCodeBuffer);\r
697\r
698 return Status;\r
699}\r
700\r
701/**\r
702 Load and execute the platform configuration.\r
703\r
704 @retval EFI_SUCCESS Configuration loaded and executed.\r
705 @return Status codes from PlatformConfigLoad().\r
706**/\r
707STATIC\r
708EFI_STATUS\r
709EFIAPI\r
710ExecutePlatformConfig (\r
711 VOID\r
712 )\r
713{\r
714 EFI_STATUS Status;\r
715 PLATFORM_CONFIG PlatformConfig;\r
716 UINT64 OptionalElements;\r
717 RETURN_STATUS PcdStatus;\r
718\r
719 Status = PlatformConfigLoad (&PlatformConfig, &OptionalElements);\r
720 if (EFI_ERROR (Status)) {\r
721 DEBUG ((\r
722 (Status == EFI_NOT_FOUND) ? DEBUG_VERBOSE : DEBUG_ERROR,\r
723 "%a: failed to load platform config: %r\n",\r
724 __FUNCTION__,\r
725 Status\r
726 ));\r
727 return Status;\r
728 }\r
729\r
730 if (OptionalElements & PLATFORM_CONFIG_F_GRAPHICS_RESOLUTION) {\r
731 //\r
732 // Pass the preferred resolution to GraphicsConsoleDxe via dynamic PCDs.\r
733 //\r
734 PcdStatus = PcdSet32S (\r
735 PcdVideoHorizontalResolution,\r
736 PlatformConfig.HorizontalResolution\r
737 );\r
738 ASSERT_RETURN_ERROR (PcdStatus);\r
739\r
740 PcdStatus = PcdSet32S (\r
741 PcdVideoVerticalResolution,\r
742 PlatformConfig.VerticalResolution\r
743 );\r
744 ASSERT_RETURN_ERROR (PcdStatus);\r
745\r
746 PcdStatus = PcdSet8S (PcdVideoResolutionSource, 1);\r
747 ASSERT_RETURN_ERROR (PcdStatus);\r
748 }\r
749\r
750 return EFI_SUCCESS;\r
751}\r
752\r
753/**\r
754 Notification callback for GOP interface installation.\r
755\r
756 @param[in] Event Event whose notification function is being invoked.\r
757\r
758 @param[in] Context The pointer to the notification function's context, which\r
759 is implementation-dependent.\r
760**/\r
761STATIC\r
762VOID\r
763EFIAPI\r
764GopInstalled (\r
765 IN EFI_EVENT Event,\r
766 IN VOID *Context\r
767 )\r
768{\r
769 EFI_STATUS Status;\r
770 EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop;\r
771\r
772 ASSERT (Event == mGopEvent);\r
773\r
774 //\r
775 // Check further GOPs.\r
776 //\r
777 for ( ; ;) {\r
778 mNumGopModes = 0;\r
779 mGopModes = NULL;\r
780\r
781 Status = gBS->LocateProtocol (\r
782 &gEfiGraphicsOutputProtocolGuid,\r
783 mGopTracker,\r
784 (VOID **)&Gop\r
785 );\r
786 if (EFI_ERROR (Status)) {\r
787 return;\r
788 }\r
789\r
790 Status = QueryGopModes (Gop, &mNumGopModes, &mGopModes);\r
791 if (EFI_ERROR (Status)) {\r
792 continue;\r
793 }\r
794\r
795 Status = PopulateForm (\r
796 mInstalledPackages,\r
797 &gOvmfPlatformConfigGuid,\r
798 FORMID_MAIN_FORM,\r
799 mNumGopModes,\r
800 mGopModes\r
801 );\r
802 if (EFI_ERROR (Status)) {\r
803 FreePool (mGopModes);\r
804 continue;\r
805 }\r
806\r
807 break;\r
808 }\r
809\r
810 //\r
811 // Success -- so uninstall this callback. Closing the event removes all\r
812 // pending notifications and all protocol registrations.\r
813 //\r
814 Status = gBS->CloseEvent (mGopEvent);\r
815 ASSERT_EFI_ERROR (Status);\r
816 mGopEvent = NULL;\r
817 mGopTracker = NULL;\r
818}\r
819\r
820/**\r
821 Entry point for this driver.\r
822\r
823 @param[in] ImageHandle Image handle of this driver.\r
824 @param[in] SystemTable Pointer to SystemTable.\r
825\r
826 @retval EFI_SUCESS Driver has loaded successfully.\r
827 @retval EFI_OUT_OF_RESOURCES Failed to install HII packages.\r
828 @return Error codes from lower level functions.\r
829\r
830**/\r
831EFI_STATUS\r
832EFIAPI\r
833PlatformInit (\r
834 IN EFI_HANDLE ImageHandle,\r
835 IN EFI_SYSTEM_TABLE *SystemTable\r
836 )\r
837{\r
838 EFI_STATUS Status;\r
839\r
840 ExecutePlatformConfig ();\r
841\r
842 mConfigAccess.ExtractConfig = &ExtractConfig;\r
843 mConfigAccess.RouteConfig = &RouteConfig;\r
844 mConfigAccess.Callback = &Callback;\r
845\r
846 //\r
847 // Declare ourselves suitable for HII communication.\r
848 //\r
849 Status = gBS->InstallMultipleProtocolInterfaces (\r
850 &ImageHandle,\r
851 &gEfiDevicePathProtocolGuid,\r
852 &mPkgDevicePath,\r
853 &gEfiHiiConfigAccessProtocolGuid,\r
854 &mConfigAccess,\r
855 NULL\r
856 );\r
857 if (EFI_ERROR (Status)) {\r
858 return Status;\r
859 }\r
860\r
861 //\r
862 // Publish the HII package list to HII Database.\r
863 //\r
864 mInstalledPackages = HiiAddPackages (\r
865 &gEfiCallerIdGuid, // PackageListGuid\r
866 ImageHandle, // associated DeviceHandle\r
867 PlatformDxeStrings, // 1st package\r
868 PlatformFormsBin, // 2nd package\r
869 NULL // terminator\r
870 );\r
871 if (mInstalledPackages == NULL) {\r
872 Status = EFI_OUT_OF_RESOURCES;\r
873 goto UninstallProtocols;\r
874 }\r
875\r
876 Status = gBS->CreateEvent (\r
877 EVT_NOTIFY_SIGNAL,\r
878 TPL_CALLBACK,\r
879 &GopInstalled,\r
880 NULL /* Context */,\r
881 &mGopEvent\r
882 );\r
883 if (EFI_ERROR (Status)) {\r
884 goto RemovePackages;\r
885 }\r
886\r
887 Status = gBS->RegisterProtocolNotify (\r
888 &gEfiGraphicsOutputProtocolGuid,\r
889 mGopEvent,\r
890 &mGopTracker\r
891 );\r
892 if (EFI_ERROR (Status)) {\r
893 goto CloseGopEvent;\r
894 }\r
895\r
896 //\r
897 // Check already installed GOPs.\r
898 //\r
899 Status = gBS->SignalEvent (mGopEvent);\r
900 ASSERT_EFI_ERROR (Status);\r
901\r
902 return EFI_SUCCESS;\r
903\r
904CloseGopEvent:\r
905 gBS->CloseEvent (mGopEvent);\r
906\r
907RemovePackages:\r
908 HiiRemovePackages (mInstalledPackages);\r
909\r
910UninstallProtocols:\r
911 gBS->UninstallMultipleProtocolInterfaces (\r
912 ImageHandle,\r
913 &gEfiDevicePathProtocolGuid,\r
914 &mPkgDevicePath,\r
915 &gEfiHiiConfigAccessProtocolGuid,\r
916 &mConfigAccess,\r
917 NULL\r
918 );\r
919 return Status;\r
920}\r
921\r
922/**\r
923 Unload the driver.\r
924\r
925 @param[in] ImageHandle Handle that identifies the image to evict.\r
926\r
927 @retval EFI_SUCCESS The image has been unloaded.\r
928**/\r
929EFI_STATUS\r
930EFIAPI\r
931PlatformUnload (\r
932 IN EFI_HANDLE ImageHandle\r
933 )\r
934{\r
935 if (mGopEvent == NULL) {\r
936 //\r
937 // The GOP callback ran successfully and unregistered itself. Release the\r
938 // resources allocated there.\r
939 //\r
940 ASSERT (mGopModes != NULL);\r
941 FreePool (mGopModes);\r
942 } else {\r
943 //\r
944 // Otherwise we need to unregister the callback.\r
945 //\r
946 ASSERT (mGopModes == NULL);\r
947 gBS->CloseEvent (mGopEvent);\r
948 }\r
949\r
950 //\r
951 // Release resources allocated by the entry point.\r
952 //\r
953 HiiRemovePackages (mInstalledPackages);\r
954 gBS->UninstallMultipleProtocolInterfaces (\r
955 ImageHandle,\r
956 &gEfiDevicePathProtocolGuid,\r
957 &mPkgDevicePath,\r
958 &gEfiHiiConfigAccessProtocolGuid,\r
959 &mConfigAccess,\r
960 NULL\r
961 );\r
962 return EFI_SUCCESS;\r
963}\r