]> git.proxmox.com Git - mirror_edk2.git/blame - FatPkg/EnhancedFatDxe/Open.c
Add the missing EFIAPI keyword to solve build failure in GCC.
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / Open.c
CommitLineData
b9ec9330
QH
1/*++\r
2\r
149d6335 3Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.<BR>\r
6163cc98 4This program and the accompanying materials are licensed and made available\r
b9ec9330
QH
5under the terms and conditions of the BSD License which accompanies this\r
6distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12\r
13Module Name:\r
14\r
15 open.c\r
16\r
17Abstract:\r
18\r
19 Routines dealing with file open\r
20\r
21Revision History\r
22\r
23--*/\r
24\r
25#include "Fat.h"\r
26\r
27EFI_STATUS\r
28FatAllocateIFile (\r
29 IN FAT_OFILE *OFile,\r
30 OUT FAT_IFILE **PtrIFile\r
31 )\r
32/*++\r
33\r
34Routine Description:\r
35\r
36 Create an Open instance for the existing OFile.\r
37 The IFile of the newly opened file is passed out.\r
38\r
39Arguments:\r
40\r
41 OFile - The file that serves as a starting reference point.\r
42 PtrIFile - The newly generated IFile instance.\r
43\r
44Returns:\r
45\r
46 EFI_OUT_OF_RESOURCES - Can not allocate the memory for the IFile\r
47 EFI_SUCCESS - Create the new IFile for the OFile successfully\r
48\r
49--*/\r
50{\r
51 FAT_IFILE *IFile;\r
52\r
53 ASSERT_VOLUME_LOCKED (OFile->Volume);\r
54\r
55 //\r
56 // Allocate a new open instance\r
57 //\r
58 IFile = AllocateZeroPool (sizeof (FAT_IFILE));\r
59 if (IFile == NULL) {\r
60 return EFI_OUT_OF_RESOURCES;\r
61 }\r
62\r
63 IFile->Signature = FAT_IFILE_SIGNATURE;\r
64\r
dba03ba1 65 CopyMem (&(IFile->Handle), &FatFileInterface, sizeof (EFI_FILE_PROTOCOL));\r
b9ec9330 66\r
149d6335
RN
67 //\r
68 // Report the correct revision number based on the DiskIo2 availability\r
69 //\r
70 if (OFile->Volume->DiskIo2 != NULL) {\r
71 IFile->Handle.Revision = EFI_FILE_PROTOCOL_REVISION2;\r
72 } else {\r
73 IFile->Handle.Revision = EFI_FILE_PROTOCOL_REVISION;\r
74 }\r
75\r
b9ec9330
QH
76 IFile->OFile = OFile;\r
77 InsertTailList (&OFile->Opens, &IFile->Link);\r
149d6335 78 InitializeListHead (&IFile->Tasks);\r
b9ec9330
QH
79\r
80 *PtrIFile = IFile;\r
81 return EFI_SUCCESS;\r
82}\r
83\r
84EFI_STATUS\r
85FatOFileOpen (\r
86 IN FAT_OFILE *OFile,\r
87 OUT FAT_IFILE **NewIFile,\r
88 IN CHAR16 *FileName,\r
89 IN UINT64 OpenMode,\r
90 IN UINT8 Attributes\r
91 )\r
92/*++\r
93\r
94Routine Description:\r
95\r
96 Open a file for a file name relative to an existing OFile.\r
97 The IFile of the newly opened file is passed out.\r
98\r
99Arguments:\r
100\r
101 OFile - The file that serves as a starting reference point.\r
102 NewIFile - The newly generated IFile instance.\r
103 FileName - The file name relative to the OFile.\r
104 OpenMode - Open mode.\r
105 Attributes - Attributes to set if the file is created.\r
106\r
107Returns:\r
108\r
109 EFI_SUCCESS - Open the file successfully.\r
110 EFI_INVALID_PARAMETER - The open mode is conflict with the attributes\r
111 or the file name is not valid.\r
112 EFI_NOT_FOUND - Conficts between dir intention and attribute.\r
113 EFI_WRITE_PROTECTED - Can't open for write if the volume is read only.\r
114 EFI_ACCESS_DENIED - If the file's attribute is read only, and the\r
115 open is for read-write fail it.\r
116 EFI_OUT_OF_RESOURCES - Can not allocate the memory.\r
117\r
118--*/\r
119{\r
120 FAT_VOLUME *Volume;\r
121 EFI_STATUS Status;\r
122 CHAR16 NewFileName[EFI_PATH_STRING_LENGTH];\r
123 FAT_DIRENT *DirEnt;\r
124 UINT8 FileAttributes;\r
125 BOOLEAN WriteMode;\r
126\r
127 Volume = OFile->Volume;\r
128 ASSERT_VOLUME_LOCKED (Volume);\r
129 WriteMode = (BOOLEAN) (OpenMode & EFI_FILE_MODE_WRITE);\r
130 if (Volume->ReadOnly && WriteMode) {\r
131 return EFI_WRITE_PROTECTED;\r
132 }\r
133 //\r
134 // Verify the source file handle isn't in an error state\r
135 //\r
136 Status = OFile->Error;\r
137 if (EFI_ERROR (Status)) {\r
138 return Status;\r
139 }\r
140 //\r
141 // Get new OFile for the file\r
142 //\r
143 Status = FatLocateOFile (&OFile, FileName, Attributes, NewFileName);\r
144 if (EFI_ERROR (Status)) {\r
145 return Status;\r
146 }\r
147\r
148 if (*NewFileName != 0) {\r
149 //\r
150 // If there's a remaining part of the name, then we had\r
151 // better be creating the file in the directory\r
152 //\r
153 if ((OpenMode & EFI_FILE_MODE_CREATE) == 0) {\r
154 return EFI_NOT_FOUND;\r
155 }\r
156\r
157 Status = FatCreateDirEnt (OFile, NewFileName, Attributes, &DirEnt);\r
158 if (EFI_ERROR (Status)) {\r
159 return Status;\r
160 }\r
161\r
162 Status = FatOpenDirEnt (OFile, DirEnt);\r
163 if (EFI_ERROR (Status)) {\r
164 return Status;\r
165 }\r
166\r
167 OFile = DirEnt->OFile;\r
168 if (OFile->ODir != NULL) {\r
169 //\r
170 // If we just created a directory, we need to create "." and ".."\r
171 //\r
172 Status = FatCreateDotDirEnts (OFile);\r
173 if (EFI_ERROR (Status)) {\r
174 return Status;\r
175 }\r
176 }\r
177 }\r
178 //\r
179 // If the file's attribute is read only, and the open is for\r
180 // read-write, then the access is denied.\r
181 //\r
182 FileAttributes = OFile->DirEnt->Entry.Attributes;\r
183 if ((FileAttributes & EFI_FILE_READ_ONLY) != 0 && (FileAttributes & FAT_ATTRIBUTE_DIRECTORY) == 0 && WriteMode) {\r
184 return EFI_ACCESS_DENIED;\r
185 }\r
186 //\r
187 // Create an open instance of the OFile\r
188 //\r
189 Status = FatAllocateIFile (OFile, NewIFile);\r
190 if (EFI_ERROR (Status)) {\r
191 return Status;\r
192 }\r
193\r
194 (*NewIFile)->ReadOnly = (BOOLEAN)!WriteMode;\r
195\r
196 DEBUG ((EFI_D_INFO, "FSOpen: Open '%S' %r\n", FileName, Status));\r
197 return FatOFileFlush (OFile);\r
198}\r
199\r
200EFI_STATUS\r
833b5a77 201EFIAPI\r
149d6335
RN
202FatOpenEx (\r
203 IN EFI_FILE_PROTOCOL *FHand,\r
204 OUT EFI_FILE_PROTOCOL **NewHandle,\r
205 IN CHAR16 *FileName,\r
206 IN UINT64 OpenMode,\r
207 IN UINT64 Attributes,\r
208 IN OUT EFI_FILE_IO_TOKEN *Token\r
b9ec9330
QH
209 )\r
210/*++\r
211Routine Description:\r
212\r
149d6335 213 Implements OpenEx() of Simple File System Protocol.\r
b9ec9330
QH
214\r
215Arguments:\r
216\r
217 FHand - File handle of the file serves as a starting reference point.\r
218 NewHandle - Handle of the file that is newly opened.\r
219 FileName - File name relative to FHand.\r
220 OpenMode - Open mode.\r
221 Attributes - Attributes to set if the file is created.\r
149d6335 222 Token - A pointer to the token associated with the transaction.\r
b9ec9330
QH
223\r
224Returns:\r
225\r
226 EFI_INVALID_PARAMETER - The FileName is NULL or the file string is empty.\r
227 The OpenMode is not supported.\r
228 The Attributes is not the valid attributes.\r
229 EFI_OUT_OF_RESOURCES - Can not allocate the memory for file string.\r
230 EFI_SUCCESS - Open the file successfully.\r
231 Others - The status of open file.\r
232\r
233--*/\r
234{\r
235 FAT_IFILE *IFile;\r
236 FAT_IFILE *NewIFile;\r
237 FAT_OFILE *OFile;\r
238 EFI_STATUS Status;\r
149d6335 239 FAT_TASK *Task;\r
b9ec9330
QH
240\r
241 //\r
242 // Perform some parameter checking\r
243 //\r
244 if (FileName == NULL) {\r
245 return EFI_INVALID_PARAMETER;\r
246 }\r
247 //\r
248 // Check for a valid mode\r
249 //\r
250 switch (OpenMode) {\r
251 case EFI_FILE_MODE_READ:\r
252 case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE:\r
253 case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE:\r
254 break;\r
255\r
256 default:\r
257 return EFI_INVALID_PARAMETER;\r
258 }\r
149d6335 259\r
b9ec9330 260 //\r
5ff2f40e 261 // Check for valid Attributes for file creation case. \r
b9ec9330 262 //\r
5ff2f40e 263 if (((OpenMode & EFI_FILE_MODE_CREATE) != 0) && (Attributes & (EFI_FILE_READ_ONLY | (~EFI_FILE_VALID_ATTR))) != 0) {\r
149d6335 264 return EFI_INVALID_PARAMETER;\r
b9ec9330 265 }\r
149d6335 266\r
b9ec9330
QH
267 IFile = IFILE_FROM_FHAND (FHand);\r
268 OFile = IFile->OFile;\r
149d6335
RN
269 Task = NULL;\r
270\r
271 if (Token == NULL) {\r
272 FatWaitNonblockingTask (IFile);\r
273 } else {\r
274 //\r
275 // Caller shouldn't call the non-blocking interfaces if the low layer doesn't support DiskIo2.\r
276 // But if it calls, the below check can avoid crash.\r
277 //\r
278 if (FHand->Revision < EFI_FILE_PROTOCOL_REVISION2) {\r
279 return EFI_UNSUPPORTED;\r
280 }\r
281 Task = FatCreateTask (IFile, Token);\r
282 if (Task == NULL) {\r
283 return EFI_OUT_OF_RESOURCES;\r
284 }\r
285 }\r
b9ec9330
QH
286\r
287 //\r
288 // Lock\r
289 //\r
290 FatAcquireLock ();\r
291\r
292 //\r
293 // Open the file\r
294 //\r
295 Status = FatOFileOpen (OFile, &NewIFile, FileName, OpenMode, (UINT8) Attributes);\r
296\r
297 //\r
298 // If the file was opened, return the handle to the caller\r
299 //\r
300 if (!EFI_ERROR (Status)) {\r
301 *NewHandle = &NewIFile->Handle;\r
302 }\r
303 //\r
304 // Unlock\r
305 //\r
149d6335 306 Status = FatCleanupVolume (OFile->Volume, NULL, Status, Task);\r
b9ec9330
QH
307 FatReleaseLock ();\r
308\r
149d6335
RN
309 if (Token != NULL) {\r
310 if (!EFI_ERROR (Status)) {\r
311 Status = FatQueueTask (IFile, Task);\r
312 } else {\r
313 FatDestroyTask (Task);\r
314 }\r
315 }\r
316\r
b9ec9330
QH
317 return Status;\r
318}\r
149d6335
RN
319\r
320EFI_STATUS\r
321EFIAPI\r
322FatOpen (\r
323 IN EFI_FILE_PROTOCOL *FHand,\r
324 OUT EFI_FILE_PROTOCOL **NewHandle,\r
325 IN CHAR16 *FileName,\r
326 IN UINT64 OpenMode,\r
327 IN UINT64 Attributes\r
328 )\r
329/*++\r
330Routine Description:\r
331\r
332 Implements Open() of Simple File System Protocol.\r
333\r
334Arguments:\r
335\r
336 FHand - File handle of the file serves as a starting reference point.\r
337 NewHandle - Handle of the file that is newly opened.\r
338 FileName - File name relative to FHand.\r
339 OpenMode - Open mode.\r
340 Attributes - Attributes to set if the file is created.\r
341\r
342Returns:\r
343\r
344 EFI_INVALID_PARAMETER - The FileName is NULL or the file string is empty.\r
345 The OpenMode is not supported.\r
346 The Attributes is not the valid attributes.\r
347 EFI_OUT_OF_RESOURCES - Can not allocate the memory for file string.\r
348 EFI_SUCCESS - Open the file successfully.\r
349 Others - The status of open file.\r
350\r
351--*/\r
352{\r
353 return FatOpenEx (FHand, NewHandle, FileName, OpenMode, Attributes, NULL);\r
354}\r