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