VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/DcsInfo/DcsInfo.c
blob: 70f235e143f46a9ed9cc815b6db2973f3a9380a0 (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
/** @file
  This is DCS platform information application

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 <Uefi.h>
#include <Library/CommonLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/DevicePathLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/PrintLib.h>
#include <Guid/GlobalVariable.h>
#include <Library/PasswordLib.h>
#include <Library/GraphLib.h>

#ifdef _M_X64
#define ARCH_NAME "X64"
#else
#define ARCH_NAME "IA32"
#endif
CHAR8 Temp[1024];
CHAR8 StrBuffer[1024];
UINTN gXmlTabs = 0;

UINTN
XmlOutTab() {
	UINTN len;
	UINTN i = gXmlTabs;
	CHAR8*   pos = (CHAR8*)StrBuffer;
	INTN     remains = sizeof(StrBuffer) - 1;
	while (i > 0 && remains > 0) {
		*pos = ' ';
		remains--;
		i--;
		pos++;
	}
	len = sizeof(StrBuffer) - remains - 1;
	return len;
}

UINTN
XmlTag(
	IN EFI_FILE            *infoFileTxt,
	IN CONST CHAR8         *tag,
	IN BOOLEAN             closeTag,
	IN CONST CHAR8         *value,
	...
	) {
	VA_LIST  args;
	UINTN    len = XmlOutTab();
	CHAR8*   pos = (CHAR8*)StrBuffer + len;
	CHAR8*   attrFormat = NULL;
	INTN     remains = sizeof(StrBuffer) - 1 - len;
	if (infoFileTxt == NULL) return 0;
	VA_START(args, value);
	len = AsciiSPrint(pos, remains, "<%a", tag);
	remains -= len;
	pos += len;
	if ((attrFormat = VA_ARG(args, CHAR8 *)) != NULL) {
		len = AsciiVSPrint(pos, remains, attrFormat, args);
		remains -= len;
		pos += len;
	}
	VA_END(args);
	if (closeTag) {
		if (value == NULL) {
			len = AsciiSPrint(pos, remains, "/>\n");
			remains -= len;
			pos += len;
		}
		else {
			len = AsciiSPrint(pos, remains, ">%a</%a>\n", value, tag);
			remains -= len;
			pos += len;
		}
	}	else {
		if (value == NULL) {
			len = AsciiSPrint(pos, remains, ">");
			remains -= len;
			pos += len;
		}
		else {
			len = AsciiSPrint(pos, remains, ">%a", value, tag);
			remains -= len;
			pos += len;
		}
	}
	len = sizeof(StrBuffer) - remains - 1;
	infoFileTxt->Write(infoFileTxt, &len, StrBuffer);
	return len;
}

UINTN
XmlStartTag(
	IN EFI_FILE            *infoFileTxt,
	IN CONST CHAR8         *tag) 
{
	UINTN    len = XmlOutTab();
	CHAR8*   pos = (CHAR8*)StrBuffer + len;
	INTN     remains = sizeof(StrBuffer) - 1 - len;
	gXmlTabs += remains > 0 ? 1 : 0;
	len = AsciiSPrint(pos, remains, "<%a>\n", tag);
	remains -= len;
	pos += len;
	len = sizeof(StrBuffer) - remains - 1;
	infoFileTxt->Write(infoFileTxt, &len, StrBuffer);

	return len;
}

UINTN
XmlEndTag(
	IN EFI_FILE            *infoFileTxt,
	IN CONST CHAR8         *tag
	)
{
	UINTN    len;
	CHAR8*   pos;
	INTN     remains;
	gXmlTabs -= gXmlTabs > 0 ? 1 : 0;
	len = XmlOutTab();
	pos = (CHAR8*)StrBuffer + len;
	remains = sizeof(StrBuffer) - 1 - len;

	if (infoFileTxt == NULL) return 0;
	len = AsciiSPrint(pos, remains, "</%a>\n", tag);
	remains -= len;
	pos += len;
	len = sizeof(StrBuffer) - remains - 1;
	infoFileTxt->Write(infoFileTxt, &len, StrBuffer);
	return len;
}


UINTN
XmlEndTagPrint(
	IN EFI_FILE            *infoFileTxt,
	IN CONST CHAR8         *tag,
	IN CONST CHAR8         *formatValue,
	...
	)
{
	VA_LIST  args;
	UINTN    len = 0;
	CHAR8*   pos = (CHAR8*)StrBuffer + len;
	INTN     remains = sizeof(StrBuffer) - 1 - len;
	if (infoFileTxt == NULL) return 0;
	VA_START(args, formatValue);
	if (formatValue != NULL) {
		len = AsciiVSPrint(pos, remains, formatValue, args);
		remains -= len;
		pos += len;
	}
	VA_END(args);
	len = AsciiSPrint(pos, remains, "</%a>\n", tag);
	remains -= len;
	pos += len;
	len = sizeof(StrBuffer) - remains -1;
	infoFileTxt->Write(infoFileTxt, &len, StrBuffer);
	return len;
}

EFI_FILE            *fInfo;

VOID
InfoEFI() {
	XmlStartTag(fInfo, "EFI");
	XmlTag(fInfo, "Version", FALSE, NULL, NULL);
	XmlEndTagPrint(fInfo, "Version", "%d.%d", gST->Hdr.Revision >> 16, gST->Hdr.Revision & 0xFFFF);
	XmlTag(fInfo, "Vendor", FALSE, NULL, NULL);
	XmlEndTagPrint(fInfo, "Vendor", "%s", gST->FirmwareVendor);
	XmlTag(fInfo, "Revision", FALSE, NULL, NULL);
	XmlEndTagPrint(fInfo, "Revision", "0x0%x", gST->FirmwareRevision);
	XmlTag(fInfo, "Architecture", TRUE, ARCH_NAME, NULL);
	XmlEndTag(fInfo, "EFI");
}

VOID
InfoSystem() {
	EFI_STATUS res;
	res = SMBIOSGetSerials();
	if (!EFI_ERROR(res)) {
		//		XmlTag(info, "System",FALSE, NULL, NULL);
		XmlStartTag(fInfo, "System");
		XmlTag(fInfo, "Manufacture", TRUE, gSmbSystemManufacture, NULL);
		XmlTag(fInfo, "Model", TRUE, gSmbSystemModel, NULL);
		XmlTag(fInfo, "Version", TRUE, gSmbSystemVersion, NULL);
		XmlEndTag(fInfo, "System");
		XmlStartTag(fInfo, "BIOS");
		XmlTag(fInfo, "Vendor", TRUE, gSmbBiosVendor, NULL);
		XmlTag(fInfo, "Version", TRUE, gSmbBiosVersion, NULL);
		XmlTag(fInfo, "Date", TRUE, gSmbBiosDate, NULL);
		XmlEndTag(fInfo, "BIOS");
	}
}

VOID
InfoTcg() {
	InitTcg();
	XmlTag(fInfo, "TPM12", TRUE, NULL, " count=\"%d\"", gTcgCount, NULL);
	XmlTag(fInfo, "TPM20", TRUE, NULL, " count=\"%d\"", gTcg2Count, NULL);
}

VOID
InfoBlockDevices() {
    UINTN i;
    XmlTag(fInfo, "BlockDevices", FALSE, NULL, " count=\"%d\"", gBIOCount, NULL);
    FileAsciiPrint(fInfo, "\n");
    gXmlTabs++;
    for (i = 0; i < gBIOCount; ++i) {
        EFI_BLOCK_IO_PROTOCOL *bio;
        bio = EfiGetBlockIO(gBIOHandles[i]);
        if (bio != NULL && bio->Media != NULL) {
            XmlTag(fInfo, "BlockDevice", TRUE, NULL,
                " index=\"%d\" logical=\"%d\" block_size=\"%d\" revision=\"%llx\" read_only=\"%d\" last_block=\"%lld\"", i,
                bio->Media->LogicalPartition, bio->Media->BlockSize, bio->Revision,
                bio->Media->ReadOnly,
                bio->Media->LastBlock, NULL);
        }
    }
    XmlEndTag(fInfo, "BlockDevices");
}

VOID
InfoUsbDevices() {
	InitUsb();
	XmlTag(fInfo, "UsbDevices", TRUE, NULL, " count=\"%d\"", gUSBCount, NULL);
}

VOID
InfoTouch() {
	EFI_STATUS res;
	UINTN i;
	InitTouch();
	XmlTag(fInfo, "TouchDevices", FALSE, NULL, " count=\"%d\"", gTouchCount, NULL);
	FileAsciiPrint(fInfo, "\n");
	gXmlTabs++;
	for (i = 0; i < gTouchCount; ++i) {
		EFI_ABSOLUTE_POINTER_PROTOCOL *aio;
		res = TouchGetIO(gTouchHandles[i], &aio);
		if (!EFI_ERROR(res)) {
			XmlTag(fInfo, "TouchDevice", TRUE, NULL,
				" index=\"%d\" minx=\"%d\" miny=\"%d\" minz=\"%d\" maxx=\"%d\" maxy=\"%d\" maxz=\"%d\" attr=\"0x0%x\"", i,
				aio->Mode->AbsoluteMinX, aio->Mode->AbsoluteMinY, aio->Mode->AbsoluteMinZ,
				aio->Mode->AbsoluteMaxX, aio->Mode->AbsoluteMaxY, aio->Mode->AbsoluteMaxZ,
				aio->Mode->Attributes, NULL);
		}
	}
	XmlEndTag(fInfo, "TouchDevices");
}

VOID
InfoGraph() {
	EFI_STATUS res;
	UINTN i, j;
	InitGraph();
	XmlTag(fInfo, "GraphDevices", FALSE, NULL, " count=\"%d\"", gGraphCount, NULL);
	FileAsciiPrint(fInfo, "\n");
	gXmlTabs++;
	for (i = 0; i < gGraphCount; ++i) {
		EFI_GRAPHICS_OUTPUT_PROTOCOL *gio;
		res = GraphGetIO(gGraphHandles[i], &gio);
		if (!EFI_ERROR(res)) {
			XmlTag(fInfo, "GraphDevice", FALSE, NULL,
				" index=\"%d\" modes=\"%d\" H=\"%d\" V=\"%d\"", i,
				gio->Mode->MaxMode, gio->Mode->Info->HorizontalResolution, gio->Mode->Info->VerticalResolution,
				NULL);
			FileAsciiPrint(fInfo, "\n");
			gXmlTabs++;
			for (j = 0; j < gio->Mode->MaxMode; ++j) {
				EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *mode;
				UINTN sz = sizeof(mode);
				res = gio->QueryMode(gio, (UINT32)j, &sz, &mode);
				if (!EFI_ERROR(res)) {
					XmlTag(fInfo, "GraphMode", TRUE, NULL,
						" index=\"%d\" H=\"%d\" V=\"%d\"", j,
						mode->HorizontalResolution, mode->VerticalResolution,
						NULL);
				}
			}
			XmlEndTag(fInfo, "GraphDevice");
		}
	}
	XmlEndTag(fInfo, "GraphDevices");
}

VOID
InfoBluetooth() {
	InitBluetooth();
	XmlTag(fInfo, "BluetoothIo", TRUE, NULL, " count=\"%d\"", gBluetoothIoCount, NULL);
	XmlTag(fInfo, "BluetoothConfig", TRUE, NULL, " count=\"%d\"", gBluetoothConfigCount, NULL);
	XmlTag(fInfo, "BluetoothHC", TRUE, NULL, " count=\"%d\"", gBluetoothHcCount, NULL);
}

/**
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
DcsInfoMain(
   IN EFI_HANDLE        ImageHandle,
   IN EFI_SYSTEM_TABLE  *SystemTable
   )
{
   EFI_STATUS          res;
//	EFI_INPUT_KEY       key;
	InitBio();
   res = InitFS();
   if (EFI_ERROR(res)) {
      ERR_PRINT(L"InitFS %r\n", res);
		return res;
   }
	res = FileOpen(NULL, L"EFI\\VeraCrypt\\PlatformInfo", &fInfo, EFI_FILE_MODE_READ | EFI_FILE_MODE_CREATE | EFI_FILE_MODE_WRITE, 0);
	if (EFI_ERROR(res)) {
		ERR_PRINT(L"PlatformInfo create %r\n", res);
		return res;
	}
	FileAsciiPrint(fInfo, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
	XmlStartTag(fInfo, "PlatformInfo");
	// General info
	InfoEFI();
	InfoSystem();

	// Devices info
	InfoTcg();
	InfoBlockDevices();
	InfoUsbDevices();
	InfoTouch();
	InfoGraph();
	InfoBluetooth();
	XmlEndTag(fInfo, "PlatformInfo");

	FileClose(fInfo);
	return EFI_SUCCESS;
}