VirtNorFlashFvb.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*++ @file NorFlashFvbDxe.c
  2. Copyright (c) 2011 - 2021, Arm Limited. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. --*/
  5. #include <PiDxe.h>
  6. #include <Library/BaseLib.h>
  7. #include <Library/BaseMemoryLib.h>
  8. #include <Library/MemoryAllocationLib.h>
  9. #include <Library/PcdLib.h>
  10. #include <Library/UefiLib.h>
  11. #include <Guid/NvVarStoreFormatted.h>
  12. #include <Guid/SystemNvDataGuid.h>
  13. #include <Guid/VariableFormat.h>
  14. #include "VirtNorFlash.h"
  15. extern UINTN mFlashNvStorageVariableBase;
  16. ///
  17. /// The Firmware Volume Block Protocol is the low-level interface
  18. /// to a firmware volume. File-level access to a firmware volume
  19. /// should not be done using the Firmware Volume Block Protocol.
  20. /// Normal access to a firmware volume must use the Firmware
  21. /// Volume Protocol. Typically, only the file system driver that
  22. /// produces the Firmware Volume Protocol will bind to the
  23. /// Firmware Volume Block Protocol.
  24. ///
  25. /**
  26. Initialises the FV Header and Variable Store Header
  27. to support variable operations.
  28. @param[in] Ptr - Location to initialise the headers
  29. **/
  30. EFI_STATUS
  31. InitializeFvAndVariableStoreHeaders (
  32. IN NOR_FLASH_INSTANCE *Instance
  33. )
  34. {
  35. EFI_STATUS Status;
  36. VOID *Headers;
  37. UINTN HeadersLength;
  38. EFI_FIRMWARE_VOLUME_HEADER *FirmwareVolumeHeader;
  39. VARIABLE_STORE_HEADER *VariableStoreHeader;
  40. UINT32 NvStorageFtwSpareSize;
  41. UINT32 NvStorageFtwWorkingSize;
  42. UINT32 NvStorageVariableSize;
  43. UINT64 NvStorageFtwSpareBase;
  44. UINT64 NvStorageFtwWorkingBase;
  45. UINT64 NvStorageVariableBase;
  46. HeadersLength = sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY) + sizeof (VARIABLE_STORE_HEADER);
  47. Headers = AllocateZeroPool (HeadersLength);
  48. NvStorageFtwWorkingSize = PcdGet32 (PcdFlashNvStorageFtwWorkingSize);
  49. NvStorageFtwSpareSize = PcdGet32 (PcdFlashNvStorageFtwSpareSize);
  50. NvStorageVariableSize = PcdGet32 (PcdFlashNvStorageVariableSize);
  51. NvStorageFtwSpareBase = (PcdGet64 (PcdFlashNvStorageFtwSpareBase64) != 0) ?
  52. PcdGet64 (PcdFlashNvStorageFtwSpareBase64) : PcdGet32 (PcdFlashNvStorageFtwSpareBase);
  53. NvStorageFtwWorkingBase = (PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) != 0) ?
  54. PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) : PcdGet32 (PcdFlashNvStorageFtwWorkingBase);
  55. NvStorageVariableBase = (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0) ?
  56. PcdGet64 (PcdFlashNvStorageVariableBase64) : PcdGet32 (PcdFlashNvStorageVariableBase);
  57. // FirmwareVolumeHeader->FvLength is declared to have the Variable area AND the FTW working area AND the FTW Spare contiguous.
  58. if ((NvStorageVariableBase + NvStorageVariableSize) != NvStorageFtwWorkingBase) {
  59. DEBUG ((
  60. DEBUG_ERROR,
  61. "%a: NvStorageFtwWorkingBase is not contiguous with NvStorageVariableBase region\n",
  62. __FUNCTION__
  63. ));
  64. return EFI_INVALID_PARAMETER;
  65. }
  66. if ((NvStorageFtwWorkingBase + NvStorageFtwWorkingSize) != NvStorageFtwSpareBase) {
  67. DEBUG ((
  68. DEBUG_ERROR,
  69. "%a: NvStorageFtwSpareBase is not contiguous with NvStorageFtwWorkingBase region\n",
  70. __FUNCTION__
  71. ));
  72. return EFI_INVALID_PARAMETER;
  73. }
  74. // Check if the size of the area is at least one block size
  75. if ((NvStorageVariableSize <= 0) || (NvStorageVariableSize / Instance->BlockSize <= 0)) {
  76. DEBUG ((
  77. DEBUG_ERROR,
  78. "%a: NvStorageVariableSize is 0x%x, should be atleast one block size\n",
  79. __FUNCTION__,
  80. NvStorageVariableSize
  81. ));
  82. return EFI_INVALID_PARAMETER;
  83. }
  84. if ((NvStorageFtwWorkingSize <= 0) || (NvStorageFtwWorkingSize / Instance->BlockSize <= 0)) {
  85. DEBUG ((
  86. DEBUG_ERROR,
  87. "%a: NvStorageFtwWorkingSize is 0x%x, should be atleast one block size\n",
  88. __FUNCTION__,
  89. NvStorageFtwWorkingSize
  90. ));
  91. return EFI_INVALID_PARAMETER;
  92. }
  93. if ((NvStorageFtwSpareSize <= 0) || (NvStorageFtwSpareSize / Instance->BlockSize <= 0)) {
  94. DEBUG ((
  95. DEBUG_ERROR,
  96. "%a: NvStorageFtwSpareSize is 0x%x, should be atleast one block size\n",
  97. __FUNCTION__,
  98. NvStorageFtwSpareSize
  99. ));
  100. return EFI_INVALID_PARAMETER;
  101. }
  102. // Ensure the Variable area Base Addresses are aligned on a block size boundaries
  103. if ((NvStorageVariableBase % Instance->BlockSize != 0) ||
  104. (NvStorageFtwWorkingBase % Instance->BlockSize != 0) ||
  105. (NvStorageFtwSpareBase % Instance->BlockSize != 0))
  106. {
  107. DEBUG ((DEBUG_ERROR, "%a: NvStorage Base addresses must be aligned to block size boundaries", __FUNCTION__));
  108. return EFI_INVALID_PARAMETER;
  109. }
  110. //
  111. // EFI_FIRMWARE_VOLUME_HEADER
  112. //
  113. FirmwareVolumeHeader = (EFI_FIRMWARE_VOLUME_HEADER *)Headers;
  114. CopyGuid (&FirmwareVolumeHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid);
  115. FirmwareVolumeHeader->FvLength =
  116. PcdGet32 (PcdFlashNvStorageVariableSize) +
  117. PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +
  118. PcdGet32 (PcdFlashNvStorageFtwSpareSize);
  119. FirmwareVolumeHeader->Signature = EFI_FVH_SIGNATURE;
  120. FirmwareVolumeHeader->Attributes = (EFI_FVB_ATTRIBUTES_2)(
  121. EFI_FVB2_READ_ENABLED_CAP | // Reads may be enabled
  122. EFI_FVB2_READ_STATUS | // Reads are currently enabled
  123. EFI_FVB2_STICKY_WRITE | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
  124. EFI_FVB2_MEMORY_MAPPED | // It is memory mapped
  125. EFI_FVB2_ERASE_POLARITY | // After erasure all bits take this value (i.e. '1')
  126. EFI_FVB2_WRITE_STATUS | // Writes are currently enabled
  127. EFI_FVB2_WRITE_ENABLED_CAP // Writes may be enabled
  128. );
  129. FirmwareVolumeHeader->HeaderLength = sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY);
  130. FirmwareVolumeHeader->Revision = EFI_FVH_REVISION;
  131. FirmwareVolumeHeader->BlockMap[0].NumBlocks = Instance->LastBlock + 1;
  132. FirmwareVolumeHeader->BlockMap[0].Length = Instance->BlockSize;
  133. FirmwareVolumeHeader->BlockMap[1].NumBlocks = 0;
  134. FirmwareVolumeHeader->BlockMap[1].Length = 0;
  135. FirmwareVolumeHeader->Checksum = CalculateCheckSum16 ((UINT16 *)FirmwareVolumeHeader, FirmwareVolumeHeader->HeaderLength);
  136. //
  137. // VARIABLE_STORE_HEADER
  138. //
  139. VariableStoreHeader = (VARIABLE_STORE_HEADER *)((UINTN)Headers + FirmwareVolumeHeader->HeaderLength);
  140. CopyGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGuid);
  141. VariableStoreHeader->Size = PcdGet32 (PcdFlashNvStorageVariableSize) - FirmwareVolumeHeader->HeaderLength;
  142. VariableStoreHeader->Format = VARIABLE_STORE_FORMATTED;
  143. VariableStoreHeader->State = VARIABLE_STORE_HEALTHY;
  144. // Install the combined super-header in the NorFlash
  145. Status = FvbWrite (&Instance->FvbProtocol, 0, 0, &HeadersLength, Headers);
  146. FreePool (Headers);
  147. return Status;
  148. }
  149. /**
  150. Check the integrity of firmware volume header.
  151. @param[in] FwVolHeader - A pointer to a firmware volume header
  152. @retval EFI_SUCCESS - The firmware volume is consistent
  153. @retval EFI_NOT_FOUND - The firmware volume has been corrupted.
  154. **/
  155. EFI_STATUS
  156. ValidateFvHeader (
  157. IN NOR_FLASH_INSTANCE *Instance
  158. )
  159. {
  160. UINT16 Checksum;
  161. EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
  162. VARIABLE_STORE_HEADER *VariableStoreHeader;
  163. UINTN VariableStoreLength;
  164. UINTN FvLength;
  165. FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)Instance->RegionBaseAddress;
  166. FvLength = PcdGet32 (PcdFlashNvStorageVariableSize) + PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +
  167. PcdGet32 (PcdFlashNvStorageFtwSpareSize);
  168. //
  169. // Verify the header revision, header signature, length
  170. // Length of FvBlock cannot be 2**64-1
  171. // HeaderLength cannot be an odd number
  172. //
  173. if ( (FwVolHeader->Revision != EFI_FVH_REVISION)
  174. || (FwVolHeader->Signature != EFI_FVH_SIGNATURE)
  175. || (FwVolHeader->FvLength != FvLength)
  176. )
  177. {
  178. DEBUG ((
  179. DEBUG_INFO,
  180. "%a: No Firmware Volume header present\n",
  181. __FUNCTION__
  182. ));
  183. return EFI_NOT_FOUND;
  184. }
  185. // Check the Firmware Volume Guid
  186. if ( CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid) == FALSE ) {
  187. DEBUG ((
  188. DEBUG_INFO,
  189. "%a: Firmware Volume Guid non-compatible\n",
  190. __FUNCTION__
  191. ));
  192. return EFI_NOT_FOUND;
  193. }
  194. // Verify the header checksum
  195. Checksum = CalculateSum16 ((UINT16 *)FwVolHeader, FwVolHeader->HeaderLength);
  196. if (Checksum != 0) {
  197. DEBUG ((
  198. DEBUG_INFO,
  199. "%a: FV checksum is invalid (Checksum:0x%X)\n",
  200. __FUNCTION__,
  201. Checksum
  202. ));
  203. return EFI_NOT_FOUND;
  204. }
  205. VariableStoreHeader = (VARIABLE_STORE_HEADER *)((UINTN)FwVolHeader + FwVolHeader->HeaderLength);
  206. // Check the Variable Store Guid
  207. if (!CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) &&
  208. !CompareGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGuid))
  209. {
  210. DEBUG ((
  211. DEBUG_INFO,
  212. "%a: Variable Store Guid non-compatible\n",
  213. __FUNCTION__
  214. ));
  215. return EFI_NOT_FOUND;
  216. }
  217. VariableStoreLength = PcdGet32 (PcdFlashNvStorageVariableSize) - FwVolHeader->HeaderLength;
  218. if (VariableStoreHeader->Size != VariableStoreLength) {
  219. DEBUG ((
  220. DEBUG_INFO,
  221. "%a: Variable Store Length does not match\n",
  222. __FUNCTION__
  223. ));
  224. return EFI_NOT_FOUND;
  225. }
  226. return EFI_SUCCESS;
  227. }
  228. /**
  229. The GetAttributes() function retrieves the attributes and
  230. current settings of the block.
  231. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  232. @param Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the attributes and
  233. current settings are returned.
  234. Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
  235. @retval EFI_SUCCESS The firmware volume attributes were returned.
  236. **/
  237. EFI_STATUS
  238. EFIAPI
  239. FvbGetAttributes (
  240. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  241. OUT EFI_FVB_ATTRIBUTES_2 *Attributes
  242. )
  243. {
  244. EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes;
  245. FlashFvbAttributes = (EFI_FVB_ATTRIBUTES_2)(
  246. EFI_FVB2_READ_ENABLED_CAP | // Reads may be enabled
  247. EFI_FVB2_READ_STATUS | // Reads are currently enabled
  248. EFI_FVB2_STICKY_WRITE | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
  249. EFI_FVB2_MEMORY_MAPPED | // It is memory mapped
  250. EFI_FVB2_ERASE_POLARITY | // After erasure all bits take this value (i.e. '1')
  251. EFI_FVB2_WRITE_STATUS | // Writes are currently enabled
  252. EFI_FVB2_WRITE_ENABLED_CAP // Writes may be enabled
  253. );
  254. *Attributes = FlashFvbAttributes;
  255. DEBUG ((DEBUG_BLKIO, "FvbGetAttributes(0x%X)\n", *Attributes));
  256. return EFI_SUCCESS;
  257. }
  258. /**
  259. The SetAttributes() function sets configurable firmware volume attributes
  260. and returns the new settings of the firmware volume.
  261. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  262. @param Attributes On input, Attributes is a pointer to EFI_FVB_ATTRIBUTES_2
  263. that contains the desired firmware volume settings.
  264. On successful return, it contains the new settings of
  265. the firmware volume.
  266. Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
  267. @retval EFI_SUCCESS The firmware volume attributes were returned.
  268. @retval EFI_INVALID_PARAMETER The attributes requested are in conflict with the capabilities
  269. as declared in the firmware volume header.
  270. **/
  271. EFI_STATUS
  272. EFIAPI
  273. FvbSetAttributes (
  274. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  275. IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
  276. )
  277. {
  278. DEBUG ((DEBUG_BLKIO, "FvbSetAttributes(0x%X) is not supported\n", *Attributes));
  279. return EFI_UNSUPPORTED;
  280. }
  281. /**
  282. The GetPhysicalAddress() function retrieves the base address of
  283. a memory-mapped firmware volume. This function should be called
  284. only for memory-mapped firmware volumes.
  285. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  286. @param Address Pointer to a caller-allocated
  287. EFI_PHYSICAL_ADDRESS that, on successful
  288. return from GetPhysicalAddress(), contains the
  289. base address of the firmware volume.
  290. @retval EFI_SUCCESS The firmware volume base address was returned.
  291. @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.
  292. **/
  293. EFI_STATUS
  294. EFIAPI
  295. FvbGetPhysicalAddress (
  296. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  297. OUT EFI_PHYSICAL_ADDRESS *Address
  298. )
  299. {
  300. NOR_FLASH_INSTANCE *Instance;
  301. Instance = INSTANCE_FROM_FVB_THIS (This);
  302. DEBUG ((DEBUG_BLKIO, "FvbGetPhysicalAddress(BaseAddress=0x%08x)\n", Instance->RegionBaseAddress));
  303. ASSERT (Address != NULL);
  304. *Address = mFlashNvStorageVariableBase;
  305. return EFI_SUCCESS;
  306. }
  307. /**
  308. The GetBlockSize() function retrieves the size of the requested
  309. block. It also returns the number of additional blocks with
  310. the identical size. The GetBlockSize() function is used to
  311. retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).
  312. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  313. @param Lba Indicates the block for which to return the size.
  314. @param BlockSize Pointer to a caller-allocated UINTN in which
  315. the size of the block is returned.
  316. @param NumberOfBlocks Pointer to a caller-allocated UINTN in
  317. which the number of consecutive blocks,
  318. starting with Lba, is returned. All
  319. blocks in this range have a size of
  320. BlockSize.
  321. @retval EFI_SUCCESS The firmware volume base address was returned.
  322. @retval EFI_INVALID_PARAMETER The requested LBA is out of range.
  323. **/
  324. EFI_STATUS
  325. EFIAPI
  326. FvbGetBlockSize (
  327. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  328. IN EFI_LBA Lba,
  329. OUT UINTN *BlockSize,
  330. OUT UINTN *NumberOfBlocks
  331. )
  332. {
  333. EFI_STATUS Status;
  334. NOR_FLASH_INSTANCE *Instance;
  335. Instance = INSTANCE_FROM_FVB_THIS (This);
  336. DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize(Lba=%ld, BlockSize=0x%x, LastBlock=%ld)\n", Lba, Instance->BlockSize, Instance->LastBlock));
  337. if (Lba > Instance->LastBlock) {
  338. DEBUG ((DEBUG_ERROR, "FvbGetBlockSize: ERROR - Parameter LBA %ld is beyond the last Lba (%ld).\n", Lba, Instance->LastBlock));
  339. Status = EFI_INVALID_PARAMETER;
  340. } else {
  341. // This is easy because in this platform each NorFlash device has equal sized blocks.
  342. *BlockSize = (UINTN)Instance->BlockSize;
  343. *NumberOfBlocks = (UINTN)(Instance->LastBlock - Lba + 1);
  344. DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize: *BlockSize=0x%x, *NumberOfBlocks=0x%x.\n", *BlockSize, *NumberOfBlocks));
  345. Status = EFI_SUCCESS;
  346. }
  347. return Status;
  348. }
  349. /**
  350. Reads the specified number of bytes into a buffer from the specified block.
  351. The Read() function reads the requested number of bytes from the
  352. requested block and stores them in the provided buffer.
  353. Implementations should be mindful that the firmware volume
  354. might be in the ReadDisabled state. If it is in this state,
  355. the Read() function must return the status code
  356. EFI_ACCESS_DENIED without modifying the contents of the
  357. buffer. The Read() function must also prevent spanning block
  358. boundaries. If a read is requested that would span a block
  359. boundary, the read must read up to the boundary but not
  360. beyond. The output parameter NumBytes must be set to correctly
  361. indicate the number of bytes actually read. The caller must be
  362. aware that a read may be partially completed.
  363. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  364. @param Lba The starting logical block index from which to read.
  365. @param Offset Offset into the block at which to begin reading.
  366. @param NumBytes Pointer to a UINTN.
  367. At entry, *NumBytes contains the total size of the buffer.
  368. At exit, *NumBytes contains the total number of bytes read.
  369. @param Buffer Pointer to a caller-allocated buffer that will be used
  370. to hold the data that is read.
  371. @retval EFI_SUCCESS The firmware volume was read successfully, and contents are
  372. in Buffer.
  373. @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.
  374. On output, NumBytes contains the total number of bytes
  375. returned in Buffer.
  376. @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state.
  377. @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be read.
  378. **/
  379. EFI_STATUS
  380. EFIAPI
  381. FvbRead (
  382. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  383. IN EFI_LBA Lba,
  384. IN UINTN Offset,
  385. IN OUT UINTN *NumBytes,
  386. IN OUT UINT8 *Buffer
  387. )
  388. {
  389. EFI_STATUS TempStatus;
  390. UINTN BlockSize;
  391. NOR_FLASH_INSTANCE *Instance;
  392. Instance = INSTANCE_FROM_FVB_THIS (This);
  393. DEBUG ((DEBUG_BLKIO, "FvbRead(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Instance->StartLba + Lba, Offset, *NumBytes, Buffer));
  394. TempStatus = EFI_SUCCESS;
  395. // Cache the block size to avoid de-referencing pointers all the time
  396. BlockSize = Instance->BlockSize;
  397. DEBUG ((DEBUG_BLKIO, "FvbRead: Check if (Offset=0x%x + NumBytes=0x%x) <= BlockSize=0x%x\n", Offset, *NumBytes, BlockSize));
  398. // The read must not span block boundaries.
  399. // We need to check each variable individually because adding two large values together overflows.
  400. if ((Offset >= BlockSize) ||
  401. (*NumBytes > BlockSize) ||
  402. ((Offset + *NumBytes) > BlockSize))
  403. {
  404. DEBUG ((DEBUG_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize));
  405. return EFI_BAD_BUFFER_SIZE;
  406. }
  407. // We must have some bytes to read
  408. if (*NumBytes == 0) {
  409. return EFI_BAD_BUFFER_SIZE;
  410. }
  411. // Decide if we are doing full block reads or not.
  412. if (*NumBytes % BlockSize != 0) {
  413. TempStatus = NorFlashRead (Instance, Instance->StartLba + Lba, Offset, *NumBytes, Buffer);
  414. if (EFI_ERROR (TempStatus)) {
  415. return EFI_DEVICE_ERROR;
  416. }
  417. } else {
  418. // Read NOR Flash data into shadow buffer
  419. TempStatus = NorFlashReadBlocks (Instance, Instance->StartLba + Lba, BlockSize, Buffer);
  420. if (EFI_ERROR (TempStatus)) {
  421. // Return one of the pre-approved error statuses
  422. return EFI_DEVICE_ERROR;
  423. }
  424. }
  425. return EFI_SUCCESS;
  426. }
  427. /**
  428. Writes the specified number of bytes from the input buffer to the block.
  429. The Write() function writes the specified number of bytes from
  430. the provided buffer to the specified block and offset. If the
  431. firmware volume is sticky write, the caller must ensure that
  432. all the bits of the specified range to write are in the
  433. EFI_FVB_ERASE_POLARITY state before calling the Write()
  434. function, or else the result will be unpredictable. This
  435. unpredictability arises because, for a sticky-write firmware
  436. volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY
  437. state but cannot flip it back again. Before calling the
  438. Write() function, it is recommended for the caller to first call
  439. the EraseBlocks() function to erase the specified block to
  440. write. A block erase cycle will transition bits from the
  441. (NOT)EFI_FVB_ERASE_POLARITY state back to the
  442. EFI_FVB_ERASE_POLARITY state. Implementations should be
  443. mindful that the firmware volume might be in the WriteDisabled
  444. state. If it is in this state, the Write() function must
  445. return the status code EFI_ACCESS_DENIED without modifying the
  446. contents of the firmware volume. The Write() function must
  447. also prevent spanning block boundaries. If a write is
  448. requested that spans a block boundary, the write must store up
  449. to the boundary but not beyond. The output parameter NumBytes
  450. must be set to correctly indicate the number of bytes actually
  451. written. The caller must be aware that a write may be
  452. partially completed. All writes, partial or otherwise, must be
  453. fully flushed to the hardware before the Write() service
  454. returns.
  455. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  456. @param Lba The starting logical block index to write to.
  457. @param Offset Offset into the block at which to begin writing.
  458. @param NumBytes The pointer to a UINTN.
  459. At entry, *NumBytes contains the total size of the buffer.
  460. At exit, *NumBytes contains the total number of bytes actually written.
  461. @param Buffer The pointer to a caller-allocated buffer that contains the source for the write.
  462. @retval EFI_SUCCESS The firmware volume was written successfully.
  463. @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
  464. On output, NumBytes contains the total number of bytes
  465. actually written.
  466. @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
  467. @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not be written.
  468. **/
  469. EFI_STATUS
  470. EFIAPI
  471. FvbWrite (
  472. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  473. IN EFI_LBA Lba,
  474. IN UINTN Offset,
  475. IN OUT UINTN *NumBytes,
  476. IN UINT8 *Buffer
  477. )
  478. {
  479. NOR_FLASH_INSTANCE *Instance;
  480. Instance = INSTANCE_FROM_FVB_THIS (This);
  481. return NorFlashWriteSingleBlock (Instance, Instance->StartLba + Lba, Offset, NumBytes, Buffer);
  482. }
  483. /**
  484. Erases and initialises a firmware volume block.
  485. The EraseBlocks() function erases one or more blocks as denoted
  486. by the variable argument list. The entire parameter list of
  487. blocks must be verified before erasing any blocks. If a block is
  488. requested that does not exist within the associated firmware
  489. volume (it has a larger index than the last block of the
  490. firmware volume), the EraseBlocks() function must return the
  491. status code EFI_INVALID_PARAMETER without modifying the contents
  492. of the firmware volume. Implementations should be mindful that
  493. the firmware volume might be in the WriteDisabled state. If it
  494. is in this state, the EraseBlocks() function must return the
  495. status code EFI_ACCESS_DENIED without modifying the contents of
  496. the firmware volume. All calls to EraseBlocks() must be fully
  497. flushed to the hardware before the EraseBlocks() service
  498. returns.
  499. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
  500. instance.
  501. @param ... The variable argument list is a list of tuples.
  502. Each tuple describes a range of LBAs to erase
  503. and consists of the following:
  504. - An EFI_LBA that indicates the starting LBA
  505. - A UINTN that indicates the number of blocks to erase.
  506. The list is terminated with an EFI_LBA_LIST_TERMINATOR.
  507. For example, the following indicates that two ranges of blocks
  508. (5-7 and 10-11) are to be erased:
  509. EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);
  510. @retval EFI_SUCCESS The erase request successfully completed.
  511. @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
  512. @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be written.
  513. The firmware device may have been partially erased.
  514. @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable argument list do
  515. not exist in the firmware volume.
  516. **/
  517. EFI_STATUS
  518. EFIAPI
  519. FvbEraseBlocks (
  520. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  521. ...
  522. )
  523. {
  524. EFI_STATUS Status;
  525. VA_LIST Args;
  526. UINTN BlockAddress; // Physical address of Lba to erase
  527. EFI_LBA StartingLba; // Lba from which we start erasing
  528. UINTN NumOfLba; // Number of Lba blocks to erase
  529. NOR_FLASH_INSTANCE *Instance;
  530. Instance = INSTANCE_FROM_FVB_THIS (This);
  531. DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks()\n"));
  532. Status = EFI_SUCCESS;
  533. // Before erasing, check the entire list of parameters to ensure all specified blocks are valid
  534. VA_START (Args, This);
  535. do {
  536. // Get the Lba from which we start erasing
  537. StartingLba = VA_ARG (Args, EFI_LBA);
  538. // Have we reached the end of the list?
  539. if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
  540. // Exit the while loop
  541. break;
  542. }
  543. // How many Lba blocks are we requested to erase?
  544. NumOfLba = VA_ARG (Args, UINTN);
  545. // All blocks must be within range
  546. DEBUG ((
  547. DEBUG_BLKIO,
  548. "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%Lu - 1 ) > LastBlock=%ld.\n",
  549. Instance->StartLba + StartingLba,
  550. (UINT64)NumOfLba,
  551. Instance->LastBlock
  552. ));
  553. if ((NumOfLba == 0) || ((Instance->StartLba + StartingLba + NumOfLba - 1) > Instance->LastBlock)) {
  554. VA_END (Args);
  555. DEBUG ((DEBUG_ERROR, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n"));
  556. Status = EFI_INVALID_PARAMETER;
  557. goto EXIT;
  558. }
  559. } while (TRUE);
  560. VA_END (Args);
  561. //
  562. // To get here, all must be ok, so start erasing
  563. //
  564. VA_START (Args, This);
  565. do {
  566. // Get the Lba from which we start erasing
  567. StartingLba = VA_ARG (Args, EFI_LBA);
  568. // Have we reached the end of the list?
  569. if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
  570. // Exit the while loop
  571. break;
  572. }
  573. // How many Lba blocks are we requested to erase?
  574. NumOfLba = VA_ARG (Args, UINTN);
  575. // Go through each one and erase it
  576. while (NumOfLba > 0) {
  577. // Get the physical address of Lba to erase
  578. BlockAddress = GET_NOR_BLOCK_ADDRESS (
  579. Instance->RegionBaseAddress,
  580. Instance->StartLba + StartingLba,
  581. Instance->BlockSize
  582. );
  583. // Erase it
  584. DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks: Erasing Lba=%ld @ 0x%08x.\n", Instance->StartLba + StartingLba, BlockAddress));
  585. Status = NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress);
  586. if (EFI_ERROR (Status)) {
  587. VA_END (Args);
  588. Status = EFI_DEVICE_ERROR;
  589. goto EXIT;
  590. }
  591. // Move to the next Lba
  592. StartingLba++;
  593. NumOfLba--;
  594. }
  595. } while (TRUE);
  596. VA_END (Args);
  597. EXIT:
  598. return Status;
  599. }
  600. /**
  601. Fixup internal data so that EFI can be call in virtual mode.
  602. Call the passed in Child Notify event and convert any pointers in
  603. lib to virtual mode.
  604. @param[in] Event The Event that is being processed
  605. @param[in] Context Event Context
  606. **/
  607. VOID
  608. EFIAPI
  609. FvbVirtualNotifyEvent (
  610. IN EFI_EVENT Event,
  611. IN VOID *Context
  612. )
  613. {
  614. EfiConvertPointer (0x0, (VOID **)&mFlashNvStorageVariableBase);
  615. return;
  616. }