FvbService.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. /** @file
  2. Firmware Volume Block Driver for Lakeport Platform.
  3. Firmware volume block driver for FWH or SPI device.
  4. It depends on which Flash Device Library to be linked with this driver.
  5. Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #include "FvbService.h"
  9. //
  10. // Global variable for this FVB driver which contains
  11. // the private data of all firmware volume block instances.
  12. //
  13. FWB_GLOBAL mFvbModuleGlobal;
  14. //
  15. // This platform driver knows there are 3 FVs on
  16. // FD, which are FvRecovery, FvMain and FvNvStorage.
  17. //
  18. UINT32 mPlatformFvBaseAddress[] = {
  19. FixedPcdGet32(PcdFlashNvStorageVariableBase),
  20. };
  21. FV_MEMMAP_DEVICE_PATH mFvMemmapDevicePathTemplate = {
  22. {
  23. {
  24. HARDWARE_DEVICE_PATH,
  25. HW_MEMMAP_DP,
  26. {
  27. (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),
  28. (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8)
  29. }
  30. },
  31. EfiMemoryMappedIO,
  32. (EFI_PHYSICAL_ADDRESS) 0,
  33. (EFI_PHYSICAL_ADDRESS) 0,
  34. },
  35. {
  36. END_DEVICE_PATH_TYPE,
  37. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  38. {
  39. END_DEVICE_PATH_LENGTH,
  40. 0
  41. }
  42. }
  43. };
  44. FV_PIWG_DEVICE_PATH mFvPIWGDevicePathTemplate = {
  45. {
  46. {
  47. MEDIA_DEVICE_PATH,
  48. MEDIA_PIWG_FW_VOL_DP,
  49. {
  50. (UINT8)(sizeof (MEDIA_FW_VOL_DEVICE_PATH)),
  51. (UINT8)(sizeof (MEDIA_FW_VOL_DEVICE_PATH) >> 8)
  52. }
  53. },
  54. { 0 }
  55. },
  56. {
  57. END_DEVICE_PATH_TYPE,
  58. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  59. {
  60. END_DEVICE_PATH_LENGTH,
  61. 0
  62. }
  63. }
  64. };
  65. //
  66. // Template structure used when installing FVB protocol.
  67. //
  68. EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate = {
  69. FVB_DEVICE_SIGNATURE,
  70. NULL,
  71. 0, // Instance
  72. {
  73. FvbProtocolGetAttributes,
  74. FvbProtocolSetAttributes,
  75. FvbProtocolGetPhysicalAddress,
  76. FvbProtocolGetBlockSize,
  77. FvbProtocolRead,
  78. FvbProtocolWrite,
  79. FvbProtocolEraseBlocks,
  80. NULL
  81. } // FwVolBlockInstance
  82. };
  83. /**
  84. Get the pointer to EFI_FW_VOL_INSTANCE from the buffer pointed
  85. by mFvbModuleGlobal.FvInstance based on a index.
  86. Each EFI_FW_VOL_INSTANCE is with variable length as
  87. we have a block map at the end of the EFI_FIRMWARE_VOLUME_HEADER.
  88. @param[in] Instance The index of the EFI_FW_VOL_INSTANCE.
  89. @return A pointer to EFI_FW_VOL_INSTANCE.
  90. **/
  91. EFI_FW_VOL_INSTANCE *
  92. GetFvbInstance (
  93. IN UINTN Instance
  94. )
  95. {
  96. EFI_FW_VOL_INSTANCE *FwhRecord;
  97. if ( Instance >= mFvbModuleGlobal.NumFv ) {
  98. ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);
  99. return NULL;
  100. }
  101. //
  102. // Find the right instance of the FVB private data.
  103. //
  104. FwhRecord = mFvbModuleGlobal.FvInstance;
  105. while ( Instance > 0 ) {
  106. FwhRecord = (EFI_FW_VOL_INSTANCE *) ((UINTN)((UINT8 *)FwhRecord) +
  107. FwhRecord->VolumeHeader.HeaderLength +
  108. (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER)));
  109. Instance --;
  110. }
  111. return FwhRecord;
  112. }
  113. /**
  114. Get the EFI_FVB_ATTRIBUTES_2 of a FV.
  115. @param[in] The index of the EFI_FW_VOL_INSTANCE.
  116. @return EFI_FVB_ATTRIBUTES_2 of the FV identified by Instance.
  117. **/
  118. STATIC
  119. EFI_FVB_ATTRIBUTES_2
  120. FvbGetVolumeAttributes (
  121. IN UINTN Instance
  122. )
  123. {
  124. EFI_FW_VOL_INSTANCE * FwInstance = NULL;
  125. FwInstance = GetFvbInstance(Instance);
  126. ASSERT (FwInstance != NULL);
  127. if ( FwInstance != NULL ) {
  128. return FwInstance->VolumeHeader.Attributes;
  129. } else {
  130. return 0;
  131. }
  132. }
  133. /**
  134. Retrieves the starting address of an LBA in an FV. It also
  135. return a few other attribut of the FV.
  136. @param[in] Instance The index of the EFI_FW_VOL_INSTANCE.
  137. @param[in] Lba The logical block address.
  138. @param[out] LbaAddress On output, contains the physical starting address
  139. of the Lba.
  140. @param[out] LbaLength On output, contains the length of the block.
  141. @param[out] NumOfBlocks A pointer to a caller allocated UINTN in which the
  142. number of consecutive blocks starting with Lba is
  143. returned. All blocks in this range have a size of
  144. BlockSize.
  145. @retval EFI_SUCCESS Successfully returns.
  146. @retval EFI_INVALID_PARAMETER Instance not found.
  147. **/
  148. STATIC
  149. EFI_STATUS
  150. FvbGetLbaAddress (
  151. IN UINTN Instance,
  152. IN EFI_LBA Lba,
  153. OUT UINTN *LbaAddress,
  154. OUT UINTN *LbaLength,
  155. OUT UINTN *NumOfBlocks
  156. )
  157. {
  158. UINT32 NumBlocks = 0;
  159. UINT32 BlockLength = 0;
  160. UINTN Offset;
  161. EFI_LBA StartLba;
  162. EFI_LBA NextLba;
  163. EFI_FW_VOL_INSTANCE *FwhInstance;
  164. EFI_FV_BLOCK_MAP_ENTRY *BlockMap = NULL;
  165. //
  166. // Find the right instance of the FVB private data.
  167. //
  168. FwhInstance = GetFvbInstance (Instance);
  169. StartLba = 0;
  170. Offset = 0;
  171. BlockMap = &(FwhInstance->VolumeHeader.BlockMap[0]);
  172. ASSERT (BlockMap != NULL);
  173. //
  174. // Parse the blockmap of the FV to find which map entry the Lba belongs to.
  175. //
  176. while (TRUE) {
  177. if ( BlockMap != NULL) {
  178. NumBlocks = BlockMap->NumBlocks;
  179. BlockLength = BlockMap->Length;
  180. }
  181. if ( NumBlocks == 0 || BlockLength == 0) {
  182. return EFI_INVALID_PARAMETER;
  183. }
  184. NextLba = StartLba + NumBlocks;
  185. //
  186. // The map entry found.
  187. //
  188. if (Lba >= StartLba && Lba < NextLba) {
  189. Offset = Offset + (UINTN)MultU64x32((Lba - StartLba), BlockLength);
  190. if ( LbaAddress && FwhInstance ) {
  191. *LbaAddress = FwhInstance->FvBase + Offset;
  192. }
  193. if (LbaLength ) {
  194. *LbaLength = BlockLength;
  195. }
  196. if (NumOfBlocks ) {
  197. *NumOfBlocks = (UINTN)(NextLba - Lba);
  198. }
  199. return EFI_SUCCESS;
  200. }
  201. StartLba = NextLba;
  202. Offset = Offset + NumBlocks * BlockLength;
  203. BlockMap++;
  204. }
  205. }
  206. /**
  207. Reads specified number of bytes into a buffer from the specified block.
  208. @param[in] Instance The FV instance to be read from.
  209. @param[in] Lba The logical block address to be read from.
  210. @param[in] BlockOffset Offset into the block at which to begin reading.
  211. @param[in] NumBytes Pointer that on input contains the total size of
  212. the buffer. On output, it contains the total number
  213. of bytes read.
  214. @param[in] Buffer Pointer to a caller allocated buffer that will be
  215. used to hold the data read.
  216. @retval EFI_SUCCESS The firmware volume was read successfully and
  217. contents are in Buffer.
  218. @retval EFI_BAD_BUFFER_SIZE Read attempted across a LBA boundary. On output,
  219. NumBytes contains the total number of bytes returned
  220. in Buffer.
  221. @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state.
  222. @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
  223. could not be read.
  224. @retval EFI_INVALID_PARAMETER Instance not found, or NumBytes, Buffer are NULL.
  225. **/
  226. STATIC
  227. EFI_STATUS
  228. FvbReadBlock (
  229. IN UINTN Instance,
  230. IN EFI_LBA Lba,
  231. IN UINTN BlockOffset,
  232. IN OUT UINTN *NumBytes,
  233. IN UINT8 *Buffer
  234. )
  235. {
  236. EFI_FVB_ATTRIBUTES_2 Attributes;
  237. UINTN LbaAddress;
  238. UINTN LbaLength;
  239. EFI_STATUS Status;
  240. if ( (NumBytes == NULL) || (Buffer == NULL)) {
  241. return (EFI_INVALID_PARAMETER);
  242. }
  243. if (*NumBytes == 0) {
  244. return (EFI_INVALID_PARAMETER);
  245. }
  246. Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL);
  247. if (EFI_ERROR(Status)) {
  248. return Status;
  249. }
  250. Attributes = FvbGetVolumeAttributes (Instance);
  251. if ( (Attributes & EFI_FVB2_READ_STATUS) == 0) {
  252. return (EFI_ACCESS_DENIED);
  253. }
  254. if (BlockOffset > LbaLength) {
  255. return (EFI_INVALID_PARAMETER);
  256. }
  257. if (LbaLength < ( *NumBytes + BlockOffset ) ) {
  258. *NumBytes = (UINT32) (LbaLength - BlockOffset);
  259. Status = EFI_BAD_BUFFER_SIZE;
  260. }
  261. LibFvbFlashDeviceRead (LbaAddress + BlockOffset, NumBytes, Buffer);
  262. return Status;
  263. }
  264. /**
  265. Writes specified number of bytes from the input buffer to the block.
  266. @param[in] Instance The FV instance to be written to.
  267. @param[in] Lba The starting logical block index to write to.
  268. @param[in] BlockOffset Offset into the block at which to begin writing.
  269. @param[in] NumBytes Pointer that on input contains the total size of
  270. the buffer. On output, it contains the total number
  271. of bytes actually written.
  272. @param[in] Buffer Pointer to a caller allocated buffer that contains
  273. the source for the write.
  274. @retval EFI_SUCCESS The firmware volume was written successfully.
  275. @retval EFI_BAD_BUFFER_SIZE Write attempted across a LBA boundary. On output,
  276. NumBytes contains the total number of bytes
  277. actually writte.
  278. @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
  279. @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
  280. could not be written.
  281. @retval EFI_INVALID_PARAMETER Instance not found, or NumBytes, Buffer are NULL.
  282. **/
  283. EFI_STATUS
  284. FvbWriteBlock (
  285. IN UINTN Instance,
  286. IN EFI_LBA Lba,
  287. IN UINTN BlockOffset,
  288. IN OUT UINTN *NumBytes,
  289. IN UINT8 *Buffer
  290. )
  291. {
  292. EFI_FVB_ATTRIBUTES_2 Attributes;
  293. UINTN LbaAddress;
  294. UINTN LbaLength;
  295. EFI_STATUS Status;
  296. EFI_STATUS Status1;
  297. if ( (NumBytes == NULL) || (Buffer == NULL)) {
  298. return (EFI_INVALID_PARAMETER);
  299. }
  300. if (*NumBytes == 0) {
  301. return (EFI_INVALID_PARAMETER);
  302. }
  303. Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL);
  304. if (EFI_ERROR(Status)) {
  305. return Status;
  306. }
  307. //
  308. // Check if the FV is write enabled.
  309. //
  310. Attributes = FvbGetVolumeAttributes (Instance);
  311. if ( (Attributes & EFI_FVB2_WRITE_STATUS) == 0) {
  312. return (EFI_ACCESS_DENIED);
  313. }
  314. //
  315. // Perform boundary checks and adjust NumBytes.
  316. //
  317. if (BlockOffset > LbaLength) {
  318. return (EFI_INVALID_PARAMETER);
  319. }
  320. if ( LbaLength < ( *NumBytes + BlockOffset ) ) {
  321. DEBUG ((EFI_D_ERROR,
  322. "FvWriteBlock: Reducing Numbytes from 0x%x to 0x%x\n",
  323. *NumBytes,
  324. (UINT32)(LbaLength-BlockOffset))
  325. );
  326. *NumBytes = (UINT32) (LbaLength - BlockOffset);
  327. Status = EFI_BAD_BUFFER_SIZE;
  328. }
  329. LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, FALSE);
  330. Status1 = LibFvbFlashDeviceWrite (LbaAddress + BlockOffset, NumBytes, Buffer);
  331. LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, TRUE);
  332. WriteBackInvalidateDataCacheRange ((VOID *) (LbaAddress + BlockOffset), *NumBytes);
  333. if ( EFI_ERROR (Status1) ) {
  334. return Status1;
  335. }
  336. return Status;
  337. }
  338. /**
  339. Erases and initializes a firmware volume block.
  340. @param[in] Instance The FV instance to be erased.
  341. @param[in] Lba The logical block index to be erased.
  342. @retval EFI_SUCCESS The erase request was successfully completed.
  343. @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
  344. @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
  345. could not be written. Firmware device may have been
  346. partially erased.
  347. @retval EFI_INVALID_PARAMETER Instance not found.
  348. **/
  349. EFI_STATUS
  350. FvbEraseBlock (
  351. IN UINTN Instance,
  352. IN EFI_LBA Lba
  353. )
  354. {
  355. EFI_FVB_ATTRIBUTES_2 Attributes;
  356. UINTN LbaAddress;
  357. UINTN LbaLength;
  358. EFI_STATUS Status;
  359. //
  360. // Check if the FV is write enabled.
  361. //
  362. Attributes = FvbGetVolumeAttributes (Instance);
  363. if( (Attributes & EFI_FVB2_WRITE_STATUS) == 0) {
  364. return (EFI_ACCESS_DENIED);
  365. }
  366. //
  367. // Get the starting address of the block for erase.
  368. //
  369. Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL);
  370. if (EFI_ERROR(Status)) {
  371. return Status;
  372. }
  373. LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, FALSE);
  374. Status = LibFvbFlashDeviceBlockErase (LbaAddress, LbaLength);
  375. LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, TRUE);
  376. WriteBackInvalidateDataCacheRange ((VOID *) LbaAddress, LbaLength);
  377. return Status;
  378. }
  379. /**
  380. Modifies the current settings of the firmware volume according to the
  381. input parameter, and returns the new setting of the volume.
  382. @param[in] Instance The FV instance whose attributes is going to be
  383. modified.
  384. @param[in] Attributes On input, it is a pointer to EFI_FVB_ATTRIBUTES_2
  385. containing the desired firmware volume settings.
  386. On successful return, it contains the new settings
  387. of the firmware volume.
  388. @retval EFI_SUCCESS Successfully returns.
  389. @retval EFI_ACCESS_DENIED The volume setting is locked and cannot be modified.
  390. @retval EFI_INVALID_PARAMETER Instance not found, or The attributes requested are
  391. in conflict with the capabilities as declared in the
  392. firmware volume header.
  393. **/
  394. STATIC
  395. EFI_STATUS
  396. FvbSetVolumeAttributes (
  397. IN UINTN Instance,
  398. IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
  399. )
  400. {
  401. EFI_FW_VOL_INSTANCE *FwhInstance = NULL;
  402. EFI_FVB_ATTRIBUTES_2 OldAttributes = 0;
  403. EFI_FVB_ATTRIBUTES_2 *AttribPtr = NULL;
  404. EFI_FVB_ATTRIBUTES_2 UnchangedAttributes;
  405. UINT32 Capabilities;
  406. UINT32 OldStatus, NewStatus;
  407. //
  408. // Find the right instance of the FVB private data.
  409. //
  410. FwhInstance = GetFvbInstance (Instance);
  411. AttribPtr = (EFI_FVB_ATTRIBUTES_2 *) & (FwhInstance->VolumeHeader.Attributes);
  412. ASSERT (AttribPtr != NULL);
  413. if ( AttribPtr != NULL) {
  414. OldAttributes = *AttribPtr;
  415. }
  416. Capabilities = OldAttributes & EFI_FVB2_CAPABILITIES;
  417. OldStatus = OldAttributes & EFI_FVB2_STATUS;
  418. NewStatus = *Attributes & EFI_FVB2_STATUS;
  419. UnchangedAttributes = EFI_FVB2_READ_DISABLED_CAP | \
  420. EFI_FVB2_READ_ENABLED_CAP | \
  421. EFI_FVB2_WRITE_DISABLED_CAP | \
  422. EFI_FVB2_WRITE_ENABLED_CAP | \
  423. EFI_FVB2_LOCK_CAP | \
  424. EFI_FVB2_STICKY_WRITE | \
  425. EFI_FVB2_MEMORY_MAPPED | \
  426. EFI_FVB2_ERASE_POLARITY | \
  427. EFI_FVB2_READ_LOCK_CAP | \
  428. EFI_FVB2_WRITE_LOCK_CAP | \
  429. EFI_FVB2_ALIGNMENT;
  430. //
  431. // Some attributes of FV is read only can *not* be set.
  432. //
  433. if ((OldAttributes & UnchangedAttributes) ^ (*Attributes & UnchangedAttributes)) {
  434. return EFI_INVALID_PARAMETER;
  435. }
  436. //
  437. // If firmware volume is locked, no status bit can be updated.
  438. //
  439. if ( OldAttributes & EFI_FVB2_LOCK_STATUS ) {
  440. if ( OldStatus ^ NewStatus ) {
  441. return EFI_ACCESS_DENIED;
  442. }
  443. }
  444. //
  445. // Test read disable.
  446. //
  447. if ((Capabilities & EFI_FVB2_READ_DISABLED_CAP) == 0) {
  448. if ((NewStatus & EFI_FVB2_READ_STATUS) == 0) {
  449. return EFI_INVALID_PARAMETER;
  450. }
  451. }
  452. //
  453. // Test read enable.
  454. //
  455. if ((Capabilities & EFI_FVB2_READ_ENABLED_CAP) == 0) {
  456. if (NewStatus & EFI_FVB2_READ_STATUS) {
  457. return EFI_INVALID_PARAMETER;
  458. }
  459. }
  460. //
  461. // Test write disable.
  462. //
  463. if ((Capabilities & EFI_FVB2_WRITE_DISABLED_CAP) == 0) {
  464. if ((NewStatus & EFI_FVB2_WRITE_STATUS) == 0) {
  465. return EFI_INVALID_PARAMETER;
  466. }
  467. }
  468. //
  469. // Test write enable.
  470. //
  471. if ((Capabilities & EFI_FVB2_WRITE_ENABLED_CAP) == 0) {
  472. if (NewStatus & EFI_FVB2_WRITE_STATUS) {
  473. return EFI_INVALID_PARAMETER;
  474. }
  475. }
  476. //
  477. // Test lock.
  478. //
  479. if ((Capabilities & EFI_FVB2_LOCK_CAP) == 0) {
  480. if (NewStatus & EFI_FVB2_LOCK_STATUS) {
  481. return EFI_INVALID_PARAMETER;
  482. }
  483. }
  484. *AttribPtr = (*AttribPtr) & (0xFFFFFFFF & (~EFI_FVB2_STATUS));
  485. *AttribPtr = (*AttribPtr) | NewStatus;
  486. *Attributes = *AttribPtr;
  487. return EFI_SUCCESS;
  488. }
  489. //
  490. // FVB protocol APIs.
  491. //
  492. /**
  493. Retrieves the physical address of the device.
  494. @param[in] This A pointer to EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL.
  495. @param[out] Address Output buffer containing the address.
  496. retval EFI_SUCCESS The function always return successfully.
  497. **/
  498. EFI_STATUS
  499. EFIAPI
  500. FvbProtocolGetPhysicalAddress (
  501. IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
  502. OUT EFI_PHYSICAL_ADDRESS *Address
  503. )
  504. {
  505. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  506. EFI_FW_VOL_INSTANCE *FvInstance;
  507. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  508. FvInstance = GetFvbInstance(FvbDevice->Instance);
  509. if (FvInstance != NULL) {
  510. *Address = FvInstance->FvBase;
  511. }
  512. return EFI_SUCCESS;
  513. }
  514. /**
  515. Retrieve the size of a logical block.
  516. @param[in] This Calling context.
  517. @param[in] Lba Indicates which block to return the size for.
  518. @param[out] BlockSize A pointer to a caller allocated UINTN in which
  519. the size of the block is returned.
  520. @param[out] NumOfBlocks A pointer to a caller allocated UINTN in which the
  521. number of consecutive blocks starting with Lba is
  522. returned. All blocks in this range have a size of
  523. BlockSize.
  524. @retval EFI_SUCCESS The function always return successfully.
  525. **/
  526. EFI_STATUS
  527. EFIAPI
  528. FvbProtocolGetBlockSize (
  529. IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
  530. IN EFI_LBA Lba,
  531. OUT UINTN *BlockSize,
  532. OUT UINTN *NumOfBlocks
  533. )
  534. {
  535. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  536. DEBUG((EFI_D_INFO,
  537. "FvbProtocolGetBlockSize: Lba: 0x%lx BlockSize: 0x%x NumOfBlocks: 0x%x\n",
  538. Lba,
  539. BlockSize,
  540. NumOfBlocks)
  541. );
  542. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  543. return FvbGetLbaAddress (
  544. FvbDevice->Instance,
  545. Lba,
  546. NULL,
  547. BlockSize,
  548. NumOfBlocks
  549. );
  550. }
  551. /**
  552. Retrieves Volume attributes. No polarity translations are done.
  553. @param[in] This Calling context.
  554. @param[out] Attributes Output buffer which contains attributes.
  555. @retval EFI_SUCCESS The function always return successfully.
  556. **/
  557. EFI_STATUS
  558. EFIAPI
  559. FvbProtocolGetAttributes (
  560. IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
  561. OUT EFI_FVB_ATTRIBUTES_2 *Attributes
  562. )
  563. {
  564. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  565. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  566. *Attributes = FvbGetVolumeAttributes (FvbDevice->Instance);
  567. DEBUG ((EFI_D_INFO,
  568. "FvbProtocolGetAttributes: This: 0x%x Attributes: 0x%x\n",
  569. This,
  570. *Attributes)
  571. );
  572. return EFI_SUCCESS;
  573. }
  574. /**
  575. Sets Volume attributes. No polarity translations are done.
  576. @param[in] This Calling context.
  577. @param[out] Attributes Output buffer which contains attributes.
  578. @retval EFI_SUCCESS The function always return successfully.
  579. **/
  580. EFI_STATUS
  581. EFIAPI
  582. FvbProtocolSetAttributes (
  583. IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
  584. IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
  585. )
  586. {
  587. EFI_STATUS Status;
  588. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  589. DEBUG((EFI_D_INFO,
  590. "FvbProtocolSetAttributes: Before SET - This: 0x%x Attributes: 0x%x\n",
  591. This,
  592. *Attributes)
  593. );
  594. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  595. Status = FvbSetVolumeAttributes (FvbDevice->Instance, Attributes);
  596. DEBUG((EFI_D_INFO,
  597. "FvbProtocolSetAttributes: After SET - This: 0x%x Attributes: 0x%x\n",
  598. This,
  599. *Attributes)
  600. );
  601. return Status;
  602. }
  603. /**
  604. The EraseBlock() function erases one or more blocks as denoted by the
  605. variable argument list. The entire parameter list of blocks must be verified
  606. prior to erasing any blocks. If a block is requested that does not exist
  607. within the associated firmware volume (it has a larger index than the last
  608. block of the firmware volume), the EraseBlock() function must return
  609. EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.
  610. @param[in] This Calling context.
  611. @param[in] ... Starting LBA followed by Number of Lba to erase.
  612. a -1 to terminate the list.
  613. @retval EFI_SUCCESS The erase request was successfully completed.
  614. @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
  615. @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
  616. could not be written. Firmware device may have been
  617. partially erased.
  618. **/
  619. EFI_STATUS
  620. EFIAPI
  621. FvbProtocolEraseBlocks (
  622. IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
  623. ...
  624. )
  625. {
  626. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  627. EFI_FW_VOL_INSTANCE *FwhInstance;
  628. UINTN NumOfBlocks = 0;
  629. VA_LIST args;
  630. EFI_LBA StartingLba;
  631. UINTN NumOfLba;
  632. EFI_STATUS Status;
  633. DEBUG((EFI_D_INFO, "FvbProtocolEraseBlocks: \n"));
  634. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  635. FwhInstance = GetFvbInstance (FvbDevice->Instance);
  636. if (FwhInstance != NULL) {
  637. NumOfBlocks = FwhInstance->NumOfBlocks;
  638. }
  639. VA_START (args, This);
  640. do {
  641. StartingLba = VA_ARG (args, EFI_LBA);
  642. if ( StartingLba == EFI_LBA_LIST_TERMINATOR ) {
  643. break;
  644. }
  645. NumOfLba = VA_ARG (args, UINTN);
  646. //
  647. // Check input parameters.
  648. //
  649. if (NumOfLba == 0) {
  650. VA_END (args);
  651. return EFI_INVALID_PARAMETER;
  652. }
  653. if ( ( StartingLba + NumOfLba ) > NumOfBlocks ) {
  654. return EFI_INVALID_PARAMETER;
  655. }
  656. } while ( 1 );
  657. VA_END (args);
  658. VA_START (args, This);
  659. do {
  660. StartingLba = VA_ARG (args, EFI_LBA);
  661. if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
  662. break;
  663. }
  664. NumOfLba = VA_ARG (args, UINTN);
  665. while ( NumOfLba > 0 ) {
  666. Status = FvbEraseBlock (FvbDevice->Instance, StartingLba);
  667. if ( EFI_ERROR(Status)) {
  668. VA_END (args);
  669. return Status;
  670. }
  671. StartingLba ++;
  672. NumOfLba --;
  673. }
  674. } while ( 1 );
  675. VA_END (args);
  676. return EFI_SUCCESS;
  677. }
  678. /**
  679. Writes data beginning at Lba:Offset from FV. The write terminates either
  680. when *NumBytes of data have been written, or when a block boundary is
  681. reached. *NumBytes is updated to reflect the actual number of bytes
  682. written. The write opertion does not include erase. This routine will
  683. attempt to write only the specified bytes. If the writes do not stick,
  684. it will return an error.
  685. @param[in] This Calling context.
  686. @param[in] Lba Block in which to begin write.
  687. @param[in] Offset Offset in the block at which to begin write.
  688. @param[in,out] NumBytes On input, indicates the requested write size. On
  689. output, indicates the actual number of bytes written
  690. @param[in] Buffer Buffer containing source data for the write.
  691. @retval EFI_SUCCESS The firmware volume was written successfully.
  692. @retval EFI_BAD_BUFFER_SIZE Write attempted across a LBA boundary. On output,
  693. NumBytes contains the total number of bytes
  694. actually written.
  695. @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
  696. @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
  697. could not be written.
  698. @retval EFI_INVALID_PARAMETER NumBytes or Buffer are NULL.
  699. **/
  700. EFI_STATUS
  701. EFIAPI
  702. FvbProtocolWrite (
  703. IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
  704. IN EFI_LBA Lba,
  705. IN UINTN Offset,
  706. IN OUT UINTN *NumBytes,
  707. IN UINT8 *Buffer
  708. )
  709. {
  710. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  711. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  712. DEBUG((EFI_D_INFO,
  713. "FvbProtocolWrite: Lba: 0x%lx Offset: 0x%x NumBytes: 0x%x, Buffer: 0x%x\n",
  714. Lba,
  715. Offset,
  716. *NumBytes,
  717. Buffer)
  718. );
  719. return FvbWriteBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer);
  720. }
  721. /**
  722. Reads data beginning at Lba:Offset from FV. The Read terminates either
  723. when *NumBytes of data have been read, or when a block boundary is
  724. reached. *NumBytes is updated to reflect the actual number of bytes
  725. written. The write opertion does not include erase. This routine will
  726. attempt to write only the specified bytes. If the writes do not stick,
  727. it will return an error.
  728. @param[in] This Calling context.
  729. @param[in] Lba Block in which to begin write.
  730. @param[in] Offset Offset in the block at which to begin write
  731. @param[in,out] NumBytes On input, indicates the requested write size. On
  732. output, indicates the actual number of bytes written.
  733. @param[in] Buffer Buffer containing source data for the write.
  734. Returns:
  735. @retval EFI_SUCCESS The firmware volume was read successfully and
  736. contents are in Buffer.
  737. @retval EFI_BAD_BUFFER_SIZE Read attempted across a LBA boundary. On output,
  738. NumBytes contains the total number of bytes returned
  739. in Buffer.
  740. @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state
  741. @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
  742. could not be read.
  743. @retval EFI_INVALID_PARAMETER NumBytes or Buffer are NULL.
  744. **/
  745. EFI_STATUS
  746. EFIAPI
  747. FvbProtocolRead (
  748. IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
  749. IN EFI_LBA Lba,
  750. IN UINTN Offset,
  751. IN OUT UINTN *NumBytes,
  752. OUT UINT8 *Buffer
  753. )
  754. {
  755. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  756. EFI_STATUS Status;
  757. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  758. Status = FvbReadBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer);
  759. DEBUG((EFI_D_INFO,
  760. "FvbProtocolRead: Lba: 0x%lx Offset: 0x%x NumBytes: 0x%x, Buffer: 0x%x\n",
  761. Lba,
  762. Offset,
  763. *NumBytes,
  764. Buffer)
  765. );
  766. return Status;
  767. }
  768. /**
  769. Check the integrity of firmware volume header.
  770. @param[in] FwVolHeader A pointer to a firmware volume header.
  771. @retval TRUE The firmware volume is consistent.
  772. @retval FALSE The firmware volume has corrupted.
  773. **/
  774. BOOLEAN
  775. IsFvHeaderValid (
  776. IN EFI_PHYSICAL_ADDRESS FvBase,
  777. IN CONST EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader
  778. )
  779. {
  780. if (FvBase == PcdGet32(PcdFlashNvStorageVariableBase)) {
  781. if (CompareMem (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid, sizeof(EFI_GUID)) != 0 ) {
  782. return FALSE;
  783. }
  784. } else {
  785. if (CompareMem (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem2Guid, sizeof(EFI_GUID)) != 0 ) {
  786. return FALSE;
  787. }
  788. }
  789. if ( (FwVolHeader->Revision != EFI_FVH_REVISION) ||
  790. (FwVolHeader->Signature != EFI_FVH_SIGNATURE) ||
  791. (FwVolHeader->FvLength == ((UINTN) -1)) ||
  792. ((FwVolHeader->HeaderLength & 0x01 ) !=0) ) {
  793. return FALSE;
  794. }
  795. if (CalculateCheckSum16 ((UINT16 *) FwVolHeader, FwVolHeader->HeaderLength) != 0) {
  796. return FALSE;
  797. }
  798. return TRUE;
  799. }
  800. /**
  801. The function does the necessary initialization work for
  802. Firmware Volume Block Driver.
  803. @retval EFI_SUCCESS This funtion always return EFI_SUCCESS.
  804. It will ASSERT on errors.
  805. **/
  806. EFI_STATUS
  807. FvbInitialize (
  808. VOID
  809. )
  810. {
  811. EFI_FW_VOL_INSTANCE *FwhInstance;
  812. EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
  813. EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
  814. EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;
  815. EFI_PHYSICAL_ADDRESS BaseAddress;
  816. EFI_STATUS Status;
  817. UINTN BufferSize;
  818. UINTN TmpHeaderLength;
  819. UINTN Idx;
  820. UINT32 MaxLbaSize;
  821. //
  822. // Calculate the total size for all firmware volume block instances.
  823. //
  824. BufferSize = 0;
  825. for (Idx = 0; Idx < 1; Idx++) {
  826. FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) mPlatformFvBaseAddress[Idx];
  827. BufferSize += (FvHeader->HeaderLength +
  828. sizeof (EFI_FW_VOL_INSTANCE) -
  829. sizeof (EFI_FIRMWARE_VOLUME_HEADER)
  830. );
  831. }
  832. mFvbModuleGlobal.FvInstance = (EFI_FW_VOL_INSTANCE *) AllocateRuntimeZeroPool (BufferSize);
  833. ASSERT (NULL != mFvbModuleGlobal.FvInstance);
  834. MaxLbaSize = 0;
  835. FwhInstance = mFvbModuleGlobal.FvInstance;
  836. mFvbModuleGlobal.NumFv = 0;
  837. for (Idx = 0; Idx < 1; Idx++) {
  838. BaseAddress = mPlatformFvBaseAddress[Idx];
  839. FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) BaseAddress;
  840. if (!IsFvHeaderValid (BaseAddress, FwVolHeader)) {
  841. //
  842. // If not valid, get FvbInfo from the information carried in
  843. // FVB driver.
  844. //
  845. DEBUG ((EFI_D_ERROR, "Fvb: FV header @ 0x%lx invalid\n", BaseAddress));
  846. Status = GetFvbInfo (BaseAddress, &FwVolHeader);
  847. ASSERT_EFI_ERROR(Status);
  848. //
  849. // Write back a healthy FV header.
  850. //
  851. DEBUG ((EFI_D_ERROR, "FwBlockService.c: Writing back healthy FV header\n"));
  852. LibFvbFlashDeviceBlockLock ((UINTN)BaseAddress, FwVolHeader->BlockMap->Length, FALSE);
  853. Status = LibFvbFlashDeviceBlockErase ((UINTN)BaseAddress, FwVolHeader->BlockMap->Length);
  854. TmpHeaderLength = (UINTN) FwVolHeader->HeaderLength;
  855. Status = LibFvbFlashDeviceWrite (
  856. (UINTN)BaseAddress,
  857. &TmpHeaderLength,
  858. (UINT8 *) FwVolHeader
  859. );
  860. LibFvbFlashDeviceBlockLock ((UINTN)BaseAddress, FwVolHeader->BlockMap->Length, TRUE);
  861. WriteBackInvalidateDataCacheRange (
  862. (VOID *) (UINTN) BaseAddress,
  863. FwVolHeader->BlockMap->Length
  864. );
  865. }
  866. CopyMem (&(FwhInstance->VolumeHeader), FwVolHeader, FwVolHeader->HeaderLength);
  867. FwVolHeader = &(FwhInstance->VolumeHeader);
  868. FwhInstance->FvBase = (UINTN)BaseAddress;
  869. //
  870. // Process the block map for each FV.
  871. //
  872. FwhInstance->NumOfBlocks = 0;
  873. for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {
  874. //
  875. // Get the maximum size of a block.
  876. //
  877. if (MaxLbaSize < PtrBlockMapEntry->Length) {
  878. MaxLbaSize = PtrBlockMapEntry->Length;
  879. }
  880. FwhInstance->NumOfBlocks += PtrBlockMapEntry->NumBlocks;
  881. }
  882. //
  883. // Add a FVB Protocol Instance.
  884. //
  885. mFvbModuleGlobal.NumFv++;
  886. InstallFvbProtocol (FwhInstance, mFvbModuleGlobal.NumFv - 1);
  887. //
  888. // Move on to the next FwhInstance.
  889. //
  890. FwhInstance = (EFI_FW_VOL_INSTANCE *) ((UINTN)((UINT8 *)FwhInstance) +
  891. FwVolHeader->HeaderLength +
  892. (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER)));
  893. }
  894. return EFI_SUCCESS;
  895. }