VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/Library/CommonLib/EfiVar.c
blob: e9c10829fd0826426b8b90889e72048e5e4a7427 (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
/** @file
EFI firmware variable 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 <Library/CommonLib.h>

#include <Uefi.h>
#include <Guid/GlobalVariable.h>
#include <Protocol/DevicePath.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/DevicePathLib.h>
#include <Library/BaseMemoryLib.h>

//////////////////////////////////////////////////////////////////////////
// Efi variables
//////////////////////////////////////////////////////////////////////////

EFI_STATUS
EfiGetVar(
   IN  CONST CHAR16*    varName,
   IN  EFI_GUID*        varGuid,
   OUT VOID**           varValue,
   OUT UINTN*           varSize,
   OUT UINT32*          varAttr
   ) {
   EFI_STATUS          Status;
   CHAR8               *varData = NULL;
   if (varSize == NULL) return EFI_INVALID_PARAMETER;
   *varSize = 0;
   if (varGuid == NULL) varGuid = &gEfiDcsVariableGuid;
   Status = gST->RuntimeServices->GetVariable((CHAR16*)varName, varGuid, varAttr, varSize, varData);
   if (Status == EFI_BUFFER_TOO_SMALL) {
      varData = MEM_ALLOC(*varSize);
      if (varData == NULL) {
         return EFI_BUFFER_TOO_SMALL;
      }
      Status = gST->RuntimeServices->GetVariable((CHAR16*)varName, varGuid, varAttr, varSize, varData);
      *varValue = varData;
   }
   return Status;
}


EFI_STATUS
EfiSetVar(
   IN  CONST CHAR16*    varName,
   IN  EFI_GUID*        varGuid,
   IN  VOID*            varValue,
   IN  UINTN            varSize,
   IN  UINT32           varAttr
   ) {
   EFI_STATUS          Status;
   if (varGuid == NULL) varGuid = &gEfiDcsVariableGuid;
   Status = gST->RuntimeServices->SetVariable((CHAR16*)varName, varGuid, varAttr, varSize, varValue);
   return Status;
}

EFI_STATUS
BootOrderInsert(
	IN CHAR16 *OrderVarName,
	IN UINTN index,
	UINT16   value)
{
	EFI_STATUS res = EFI_NOT_READY;
	UINT16*   varBootOrderNew;
	UINT16*   varBootOrder;
	UINTN     varBootOrderSize;
	UINT32    varBootOrderAttr;
	UINTN     BootOrderCount;
	UINTN     i;
	UINTN     j;
	res = EfiGetVar(OrderVarName, &gEfiGlobalVariableGuid, &varBootOrder, &varBootOrderSize, &varBootOrderAttr);
	if (EFI_ERROR(res)) {
		res = EfiSetVar(OrderVarName, &gEfiGlobalVariableGuid, &value, 2,
			EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS);
		return res;
	}
	BootOrderCount = varBootOrderSize / sizeof(UINT16);
	varBootOrderNew = MEM_ALLOC((BootOrderCount + 1) * sizeof(UINT16));
	if (BootOrderCount < index) index = BootOrderCount - 1;
	for (j = 0, i = 0; i < BootOrderCount; ++i) {
		if (j == index) {
			varBootOrderNew[j] = value;
			j++;
		}
		if (varBootOrder[i] == value) {
			continue;
		}
		varBootOrderNew[j] = varBootOrder[i];
		++j;
	}
	BootOrderCount = j;

	res = EfiSetVar(OrderVarName, &gEfiGlobalVariableGuid,
		varBootOrderNew, BootOrderCount * sizeof(UINT16),
		EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS);
	MEM_FREE(varBootOrderNew);
	MEM_FREE(varBootOrder);
	return res;
}

EFI_STATUS
BootOrderRemove(
	IN CHAR16 *OrderVarName,
	UINT16   value)
{
	EFI_STATUS res = EFI_NOT_READY;
	UINT16*   varBootOrderNew;
	UINT16*   varBootOrder;
	UINTN     varBootOrderSize;
	UINT32    varBootOrderAttr;
	UINTN     BootOrderCount;
	UINTN     i;
	UINTN     j;

	res = EfiGetVar(OrderVarName, &gEfiGlobalVariableGuid, &varBootOrder, &varBootOrderSize, &varBootOrderAttr);
	if (EFI_ERROR(res)) return res;
	BootOrderCount = varBootOrderSize / sizeof(UINT16);
	varBootOrderNew = MEM_ALLOC((BootOrderCount + 1) * sizeof(UINT16));
	for (j = 0, i = 0; i < BootOrderCount; ++i) {
		if (varBootOrder[i] == value) {
			continue;
		}
		varBootOrderNew[j] = varBootOrder[i];
		++j;
	}
	BootOrderCount = j;

	res = EfiSetVar(OrderVarName, &gEfiGlobalVariableGuid,
		varBootOrderNew, BootOrderCount * sizeof(UINT16),
		EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS);

	MEM_FREE(varBootOrderNew);
	MEM_FREE(varBootOrder);
	return res;
}

EFI_STATUS
BootOrderPresent(
    IN CHAR16 *OrderVarName,
    UINT16   value)
{
    EFI_STATUS res = EFI_NOT_READY;
    UINT16*   varBootOrder;
    UINTN     varBootOrderSize;
    UINT32    varBootOrderAttr;
    UINTN     BootOrderCount;
    UINTN     i;
    UINTN     j;

    res = EfiGetVar(OrderVarName, &gEfiGlobalVariableGuid, &varBootOrder, &varBootOrderSize, &varBootOrderAttr);
    if (EFI_ERROR(res)) return res;
    BootOrderCount = varBootOrderSize / sizeof(UINT16);
    res = EFI_NOT_FOUND;
    for (j = 0, i = 0; i < BootOrderCount; ++i) {
        if (varBootOrder[i] == value) {
            MEM_FREE(varBootOrder);
            res = EFI_SUCCESS;
            break;
        }
    }
    return res;
}

EFI_STATUS
BootMenuItemRemove(
	IN CHAR16     *VarName
	)
{
	EFI_STATUS res = EFI_NOT_READY;
	res = EfiSetVar(
		VarName, &gEfiGlobalVariableGuid,
		NULL, 0,
		EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS);
	return res;
}

EFI_STATUS
BootMenuItemCreate(
	IN CHAR16     *VarName,
	IN CHAR16     *Desc,
	IN EFI_HANDLE volumeHandle,
	IN CHAR16     *Path,
	IN BOOLEAN    Reduced
	)
{
	EFI_STATUS res = EFI_NOT_READY;
	UINT8*    varBoot;
	UINT8*    TempByteBuffer;
	UINTN     DescSize;
	UINTN     FilePathSize;
	EFI_DEVICE_PATH_PROTOCOL*  DevPath;
	EFI_DEVICE_PATH_PROTOCOL*  DevicePath;
	EFI_DEVICE_PATH_PROTOCOL*  FileNode;
	EFI_DEVICE_PATH_PROTOCOL*  FilePath;

	// Prepare
	DevicePath = DevicePathFromHandle(volumeHandle);
	if (Reduced) {
		DevPath = DevicePath;
		while (!IsDevicePathEnd(DevPath)) {
			if ((DevicePathType(DevPath) == MEDIA_DEVICE_PATH) &&
				(DevicePathSubType(DevPath) == MEDIA_HARDDRIVE_DP)) {

				//
				// If we find it use it instead
				//
				DevicePath = DevPath;
				break;
			}
			DevPath = NextDevicePathNode(DevPath);
		}
	}
	//
	// append the file
	//
	FileNode = FileDevicePath(NULL, Path);
	FilePath = AppendDevicePath(DevicePath, FileNode);
	FreePool(FileNode);
	//
	// Add the option
	//
	DescSize = StrSize(Desc);
	FilePathSize = GetDevicePathSize(FilePath);

	varBoot = MEM_ALLOC(sizeof(UINT32) + sizeof(UINT16) + DescSize + FilePathSize);
	if (varBoot != NULL) {
		TempByteBuffer = varBoot;
		*((UINT32 *)TempByteBuffer) = LOAD_OPTION_ACTIVE;      // Attributes
		TempByteBuffer += sizeof(UINT32);

		*((UINT16 *)TempByteBuffer) = (UINT16)FilePathSize;    // FilePathListLength
		TempByteBuffer += sizeof(UINT16);

		CopyMem(TempByteBuffer, Desc, DescSize);
		TempByteBuffer += DescSize;
		CopyMem(TempByteBuffer, FilePath, FilePathSize);

		res = EfiSetVar(VarName, &gEfiGlobalVariableGuid,
			varBoot, sizeof(UINT32) + sizeof(UINT16) + DescSize + FilePathSize,
			EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS);
	}
	else {
		return EFI_BUFFER_TOO_SMALL;
	}

	MEM_FREE(varBoot);
	return res;
}