summaryrefslogtreecommitdiff
path: root/lib/simple_file.c
blob: abbc4975ea5b128aeb14a9f45e824907c9d068bc (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
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
// SPDX-License-Identifier: BSD-2-Clause-Patent
/*
 * Copyright 2012 <James.Bottomley@HansenPartnership.com>
 */
#include "shim.h"

EFI_STATUS
simple_file_open_by_handle(EFI_HANDLE device, CHAR16 *name, EFI_FILE **file, UINT64 mode)
{
	EFI_STATUS efi_status;
	EFI_FILE_IO_INTERFACE *drive;
	EFI_FILE *root;

	efi_status = BS->HandleProtocol(device, &EFI_SIMPLE_FILE_SYSTEM_GUID,
					(void **)&drive);
	if (EFI_ERROR(efi_status)) {
		console_print(L"Unable to find simple file protocol (%d)\n",
			      efi_status);
		goto error;
	}

	efi_status = drive->OpenVolume(drive, &root);
	if (EFI_ERROR(efi_status)) {
		console_print(L"Failed to open drive volume (%d)\n", efi_status);
		goto error;
	}

	efi_status = root->Open(root, file, name, mode, 0);

 error:
	return efi_status;
}

EFI_STATUS
simple_file_open(EFI_HANDLE image, CHAR16 *name, EFI_FILE **file, UINT64 mode)
{
	EFI_STATUS efi_status;
	EFI_HANDLE device;
	EFI_LOADED_IMAGE *li;
	EFI_DEVICE_PATH *loadpath = NULL;
	CHAR16 *PathName = NULL;

	efi_status = BS->HandleProtocol(image, &IMAGE_PROTOCOL,
					(void **) &li);
	if (EFI_ERROR(efi_status))
		return simple_file_open_by_handle(image, name, file, mode);

	efi_status = generate_path(name, li, &loadpath, &PathName);
	if (EFI_ERROR(efi_status)) {
		console_print(L"Unable to generate load path for %s\n", name);
		return efi_status;
	}

	device = li->DeviceHandle;

	efi_status = simple_file_open_by_handle(device, PathName, file, mode);

	FreePool(PathName);
	FreePool(loadpath);

	return efi_status;
}

EFI_STATUS
simple_dir_read_all_by_handle(EFI_HANDLE image UNUSED, EFI_FILE *file,
			      CHAR16* name, EFI_FILE_INFO **entries, int *count)
{
	EFI_STATUS efi_status;
	char buf[4096];
	UINTN size = sizeof(buf);
	EFI_FILE_INFO *fi = (void *)buf;

	efi_status = file->GetInfo(file, &EFI_FILE_INFO_GUID, &size, fi);
	if (EFI_ERROR(efi_status)) {
		console_print(L"Failed to get file info\n");
		goto out;
	}
	if ((fi->Attribute & EFI_FILE_DIRECTORY) == 0) {
		console_print(L"Not a directory %s\n", name);
		efi_status = EFI_INVALID_PARAMETER;
		goto out;
	}
	size = 0;
	*count = 0;
	for (;;) {
		UINTN len = sizeof(buf);
		efi_status = file->Read(file, &len, buf);
		if (EFI_ERROR(efi_status) || len == 0)
			break;
		(*count)++;
		size += len;
	}
	file->SetPosition(file, 0);

	char *ptr = AllocatePool(size);
	*entries = (EFI_FILE_INFO *)ptr;
	if (!*entries)
		return EFI_OUT_OF_RESOURCES;
	int i;
	for (i = 0; i < *count; i++) {
		UINTN len = size;
		file->Read(file, &len, ptr);
		ptr += len;
		size -= len;
	}
	efi_status = EFI_SUCCESS;
 out:
	file->Close(file);
	if (EFI_ERROR(efi_status) && *entries) {
		FreePool(*entries);
		*entries = NULL;
	}
	return efi_status;
}

EFI_STATUS
simple_dir_read_all(EFI_HANDLE image, CHAR16 *name, EFI_FILE_INFO **entries,
		    int *count)
{
	EFI_FILE *file;
	EFI_STATUS efi_status;

	efi_status = simple_file_open(image, name, &file, EFI_FILE_MODE_READ);
	if (EFI_ERROR(efi_status)) {
		console_print(L"failed to open file %s: %d\n", name, efi_status);
		return efi_status;
	}

	return simple_dir_read_all_by_handle(image, file, name, entries, count);
}

EFI_STATUS
simple_file_read_all(EFI_FILE *file, UINTN *size, void **buffer)
{
	EFI_STATUS efi_status;
	EFI_FILE_INFO *fi;
	char buf[1024];

	*size = sizeof(buf);
	fi = (void *)buf;

	efi_status = file->GetInfo(file, &EFI_FILE_INFO_GUID, size, fi);
	if (EFI_ERROR(efi_status)) {
		console_print(L"Failed to get file info\n");
		return efi_status;
	}

	*size = fi->FileSize;

	*buffer = AllocatePool(*size);
	if (!*buffer) {
		console_print(L"Failed to allocate buffer of size %d\n", *size);
		return EFI_OUT_OF_RESOURCES;
	}

	efi_status = file->Read(file, size, *buffer);
	return efi_status;
}


EFI_STATUS
simple_file_write_all(EFI_FILE *file, UINTN size, void *buffer)
{
	EFI_STATUS efi_status;

	efi_status = file->Write(file, &size, buffer);
	return efi_status;
}

EFI_STATUS
simple_volume_selector(CHAR16 **title, CHAR16 **selected, EFI_HANDLE *h)
{
	UINTN count, i, j;
	EFI_HANDLE *vol_handles = NULL;
	EFI_STATUS efi_status;
	CHAR16 **entries;
	int val;

	efi_status = BS->LocateHandleBuffer(ByProtocol,
					    &EFI_SIMPLE_FILE_SYSTEM_GUID,
					    NULL, &count, &vol_handles);
	if (EFI_ERROR(efi_status))
		return efi_status;
	if (!count || !vol_handles)
		return EFI_NOT_FOUND;

	entries = AllocateZeroPool(sizeof(CHAR16 *) * (count+1));
	if (!entries)
		return EFI_OUT_OF_RESOURCES;

	for (i = 0, j = 0; i < count; i++) {
		char buf[4096];
		UINTN size = sizeof(buf);
		EFI_FILE_SYSTEM_INFO *fi = (void *)buf;
		EFI_FILE *root;
		CHAR16 *name;
		EFI_FILE_IO_INTERFACE *drive;

		efi_status = BS->HandleProtocol(vol_handles[i],
						&EFI_SIMPLE_FILE_SYSTEM_GUID,
						(void **) &drive);
		if (EFI_ERROR(efi_status) || !drive)
			continue;

		efi_status = drive->OpenVolume(drive, &root);
		if (EFI_ERROR(efi_status))
			continue;

		efi_status = root->GetInfo(root, &EFI_FILE_SYSTEM_INFO_GUID,
					   &size, fi);
		/* If GetInfo fails, try to form a name from DevicePath. */
		if (EFI_ERROR(efi_status)){
			name = NULL;
		} else {
			name = fi->VolumeLabel;
		}

		if (!name || StrLen(name) == 0 || StrCmp(name, L" ") == 0)
			name = DevicePathToStr(DevicePathFromHandle(vol_handles[i]));

		entries[j] = AllocatePool((StrLen(name) + 2) * sizeof(CHAR16));
		if (!entries[j])
			break;
		StrCpy(entries[j++], name);
	}
	entries[j] = NULL;

	val = console_select(title, entries, 0);

	if (val >= 0) {
		*selected = AllocatePool((StrLen(entries[val]) + 1) * sizeof(CHAR16));
		if (*selected) {
			StrCpy(*selected , entries[val]);
		}
		*h = vol_handles[val];
	} else {
		*selected = NULL;
		*h = 0;
	}

	for (i = 0; i < count; i++) {
		if (entries[i])
			FreePool(entries[i]);
	}
	FreePool(entries);
	FreePool(vol_handles);

	return EFI_SUCCESS;
}

EFI_STATUS
simple_dir_filter(EFI_HANDLE image, CHAR16 *name, CHAR16 *filter,
		  CHAR16 ***result, int *count, EFI_FILE_INFO **entries)
{
	EFI_STATUS efi_status;
	int tot, offs = StrLen(filter), i, c, filtercount = 1;
	EFI_FILE_INFO *next;
	void *ptr;
	CHAR16 *newfilter = AllocatePool((StrLen(filter) + 1) * sizeof(CHAR16)),
		**filterarr;

	if (!newfilter)
		return EFI_OUT_OF_RESOURCES;

	/* just in case efi ever stops writeable strings */
	StrCpy(newfilter, filter);

	for (i = 0; i < offs; i++) {
		if (filter[i] == '|')
			filtercount++;
	}
	filterarr = AllocatePool(filtercount * sizeof(void *));
	if (!filterarr)
		return EFI_OUT_OF_RESOURCES;
	c = 0;
	filterarr[c++] = newfilter;
	for (i = 0; i < offs; i++) {
		if (filter[i] == '|') {
			newfilter[i] = '\0';
			filterarr[c++] = &newfilter[i+1];
		}
	}

	*count = 0;

	efi_status = simple_dir_read_all(image, name, entries, &tot);
	if (EFI_ERROR(efi_status))
		goto out;
	ptr = next = *entries;

	for (i = 0; next && i < tot; i++) {
		int len = StrLen(next->FileName);

		for (c = 0; c < filtercount; c++) {
			offs = StrLen(filterarr[c]);

			if (StrCmp(&next->FileName[len - offs], filterarr[c]) == 0
			    || (next->Attribute & EFI_FILE_DIRECTORY)) {
				(*count)++;
				break;
			}
		}
		ptr += offsetof(EFI_FILE_INFO, FileName) + (len + 1)*sizeof(CHAR16);
		next = ptr;
	}
	if (*count)
		*result = AllocatePool(((*count) + 1) * sizeof(void *));
	else
		*result = AllocatePool(2 * sizeof(void *));

	*count = 0;
	ptr = next = *entries;

	for (i = 0; next && i < tot; i++) {
		int len = StrLen(next->FileName);

		if (StrCmp(next->FileName, L".") == 0)
			/* ignore . directory */
			goto next;

		if (next->Attribute & EFI_FILE_DIRECTORY) {
				(*result)[(*count)] = PoolPrint(L"%s/", next->FileName);
				if (!(*result)[(*count)]) {
					console_print(L"Failed to allocate buffer");
					return EFI_OUT_OF_RESOURCES;
				}
				(*count)++;
				goto next;
		}

		for (c = 0; c < filtercount; c++) {
			offs = StrLen(filterarr[c]);

			if (StrCmp(&next->FileName[len - offs], filterarr[c]) == 0) {
				(*result)[(*count)] = StrDuplicate(next->FileName);
				if (!(*result)[(*count)]) {
					console_print(L"Failed to allocate buffer");
					return EFI_OUT_OF_RESOURCES;
				}
				(*count)++;
			} else {
				continue;
			}
			break;
		}

	next:
		if (StrCmp(next->FileName, L"..") == 0) {
			/* place .. directory first */
			CHAR16 *tmp = (*result)[(*count) - 1];

			(*result)[(*count) - 1] = (*result)[0];
			(*result)[0] = tmp;
		}

		ptr += offsetof(EFI_FILE_INFO, FileName) + (len + 1)*sizeof(CHAR16);
		next = ptr;
	}
	if (*count == 0) {
		/* no entries at all ... can happen because top level dir has no . or .. */
		(*result)[(*count)++] = L"./";
	}
	(*result)[*count] = NULL;
	efi_status = EFI_SUCCESS;

 out:
	if (EFI_ERROR(efi_status)) {
		if (*entries)
			FreePool(*entries);
		*entries = NULL;
		if (*result)
			FreePool(*result);
		*result = NULL;
	}
	return efi_status;
}

static void
free_entries(CHAR16 **entries, int count)
{
	int i;

	for (i = 0; i<count; i++)
		FreePool(entries[i]);
}

void
simple_file_selector(EFI_HANDLE * im, CHAR16 ** title, CHAR16 * name,
		     CHAR16 * filter, CHAR16 ** result)
{
	EFI_STATUS efi_status;
	CHAR16 **entries = NULL;
	EFI_FILE_INFO *dmp = NULL;
	int count, select, len;
	CHAR16 *newname, *selected;

	*result = NULL;
	if (!name)
		name = L"\\";
	if (!filter)
		filter = L"";
	if (!*im) {
		EFI_HANDLE h;
		CHAR16 *volname = NULL;

		efi_status = simple_volume_selector(title, &volname, &h);
		if (EFI_ERROR(efi_status) || !volname)
			return;
		FreePool(volname);
		*im = h;
	}

	newname = AllocatePool((StrLen(name) + 1) * sizeof(CHAR16));
	if (!newname)
		return;

	StrCpy(newname, name);
	name = newname;

redo:
	efi_status = simple_dir_filter(*im, name, filter, &entries, &count,
				       &dmp);
	if (EFI_ERROR(efi_status))
		goto out_free;

	select = console_select(title, entries, 0);
	if (select < 0)
		/* ESC key */
		goto out_free;
	selected = entries[select];
	/* note that memory used by selected is valid until dmp is freed */
	len = StrLen(selected);
	if (selected[len - 1] == '/') {
		CHAR16 *newname;

		/* stay where we are */
		if (StrCmp(selected, L"./") == 0) {
			free_entries(entries, count);
			FreePool(entries);
			entries = NULL;
			FreePool(dmp);
			goto redo;
		} else if (StrCmp(selected, L"../") == 0) {
			int i;

			for (i = StrLen(name); i > 0; --i) {
				if (name[i] == '\\')
					break;
			}
			if (i == 0)
				i = 1;

			if (StrCmp(name, L"\\") != 0
			    && StrCmp(&name[i], L"..") != 0) {
				name[i] = '\0';
				free_entries(entries, count);
				FreePool(entries);
				entries = NULL;
				FreePool(dmp);
				goto redo;
			}
		}
		newname =
		    AllocatePool((StrLen(name) + len + 2) * sizeof(CHAR16));
		if (!newname)
			goto out_free;
		StrCpy(newname, name);

		if (name[StrLen(name) - 1] != '\\')
			StrCat(newname, L"\\");
		StrCat(newname, selected);
		/* remove trailing / */
		newname[StrLen(newname) - 1] = '\0';

		free_entries(entries, count);
		FreePool(entries);
		entries = NULL;
		FreePool(dmp);
		FreePool(name);
		name = newname;

		goto redo;
	}
	*result = AllocatePool((StrLen(name) + len + 2) * sizeof(CHAR16));
	if (*result) {
		StrCpy(*result, name);
		if (name[StrLen(name) - 1] != '\\')
			StrCat(*result, L"\\");
		StrCat(*result, selected);
	}

out_free:
	if (dmp)
		FreePool(dmp);
	if (entries) {
		free_entries(entries, count);
		FreePool(entries);
	}
	FreePool(name);
}