This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FileIo.c
168 lines (150 loc) · 5.02 KB
/
FileIo.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
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Guid/FileInfo.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Protocol/SimpleFileSystem.h>
VOID
CheckStatus (
IN CHAR16 *Text,
IN EFI_STATUS Status
)
{
if (EFI_ERROR(Status)){
Print(L"Error Status %d of %s", Status, Text);
}
else {
Print(L"Status Success %d\n", Status);
}
}
// typedef callback function pointer
typedef VOID (*AccessFileInfo)(EFI_FILE_INFO* FileInfo);
// callback funtion to print out file info
VOID ListFileInfo(EFI_FILE_INFO* FileInfo)
{
Print(L"Size : %d\nFileSize:%d \nPhysical Size:%d\n", FileInfo->Size, FileInfo->FileSize, FileInfo->PhysicalSize);
Print(L"%s\n", FileInfo->FileName);
}
EFI_STATUS
ListDirectory(EFI_FILE_PROTOCOL* Directory, AccessFileInfo Callbk)
{
UINTN BufferSize;
UINTN ReadSize;
EFI_STATUS Status = 0;
EFI_FILE_INFO* FileInfo;
BufferSize = sizeof(EFI_FILE_INFO) + sizeof(CHAR16) * 512;
Status = gBS->AllocatePool(EfiBootServicesCode, BufferSize, (VOID**)&FileInfo);
while(1){
ReadSize = BufferSize;
Status = Directory -> Read(Directory, &ReadSize, FileInfo);
if(Status == EFI_BUFFER_TOO_SMALL){
BufferSize = ReadSize;
Status = gBS -> FreePool(FileInfo);
if(EFI_ERROR(Status)){
Print(L"File Read Directory Error Free: %d\n", Status);
break;
}
Status = gBS -> AllocatePool( EfiBootServicesCode, BufferSize, (VOID**)&FileInfo);
if(EFI_ERROR(Status)){
Print(L"File Read Directory Error Allocate: %d\n", Status);
break;
}
Status = Directory-> Read(Directory, &ReadSize, FileInfo);
if(EFI_ERROR(Status)){
Print(L"File Read Directory Error Read: %d\n", Status);
break;
}
}
if(ReadSize == 0) break;
if(EFI_ERROR(Status)){
Print(L"File Read Directory Error: %d\n", Status);
break;
}
Callbk(FileInfo);
}
Status = gBS -> FreePool( FileInfo);
return 0;
}
EFI_STATUS
GetFileIo( EFI_FILE_PROTOCOL** Root)
{
EFI_STATUS Status = 0;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFileSystem;
Status = gBS->LocateProtocol(
&gEfiSimpleFileSystemProtocolGuid,
NULL,
(VOID**)&SimpleFileSystem);
if (EFI_ERROR(Status)) {
Print(L"Cannot Locate the Simple File System Protocol\n");
return Status;
}
// Get the Handle of the FS Root Directory
Status = SimpleFileSystem->OpenVolume(SimpleFileSystem, Root);
return Status;
}
EFI_STATUS
DumpData (
IN VOID *TextBuffer,
CONST CHAR16 *Filename,
IN OUT UINTN *BufSize
)
{
//UINTN BufferSize;
EFI_STATUS Status;
EFI_FILE_PROTOCOL *Root; // File Handle of the FS Root Directory
Status = GetFileIo(&Root);
if (EFI_ERROR(Status)) {
Print(L"Failed to Acquire the Root Handle: %d\n", Status);
return Status;
}
// Open or Create a new file
EFI_FILE_PROTOCOL *FileHandle = 0;
//CHAR16 *Buf = (CHAR16*)L"This is the Content\n";
Status = Root->Open( Root,
&FileHandle,
(CHAR16*)Filename,
EFI_FILE_MODE_CREATE | EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE,
0);
// Check and write into the file
CheckStatus(L" EFI_FILE_PROTOCOL Create file \n", Status);
if (FileHandle && !(EFI_ERROR(Status))) {
//BufferSize = StrLen(TextBuffer) * 2;
Status = FileHandle->Write(FileHandle, BufSize, TextBuffer);
CheckStatus(L" EFI_FILE_PROTOCOL Write file \n ", Status);
Status = FileHandle->Close(FileHandle);
return Status;
}
return EFI_ABORTED;
}
//
// Read Local file Content and Dump to Memeory
//
EFI_STATUS ReadFileToMem (
IN OUT CHAR16* Buffer,
IN UINTN* BufferSize,
IN CHAR16* Filename
)
{
EFI_STATUS Status = EFI_SUCCESS;
EFI_FILE_PROTOCOL *Root;
EFI_FILE_PROTOCOL *SystemFile = 0;
Status = GetFileIo(&Root);
Status = Root->Open ( Root,
&SystemFile,
(CHAR16*)Filename,
EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE,
0);
Status = SystemFile->Read ( SystemFile,
BufferSize,
Buffer);
if(!EFI_ERROR(Status)){
Print(L"\nFinished Reading Content of %s\n", Filename);
UINTN Index = (*BufferSize);
Buffer[Index] = 0;
Print(L"%d, %s\n", Index, Buffer);
DumpData(Buffer, (CHAR16*)L"data.log", BufferSize);
}
// Test List Directory
// ListDirectory(Root, ListFileInfo);
Status = SystemFile->Close(SystemFile);
return Status;
}