]> code.delx.au - refind/blob - mok/simple_file.c
Files for new MOK/shim code based on Bottomley's PreLoader.
[refind] / mok / simple_file.c
1 /*
2 * Copyright 2012 <James.Bottomley@HansenPartnership.com>
3 *
4 * see COPYING file
5 */
6
7 #include <global.h>
8 #include "../include/refit_call_wrapper.h"
9
10 #include "console.h"
11 #include "simple_file.h"
12 //#include "efiauthenticated.h"
13 #include "execute.h" /* for generate_path() */
14
15 static EFI_GUID IMAGE_PROTOCOL = LOADED_IMAGE_PROTOCOL;
16 static EFI_GUID SIMPLE_FS_PROTOCOL = SIMPLE_FILE_SYSTEM_PROTOCOL;
17 static EFI_GUID FILE_INFO = EFI_FILE_INFO_ID;
18
19 EFI_STATUS
20 simple_file_open_by_handle(EFI_HANDLE device, CHAR16 *name, EFI_FILE **file, UINT64 mode)
21 {
22 EFI_STATUS efi_status;
23 EFI_FILE_IO_INTERFACE *drive;
24 EFI_FILE *root;
25
26 efi_status = uefi_call_wrapper(BS->HandleProtocol, 3, device,
27 &SIMPLE_FS_PROTOCOL, (VOID**) &drive);
28
29 if (efi_status != EFI_SUCCESS) {
30 Print(L"Unable to find simple file protocol\n");
31 goto error;
32 }
33
34 efi_status = uefi_call_wrapper(drive->OpenVolume, 2, drive, &root);
35
36 if (efi_status != EFI_SUCCESS) {
37 Print(L"Failed to open drive volume\n");
38 goto error;
39 }
40
41 efi_status = uefi_call_wrapper(root->Open, 5, root, file, name,
42 mode, 0);
43
44 error:
45 return efi_status;
46 }
47
48 EFI_STATUS
49 simple_file_open(EFI_HANDLE image, CHAR16 *name, EFI_FILE **file, UINT64 mode)
50 {
51 EFI_STATUS efi_status;
52 EFI_HANDLE device;
53 EFI_LOADED_IMAGE *li;
54 EFI_DEVICE_PATH *loadpath = NULL;
55 CHAR16 *PathName = NULL;
56
57 efi_status = uefi_call_wrapper(BS->HandleProtocol, 3, image,
58 &IMAGE_PROTOCOL, (VOID**) &li);
59
60 if (efi_status != EFI_SUCCESS)
61 return simple_file_open_by_handle(image, name, file, mode);
62
63 efi_status = generate_path(name, li, &loadpath, &PathName);
64
65 if (efi_status != EFI_SUCCESS) {
66 Print(L"Unable to generate load path for %s\n", name);
67 return efi_status;
68 }
69
70 device = li->DeviceHandle;
71
72 efi_status = simple_file_open_by_handle(device, PathName, file, mode);
73
74 FreePool(PathName);
75 FreePool(loadpath);
76
77 return efi_status;
78 }
79
80 EFI_STATUS
81 simple_file_read_all(EFI_FILE *file, UINTN *size, void **buffer)
82 {
83 EFI_STATUS efi_status;
84 EFI_FILE_INFO *fi;
85 char buf[1024];
86
87 *size = sizeof(buf);
88 fi = (void *)buf;
89
90
91 efi_status = uefi_call_wrapper(file->GetInfo, 4, file, &FILE_INFO,
92 size, fi);
93 if (efi_status != EFI_SUCCESS) {
94 Print(L"Failed to get file info\n");
95 return efi_status;
96 }
97
98 *size = fi->FileSize;
99
100 *buffer = AllocatePool(*size);
101 if (!*buffer) {
102 Print(L"Failed to allocate buffer of size %d\n", *size);
103 return EFI_OUT_OF_RESOURCES;
104 }
105 efi_status = uefi_call_wrapper(file->Read, 3, file, size, *buffer);
106
107 return efi_status;
108 }
109
110 void
111 simple_file_close(EFI_FILE *file)
112 {
113 uefi_call_wrapper(file->Close, 1, file);
114 }