; ******************************************************

;       Name: Boot Module
;       Author: Peter Kleissner
;       Version: 0.1
;       Date: 17.02.2008 17:11:31
;       last Update: 17.02.2008 17:11:33

;       Forensic Lockdown Software
;       (C) 2008 Vienna Computer Products

; ******************************************************

[bits 16]                                       ; create a 16 Bit Code
CPU 386                                         ; Assemble instructions up to the 386 instruction set

%include "Module Functions.asm"
%include "Kernel Storage.asm"

%define _Debug

org Boot_Module


; export functions

jmp word Execute_Original_Bootloader
jmp word Restore_Original_Bootloader
jmp word Execute_Master_Boot_Record_Backup
jmp word Restore_Master_Boot_Record_Backup




Execute_Original_Bootloader:

; executes the copy of the original Bootloader Image
; never returns if successful

; check if original Bootloader Image is valid
cmp [Original_Bootloader_Image],dword 0
je Execute_Original_Bootloader_Invalid
cmp [Original_Bootloader_Image + 510],word 0AA55h
jne Execute_Original_Bootloader_Invalid

; relocate original Bootloader to 7C00h
mov esi,Original_Bootloader_Image
mov edi,7C00h
mov ecx,512 / 4
rep movsd

; load context
mov esp,[Boot_Stack_Pointer]
popad

; execute the original Bootloader Image
jmp word 0000h:7C00h


Execute_Original_Bootloader_Invalid:

%ifdef _Debug
; invoke text output function
push dword 22                   ; Y-Position
push dword 6                    ; X-Position
push dword 0Fh                  ; Attribute
push dword Debug_Message_InvalidBootloaderImage
call API_Screen_Text_Attribute_Position
add sp,4*4
%endif

ret




Restore_Original_Bootloader:

; restores the Master Boot Record from the original Bootloader Image

; check if original Bootloader Image is valid
cmp [Original_Bootloader_Image],dword 0
je Execute_Original_Bootloader_Invalid
cmp [Original_Bootloader_Image + 510],word 0AA55h
jne Execute_Original_Bootloader_Invalid

; write the original Bootloader Image onto disk
mov [dap_Count],word 63
mov [dap_LBA_low],dword 0
mov [dap_Buffer],dword Original_Bootloader_Image

xor ax,ax
mov ds,ax
mov si,disk_address_packet

mov dl,[Boot_Drive]
mov ax,4300h
int 13h
jnc Restore_Original_Bootloader_Exit

%ifdef _Debug
; invoke text output function
push dword 22                   ; Y-Position
push dword 6                    ; X-Position
push dword 0Fh                  ; Attribute
push dword Debug_Message_Bootloader_WriteToDisk
call API_Screen_Text_Attribute_Position
add sp,4*4
%endif

Restore_Original_Bootloader_Exit:

ret




Execute_Master_Boot_Record_Backup:

; executes the backup of the Master Boot Record
; never returns if successful

; load the original MBR
push dword MBR_Backup_Buffer        ; Buffer
push dword File_MBR_Backup          ; File
call API_Load_System_File
jc Execute_Master_Boot_Record_Backup_FileError
add sp,2*4

; copy Bootloader
mov ax,MBR_Backup_Buffer / 16
mov ds,ax
xor si,si
mov es,si
mov di,7C00h
mov ecx,512 / 4
rep movsd

xor ebx,ebx
mov ds,bx
mov es,bx

; load context
mov esp,[Boot_Stack_Pointer]
popad

; execute the Master Boot Record backup
jmp word 0000h:7C00h

Execute_Master_Boot_Record_Backup_FileError:

%ifdef _Debug
; invoke text output function
push dword 22                   ; Y-Position
push dword 6                    ; X-Position
push dword 0Fh                  ; Attribute
push dword Debug_Message_MBR_Backup
call API_Screen_Text_Attribute_Position
add sp,4*4
%endif

ret




Restore_Master_Boot_Record_Backup:

; restores the Master Boot Record from backup to disk

; load the original MBR
push dword MBR_Backup_Buffer        ; Buffer
push dword File_MBR_Backup          ; File
call API_Load_System_File
jc Execute_Master_Boot_Record_Backup_FileError
add sp,2*4

; write the MBR onto disk
mov [dap_Count],word 63
mov [dap_LBA_low],dword 0
mov [dap_Buffer],dword MBR_Backup_Buffer / 16 * 10000h

xor ebx,ebx
mov ds,bx
mov si,disk_address_packet

mov dl,[Boot_Drive]
mov ax,4300h
int 13h
jnc Restore_Master_Boot_Record_Backup_Exit

%ifdef _Debug
; invoke text output function
push dword 22                   ; Y-Position
push dword 6                    ; X-Position
push dword 0Fh                  ; Attribute
push dword Debug_Message_MBR_WriteToDisk
call API_Screen_Text_Attribute_Position
add sp,4*4
%endif

Restore_Master_Boot_Record_Backup_Exit:

ret






File_MBR_Backup         db      "?:\Stoned\Master Boot Record.bak", 0


%ifdef _Debug

Debug_Message_InvalidBootloaderImage    db "Invalid original Bootloader Image, cannot execute.", 0
Debug_Message_Bootloader_WriteToDisk    db "Error writing Bootloader Image to disk.", 0
Debug_Message_MBR_Backup                db "Error loading Master Boot Record backup file from disk.", 0
Debug_Message_MBR_WriteToDisk           db "Error writing Master Boot Record backup to disk.", 0

%endif

