Platform.c 29 KB

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