FWBlockService.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. /*++ @file
  2. Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
  3. Portions copyright (c) 2011, Apple Inc. All rights reserved.
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "PiDxe.h"
  7. #include <Guid/EventGroup.h>
  8. #include <Protocol/FirmwareVolumeBlock.h>
  9. #include <Protocol/DevicePath.h>
  10. #include <Library/UefiLib.h>
  11. #include <Library/UefiDriverEntryPoint.h>
  12. #include <Library/BaseLib.h>
  13. #include <Library/DxeServicesTableLib.h>
  14. #include <Library/UefiRuntimeLib.h>
  15. #include <Library/DebugLib.h>
  16. #include <Library/HobLib.h>
  17. #include <Library/BaseMemoryLib.h>
  18. #include <Library/MemoryAllocationLib.h>
  19. #include <Library/UefiBootServicesTableLib.h>
  20. #include <Library/DevicePathLib.h>
  21. #include "FwBlockService.h"
  22. ESAL_FWB_GLOBAL *mFvbModuleGlobal;
  23. #define EFI_FVB2_STATUS (EFI_FVB2_READ_STATUS | EFI_FVB2_WRITE_STATUS | EFI_FVB2_LOCK_STATUS)
  24. EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate = {
  25. FVB_DEVICE_SIGNATURE,
  26. {
  27. {
  28. {
  29. HARDWARE_DEVICE_PATH,
  30. HW_MEMMAP_DP,
  31. {
  32. sizeof (MEMMAP_DEVICE_PATH),
  33. 0
  34. }
  35. },
  36. EfiMemoryMappedIO,
  37. 0,
  38. 0,
  39. },
  40. {
  41. END_DEVICE_PATH_TYPE,
  42. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  43. {
  44. sizeof (EFI_DEVICE_PATH_PROTOCOL),
  45. 0
  46. }
  47. }
  48. },
  49. 0,
  50. {
  51. FvbProtocolGetAttributes,
  52. FvbProtocolSetAttributes,
  53. FvbProtocolGetPhysicalAddress,
  54. FvbProtocolGetBlockSize,
  55. FvbProtocolRead,
  56. FvbProtocolWrite,
  57. FvbProtocolEraseBlocks,
  58. NULL
  59. }
  60. };
  61. VOID
  62. EFIAPI
  63. FvbVirtualddressChangeEvent (
  64. IN EFI_EVENT Event,
  65. IN VOID *Context
  66. )
  67. /*++
  68. Routine Description:
  69. Fixup internal data so that EFI and SAL can be call in virtual mode.
  70. Call the passed in Child Notify event and convert the mFvbModuleGlobal
  71. date items to there virtual address.
  72. mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] - Physical copy of instance data
  73. mFvbModuleGlobal->FvInstance[FVB_VIRTUAL] - Virtual pointer to common
  74. instance data.
  75. Arguments:
  76. (Standard EFI notify event - EFI_EVENT_NOTIFY)
  77. Returns:
  78. None
  79. **/
  80. {
  81. EFI_FW_VOL_INSTANCE *FwhInstance;
  82. UINTN Index;
  83. EfiConvertPointer (0x0, (VOID **)&mFvbModuleGlobal->FvInstance[FVB_VIRTUAL]);
  84. //
  85. // Convert the base address of all the instances
  86. //
  87. Index = 0;
  88. FwhInstance = mFvbModuleGlobal->FvInstance[FVB_PHYSICAL];
  89. while (Index < mFvbModuleGlobal->NumFv) {
  90. EfiConvertPointer (0x0, (VOID **)&FwhInstance->FvBase[FVB_VIRTUAL]);
  91. FwhInstance = (EFI_FW_VOL_INSTANCE *)
  92. (
  93. (UINTN)((UINT8 *)FwhInstance) + FwhInstance->VolumeHeader.HeaderLength +
  94. (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))
  95. );
  96. Index++;
  97. }
  98. EfiConvertPointer (0x0, (VOID **)&mFvbModuleGlobal);
  99. }
  100. EFI_STATUS
  101. GetFvbInstance (
  102. IN UINTN Instance,
  103. IN ESAL_FWB_GLOBAL *Global,
  104. OUT EFI_FW_VOL_INSTANCE **FwhInstance,
  105. IN BOOLEAN Virtual
  106. )
  107. /*++
  108. Routine Description:
  109. Retrieves the physical address of a memory mapped FV
  110. Arguments:
  111. Instance - The FV instance whose base address is going to be
  112. returned
  113. Global - Pointer to ESAL_FWB_GLOBAL that contains all
  114. instance data
  115. FwhInstance - The EFI_FW_VOL_INSTANCE fimrware instance structure
  116. Virtual - Whether CPU is in virtual or physical mode
  117. Returns:
  118. EFI_SUCCESS - Successfully returns
  119. EFI_INVALID_PARAMETER - Instance not found
  120. **/
  121. {
  122. EFI_FW_VOL_INSTANCE *FwhRecord;
  123. if (Instance >= Global->NumFv) {
  124. return EFI_INVALID_PARAMETER;
  125. }
  126. //
  127. // Find the right instance of the FVB private data
  128. //
  129. FwhRecord = Global->FvInstance[Virtual];
  130. while (Instance > 0) {
  131. FwhRecord = (EFI_FW_VOL_INSTANCE *)
  132. (
  133. (UINTN)((UINT8 *)FwhRecord) + FwhRecord->VolumeHeader.HeaderLength +
  134. (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))
  135. );
  136. Instance--;
  137. }
  138. *FwhInstance = FwhRecord;
  139. return EFI_SUCCESS;
  140. }
  141. EFI_STATUS
  142. FvbGetPhysicalAddress (
  143. IN UINTN Instance,
  144. OUT EFI_PHYSICAL_ADDRESS *Address,
  145. IN ESAL_FWB_GLOBAL *Global,
  146. IN BOOLEAN Virtual
  147. )
  148. /*++
  149. Routine Description:
  150. Retrieves the physical address of a memory mapped FV
  151. Arguments:
  152. Instance - The FV instance whose base address is going to be
  153. returned
  154. Address - Pointer to a caller allocated EFI_PHYSICAL_ADDRESS
  155. that on successful return, contains the base address
  156. of the firmware volume.
  157. Global - Pointer to ESAL_FWB_GLOBAL that contains all
  158. instance data
  159. Virtual - Whether CPU is in virtual or physical mode
  160. Returns:
  161. EFI_SUCCESS - Successfully returns
  162. EFI_INVALID_PARAMETER - Instance not found
  163. **/
  164. {
  165. EFI_FW_VOL_INSTANCE *FwhInstance = NULL;
  166. EFI_STATUS Status;
  167. //
  168. // Find the right instance of the FVB private data
  169. //
  170. Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);
  171. ASSERT_EFI_ERROR (Status);
  172. *Address = FwhInstance->FvBase[Virtual];
  173. return EFI_SUCCESS;
  174. }
  175. EFI_STATUS
  176. FvbGetVolumeAttributes (
  177. IN UINTN Instance,
  178. OUT EFI_FVB_ATTRIBUTES_2 *Attributes,
  179. IN ESAL_FWB_GLOBAL *Global,
  180. IN BOOLEAN Virtual
  181. )
  182. /*++
  183. Routine Description:
  184. Retrieves attributes, insures positive polarity of attribute bits, returns
  185. resulting attributes in output parameter
  186. Arguments:
  187. Instance - The FV instance whose attributes is going to be
  188. returned
  189. Attributes - Output buffer which contains attributes
  190. Global - Pointer to ESAL_FWB_GLOBAL that contains all
  191. instance data
  192. Virtual - Whether CPU is in virtual or physical mode
  193. Returns:
  194. EFI_SUCCESS - Successfully returns
  195. EFI_INVALID_PARAMETER - Instance not found
  196. **/
  197. {
  198. EFI_FW_VOL_INSTANCE *FwhInstance = NULL;
  199. EFI_STATUS Status;
  200. //
  201. // Find the right instance of the FVB private data
  202. //
  203. Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);
  204. ASSERT_EFI_ERROR (Status);
  205. *Attributes = FwhInstance->VolumeHeader.Attributes;
  206. return EFI_SUCCESS;
  207. }
  208. EFI_STATUS
  209. FvbGetLbaAddress (
  210. IN UINTN Instance,
  211. IN EFI_LBA Lba,
  212. OUT UINTN *LbaAddress,
  213. OUT UINTN *LbaLength,
  214. OUT UINTN *NumOfBlocks,
  215. IN ESAL_FWB_GLOBAL *Global,
  216. IN BOOLEAN Virtual
  217. )
  218. /*++
  219. Routine Description:
  220. Retrieves the starting address of an LBA in an FV
  221. Arguments:
  222. Instance - The FV instance which the Lba belongs to
  223. Lba - The logical block address
  224. LbaAddress - On output, contains the physical starting address
  225. of the Lba
  226. LbaLength - On output, contains the length of the block
  227. NumOfBlocks - A pointer to a caller allocated UINTN in which the
  228. number of consecutive blocks starting with Lba is
  229. returned. All blocks in this range have a size of
  230. BlockSize
  231. Global - Pointer to ESAL_FWB_GLOBAL that contains all
  232. instance data
  233. Virtual - Whether CPU is in virtual or physical mode
  234. Returns:
  235. EFI_SUCCESS - Successfully returns
  236. EFI_INVALID_PARAMETER - Instance not found
  237. **/
  238. {
  239. UINT32 NumBlocks;
  240. UINT32 BlockLength;
  241. UINTN Offset;
  242. EFI_LBA StartLba;
  243. EFI_LBA NextLba;
  244. EFI_FW_VOL_INSTANCE *FwhInstance = NULL;
  245. EFI_FV_BLOCK_MAP_ENTRY *BlockMap;
  246. EFI_STATUS Status;
  247. //
  248. // Find the right instance of the FVB private data
  249. //
  250. Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);
  251. ASSERT_EFI_ERROR (Status);
  252. StartLba = 0;
  253. Offset = 0;
  254. BlockMap = &(FwhInstance->VolumeHeader.BlockMap[0]);
  255. //
  256. // Parse the blockmap of the FV to find which map entry the Lba belongs to
  257. //
  258. while (TRUE) {
  259. NumBlocks = BlockMap->NumBlocks;
  260. BlockLength = BlockMap->Length;
  261. if ((NumBlocks == 0) || (BlockLength == 0)) {
  262. return EFI_INVALID_PARAMETER;
  263. }
  264. NextLba = StartLba + NumBlocks;
  265. //
  266. // The map entry found
  267. //
  268. if ((Lba >= StartLba) && (Lba < NextLba)) {
  269. Offset = Offset + (UINTN)MultU64x32 ((Lba - StartLba), BlockLength);
  270. if (LbaAddress != NULL) {
  271. *LbaAddress = FwhInstance->FvBase[Virtual] + Offset;
  272. }
  273. if (LbaLength != NULL) {
  274. *LbaLength = BlockLength;
  275. }
  276. if (NumOfBlocks != NULL) {
  277. *NumOfBlocks = (UINTN)(NextLba - Lba);
  278. }
  279. return EFI_SUCCESS;
  280. }
  281. StartLba = NextLba;
  282. Offset = Offset + NumBlocks * BlockLength;
  283. BlockMap++;
  284. }
  285. }
  286. EFI_STATUS
  287. FvbReadBlock (
  288. IN UINTN Instance,
  289. IN EFI_LBA Lba,
  290. IN UINTN BlockOffset,
  291. IN OUT UINTN *NumBytes,
  292. IN UINT8 *Buffer,
  293. IN ESAL_FWB_GLOBAL *Global,
  294. IN BOOLEAN Virtual
  295. )
  296. /*++
  297. Routine Description:
  298. Reads specified number of bytes into a buffer from the specified block
  299. Arguments:
  300. Instance - The FV instance to be read from
  301. Lba - The logical block address to be read from
  302. BlockOffset - Offset into the block at which to begin reading
  303. NumBytes - Pointer that on input contains the total size of
  304. the buffer. On output, it contains the total number
  305. of bytes read
  306. Buffer - Pointer to a caller allocated buffer that will be
  307. used to hold the data read
  308. Global - Pointer to ESAL_FWB_GLOBAL that contains all
  309. instance data
  310. Virtual - Whether CPU is in virtual or physical mode
  311. Returns:
  312. EFI_SUCCESS - The firmware volume was read successfully and
  313. contents are in Buffer
  314. EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,
  315. NumBytes contains the total number of bytes returned
  316. in Buffer
  317. EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state
  318. EFI_DEVICE_ERROR - The block device is not functioning correctly and
  319. could not be read
  320. EFI_INVALID_PARAMETER - Instance not found, or NumBytes, Buffer are NULL
  321. **/
  322. {
  323. EFI_FVB_ATTRIBUTES_2 Attributes;
  324. UINTN LbaAddress;
  325. UINTN LbaLength;
  326. EFI_STATUS Status;
  327. //
  328. // Check for invalid conditions
  329. //
  330. if ((NumBytes == NULL) || (Buffer == NULL)) {
  331. return EFI_INVALID_PARAMETER;
  332. }
  333. if (*NumBytes == 0) {
  334. return EFI_INVALID_PARAMETER;
  335. }
  336. Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL, Global, Virtual);
  337. if (EFI_ERROR (Status)) {
  338. return Status;
  339. }
  340. //
  341. // Check if the FV is read enabled
  342. //
  343. FvbGetVolumeAttributes (Instance, &Attributes, Global, Virtual);
  344. if ((Attributes & EFI_FVB2_READ_STATUS) == 0) {
  345. return EFI_ACCESS_DENIED;
  346. }
  347. //
  348. // Perform boundary checks and adjust NumBytes
  349. //
  350. if (BlockOffset > LbaLength) {
  351. return EFI_INVALID_PARAMETER;
  352. }
  353. if (LbaLength < (*NumBytes + BlockOffset)) {
  354. *NumBytes = (UINT32)(LbaLength - BlockOffset);
  355. Status = EFI_BAD_BUFFER_SIZE;
  356. }
  357. CopyMem (Buffer, (UINT8 *)(LbaAddress + BlockOffset), (UINTN)(*NumBytes));
  358. return Status;
  359. }
  360. EFI_STATUS
  361. FvbWriteBlock (
  362. IN UINTN Instance,
  363. IN EFI_LBA Lba,
  364. IN UINTN BlockOffset,
  365. IN OUT UINTN *NumBytes,
  366. IN UINT8 *Buffer,
  367. IN ESAL_FWB_GLOBAL *Global,
  368. IN BOOLEAN Virtual
  369. )
  370. /*++
  371. Routine Description:
  372. Writes specified number of bytes from the input buffer to the block
  373. Arguments:
  374. Instance - The FV instance to be written to
  375. Lba - The starting logical block index to write to
  376. BlockOffset - Offset into the block at which to begin writing
  377. NumBytes - Pointer that on input contains the total size of
  378. the buffer. On output, it contains the total number
  379. of bytes actually written
  380. Buffer - Pointer to a caller allocated buffer that contains
  381. the source for the write
  382. Global - Pointer to ESAL_FWB_GLOBAL that contains all
  383. instance data
  384. Virtual - Whether CPU is in virtual or physical mode
  385. Returns:
  386. EFI_SUCCESS - The firmware volume was written successfully
  387. EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,
  388. NumBytes contains the total number of bytes
  389. actually written
  390. EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
  391. EFI_DEVICE_ERROR - The block device is not functioning correctly and
  392. could not be written
  393. EFI_INVALID_PARAMETER - Instance not found, or NumBytes, Buffer are NULL
  394. **/
  395. {
  396. EFI_FVB_ATTRIBUTES_2 Attributes;
  397. UINTN LbaAddress;
  398. UINTN LbaLength;
  399. EFI_STATUS Status;
  400. //
  401. // Check for invalid conditions
  402. //
  403. if ((NumBytes == NULL) || (Buffer == NULL)) {
  404. return EFI_INVALID_PARAMETER;
  405. }
  406. if (*NumBytes == 0) {
  407. return EFI_INVALID_PARAMETER;
  408. }
  409. Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL, Global, Virtual);
  410. if (EFI_ERROR (Status)) {
  411. return Status;
  412. }
  413. //
  414. // Check if the FV is write enabled
  415. //
  416. FvbGetVolumeAttributes (Instance, &Attributes, Global, Virtual);
  417. if ((Attributes & EFI_FVB2_WRITE_STATUS) == 0) {
  418. return EFI_ACCESS_DENIED;
  419. }
  420. //
  421. // Perform boundary checks and adjust NumBytes
  422. //
  423. if (BlockOffset > LbaLength) {
  424. return EFI_INVALID_PARAMETER;
  425. }
  426. if (LbaLength < (*NumBytes + BlockOffset)) {
  427. *NumBytes = (UINT32)(LbaLength - BlockOffset);
  428. Status = EFI_BAD_BUFFER_SIZE;
  429. }
  430. //
  431. // Write data
  432. //
  433. CopyMem ((UINT8 *)(LbaAddress + BlockOffset), Buffer, (UINTN)(*NumBytes));
  434. return Status;
  435. }
  436. EFI_STATUS
  437. FvbEraseBlock (
  438. IN UINTN Instance,
  439. IN EFI_LBA Lba,
  440. IN ESAL_FWB_GLOBAL *Global,
  441. IN BOOLEAN Virtual
  442. )
  443. /*++
  444. Routine Description:
  445. Erases and initializes a firmware volume block
  446. Arguments:
  447. Instance - The FV instance to be erased
  448. Lba - The logical block index to be erased
  449. Global - Pointer to ESAL_FWB_GLOBAL that contains all
  450. instance data
  451. Virtual - Whether CPU is in virtual or physical mode
  452. Returns:
  453. EFI_SUCCESS - The erase request was successfully completed
  454. EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
  455. EFI_DEVICE_ERROR - The block device is not functioning correctly and
  456. could not be written. Firmware device may have been
  457. partially erased
  458. EFI_INVALID_PARAMETER - Instance not found
  459. **/
  460. {
  461. EFI_FVB_ATTRIBUTES_2 Attributes;
  462. UINTN LbaAddress;
  463. UINTN LbaLength;
  464. EFI_STATUS Status;
  465. UINT8 Data;
  466. //
  467. // Check if the FV is write enabled
  468. //
  469. FvbGetVolumeAttributes (Instance, &Attributes, Global, Virtual);
  470. if ((Attributes & EFI_FVB2_WRITE_STATUS) == 0) {
  471. return EFI_ACCESS_DENIED;
  472. }
  473. //
  474. // Get the starting address of the block for erase.
  475. //
  476. Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL, Global, Virtual);
  477. if (EFI_ERROR (Status)) {
  478. return Status;
  479. }
  480. if ((Attributes & EFI_FVB2_ERASE_POLARITY) != 0) {
  481. Data = 0xFF;
  482. } else {
  483. Data = 0x0;
  484. }
  485. SetMem ((UINT8 *)LbaAddress, LbaLength, Data);
  486. return EFI_SUCCESS;
  487. }
  488. EFI_STATUS
  489. FvbSetVolumeAttributes (
  490. IN UINTN Instance,
  491. IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes,
  492. IN ESAL_FWB_GLOBAL *Global,
  493. IN BOOLEAN Virtual
  494. )
  495. /*++
  496. Routine Description:
  497. Modifies the current settings of the firmware volume according to the
  498. input parameter, and returns the new setting of the volume
  499. Arguments:
  500. Instance - The FV instance whose attributes is going to be
  501. modified
  502. Attributes - On input, it is a pointer to EFI_FVB_ATTRIBUTES_2
  503. containing the desired firmware volume settings.
  504. On successful return, it contains the new settings
  505. of the firmware volume
  506. Global - Pointer to ESAL_FWB_GLOBAL that contains all
  507. instance data
  508. Virtual - Whether CPU is in virtual or physical mode
  509. Returns:
  510. EFI_SUCCESS - Successfully returns
  511. EFI_ACCESS_DENIED - The volume setting is locked and cannot be modified
  512. EFI_INVALID_PARAMETER - Instance not found, or The attributes requested are
  513. in conflict with the capabilities as declared in the
  514. firmware volume header
  515. **/
  516. {
  517. EFI_FW_VOL_INSTANCE *FwhInstance = NULL;
  518. EFI_FVB_ATTRIBUTES_2 OldAttributes;
  519. EFI_FVB_ATTRIBUTES_2 *AttribPtr;
  520. UINT32 Capabilities;
  521. UINT32 OldStatus;
  522. UINT32 NewStatus;
  523. EFI_STATUS Status;
  524. EFI_FVB_ATTRIBUTES_2 UnchangedAttributes;
  525. //
  526. // Find the right instance of the FVB private data
  527. //
  528. Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);
  529. ASSERT_EFI_ERROR (Status);
  530. AttribPtr = (EFI_FVB_ATTRIBUTES_2 *)&(FwhInstance->VolumeHeader.Attributes);
  531. OldAttributes = *AttribPtr;
  532. Capabilities = OldAttributes & (EFI_FVB2_READ_DISABLED_CAP | \
  533. EFI_FVB2_READ_ENABLED_CAP | \
  534. EFI_FVB2_WRITE_DISABLED_CAP | \
  535. EFI_FVB2_WRITE_ENABLED_CAP | \
  536. EFI_FVB2_LOCK_CAP \
  537. );
  538. OldStatus = OldAttributes & EFI_FVB2_STATUS;
  539. NewStatus = *Attributes & EFI_FVB2_STATUS;
  540. UnchangedAttributes = EFI_FVB2_READ_DISABLED_CAP | \
  541. EFI_FVB2_READ_ENABLED_CAP | \
  542. EFI_FVB2_WRITE_DISABLED_CAP | \
  543. EFI_FVB2_WRITE_ENABLED_CAP | \
  544. EFI_FVB2_LOCK_CAP | \
  545. EFI_FVB2_STICKY_WRITE | \
  546. EFI_FVB2_MEMORY_MAPPED | \
  547. EFI_FVB2_ERASE_POLARITY | \
  548. EFI_FVB2_READ_LOCK_CAP | \
  549. EFI_FVB2_WRITE_LOCK_CAP | \
  550. EFI_FVB2_ALIGNMENT;
  551. //
  552. // Some attributes of FV is read only can *not* be set
  553. //
  554. if ((OldAttributes & UnchangedAttributes) ^ (*Attributes & UnchangedAttributes)) {
  555. return EFI_INVALID_PARAMETER;
  556. }
  557. //
  558. // If firmware volume is locked, no status bit can be updated
  559. //
  560. if (OldAttributes & EFI_FVB2_LOCK_STATUS) {
  561. if (OldStatus ^ NewStatus) {
  562. return EFI_ACCESS_DENIED;
  563. }
  564. }
  565. //
  566. // Test read disable
  567. //
  568. if ((Capabilities & EFI_FVB2_READ_DISABLED_CAP) == 0) {
  569. if ((NewStatus & EFI_FVB2_READ_STATUS) == 0) {
  570. return EFI_INVALID_PARAMETER;
  571. }
  572. }
  573. //
  574. // Test read enable
  575. //
  576. if ((Capabilities & EFI_FVB2_READ_ENABLED_CAP) == 0) {
  577. if (NewStatus & EFI_FVB2_READ_STATUS) {
  578. return EFI_INVALID_PARAMETER;
  579. }
  580. }
  581. //
  582. // Test write disable
  583. //
  584. if ((Capabilities & EFI_FVB2_WRITE_DISABLED_CAP) == 0) {
  585. if ((NewStatus & EFI_FVB2_WRITE_STATUS) == 0) {
  586. return EFI_INVALID_PARAMETER;
  587. }
  588. }
  589. //
  590. // Test write enable
  591. //
  592. if ((Capabilities & EFI_FVB2_WRITE_ENABLED_CAP) == 0) {
  593. if (NewStatus & EFI_FVB2_WRITE_STATUS) {
  594. return EFI_INVALID_PARAMETER;
  595. }
  596. }
  597. //
  598. // Test lock
  599. //
  600. if ((Capabilities & EFI_FVB2_LOCK_CAP) == 0) {
  601. if (NewStatus & EFI_FVB2_LOCK_STATUS) {
  602. return EFI_INVALID_PARAMETER;
  603. }
  604. }
  605. *AttribPtr = (*AttribPtr) & (0xFFFFFFFF & (~EFI_FVB2_STATUS));
  606. *AttribPtr = (*AttribPtr) | NewStatus;
  607. *Attributes = *AttribPtr;
  608. return EFI_SUCCESS;
  609. }
  610. //
  611. // FVB protocol APIs
  612. //
  613. EFI_STATUS
  614. EFIAPI
  615. FvbProtocolGetPhysicalAddress (
  616. IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
  617. OUT EFI_PHYSICAL_ADDRESS *Address
  618. )
  619. /*++
  620. Routine Description:
  621. Retrieves the physical address of the device.
  622. Arguments:
  623. This - Calling context
  624. Address - Output buffer containing the address.
  625. Returns:
  626. Returns:
  627. EFI_SUCCESS - Successfully returns
  628. **/
  629. {
  630. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  631. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  632. return FvbGetPhysicalAddress (FvbDevice->Instance, Address, mFvbModuleGlobal, EfiGoneVirtual ());
  633. }
  634. EFI_STATUS
  635. EFIAPI
  636. FvbProtocolGetBlockSize (
  637. IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
  638. IN EFI_LBA Lba,
  639. OUT UINTN *BlockSize,
  640. OUT UINTN *NumOfBlocks
  641. )
  642. /*++
  643. Routine Description:
  644. Retrieve the size of a logical block
  645. Arguments:
  646. This - Calling context
  647. Lba - Indicates which block to return the size for.
  648. BlockSize - A pointer to a caller allocated UINTN in which
  649. the size of the block is returned
  650. NumOfBlocks - a pointer to a caller allocated UINTN in which the
  651. number of consecutive blocks starting with Lba is
  652. returned. All blocks in this range have a size of
  653. BlockSize
  654. Returns:
  655. EFI_SUCCESS - The firmware volume was read successfully and
  656. contents are in Buffer
  657. **/
  658. {
  659. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  660. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  661. return FvbGetLbaAddress (
  662. FvbDevice->Instance,
  663. Lba,
  664. NULL,
  665. BlockSize,
  666. NumOfBlocks,
  667. mFvbModuleGlobal,
  668. EfiGoneVirtual ()
  669. );
  670. }
  671. EFI_STATUS
  672. EFIAPI
  673. FvbProtocolGetAttributes (
  674. IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
  675. OUT EFI_FVB_ATTRIBUTES_2 *Attributes
  676. )
  677. /*++
  678. Routine Description:
  679. Retrieves Volume attributes. No polarity translations are done.
  680. Arguments:
  681. This - Calling context
  682. Attributes - output buffer which contains attributes
  683. Returns:
  684. EFI_SUCCESS - Successfully returns
  685. **/
  686. {
  687. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  688. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  689. return FvbGetVolumeAttributes (FvbDevice->Instance, Attributes, mFvbModuleGlobal, EfiGoneVirtual ());
  690. }
  691. EFI_STATUS
  692. EFIAPI
  693. FvbProtocolSetAttributes (
  694. IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
  695. IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
  696. )
  697. /*++
  698. Routine Description:
  699. Sets Volume attributes. No polarity translations are done.
  700. Arguments:
  701. This - Calling context
  702. Attributes - output buffer which contains attributes
  703. Returns:
  704. EFI_SUCCESS - Successfully returns
  705. **/
  706. {
  707. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  708. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  709. return FvbSetVolumeAttributes (FvbDevice->Instance, Attributes, mFvbModuleGlobal, EfiGoneVirtual ());
  710. }
  711. EFI_STATUS
  712. EFIAPI
  713. FvbProtocolEraseBlocks (
  714. IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
  715. ...
  716. )
  717. /*++
  718. Routine Description:
  719. The EraseBlock() function erases one or more blocks as denoted by the
  720. variable argument list. The entire parameter list of blocks must be verified
  721. prior to erasing any blocks. If a block is requested that does not exist
  722. within the associated firmware volume (it has a larger index than the last
  723. block of the firmware volume), the EraseBlock() function must return
  724. EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.
  725. Arguments:
  726. This - Calling context
  727. ... - Starting LBA followed by Number of Lba to erase.
  728. a -1 to terminate the list.
  729. Returns:
  730. EFI_SUCCESS - The erase request was successfully completed
  731. EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
  732. EFI_DEVICE_ERROR - The block device is not functioning correctly and
  733. could not be written. Firmware device may have been
  734. partially erased
  735. **/
  736. {
  737. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  738. EFI_FW_VOL_INSTANCE *FwhInstance = NULL;
  739. UINTN NumOfBlocks;
  740. VA_LIST args;
  741. EFI_LBA StartingLba;
  742. UINTN NumOfLba;
  743. EFI_STATUS Status;
  744. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  745. Status = GetFvbInstance (FvbDevice->Instance, mFvbModuleGlobal, &FwhInstance, EfiGoneVirtual ());
  746. ASSERT_EFI_ERROR (Status);
  747. NumOfBlocks = FwhInstance->NumOfBlocks;
  748. VA_START (args, This);
  749. do {
  750. StartingLba = VA_ARG (args, EFI_LBA);
  751. if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
  752. break;
  753. }
  754. NumOfLba = VA_ARG (args, UINTN);
  755. //
  756. // Check input parameters
  757. //
  758. if ((NumOfLba == 0) || ((StartingLba + NumOfLba) > NumOfBlocks)) {
  759. VA_END (args);
  760. return EFI_INVALID_PARAMETER;
  761. }
  762. } while (1);
  763. VA_END (args);
  764. VA_START (args, This);
  765. do {
  766. StartingLba = VA_ARG (args, EFI_LBA);
  767. if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
  768. break;
  769. }
  770. NumOfLba = VA_ARG (args, UINTN);
  771. while (NumOfLba > 0) {
  772. Status = FvbEraseBlock (FvbDevice->Instance, StartingLba, mFvbModuleGlobal, EfiGoneVirtual ());
  773. if (EFI_ERROR (Status)) {
  774. VA_END (args);
  775. return Status;
  776. }
  777. StartingLba++;
  778. NumOfLba--;
  779. }
  780. } while (1);
  781. VA_END (args);
  782. return EFI_SUCCESS;
  783. }
  784. EFI_STATUS
  785. EFIAPI
  786. FvbProtocolWrite (
  787. IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
  788. IN EFI_LBA Lba,
  789. IN UINTN Offset,
  790. IN OUT UINTN *NumBytes,
  791. IN UINT8 *Buffer
  792. )
  793. /*++
  794. Routine Description:
  795. Writes data beginning at Lba:Offset from FV. The write terminates either
  796. when *NumBytes of data have been written, or when a block boundary is
  797. reached. *NumBytes is updated to reflect the actual number of bytes
  798. written. The write opertion does not include erase. This routine will
  799. attempt to write only the specified bytes. If the writes do not stick,
  800. it will return an error.
  801. Arguments:
  802. This - Calling context
  803. Lba - Block in which to begin write
  804. Offset - Offset in the block at which to begin write
  805. NumBytes - On input, indicates the requested write size. On
  806. output, indicates the actual number of bytes written
  807. Buffer - Buffer containing source data for the write.
  808. Returns:
  809. EFI_SUCCESS - The firmware volume was written successfully
  810. EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,
  811. NumBytes contains the total number of bytes
  812. actually written
  813. EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
  814. EFI_DEVICE_ERROR - The block device is not functioning correctly and
  815. could not be written
  816. EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL
  817. **/
  818. {
  819. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  820. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  821. return FvbWriteBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer, mFvbModuleGlobal, EfiGoneVirtual ());
  822. }
  823. EFI_STATUS
  824. EFIAPI
  825. FvbProtocolRead (
  826. IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
  827. IN EFI_LBA Lba,
  828. IN UINTN Offset,
  829. IN OUT UINTN *NumBytes,
  830. IN UINT8 *Buffer
  831. )
  832. /*++
  833. Routine Description:
  834. Reads data beginning at Lba:Offset from FV. The Read terminates either
  835. when *NumBytes of data have been read, or when a block boundary is
  836. reached. *NumBytes is updated to reflect the actual number of bytes
  837. written. The write opertion does not include erase. This routine will
  838. attempt to write only the specified bytes. If the writes do not stick,
  839. it will return an error.
  840. Arguments:
  841. This - Calling context
  842. Lba - Block in which to begin Read
  843. Offset - Offset in the block at which to begin Read
  844. NumBytes - On input, indicates the requested write size. On
  845. output, indicates the actual number of bytes Read
  846. Buffer - Buffer containing source data for the Read.
  847. Returns:
  848. EFI_SUCCESS - The firmware volume was read successfully and
  849. contents are in Buffer
  850. EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,
  851. NumBytes contains the total number of bytes returned
  852. in Buffer
  853. EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state
  854. EFI_DEVICE_ERROR - The block device is not functioning correctly and
  855. could not be read
  856. EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL
  857. **/
  858. {
  859. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  860. FvbDevice = FVB_DEVICE_FROM_THIS (This);
  861. return FvbReadBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer, mFvbModuleGlobal, EfiGoneVirtual ());
  862. }
  863. EFI_STATUS
  864. ValidateFvHeader (
  865. EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader
  866. )
  867. /*++
  868. Routine Description:
  869. Check the integrity of firmware volume header
  870. Arguments:
  871. FwVolHeader - A pointer to a firmware volume header
  872. Returns:
  873. EFI_SUCCESS - The firmware volume is consistent
  874. EFI_NOT_FOUND - The firmware volume has corrupted. So it is not an FV
  875. **/
  876. {
  877. UINT16 *Ptr;
  878. UINT16 HeaderLength;
  879. UINT16 Checksum;
  880. //
  881. // Verify the header revision, header signature, length
  882. // Length of FvBlock cannot be 2**64-1
  883. // HeaderLength cannot be an odd number
  884. //
  885. if ((FwVolHeader->Revision != EFI_FVH_REVISION) ||
  886. (FwVolHeader->Signature != EFI_FVH_SIGNATURE) ||
  887. (FwVolHeader->FvLength == ((UINTN)-1)) ||
  888. ((FwVolHeader->HeaderLength & 0x01) != 0)
  889. )
  890. {
  891. return EFI_NOT_FOUND;
  892. }
  893. //
  894. // Verify the header checksum
  895. //
  896. HeaderLength = (UINT16)(FwVolHeader->HeaderLength / 2);
  897. Ptr = (UINT16 *)FwVolHeader;
  898. Checksum = 0;
  899. while (HeaderLength > 0) {
  900. Checksum = Checksum + (*Ptr);
  901. HeaderLength--;
  902. Ptr++;
  903. }
  904. if (Checksum != 0) {
  905. return EFI_NOT_FOUND;
  906. }
  907. return EFI_SUCCESS;
  908. }
  909. EFI_STATUS
  910. EFIAPI
  911. FvbInitialize (
  912. IN EFI_HANDLE ImageHandle,
  913. IN EFI_SYSTEM_TABLE *SystemTable
  914. )
  915. /*++
  916. Routine Description:
  917. This function does common initialization for FVB services
  918. Arguments:
  919. Returns:
  920. **/
  921. {
  922. EFI_STATUS Status;
  923. EFI_FW_VOL_INSTANCE *FwhInstance = NULL;
  924. EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
  925. EFI_DXE_SERVICES *DxeServices;
  926. EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;
  927. UINT32 BufferSize;
  928. EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;
  929. EFI_HANDLE FwbHandle;
  930. EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
  931. EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *OldFwbInterface;
  932. EFI_DEVICE_PATH_PROTOCOL *TempFwbDevicePath;
  933. FV_DEVICE_PATH TempFvbDevicePathData;
  934. UINT32 MaxLbaSize;
  935. EFI_PHYSICAL_ADDRESS BaseAddress;
  936. UINT64 Length;
  937. UINTN NumOfBlocks;
  938. EFI_PEI_HOB_POINTERS FvHob;
  939. //
  940. // Get the DXE services table
  941. //
  942. DxeServices = gDS;
  943. //
  944. // Allocate runtime services data for global variable, which contains
  945. // the private data of all firmware volume block instances
  946. //
  947. Status = gBS->AllocatePool (
  948. EfiRuntimeServicesData,
  949. sizeof (ESAL_FWB_GLOBAL),
  950. (VOID **)&mFvbModuleGlobal
  951. );
  952. ASSERT_EFI_ERROR (Status);
  953. //
  954. // Calculate the total size for all firmware volume block instances
  955. //
  956. BufferSize = 0;
  957. FvHob.Raw = GetHobList ();
  958. while ((FvHob.Raw = GetNextHob (EFI_HOB_TYPE_FV, FvHob.Raw)) != NULL) {
  959. BaseAddress = FvHob.FirmwareVolume->BaseAddress;
  960. Length = FvHob.FirmwareVolume->Length;
  961. //
  962. // Check if it is a "real" flash
  963. //
  964. Status = DxeServices->GetMemorySpaceDescriptor (
  965. BaseAddress,
  966. &Descriptor
  967. );
  968. if (EFI_ERROR (Status)) {
  969. break;
  970. }
  971. if (Descriptor.GcdMemoryType != EfiGcdMemoryTypeMemoryMappedIo) {
  972. FvHob.Raw = GET_NEXT_HOB (FvHob);
  973. continue;
  974. }
  975. FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)BaseAddress;
  976. Status = ValidateFvHeader (FwVolHeader);
  977. if (EFI_ERROR (Status)) {
  978. //
  979. // Get FvbInfo
  980. //
  981. Status = GetFvbInfo (Length, &FwVolHeader);
  982. if (EFI_ERROR (Status)) {
  983. FvHob.Raw = GET_NEXT_HOB (FvHob);
  984. continue;
  985. }
  986. }
  987. BufferSize += (sizeof (EFI_FW_VOL_INSTANCE) + FwVolHeader->HeaderLength - sizeof (EFI_FIRMWARE_VOLUME_HEADER));
  988. FvHob.Raw = GET_NEXT_HOB (FvHob);
  989. }
  990. //
  991. // Only need to allocate once. There is only one copy of physical memory for
  992. // the private data of each FV instance. But in virtual mode or in physical
  993. // mode, the address of the the physical memory may be different.
  994. //
  995. Status = gBS->AllocatePool (
  996. EfiRuntimeServicesData,
  997. BufferSize,
  998. (VOID **)&mFvbModuleGlobal->FvInstance[FVB_PHYSICAL]
  999. );
  1000. ASSERT_EFI_ERROR (Status);
  1001. //
  1002. // Make a virtual copy of the FvInstance pointer.
  1003. //
  1004. FwhInstance = mFvbModuleGlobal->FvInstance[FVB_PHYSICAL];
  1005. mFvbModuleGlobal->FvInstance[FVB_VIRTUAL] = FwhInstance;
  1006. mFvbModuleGlobal->NumFv = 0;
  1007. MaxLbaSize = 0;
  1008. FvHob.Raw = GetHobList ();
  1009. while (NULL != (FvHob.Raw = GetNextHob (EFI_HOB_TYPE_FV, FvHob.Raw))) {
  1010. BaseAddress = FvHob.FirmwareVolume->BaseAddress;
  1011. Length = FvHob.FirmwareVolume->Length;
  1012. //
  1013. // Check if it is a "real" flash
  1014. //
  1015. Status = DxeServices->GetMemorySpaceDescriptor (
  1016. BaseAddress,
  1017. &Descriptor
  1018. );
  1019. if (EFI_ERROR (Status)) {
  1020. break;
  1021. }
  1022. if (Descriptor.GcdMemoryType != EfiGcdMemoryTypeMemoryMappedIo) {
  1023. FvHob.Raw = GET_NEXT_HOB (FvHob);
  1024. continue;
  1025. }
  1026. FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)BaseAddress;
  1027. Status = ValidateFvHeader (FwVolHeader);
  1028. if (EFI_ERROR (Status)) {
  1029. //
  1030. // Get FvbInfo to provide in FwhInstance.
  1031. //
  1032. Status = GetFvbInfo (Length, &FwVolHeader);
  1033. if (EFI_ERROR (Status)) {
  1034. FvHob.Raw = GET_NEXT_HOB (FvHob);
  1035. continue;
  1036. }
  1037. //
  1038. // Write healthy FV header back.
  1039. //
  1040. CopyMem (
  1041. (VOID *)(UINTN)BaseAddress,
  1042. (VOID *)FwVolHeader,
  1043. FwVolHeader->HeaderLength
  1044. );
  1045. }
  1046. FwhInstance->FvBase[FVB_PHYSICAL] = (UINTN)BaseAddress;
  1047. FwhInstance->FvBase[FVB_VIRTUAL] = (UINTN)BaseAddress;
  1048. CopyMem ((UINTN *)&(FwhInstance->VolumeHeader), (UINTN *)FwVolHeader, FwVolHeader->HeaderLength);
  1049. FwVolHeader = &(FwhInstance->VolumeHeader);
  1050. EfiInitializeLock (&(FwhInstance->FvbDevLock), TPL_HIGH_LEVEL);
  1051. NumOfBlocks = 0;
  1052. for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {
  1053. //
  1054. // Get the maximum size of a block. The size will be used to allocate
  1055. // buffer for Scratch space, the intermediate buffer for FVB extension
  1056. // protocol
  1057. //
  1058. if (MaxLbaSize < PtrBlockMapEntry->Length) {
  1059. MaxLbaSize = PtrBlockMapEntry->Length;
  1060. }
  1061. NumOfBlocks = NumOfBlocks + PtrBlockMapEntry->NumBlocks;
  1062. }
  1063. //
  1064. // The total number of blocks in the FV.
  1065. //
  1066. FwhInstance->NumOfBlocks = NumOfBlocks;
  1067. //
  1068. // Add a FVB Protocol Instance
  1069. //
  1070. Status = gBS->AllocatePool (
  1071. EfiRuntimeServicesData,
  1072. sizeof (EFI_FW_VOL_BLOCK_DEVICE),
  1073. (VOID **)&FvbDevice
  1074. );
  1075. ASSERT_EFI_ERROR (Status);
  1076. CopyMem (FvbDevice, &mFvbDeviceTemplate, sizeof (EFI_FW_VOL_BLOCK_DEVICE));
  1077. FvbDevice->Instance = mFvbModuleGlobal->NumFv;
  1078. mFvbModuleGlobal->NumFv++;
  1079. //
  1080. // Set up the devicepath
  1081. //
  1082. FvbDevice->DevicePath.MemMapDevPath.StartingAddress = BaseAddress;
  1083. FvbDevice->DevicePath.MemMapDevPath.EndingAddress = BaseAddress + (FwVolHeader->FvLength - 1);
  1084. //
  1085. // Find a handle with a matching device path that has supports FW Block protocol
  1086. //
  1087. TempFwbDevicePath = (EFI_DEVICE_PATH_PROTOCOL *)&TempFvbDevicePathData;
  1088. CopyMem (TempFwbDevicePath, &FvbDevice->DevicePath, sizeof (FV_DEVICE_PATH));
  1089. Status = gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid, &TempFwbDevicePath, &FwbHandle);
  1090. if (EFI_ERROR (Status)) {
  1091. //
  1092. // LocateDevicePath fails so install a new interface and device path
  1093. //
  1094. FwbHandle = NULL;
  1095. Status = gBS->InstallMultipleProtocolInterfaces (
  1096. &FwbHandle,
  1097. &gEfiFirmwareVolumeBlockProtocolGuid,
  1098. &FvbDevice->FwVolBlockInstance,
  1099. &gEfiDevicePathProtocolGuid,
  1100. &FvbDevice->DevicePath,
  1101. NULL
  1102. );
  1103. ASSERT_EFI_ERROR (Status);
  1104. } else if (IsDevicePathEnd (TempFwbDevicePath)) {
  1105. //
  1106. // Device allready exists, so reinstall the FVB protocol
  1107. //
  1108. Status = gBS->HandleProtocol (
  1109. FwbHandle,
  1110. &gEfiFirmwareVolumeBlockProtocolGuid,
  1111. (VOID **)&OldFwbInterface
  1112. );
  1113. ASSERT_EFI_ERROR (Status);
  1114. Status = gBS->ReinstallProtocolInterface (
  1115. FwbHandle,
  1116. &gEfiFirmwareVolumeBlockProtocolGuid,
  1117. OldFwbInterface,
  1118. &FvbDevice->FwVolBlockInstance
  1119. );
  1120. ASSERT_EFI_ERROR (Status);
  1121. } else {
  1122. //
  1123. // There was a FVB protocol on an End Device Path node
  1124. //
  1125. ASSERT (FALSE);
  1126. }
  1127. FwhInstance = (EFI_FW_VOL_INSTANCE *)
  1128. (
  1129. (UINTN)((UINT8 *)FwhInstance) + FwVolHeader->HeaderLength +
  1130. (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))
  1131. );
  1132. FvHob.Raw = GET_NEXT_HOB (FvHob);
  1133. }
  1134. return EFI_SUCCESS;
  1135. }