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

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

unix.superglobalmegacorp.com

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