Misc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /** @file
  2. Miscellaneous functions.
  3. Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Fat.h"
  7. UINT8 mMonthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  8. /**
  9. Create the task
  10. @param IFile - The instance of the open file.
  11. @param Token - A pointer to the token associated with the transaction.
  12. @return FAT_TASK * - Return the task instance.
  13. **/
  14. FAT_TASK *
  15. FatCreateTask (
  16. FAT_IFILE *IFile,
  17. EFI_FILE_IO_TOKEN *Token
  18. )
  19. {
  20. FAT_TASK *Task;
  21. Task = AllocateZeroPool (sizeof (*Task));
  22. if (Task != NULL) {
  23. Task->Signature = FAT_TASK_SIGNATURE;
  24. Task->IFile = IFile;
  25. Task->FileIoToken = Token;
  26. InitializeListHead (&Task->Subtasks);
  27. InitializeListHead (&Task->Link);
  28. }
  29. return Task;
  30. }
  31. /**
  32. Destroy the task.
  33. @param Task - The task to be destroyed.
  34. **/
  35. VOID
  36. FatDestroyTask (
  37. FAT_TASK *Task
  38. )
  39. {
  40. LIST_ENTRY *Link;
  41. FAT_SUBTASK *Subtask;
  42. Link = GetFirstNode (&Task->Subtasks);
  43. while (!IsNull (&Task->Subtasks, Link)) {
  44. Subtask = CR (Link, FAT_SUBTASK, Link, FAT_SUBTASK_SIGNATURE);
  45. Link = FatDestroySubtask (Subtask);
  46. }
  47. FreePool (Task);
  48. }
  49. /**
  50. Wait all non-blocking requests complete.
  51. @param IFile - The instance of the open file.
  52. **/
  53. VOID
  54. FatWaitNonblockingTask (
  55. FAT_IFILE *IFile
  56. )
  57. {
  58. BOOLEAN TaskQueueEmpty;
  59. do {
  60. EfiAcquireLock (&FatTaskLock);
  61. TaskQueueEmpty = IsListEmpty (&IFile->Tasks);
  62. EfiReleaseLock (&FatTaskLock);
  63. } while (!TaskQueueEmpty);
  64. }
  65. /**
  66. Remove the subtask from subtask list.
  67. @param Subtask - The subtask to be removed.
  68. @return LIST_ENTRY * - The next node in the list.
  69. **/
  70. LIST_ENTRY *
  71. FatDestroySubtask (
  72. FAT_SUBTASK *Subtask
  73. )
  74. {
  75. LIST_ENTRY *Link;
  76. gBS->CloseEvent (Subtask->DiskIo2Token.Event);
  77. Link = RemoveEntryList (&Subtask->Link);
  78. FreePool (Subtask);
  79. return Link;
  80. }
  81. /**
  82. Execute the task.
  83. @param IFile - The instance of the open file.
  84. @param Task - The task to be executed.
  85. @retval EFI_SUCCESS - The task was executed sucessfully.
  86. @return other - An error occurred when executing the task.
  87. **/
  88. EFI_STATUS
  89. FatQueueTask (
  90. IN FAT_IFILE *IFile,
  91. IN FAT_TASK *Task
  92. )
  93. {
  94. EFI_STATUS Status;
  95. LIST_ENTRY *Link;
  96. LIST_ENTRY *NextLink;
  97. FAT_SUBTASK *Subtask;
  98. //
  99. // Sometimes the Task doesn't contain any subtasks, signal the event directly.
  100. //
  101. if (IsListEmpty (&Task->Subtasks)) {
  102. Task->FileIoToken->Status = EFI_SUCCESS;
  103. gBS->SignalEvent (Task->FileIoToken->Event);
  104. FreePool (Task);
  105. return EFI_SUCCESS;
  106. }
  107. EfiAcquireLock (&FatTaskLock);
  108. InsertTailList (&IFile->Tasks, &Task->Link);
  109. EfiReleaseLock (&FatTaskLock);
  110. Status = EFI_SUCCESS;
  111. //
  112. // Use NextLink to store the next link of the list, because Link might be remove from the
  113. // doubly-linked list and get freed in the end of current loop.
  114. //
  115. // Also, list operation APIs like IsNull() and GetNextNode() are avoided during the loop, since
  116. // they may check the validity of doubly-linked lists by traversing them. These APIs cannot
  117. // handle list elements being removed during the traverse.
  118. //
  119. for ( Link = GetFirstNode (&Task->Subtasks), NextLink = GetNextNode (&Task->Subtasks, Link)
  120. ; Link != &Task->Subtasks
  121. ; Link = NextLink, NextLink = Link->ForwardLink
  122. ) {
  123. Subtask = CR (Link, FAT_SUBTASK, Link, FAT_SUBTASK_SIGNATURE);
  124. if (Subtask->Write) {
  125. Status = IFile->OFile->Volume->DiskIo2->WriteDiskEx (
  126. IFile->OFile->Volume->DiskIo2,
  127. IFile->OFile->Volume->MediaId,
  128. Subtask->Offset,
  129. &Subtask->DiskIo2Token,
  130. Subtask->BufferSize,
  131. Subtask->Buffer
  132. );
  133. } else {
  134. Status = IFile->OFile->Volume->DiskIo2->ReadDiskEx (
  135. IFile->OFile->Volume->DiskIo2,
  136. IFile->OFile->Volume->MediaId,
  137. Subtask->Offset,
  138. &Subtask->DiskIo2Token,
  139. Subtask->BufferSize,
  140. Subtask->Buffer
  141. );
  142. }
  143. if (EFI_ERROR (Status)) {
  144. break;
  145. }
  146. }
  147. if (EFI_ERROR (Status)) {
  148. EfiAcquireLock (&FatTaskLock);
  149. //
  150. // Remove all the remaining subtasks when failure.
  151. // We shouldn't remove all the tasks because the non-blocking requests have
  152. // been submitted and cannot be canceled.
  153. //
  154. while (!IsNull (&Task->Subtasks, Link)) {
  155. Subtask = CR (Link, FAT_SUBTASK, Link, FAT_SUBTASK_SIGNATURE);
  156. Link = FatDestroySubtask (Subtask);
  157. }
  158. if (IsListEmpty (&Task->Subtasks)) {
  159. RemoveEntryList (&Task->Link);
  160. FreePool (Task);
  161. } else {
  162. //
  163. // If one or more subtasks have been already submitted, set FileIoToken
  164. // to NULL so that the callback won't signal the event.
  165. //
  166. Task->FileIoToken = NULL;
  167. }
  168. EfiReleaseLock (&FatTaskLock);
  169. }
  170. return Status;
  171. }
  172. /**
  173. Set the volume as dirty or not.
  174. @param Volume - FAT file system volume.
  175. @param IoMode - The access mode.
  176. @param DirtyValue - Set the volume as dirty or not.
  177. @retval EFI_SUCCESS - Set the new FAT entry value sucessfully.
  178. @return other - An error occurred when operation the FAT entries.
  179. **/
  180. EFI_STATUS
  181. FatAccessVolumeDirty (
  182. IN FAT_VOLUME *Volume,
  183. IN IO_MODE IoMode,
  184. IN VOID *DirtyValue
  185. )
  186. {
  187. UINTN WriteCount;
  188. WriteCount = Volume->FatEntrySize;
  189. return FatDiskIo (Volume, IoMode, Volume->FatPos + WriteCount, WriteCount, DirtyValue, NULL);
  190. }
  191. /**
  192. Invoke a notification event.
  193. @param Event Event whose notification function is being invoked.
  194. @param Context The pointer to the notification function's context,
  195. which is implementation-dependent.
  196. **/
  197. VOID
  198. EFIAPI
  199. FatOnAccessComplete (
  200. IN EFI_EVENT Event,
  201. IN VOID *Context
  202. )
  203. {
  204. EFI_STATUS Status;
  205. FAT_SUBTASK *Subtask;
  206. FAT_TASK *Task;
  207. //
  208. // Avoid someone in future breaks the below assumption.
  209. //
  210. ASSERT (EfiGetCurrentTpl () == FatTaskLock.Tpl);
  211. Subtask = (FAT_SUBTASK *) Context;
  212. Task = Subtask->Task;
  213. Status = Subtask->DiskIo2Token.TransactionStatus;
  214. ASSERT (Task->Signature == FAT_TASK_SIGNATURE);
  215. ASSERT (Subtask->Signature == FAT_SUBTASK_SIGNATURE);
  216. //
  217. // Remove the task unconditionally
  218. //
  219. FatDestroySubtask (Subtask);
  220. //
  221. // Task->FileIoToken is NULL which means the task will be ignored (just recycle the subtask and task memory).
  222. //
  223. if (Task->FileIoToken != NULL) {
  224. if (IsListEmpty (&Task->Subtasks) || EFI_ERROR (Status)) {
  225. Task->FileIoToken->Status = Status;
  226. gBS->SignalEvent (Task->FileIoToken->Event);
  227. //
  228. // Mark Task->FileIoToken to NULL so that the subtasks belonging to the task will be ignored.
  229. //
  230. Task->FileIoToken = NULL;
  231. }
  232. }
  233. if (IsListEmpty (&Task->Subtasks)) {
  234. RemoveEntryList (&Task->Link);
  235. FreePool (Task);
  236. }
  237. }
  238. /**
  239. General disk access function.
  240. @param Volume - FAT file system volume.
  241. @param IoMode - The access mode (disk read/write or cache access).
  242. @param Offset - The starting byte offset to read from.
  243. @param BufferSize - Size of Buffer.
  244. @param Buffer - Buffer containing read data.
  245. @param Task point to task instance.
  246. @retval EFI_SUCCESS - The operation is performed successfully.
  247. @retval EFI_VOLUME_CORRUPTED - The accesss is
  248. @return Others - The status of read/write the disk
  249. **/
  250. EFI_STATUS
  251. FatDiskIo (
  252. IN FAT_VOLUME *Volume,
  253. IN IO_MODE IoMode,
  254. IN UINT64 Offset,
  255. IN UINTN BufferSize,
  256. IN OUT VOID *Buffer,
  257. IN FAT_TASK *Task
  258. )
  259. {
  260. EFI_STATUS Status;
  261. EFI_DISK_IO_PROTOCOL *DiskIo;
  262. EFI_DISK_READ IoFunction;
  263. FAT_SUBTASK *Subtask;
  264. //
  265. // Verify the IO is in devices range
  266. //
  267. Status = EFI_VOLUME_CORRUPTED;
  268. if (Offset + BufferSize <= Volume->VolumeSize) {
  269. if (CACHE_ENABLED (IoMode)) {
  270. //
  271. // Access cache
  272. //
  273. Status = FatAccessCache (Volume, CACHE_TYPE (IoMode), RAW_ACCESS (IoMode), Offset, BufferSize, Buffer, Task);
  274. } else {
  275. //
  276. // Access disk directly
  277. //
  278. if (Task == NULL) {
  279. //
  280. // Blocking access
  281. //
  282. DiskIo = Volume->DiskIo;
  283. IoFunction = (IoMode == ReadDisk) ? DiskIo->ReadDisk : DiskIo->WriteDisk;
  284. Status = IoFunction (DiskIo, Volume->MediaId, Offset, BufferSize, Buffer);
  285. } else {
  286. //
  287. // Non-blocking access
  288. //
  289. Subtask = AllocateZeroPool (sizeof (*Subtask));
  290. if (Subtask == NULL) {
  291. Status = EFI_OUT_OF_RESOURCES;
  292. } else {
  293. Subtask->Signature = FAT_SUBTASK_SIGNATURE;
  294. Subtask->Task = Task;
  295. Subtask->Write = (BOOLEAN) (IoMode == WriteDisk);
  296. Subtask->Offset = Offset;
  297. Subtask->Buffer = Buffer;
  298. Subtask->BufferSize = BufferSize;
  299. Status = gBS->CreateEvent (
  300. EVT_NOTIFY_SIGNAL,
  301. TPL_NOTIFY,
  302. FatOnAccessComplete,
  303. Subtask,
  304. &Subtask->DiskIo2Token.Event
  305. );
  306. if (!EFI_ERROR (Status)) {
  307. InsertTailList (&Task->Subtasks, &Subtask->Link);
  308. } else {
  309. FreePool (Subtask);
  310. }
  311. }
  312. }
  313. }
  314. }
  315. if (EFI_ERROR (Status)) {
  316. Volume->DiskError = TRUE;
  317. DEBUG ((EFI_D_ERROR, "FatDiskIo: error %r\n", Status));
  318. }
  319. return Status;
  320. }
  321. /**
  322. Lock the volume.
  323. **/
  324. VOID
  325. FatAcquireLock (
  326. VOID
  327. )
  328. {
  329. EfiAcquireLock (&FatFsLock);
  330. }
  331. /**
  332. Lock the volume.
  333. If the lock is already in the acquired state, then EFI_ACCESS_DENIED is returned.
  334. Otherwise, EFI_SUCCESS is returned.
  335. @retval EFI_SUCCESS - The volume is locked.
  336. @retval EFI_ACCESS_DENIED - The volume could not be locked because it is already locked.
  337. **/
  338. EFI_STATUS
  339. FatAcquireLockOrFail (
  340. VOID
  341. )
  342. {
  343. return EfiAcquireLockOrFail (&FatFsLock);
  344. }
  345. /**
  346. Unlock the volume.
  347. **/
  348. VOID
  349. FatReleaseLock (
  350. VOID
  351. )
  352. {
  353. EfiReleaseLock (&FatFsLock);
  354. }
  355. /**
  356. Free directory entry.
  357. @param DirEnt - The directory entry to be freed.
  358. **/
  359. VOID
  360. FatFreeDirEnt (
  361. IN FAT_DIRENT *DirEnt
  362. )
  363. {
  364. if (DirEnt->FileString != NULL) {
  365. FreePool (DirEnt->FileString);
  366. }
  367. FreePool (DirEnt);
  368. }
  369. /**
  370. Free volume structure (including the contents of directory cache and disk cache).
  371. @param Volume - The volume structure to be freed.
  372. **/
  373. VOID
  374. FatFreeVolume (
  375. IN FAT_VOLUME *Volume
  376. )
  377. {
  378. //
  379. // Free disk cache
  380. //
  381. if (Volume->CacheBuffer != NULL) {
  382. FreePool (Volume->CacheBuffer);
  383. }
  384. //
  385. // Free directory cache
  386. //
  387. FatCleanupODirCache (Volume);
  388. FreePool (Volume);
  389. }
  390. /**
  391. Translate EFI time to FAT time.
  392. @param ETime - The time of EFI_TIME.
  393. @param FTime - The time of FAT_DATE_TIME.
  394. **/
  395. VOID
  396. FatEfiTimeToFatTime (
  397. IN EFI_TIME *ETime,
  398. OUT FAT_DATE_TIME *FTime
  399. )
  400. {
  401. //
  402. // ignores timezone info in source ETime
  403. //
  404. if (ETime->Year > 1980) {
  405. FTime->Date.Year = (UINT16) (ETime->Year - 1980);
  406. }
  407. if (ETime->Year >= 1980 + FAT_MAX_YEAR_FROM_1980) {
  408. FTime->Date.Year = FAT_MAX_YEAR_FROM_1980;
  409. }
  410. FTime->Date.Month = ETime->Month;
  411. FTime->Date.Day = ETime->Day;
  412. FTime->Time.Hour = ETime->Hour;
  413. FTime->Time.Minute = ETime->Minute;
  414. FTime->Time.DoubleSecond = (UINT16) (ETime->Second / 2);
  415. }
  416. /**
  417. Translate Fat time to EFI time.
  418. @param FTime - The time of FAT_DATE_TIME.
  419. @param ETime - The time of EFI_TIME..
  420. **/
  421. VOID
  422. FatFatTimeToEfiTime (
  423. IN FAT_DATE_TIME *FTime,
  424. OUT EFI_TIME *ETime
  425. )
  426. {
  427. ETime->Year = (UINT16) (FTime->Date.Year + 1980);
  428. ETime->Month = (UINT8) FTime->Date.Month;
  429. ETime->Day = (UINT8) FTime->Date.Day;
  430. ETime->Hour = (UINT8) FTime->Time.Hour;
  431. ETime->Minute = (UINT8) FTime->Time.Minute;
  432. ETime->Second = (UINT8) (FTime->Time.DoubleSecond * 2);
  433. ETime->Nanosecond = 0;
  434. ETime->TimeZone = EFI_UNSPECIFIED_TIMEZONE;
  435. ETime->Daylight = 0;
  436. }
  437. /**
  438. Get Current FAT time.
  439. @param FatNow - Current FAT time.
  440. **/
  441. VOID
  442. FatGetCurrentFatTime (
  443. OUT FAT_DATE_TIME *FatNow
  444. )
  445. {
  446. EFI_STATUS Status;
  447. EFI_TIME Now;
  448. Status = gRT->GetTime (&Now, NULL);
  449. if (!EFI_ERROR (Status)) {
  450. FatEfiTimeToFatTime (&Now, FatNow);
  451. } else {
  452. ZeroMem (&Now, sizeof (EFI_TIME));
  453. Now.Year = 1980;
  454. Now.Month = 1;
  455. Now.Day = 1;
  456. FatEfiTimeToFatTime (&Now, FatNow);
  457. }
  458. }
  459. /**
  460. Check whether a time is valid.
  461. @param Time - The time of EFI_TIME.
  462. @retval TRUE - The time is valid.
  463. @retval FALSE - The time is not valid.
  464. **/
  465. BOOLEAN
  466. FatIsValidTime (
  467. IN EFI_TIME *Time
  468. )
  469. {
  470. UINTN Day;
  471. BOOLEAN ValidTime;
  472. ValidTime = TRUE;
  473. //
  474. // Check the fields for range problems
  475. // Fat can only support from 1980
  476. //
  477. if (Time->Year < 1980 ||
  478. Time->Month < 1 ||
  479. Time->Month > 12 ||
  480. Time->Day < 1 ||
  481. Time->Day > 31 ||
  482. Time->Hour > 23 ||
  483. Time->Minute > 59 ||
  484. Time->Second > 59 ||
  485. Time->Nanosecond > 999999999
  486. ) {
  487. ValidTime = FALSE;
  488. } else {
  489. //
  490. // Perform a more specific check of the day of the month
  491. //
  492. Day = mMonthDays[Time->Month - 1];
  493. if (Time->Month == 2 && IS_LEAP_YEAR (Time->Year)) {
  494. Day += 1;
  495. //
  496. // 1 extra day this month
  497. //
  498. }
  499. if (Time->Day > Day) {
  500. ValidTime = FALSE;
  501. }
  502. }
  503. return ValidTime;
  504. }