]> git.proxmox.com Git - mirror_edk2.git/blob - FatPkg/EnhancedFatDxe/Open.c
Update copyright notice format
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / Open.c
1 /*++
2
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
8
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.
11
12
13 Module Name:
14
15 open.c
16
17 Abstract:
18
19 Routines dealing with file open
20
21 Revision History
22
23 --*/
24
25 #include "Fat.h"
26
27 EFI_STATUS
28 FatAllocateIFile (
29 IN FAT_OFILE *OFile,
30 OUT FAT_IFILE **PtrIFile
31 )
32 /*++
33
34 Routine Description:
35
36 Create an Open instance for the existing OFile.
37 The IFile of the newly opened file is passed out.
38
39 Arguments:
40
41 OFile - The file that serves as a starting reference point.
42 PtrIFile - The newly generated IFile instance.
43
44 Returns:
45
46 EFI_OUT_OF_RESOURCES - Can not allocate the memory for the IFile
47 EFI_SUCCESS - Create the new IFile for the OFile successfully
48
49 --*/
50 {
51 FAT_IFILE *IFile;
52
53 ASSERT_VOLUME_LOCKED (OFile->Volume);
54
55 //
56 // Allocate a new open instance
57 //
58 IFile = AllocateZeroPool (sizeof (FAT_IFILE));
59 if (IFile == NULL) {
60 return EFI_OUT_OF_RESOURCES;
61 }
62
63 IFile->Signature = FAT_IFILE_SIGNATURE;
64
65 CopyMem (&(IFile->Handle), &FatFileInterface, sizeof (EFI_FILE_PROTOCOL));
66
67 IFile->OFile = OFile;
68 InsertTailList (&OFile->Opens, &IFile->Link);
69
70 *PtrIFile = IFile;
71 return EFI_SUCCESS;
72 }
73
74 EFI_STATUS
75 FatOFileOpen (
76 IN FAT_OFILE *OFile,
77 OUT FAT_IFILE **NewIFile,
78 IN CHAR16 *FileName,
79 IN UINT64 OpenMode,
80 IN UINT8 Attributes
81 )
82 /*++
83
84 Routine Description:
85
86 Open a file for a file name relative to an existing OFile.
87 The IFile of the newly opened file is passed out.
88
89 Arguments:
90
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.
94 OpenMode - Open mode.
95 Attributes - Attributes to set if the file is created.
96
97 Returns:
98
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.
107
108 --*/
109 {
110 FAT_VOLUME *Volume;
111 EFI_STATUS Status;
112 CHAR16 NewFileName[EFI_PATH_STRING_LENGTH];
113 FAT_DIRENT *DirEnt;
114 UINT8 FileAttributes;
115 BOOLEAN WriteMode;
116
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;
122 }
123 //
124 // Verify the source file handle isn't in an error state
125 //
126 Status = OFile->Error;
127 if (EFI_ERROR (Status)) {
128 return Status;
129 }
130 //
131 // Get new OFile for the file
132 //
133 Status = FatLocateOFile (&OFile, FileName, Attributes, NewFileName);
134 if (EFI_ERROR (Status)) {
135 return Status;
136 }
137
138 if (*NewFileName != 0) {
139 //
140 // If there's a remaining part of the name, then we had
141 // better be creating the file in the directory
142 //
143 if ((OpenMode & EFI_FILE_MODE_CREATE) == 0) {
144 return EFI_NOT_FOUND;
145 }
146
147 Status = FatCreateDirEnt (OFile, NewFileName, Attributes, &DirEnt);
148 if (EFI_ERROR (Status)) {
149 return Status;
150 }
151
152 Status = FatOpenDirEnt (OFile, DirEnt);
153 if (EFI_ERROR (Status)) {
154 return Status;
155 }
156
157 OFile = DirEnt->OFile;
158 if (OFile->ODir != NULL) {
159 //
160 // If we just created a directory, we need to create "." and ".."
161 //
162 Status = FatCreateDotDirEnts (OFile);
163 if (EFI_ERROR (Status)) {
164 return Status;
165 }
166 }
167 }
168 //
169 // If the file's attribute is read only, and the open is for
170 // read-write, then the access is denied.
171 //
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;
175 }
176 //
177 // Create an open instance of the OFile
178 //
179 Status = FatAllocateIFile (OFile, NewIFile);
180 if (EFI_ERROR (Status)) {
181 return Status;
182 }
183
184 (*NewIFile)->ReadOnly = (BOOLEAN)!WriteMode;
185
186 DEBUG ((EFI_D_INFO, "FSOpen: Open '%S' %r\n", FileName, Status));
187 return FatOFileFlush (OFile);
188 }
189
190 EFI_STATUS
191 EFIAPI
192 FatOpen (
193 IN EFI_FILE_PROTOCOL *FHand,
194 OUT EFI_FILE_PROTOCOL **NewHandle,
195 IN CHAR16 *FileName,
196 IN UINT64 OpenMode,
197 IN UINT64 Attributes
198 )
199 /*++
200 Routine Description:
201
202 Implements Open() of Simple File System Protocol.
203
204 Arguments:
205
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.
211
212 Returns:
213
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.
220
221 --*/
222 {
223 FAT_IFILE *IFile;
224 FAT_IFILE *NewIFile;
225 FAT_OFILE *OFile;
226 EFI_STATUS Status;
227
228 //
229 // Perform some parameter checking
230 //
231 if (FileName == NULL) {
232 return EFI_INVALID_PARAMETER;
233 }
234 //
235 // Check for a valid mode
236 //
237 switch (OpenMode) {
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:
241 break;
242
243 default:
244 return EFI_INVALID_PARAMETER;
245 }
246
247 //
248 // Check for valid Attributes for file creation case.
249 //
250 if (((OpenMode & EFI_FILE_MODE_CREATE) != 0) && (Attributes & (EFI_FILE_READ_ONLY | (~EFI_FILE_VALID_ATTR))) != 0) {
251 return EFI_INVALID_PARAMETER;
252 }
253
254 IFile = IFILE_FROM_FHAND (FHand);
255 OFile = IFile->OFile;
256
257 //
258 // Lock
259 //
260 FatAcquireLock ();
261
262 //
263 // Open the file
264 //
265 Status = FatOFileOpen (OFile, &NewIFile, FileName, OpenMode, (UINT8) Attributes);
266
267 //
268 // If the file was opened, return the handle to the caller
269 //
270 if (!EFI_ERROR (Status)) {
271 *NewHandle = &NewIFile->Handle;
272 }
273 //
274 // Unlock
275 //
276 Status = FatCleanupVolume (OFile->Volume, NULL, Status);
277 FatReleaseLock ();
278
279 return Status;
280 }