"Fossies" - the Fresh Open Source Software Archive

Member "src/Boot/Windows/BootSector.asm" (10 Oct 2018, 5196 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) Generic Assembler source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    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 .MODEL tiny
   14 .386
   15 _TEXT SEGMENT USE16
   16 
   17 INCLUDE BootDefs.i
   18 
   19 ORG 7C00h   ; Standard boot sector offset
   20 
   21 start:
   22     ; BIOS executes boot sector from 0:7C00 or 7C0:0000 (default CD boot loader address).
   23     ; Far jump to the next instruction sets IP to the standard offset 7C00.
   24     db 0EAh             ; jmp 0:main
   25     dw main, 0
   26 
   27 loader_name_msg:
   28     db ' VeraCrypt Boot Loader', 13, 10, 0
   29 
   30 main:
   31     cli
   32     xor ax, ax
   33     mov ds, ax
   34     mov ss, ax
   35     mov sp, 7C00h
   36     sti
   37 
   38     ; Display boot loader name
   39     test byte ptr [start + TC_BOOT_SECTOR_USER_CONFIG_OFFSET], TC_BOOT_USER_CFG_FLAG_SILENT_MODE
   40     jnz skip_loader_name_msg
   41 
   42     lea si, loader_name_msg
   43     call print
   44 skip_loader_name_msg:
   45 
   46     ; Determine boot loader segment
   47     mov ax, TC_BOOT_LOADER_SEGMENT
   48 
   49     ; Check available memory
   50     cmp word ptr [ds:413h], TC_BOOT_LOADER_SEGMENT / 1024 * 16 + TC_BOOT_MEMORY_REQUIRED
   51     jge memory_ok
   52 
   53     mov ax, TC_BOOT_LOADER_SEGMENT_LOW
   54 
   55     cmp word ptr [ds:413h], TC_BOOT_LOADER_SEGMENT_LOW / 1024 * 16 + TC_BOOT_MEMORY_REQUIRED
   56     jge memory_ok
   57 
   58     ; Insufficient memory
   59     mov ax, TC_BOOT_LOADER_LOWMEM_SEGMENT
   60 
   61 memory_ok:
   62     mov es, ax
   63 
   64     ; Clear BSS section
   65     xor al, al
   66     mov di, TC_COM_EXECUTABLE_OFFSET
   67     mov cx, TC_BOOT_MEMORY_REQUIRED * 1024 - TC_COM_EXECUTABLE_OFFSET - 1
   68     cld
   69     rep stosb
   70 
   71     mov ax, es
   72     sub ax, TC_BOOT_LOADER_DECOMPRESSOR_MEMORY_SIZE / 16    ; Decompressor segment
   73     mov es, ax
   74 
   75     ; Load decompressor
   76     mov cl, TC_BOOT_LOADER_DECOMPRESSOR_START_SECTOR
   77 retry_backup:
   78     mov al, TC_BOOT_LOADER_DECOMPRESSOR_SECTOR_COUNT
   79     mov bx, TC_COM_EXECUTABLE_OFFSET
   80     call read_sectors
   81 
   82     ; Decompressor checksum
   83     xor ebx, ebx
   84     mov si, TC_COM_EXECUTABLE_OFFSET
   85     mov cx, TC_BOOT_LOADER_DECOMPRESSOR_SECTOR_COUNT * TC_LB_SIZE
   86     call checksum
   87     push ebx
   88 
   89     ; Load compressed boot loader
   90     mov bx, TC_BOOT_LOADER_COMPRESSED_BUFFER_OFFSET
   91     mov cl, TC_BOOT_LOADER_START_SECTOR
   92     mov al, TC_MAX_BOOT_LOADER_SECTOR_COUNT
   93 
   94     test backup_loader_used, 1
   95     jz non_backup
   96     mov al, TC_BOOT_LOADER_BACKUP_SECTOR_COUNT - TC_BOOT_LOADER_DECOMPRESSOR_SECTOR_COUNT
   97     mov cl, TC_BOOT_LOADER_START_SECTOR + TC_BOOT_LOADER_BACKUP_SECTOR_COUNT
   98 
   99 non_backup:
  100     call read_sectors
  101 
  102     ; Boot loader checksum
  103     pop ebx
  104     mov si, TC_BOOT_LOADER_COMPRESSED_BUFFER_OFFSET
  105     mov cx, word ptr [start + TC_BOOT_SECTOR_LOADER_LENGTH_OFFSET]
  106     call checksum
  107 
  108     ; Verify checksum
  109     cmp ebx, dword ptr [start + TC_BOOT_SECTOR_LOADER_CHECKSUM_OFFSET]
  110     je checksum_ok
  111 
  112     ; Checksum incorrect - try using backup if available
  113     test backup_loader_used, 1
  114     jnz loader_damaged
  115 
  116     mov backup_loader_used, 1
  117     mov cl, TC_BOOT_LOADER_DECOMPRESSOR_START_SECTOR + TC_BOOT_LOADER_BACKUP_SECTOR_COUNT
  118 
  119     test TC_BOOT_CFG_FLAG_BACKUP_LOADER_AVAILABLE, byte ptr [start + TC_BOOT_SECTOR_CONFIG_OFFSET]
  120     jnz retry_backup
  121 
  122 loader_damaged:
  123     lea si, loader_damaged_msg
  124     call print
  125     lea si, loader_name_msg
  126     call print
  127     jmp $
  128 checksum_ok:
  129 
  130     ; Set up decompressor segment
  131     mov ax, es
  132     mov ds, ax
  133     cli
  134     mov ss, ax
  135     mov sp, TC_BOOT_LOADER_DECOMPRESSOR_MEMORY_SIZE
  136     sti
  137 
  138     push dx
  139 
  140     ; Decompress boot loader
  141     mov cx, word ptr [start + TC_BOOT_SECTOR_LOADER_LENGTH_OFFSET]
  142     sub cx, TC_GZIP_HEADER_SIZE
  143     push cx                                                                     ; Compressed data size
  144     push TC_BOOT_LOADER_COMPRESSED_BUFFER_OFFSET + TC_GZIP_HEADER_SIZE          ; Compressed data
  145     push TC_MAX_BOOT_LOADER_DECOMPRESSED_SIZE                                   ; Output buffer size
  146     push TC_BOOT_LOADER_DECOMPRESSOR_MEMORY_SIZE + TC_COM_EXECUTABLE_OFFSET     ; Output buffer
  147 
  148     push cs
  149     push decompressor_ret
  150     push es
  151     push TC_COM_EXECUTABLE_OFFSET
  152     retf
  153 decompressor_ret:
  154 
  155     add sp, 8
  156     pop dx
  157 
  158     ; Restore boot sector segment
  159     push cs
  160     pop ds
  161 
  162     ; Check decompression result
  163     test ax, ax
  164     jz decompression_ok
  165 
  166     lea si, loader_damaged_msg
  167     call print
  168     jmp $
  169 decompression_ok:
  170 
  171     ; DH = boot sector flags
  172     mov dh, byte ptr [start + TC_BOOT_SECTOR_CONFIG_OFFSET]
  173 
  174     ; Set up boot loader segment
  175     mov ax, es
  176     add ax, TC_BOOT_LOADER_DECOMPRESSOR_MEMORY_SIZE / 16
  177     mov es, ax
  178     mov ds, ax
  179     cli
  180     mov ss, ax
  181     mov sp, TC_BOOT_LOADER_STACK_TOP
  182     sti
  183 
  184     ; Execute boot loader
  185     push es
  186     push TC_COM_EXECUTABLE_OFFSET
  187     retf
  188 
  189     ; Print string
  190 print:
  191     xor bx, bx
  192     mov ah, 0eh
  193     cld
  194 
  195 @@: lodsb
  196     test al, al
  197     jz print_end
  198 
  199     int 10h
  200     jmp @B
  201 
  202 print_end:
  203     ret
  204 
  205     ; Read sectors of the first cylinder
  206 read_sectors:
  207     mov ch, 0           ; Cylinder
  208     mov dh, 0           ; Head
  209                         ; DL = drive number passed from BIOS
  210     mov ah, 2
  211     int 13h
  212     jnc read_ok
  213 
  214     lea si, disk_error_msg
  215     call print
  216 read_ok:
  217     ret
  218 
  219     ; Calculate checksum
  220 checksum:
  221     push ds
  222     push es
  223     pop ds
  224     xor eax, eax
  225     cld
  226 
  227 @@: lodsb
  228     add ebx, eax
  229     rol ebx, 1
  230     loop @B
  231 
  232     pop ds
  233     ret
  234 
  235 backup_loader_used      db 0
  236 
  237 disk_error_msg          db 'Disk error', 13, 10, 7, 0
  238 loader_damaged_msg      db 7, 'Loader damaged! Repair with Rescue Disk', 0
  239 
  240 ORG 7C00h + 510
  241     dw 0AA55h           ; Boot sector signature
  242 
  243 _TEXT ENDS
  244 END start