FatLiteApi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /** @file
  2. FAT recovery PEIM entry point, Ppi Functions and FAT Api functions.
  3. Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "FatLitePeim.h"
  7. PEI_FAT_PRIVATE_DATA *mPrivateData = NULL;
  8. /**
  9. BlockIo installation notification function. Find out all the current BlockIO
  10. PPIs in the system and add them into private data. Assume there is
  11. @param PeiServices General purpose services available to every
  12. PEIM.
  13. @param NotifyDescriptor The typedef structure of the notification
  14. descriptor. Not used in this function.
  15. @param Ppi The typedef structure of the PPI descriptor.
  16. Not used in this function.
  17. @retval EFI_SUCCESS The function completed successfully.
  18. **/
  19. EFI_STATUS
  20. EFIAPI
  21. BlockIoNotifyEntry (
  22. IN EFI_PEI_SERVICES **PeiServices,
  23. IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
  24. IN VOID *Ppi
  25. );
  26. /**
  27. Discover all the block I/O devices to find the FAT volume.
  28. @param PrivateData Global memory map for accessing global
  29. variables.
  30. @param BlockIo2 Boolean to show whether using BlockIo2 or BlockIo
  31. @retval EFI_SUCCESS The function completed successfully.
  32. **/
  33. EFI_STATUS
  34. UpdateBlocksAndVolumes (
  35. IN OUT PEI_FAT_PRIVATE_DATA *PrivateData,
  36. IN BOOLEAN BlockIo2
  37. )
  38. {
  39. EFI_STATUS Status;
  40. EFI_PEI_PPI_DESCRIPTOR *TempPpiDescriptor;
  41. UINTN BlockIoPpiInstance;
  42. EFI_PEI_RECOVERY_BLOCK_IO_PPI *BlockIoPpi;
  43. EFI_PEI_RECOVERY_BLOCK_IO2_PPI *BlockIo2Ppi;
  44. UINTN NumberBlockDevices;
  45. UINTN Index;
  46. EFI_PEI_BLOCK_IO_MEDIA Media;
  47. EFI_PEI_BLOCK_IO2_MEDIA Media2;
  48. PEI_FAT_VOLUME Volume;
  49. EFI_PEI_SERVICES **PeiServices;
  50. PeiServices = (EFI_PEI_SERVICES **)GetPeiServicesTablePointer ();
  51. BlockIo2Ppi = NULL;
  52. BlockIoPpi = NULL;
  53. //
  54. // Clean up caches
  55. //
  56. for (Index = 0; Index < PEI_FAT_CACHE_SIZE; Index++) {
  57. PrivateData->CacheBuffer[Index].Valid = FALSE;
  58. }
  59. PrivateData->BlockDeviceCount = 0;
  60. //
  61. // Find out all Block Io Ppi instances within the system
  62. // Assuming all device Block Io Peims are dispatched already
  63. //
  64. for (BlockIoPpiInstance = 0; BlockIoPpiInstance < PEI_FAT_MAX_BLOCK_IO_PPI; BlockIoPpiInstance++) {
  65. if (BlockIo2) {
  66. Status = PeiServicesLocatePpi (
  67. &gEfiPeiVirtualBlockIo2PpiGuid,
  68. BlockIoPpiInstance,
  69. &TempPpiDescriptor,
  70. (VOID **)&BlockIo2Ppi
  71. );
  72. } else {
  73. Status = PeiServicesLocatePpi (
  74. &gEfiPeiVirtualBlockIoPpiGuid,
  75. BlockIoPpiInstance,
  76. &TempPpiDescriptor,
  77. (VOID **)&BlockIoPpi
  78. );
  79. }
  80. if (EFI_ERROR (Status)) {
  81. //
  82. // Done with all Block Io Ppis
  83. //
  84. break;
  85. }
  86. if (BlockIo2) {
  87. Status = BlockIo2Ppi->GetNumberOfBlockDevices (
  88. PeiServices,
  89. BlockIo2Ppi,
  90. &NumberBlockDevices
  91. );
  92. } else {
  93. Status = BlockIoPpi->GetNumberOfBlockDevices (
  94. PeiServices,
  95. BlockIoPpi,
  96. &NumberBlockDevices
  97. );
  98. }
  99. if (EFI_ERROR (Status)) {
  100. continue;
  101. }
  102. for (Index = 1; Index <= NumberBlockDevices && PrivateData->BlockDeviceCount < PEI_FAT_MAX_BLOCK_DEVICE; Index++) {
  103. if (BlockIo2) {
  104. Status = BlockIo2Ppi->GetBlockDeviceMediaInfo (
  105. PeiServices,
  106. BlockIo2Ppi,
  107. Index,
  108. &Media2
  109. );
  110. if (EFI_ERROR (Status) || !Media2.MediaPresent) {
  111. continue;
  112. }
  113. PrivateData->BlockDevice[PrivateData->BlockDeviceCount].BlockIo2 = BlockIo2Ppi;
  114. PrivateData->BlockDevice[PrivateData->BlockDeviceCount].InterfaceType = Media2.InterfaceType;
  115. PrivateData->BlockDevice[PrivateData->BlockDeviceCount].LastBlock = Media2.LastBlock;
  116. PrivateData->BlockDevice[PrivateData->BlockDeviceCount].BlockSize = Media2.BlockSize;
  117. } else {
  118. Status = BlockIoPpi->GetBlockDeviceMediaInfo (
  119. PeiServices,
  120. BlockIoPpi,
  121. Index,
  122. &Media
  123. );
  124. if (EFI_ERROR (Status) || !Media.MediaPresent) {
  125. continue;
  126. }
  127. PrivateData->BlockDevice[PrivateData->BlockDeviceCount].BlockIo = BlockIoPpi;
  128. PrivateData->BlockDevice[PrivateData->BlockDeviceCount].DevType = Media.DeviceType;
  129. PrivateData->BlockDevice[PrivateData->BlockDeviceCount].LastBlock = Media.LastBlock;
  130. PrivateData->BlockDevice[PrivateData->BlockDeviceCount].BlockSize = (UINT32)Media.BlockSize;
  131. }
  132. PrivateData->BlockDevice[PrivateData->BlockDeviceCount].IoAlign = 0;
  133. //
  134. // Not used here
  135. //
  136. PrivateData->BlockDevice[PrivateData->BlockDeviceCount].Logical = FALSE;
  137. PrivateData->BlockDevice[PrivateData->BlockDeviceCount].PartitionChecked = FALSE;
  138. PrivateData->BlockDevice[PrivateData->BlockDeviceCount].PhysicalDevNo = (UINT8)Index;
  139. PrivateData->BlockDeviceCount++;
  140. }
  141. }
  142. //
  143. // Find out all logical devices
  144. //
  145. FatFindPartitions (PrivateData);
  146. //
  147. // Build up file system volume array
  148. //
  149. PrivateData->VolumeCount = 0;
  150. for (Index = 0; Index < PrivateData->BlockDeviceCount; Index++) {
  151. Volume.BlockDeviceNo = Index;
  152. Status = FatGetBpbInfo (PrivateData, &Volume);
  153. if (Status == EFI_SUCCESS) {
  154. //
  155. // Add the detected volume to the volume array
  156. //
  157. CopyMem (
  158. (UINT8 *)&(PrivateData->Volume[PrivateData->VolumeCount]),
  159. (UINT8 *)&Volume,
  160. sizeof (PEI_FAT_VOLUME)
  161. );
  162. PrivateData->VolumeCount += 1;
  163. if (PrivateData->VolumeCount >= PEI_FAT_MAX_VOLUME) {
  164. break;
  165. }
  166. }
  167. }
  168. return EFI_SUCCESS;
  169. }
  170. /**
  171. BlockIo installation notification function. Find out all the current BlockIO
  172. PPIs in the system and add them into private data. Assume there is
  173. @param PeiServices General purpose services available to every
  174. PEIM.
  175. @param NotifyDescriptor The typedef structure of the notification
  176. descriptor. Not used in this function.
  177. @param Ppi The typedef structure of the PPI descriptor.
  178. Not used in this function.
  179. @retval EFI_SUCCESS The function completed successfully.
  180. **/
  181. EFI_STATUS
  182. EFIAPI
  183. BlockIoNotifyEntry (
  184. IN EFI_PEI_SERVICES **PeiServices,
  185. IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
  186. IN VOID *Ppi
  187. )
  188. {
  189. if (CompareGuid (NotifyDescriptor->Guid, &gEfiPeiVirtualBlockIo2PpiGuid)) {
  190. UpdateBlocksAndVolumes (mPrivateData, TRUE);
  191. } else {
  192. UpdateBlocksAndVolumes (mPrivateData, FALSE);
  193. }
  194. return EFI_SUCCESS;
  195. }
  196. /**
  197. Installs the Device Recovery Module PPI, Initialize BlockIo Ppi
  198. installation notification
  199. @param FileHandle Handle of the file being invoked. Type
  200. EFI_PEI_FILE_HANDLE is defined in
  201. FfsFindNextFile().
  202. @param PeiServices Describes the list of possible PEI Services.
  203. @retval EFI_SUCCESS The entry point was executed successfully.
  204. @retval EFI_OUT_OF_RESOURCES There is no enough memory to complete the
  205. operations.
  206. **/
  207. EFI_STATUS
  208. EFIAPI
  209. FatPeimEntry (
  210. IN EFI_PEI_FILE_HANDLE FileHandle,
  211. IN CONST EFI_PEI_SERVICES **PeiServices
  212. )
  213. {
  214. EFI_STATUS Status;
  215. EFI_PHYSICAL_ADDRESS Address;
  216. PEI_FAT_PRIVATE_DATA *PrivateData;
  217. Status = PeiServicesRegisterForShadow (FileHandle);
  218. if (!EFI_ERROR (Status)) {
  219. return Status;
  220. }
  221. Status = PeiServicesAllocatePages (
  222. EfiBootServicesCode,
  223. (sizeof (PEI_FAT_PRIVATE_DATA) - 1) / PEI_FAT_MEMORY_PAGE_SIZE + 1,
  224. &Address
  225. );
  226. if (EFI_ERROR (Status)) {
  227. return EFI_OUT_OF_RESOURCES;
  228. }
  229. PrivateData = (PEI_FAT_PRIVATE_DATA *)(UINTN)Address;
  230. //
  231. // Initialize Private Data (to zero, as is required by subsequent operations)
  232. //
  233. ZeroMem ((UINT8 *)PrivateData, sizeof (PEI_FAT_PRIVATE_DATA));
  234. PrivateData->Signature = PEI_FAT_PRIVATE_DATA_SIGNATURE;
  235. //
  236. // Installs Ppi
  237. //
  238. PrivateData->DeviceRecoveryPpi.GetNumberRecoveryCapsules = GetNumberRecoveryCapsules;
  239. PrivateData->DeviceRecoveryPpi.GetRecoveryCapsuleInfo = GetRecoveryCapsuleInfo;
  240. PrivateData->DeviceRecoveryPpi.LoadRecoveryCapsule = LoadRecoveryCapsule;
  241. PrivateData->PpiDescriptor.Flags = (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);
  242. PrivateData->PpiDescriptor.Guid = &gEfiPeiDeviceRecoveryModulePpiGuid;
  243. PrivateData->PpiDescriptor.Ppi = &PrivateData->DeviceRecoveryPpi;
  244. Status = PeiServicesInstallPpi (&PrivateData->PpiDescriptor);
  245. if (EFI_ERROR (Status)) {
  246. return EFI_OUT_OF_RESOURCES;
  247. }
  248. //
  249. // Other initializations
  250. //
  251. PrivateData->BlockDeviceCount = 0;
  252. UpdateBlocksAndVolumes (PrivateData, TRUE);
  253. UpdateBlocksAndVolumes (PrivateData, FALSE);
  254. //
  255. // PrivateData is allocated now, set it to the module variable
  256. //
  257. mPrivateData = PrivateData;
  258. //
  259. // Installs Block Io Ppi notification function
  260. //
  261. PrivateData->NotifyDescriptor[0].Flags =
  262. (
  263. EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
  264. );
  265. PrivateData->NotifyDescriptor[0].Guid = &gEfiPeiVirtualBlockIoPpiGuid;
  266. PrivateData->NotifyDescriptor[0].Notify = BlockIoNotifyEntry;
  267. PrivateData->NotifyDescriptor[1].Flags =
  268. (
  269. EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK |
  270. EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
  271. );
  272. PrivateData->NotifyDescriptor[1].Guid = &gEfiPeiVirtualBlockIo2PpiGuid;
  273. PrivateData->NotifyDescriptor[1].Notify = BlockIoNotifyEntry;
  274. return PeiServicesNotifyPpi (&PrivateData->NotifyDescriptor[0]);
  275. }
  276. /**
  277. Returns the number of DXE capsules residing on the device.
  278. This function searches for DXE capsules from the associated device and returns
  279. the number and maximum size in bytes of the capsules discovered. Entry 1 is
  280. assumed to be the highest load priority and entry N is assumed to be the lowest
  281. priority.
  282. @param[in] PeiServices General-purpose services that are available
  283. to every PEIM
  284. @param[in] This Indicates the EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
  285. instance.
  286. @param[out] NumberRecoveryCapsules Pointer to a caller-allocated UINTN. On
  287. output, *NumberRecoveryCapsules contains
  288. the number of recovery capsule images
  289. available for retrieval from this PEIM
  290. instance.
  291. @retval EFI_SUCCESS One or more capsules were discovered.
  292. @retval EFI_DEVICE_ERROR A device error occurred.
  293. @retval EFI_NOT_FOUND A recovery DXE capsule cannot be found.
  294. **/
  295. EFI_STATUS
  296. EFIAPI
  297. GetNumberRecoveryCapsules (
  298. IN EFI_PEI_SERVICES **PeiServices,
  299. IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI *This,
  300. OUT UINTN *NumberRecoveryCapsules
  301. )
  302. {
  303. EFI_STATUS Status;
  304. PEI_FAT_PRIVATE_DATA *PrivateData;
  305. UINTN Index;
  306. UINTN RecoveryCapsuleCount;
  307. PEI_FILE_HANDLE Handle;
  308. PrivateData = PEI_FAT_PRIVATE_DATA_FROM_THIS (This);
  309. //
  310. // Search each volume in the root directory for the Recovery capsule
  311. //
  312. RecoveryCapsuleCount = 0;
  313. for (Index = 0; Index < PrivateData->VolumeCount; Index++) {
  314. Status = FindRecoveryFile (PrivateData, Index, (CHAR16 *)PcdGetPtr (PcdRecoveryFileName), &Handle);
  315. if (EFI_ERROR (Status)) {
  316. continue;
  317. }
  318. RecoveryCapsuleCount++;
  319. }
  320. *NumberRecoveryCapsules = RecoveryCapsuleCount;
  321. if (*NumberRecoveryCapsules == 0) {
  322. return EFI_NOT_FOUND;
  323. }
  324. return EFI_SUCCESS;
  325. }
  326. /**
  327. Returns the size and type of the requested recovery capsule.
  328. This function gets the size and type of the capsule specified by CapsuleInstance.
  329. @param[in] PeiServices General-purpose services that are available to every PEIM
  330. @param[in] This Indicates the EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
  331. instance.
  332. @param[in] CapsuleInstance Specifies for which capsule instance to retrieve
  333. the information. This parameter must be between
  334. one and the value returned by GetNumberRecoveryCapsules()
  335. in NumberRecoveryCapsules.
  336. @param[out] Size A pointer to a caller-allocated UINTN in which
  337. the size of the requested recovery module is
  338. returned.
  339. @param[out] CapsuleType A pointer to a caller-allocated EFI_GUID in which
  340. the type of the requested recovery capsule is
  341. returned. The semantic meaning of the value
  342. returned is defined by the implementation.
  343. @retval EFI_SUCCESS One or more capsules were discovered.
  344. @retval EFI_DEVICE_ERROR A device error occurred.
  345. @retval EFI_NOT_FOUND A recovery DXE capsule cannot be found.
  346. **/
  347. EFI_STATUS
  348. EFIAPI
  349. GetRecoveryCapsuleInfo (
  350. IN EFI_PEI_SERVICES **PeiServices,
  351. IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI *This,
  352. IN UINTN CapsuleInstance,
  353. OUT UINTN *Size,
  354. OUT EFI_GUID *CapsuleType
  355. )
  356. {
  357. EFI_STATUS Status;
  358. PEI_FAT_PRIVATE_DATA *PrivateData;
  359. UINTN Index;
  360. UINTN BlockDeviceNo;
  361. UINTN RecoveryCapsuleCount;
  362. PEI_FILE_HANDLE Handle;
  363. UINTN NumberRecoveryCapsules;
  364. Status = GetNumberRecoveryCapsules (PeiServices, This, &NumberRecoveryCapsules);
  365. if (EFI_ERROR (Status)) {
  366. return Status;
  367. }
  368. if ((CapsuleInstance == 0) || (CapsuleInstance > NumberRecoveryCapsules)) {
  369. return EFI_NOT_FOUND;
  370. }
  371. PrivateData = PEI_FAT_PRIVATE_DATA_FROM_THIS (This);
  372. //
  373. // Search each volume in the root directory for the Recovery capsule
  374. //
  375. RecoveryCapsuleCount = 0;
  376. for (Index = 0; Index < PrivateData->VolumeCount; Index++) {
  377. Status = FindRecoveryFile (PrivateData, Index, (CHAR16 *)PcdGetPtr (PcdRecoveryFileName), &Handle);
  378. if (EFI_ERROR (Status)) {
  379. continue;
  380. }
  381. if (CapsuleInstance - 1 == RecoveryCapsuleCount) {
  382. //
  383. // Get file size
  384. //
  385. *Size = (UINTN)(((PEI_FAT_FILE *)Handle)->FileSize);
  386. //
  387. // Find corresponding physical block device
  388. //
  389. BlockDeviceNo = PrivateData->Volume[Index].BlockDeviceNo;
  390. while (PrivateData->BlockDevice[BlockDeviceNo].Logical && BlockDeviceNo < PrivateData->BlockDeviceCount) {
  391. BlockDeviceNo = PrivateData->BlockDevice[BlockDeviceNo].ParentDevNo;
  392. }
  393. //
  394. // Fill in the Capsule Type GUID according to the block device type
  395. //
  396. if (BlockDeviceNo < PrivateData->BlockDeviceCount) {
  397. if (PrivateData->BlockDevice[BlockDeviceNo].BlockIo2 != NULL) {
  398. switch (PrivateData->BlockDevice[BlockDeviceNo].InterfaceType) {
  399. case MSG_ATAPI_DP:
  400. CopyGuid (CapsuleType, &gRecoveryOnFatIdeDiskGuid);
  401. break;
  402. case MSG_USB_DP:
  403. CopyGuid (CapsuleType, &gRecoveryOnFatUsbDiskGuid);
  404. break;
  405. case MSG_NVME_NAMESPACE_DP:
  406. CopyGuid (CapsuleType, &gRecoveryOnFatNvmeDiskGuid);
  407. break;
  408. default:
  409. break;
  410. }
  411. }
  412. if (PrivateData->BlockDevice[BlockDeviceNo].BlockIo != NULL) {
  413. switch (PrivateData->BlockDevice[BlockDeviceNo].DevType) {
  414. case LegacyFloppy:
  415. CopyGuid (CapsuleType, &gRecoveryOnFatFloppyDiskGuid);
  416. break;
  417. case IdeCDROM:
  418. case IdeLS120:
  419. CopyGuid (CapsuleType, &gRecoveryOnFatIdeDiskGuid);
  420. break;
  421. case UsbMassStorage:
  422. CopyGuid (CapsuleType, &gRecoveryOnFatUsbDiskGuid);
  423. break;
  424. default:
  425. break;
  426. }
  427. }
  428. }
  429. return EFI_SUCCESS;
  430. }
  431. RecoveryCapsuleCount++;
  432. }
  433. return EFI_NOT_FOUND;
  434. }
  435. /**
  436. Loads a DXE capsule from some media into memory.
  437. This function, by whatever mechanism, retrieves a DXE capsule from some device
  438. and loads it into memory. Note that the published interface is device neutral.
  439. @param[in] PeiServices General-purpose services that are available
  440. to every PEIM
  441. @param[in] This Indicates the EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
  442. instance.
  443. @param[in] CapsuleInstance Specifies which capsule instance to retrieve.
  444. @param[out] Buffer Specifies a caller-allocated buffer in which
  445. the requested recovery capsule will be returned.
  446. @retval EFI_SUCCESS The capsule was loaded correctly.
  447. @retval EFI_DEVICE_ERROR A device error occurred.
  448. @retval EFI_NOT_FOUND A requested recovery DXE capsule cannot be found.
  449. **/
  450. EFI_STATUS
  451. EFIAPI
  452. LoadRecoveryCapsule (
  453. IN EFI_PEI_SERVICES **PeiServices,
  454. IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI *This,
  455. IN UINTN CapsuleInstance,
  456. OUT VOID *Buffer
  457. )
  458. {
  459. EFI_STATUS Status;
  460. PEI_FAT_PRIVATE_DATA *PrivateData;
  461. UINTN Index;
  462. UINTN RecoveryCapsuleCount;
  463. PEI_FILE_HANDLE Handle;
  464. UINTN NumberRecoveryCapsules;
  465. Status = GetNumberRecoveryCapsules (PeiServices, This, &NumberRecoveryCapsules);
  466. if (EFI_ERROR (Status)) {
  467. return Status;
  468. }
  469. if ((CapsuleInstance == 0) || (CapsuleInstance > NumberRecoveryCapsules)) {
  470. return EFI_NOT_FOUND;
  471. }
  472. PrivateData = PEI_FAT_PRIVATE_DATA_FROM_THIS (This);
  473. //
  474. // Search each volume in the root directory for the Recovery capsule
  475. //
  476. RecoveryCapsuleCount = 0;
  477. for (Index = 0; Index < PrivateData->VolumeCount; Index++) {
  478. Status = FindRecoveryFile (PrivateData, Index, (CHAR16 *)PcdGetPtr (PcdRecoveryFileName), &Handle);
  479. if (EFI_ERROR (Status)) {
  480. continue;
  481. }
  482. if (CapsuleInstance - 1 == RecoveryCapsuleCount) {
  483. Status = FatReadFile (
  484. PrivateData,
  485. Handle,
  486. (UINTN)(((PEI_FAT_FILE *)Handle)->FileSize),
  487. Buffer
  488. );
  489. return Status;
  490. }
  491. RecoveryCapsuleCount++;
  492. }
  493. return EFI_NOT_FOUND;
  494. }
  495. /**
  496. Finds the recovery file on a FAT volume.
  497. This function finds the recovery file named FileName on a specified FAT volume and returns
  498. its FileHandle pointer.
  499. @param PrivateData Global memory map for accessing global
  500. variables.
  501. @param VolumeIndex The index of the volume.
  502. @param FileName The recovery file name to find.
  503. @param Handle The output file handle.
  504. @retval EFI_DEVICE_ERROR Some error occurred when operating the FAT
  505. volume.
  506. @retval EFI_NOT_FOUND The recovery file was not found.
  507. @retval EFI_SUCCESS The recovery file was successfully found on the
  508. FAT volume.
  509. **/
  510. EFI_STATUS
  511. FindRecoveryFile (
  512. IN PEI_FAT_PRIVATE_DATA *PrivateData,
  513. IN UINTN VolumeIndex,
  514. IN CHAR16 *FileName,
  515. OUT PEI_FILE_HANDLE *Handle
  516. )
  517. {
  518. EFI_STATUS Status;
  519. PEI_FAT_FILE Parent;
  520. PEI_FAT_FILE *File;
  521. File = &PrivateData->File;
  522. //
  523. // VolumeIndex must be less than PEI_FAT_MAX_VOLUME because PrivateData->VolumeCount
  524. // cannot be larger than PEI_FAT_MAX_VOLUME when detecting recovery volume.
  525. //
  526. ASSERT (VolumeIndex < PEI_FAT_MAX_VOLUME);
  527. //
  528. // Construct root directory file
  529. //
  530. ZeroMem (&Parent, sizeof (PEI_FAT_FILE));
  531. Parent.IsFixedRootDir = (BOOLEAN)((PrivateData->Volume[VolumeIndex].FatType == Fat32) ? FALSE : TRUE);
  532. Parent.Attributes = FAT_ATTR_DIRECTORY;
  533. Parent.CurrentPos = 0;
  534. Parent.CurrentCluster = Parent.IsFixedRootDir ? 0 : PrivateData->Volume[VolumeIndex].RootDirCluster;
  535. Parent.StartingCluster = Parent.CurrentCluster;
  536. Parent.Volume = &PrivateData->Volume[VolumeIndex];
  537. Status = FatSetFilePos (PrivateData, &Parent, 0);
  538. if (EFI_ERROR (Status)) {
  539. return EFI_DEVICE_ERROR;
  540. }
  541. //
  542. // Search for recovery capsule in root directory
  543. //
  544. Status = FatReadNextDirectoryEntry (PrivateData, &Parent, File);
  545. while (Status == EFI_SUCCESS) {
  546. //
  547. // Compare whether the file name is recovery file name.
  548. //
  549. if (EngStriColl (PrivateData, FileName, File->FileName)) {
  550. break;
  551. }
  552. Status = FatReadNextDirectoryEntry (PrivateData, &Parent, File);
  553. }
  554. if (EFI_ERROR (Status)) {
  555. return EFI_NOT_FOUND;
  556. }
  557. //
  558. // Get the recovery file, set its file position to 0.
  559. //
  560. if (File->StartingCluster != 0) {
  561. Status = FatSetFilePos (PrivateData, File, 0);
  562. }
  563. *Handle = File;
  564. return EFI_SUCCESS;
  565. }