FlashFvbDxe.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. /** @file
  2. *
  3. * Copyright (c) 2011-2015, ARM Limited. All rights reserved.
  4. * Copyright (c) 2015, Hisilicon Limited. All rights reserved.
  5. * Copyright (c) 2015, Linaro Limited. All rights reserved.
  6. *
  7. * This program and the accompanying materials
  8. * are licensed and made available under the terms and conditions of the BSD License
  9. * which accompanies this distribution. The full text of the license may be found at
  10. * http://opensource.org/licenses/bsd-license.php
  11. *
  12. * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  14. *
  15. * Based on files under ArmPlatformPkg/Drivers/NorFlashDxe/
  16. **/
  17. #include "FlashFvbDxe.h"
  18. STATIC EFI_EVENT mFlashFvbVirtualAddrChangeEvent;
  19. STATIC UINTN mFlashNvStorageVariableBase;
  20. //
  21. // Global variable declarations
  22. //
  23. FLASH_DESCRIPTION mFlashDevices[FLASH_DEVICE_COUNT] =
  24. {
  25. {
  26. // UEFI Variable Services non-volatile storage
  27. FixedPcdGet64 (PcdSFCMEM0BaseAddress),
  28. FixedPcdGet64 (PcdFlashNvStorageVariableBase64),
  29. 0x20000,
  30. SIZE_64KB,
  31. {0xCC2CBF29, 0x1498, 0x4CDD, {0x81, 0x71, 0xF8, 0xB6, 0xB4, 0x1D, 0x09, 0x09}}
  32. }
  33. };
  34. FLASH_INSTANCE** mFlashInstances;
  35. FLASH_INSTANCE mFlashInstanceTemplate =
  36. {
  37. FLASH_SIGNATURE, // Signature
  38. NULL, // Handle ... NEED TO BE FILLED
  39. FALSE, // Initialized
  40. NULL, // Initialize
  41. 0, // DeviceBaseAddress ... NEED TO BE FILLED
  42. 0, // RegionBaseAddress ... NEED TO BE FILLED
  43. 0, // Size ... NEED TO BE FILLED
  44. 0, // StartLba
  45. {
  46. EFI_BLOCK_IO_PROTOCOL_REVISION2, // Revision
  47. NULL, // Media ... NEED TO BE FILLED
  48. NULL, //NorFlashBlockIoReset
  49. FlashBlockIoReadBlocks,
  50. FlashBlockIoWriteBlocks,
  51. FlashBlockIoFlushBlocks
  52. }, // BlockIoProtocol
  53. {
  54. 0, // MediaId ... NEED TO BE FILLED
  55. FALSE, // RemovableMedia
  56. TRUE, // MediaPresent
  57. FALSE, // LogicalPartition
  58. FALSE, // ReadOnly
  59. FALSE, // WriteCaching;
  60. SIZE_64KB, // BlockSize ... NEED TO BE FILLED
  61. 4, // IoAlign
  62. 0, // LastBlock ... NEED TO BE FILLED
  63. 0, // LowestAlignedLba
  64. 1, // LogicalBlocksPerPhysicalBlock
  65. }, //Media;
  66. FALSE, // SupportFvb ... NEED TO BE FILLED
  67. {
  68. FvbGetAttributes,
  69. FvbSetAttributes,
  70. FvbGetPhysicalAddress,
  71. FvbGetBlockSize,
  72. FvbRead,
  73. FvbWrite,
  74. FvbEraseBlocks,
  75. NULL, //ParentHandle
  76. }, // FvbProtoccol;
  77. {
  78. {
  79. {
  80. HARDWARE_DEVICE_PATH,
  81. HW_VENDOR_DP,
  82. {(UINT8)(sizeof(VENDOR_DEVICE_PATH)),
  83. (UINT8)((sizeof(VENDOR_DEVICE_PATH)) >> 8)},
  84. },
  85. { 0x0, 0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }}, // GUID ... NEED TO BE FILLED
  86. },
  87. {
  88. END_DEVICE_PATH_TYPE,
  89. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  90. {sizeof (EFI_DEVICE_PATH_PROTOCOL),
  91. 0}
  92. }
  93. } // DevicePath
  94. };
  95. HISI_SPI_FLASH_PROTOCOL* mFlash;
  96. ///
  97. /// The Firmware Volume Block Protocol is the low-level interface
  98. /// to a firmware volume. File-level access to a firmware volume
  99. /// should not be done using the Firmware Volume Block Protocol.
  100. /// Normal access to a firmware volume must use the Firmware
  101. /// Volume Protocol. Typically, only the file system driver that
  102. /// produces the Firmware Volume Protocol will bind to the
  103. /// Firmware Volume Block Protocol.
  104. ///
  105. /**
  106. Initialises the FV Header and Variable Store Header
  107. to support variable operations.
  108. @param[in] Ptr - Location to initialise the headers
  109. **/
  110. EFI_STATUS
  111. InitializeFvAndVariableStoreHeaders (
  112. IN FLASH_INSTANCE* Instance
  113. )
  114. {
  115. EFI_STATUS Status;
  116. VOID* Headers;
  117. UINTN HeadersLength;
  118. EFI_FIRMWARE_VOLUME_HEADER* FirmwareVolumeHeader;
  119. VARIABLE_STORE_HEADER* VariableStoreHeader;
  120. if (!Instance->Initialized && Instance->Initialize)
  121. {
  122. Instance->Initialize (Instance);
  123. }
  124. HeadersLength = sizeof(EFI_FIRMWARE_VOLUME_HEADER) + sizeof(EFI_FV_BLOCK_MAP_ENTRY) + sizeof(VARIABLE_STORE_HEADER);
  125. Headers = AllocateZeroPool(HeadersLength);
  126. // FirmwareVolumeHeader->FvLength is declared to have the Variable area AND the FTW working area AND the FTW Spare contiguous.
  127. ASSERT(PcdGet64(PcdFlashNvStorageVariableBase64) + PcdGet32(PcdFlashNvStorageVariableSize) == PcdGet64(PcdFlashNvStorageFtwWorkingBase64));
  128. ASSERT(PcdGet64(PcdFlashNvStorageFtwWorkingBase64) + PcdGet32(PcdFlashNvStorageFtwWorkingSize) == PcdGet64(PcdFlashNvStorageFtwSpareBase64));
  129. // Check if the size of the area is at least one block size
  130. ASSERT((PcdGet32(PcdFlashNvStorageVariableSize) > 0) && ((UINT32)PcdGet32(PcdFlashNvStorageVariableSize) / Instance->Media.BlockSize > 0));
  131. ASSERT((PcdGet32(PcdFlashNvStorageFtwWorkingSize) > 0) && ((UINT32)PcdGet32(PcdFlashNvStorageFtwWorkingSize) / Instance->Media.BlockSize > 0));
  132. ASSERT((PcdGet32(PcdFlashNvStorageFtwSpareSize) > 0) && ((UINT32)PcdGet32(PcdFlashNvStorageFtwSpareSize) / Instance->Media.BlockSize > 0));
  133. // Ensure the Variable area Base Addresses are aligned on a block size boundaries
  134. ASSERT((UINT32)PcdGet64(PcdFlashNvStorageVariableBase64) % Instance->Media.BlockSize == 0);
  135. ASSERT((UINT32)PcdGet64(PcdFlashNvStorageFtwWorkingBase64) % Instance->Media.BlockSize == 0);
  136. ASSERT((UINT32)PcdGet64(PcdFlashNvStorageFtwSpareBase64) % Instance->Media.BlockSize == 0);
  137. //
  138. // EFI_FIRMWARE_VOLUME_HEADER
  139. //
  140. FirmwareVolumeHeader = (EFI_FIRMWARE_VOLUME_HEADER*)Headers;
  141. CopyGuid (&FirmwareVolumeHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid);
  142. FirmwareVolumeHeader->FvLength =
  143. PcdGet32(PcdFlashNvStorageVariableSize) +
  144. PcdGet32(PcdFlashNvStorageFtwWorkingSize) +
  145. PcdGet32(PcdFlashNvStorageFtwSpareSize);
  146. FirmwareVolumeHeader->Signature = EFI_FVH_SIGNATURE;
  147. FirmwareVolumeHeader->Attributes = (EFI_FVB_ATTRIBUTES_2) (
  148. EFI_FVB2_READ_ENABLED_CAP | // Reads may be enabled
  149. EFI_FVB2_READ_STATUS | // Reads are currently enabled
  150. EFI_FVB2_STICKY_WRITE | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
  151. EFI_FVB2_MEMORY_MAPPED | // It is memory mapped
  152. EFI_FVB2_ERASE_POLARITY | // After erasure all bits take this value (i.e. '1')
  153. EFI_FVB2_WRITE_STATUS | // Writes are currently enabled
  154. EFI_FVB2_WRITE_ENABLED_CAP // Writes may be enabled
  155. );
  156. FirmwareVolumeHeader->HeaderLength = sizeof(EFI_FIRMWARE_VOLUME_HEADER) + sizeof(EFI_FV_BLOCK_MAP_ENTRY);
  157. FirmwareVolumeHeader->Revision = EFI_FVH_REVISION;
  158. FirmwareVolumeHeader->BlockMap[0].NumBlocks = Instance->Media.LastBlock + 1;
  159. FirmwareVolumeHeader->BlockMap[0].Length = Instance->Media.BlockSize;
  160. FirmwareVolumeHeader->BlockMap[1].NumBlocks = 0;
  161. FirmwareVolumeHeader->BlockMap[1].Length = 0;
  162. FirmwareVolumeHeader->Checksum = CalculateCheckSum16 ((UINT16*)FirmwareVolumeHeader, FirmwareVolumeHeader->HeaderLength);
  163. //
  164. // VARIABLE_STORE_HEADER
  165. //
  166. VariableStoreHeader = (VARIABLE_STORE_HEADER*)((UINTN)Headers + (UINTN)FirmwareVolumeHeader->HeaderLength);
  167. CopyGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid);
  168. VariableStoreHeader->Size = PcdGet32(PcdFlashNvStorageVariableSize) - FirmwareVolumeHeader->HeaderLength;
  169. VariableStoreHeader->Format = VARIABLE_STORE_FORMATTED;
  170. VariableStoreHeader->State = VARIABLE_STORE_HEALTHY;
  171. // Install the combined super-header in the NorFlash
  172. Status = FvbWrite (&Instance->FvbProtocol, 0, 0, &HeadersLength, Headers);
  173. FreePool (Headers);
  174. return Status;
  175. }
  176. /**
  177. Check the integrity of firmware volume header.
  178. @param[in] FwVolHeader - A pointer to a firmware volume header
  179. @retval EFI_SUCCESS - The firmware volume is consistent
  180. @retval EFI_NOT_FOUND - The firmware volume has been corrupted.
  181. **/
  182. EFI_STATUS
  183. ValidateFvHeader (
  184. IN FLASH_INSTANCE* Instance
  185. )
  186. {
  187. UINT16 Checksum;
  188. EFI_FIRMWARE_VOLUME_HEADER* FwVolHeader;
  189. VARIABLE_STORE_HEADER* VariableStoreHeader;
  190. UINTN VariableStoreLength;
  191. UINTN FvLength;
  192. FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER*)Instance->RegionBaseAddress;
  193. FvLength = PcdGet32(PcdFlashNvStorageVariableSize) + PcdGet32(PcdFlashNvStorageFtwWorkingSize) +
  194. PcdGet32(PcdFlashNvStorageFtwSpareSize);
  195. //
  196. // Verify the header revision, header signature, length
  197. // Length of FvBlock cannot be 2**64-1
  198. // HeaderLength cannot be an odd number
  199. //
  200. if ( (FwVolHeader->Revision != EFI_FVH_REVISION)
  201. || (FwVolHeader->Signature != EFI_FVH_SIGNATURE)
  202. || (FwVolHeader->FvLength != FvLength)
  203. )
  204. {
  205. DEBUG ((EFI_D_ERROR, "ValidateFvHeader: No Firmware Volume header present\n"));
  206. return EFI_NOT_FOUND;
  207. }
  208. // Check the Firmware Volume Guid
  209. if ( CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid) == FALSE )
  210. {
  211. DEBUG ((EFI_D_ERROR, "ValidateFvHeader: Firmware Volume Guid non-compatible\n"));
  212. return EFI_NOT_FOUND;
  213. }
  214. // Verify the header checksum
  215. Checksum = CalculateSum16((UINT16*)FwVolHeader, FwVolHeader->HeaderLength);
  216. if (Checksum != 0)
  217. {
  218. DEBUG ((EFI_D_ERROR, "ValidateFvHeader: FV checksum is invalid (Checksum:0x%X)\n", Checksum));
  219. return EFI_NOT_FOUND;
  220. }
  221. VariableStoreHeader = (VARIABLE_STORE_HEADER*)((UINTN)FwVolHeader + (UINTN)FwVolHeader->HeaderLength);
  222. // Check the Variable Store Guid
  223. if ( CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) == FALSE )
  224. {
  225. DEBUG ((EFI_D_ERROR, "ValidateFvHeader: Variable Store Guid non-compatible\n"));
  226. return EFI_NOT_FOUND;
  227. }
  228. VariableStoreLength = PcdGet32 (PcdFlashNvStorageVariableSize) - FwVolHeader->HeaderLength;
  229. if (VariableStoreHeader->Size != VariableStoreLength)
  230. {
  231. DEBUG ((EFI_D_ERROR, "ValidateFvHeader: Variable Store Length does not match\n"));
  232. return EFI_NOT_FOUND;
  233. }
  234. return EFI_SUCCESS;
  235. }
  236. /**
  237. The FvbGetAttributes() function retrieves the attributes and
  238. current settings of the block.
  239. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  240. @param Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the attributes and
  241. current settings are returned.
  242. Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
  243. @retval EFI_SUCCESS The firmware volume attributes were returned.
  244. **/
  245. EFI_STATUS
  246. EFIAPI
  247. FvbGetAttributes(
  248. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL* This,
  249. OUT EFI_FVB_ATTRIBUTES_2* Attributes
  250. )
  251. {
  252. EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes;
  253. FLASH_INSTANCE* Instance;
  254. Instance = INSTANCE_FROM_FVB_THIS(This);
  255. FlashFvbAttributes = (EFI_FVB_ATTRIBUTES_2) (
  256. EFI_FVB2_READ_ENABLED_CAP | // Reads may be enabled
  257. EFI_FVB2_READ_STATUS | // Reads are currently enabled
  258. EFI_FVB2_STICKY_WRITE | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
  259. EFI_FVB2_MEMORY_MAPPED | // It is memory mapped
  260. EFI_FVB2_ERASE_POLARITY // After erasure all bits take this value (i.e. '1')
  261. );
  262. // Check if it is write protected
  263. if (Instance->Media.ReadOnly != TRUE)
  264. {
  265. FlashFvbAttributes = FlashFvbAttributes |
  266. EFI_FVB2_WRITE_STATUS | // Writes are currently enabled
  267. EFI_FVB2_WRITE_ENABLED_CAP; // Writes may be enabled
  268. }
  269. *Attributes = FlashFvbAttributes;
  270. return EFI_SUCCESS;
  271. }
  272. /**
  273. The FvbSetAttributes() function sets configurable firmware volume attributes
  274. and returns the new settings of the firmware volume.
  275. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  276. @param Attributes On input, Attributes is a pointer to EFI_FVB_ATTRIBUTES_2
  277. that contains the desired firmware volume settings.
  278. On successful return, it contains the new settings of
  279. the firmware volume.
  280. Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
  281. @retval EFI_SUCCESS The firmware volume attributes were returned.
  282. @retval EFI_INVALID_PARAMETER The attributes requested are in conflict with the capabilities
  283. as declared in the firmware volume header.
  284. **/
  285. EFI_STATUS
  286. EFIAPI
  287. FvbSetAttributes(
  288. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL* This,
  289. IN OUT EFI_FVB_ATTRIBUTES_2* Attributes
  290. )
  291. {
  292. DEBUG ((EFI_D_ERROR, "FvbSetAttributes(0x%X) is not supported\n", *Attributes));
  293. return EFI_UNSUPPORTED;
  294. }
  295. /**
  296. The GetPhysicalAddress() function retrieves the base address of
  297. a memory-mapped firmware volume. This function should be called
  298. only for memory-mapped firmware volumes.
  299. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  300. @param Address Pointer to a caller-allocated
  301. EFI_PHYSICAL_ADDRESS that, on successful
  302. return from GetPhysicalAddress(), contains the
  303. base address of the firmware volume.
  304. @retval EFI_SUCCESS The firmware volume base address was returned.
  305. @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.
  306. **/
  307. EFI_STATUS
  308. EFIAPI
  309. FvbGetPhysicalAddress (
  310. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL* This,
  311. OUT EFI_PHYSICAL_ADDRESS* Address
  312. )
  313. {
  314. if(NULL == Address)
  315. {
  316. return EFI_UNSUPPORTED;
  317. };
  318. *Address = mFlashNvStorageVariableBase;
  319. return EFI_SUCCESS;
  320. }
  321. /**
  322. The GetBlockSize() function retrieves the size of the requested
  323. block. It also returns the number of additional blocks with
  324. the identical size. The GetBlockSize() function is used to
  325. retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).
  326. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  327. @param Lba Indicates the block for which to return the size.
  328. @param BlockSize Pointer to a caller-allocated UINTN in which
  329. the size of the block is returned.
  330. @param NumberOfBlocks Pointer to a caller-allocated UINTN in
  331. which the number of consecutive blocks,
  332. starting with Lba, is returned. All
  333. blocks in this range have a size of
  334. BlockSize.
  335. @retval EFI_SUCCESS The firmware volume base address was returned.
  336. @retval EFI_INVALID_PARAMETER The requested LBA is out of range.
  337. **/
  338. EFI_STATUS
  339. EFIAPI
  340. FvbGetBlockSize (
  341. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL* This,
  342. IN EFI_LBA Lba,
  343. OUT UINTN* BlockSize,
  344. OUT UINTN* NumberOfBlocks
  345. )
  346. {
  347. EFI_STATUS Status;
  348. FLASH_INSTANCE* Instance;
  349. Instance = INSTANCE_FROM_FVB_THIS(This);
  350. if (Lba > Instance->Media.LastBlock)
  351. {
  352. Status = EFI_INVALID_PARAMETER;
  353. }
  354. else
  355. {
  356. // This is easy because in this platform each NorFlash device has equal sized blocks.
  357. *BlockSize = (UINTN) Instance->Media.BlockSize;
  358. *NumberOfBlocks = (UINTN) (Instance->Media.LastBlock - Lba + 1);
  359. Status = EFI_SUCCESS;
  360. }
  361. return Status;
  362. }
  363. STATIC
  364. EFI_STATUS
  365. EFIAPI
  366. FvbReset(
  367. IN EFI_BLOCK_IO_PROTOCOL *This,
  368. IN BOOLEAN ExtendedVerification
  369. )
  370. {
  371. return EFI_SUCCESS;
  372. }
  373. /**
  374. Reads the specified number of bytes into a buffer from the specified block.
  375. The Read() function reads the requested number of bytes from the
  376. requested block and stores them in the provided buffer.
  377. Implementations should be mindful that the firmware volume
  378. might be in the ReadDisabled state. If it is in this state,
  379. the Read() function must return the status code
  380. EFI_ACCESS_DENIED without modifying the contents of the
  381. buffer. The Read() function must also prevent spanning block
  382. boundaries. If a read is requested that would span a block
  383. boundary, the read must read up to the boundary but not
  384. beyond. The output parameter NumBytes must be set to correctly
  385. indicate the number of bytes actually read. The caller must be
  386. aware that a read may be partially completed.
  387. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  388. @param Lba The starting logical block index from which to read.
  389. @param Offset Offset into the block at which to begin reading.
  390. @param NumBytes Pointer to a UINTN.
  391. At entry, *NumBytes contains the total size of the buffer.
  392. At exit, *NumBytes contains the total number of bytes read.
  393. @param Buffer Pointer to a caller-allocated buffer that will be used
  394. to hold the data that is read.
  395. @retval EFI_SUCCESS The firmware volume was read successfully, and contents are
  396. in Buffer.
  397. @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.
  398. On output, NumBytes contains the total number of bytes
  399. returned in Buffer.
  400. @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state.
  401. @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be read.
  402. **/
  403. EFI_STATUS
  404. EFIAPI
  405. FvbRead (
  406. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL* This,
  407. IN EFI_LBA Lba,
  408. IN UINTN Offset,
  409. IN OUT UINTN* NumBytes,
  410. IN OUT UINT8* Buffer
  411. )
  412. {
  413. EFI_STATUS Status;
  414. UINTN BlockSize;
  415. FLASH_INSTANCE* Instance;
  416. UINTN StartAddress;
  417. UINTN ReadAddress;
  418. Instance = INSTANCE_FROM_FVB_THIS(This);
  419. if (!Instance->Initialized && Instance->Initialize)
  420. {
  421. if (EfiAtRuntime ()) {
  422. DEBUG ((EFI_D_ERROR, "[%a]:[%dL] Initialize at runtime is not supported!\n", __FUNCTION__, __LINE__));
  423. return EFI_UNSUPPORTED;
  424. }
  425. Instance->Initialize(Instance);
  426. }
  427. Status = EFI_SUCCESS;
  428. // Cache the block size to avoid de-referencing pointers all the time
  429. BlockSize = Instance->Media.BlockSize;
  430. // The read must not span block boundaries.
  431. // We need to check each variable individually because adding two large values together overflows.
  432. if ((Offset >= BlockSize) ||
  433. (*NumBytes > BlockSize) ||
  434. ((Offset + *NumBytes) > BlockSize))
  435. {
  436. DEBUG ((EFI_D_ERROR, "[%a]:[%dL] ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", __FUNCTION__, __LINE__, Offset, *NumBytes, BlockSize ));
  437. return EFI_BAD_BUFFER_SIZE;
  438. }
  439. // We must have some bytes to read
  440. if (*NumBytes == 0)
  441. {
  442. return EFI_BAD_BUFFER_SIZE;
  443. }
  444. // Get the address to start reading from
  445. StartAddress = GET_BLOCK_ADDRESS (Instance->RegionBaseAddress,
  446. Lba,
  447. BlockSize
  448. );
  449. ReadAddress = StartAddress - Instance->DeviceBaseAddress + Offset;
  450. Status = mFlash->Read(mFlash, (UINT32)ReadAddress, Buffer, *NumBytes);
  451. if (EFI_SUCCESS != Status)
  452. {
  453. // Return one of the pre-approved error statuses
  454. Status = EFI_DEVICE_ERROR;
  455. return Status;
  456. }
  457. return Status;
  458. }
  459. /**
  460. Writes the specified number of bytes from the input buffer to the block.
  461. The Write() function writes the specified number of bytes from
  462. the provided buffer to the specified block and offset. If the
  463. firmware volume is sticky write, the caller must ensure that
  464. all the bits of the specified range to write are in the
  465. EFI_FVB_ERASE_POLARITY state before calling the Write()
  466. function, or else the result will be unpredictable. This
  467. unpredictability arises because, for a sticky-write firmware
  468. volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY
  469. state but cannot flip it back again. Before calling the
  470. Write() function, it is recommended for the caller to first call
  471. the EraseBlocks() function to erase the specified block to
  472. write. A block erase cycle will transition bits from the
  473. (NOT)EFI_FVB_ERASE_POLARITY state back to the
  474. EFI_FVB_ERASE_POLARITY state. Implementations should be
  475. mindful that the firmware volume might be in the WriteDisabled
  476. state. If it is in this state, the Write() function must
  477. return the status code EFI_ACCESS_DENIED without modifying the
  478. contents of the firmware volume. The Write() function must
  479. also prevent spanning block boundaries. If a write is
  480. requested that spans a block boundary, the write must store up
  481. to the boundary but not beyond. The output parameter NumBytes
  482. must be set to correctly indicate the number of bytes actually
  483. written. The caller must be aware that a write may be
  484. partially completed. All writes, partial or otherwise, must be
  485. fully flushed to the hardware before the Write() service
  486. returns.
  487. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
  488. @param Lba The starting logical block index to write to.
  489. @param Offset Offset into the block at which to begin writing.
  490. @param NumBytes The pointer to a UINTN.
  491. At entry, *NumBytes contains the total size of the buffer.
  492. At exit, *NumBytes contains the total number of bytes actually written.
  493. @param Buffer The pointer to a caller-allocated buffer that contains the source for the write.
  494. @retval EFI_SUCCESS The firmware volume was written successfully.
  495. @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
  496. On output, NumBytes contains the total number of bytes
  497. actually written.
  498. @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
  499. @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not be written.
  500. **/
  501. EFI_STATUS
  502. EFIAPI
  503. FvbWrite (
  504. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL* This,
  505. IN EFI_LBA Lba,
  506. IN UINTN Offset,
  507. IN OUT UINTN* NumBytes,
  508. IN UINT8* Buffer
  509. )
  510. {
  511. EFI_STATUS Status;
  512. UINTN BlockSize;
  513. FLASH_INSTANCE* Instance;
  514. UINTN BlockAddress;
  515. UINTN WriteAddress;
  516. Instance = INSTANCE_FROM_FVB_THIS(This);
  517. if (NULL == Instance)
  518. {
  519. return EFI_INVALID_PARAMETER;
  520. }
  521. if (!Instance->Initialized && Instance->Initialize)
  522. {
  523. if (EfiAtRuntime ()) {
  524. DEBUG ((EFI_D_ERROR, "[%a]:[%dL] Initialize at runtime is not supported!\n", __FUNCTION__, __LINE__));
  525. return EFI_UNSUPPORTED;
  526. }
  527. Instance->Initialize(Instance);
  528. }
  529. Status = EFI_SUCCESS;
  530. // Detect WriteDisabled state
  531. if (Instance->Media.ReadOnly == TRUE)
  532. {
  533. DEBUG ((EFI_D_ERROR, "FvbWrite: ERROR - Can not write: Device is in WriteDisabled state.\n"));
  534. // It is in WriteDisabled state, return an error right away
  535. return EFI_ACCESS_DENIED;
  536. }
  537. // Cache the block size to avoid de-referencing pointers all the time
  538. BlockSize = Instance->Media.BlockSize;
  539. // The write must not span block boundaries.
  540. // We need to check each variable individually because adding two large values together overflows.
  541. if ( ( Offset >= BlockSize ) ||
  542. ( *NumBytes > BlockSize ) ||
  543. ( (Offset + *NumBytes) > BlockSize ) )
  544. {
  545. DEBUG ((EFI_D_ERROR, "FvbWrite: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize ));
  546. return EFI_BAD_BUFFER_SIZE;
  547. }
  548. // We must have some bytes to write
  549. if (*NumBytes == 0)
  550. {
  551. DEBUG ((EFI_D_ERROR, "FvbWrite: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize ));
  552. return EFI_BAD_BUFFER_SIZE;
  553. }
  554. BlockAddress = GET_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, BlockSize);
  555. WriteAddress = BlockAddress - Instance->DeviceBaseAddress + Offset;
  556. Status = mFlash->Write(mFlash, (UINT32)WriteAddress, (UINT8*)Buffer, *NumBytes);
  557. if (EFI_SUCCESS != Status)
  558. {
  559. DEBUG((EFI_D_ERROR, "%s - %d Status=%r\n", __FILE__, __LINE__, Status));
  560. return Status;
  561. }
  562. return Status;
  563. }
  564. /**
  565. Erases and initialises a firmware volume block.
  566. The EraseBlocks() function erases one or more blocks as denoted
  567. by the variable argument list. The entire parameter list of
  568. blocks must be verified before erasing any blocks. If a block is
  569. requested that does not exist within the associated firmware
  570. volume (it has a larger index than the last block of the
  571. firmware volume), the EraseBlocks() function must return the
  572. status code EFI_INVALID_PARAMETER without modifying the contents
  573. of the firmware volume. Implementations should be mindful that
  574. the firmware volume might be in the WriteDisabled state. If it
  575. is in this state, the EraseBlocks() function must return the
  576. status code EFI_ACCESS_DENIED without modifying the contents of
  577. the firmware volume. All calls to EraseBlocks() must be fully
  578. flushed to the hardware before the EraseBlocks() service
  579. returns.
  580. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
  581. instance.
  582. @param ... The variable argument list is a list of tuples.
  583. Each tuple describes a range of LBAs to erase
  584. and consists of the following:
  585. - An EFI_LBA that indicates the starting LBA
  586. - A UINTN that indicates the number of blocks to erase.
  587. The list is terminated with an EFI_LBA_LIST_TERMINATOR.
  588. For example, the following indicates that two ranges of blocks
  589. (5-7 and 10-11) are to be erased:
  590. EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);
  591. @retval EFI_SUCCESS The erase request successfully completed.
  592. @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
  593. @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be written.
  594. The firmware device may have been partially erased.
  595. @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable argument list do
  596. not exist in the firmware volume.
  597. **/
  598. EFI_STATUS
  599. EFIAPI
  600. FvbEraseBlocks (
  601. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL* This,
  602. ...
  603. )
  604. {
  605. EFI_STATUS Status;
  606. VA_LIST Args;
  607. UINTN BlockAddress; // Physical address of Lba to erase
  608. EFI_LBA StartingLba; // Lba from which we start erasing
  609. UINTN NumOfLba; // Number of Lba blocks to erase
  610. FLASH_INSTANCE* Instance;
  611. Instance = INSTANCE_FROM_FVB_THIS(This);
  612. Status = EFI_SUCCESS;
  613. // Detect WriteDisabled state
  614. if (Instance->Media.ReadOnly == TRUE)
  615. {
  616. // Firmware volume is in WriteDisabled state
  617. return EFI_ACCESS_DENIED;
  618. }
  619. // Before erasing, check the entire list of parameters to ensure all specified blocks are valid
  620. VA_START (Args, This);
  621. do
  622. {
  623. // Get the Lba from which we start erasing
  624. StartingLba = VA_ARG (Args, EFI_LBA);
  625. // Have we reached the end of the list?
  626. if (StartingLba == EFI_LBA_LIST_TERMINATOR)
  627. {
  628. //Exit the while loop
  629. break;
  630. }
  631. // How many Lba blocks are we requested to erase?
  632. NumOfLba = VA_ARG (Args, UINT32);
  633. // All blocks must be within range
  634. if ((NumOfLba == 0) || ((Instance->StartLba + StartingLba + NumOfLba - 1) > Instance->Media.LastBlock))
  635. {
  636. VA_END (Args);
  637. Status = EFI_INVALID_PARAMETER;
  638. goto EXIT;
  639. }
  640. }
  641. while (TRUE);
  642. VA_END (Args);
  643. //
  644. // To get here, all must be ok, so start erasing
  645. //
  646. VA_START (Args, This);
  647. do
  648. {
  649. // Get the Lba from which we start erasing
  650. StartingLba = VA_ARG (Args, EFI_LBA);
  651. // Have we reached the end of the list?
  652. if (StartingLba == EFI_LBA_LIST_TERMINATOR)
  653. {
  654. // Exit the while loop
  655. break;
  656. }
  657. // How many Lba blocks are we requested to erase?
  658. NumOfLba = VA_ARG (Args, UINT32);
  659. // Go through each one and erase it
  660. while (NumOfLba > 0)
  661. {
  662. // Get the physical address of Lba to erase
  663. BlockAddress = GET_BLOCK_ADDRESS (
  664. Instance->RegionBaseAddress,
  665. Instance->StartLba + StartingLba,
  666. Instance->Media.BlockSize
  667. );
  668. // Erase it
  669. Status = FlashUnlockAndEraseSingleBlock (Instance, BlockAddress);
  670. if (EFI_ERROR(Status))
  671. {
  672. VA_END (Args);
  673. Status = EFI_DEVICE_ERROR;
  674. goto EXIT;
  675. }
  676. // Move to the next Lba
  677. StartingLba++;
  678. NumOfLba--;
  679. }
  680. }
  681. while (TRUE);
  682. VA_END (Args);
  683. EXIT:
  684. return Status;
  685. }
  686. EFI_STATUS
  687. EFIAPI
  688. FvbInitialize (
  689. IN FLASH_INSTANCE* Instance
  690. )
  691. {
  692. EFI_STATUS Status;
  693. UINT32 FvbNumLba;
  694. Instance->Initialized = TRUE;
  695. mFlashNvStorageVariableBase = FixedPcdGet64 (PcdFlashNvStorageVariableBase64);
  696. // Set the index of the first LBA for the FVB
  697. Instance->StartLba = (PcdGet64 (PcdFlashNvStorageVariableBase64) - Instance->RegionBaseAddress) / Instance->Media.BlockSize;
  698. // Determine if there is a valid header at the beginning of the Flash
  699. Status = ValidateFvHeader (Instance);
  700. if (EFI_ERROR(Status))
  701. {
  702. // There is no valid header, so time to install one.
  703. // Erase all the Flash that is reserved for variable storage
  704. FvbNumLba = (PcdGet32(PcdFlashNvStorageVariableSize) + PcdGet32(PcdFlashNvStorageFtwWorkingSize) + (UINT32)PcdGet32(PcdFlashNvStorageFtwSpareSize)) / Instance->Media.BlockSize;
  705. Status = FvbEraseBlocks (&Instance->FvbProtocol, (EFI_LBA)0, FvbNumLba, EFI_LBA_LIST_TERMINATOR);
  706. if (EFI_ERROR(Status))
  707. {
  708. return Status;
  709. }
  710. // Install all appropriate headers
  711. Status = InitializeFvAndVariableStoreHeaders (Instance);
  712. if (EFI_ERROR(Status))
  713. {
  714. return Status;
  715. }
  716. }
  717. return Status;
  718. }
  719. EFI_STATUS
  720. FlashPlatformGetDevices (
  721. OUT FLASH_DESCRIPTION** FlashDevices,
  722. OUT UINT32* Count
  723. )
  724. {
  725. if ((FlashDevices == NULL) || (Count == NULL))
  726. {
  727. return EFI_INVALID_PARAMETER;
  728. }
  729. *FlashDevices = mFlashDevices;
  730. *Count = FLASH_DEVICE_COUNT;
  731. return EFI_SUCCESS;
  732. }
  733. EFI_STATUS
  734. FlashCreateInstance (
  735. IN UINTN FlashDeviceBase,
  736. IN UINTN FlashRegionBase,
  737. IN UINTN FlashSize,
  738. IN UINT32 MediaId,
  739. IN UINT32 BlockSize,
  740. IN BOOLEAN SupportFvb,
  741. IN CONST GUID* FlashGuid,
  742. OUT FLASH_INSTANCE** FlashInstance
  743. )
  744. {
  745. EFI_STATUS Status;
  746. FLASH_INSTANCE* Instance;
  747. if (FlashInstance == NULL)
  748. {
  749. return EFI_INVALID_PARAMETER;
  750. }
  751. Instance = AllocateRuntimeCopyPool (sizeof(FLASH_INSTANCE), &mFlashInstanceTemplate);
  752. if (Instance == NULL)
  753. {
  754. return EFI_INVALID_PARAMETER;
  755. }
  756. Instance->DeviceBaseAddress = FlashDeviceBase;
  757. Instance->RegionBaseAddress = FlashRegionBase;
  758. Instance->Size = FlashSize;
  759. Instance->BlockIoProtocol.Media = &Instance->Media;
  760. Instance->BlockIoProtocol.Reset = FvbReset;
  761. Instance->Media.MediaId = MediaId;
  762. Instance->Media.BlockSize = BlockSize;
  763. Instance->Media.LastBlock = (FlashSize / BlockSize) - 1;
  764. CopyGuid (&Instance->DevicePath.Vendor.Guid, FlashGuid);
  765. if (SupportFvb)
  766. {
  767. Instance->SupportFvb = TRUE;
  768. Instance->Initialize = FvbInitialize;
  769. Status = gBS->InstallMultipleProtocolInterfaces (
  770. &Instance->Handle,
  771. &gEfiDevicePathProtocolGuid, &Instance->DevicePath,
  772. &gEfiBlockIoProtocolGuid, &Instance->BlockIoProtocol,
  773. &gEfiFirmwareVolumeBlockProtocolGuid, &Instance->FvbProtocol,
  774. NULL
  775. );
  776. if (EFI_ERROR(Status))
  777. {
  778. FreePool(Instance);
  779. return Status;
  780. }
  781. }
  782. else
  783. {
  784. Instance->Initialized = TRUE;
  785. Status = gBS->InstallMultipleProtocolInterfaces (
  786. &Instance->Handle,
  787. &gEfiDevicePathProtocolGuid, &Instance->DevicePath,
  788. &gEfiBlockIoProtocolGuid, &Instance->BlockIoProtocol,
  789. NULL
  790. );
  791. if (EFI_ERROR(Status))
  792. {
  793. FreePool(Instance);
  794. return Status;
  795. }
  796. }
  797. *FlashInstance = Instance;
  798. return Status;
  799. }
  800. EFI_STATUS
  801. FlashUnlockSingleBlockIfNecessary (
  802. IN FLASH_INSTANCE* Instance,
  803. IN UINTN BlockAddress
  804. )
  805. {
  806. return EFI_SUCCESS;
  807. }
  808. EFI_STATUS
  809. FlashEraseSingleBlock (
  810. IN FLASH_INSTANCE* Instance,
  811. IN UINTN BlockAddress
  812. )
  813. {
  814. EFI_STATUS Status;
  815. UINTN EraseAddress;
  816. Status = EFI_SUCCESS;
  817. EraseAddress = BlockAddress - Instance->DeviceBaseAddress;
  818. Status = mFlash->Erase(mFlash, (UINT32)EraseAddress, Instance->Media.BlockSize);
  819. if (EFI_SUCCESS != Status)
  820. {
  821. DEBUG((EFI_D_ERROR, "%s - %d Status=%r\n", __FILE__, __LINE__, Status));
  822. return Status;
  823. }
  824. return EFI_SUCCESS;
  825. }
  826. /**
  827. * The following function presumes that the block has already been unlocked.
  828. **/
  829. EFI_STATUS
  830. FlashUnlockAndEraseSingleBlock (
  831. IN FLASH_INSTANCE* Instance,
  832. IN UINTN BlockAddress
  833. )
  834. {
  835. EFI_STATUS Status;
  836. UINTN Index;
  837. Index = 0;
  838. // The block erase might fail a first time (SW bug ?). Retry it ...
  839. do
  840. {
  841. // Unlock the block if we have to
  842. Status = FlashUnlockSingleBlockIfNecessary (Instance, BlockAddress);
  843. if (!EFI_ERROR(Status))
  844. {
  845. Status = FlashEraseSingleBlock (Instance, BlockAddress);
  846. }
  847. Index++;
  848. }
  849. while ((Index < FLASH_ERASE_RETRY) && (Status == EFI_WRITE_PROTECTED));
  850. if (Index == FLASH_ERASE_RETRY)
  851. {
  852. DEBUG((EFI_D_ERROR, "EraseSingleBlock(BlockAddress=0x%08x: Block Locked Error (try to erase %d times)\n", BlockAddress, Index));
  853. }
  854. return Status;
  855. }
  856. EFI_STATUS
  857. FlashWriteBlocks (
  858. IN FLASH_INSTANCE* Instance,
  859. IN EFI_LBA Lba,
  860. IN UINTN BufferSizeInBytes,
  861. IN VOID* Buffer
  862. )
  863. {
  864. EFI_STATUS Status = EFI_SUCCESS;
  865. UINTN BlockAddress;
  866. UINT32 NumBlocks;
  867. UINTN WriteAddress;
  868. // The buffer must be valid
  869. if (Buffer == NULL)
  870. {
  871. return EFI_INVALID_PARAMETER;
  872. }
  873. if (Instance->Media.ReadOnly == TRUE)
  874. {
  875. return EFI_WRITE_PROTECTED;
  876. }
  877. // We must have some bytes to read
  878. if (BufferSizeInBytes == 0)
  879. {
  880. return EFI_BAD_BUFFER_SIZE;
  881. }
  882. // The size of the buffer must be a multiple of the block size
  883. if ((BufferSizeInBytes % Instance->Media.BlockSize) != 0)
  884. {
  885. return EFI_BAD_BUFFER_SIZE;
  886. }
  887. // All blocks must be within the device
  888. NumBlocks = ((UINT32)BufferSizeInBytes) / Instance->Media.BlockSize ;
  889. if ((Lba + NumBlocks) > (Instance->Media.LastBlock + 1))
  890. {
  891. DEBUG((EFI_D_ERROR, "[%a]:[%dL]ERROR - Write will exceed last block.\n", __FUNCTION__, __LINE__ ));
  892. return EFI_INVALID_PARAMETER;
  893. }
  894. BlockAddress = GET_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, Instance->Media.BlockSize);
  895. WriteAddress = BlockAddress - Instance->DeviceBaseAddress;
  896. Status = mFlash->Write(mFlash, (UINT32)WriteAddress, (UINT8*)Buffer, BufferSizeInBytes);
  897. if (EFI_SUCCESS != Status)
  898. {
  899. DEBUG((EFI_D_ERROR, "%s - %d Status=%r\n", __FILE__, __LINE__, Status));
  900. return Status;
  901. }
  902. return Status;
  903. }
  904. EFI_STATUS
  905. FlashReadBlocks (
  906. IN FLASH_INSTANCE* Instance,
  907. IN EFI_LBA Lba,
  908. IN UINTN BufferSizeInBytes,
  909. OUT VOID* Buffer
  910. )
  911. {
  912. UINT32 NumBlocks;
  913. UINTN StartAddress;
  914. UINTN ReadAddress;
  915. EFI_STATUS Status;
  916. // The buffer must be valid
  917. if (Buffer == NULL)
  918. {
  919. return EFI_INVALID_PARAMETER;
  920. }
  921. // We must have some bytes to read
  922. if (BufferSizeInBytes == 0)
  923. {
  924. return EFI_BAD_BUFFER_SIZE;
  925. }
  926. // The size of the buffer must be a multiple of the block size
  927. if ((BufferSizeInBytes % Instance->Media.BlockSize) != 0)
  928. {
  929. return EFI_BAD_BUFFER_SIZE;
  930. }
  931. // All blocks must be within the device
  932. NumBlocks = ((UINT32)BufferSizeInBytes) / Instance->Media.BlockSize ;
  933. if ((Lba + NumBlocks) > (Instance->Media.LastBlock + 1))
  934. {
  935. DEBUG((EFI_D_ERROR, "FlashReadBlocks: ERROR - Read will exceed last block\n"));
  936. return EFI_INVALID_PARAMETER;
  937. }
  938. // Get the address to start reading from
  939. StartAddress = GET_BLOCK_ADDRESS (Instance->RegionBaseAddress,
  940. Lba,
  941. Instance->Media.BlockSize
  942. );
  943. ReadAddress = StartAddress - Instance->DeviceBaseAddress;
  944. Status = mFlash->Read(mFlash, (UINT32)ReadAddress, Buffer, BufferSizeInBytes);
  945. if (EFI_SUCCESS != Status)
  946. {
  947. DEBUG((EFI_D_ERROR, "%s - %d Status=%r\n", __FILE__, __LINE__, Status));
  948. return Status;
  949. }
  950. return EFI_SUCCESS;
  951. }
  952. VOID
  953. EFIAPI
  954. FlashFvbVirtualNotifyEvent (
  955. IN EFI_EVENT Event,
  956. IN VOID *Context
  957. )
  958. {
  959. EfiConvertPointer (0x0, (VOID**)&mFlash);
  960. EfiConvertPointer (0x0, (VOID**)&mFlashNvStorageVariableBase);
  961. return;
  962. }
  963. EFI_STATUS
  964. EFIAPI
  965. FlashFvbInitialize (
  966. IN EFI_HANDLE ImageHandle,
  967. IN EFI_SYSTEM_TABLE* SystemTable
  968. )
  969. {
  970. EFI_STATUS Status;
  971. UINT32 Index;
  972. FLASH_DESCRIPTION* FlashDevices;
  973. UINT32 FlashDeviceCount;
  974. BOOLEAN ContainVariableStorage;
  975. Status = FlashPlatformGetDevices (&FlashDevices, &FlashDeviceCount);
  976. if (EFI_ERROR(Status))
  977. {
  978. DEBUG((EFI_D_ERROR, "[%a]:[%dL] Fail to get Flash devices\n", __FUNCTION__, __LINE__));
  979. return Status;
  980. }
  981. mFlashInstances = AllocatePool ((UINT32)(sizeof(FLASH_INSTANCE*) * FlashDeviceCount));
  982. Status = gBS->LocateProtocol (&gHisiSpiFlashProtocolGuid, NULL, (VOID*) &mFlash);
  983. if (EFI_ERROR(Status))
  984. {
  985. DEBUG((EFI_D_ERROR, "[%a]:[%dL] Status=%r\n", __FUNCTION__, __LINE__, Status));
  986. return Status;
  987. }
  988. for (Index = 0; Index < FlashDeviceCount; Index++)
  989. {
  990. // Check if this Flash device contain the variable storage region
  991. ContainVariableStorage =
  992. (FlashDevices[Index].RegionBaseAddress <= PcdGet64 (PcdFlashNvStorageVariableBase64)) &&
  993. ((PcdGet64 (PcdFlashNvStorageVariableBase64) + PcdGet32 (PcdFlashNvStorageVariableSize)) <= FlashDevices[Index].RegionBaseAddress + FlashDevices[Index].Size);
  994. Status = FlashCreateInstance (
  995. FlashDevices[Index].DeviceBaseAddress,
  996. FlashDevices[Index].RegionBaseAddress,
  997. FlashDevices[Index].Size,
  998. Index,
  999. FlashDevices[Index].BlockSize,
  1000. ContainVariableStorage,
  1001. &FlashDevices[Index].Guid,
  1002. &mFlashInstances[Index]
  1003. );
  1004. if (EFI_ERROR(Status))
  1005. {
  1006. DEBUG((EFI_D_ERROR, "[%a]:[%dL] Fail to create instance for Flash[%d]\n", __FUNCTION__, __LINE__, Index));
  1007. }
  1008. }
  1009. //
  1010. // Register for the virtual address change event
  1011. //
  1012. Status = gBS->CreateEventEx (
  1013. EVT_NOTIFY_SIGNAL,
  1014. TPL_NOTIFY,
  1015. FlashFvbVirtualNotifyEvent,
  1016. NULL,
  1017. &gEfiEventVirtualAddressChangeGuid,
  1018. &mFlashFvbVirtualAddrChangeEvent
  1019. );
  1020. ASSERT_EFI_ERROR (Status);
  1021. return Status;
  1022. }