"Fossies" - the Fresh Open Source Software Archive

Member "src/Boot/Windows/BootConsoleIo.cpp" (10 Oct 2018, 4972 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "BootConsoleIo.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.21_Source_vs_1.22_Source.

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