;CLDR the crutch bootloader
;copyright (c) 2008 Kristian Hart
use16
org 0x7C00

start:
;clear segments
cli
mov [bdrive],dl;save the drive we booted from
xor ax,ax
mov ds,ax
mov es,ax
mov ax,0x8F00
mov sp,ax
mov bp,ax
sti
;get drive parameters ]
GETPARAM:
mov ah,0x8;bios functions 8 get drive parameters
mov dl,[bdrive];this is the drive we booted from stored in DL
int 0x13;call interrupt
jc GETPARAM;if failed try again
mov dh,[numheads];store the number of heads so that LBACHS can execute correctly
mov bl,cl;I need to free cl so I can shift the contents of what was in cl
mov cl,2;this is how many bits we shift
rol bl,cl;shift bl so I have only the max sector bits 0-5
ror bl,cl;mov the bits back to where they were so that I have the original number
mov [sectorspertrack],bl;save sectors per track so that LBACHS can execute correctly



;now start loading stage2
mov bx,0x7E00;load the super block right after stage1
mov ax,3;start of super block
call READB;read super block




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
functions:;all functions after here(readsectors...stuff for loading stage2)

READB:;read BLOCK lba = ax es:bx=where to read to
call LBACHS
mov ah,0x02
mov al,2;always read two sectors or 1kb blocks
mov cl,[sector]
mov ch,[cylinder]
mov dh,[head]
mov dl,[bdrive]
int 0x13
ret



LBACHS:;ax = lba
inc ax
div BYTE[sectorspertrack]
push ax
mov [sector],ah
div  BYTE[numheads]
mov [cylinder],al
mov [head],ah
ret



.data:
bdrive: db 0
sector: db 0
head:db 0
cylinder: db 0
sectorspertrack: db 0
numheads: db 0

