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

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

unix.superglobalmegacorp.com

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