Misc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*++
  2. Copyright (c) 2005 - 2010, Intel Corporation. All rights reserved.<BR>
  3. This program and the accompanying materials are licensed and made available
  4. under the terms and conditions of the BSD License which accompanies this
  5. distribution. The full text of the license may be found at
  6. http://opensource.org/licenses/bsd-license.php
  7. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  9. Module Name:
  10. Misc.c
  11. Abstract:
  12. Miscellaneous functions
  13. Revision History
  14. --*/
  15. #include "Fat.h"
  16. EFI_STATUS
  17. FatAccessVolumeDirty (
  18. IN FAT_VOLUME *Volume,
  19. IN IO_MODE IoMode,
  20. IN VOID *DirtyValue
  21. )
  22. /*++
  23. Routine Description:
  24. Set the volume as dirty or not
  25. Arguments:
  26. Volume - FAT file system volume.
  27. IoMode - The access mode.
  28. DirtyValue - Set the volume as dirty or not.
  29. Returns:
  30. EFI_SUCCESS - Set the new FAT entry value sucessfully.
  31. other - An error occurred when operation the FAT entries.
  32. --*/
  33. {
  34. UINTN WriteCount;
  35. WriteCount = Volume->FatEntrySize;
  36. return FatDiskIo (Volume, IoMode, Volume->FatPos + WriteCount, WriteCount, DirtyValue);
  37. }
  38. EFI_STATUS
  39. FatDiskIo (
  40. IN FAT_VOLUME *Volume,
  41. IN IO_MODE IoMode,
  42. IN UINT64 Offset,
  43. IN UINTN BufferSize,
  44. IN OUT VOID *Buffer
  45. )
  46. /*++
  47. Routine Description:
  48. General disk access function
  49. Arguments:
  50. Volume - FAT file system volume.
  51. IoMode - The access mode (disk read/write or cache access).
  52. Offset - The starting byte offset to read from.
  53. BufferSize - Size of Buffer.
  54. Buffer - Buffer containing read data.
  55. Returns:
  56. EFI_SUCCESS - The operation is performed successfully.
  57. EFI_VOLUME_CORRUPTED - The accesss is
  58. Others - The status of read/write the disk
  59. --*/
  60. {
  61. EFI_STATUS Status;
  62. EFI_DISK_IO_PROTOCOL *DiskIo;
  63. EFI_DISK_READ IoFunction;
  64. //
  65. // Verify the IO is in devices range
  66. //
  67. Status = EFI_VOLUME_CORRUPTED;
  68. if (Offset + BufferSize <= Volume->VolumeSize) {
  69. if (CACHE_ENABLED (IoMode)) {
  70. //
  71. // Access cache
  72. //
  73. Status = FatAccessCache (Volume, CACHE_TYPE (IoMode), RAW_ACCESS (IoMode), Offset, BufferSize, Buffer);
  74. } else {
  75. //
  76. // Access disk directly
  77. //
  78. DiskIo = Volume->DiskIo;
  79. IoFunction = (IoMode == READ_DISK) ? DiskIo->ReadDisk : DiskIo->WriteDisk;
  80. Status = IoFunction (DiskIo, Volume->MediaId, Offset, BufferSize, Buffer);
  81. }
  82. }
  83. if (EFI_ERROR (Status)) {
  84. Volume->DiskError = TRUE;
  85. DEBUG ((EFI_D_INFO, "FatDiskIo: error %r\n", Status));
  86. }
  87. return Status;
  88. }
  89. VOID
  90. FatAcquireLock (
  91. VOID
  92. )
  93. /*++
  94. Routine Description:
  95. Lock the volume.
  96. Arguments:
  97. None.
  98. Returns:
  99. None.
  100. --*/
  101. {
  102. EfiAcquireLock (&FatFsLock);
  103. }
  104. EFI_STATUS
  105. FatAcquireLockOrFail (
  106. VOID
  107. )
  108. /*++
  109. Routine Description:
  110. Lock the volume.
  111. If the lock is already in the acquired state, then EFI_ACCESS_DENIED is returned.
  112. Otherwise, EFI_SUCCESS is returned.
  113. Arguments:
  114. None.
  115. Returns:
  116. EFI_SUCCESS - The volume is locked.
  117. EFI_ACCESS_DENIED - The volume could not be locked because it is already locked.
  118. --*/
  119. {
  120. return EfiAcquireLockOrFail (&FatFsLock);
  121. }
  122. VOID
  123. FatReleaseLock (
  124. VOID
  125. )
  126. /*++
  127. Routine Description:
  128. Unlock the volume.
  129. Arguments:
  130. Null.
  131. Returns:
  132. None.
  133. --*/
  134. {
  135. EfiReleaseLock (&FatFsLock);
  136. }
  137. VOID
  138. FatFreeDirEnt (
  139. IN FAT_DIRENT *DirEnt
  140. )
  141. /*++
  142. Routine Description:
  143. Free directory entry.
  144. Arguments:
  145. DirEnt - The directory entry to be freed.
  146. Returns:
  147. None.
  148. --*/
  149. {
  150. if (DirEnt->FileString != NULL) {
  151. FreePool (DirEnt->FileString);
  152. }
  153. FreePool (DirEnt);
  154. }
  155. VOID
  156. FatFreeVolume (
  157. IN FAT_VOLUME *Volume
  158. )
  159. /*++
  160. Routine Description:
  161. Free volume structure (including the contents of directory cache and disk cache).
  162. Arguments:
  163. Volume - The volume structure to be freed.
  164. Returns:
  165. None.
  166. --*/
  167. {
  168. //
  169. // Free disk cache
  170. //
  171. if (Volume->CacheBuffer != NULL) {
  172. FreePool (Volume->CacheBuffer);
  173. }
  174. //
  175. // Free directory cache
  176. //
  177. FatCleanupODirCache (Volume);
  178. FreePool (Volume);
  179. }
  180. VOID
  181. FatEfiTimeToFatTime (
  182. IN EFI_TIME *ETime,
  183. OUT FAT_DATE_TIME *FTime
  184. )
  185. /*++
  186. Routine Description:
  187. Translate EFI time to FAT time.
  188. Arguments:
  189. ETime - The time of EFI_TIME.
  190. FTime - The time of FAT_DATE_TIME.
  191. Returns:
  192. None.
  193. --*/
  194. {
  195. //
  196. // ignores timezone info in source ETime
  197. //
  198. if (ETime->Year > 1980) {
  199. FTime->Date.Year = (UINT16) (ETime->Year - 1980);
  200. }
  201. if (ETime->Year >= 1980 + FAT_MAX_YEAR_FROM_1980) {
  202. FTime->Date.Year = FAT_MAX_YEAR_FROM_1980;
  203. }
  204. FTime->Date.Month = ETime->Month;
  205. FTime->Date.Day = ETime->Day;
  206. FTime->Time.Hour = ETime->Hour;
  207. FTime->Time.Minute = ETime->Minute;
  208. FTime->Time.DoubleSecond = (UINT16) (ETime->Second / 2);
  209. }
  210. VOID
  211. FatFatTimeToEfiTime (
  212. IN FAT_DATE_TIME *FTime,
  213. OUT EFI_TIME *ETime
  214. )
  215. /*++
  216. Routine Description:
  217. Translate Fat time to EFI time.
  218. Arguments:
  219. FTime - The time of FAT_DATE_TIME.
  220. ETime - The time of EFI_TIME.
  221. Returns:
  222. None.
  223. --*/
  224. {
  225. ETime->Year = (UINT16) (FTime->Date.Year + 1980);
  226. ETime->Month = (UINT8) FTime->Date.Month;
  227. ETime->Day = (UINT8) FTime->Date.Day;
  228. ETime->Hour = (UINT8) FTime->Time.Hour;
  229. ETime->Minute = (UINT8) FTime->Time.Minute;
  230. ETime->Second = (UINT8) (FTime->Time.DoubleSecond * 2);
  231. ETime->Nanosecond = 0;
  232. ETime->TimeZone = EFI_UNSPECIFIED_TIMEZONE;
  233. ETime->Daylight = 0;
  234. }
  235. VOID
  236. FatGetCurrentFatTime (
  237. OUT FAT_DATE_TIME *FatNow
  238. )
  239. /*++
  240. Routine Description:
  241. Get Current FAT time.
  242. Arguments:
  243. FatNow - Current FAT time.
  244. Returns:
  245. None.
  246. --*/
  247. {
  248. EFI_TIME Now;
  249. gRT->GetTime (&Now, NULL);
  250. FatEfiTimeToFatTime (&Now, FatNow);
  251. }
  252. BOOLEAN
  253. FatIsValidTime (
  254. IN EFI_TIME *Time
  255. )
  256. /*++
  257. Routine Description:
  258. Check whether a time is valid.
  259. Arguments:
  260. Time - The time of EFI_TIME.
  261. Returns:
  262. TRUE - The time is valid.
  263. FALSE - The time is not valid.
  264. --*/
  265. {
  266. static UINT8 MonthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  267. UINTN Day;
  268. BOOLEAN ValidTime;
  269. ValidTime = TRUE;
  270. //
  271. // Check the fields for range problems
  272. // Fat can only support from 1980
  273. //
  274. if (Time->Year < 1980 ||
  275. Time->Month < 1 ||
  276. Time->Month > 12 ||
  277. Time->Day < 1 ||
  278. Time->Day > 31 ||
  279. Time->Hour > 23 ||
  280. Time->Minute > 59 ||
  281. Time->Second > 59 ||
  282. Time->Nanosecond > 999999999
  283. ) {
  284. ValidTime = FALSE;
  285. } else {
  286. //
  287. // Perform a more specific check of the day of the month
  288. //
  289. Day = MonthDays[Time->Month - 1];
  290. if (Time->Month == 2 && IS_LEAP_YEAR (Time->Year)) {
  291. Day += 1;
  292. //
  293. // 1 extra day this month
  294. //
  295. }
  296. if (Time->Day > Day) {
  297. ValidTime = FALSE;
  298. }
  299. }
  300. return ValidTime;
  301. }