fwvolio.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. fwvolio.c
  5. Abstract:
  6. This module implements support for firmware volume I/O.
  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. EFI_FV_FILE_ATTRIBUTES
  27. EfipFvConvertFfsAttributesToFileAttributes (
  28. EFI_FFS_FILE_ATTRIBUTES FfsAttributes
  29. );
  30. //
  31. // -------------------------------------------------------------------- Globals
  32. //
  33. //
  34. // Define the conversion between FFS alignments and FW volume alignments.
  35. //
  36. UINT8 EfiFvFfsAlignments[] = {0, 4, 7, 9, 10, 12, 15, 16};
  37. //
  38. // ------------------------------------------------------------------ Functions
  39. //
  40. EFIAPI
  41. EFI_STATUS
  42. EfiFvGetVolumeAttributes (
  43. CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,
  44. EFI_FV_ATTRIBUTES *Attributes
  45. )
  46. /*++
  47. Routine Description:
  48. This routine returns the attributes and current settings of the firmware
  49. volume. Because of constraints imposed by the underlying firmware storage,
  50. an instance of the Firmware Volume Protocol may not be to able to support
  51. all possible variations of this architecture. These constraints and the
  52. current state of the firmware volume are exposed to the caller using the
  53. get volume attributes function. The get volume attributes function is
  54. callable only from TPL_NOTIFY and below. Behavior of this routine at any
  55. EFI_TPL above TPL_NOTIFY is undefined.
  56. Arguments:
  57. This - Supplies the protocol instance.
  58. Attributes - Supplies a pointer where the volume attributes and current
  59. settings are returned.
  60. Return Value:
  61. EFI_SUCCESS on success.
  62. --*/
  63. {
  64. EFI_FVB_ATTRIBUTES BlockAttributes;
  65. EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *BlockIo;
  66. PEFI_FIRMWARE_VOLUME Device;
  67. EFI_STATUS Status;
  68. Device = EFI_FIRMWARE_VOLUME_FROM_THIS(This);
  69. BlockIo = Device->BlockIo;
  70. //
  71. // Get the firmware volume block attributes, then mask out anything that's
  72. // irrelevant.
  73. //
  74. Status = BlockIo->GetAttributes(BlockIo, &BlockAttributes);
  75. BlockAttributes &= 0xFFFFF0FF;
  76. *Attributes = (EFI_FV_ATTRIBUTES)BlockAttributes;
  77. return Status;
  78. }
  79. EFIAPI
  80. EFI_STATUS
  81. EfiFvSetVolumeAttributes (
  82. CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,
  83. EFI_FV_ATTRIBUTES *Attributes
  84. )
  85. /*++
  86. Routine Description:
  87. This routine modifies current settings of the firmware volume according to
  88. the input parameter. This function is used to set configurable firmware
  89. volume attributes. Only EFI_FV_READ_STATUS, EFI_FV_WRITE_STATUS, and
  90. EFI_FV_LOCK_STATUS may be modified, and then only in accordance with the
  91. declared capabilities. All other bits of FvAttributes are ignored on input.
  92. On successful return, all bits of *FvAttributes are valid and it contains
  93. the completed EFI_FV_ATTRIBUTES for the volume. To modify an attribute, the
  94. corresponding status bit in the EFI_FV_ATTRIBUTES is set to the desired
  95. value on input. The EFI_FV_LOCK_STATUS bit does not affect the ability to
  96. read or write the firmware volume. Rather, once the EFI_FV_LOCK_STATUS bit
  97. is set, it prevents further modification to all the attribute bits. This
  98. routine is callable only from TPL_NOTIFY and below. Behavior of this
  99. routine at any EFI_TPL above TPL_NOTIFY is undefined.
  100. Arguments:
  101. This - Supplies the protocol instance.
  102. Attributes - Supplies a that on input contains the attributes to set. On
  103. output, the new current settings of the firmware volume are returned.
  104. Return Value:
  105. EFI_SUCCESS on success.
  106. EFI_INVALID_PARAMETER if the device does not support the status bit
  107. settings.
  108. EFI_ACCESS_DENIED if the device is locked and does not allow modification.
  109. --*/
  110. {
  111. return EFI_UNSUPPORTED;
  112. }
  113. EFIAPI
  114. EFI_STATUS
  115. EfiFvGetVolumeInfo (
  116. CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,
  117. CONST EFI_GUID *InformationType,
  118. UINTN *BufferSize,
  119. VOID *Buffer
  120. )
  121. /*++
  122. Routine Description:
  123. This routine returns information about a firmware volume. this routine
  124. returns information of the requested type for the requested firmware
  125. volume. If the volume does not support the requested information type, then
  126. EFI_UNSUPPORTED is returned. If the buffer is not large enough to hold the
  127. requested structure, EFI_BUFFER_TOO_SMALL is returned and the buffer size
  128. is set to the size of buffer that is required to make the request. The
  129. information types defined by this specification are required information
  130. types that all file systems must support.
  131. Arguments:
  132. This - Supplies the protocol instance.
  133. InformationType - Supplies a pointer to the GUID defining the type of
  134. information being requested.
  135. BufferSize - Supplies a pointer that on input contains the size of the
  136. buffer. On output, contains the size of the data, even if the buffer
  137. was too small.
  138. Buffer - Supplies the buffer where the information value will be returned.
  139. Return Value:
  140. EFI_SUCCESS on success.
  141. EFI_UNSUPPORTED if the information type is not known.
  142. EFI_NO_MEDIA if the device has no media inserted.
  143. EFI_DEVICE_ERROR if a hardware error occurred accessing the device.
  144. EFI_VOLUME_CORRUPTED if the file system structures are gonezo.
  145. EFI_BUFFER_TOO_SMALL if the supplied buffer was not large enough to hold
  146. the requested value. In this case the buffer size will have been updated
  147. to contain the needed value.
  148. --*/
  149. {
  150. return EFI_UNSUPPORTED;
  151. }
  152. EFIAPI
  153. EFI_STATUS
  154. EfiFvSetVolumeInfo (
  155. CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,
  156. CONST EFI_GUID *InformationType,
  157. UINTN BufferSize,
  158. CONST VOID *Buffer
  159. )
  160. /*++
  161. Routine Description:
  162. This routine sets information about a firmware volume.
  163. Arguments:
  164. This - Supplies the protocol instance.
  165. InformationType - Supplies a pointer to the GUID defining the type of
  166. information being set.
  167. BufferSize - Supplies the size of the information value in bytes.
  168. Buffer - Supplies the information value.
  169. Return Value:
  170. EFI_SUCCESS on success.
  171. EFI_UNSUPPORTED if the information type is not known.
  172. EFI_NO_MEDIA if the device has no media inserted.
  173. EFI_DEVICE_ERROR if a hardware error occurred accessing the device.
  174. EFI_VOLUME_CORRUPTED if the file system structures are gonezo.
  175. EFI_WRITE_PROTECTED if the volume is read-only.
  176. EFI_BAD_BUFFER_SIZE if the buffer size was smaller than the size of the
  177. type indicated in the information type GUID.
  178. --*/
  179. {
  180. return EFI_UNSUPPORTED;
  181. }
  182. EFIAPI
  183. EFI_STATUS
  184. EfiFvReadFileSection (
  185. CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,
  186. CONST EFI_GUID *NameGuid,
  187. EFI_SECTION_TYPE SectionType,
  188. UINTN SectionInstance,
  189. VOID **Buffer,
  190. UINTN *BufferSize,
  191. UINT32 *AuthenticationStatus
  192. )
  193. /*++
  194. Routine Description:
  195. This routine locates the requested section within a file and returns it in
  196. a buffer. This routine is used to retrieve a specific section from a file
  197. within a firmware volume. The section returned is determined using a
  198. depth-first, left-to-right search algorithm through all sections found in
  199. the specified file. The output buffer is specified by a double indirection
  200. of the buffer parameter. The input value of Buffer is used to determine if
  201. the output buffer is caller allocated or is dynamically allocated by this
  202. routine. If the input value of the buffer is not NULL, it indicates that
  203. the output buffer is caller allocated. In this case, the input value of
  204. *BufferSize indicates the size of the caller-allocated output buffer. If
  205. the output buffer is not large enough to contain the entire requested
  206. output, it is filled up to the point that the output buffer is exhausted
  207. and EFI_WARN_BUFFER_TOO_SMALL is returned, and then BufferSize is returned
  208. with the size that is required to successfully complete the read. All other
  209. output parameters are returned with valid values. If the input value of the
  210. buffer is NULL, it indicates the output buffer is to be allocated by this
  211. routine. In this case, this routine will allocate an appropriately sized
  212. buffer from boot services pool memory, which will be returned in the buffer
  213. value. The size of the new buffer is returned in *BufferSize and all other
  214. output parameters are returned with valid values. This routine is callable
  215. only from TPL_NOTIFY and below. Behavior of this routine at any EFI_TPL
  216. above TPL_NOTIFY is undefined.
  217. Arguments:
  218. This - Supplies the protocol instance.
  219. NameGuid - Supplies a pointer to a GUID containing the name of the file.
  220. All firmware file names are EFI_GUIDs. Each firmware volume file name
  221. must be unique.
  222. SectionType - Supplies the type of section to return.
  223. SectionInstance - Supplies the occurrence of the given section type to
  224. return.
  225. Buffer - Supplies a pointer to a buffer in which the file contents are
  226. returned, not including the file header.
  227. BufferSize - Supplies a pointer that on input indicates the size of the
  228. supplied buffer in bytes.
  229. AuthenticationStatus - Supplies a pointer where the file authentication
  230. status will be returned.
  231. Return Value:
  232. EFI_SUCCESS on success.
  233. EFI_WARN_TOO_SMALL if the buffer is too small to contain the requested
  234. output. The buffer is filled and truncated.
  235. EFI_OUT_OF_RESOURCES if an allocation failed.
  236. EFI_NOT_FOUND if the requested file or section was not found in the
  237. firmware volume or file.
  238. EFI_DEVICE_ERROR if a hardware error occurred accessing the device.
  239. EFI_ACCESS_DENIED if the firmware volume is configured to disallow reads.
  240. EFI_PROTOCOL_ERROR if the requested section was not found, but the file
  241. could not be fully parsed because a required
  242. GUIDED_SECTION_EXTRACTION_PROTOCOL was not found. The requested section
  243. might have been found if only it could be extracted.
  244. --*/
  245. {
  246. PEFI_FIRMWARE_VOLUME Device;
  247. PEFI_FFS_FILE_LIST_ENTRY FfsEntry;
  248. EFI_FV_FILE_ATTRIBUTES FileAttributes;
  249. UINT8 *FileBuffer;
  250. UINTN FileSize;
  251. EFI_FV_FILETYPE FileType;
  252. EFI_STATUS Status;
  253. if ((NameGuid == NULL) || (Buffer == NULL)) {
  254. return EFI_INVALID_PARAMETER;
  255. }
  256. Device = EFI_FIRMWARE_VOLUME_FROM_THIS(This);
  257. //
  258. // Read the whole file into a buffer.
  259. //
  260. FileBuffer = NULL;
  261. FileSize = 0;
  262. Status = EfiFvReadFile(This,
  263. NameGuid,
  264. (VOID **)&FileBuffer,
  265. &FileSize,
  266. &FileType,
  267. &FileAttributes,
  268. AuthenticationStatus);
  269. if (EFI_ERROR(Status)) {
  270. return Status;
  271. }
  272. //
  273. // Get the last key used by the call to read file as it is the FFS entry
  274. // for this file.
  275. //
  276. FfsEntry = (PEFI_FFS_FILE_LIST_ENTRY)(Device->LastKey);
  277. if (FileType == EFI_FV_FILETYPE_RAW) {
  278. Status = EFI_NOT_FOUND;
  279. goto FvReadSectionEnd;
  280. }
  281. if (FfsEntry->StreamHandle == 0) {
  282. Status = EfiFvOpenSectionStream(FileSize,
  283. FileBuffer,
  284. &(FfsEntry->StreamHandle));
  285. if (EFI_ERROR(Status)) {
  286. goto FvReadSectionEnd;
  287. }
  288. }
  289. //
  290. // If the section type is zero then the whole stream is needed.
  291. //
  292. if (SectionType == 0) {
  293. Status = EfiFvGetSection(FfsEntry->StreamHandle,
  294. NULL,
  295. NULL,
  296. 0,
  297. Buffer,
  298. BufferSize,
  299. AuthenticationStatus,
  300. Device->IsFfs3);
  301. } else {
  302. Status = EfiFvGetSection(FfsEntry->StreamHandle,
  303. &SectionType,
  304. NULL,
  305. SectionInstance,
  306. Buffer,
  307. BufferSize,
  308. AuthenticationStatus,
  309. Device->IsFfs3);
  310. }
  311. if (!EFI_ERROR(Status)) {
  312. Status = Device->AuthenticationStatus;
  313. }
  314. FvReadSectionEnd:
  315. EfiCoreFreePool(FileBuffer);
  316. return Status;
  317. }
  318. EFIAPI
  319. EFI_STATUS
  320. EfiFvReadFile (
  321. CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,
  322. CONST EFI_GUID *NameGuid,
  323. VOID **Buffer,
  324. UINTN *BufferSize,
  325. EFI_FV_FILETYPE *FoundType,
  326. EFI_FV_FILE_ATTRIBUTES *FileAttributes,
  327. UINT32 *AuthenticationStatus
  328. )
  329. /*++
  330. Routine Description:
  331. This routine retrieves a file and/or file information from the firmware
  332. volume. This routine is used to retrieve any file from a firmware volume
  333. during the DXE phase. The actual binary encoding of the file in the
  334. firmware volume media may be in any arbitrary format as long as it does the
  335. following: It is accessed using the Firmware Volume Protocol. The image
  336. that is returned follows the image format defined in Code Definitions:
  337. PI Firmware File Format. If the input buffer is NULL, it indicates the
  338. caller is requesting only that the type, attributes, and size of the file
  339. be returned and that there is no output buffer. In this case, the following
  340. occurs:
  341. - BufferSize is returned with the size that is required to
  342. successfully complete the read.
  343. - The output parameters FoundType and *FileAttributes are
  344. returned with valid values.
  345. - The returned value of *AuthenticationStatus is undefined.
  346. If the input buffer is not NULL, the output buffer is specified by a double
  347. indirection of the Buffer parameter. The input value of *Buffer is used to
  348. determine if the output buffer is caller allocated or is dynamically
  349. allocated by this routine. If the input value of *Buffer is not NULL, it
  350. indicates the output buffer is caller allocated. In this case, the input
  351. value of *BufferSize indicates the size of the caller-allocated output
  352. buffer. If the output buffer is not large enough to contain the entire
  353. requested output, it is filled up to the point that the output buffer is
  354. exhausted and EFI_WARN_BUFFER_TOO_SMALL is returned, and then BufferSize is
  355. returned with the size required to successfully complete the read. All
  356. other output parameters are returned with valid values. If the input buffer
  357. is NULL, it indicates the output buffer is to be allocated by this routine.
  358. In this case, this routine will allocate an appropriately sized buffer from
  359. boot services pool memory, which will be returned in Buffer. The size of
  360. the new buffer is returned in the buffer size parameter and all other
  361. output parameters are returned with valid values. This routine is callable
  362. only from TPL_NOTIFY and below. Behavior of this routine at any EFI_TPL
  363. above TPL_NOTIFY is undefined.
  364. Arguments:
  365. This - Supplies the protocol instance.
  366. NameGuid - Supplies a pointer to a GUID containing the name of the file.
  367. All firmware file names are EFI_GUIDs. Each firmware volume file name
  368. must be unique.
  369. Buffer - Supplies a pointer to a buffer in which the file contents are
  370. returned, not including the file header.
  371. BufferSize - Supplies a pointer that on input indicates the size of the
  372. supplied buffer in bytes.
  373. FoundType - Supplies a pointer where the file type will be returned.
  374. FileAttributes - Supplies a pointer where the file attributes will be
  375. returned.
  376. AuthenticationStatus - Supplies a pointer where the file authentication
  377. status will be returned.
  378. Return Value:
  379. EFI_SUCCESS on success.
  380. EFI_WARN_TOO_SMALL if the buffer is too small to contain the requested
  381. output. The buffer is filled and truncated.
  382. EFI_OUT_OF_RESOURCES if an allocation failed.
  383. EFI_NOT_FOUND if the given name was not in the firmware volume.
  384. EFI_DEVICE_ERROR if a hardware error occurred accessing the device.
  385. EFI_ACCESS_DENIED if the firmware volume is configured to disallow reads.
  386. --*/
  387. {
  388. PEFI_FIRMWARE_VOLUME Device;
  389. EFI_FFS_FILE_HEADER *FfsHeader;
  390. UINTN FileSize;
  391. UINTN InputBufferSize;
  392. EFI_FV_FILE_ATTRIBUTES LocalAttributes;
  393. EFI_FV_FILETYPE LocalFoundType;
  394. EFI_GUID SearchNameGuid;
  395. UINT8 *SourcePointer;
  396. EFI_STATUS Status;
  397. if (NameGuid == NULL) {
  398. return EFI_INVALID_PARAMETER;
  399. }
  400. Device = EFI_FIRMWARE_VOLUME_FROM_THIS(This);
  401. //
  402. // Keep working until the matching name GUID is found. The key is really an
  403. // FFS file list entry.
  404. //
  405. Device->LastKey = NULL;
  406. do {
  407. LocalFoundType = 0;
  408. Status = EfiFvGetNextFile(This,
  409. &(Device->LastKey),
  410. &LocalFoundType,
  411. &SearchNameGuid,
  412. &LocalAttributes,
  413. &FileSize);
  414. if (EFI_ERROR(Status)) {
  415. return EFI_NOT_FOUND;
  416. }
  417. } while (EfiCoreCompareGuids(&SearchNameGuid, (EFI_GUID *)NameGuid) ==
  418. FALSE);
  419. FfsHeader = Device->LastKey->FileHeader;
  420. InputBufferSize = *BufferSize;
  421. *FoundType = FfsHeader->Type;
  422. *FileAttributes = EfipFvConvertFfsAttributesToFileAttributes(
  423. FfsHeader->Attributes);
  424. if ((Device->VolumeHeader->Attributes & EFI_FVB_MEMORY_MAPPED) != 0) {
  425. *FileAttributes |= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED;
  426. }
  427. *AuthenticationStatus = 0;
  428. *BufferSize = FileSize;
  429. //
  430. // If the buffer is NULL, then just the information is needed.
  431. //
  432. if (Buffer == NULL) {
  433. return EFI_SUCCESS;
  434. }
  435. //
  436. // Skip over the file header.
  437. //
  438. if (EFI_IS_FFS_FILE2(FfsHeader)) {
  439. SourcePointer = ((UINT8 *)FfsHeader) + sizeof(EFI_FFS_FILE_HEADER2);
  440. } else {
  441. SourcePointer = ((UINT8 *)FfsHeader) + sizeof(EFI_FFS_FILE_HEADER);
  442. }
  443. Status = EFI_SUCCESS;
  444. if (*Buffer == NULL) {
  445. *Buffer = EfiCoreAllocateBootPool(FileSize);
  446. if (*Buffer == NULL) {
  447. return EFI_OUT_OF_RESOURCES;
  448. }
  449. } else if (FileSize > InputBufferSize) {
  450. Status = EFI_WARN_BUFFER_TOO_SMALL;
  451. FileSize = InputBufferSize;
  452. }
  453. EfiCoreCopyMemory(*Buffer, SourcePointer, FileSize);
  454. return Status;
  455. }
  456. EFIAPI
  457. EFI_STATUS
  458. EfiFvGetNextFile (
  459. CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,
  460. VOID *Key,
  461. EFI_FV_FILETYPE *FileType,
  462. EFI_GUID *NameGuid,
  463. EFI_FV_FILE_ATTRIBUTES *Attributes,
  464. UINTN *Size
  465. )
  466. /*++
  467. Routine Description:
  468. This routine retrieves information about the next file in the firmware
  469. volume store that matches the search criteria. This routine is the
  470. interface that is used to search a firmware volume for a particular file.
  471. It is called successively until the desired file is located or the function
  472. returns EFI_NOT_FOUND. To filter uninteresting files from the output, the
  473. type of file to search for may be specified in FileType. For example, if the
  474. file type is EFI_FV_FILETYPE_DRIVER, only files of this type will be
  475. returned in the output. If the file type is EFI_FV_FILETYPE_ALL, no
  476. filtering of file types is done. The key parameter is used to indicate a
  477. starting point of the search. If the value of the key parameter is
  478. completely initialized to zero, the search re-initialized and starts at the
  479. beginning. Subsequent calls to this routine must maintain the value of *Key
  480. returned by the immediately previous call. The actual contents of *Key are
  481. implementation specific and no semantic content is implied. This routine is
  482. callable only from TPL_NOTIFY and below. Behavior of this routine at any
  483. EFI_TPL above TPL_NOTIFY is undefined.
  484. Arguments:
  485. This - Supplies the protocol instance.
  486. Key - Supplies a pointer to a caller-allocated buffer containing the
  487. implementation-specific data used to track the current position of the
  488. search. The size of the buffer must be at least This->KeySize bytes
  489. long. To re-initialize the search and begin from the beginning of the
  490. firmware volume, the entire buffer must be cleared to zero. Other than
  491. clearing the buffer to initiate a new search, the caller must not
  492. modify the data in the buffer between calls to this routine.
  493. FileType - Supplies a pointer that on input contains the filter of file
  494. types to search for. If a file is found, its type will be returned
  495. here.
  496. NameGuid - Supplies a pointer where the name GUID of the file will be
  497. returned on success.
  498. Attributes - Supplies a pointer where the file attributes will be returned
  499. on success.
  500. Size - Supplies a pointer where the size of the file is returned on success.
  501. Return Value:
  502. EFI_SUCCESS on success.
  503. EFI_NOT_FOUND if no files of the given file type were found.
  504. EFI_DEVICE_ERROR if a hardware error occurred accessing the device.
  505. EFI_ACCESS_DENIED if the firmware volume does not allow reads.
  506. --*/
  507. {
  508. PLIST_ENTRY CurrentEntry;
  509. PEFI_FIRMWARE_VOLUME Device;
  510. PEFI_FFS_FILE_LIST_ENTRY FfsEntry;
  511. EFI_FFS_FILE_HEADER *FfsHeader;
  512. UINTN *KeyValue;
  513. EFI_STATUS Status;
  514. EFI_FV_ATTRIBUTES VolumeAttributes;
  515. Device = EFI_FIRMWARE_VOLUME_FROM_THIS(This);
  516. Status = EfiFvGetVolumeAttributes(This, &VolumeAttributes);
  517. if (EFI_ERROR(Status)) {
  518. return Status;
  519. }
  520. //
  521. // Fail if the read operation is not enabled.
  522. //
  523. if ((VolumeAttributes & EFI_FV2_READ_STATUS) == 0) {
  524. return EFI_ACCESS_DENIED;
  525. }
  526. if (*FileType > EFI_FV_FILETYPE_SMM_CORE) {
  527. return EFI_NOT_FOUND;
  528. }
  529. KeyValue = (UINTN *)Key;
  530. while (TRUE) {
  531. if (*KeyValue == 0) {
  532. CurrentEntry = &(Device->FfsFileList);
  533. } else {
  534. CurrentEntry = (PLIST_ENTRY)(*KeyValue);
  535. }
  536. //
  537. // If the next entry is the end of the list then there are no more
  538. // files.
  539. //
  540. if (CurrentEntry->Next == &(Device->FfsFileList)) {
  541. return EFI_NOT_FOUND;
  542. }
  543. FfsEntry = LIST_VALUE(CurrentEntry->Next,
  544. EFI_FFS_FILE_LIST_ENTRY,
  545. ListEntry);
  546. FfsHeader = FfsEntry->FileHeader;
  547. //
  548. // Save the key.
  549. //
  550. *KeyValue = (UINTN)&(FfsEntry->ListEntry);
  551. //
  552. // Stop if there's a match. Ignore pad files.
  553. //
  554. if (FfsHeader->Type == EFI_FV_FILETYPE_FFS_PAD) {
  555. continue;
  556. }
  557. if ((*FileType == EFI_FV_FILETYPE_ALL) ||
  558. (*FileType == FfsHeader->Type)) {
  559. break;
  560. }
  561. }
  562. //
  563. // Populate the return values.
  564. //
  565. *FileType = FfsHeader->Type;
  566. EfiCoreCopyMemory(NameGuid, &(FfsHeader->Name), sizeof(EFI_GUID));
  567. *Attributes = EfipFvConvertFfsAttributesToFileAttributes(
  568. FfsHeader->Attributes);
  569. if ((Device->VolumeHeader->Attributes & EFI_FVB_MEMORY_MAPPED) != 0) {
  570. *Attributes |= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED;
  571. }
  572. //
  573. // Subtract the size of the header.
  574. //
  575. if (EFI_IS_FFS_FILE2(FfsHeader)) {
  576. *Size = EFI_FFS_FILE2_SIZE(FfsHeader) - sizeof(EFI_FFS_FILE_HEADER2);
  577. } else {
  578. *Size = EFI_FFS_FILE_SIZE(FfsHeader) - sizeof(EFI_FFS_FILE_HEADER);
  579. }
  580. return EFI_SUCCESS;
  581. }
  582. EFIAPI
  583. EFI_STATUS
  584. EfiFvWriteFile (
  585. CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,
  586. UINT32 NumberOfFiles,
  587. EFI_FV_WRITE_POLICY WritePolicy,
  588. EFI_FV_WRITE_FILE_DATA *FileData
  589. )
  590. /*++
  591. Routine Description:
  592. This routine is used to write one or more files to a firmware volume. Each
  593. file to be written is described by an EFI_FV_WRITE_FILE_DATA structure.
  594. The caller must ensure that any required alignment for all files listed in
  595. the file data array is compatible with the firmware volume. Firmware volume
  596. capabilities can be determined using the get volume attributes function.
  597. Similarly, if the write policy is set to EFI_FV_RELIABLE_WRITE, the caller
  598. must check the firmware volume capabilities to ensure EFI_FV_RELIABLE_WRITE
  599. is supported by the firmware volume. EFI_FV_UNRELIABLE_WRITE must always be
  600. supported. Writing a file with a size of zero (FileData[n].BufferSize == 0)
  601. deletes the file from the firmware volume if it exists. Deleting a file
  602. must be done one at a time. Deleting a file as part of a multiple file
  603. write is not allowed. This routine is callable only from TPL_NOTIFY and
  604. below. Behavior of this routine at any EFI_TPL above TPL_NOTIFY is
  605. undefined.
  606. Arguments:
  607. This - Supplies the protocol instance.
  608. NumberOfFiles - Supplies the number of elements in the write file data
  609. array.
  610. WritePolicy - Supplies the level of reliability for the write in the event
  611. of a power failure or other system failure during the write operation.
  612. FileData - Supplies a pointer to an array of file data structures, where
  613. each element represents a file to be written.
  614. Return Value:
  615. EFI_SUCCESS on success.
  616. EFI_OUT_OF_RESOURCES if an allocation failed.
  617. EFI_NOT_FOUND if a delete was requested but the specified file was not
  618. found.
  619. EFI_DEVICE_ERROR if a hardware error occurred accessing the device.
  620. EFI_WRITE_PROTECTED if the firmware volume is configured to disallow writes.
  621. EFI_INVALID_PARAMETER if a delete was requested with multiple file writes,
  622. the write policy was unsupported, an unknown file type was specified, or a
  623. file system specific error occurred.
  624. --*/
  625. {
  626. return EFI_UNSUPPORTED;
  627. }
  628. //
  629. // --------------------------------------------------------- Internal Functions
  630. //
  631. EFI_FV_FILE_ATTRIBUTES
  632. EfipFvConvertFfsAttributesToFileAttributes (
  633. EFI_FFS_FILE_ATTRIBUTES FfsAttributes
  634. )
  635. /*++
  636. Routine Description:
  637. This routine converts FFS file attributes into Firmware Volume file
  638. attributes.
  639. Arguments:
  640. FfsAttributes - Supplies the FFS file attributes.
  641. Return Value:
  642. Returns the file attributes.
  643. --*/
  644. {
  645. UINT8 DataAlignment;
  646. EFI_FV_FILE_ATTRIBUTES FileAttributes;
  647. DataAlignment = (UINT8)((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT) >> 3);
  648. ASSERT(DataAlignment < 8);
  649. FileAttributes = EfiFvFfsAlignments[DataAlignment];
  650. if ((FfsAttributes & FFS_ATTRIB_FIXED) != 0) {
  651. FileAttributes |= EFI_FV_FILE_ATTRIB_FIXED;
  652. }
  653. return FileAttributes;
  654. }