]>
Commit | Line | Data |
---|---|---|
b9ec9330 QH |
1 | /*++\r |
2 | \r | |
6163cc98 HT |
3 | Copyright (c) 2005 - 2009, Intel Corporation. All rights reserved.<BR>\r |
4 | This program and the accompanying materials are licensed and made available\r | |
b9ec9330 QH |
5 | under the terms and conditions of the BSD License which accompanies this\r |
6 | distribution. The full text of the license may be found at\r | |
7 | http://opensource.org/licenses/bsd-license.php\r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
11 | \r | |
12 | \r | |
13 | Module Name:\r | |
14 | \r | |
15 | open.c\r | |
16 | \r | |
17 | Abstract:\r | |
18 | \r | |
19 | Routines dealing with file open\r | |
20 | \r | |
21 | Revision History\r | |
22 | \r | |
23 | --*/\r | |
24 | \r | |
25 | #include "Fat.h"\r | |
26 | \r | |
27 | EFI_STATUS\r | |
28 | FatAllocateIFile (\r | |
29 | IN FAT_OFILE *OFile,\r | |
30 | OUT FAT_IFILE **PtrIFile\r | |
31 | )\r | |
32 | /*++\r | |
33 | \r | |
34 | Routine 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 | |
39 | Arguments:\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 | |
44 | Returns:\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 QH |
66 | \r |
67 | IFile->OFile = OFile;\r | |
68 | InsertTailList (&OFile->Opens, &IFile->Link);\r | |
69 | \r | |
70 | *PtrIFile = IFile;\r | |
71 | return EFI_SUCCESS;\r | |
72 | }\r | |
73 | \r | |
74 | EFI_STATUS\r | |
75 | FatOFileOpen (\r | |
76 | IN FAT_OFILE *OFile,\r | |
77 | OUT FAT_IFILE **NewIFile,\r | |
78 | IN CHAR16 *FileName,\r | |
79 | IN UINT64 OpenMode,\r | |
80 | IN UINT8 Attributes\r | |
81 | )\r | |
82 | /*++\r | |
83 | \r | |
84 | Routine Description:\r | |
85 | \r | |
86 | Open a file for a file name relative to an existing OFile.\r | |
87 | The IFile of the newly opened file is passed out.\r | |
88 | \r | |
89 | Arguments:\r | |
90 | \r | |
91 | OFile - The file that serves as a starting reference point.\r | |
92 | NewIFile - The newly generated IFile instance.\r | |
93 | FileName - The file name relative to the OFile.\r | |
94 | OpenMode - Open mode.\r | |
95 | Attributes - Attributes to set if the file is created.\r | |
96 | \r | |
97 | Returns:\r | |
98 | \r | |
99 | EFI_SUCCESS - Open the file successfully.\r | |
100 | EFI_INVALID_PARAMETER - The open mode is conflict with the attributes\r | |
101 | or the file name is not valid.\r | |
102 | EFI_NOT_FOUND - Conficts between dir intention and attribute.\r | |
103 | EFI_WRITE_PROTECTED - Can't open for write if the volume is read only.\r | |
104 | EFI_ACCESS_DENIED - If the file's attribute is read only, and the\r | |
105 | open is for read-write fail it.\r | |
106 | EFI_OUT_OF_RESOURCES - Can not allocate the memory.\r | |
107 | \r | |
108 | --*/\r | |
109 | {\r | |
110 | FAT_VOLUME *Volume;\r | |
111 | EFI_STATUS Status;\r | |
112 | CHAR16 NewFileName[EFI_PATH_STRING_LENGTH];\r | |
113 | FAT_DIRENT *DirEnt;\r | |
114 | UINT8 FileAttributes;\r | |
115 | BOOLEAN WriteMode;\r | |
116 | \r | |
117 | Volume = OFile->Volume;\r | |
118 | ASSERT_VOLUME_LOCKED (Volume);\r | |
119 | WriteMode = (BOOLEAN) (OpenMode & EFI_FILE_MODE_WRITE);\r | |
120 | if (Volume->ReadOnly && WriteMode) {\r | |
121 | return EFI_WRITE_PROTECTED;\r | |
122 | }\r | |
123 | //\r | |
124 | // Verify the source file handle isn't in an error state\r | |
125 | //\r | |
126 | Status = OFile->Error;\r | |
127 | if (EFI_ERROR (Status)) {\r | |
128 | return Status;\r | |
129 | }\r | |
130 | //\r | |
131 | // Get new OFile for the file\r | |
132 | //\r | |
133 | Status = FatLocateOFile (&OFile, FileName, Attributes, NewFileName);\r | |
134 | if (EFI_ERROR (Status)) {\r | |
135 | return Status;\r | |
136 | }\r | |
137 | \r | |
138 | if (*NewFileName != 0) {\r | |
139 | //\r | |
140 | // If there's a remaining part of the name, then we had\r | |
141 | // better be creating the file in the directory\r | |
142 | //\r | |
143 | if ((OpenMode & EFI_FILE_MODE_CREATE) == 0) {\r | |
144 | return EFI_NOT_FOUND;\r | |
145 | }\r | |
146 | \r | |
147 | Status = FatCreateDirEnt (OFile, NewFileName, Attributes, &DirEnt);\r | |
148 | if (EFI_ERROR (Status)) {\r | |
149 | return Status;\r | |
150 | }\r | |
151 | \r | |
152 | Status = FatOpenDirEnt (OFile, DirEnt);\r | |
153 | if (EFI_ERROR (Status)) {\r | |
154 | return Status;\r | |
155 | }\r | |
156 | \r | |
157 | OFile = DirEnt->OFile;\r | |
158 | if (OFile->ODir != NULL) {\r | |
159 | //\r | |
160 | // If we just created a directory, we need to create "." and ".."\r | |
161 | //\r | |
162 | Status = FatCreateDotDirEnts (OFile);\r | |
163 | if (EFI_ERROR (Status)) {\r | |
164 | return Status;\r | |
165 | }\r | |
166 | }\r | |
167 | }\r | |
168 | //\r | |
169 | // If the file's attribute is read only, and the open is for\r | |
170 | // read-write, then the access is denied.\r | |
171 | //\r | |
172 | FileAttributes = OFile->DirEnt->Entry.Attributes;\r | |
173 | if ((FileAttributes & EFI_FILE_READ_ONLY) != 0 && (FileAttributes & FAT_ATTRIBUTE_DIRECTORY) == 0 && WriteMode) {\r | |
174 | return EFI_ACCESS_DENIED;\r | |
175 | }\r | |
176 | //\r | |
177 | // Create an open instance of the OFile\r | |
178 | //\r | |
179 | Status = FatAllocateIFile (OFile, NewIFile);\r | |
180 | if (EFI_ERROR (Status)) {\r | |
181 | return Status;\r | |
182 | }\r | |
183 | \r | |
184 | (*NewIFile)->ReadOnly = (BOOLEAN)!WriteMode;\r | |
185 | \r | |
186 | DEBUG ((EFI_D_INFO, "FSOpen: Open '%S' %r\n", FileName, Status));\r | |
187 | return FatOFileFlush (OFile);\r | |
188 | }\r | |
189 | \r | |
190 | EFI_STATUS\r | |
191 | EFIAPI\r | |
192 | FatOpen (\r | |
dba03ba1 QH |
193 | IN EFI_FILE_PROTOCOL *FHand,\r |
194 | OUT EFI_FILE_PROTOCOL **NewHandle,\r | |
195 | IN CHAR16 *FileName,\r | |
196 | IN UINT64 OpenMode,\r | |
197 | IN UINT64 Attributes\r | |
b9ec9330 QH |
198 | )\r |
199 | /*++\r | |
200 | Routine Description:\r | |
201 | \r | |
202 | Implements Open() of Simple File System Protocol.\r | |
203 | \r | |
204 | Arguments:\r | |
205 | \r | |
206 | FHand - File handle of the file serves as a starting reference point.\r | |
207 | NewHandle - Handle of the file that is newly opened.\r | |
208 | FileName - File name relative to FHand.\r | |
209 | OpenMode - Open mode.\r | |
210 | Attributes - Attributes to set if the file is created.\r | |
211 | \r | |
212 | Returns:\r | |
213 | \r | |
214 | EFI_INVALID_PARAMETER - The FileName is NULL or the file string is empty.\r | |
215 | The OpenMode is not supported.\r | |
216 | The Attributes is not the valid attributes.\r | |
217 | EFI_OUT_OF_RESOURCES - Can not allocate the memory for file string.\r | |
218 | EFI_SUCCESS - Open the file successfully.\r | |
219 | Others - The status of open file.\r | |
220 | \r | |
221 | --*/\r | |
222 | {\r | |
223 | FAT_IFILE *IFile;\r | |
224 | FAT_IFILE *NewIFile;\r | |
225 | FAT_OFILE *OFile;\r | |
226 | EFI_STATUS Status;\r | |
227 | \r | |
228 | //\r | |
229 | // Perform some parameter checking\r | |
230 | //\r | |
231 | if (FileName == NULL) {\r | |
232 | return EFI_INVALID_PARAMETER;\r | |
233 | }\r | |
234 | //\r | |
235 | // Check for a valid mode\r | |
236 | //\r | |
237 | switch (OpenMode) {\r | |
238 | case EFI_FILE_MODE_READ:\r | |
239 | case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE:\r | |
240 | case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE:\r | |
241 | break;\r | |
242 | \r | |
243 | default:\r | |
244 | return EFI_INVALID_PARAMETER;\r | |
245 | }\r | |
5ff2f40e | 246 | \r |
b9ec9330 | 247 | //\r |
5ff2f40e | 248 | // Check for valid Attributes for file creation case. \r |
b9ec9330 | 249 | //\r |
5ff2f40e QH |
250 | if (((OpenMode & EFI_FILE_MODE_CREATE) != 0) && (Attributes & (EFI_FILE_READ_ONLY | (~EFI_FILE_VALID_ATTR))) != 0) {\r |
251 | return EFI_INVALID_PARAMETER;\r | |
b9ec9330 | 252 | }\r |
5ff2f40e | 253 | \r |
b9ec9330 QH |
254 | IFile = IFILE_FROM_FHAND (FHand);\r |
255 | OFile = IFile->OFile;\r | |
256 | \r | |
257 | //\r | |
258 | // Lock\r | |
259 | //\r | |
260 | FatAcquireLock ();\r | |
261 | \r | |
262 | //\r | |
263 | // Open the file\r | |
264 | //\r | |
265 | Status = FatOFileOpen (OFile, &NewIFile, FileName, OpenMode, (UINT8) Attributes);\r | |
266 | \r | |
267 | //\r | |
268 | // If the file was opened, return the handle to the caller\r | |
269 | //\r | |
270 | if (!EFI_ERROR (Status)) {\r | |
271 | *NewHandle = &NewIFile->Handle;\r | |
272 | }\r | |
273 | //\r | |
274 | // Unlock\r | |
275 | //\r | |
276 | Status = FatCleanupVolume (OFile->Volume, NULL, Status);\r | |
277 | FatReleaseLock ();\r | |
278 | \r | |
279 | return Status;\r | |
280 | }\r |