;-----------------------------------------------------;
;                   BOS bootsector                    ;
;-----------------------------------------------------;
;                                                     ;
; - Error-checking and messages.                      ;
; - 386+ processor check.                             ;
; - Tests that memory is 4mb or more.                 ;
; - Sets Protected mode & A20-gate.                   ;
; - Jumps to kernel at 1mb border.                    ;
;                                                     ;
; NOTE: Kernel must be the first thing on disk after  ;
;       the bootsector. Offset 0x200.                 ;
;                                                     ;
;          by: Christoffer Bubach, 2004               ;
;-----------------------------------------------------;
use16
org 0x7C00

;-----------------;
;   "fix"-jump    ;
;-----------------;
fix:
	  jmp	  0x0000:start



;-----------------------;
;      variables        ;
;-----------------------;
	  loading	  db  'Starting BOS'
	  ram_error	  db  'RAM'
	  a20_error	  db  'A20'
	  cpu_error	  db  'CPU'
	  read_error	  db  'FDD'
	  msg_error	  db  ' error..'



;-----------------------------------------------;
;   "real" starting point of bootsector code    ;
;-----------------------------------------------;
start:
	  cli
	  mov	  ax, cs
	  mov	  ds, ax
	  mov	  es, ax

	  mov	  ax, 0x9000
	  mov	  ss, ax
	  mov	  sp, 0x8000

       ;   mov     [bpbDriveNo], dl
	  sti

;-------------------------------------------;
;  clear the screen. only needed for 80x50  ;
;-------------------------------------------;
	  mov	  ax, 3
	  mov	  bl, 0
	  int	  0x10

     ;----------------------------------;
     ;   set to 0x1112 for 80x50 mode   ;
     ;----------------------------------;
	  mov	  ax, 3
	  int	  0x10

     ;-----------------------;
     ;  print loading mess   ;
     ;-----------------------;
	  mov	  bp, loading
	  mov	  ax, 0x1301
	  mov	  bx, 7
	  mov	  cx, 12
	  mov	  dx, 0x0102
	  int	  0x10

     ;------------------------;
     ;  set cursor location   ;
     ;------------------------;
	  mov	  bl, 2
	  mov	  ah, 2
	  mov	  dx, 0x0201
	  int	  0x10

     ;----------------;
     ;   print dots   ;
     ;----------------;
	  mov	  ah, 9
	  mov	  al, '.'
	  mov	  cx, 14
	  int	  0x10


;----------------------------------------------;
; 386 check, i have room for such luxury.. :-) ;
;----------------------------------------------;
	  pushf
	  pop	  ax
	  mov	  dx, ax
	  xor	  ax, 0x4000
	  push	  ax
	  popf
	  pushf
	  pop	  ax
	  and	  ax, 0x4000
	  and	  dx, 0x4000
	  cmp	  ax, dx

	  mov	  bp, cpu_error
	  jz	  error


;-------------------------------------;
;   memory check, only up to 64 mb    ;
;-------------------------------------;
	  mov	  ah, 0x88
	  int	  0x15
	  cmp	  ax, 3072

	  mov	  bp, ram_error
	  jb	  error


;-----------------------;
;   set A20 ver. 1      ;
;-----------------------;
	  jmp	  .out
     .cmd_wait:
	  in	  al, 0x64
	  test	  al, 0x02
	  jnz	  .cmd_wait
	  ret
     .read:
	  in	  al, 0x60
     .out:
	  in	  al, 0x64
	  test	  al, 0x01
	  jnz	  .read

	  call	  .cmd_wait
	  mov	  al, 0xd1
	  out	  0x64, al

	  call	  .cmd_wait
	  mov	  al, 0xdf
	  out	  0x60, al

	  call	  .cmd_wait

	  call	  A20_test
	  jz	  read

;-------------------;
;  set A20 ver. 2   ;
;-------------------;
	  in	  al, 0x92
	  or	  al, 0x02
	  out	  0x92, al

	  call	  A20_test
	  jz	  read

     ;------------------;
     ;   a20 error..    ;
     ;------------------;
	  mov	  bp, a20_error

     ;------------;
     ;  error..   ;
     ;------------;
     error:
	  push	  ds
	  pop	  es

	  mov	  ax, 0x1301
	  mov	  bx, 4
	  mov	  cx, 3
	  mov	  dx, 0x0401
	  int	  0x10

	  mov	  bp, msg_error
	  mov	  cx, 8
	  mov	  dl, 4
	  int	  0x10

	  mov	  ah, 0
	  int	  0x16
	  int	  0x19

     ;-----------------------;
     ;  test if A20 is set   ;
     ;-----------------------;
     A20_test:
	  mov	  al, byte [fs:0]
	  mov	  ah, al
	  not	  al
	  xchg	  al, byte [gs:0x10]
	  cmp	  ah, byte [fs:0]
	  mov	  [gs:0x10], al
	  ret


;-------------------------------------------------;
;   read 50 kb of the disk to to memory at 1mb.   ;
;-------------------------------------------------;
read:
     ;--------------------------------------------------;
     ; error checking. we can fail a total of 10 times. ;
     ;--------------------------------------------------;
	  xor	  cx, cx
	  push	  cx
     .reset:
	  pop	  cx
	  inc	  cx
	  cmp	  cx, 11
	  mov	  bp, read_error
	  je	  error

	  push	  cx

     ;-------------;
     ; reset drive ;
     ;-------------;
	  xor	  ax, ax
      ;    mov     dl, [bpbDriveNo]
	  int	  0x13
	  jc	  .reset

     ;---------------------------------------------------;
     ; set es and hope that bios does not mess with it.. ;
     ;---------------------------------------------------;
	  push	  0xffff
	  pop	  es

     ;------------------;
     ; read 17 sectors  ;
     ;------------------;
	  mov	  ax, 0x0211
	  mov	  bx, 0x10
	  mov	  cx, 2
	  mov	  dh, 0
	  int	  0x13
	  jc	  .reset

     ;--------------;
     ; read 18 more ;
     ;--------------;
	  mov	  al, 12
	  mov	  bx, 0x2410
	  mov	  dh, 1
	  int	  0x13
	  jc	  .reset

     ;--------------;
     ; read 18 more ;
     ;--------------;
	  mov	  bx, 0x4810
	  mov	  ch, 1
	  mov	  dh, 0
	  int	  0x13
	  jc	  .reset

     ;--------------;
     ; read 18 more ;
     ;--------------;
	  mov	  bx, 0x6C10
	  mov	  dh, 1
	  int	  0x13
	  jc	  .reset

     ;--------------;
     ; read 18 more ;
     ;--------------;
	  mov	  bx, 0x9010
	  mov	  ch, 2
	  mov	  dh, 0
	  int	  0x13
	  jc	  .reset

     ;-------------------;
     ; read the last 11  ;
     ;-------------------;
	  mov	  al, 11
	  mov	  bx, 0xB410
	  mov	  dh, 1
	  int	  0x13
	  jc	  .reset

     ;------------;
     ; stop motor ;
     ;------------;
	  mov	  dx, 0x3F2
	  mov	  al, 0x0C
	  out	  dx, al

	  pop	  cx



;----------------------------------;
;   set protected mode (32-bit)    ;
;----------------------------------;
	  cli

	  lgdt	  [gdtr]

	  mov	  eax, cr0
	  or	  eax, 1
	  mov	  cr0, eax

	  jmp	  0x08:flush



;---------------------------------------------------------;
; start of 32-bit area. flush segments and jump to kernel ;
;---------------------------------------------------------;
use32
flush:
     ;-------------------------------;
     ; refresh all segment registers ;
     ;-------------------------------;
	  mov	  eax, 0x10
	  mov	  ds, eax
	  mov	  es, eax
	  mov	  fs, eax
	  mov	  gs, eax
	  mov	  ss, eax
	  mov	  esp, 0xfffc


;-------------------------------------;
;  "send" info to the loaded file     ;
;-------------------------------------;

     ;-------------------------------------;
     ;  not sending ram.. we already know  ;
     ;  that we have 4mb or more.   ;-)    ;
     ;-------------------------------------;
       ;   mov     dl, [bpbDriveNo]

     ;--------------------------;
     ;  report "BosBoot".. ;-)  ;
     ;--------------------------;
	  mov	  eax, 0xB05B007



;-----------------------------------;
; jump to loaded file (1mb in mem)  ;
;-----------------------------------;
	  jmp	  0x08:0x100000




;--------------------------------;
;  global descriptor table (gdt) ;
;--------------------------------;
gdt:			  dw  0x0000, 0x0000, 0x0000, 0x0000
codesel:		  dw  0xFFFF, 0x0000, 0x9800, 0x00CF
datasel:		  dw  0xFFFF, 0x0000, 0x9200, 0x00CF
gdt_end:

gdtr:			  dw  gdt_end - gdt - 1
			  dd  gdt


;-------------------------------------;
; set the BOOT-signature at byte 510. ;
;-------------------------------------;
	  rb boot+512-2-$
	  dw 0xAA55