NorFlashFvb.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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/PcdLib.h>
  7. #include <Library/BaseLib.h>
  8. #include <Library/UefiLib.h>
  9. #include <Library/BaseMemoryLib.h>
  10. #include <Library/MemoryAllocationLib.h>
  11. #include <Guid/VariableFormat.h>
  12. #include <Guid/SystemNvDataGuid.h>
  13. #include <Guid/NvVarStoreFormatted.h>
  14. #include "NorFlash.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->Media.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->Media.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->Media.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->Media.BlockSize != 0) ||
  104. (NvStorageFtwWorkingBase % Instance->Media.BlockSize != 0) ||
  105. (NvStorageFtwSpareBase % Instance->Media.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->Media.LastBlock + 1;
  132. FirmwareVolumeHeader->BlockMap[0].Length = Instance->Media.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. NOR_FLASH_INSTANCE *Instance;
  246. Instance = INSTANCE_FROM_FVB_THIS (This);
  247. FlashFvbAttributes = (EFI_FVB_ATTRIBUTES_2)(
  248. EFI_FVB2_READ_ENABLED_CAP | // Reads may be enabled
  249. EFI_FVB2_READ_STATUS | // Reads are currently enabled
  250. EFI_FVB2_STICKY_WRITE | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
  251. EFI_FVB2_MEMORY_MAPPED | // It is memory mapped
  252. EFI_FVB2_ERASE_POLARITY // After erasure all bits take this value (i.e. '1')
  253. );
  254. // Check if it is write protected
  255. if (Instance->Media.ReadOnly != TRUE) {
  256. FlashFvbAttributes = FlashFvbAttributes |
  257. EFI_FVB2_WRITE_STATUS | // Writes are currently enabled
  258. EFI_FVB2_WRITE_ENABLED_CAP; // Writes may be enabled
  259. }
  260. *Attributes = FlashFvbAttributes;
  261. DEBUG ((DEBUG_BLKIO, "FvbGetAttributes(0x%X)\n", *Attributes));
  262. return EFI_SUCCESS;
  263. }
  264. /**
  265. The SetAttributes() function sets configurable firmware volume attributes
  266. and returns the new settings of the firmware volume.
  267. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  268. @param Attributes On input, Attributes is a pointer to EFI_FVB_ATTRIBUTES_2
  269. that contains the desired firmware volume settings.
  270. On successful return, it contains the new settings of
  271. the firmware volume.
  272. Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
  273. @retval EFI_SUCCESS The firmware volume attributes were returned.
  274. @retval EFI_INVALID_PARAMETER The attributes requested are in conflict with the capabilities
  275. as declared in the firmware volume header.
  276. **/
  277. EFI_STATUS
  278. EFIAPI
  279. FvbSetAttributes (
  280. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  281. IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
  282. )
  283. {
  284. DEBUG ((DEBUG_BLKIO, "FvbSetAttributes(0x%X) is not supported\n", *Attributes));
  285. return EFI_UNSUPPORTED;
  286. }
  287. /**
  288. The GetPhysicalAddress() function retrieves the base address of
  289. a memory-mapped firmware volume. This function should be called
  290. only for memory-mapped firmware volumes.
  291. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  292. @param Address Pointer to a caller-allocated
  293. EFI_PHYSICAL_ADDRESS that, on successful
  294. return from GetPhysicalAddress(), contains the
  295. base address of the firmware volume.
  296. @retval EFI_SUCCESS The firmware volume base address was returned.
  297. @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.
  298. **/
  299. EFI_STATUS
  300. EFIAPI
  301. FvbGetPhysicalAddress (
  302. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  303. OUT EFI_PHYSICAL_ADDRESS *Address
  304. )
  305. {
  306. NOR_FLASH_INSTANCE *Instance;
  307. Instance = INSTANCE_FROM_FVB_THIS (This);
  308. DEBUG ((DEBUG_BLKIO, "FvbGetPhysicalAddress(BaseAddress=0x%08x)\n", Instance->RegionBaseAddress));
  309. ASSERT (Address != NULL);
  310. *Address = mFlashNvStorageVariableBase;
  311. return EFI_SUCCESS;
  312. }
  313. /**
  314. The GetBlockSize() function retrieves the size of the requested
  315. block. It also returns the number of additional blocks with
  316. the identical size. The GetBlockSize() function is used to
  317. retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).
  318. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  319. @param Lba Indicates the block for which to return the size.
  320. @param BlockSize Pointer to a caller-allocated UINTN in which
  321. the size of the block is returned.
  322. @param NumberOfBlocks Pointer to a caller-allocated UINTN in
  323. which the number of consecutive blocks,
  324. starting with Lba, is returned. All
  325. blocks in this range have a size of
  326. BlockSize.
  327. @retval EFI_SUCCESS The firmware volume base address was returned.
  328. @retval EFI_INVALID_PARAMETER The requested LBA is out of range.
  329. **/
  330. EFI_STATUS
  331. EFIAPI
  332. FvbGetBlockSize (
  333. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  334. IN EFI_LBA Lba,
  335. OUT UINTN *BlockSize,
  336. OUT UINTN *NumberOfBlocks
  337. )
  338. {
  339. EFI_STATUS Status;
  340. NOR_FLASH_INSTANCE *Instance;
  341. Instance = INSTANCE_FROM_FVB_THIS (This);
  342. DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize(Lba=%ld, BlockSize=0x%x, LastBlock=%ld)\n", Lba, Instance->Media.BlockSize, Instance->Media.LastBlock));
  343. if (Lba > Instance->Media.LastBlock) {
  344. DEBUG ((DEBUG_ERROR, "FvbGetBlockSize: ERROR - Parameter LBA %ld is beyond the last Lba (%ld).\n", Lba, Instance->Media.LastBlock));
  345. Status = EFI_INVALID_PARAMETER;
  346. } else {
  347. // This is easy because in this platform each NorFlash device has equal sized blocks.
  348. *BlockSize = (UINTN)Instance->Media.BlockSize;
  349. *NumberOfBlocks = (UINTN)(Instance->Media.LastBlock - Lba + 1);
  350. DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize: *BlockSize=0x%x, *NumberOfBlocks=0x%x.\n", *BlockSize, *NumberOfBlocks));
  351. Status = EFI_SUCCESS;
  352. }
  353. return Status;
  354. }
  355. /**
  356. Reads the specified number of bytes into a buffer from the specified block.
  357. The Read() function reads the requested number of bytes from the
  358. requested block and stores them in the provided buffer.
  359. Implementations should be mindful that the firmware volume
  360. might be in the ReadDisabled state. If it is in this state,
  361. the Read() function must return the status code
  362. EFI_ACCESS_DENIED without modifying the contents of the
  363. buffer. The Read() function must also prevent spanning block
  364. boundaries. If a read is requested that would span a block
  365. boundary, the read must read up to the boundary but not
  366. beyond. The output parameter NumBytes must be set to correctly
  367. indicate the number of bytes actually read. The caller must be
  368. aware that a read may be partially completed.
  369. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  370. @param Lba The starting logical block index from which to read.
  371. @param Offset Offset into the block at which to begin reading.
  372. @param NumBytes Pointer to a UINTN.
  373. At entry, *NumBytes contains the total size of the buffer.
  374. At exit, *NumBytes contains the total number of bytes read.
  375. @param Buffer Pointer to a caller-allocated buffer that will be used
  376. to hold the data that is read.
  377. @retval EFI_SUCCESS The firmware volume was read successfully, and contents are
  378. in Buffer.
  379. @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.
  380. On output, NumBytes contains the total number of bytes
  381. returned in Buffer.
  382. @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state.
  383. @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be read.
  384. **/
  385. EFI_STATUS
  386. EFIAPI
  387. FvbRead (
  388. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  389. IN EFI_LBA Lba,
  390. IN UINTN Offset,
  391. IN OUT UINTN *NumBytes,
  392. IN OUT UINT8 *Buffer
  393. )
  394. {
  395. EFI_STATUS TempStatus;
  396. UINTN BlockSize;
  397. NOR_FLASH_INSTANCE *Instance;
  398. Instance = INSTANCE_FROM_FVB_THIS (This);
  399. DEBUG ((DEBUG_BLKIO, "FvbRead(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Instance->StartLba + Lba, Offset, *NumBytes, Buffer));
  400. TempStatus = EFI_SUCCESS;
  401. // Cache the block size to avoid de-referencing pointers all the time
  402. BlockSize = Instance->Media.BlockSize;
  403. DEBUG ((DEBUG_BLKIO, "FvbRead: Check if (Offset=0x%x + NumBytes=0x%x) <= BlockSize=0x%x\n", Offset, *NumBytes, BlockSize));
  404. // The read must not span block boundaries.
  405. // We need to check each variable individually because adding two large values together overflows.
  406. if ((Offset >= BlockSize) ||
  407. (*NumBytes > BlockSize) ||
  408. ((Offset + *NumBytes) > BlockSize))
  409. {
  410. DEBUG ((DEBUG_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize));
  411. return EFI_BAD_BUFFER_SIZE;
  412. }
  413. // We must have some bytes to read
  414. if (*NumBytes == 0) {
  415. return EFI_BAD_BUFFER_SIZE;
  416. }
  417. // Decide if we are doing full block reads or not.
  418. if (*NumBytes % BlockSize != 0) {
  419. TempStatus = NorFlashRead (Instance, Instance->StartLba + Lba, Offset, *NumBytes, Buffer);
  420. if (EFI_ERROR (TempStatus)) {
  421. return EFI_DEVICE_ERROR;
  422. }
  423. } else {
  424. // Read NOR Flash data into shadow buffer
  425. TempStatus = NorFlashReadBlocks (Instance, Instance->StartLba + Lba, BlockSize, Buffer);
  426. if (EFI_ERROR (TempStatus)) {
  427. // Return one of the pre-approved error statuses
  428. return EFI_DEVICE_ERROR;
  429. }
  430. }
  431. return EFI_SUCCESS;
  432. }
  433. /**
  434. Writes the specified number of bytes from the input buffer to the block.
  435. The Write() function writes the specified number of bytes from
  436. the provided buffer to the specified block and offset. If the
  437. firmware volume is sticky write, the caller must ensure that
  438. all the bits of the specified range to write are in the
  439. EFI_FVB_ERASE_POLARITY state before calling the Write()
  440. function, or else the result will be unpredictable. This
  441. unpredictability arises because, for a sticky-write firmware
  442. volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY
  443. state but cannot flip it back again. Before calling the
  444. Write() function, it is recommended for the caller to first call
  445. the EraseBlocks() function to erase the specified block to
  446. write. A block erase cycle will transition bits from the
  447. (NOT)EFI_FVB_ERASE_POLARITY state back to the
  448. EFI_FVB_ERASE_POLARITY state. Implementations should be
  449. mindful that the firmware volume might be in the WriteDisabled
  450. state. If it is in this state, the Write() function must
  451. return the status code EFI_ACCESS_DENIED without modifying the
  452. contents of the firmware volume. The Write() function must
  453. also prevent spanning block boundaries. If a write is
  454. requested that spans a block boundary, the write must store up
  455. to the boundary but not beyond. The output parameter NumBytes
  456. must be set to correctly indicate the number of bytes actually
  457. written. The caller must be aware that a write may be
  458. partially completed. All writes, partial or otherwise, must be
  459. fully flushed to the hardware before the Write() service
  460. returns.
  461. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  462. @param Lba The starting logical block index to write to.
  463. @param Offset Offset into the block at which to begin writing.
  464. @param NumBytes The pointer to a UINTN.
  465. At entry, *NumBytes contains the total size of the buffer.
  466. At exit, *NumBytes contains the total number of bytes actually written.
  467. @param Buffer The pointer to a caller-allocated buffer that contains the source for the write.
  468. @retval EFI_SUCCESS The firmware volume was written successfully.
  469. @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
  470. On output, NumBytes contains the total number of bytes
  471. actually written.
  472. @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
  473. @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not be written.
  474. **/
  475. EFI_STATUS
  476. EFIAPI
  477. FvbWrite (
  478. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  479. IN EFI_LBA Lba,
  480. IN UINTN Offset,
  481. IN OUT UINTN *NumBytes,
  482. IN UINT8 *Buffer
  483. )
  484. {
  485. NOR_FLASH_INSTANCE *Instance;
  486. Instance = INSTANCE_FROM_FVB_THIS (This);
  487. return NorFlashWriteSingleBlock (Instance, Instance->StartLba + Lba, Offset, NumBytes, Buffer);
  488. }
  489. /**
  490. Erases and initialises a firmware volume block.
  491. The EraseBlocks() function erases one or more blocks as denoted
  492. by the variable argument list. The entire parameter list of
  493. blocks must be verified before erasing any blocks. If a block is
  494. requested that does not exist within the associated firmware
  495. volume (it has a larger index than the last block of the
  496. firmware volume), the EraseBlocks() function must return the
  497. status code EFI_INVALID_PARAMETER without modifying the contents
  498. of the firmware volume. Implementations should be mindful that
  499. the firmware volume might be in the WriteDisabled state. If it
  500. is in this state, the EraseBlocks() function must return the
  501. status code EFI_ACCESS_DENIED without modifying the contents of
  502. the firmware volume. All calls to EraseBlocks() must be fully
  503. flushed to the hardware before the EraseBlocks() service
  504. returns.
  505. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
  506. instance.
  507. @param ... The variable argument list is a list of tuples.
  508. Each tuple describes a range of LBAs to erase
  509. and consists of the following:
  510. - An EFI_LBA that indicates the starting LBA
  511. - A UINTN that indicates the number of blocks to erase.
  512. The list is terminated with an EFI_LBA_LIST_TERMINATOR.
  513. For example, the following indicates that two ranges of blocks
  514. (5-7 and 10-11) are to be erased:
  515. EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);
  516. @retval EFI_SUCCESS The erase request successfully completed.
  517. @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
  518. @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be written.
  519. The firmware device may have been partially erased.
  520. @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable argument list do
  521. not exist in the firmware volume.
  522. **/
  523. EFI_STATUS
  524. EFIAPI
  525. FvbEraseBlocks (
  526. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  527. ...
  528. )
  529. {
  530. EFI_STATUS Status;
  531. VA_LIST Args;
  532. UINTN BlockAddress; // Physical address of Lba to erase
  533. EFI_LBA StartingLba; // Lba from which we start erasing
  534. UINTN NumOfLba; // Number of Lba blocks to erase
  535. NOR_FLASH_INSTANCE *Instance;
  536. Instance = INSTANCE_FROM_FVB_THIS (This);
  537. DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks()\n"));
  538. Status = EFI_SUCCESS;
  539. // Detect WriteDisabled state
  540. if (Instance->Media.ReadOnly == TRUE) {
  541. // Firmware volume is in WriteDisabled state
  542. DEBUG ((DEBUG_ERROR, "FvbEraseBlocks: ERROR - Device is in WriteDisabled state.\n"));
  543. return EFI_ACCESS_DENIED;
  544. }
  545. // Before erasing, check the entire list of parameters to ensure all specified blocks are valid
  546. VA_START (Args, This);
  547. do {
  548. // Get the Lba from which we start erasing
  549. StartingLba = VA_ARG (Args, EFI_LBA);
  550. // Have we reached the end of the list?
  551. if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
  552. // Exit the while loop
  553. break;
  554. }
  555. // How many Lba blocks are we requested to erase?
  556. NumOfLba = VA_ARG (Args, UINTN);
  557. // All blocks must be within range
  558. DEBUG ((
  559. DEBUG_BLKIO,
  560. "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%Lu - 1 ) > LastBlock=%ld.\n",
  561. Instance->StartLba + StartingLba,
  562. (UINT64)NumOfLba,
  563. Instance->Media.LastBlock
  564. ));
  565. if ((NumOfLba == 0) || ((Instance->StartLba + StartingLba + NumOfLba - 1) > Instance->Media.LastBlock)) {
  566. VA_END (Args);
  567. DEBUG ((DEBUG_ERROR, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n"));
  568. Status = EFI_INVALID_PARAMETER;
  569. goto EXIT;
  570. }
  571. } while (TRUE);
  572. VA_END (Args);
  573. //
  574. // To get here, all must be ok, so start erasing
  575. //
  576. VA_START (Args, This);
  577. do {
  578. // Get the Lba from which we start erasing
  579. StartingLba = VA_ARG (Args, EFI_LBA);
  580. // Have we reached the end of the list?
  581. if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
  582. // Exit the while loop
  583. break;
  584. }
  585. // How many Lba blocks are we requested to erase?
  586. NumOfLba = VA_ARG (Args, UINTN);
  587. // Go through each one and erase it
  588. while (NumOfLba > 0) {
  589. // Get the physical address of Lba to erase
  590. BlockAddress = GET_NOR_BLOCK_ADDRESS (
  591. Instance->RegionBaseAddress,
  592. Instance->StartLba + StartingLba,
  593. Instance->Media.BlockSize
  594. );
  595. // Erase it
  596. DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks: Erasing Lba=%ld @ 0x%08x.\n", Instance->StartLba + StartingLba, BlockAddress));
  597. Status = NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress);
  598. if (EFI_ERROR (Status)) {
  599. VA_END (Args);
  600. Status = EFI_DEVICE_ERROR;
  601. goto EXIT;
  602. }
  603. // Move to the next Lba
  604. StartingLba++;
  605. NumOfLba--;
  606. }
  607. } while (TRUE);
  608. VA_END (Args);
  609. EXIT:
  610. return Status;
  611. }
  612. /**
  613. Fixup internal data so that EFI can be call in virtual mode.
  614. Call the passed in Child Notify event and convert any pointers in
  615. lib to virtual mode.
  616. @param[in] Event The Event that is being processed
  617. @param[in] Context Event Context
  618. **/
  619. VOID
  620. EFIAPI
  621. FvbVirtualNotifyEvent (
  622. IN EFI_EVENT Event,
  623. IN VOID *Context
  624. )
  625. {
  626. EfiConvertPointer (0x0, (VOID **)&mFlashNvStorageVariableBase);
  627. return;
  628. }