Annotation of truecrypt/boot/windows/bootconsoleio.cpp, revision 1.1.1.6

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.