fwvol.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. fwvol.c
  5. Abstract:
  6. This module implements UEFI core protocol support for firmware volumes.
  7. Author:
  8. Evan Green 11-Mar-2014
  9. Environment:
  10. Firmware
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include "ueficore.h"
  16. #include "fwvolp.h"
  17. //
  18. // ---------------------------------------------------------------- Definitions
  19. //
  20. //
  21. // ------------------------------------------------------ Data Type Definitions
  22. //
  23. //
  24. // ----------------------------------------------- Internal Function Prototypes
  25. //
  26. EFIAPI
  27. VOID
  28. EfipFvBlockNotify (
  29. EFI_EVENT Event,
  30. VOID *Context
  31. );
  32. EFI_STATUS
  33. EfipFvCheck (
  34. PEFI_FIRMWARE_VOLUME Device
  35. );
  36. VOID
  37. EfipFvFreeDeviceResource (
  38. PEFI_FIRMWARE_VOLUME Volume
  39. );
  40. BOOLEAN
  41. EfipFvIsBufferErased (
  42. UINT8 ErasePolarity,
  43. VOID *Buffer,
  44. UINTN BufferSize
  45. );
  46. BOOLEAN
  47. EfipFvIsValidFfsHeader (
  48. UINT8 ErasePolarity,
  49. EFI_FFS_FILE_HEADER *FfsHeader,
  50. EFI_FFS_FILE_STATE *FileState
  51. );
  52. BOOLEAN
  53. EfipFvIsValidFfsFile (
  54. UINT8 ErasePolarity,
  55. EFI_FFS_FILE_HEADER *FfsHeader
  56. );
  57. EFI_FFS_FILE_STATE
  58. EfipFvGetFileState (
  59. UINT8 ErasePolarity,
  60. EFI_FFS_FILE_HEADER *FfsHeader
  61. );
  62. BOOLEAN
  63. EfipFvVerifyFileHeaderChecksum (
  64. EFI_FFS_FILE_HEADER *FfsHeader
  65. );
  66. UINT16
  67. EfipFvCalculateSum16 (
  68. UINT16 *Buffer,
  69. UINTN Size
  70. );
  71. UINT8
  72. EfipFvCalculateChecksum8 (
  73. UINT8 *Buffer,
  74. UINTN Size
  75. );
  76. UINT8
  77. EfipFvCalculateSum8 (
  78. UINT8 *Buffer,
  79. UINTN Size
  80. );
  81. //
  82. // -------------------------------------------------------------------- Globals
  83. //
  84. //
  85. // Keep protocol notify related globals.
  86. //
  87. VOID *EfiFvBlockNotifyRegistration;
  88. EFI_EVENT EfiFvBlockEvent;
  89. EFI_GUID EfiFirmwareFileSystem2Guid = EFI_FIRMWARE_FILE_SYSTEM2_GUID;
  90. EFI_GUID EfiFirmwareFileSystem3Guid = EFI_FIRMWARE_FILE_SYSTEM3_GUID;
  91. EFI_GUID EfiFirmwareVolume2ProtocolGuid = EFI_FIRMWARE_VOLUME2_PROTOCOL_GUID;
  92. //
  93. // Store the template for firmware volumes.
  94. //
  95. EFI_FIRMWARE_VOLUME EfiFirmwareVolumeTemplate = {
  96. EFI_FIRMWARE_VOLUME_MAGIC,
  97. NULL,
  98. NULL,
  99. {
  100. EfiFvGetVolumeAttributes,
  101. EfiFvSetVolumeAttributes,
  102. EfiFvReadFile,
  103. EfiFvReadFileSection,
  104. EfiFvWriteFile,
  105. EfiFvGetNextFile,
  106. sizeof(UINTN),
  107. NULL,
  108. EfiFvGetVolumeInfo,
  109. EfiFvSetVolumeInfo,
  110. },
  111. NULL,
  112. NULL,
  113. NULL,
  114. NULL,
  115. {NULL, NULL},
  116. 0,
  117. FALSE,
  118. 0
  119. };
  120. //
  121. // ------------------------------------------------------------------ Functions
  122. //
  123. EFIAPI
  124. EFI_STATUS
  125. EfiFvDriverInit (
  126. EFI_HANDLE ImageHandle,
  127. EFI_SYSTEM_TABLE *SystemTable
  128. )
  129. /*++
  130. Routine Description:
  131. This routine initializes support for UEFI firmware volumes.
  132. Arguments:
  133. ImageHandle - Supplies the image handle for this driver. This is probably
  134. the firmware core image handle.
  135. SystemTable - Supplies a pointer to the system table.
  136. Return Value:
  137. EFI status code.
  138. --*/
  139. {
  140. //
  141. // Sign up to be notified whenever a new firmware volume block device
  142. // protocol crops up.
  143. //
  144. EfiFvBlockEvent = EfiCoreCreateProtocolNotifyEvent(
  145. &EfiFirmwareVolumeBlockProtocolGuid,
  146. TPL_CALLBACK,
  147. EfipFvBlockNotify,
  148. NULL,
  149. &EfiFvBlockNotifyRegistration);
  150. ASSERT(EfiFvBlockEvent != NULL);
  151. return EFI_SUCCESS;
  152. }
  153. EFI_STATUS
  154. EfiFvGetVolumeHeader (
  155. EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *BlockProtocol,
  156. EFI_FIRMWARE_VOLUME_HEADER **Header
  157. )
  158. /*++
  159. Routine Description:
  160. This routine returns the firmware volume header of the volume represented
  161. by the given block I/O interface.
  162. Arguments:
  163. BlockProtocol - Supplies an instance of the block I/O protocol.
  164. Header - Supplies a pointer where a pointer to the volume header allocated
  165. from pool will be returned on success.
  166. Return Value:
  167. EFI status code.
  168. --*/
  169. {
  170. UINT8 *Buffer;
  171. UINTN HeaderLength;
  172. EFI_FIRMWARE_VOLUME_HEADER LocalHeader;
  173. UINTN Offset;
  174. EFI_LBA StartLba;
  175. EFI_STATUS Status;
  176. //
  177. // Read the standard firmware volume header.
  178. //
  179. *Header = NULL;
  180. StartLba = 0;
  181. Offset = 0;
  182. HeaderLength = sizeof(EFI_FIRMWARE_VOLUME_HEADER);
  183. Status = EfiFvReadData(BlockProtocol,
  184. &StartLba,
  185. &Offset,
  186. HeaderLength,
  187. (UINT8 *)&LocalHeader);
  188. if (EFI_ERROR(Status)) {
  189. return Status;
  190. }
  191. if (LocalHeader.HeaderLength < sizeof(EFI_FIRMWARE_VOLUME_HEADER)) {
  192. return EFI_INVALID_PARAMETER;
  193. }
  194. //
  195. // Allocate a buffer for the header.
  196. //
  197. *Header = EfiCoreAllocateBootPool(LocalHeader.HeaderLength);
  198. if (*Header == NULL) {
  199. return EFI_OUT_OF_RESOURCES;
  200. }
  201. EfiCopyMem(*Header, &LocalHeader, sizeof(EFI_FIRMWARE_VOLUME_HEADER));
  202. //
  203. // Read the rest of the header.
  204. //
  205. HeaderLength = LocalHeader.HeaderLength -
  206. sizeof(EFI_FIRMWARE_VOLUME_HEADER);
  207. Buffer = (UINT8 *)*Header + sizeof(EFI_FIRMWARE_VOLUME_HEADER);
  208. Status = EfiFvReadData(BlockProtocol,
  209. &StartLba,
  210. &Offset,
  211. HeaderLength,
  212. Buffer);
  213. if (EFI_ERROR(Status)) {
  214. EfiCoreFreePool(*Header);
  215. *Header = NULL;
  216. }
  217. return Status;
  218. }
  219. EFI_STATUS
  220. EfiFvReadData (
  221. EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *BlockProtocol,
  222. EFI_LBA *StartLba,
  223. UINTN *Offset,
  224. UINTN DataSize,
  225. UINT8 *Data
  226. )
  227. /*++
  228. Routine Description:
  229. This routine reads data from the firmware volume represented by the given
  230. block I/O interface. This data may span multiple block ranges.
  231. Arguments:
  232. BlockProtocol - Supplies an instance of the block I/O protocol.
  233. StartLba - Supplies a pointer that on input contains the logical block
  234. address to read from. On output, this will contain the logical block
  235. address after reading.
  236. Offset - Supplies a pointer that on input contains the offset within the
  237. block to start reading. On output, the offset into the block after
  238. reading will be returned.
  239. DataSize - Supplies the size of the data to read in bytes.
  240. Data - Supplies a pointer where the read data will be returned.
  241. Return Value:
  242. EFI status code.
  243. --*/
  244. {
  245. UINTN BlockIndex;
  246. UINTN BlockSize;
  247. UINTN NumberOfBlocks;
  248. UINTN ReadDataSize;
  249. EFI_STATUS Status;
  250. //
  251. // Try to read data in the current block.
  252. //
  253. BlockIndex = 0;
  254. ReadDataSize = DataSize;
  255. Status = BlockProtocol->Read(BlockProtocol,
  256. *StartLba,
  257. *Offset,
  258. &ReadDataSize,
  259. Data);
  260. if (Status == EFI_SUCCESS) {
  261. *Offset += DataSize;
  262. return EFI_SUCCESS;
  263. } else if (Status != EFI_BAD_BUFFER_SIZE) {
  264. return Status;
  265. }
  266. //
  267. // The read cross block boundaries, so read data from the next block.
  268. //
  269. DataSize -= ReadDataSize;
  270. Data += ReadDataSize;
  271. *StartLba += 1;
  272. while (DataSize > 0) {
  273. Status = BlockProtocol->GetBlockSize(BlockProtocol,
  274. *StartLba,
  275. &BlockSize,
  276. &NumberOfBlocks);
  277. if (EFI_ERROR(Status)) {
  278. return Status;
  279. }
  280. //
  281. // Read data now that a block boundary was just crossed.
  282. //
  283. BlockIndex = 0;
  284. while ((BlockIndex < NumberOfBlocks) && (DataSize >= BlockSize)) {
  285. Status = BlockProtocol->Read(BlockProtocol,
  286. *StartLba + BlockIndex,
  287. 0,
  288. &BlockSize,
  289. Data);
  290. if (EFI_ERROR(Status)) {
  291. return Status;
  292. }
  293. Data += BlockSize;
  294. DataSize -= BlockSize;
  295. BlockIndex += 1;
  296. }
  297. //
  298. // If data doesn't exceed the block range, there's no need to loop
  299. // back around.
  300. //
  301. if (DataSize < BlockSize) {
  302. break;
  303. }
  304. //
  305. // Request block size information from the next range.
  306. //
  307. *StartLba += NumberOfBlocks;
  308. }
  309. //
  310. // Read the last partial block.
  311. //
  312. if (DataSize > 0) {
  313. Status = BlockProtocol->Read(BlockProtocol,
  314. *StartLba + BlockIndex,
  315. 0,
  316. &DataSize,
  317. Data);
  318. if (EFI_ERROR(Status)) {
  319. return Status;
  320. }
  321. }
  322. //
  323. // Update the LBA and offset used by the following read.
  324. //
  325. *StartLba += BlockIndex;
  326. *Offset = DataSize;
  327. return EFI_SUCCESS;
  328. }
  329. BOOLEAN
  330. EfiFvVerifyHeaderChecksum (
  331. EFI_FIRMWARE_VOLUME_HEADER *VolumeHeader
  332. )
  333. /*++
  334. Routine Description:
  335. This routine verifies the checksum of a firmware volume header.
  336. Arguments:
  337. VolumeHeader - Supplies a pointer to the volume header to verify.
  338. Return Value:
  339. TRUE if the checksum verification passed.
  340. FALSE if the checksum verification failed.
  341. --*/
  342. {
  343. UINT16 Checksum;
  344. Checksum = EfipFvCalculateSum16((UINT16 *)VolumeHeader,
  345. VolumeHeader->HeaderLength);
  346. if (Checksum == 0) {
  347. return TRUE;
  348. }
  349. return FALSE;
  350. }
  351. //
  352. // --------------------------------------------------------- Internal Functions
  353. //
  354. EFIAPI
  355. VOID
  356. EfipFvBlockNotify (
  357. EFI_EVENT Event,
  358. VOID *Context
  359. )
  360. /*++
  361. Routine Description:
  362. This routine is called when a new firmware volume block protocol appears
  363. in the system.
  364. Arguments:
  365. Event - Supplies a pointer to the event that fired.
  366. Context - Supplies an unused context pointer.
  367. Return Value:
  368. None.
  369. --*/
  370. {
  371. EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *BlockProtocol;
  372. UINTN BufferSize;
  373. PEFI_FIRMWARE_VOLUME Device;
  374. EFI_FIRMWARE_VOLUME2_PROTOCOL *FirmwareVolume;
  375. EFI_HANDLE Handle;
  376. BOOLEAN Match;
  377. EFI_STATUS Status;
  378. EFI_FIRMWARE_VOLUME_HEADER *VolumeHeader;
  379. //
  380. // Examine all new handles.
  381. //
  382. VolumeHeader = NULL;
  383. while (TRUE) {
  384. Status = EfiCoreLocateHandle(ByRegisterNotify,
  385. NULL,
  386. EfiFvBlockNotifyRegistration,
  387. &BufferSize,
  388. &Handle);
  389. if (Status == EFI_NOT_FOUND) {
  390. break;
  391. }
  392. if (EFI_ERROR(Status)) {
  393. continue;
  394. }
  395. //
  396. // Get the block protocol on the handle.
  397. //
  398. Status = EfiCoreHandleProtocol(Handle,
  399. &EfiFirmwareVolumeBlockProtocolGuid,
  400. (VOID **)&BlockProtocol);
  401. if (EFI_ERROR(Status)) {
  402. ASSERT(FALSE);
  403. continue;
  404. }
  405. ASSERT(BlockProtocol != NULL);
  406. Status = EfiFvGetVolumeHeader(BlockProtocol, &VolumeHeader);
  407. if (EFI_ERROR(Status)) {
  408. return;
  409. }
  410. ASSERT(VolumeHeader != NULL);
  411. Status = EfiFvVerifyHeaderChecksum(VolumeHeader);
  412. if (EFI_ERROR(Status)) {
  413. EfiCoreFreePool(VolumeHeader);
  414. return;
  415. }
  416. //
  417. // Skip file systems that aren't understood.
  418. //
  419. Match = EfiCoreCompareGuids(&(VolumeHeader->FileSystemGuid),
  420. &EfiFirmwareFileSystem2Guid);
  421. if (Match == FALSE) {
  422. Match = EfiCoreCompareGuids(&(VolumeHeader->FileSystemGuid),
  423. &EfiFirmwareFileSystem3Guid);
  424. }
  425. if (Match == FALSE) {
  426. continue;
  427. }
  428. //
  429. // Check to see if there is a firmware volume protocol already
  430. // installed on this handle.
  431. //
  432. Status = EfiCoreHandleProtocol(Handle,
  433. &EfiFirmwareVolume2ProtocolGuid,
  434. (VOID **)&FirmwareVolume);
  435. //
  436. // If there's a previously existing firmware volume protocol, then
  437. // update the block device if it was created by this driver.
  438. //
  439. if (!EFI_ERROR(Status)) {
  440. Device = PARENT_STRUCTURE(FirmwareVolume,
  441. EFI_FIRMWARE_VOLUME,
  442. VolumeProtocol);
  443. if (Device->Magic == EFI_FIRMWARE_VOLUME_MAGIC) {
  444. Device->BlockIo = BlockProtocol;
  445. }
  446. //
  447. // No firmware volume is present, create a new one.
  448. //
  449. } else {
  450. Device = EfiCoreAllocateBootPool(sizeof(EFI_FIRMWARE_VOLUME));
  451. if (Device == NULL) {
  452. return;
  453. }
  454. EfiCoreCopyMemory(Device,
  455. &EfiFirmwareVolumeTemplate,
  456. sizeof(EFI_FIRMWARE_VOLUME));
  457. Device->BlockIo = BlockProtocol;
  458. Device->Handle = Handle;
  459. Device->VolumeHeader = VolumeHeader;
  460. Match = EfiCoreCompareGuids(&(VolumeHeader->FileSystemGuid),
  461. &EfiFirmwareFileSystem3Guid);
  462. if (Match != FALSE) {
  463. Device->IsFfs3 = TRUE;
  464. }
  465. Device->VolumeProtocol.ParentHandle = BlockProtocol->ParentHandle;
  466. Status = EfipFvCheck(Device);
  467. if (!EFI_ERROR(Status)) {
  468. Status = EfiCoreInstallProtocolInterface(
  469. &Handle,
  470. &EfiFirmwareVolume2ProtocolGuid,
  471. EFI_NATIVE_INTERFACE,
  472. &(Device->VolumeProtocol));
  473. ASSERT(!EFI_ERROR(Status));
  474. }
  475. if (EFI_ERROR(Status)) {
  476. EfiCoreFreePool(Device);
  477. }
  478. }
  479. }
  480. return;
  481. }
  482. EFI_STATUS
  483. EfipFvCheck (
  484. PEFI_FIRMWARE_VOLUME Device
  485. )
  486. /*++
  487. Routine Description:
  488. This routine checks the given firmware volume for consistency and allocates
  489. a cache for it.
  490. Arguments:
  491. Device - Supplies a pointer to the device to check.
  492. Return Value:
  493. EFI status code.
  494. --*/
  495. {
  496. EFI_FVB_ATTRIBUTES Attributes;
  497. EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *BlockIo;
  498. EFI_FV_BLOCK_MAP_ENTRY *BlockMap;
  499. UINT8 *CacheLocation;
  500. BOOLEAN Erased;
  501. EFI_FFS_FILE_LIST_ENTRY *FfsFileEntry;
  502. EFI_FFS_FILE_HEADER *FfsHeader;
  503. EFI_FFS_FILE_STATE FileState;
  504. UINTN HeaderSize;
  505. UINTN Index;
  506. EFI_LBA LbaIndex;
  507. UINTN LbaOffset;
  508. UINTN Size;
  509. EFI_STATUS Status;
  510. UINTN TestLength;
  511. UINT8 *TopAddress;
  512. BOOLEAN Valid;
  513. EFI_FIRMWARE_VOLUME_HEADER *VolumeHeader;
  514. EFI_FIRMWARE_VOLUME_EXT_HEADER *VolumeHeaderExt;
  515. BlockIo = Device->BlockIo;
  516. VolumeHeader = Device->VolumeHeader;
  517. Status = BlockIo->GetAttributes(BlockIo, &Attributes);
  518. if (EFI_ERROR(Status)) {
  519. return Status;
  520. }
  521. Size = (UINTN)(VolumeHeader->Length - VolumeHeader->HeaderLength);
  522. Device->CachedVolume = EfiCoreAllocateBootPool(Size);
  523. if (Device->CachedVolume == NULL) {
  524. return EFI_OUT_OF_RESOURCES;
  525. }
  526. Device->EndOfCachedVolume = Device->CachedVolume + Size;
  527. //
  528. // Copy the firmware volume minus the header into memory using the block
  529. // map in the header.
  530. //
  531. BlockMap = VolumeHeader->BlockMap;
  532. CacheLocation = Device->CachedVolume;
  533. LbaIndex = 0;
  534. LbaOffset = 0;
  535. HeaderSize = VolumeHeader->HeaderLength;
  536. while ((BlockMap->BlockCount != 0) || (BlockMap->BlockLength != 0)) {
  537. Index = 0;
  538. Size = BlockMap->BlockLength;
  539. //
  540. // Skip the header.
  541. //
  542. if (HeaderSize > 0) {
  543. while ((Index < BlockMap->BlockCount) &&
  544. (HeaderSize >= BlockMap->BlockLength)) {
  545. HeaderSize -= BlockMap->BlockLength;
  546. LbaIndex += 1;
  547. Index += 1;
  548. }
  549. //
  550. // Check whether or not the header crosses a block boundary.
  551. //
  552. if (Index >= BlockMap->BlockCount) {
  553. BlockMap += 1;
  554. continue;
  555. } else if (HeaderSize > 0) {
  556. LbaOffset = HeaderSize;
  557. Size = BlockMap->BlockLength - HeaderSize;
  558. HeaderSize = 0;
  559. }
  560. }
  561. //
  562. // Read the firmware volume data.
  563. //
  564. while (Index < BlockMap->BlockCount) {
  565. Status = BlockIo->Read(BlockIo,
  566. LbaIndex,
  567. LbaOffset,
  568. &Size,
  569. CacheLocation);
  570. if (EFI_ERROR(Status)) {
  571. goto FvCheckEnd;
  572. }
  573. LbaIndex += 1;
  574. CacheLocation += Size;
  575. LbaOffset = 0;
  576. Size = BlockMap->BlockLength;
  577. Index += 1;
  578. }
  579. BlockMap += 1;
  580. }
  581. //
  582. // Check the free space and file list.
  583. //
  584. Device->ErasePolarity = 0;
  585. if ((Attributes & EFI_FVB_ERASE_POLARITY) != 0) {
  586. Device->ErasePolarity = 1;
  587. }
  588. //
  589. // Go through the entire firmware volume cache and check the consistency of
  590. // the firmware volume. Make a linked list of all the FFS file headers.
  591. //
  592. Status = EFI_SUCCESS;
  593. INITIALIZE_LIST_HEAD(&(Device->FfsFileList));
  594. if (VolumeHeader->ExtHeaderOffset != 0) {
  595. VolumeHeaderExt =
  596. (EFI_FIRMWARE_VOLUME_EXT_HEADER *)(Device->CachedVolume +
  597. (VolumeHeader->ExtHeaderOffset -
  598. VolumeHeader->HeaderLength));
  599. FfsHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)VolumeHeaderExt +
  600. VolumeHeaderExt->ExtHeaderSize);
  601. FfsHeader = (EFI_FFS_FILE_HEADER *)ALIGN_POINTER(FfsHeader, 8);
  602. } else {
  603. FfsHeader = (EFI_FFS_FILE_HEADER *)(Device->CachedVolume);
  604. }
  605. TopAddress = Device->EndOfCachedVolume;
  606. while ((UINT8 *)FfsHeader < TopAddress) {
  607. TestLength = TopAddress - ((UINT8 *)FfsHeader);
  608. if (TestLength > sizeof(EFI_FFS_FILE_HEADER)) {
  609. TestLength = sizeof(EFI_FFS_FILE_HEADER);
  610. }
  611. //
  612. // If this is all free space then that's it.
  613. //
  614. Erased = EfipFvIsBufferErased(Device->ErasePolarity,
  615. FfsHeader,
  616. TestLength);
  617. if (Erased != FALSE) {
  618. goto FvCheckEnd;
  619. }
  620. Valid = EfipFvIsValidFfsHeader(Device->ErasePolarity,
  621. FfsHeader,
  622. &FileState);
  623. if (Valid == FALSE) {
  624. if ((FileState == EFI_FILE_HEADER_INVALID) ||
  625. (FileState == EFI_FILE_HEADER_CONSTRUCTION)) {
  626. if (EFI_IS_FFS_FILE2(FfsHeader)) {
  627. if (Device->IsFfs3 == FALSE) {
  628. RtlDebugPrint("Warning: Found an FFS3 file in an FFS2 "
  629. "volume!\n");
  630. }
  631. FfsHeader =
  632. (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsHeader +
  633. sizeof(EFI_FFS_FILE_HEADER2));
  634. } else {
  635. FfsHeader =
  636. (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsHeader +
  637. sizeof(EFI_FFS_FILE_HEADER));
  638. }
  639. continue;
  640. } else {
  641. Status = EFI_VOLUME_CORRUPTED;
  642. goto FvCheckEnd;
  643. }
  644. }
  645. Valid = EfipFvIsValidFfsFile(Device->ErasePolarity, FfsHeader);
  646. if (Valid == FALSE) {
  647. Status = EFI_VOLUME_CORRUPTED;
  648. goto FvCheckEnd;
  649. }
  650. if (EFI_IS_FFS_FILE2(FfsHeader)) {
  651. ASSERT(EFI_FFS_FILE2_SIZE(FfsHeader) > MAX_FFS_SIZE);
  652. if (Device->IsFfs3 == FALSE) {
  653. RtlDebugPrint("Warning: Found an FFS3 file in an FFS2 "
  654. "volume!\n");
  655. //
  656. // Skip the file and align up the the next 8-byte boundary.
  657. //
  658. FfsHeader =
  659. (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsHeader +
  660. EFI_FFS_FILE2_SIZE(FfsHeader));
  661. FfsHeader = (EFI_FFS_FILE_HEADER *)ALIGN_POINTER(FfsHeader, 8);
  662. continue;
  663. }
  664. }
  665. FileState = EfipFvGetFileState(Device->ErasePolarity, FfsHeader);
  666. if (FileState != EFI_FILE_DELETED) {
  667. FfsFileEntry = EfiCoreAllocateBootPool(
  668. sizeof(EFI_FFS_FILE_LIST_ENTRY));
  669. if (FfsFileEntry == NULL) {
  670. Status = EFI_OUT_OF_RESOURCES;
  671. goto FvCheckEnd;
  672. }
  673. EfiCoreSetMemory(FfsFileEntry, sizeof(EFI_FFS_FILE_LIST_ENTRY), 0);
  674. FfsFileEntry->FileHeader = FfsHeader;
  675. INSERT_BEFORE(&(FfsFileEntry->ListEntry), &(Device->FfsFileList));
  676. }
  677. //
  678. // Move to the next file header (aligned to an 8-byte boundary).
  679. //
  680. if (EFI_IS_FFS_FILE2(FfsHeader)) {
  681. FfsHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsHeader +
  682. EFI_FFS_FILE2_SIZE(FfsHeader));
  683. } else {
  684. FfsHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsHeader +
  685. EFI_FFS_FILE_SIZE(FfsHeader));
  686. }
  687. FfsHeader = (EFI_FFS_FILE_HEADER *)ALIGN_POINTER(FfsHeader, 8);
  688. }
  689. FvCheckEnd:
  690. if (EFI_ERROR(Status)) {
  691. EfipFvFreeDeviceResource(Device);
  692. }
  693. return Status;
  694. }
  695. VOID
  696. EfipFvFreeDeviceResource (
  697. PEFI_FIRMWARE_VOLUME Volume
  698. )
  699. /*++
  700. Routine Description:
  701. This routine destroys a firmware device volume.
  702. Arguments:
  703. Volume - Supplies a pointer to the volume to destroy.
  704. Return Value:
  705. None.
  706. --*/
  707. {
  708. PEFI_FFS_FILE_LIST_ENTRY FfsFileEntry;
  709. //
  710. // Free all the FFS file list entries.
  711. //
  712. while (LIST_EMPTY(&(Volume->FfsFileList)) == FALSE) {
  713. FfsFileEntry = LIST_VALUE(Volume->FfsFileList.Next,
  714. EFI_FFS_FILE_LIST_ENTRY,
  715. ListEntry);
  716. if (FfsFileEntry->StreamHandle != 0) {
  717. EfiFvCloseSectionStream(FfsFileEntry->StreamHandle);
  718. }
  719. LIST_REMOVE(&(FfsFileEntry->ListEntry));
  720. EfiCoreFreePool(FfsFileEntry);
  721. }
  722. if (Volume->CachedVolume != NULL) {
  723. EfiCoreFreePool(Volume->CachedVolume);
  724. }
  725. if (Volume->VolumeHeader != NULL) {
  726. EfiCoreFreePool(Volume->VolumeHeader);
  727. }
  728. return;
  729. }
  730. BOOLEAN
  731. EfipFvIsBufferErased (
  732. UINT8 ErasePolarity,
  733. VOID *Buffer,
  734. UINTN BufferSize
  735. )
  736. /*++
  737. Routine Description:
  738. This routine determines if the given buffer is all erased data.
  739. Arguments:
  740. ErasePolarity - Supplies the erase polarity of the volume.
  741. Buffer - Supplies the buffer to be checked.
  742. BufferSize - Supplies the size of the buffer.
  743. Return Value:
  744. TRUE if the block of buffer is erased.
  745. FALSE if the block of buffer is not erased.
  746. --*/
  747. {
  748. UINT8 *CheckBuffer;
  749. UINTN Count;
  750. UINT8 EraseByte;
  751. EraseByte = 0;
  752. if (ErasePolarity != 0) {
  753. EraseByte = 0xFF;
  754. }
  755. CheckBuffer = Buffer;
  756. for (Count = 0; Count < BufferSize; Count += 1) {
  757. if (CheckBuffer[Count] != EraseByte) {
  758. return FALSE;
  759. }
  760. }
  761. return TRUE;
  762. }
  763. BOOLEAN
  764. EfipFvIsValidFfsHeader (
  765. UINT8 ErasePolarity,
  766. EFI_FFS_FILE_HEADER *FfsHeader,
  767. EFI_FFS_FILE_STATE *FileState
  768. )
  769. /*++
  770. Routine Description:
  771. This routine determines if the given supposed FFS file header is valid.
  772. Arguments:
  773. ErasePolarity - Supplies the erase polarity of the volume.
  774. FfsHeader - Supplies a pointer to the FFS file header to be checked.
  775. FileState - Supplies a pointer where the file state will be returned if the
  776. header is valid.
  777. Return Value:
  778. TRUE if the FFS file header is valid.
  779. FALSE if the header is not valid.
  780. --*/
  781. {
  782. *FileState = EfipFvGetFileState(ErasePolarity, FfsHeader);
  783. switch (*FileState) {
  784. //
  785. // If the file state looks good verify the header checksum.
  786. //
  787. case EFI_FILE_HEADER_VALID:
  788. case EFI_FILE_DATA_VALID:
  789. case EFI_FILE_MARKED_FOR_UPDATE:
  790. case EFI_FILE_DELETED:
  791. return EfipFvVerifyFileHeaderChecksum(FfsHeader);
  792. case EFI_FILE_HEADER_CONSTRUCTION:
  793. case EFI_FILE_HEADER_INVALID:
  794. default:
  795. break;
  796. }
  797. return FALSE;
  798. }
  799. BOOLEAN
  800. EfipFvIsValidFfsFile (
  801. UINT8 ErasePolarity,
  802. EFI_FFS_FILE_HEADER *FfsHeader
  803. )
  804. /*++
  805. Routine Description:
  806. This routine determines if the given supposed FFS file is valid.
  807. Arguments:
  808. ErasePolarity - Supplies the erase polarity of the volume.
  809. FfsHeader - Supplies a pointer to the FFS file header to be checked.
  810. Return Value:
  811. TRUE if the FFS file is valid.
  812. FALSE if the header is not valid.
  813. --*/
  814. {
  815. UINT8 DataChecksum;
  816. UINT8 *FileData;
  817. UINTN FileDataSize;
  818. EFI_FFS_FILE_STATE FileState;
  819. FileState = EfipFvGetFileState(ErasePolarity, FfsHeader);
  820. switch (FileState) {
  821. case EFI_FILE_DELETED:
  822. case EFI_FILE_DATA_VALID:
  823. case EFI_FILE_MARKED_FOR_UPDATE:
  824. DataChecksum = FFS_FIXED_CHECKSUM;
  825. if ((FfsHeader->Attributes & FFS_ATTRIB_CHECKSUM) != 0) {
  826. if (EFI_IS_FFS_FILE2(FfsHeader)) {
  827. FileData = (UINT8 *)FfsHeader + sizeof(EFI_FFS_FILE_HEADER2);
  828. FileDataSize = EFI_FFS_FILE2_SIZE(FfsHeader) -
  829. sizeof(EFI_FFS_FILE_HEADER2);
  830. } else {
  831. FileData = (UINT8 *)FfsHeader + sizeof(EFI_FFS_FILE_HEADER);
  832. FileDataSize = EFI_FFS_FILE_SIZE(FfsHeader) -
  833. sizeof(EFI_FFS_FILE_HEADER);
  834. }
  835. DataChecksum = EfipFvCalculateChecksum8(FileData, FileDataSize);
  836. }
  837. if (FfsHeader->IntegrityCheck.Checksum.File == DataChecksum) {
  838. return TRUE;
  839. }
  840. break;
  841. default:
  842. break;
  843. }
  844. return FALSE;
  845. }
  846. EFI_FFS_FILE_STATE
  847. EfipFvGetFileState (
  848. UINT8 ErasePolarity,
  849. EFI_FFS_FILE_HEADER *FfsHeader
  850. )
  851. /*++
  852. Routine Description:
  853. This routine returns the FFS file state.
  854. Arguments:
  855. ErasePolarity - Supplies the erase polarity of the volume.
  856. FfsHeader - Supplies a pointer to the FFS file header.
  857. Return Value:
  858. Returns the FFS file state.
  859. --*/
  860. {
  861. EFI_FFS_FILE_STATE FileState;
  862. UINT8 HighestBit;
  863. FileState = FfsHeader->State;
  864. if (ErasePolarity != 0) {
  865. FileState = (EFI_FFS_FILE_STATE)~FileState;
  866. }
  867. HighestBit = 0x80;
  868. while ((HighestBit != 0) && ((HighestBit & FileState) == 0)) {
  869. HighestBit >>= 1;
  870. }
  871. return (EFI_FFS_FILE_STATE)HighestBit;
  872. }
  873. BOOLEAN
  874. EfipFvVerifyFileHeaderChecksum (
  875. EFI_FFS_FILE_HEADER *FfsHeader
  876. )
  877. /*++
  878. Routine Description:
  879. This routine verifies the checksum of an FFS file header.
  880. Arguments:
  881. FfsHeader - Supplies a pointer to the volume header to verify.
  882. Return Value:
  883. TRUE if the checksum verification passed.
  884. FALSE if the checksum verification failed.
  885. --*/
  886. {
  887. UINT8 HeaderChecksum;
  888. if (EFI_IS_FFS_FILE2(FfsHeader)) {
  889. HeaderChecksum = EfipFvCalculateSum8((UINT8 *)FfsHeader,
  890. sizeof(EFI_FFS_FILE_HEADER2));
  891. } else {
  892. HeaderChecksum = EfipFvCalculateSum8((UINT8 *)FfsHeader,
  893. sizeof(EFI_FFS_FILE_HEADER));
  894. }
  895. HeaderChecksum = (UINT8)(HeaderChecksum - FfsHeader->State -
  896. FfsHeader->IntegrityCheck.Checksum.File);
  897. if (HeaderChecksum == 0) {
  898. return TRUE;
  899. }
  900. return FALSE;
  901. }
  902. UINT16
  903. EfipFvCalculateSum16 (
  904. UINT16 *Buffer,
  905. UINTN Size
  906. )
  907. /*++
  908. Routine Description:
  909. This routine calculates the 16-bit checksum of the bytes in the given
  910. buffer.
  911. Arguments:
  912. Buffer - Supplies a pointer to a buffer containing byte data.
  913. Size - Supplies the size of the buffer in bytes.
  914. Return Value:
  915. Returns the 16-bit sum of the buffer words.
  916. --*/
  917. {
  918. UINTN Index;
  919. UINT16 Sum;
  920. Size = Size / sizeof(UINT16);
  921. Sum = 0;
  922. for (Index = 0; Index < Size; Index += 1) {
  923. Sum = (UINT16)(Sum + Buffer[Index]);
  924. }
  925. return Sum;
  926. }
  927. UINT8
  928. EfipFvCalculateChecksum8 (
  929. UINT8 *Buffer,
  930. UINTN Size
  931. )
  932. /*++
  933. Routine Description:
  934. This routine calculates the 8-bit checksum of the bytes in the given
  935. buffer.
  936. Arguments:
  937. Buffer - Supplies a pointer to a buffer containing byte data.
  938. Size - Supplies the size of the buffer.
  939. Return Value:
  940. Returns the 8-bit checksum of each byte in the buffer.
  941. --*/
  942. {
  943. return 0x100 - EfipFvCalculateSum8(Buffer, Size);
  944. }
  945. UINT8
  946. EfipFvCalculateSum8 (
  947. UINT8 *Buffer,
  948. UINTN Size
  949. )
  950. /*++
  951. Routine Description:
  952. This routine calculates the 8-bit sum of the bytes in the given buffer.
  953. Arguments:
  954. Buffer - Supplies a pointer to a buffer containing byte data.
  955. Size - Supplies the size of the buffer.
  956. Return Value:
  957. Returns the 8-bit checksum of each byte in the buffer.
  958. --*/
  959. {
  960. UINTN Index;
  961. UINT8 Sum;
  962. Sum = 0;
  963. for (Index = 0; Index < Size; Index += 1) {
  964. Sum = (UINT8)(Sum + Buffer[Index]);
  965. }
  966. return Sum;
  967. }