1 | /** @file
|
---|
2 | Routines dealing with file open.
|
---|
3 |
|
---|
4 | Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
|
---|
5 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
6 |
|
---|
7 | **/
|
---|
8 |
|
---|
9 | #include "Fat.h"
|
---|
10 |
|
---|
11 | /**
|
---|
12 |
|
---|
13 | Create an Open instance for the existing OFile.
|
---|
14 | The IFile of the newly opened file is passed out.
|
---|
15 |
|
---|
16 | @param OFile - The file that serves as a starting reference point.
|
---|
17 | @param PtrIFile - The newly generated IFile instance.
|
---|
18 |
|
---|
19 | @retval EFI_OUT_OF_RESOURCES - Can not allocate the memory for the IFile
|
---|
20 | @retval EFI_SUCCESS - Create the new IFile for the OFile successfully
|
---|
21 |
|
---|
22 | **/
|
---|
23 | EFI_STATUS
|
---|
24 | FatAllocateIFile (
|
---|
25 | IN FAT_OFILE *OFile,
|
---|
26 | OUT FAT_IFILE **PtrIFile
|
---|
27 | )
|
---|
28 | {
|
---|
29 | FAT_IFILE *IFile;
|
---|
30 |
|
---|
31 | ASSERT_VOLUME_LOCKED (OFile->Volume);
|
---|
32 |
|
---|
33 | //
|
---|
34 | // Allocate a new open instance
|
---|
35 | //
|
---|
36 | IFile = AllocateZeroPool (sizeof (FAT_IFILE));
|
---|
37 | if (IFile == NULL) {
|
---|
38 | return EFI_OUT_OF_RESOURCES;
|
---|
39 | }
|
---|
40 |
|
---|
41 | IFile->Signature = FAT_IFILE_SIGNATURE;
|
---|
42 |
|
---|
43 | CopyMem (&(IFile->Handle), &FatFileInterface, sizeof (EFI_FILE_PROTOCOL));
|
---|
44 |
|
---|
45 | //
|
---|
46 | // Report the correct revision number based on the DiskIo2 availability
|
---|
47 | //
|
---|
48 | if (OFile->Volume->DiskIo2 != NULL) {
|
---|
49 | IFile->Handle.Revision = EFI_FILE_PROTOCOL_REVISION2;
|
---|
50 | } else {
|
---|
51 | IFile->Handle.Revision = EFI_FILE_PROTOCOL_REVISION;
|
---|
52 | }
|
---|
53 |
|
---|
54 | IFile->OFile = OFile;
|
---|
55 | InsertTailList (&OFile->Opens, &IFile->Link);
|
---|
56 | InitializeListHead (&IFile->Tasks);
|
---|
57 |
|
---|
58 | *PtrIFile = IFile;
|
---|
59 | return EFI_SUCCESS;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 |
|
---|
64 | Open a file for a file name relative to an existing OFile.
|
---|
65 | The IFile of the newly opened file is passed out.
|
---|
66 |
|
---|
67 | @param OFile - The file that serves as a starting reference point.
|
---|
68 | @param NewIFile - The newly generated IFile instance.
|
---|
69 | @param FileName - The file name relative to the OFile.
|
---|
70 | @param OpenMode - Open mode.
|
---|
71 | @param Attributes - Attributes to set if the file is created.
|
---|
72 |
|
---|
73 |
|
---|
74 | @retval EFI_SUCCESS - Open the file successfully.
|
---|
75 | @retval EFI_INVALID_PARAMETER - The open mode is conflict with the attributes
|
---|
76 | or the file name is not valid.
|
---|
77 | @retval EFI_NOT_FOUND - Conflicts between dir intention and attribute.
|
---|
78 | @retval EFI_WRITE_PROTECTED - Can't open for write if the volume is read only.
|
---|
79 | @retval EFI_ACCESS_DENIED - If the file's attribute is read only, and the
|
---|
80 | open is for read-write fail it.
|
---|
81 | @retval EFI_OUT_OF_RESOURCES - Can not allocate the memory.
|
---|
82 |
|
---|
83 | **/
|
---|
84 | EFI_STATUS
|
---|
85 | FatOFileOpen (
|
---|
86 | IN FAT_OFILE *OFile,
|
---|
87 | OUT FAT_IFILE **NewIFile,
|
---|
88 | IN CHAR16 *FileName,
|
---|
89 | IN UINT64 OpenMode,
|
---|
90 | IN UINT8 Attributes
|
---|
91 | )
|
---|
92 | {
|
---|
93 | FAT_VOLUME *Volume;
|
---|
94 | EFI_STATUS Status;
|
---|
95 | CHAR16 NewFileName[EFI_PATH_STRING_LENGTH];
|
---|
96 | FAT_DIRENT *DirEnt;
|
---|
97 | UINT8 FileAttributes;
|
---|
98 | BOOLEAN WriteMode;
|
---|
99 |
|
---|
100 | DirEnt = NULL;
|
---|
101 | Volume = OFile->Volume;
|
---|
102 | ASSERT_VOLUME_LOCKED (Volume);
|
---|
103 | WriteMode = (BOOLEAN) (OpenMode & EFI_FILE_MODE_WRITE);
|
---|
104 | if (Volume->ReadOnly && WriteMode) {
|
---|
105 | return EFI_WRITE_PROTECTED;
|
---|
106 | }
|
---|
107 | //
|
---|
108 | // Verify the source file handle isn't in an error state
|
---|
109 | //
|
---|
110 | Status = OFile->Error;
|
---|
111 | if (EFI_ERROR (Status)) {
|
---|
112 | return Status;
|
---|
113 | }
|
---|
114 | //
|
---|
115 | // Get new OFile for the file
|
---|
116 | //
|
---|
117 | Status = FatLocateOFile (&OFile, FileName, Attributes, NewFileName);
|
---|
118 | if (EFI_ERROR (Status)) {
|
---|
119 | return Status;
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (*NewFileName != 0) {
|
---|
123 | //
|
---|
124 | // If there's a remaining part of the name, then we had
|
---|
125 | // better be creating the file in the directory
|
---|
126 | //
|
---|
127 | if ((OpenMode & EFI_FILE_MODE_CREATE) == 0) {
|
---|
128 | return EFI_NOT_FOUND;
|
---|
129 | }
|
---|
130 |
|
---|
131 | Status = FatCreateDirEnt (OFile, NewFileName, Attributes, &DirEnt);
|
---|
132 | if (EFI_ERROR (Status)) {
|
---|
133 | return Status;
|
---|
134 | }
|
---|
135 |
|
---|
136 | ASSERT (DirEnt != NULL);
|
---|
137 | Status = FatOpenDirEnt (OFile, DirEnt);
|
---|
138 | if (EFI_ERROR (Status)) {
|
---|
139 | return Status;
|
---|
140 | }
|
---|
141 |
|
---|
142 | OFile = DirEnt->OFile;
|
---|
143 | if (OFile->ODir != NULL) {
|
---|
144 | //
|
---|
145 | // If we just created a directory, we need to create "." and ".."
|
---|
146 | //
|
---|
147 | Status = FatCreateDotDirEnts (OFile);
|
---|
148 | if (EFI_ERROR (Status)) {
|
---|
149 | return Status;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | //
|
---|
154 | // If the file's attribute is read only, and the open is for
|
---|
155 | // read-write, then the access is denied.
|
---|
156 | //
|
---|
157 | FileAttributes = OFile->DirEnt->Entry.Attributes;
|
---|
158 | if ((FileAttributes & EFI_FILE_READ_ONLY) != 0 && (FileAttributes & FAT_ATTRIBUTE_DIRECTORY) == 0 && WriteMode) {
|
---|
159 | return EFI_ACCESS_DENIED;
|
---|
160 | }
|
---|
161 | //
|
---|
162 | // Create an open instance of the OFile
|
---|
163 | //
|
---|
164 | Status = FatAllocateIFile (OFile, NewIFile);
|
---|
165 | if (EFI_ERROR (Status)) {
|
---|
166 | return Status;
|
---|
167 | }
|
---|
168 |
|
---|
169 | (*NewIFile)->ReadOnly = (BOOLEAN)!WriteMode;
|
---|
170 |
|
---|
171 | DEBUG ((EFI_D_INFO, "FSOpen: Open '%S' %r\n", FileName, Status));
|
---|
172 | return FatOFileFlush (OFile);
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 |
|
---|
177 | Implements OpenEx() of Simple File System Protocol.
|
---|
178 |
|
---|
179 | @param FHand - File handle of the file serves as a starting reference point.
|
---|
180 | @param NewHandle - Handle of the file that is newly opened.
|
---|
181 | @param FileName - File name relative to FHand.
|
---|
182 | @param OpenMode - Open mode.
|
---|
183 | @param Attributes - Attributes to set if the file is created.
|
---|
184 | @param Token - A pointer to the token associated with the transaction.:
|
---|
185 |
|
---|
186 | @retval EFI_INVALID_PARAMETER - The FileName is NULL or the file string is empty.
|
---|
187 | The OpenMode is not supported.
|
---|
188 | The Attributes is not the valid attributes.
|
---|
189 | @retval EFI_OUT_OF_RESOURCES - Can not allocate the memory for file string.
|
---|
190 | @retval EFI_SUCCESS - Open the file successfully.
|
---|
191 | @return Others - The status of open file.
|
---|
192 |
|
---|
193 | **/
|
---|
194 | EFI_STATUS
|
---|
195 | EFIAPI
|
---|
196 | FatOpenEx (
|
---|
197 | IN EFI_FILE_PROTOCOL *FHand,
|
---|
198 | OUT EFI_FILE_PROTOCOL **NewHandle,
|
---|
199 | IN CHAR16 *FileName,
|
---|
200 | IN UINT64 OpenMode,
|
---|
201 | IN UINT64 Attributes,
|
---|
202 | IN OUT EFI_FILE_IO_TOKEN *Token
|
---|
203 | )
|
---|
204 | {
|
---|
205 | FAT_IFILE *IFile;
|
---|
206 | FAT_IFILE *NewIFile;
|
---|
207 | FAT_OFILE *OFile;
|
---|
208 | EFI_STATUS Status;
|
---|
209 | FAT_TASK *Task;
|
---|
210 |
|
---|
211 | //
|
---|
212 | // Perform some parameter checking
|
---|
213 | //
|
---|
214 | if (FileName == NULL) {
|
---|
215 | return EFI_INVALID_PARAMETER;
|
---|
216 | }
|
---|
217 | //
|
---|
218 | // Check for a valid mode
|
---|
219 | //
|
---|
220 | switch (OpenMode) {
|
---|
221 | case EFI_FILE_MODE_READ:
|
---|
222 | case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE:
|
---|
223 | case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE:
|
---|
224 | break;
|
---|
225 |
|
---|
226 | default:
|
---|
227 | return EFI_INVALID_PARAMETER;
|
---|
228 | }
|
---|
229 |
|
---|
230 | //
|
---|
231 | // Check for valid Attributes for file creation case.
|
---|
232 | //
|
---|
233 | if (((OpenMode & EFI_FILE_MODE_CREATE) != 0) && (Attributes & (EFI_FILE_READ_ONLY | (~EFI_FILE_VALID_ATTR))) != 0) {
|
---|
234 | return EFI_INVALID_PARAMETER;
|
---|
235 | }
|
---|
236 |
|
---|
237 | IFile = IFILE_FROM_FHAND (FHand);
|
---|
238 | OFile = IFile->OFile;
|
---|
239 | Task = NULL;
|
---|
240 |
|
---|
241 | if (Token == NULL) {
|
---|
242 | FatWaitNonblockingTask (IFile);
|
---|
243 | } else {
|
---|
244 | //
|
---|
245 | // Caller shouldn't call the non-blocking interfaces if the low layer doesn't support DiskIo2.
|
---|
246 | // But if it calls, the below check can avoid crash.
|
---|
247 | //
|
---|
248 | if (FHand->Revision < EFI_FILE_PROTOCOL_REVISION2) {
|
---|
249 | return EFI_UNSUPPORTED;
|
---|
250 | }
|
---|
251 | Task = FatCreateTask (IFile, Token);
|
---|
252 | if (Task == NULL) {
|
---|
253 | return EFI_OUT_OF_RESOURCES;
|
---|
254 | }
|
---|
255 | }
|
---|
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, Task);
|
---|
277 | FatReleaseLock ();
|
---|
278 |
|
---|
279 | if (Token != NULL) {
|
---|
280 | if (!EFI_ERROR (Status)) {
|
---|
281 | Status = FatQueueTask (IFile, Task);
|
---|
282 | } else {
|
---|
283 | FatDestroyTask (Task);
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | return Status;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 |
|
---|
292 | Implements Open() of Simple File System Protocol.
|
---|
293 |
|
---|
294 |
|
---|
295 | @param FHand - File handle of the file serves as a starting reference point.
|
---|
296 | @param NewHandle - Handle of the file that is newly opened.
|
---|
297 | @param FileName - File name relative to FHand.
|
---|
298 | @param OpenMode - Open mode.
|
---|
299 | @param Attributes - Attributes to set if the file is created.
|
---|
300 |
|
---|
301 | @retval EFI_INVALID_PARAMETER - The FileName is NULL or the file string is empty.
|
---|
302 | The OpenMode is not supported.
|
---|
303 | The Attributes is not the valid attributes.
|
---|
304 | @retval EFI_OUT_OF_RESOURCES - Can not allocate the memory for file string.
|
---|
305 | @retval EFI_SUCCESS - Open the file successfully.
|
---|
306 | @return Others - The status of open file.
|
---|
307 |
|
---|
308 | **/
|
---|
309 | EFI_STATUS
|
---|
310 | EFIAPI
|
---|
311 | FatOpen (
|
---|
312 | IN EFI_FILE_PROTOCOL *FHand,
|
---|
313 | OUT EFI_FILE_PROTOCOL **NewHandle,
|
---|
314 | IN CHAR16 *FileName,
|
---|
315 | IN UINT64 OpenMode,
|
---|
316 | IN UINT64 Attributes
|
---|
317 | )
|
---|
318 | {
|
---|
319 | return FatOpenEx (FHand, NewHandle, FileName, OpenMode, Attributes, NULL);
|
---|
320 | }
|
---|