NorFlashStandaloneMm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /** @file NorFlashStandaloneMm.c
  2. Copyright (c) 2011 - 2021, Arm Limited. All rights reserved.<BR>
  3. Copyright (c) 2020, Linaro, Ltd. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Library/BaseMemoryLib.h>
  7. #include <Library/MemoryAllocationLib.h>
  8. #include <Library/MmServicesTableLib.h>
  9. #include "NorFlash.h"
  10. //
  11. // Global variable declarations
  12. //
  13. NOR_FLASH_INSTANCE **mNorFlashInstances;
  14. UINT32 mNorFlashDeviceCount;
  15. UINTN mFlashNvStorageVariableBase;
  16. NOR_FLASH_INSTANCE mNorFlashInstanceTemplate = {
  17. NOR_FLASH_SIGNATURE, // Signature
  18. NULL, // Handle ... NEED TO BE FILLED
  19. 0, // DeviceBaseAddress ... NEED TO BE FILLED
  20. 0, // RegionBaseAddress ... NEED TO BE FILLED
  21. 0, // Size ... NEED TO BE FILLED
  22. 0, // StartLba
  23. {
  24. EFI_BLOCK_IO_PROTOCOL_REVISION2, // Revision
  25. NULL, // Media ... NEED TO BE FILLED
  26. NULL, // Reset;
  27. NULL, // ReadBlocks
  28. NULL, // WriteBlocks
  29. NULL // FlushBlocks
  30. }, // BlockIoProtocol
  31. {
  32. 0, // MediaId ... NEED TO BE FILLED
  33. FALSE, // RemovableMedia
  34. TRUE, // MediaPresent
  35. FALSE, // LogicalPartition
  36. FALSE, // ReadOnly
  37. FALSE, // WriteCaching;
  38. 0, // BlockSize ... NEED TO BE FILLED
  39. 4, // IoAlign
  40. 0, // LastBlock ... NEED TO BE FILLED
  41. 0, // LowestAlignedLba
  42. 1, // LogicalBlocksPerPhysicalBlock
  43. }, // Media;
  44. {
  45. EFI_DISK_IO_PROTOCOL_REVISION, // Revision
  46. NULL, // ReadDisk
  47. NULL // WriteDisk
  48. },
  49. {
  50. FvbGetAttributes, // GetAttributes
  51. FvbSetAttributes, // SetAttributes
  52. FvbGetPhysicalAddress, // GetPhysicalAddress
  53. FvbGetBlockSize, // GetBlockSize
  54. FvbRead, // Read
  55. FvbWrite, // Write
  56. FvbEraseBlocks, // EraseBlocks
  57. NULL, // ParentHandle
  58. }, // FvbProtoccol;
  59. NULL, // ShadowBuffer
  60. {
  61. {
  62. {
  63. HARDWARE_DEVICE_PATH,
  64. HW_VENDOR_DP,
  65. {
  66. (UINT8)(OFFSET_OF (NOR_FLASH_DEVICE_PATH, End)),
  67. (UINT8)(OFFSET_OF (NOR_FLASH_DEVICE_PATH, End) >> 8)
  68. }
  69. },
  70. { 0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }
  71. }, // GUID ... NEED TO BE FILLED
  72. },
  73. 0, // Index
  74. {
  75. END_DEVICE_PATH_TYPE,
  76. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  77. { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 }
  78. }
  79. } // DevicePath
  80. };
  81. EFI_STATUS
  82. NorFlashCreateInstance (
  83. IN UINTN NorFlashDeviceBase,
  84. IN UINTN NorFlashRegionBase,
  85. IN UINTN NorFlashSize,
  86. IN UINT32 Index,
  87. IN UINT32 BlockSize,
  88. IN BOOLEAN SupportFvb,
  89. OUT NOR_FLASH_INSTANCE **NorFlashInstance
  90. )
  91. {
  92. EFI_STATUS Status;
  93. NOR_FLASH_INSTANCE *Instance;
  94. ASSERT (NorFlashInstance != NULL);
  95. Instance = AllocateRuntimeCopyPool (sizeof (NOR_FLASH_INSTANCE), &mNorFlashInstanceTemplate);
  96. if (Instance == NULL) {
  97. return EFI_OUT_OF_RESOURCES;
  98. }
  99. Instance->DeviceBaseAddress = NorFlashDeviceBase;
  100. Instance->RegionBaseAddress = NorFlashRegionBase;
  101. Instance->Size = NorFlashSize;
  102. Instance->BlockIoProtocol.Media = &Instance->Media;
  103. Instance->Media.MediaId = Index;
  104. Instance->Media.BlockSize = BlockSize;
  105. Instance->Media.LastBlock = (NorFlashSize / BlockSize)-1;
  106. CopyGuid (&Instance->DevicePath.Vendor.Guid, &gEfiCallerIdGuid);
  107. Instance->DevicePath.Index = (UINT8)Index;
  108. Instance->ShadowBuffer = AllocateRuntimePool (BlockSize);
  109. if (Instance->ShadowBuffer == NULL) {
  110. return EFI_OUT_OF_RESOURCES;
  111. }
  112. if (SupportFvb) {
  113. NorFlashFvbInitialize (Instance);
  114. Status = gMmst->MmInstallProtocolInterface (
  115. &Instance->Handle,
  116. &gEfiSmmFirmwareVolumeBlockProtocolGuid,
  117. EFI_NATIVE_INTERFACE,
  118. &Instance->FvbProtocol
  119. );
  120. if (EFI_ERROR (Status)) {
  121. FreePool (Instance);
  122. return Status;
  123. }
  124. } else {
  125. DEBUG ((DEBUG_ERROR, "standalone MM NOR Flash driver only support FVB.\n"));
  126. FreePool (Instance);
  127. return EFI_UNSUPPORTED;
  128. }
  129. *NorFlashInstance = Instance;
  130. return Status;
  131. }
  132. /**
  133. * This function unlock and erase an entire NOR Flash block.
  134. **/
  135. EFI_STATUS
  136. NorFlashUnlockAndEraseSingleBlock (
  137. IN NOR_FLASH_INSTANCE *Instance,
  138. IN UINTN BlockAddress
  139. )
  140. {
  141. EFI_STATUS Status;
  142. UINTN Index;
  143. Index = 0;
  144. // The block erase might fail a first time (SW bug ?). Retry it ...
  145. do {
  146. // Unlock the block if we have to
  147. Status = NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddress);
  148. if (EFI_ERROR (Status)) {
  149. break;
  150. }
  151. Status = NorFlashEraseSingleBlock (Instance, BlockAddress);
  152. Index++;
  153. } while ((Index < NOR_FLASH_ERASE_RETRY) && (Status == EFI_WRITE_PROTECTED));
  154. if (Index == NOR_FLASH_ERASE_RETRY) {
  155. DEBUG ((DEBUG_ERROR, "EraseSingleBlock(BlockAddress=0x%08x: Block Locked Error (try to erase %d times)\n", BlockAddress, Index));
  156. }
  157. return Status;
  158. }
  159. EFI_STATUS
  160. NorFlashWriteFullBlock (
  161. IN NOR_FLASH_INSTANCE *Instance,
  162. IN EFI_LBA Lba,
  163. IN UINT32 *DataBuffer,
  164. IN UINT32 BlockSizeInWords
  165. )
  166. {
  167. EFI_STATUS Status;
  168. UINTN WordAddress;
  169. UINT32 WordIndex;
  170. UINTN BufferIndex;
  171. UINTN BlockAddress;
  172. UINTN BuffersInBlock;
  173. UINTN RemainingWords;
  174. UINTN Cnt;
  175. Status = EFI_SUCCESS;
  176. // Get the physical address of the block
  177. BlockAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, BlockSizeInWords * 4);
  178. // Start writing from the first address at the start of the block
  179. WordAddress = BlockAddress;
  180. Status = NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress);
  181. if (EFI_ERROR (Status)) {
  182. DEBUG ((DEBUG_ERROR, "WriteSingleBlock: ERROR - Failed to Unlock and Erase the single block at 0x%X\n", BlockAddress));
  183. goto EXIT;
  184. }
  185. // To speed up the programming operation, NOR Flash is programmed using the Buffered Programming method.
  186. // Check that the address starts at a 32-word boundary, i.e. last 7 bits must be zero
  187. if ((WordAddress & BOUNDARY_OF_32_WORDS) == 0x00) {
  188. // First, break the entire block into buffer-sized chunks.
  189. BuffersInBlock = (UINTN)(BlockSizeInWords * 4) / P30_MAX_BUFFER_SIZE_IN_BYTES;
  190. // Then feed each buffer chunk to the NOR Flash
  191. // If a buffer does not contain any data, don't write it.
  192. for (BufferIndex = 0;
  193. BufferIndex < BuffersInBlock;
  194. BufferIndex++, WordAddress += P30_MAX_BUFFER_SIZE_IN_BYTES, DataBuffer += P30_MAX_BUFFER_SIZE_IN_WORDS
  195. )
  196. {
  197. // Check the buffer to see if it contains any data (not set all 1s).
  198. for (Cnt = 0; Cnt < P30_MAX_BUFFER_SIZE_IN_WORDS; Cnt++) {
  199. if (~DataBuffer[Cnt] != 0 ) {
  200. // Some data found, write the buffer.
  201. Status = NorFlashWriteBuffer (
  202. Instance,
  203. WordAddress,
  204. P30_MAX_BUFFER_SIZE_IN_BYTES,
  205. DataBuffer
  206. );
  207. if (EFI_ERROR (Status)) {
  208. goto EXIT;
  209. }
  210. break;
  211. }
  212. }
  213. }
  214. // Finally, finish off any remaining words that are less than the maximum size of the buffer
  215. RemainingWords = BlockSizeInWords % P30_MAX_BUFFER_SIZE_IN_WORDS;
  216. if (RemainingWords != 0) {
  217. Status = NorFlashWriteBuffer (Instance, WordAddress, (RemainingWords * 4), DataBuffer);
  218. if (EFI_ERROR (Status)) {
  219. goto EXIT;
  220. }
  221. }
  222. } else {
  223. // For now, use the single word programming algorithm
  224. // It is unlikely that the NOR Flash will exist in an address which falls within a 32 word boundary range,
  225. // i.e. which ends in the range 0x......01 - 0x......7F.
  226. for (WordIndex = 0; WordIndex < BlockSizeInWords; WordIndex++, DataBuffer++, WordAddress = WordAddress + 4) {
  227. Status = NorFlashWriteSingleWord (Instance, WordAddress, *DataBuffer);
  228. if (EFI_ERROR (Status)) {
  229. goto EXIT;
  230. }
  231. }
  232. }
  233. EXIT:
  234. if (EFI_ERROR (Status)) {
  235. DEBUG ((DEBUG_ERROR, "NOR FLASH Programming [WriteSingleBlock] failed at address 0x%08x. Exit Status = \"%r\".\n", WordAddress, Status));
  236. }
  237. return Status;
  238. }
  239. EFI_STATUS
  240. EFIAPI
  241. NorFlashInitialise (
  242. IN EFI_HANDLE ImageHandle,
  243. IN EFI_MM_SYSTEM_TABLE *MmSystemTable
  244. )
  245. {
  246. EFI_STATUS Status;
  247. UINT32 Index;
  248. NOR_FLASH_DESCRIPTION *NorFlashDevices;
  249. BOOLEAN ContainVariableStorage;
  250. Status = NorFlashPlatformInitialization ();
  251. if (EFI_ERROR (Status)) {
  252. DEBUG ((DEBUG_ERROR, "NorFlashInitialise: Fail to initialize Nor Flash devices\n"));
  253. return Status;
  254. }
  255. Status = NorFlashPlatformGetDevices (&NorFlashDevices, &mNorFlashDeviceCount);
  256. if (EFI_ERROR (Status)) {
  257. DEBUG ((DEBUG_ERROR, "NorFlashInitialise: Fail to get Nor Flash devices\n"));
  258. return Status;
  259. }
  260. mNorFlashInstances = AllocatePool (sizeof (NOR_FLASH_INSTANCE *) * mNorFlashDeviceCount);
  261. for (Index = 0; Index < mNorFlashDeviceCount; Index++) {
  262. // Check if this NOR Flash device contain the variable storage region
  263. if (FixedPcdGet64 (PcdFlashNvStorageVariableBase64) != 0) {
  264. ContainVariableStorage =
  265. (NorFlashDevices[Index].RegionBaseAddress <= FixedPcdGet64 (PcdFlashNvStorageVariableBase64)) &&
  266. (FixedPcdGet64 (PcdFlashNvStorageVariableBase64) + FixedPcdGet32 (PcdFlashNvStorageVariableSize) <=
  267. NorFlashDevices[Index].RegionBaseAddress + NorFlashDevices[Index].Size);
  268. } else {
  269. ContainVariableStorage =
  270. (NorFlashDevices[Index].RegionBaseAddress <= FixedPcdGet32 (PcdFlashNvStorageVariableBase)) &&
  271. (FixedPcdGet32 (PcdFlashNvStorageVariableBase) + FixedPcdGet32 (PcdFlashNvStorageVariableSize) <=
  272. NorFlashDevices[Index].RegionBaseAddress + NorFlashDevices[Index].Size);
  273. }
  274. Status = NorFlashCreateInstance (
  275. NorFlashDevices[Index].DeviceBaseAddress,
  276. NorFlashDevices[Index].RegionBaseAddress,
  277. NorFlashDevices[Index].Size,
  278. Index,
  279. NorFlashDevices[Index].BlockSize,
  280. ContainVariableStorage,
  281. &mNorFlashInstances[Index]
  282. );
  283. if (EFI_ERROR (Status)) {
  284. DEBUG ((DEBUG_ERROR, "NorFlashInitialise: Fail to create instance for NorFlash[%d]\n", Index));
  285. }
  286. }
  287. return Status;
  288. }
  289. EFI_STATUS
  290. EFIAPI
  291. NorFlashFvbInitialize (
  292. IN NOR_FLASH_INSTANCE *Instance
  293. )
  294. {
  295. EFI_STATUS Status;
  296. UINT32 FvbNumLba;
  297. ASSERT ((Instance != NULL));
  298. mFlashNvStorageVariableBase = (FixedPcdGet64 (PcdFlashNvStorageVariableBase64) != 0) ?
  299. FixedPcdGet64 (PcdFlashNvStorageVariableBase64) : FixedPcdGet32 (PcdFlashNvStorageVariableBase);
  300. // Set the index of the first LBA for the FVB
  301. Instance->StartLba = (mFlashNvStorageVariableBase - Instance->RegionBaseAddress) / Instance->Media.BlockSize;
  302. // Determine if there is a valid header at the beginning of the NorFlash
  303. Status = ValidateFvHeader (Instance);
  304. // Install the Default FVB header if required
  305. if (EFI_ERROR (Status)) {
  306. // There is no valid header, so time to install one.
  307. DEBUG ((DEBUG_INFO, "%a: The FVB Header is not valid.\n", __FUNCTION__));
  308. DEBUG ((
  309. DEBUG_INFO,
  310. "%a: Installing a correct one for this volume.\n",
  311. __FUNCTION__
  312. ));
  313. // Erase all the NorFlash that is reserved for variable storage
  314. FvbNumLba = (PcdGet32 (PcdFlashNvStorageVariableSize) + PcdGet32 (PcdFlashNvStorageFtwWorkingSize) + PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / Instance->Media.BlockSize;
  315. Status = FvbEraseBlocks (&Instance->FvbProtocol, (EFI_LBA)0, FvbNumLba, EFI_LBA_LIST_TERMINATOR);
  316. if (EFI_ERROR (Status)) {
  317. return Status;
  318. }
  319. // Install all appropriate headers
  320. Status = InitializeFvAndVariableStoreHeaders (Instance);
  321. if (EFI_ERROR (Status)) {
  322. return Status;
  323. }
  324. }
  325. return Status;
  326. }