Fvb.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /** @file
  2. Firmware Block Services to support emulating non-volatile variables
  3. by pretending that a memory buffer is storage for the NV variables.
  4. Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "PiDxe.h"
  8. #include <Guid/EventGroup.h>
  9. #include <Guid/SystemNvDataGuid.h>
  10. #include <Guid/VariableFormat.h>
  11. #include <Protocol/FirmwareVolumeBlock.h>
  12. #include <Protocol/DevicePath.h>
  13. #include <Library/UefiLib.h>
  14. #include <Library/UefiDriverEntryPoint.h>
  15. #include <Library/BaseLib.h>
  16. #include <Library/UefiRuntimeLib.h>
  17. #include <Library/DebugLib.h>
  18. #include <Library/BaseMemoryLib.h>
  19. #include <Library/MemoryAllocationLib.h>
  20. #include <Library/UefiBootServicesTableLib.h>
  21. #include <Library/DevicePathLib.h>
  22. #include <Library/PcdLib.h>
  23. #include <Library/PlatformFvbLib.h>
  24. #include "Fvb.h"
  25. #define EFI_AUTHENTICATED_VARIABLE_GUID \
  26. { 0xaaf32c78, 0x947b, 0x439a, { 0xa1, 0x80, 0x2e, 0x14, 0x4e, 0xc3, 0x77, 0x92 } }
  27. //
  28. // Virtual Address Change Event
  29. //
  30. // This is needed for runtime variable access.
  31. //
  32. EFI_EVENT mEmuVarsFvbAddrChangeEvent = NULL;
  33. //
  34. // This is the single instance supported by this driver. It
  35. // supports the FVB and Device Path protocols.
  36. //
  37. EFI_FW_VOL_BLOCK_DEVICE mEmuVarsFvb = {
  38. FVB_DEVICE_SIGNATURE,
  39. { // DevicePath
  40. {
  41. {
  42. HARDWARE_DEVICE_PATH,
  43. HW_MEMMAP_DP,
  44. {
  45. sizeof (MEMMAP_DEVICE_PATH),
  46. 0
  47. }
  48. },
  49. EfiMemoryMappedIO,
  50. 0,
  51. 0,
  52. },
  53. {
  54. END_DEVICE_PATH_TYPE,
  55. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  56. {
  57. sizeof (EFI_DEVICE_PATH_PROTOCOL),
  58. 0
  59. }
  60. }
  61. },
  62. NULL, // BufferPtr
  63. EMU_FVB_BLOCK_SIZE, // BlockSize
  64. EMU_FVB_SIZE, // Size
  65. { // FwVolBlockInstance
  66. FvbProtocolGetAttributes,
  67. FvbProtocolSetAttributes,
  68. FvbProtocolGetPhysicalAddress,
  69. FvbProtocolGetBlockSize,
  70. FvbProtocolRead,
  71. FvbProtocolWrite,
  72. FvbProtocolEraseBlocks,
  73. NULL
  74. },
  75. };
  76. /**
  77. Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
  78. This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
  79. It converts pointer to new virtual address.
  80. @param Event Event whose notification function is being invoked.
  81. @param Context Pointer to the notification function's context.
  82. **/
  83. VOID
  84. EFIAPI
  85. FvbVirtualAddressChangeEvent (
  86. IN EFI_EVENT Event,
  87. IN VOID *Context
  88. )
  89. {
  90. EfiConvertPointer (0x0, &mEmuVarsFvb.BufferPtr);
  91. }
  92. //
  93. // FVB protocol APIs
  94. //
  95. /**
  96. The GetPhysicalAddress() function retrieves the base address of
  97. a memory-mapped firmware volume. This function should be called
  98. only for memory-mapped firmware volumes.
  99. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  100. @param Address Pointer to a caller-allocated
  101. EFI_PHYSICAL_ADDRESS that, on successful
  102. return from GetPhysicalAddress(), contains the
  103. base address of the firmware volume.
  104. @retval EFI_SUCCESS The firmware volume base address is returned.
  105. @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.
  106. **/
  107. EFI_STATUS
  108. EFIAPI
  109. FvbProtocolGetPhysicalAddress (
  110. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  111. OUT EFI_PHYSICAL_ADDRESS *Address
  112. )
  113. {
  114. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  115. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  116. *Address = (EFI_PHYSICAL_ADDRESS)(UINTN)FvbDevice->BufferPtr;
  117. return EFI_SUCCESS;
  118. }
  119. /**
  120. The GetBlockSize() function retrieves the size of the requested
  121. block. It also returns the number of additional blocks with
  122. the identical size. The GetBlockSize() function is used to
  123. retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).
  124. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  125. @param Lba Indicates the block for which to return the size.
  126. @param BlockSize Pointer to a caller-allocated UINTN in which
  127. the size of the block is returned.
  128. @param NumberOfBlocks Pointer to a caller-allocated UINTN in
  129. which the number of consecutive blocks,
  130. starting with Lba, is returned. All
  131. blocks in this range have a size of
  132. BlockSize.
  133. @retval EFI_SUCCESS The firmware volume base address is returned.
  134. @retval EFI_INVALID_PARAMETER The requested LBA is out of range.
  135. **/
  136. EFI_STATUS
  137. EFIAPI
  138. FvbProtocolGetBlockSize (
  139. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  140. IN EFI_LBA Lba,
  141. OUT UINTN *BlockSize,
  142. OUT UINTN *NumberOfBlocks
  143. )
  144. {
  145. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  146. if (Lba >= EMU_FVB_NUM_TOTAL_BLOCKS) {
  147. return EFI_INVALID_PARAMETER;
  148. }
  149. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  150. *BlockSize = FvbDevice->BlockSize;
  151. *NumberOfBlocks = (UINTN)(EMU_FVB_NUM_TOTAL_BLOCKS - Lba);
  152. return EFI_SUCCESS;
  153. }
  154. /**
  155. The GetAttributes() function retrieves the attributes and
  156. current settings of the block. Status Codes Returned
  157. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  158. @param Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the
  159. attributes and current settings are
  160. returned. Type EFI_FVB_ATTRIBUTES_2 is defined
  161. in EFI_FIRMWARE_VOLUME_HEADER.
  162. @retval EFI_SUCCESS The firmware volume attributes were
  163. returned.
  164. **/
  165. EFI_STATUS
  166. EFIAPI
  167. FvbProtocolGetAttributes (
  168. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  169. OUT EFI_FVB_ATTRIBUTES_2 *Attributes
  170. )
  171. {
  172. *Attributes =
  173. (EFI_FVB_ATTRIBUTES_2)(
  174. EFI_FVB2_READ_ENABLED_CAP |
  175. EFI_FVB2_READ_STATUS |
  176. EFI_FVB2_WRITE_ENABLED_CAP |
  177. EFI_FVB2_WRITE_STATUS |
  178. EFI_FVB2_ERASE_POLARITY
  179. );
  180. return EFI_SUCCESS;
  181. }
  182. /**
  183. The SetAttributes() function sets configurable firmware volume
  184. attributes and returns the new settings of the firmware volume.
  185. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  186. @param Attributes On input, Attributes is a pointer to
  187. EFI_FVB_ATTRIBUTES_2 that contains the
  188. desired firmware volume settings. On
  189. successful return, it contains the new
  190. settings of the firmware volume. Type
  191. EFI_FVB_ATTRIBUTES_2 is defined in
  192. EFI_FIRMWARE_VOLUME_HEADER.
  193. @retval EFI_SUCCESS The firmware volume attributes were returned.
  194. @retval EFI_INVALID_PARAMETER The attributes requested are in
  195. conflict with the capabilities
  196. as declared in the firmware
  197. volume header.
  198. **/
  199. EFI_STATUS
  200. EFIAPI
  201. FvbProtocolSetAttributes (
  202. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  203. IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
  204. )
  205. {
  206. return EFI_ACCESS_DENIED;
  207. }
  208. /**
  209. Erases and initializes a firmware volume block.
  210. The EraseBlocks() function erases one or more blocks as denoted
  211. by the variable argument list. The entire parameter list of
  212. blocks must be verified before erasing any blocks. If a block is
  213. requested that does not exist within the associated firmware
  214. volume (it has a larger index than the last block of the
  215. firmware volume), the EraseBlocks() function must return the
  216. status code EFI_INVALID_PARAMETER without modifying the contents
  217. of the firmware volume. Implementations should be mindful that
  218. the firmware volume might be in the WriteDisabled state. If it
  219. is in this state, the EraseBlocks() function must return the
  220. status code EFI_ACCESS_DENIED without modifying the contents of
  221. the firmware volume. All calls to EraseBlocks() must be fully
  222. flushed to the hardware before the EraseBlocks() service
  223. returns.
  224. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
  225. instance.
  226. @param ... The variable argument list is a list of tuples.
  227. Each tuple describes a range of LBAs to erase
  228. and consists of the following:
  229. - An EFI_LBA that indicates the starting LBA
  230. - A UINTN that indicates the number of blocks to
  231. erase
  232. The list is terminated with an
  233. EFI_LBA_LIST_TERMINATOR. For example, the
  234. following indicates that two ranges of blocks
  235. (5-7 and 10-11) are to be erased: EraseBlocks
  236. (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);
  237. @retval EFI_SUCCESS The erase request was successfully
  238. completed.
  239. @retval EFI_ACCESS_DENIED The firmware volume is in the
  240. WriteDisabled state.
  241. @retval EFI_DEVICE_ERROR The block device is not functioning
  242. correctly and could not be written.
  243. The firmware device may have been
  244. partially erased.
  245. @retval EFI_INVALID_PARAMETER One or more of the LBAs listed
  246. in the variable argument list do
  247. not exist in the firmware volume.
  248. **/
  249. EFI_STATUS
  250. EFIAPI
  251. FvbProtocolEraseBlocks (
  252. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  253. ...
  254. )
  255. {
  256. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  257. VA_LIST Args;
  258. EFI_LBA StartingLba;
  259. UINTN NumOfLba;
  260. UINT8 *ErasePtr;
  261. UINTN EraseSize;
  262. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  263. //
  264. // Check input parameters
  265. //
  266. VA_START (Args, This);
  267. do {
  268. StartingLba = VA_ARG (Args, EFI_LBA);
  269. if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
  270. break;
  271. }
  272. NumOfLba = VA_ARG (Args, UINTN);
  273. if ((StartingLba > EMU_FVB_NUM_TOTAL_BLOCKS) ||
  274. (NumOfLba > EMU_FVB_NUM_TOTAL_BLOCKS - StartingLba))
  275. {
  276. VA_END (Args);
  277. return EFI_INVALID_PARAMETER;
  278. }
  279. } while (1);
  280. VA_END (Args);
  281. //
  282. // Erase blocks
  283. //
  284. VA_START (Args, This);
  285. do {
  286. StartingLba = VA_ARG (Args, EFI_LBA);
  287. if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
  288. break;
  289. }
  290. NumOfLba = VA_ARG (Args, UINTN);
  291. ErasePtr = FvbDevice->BufferPtr;
  292. ErasePtr += (UINTN)StartingLba * FvbDevice->BlockSize;
  293. EraseSize = NumOfLba * FvbDevice->BlockSize;
  294. SetMem (ErasePtr, EraseSize, ERASED_UINT8);
  295. } while (1);
  296. VA_END (Args);
  297. //
  298. // Call platform hook
  299. //
  300. VA_START (Args, This);
  301. PlatformFvbBlocksErased (This, Args);
  302. VA_END (Args);
  303. return EFI_SUCCESS;
  304. }
  305. /**
  306. Writes the specified number of bytes from the input buffer to the block.
  307. The Write() function writes the specified number of bytes from
  308. the provided buffer to the specified block and offset. If the
  309. firmware volume is sticky write, the caller must ensure that
  310. all the bits of the specified range to write are in the
  311. EFI_FVB_ERASE_POLARITY state before calling the Write()
  312. function, or else the result will be unpredictable. This
  313. unpredictability arises because, for a sticky-write firmware
  314. volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY
  315. state but cannot flip it back again. In general, before
  316. calling the Write() function, the caller should call the
  317. EraseBlocks() function first to erase the specified block to
  318. write. A block erase cycle will transition bits from the
  319. (NOT)EFI_FVB_ERASE_POLARITY state back to the
  320. EFI_FVB_ERASE_POLARITY state. Implementations should be
  321. mindful that the firmware volume might be in the WriteDisabled
  322. state. If it is in this state, the Write() function must
  323. return the status code EFI_ACCESS_DENIED without modifying the
  324. contents of the firmware volume. The Write() function must
  325. also prevent spanning block boundaries. If a write is
  326. requested that spans a block boundary, the write must store up
  327. to the boundary but not beyond. The output parameter NumBytes
  328. must be set to correctly indicate the number of bytes actually
  329. written. The caller must be aware that a write may be
  330. partially completed. All writes, partial or otherwise, must be
  331. fully flushed to the hardware before the Write() service
  332. returns.
  333. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  334. @param Lba The starting logical block index to write to.
  335. @param Offset Offset into the block at which to begin writing.
  336. @param NumBytes Pointer to a UINTN. At entry, *NumBytes
  337. contains the total size of the buffer. At
  338. exit, *NumBytes contains the total number of
  339. bytes actually written.
  340. @param Buffer Pointer to a caller-allocated buffer that
  341. contains the source for the write.
  342. @retval EFI_SUCCESS The firmware volume was written successfully.
  343. @retval EFI_BAD_BUFFER_SIZE The write was attempted across an
  344. LBA boundary. On output, NumBytes
  345. contains the total number of bytes
  346. actually written.
  347. @retval EFI_ACCESS_DENIED The firmware volume is in the
  348. WriteDisabled state.
  349. @retval EFI_DEVICE_ERROR The block device is malfunctioning
  350. and could not be written.
  351. **/
  352. EFI_STATUS
  353. EFIAPI
  354. FvbProtocolWrite (
  355. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  356. IN EFI_LBA Lba,
  357. IN UINTN Offset,
  358. IN OUT UINTN *NumBytes,
  359. IN UINT8 *Buffer
  360. )
  361. {
  362. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  363. UINT8 *FvbDataPtr;
  364. EFI_STATUS Status;
  365. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  366. if ((Lba >= EMU_FVB_NUM_TOTAL_BLOCKS) ||
  367. (Offset > FvbDevice->BlockSize))
  368. {
  369. return EFI_INVALID_PARAMETER;
  370. }
  371. Status = EFI_SUCCESS;
  372. if (*NumBytes > FvbDevice->BlockSize - Offset) {
  373. *NumBytes = FvbDevice->BlockSize - Offset;
  374. Status = EFI_BAD_BUFFER_SIZE;
  375. }
  376. FvbDataPtr = FvbDevice->BufferPtr;
  377. FvbDataPtr += (UINTN)Lba * FvbDevice->BlockSize;
  378. FvbDataPtr += Offset;
  379. CopyMem (FvbDataPtr, Buffer, *NumBytes);
  380. PlatformFvbDataWritten (This, Lba, Offset, *NumBytes, Buffer);
  381. return Status;
  382. }
  383. /**
  384. Reads the specified number of bytes into a buffer from the specified block.
  385. The Read() function reads the requested number of bytes from the
  386. requested block and stores them in the provided buffer.
  387. Implementations should be mindful that the firmware volume
  388. might be in the ReadDisabled state. If it is in this state,
  389. the Read() function must return the status code
  390. EFI_ACCESS_DENIED without modifying the contents of the
  391. buffer. The Read() function must also prevent spanning block
  392. boundaries. If a read is requested that would span a block
  393. boundary, the read must read up to the boundary but not
  394. beyond. The output parameter NumBytes must be set to correctly
  395. indicate the number of bytes actually read. The caller must be
  396. aware that a read may be partially completed.
  397. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  398. @param Lba The starting logical block index
  399. from which to read.
  400. @param Offset Offset into the block at which to begin reading.
  401. @param NumBytes Pointer to a UINTN. At entry, *NumBytes
  402. contains the total size of the buffer. At
  403. exit, *NumBytes contains the total number of
  404. bytes read.
  405. @param Buffer Pointer to a caller-allocated buffer that will
  406. be used to hold the data that is read.
  407. @retval EFI_SUCCESS The firmware volume was read successfully
  408. and contents are in Buffer.
  409. @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA
  410. boundary. On output, NumBytes
  411. contains the total number of bytes
  412. returned in Buffer.
  413. @retval EFI_ACCESS_DENIED The firmware volume is in the
  414. ReadDisabled state.
  415. @retval EFI_DEVICE_ERROR The block device is not
  416. functioning correctly and could
  417. not be read.
  418. **/
  419. EFI_STATUS
  420. EFIAPI
  421. FvbProtocolRead (
  422. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  423. IN EFI_LBA Lba,
  424. IN UINTN Offset,
  425. IN OUT UINTN *NumBytes,
  426. IN OUT UINT8 *Buffer
  427. )
  428. {
  429. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  430. UINT8 *FvbDataPtr;
  431. EFI_STATUS Status;
  432. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  433. if ((Lba >= EMU_FVB_NUM_TOTAL_BLOCKS) ||
  434. (Offset > FvbDevice->BlockSize))
  435. {
  436. return EFI_INVALID_PARAMETER;
  437. }
  438. Status = EFI_SUCCESS;
  439. if (*NumBytes > FvbDevice->BlockSize - Offset) {
  440. *NumBytes = FvbDevice->BlockSize - Offset;
  441. Status = EFI_BAD_BUFFER_SIZE;
  442. }
  443. FvbDataPtr = FvbDevice->BufferPtr;
  444. FvbDataPtr += (UINTN)Lba * FvbDevice->BlockSize;
  445. FvbDataPtr += Offset;
  446. CopyMem (Buffer, FvbDataPtr, *NumBytes);
  447. PlatformFvbDataRead (This, Lba, Offset, *NumBytes, Buffer);
  448. return Status;
  449. }
  450. /**
  451. Check the integrity of firmware volume header.
  452. @param[in] FwVolHeader - A pointer to a firmware volume header
  453. @retval EFI_SUCCESS - The firmware volume is consistent
  454. @retval EFI_NOT_FOUND - The firmware volume has been corrupted.
  455. **/
  456. EFI_STATUS
  457. ValidateFvHeader (
  458. IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader
  459. )
  460. {
  461. UINT16 Checksum;
  462. //
  463. // Verify the header revision, header signature, length
  464. // Length of FvBlock cannot be 2**64-1
  465. // HeaderLength cannot be an odd number
  466. //
  467. if ((FwVolHeader->Revision != EFI_FVH_REVISION) ||
  468. (FwVolHeader->Signature != EFI_FVH_SIGNATURE) ||
  469. (FwVolHeader->FvLength != EMU_FVB_SIZE) ||
  470. (FwVolHeader->HeaderLength != EMU_FV_HEADER_LENGTH)
  471. )
  472. {
  473. DEBUG ((DEBUG_INFO, "EMU Variable FVB: Basic FV headers were invalid\n"));
  474. return EFI_NOT_FOUND;
  475. }
  476. //
  477. // Verify the header checksum
  478. //
  479. Checksum = CalculateSum16 ((VOID *)FwVolHeader, FwVolHeader->HeaderLength);
  480. if (Checksum != 0) {
  481. DEBUG ((DEBUG_INFO, "EMU Variable FVB: FV checksum was invalid\n"));
  482. return EFI_NOT_FOUND;
  483. }
  484. return EFI_SUCCESS;
  485. }
  486. /**
  487. Initializes the FV Header and Variable Store Header
  488. to support variable operations.
  489. @param[in] Ptr - Location to initialize the headers
  490. **/
  491. VOID
  492. InitializeFvAndVariableStoreHeaders (
  493. IN VOID *Ptr
  494. )
  495. {
  496. //
  497. // Templates for authenticated variable FV header
  498. //
  499. STATIC FVB_FV_HDR_AND_VARS_TEMPLATE FvAndAuthenticatedVarTemplate = {
  500. { // EFI_FIRMWARE_VOLUME_HEADER FvHdr;
  501. // UINT8 ZeroVector[16];
  502. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  503. // EFI_GUID FileSystemGuid;
  504. EFI_SYSTEM_NV_DATA_FV_GUID,
  505. // UINT64 FvLength;
  506. EMU_FVB_SIZE,
  507. // UINT32 Signature;
  508. EFI_FVH_SIGNATURE,
  509. // EFI_FVB_ATTRIBUTES_2 Attributes;
  510. 0x4feff,
  511. // UINT16 HeaderLength;
  512. EMU_FV_HEADER_LENGTH,
  513. // UINT16 Checksum;
  514. 0,
  515. // UINT16 ExtHeaderOffset;
  516. 0,
  517. // UINT8 Reserved[1];
  518. { 0 },
  519. // UINT8 Revision;
  520. EFI_FVH_REVISION,
  521. // EFI_FV_BLOCK_MAP_ENTRY BlockMap[1];
  522. {
  523. {
  524. EMU_FVB_NUM_TOTAL_BLOCKS, // UINT32 NumBlocks;
  525. EMU_FVB_BLOCK_SIZE // UINT32 Length;
  526. }
  527. }
  528. },
  529. // EFI_FV_BLOCK_MAP_ENTRY EndBlockMap;
  530. { 0, 0 }, // End of block map
  531. { // VARIABLE_STORE_HEADER VarHdr;
  532. // EFI_GUID Signature; // need authenticated variables for secure boot
  533. EFI_AUTHENTICATED_VARIABLE_GUID,
  534. // UINT32 Size;
  535. (
  536. FixedPcdGet32 (PcdFlashNvStorageVariableSize) -
  537. OFFSET_OF (FVB_FV_HDR_AND_VARS_TEMPLATE, VarHdr)
  538. ),
  539. // UINT8 Format;
  540. VARIABLE_STORE_FORMATTED,
  541. // UINT8 State;
  542. VARIABLE_STORE_HEALTHY,
  543. // UINT16 Reserved;
  544. 0,
  545. // UINT32 Reserved1;
  546. 0
  547. }
  548. };
  549. EFI_FIRMWARE_VOLUME_HEADER *Fv;
  550. //
  551. // Copy the template structure into the location
  552. //
  553. CopyMem (
  554. Ptr,
  555. &FvAndAuthenticatedVarTemplate,
  556. sizeof FvAndAuthenticatedVarTemplate
  557. );
  558. //
  559. // Update the checksum for the FV header
  560. //
  561. Fv = (EFI_FIRMWARE_VOLUME_HEADER *)Ptr;
  562. Fv->Checksum = CalculateCheckSum16 (Ptr, Fv->HeaderLength);
  563. }
  564. /**
  565. Main entry point.
  566. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  567. @param[in] SystemTable A pointer to the EFI System Table.
  568. @retval EFI_SUCCESS Successfully initialized.
  569. **/
  570. EFI_STATUS
  571. EFIAPI
  572. FvbInitialize (
  573. IN EFI_HANDLE ImageHandle,
  574. IN EFI_SYSTEM_TABLE *SystemTable
  575. )
  576. {
  577. EFI_STATUS Status;
  578. VOID *Ptr;
  579. VOID *SubPtr;
  580. BOOLEAN Initialize;
  581. EFI_HANDLE Handle;
  582. EFI_PHYSICAL_ADDRESS Address;
  583. RETURN_STATUS PcdStatus;
  584. DEBUG ((DEBUG_INFO, "EMU Variable FVB Started\n"));
  585. //
  586. // Verify that the PCD's are set correctly.
  587. //
  588. ASSERT (
  589. FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize) %
  590. EMU_FVB_BLOCK_SIZE == 0
  591. );
  592. if (
  593. (PcdGet32 (PcdFlashNvStorageVariableSize) +
  594. PcdGet32 (PcdFlashNvStorageFtwWorkingSize)
  595. ) >
  596. EMU_FVB_NUM_SPARE_BLOCKS * EMU_FVB_BLOCK_SIZE
  597. )
  598. {
  599. DEBUG ((DEBUG_ERROR, "EMU Variable invalid PCD sizes\n"));
  600. return EFI_INVALID_PARAMETER;
  601. }
  602. if (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0) {
  603. DEBUG ((
  604. DEBUG_INFO,
  605. "Disabling EMU Variable FVB since "
  606. "flash variables appear to be supported.\n"
  607. ));
  608. return EFI_ABORTED;
  609. }
  610. //
  611. // By default we will initialize the FV contents. But, if
  612. // PcdEmuVariableNvStoreReserved is non-zero, then we will
  613. // use this location for our buffer.
  614. //
  615. // If this location does not have a proper FV header, then
  616. // we will initialize it.
  617. //
  618. Initialize = TRUE;
  619. if (PcdGet64 (PcdEmuVariableNvStoreReserved) != 0) {
  620. Ptr = (VOID *)(UINTN)PcdGet64 (PcdEmuVariableNvStoreReserved);
  621. DEBUG ((
  622. DEBUG_INFO,
  623. "EMU Variable FVB: Using pre-reserved block at %p\n",
  624. Ptr
  625. ));
  626. Status = ValidateFvHeader (Ptr);
  627. if (!EFI_ERROR (Status)) {
  628. DEBUG ((DEBUG_INFO, "EMU Variable FVB: Found valid pre-existing FV\n"));
  629. Initialize = FALSE;
  630. }
  631. } else {
  632. Ptr = AllocateRuntimePages (EFI_SIZE_TO_PAGES (EMU_FVB_SIZE));
  633. }
  634. mEmuVarsFvb.BufferPtr = Ptr;
  635. //
  636. // Initialize the main FV header and variable store header
  637. //
  638. if (Initialize) {
  639. SetMem (Ptr, EMU_FVB_SIZE, ERASED_UINT8);
  640. InitializeFvAndVariableStoreHeaders (Ptr);
  641. }
  642. PcdStatus = PcdSet64S (PcdFlashNvStorageVariableBase64, (UINTN)Ptr);
  643. ASSERT_RETURN_ERROR (PcdStatus);
  644. //
  645. // Initialize the Fault Tolerant Write data area
  646. //
  647. SubPtr = (VOID *)((UINT8 *)Ptr + PcdGet32 (PcdFlashNvStorageVariableSize));
  648. PcdStatus = PcdSet64S (
  649. PcdFlashNvStorageFtwWorkingBase64,
  650. (UINTN)SubPtr
  651. );
  652. ASSERT_RETURN_ERROR (PcdStatus);
  653. //
  654. // Initialize the Fault Tolerant Write spare block
  655. //
  656. SubPtr = (VOID *)((UINT8 *)Ptr +
  657. EMU_FVB_NUM_SPARE_BLOCKS * EMU_FVB_BLOCK_SIZE);
  658. PcdStatus = PcdSet64S (
  659. PcdFlashNvStorageFtwSpareBase64,
  660. (UINTN)SubPtr
  661. );
  662. ASSERT_RETURN_ERROR (PcdStatus);
  663. //
  664. // Setup FVB device path
  665. //
  666. Address = (EFI_PHYSICAL_ADDRESS)(UINTN)Ptr;
  667. mEmuVarsFvb.DevicePath.MemMapDevPath.StartingAddress = Address;
  668. mEmuVarsFvb.DevicePath.MemMapDevPath.EndingAddress = Address + EMU_FVB_SIZE - 1;
  669. //
  670. // Install the protocols
  671. //
  672. DEBUG ((DEBUG_INFO, "Installing FVB for EMU Variable support\n"));
  673. Handle = 0;
  674. Status = gBS->InstallMultipleProtocolInterfaces (
  675. &Handle,
  676. &gEfiFirmwareVolumeBlock2ProtocolGuid,
  677. &mEmuVarsFvb.FwVolBlockInstance,
  678. &gEfiDevicePathProtocolGuid,
  679. &mEmuVarsFvb.DevicePath,
  680. NULL
  681. );
  682. ASSERT_EFI_ERROR (Status);
  683. //
  684. // Register for the virtual address change event
  685. //
  686. Status = gBS->CreateEventEx (
  687. EVT_NOTIFY_SIGNAL,
  688. TPL_NOTIFY,
  689. FvbVirtualAddressChangeEvent,
  690. NULL,
  691. &gEfiEventVirtualAddressChangeGuid,
  692. &mEmuVarsFvbAddrChangeEvent
  693. );
  694. ASSERT_EFI_ERROR (Status);
  695. return EFI_SUCCESS;
  696. }