Annotation of truecrypt/boot/windows/bootsector.asm, revision 1.1.1.4

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

unix.superglobalmegacorp.com

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