|
|
1.1 root 1: /*
1.1.1.9 root 2: Copyright (c) 2008-2009 TrueCrypt Developers Association. All rights reserved.
1.1 root 3:
1.1.1.10! root 4: Governed by the TrueCrypt License 3.0 the full text of which is contained in
1.1.1.9 root 5: the file License.txt included in TrueCrypt binary and source code distribution
6: packages.
1.1 root 7: */
8:
9: #include "Platform.h"
10: #include "Bios.h"
11: #include "BootConsoleIo.h"
12: #include "BootDebug.h"
1.1.1.8 root 13: #include "BootStrings.h"
1.1 root 14:
15:
16: static int ScreenOutputDisabled = 0;
17:
18: void DisableScreenOutput ()
19: {
20: ++ScreenOutputDisabled;
21: }
22:
23:
24: void EnableScreenOutput ()
25: {
26: --ScreenOutputDisabled;
27: }
28:
29:
30: void PrintChar (char c)
31: {
1.1.1.3 root 32: #ifdef TC_BOOT_TRACING_ENABLED
1.1 root 33: WriteDebugPort (c);
34: #endif
35:
36: if (ScreenOutputDisabled)
37: return;
38:
39: __asm
40: {
1.1.1.5 root 41: mov bx, 7
1.1 root 42: mov al, c
43: mov ah, 0xe
44: int 0x10
45: }
46: }
47:
48:
1.1.1.6 root 49: void PrintCharAtCursor (char c)
1.1 root 50: {
1.1.1.6 root 51: if (ScreenOutputDisabled)
52: return;
53:
1.1 root 54: __asm
55: {
1.1.1.5 root 56: mov bx, 7
1.1 root 57: mov al, c
58: mov cx, 1
59: mov ah, 0xa
60: int 0x10
61: }
62: }
63:
64:
65: void Print (const char *str)
66: {
67: char c;
68: while (c = *str++)
69: PrintChar (c);
70: }
71:
72:
73: void Print (uint32 number)
74: {
75: char str[12];
76: int pos = 0;
77: while (number >= 10)
78: {
1.1.1.8 root 79: str[pos++] = (char) (number % 10) + '0';
1.1 root 80: number /= 10;
81: }
1.1.1.8 root 82: str[pos] = (char) (number % 10) + '0';
1.1 root 83:
84: while (pos >= 0)
85: PrintChar (str[pos--]);
86: }
87:
88:
89: void Print (const uint64 &number)
90: {
91: if (number.HighPart == 0)
92: Print (number.LowPart);
93: else
94: PrintHex (number);
95: }
96:
97:
98: void PrintHex (byte b)
99: {
100: PrintChar (((b >> 4) >= 0xA ? 'A' - 0xA : '0') + (b >> 4));
101: PrintChar (((b & 0xF) >= 0xA ? 'A' - 0xA : '0') + (b & 0xF));
102: }
103:
104:
105: void PrintHex (uint16 data)
106: {
107: PrintHex (byte (data >> 8));
108: PrintHex (byte (data));
109: }
110:
111:
112: void PrintHex (uint32 data)
113: {
114: PrintHex (uint16 (data >> 16));
115: PrintHex (uint16 (data));
116: }
117:
118:
119: void PrintHex (const uint64 &data)
120: {
121: PrintHex (data.HighPart);
122: PrintHex (data.LowPart);
123: }
124:
125: void PrintRepeatedChar (char c, int n)
126: {
127: while (n-- > 0)
128: PrintChar (c);
129: }
130:
131:
132: void PrintEndl ()
133: {
134: Print ("\r\n");
135: }
136:
137:
138: void PrintEndl (int cnt)
139: {
140: while (cnt-- > 0)
141: PrintEndl ();
142: }
143:
144:
145: void Beep ()
146: {
147: PrintChar (7);
148: }
149:
150:
1.1.1.5 root 151: void InitVideoMode ()
152: {
1.1.1.6 root 153: if (ScreenOutputDisabled)
154: return;
155:
1.1.1.5 root 156: __asm
157: {
158: // Text mode 80x25
159: mov ax, 3
160: int 0x10
161:
162: // Page 0
163: mov ax, 0x500
164: int 0x10
165: }
166: }
167:
168:
1.1 root 169: void ClearScreen ()
170: {
1.1.1.6 root 171: if (ScreenOutputDisabled)
172: return;
173:
1.1 root 174: __asm
175: {
1.1.1.5 root 176: // White text on black
1.1 root 177: mov bh, 7
178: xor cx, cx
179: mov dx, 0x184f
180: mov ax, 0x600
181: int 0x10
182:
1.1.1.5 root 183: // Cursor at 0,0
1.1 root 184: xor bh, bh
185: xor dx, dx
186: mov ah, 2
187: int 0x10
188: }
189: }
190:
191:
192: void PrintBackspace ()
193: {
194: PrintChar (TC_BIOS_CHAR_BACKSPACE);
1.1.1.6 root 195: PrintCharAtCursor (' ');
1.1 root 196: }
197:
198:
1.1.1.8 root 199: void PrintError (const char *message)
1.1 root 200: {
1.1.1.8 root 201: Print (TC_BOOT_STR_ERROR);
1.1 root 202: Print (message);
1.1.1.8 root 203: PrintEndl();
204: Beep();
205: }
206:
1.1 root 207:
1.1.1.8 root 208: void PrintErrorNoEndl (const char *message)
209: {
210: Print (TC_BOOT_STR_ERROR);
211: Print (message);
212: Beep();
1.1 root 213: }
214:
215:
216: byte GetShiftFlags ()
217: {
218: byte flags;
219: __asm
220: {
221: mov ah, 2
222: int 0x16
223: mov flags, al
224: }
225:
226: return flags;
227: }
228:
229:
1.1.1.8 root 230: byte GetKeyboardChar ()
231: {
232: return GetKeyboardChar (nullptr);
233: }
234:
235:
1.1 root 236: byte GetKeyboardChar (byte *scanCode)
237: {
1.1.1.8 root 238: // Work around potential BIOS bugs (Windows boot manager polls the keystroke buffer)
1.1.1.4 root 239: while (!IsKeyboardCharAvailable());
240:
1.1 root 241: byte asciiCode;
242: byte scan;
243: __asm
244: {
245: mov ah, 0
246: int 0x16
247: mov asciiCode, al
248: mov scan, ah
249: }
250:
251: if (scanCode)
252: *scanCode = scan;
253:
254: return asciiCode;
255: }
256:
257:
258: bool IsKeyboardCharAvailable ()
259: {
260: bool available = false;
261: __asm
262: {
263: mov ah, 1
264: int 0x16
265: jz not_avail
266: mov available, true
267: not_avail:
268: }
269:
270: return available;
271: }
272:
273:
1.1.1.5 root 274: bool EscKeyPressed ()
275: {
276: if (IsKeyboardCharAvailable ())
277: {
278: byte keyScanCode;
279: GetKeyboardChar (&keyScanCode);
280: return keyScanCode == TC_BIOS_KEY_ESC;
281: }
282:
283: return false;
284: }
285:
286:
1.1.1.2 root 287: void ClearBiosKeystrokeBuffer ()
288: {
289: __asm
290: {
291: push es
292: xor ax, ax
293: mov es, ax
294: mov di, 0x41e
295: mov cx, 32
296: cld
297: rep stosb
298: pop es
299: }
300: }
301:
302:
1.1 root 303: bool IsPrintable (char c)
304: {
305: return c >= ' ' && c <= '~';
306: }
307:
308:
309: int GetString (char *buffer, size_t bufferSize)
310: {
311: byte c;
312: byte scanCode;
313: size_t pos = 0;
314:
315: while (pos < bufferSize)
316: {
317: c = GetKeyboardChar (&scanCode);
318:
319: if (scanCode == TC_BIOS_KEY_ENTER)
320: break;
321:
322: if (scanCode == TC_BIOS_KEY_ESC)
323: return 0;
324:
325: buffer[pos++] = c;
326: PrintChar (IsPrintable (c) ? c : ' ');
327: }
328:
329: return pos;
330: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.