Open.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*++
  2. Copyright (c) 2005 - 2009, 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. open.c
  11. Abstract:
  12. Routines dealing with file open
  13. Revision History
  14. --*/
  15. #include "Fat.h"
  16. EFI_STATUS
  17. FatAllocateIFile (
  18. IN FAT_OFILE *OFile,
  19. OUT FAT_IFILE **PtrIFile
  20. )
  21. /*++
  22. Routine Description:
  23. Create an Open instance for the existing OFile.
  24. The IFile of the newly opened file is passed out.
  25. Arguments:
  26. OFile - The file that serves as a starting reference point.
  27. PtrIFile - The newly generated IFile instance.
  28. Returns:
  29. EFI_OUT_OF_RESOURCES - Can not allocate the memory for the IFile
  30. EFI_SUCCESS - Create the new IFile for the OFile successfully
  31. --*/
  32. {
  33. FAT_IFILE *IFile;
  34. ASSERT_VOLUME_LOCKED (OFile->Volume);
  35. //
  36. // Allocate a new open instance
  37. //
  38. IFile = AllocateZeroPool (sizeof (FAT_IFILE));
  39. if (IFile == NULL) {
  40. return EFI_OUT_OF_RESOURCES;
  41. }
  42. IFile->Signature = FAT_IFILE_SIGNATURE;
  43. CopyMem (&(IFile->Handle), &FatFileInterface, sizeof (EFI_FILE_PROTOCOL));
  44. IFile->OFile = OFile;
  45. InsertTailList (&OFile->Opens, &IFile->Link);
  46. *PtrIFile = IFile;
  47. return EFI_SUCCESS;
  48. }
  49. EFI_STATUS
  50. FatOFileOpen (
  51. IN FAT_OFILE *OFile,
  52. OUT FAT_IFILE **NewIFile,
  53. IN CHAR16 *FileName,
  54. IN UINT64 OpenMode,
  55. IN UINT8 Attributes
  56. )
  57. /*++
  58. Routine Description:
  59. Open a file for a file name relative to an existing OFile.
  60. The IFile of the newly opened file is passed out.
  61. Arguments:
  62. OFile - The file that serves as a starting reference point.
  63. NewIFile - The newly generated IFile instance.
  64. FileName - The file name relative to the OFile.
  65. OpenMode - Open mode.
  66. Attributes - Attributes to set if the file is created.
  67. Returns:
  68. EFI_SUCCESS - Open the file successfully.
  69. EFI_INVALID_PARAMETER - The open mode is conflict with the attributes
  70. or the file name is not valid.
  71. EFI_NOT_FOUND - Conficts between dir intention and attribute.
  72. EFI_WRITE_PROTECTED - Can't open for write if the volume is read only.
  73. EFI_ACCESS_DENIED - If the file's attribute is read only, and the
  74. open is for read-write fail it.
  75. EFI_OUT_OF_RESOURCES - Can not allocate the memory.
  76. --*/
  77. {
  78. FAT_VOLUME *Volume;
  79. EFI_STATUS Status;
  80. CHAR16 NewFileName[EFI_PATH_STRING_LENGTH];
  81. FAT_DIRENT *DirEnt;
  82. UINT8 FileAttributes;
  83. BOOLEAN WriteMode;
  84. Volume = OFile->Volume;
  85. ASSERT_VOLUME_LOCKED (Volume);
  86. WriteMode = (BOOLEAN) (OpenMode & EFI_FILE_MODE_WRITE);
  87. if (Volume->ReadOnly && WriteMode) {
  88. return EFI_WRITE_PROTECTED;
  89. }
  90. //
  91. // Verify the source file handle isn't in an error state
  92. //
  93. Status = OFile->Error;
  94. if (EFI_ERROR (Status)) {
  95. return Status;
  96. }
  97. //
  98. // Get new OFile for the file
  99. //
  100. Status = FatLocateOFile (&OFile, FileName, Attributes, NewFileName);
  101. if (EFI_ERROR (Status)) {
  102. return Status;
  103. }
  104. if (*NewFileName != 0) {
  105. //
  106. // If there's a remaining part of the name, then we had
  107. // better be creating the file in the directory
  108. //
  109. if ((OpenMode & EFI_FILE_MODE_CREATE) == 0) {
  110. return EFI_NOT_FOUND;
  111. }
  112. Status = FatCreateDirEnt (OFile, NewFileName, Attributes, &DirEnt);
  113. if (EFI_ERROR (Status)) {
  114. return Status;
  115. }
  116. Status = FatOpenDirEnt (OFile, DirEnt);
  117. if (EFI_ERROR (Status)) {
  118. return Status;
  119. }
  120. OFile = DirEnt->OFile;
  121. if (OFile->ODir != NULL) {
  122. //
  123. // If we just created a directory, we need to create "." and ".."
  124. //
  125. Status = FatCreateDotDirEnts (OFile);
  126. if (EFI_ERROR (Status)) {
  127. return Status;
  128. }
  129. }
  130. }
  131. //
  132. // If the file's attribute is read only, and the open is for
  133. // read-write, then the access is denied.
  134. //
  135. FileAttributes = OFile->DirEnt->Entry.Attributes;
  136. if ((FileAttributes & EFI_FILE_READ_ONLY) != 0 && (FileAttributes & FAT_ATTRIBUTE_DIRECTORY) == 0 && WriteMode) {
  137. return EFI_ACCESS_DENIED;
  138. }
  139. //
  140. // Create an open instance of the OFile
  141. //
  142. Status = FatAllocateIFile (OFile, NewIFile);
  143. if (EFI_ERROR (Status)) {
  144. return Status;
  145. }
  146. (*NewIFile)->ReadOnly = (BOOLEAN)!WriteMode;
  147. DEBUG ((EFI_D_INFO, "FSOpen: Open '%S' %r\n", FileName, Status));
  148. return FatOFileFlush (OFile);
  149. }
  150. EFI_STATUS
  151. EFIAPI
  152. FatOpen (
  153. IN EFI_FILE_PROTOCOL *FHand,
  154. OUT EFI_FILE_PROTOCOL **NewHandle,
  155. IN CHAR16 *FileName,
  156. IN UINT64 OpenMode,
  157. IN UINT64 Attributes
  158. )
  159. /*++
  160. Routine Description:
  161. Implements Open() of Simple File System Protocol.
  162. Arguments:
  163. FHand - File handle of the file serves as a starting reference point.
  164. NewHandle - Handle of the file that is newly opened.
  165. FileName - File name relative to FHand.
  166. OpenMode - Open mode.
  167. Attributes - Attributes to set if the file is created.
  168. Returns:
  169. EFI_INVALID_PARAMETER - The FileName is NULL or the file string is empty.
  170. The OpenMode is not supported.
  171. The Attributes is not the valid attributes.
  172. EFI_OUT_OF_RESOURCES - Can not allocate the memory for file string.
  173. EFI_SUCCESS - Open the file successfully.
  174. Others - The status of open file.
  175. --*/
  176. {
  177. FAT_IFILE *IFile;
  178. FAT_IFILE *NewIFile;
  179. FAT_OFILE *OFile;
  180. EFI_STATUS Status;
  181. //
  182. // Perform some parameter checking
  183. //
  184. if (FileName == NULL) {
  185. return EFI_INVALID_PARAMETER;
  186. }
  187. //
  188. // Check for a valid mode
  189. //
  190. switch (OpenMode) {
  191. case EFI_FILE_MODE_READ:
  192. case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE:
  193. case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE:
  194. break;
  195. default:
  196. return EFI_INVALID_PARAMETER;
  197. }
  198. //
  199. // Check for valid Attributes for file creation case.
  200. //
  201. if (((OpenMode & EFI_FILE_MODE_CREATE) != 0) && (Attributes & (EFI_FILE_READ_ONLY | (~EFI_FILE_VALID_ATTR))) != 0) {
  202. return EFI_INVALID_PARAMETER;
  203. }
  204. IFile = IFILE_FROM_FHAND (FHand);
  205. OFile = IFile->OFile;
  206. //
  207. // Lock
  208. //
  209. FatAcquireLock ();
  210. //
  211. // Open the file
  212. //
  213. Status = FatOFileOpen (OFile, &NewIFile, FileName, OpenMode, (UINT8) Attributes);
  214. //
  215. // If the file was opened, return the handle to the caller
  216. //
  217. if (!EFI_ERROR (Status)) {
  218. *NewHandle = &NewIFile->Handle;
  219. }
  220. //
  221. // Unlock
  222. //
  223. Status = FatCleanupVolume (OFile->Volume, NULL, Status);
  224. FatReleaseLock ();
  225. return Status;
  226. }