VirtNorFlashDxe.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /** @file NorFlashDxe.c
  2. Copyright (c) 2011 - 2021, Arm Limited. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include <Library/BaseMemoryLib.h>
  6. #include <Library/DxeServicesTableLib.h>
  7. #include <Library/HobLib.h>
  8. #include <Library/MemoryAllocationLib.h>
  9. #include <Library/PcdLib.h>
  10. #include <Library/UefiBootServicesTableLib.h>
  11. #include <Library/UefiLib.h>
  12. #include "VirtNorFlash.h"
  13. STATIC EFI_EVENT mNorFlashVirtualAddrChangeEvent;
  14. //
  15. // Global variable declarations
  16. //
  17. NOR_FLASH_INSTANCE **mNorFlashInstances;
  18. UINT32 mNorFlashDeviceCount;
  19. UINTN mFlashNvStorageVariableBase;
  20. EFI_EVENT mFvbVirtualAddrChangeEvent;
  21. NOR_FLASH_INSTANCE mNorFlashInstanceTemplate = {
  22. NOR_FLASH_SIGNATURE, // Signature
  23. NULL, // Handle ... NEED TO BE FILLED
  24. 0, // DeviceBaseAddress ... NEED TO BE FILLED
  25. 0, // RegionBaseAddress ... NEED TO BE FILLED
  26. 0, // Size ... NEED TO BE FILLED
  27. 0, // StartLba
  28. 0, // LastBlock
  29. 0, // BlockSize
  30. {
  31. FvbGetAttributes, // GetAttributes
  32. FvbSetAttributes, // SetAttributes
  33. FvbGetPhysicalAddress, // GetPhysicalAddress
  34. FvbGetBlockSize, // GetBlockSize
  35. FvbRead, // Read
  36. FvbWrite, // Write
  37. FvbEraseBlocks, // EraseBlocks
  38. NULL, // ParentHandle
  39. }, // FvbProtoccol;
  40. NULL, // ShadowBuffer
  41. {
  42. {
  43. {
  44. HARDWARE_DEVICE_PATH,
  45. HW_VENDOR_DP,
  46. {
  47. (UINT8)(OFFSET_OF (NOR_FLASH_DEVICE_PATH, End)),
  48. (UINT8)(OFFSET_OF (NOR_FLASH_DEVICE_PATH, End) >> 8)
  49. }
  50. },
  51. { 0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }
  52. }, // GUID ... NEED TO BE FILLED
  53. },
  54. 0, // Index
  55. {
  56. END_DEVICE_PATH_TYPE,
  57. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  58. { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 }
  59. }
  60. } // DevicePath
  61. };
  62. EFI_STATUS
  63. NorFlashCreateInstance (
  64. IN UINTN NorFlashDeviceBase,
  65. IN UINTN NorFlashRegionBase,
  66. IN UINTN NorFlashSize,
  67. IN UINT32 Index,
  68. IN UINT32 BlockSize,
  69. IN BOOLEAN SupportFvb,
  70. OUT NOR_FLASH_INSTANCE **NorFlashInstance
  71. )
  72. {
  73. EFI_STATUS Status;
  74. NOR_FLASH_INSTANCE *Instance;
  75. ASSERT (NorFlashInstance != NULL);
  76. Instance = AllocateRuntimeCopyPool (sizeof (NOR_FLASH_INSTANCE), &mNorFlashInstanceTemplate);
  77. if (Instance == NULL) {
  78. return EFI_OUT_OF_RESOURCES;
  79. }
  80. Instance->DeviceBaseAddress = NorFlashDeviceBase;
  81. Instance->RegionBaseAddress = NorFlashRegionBase;
  82. Instance->Size = NorFlashSize;
  83. Instance->BlockSize = BlockSize;
  84. Instance->LastBlock = (NorFlashSize / BlockSize) - 1;
  85. CopyGuid (&Instance->DevicePath.Vendor.Guid, &gEfiCallerIdGuid);
  86. Instance->DevicePath.Index = (UINT8)Index;
  87. Instance->ShadowBuffer = AllocateRuntimePool (BlockSize);
  88. if (Instance->ShadowBuffer == NULL) {
  89. return EFI_OUT_OF_RESOURCES;
  90. }
  91. if (SupportFvb) {
  92. NorFlashFvbInitialize (Instance);
  93. Status = gBS->InstallMultipleProtocolInterfaces (
  94. &Instance->Handle,
  95. &gEfiDevicePathProtocolGuid,
  96. &Instance->DevicePath,
  97. &gEfiFirmwareVolumeBlockProtocolGuid,
  98. &Instance->FvbProtocol,
  99. NULL
  100. );
  101. if (EFI_ERROR (Status)) {
  102. FreePool (Instance);
  103. return Status;
  104. }
  105. } else {
  106. Status = gBS->InstallMultipleProtocolInterfaces (
  107. &Instance->Handle,
  108. &gEfiDevicePathProtocolGuid,
  109. &Instance->DevicePath,
  110. NULL
  111. );
  112. if (EFI_ERROR (Status)) {
  113. FreePool (Instance);
  114. return Status;
  115. }
  116. }
  117. *NorFlashInstance = Instance;
  118. return Status;
  119. }
  120. /**
  121. * This function unlock and erase an entire NOR Flash block.
  122. **/
  123. EFI_STATUS
  124. NorFlashUnlockAndEraseSingleBlock (
  125. IN NOR_FLASH_INSTANCE *Instance,
  126. IN UINTN BlockAddress
  127. )
  128. {
  129. EFI_STATUS Status;
  130. UINTN Index;
  131. EFI_TPL OriginalTPL;
  132. if (!EfiAtRuntime ()) {
  133. // Raise TPL to TPL_HIGH to stop anyone from interrupting us.
  134. OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
  135. } else {
  136. // This initialization is only to prevent the compiler to complain about the
  137. // use of uninitialized variables
  138. OriginalTPL = TPL_HIGH_LEVEL;
  139. }
  140. Index = 0;
  141. // The block erase might fail a first time (SW bug ?). Retry it ...
  142. do {
  143. // Unlock the block if we have to
  144. Status = NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddress);
  145. if (EFI_ERROR (Status)) {
  146. break;
  147. }
  148. Status = NorFlashEraseSingleBlock (Instance, BlockAddress);
  149. Index++;
  150. } while ((Index < NOR_FLASH_ERASE_RETRY) && (Status == EFI_WRITE_PROTECTED));
  151. if (Index == NOR_FLASH_ERASE_RETRY) {
  152. DEBUG ((DEBUG_ERROR, "EraseSingleBlock(BlockAddress=0x%08x: Block Locked Error (try to erase %d times)\n", BlockAddress, Index));
  153. }
  154. if (!EfiAtRuntime ()) {
  155. // Interruptions can resume.
  156. gBS->RestoreTPL (OriginalTPL);
  157. }
  158. return Status;
  159. }
  160. EFI_STATUS
  161. NorFlashWriteFullBlock (
  162. IN NOR_FLASH_INSTANCE *Instance,
  163. IN EFI_LBA Lba,
  164. IN UINT32 *DataBuffer,
  165. IN UINT32 BlockSizeInWords
  166. )
  167. {
  168. EFI_STATUS Status;
  169. UINTN WordAddress;
  170. UINT32 WordIndex;
  171. UINTN BufferIndex;
  172. UINTN BlockAddress;
  173. UINTN BuffersInBlock;
  174. UINTN RemainingWords;
  175. EFI_TPL OriginalTPL;
  176. UINTN Cnt;
  177. Status = EFI_SUCCESS;
  178. // Get the physical address of the block
  179. BlockAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, BlockSizeInWords * 4);
  180. // Start writing from the first address at the start of the block
  181. WordAddress = BlockAddress;
  182. if (!EfiAtRuntime ()) {
  183. // Raise TPL to TPL_HIGH to stop anyone from interrupting us.
  184. OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
  185. } else {
  186. // This initialization is only to prevent the compiler to complain about the
  187. // use of uninitialized variables
  188. OriginalTPL = TPL_HIGH_LEVEL;
  189. }
  190. Status = NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress);
  191. if (EFI_ERROR (Status)) {
  192. DEBUG ((DEBUG_ERROR, "WriteSingleBlock: ERROR - Failed to Unlock and Erase the single block at 0x%X\n", BlockAddress));
  193. goto EXIT;
  194. }
  195. // To speed up the programming operation, NOR Flash is programmed using the Buffered Programming method.
  196. // Check that the address starts at a 32-word boundary, i.e. last 7 bits must be zero
  197. if ((WordAddress & BOUNDARY_OF_32_WORDS) == 0x00) {
  198. // First, break the entire block into buffer-sized chunks.
  199. BuffersInBlock = (UINTN)(BlockSizeInWords * 4) / P30_MAX_BUFFER_SIZE_IN_BYTES;
  200. // Then feed each buffer chunk to the NOR Flash
  201. // If a buffer does not contain any data, don't write it.
  202. for (BufferIndex = 0;
  203. BufferIndex < BuffersInBlock;
  204. BufferIndex++, WordAddress += P30_MAX_BUFFER_SIZE_IN_BYTES, DataBuffer += P30_MAX_BUFFER_SIZE_IN_WORDS
  205. )
  206. {
  207. // Check the buffer to see if it contains any data (not set all 1s).
  208. for (Cnt = 0; Cnt < P30_MAX_BUFFER_SIZE_IN_WORDS; Cnt++) {
  209. if (~DataBuffer[Cnt] != 0 ) {
  210. // Some data found, write the buffer.
  211. Status = NorFlashWriteBuffer (
  212. Instance,
  213. WordAddress,
  214. P30_MAX_BUFFER_SIZE_IN_BYTES,
  215. DataBuffer
  216. );
  217. if (EFI_ERROR (Status)) {
  218. goto EXIT;
  219. }
  220. break;
  221. }
  222. }
  223. }
  224. // Finally, finish off any remaining words that are less than the maximum size of the buffer
  225. RemainingWords = BlockSizeInWords % P30_MAX_BUFFER_SIZE_IN_WORDS;
  226. if (RemainingWords != 0) {
  227. Status = NorFlashWriteBuffer (Instance, WordAddress, (RemainingWords * 4), DataBuffer);
  228. if (EFI_ERROR (Status)) {
  229. goto EXIT;
  230. }
  231. }
  232. } else {
  233. // For now, use the single word programming algorithm
  234. // It is unlikely that the NOR Flash will exist in an address which falls within a 32 word boundary range,
  235. // i.e. which ends in the range 0x......01 - 0x......7F.
  236. for (WordIndex = 0; WordIndex < BlockSizeInWords; WordIndex++, DataBuffer++, WordAddress = WordAddress + 4) {
  237. Status = NorFlashWriteSingleWord (Instance, WordAddress, *DataBuffer);
  238. if (EFI_ERROR (Status)) {
  239. goto EXIT;
  240. }
  241. }
  242. }
  243. EXIT:
  244. // Put device back into Read Array mode
  245. SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);
  246. if (!EfiAtRuntime ()) {
  247. // Interruptions can resume.
  248. gBS->RestoreTPL (OriginalTPL);
  249. }
  250. if (EFI_ERROR (Status)) {
  251. DEBUG ((DEBUG_ERROR, "NOR FLASH Programming [WriteSingleBlock] failed at address 0x%08x. Exit Status = \"%r\".\n", WordAddress, Status));
  252. }
  253. return Status;
  254. }
  255. EFI_STATUS
  256. EFIAPI
  257. NorFlashInitialise (
  258. IN EFI_HANDLE ImageHandle,
  259. IN EFI_SYSTEM_TABLE *SystemTable
  260. )
  261. {
  262. EFI_STATUS Status;
  263. UINT32 Index;
  264. VIRT_NOR_FLASH_DESCRIPTION *NorFlashDevices;
  265. BOOLEAN ContainVariableStorage;
  266. Status = VirtNorFlashPlatformInitialization ();
  267. if (EFI_ERROR (Status)) {
  268. DEBUG ((DEBUG_ERROR, "NorFlashInitialise: Fail to initialize Nor Flash devices\n"));
  269. return Status;
  270. }
  271. Status = VirtNorFlashPlatformGetDevices (&NorFlashDevices, &mNorFlashDeviceCount);
  272. if (EFI_ERROR (Status)) {
  273. DEBUG ((DEBUG_ERROR, "NorFlashInitialise: Fail to get Nor Flash devices\n"));
  274. return Status;
  275. }
  276. mNorFlashInstances = AllocateRuntimePool (sizeof (NOR_FLASH_INSTANCE *) * mNorFlashDeviceCount);
  277. for (Index = 0; Index < mNorFlashDeviceCount; Index++) {
  278. // Check if this NOR Flash device contain the variable storage region
  279. if (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0) {
  280. ContainVariableStorage =
  281. (NorFlashDevices[Index].RegionBaseAddress <= PcdGet64 (PcdFlashNvStorageVariableBase64)) &&
  282. (PcdGet64 (PcdFlashNvStorageVariableBase64) + PcdGet32 (PcdFlashNvStorageVariableSize) <=
  283. NorFlashDevices[Index].RegionBaseAddress + NorFlashDevices[Index].Size);
  284. } else {
  285. ContainVariableStorage =
  286. (NorFlashDevices[Index].RegionBaseAddress <= PcdGet32 (PcdFlashNvStorageVariableBase)) &&
  287. (PcdGet32 (PcdFlashNvStorageVariableBase) + PcdGet32 (PcdFlashNvStorageVariableSize) <=
  288. NorFlashDevices[Index].RegionBaseAddress + NorFlashDevices[Index].Size);
  289. }
  290. Status = NorFlashCreateInstance (
  291. NorFlashDevices[Index].DeviceBaseAddress,
  292. NorFlashDevices[Index].RegionBaseAddress,
  293. NorFlashDevices[Index].Size,
  294. Index,
  295. NorFlashDevices[Index].BlockSize,
  296. ContainVariableStorage,
  297. &mNorFlashInstances[Index]
  298. );
  299. if (EFI_ERROR (Status)) {
  300. DEBUG ((DEBUG_ERROR, "NorFlashInitialise: Fail to create instance for NorFlash[%d]\n", Index));
  301. }
  302. }
  303. //
  304. // Register for the virtual address change event
  305. //
  306. Status = gBS->CreateEventEx (
  307. EVT_NOTIFY_SIGNAL,
  308. TPL_NOTIFY,
  309. NorFlashVirtualNotifyEvent,
  310. NULL,
  311. &gEfiEventVirtualAddressChangeGuid,
  312. &mNorFlashVirtualAddrChangeEvent
  313. );
  314. ASSERT_EFI_ERROR (Status);
  315. return Status;
  316. }
  317. EFI_STATUS
  318. EFIAPI
  319. NorFlashFvbInitialize (
  320. IN NOR_FLASH_INSTANCE *Instance
  321. )
  322. {
  323. EFI_STATUS Status;
  324. UINT32 FvbNumLba;
  325. EFI_BOOT_MODE BootMode;
  326. UINTN RuntimeMmioRegionSize;
  327. DEBUG ((DEBUG_BLKIO, "NorFlashFvbInitialize\n"));
  328. ASSERT ((Instance != NULL));
  329. //
  330. // Declare the Non-Volatile storage as EFI_MEMORY_RUNTIME
  331. //
  332. // Note: all the NOR Flash region needs to be reserved into the UEFI Runtime memory;
  333. // even if we only use the small block region at the top of the NOR Flash.
  334. // The reason is when the NOR Flash memory is set into program mode, the command
  335. // is written as the base of the flash region (ie: Instance->DeviceBaseAddress)
  336. RuntimeMmioRegionSize = (Instance->RegionBaseAddress - Instance->DeviceBaseAddress) + Instance->Size;
  337. Status = gDS->AddMemorySpace (
  338. EfiGcdMemoryTypeMemoryMappedIo,
  339. Instance->DeviceBaseAddress,
  340. RuntimeMmioRegionSize,
  341. EFI_MEMORY_WC | EFI_MEMORY_RUNTIME
  342. );
  343. ASSERT_EFI_ERROR (Status);
  344. Status = gDS->SetMemorySpaceAttributes (
  345. Instance->DeviceBaseAddress,
  346. RuntimeMmioRegionSize,
  347. EFI_MEMORY_WC | EFI_MEMORY_RUNTIME
  348. );
  349. ASSERT_EFI_ERROR (Status);
  350. mFlashNvStorageVariableBase = (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0) ?
  351. PcdGet64 (PcdFlashNvStorageVariableBase64) : PcdGet32 (PcdFlashNvStorageVariableBase);
  352. // Set the index of the first LBA for the FVB
  353. Instance->StartLba = (mFlashNvStorageVariableBase - Instance->RegionBaseAddress) / Instance->BlockSize;
  354. BootMode = GetBootModeHob ();
  355. if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {
  356. Status = EFI_INVALID_PARAMETER;
  357. } else {
  358. // Determine if there is a valid header at the beginning of the NorFlash
  359. Status = ValidateFvHeader (Instance);
  360. }
  361. // Install the Default FVB header if required
  362. if (EFI_ERROR (Status)) {
  363. // There is no valid header, so time to install one.
  364. DEBUG ((DEBUG_INFO, "%a: The FVB Header is not valid.\n", __FUNCTION__));
  365. DEBUG ((
  366. DEBUG_INFO,
  367. "%a: Installing a correct one for this volume.\n",
  368. __FUNCTION__
  369. ));
  370. // Erase all the NorFlash that is reserved for variable storage
  371. FvbNumLba = (PcdGet32 (PcdFlashNvStorageVariableSize) + PcdGet32 (PcdFlashNvStorageFtwWorkingSize) + PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / Instance->BlockSize;
  372. Status = FvbEraseBlocks (&Instance->FvbProtocol, (EFI_LBA)0, FvbNumLba, EFI_LBA_LIST_TERMINATOR);
  373. if (EFI_ERROR (Status)) {
  374. return Status;
  375. }
  376. // Install all appropriate headers
  377. Status = InitializeFvAndVariableStoreHeaders (Instance);
  378. if (EFI_ERROR (Status)) {
  379. return Status;
  380. }
  381. }
  382. //
  383. // The driver implementing the variable read service can now be dispatched;
  384. // the varstore headers are in place.
  385. //
  386. Status = gBS->InstallProtocolInterface (
  387. &gImageHandle,
  388. &gEdkiiNvVarStoreFormattedGuid,
  389. EFI_NATIVE_INTERFACE,
  390. NULL
  391. );
  392. ASSERT_EFI_ERROR (Status);
  393. //
  394. // Register for the virtual address change event
  395. //
  396. Status = gBS->CreateEventEx (
  397. EVT_NOTIFY_SIGNAL,
  398. TPL_NOTIFY,
  399. FvbVirtualNotifyEvent,
  400. NULL,
  401. &gEfiEventVirtualAddressChangeGuid,
  402. &mFvbVirtualAddrChangeEvent
  403. );
  404. ASSERT_EFI_ERROR (Status);
  405. return Status;
  406. }