VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Boot/Windows/IntFilter.cpp
blob: cd0ea574e742e6cf1441fddbd52da32cd6076689 (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
 Copyright (c) 2008 TrueCrypt Developers Association. All rights reserved.

 Governed by the TrueCrypt License 3.0 the full text of which is contained in
 the file License.txt included in TrueCrypt binary and source code distribution
 packages.
*/

#include "Platform.h"
#include "BootMemory.h"
#include "BootConfig.h"
#include "BootConsoleIo.h"
#include "BootDebug.h"
#include "BootDefs.h"
#include "BootDiskIo.h"
#include "BootEncryptedIo.h"
#include "BootStrings.h"
#include "IntFilter.h"

static uint32 OriginalInt13Handler;
static uint32 OriginalInt15Handler;

static Registers IntRegisters;


bool Int13Filter ()
{
	CheckStack();

	Registers regs;
	memcpy (&regs, &IntRegisters, sizeof (regs));
	__asm sti

	static int ReEntryCount = -1;
	++ReEntryCount;

	byte function = (byte) (regs.AX >> 8);

#ifdef TC_TRACE_INT13
	DisableScreenOutput();

	PrintHex (function);

	Print (" EN:"); Print (ReEntryCount);
	Print (" SS:"); PrintHex (regs.SS);

	uint16 spdbg;
	__asm mov spdbg, sp
	PrintChar (' ');
	PrintHex (spdbg);
	PrintChar ('<'); PrintHex (TC_BOOT_LOADER_STACK_TOP);

#endif

	bool passOriginalRequest = true;

	switch (function)
	{
	case 0x2: // Read sectors
	case 0x3: // Write sectors
		{
			byte drive = (byte) regs.DX;

			ChsAddress chs;
			chs.Cylinder = ((regs.CX << 2) & 0x300) | (regs.CX >> 8);
			chs.Head = regs.DX >> 8;
			chs.Sector = regs.CX & 0x3f;

			byte sectorCount = (byte) regs.AX;

#ifdef TC_TRACE_INT13
			PrintVal (": Drive", drive - TC_FIRST_BIOS_DRIVE, false);
			Print (" Chs: "); Print (chs);
#endif

			uint64 sector;
			if (drive == BootDrive)
			{
				if (!BootDriveGeometryValid)
					TC_THROW_FATAL_EXCEPTION;

				ChsToLba (BootDriveGeometry, chs, sector);
#ifdef TC_TRACE_INT13
				PrintVal (" Sec", sector.LowPart, false);
#endif
			}

#ifdef TC_TRACE_INT13
			PrintVal (" Count", sectorCount, false);
			Print (" Buf: "); PrintHex (regs.ES); PrintChar (':'); PrintHex (regs.BX);
			PrintEndl();
#endif

			if (ReEntryCount == 0 && drive == EncryptedVirtualPartition.Drive)
			{
				BiosResult result;
				
				if (function == 0x3)
					result = WriteEncryptedSectors (regs.ES, regs.BX, drive, sector, sectorCount);
				else
					result = ReadEncryptedSectors (regs.ES, regs.BX, drive, sector, sectorCount);

				__asm cli

				memcpy (&IntRegisters, &regs, sizeof (regs));
				IntRegisters.AX = (uint16) result << 8;

				if (result == BiosResultSuccess)
				{
					IntRegisters.AX |= sectorCount;
					IntRegisters.Flags &= ~TC_X86_CARRY_FLAG;
				}
				else
					IntRegisters.Flags |= TC_X86_CARRY_FLAG;

				passOriginalRequest = false;
			}
		}
		break;

	case 0x42: // Read sectors LBA
	case 0x43: // Write sectors LBA
		{
			byte drive = (byte) regs.DX;
			
			BiosLbaPacket lba;
			CopyMemory (regs.DS, regs.SI, (byte *) &lba, sizeof (lba));

#ifdef TC_TRACE_INT13
			PrintVal (": Drive", drive - TC_FIRST_BIOS_DRIVE, false);
			PrintVal (" Sec", lba.Sector.LowPart, false);
			PrintVal (" Count", lba.SectorCount, false);
			PrintVal (" Buf", lba.Buffer, false, true);
			PrintEndl();
#endif

			if (ReEntryCount == 0 && drive == EncryptedVirtualPartition.Drive)
			{
				BiosResult result;
				
				uint16 segment = (uint16) (lba.Buffer >> 16);
				uint16 offset = (uint16) lba.Buffer;

				if (function == 0x43)
					result = WriteEncryptedSectors (segment, offset, drive, lba.Sector, lba.SectorCount);
				else
					result = ReadEncryptedSectors (segment, offset, drive, lba.Sector, lba.SectorCount);

				__asm cli

				memcpy (&IntRegisters, &regs, sizeof (regs));
				IntRegisters.AX = (IntRegisters.AX & 0xff) | ((uint16) result << 8);

				if (result == BiosResultSuccess)
					IntRegisters.Flags &= ~TC_X86_CARRY_FLAG;
				else
					IntRegisters.Flags |= TC_X86_CARRY_FLAG;

				passOriginalRequest = false;
			}
		}
		break;

	default:
#ifdef TC_TRACE_INT13
		PrintEndl();
#endif
		break;
	}

#ifdef TC_TRACE_INT13
	EnableScreenOutput();
#endif
	--ReEntryCount;

	return passOriginalRequest;
}


#define TC_MAX_MEMORY_MAP_SIZE 80

BiosMemoryMapEntry BiosMemoryMap[TC_MAX_MEMORY_MAP_SIZE];
static size_t BiosMemoryMapSize;


static void CreateBootLoaderMemoryMapEntry (BiosMemoryMapEntry *newMapEntry, uint32 bootLoaderStart)
{
	newMapEntry->Type = 0x2;
	newMapEntry->BaseAddress.HighPart = 0;
	newMapEntry->BaseAddress.LowPart = bootLoaderStart;
	newMapEntry->Length.HighPart = 0;
	newMapEntry->Length.LowPart = TC_BOOT_MEMORY_REQUIRED * 1024UL;
}


static bool CreateNewBiosMemoryMap ()
{
	// Create a new BIOS memory map presenting the memory area of the loader as reserved

	BiosMemoryMapSize = 0;
	BiosMemoryMapEntry entry;
	BiosMemoryMapEntry *newMapEntry = BiosMemoryMap;

	const BiosMemoryMapEntry *mapEnd = BiosMemoryMap + TC_MAX_MEMORY_MAP_SIZE;

	uint64 bootLoaderStart;
	bootLoaderStart.HighPart = 0;

	uint16 codeSeg;
	__asm mov codeSeg, cs
	bootLoaderStart.LowPart = GetLinearAddress (codeSeg, 0);

	uint64 bootLoaderEnd;
	bootLoaderEnd.HighPart = 0;
	bootLoaderEnd.LowPart = bootLoaderStart.LowPart + TC_BOOT_MEMORY_REQUIRED * 1024UL;

	bool loaderEntryInserted = false;

	if (GetFirstBiosMemoryMapEntry (entry))
	{
		do
		{
			uint64 entryEnd = entry.BaseAddress + entry.Length;

			if (entry.Type == 0x1 && RegionsIntersect (bootLoaderStart, TC_BOOT_MEMORY_REQUIRED * 1024UL, entry.BaseAddress, entryEnd - 1))
			{
				// Free map entry covers the boot loader area

				if (entry.BaseAddress < bootLoaderStart)
				{
					// Create free entry below the boot loader area
					if (newMapEntry >= mapEnd)
						goto mapOverflow;

					*newMapEntry = entry;
					newMapEntry->Length = bootLoaderStart - entry.BaseAddress;
					++newMapEntry;
				}

				if (!loaderEntryInserted)
				{
					// Create reserved entry for the boot loader if it has not been done yet
					if (newMapEntry >= mapEnd)
						goto mapOverflow;

					CreateBootLoaderMemoryMapEntry (newMapEntry, bootLoaderStart.LowPart);
					++newMapEntry;
					loaderEntryInserted = true;
				}

				if (bootLoaderEnd < entryEnd)
				{
					// Create free entry above the boot loader area
					if (newMapEntry >= mapEnd)
						goto mapOverflow;

					newMapEntry->Type = 0x1;
					newMapEntry->BaseAddress = bootLoaderEnd;
					newMapEntry->Length = entryEnd - bootLoaderEnd;
					++newMapEntry;
				}
			}
			else
			{
				if (newMapEntry >= mapEnd)
					goto mapOverflow;

				if (!loaderEntryInserted && entry.BaseAddress > bootLoaderStart)
				{
					// Create reserved entry for the boot loader if it has not been done yet
					CreateBootLoaderMemoryMapEntry (newMapEntry, bootLoaderStart.LowPart);
					++newMapEntry;
					loaderEntryInserted = true;
				}

				// Copy map entry
				*newMapEntry++ = entry;
			}

		} while (GetNextBiosMemoryMapEntry (entry));
	}

	BiosMemoryMapSize = newMapEntry - BiosMemoryMap;
	return true;

mapOverflow:
	size_t overSize = 0;
	while (GetNextBiosMemoryMapEntry (entry))
	{
		++overSize;
	}

	PrintErrorNoEndl ("MMP:");
	Print (overSize);
	PrintEndl();

	return false;
}


bool Int15Filter ()
{
	CheckStack();

#ifdef TC_TRACE_INT15
	DisableScreenOutput();

	Print ("15-");
	PrintHex (IntRegisters.AX);

	Print (" SS:"); PrintHex (IntRegisters.SS);

	uint16 spdbg;
	__asm mov spdbg, sp
	PrintChar (' ');
	PrintHex (spdbg);
	PrintChar ('<'); PrintHex (TC_BOOT_LOADER_STACK_TOP);

	Print (" EAX:"); PrintHex (IntRegisters.EAX);
	Print (" EBX:"); PrintHex (IntRegisters.EBX);
	Print (" ECX:"); PrintHex (IntRegisters.ECX);
	Print (" EDX:"); PrintHex (IntRegisters.EDX);
	Print (" DI:"); PrintHex (IntRegisters.DI);
	PrintEndl();

#endif

	if (IntRegisters.EBX >= BiosMemoryMapSize)
	{
		IntRegisters.Flags |= TC_X86_CARRY_FLAG;
		IntRegisters.EBX = 0;
		IntRegisters.AX = -1;
	}
	else
	{
		CopyMemory ((byte *) &BiosMemoryMap[IntRegisters.EBX], IntRegisters.ES, IntRegisters.DI, sizeof (BiosMemoryMap[0]));

		IntRegisters.Flags &= ~TC_X86_CARRY_FLAG;
		IntRegisters.EAX = 0x534D4150UL;

		++IntRegisters.EBX;
		if (IntRegisters.EBX >= BiosMemoryMapSize)
			IntRegisters.EBX = 0;

		IntRegisters.ECX = sizeof (BiosMemoryMap[0]);
	}

	if (IntRegisters.EBX == 0 && !(BootSectorFlags & TC_BOOT_CFG_FLAG_WINDOWS_VISTA_OR_LATER))
	{
		// Uninstall filter when the modified map has been issued three times to prevent
		// problems with hardware drivers on some notebooks running Windows XP.

		static int CompleteMapIssueCount = 0;
		if (++CompleteMapIssueCount >= 3)
		{
			__asm
			{
				cli
				push es

				lea si, OriginalInt15Handler
				xor ax, ax
				mov es, ax
				mov di, 0x15 * 4

				mov ax, [si]
				mov es:[di], ax
				mov ax, [si + 2]
				mov es:[di + 2], ax

				pop es
				sti
			}
		}
	}

#ifdef TC_TRACE_INT15
	BiosMemoryMapEntry entry;
	CopyMemory (IntRegisters.ES, IntRegisters.DI, (byte *) &entry, sizeof (entry));
	PrintHex (entry.Type); PrintChar (' ');
	PrintHex (entry.BaseAddress); PrintChar (' ');
	PrintHex (entry.Length); PrintChar (' ');
	PrintHex (entry.BaseAddress + entry.Length); PrintEndl();

	Print ("EAX:"); PrintHex (IntRegisters.EAX);
	Print (" EBX:"); PrintHex (IntRegisters.EBX);
	Print (" ECX:"); PrintHex (IntRegisters.ECX);
	Print (" EDX:"); PrintHex (IntRegisters.EDX);
	Print (" DI:"); PrintHex (IntRegisters.DI);
	Print (" FL:"); PrintHex (IntRegisters.Flags);
	PrintEndl (2);
#endif

#ifdef TC_TRACE_INT15
	EnableScreenOutput();
#endif
	return false;
}


void IntFilterEntry ()
{
	// No automatic variables should be used in this scope as SS may change
	static uint16 OrigStackPointer;
	static uint16 OrigStackSegment;

	__asm
	{
		pushf
		pushad

		cli
		mov cs:IntRegisters.DI, di

		lea di, cs:IntRegisters.EAX
		TC_ASM_EMIT4 (66,2E,89,05) // mov [cs:di], eax
		lea di, cs:IntRegisters.EBX
		TC_ASM_EMIT4 (66,2E,89,1D) // mov [cs:di], ebx
		lea di, cs:IntRegisters.ECX
		TC_ASM_EMIT4 (66,2E,89,0D) // mov [cs:di], ecx
		lea di, cs:IntRegisters.EDX
		TC_ASM_EMIT4 (66,2E,89,15) // mov [cs:di], edx

		mov ax, [bp + 8]
		mov cs:IntRegisters.Flags, ax

		mov cs:IntRegisters.SI, si
		mov si, [bp + 2] // Int number

		mov cs:IntRegisters.DS, ds
		mov cs:IntRegisters.ES, es
		mov cs:IntRegisters.SS, ss

		// Compiler assumes SS == DS - use our stack if this condition is not met
		mov ax, ss
		mov bx, cs
		cmp ax, bx
		jz stack_ok

		mov cs:OrigStackPointer, sp
		mov cs:OrigStackSegment, ss
		mov ax, cs
		mov ss, ax
		mov sp, TC_BOOT_LOADER_STACK_TOP

	stack_ok:
		// DS = CS
		push ds
		push es
		mov ax, cs
		mov ds, ax
		mov es, ax

		push si // Int number

		// Filter request
		cmp si, 0x15
		je filter15
		cmp si, 0x13
		jne $

		call Int13Filter
		jmp s0

	filter15:
		call Int15Filter

	s0:
		pop si // Int number
		pop es
		pop ds

		// Restore original SS:SP if our stack is empty
		cli
		mov bx, TC_BOOT_LOADER_STACK_TOP
		cmp bx, sp
		jnz stack_in_use

		mov ss, cs:OrigStackSegment
		mov sp, cs:OrigStackPointer
	stack_in_use:

		test ax, ax // passOriginalRequest
		jnz pass_request

		// Return results of filtered request
		popad
		popf
		mov ax, cs:IntRegisters.Flags
		mov [bp + 8], ax
		leave

		lea di, cs:IntRegisters.EAX
		TC_ASM_EMIT4 (66,2E,8B,05) // mov eax, [cs:di]
		lea di, cs:IntRegisters.EBX
		TC_ASM_EMIT4 (66,2E,8B,1D) // mov ebx, [cs:di]
		lea di, cs:IntRegisters.ECX
		TC_ASM_EMIT4 (66,2E,8B,0D) // mov ecx, [cs:di]
		lea di, cs:IntRegisters.EDX
		TC_ASM_EMIT4 (66,2E,8B,15) // mov edx, [cs:di]

		mov di, cs:IntRegisters.DI
		mov si, cs:IntRegisters.SI
		mov es, cs:IntRegisters.ES
		mov ds, cs:IntRegisters.DS

		sti
		add sp, 2
		iret

		// Pass original request
	pass_request:
		sti
		cmp si, 0x15
		je pass15
		cmp si, 0x13
		jne $

		popad
		popf
		leave
		add sp, 2
		jmp cs:OriginalInt13Handler	

	pass15:
		popad
		popf
		leave
		add sp, 2
		jmp cs:OriginalInt15Handler
	}
}


void Int13FilterEntry ()
{
	__asm
	{
		leave
		push 0x13
		jmp IntFilterEntry
	}
}


static void Int15FilterEntry ()
{
	__asm
	{
		pushf
		cmp ax, 0xe820 // Get system memory map
		je filter
		
		popf
		leave
		jmp cs:OriginalInt15Handler

	filter:
		leave
		push 0x15
		jmp IntFilterEntry
	}
}


bool InstallInterruptFilters ()
{

#ifndef TC_WINDOWS_BOOT_RESCUE_DISK_MODE

	// If the filters have already been installed, it usually indicates stack corruption
	// and a consequent reentry of this routine without a system reset.

	uint32 currentInt13Handler;
	CopyMemory (0, 0x13 * 4, &currentInt13Handler, sizeof (currentInt13Handler));

	if (currentInt13Handler == (uint32) Int13FilterEntry)
	{
		PrintError ("Memory corrupted");
		Print (TC_BOOT_STR_UPGRADE_BIOS);

		GetKeyboardChar();
		return true;
	}

#endif

	if (!CreateNewBiosMemoryMap())
		return false;

	__asm
	{
		cli
		push es

		// Save original INT 13 handler
		xor ax, ax
		mov es, ax
		
		mov si, 0x13 * 4
		lea di, OriginalInt13Handler

		mov ax, es:[si]
		mov [di], ax
		mov ax, es:[si + 2]
		mov [di + 2], ax
		
		// Install INT 13 filter
		lea ax, Int13FilterEntry
		mov es:[si], ax
		mov es:[si + 2], cs

		// Save original INT 15 handler
		mov si, 0x15 * 4	
		lea di, OriginalInt15Handler

		mov ax, es:[si]
		mov [di], ax
		mov ax, es:[si + 2]
		mov [di + 2], ax

		// Install INT 15 filter
		lea ax, Int15FilterEntry
		mov es:[si], ax
		mov es:[si + 2], cs

		// If the BIOS does not support system memory map (INT15 0xe820),
		// set amount of available memory to CS:0000 - 0:0000
		cmp BiosMemoryMapSize, 1
		jg mem_map_ok
		mov ax, cs
		shr ax, 10 - 4		// CS * 16 / 1024
		mov es:[0x413], ax	// = KBytes available
	mem_map_ok:

		pop es
		sti
	}

	return true;
}