VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/Library/CommonLib/EfiFile.c
blob: 8a9d6d7ecddac72609e65b2e15d4b574e98b94c8 (plain)
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
/** @file
EFI file system helpers

Copyright (c) 2016. Disk Cryptography Services for EFI (DCS), Alex Kolotnikov
Copyright (c) 2016. VeraCrypt, Mounir IDRASSI 

This program and the accompanying materials are licensed and made available
under the terms and conditions of the GNU Lesser General Public License, version 3.0 (LGPL-3.0).

The full text of the license may be found at
https://opensource.org/licenses/LGPL-3.0
**/

#include <Uefi.h>
#include <Library/CommonLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Protocol/SimpleFileSystem.h>
#include <Guid/FileInfo.h>
#include <Guid/FileSystemInfo.h>
#include <Library/BaseMemoryLib.h>
#include <Library/PrintLib.h>

//////////////////////////////////////////////////////////////////////////
// EFI file 
//////////////////////////////////////////////////////////////////////////

EFI_FILE*      gFileRoot = NULL;
EFI_HANDLE     gFileRootHandle = NULL;

EFI_HANDLE* gFSHandles = NULL;
UINTN       gFSCount = 0;

EFI_STATUS
InitFS() {
   EFI_STATUS  res;
   EfiGetHandles(ByProtocol, &gEfiSimpleFileSystemProtocolGuid, 0, &gFSHandles, &gFSCount);
   res = EfiGetStartDevice(&gFileRootHandle);
   if (!EFI_ERROR(res)) {
      res = FileOpenRoot(gFileRootHandle, &gFileRoot);
   }
   return res;
}

EFI_STATUS
DirectoryCreate(
   IN    EFI_FILE*   root,
   IN    CHAR16*     name
   )
{
   EFI_FILE*      file;
   EFI_STATUS     res;
   if (!name) { return EFI_INVALID_PARAMETER; }

   res = FileOpen(root, name, &file, EFI_FILE_MODE_READ | EFI_FILE_MODE_CREATE | EFI_FILE_MODE_WRITE, EFI_FILE_DIRECTORY);
   if (EFI_ERROR(res)) return res;
   FileClose(file);
   return res;
}

EFI_STATUS
FileOpenRoot(
   IN    EFI_HANDLE rootHandle,
   OUT   EFI_FILE** rootFile)
{
   EFI_STATUS res = 0;
   EFI_FILE_IO_INTERFACE* fSysProtocol = NULL;
   res = gBS->HandleProtocol(rootHandle, &gEfiSimpleFileSystemProtocolGuid, (VOID**)&fSysProtocol);
   if (res == RETURN_SUCCESS && fSysProtocol != NULL) {
      res = fSysProtocol->OpenVolume(fSysProtocol, rootFile);
   }
   return res;
}

EFI_STATUS 
FileOpen(
   IN    EFI_FILE*   root, 
   IN    CHAR16*     name,
   OUT   EFI_FILE**  file,
   IN    UINT64      mode,
   IN    UINT64      attributes
   )
{
   EFI_STATUS res;

   if (!name || !file) { return EFI_INVALID_PARAMETER; }
   if (!root) root = gFileRoot;
   if (!root) { return EFI_INVALID_PARAMETER; }
   if (mode == 0) {
      mode = EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE;
   }
   res = root->Open(root, file, name, mode , attributes);
   return res;
}

EFI_STATUS 
FileClose(
   IN EFI_FILE* f) 
{
   if (!f) { return EFI_INVALID_PARAMETER; }
   return f->Close(f);
}

EFI_STATUS 
FileDelete(
   IN    EFI_FILE*   root,
   IN    CHAR16*     name
   )
{
   EFI_FILE*      f;
   EFI_STATUS res;
   if (!name) { return EFI_INVALID_PARAMETER; }
   res = FileOpen(root, name, &f, 0, 0);
   if (EFI_ERROR(res)) {
      return res;
   }
   res = f->Delete(f);
//   f->Close(f);
   return res;
}

EFI_STATUS 
FileRead(
   IN       EFI_FILE*   f, 
   OUT      VOID*       data, 
   IN OUT   UINTN*      bytes,
   IN OUT   UINT64*     position)
{
   EFI_STATUS res;

   if (!f || !data || !bytes) { 
      return EFI_INVALID_PARAMETER; 
   }
   if (position != NULL) {
      res = f->SetPosition(f, *position);
      if (EFI_ERROR(res)) { 
         return res; 
      }
   }
   res = f->Read(f, bytes, data);
   if (position != NULL) {
      f->GetPosition(f, position);
   }
   return res;
}

EFI_STATUS 
FileWrite(
   IN       EFI_FILE*   f, 
   IN       VOID*       data,
   IN OUT   UINTN      bytes, 
   IN OUT   UINT64*     position) 
{
   EFI_STATUS res;
   UINTN remaining;
   UINT8* pbData = (UINT8*) data;

   if (!f || !data) { 
      return EFI_INVALID_PARAMETER; 
   }
   if (position != NULL) {
      res = f->SetPosition(f, *position);
      if (EFI_ERROR(res)) {
         return res;
      }
   }
   remaining = bytes;
   res = f->Write(f, &bytes, pbData);
   if (!EFI_ERROR(res)) {
	   remaining -= bytes;
	   pbData += bytes;
	   bytes = remaining;
	   while ((remaining > 0) && !EFI_ERROR(res))
	   {
		   res = f->Write(f, &bytes, pbData);
		   remaining -= bytes;
		   pbData += bytes;
		   bytes = remaining;
 	   }
   }
   if (position != NULL) {
      f->GetPosition(f, position);
   }
   return res;
}

CHAR8 gFileAsciiPrintBuffer[1024];

UINTN
FileAsciiPrint(
	IN EFI_FILE            *f,
	IN CONST CHAR8         *format,
	...
	) {
	VA_LIST  marker;
	UINTN    len;
	if (f == NULL) return 0;
	VA_START(marker, format);
	len = AsciiVSPrint((CHAR8*)gFileAsciiPrintBuffer, sizeof(gFileAsciiPrintBuffer), format, marker);
	VA_END(marker);
	f->Write(f, &len, gFileAsciiPrintBuffer);
	return len;
}

EFI_STATUS
FileGetInfo(
   IN    EFI_FILE*         f,
   OUT   EFI_FILE_INFO**   info,
   OUT   UINTN*            size
   )
{
   EFI_STATUS     res;
   UINTN          sz = 0;
   if (!f || !info) { return EFI_INVALID_PARAMETER; }
   res = f->GetInfo(f, &gEfiFileInfoGuid, &sz, NULL);
   if (res == RETURN_BUFFER_TOO_SMALL) {
      *info = (EFI_FILE_INFO*)MEM_ALLOC(sz);
      if (!(*info)) {
         return res;
      }
      res = f->GetInfo(f, &gEfiFileInfoGuid, &sz, *info);
      if (EFI_ERROR(res)) {
         MEM_FREE(*info);
         *info = NULL;
         sz = 0;
      }
   }
   if (size) {
      *size = sz;
   }
   return res;
}

EFI_STATUS 
FileGetSize(
   IN    EFI_FILE*   f,
   OUT   UINTN*     size
   ) 
{
   EFI_STATUS  res;
   EFI_FILE_INFO* info = NULL;
   if (!f || !size) { return EFI_INVALID_PARAMETER; }
   res = FileGetInfo(f, &info, NULL);
   if (!EFI_ERROR(res)) {
      *size = (UINTN)info->FileSize;
      MEM_FREE(info);
   }
   return res;
}

EFI_STATUS 
FileLoad(
   IN    EFI_FILE*   root,
   IN    CHAR16*     name,
   OUT   VOID**      data,
   OUT   UINTN*      size
   )
{
   EFI_FILE*      file;
   EFI_STATUS     res;
   UINTN          sz;
   if (!data) { 
      return EFI_INVALID_PARAMETER; 
   }
   res = FileOpen(root, name, &file, EFI_FILE_MODE_READ, 0);
   if (EFI_ERROR(res)) return res;
   res = FileGetSize(file, &sz);
   if (EFI_ERROR(res)) {
      FileClose(file);
      return res;
   }
   *data = MEM_ALLOC(sz);
   if (*data == NULL) {
      FileClose(file);
      return EFI_BUFFER_TOO_SMALL;
   }
   res = FileRead(file, *data, &sz, NULL);
   if (EFI_ERROR(res)) {
      FileClose(file);
      MEM_FREE(*data);
      return res;
   }
   FileClose(file);
   if (size != NULL) { 
      *size = sz; 
   }
   return res;
}

EFI_STATUS
FileSave(
   IN    EFI_FILE*   root,
   IN    CHAR16*     name,
   IN    VOID*       data,
   IN    UINTN      size
   )
{
   EFI_FILE*      file;
   EFI_STATUS     res;
   if (!data || !name) { return EFI_INVALID_PARAMETER; }
   FileDelete(root, name);
   res = FileOpen(root, name, &file, EFI_FILE_MODE_READ | EFI_FILE_MODE_CREATE | EFI_FILE_MODE_WRITE, 0);
   if (EFI_ERROR(res)) return res;
   res = FileWrite(file, data, size, NULL);
   FileClose(file);
   return res;
}

EFI_STATUS
FileExist(
	IN    EFI_FILE*   root,
	IN    CHAR16*     name)
{
	EFI_FILE*      file;
	EFI_STATUS     res;
	res = FileOpen(root, name, &file, EFI_FILE_MODE_READ, 0);
	if (EFI_ERROR(res)) return res;
	FileClose(file);
	return EFI_SUCCESS;
}

EFI_STATUS
FileRename(
	IN    EFI_FILE*   root,
	IN    CHAR16*     src,
	IN    CHAR16*     dst)
{
	EFI_STATUS     res;
	EFI_FILE*      file;
	EFI_FILE_INFO* info = NULL;
	UINTN          sz;
	EFI_FILE_INFO* dstinfo = NULL;
	UINTN          dstinfosz;

	res = FileOpen(root, src, &file, EFI_FILE_MODE_READ, 0);
	if (EFI_ERROR(res)) return res;
	res = FileGetInfo(file, &info, &sz);
	if (EFI_ERROR(res)) return res;
	dstinfosz = SIZE_OF_EFI_FILE_INFO + StrSize(dst);
	dstinfo = (EFI_FILE_INFO*)MEM_ALLOC(dstinfosz);
	if (dstinfo != NULL) {
		CopyMem(dstinfo, info, SIZE_OF_EFI_FILE_INFO);
		dstinfo->FileName[0] = 0;
		StrCat(dstinfo->FileName, dst);
		res = file->SetInfo(file, &gEfiFileInfoGuid, dstinfosz, dstinfo);
	}	else {
		res = EFI_BUFFER_TOO_SMALL;
	}

	MEM_FREE(info);
	MEM_FREE(dstinfo);
	FileClose(file);
	return res;
}

EFI_STATUS
FileCopy(
	IN    EFI_FILE*   srcroot,
	IN    CHAR16*     src,
	IN    EFI_FILE*   dstroot,
	IN    CHAR16*     dst,
	IN    UINTN       bufSz
	)
{
	EFI_STATUS     res;
	EFI_FILE*      srcfile = NULL;
	EFI_FILE*      dstfile = NULL;
	UINTN          remains;
	CHAR8*         data = NULL;
	UINTN          datasz = bufSz;

	res = FileOpen(srcroot, src, &srcfile, EFI_FILE_MODE_READ, 0);
	if (EFI_ERROR(res)) return res;
	res = FileGetSize(srcfile, &remains);
	if (EFI_ERROR(res)) return res;

	data = (CHAR8*)MEM_ALLOC(bufSz);
	if (data == NULL) {
		res = EFI_BUFFER_TOO_SMALL;
		goto copyerr;
	}
	
	FileDelete (dstroot, dst);
	res = FileOpen(dstroot, dst, &dstfile, EFI_FILE_MODE_CREATE | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ, 0);
	if (EFI_ERROR(res)) goto copyerr;

	do {
		datasz = remains > bufSz ? bufSz : remains;
		res =FileRead(srcfile, data, &datasz, NULL);
		if (EFI_ERROR(res)) goto copyerr;
		res = FileWrite(dstfile, data, datasz, NULL);
		if (EFI_ERROR(res)) goto copyerr;
		remains -= datasz;
	} while (remains > 0);

copyerr:
	MEM_FREE(data);
	FileClose(srcfile);
	FileClose(dstfile);
	return res;
}