]>
git.proxmox.com Git - mirror_edk2.git/blob - FatPkg/EnhancedFatDxe/Open.c
3 Copyright (c) 2005 - 2009, 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 InsertTailList (&OFile
->Opens
, &IFile
->Link
);
77 OUT FAT_IFILE
**NewIFile
,
86 Open a file for a file name relative to an existing OFile.
87 The IFile of the newly opened file is passed out.
91 OFile - The file that serves as a starting reference point.
92 NewIFile - The newly generated IFile instance.
93 FileName - The file name relative to the OFile.
95 Attributes - Attributes to set if the file is created.
99 EFI_SUCCESS - Open the file successfully.
100 EFI_INVALID_PARAMETER - The open mode is conflict with the attributes
101 or the file name is not valid.
102 EFI_NOT_FOUND - Conficts between dir intention and attribute.
103 EFI_WRITE_PROTECTED - Can't open for write if the volume is read only.
104 EFI_ACCESS_DENIED - If the file's attribute is read only, and the
105 open is for read-write fail it.
106 EFI_OUT_OF_RESOURCES - Can not allocate the memory.
112 CHAR16 NewFileName
[EFI_PATH_STRING_LENGTH
];
114 UINT8 FileAttributes
;
117 Volume
= OFile
->Volume
;
118 ASSERT_VOLUME_LOCKED (Volume
);
119 WriteMode
= (BOOLEAN
) (OpenMode
& EFI_FILE_MODE_WRITE
);
120 if (Volume
->ReadOnly
&& WriteMode
) {
121 return EFI_WRITE_PROTECTED
;
124 // Verify the source file handle isn't in an error state
126 Status
= OFile
->Error
;
127 if (EFI_ERROR (Status
)) {
131 // Get new OFile for the file
133 Status
= FatLocateOFile (&OFile
, FileName
, Attributes
, NewFileName
);
134 if (EFI_ERROR (Status
)) {
138 if (*NewFileName
!= 0) {
140 // If there's a remaining part of the name, then we had
141 // better be creating the file in the directory
143 if ((OpenMode
& EFI_FILE_MODE_CREATE
) == 0) {
144 return EFI_NOT_FOUND
;
147 Status
= FatCreateDirEnt (OFile
, NewFileName
, Attributes
, &DirEnt
);
148 if (EFI_ERROR (Status
)) {
152 Status
= FatOpenDirEnt (OFile
, DirEnt
);
153 if (EFI_ERROR (Status
)) {
157 OFile
= DirEnt
->OFile
;
158 if (OFile
->ODir
!= NULL
) {
160 // If we just created a directory, we need to create "." and ".."
162 Status
= FatCreateDotDirEnts (OFile
);
163 if (EFI_ERROR (Status
)) {
169 // If the file's attribute is read only, and the open is for
170 // read-write, then the access is denied.
172 FileAttributes
= OFile
->DirEnt
->Entry
.Attributes
;
173 if ((FileAttributes
& EFI_FILE_READ_ONLY
) != 0 && (FileAttributes
& FAT_ATTRIBUTE_DIRECTORY
) == 0 && WriteMode
) {
174 return EFI_ACCESS_DENIED
;
177 // Create an open instance of the OFile
179 Status
= FatAllocateIFile (OFile
, NewIFile
);
180 if (EFI_ERROR (Status
)) {
184 (*NewIFile
)->ReadOnly
= (BOOLEAN
)!WriteMode
;
186 DEBUG ((EFI_D_INFO
, "FSOpen: Open '%S' %r\n", FileName
, Status
));
187 return FatOFileFlush (OFile
);
193 IN EFI_FILE_PROTOCOL
*FHand
,
194 OUT EFI_FILE_PROTOCOL
**NewHandle
,
202 Implements Open() of Simple File System Protocol.
206 FHand - File handle of the file serves as a starting reference point.
207 NewHandle - Handle of the file that is newly opened.
208 FileName - File name relative to FHand.
209 OpenMode - Open mode.
210 Attributes - Attributes to set if the file is created.
214 EFI_INVALID_PARAMETER - The FileName is NULL or the file string is empty.
215 The OpenMode is not supported.
216 The Attributes is not the valid attributes.
217 EFI_OUT_OF_RESOURCES - Can not allocate the memory for file string.
218 EFI_SUCCESS - Open the file successfully.
219 Others - The status of open file.
229 // Perform some parameter checking
231 if (FileName
== NULL
) {
232 return EFI_INVALID_PARAMETER
;
235 // Check for a valid mode
238 case EFI_FILE_MODE_READ
:
239 case EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
:
240 case EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
:
244 return EFI_INVALID_PARAMETER
;
248 // Check for valid Attributes for file creation case.
250 if (((OpenMode
& EFI_FILE_MODE_CREATE
) != 0) && (Attributes
& (EFI_FILE_READ_ONLY
| (~EFI_FILE_VALID_ATTR
))) != 0) {
251 return EFI_INVALID_PARAMETER
;
254 IFile
= IFILE_FROM_FHAND (FHand
);
255 OFile
= IFile
->OFile
;
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
);