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

unix.superglobalmegacorp.com

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