2 Routines dealing with file open.
4 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
13 Create an Open instance for the existing OFile.
14 The IFile of the newly opened file is passed out.
16 @param OFile - The file that serves as a starting reference point.
17 @param PtrIFile - The newly generated IFile instance.
19 @retval EFI_OUT_OF_RESOURCES - Can not allocate the memory for the IFile
20 @retval EFI_SUCCESS - Create the new IFile for the OFile successfully
26 OUT FAT_IFILE
**PtrIFile
31 ASSERT_VOLUME_LOCKED (OFile
->Volume
);
34 // Allocate a new open instance
36 IFile
= AllocateZeroPool (sizeof (FAT_IFILE
));
38 return EFI_OUT_OF_RESOURCES
;
41 IFile
->Signature
= FAT_IFILE_SIGNATURE
;
43 CopyMem (&(IFile
->Handle
), &FatFileInterface
, sizeof (EFI_FILE_PROTOCOL
));
46 // Report the correct revision number based on the DiskIo2 availability
48 if (OFile
->Volume
->DiskIo2
!= NULL
) {
49 IFile
->Handle
.Revision
= EFI_FILE_PROTOCOL_REVISION2
;
51 IFile
->Handle
.Revision
= EFI_FILE_PROTOCOL_REVISION
;
55 InsertTailList (&OFile
->Opens
, &IFile
->Link
);
56 InitializeListHead (&IFile
->Tasks
);
64 Open a file for a file name relative to an existing OFile.
65 The IFile of the newly opened file is passed out.
67 @param OFile - The file that serves as a starting reference point.
68 @param NewIFile - The newly generated IFile instance.
69 @param FileName - The file name relative to the OFile.
70 @param OpenMode - Open mode.
71 @param Attributes - Attributes to set if the file is created.
74 @retval EFI_SUCCESS - Open the file successfully.
75 @retval EFI_INVALID_PARAMETER - The open mode is conflict with the attributes
76 or the file name is not valid.
77 @retval EFI_NOT_FOUND - Conflicts between dir intention and attribute.
78 @retval EFI_WRITE_PROTECTED - Can't open for write if the volume is read only.
79 @retval EFI_ACCESS_DENIED - If the file's attribute is read only, and the
80 open is for read-write fail it.
81 @retval EFI_OUT_OF_RESOURCES - Can not allocate the memory.
87 OUT FAT_IFILE
**NewIFile
,
95 CHAR16 NewFileName
[EFI_PATH_STRING_LENGTH
];
101 Volume
= OFile
->Volume
;
102 ASSERT_VOLUME_LOCKED (Volume
);
103 WriteMode
= (BOOLEAN
) (OpenMode
& EFI_FILE_MODE_WRITE
);
104 if (Volume
->ReadOnly
&& WriteMode
) {
105 return EFI_WRITE_PROTECTED
;
108 // Verify the source file handle isn't in an error state
110 Status
= OFile
->Error
;
111 if (EFI_ERROR (Status
)) {
115 // Get new OFile for the file
117 Status
= FatLocateOFile (&OFile
, FileName
, Attributes
, NewFileName
);
118 if (EFI_ERROR (Status
)) {
122 if (*NewFileName
!= 0) {
124 // If there's a remaining part of the name, then we had
125 // better be creating the file in the directory
127 if ((OpenMode
& EFI_FILE_MODE_CREATE
) == 0) {
128 return EFI_NOT_FOUND
;
131 Status
= FatCreateDirEnt (OFile
, NewFileName
, Attributes
, &DirEnt
);
132 if (EFI_ERROR (Status
)) {
136 ASSERT (DirEnt
!= NULL
);
137 Status
= FatOpenDirEnt (OFile
, DirEnt
);
138 if (EFI_ERROR (Status
)) {
142 OFile
= DirEnt
->OFile
;
143 if (OFile
->ODir
!= NULL
) {
145 // If we just created a directory, we need to create "." and ".."
147 Status
= FatCreateDotDirEnts (OFile
);
148 if (EFI_ERROR (Status
)) {
154 // If the file's attribute is read only, and the open is for
155 // read-write, then the access is denied.
157 FileAttributes
= OFile
->DirEnt
->Entry
.Attributes
;
158 if ((FileAttributes
& EFI_FILE_READ_ONLY
) != 0 && (FileAttributes
& FAT_ATTRIBUTE_DIRECTORY
) == 0 && WriteMode
) {
159 return EFI_ACCESS_DENIED
;
162 // Create an open instance of the OFile
164 Status
= FatAllocateIFile (OFile
, NewIFile
);
165 if (EFI_ERROR (Status
)) {
169 (*NewIFile
)->ReadOnly
= (BOOLEAN
)!WriteMode
;
171 DEBUG ((EFI_D_INFO
, "FSOpen: Open '%S' %r\n", FileName
, Status
));
172 return FatOFileFlush (OFile
);
177 Implements OpenEx() of Simple File System Protocol.
179 @param FHand - File handle of the file serves as a starting reference point.
180 @param NewHandle - Handle of the file that is newly opened.
181 @param FileName - File name relative to FHand.
182 @param OpenMode - Open mode.
183 @param Attributes - Attributes to set if the file is created.
184 @param Token - A pointer to the token associated with the transaction.:
186 @retval EFI_INVALID_PARAMETER - The FileName is NULL or the file string is empty.
187 The OpenMode is not supported.
188 The Attributes is not the valid attributes.
189 @retval EFI_OUT_OF_RESOURCES - Can not allocate the memory for file string.
190 @retval EFI_SUCCESS - Open the file successfully.
191 @return Others - The status of open file.
197 IN EFI_FILE_PROTOCOL
*FHand
,
198 OUT EFI_FILE_PROTOCOL
**NewHandle
,
201 IN UINT64 Attributes
,
202 IN OUT EFI_FILE_IO_TOKEN
*Token
212 // Perform some parameter checking
214 if (FileName
== NULL
) {
215 return EFI_INVALID_PARAMETER
;
218 // Check for a valid mode
221 case EFI_FILE_MODE_READ
:
222 case EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
:
223 case EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
:
227 return EFI_INVALID_PARAMETER
;
231 // Check for valid Attributes for file creation case.
233 if (((OpenMode
& EFI_FILE_MODE_CREATE
) != 0) && (Attributes
& (EFI_FILE_READ_ONLY
| (~EFI_FILE_VALID_ATTR
))) != 0) {
234 return EFI_INVALID_PARAMETER
;
237 IFile
= IFILE_FROM_FHAND (FHand
);
238 OFile
= IFile
->OFile
;
242 FatWaitNonblockingTask (IFile
);
245 // Caller shouldn't call the non-blocking interfaces if the low layer doesn't support DiskIo2.
246 // But if it calls, the below check can avoid crash.
248 if (FHand
->Revision
< EFI_FILE_PROTOCOL_REVISION2
) {
249 return EFI_UNSUPPORTED
;
251 Task
= FatCreateTask (IFile
, Token
);
253 return EFI_OUT_OF_RESOURCES
;
265 Status
= FatOFileOpen (OFile
, &NewIFile
, FileName
, OpenMode
, (UINT8
) Attributes
);
268 // If the file was opened, return the handle to the caller
270 if (!EFI_ERROR (Status
)) {
271 *NewHandle
= &NewIFile
->Handle
;
276 Status
= FatCleanupVolume (OFile
->Volume
, NULL
, Status
, Task
);
280 if (!EFI_ERROR (Status
)) {
281 Status
= FatQueueTask (IFile
, Task
);
283 FatDestroyTask (Task
);
292 Implements Open() of Simple File System Protocol.
295 @param FHand - File handle of the file serves as a starting reference point.
296 @param NewHandle - Handle of the file that is newly opened.
297 @param FileName - File name relative to FHand.
298 @param OpenMode - Open mode.
299 @param Attributes - Attributes to set if the file is created.
301 @retval EFI_INVALID_PARAMETER - The FileName is NULL or the file string is empty.
302 The OpenMode is not supported.
303 The Attributes is not the valid attributes.
304 @retval EFI_OUT_OF_RESOURCES - Can not allocate the memory for file string.
305 @retval EFI_SUCCESS - Open the file successfully.
306 @return Others - The status of open file.
312 IN EFI_FILE_PROTOCOL
*FHand
,
313 OUT EFI_FILE_PROTOCOL
**NewHandle
,
319 return FatOpenEx (FHand
, NewHandle
, FileName
, OpenMode
, Attributes
, NULL
);