FvbDxe.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. /** @file
  2. *
  3. * Copyright (c) 2023, Yuin Yee, Chew (John) <yuinyee.chew@starfivetech.com>.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause-Patent
  6. *
  7. **/
  8. #include <PiDxe.h>
  9. #include <Library/BaseLib.h>
  10. #include <Library/BaseMemoryLib.h>
  11. #include <Library/DebugLib.h>
  12. #include <Library/DxeServicesTableLib.h>
  13. #include <Library/HobLib.h>
  14. #include <Library/MemoryAllocationLib.h>
  15. #include <Library/PcdLib.h>
  16. #include <Library/UefiBootServicesTableLib.h>
  17. #include <Library/UefiLib.h>
  18. #include <Library/UefiRuntimeLib.h>
  19. #include <Guid/NvVarStoreFormatted.h>
  20. #include <Guid/SystemNvDataGuid.h>
  21. #include <Guid/VariableFormat.h>
  22. #include "FvbDxe.h"
  23. STATIC EFI_EVENT mFvbVirtualAddrChangeEvent;
  24. STATIC FVB_DEVICE *mFvbDevice;
  25. STATIC CONST FVB_DEVICE mFvbFlashInstanceTemplate = {
  26. {
  27. 0, // SpiFlash Transfer Mode
  28. 0, // SpiFlash Address Size
  29. NULL, // SpiFlash detailed information
  30. 0, // SpiFlash RegBase
  31. 0, // SpiFlash MaxHz
  32. 0, // SpiFlash PrevHz
  33. 0, // SpiFlash CaliHz
  34. 0, // SpiFlash ReadDelay
  35. 0, // SpiFlash RefClkHz
  36. 0, // SpiFlash CaliCs
  37. 0, // SpiFlash IsDecodedCs
  38. 0, // SpiFlash AbhBase
  39. 0, // SpiFlash AhbSize
  40. 0, // SpiFlash FifoDepth
  41. 0, // SpiFlash FifoWidth
  42. 0, // SpiFlash TrigAdd
  43. 0, // SpiFlash DacMode
  44. 0, // SpiFlash WRDelay
  45. 0, // SpiFlash Dtr
  46. }, // SpiDevice
  47. NULL, // SpiFlashProtocol
  48. NULL, // SpiMasterProtocol
  49. NULL, // Handle
  50. FVB_FLASH_SIGNATURE, // Signature
  51. FALSE, // IsMemoryMapped
  52. 0, // RegionBaseAddress
  53. SIZE_256KB, // Size
  54. 0, // FvbOffset
  55. 0, // FvbSize
  56. 0, // StartLba
  57. {
  58. 0, // MediaId
  59. FALSE, // RemovableMedia
  60. TRUE, // MediaPresent
  61. FALSE, // LogicalPartition
  62. FALSE, // ReadOnly
  63. FALSE, // WriteCaching;
  64. 0, // BlockSize
  65. 4, // IoAlign
  66. 0, // LastBlock
  67. 0, // LowestAlignedLba
  68. 1, // LogicalBlocksPerPhysicalBlock
  69. }, //Media;
  70. {
  71. FvbGetAttributes, // GetAttributes
  72. FvbSetAttributes, // SetAttributes
  73. FvbGetPhysicalAddress, // GetPhysicalAddress
  74. FvbGetBlockSize, // GetBlockSize
  75. FvbRead, // Read
  76. FvbWrite, // Write
  77. FvbEraseBlocks, // EraseBlocks
  78. NULL, // ParentHandle
  79. }, // FvbProtocol;
  80. {
  81. {
  82. {
  83. HARDWARE_DEVICE_PATH,
  84. HW_VENDOR_DP,
  85. {
  86. (UINT8)sizeof (VENDOR_DEVICE_PATH),
  87. (UINT8)((sizeof (VENDOR_DEVICE_PATH)) >> 8)
  88. }
  89. },
  90. { 0xfc0cb972, 0x21df, 0x44d2, { 0x92, 0xa5, 0x78, 0x98, 0x99, 0xcb, 0xf6, 0x61 } }
  91. },
  92. {
  93. END_DEVICE_PATH_TYPE,
  94. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  95. { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 }
  96. }
  97. } // DevicePath
  98. };
  99. //
  100. // The Firmware Volume Block Protocol is the low-level interface
  101. // to a firmware volume. File-level access to a firmware volume
  102. // should not be done using the Firmware Volume Block Protocol.
  103. // Normal access to a firmware volume must use the Firmware
  104. // Volume Protocol. Typically, only the file system driver that
  105. // produces the Firmware Volume Protocol will bind to the
  106. // Firmware Volume Block Protocol.
  107. //
  108. /**
  109. Initialises the FV Header and Variable Store Header
  110. to support variable operations.
  111. @param[in] Ptr - Location to initialise the headers
  112. **/
  113. STATIC
  114. EFI_STATUS
  115. FvbInitFvAndVariableStoreHeaders (
  116. IN FVB_DEVICE *FlashInstance
  117. )
  118. {
  119. EFI_FIRMWARE_VOLUME_HEADER *FirmwareVolumeHeader;
  120. VARIABLE_STORE_HEADER *VariableStoreHeader;
  121. EFI_STATUS Status;
  122. VOID* Headers;
  123. UINTN HeadersLength;
  124. UINTN BlockSize;
  125. HeadersLength = sizeof (EFI_FIRMWARE_VOLUME_HEADER) +
  126. sizeof (EFI_FV_BLOCK_MAP_ENTRY) +
  127. sizeof (VARIABLE_STORE_HEADER);
  128. Headers = AllocateZeroPool (HeadersLength);
  129. BlockSize = FlashInstance->Media.BlockSize;
  130. //
  131. // FirmwareVolumeHeader->FvLength is declared to have the Variable area
  132. // AND the FTW working area AND the FTW Spare contiguous.
  133. //
  134. ASSERT (PcdGet64 (PcdFlashNvStorageVariableBase64) +
  135. PcdGet32 (PcdFlashNvStorageVariableSize) ==
  136. PcdGet64 (PcdFlashNvStorageFtwWorkingBase64));
  137. ASSERT (PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) +
  138. PcdGet32 (PcdFlashNvStorageFtwWorkingSize) ==
  139. PcdGet64 (PcdFlashNvStorageFtwSpareBase64));
  140. // Check if the size of the area is at least one block size
  141. ASSERT ((PcdGet32 (PcdFlashNvStorageVariableSize) > 0) &&
  142. (PcdGet32 (PcdFlashNvStorageVariableSize) / BlockSize > 0));
  143. ASSERT ((PcdGet32 (PcdFlashNvStorageFtwWorkingSize) > 0) &&
  144. (PcdGet32 (PcdFlashNvStorageFtwWorkingSize) / BlockSize > 0));
  145. ASSERT ((PcdGet32 (PcdFlashNvStorageFtwSpareSize) > 0) &&
  146. (PcdGet32 (PcdFlashNvStorageFtwSpareSize) / BlockSize > 0));
  147. // Ensure the Variable areas are aligned on block size boundaries
  148. ASSERT ((PcdGet64 (PcdFlashNvStorageVariableBase64) % BlockSize) == 0);
  149. ASSERT ((PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) % BlockSize) == 0);
  150. ASSERT ((PcdGet64 (PcdFlashNvStorageFtwSpareBase64) % BlockSize) == 0);
  151. //
  152. // EFI_FIRMWARE_VOLUME_HEADER
  153. //
  154. FirmwareVolumeHeader = (EFI_FIRMWARE_VOLUME_HEADER*)Headers;
  155. CopyGuid (&FirmwareVolumeHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid);
  156. FirmwareVolumeHeader->FvLength = FlashInstance->FvbSize;
  157. FirmwareVolumeHeader->Signature = EFI_FVH_SIGNATURE;
  158. FirmwareVolumeHeader->Attributes = EFI_FVB2_READ_ENABLED_CAP |
  159. EFI_FVB2_READ_STATUS |
  160. EFI_FVB2_STICKY_WRITE |
  161. EFI_FVB2_ERASE_POLARITY |
  162. EFI_FVB2_WRITE_STATUS |
  163. EFI_FVB2_WRITE_ENABLED_CAP;
  164. if (FlashInstance->IsMemoryMapped) {
  165. FirmwareVolumeHeader->Attributes |= EFI_FVB2_MEMORY_MAPPED;
  166. }
  167. FirmwareVolumeHeader->HeaderLength = sizeof (EFI_FIRMWARE_VOLUME_HEADER) +
  168. sizeof (EFI_FV_BLOCK_MAP_ENTRY);
  169. FirmwareVolumeHeader->Revision = EFI_FVH_REVISION;
  170. FirmwareVolumeHeader->BlockMap[0].NumBlocks = FlashInstance->Media.LastBlock + 1;
  171. FirmwareVolumeHeader->BlockMap[0].Length = FlashInstance->Media.BlockSize;
  172. FirmwareVolumeHeader->BlockMap[1].NumBlocks = 0;
  173. FirmwareVolumeHeader->BlockMap[1].Length = 0;
  174. FirmwareVolumeHeader->Checksum = CalculateCheckSum16 (
  175. (UINT16 *)FirmwareVolumeHeader,
  176. FirmwareVolumeHeader->HeaderLength);
  177. //
  178. // VARIABLE_STORE_HEADER
  179. //
  180. VariableStoreHeader = (VOID *)((UINTN)Headers +
  181. FirmwareVolumeHeader->HeaderLength);
  182. CopyGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGuid);
  183. VariableStoreHeader->Size = PcdGet32(PcdFlashNvStorageVariableSize) -
  184. FirmwareVolumeHeader->HeaderLength;
  185. VariableStoreHeader->Format = VARIABLE_STORE_FORMATTED;
  186. VariableStoreHeader->State = VARIABLE_STORE_HEALTHY;
  187. // Install the combined super-header in the flash device
  188. Status = FvbWrite (&FlashInstance->FvbProtocol, 0, 0, &HeadersLength, Headers);
  189. FreePool (Headers);
  190. return Status;
  191. }
  192. /**
  193. Check the integrity of firmware volume header.
  194. @param[in] FwVolHeader - A pointer to a firmware volume header
  195. @retval EFI_SUCCESS - The firmware volume is consistent
  196. @retval EFI_NOT_FOUND - The firmware volume has been corrupted.
  197. **/
  198. STATIC
  199. EFI_STATUS
  200. FvbValidateFvHeader (
  201. IN FVB_DEVICE *FlashInstance
  202. )
  203. {
  204. UINT16 Checksum;
  205. EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
  206. VARIABLE_STORE_HEADER *VariableStoreHeader;
  207. UINTN VariableStoreLength;
  208. FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)FlashInstance->RegionBaseAddress;
  209. // Verify the header revision, header signature, length
  210. if ((FwVolHeader->Revision != EFI_FVH_REVISION) ||
  211. (FwVolHeader->Signature != EFI_FVH_SIGNATURE) ||
  212. (FwVolHeader->FvLength != FlashInstance->FvbSize)) {
  213. DEBUG ((DEBUG_ERROR,
  214. "%a: No Firmware Volume header present\n",
  215. __FUNCTION__));
  216. return EFI_NOT_FOUND;
  217. }
  218. // Check the Firmware Volume Guid
  219. if (!CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid)) {
  220. DEBUG ((DEBUG_ERROR,
  221. "%a: Firmware Volume Guid non-compatible\n",
  222. __FUNCTION__));
  223. return EFI_NOT_FOUND;
  224. }
  225. // Verify the header checksum
  226. Checksum = CalculateSum16 ((UINT16 *)FwVolHeader, FwVolHeader->HeaderLength);
  227. if (Checksum != 0) {
  228. DEBUG ((DEBUG_ERROR,
  229. "%a: FV checksum is invalid (Checksum:0x%x)\n",
  230. __FUNCTION__,
  231. Checksum));
  232. return EFI_NOT_FOUND;
  233. }
  234. VariableStoreHeader = (VOID *)((UINTN)FwVolHeader + FwVolHeader->HeaderLength);
  235. // Check the Variable Store Guid
  236. if (!CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) &&
  237. !CompareGuid (&VariableStoreHeader->Signature,
  238. &gEfiAuthenticatedVariableGuid)) {
  239. DEBUG ((DEBUG_ERROR,
  240. "%a: Variable Store Guid non-compatible\n",
  241. __FUNCTION__));
  242. return EFI_NOT_FOUND;
  243. }
  244. VariableStoreLength = PcdGet32 (PcdFlashNvStorageVariableSize) -
  245. FwVolHeader->HeaderLength;
  246. if (VariableStoreHeader->Size != VariableStoreLength) {
  247. DEBUG ((DEBUG_ERROR,
  248. "%a: Variable Store Length does not match\n",
  249. __FUNCTION__));
  250. return EFI_NOT_FOUND;
  251. }
  252. return EFI_SUCCESS;
  253. }
  254. /**
  255. The GetAttributes() function retrieves the attributes and
  256. current settings of the block.
  257. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  258. @param Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the attributes and
  259. current settings are returned.
  260. Type EFI_FVB_ATTRIBUTES_2 is defined in
  261. EFI_FIRMWARE_VOLUME_HEADER.
  262. @retval EFI_SUCCESS The firmware volume attributes were returned.
  263. **/
  264. EFI_STATUS
  265. EFIAPI
  266. FvbGetAttributes (
  267. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  268. OUT EFI_FVB_ATTRIBUTES_2 *Attributes
  269. )
  270. {
  271. EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
  272. EFI_FVB_ATTRIBUTES_2 *FlashFvbAttributes;
  273. FVB_DEVICE *FlashInstance;
  274. FlashInstance = INSTANCE_FROM_FVB_THIS (This);
  275. FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)FlashInstance->RegionBaseAddress;
  276. FlashFvbAttributes = (EFI_FVB_ATTRIBUTES_2 *)&(FwVolHeader->Attributes);
  277. *Attributes = *FlashFvbAttributes;
  278. return EFI_SUCCESS;
  279. }
  280. /**
  281. The SetAttributes() function sets configurable firmware volume attributes
  282. and returns the new settings of the firmware volume.
  283. @param This EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  284. @param Attributes On input, Attributes is a pointer to
  285. EFI_FVB_ATTRIBUTES_2 that contains the desired
  286. firmware volume settings.
  287. On successful return, it contains the new
  288. settings of the firmware volume.
  289. @retval EFI_SUCCESS The firmware volume attributes were returned.
  290. @retval EFI_INVALID_PARAMETER The attributes requested are in conflict with
  291. the capabilities as declared in the firmware
  292. volume header.
  293. **/
  294. EFI_STATUS
  295. EFIAPI
  296. FvbSetAttributes (
  297. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  298. IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
  299. )
  300. {
  301. EFI_FVB_ATTRIBUTES_2 OldAttributes;
  302. EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes;
  303. EFI_FVB_ATTRIBUTES_2 UnchangedAttributes;
  304. FVB_DEVICE *FlashInstance;
  305. UINT32 Capabilities;
  306. UINT32 OldStatus;
  307. UINT32 NewStatus;
  308. FlashInstance = INSTANCE_FROM_FVB_THIS (This);
  309. //
  310. // Obtain attributes from FVB header
  311. //
  312. FvbGetAttributes (This, &FlashFvbAttributes);
  313. OldAttributes = FlashFvbAttributes;
  314. Capabilities = OldAttributes & EFI_FVB2_CAPABILITIES;
  315. OldStatus = OldAttributes & EFI_FVB2_STATUS;
  316. NewStatus = *Attributes & EFI_FVB2_STATUS;
  317. UnchangedAttributes = EFI_FVB2_READ_DISABLED_CAP | \
  318. EFI_FVB2_READ_ENABLED_CAP | \
  319. EFI_FVB2_WRITE_DISABLED_CAP | \
  320. EFI_FVB2_WRITE_ENABLED_CAP | \
  321. EFI_FVB2_LOCK_CAP | \
  322. EFI_FVB2_STICKY_WRITE | \
  323. EFI_FVB2_ERASE_POLARITY | \
  324. EFI_FVB2_READ_LOCK_CAP | \
  325. EFI_FVB2_WRITE_LOCK_CAP | \
  326. EFI_FVB2_ALIGNMENT;
  327. if (FlashInstance->IsMemoryMapped) {
  328. UnchangedAttributes |= EFI_FVB2_MEMORY_MAPPED;
  329. }
  330. //
  331. // Some attributes of FV is read only can *not* be set
  332. //
  333. if ((OldAttributes & UnchangedAttributes) ^
  334. (*Attributes & UnchangedAttributes)) {
  335. return EFI_INVALID_PARAMETER;
  336. }
  337. //
  338. // If firmware volume is locked, no status bit can be updated
  339. //
  340. if (OldAttributes & EFI_FVB2_LOCK_STATUS) {
  341. if (OldStatus ^ NewStatus) {
  342. return EFI_ACCESS_DENIED;
  343. }
  344. }
  345. //
  346. // Test read disable
  347. //
  348. if ((Capabilities & EFI_FVB2_READ_DISABLED_CAP) == 0) {
  349. if ((NewStatus & EFI_FVB2_READ_STATUS) == 0) {
  350. return EFI_INVALID_PARAMETER;
  351. }
  352. }
  353. //
  354. // Test read enable
  355. //
  356. if ((Capabilities & EFI_FVB2_READ_ENABLED_CAP) == 0) {
  357. if (NewStatus & EFI_FVB2_READ_STATUS) {
  358. return EFI_INVALID_PARAMETER;
  359. }
  360. }
  361. //
  362. // Test write disable
  363. //
  364. if ((Capabilities & EFI_FVB2_WRITE_DISABLED_CAP) == 0) {
  365. if ((NewStatus & EFI_FVB2_WRITE_STATUS) == 0) {
  366. return EFI_INVALID_PARAMETER;
  367. }
  368. }
  369. //
  370. // Test write enable
  371. //
  372. if ((Capabilities & EFI_FVB2_WRITE_ENABLED_CAP) == 0) {
  373. if (NewStatus & EFI_FVB2_WRITE_STATUS) {
  374. return EFI_INVALID_PARAMETER;
  375. }
  376. }
  377. //
  378. // Test lock
  379. //
  380. if ((Capabilities & EFI_FVB2_LOCK_CAP) == 0) {
  381. if (NewStatus & EFI_FVB2_LOCK_STATUS) {
  382. return EFI_INVALID_PARAMETER;
  383. }
  384. }
  385. FlashFvbAttributes = FlashFvbAttributes & (0xFFFFFFFF & (~EFI_FVB2_STATUS));
  386. FlashFvbAttributes = FlashFvbAttributes | NewStatus;
  387. *Attributes = FlashFvbAttributes;
  388. return EFI_SUCCESS;
  389. }
  390. /**
  391. The GetPhysicalAddress() function retrieves the base address of
  392. a memory-mapped firmware volume. This function should be called
  393. only for memory-mapped firmware volumes.
  394. @param This EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  395. @param Address Pointer to a caller-allocated
  396. EFI_PHYSICAL_ADDRESS that, on successful
  397. return from GetPhysicalAddress(), contains the
  398. base address of the firmware volume.
  399. @retval EFI_SUCCESS The firmware volume base address was returned.
  400. @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.
  401. **/
  402. EFI_STATUS
  403. EFIAPI
  404. FvbGetPhysicalAddress (
  405. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  406. OUT EFI_PHYSICAL_ADDRESS *Address
  407. )
  408. {
  409. FVB_DEVICE *FlashInstance;
  410. ASSERT (Address != NULL);
  411. FlashInstance = INSTANCE_FROM_FVB_THIS (This);
  412. *Address = FlashInstance->RegionBaseAddress;
  413. return EFI_SUCCESS;
  414. }
  415. /**
  416. The GetBlockSize() function retrieves the size of the requested
  417. block. It also returns the number of additional blocks with
  418. the identical size. The GetBlockSize() function is used to
  419. retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).
  420. @param This EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  421. @param Lba Indicates the block whose size to return
  422. @param BlockSize Pointer to a caller-allocated UINTN in which
  423. the size of the block is returned.
  424. @param NumberOfBlocks Pointer to a caller-allocated UINTN in
  425. which the number of consecutive blocks,
  426. starting with Lba, is returned. All
  427. blocks in this range have a size of
  428. BlockSize.
  429. @retval EFI_SUCCESS The firmware volume base address was returned.
  430. @retval EFI_INVALID_PARAMETER The requested LBA is out of range.
  431. **/
  432. EFI_STATUS
  433. EFIAPI
  434. FvbGetBlockSize (
  435. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  436. IN EFI_LBA Lba,
  437. OUT UINTN *BlockSize,
  438. OUT UINTN *NumberOfBlocks
  439. )
  440. {
  441. FVB_DEVICE *FlashInstance;
  442. FlashInstance = INSTANCE_FROM_FVB_THIS (This);
  443. if (Lba > FlashInstance->Media.LastBlock) {
  444. DEBUG ((DEBUG_ERROR,
  445. "%a: Error: Requested LBA %ld is beyond the last available LBA (%ld).\n",
  446. __FUNCTION__,
  447. Lba,
  448. FlashInstance->Media.LastBlock));
  449. return EFI_INVALID_PARAMETER;
  450. } else {
  451. // Assume equal sized blocks in all flash devices
  452. *BlockSize = (UINTN)FlashInstance->Media.BlockSize;
  453. *NumberOfBlocks = (UINTN)(FlashInstance->Media.LastBlock - Lba + 1);
  454. return EFI_SUCCESS;
  455. }
  456. }
  457. /**
  458. Reads the specified number of bytes into a buffer from the specified block.
  459. The Read() function reads the requested number of bytes from the
  460. requested block and stores them in the provided buffer.
  461. Implementations should be mindful that the firmware volume
  462. might be in the ReadDisabled state. If it is in this state,
  463. the Read() function must return the status code
  464. EFI_ACCESS_DENIED without modifying the contents of the
  465. buffer. The Read() function must also prevent spanning block
  466. boundaries. If a read is requested that would span a block
  467. boundary, the read must read up to the boundary but not
  468. beyond. The output parameter NumBytes must be set to correctly
  469. indicate the number of bytes actually read. The caller must be
  470. aware that a read may be partially completed.
  471. @param This EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  472. @param Lba The starting logical block index from which to read
  473. @param Offset Offset into the block at which to begin reading.
  474. @param NumBytes Pointer to a UINTN.
  475. At entry, *NumBytes contains the total size of the
  476. buffer.
  477. At exit, *NumBytes contains the total number of
  478. bytes read.
  479. @param Buffer Pointer to a caller-allocated buffer that will be
  480. used to hold the data that is read.
  481. @retval EFI_SUCCESS The firmware volume was read successfully, and
  482. contents are in Buffer.
  483. @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.
  484. On output, NumBytes contains the total number of
  485. bytes returned in Buffer.
  486. @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state.
  487. @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
  488. could not be read.
  489. **/
  490. EFI_STATUS
  491. EFIAPI
  492. FvbRead (
  493. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  494. IN EFI_LBA Lba,
  495. IN UINTN Offset,
  496. IN OUT UINTN *NumBytes,
  497. IN OUT UINT8 *Buffer
  498. )
  499. {
  500. FVB_DEVICE *FlashInstance;
  501. UINTN BlockSize;
  502. UINTN DataOffset;
  503. FlashInstance = INSTANCE_FROM_FVB_THIS (This);
  504. // Cache the block size to avoid de-referencing pointers all the time
  505. BlockSize = FlashInstance->Media.BlockSize;
  506. //
  507. // The read must not span block boundaries.
  508. // We need to check each variable individually because adding two large
  509. // values together overflows.
  510. //
  511. if (Offset >= BlockSize ||
  512. *NumBytes > BlockSize ||
  513. (Offset + *NumBytes) > BlockSize) {
  514. DEBUG ((DEBUG_ERROR,
  515. "%a: Wrong buffer size: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n",
  516. __FUNCTION__,
  517. Offset,
  518. *NumBytes,
  519. BlockSize));
  520. return EFI_BAD_BUFFER_SIZE;
  521. }
  522. // No bytes to read
  523. if (*NumBytes == 0) {
  524. return EFI_SUCCESS;
  525. }
  526. DataOffset = GET_DATA_OFFSET (FlashInstance->RegionBaseAddress + Offset,
  527. FlashInstance->StartLba + Lba,
  528. FlashInstance->Media.BlockSize);
  529. // Read the memory-mapped data
  530. CopyMem (Buffer, (UINTN *)DataOffset, *NumBytes);
  531. return EFI_SUCCESS;
  532. }
  533. /**
  534. Writes the specified number of bytes from the input buffer to the block.
  535. The Write() function writes the specified number of bytes from
  536. the provided buffer to the specified block and offset. If the
  537. firmware volume is sticky write, the caller must ensure that
  538. all the bits of the specified range to write are in the
  539. EFI_FVB_ERASE_POLARITY state before calling the Write()
  540. function, or else the result will be unpredictable. This
  541. unpredictability arises because, for a sticky-write firmware
  542. volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY
  543. state but cannot flip it back again. Before calling the
  544. Write() function, it is recommended for the caller to first call
  545. the EraseBlocks() function to erase the specified block to
  546. write. A block erase cycle will transition bits from the
  547. (NOT)EFI_FVB_ERASE_POLARITY state back to the
  548. EFI_FVB_ERASE_POLARITY state. Implementations should be
  549. mindful that the firmware volume might be in the WriteDisabled
  550. state. If it is in this state, the Write() function must
  551. return the status code EFI_ACCESS_DENIED without modifying the
  552. contents of the firmware volume. The Write() function must
  553. also prevent spanning block boundaries. If a write is
  554. requested that spans a block boundary, the write must store up
  555. to the boundary but not beyond. The output parameter NumBytes
  556. must be set to correctly indicate the number of bytes actually
  557. written. The caller must be aware that a write may be
  558. partially completed. All writes, partial or otherwise, must be
  559. fully flushed to the hardware before the Write() service
  560. returns.
  561. @param This EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  562. @param Lba The starting logical block index to write to.
  563. @param Offset Offset into the block at which to begin writing.
  564. @param NumBytes The pointer to a UINTN.
  565. At entry, *NumBytes contains the total size of the
  566. buffer.
  567. At exit, *NumBytes contains the total number of
  568. bytes actually written.
  569. @param Buffer The pointer to a caller-allocated buffer that
  570. contains the source for the write.
  571. @retval EFI_SUCCESS The firmware volume was written successfully.
  572. @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
  573. On output, NumBytes contains the total number of
  574. bytes actually written.
  575. @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
  576. @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not be
  577. written.
  578. **/
  579. EFI_STATUS
  580. EFIAPI
  581. FvbWrite (
  582. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  583. IN EFI_LBA Lba,
  584. IN UINTN Offset,
  585. IN OUT UINTN *NumBytes,
  586. IN UINT8 *Buffer
  587. )
  588. {
  589. EFI_STATUS Status;
  590. FVB_DEVICE *FlashInstance;
  591. UINTN DataOffset;
  592. FlashInstance = INSTANCE_FROM_FVB_THIS (This);
  593. DataOffset = GET_DATA_OFFSET (FlashInstance->FvbOffset + Offset,
  594. FlashInstance->StartLba + Lba,
  595. FlashInstance->Media.BlockSize);
  596. Status = FlashInstance->SpiFlashProtocol->Write (&FlashInstance->SpiDevice,
  597. DataOffset,
  598. *NumBytes,
  599. Buffer);
  600. if (EFI_ERROR (Status)) {
  601. DEBUG ((DEBUG_ERROR,
  602. "%a: Failed to write to Spi device\n",
  603. __FUNCTION__));
  604. return Status;
  605. }
  606. // Update shadow buffer
  607. if (!FlashInstance->IsMemoryMapped) {
  608. DataOffset = GET_DATA_OFFSET (FlashInstance->RegionBaseAddress + Offset,
  609. FlashInstance->StartLba + Lba,
  610. FlashInstance->Media.BlockSize);
  611. CopyMem ((UINTN *)DataOffset, Buffer, *NumBytes);
  612. }
  613. return EFI_SUCCESS;
  614. }
  615. /**
  616. Erases and initialises a firmware volume block.
  617. The EraseBlocks() function erases one or more blocks as denoted
  618. by the variable argument list. The entire parameter list of
  619. blocks must be verified before erasing any blocks. If a block is
  620. requested that does not exist within the associated firmware
  621. volume (it has a larger index than the last block of the
  622. firmware volume), the EraseBlocks() function must return the
  623. status code EFI_INVALID_PARAMETER without modifying the contents
  624. of the firmware volume. Implementations should be mindful that
  625. the firmware volume might be in the WriteDisabled state. If it
  626. is in this state, the EraseBlocks() function must return the
  627. status code EFI_ACCESS_DENIED without modifying the contents of
  628. the firmware volume. All calls to EraseBlocks() must be fully
  629. flushed to the hardware before the EraseBlocks() service
  630. returns.
  631. @param This EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
  632. instance.
  633. @param ... The variable argument list is a list of tuples.
  634. Each tuple describes a range of LBAs to erase
  635. and consists of the following:
  636. - An EFI_LBA that indicates the starting LBA
  637. - A UINTN that indicates the number of blocks
  638. to erase.
  639. The list is terminated with an
  640. EFI_LBA_LIST_TERMINATOR.
  641. @retval EFI_SUCCESS The erase request successfully completed.
  642. @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled
  643. state.
  644. @retval EFI_DEVICE_ERROR The block device is not functioning correctly
  645. and could not be written.
  646. The firmware device may have been partially
  647. erased.
  648. @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable
  649. argument list do not exist in the firmware
  650. volume.
  651. **/
  652. EFI_STATUS
  653. EFIAPI
  654. FvbEraseBlocks (
  655. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  656. ...
  657. )
  658. {
  659. EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes;
  660. FVB_DEVICE *FlashInstance;
  661. EFI_STATUS Status;
  662. VA_LIST Args;
  663. UINTN BlockAddress; // Physical address of Lba to erase
  664. EFI_LBA StartingLba; // Lba from which we start erasing
  665. UINTN NumOfLba; // Number of Lba blocks to erase
  666. FlashInstance = INSTANCE_FROM_FVB_THIS (This);
  667. Status = EFI_SUCCESS;
  668. // Detect WriteDisabled state
  669. FvbGetAttributes (This, &FlashFvbAttributes);
  670. if ((FlashFvbAttributes & EFI_FVB2_WRITE_STATUS) == 0) {
  671. DEBUG ((DEBUG_ERROR,
  672. "%a: Device is in WriteDisabled state.\n",
  673. __FUNCTION__));
  674. return EFI_ACCESS_DENIED;
  675. }
  676. //
  677. // Before erasing, check the entire list of parameters to ensure
  678. // all specified blocks are valid.
  679. //
  680. VA_START (Args, This);
  681. do {
  682. // Get the Lba from which we start erasing
  683. StartingLba = VA_ARG (Args, EFI_LBA);
  684. // Have we reached the end of the list?
  685. if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
  686. //Exit the while loop
  687. break;
  688. }
  689. // How many Lba blocks are we requested to erase?
  690. NumOfLba = VA_ARG (Args, UINT32);
  691. // All blocks must be within range
  692. if (NumOfLba == 0 ||
  693. (FlashInstance->StartLba + StartingLba + NumOfLba - 1) >
  694. FlashInstance->Media.LastBlock) {
  695. DEBUG ((DEBUG_ERROR,
  696. "%a: Error: Requested LBA are beyond the last available LBA (%ld).\n",
  697. __FUNCTION__,
  698. FlashInstance->Media.LastBlock));
  699. VA_END (Args);
  700. return EFI_INVALID_PARAMETER;
  701. }
  702. } while (TRUE);
  703. VA_END (Args);
  704. //
  705. // Start erasing
  706. //
  707. VA_START (Args, This);
  708. do {
  709. // Get the Lba from which we start erasing
  710. StartingLba = VA_ARG (Args, EFI_LBA);
  711. // Have we reached the end of the list?
  712. if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
  713. // Exit the while loop
  714. break;
  715. }
  716. // How many Lba blocks are we requested to erase?
  717. NumOfLba = VA_ARG (Args, UINT32);
  718. // Go through each one and erase it
  719. while (NumOfLba > 0) {
  720. // Get the physical address of Lba to erase
  721. BlockAddress = GET_DATA_OFFSET (FlashInstance->FvbOffset,
  722. FlashInstance->StartLba + StartingLba,
  723. FlashInstance->Media.BlockSize);
  724. // Erase single block
  725. Status = FlashInstance->SpiFlashProtocol->Erase (&FlashInstance->SpiDevice,
  726. BlockAddress,
  727. FlashInstance->Media.BlockSize);
  728. if (EFI_ERROR (Status)) {
  729. VA_END (Args);
  730. return EFI_DEVICE_ERROR;
  731. }
  732. // Move to the next Lba
  733. StartingLba++;
  734. NumOfLba--;
  735. }
  736. } while (TRUE);
  737. VA_END (Args);
  738. return EFI_SUCCESS;
  739. }
  740. /**
  741. Fixup internal data so that EFI can be call in virtual mode.
  742. Call the passed in Child Notify event and convert any pointers in
  743. lib to virtual mode.
  744. @param[in] Event The Event that is being processed
  745. @param[in] Context Event Context
  746. **/
  747. STATIC
  748. VOID
  749. EFIAPI
  750. FvbVirtualNotifyEvent (
  751. IN EFI_EVENT Event,
  752. IN VOID *Context
  753. )
  754. {
  755. // Convert SPI memory mapped region
  756. EfiConvertPointer (0x0, (VOID**)&mFvbDevice->RegionBaseAddress);
  757. // Convert SPI device description
  758. EfiConvertPointer (0x0, (VOID**)&mFvbDevice->SpiDevice.Info);
  759. EfiConvertPointer (0x0, (VOID**)&mFvbDevice->SpiDevice.RegBase);
  760. EfiConvertPointer (0x0, (VOID**)&mFvbDevice->SpiDevice);
  761. // Convert SpiFlashProtocol
  762. EfiConvertPointer (0x0, (VOID**)&mFvbDevice->SpiFlashProtocol->Erase);
  763. EfiConvertPointer (0x0, (VOID**)&mFvbDevice->SpiFlashProtocol->Write);
  764. EfiConvertPointer (0x0, (VOID**)&mFvbDevice->SpiFlashProtocol);
  765. return;
  766. }
  767. STATIC
  768. EFI_STATUS
  769. FvbFlashProbe (
  770. IN FVB_DEVICE *FlashInstance
  771. )
  772. {
  773. SPI_FLASH_PROTOCOL *SpiFlashProtocol;
  774. EFI_STATUS Status;
  775. SpiFlashProtocol = FlashInstance->SpiFlashProtocol;
  776. // Read SPI flash ID
  777. Status = SpiFlashProtocol->ReadId (&FlashInstance->SpiDevice, TRUE);
  778. if (EFI_ERROR (Status)) {
  779. return EFI_NOT_FOUND;
  780. }
  781. Status = SpiFlashProtocol->Init (SpiFlashProtocol, &FlashInstance->SpiDevice);
  782. if (EFI_ERROR (Status)) {
  783. DEBUG ((DEBUG_ERROR, "%a: Cannot initialize flash device\n", __FUNCTION__));
  784. return EFI_DEVICE_ERROR;
  785. }
  786. //
  787. // SPI flash may require 20ms interval between enabling it and
  788. // accessing in Direct Mode to its memory mapped content.
  789. //
  790. gBS->Stall (20000);
  791. return EFI_SUCCESS;
  792. }
  793. STATIC
  794. EFI_STATUS
  795. FvbPrepareFvHeader (
  796. IN FVB_DEVICE *FlashInstance
  797. )
  798. {
  799. EFI_BOOT_MODE BootMode;
  800. EFI_STATUS Status;
  801. // Check if it is required to use default environment
  802. BootMode = GetBootModeHob ();
  803. if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {
  804. Status = EFI_INVALID_PARAMETER;
  805. } else {
  806. // Validate header at the beginning of FV region
  807. Status = FvbValidateFvHeader (FlashInstance);
  808. }
  809. // Install the default FVB header if required
  810. if (EFI_ERROR (Status)) {
  811. //if (1) {
  812. // There is no valid header, so time to install one.
  813. DEBUG ((DEBUG_ERROR, "%a: The FVB Header is not valid.\n", __FUNCTION__));
  814. DEBUG ((DEBUG_ERROR,
  815. "%a: Installing a correct one for this volume.\n",
  816. __FUNCTION__));
  817. // Erase entire region that is reserved for variable storage
  818. Status = FlashInstance->SpiFlashProtocol->Erase (&FlashInstance->SpiDevice,
  819. FlashInstance->FvbOffset,
  820. FlashInstance->FvbSize);
  821. if (EFI_ERROR (Status)) {
  822. return Status;
  823. }
  824. // Install all appropriate headers
  825. Status = FvbInitFvAndVariableStoreHeaders (FlashInstance);
  826. if (EFI_ERROR (Status)) {
  827. return Status;
  828. }
  829. }
  830. return EFI_SUCCESS;
  831. }
  832. STATIC
  833. EFI_STATUS
  834. FvbConfigureFlashInstance (
  835. IN OUT FVB_DEVICE *FlashInstance
  836. )
  837. {
  838. EFI_STATUS Status;
  839. UINTN DataOffset;
  840. UINTN VariableSize, FtwWorkingSize, FtwSpareSize, MemorySize;
  841. // Locate SPI protocols
  842. Status = gBS->LocateProtocol (&gJH7110SpiFlashProtocolGuid,
  843. NULL,
  844. (VOID **)&FlashInstance->SpiFlashProtocol);
  845. if (EFI_ERROR (Status)) {
  846. DEBUG ((DEBUG_ERROR, "%a: Cannot locate SpiFlash protocol\n", __FUNCTION__));
  847. return Status;
  848. }
  849. Status = gBS->LocateProtocol (&gJH7110SpiMasterProtocolGuid,
  850. NULL,
  851. (VOID **)&FlashInstance->SpiMasterProtocol);
  852. if (EFI_ERROR (Status)) {
  853. DEBUG ((DEBUG_ERROR, "%a: Cannot locate SpiMaster protocol\n", __FUNCTION__));
  854. return Status;
  855. }
  856. // Setup and probe SPI flash
  857. FlashInstance->SpiMasterProtocol->SetupDevice (FlashInstance->SpiMasterProtocol,
  858. &FlashInstance->SpiDevice,
  859. 0);
  860. Status = FvbFlashProbe (FlashInstance);
  861. if (EFI_ERROR (Status)) {
  862. DEBUG ((DEBUG_ERROR,
  863. "%a: Error while performing SPI flash probe\n",
  864. __FUNCTION__));
  865. return Status;
  866. }
  867. // Fill remaining flash description
  868. VariableSize = PcdGet32 (PcdFlashNvStorageVariableSize);
  869. FtwWorkingSize = PcdGet32 (PcdFlashNvStorageFtwWorkingSize);
  870. FtwSpareSize = PcdGet32 (PcdFlashNvStorageFtwSpareSize);
  871. FlashInstance->IsMemoryMapped = PcdGetBool (PcdSpiMemoryMapped);
  872. FlashInstance->FvbSize = VariableSize + FtwWorkingSize + FtwSpareSize;
  873. FlashInstance->FvbOffset = PcdGet32 (PcdSpiVariableOffset);
  874. FlashInstance->Media.MediaId = 0;
  875. FlashInstance->Media.BlockSize = FlashInstance->SpiDevice.Info->SectorSize;
  876. FlashInstance->Media.LastBlock = FlashInstance->Size /
  877. FlashInstance->Media.BlockSize - 1;
  878. if (FlashInstance->IsMemoryMapped) {
  879. FlashInstance->RegionBaseAddress = PcdGet64 (PcdFlashNvStorageVariableBase64);
  880. } else {
  881. MemorySize = EFI_SIZE_TO_PAGES (FlashInstance->FvbSize);
  882. // FaultTolerantWriteDxe requires memory to be aligned to FtwWorkingSize
  883. FlashInstance->RegionBaseAddress = (UINTN) AllocateAlignedRuntimePages (MemorySize,
  884. SIZE_64KB);
  885. if (FlashInstance->RegionBaseAddress == (UINTN) NULL) {
  886. return EFI_OUT_OF_RESOURCES;
  887. }
  888. Status = PcdSet64S (PcdFlashNvStorageVariableBase64,
  889. (UINT64) FlashInstance->RegionBaseAddress);
  890. ASSERT_EFI_ERROR (Status);
  891. Status = PcdSet64S (PcdFlashNvStorageFtwWorkingBase64,
  892. (UINT64) FlashInstance->RegionBaseAddress
  893. + VariableSize);
  894. ASSERT_EFI_ERROR (Status);
  895. Status = PcdSet64S (PcdFlashNvStorageFtwSpareBase64,
  896. (UINT64) FlashInstance->RegionBaseAddress
  897. + VariableSize
  898. + FtwWorkingSize);
  899. ASSERT_EFI_ERROR (Status);
  900. // Fill the buffer with data from flash
  901. DataOffset = GET_DATA_OFFSET (FlashInstance->FvbOffset,
  902. FlashInstance->StartLba,
  903. FlashInstance->Media.BlockSize);
  904. Status = FlashInstance->SpiFlashProtocol->Read (&FlashInstance->SpiDevice,
  905. DataOffset,
  906. FlashInstance->FvbSize,
  907. (VOID *)FlashInstance->RegionBaseAddress);
  908. if (EFI_ERROR (Status)) {
  909. goto ErrorFreeAllocatedPages;
  910. }
  911. }
  912. Status = gBS->InstallMultipleProtocolInterfaces (&FlashInstance->Handle,
  913. &gEfiDevicePathProtocolGuid, &FlashInstance->DevicePath,
  914. &gEfiFirmwareVolumeBlockProtocolGuid, &FlashInstance->FvbProtocol,
  915. NULL);
  916. if (EFI_ERROR (Status)) {
  917. goto ErrorFreeAllocatedPages;
  918. }
  919. Status = FvbPrepareFvHeader (FlashInstance);
  920. if (EFI_ERROR (Status)) {
  921. goto ErrorPrepareFvbHeader;
  922. }
  923. return EFI_SUCCESS;
  924. ErrorPrepareFvbHeader:
  925. gBS->UninstallMultipleProtocolInterfaces (&FlashInstance->Handle,
  926. &gEfiDevicePathProtocolGuid,
  927. &gEfiFirmwareVolumeBlockProtocolGuid,
  928. NULL);
  929. ErrorFreeAllocatedPages:
  930. if (!FlashInstance->IsMemoryMapped) {
  931. FreeAlignedPages ((VOID *)FlashInstance->RegionBaseAddress,
  932. MemorySize);
  933. }
  934. return Status;
  935. }
  936. EFI_STATUS
  937. EFIAPI
  938. FvbEntryPoint (
  939. IN EFI_HANDLE ImageHandle,
  940. IN EFI_SYSTEM_TABLE *SystemTable
  941. )
  942. {
  943. EFI_STATUS Status;
  944. UINTN RuntimeMmioRegionSize;
  945. UINTN RegionBaseAddress;
  946. //
  947. // Create FVB flash device
  948. //
  949. mFvbDevice = AllocateRuntimeCopyPool (sizeof (mFvbFlashInstanceTemplate),
  950. &mFvbFlashInstanceTemplate);
  951. if (mFvbDevice == NULL) {
  952. DEBUG ((DEBUG_ERROR, "%a: Cannot allocate memory\n", __FUNCTION__));
  953. return EFI_OUT_OF_RESOURCES;
  954. }
  955. //
  956. // Detect and configure flash device
  957. //
  958. Status = FvbConfigureFlashInstance (mFvbDevice);
  959. if (EFI_ERROR (Status)) {
  960. DEBUG ((DEBUG_ERROR, "%a: Fail to configure Fvb SPI device\n", __FUNCTION__));
  961. goto ErrorConfigureFlash;
  962. }
  963. //
  964. // The driver implementing the variable read service can now be dispatched;
  965. // the varstore headers are in place.
  966. //
  967. Status = gBS->InstallProtocolInterface (&gImageHandle,
  968. &gEdkiiNvVarStoreFormattedGuid,
  969. EFI_NATIVE_INTERFACE,
  970. NULL);
  971. if (EFI_ERROR (Status)) {
  972. DEBUG ((DEBUG_ERROR,
  973. "%a: Failed to install gEdkiiNvVarStoreFormattedGuid\n",
  974. __FUNCTION__));
  975. goto ErrorInstallNvVarStoreFormatted;
  976. }
  977. //
  978. // Declare the Non-Volatile storage as EFI_MEMORY_RUNTIME
  979. //
  980. RegionBaseAddress = mFvbDevice->RegionBaseAddress;
  981. if (mFvbDevice->IsMemoryMapped) {
  982. RuntimeMmioRegionSize = mFvbDevice->FvbSize;
  983. Status = gDS->AddMemorySpace (EfiGcdMemoryTypeMemoryMappedIo,
  984. RegionBaseAddress,
  985. RuntimeMmioRegionSize,
  986. EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);
  987. if (EFI_ERROR (Status)) {
  988. DEBUG ((DEBUG_ERROR, "%a: Failed to add memory space\n", __FUNCTION__));
  989. goto ErrorAddSpace;
  990. }
  991. Status = gDS->SetMemorySpaceAttributes (RegionBaseAddress,
  992. RuntimeMmioRegionSize,
  993. EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);
  994. if (EFI_ERROR (Status)) {
  995. DEBUG ((DEBUG_ERROR, "%a: Failed to set memory attributes\n", __FUNCTION__));
  996. goto ErrorSetMemAttr;
  997. }
  998. }
  999. //
  1000. // Register for the virtual address change event
  1001. //
  1002. Status = gBS->CreateEventEx (EVT_NOTIFY_SIGNAL,
  1003. TPL_NOTIFY,
  1004. FvbVirtualNotifyEvent,
  1005. NULL,
  1006. &gEfiEventVirtualAddressChangeGuid,
  1007. &mFvbVirtualAddrChangeEvent);
  1008. if (EFI_ERROR (Status)) {
  1009. DEBUG ((DEBUG_ERROR, "%a: Failed to register VA change event\n", __FUNCTION__));
  1010. goto ErrorSetMemAttr;
  1011. }
  1012. return Status;
  1013. ErrorSetMemAttr:
  1014. gDS->RemoveMemorySpace (RegionBaseAddress, RuntimeMmioRegionSize);
  1015. ErrorAddSpace:
  1016. gBS->UninstallProtocolInterface (gImageHandle,
  1017. &gEdkiiNvVarStoreFormattedGuid,
  1018. NULL);
  1019. ErrorInstallNvVarStoreFormatted:
  1020. gBS->UninstallMultipleProtocolInterfaces (&mFvbDevice->Handle,
  1021. &gEfiDevicePathProtocolGuid,
  1022. &gEfiFirmwareVolumeBlockProtocolGuid,
  1023. NULL);
  1024. ErrorConfigureFlash:
  1025. FreePool (mFvbDevice);
  1026. return Status;
  1027. }