VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/Library/CommonLib/EfiBio.c
blob: e6509f9f5584d6d5b3820139f2ad004f6929107e (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
/** @file
EFI block I/O helpers routines/wrappers

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 <Library/CommonLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/DevicePathLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseMemoryLib.h>
#include <Protocol/LoadedImage.h>

//////////////////////////////////////////////////////////////////////////
// Print handle info
//////////////////////////////////////////////////////////////////////////

VOID
EfiPrintDevicePath(
   IN EFI_HANDLE handle)
{
   CHAR16                      *StrPath;
   EFI_DEVICE_PATH             *DevicePath;
   OUT_PRINT(L"(%0X): ", handle);
   DevicePath = DevicePathFromHandle(handle);
   if (DevicePath == NULL) {
      ERR_PRINT(L"No path found");
      return;
   }
   StrPath = ConvertDevicePathToText(DevicePath, FALSE, FALSE);
   OUT_PRINT(StrPath);
   MEM_FREE(StrPath);
}

VOID
EfiPrintProtocols(
   IN EFI_HANDLE handle)
{
   EFI_GUID       **guids;
   UINTN          count;
   EFI_STATUS     status;
   status = gBS->ProtocolsPerHandle(handle, &guids, &count);
   if (!EFI_ERROR(status)) {
      UINTN i;
      for (i = 0; i < count; ++i) {
         OUT_PRINT(L"%d: %g\n", i, guids[i]);
      }
      FreePool(guids);
   }
}

//////////////////////////////////////////////////////////////////////////
// Handles
//////////////////////////////////////////////////////////////////////////

EFI_STATUS
EfiGetHandles(
   IN  EFI_LOCATE_SEARCH_TYPE   SearchType,
   IN  EFI_GUID                 *Protocol,    OPTIONAL
   IN  VOID                     *SearchKey,   OPTIONAL
   OUT EFI_HANDLE               **Buffer,
   OUT UINTN                    *Count
   ) 
{
   EFI_STATUS res = EFI_BUFFER_TOO_SMALL;
   UINTN      BufferSize;
	if ((Buffer == NULL) || (Count == NULL)) return EFI_INVALID_PARAMETER;
   if(*Buffer != NULL) MEM_FREE(*Buffer);
   *Count = 0;
   *Buffer = (EFI_HANDLE*) MEM_ALLOC(sizeof(EFI_HANDLE));
   if (*Buffer) {
      BufferSize = sizeof(EFI_HANDLE);
      res = gBS->LocateHandle(SearchType, Protocol, SearchKey, &BufferSize, *Buffer);
      if (res == RETURN_BUFFER_TOO_SMALL) {
         MEM_FREE(*Buffer);
         *Buffer = (EFI_HANDLE*)MEM_ALLOC(BufferSize);
         if (*Buffer == NULL) {
            return EFI_OUT_OF_RESOURCES;
         }
         res = gBS->LocateHandle(SearchType, Protocol, SearchKey, &BufferSize, *Buffer);
         if(res != EFI_SUCCESS) {
            MEM_FREE(*Buffer);
            *Buffer = (EFI_HANDLE*)NULL;
            return res;
         }
      } else if (EFI_ERROR(res)) {
         MEM_FREE(*Buffer);
         *Buffer = (EFI_HANDLE*)NULL;
         return res;
      }
      *Count = (UINTN)(BufferSize / sizeof(EFI_HANDLE));
   }
   return res;
}

EFI_STATUS 
EfiGetStartDevice(
   OUT EFI_HANDLE* handle) 
{
   EFI_STATUS                  Status;
   EFI_LOADED_IMAGE_PROTOCOL   *LoadedImage;
   Status = gBS->HandleProtocol(gImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&LoadedImage);
   if (EFI_ERROR(Status)) {
      return Status;
   }
   *handle = LoadedImage->DeviceHandle;
   return Status;
}

//////////////////////////////////////////////////////////////////////////
// Block I/O
//////////////////////////////////////////////////////////////////////////

EFI_BLOCK_IO_PROTOCOL*
EfiGetBlockIO(
   IN EFI_HANDLE handle
   )
{
   EFI_STATUS res;
   EFI_BLOCK_IO_PROTOCOL* blockIOProtocol = NULL;
   res = gBS->HandleProtocol(handle, &gEfiBlockIoProtocolGuid, (VOID**)&blockIOProtocol);
   if (res == RETURN_SUCCESS &&
      blockIOProtocol != NULL &&
      blockIOProtocol->Media->MediaPresent) {
      return blockIOProtocol;
   }
   return NULL;
}


EFI_HANDLE* gBIOHandles;
UINTN       gBIOCount;

EFI_STATUS
InitBio() {
   EFI_STATUS res;
   res = EfiGetHandles(ByProtocol, &gEfiBlockIoProtocolGuid, 0, &gBIOHandles, &gBIOCount);
   return res;
}

BOOLEAN
EfiIsPartition(
	IN    EFI_HANDLE              h
	)
{
	EFI_DEVICE_PATH_PROTOCOL*  node;
	node = DevicePathFromHandle(h);
	if (node == NULL) return FALSE;
	while (!IsDevicePathEnd(node)) {
		if (node->Type == MEDIA_DEVICE_PATH && node->SubType == MEDIA_HARDDRIVE_DP) {
			return TRUE;
		}
		node = NextDevicePathNode(node);
	}
	return FALSE;
}

EFI_STATUS 
EfiGetPartDetails(
   IN    EFI_HANDLE              h,
   OUT   HARDDRIVE_DEVICE_PATH*  dpVolme,
   OUT   EFI_HANDLE*             hDisk)
{
   EFI_DEVICE_PATH_PROTOCOL*  node;
   EFI_DEVICE_PATH_PROTOCOL*  dpVolume;
   EFI_DEVICE_PATH_PROTOCOL*  dpDisk;
   EFI_STATUS                 res;
   dpVolume = DevicePathFromHandle(h);
   dpDisk = DuplicateDevicePath(dpVolume);
   node = (EFI_DEVICE_PATH_PROTOCOL *)dpDisk;
   while (!IsDevicePathEnd(node)) {
      if (node->Type == MEDIA_DEVICE_PATH && node->SubType == MEDIA_HARDDRIVE_DP) {
         CopyMem(dpVolme, node, sizeof(HARDDRIVE_DEVICE_PATH));
         SetDevicePathEndNode(node);
         res = gBS->LocateDevicePath(&gEfiBlockIoProtocolGuid, &dpDisk, hDisk);
         return res;
      }
      node = NextDevicePathNode(node);
   }
   return EFI_NOT_FOUND;
}

EFI_STATUS
EfiGetPartGUID(
	IN    EFI_HANDLE              h,
	OUT   EFI_GUID*               guid)
{
	EFI_DEVICE_PATH_PROTOCOL*  node;
	EFI_DEVICE_PATH_PROTOCOL*  dpVolume;
	if (guid == NULL) return EFI_INVALID_PARAMETER;
	dpVolume = DevicePathFromHandle(h);
	node = (EFI_DEVICE_PATH_PROTOCOL *)dpVolume;
	while (!IsDevicePathEnd(node)) {
		if (node->Type == MEDIA_DEVICE_PATH && node->SubType == MEDIA_HARDDRIVE_DP) {
			HARDDRIVE_DEVICE_PATH* hdpNode = (HARDDRIVE_DEVICE_PATH*)node;
			CopyMem(guid, hdpNode->Signature, sizeof(*guid));
			return EFI_SUCCESS;
		}
		node = NextDevicePathNode(node);
	}
	return EFI_NOT_FOUND;
}

EFI_STATUS
EfiFindPartByGUID(
	IN   EFI_GUID*               guid,
	OUT  EFI_HANDLE*             h
	)
{
	EFI_STATUS                 res;
	EFI_GUID                   guidI;
	UINTN                      i;
	if (guid == NULL || h == NULL) return EFI_INVALID_PARAMETER;
	for (i = 0; i < gBIOCount; ++i) {
		res = EfiGetPartGUID(gBIOHandles[i], &guidI);
		if (!EFI_ERROR(res)) {
			if (CompareMem(&guidI, guid, sizeof(guidI)) == 0) {
				*h = gBIOHandles[i];
				return EFI_SUCCESS;
			}
		}
	}
	return EFI_NOT_FOUND;
}