Platform.c 24 KB

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