; ===========================================================================
; === Trebuchet Launcher                                                  ===
; ===========================================================================
; 3-22-2007 -CE added mbr loader for first IDE drive - based on mylilo from
; my hacked up MSDOS 3.31 from 1994!  :)

MBRBASE         EQU     600h
PARTTABL	EQU	1BEh

boot_ind	EQU	0	; 80h=active
start_head	EQU	1
start_sector    EQU     2       ; bits 0-5
start_cyl       EQU     3       ; bits 8,9 in bits 6,7 of sector
os_ind          EQU     4       ; OS indicator
end_head        EQU     5
end_sector      EQU     6       ; bits 0-5
end_track       EQU     7       ; bits 8,9 in bits 6,7 of sector
nsect           EQU     8       ; sectors preceding partition
lenght		EQU	0Ch	; length of partition in sectors

mylilo proc far
        mov  ah, 41h
        mov  bx, 55aah
        mov  dl, 80h
        int  13h
        jc   treb_biosread
        cmp  bx, 0aa55h
        jnz  treb_biosread
        test cl, 1
        jz   treb_biosread
        mov  ax, word ptr cs:[mbr+1beh+08]
        mov  word ptr cs:[_bios_LBA_low], ax
        mov  ax, word ptr cs:[mbr+1beh+08+02]
        mov  word ptr cs:[_bios_LBA_high], ax
        mov  ax, 4200h
        mov  dl, 80h
        mov  si, offset _disk_address_packet
        int  13h
        jmp  mbrexec
treb_biosread:
        xor  ax, ax
        mov  es, ax
        mov  ax, 0204h  ; bios read
        mov  bx, 7c00h
        mov  cx, word ptr cs:mbr+1beh+start_sector
        mov  dh, byte ptr cs:mbr+1beh+start_head
        mov  dl, 80h
        int  13h
mbrexec:
        mov  dl, 80h
        push es
        push 7c00h
        sti
        retf
mylilo endp

; ===========================================================================
trebuchet proc far
; ===========================================================================
        pusha

        ; if file exist kernel.sys then load into 0060:0000 else
        ; if file exist ntldr      then load into 0060:0000 else
        ; error message 
        mov  al, byte ptr cs:bootdrive
        cmp  al, 80h
        jge  mbrx
        push cs
        pop  es
        mov  bx, offset cs:mbr
        mov  dl, 80h
        mov  dh, 0
        mov  cl, 1
        mov  ch, 0
        mov  al, 1
        mov  ah, 02h
        int  13h
        jc   mbrx
        ;
       ; push cs
       ; pop  es
       ; lea  si, mbr
       ; add  si, 1beh
       ; mov  cx, 16*4  
       ; lea  di, p1_boot
       ; cld
       ; rep  movsb
        ;
        ; display partition names and ask for input 
        ;
        mov  al, byte ptr cs:mbr+1beh
        cmp  al, 80h
        jne  mbrx
        _print hdd_bootable, 1
        call readkey
        cmp  al, 'Y'
        jne  mbrx
        call mylilo
mbrx:
        popa
        ret
trebuchet endp

; Partition MBR record

mbr        db 512 dup(0)
_disk_address_packet:
                        DB      10h    ; size of the packet
			DB	0	; reserved
			DW	4	; number of sectors to read
			DW	7C00h	; offset
			DW	0000h   ; segment of buffer
_bios_LBA_low		DW	0
_bios_LBA_high		DW	0
			DW	0
			DW	0

;resb    32

;times   1BEh-$+$$      DB      0
;times   64             DB      0       ; partition table

                        DW      0AA55h
; Trebuchet_msg db 0ffh,'Trebuchet 0.0.1b',0ffh,0
hdd_bootable  db 'Harddisk bootable. Load ? [y/N]',0

