
main:

IF (REACTOS)   
    xor  cx, cx
    mov  ss, cx
    mov  sp, BASE - 16                ; 7bf0h - 16bytes of temp space (BSS vars)
    mov  ds, cx
    mov  ax, 2000h
    mov  es, ax
    cld
    mov  bp, BASE
    cmp  byte ptr [bp+$bootDrive], cl  ; Compare boot drive  is it a hraddrive?
    je   floppy_boot

    mov  ax, cx
    cwd                               ; Zero DX:AX
    call read_one_sector
    jc   floppy_boot
    sub  bx, 3ah                      ; 512 = 454 bytes
.386
    mov  eax, [bp+$lHiddensects]
find_our_partion:
    cmp  eax, [es:bx]
    mov  dl,  [es:bx-4]               ; partition type 
    jnz  next_partition_entry
    or   dl, 2                        ; make ptype 4 & 6 identical
    mov  [bp+2], al  
next_partition_entry:
    add  bl, 10h
    jnc  find_our_partition      

floppy_boot:
.286c

; We now have our partition type at 0000:7C02 
; If the type was 4 or 6 then that byte is 6
; I can't imagine why the boot sector needs to store this information, but hopefully I will uncover it
; as I further disassemble this boot sector.


floppy_boot:
    xor  cx, cx
    mov  al, [bp+$bFats]
    cbw                    ; sign extend ax
    mul  word ptr [bp+$bFats]
    ; Multiply it with NumberOfFats PLUS the low byte of MaxRootEntries!!??
    add  ax, [bp+$lHiddenSects]
    add  dx, [bp+$lHiddenSects+2]
    add  ax, [bp+$wReservedsects]
    adc  dx, cx
    mov  si, [bp+$wRootentries]
    ; DX:AX == starting sector of root 
    pusha
    ; save start root
    mov  [bp-4], ax
    mov  [bp-2], dx
    mov  ax, 32      ; 20h = direntry
    mul  si
    mov  bx, [bp+$wSectsize]    
    add  ax, bx   ; maybe should be : adc  dx, 0 (carry)
    dec  ax       ; rounding up
    div  bx
    ; Add the number of sectors of the root directory to our other value
    add  [bp-4], ax
    ; Now the first 4 bytes before the boot sector contain the starting sector of the data area
    adc  [bp-2], cx
    popa
load_root_dir_sector:
    mov  di, 0
    call read_one_sector
    jc   diskerror         ; fail. 
search_directory:
 	 ; If the first byte of the directory entry is zero then we have reached the end
         ; of the directory and NTLDR is not here so reboot
         cmp  [es:di], ch
         jz   loadererr 
              pusha
              mov  cl, 11  ; 0bh - filename len
              mov  si, BASE + OFFSET Kernel
              repe cmpsb
              popa
              jz   foundkernel
              dec  si   ; maxentries--
              jz   loadererr 
         add  di, 32  ; 20h  - next entry
         cmp  di, bx
         jc   search_directory
         jmp  short load_root_dir_sector	
         ; Didn't find NTLDR in this directory sector, try again
foundkernel:
found_ntldr:
         mov  dx, [es:di+26] ; 1ah - Start cluster 
         push dx
         mov  al, 1 
         mov  bx, 0
         ; ES:BX = load address 2000:0000
         call read_cluster
         jc   diskerror
         pop  bx

          
00007D05  8A5624            mov dl,[bp+BootDrive]		; Get boot drive in DL
00007D08  BE0B7C            mov si,0x7c0b
00007D0B  8BFC              mov di,sp
00007D0D  C746F03D7D        mov word [bp-0x10],read_cluster
00007D12  C746F4297D        mov word [bp-0xc],0x7d29
00007D17  8CD9              mov cx,ds
00007D19  894EF2            mov [bp-0xe],cx
00007D1C  894EF6            mov [bp-0xa],cx
00007D1F  C606967DCB        mov byte [0x7d96],0xcb
00007D24  EA03000020        jmp 0x2000:0x3
00007D29  0FB6C8            movzx cx,al
00007D2C  668B46F8          mov eax,[bp-0x8]
00007D30  6603461C          add eax,[bp+HiddenSectors]
00007D34  668BD0            mov edx,eax
00007D37  66C1EA10          shr edx,0x10
00007D3B  EB5E              jmp short 0x7d9b

read_cluster proc near
      movzx  cx, al
      dec    dx
      dec    dx
      mov    al, [bp+$bSectsperclust] 
      xor    ah, ah
      mul    dx
      add    ax, [bp-4]
      adc    dx, [bp-2]
      call   read_one_Sector
      ret
read_cluster endp

read_sectors proc near
     push  dx
     push  ax
     push  es
     push  bx
     push  1
     push  10h
     xchg  ax, cx
     mov   ax, [bp+$wSectspertrack]
     xchg  ax, si
     xchg  ax, dx
     xor   dx, dx
     div   si
     xchg  ax, cx
     div   si
     inc   dx
     xchg  cx, dx
     div   word ptr [bp+$wHeads]
     mov   dh, dl
     mov   ch, al
     ror   ah, 2
     or    cl, ah
; int13 read
     mov   ax, 0201h
     cmp   byte ptr [bp+2], 0eh
     jnz   short doread           ; 7d82
ext13read:
     mov   ah, 42h
     mov   si, sp
doread:
     mov   dl, [bp+$Bootdrive]  
     int   13h
     popa
     popa
     jc    readexit
     inc   ax
     jnz   read1       
     inc   dx
read1:
     add   bx, [bp+$wSectsize] 
     dec   cx
     jnz   @1   ; 7d9b
     clc 
readexit:
     ret

read_one_sector proc near
      inc  cx
      mov  bx, 0
@1:
      pusha
      ; push 0
      call read_sector       
      ret
read_one_sector endp


loadererr:
      mov  si, BASE + OFFSET NTLDR_ERR
      call putstr
diskerror:
      mov  si, BASE + OFFSET DISK_ERR
      call putstr
putstr:
      lodsb 
      cbw 
      cmp  al, 0
      je   short pstrx
      mov  ah, 0eh
      mov  bx, 7
      int  10h     
      inc  si
      jmp  short putstr      
pstrx:

keyw:
      int  16h
reboot:
      int  19h

NTLDR_ERR   db 0dh,0ah,'[missing]',0ffh
DISK_ERR    db 0dh,0ah,'[error]',0ffh
RESTART_ERR db 0dh,0ah,'[ESC = restart]',0ffh
kernel      db 'NXBIO   SYS',0
; NTLDR	    db 'NTLDR      '

ENDIF

IF (NXDOS)
   ; NXDOS FAT16 HDD Boot sector loader
   ; Chris Evans (c) 2006
   bss_stack

ERRMSG DB 0Dh,0Ah,'W1'
       DB 0Dh,0Ah,'E.'
NULL00 DB 0,0

       ;  '1234567 123'
KERNELFILE DB 'NXBIO   SYS'
           DB 'WINBOOT SYS'
           DB 'IO      SYS'
           DB 0
;
; db 00h, 'NONE ',0
; db 01h, 'FAT12',0
; db 04h, 'FAT16',0
; db 05h, 'EXT  ',0
; >32MB
; db 06h, 'FAT16',0
; db 07h, '     ',0
; db 08h, '     ',0
; db 09h, '     ',0
; db 0ah, '     ',0
; db 0bh, 'FAT32',0
; LBA 
; db 0ch, 'FAT32',0
; db 0eh, 'FAT16',0
; db 0fh, 'EXT  ',0
;

ENDIF

include \nxdos\src\boot\f16.inc
