DirectoryManage.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  1. /** @file
  2. Functions for performing directory entry io.
  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 a directory entry from disk for the Ofile.
  9. @param Parent - The parent of the OFile which need to update.
  10. @param IoMode - Indicate whether to read directory entry or write directroy entry.
  11. @param EntryPos - The position of the directory entry to be accessed.
  12. @param Entry - The directory entry read or written.
  13. @retval EFI_SUCCESS - Access the directory entry sucessfully.
  14. @return other - An error occurred when reading the directory entry.
  15. **/
  16. STATIC
  17. EFI_STATUS
  18. FatAccessEntry (
  19. IN FAT_OFILE *Parent,
  20. IN IO_MODE IoMode,
  21. IN UINTN EntryPos,
  22. IN OUT VOID *Entry
  23. )
  24. {
  25. UINTN Position;
  26. UINTN BufferSize;
  27. Position = EntryPos * sizeof (FAT_DIRECTORY_ENTRY);
  28. if (Position >= Parent->FileSize) {
  29. //
  30. // End of directory
  31. //
  32. ASSERT (IoMode == ReadData);
  33. ((FAT_DIRECTORY_ENTRY *) Entry)->FileName[0] = EMPTY_ENTRY_MARK;
  34. ((FAT_DIRECTORY_ENTRY *) Entry)->Attributes = 0;
  35. return EFI_SUCCESS;
  36. }
  37. BufferSize = sizeof (FAT_DIRECTORY_ENTRY);
  38. return FatAccessOFile (Parent, IoMode, Position, &BufferSize, Entry, NULL);
  39. }
  40. /**
  41. Save the directory entry to disk.
  42. @param OFile - The parent OFile which needs to update.
  43. @param DirEnt - The directory entry to be saved.
  44. @retval EFI_SUCCESS - Store the directory entry successfully.
  45. @return other - An error occurred when writing the directory entry.
  46. **/
  47. EFI_STATUS
  48. FatStoreDirEnt (
  49. IN FAT_OFILE *OFile,
  50. IN FAT_DIRENT *DirEnt
  51. )
  52. {
  53. EFI_STATUS Status;
  54. FAT_DIRECTORY_LFN LfnEntry;
  55. UINTN EntryPos;
  56. CHAR16 *LfnBufferPointer;
  57. CHAR16 LfnBuffer[MAX_LFN_ENTRIES * LFN_CHAR_TOTAL + 1];
  58. UINT8 EntryCount;
  59. UINT8 LfnOrdinal;
  60. EntryPos = DirEnt->EntryPos;
  61. EntryCount = DirEnt->EntryCount;
  62. //
  63. // Write directory entry
  64. //
  65. Status = FatAccessEntry (OFile, WriteData, EntryPos, &DirEnt->Entry);
  66. if (EFI_ERROR (Status)) {
  67. return Status;
  68. }
  69. if (--EntryCount > 0) {
  70. //
  71. // Write LFN directory entry
  72. //
  73. SetMem (LfnBuffer, sizeof (CHAR16) * LFN_CHAR_TOTAL * EntryCount, 0xff);
  74. Status = StrCpyS (
  75. LfnBuffer,
  76. ARRAY_SIZE (LfnBuffer),
  77. DirEnt->FileString
  78. );
  79. if (EFI_ERROR (Status)) {
  80. return Status;
  81. }
  82. LfnBufferPointer = LfnBuffer;
  83. LfnEntry.Attributes = FAT_ATTRIBUTE_LFN;
  84. LfnEntry.Type = 0;
  85. LfnEntry.MustBeZero = 0;
  86. LfnEntry.Checksum = FatCheckSum (DirEnt->Entry.FileName);
  87. for (LfnOrdinal = 1; LfnOrdinal <= EntryCount; LfnOrdinal++) {
  88. LfnEntry.Ordinal = LfnOrdinal;
  89. if (LfnOrdinal == EntryCount) {
  90. LfnEntry.Ordinal |= FAT_LFN_LAST;
  91. }
  92. CopyMem (LfnEntry.Name1, LfnBufferPointer, sizeof (CHAR16) * LFN_CHAR1_LEN);
  93. LfnBufferPointer += LFN_CHAR1_LEN;
  94. CopyMem (LfnEntry.Name2, LfnBufferPointer, sizeof (CHAR16) * LFN_CHAR2_LEN);
  95. LfnBufferPointer += LFN_CHAR2_LEN;
  96. CopyMem (LfnEntry.Name3, LfnBufferPointer, sizeof (CHAR16) * LFN_CHAR3_LEN);
  97. LfnBufferPointer += LFN_CHAR3_LEN;
  98. EntryPos--;
  99. if (DirEnt->Invalid) {
  100. LfnEntry.Ordinal = DELETE_ENTRY_MARK;
  101. }
  102. Status = FatAccessEntry (OFile, WriteData, EntryPos, &LfnEntry);
  103. if (EFI_ERROR (Status)) {
  104. return Status;
  105. }
  106. }
  107. }
  108. return EFI_SUCCESS;
  109. }
  110. /**
  111. Determine whether the directory entry is "." or ".." entry.
  112. @param DirEnt - The corresponding directory entry.
  113. @retval TRUE - The directory entry is "." or ".." directory entry
  114. @retval FALSE - The directory entry is not "." or ".." directory entry
  115. **/
  116. BOOLEAN
  117. FatIsDotDirEnt (
  118. IN FAT_DIRENT *DirEnt
  119. )
  120. {
  121. CHAR16 *FileString;
  122. FileString = DirEnt->FileString;
  123. if (StrCmp (FileString, L".") == 0 || StrCmp (FileString, L"..") == 0) {
  124. return TRUE;
  125. }
  126. return FALSE;
  127. }
  128. /**
  129. Set the OFile's cluster info in its directory entry.
  130. @param OFile - The corresponding OFile.
  131. **/
  132. STATIC
  133. VOID
  134. FatSetDirEntCluster (
  135. IN FAT_OFILE *OFile
  136. )
  137. {
  138. UINTN Cluster;
  139. FAT_DIRENT *DirEnt;
  140. DirEnt = OFile->DirEnt;
  141. Cluster = OFile->FileCluster;
  142. DirEnt->Entry.FileClusterHigh = (UINT16) (Cluster >> 16);
  143. DirEnt->Entry.FileCluster = (UINT16) Cluster;
  144. }
  145. /**
  146. Set the OFile's cluster and size info in its directory entry.
  147. @param OFile - The corresponding OFile.
  148. **/
  149. VOID
  150. FatUpdateDirEntClusterSizeInfo (
  151. IN FAT_OFILE *OFile
  152. )
  153. {
  154. ASSERT (OFile->ODir == NULL);
  155. OFile->DirEnt->Entry.FileSize = (UINT32) OFile->FileSize;
  156. FatSetDirEntCluster (OFile);
  157. }
  158. /**
  159. Copy all the information of DirEnt2 to DirEnt1 except for 8.3 name.
  160. @param DirEnt1 - The destination directory entry.
  161. @param DirEnt2 - The source directory entry.
  162. **/
  163. VOID
  164. FatCloneDirEnt (
  165. IN FAT_DIRENT *DirEnt1,
  166. IN FAT_DIRENT *DirEnt2
  167. )
  168. {
  169. UINT8 *Entry1;
  170. UINT8 *Entry2;
  171. Entry1 = (UINT8 *) &DirEnt1->Entry;
  172. Entry2 = (UINT8 *) &DirEnt2->Entry;
  173. CopyMem (
  174. Entry1 + FAT_ENTRY_INFO_OFFSET,
  175. Entry2 + FAT_ENTRY_INFO_OFFSET,
  176. sizeof (FAT_DIRECTORY_ENTRY) - FAT_ENTRY_INFO_OFFSET
  177. );
  178. }
  179. /**
  180. Get the LFN for the directory entry.
  181. @param Parent - The parent directory.
  182. @param DirEnt - The directory entry to get LFN.
  183. **/
  184. STATIC
  185. VOID
  186. FatLoadLongNameEntry (
  187. IN FAT_OFILE *Parent,
  188. IN FAT_DIRENT *DirEnt
  189. )
  190. {
  191. CHAR16 LfnBuffer[MAX_LFN_ENTRIES * LFN_CHAR_TOTAL + 1];
  192. CHAR16 *LfnBufferPointer;
  193. CHAR8 *File8Dot3Name;
  194. UINTN EntryPos;
  195. UINT8 LfnOrdinal;
  196. UINT8 LfnChecksum;
  197. FAT_DIRECTORY_LFN LfnEntry;
  198. EFI_STATUS Status;
  199. EntryPos = DirEnt->EntryPos;
  200. File8Dot3Name = DirEnt->Entry.FileName;
  201. LfnBufferPointer = LfnBuffer;
  202. //
  203. // Computes checksum for LFN
  204. //
  205. LfnChecksum = FatCheckSum (File8Dot3Name);
  206. LfnOrdinal = 1;
  207. do {
  208. if (EntryPos == 0) {
  209. LfnBufferPointer = LfnBuffer;
  210. break;
  211. }
  212. EntryPos--;
  213. Status = FatAccessEntry (Parent, ReadData, EntryPos, &LfnEntry);
  214. if (EFI_ERROR (Status) ||
  215. LfnEntry.Attributes != FAT_ATTRIBUTE_LFN ||
  216. LfnEntry.MustBeZero != 0 ||
  217. LfnEntry.Checksum != LfnChecksum ||
  218. (LfnEntry.Ordinal & (~FAT_LFN_LAST)) != LfnOrdinal ||
  219. LfnOrdinal > MAX_LFN_ENTRIES
  220. ) {
  221. //
  222. // The directory entry does not have a long file name or
  223. // some error occurs when loading long file name for a directory entry,
  224. // and then we load the long name from short name
  225. //
  226. LfnBufferPointer = LfnBuffer;
  227. break;
  228. }
  229. CopyMem (LfnBufferPointer, LfnEntry.Name1, sizeof (CHAR16) * LFN_CHAR1_LEN);
  230. LfnBufferPointer += LFN_CHAR1_LEN;
  231. CopyMem (LfnBufferPointer, LfnEntry.Name2, sizeof (CHAR16) * LFN_CHAR2_LEN);
  232. LfnBufferPointer += LFN_CHAR2_LEN;
  233. CopyMem (LfnBufferPointer, LfnEntry.Name3, sizeof (CHAR16) * LFN_CHAR3_LEN);
  234. LfnBufferPointer += LFN_CHAR3_LEN;
  235. LfnOrdinal++;
  236. } while ((LfnEntry.Ordinal & FAT_LFN_LAST) == 0);
  237. DirEnt->EntryCount = LfnOrdinal;
  238. //
  239. // Terminate current Lfnbuffer
  240. //
  241. *LfnBufferPointer = 0;
  242. if (LfnBufferPointer == LfnBuffer) {
  243. //
  244. // Fail to get the long file name from long file name entry,
  245. // get the file name from short name
  246. //
  247. FatGetFileNameViaCaseFlag (
  248. DirEnt,
  249. LfnBuffer,
  250. ARRAY_SIZE (LfnBuffer)
  251. );
  252. }
  253. DirEnt->FileString = AllocateCopyPool (StrSize (LfnBuffer), LfnBuffer);
  254. }
  255. /**
  256. Add this directory entry node to the list of directory entries and hash table.
  257. @param ODir - The parent OFile which needs to be updated.
  258. @param DirEnt - The directory entry to be added.
  259. **/
  260. STATIC
  261. VOID
  262. FatAddDirEnt (
  263. IN FAT_ODIR *ODir,
  264. IN FAT_DIRENT *DirEnt
  265. )
  266. {
  267. if (DirEnt->Link.BackLink == NULL) {
  268. DirEnt->Link.BackLink = &ODir->ChildList;
  269. }
  270. InsertTailList (DirEnt->Link.BackLink, &DirEnt->Link);
  271. FatInsertToHashTable (ODir, DirEnt);
  272. }
  273. /**
  274. Load from disk the next directory entry at current end of directory position.
  275. @param OFile - The parent OFile.
  276. @param PtrDirEnt - The directory entry that is loaded.
  277. @retval EFI_SUCCESS - Load the directory entry successfully.
  278. @retval EFI_OUT_OF_RESOURCES - Out of resource.
  279. @return other - An error occurred when reading the directory entries.
  280. **/
  281. STATIC
  282. EFI_STATUS
  283. FatLoadNextDirEnt (
  284. IN FAT_OFILE *OFile,
  285. OUT FAT_DIRENT **PtrDirEnt
  286. )
  287. {
  288. EFI_STATUS Status;
  289. FAT_DIRENT *DirEnt;
  290. FAT_ODIR *ODir;
  291. FAT_DIRECTORY_ENTRY Entry;
  292. ODir = OFile->ODir;
  293. //
  294. // Make sure the parent's directory has been opened
  295. //
  296. ASSERT (ODir != NULL);
  297. //
  298. // Assert we have not reached the end of directory
  299. //
  300. ASSERT (!ODir->EndOfDir);
  301. DirEnt = NULL;
  302. for (;;) {
  303. //
  304. // Read the next directory entry until we find a valid directory entry (excluding lfn entry)
  305. //
  306. Status = FatAccessEntry (OFile, ReadData, ODir->CurrentEndPos, &Entry);
  307. if (EFI_ERROR (Status)) {
  308. return Status;
  309. }
  310. if (((UINT8) Entry.FileName[0] != DELETE_ENTRY_MARK) && (Entry.Attributes & FAT_ATTRIBUTE_VOLUME_ID) == 0) {
  311. //
  312. // We get a valid directory entry, then handle it
  313. //
  314. break;
  315. }
  316. ODir->CurrentEndPos++;
  317. }
  318. if (Entry.FileName[0] != EMPTY_ENTRY_MARK) {
  319. //
  320. // Although FAT spec states this field is always 0 for FAT12 & FAT16, some applications
  321. // might use it for some special usage, it is safer to zero it in memory for FAT12 & FAT16.
  322. //
  323. if (OFile->Volume->FatType != Fat32) {
  324. Entry.FileClusterHigh = 0;
  325. }
  326. //
  327. // This is a valid directory entry
  328. //
  329. DirEnt = AllocateZeroPool (sizeof (FAT_DIRENT));
  330. if (DirEnt == NULL) {
  331. return EFI_OUT_OF_RESOURCES;
  332. }
  333. DirEnt->Signature = FAT_DIRENT_SIGNATURE;
  334. //
  335. // Remember the directory's entry position on disk
  336. //
  337. DirEnt->EntryPos = (UINT16) ODir->CurrentEndPos;
  338. CopyMem (&DirEnt->Entry, &Entry, sizeof (FAT_DIRECTORY_ENTRY));
  339. FatLoadLongNameEntry (OFile, DirEnt);
  340. if (DirEnt->FileString == NULL) {
  341. Status = EFI_OUT_OF_RESOURCES;
  342. goto Done;
  343. }
  344. //
  345. // Add this directory entry to directory
  346. //
  347. FatAddDirEnt (ODir, DirEnt);
  348. //
  349. // Point to next directory entry
  350. //
  351. ODir->CurrentEndPos++;
  352. } else {
  353. ODir->EndOfDir = TRUE;
  354. }
  355. *PtrDirEnt = DirEnt;
  356. return EFI_SUCCESS;
  357. Done:
  358. FatFreeDirEnt (DirEnt);
  359. return Status;
  360. }
  361. /**
  362. Get the directory entry's info into Buffer.
  363. @param Volume - FAT file system volume.
  364. @param DirEnt - The corresponding directory entry.
  365. @param BufferSize - Size of Buffer.
  366. @param Buffer - Buffer containing file info.
  367. @retval EFI_SUCCESS - Get the file info successfully.
  368. @retval EFI_BUFFER_TOO_SMALL - The buffer is too small.
  369. **/
  370. EFI_STATUS
  371. FatGetDirEntInfo (
  372. IN FAT_VOLUME *Volume,
  373. IN FAT_DIRENT *DirEnt,
  374. IN OUT UINTN *BufferSize,
  375. OUT VOID *Buffer
  376. )
  377. {
  378. UINTN Size;
  379. UINTN NameSize;
  380. UINTN ResultSize;
  381. UINTN Cluster;
  382. EFI_STATUS Status;
  383. EFI_FILE_INFO *Info;
  384. FAT_DIRECTORY_ENTRY *Entry;
  385. FAT_DATE_TIME FatLastAccess;
  386. ASSERT_VOLUME_LOCKED (Volume);
  387. Size = SIZE_OF_EFI_FILE_INFO;
  388. NameSize = StrSize (DirEnt->FileString);
  389. ResultSize = Size + NameSize;
  390. Status = EFI_BUFFER_TOO_SMALL;
  391. if (*BufferSize >= ResultSize) {
  392. Status = EFI_SUCCESS;
  393. Entry = &DirEnt->Entry;
  394. Info = Buffer;
  395. Info->Size = ResultSize;
  396. if ((Entry->Attributes & FAT_ATTRIBUTE_DIRECTORY) != 0) {
  397. Cluster = (Entry->FileClusterHigh << 16) | Entry->FileCluster;
  398. Info->PhysicalSize = FatPhysicalDirSize (Volume, Cluster);
  399. Info->FileSize = Info->PhysicalSize;
  400. } else {
  401. Info->FileSize = Entry->FileSize;
  402. Info->PhysicalSize = FatPhysicalFileSize (Volume, Entry->FileSize);
  403. }
  404. ZeroMem (&FatLastAccess.Time, sizeof (FatLastAccess.Time));
  405. CopyMem (&FatLastAccess.Date, &Entry->FileLastAccess, sizeof (FatLastAccess.Date));
  406. FatFatTimeToEfiTime (&FatLastAccess, &Info->LastAccessTime);
  407. FatFatTimeToEfiTime (&Entry->FileCreateTime, &Info->CreateTime);
  408. FatFatTimeToEfiTime (&Entry->FileModificationTime, &Info->ModificationTime);
  409. Info->Attribute = Entry->Attributes & EFI_FILE_VALID_ATTR;
  410. CopyMem ((CHAR8 *) Buffer + Size, DirEnt->FileString, NameSize);
  411. }
  412. *BufferSize = ResultSize;
  413. return Status;
  414. }
  415. /**
  416. Search the directory for the directory entry whose filename is FileNameString.
  417. @param OFile - The parent OFile whose directory is to be searched.
  418. @param FileNameString - The filename to be searched.
  419. @param PtrDirEnt - pointer to the directory entry if found.
  420. @retval EFI_SUCCESS - Find the directory entry or not found.
  421. @return other - An error occurred when reading the directory entries.
  422. **/
  423. STATIC
  424. EFI_STATUS
  425. FatSearchODir (
  426. IN FAT_OFILE *OFile,
  427. IN CHAR16 *FileNameString,
  428. OUT FAT_DIRENT **PtrDirEnt
  429. )
  430. {
  431. BOOLEAN PossibleShortName;
  432. CHAR8 File8Dot3Name[FAT_NAME_LEN];
  433. FAT_ODIR *ODir;
  434. FAT_DIRENT *DirEnt;
  435. EFI_STATUS Status;
  436. ODir = OFile->ODir;
  437. ASSERT (ODir != NULL);
  438. //
  439. // Check if the file name is a valid short name
  440. //
  441. PossibleShortName = FatCheckIs8Dot3Name (FileNameString, File8Dot3Name);
  442. //
  443. // Search the hash table first
  444. //
  445. DirEnt = *FatLongNameHashSearch (ODir, FileNameString);
  446. if (DirEnt == NULL && PossibleShortName) {
  447. DirEnt = *FatShortNameHashSearch (ODir, File8Dot3Name);
  448. }
  449. if (DirEnt == NULL) {
  450. //
  451. // We fail to get the directory entry from hash table; we then
  452. // search the rest directory
  453. //
  454. while (!ODir->EndOfDir) {
  455. Status = FatLoadNextDirEnt (OFile, &DirEnt);
  456. if (EFI_ERROR (Status)) {
  457. return Status;
  458. }
  459. if (DirEnt != NULL) {
  460. if (FatStriCmp (FileNameString, DirEnt->FileString) == 0) {
  461. break;
  462. }
  463. if (PossibleShortName && CompareMem (File8Dot3Name, DirEnt->Entry.FileName, FAT_NAME_LEN) == 0) {
  464. break;
  465. }
  466. }
  467. }
  468. }
  469. *PtrDirEnt = DirEnt;
  470. return EFI_SUCCESS;
  471. }
  472. /**
  473. Set the OFile's current directory cursor to the list head.
  474. @param OFile - The directory OFile whose directory cursor is reset.
  475. **/
  476. VOID
  477. FatResetODirCursor (
  478. IN FAT_OFILE *OFile
  479. )
  480. {
  481. FAT_ODIR *ODir;
  482. ODir = OFile->ODir;
  483. ASSERT (ODir != NULL);
  484. ODir->CurrentCursor = &(ODir->ChildList);
  485. ODir->CurrentPos = 0;
  486. }
  487. /**
  488. Set the directory's cursor to the next and get the next directory entry.
  489. @param OFile - The parent OFile.
  490. @param PtrDirEnt - The next directory entry.
  491. @retval EFI_SUCCESS - We get the next directory entry successfully.
  492. @return other - An error occurred when get next directory entry.
  493. **/
  494. EFI_STATUS
  495. FatGetNextDirEnt (
  496. IN FAT_OFILE *OFile,
  497. OUT FAT_DIRENT **PtrDirEnt
  498. )
  499. {
  500. EFI_STATUS Status;
  501. FAT_DIRENT *DirEnt;
  502. FAT_ODIR *ODir;
  503. ODir = OFile->ODir;
  504. ASSERT (ODir != NULL);
  505. if (ODir->CurrentCursor->ForwardLink == &ODir->ChildList) {
  506. //
  507. // End of directory, we will try one more time
  508. //
  509. if (!ODir->EndOfDir) {
  510. //
  511. // Read directory from disk
  512. //
  513. Status = FatLoadNextDirEnt (OFile, &DirEnt);
  514. if (EFI_ERROR (Status)) {
  515. return Status;
  516. }
  517. }
  518. }
  519. if (ODir->CurrentCursor->ForwardLink == &ODir->ChildList) {
  520. //
  521. // End of directory, return NULL
  522. //
  523. DirEnt = NULL;
  524. ODir->CurrentPos = ODir->CurrentEndPos;
  525. } else {
  526. ODir->CurrentCursor = ODir->CurrentCursor->ForwardLink;
  527. DirEnt = DIRENT_FROM_LINK (ODir->CurrentCursor);
  528. ODir->CurrentPos = DirEnt->EntryPos + 1;
  529. }
  530. *PtrDirEnt = DirEnt;
  531. return EFI_SUCCESS;
  532. }
  533. /**
  534. Set the directory entry count according to the filename.
  535. @param OFile - The corresponding OFile.
  536. @param DirEnt - The directory entry to be set.
  537. **/
  538. STATIC
  539. VOID
  540. FatSetEntryCount (
  541. IN FAT_OFILE *OFile,
  542. IN FAT_DIRENT *DirEnt
  543. )
  544. {
  545. CHAR16 *FileString;
  546. CHAR8 *File8Dot3Name;
  547. //
  548. // Get new entry count and set the 8.3 name
  549. //
  550. DirEnt->EntryCount = 1;
  551. FileString = DirEnt->FileString;
  552. File8Dot3Name = DirEnt->Entry.FileName;
  553. SetMem (File8Dot3Name, FAT_NAME_LEN, ' ');
  554. if (StrCmp (FileString, L".") == 0) {
  555. //
  556. // "." entry
  557. //
  558. File8Dot3Name[0] = '.';
  559. FatCloneDirEnt (DirEnt, OFile->DirEnt);
  560. } else if (StrCmp (FileString, L"..") == 0) {
  561. //
  562. // ".." entry
  563. //
  564. File8Dot3Name[0] = '.';
  565. File8Dot3Name[1] = '.';
  566. FatCloneDirEnt (DirEnt, OFile->Parent->DirEnt);
  567. } else {
  568. //
  569. // Normal name
  570. //
  571. if (FatCheckIs8Dot3Name (FileString, File8Dot3Name)) {
  572. //
  573. // This file name is a valid 8.3 file name, we need to further check its case flag
  574. //
  575. FatSetCaseFlag (DirEnt);
  576. } else {
  577. //
  578. // The file name is not a valid 8.3 name we need to generate an 8.3 name for it
  579. //
  580. FatCreate8Dot3Name (OFile, DirEnt);
  581. DirEnt->EntryCount = (UINT8)(LFN_ENTRY_NUMBER (StrLen (FileString)) + DirEnt->EntryCount);
  582. }
  583. }
  584. }
  585. /**
  586. Append a zero cluster to the current OFile.
  587. @param OFile - The directory OFile which needs to be updated.
  588. @retval EFI_SUCCESS - Append a zero cluster to the OFile successfully.
  589. @return other - An error occurred when appending the zero cluster.
  590. **/
  591. STATIC
  592. EFI_STATUS
  593. FatExpandODir (
  594. IN FAT_OFILE *OFile
  595. )
  596. {
  597. return FatExpandOFile (OFile, OFile->FileSize + OFile->Volume->ClusterSize);
  598. }
  599. /**
  600. Search the Root OFile for the possible volume label.
  601. @param Root - The Root OFile.
  602. @param DirEnt - The returned directory entry of volume label.
  603. @retval EFI_SUCCESS - The search process is completed successfully.
  604. @return other - An error occurred when searching volume label.
  605. **/
  606. STATIC
  607. EFI_STATUS
  608. FatSeekVolumeId (
  609. IN FAT_OFILE *Root,
  610. OUT FAT_DIRENT *DirEnt
  611. )
  612. {
  613. EFI_STATUS Status;
  614. UINTN EntryPos;
  615. FAT_DIRECTORY_ENTRY *Entry;
  616. EntryPos = 0;
  617. Entry = &DirEnt->Entry;
  618. DirEnt->Invalid = TRUE;
  619. do {
  620. Status = FatAccessEntry (Root, ReadData, EntryPos, Entry);
  621. if (EFI_ERROR (Status)) {
  622. return Status;
  623. }
  624. if (((UINT8) Entry->FileName[0] != DELETE_ENTRY_MARK) && (((Entry->Attributes) & (~FAT_ATTRIBUTE_ARCHIVE)) == FAT_ATTRIBUTE_VOLUME_ID)) {
  625. DirEnt->EntryPos = (UINT16) EntryPos;
  626. DirEnt->EntryCount = 1;
  627. DirEnt->Invalid = FALSE;
  628. break;
  629. }
  630. EntryPos++;
  631. } while (Entry->FileName[0] != EMPTY_ENTRY_MARK);
  632. return EFI_SUCCESS;
  633. }
  634. /**
  635. Use First Fit Algorithm to insert directory entry.
  636. Only this function will erase "E5" entries in a directory.
  637. In view of safest recovery, this function will only be triggered
  638. when maximum directory entry number has reached.
  639. @param OFile - The corresponding OFile.
  640. @param DirEnt - The directory entry to be inserted.
  641. @retval EFI_SUCCESS - The directory entry has been successfully inserted.
  642. @retval EFI_VOLUME_FULL - The directory can not hold more directory entries.
  643. @return Others - Some error occurred when inserting new directory entries.
  644. **/
  645. STATIC
  646. EFI_STATUS
  647. FatFirstFitInsertDirEnt (
  648. IN FAT_OFILE *OFile,
  649. IN FAT_DIRENT *DirEnt
  650. )
  651. {
  652. EFI_STATUS Status;
  653. FAT_ODIR *ODir;
  654. LIST_ENTRY *CurrentEntry;
  655. FAT_DIRENT *CurrentDirEnt;
  656. UINT32 CurrentPos;
  657. UINT32 LabelPos;
  658. UINT32 NewEntryPos;
  659. UINT16 EntryCount;
  660. FAT_DIRENT LabelDirEnt;
  661. LabelPos = 0;
  662. if (OFile->Parent == NULL) {
  663. Status = FatSeekVolumeId (OFile, &LabelDirEnt);
  664. if (EFI_ERROR (Status)) {
  665. return Status;
  666. }
  667. if (!LabelDirEnt.Invalid) {
  668. LabelPos = LabelDirEnt.EntryPos;
  669. }
  670. }
  671. EntryCount = DirEnt->EntryCount;
  672. NewEntryPos = EntryCount;
  673. CurrentPos = 0;
  674. ODir = OFile->ODir;
  675. for (CurrentEntry = ODir->ChildList.ForwardLink;
  676. CurrentEntry != &ODir->ChildList;
  677. CurrentEntry = CurrentEntry->ForwardLink
  678. ) {
  679. CurrentDirEnt = DIRENT_FROM_LINK (CurrentEntry);
  680. if (NewEntryPos + CurrentDirEnt->EntryCount <= CurrentDirEnt->EntryPos) {
  681. if (LabelPos > NewEntryPos || LabelPos <= CurrentPos) {
  682. //
  683. // first fit succeeded
  684. //
  685. goto Done;
  686. }
  687. }
  688. CurrentPos = CurrentDirEnt->EntryPos;
  689. NewEntryPos = CurrentPos + EntryCount;
  690. }
  691. if (NewEntryPos >= ODir->CurrentEndPos) {
  692. return EFI_VOLUME_FULL;
  693. }
  694. Done:
  695. DirEnt->EntryPos = (UINT16) NewEntryPos;
  696. DirEnt->Link.BackLink = CurrentEntry;
  697. return EFI_SUCCESS;
  698. }
  699. /**
  700. Find the new directory entry position for the directory entry.
  701. @param OFile - The corresponding OFile.
  702. @param DirEnt - The directory entry whose new position is to be set.
  703. @retval EFI_SUCCESS - The new directory entry position is successfully found.
  704. @retval EFI_VOLUME_FULL - The directory has reach its maximum capacity.
  705. @return other - An error occurred when reading the directory entry.
  706. **/
  707. STATIC
  708. EFI_STATUS
  709. FatNewEntryPos (
  710. IN FAT_OFILE *OFile,
  711. IN FAT_DIRENT *DirEnt
  712. )
  713. {
  714. EFI_STATUS Status;
  715. FAT_ODIR *ODir;
  716. FAT_DIRENT *TempDirEnt;
  717. UINT32 NewEndPos;
  718. ODir = OFile->ODir;
  719. ASSERT (ODir != NULL);
  720. //
  721. // Make sure the whole directory has been loaded
  722. //
  723. while (!ODir->EndOfDir) {
  724. Status = FatLoadNextDirEnt (OFile, &TempDirEnt);
  725. if (EFI_ERROR (Status)) {
  726. return Status;
  727. }
  728. }
  729. //
  730. // We will append this entry to the end of directory
  731. //
  732. FatGetCurrentFatTime (&DirEnt->Entry.FileCreateTime);
  733. CopyMem (&DirEnt->Entry.FileModificationTime, &DirEnt->Entry.FileCreateTime, sizeof (FAT_DATE_TIME));
  734. CopyMem (&DirEnt->Entry.FileLastAccess, &DirEnt->Entry.FileCreateTime.Date, sizeof (FAT_DATE));
  735. NewEndPos = ODir->CurrentEndPos + DirEnt->EntryCount;
  736. if (NewEndPos * sizeof (FAT_DIRECTORY_ENTRY) > OFile->FileSize) {
  737. if (NewEndPos >= (OFile->IsFixedRootDir ? OFile->Volume->RootEntries : FAT_MAX_DIRENTRY_COUNT)) {
  738. //
  739. // We try to use fist fit algorithm to insert this directory entry
  740. //
  741. return FatFirstFitInsertDirEnt (OFile, DirEnt);
  742. }
  743. //
  744. // We should allocate a new cluster for this directory
  745. //
  746. Status = FatExpandODir (OFile);
  747. if (EFI_ERROR (Status)) {
  748. return Status;
  749. }
  750. }
  751. //
  752. // We append our directory entry at the end of directory file
  753. //
  754. ODir->CurrentEndPos = NewEndPos;
  755. DirEnt->EntryPos = (UINT16) (ODir->CurrentEndPos - 1);
  756. return EFI_SUCCESS;
  757. }
  758. /**
  759. Get the directory entry for the volume.
  760. @param Volume - FAT file system volume.
  761. @param Name - The file name of the volume.
  762. @retval EFI_SUCCESS - Update the volume with the directory entry sucessfully.
  763. @return others - An error occurred when getting volume label.
  764. **/
  765. EFI_STATUS
  766. FatGetVolumeEntry (
  767. IN FAT_VOLUME *Volume,
  768. IN CHAR16 *Name
  769. )
  770. {
  771. EFI_STATUS Status;
  772. FAT_DIRENT LabelDirEnt;
  773. *Name = 0;
  774. Status = FatSeekVolumeId (Volume->Root, &LabelDirEnt);
  775. if (!EFI_ERROR (Status)) {
  776. if (!LabelDirEnt.Invalid) {
  777. FatNameToStr (LabelDirEnt.Entry.FileName, FAT_NAME_LEN, FALSE, Name);
  778. }
  779. }
  780. return Status;
  781. }
  782. /**
  783. Set the relevant directory entry into disk for the volume.
  784. @param Volume - FAT file system volume.
  785. @param Name - The new file name of the volume.
  786. @retval EFI_SUCCESS - Update the Volume sucessfully.
  787. @retval EFI_UNSUPPORTED - The input label is not a valid volume label.
  788. @return other - An error occurred when setting volume label.
  789. **/
  790. EFI_STATUS
  791. FatSetVolumeEntry (
  792. IN FAT_VOLUME *Volume,
  793. IN CHAR16 *Name
  794. )
  795. {
  796. EFI_STATUS Status;
  797. FAT_DIRENT LabelDirEnt;
  798. FAT_OFILE *Root;
  799. Root = Volume->Root;
  800. Status = FatSeekVolumeId (Volume->Root, &LabelDirEnt);
  801. if (EFI_ERROR (Status)) {
  802. return Status;
  803. }
  804. if (LabelDirEnt.Invalid) {
  805. //
  806. // If there is not the relevant directory entry, create a new one
  807. //
  808. ZeroMem (&LabelDirEnt, sizeof (FAT_DIRENT));
  809. LabelDirEnt.EntryCount = 1;
  810. Status = FatNewEntryPos (Root, &LabelDirEnt);
  811. if (EFI_ERROR (Status)) {
  812. return Status;
  813. }
  814. LabelDirEnt.Entry.Attributes = FAT_ATTRIBUTE_VOLUME_ID;
  815. }
  816. SetMem (LabelDirEnt.Entry.FileName, FAT_NAME_LEN, ' ');
  817. if (FatStrToFat (Name, FAT_NAME_LEN, LabelDirEnt.Entry.FileName)) {
  818. return EFI_UNSUPPORTED;
  819. }
  820. FatGetCurrentFatTime (&LabelDirEnt.Entry.FileModificationTime);
  821. return FatStoreDirEnt (Root, &LabelDirEnt);
  822. }
  823. /**
  824. Create "." and ".." directory entries in the newly-created parent OFile.
  825. @param OFile - The parent OFile.
  826. @retval EFI_SUCCESS - The dot directory entries are successfully created.
  827. @return other - An error occurred when creating the directory entry.
  828. **/
  829. EFI_STATUS
  830. FatCreateDotDirEnts (
  831. IN FAT_OFILE *OFile
  832. )
  833. {
  834. EFI_STATUS Status;
  835. FAT_DIRENT *DirEnt;
  836. Status = FatExpandODir (OFile);
  837. if (EFI_ERROR (Status)) {
  838. return Status;
  839. }
  840. FatSetDirEntCluster (OFile);
  841. //
  842. // Create "."
  843. //
  844. Status = FatCreateDirEnt (OFile, L".", FAT_ATTRIBUTE_DIRECTORY, &DirEnt);
  845. if (EFI_ERROR (Status)) {
  846. return Status;
  847. }
  848. //
  849. // Create ".."
  850. //
  851. Status = FatCreateDirEnt (OFile, L"..", FAT_ATTRIBUTE_DIRECTORY, &DirEnt);
  852. return Status;
  853. }
  854. /**
  855. Create a directory entry in the parent OFile.
  856. @param OFile - The parent OFile.
  857. @param FileName - The filename of the newly-created directory entry.
  858. @param Attributes - The attribute of the newly-created directory entry.
  859. @param PtrDirEnt - The pointer to the newly-created directory entry.
  860. @retval EFI_SUCCESS - The directory entry is successfully created.
  861. @retval EFI_OUT_OF_RESOURCES - Not enough memory to create the directory entry.
  862. @return other - An error occurred when creating the directory entry.
  863. **/
  864. EFI_STATUS
  865. FatCreateDirEnt (
  866. IN FAT_OFILE *OFile,
  867. IN CHAR16 *FileName,
  868. IN UINT8 Attributes,
  869. OUT FAT_DIRENT **PtrDirEnt
  870. )
  871. {
  872. FAT_DIRENT *DirEnt;
  873. FAT_ODIR *ODir;
  874. EFI_STATUS Status;
  875. ASSERT (OFile != NULL);
  876. ODir = OFile->ODir;
  877. ASSERT (ODir != NULL);
  878. DirEnt = AllocateZeroPool (sizeof (FAT_DIRENT));
  879. if (DirEnt == NULL) {
  880. return EFI_OUT_OF_RESOURCES;
  881. }
  882. DirEnt->Signature = FAT_DIRENT_SIGNATURE;
  883. DirEnt->FileString = AllocateCopyPool (StrSize (FileName), FileName);
  884. if (DirEnt->FileString == NULL) {
  885. Status = EFI_OUT_OF_RESOURCES;
  886. goto Done;
  887. }
  888. //
  889. // Determine how many directory entries we need
  890. //
  891. FatSetEntryCount (OFile, DirEnt);
  892. //
  893. // Determine the file's directory entry position
  894. //
  895. Status = FatNewEntryPos (OFile, DirEnt);
  896. if (EFI_ERROR (Status)) {
  897. goto Done;
  898. }
  899. FatAddDirEnt (ODir, DirEnt);
  900. DirEnt->Entry.Attributes = Attributes;
  901. *PtrDirEnt = DirEnt;
  902. DEBUG ((EFI_D_INFO, "FSOpen: Created new directory entry '%S'\n", DirEnt->FileString));
  903. return FatStoreDirEnt (OFile, DirEnt);
  904. Done:
  905. FatFreeDirEnt (DirEnt);
  906. return Status;
  907. }
  908. /**
  909. Remove this directory entry node from the list of directory entries and hash table.
  910. @param OFile - The parent OFile.
  911. @param DirEnt - The directory entry to be removed.
  912. @retval EFI_SUCCESS - The directory entry is successfully removed.
  913. @return other - An error occurred when removing the directory entry.
  914. **/
  915. EFI_STATUS
  916. FatRemoveDirEnt (
  917. IN FAT_OFILE *OFile,
  918. IN FAT_DIRENT *DirEnt
  919. )
  920. {
  921. FAT_ODIR *ODir;
  922. ODir = OFile->ODir;
  923. if (ODir->CurrentCursor == &DirEnt->Link) {
  924. //
  925. // Move the directory cursor to its previous directory entry
  926. //
  927. ODir->CurrentCursor = ODir->CurrentCursor->BackLink;
  928. }
  929. //
  930. // Remove from directory entry list
  931. //
  932. RemoveEntryList (&DirEnt->Link);
  933. //
  934. // Remove from hash table
  935. //
  936. FatDeleteFromHashTable (ODir, DirEnt);
  937. DirEnt->Entry.FileName[0] = DELETE_ENTRY_MARK;
  938. DirEnt->Invalid = TRUE;
  939. return FatStoreDirEnt (OFile, DirEnt);
  940. }
  941. /**
  942. Open the directory entry to get the OFile.
  943. @param Parent - The parent OFile.
  944. @param DirEnt - The directory entry to be opened.
  945. @retval EFI_SUCCESS - The directory entry is successfully opened.
  946. @retval EFI_OUT_OF_RESOURCES - not enough memory to allocate a new OFile.
  947. @return other - An error occurred when opening the directory entry.
  948. **/
  949. EFI_STATUS
  950. FatOpenDirEnt (
  951. IN FAT_OFILE *Parent,
  952. IN FAT_DIRENT *DirEnt
  953. )
  954. {
  955. FAT_OFILE *OFile;
  956. FAT_VOLUME *Volume;
  957. if (DirEnt->OFile == NULL) {
  958. //
  959. // Open the directory entry
  960. //
  961. OFile = AllocateZeroPool (sizeof (FAT_OFILE));
  962. if (OFile == NULL) {
  963. return EFI_OUT_OF_RESOURCES;
  964. }
  965. OFile->Signature = FAT_OFILE_SIGNATURE;
  966. InitializeListHead (&OFile->Opens);
  967. InitializeListHead (&OFile->ChildHead);
  968. OFile->Parent = Parent;
  969. OFile->DirEnt = DirEnt;
  970. if (Parent != NULL) {
  971. //
  972. // The newly created OFile is not root
  973. //
  974. Volume = Parent->Volume;
  975. OFile->FullPathLen = Parent->FullPathLen + 1 + StrLen (DirEnt->FileString);
  976. OFile->FileCluster = ((DirEnt->Entry.FileClusterHigh) << 16) | (DirEnt->Entry.FileCluster);
  977. InsertTailList (&Parent->ChildHead, &OFile->ChildLink);
  978. } else {
  979. //
  980. // The newly created OFile is root
  981. //
  982. Volume = VOLUME_FROM_ROOT_DIRENT (DirEnt);
  983. Volume->Root = OFile;
  984. OFile->FileCluster = Volume->RootCluster;
  985. if (Volume->FatType != Fat32) {
  986. OFile->IsFixedRootDir = TRUE;
  987. }
  988. }
  989. OFile->FileCurrentCluster = OFile->FileCluster;
  990. OFile->Volume = Volume;
  991. InsertHeadList (&Volume->CheckRef, &OFile->CheckLink);
  992. OFile->FileSize = DirEnt->Entry.FileSize;
  993. if ((DirEnt->Entry.Attributes & FAT_ATTRIBUTE_DIRECTORY) != 0) {
  994. if (OFile->IsFixedRootDir) {
  995. OFile->FileSize = Volume->RootEntries * sizeof (FAT_DIRECTORY_ENTRY);
  996. } else {
  997. OFile->FileSize = FatPhysicalDirSize (Volume, OFile->FileCluster);
  998. }
  999. FatRequestODir (OFile);
  1000. if (OFile->ODir == NULL) {
  1001. return EFI_OUT_OF_RESOURCES;
  1002. }
  1003. }
  1004. DirEnt->OFile = OFile;
  1005. }
  1006. return EFI_SUCCESS;
  1007. }
  1008. /**
  1009. Close the directory entry and free the OFile.
  1010. @param DirEnt - The directory entry to be closed.
  1011. **/
  1012. VOID
  1013. FatCloseDirEnt (
  1014. IN FAT_DIRENT *DirEnt
  1015. )
  1016. {
  1017. FAT_OFILE *OFile;
  1018. FAT_VOLUME *Volume;
  1019. OFile = DirEnt->OFile;
  1020. ASSERT (OFile != NULL);
  1021. Volume = OFile->Volume;
  1022. if (OFile->ODir != NULL) {
  1023. FatDiscardODir (OFile);
  1024. }
  1025. if (OFile->Parent == NULL) {
  1026. Volume->Root = NULL;
  1027. } else {
  1028. RemoveEntryList (&OFile->ChildLink);
  1029. }
  1030. FreePool (OFile);
  1031. DirEnt->OFile = NULL;
  1032. if (DirEnt->Invalid == TRUE) {
  1033. //
  1034. // Free directory entry itself
  1035. //
  1036. FatFreeDirEnt (DirEnt);
  1037. }
  1038. }
  1039. /**
  1040. Traverse filename and open all OFiles that can be opened.
  1041. Update filename pointer to the component that can't be opened.
  1042. If more than one name component remains, returns an error;
  1043. otherwise, return the remaining name component so that the caller might choose to create it.
  1044. @param PtrOFile - As input, the reference OFile; as output, the located OFile.
  1045. @param FileName - The file name relevant to the OFile.
  1046. @param Attributes - The attribute of the destination OFile.
  1047. @param NewFileName - The remaining file name.
  1048. @retval EFI_NOT_FOUND - The file name can't be opened and there is more than one
  1049. components within the name left (this means the name can
  1050. not be created either).
  1051. @retval EFI_INVALID_PARAMETER - The parameter is not valid.
  1052. @retval EFI_SUCCESS - Open the file successfully.
  1053. @return other - An error occured when locating the OFile.
  1054. **/
  1055. EFI_STATUS
  1056. FatLocateOFile (
  1057. IN OUT FAT_OFILE **PtrOFile,
  1058. IN CHAR16 *FileName,
  1059. IN UINT8 Attributes,
  1060. OUT CHAR16 *NewFileName
  1061. )
  1062. {
  1063. EFI_STATUS Status;
  1064. FAT_VOLUME *Volume;
  1065. CHAR16 ComponentName[EFI_PATH_STRING_LENGTH];
  1066. UINTN FileNameLen;
  1067. BOOLEAN DirIntended;
  1068. CHAR16 *Next;
  1069. FAT_OFILE *OFile;
  1070. FAT_DIRENT *DirEnt;
  1071. DirEnt = NULL;
  1072. FileNameLen = StrLen (FileName);
  1073. if (FileNameLen == 0) {
  1074. return EFI_INVALID_PARAMETER;
  1075. }
  1076. OFile = *PtrOFile;
  1077. Volume = OFile->Volume;
  1078. DirIntended = FALSE;
  1079. if (FileName[FileNameLen - 1] == PATH_NAME_SEPARATOR) {
  1080. DirIntended = TRUE;
  1081. }
  1082. //
  1083. // If name starts with path name separator, then move to root OFile
  1084. //
  1085. if (*FileName == PATH_NAME_SEPARATOR) {
  1086. OFile = Volume->Root;
  1087. FileName++;
  1088. FileNameLen--;
  1089. }
  1090. //
  1091. // Per FAT Spec the file name should meet the following criteria:
  1092. // C1. Length (FileLongName) <= 255
  1093. // C2. Length (X:FileFullPath<NUL>) <= 260
  1094. // Here we check C2 first.
  1095. //
  1096. if (2 + OFile->FullPathLen + 1 + FileNameLen + 1 > EFI_PATH_STRING_LENGTH) {
  1097. //
  1098. // Full path length can not surpass 256
  1099. //
  1100. return EFI_INVALID_PARAMETER;
  1101. }
  1102. //
  1103. // Start at current location
  1104. //
  1105. Next = FileName;
  1106. for (;;) {
  1107. //
  1108. // Get the next component name
  1109. //
  1110. FileName = Next;
  1111. Next = FatGetNextNameComponent (FileName, ComponentName);
  1112. //
  1113. // If end of the file name, we're done
  1114. //
  1115. if (ComponentName[0] == 0) {
  1116. if (DirIntended && OFile->ODir == NULL) {
  1117. return EFI_NOT_FOUND;
  1118. }
  1119. NewFileName[0] = 0;
  1120. break;
  1121. }
  1122. //
  1123. // If "dot", then current
  1124. //
  1125. if (StrCmp (ComponentName, L".") == 0) {
  1126. continue;
  1127. }
  1128. //
  1129. // If "dot dot", then parent
  1130. //
  1131. if (StrCmp (ComponentName, L"..") == 0) {
  1132. if (OFile->Parent == NULL) {
  1133. return EFI_INVALID_PARAMETER;
  1134. }
  1135. OFile = OFile->Parent;
  1136. continue;
  1137. }
  1138. if (!FatFileNameIsValid (ComponentName, NewFileName)) {
  1139. return EFI_INVALID_PARAMETER;
  1140. }
  1141. //
  1142. // We have a component name, try to open it
  1143. //
  1144. if (OFile->ODir == NULL) {
  1145. //
  1146. // This file isn't a directory, can't open it
  1147. //
  1148. return EFI_NOT_FOUND;
  1149. }
  1150. //
  1151. // Search the compName in the directory
  1152. //
  1153. Status = FatSearchODir (OFile, NewFileName, &DirEnt);
  1154. if (EFI_ERROR (Status)) {
  1155. return Status;
  1156. }
  1157. if (DirEnt == NULL) {
  1158. //
  1159. // component name is not found in the directory
  1160. //
  1161. if (*Next != 0) {
  1162. return EFI_NOT_FOUND;
  1163. }
  1164. if (DirIntended && (Attributes & FAT_ATTRIBUTE_DIRECTORY) == 0) {
  1165. return EFI_INVALID_PARAMETER;
  1166. }
  1167. //
  1168. // It's the last component name - return with the open
  1169. // path and the remaining name
  1170. //
  1171. break;
  1172. }
  1173. Status = FatOpenDirEnt (OFile, DirEnt);
  1174. if (EFI_ERROR (Status)) {
  1175. return Status;
  1176. }
  1177. OFile = DirEnt->OFile;
  1178. }
  1179. *PtrOFile = OFile;
  1180. return EFI_SUCCESS;
  1181. }