-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtils.c
More file actions
467 lines (403 loc) · 13.9 KB
/
Copy pathFileUtils.c
File metadata and controls
467 lines (403 loc) · 13.9 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include "FileUtils.h"
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/BaseLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Protocol/SimpleFileSystem.h>
#include <Protocol/LoadedImage.h>
#include <Guid/FileInfo.h>
EFI_STATUS
ReadFile (
IN CHAR16 *FileName,
OUT VOID **Buffer,
OUT UINTN *FileSize
)
{
EFI_STATUS Status;
EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem;
EFI_FILE_PROTOCOL *RootDir;
EFI_FILE_PROTOCOL *File;
// Get the loaded image protocol to find our device
Status = gBS->HandleProtocol (
gImageHandle,
&gEfiLoadedImageProtocolGuid,
(VOID **)&LoadedImage
);
if (EFI_ERROR (Status)) return Status;
// Open the file system on our boot device
Status = gBS->HandleProtocol (
LoadedImage->DeviceHandle,
&gEfiSimpleFileSystemProtocolGuid,
(VOID **)&FileSystem
);
if (EFI_ERROR (Status)) return Status;
// Open the volume (root directory)
Status = FileSystem->OpenVolume (FileSystem, &RootDir);
if (EFI_ERROR (Status)) return Status;
// Open the file
Status = RootDir->Open (RootDir, &File, FileName,
EFI_FILE_MODE_READ, 0);
if (EFI_ERROR (Status)) return Status;
// Get file size
UINTN InfoSize = 0;
EFI_FILE_INFO *FileInfo = NULL;
Status = File->GetInfo (File, &gEfiFileInfoGuid,
&InfoSize, FileInfo);
if (Status == EFI_BUFFER_TOO_SMALL) {
FileInfo = AllocatePool (InfoSize);
Status = File->GetInfo (File, &gEfiFileInfoGuid,
&InfoSize, FileInfo);
}
if (EFI_ERROR (Status)) {
File->Close (File);
return Status;
}
*FileSize = (UINTN) FileInfo->FileSize;
FreePool (FileInfo);
// Read the whole file
*Buffer = AllocatePool (*FileSize);
if (*Buffer == NULL) {
File->Close (File);
return EFI_OUT_OF_RESOURCES;
}
UINTN ReadSize = *FileSize;
Status = File->Read (File, &ReadSize, *Buffer);
File->Close (File);
if (EFI_ERROR (Status)) {
FreePool (*Buffer);
*Buffer = NULL;
}
return Status;
}
/**
Enumerate the entries in a directory, collecting matching names into an array.
Shared helper for ListDirectories() and ListFiles().
@param[in] DirName Full path of the directory to enumerate (or L"\\" for root).
@param[in] WantDirs TRUE to collect subdirectories, FALSE to collect files.
@param[out] Names Array of names (NULL-terminated CHAR16* each). Caller frees
each entry and the array with FreePool().
@param[out] Count Number of names collected.
@retval EFI_SUCCESS Names populated.
@retval other Failure from opening/reading the directory.
**/
EFI_STATUS
ListDirEntries (
IN CHAR16 *DirName,
IN BOOLEAN WantDirs,
OUT CHAR16 ***Names,
OUT UINTN *Count
)
{
EFI_STATUS Status;
EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem;
EFI_FILE_PROTOCOL *RootDir;
EFI_FILE_PROTOCOL *Dir;
EFI_FILE_INFO *FileInfo;
UINTN InfoSize;
BOOLEAN NoMore;
*Names = NULL;
*Count = 0;
// Get the loaded image protocol to find our device
Status = gBS->HandleProtocol (
gImageHandle,
&gEfiLoadedImageProtocolGuid,
(VOID **)&LoadedImage
);
if (EFI_ERROR (Status)) return Status;
// Open the file system on our boot device
Status = gBS->HandleProtocol (
LoadedImage->DeviceHandle,
&gEfiSimpleFileSystemProtocolGuid,
(VOID **)&FileSystem
);
if (EFI_ERROR (Status)) return Status;
// Open the volume (root directory)
Status = FileSystem->OpenVolume (FileSystem, &RootDir);
if (EFI_ERROR (Status)) return Status;
// Open the target directory
Status = RootDir->Open (RootDir, &Dir, DirName, EFI_FILE_MODE_READ, 0);
RootDir->Close (RootDir);
if (EFI_ERROR (Status)) return Status;
// Loop reading entries until end-of-directory
InfoSize = 0;
FileInfo = NULL;
NoMore = FALSE;
while (!NoMore) {
EFI_FILE_INFO *CurInfo = NULL;
UINTN CurSize = 0;
// Read one entry. Start small; on EFI_BUFFER_TOO_SMALL the driver reports the
// required size in InfoSize and does NOT advance the position, so grow and retry.
InfoSize = 0;
do {
if (CurInfo != NULL) {
FreePool (CurInfo);
CurInfo = NULL;
}
if (InfoSize == 0) {
CurSize = SIZE_OF_EFI_FILE_INFO + 256;
} else {
CurSize = InfoSize;
}
CurInfo = AllocatePool (CurSize);
if (CurInfo == NULL) {
Dir->Close (Dir);
return EFI_OUT_OF_RESOURCES;
}
InfoSize = CurSize;
Status = Dir->Read (Dir, &InfoSize, CurInfo);
} while (Status == EFI_BUFFER_TOO_SMALL);
if (EFI_ERROR (Status)) {
FreePool (CurInfo);
Dir->Close (Dir);
return Status;
}
// End of directory reached
if (InfoSize == 0) {
FreePool (CurInfo);
NoMore = TRUE;
break;
}
// Skip "." and ".."
if (StrCmp (CurInfo->FileName, L".") != 0 &&
StrCmp (CurInfo->FileName, L"..") != 0) {
BOOLEAN IsDir = ((CurInfo->Attribute & EFI_FILE_DIRECTORY) != 0);
if (IsDir == WantDirs) {
// Grow the Names array
UINTN Len = StrLen (CurInfo->FileName);
*Names = ReallocatePool (*Count * sizeof (CHAR16 *),
(*Count + 1) * sizeof (CHAR16 *),
*Names);
if (*Names == NULL) {
FreePool (CurInfo);
Dir->Close (Dir);
return EFI_OUT_OF_RESOURCES;
}
(*Names)[*Count] = AllocateCopyPool ((Len + 1) * sizeof (CHAR16),
CurInfo->FileName);
if ((*Names)[*Count] == NULL) {
FreePool (CurInfo);
Dir->Close (Dir);
return EFI_OUT_OF_RESOURCES;
}
(*Count)++;
}
}
FreePool (CurInfo);
}
Dir->Close (Dir);
return EFI_SUCCESS;
}
/**
List the subdirectory names directly under a base directory.
@param[in] DirName Full path of the base directory (e.g. L"\EFI" or L"\").
@param[out] Names Array of subdirectory names. Caller frees each entry
and the array with FreePool().
@param[out] Count Number of subdirectories found.
@retval EFI_SUCCESS Names populated.
@retval other Failure from opening/reading the directory.
**/
EFI_STATUS
ListDirectories (
IN CHAR16 *DirName,
OUT CHAR16 ***Names,
OUT UINTN *Count
)
{
return ListDirEntries (DirName, TRUE, Names, Count);
}
/**
List the file names directly under a base directory (excluding subdirectories).
@param[in] DirName Full path of the base directory (e.g. L"\EFI" or L"\").
@param[out] Names Array of file names. Caller frees each entry
and the array with FreePool().
@param[out] Count Number of files found.
@retval EFI_SUCCESS Names populated.
@retval other Failure from opening/reading the directory.
**/
EFI_STATUS
ListFiles (
IN CHAR16 *DirName,
OUT CHAR16 ***Names,
OUT UINTN *Count
)
{
return ListDirEntries (DirName, FALSE, Names, Count);
}
/**
Create a directory under a base directory.
@param[in] DirName Full path of the directory to create, including the new
directory's name (e.g. L"\EFI\notes" or L"\mydir").
All ancestors must already exist.
@retval EFI_SUCCESS Directory created (or already existed).
@retval other Failure from opening/creating the directory.
**/
EFI_STATUS
CreateDirectory (
IN CHAR16 *DirName
)
{
EFI_STATUS Status;
EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem;
EFI_FILE_PROTOCOL *RootDir;
EFI_FILE_PROTOCOL *Dir;
// Get the loaded image protocol to find our device
Status = gBS->HandleProtocol (
gImageHandle,
&gEfiLoadedImageProtocolGuid,
(VOID **)&LoadedImage
);
if (EFI_ERROR (Status)) return Status;
// Open the file system on our boot device
Status = gBS->HandleProtocol (
LoadedImage->DeviceHandle,
&gEfiSimpleFileSystemProtocolGuid,
(VOID **)&FileSystem
);
if (EFI_ERROR (Status)) return Status;
// Open the volume (root directory)
Status = FileSystem->OpenVolume (FileSystem, &RootDir);
if (EFI_ERROR (Status)) return Status;
// Open (create if missing) the directory
Status = RootDir->Open (RootDir, &Dir, DirName,
EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
EFI_FILE_DIRECTORY);
RootDir->Close (RootDir);
if (EFI_ERROR (Status)) return Status;
Dir->Close (Dir);
return EFI_SUCCESS;
}
/**
Create (or overwrite) a file and write data to it.
@param[in] FileName Full path of the file to write (e.g. L"\notes.txt").
@param[in] Buffer Data to write.
@param[in] BufferSize Number of bytes in Buffer.
@retval EFI_SUCCESS File written.
@retval other Failure from opening/creating the file or writing.
**/
EFI_STATUS
WriteFile (
IN CHAR16 *FileName,
IN VOID *Buffer,
IN UINTN BufferSize
)
{
EFI_STATUS Status;
EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem;
EFI_FILE_PROTOCOL *RootDir;
EFI_FILE_PROTOCOL *File;
UINTN WriteSize;
// Get the loaded image protocol to find our device
Status = gBS->HandleProtocol (
gImageHandle,
&gEfiLoadedImageProtocolGuid,
(VOID **)&LoadedImage
);
if (EFI_ERROR (Status)) return Status;
// Open the file system on our boot device
Status = gBS->HandleProtocol (
LoadedImage->DeviceHandle,
&gEfiSimpleFileSystemProtocolGuid,
(VOID **)&FileSystem
);
if (EFI_ERROR (Status)) return Status;
// Open the volume (root directory)
Status = FileSystem->OpenVolume (FileSystem, &RootDir);
if (EFI_ERROR (Status)) return Status;
// Open (create/truncate) the file for writing
Status = RootDir->Open (RootDir, &File, FileName,
EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
0);
RootDir->Close (RootDir);
if (EFI_ERROR (Status)) return Status;
// Write the data
WriteSize = BufferSize;
Status = File->Write (File, &WriteSize, Buffer);
File->Close (File);
if (EFI_ERROR (Status)) return Status;
if (WriteSize != BufferSize) return EFI_DEVICE_ERROR;
return EFI_SUCCESS;
}
/**
Delete a file at the given path.
@param[in] FilePath Full path of the file to delete (e.g. L"\notes.txt").
@retval EFI_SUCCESS File deleted.
@retval other Failure from opening/deleting the file.
**/
EFI_STATUS
DeleteFile (
IN CHAR16 *FilePath
)
{
EFI_STATUS Status;
EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem;
EFI_FILE_PROTOCOL *RootDir;
EFI_FILE_PROTOCOL *File;
Status = gBS->HandleProtocol (
gImageHandle,
&gEfiLoadedImageProtocolGuid,
(VOID **)&LoadedImage
);
if (EFI_ERROR (Status)) return Status;
Status = gBS->HandleProtocol (
LoadedImage->DeviceHandle,
&gEfiSimpleFileSystemProtocolGuid,
(VOID **)&FileSystem
);
if (EFI_ERROR (Status)) return Status;
Status = FileSystem->OpenVolume (FileSystem, &RootDir);
if (EFI_ERROR (Status)) return Status;
Status = RootDir->Open (RootDir, &File, FilePath,
EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
0);
if (EFI_ERROR (Status)) {
RootDir->Close (RootDir);
return Status;
}
Status = File->Delete (File);
RootDir->Close (RootDir);
return Status;
}
/**
Validate a filename for FAT32 compatibility.
Rules enforced:
- Non-NULL, non-empty
- Length <= 255 characters
- No illegal characters: " * / \ : ? < > |
- No trailing dots or spaces
@param[in] FileName The filename string to validate.
@retval TRUE The filename is valid.
@retval FALSE The filename is invalid.
**/
bool
IsValidFileName (
IN CHAR16 *FileName
)
{
UINTN Len;
UINTN I;
if (FileName == NULL) {
return false;
}
Len = StrLen (FileName);
if (Len == 0 || Len > 255) {
return false;
}
if (FileName[Len - 1] == L'.' || FileName[Len - 1] == L' ') {
return false;
}
for (I = 0; I < Len; I++) {
CHAR16 Ch = FileName[I];
if (Ch == L'"' || Ch == L'*' || Ch == L'/' || Ch == L'\\' ||
Ch == L':' || Ch == L'?' || Ch == L'<' || Ch == L'>' ||
Ch == L'|') {
return false;
}
}
return true;
}