Info.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /** @file
  2. Routines dealing with setting/getting file/volume info
  3. Copyright (c) 2005 - 2015, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Fat.h"
  7. /**
  8. Get the volume's info into Buffer.
  9. @param Volume - FAT file system volume.
  10. @param BufferSize - Size of Buffer.
  11. @param Buffer - Buffer containing volume info.
  12. @retval EFI_SUCCESS - Get the volume info successfully.
  13. @retval EFI_BUFFER_TOO_SMALL - The buffer is too small.
  14. **/
  15. EFI_STATUS
  16. FatGetVolumeInfo (
  17. IN FAT_VOLUME *Volume,
  18. IN OUT UINTN *BufferSize,
  19. OUT VOID *Buffer
  20. );
  21. /**
  22. Set the volume's info.
  23. @param Volume - FAT file system volume.
  24. @param BufferSize - Size of Buffer.
  25. @param Buffer - Buffer containing the new volume info.
  26. @retval EFI_SUCCESS - Set the volume info successfully.
  27. @retval EFI_BAD_BUFFER_SIZE - The buffer size is error.
  28. @retval EFI_WRITE_PROTECTED - The volume is read only.
  29. @return other - An error occurred when operation the disk.
  30. **/
  31. EFI_STATUS
  32. FatSetVolumeInfo (
  33. IN FAT_VOLUME *Volume,
  34. IN UINTN BufferSize,
  35. IN VOID *Buffer
  36. );
  37. /**
  38. Set or Get the some types info of the file into Buffer.
  39. @param IsSet - TRUE:The access is set, else is get
  40. @param FHand - The handle of file
  41. @param Type - The type of the info
  42. @param BufferSize - Size of Buffer
  43. @param Buffer - Buffer containing volume info
  44. @retval EFI_SUCCESS - Get the info successfully
  45. @retval EFI_DEVICE_ERROR - Can not find the OFile for the file
  46. **/
  47. EFI_STATUS
  48. FatSetOrGetInfo (
  49. IN BOOLEAN IsSet,
  50. IN EFI_FILE_PROTOCOL *FHand,
  51. IN EFI_GUID *Type,
  52. IN OUT UINTN *BufferSize,
  53. IN OUT VOID *Buffer
  54. );
  55. /**
  56. Get the open file's info into Buffer.
  57. @param OFile - The open file.
  58. @param BufferSize - Size of Buffer.
  59. @param Buffer - Buffer containing file info.
  60. @retval EFI_SUCCESS - Get the file info successfully.
  61. @retval EFI_BUFFER_TOO_SMALL - The buffer is too small.
  62. **/
  63. EFI_STATUS
  64. FatGetFileInfo (
  65. IN FAT_OFILE *OFile,
  66. IN OUT UINTN *BufferSize,
  67. OUT VOID *Buffer
  68. )
  69. {
  70. return FatGetDirEntInfo (OFile->Volume, OFile->DirEnt, BufferSize, Buffer);
  71. }
  72. /**
  73. Get the volume's info into Buffer.
  74. @param Volume - FAT file system volume.
  75. @param BufferSize - Size of Buffer.
  76. @param Buffer - Buffer containing volume info.
  77. @retval EFI_SUCCESS - Get the volume info successfully.
  78. @retval EFI_BUFFER_TOO_SMALL - The buffer is too small.
  79. **/
  80. EFI_STATUS
  81. FatGetVolumeInfo (
  82. IN FAT_VOLUME *Volume,
  83. IN OUT UINTN *BufferSize,
  84. OUT VOID *Buffer
  85. )
  86. {
  87. UINTN Size;
  88. UINTN NameSize;
  89. UINTN ResultSize;
  90. CHAR16 Name[FAT_NAME_LEN + 1];
  91. EFI_STATUS Status;
  92. EFI_FILE_SYSTEM_INFO *Info;
  93. UINT8 ClusterAlignment;
  94. Size = SIZE_OF_EFI_FILE_SYSTEM_INFO;
  95. Status = FatGetVolumeEntry (Volume, Name);
  96. NameSize = StrSize (Name);
  97. ResultSize = Size + NameSize;
  98. ClusterAlignment = Volume->ClusterAlignment;
  99. //
  100. // If we don't have valid info, compute it now
  101. //
  102. FatComputeFreeInfo (Volume);
  103. Status = EFI_BUFFER_TOO_SMALL;
  104. if (*BufferSize >= ResultSize) {
  105. Status = EFI_SUCCESS;
  106. Info = Buffer;
  107. ZeroMem (Info, SIZE_OF_EFI_FILE_SYSTEM_INFO);
  108. Info->Size = ResultSize;
  109. Info->ReadOnly = Volume->ReadOnly;
  110. Info->BlockSize = (UINT32) Volume->ClusterSize;
  111. Info->VolumeSize = LShiftU64 (Volume->MaxCluster, ClusterAlignment);
  112. Info->FreeSpace = LShiftU64 (
  113. Volume->FatInfoSector.FreeInfo.ClusterCount,
  114. ClusterAlignment
  115. );
  116. CopyMem ((CHAR8 *) Buffer + Size, Name, NameSize);
  117. }
  118. *BufferSize = ResultSize;
  119. return Status;
  120. }
  121. /**
  122. Get the volume's label info into Buffer.
  123. @param Volume - FAT file system volume.
  124. @param BufferSize - Size of Buffer.
  125. @param Buffer - Buffer containing volume's label info.
  126. @retval EFI_SUCCESS - Get the volume's label info successfully.
  127. @retval EFI_BUFFER_TOO_SMALL - The buffer is too small.
  128. **/
  129. EFI_STATUS
  130. FatGetVolumeLabelInfo (
  131. IN FAT_VOLUME *Volume,
  132. IN OUT UINTN *BufferSize,
  133. OUT VOID *Buffer
  134. )
  135. {
  136. UINTN Size;
  137. UINTN NameSize;
  138. UINTN ResultSize;
  139. CHAR16 Name[FAT_NAME_LEN + 1];
  140. EFI_STATUS Status;
  141. Size = SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL;
  142. Status = FatGetVolumeEntry (Volume, Name);
  143. NameSize = StrSize (Name);
  144. ResultSize = Size + NameSize;
  145. Status = EFI_BUFFER_TOO_SMALL;
  146. if (*BufferSize >= ResultSize) {
  147. Status = EFI_SUCCESS;
  148. CopyMem ((CHAR8 *) Buffer + Size, Name, NameSize);
  149. }
  150. *BufferSize = ResultSize;
  151. return Status;
  152. }
  153. /**
  154. Set the volume's info.
  155. @param Volume - FAT file system volume.
  156. @param BufferSize - Size of Buffer.
  157. @param Buffer - Buffer containing the new volume info.
  158. @retval EFI_SUCCESS - Set the volume info successfully.
  159. @retval EFI_BAD_BUFFER_SIZE - The buffer size is error.
  160. @retval EFI_WRITE_PROTECTED - The volume is read only.
  161. @return other - An error occurred when operation the disk.
  162. **/
  163. EFI_STATUS
  164. FatSetVolumeInfo (
  165. IN FAT_VOLUME *Volume,
  166. IN UINTN BufferSize,
  167. IN VOID *Buffer
  168. )
  169. {
  170. EFI_FILE_SYSTEM_INFO *Info;
  171. Info = (EFI_FILE_SYSTEM_INFO *) Buffer;
  172. if (BufferSize < SIZE_OF_EFI_FILE_SYSTEM_INFO + 2 || Info->Size > BufferSize) {
  173. return EFI_BAD_BUFFER_SIZE;
  174. }
  175. return FatSetVolumeEntry (Volume, Info->VolumeLabel);
  176. }
  177. /**
  178. Set the volume's label info.
  179. @param Volume - FAT file system volume.
  180. @param BufferSize - Size of Buffer.
  181. @param Buffer - Buffer containing the new volume label info.
  182. @retval EFI_SUCCESS - Set the volume label info successfully.
  183. @retval EFI_WRITE_PROTECTED - The disk is write protected.
  184. @retval EFI_BAD_BUFFER_SIZE - The buffer size is error.
  185. @return other - An error occurred when operation the disk.
  186. **/
  187. EFI_STATUS
  188. FatSetVolumeLabelInfo (
  189. IN FAT_VOLUME *Volume,
  190. IN UINTN BufferSize,
  191. IN VOID *Buffer
  192. )
  193. {
  194. EFI_FILE_SYSTEM_VOLUME_LABEL *Info;
  195. Info = (EFI_FILE_SYSTEM_VOLUME_LABEL *) Buffer;
  196. if (BufferSize < SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL + 2) {
  197. return EFI_BAD_BUFFER_SIZE;
  198. }
  199. return FatSetVolumeEntry (Volume, Info->VolumeLabel);
  200. }
  201. /**
  202. Set the file info.
  203. @param Volume - FAT file system volume.
  204. @param IFile - The instance of the open file.
  205. @param OFile - The open file.
  206. @param BufferSize - Size of Buffer.
  207. @param Buffer - Buffer containing the new file info.
  208. @retval EFI_SUCCESS - Set the file info successfully.
  209. @retval EFI_ACCESS_DENIED - It is the root directory
  210. or the directory attribute bit can not change
  211. or try to change a directory size
  212. or something else.
  213. @retval EFI_UNSUPPORTED - The new file size is larger than 4GB.
  214. @retval EFI_WRITE_PROTECTED - The disk is write protected.
  215. @retval EFI_BAD_BUFFER_SIZE - The buffer size is error.
  216. @retval EFI_INVALID_PARAMETER - The time info or attributes info is error.
  217. @retval EFI_OUT_OF_RESOURCES - Can not allocate new memory.
  218. @retval EFI_VOLUME_CORRUPTED - The volume is corrupted.
  219. @return other - An error occurred when operation the disk.
  220. **/
  221. EFI_STATUS
  222. FatSetFileInfo (
  223. IN FAT_VOLUME *Volume,
  224. IN FAT_IFILE *IFile,
  225. IN FAT_OFILE *OFile,
  226. IN UINTN BufferSize,
  227. IN VOID *Buffer
  228. )
  229. {
  230. EFI_STATUS Status;
  231. EFI_FILE_INFO *NewInfo;
  232. FAT_OFILE *DotOFile;
  233. FAT_OFILE *Parent;
  234. CHAR16 NewFileName[EFI_PATH_STRING_LENGTH];
  235. EFI_TIME ZeroTime;
  236. FAT_DIRENT *DirEnt;
  237. FAT_DIRENT *TempDirEnt;
  238. UINT8 NewAttribute;
  239. BOOLEAN ReadOnly;
  240. ZeroMem (&ZeroTime, sizeof (EFI_TIME));
  241. Parent = OFile->Parent;
  242. DirEnt = OFile->DirEnt;
  243. //
  244. // If this is the root directory, we can't make any updates
  245. //
  246. if (Parent == NULL) {
  247. return EFI_ACCESS_DENIED;
  248. }
  249. //
  250. // Make sure there's a valid input buffer
  251. //
  252. NewInfo = Buffer;
  253. if (BufferSize < SIZE_OF_EFI_FILE_INFO + 2 || NewInfo->Size > BufferSize) {
  254. return EFI_BAD_BUFFER_SIZE;
  255. }
  256. ReadOnly = (BOOLEAN)(IFile->ReadOnly || (DirEnt->Entry.Attributes & EFI_FILE_READ_ONLY));
  257. //
  258. // if a zero time is specified, then the original time is preserved
  259. //
  260. if (CompareMem (&ZeroTime, &NewInfo->CreateTime, sizeof (EFI_TIME)) != 0) {
  261. if (!FatIsValidTime (&NewInfo->CreateTime)) {
  262. return EFI_INVALID_PARAMETER;
  263. }
  264. if (!ReadOnly) {
  265. FatEfiTimeToFatTime (&NewInfo->CreateTime, &DirEnt->Entry.FileCreateTime);
  266. }
  267. }
  268. if (CompareMem (&ZeroTime, &NewInfo->ModificationTime, sizeof (EFI_TIME)) != 0) {
  269. if (!FatIsValidTime (&NewInfo->ModificationTime)) {
  270. return EFI_INVALID_PARAMETER;
  271. }
  272. if (!ReadOnly) {
  273. FatEfiTimeToFatTime (&NewInfo->ModificationTime, &DirEnt->Entry.FileModificationTime);
  274. }
  275. OFile->PreserveLastModification = TRUE;
  276. }
  277. if (NewInfo->Attribute & (~EFI_FILE_VALID_ATTR)) {
  278. return EFI_INVALID_PARAMETER;
  279. }
  280. NewAttribute = (UINT8) NewInfo->Attribute;
  281. //
  282. // Can not change the directory attribute bit
  283. //
  284. if ((NewAttribute ^ DirEnt->Entry.Attributes) & EFI_FILE_DIRECTORY) {
  285. return EFI_ACCESS_DENIED;
  286. }
  287. //
  288. // Set the current attributes even if the IFile->ReadOnly is TRUE
  289. //
  290. DirEnt->Entry.Attributes = (UINT8) ((DirEnt->Entry.Attributes &~EFI_FILE_VALID_ATTR) | NewAttribute);
  291. //
  292. // Open the filename and see if it refers to an existing file
  293. //
  294. Status = FatLocateOFile (&Parent, NewInfo->FileName, DirEnt->Entry.Attributes, NewFileName);
  295. if (EFI_ERROR (Status)) {
  296. return Status;
  297. }
  298. if (*NewFileName != 0) {
  299. //
  300. // File was not found. We do not allow rename of the current directory if
  301. // there are open files below the current directory
  302. //
  303. if (!IsListEmpty (&OFile->ChildHead) || Parent == OFile) {
  304. return EFI_ACCESS_DENIED;
  305. }
  306. if (ReadOnly) {
  307. return EFI_ACCESS_DENIED;
  308. }
  309. Status = FatRemoveDirEnt (OFile->Parent, DirEnt);
  310. if (EFI_ERROR (Status)) {
  311. return Status;
  312. }
  313. //
  314. // Create new dirent
  315. //
  316. Status = FatCreateDirEnt (Parent, NewFileName, DirEnt->Entry.Attributes, &TempDirEnt);
  317. if (EFI_ERROR (Status)) {
  318. return Status;
  319. }
  320. FatCloneDirEnt (TempDirEnt, DirEnt);
  321. FatFreeDirEnt (DirEnt);
  322. DirEnt = TempDirEnt;
  323. DirEnt->OFile = OFile;
  324. OFile->DirEnt = DirEnt;
  325. OFile->Parent = Parent;
  326. RemoveEntryList (&OFile->ChildLink);
  327. InsertHeadList (&Parent->ChildHead, &OFile->ChildLink);
  328. //
  329. // If this is a directory, synchronize its dot directory entry
  330. //
  331. if (OFile->ODir != NULL) {
  332. //
  333. // Syncronize its dot entry
  334. //
  335. FatResetODirCursor (OFile);
  336. ASSERT (OFile->Parent != NULL);
  337. for (DotOFile = OFile; DotOFile != OFile->Parent->Parent; DotOFile = DotOFile->Parent) {
  338. Status = FatGetNextDirEnt (OFile, &DirEnt);
  339. if (EFI_ERROR (Status) || DirEnt == NULL || !FatIsDotDirEnt (DirEnt)) {
  340. return EFI_VOLUME_CORRUPTED;
  341. }
  342. FatCloneDirEnt (DirEnt, DotOFile->DirEnt);
  343. Status = FatStoreDirEnt (OFile, DirEnt);
  344. if (EFI_ERROR (Status)) {
  345. return Status;
  346. }
  347. }
  348. }
  349. //
  350. // If the file is renamed, we should append the ARCHIVE attribute
  351. //
  352. OFile->Archive = TRUE;
  353. } else if (Parent != OFile) {
  354. //
  355. // filename is to a different filename that already exists
  356. //
  357. return EFI_ACCESS_DENIED;
  358. }
  359. //
  360. // If the file size has changed, apply it
  361. //
  362. if (NewInfo->FileSize != OFile->FileSize) {
  363. if (OFile->ODir != NULL || ReadOnly) {
  364. //
  365. // If this is a directory or the file is read only, we can't change the file size
  366. //
  367. return EFI_ACCESS_DENIED;
  368. }
  369. if (NewInfo->FileSize > OFile->FileSize) {
  370. Status = FatExpandOFile (OFile, NewInfo->FileSize);
  371. } else {
  372. Status = FatTruncateOFile (OFile, (UINTN) NewInfo->FileSize);
  373. }
  374. if (EFI_ERROR (Status)) {
  375. return Status;
  376. }
  377. FatUpdateDirEntClusterSizeInfo (OFile);
  378. }
  379. OFile->Dirty = TRUE;
  380. return FatOFileFlush (OFile);
  381. }
  382. /**
  383. Set or Get the some types info of the file into Buffer.
  384. @param IsSet - TRUE:The access is set, else is get
  385. @param FHand - The handle of file
  386. @param Type - The type of the info
  387. @param BufferSize - Size of Buffer
  388. @param Buffer - Buffer containing volume info
  389. @retval EFI_SUCCESS - Get the info successfully
  390. @retval EFI_DEVICE_ERROR - Can not find the OFile for the file
  391. **/
  392. EFI_STATUS
  393. FatSetOrGetInfo (
  394. IN BOOLEAN IsSet,
  395. IN EFI_FILE_PROTOCOL *FHand,
  396. IN EFI_GUID *Type,
  397. IN OUT UINTN *BufferSize,
  398. IN OUT VOID *Buffer
  399. )
  400. {
  401. FAT_IFILE *IFile;
  402. FAT_OFILE *OFile;
  403. FAT_VOLUME *Volume;
  404. EFI_STATUS Status;
  405. IFile = IFILE_FROM_FHAND (FHand);
  406. OFile = IFile->OFile;
  407. Volume = OFile->Volume;
  408. Status = OFile->Error;
  409. if (Status == EFI_NOT_FOUND) {
  410. return EFI_DEVICE_ERROR;
  411. }
  412. FatWaitNonblockingTask (IFile);
  413. FatAcquireLock ();
  414. //
  415. // Verify the file handle isn't in an error state
  416. //
  417. if (!EFI_ERROR (Status)) {
  418. //
  419. // Get the proper information based on the request
  420. //
  421. Status = EFI_UNSUPPORTED;
  422. if (IsSet) {
  423. if (CompareGuid (Type, &gEfiFileInfoGuid)) {
  424. Status = Volume->ReadOnly ? EFI_WRITE_PROTECTED : FatSetFileInfo (Volume, IFile, OFile, *BufferSize, Buffer);
  425. }
  426. if (CompareGuid (Type, &gEfiFileSystemInfoGuid)) {
  427. Status = Volume->ReadOnly ? EFI_WRITE_PROTECTED : FatSetVolumeInfo (Volume, *BufferSize, Buffer);
  428. }
  429. if (CompareGuid (Type, &gEfiFileSystemVolumeLabelInfoIdGuid)) {
  430. Status = Volume->ReadOnly ? EFI_WRITE_PROTECTED : FatSetVolumeLabelInfo (Volume, *BufferSize, Buffer);
  431. }
  432. } else {
  433. if (CompareGuid (Type, &gEfiFileInfoGuid)) {
  434. Status = FatGetFileInfo (OFile, BufferSize, Buffer);
  435. }
  436. if (CompareGuid (Type, &gEfiFileSystemInfoGuid)) {
  437. Status = FatGetVolumeInfo (Volume, BufferSize, Buffer);
  438. }
  439. if (CompareGuid (Type, &gEfiFileSystemVolumeLabelInfoIdGuid)) {
  440. Status = FatGetVolumeLabelInfo (Volume, BufferSize, Buffer);
  441. }
  442. }
  443. }
  444. Status = FatCleanupVolume (Volume, NULL, Status, NULL);
  445. FatReleaseLock ();
  446. return Status;
  447. }
  448. /**
  449. Get the some types info of the file into Buffer.
  450. @param FHand - The handle of file.
  451. @param Type - The type of the info.
  452. @param BufferSize - Size of Buffer.
  453. @param Buffer - Buffer containing volume info.
  454. @retval EFI_SUCCESS - Get the info successfully.
  455. @retval EFI_DEVICE_ERROR - Can not find the OFile for the file.
  456. **/
  457. EFI_STATUS
  458. EFIAPI
  459. FatGetInfo (
  460. IN EFI_FILE_PROTOCOL *FHand,
  461. IN EFI_GUID *Type,
  462. IN OUT UINTN *BufferSize,
  463. OUT VOID *Buffer
  464. )
  465. {
  466. return FatSetOrGetInfo (FALSE, FHand, Type, BufferSize, Buffer);
  467. }
  468. /**
  469. Set the some types info of the file into Buffer.
  470. @param FHand - The handle of file.
  471. @param Type - The type of the info.
  472. @param BufferSize - Size of Buffer
  473. @param Buffer - Buffer containing volume info.
  474. @retval EFI_SUCCESS - Set the info successfully.
  475. @retval EFI_DEVICE_ERROR - Can not find the OFile for the file.
  476. **/
  477. EFI_STATUS
  478. EFIAPI
  479. FatSetInfo (
  480. IN EFI_FILE_PROTOCOL *FHand,
  481. IN EFI_GUID *Type,
  482. IN UINTN BufferSize,
  483. IN VOID *Buffer
  484. )
  485. {
  486. return FatSetOrGetInfo (TRUE, FHand, Type, &BufferSize, Buffer);
  487. }