forked from acidanthera/OpenCorePkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileProtocol.c
656 lines (553 loc) · 16 KB
/
FileProtocol.c
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/** @file
Copyright (C) 2019-2021, vit9696, Goldfish64, mikebeaton. All rights reserved.<BR>
SPDX-License-Identifier: BSD-3-Clause
**/
#include <Uefi.h>
#include <Guid/FileInfo.h>
#include <Protocol/OcBootstrap.h>
#include <Protocol/SimpleFileSystem.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/OcFileLib.h>
#include <Library/UefiBootServicesTableLib.h>
// Define EPOCH (1970-JANUARY-01) in the Julian Date representation
#define EPOCH_JULIAN_DATE 2440588
// Seconds per unit
#define SEC_PER_MIN ((UINTN) 60)
#define SEC_PER_HOUR ((UINTN) 3600)
#define SEC_PER_DAY ((UINTN) 86400)
#define SEC_PER_MONTH ((UINTN) 2,592,000)
#define SEC_PER_YEAR ((UINTN) 31,536,000)
STATIC
UINT32
EfiGetEpochDays (
IN EFI_TIME *Time
)
{
UINT8 a;
UINT32 y;
UINT8 m;
UINT32 JulianDate; // Absolute Julian Date representation of the supplied Time
UINT32 EpochDays; // Number of days elapsed since EPOCH_JULIAN_DAY
a = (14 - Time->Month) / 12;
y = Time->Year + 4800 - a;
m = Time->Month + (12*a) - 3;
JulianDate = Time->Day + ((153*m + 2)/5) + (365*y) + (y/4) - (y/100) + (y/400) - 32045;
ASSERT (JulianDate >= EPOCH_JULIAN_DATE);
EpochDays = JulianDate - EPOCH_JULIAN_DATE;
return EpochDays;
}
STATIC
UINT32
EfiTimeToEpoch (
IN EFI_TIME *Time
)
{
UINT32 EpochDays; // Number of days elapsed since EPOCH_JULIAN_DAY
UINT32 EpochSeconds;
EpochDays = EfiGetEpochDays (Time);
EpochSeconds = (EpochDays * SEC_PER_DAY) + (Time->Hour * SEC_PER_HOUR) + (Time->Minute * SEC_PER_MIN) + Time->Second;
return EpochSeconds;
}
EFI_STATUS
OcGetFileData (
IN EFI_FILE_PROTOCOL *File,
IN UINT32 Position,
IN UINT32 Size,
OUT UINT8 *Buffer
)
{
EFI_STATUS Status;
UINTN ReadSize;
UINTN RequestedSize;
while (Size > 0) {
Status = File->SetPosition (File, Position);
if (EFI_ERROR (Status)) {
return Status;
}
//
// We are required to read in 1 MB portions, because otherwise some
// systems namely MacBook7,1 will not read file data from APFS volumes
// but will pretend they did. Repeoduced with BootKernelExtensions.kc.
//
ReadSize = RequestedSize = MIN (Size, BASE_1MB);
Status = File->Read (File, &ReadSize, Buffer);
if (EFI_ERROR (Status)) {
File->SetPosition (File, 0);
return Status;
}
if (ReadSize != RequestedSize) {
File->SetPosition (File, 0);
return EFI_BAD_BUFFER_SIZE;
}
Position += (UINT32)ReadSize;
Buffer += ReadSize;
Size -= (UINT32)ReadSize;
}
File->SetPosition (File, 0);
return EFI_SUCCESS;
}
EFI_STATUS
OcGetFileSize (
IN EFI_FILE_PROTOCOL *File,
OUT UINT32 *Size
)
{
EFI_STATUS Status;
UINT64 Position;
EFI_FILE_INFO *FileInfo;
Status = File->SetPosition (File, 0xFFFFFFFFFFFFFFFFULL);
if (EFI_ERROR (Status)) {
//
// Some drivers, like EfiFs, return EFI_UNSUPPORTED when trying to seek
// past the file size. Use slow method via attributes for them.
//
FileInfo = OcGetFileInfo (File, &gEfiFileInfoGuid, sizeof (*FileInfo), NULL);
if (FileInfo != NULL) {
if ((UINT32)FileInfo->FileSize == FileInfo->FileSize) {
*Size = (UINT32)FileInfo->FileSize;
Status = EFI_SUCCESS;
}
FreePool (FileInfo);
}
return Status;
}
Status = File->GetPosition (File, &Position);
File->SetPosition (File, 0);
if (EFI_ERROR (Status)) {
return Status;
}
if ((UINT32)Position != Position) {
return EFI_OUT_OF_RESOURCES;
}
*Size = (UINT32)Position;
return EFI_SUCCESS;
}
EFI_STATUS
OcGetFileModificationTime (
IN EFI_FILE_PROTOCOL *File,
OUT EFI_TIME *Time
)
{
EFI_FILE_INFO *FileInfo;
FileInfo = OcGetFileInfo (File, &gEfiFileInfoGuid, 0, NULL);
if (FileInfo == NULL) {
return EFI_INVALID_PARAMETER;
}
CopyMem (Time, &FileInfo->ModificationTime, sizeof (*Time));
FreePool (FileInfo);
return EFI_SUCCESS;
}
BOOLEAN
OcIsWritableFileSystem (
IN EFI_FILE_PROTOCOL *Fs
)
{
EFI_STATUS Status;
EFI_FILE_PROTOCOL *File;
//
// We cannot test if the file system is writeable without attempting to create some file.
//
Status = OcSafeFileOpen (
Fs,
&File,
L"octest.fil",
EFI_FILE_MODE_CREATE | EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE,
0
);
if (EFI_ERROR (Status)) {
return FALSE;
}
//
// Delete the temporary file and report the found file system.
//
Fs->Delete (File);
return TRUE;
}
EFI_STATUS
OcFindWritableFileSystem (
IN OUT EFI_FILE_PROTOCOL **WritableFs
)
{
EFI_HANDLE *HandleBuffer;
UINTN HandleCount;
UINTN Index;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFs;
EFI_FILE_PROTOCOL *Fs;
//
// Locate all the simple file system devices in the system.
//
EFI_STATUS Status = gBS->LocateHandleBuffer (
ByProtocol,
&gEfiSimpleFileSystemProtocolGuid,
NULL,
&HandleCount,
&HandleBuffer
);
if (EFI_ERROR (Status)) {
return Status;
}
for (Index = 0; Index < HandleCount; ++Index) {
Status = gBS->HandleProtocol (
HandleBuffer[Index],
&gEfiSimpleFileSystemProtocolGuid,
(VOID **)&SimpleFs
);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_VERBOSE,
"OCFS: FindWritableFileSystem gBS->HandleProtocol[%u] returned %r\n",
(UINT32)Index,
Status
));
continue;
}
Status = SimpleFs->OpenVolume (SimpleFs, &Fs);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_VERBOSE,
"OCFS: FindWritableFileSystem SimpleFs->OpenVolume[%u] returned %r\n",
(UINT32)Index,
Status
));
continue;
}
if (OcIsWritableFileSystem (Fs)) {
FreePool (HandleBuffer);
*WritableFs = Fs;
return EFI_SUCCESS;
}
DEBUG ((
DEBUG_VERBOSE,
"OCFS: FindWritableFileSystem Fs->Open[%u] failed\n",
(UINT32)Index
));
}
FreePool (HandleBuffer);
return EFI_NOT_FOUND;
}
EFI_STATUS
OcFindWritableOcFileSystem (
OUT EFI_FILE_PROTOCOL **FileSystem
)
{
EFI_STATUS Status;
OC_BOOTSTRAP_PROTOCOL *Bootstrap;
EFI_HANDLE PreferedHandle;
PreferedHandle = NULL;
Status = gBS->LocateProtocol (
&gOcBootstrapProtocolGuid,
NULL,
(VOID **)&Bootstrap
);
if (!EFI_ERROR (Status) && (Bootstrap->Revision == OC_BOOTSTRAP_PROTOCOL_REVISION)) {
PreferedHandle = Bootstrap->GetLoadHandle (Bootstrap);
}
if (PreferedHandle != NULL) {
*FileSystem = OcLocateRootVolume (PreferedHandle, NULL);
} else {
*FileSystem = NULL;
}
DEBUG ((DEBUG_INFO, "OCFS: Preferred handle is %p found fs %p\n", PreferedHandle, *FileSystem));
if (*FileSystem == NULL) {
return OcFindWritableFileSystem (FileSystem);
}
return EFI_SUCCESS;
}
EFI_STATUS
OcSetFileData (
IN EFI_FILE_PROTOCOL *WritableFs OPTIONAL,
IN CONST CHAR16 *FileName,
IN CONST VOID *Buffer,
IN UINT32 Size
)
{
EFI_STATUS Status;
EFI_FILE_PROTOCOL *Fs;
EFI_FILE_PROTOCOL *File;
UINTN WrittenSize;
ASSERT (FileName != NULL);
ASSERT (Buffer != NULL);
if (WritableFs == NULL) {
Status = OcFindWritableFileSystem (&Fs);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_VERBOSE, "OCFS: WriteFileData can't find writable FS\n"));
return Status;
}
} else {
Fs = WritableFs;
}
Status = OcSafeFileOpen (
Fs,
&File,
(CHAR16 *)FileName,
EFI_FILE_MODE_CREATE | EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE,
0
);
if (!EFI_ERROR (Status)) {
WrittenSize = Size;
Status = File->Write (File, &WrittenSize, (VOID *)Buffer);
Fs->Close (File);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_VERBOSE, "OCFS: WriteFileData file->Write returned %r\n", Status));
} else if (WrittenSize != Size) {
DEBUG ((
DEBUG_VERBOSE,
"WriteFileData: File->Write truncated %u to %u\n",
Size,
(UINT32)WrittenSize
));
Status = EFI_BAD_BUFFER_SIZE;
}
} else {
DEBUG ((DEBUG_VERBOSE, "OCFS: WriteFileData Fs->Open of %s returned %r\n", FileName, Status));
}
if (WritableFs == NULL) {
Fs->Close (Fs);
}
return Status;
}
EFI_STATUS
OcAllocateCopyFileData (
IN EFI_FILE_PROTOCOL *File,
OUT UINT8 **Buffer,
OUT UINT32 *BufferSize
)
{
EFI_STATUS Status;
UINT8 *FileBuffer;
UINT32 ReadSize;
//
// Get full file data.
//
Status = OcGetFileSize (File, &ReadSize);
if (EFI_ERROR (Status)) {
return Status;
}
FileBuffer = AllocatePool (ReadSize);
if (FileBuffer == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Status = OcGetFileData (File, 0, ReadSize, FileBuffer);
if (EFI_ERROR (Status)) {
FreePool (FileBuffer);
return Status;
}
*Buffer = FileBuffer;
*BufferSize = ReadSize;
return EFI_SUCCESS;
}
VOID
OcDirectorySeachContextInit (
IN OUT DIRECTORY_SEARCH_CONTEXT *Context
)
{
ASSERT (Context != NULL);
ZeroMem (Context, sizeof (*Context));
}
EFI_STATUS
OcEnsureDirectoryFile (
IN EFI_FILE_PROTOCOL *File,
IN BOOLEAN IsDirectory
)
{
EFI_FILE_INFO *FileInfo;
//
// Ensure this is a directory/file.
//
FileInfo = OcGetFileInfo (File, &gEfiFileInfoGuid, 0, NULL);
if (FileInfo == NULL) {
return EFI_INVALID_PARAMETER;
}
if (((FileInfo->Attribute & EFI_FILE_DIRECTORY) != 0) != IsDirectory) {
FreePool (FileInfo);
return EFI_INVALID_PARAMETER;
}
FreePool (FileInfo);
return EFI_SUCCESS;
}
EFI_STATUS
OcGetNewestFileFromDirectory (
IN OUT DIRECTORY_SEARCH_CONTEXT *Context,
IN EFI_FILE_PROTOCOL *Directory,
IN CHAR16 *FileNameStartsWith OPTIONAL,
OUT EFI_FILE_INFO **FileInfo
)
{
EFI_STATUS Status;
EFI_FILE_INFO *FileInfoCurrent;
EFI_FILE_INFO *FileInfoLatest;
UINTN FileInfoSize;
UINT32 EpochCurrent;
UINTN Index;
UINTN LatestIndex;
UINT32 LatestEpoch;
ASSERT (Context != NULL);
ASSERT (Directory != NULL);
ASSERT (FileInfo != NULL);
LatestIndex = 0;
LatestEpoch = 0;
Status = OcEnsureDirectoryFile (Directory, TRUE);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Allocate two FILE_INFO structures.
// The first is used for all entries, and the second is used only for the
// latest, and eventually returned to the caller.
//
FileInfoCurrent = AllocatePool (SIZE_1KB);
if (FileInfoCurrent == NULL) {
return EFI_OUT_OF_RESOURCES;
}
FileInfoLatest = AllocateZeroPool (SIZE_1KB);
if (FileInfoLatest == NULL) {
FreePool (FileInfoCurrent);
return EFI_OUT_OF_RESOURCES;
}
Directory->SetPosition (Directory, 0);
Index = 0;
do {
//
// Apple's HFS+ driver does not adhere to the spec and will return zero for
// EFI_BUFFER_TOO_SMALL. EFI_FILE_INFO structures larger than 1KB are
// unrealistic as the filename is the only variable.
//
FileInfoSize = SIZE_1KB - sizeof (CHAR16);
Status = Directory->Read (Directory, &FileInfoSize, FileInfoCurrent);
if (EFI_ERROR (Status)) {
Directory->SetPosition (Directory, 0);
FreePool (FileInfoCurrent);
FreePool (FileInfoLatest);
return Status;
}
if (FileInfoSize > 0) {
//
// Skip any files that do not start with the desired filename.
//
if (FileNameStartsWith != NULL) {
if (StrnCmp (FileInfoCurrent->FileName, FileNameStartsWith, StrLen (FileNameStartsWith)) != 0) {
continue;
}
}
//
// Get time of current entry.
// We want to skip any entries that have been returned as "latest" in prior calls.
//
EpochCurrent = EfiTimeToEpoch (&FileInfoCurrent->ModificationTime);
DEBUG ((DEBUG_VERBOSE, "OCFS: Current file %s with time %u\n", FileInfoCurrent->FileName, EpochCurrent));
//
// Skip any entries that are newer than our previously matched entry.
//
if ((Context->PreviousTime > 0) && (EpochCurrent > Context->PreviousTime)) {
DEBUG ((DEBUG_VERBOSE, "OCFS: Skipping file %s due to time %u > %u\n", FileInfoCurrent->FileName, EpochCurrent, Context->PreviousTime));
continue;
}
//
// Skip any entries that have the same time as our previously
// matched entry and were found previously.
//
// ASSUMPTION: Entries are in the same order each time the directory is iterated.
//
if (EpochCurrent == Context->PreviousTime) {
if (Index <= Context->PreviousIndex) {
DEBUG ((DEBUG_VERBOSE, "OCFS: Skipping file %s with due to index %u <= %u\n", FileInfoCurrent->FileName, Index, Context->PreviousIndex));
Index++;
continue;
}
} else {
//
// Reset index counter if the time is different from the last.
//
Index = 0;
}
//
// Store latest entry.
//
if ((FileInfoLatest->FileName[0] == '\0') || (EpochCurrent > EfiTimeToEpoch (&FileInfoLatest->ModificationTime))) {
CopyMem (FileInfoLatest, FileInfoCurrent, FileInfoSize);
LatestIndex = Index;
LatestEpoch = EfiTimeToEpoch (&FileInfoLatest->ModificationTime);
DEBUG ((DEBUG_VERBOSE, "OCFS: Stored newest file %s\n", FileInfoCurrent->FileName));
}
}
} while (FileInfoSize > 0);
Directory->SetPosition (Directory, 0);
FreePool (FileInfoCurrent);
if (FileInfoLatest->FileName[0] == '\0') {
FreePool (FileInfoLatest);
DEBUG ((DEBUG_VERBOSE, "OCFS: No matching files found\n"));
return EFI_NOT_FOUND;
}
*FileInfo = FileInfoLatest;
Context->PreviousIndex = LatestIndex;
Context->PreviousTime = LatestEpoch;
return EFI_SUCCESS;
}
//
// TODO: OcGetNewestFileFromDirectory above and ScanExtensions in CachelessContext.c could be redone using this.
// TODO: I am unclear exactly what the Apple 32-bit HFS is being described as doing (see also OcGetFileInfo), so
// have just copied the existing handling.
//
EFI_STATUS
OcScanDirectory (
IN EFI_FILE_HANDLE Directory,
IN OC_PROCESS_DIRECTORY_ENTRY ProcessEntry,
IN OUT VOID *Context OPTIONAL
)
{
EFI_STATUS Status;
EFI_STATUS TempStatus;
EFI_FILE_INFO *FileInfo;
UINTN FileInfoSize;
ASSERT (Directory != NULL);
ASSERT (ProcessEntry != NULL);
Status = OcEnsureDirectoryFile (Directory, TRUE);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Allocate FILE_INFO structure.
//
FileInfo = AllocatePool (SIZE_1KB);
if (FileInfo == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Status = EFI_NOT_FOUND;
Directory->SetPosition (Directory, 0);
do {
//
// Apple's HFS+ driver does not adhere to the spec and will return zero for
// EFI_BUFFER_TOO_SMALL. EFI_FILE_INFO structures larger than 1KB are
// unrealistic as the filename is the only variable.
//
FileInfoSize = SIZE_1KB - sizeof (CHAR16);
TempStatus = Directory->Read (Directory, &FileInfoSize, FileInfo);
if (EFI_ERROR (TempStatus)) {
Status = TempStatus;
break;
}
if (FileInfoSize > 0) {
TempStatus = ProcessEntry (Directory, FileInfo, FileInfoSize, Context);
//
// Act as if no matching file was found.
//
if (TempStatus == EFI_NOT_FOUND) {
continue;
}
if (EFI_ERROR (TempStatus)) {
Status = TempStatus;
break;
}
//
// At least one file found.
//
Status = EFI_SUCCESS;
}
} while (FileInfoSize > 0);
Directory->SetPosition (Directory, 0);
FreePool (FileInfo);
return Status;
}