VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/DcsBml/DcsBml.c
blob: f5aaf3a1d46dd9986c09c6654a599bbac62da299 (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
/** @file
  This is DCS boot menu lock application

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

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 <Guid/EventGroup.h>
#include <Guid/GlobalVariable.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeLib.h>
#include <Library/BaseLib.h>
#include <Library/UefiLib.h>

#include <Library/CommonLib.h>

#include <Protocol/DcsBmlProto.h>
#include "DcsBml.h"

//////////////////////////////////////////////////////////////////////////
// Runtime data to lock
//////////////////////////////////////////////////////////////////////////
typedef struct _BML_GLOBALS {
	UINT64		Signature;
	UINTN			size;
} BML_GLOBALS, *PBML_GLOBALS;

STATIC PBML_GLOBALS   gBmlData = NULL;
STATIC BOOLEAN        BootMenuLocked = FALSE;
EFI_EVENT             mBmlVirtualAddrChangeEvent;
EFI_SET_VARIABLE      orgSetVariable = NULL;

EFI_STATUS
BmlSetVaribale(
	IN  CHAR16                       *VariableName,
	IN  EFI_GUID                     *VendorGuid,
	IN  UINT32                       Attributes,
	IN  UINTN                        DataSize,
	IN  VOID                         *Data
	) {
	// DcsBoot remove?
	if (VariableName != NULL && StrStr(VariableName, L"BootDC5B") == VariableName && DataSize == 0) {
		BootMenuLocked = FALSE;
	}

	if (BootMenuLocked) {
		// Block all Boot*
		if (VariableName != NULL && StrStr(VariableName, L"Boot") == VariableName) {
			return EFI_ACCESS_DENIED;
		}
	}
	return orgSetVariable(VariableName, VendorGuid, Attributes, DataSize, Data);
}

/**
Fixup internal data so that EFI can be call in virtual mode.
Call the passed in Child Notify event and convert any pointers in
lib to virtual mode.

@param[in]    Event   The Event that is being processed
@param[in]    Context Event Context
**/

VOID
EFIAPI
BmlVirtualNotifyEvent(
	IN EFI_EVENT        Event,
	IN VOID             *Context
	)
{
	EfiConvertPointer(0x0, (VOID**)&gBmlData);
	EfiConvertPointer(0x0, (VOID**)&orgSetVariable);
	return;
}

//////////////////////////////////////////////////////////////////////////
// DcsBml protocol to control lock in BS mode
//////////////////////////////////////////////////////////////////////////
CHAR16* sDcsBootEfi = L"EFI\\VeraCrypt\\DcsBoot.efi";
CHAR16* sDcsBootEfiDesc = L"VeraCrypt(DCS) loader";

GUID gEfiDcsBmlProtocolGuid = EFI_DCSBML_INTERFACE_PROTOCOL_GUID;
EFI_DCSBML_PROTOCOL gEfiDcsBmlProtocol = {
    BootMenuLock
};

EFI_STATUS
BootMenuLock(
    IN EFI_DCSBML_PROTOCOL                *This,
    IN     BOOLEAN                        Lock
    ) {
    BootMenuLocked = Lock;
    return EFI_SUCCESS;
}

//////////////////////////////////////////////////////////////////////////
// Driver
//////////////////////////////////////////////////////////////////////////

/**
Unloads an image.

@param  ImageHandle           Handle that identifies the image to be unloaded.

@retval EFI_SUCCESS           The image has been unloaded.
@retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.

**/
EFI_STATUS
EFIAPI
DcsBmlUnload(
    IN EFI_HANDLE  ImageHandle
    )
{
    EFI_STATUS  res;

    res = EFI_SUCCESS;
    //
    // Uninstall Driver Supported EFI Version Protocol onto ImageHandle
    //
    res = gBS->UninstallMultipleProtocolInterfaces(
        ImageHandle,
        &gEfiDcsBmlProtocolGuid, &gEfiDcsBmlProtocol,
        NULL
        );

    if (EFI_ERROR(res)) {
        return res;
    }
    // Clean up
    return EFI_SUCCESS;
}


/**
The actual entry point for the application.

@param[in] ImageHandle    The firmware allocated handle for the EFI image.
@param[in] SystemTable    A pointer to the EFI System Table.

@retval EFI_SUCCESS       The entry point executed successfully.
@retval other             Some error occur when executing this entry point.

**/
EFI_STATUS
EFIAPI
DcsBmlMain(
   IN EFI_HANDLE        ImageHandle,
   IN EFI_SYSTEM_TABLE  *SystemTable
   )
{
   EFI_STATUS          res;
   // Check multiple execution of DcsBml
   if (!EFI_ERROR(InitBml())) {
       return EFI_ACCESS_DENIED;
   }

   //
   // Install DcsBml protocol onto ImageHandle
   //
   res = gBS->InstallMultipleProtocolInterfaces(
       &ImageHandle,
       &gEfiDcsBmlProtocolGuid, &gEfiDcsBmlProtocol,
       NULL
       );
   ASSERT_EFI_ERROR(res);

   if (EFI_ERROR(res)) {
       Print(L"Install protocol %r\n", res);
       return res;
   }

    // runtime lock
	res = gBS->AllocatePool(
		EfiRuntimeServicesData,
		(UINTN) sizeof(BML_GLOBALS),
		(VOID**)&gBmlData
		);

	if (EFI_ERROR(res)) {
		Print(L"Allocate runtime globals %r\n", res);
		return res;
	}

	//
	// Register for the virtual address change event
	//
	res = gBS->CreateEventEx(
		EVT_NOTIFY_SIGNAL,
		TPL_NOTIFY,
		BmlVirtualNotifyEvent,
		NULL,
		&gEfiEventVirtualAddressChangeGuid,
		&mBmlVirtualAddrChangeEvent
		);

   if (EFI_ERROR(res)) {
		Print(L"Register notify %r\n", res);
		return res;
   }

	orgSetVariable = gST->RuntimeServices->SetVariable;
	gST->RuntimeServices->SetVariable = BmlSetVaribale;

    // select boot next
    {
        UINT16  DcsBootNum = 0x0DC5B;
        UINTN               len;
        UINT32              attr;
        CHAR16*             tmp = NULL;
        res = EfiGetVar(L"BootDC5B", &gEfiGlobalVariableGuid, &tmp, &len, &attr);
        if (EFI_ERROR(res)) {
            InitFS();
            res = BootMenuItemCreate(L"BootDC5B", sDcsBootEfiDesc, gFileRootHandle, sDcsBootEfi, TRUE);
            res = BootOrderInsert(L"BootOrder", 0, 0x0DC5B);
        }
        res = EfiSetVar(L"BootNext", &gEfiGlobalVariableGuid, &DcsBootNum, sizeof(DcsBootNum), EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS);
        MEM_FREE(tmp);
    }

    // Prepare BootDC5B
	return EFI_SUCCESS;
}