3 Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials are licensed and made available
5 under the terms and conditions of the BSD License which accompanies this
6 distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Routines dealing with file open
30 OUT FAT_IFILE
**PtrIFile
36 Create an Open instance for the existing OFile.
37 The IFile of the newly opened file is passed out.
41 OFile - The file that serves as a starting reference point.
42 PtrIFile - The newly generated IFile instance.
46 EFI_OUT_OF_RESOURCES - Can not allocate the memory for the IFile
47 EFI_SUCCESS - Create the new IFile for the OFile successfully
53 ASSERT_VOLUME_LOCKED (OFile
->Volume
);
56 // Allocate a new open instance
58 IFile
= AllocateZeroPool (sizeof (FAT_IFILE
));
60 return EFI_OUT_OF_RESOURCES
;
63 IFile
->Signature
= FAT_IFILE_SIGNATURE
;
65 CopyMem (&(IFile
->Handle
), &FatFileInterface
, sizeof (EFI_FILE_PROTOCOL
));
68 // Report the correct revision number based on the DiskIo2 availability
70 if (OFile
->Volume
->DiskIo2
!= NULL
) {
71 IFile
->Handle
.Revision
= EFI_FILE_PROTOCOL_REVISION2
;
73 IFile
->Handle
.Revision
= EFI_FILE_PROTOCOL_REVISION
;
77 InsertTailList (&OFile
->Opens
, &IFile
->Link
);
78 InitializeListHead (&IFile
->Tasks
);
87 OUT FAT_IFILE
**NewIFile
,
96 Open a file for a file name relative to an existing OFile.
97 The IFile of the newly opened file is passed out.
101 OFile - The file that serves as a starting reference point.
102 NewIFile - The newly generated IFile instance.
103 FileName - The file name relative to the OFile.
104 OpenMode - Open mode.
105 Attributes - Attributes to set if the file is created.
109 EFI_SUCCESS - Open the file successfully.
110 EFI_INVALID_PARAMETER - The open mode is conflict with the attributes
111 or the file name is not valid.
112 EFI_NOT_FOUND - Conficts between dir intention and attribute.
113 EFI_WRITE_PROTECTED - Can't open for write if the volume is read only.
114 EFI_ACCESS_DENIED - If the file's attribute is read only, and the
115 open is for read-write fail it.
116 EFI_OUT_OF_RESOURCES - Can not allocate the memory.
122 CHAR16 NewFileName
[EFI_PATH_STRING_LENGTH
];
124 UINT8 FileAttributes
;
127 Volume
= OFile
->Volume
;
128 ASSERT_VOLUME_LOCKED (Volume
);
129 WriteMode
= (BOOLEAN
) (OpenMode
& EFI_FILE_MODE_WRITE
);
130 if (Volume
->ReadOnly
&& WriteMode
) {
131 return EFI_WRITE_PROTECTED
;
134 // Verify the source file handle isn't in an error state
136 Status
= OFile
->Error
;
137 if (EFI_ERROR (Status
)) {
141 // Get new OFile for the file
143 Status
= FatLocateOFile (&OFile
, FileName
, Attributes
, NewFileName
);
144 if (EFI_ERROR (Status
)) {
148 if (*NewFileName
!= 0) {
150 // If there's a remaining part of the name, then we had
151 // better be creating the file in the directory
153 if ((OpenMode
& EFI_FILE_MODE_CREATE
) == 0) {
154 return EFI_NOT_FOUND
;
157 Status
= FatCreateDirEnt (OFile
, NewFileName
, Attributes
, &DirEnt
);
158 if (EFI_ERROR (Status
)) {
162 Status
= FatOpenDirEnt (OFile
, DirEnt
);
163 if (EFI_ERROR (Status
)) {
167 OFile
= DirEnt
->OFile
;
168 if (OFile
->ODir
!= NULL
) {
170 // If we just created a directory, we need to create "." and ".."
172 Status
= FatCreateDotDirEnts (OFile
);
173 if (EFI_ERROR (Status
)) {
179 // If the file's attribute is read only, and the open is for
180 // read-write, then the access is denied.
182 FileAttributes
= OFile
->DirEnt
->Entry
.Attributes
;
183 if ((FileAttributes
& EFI_FILE_READ_ONLY
) != 0 && (FileAttributes
& FAT_ATTRIBUTE_DIRECTORY
) == 0 && WriteMode
) {
184 return EFI_ACCESS_DENIED
;
187 // Create an open instance of the OFile
189 Status
= FatAllocateIFile (OFile
, NewIFile
);
190 if (EFI_ERROR (Status
)) {
194 (*NewIFile
)->ReadOnly
= (BOOLEAN
)!WriteMode
;
196 DEBUG ((EFI_D_INFO
, "FSOpen: Open '%S' %r\n", FileName
, Status
));
197 return FatOFileFlush (OFile
);
202 IN EFI_FILE_PROTOCOL
*FHand
,
203 OUT EFI_FILE_PROTOCOL
**NewHandle
,
206 IN UINT64 Attributes
,
207 IN OUT EFI_FILE_IO_TOKEN
*Token
212 Implements OpenEx() of Simple File System Protocol.
216 FHand - File handle of the file serves as a starting reference point.
217 NewHandle - Handle of the file that is newly opened.
218 FileName - File name relative to FHand.
219 OpenMode - Open mode.
220 Attributes - Attributes to set if the file is created.
221 Token - A pointer to the token associated with the transaction.
225 EFI_INVALID_PARAMETER - The FileName is NULL or the file string is empty.
226 The OpenMode is not supported.
227 The Attributes is not the valid attributes.
228 EFI_OUT_OF_RESOURCES - Can not allocate the memory for file string.
229 EFI_SUCCESS - Open the file successfully.
230 Others - The status of open file.
241 // Perform some parameter checking
243 if (FileName
== NULL
) {
244 return EFI_INVALID_PARAMETER
;
247 // Check for a valid mode
250 case EFI_FILE_MODE_READ
:
251 case EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
:
252 case EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
:
256 return EFI_INVALID_PARAMETER
;
260 // Check for valid Attributes for file creation case.
262 if (((OpenMode
& EFI_FILE_MODE_CREATE
) != 0) && (Attributes
& (EFI_FILE_READ_ONLY
| (~EFI_FILE_VALID_ATTR
))) != 0) {
263 return EFI_INVALID_PARAMETER
;
266 IFile
= IFILE_FROM_FHAND (FHand
);
267 OFile
= IFile
->OFile
;
271 FatWaitNonblockingTask (IFile
);
274 // Caller shouldn't call the non-blocking interfaces if the low layer doesn't support DiskIo2.
275 // But if it calls, the below check can avoid crash.
277 if (FHand
->Revision
< EFI_FILE_PROTOCOL_REVISION2
) {
278 return EFI_UNSUPPORTED
;
280 Task
= FatCreateTask (IFile
, Token
);
282 return EFI_OUT_OF_RESOURCES
;
294 Status
= FatOFileOpen (OFile
, &NewIFile
, FileName
, OpenMode
, (UINT8
) Attributes
);
297 // If the file was opened, return the handle to the caller
299 if (!EFI_ERROR (Status
)) {
300 *NewHandle
= &NewIFile
->Handle
;
305 Status
= FatCleanupVolume (OFile
->Volume
, NULL
, Status
, Task
);
309 if (!EFI_ERROR (Status
)) {
310 Status
= FatQueueTask (IFile
, Task
);
312 FatDestroyTask (Task
);
322 IN EFI_FILE_PROTOCOL
*FHand
,
323 OUT EFI_FILE_PROTOCOL
**NewHandle
,
331 Implements Open() of Simple File System Protocol.
335 FHand - File handle of the file serves as a starting reference point.
336 NewHandle - Handle of the file that is newly opened.
337 FileName - File name relative to FHand.
338 OpenMode - Open mode.
339 Attributes - Attributes to set if the file is created.
343 EFI_INVALID_PARAMETER - The FileName is NULL or the file string is empty.
344 The OpenMode is not supported.
345 The Attributes is not the valid attributes.
346 EFI_OUT_OF_RESOURCES - Can not allocate the memory for file string.
347 EFI_SUCCESS - Open the file successfully.
348 Others - The status of open file.
352 return FatOpenEx (FHand
, NewHandle
, FileName
, OpenMode
, Attributes
, NULL
);