[colorforth] IDE bootloader to play with PC
- Subject: [colorforth] IDE bootloader to play with PC
- From: Adam Marquis <adam.marquis@xxxxxxxxxxxx>
- Date: Thu, 19 Jun 2003 21:24:51 -0400
Hi
this time less buggy code, at least I hope.
I sent it to the FASM author to put on the website,
I hope he will like it.
It use the fact that a lot of sectors are not used on
the head 0 (62 usable sectors here) because
most partitioning program start at head 1.
So one can write his test code here and use this
bootloader to test it.
;Written in FASM 1.46 GUI for windows
;
;PC Bootloader with the following features
;- Disable all interrupts (NMI too)
;- Enable the A20 (PS2 port)
;- Go into flat 32 bit protected mode
;- Loads n sectors from IDE hard drive
; following the MBR
;- BIOS independant
;- Can work from almost any bootable medium
;
;Have fun!
macro align value { rb (value-1)-($+value-1) mod value }
use16
org 7C00h
Beginning:
cli ;Disable interrupts
cld ;Clear direction flag
in AL,070h ;Turn NMI off
or AL,080h
out 070h,AL
in AL,092h ;\
or AL,2 ; Enable the A20
jmp $+2 ; for PS/2 and up
out 092h,AL ;/
mov AX,CS ;\
mov DS,AX ; Fetch GDT
lgdt [GDT] ;/
mov EAX,CR0 ;\
or AX,1 ; Set protected mode bit
nop ; Alignment
mov CR0,EAX ;/
jmp 8:Protected ;Flush pipeline
align 8 ;should do nothing
GDT: dw 0017h ;flat memory GDT
dd GDT
dw 0000h
dw 0FFFFh,0000h,9A00h,00CFh ;code
dw 0FFFFh,0000h,9200h,00CFh ;data
use32
Protected: ;Now in 32 bit protected mode
mov EAX, 10h ;Data descriptor
mov DS, AX ;\
mov ES, AX ; Initialize crucial descriptors
mov SS, AX ;/
mov ESP, 7C00h ;Initialize the Return Stack
xor EDI, EDI ;\
mov EAX, 1 ; Loads N sectors after MBR (0=256 sectors)
call Read ;/
jmp 0 ;Jump to it, in pmode with no interrupts
Read: mov bl, al ;BL act as sector counter
mov edx,1f2h ;sector count
out dx,al
mov eax, 0A0000002h ;Base address in physical CHS format
inc edx
out dx, eax
mov edx, 1f7h ;\Write to Command Register
mov al,20h ; Read and Retry
out dx,al ;/
waitrdy:
in al,dx ;Read Status register
and al,8
jz waitrdy
mov cx, 256 ;\
mov dx,1f0h ; Sector fetch loop
rep insw ; Respect 0 = 256 sectors
dec bl ;
jnz waitrdy ;/
ret
SpaceLeft = 510-($-Beginning)
times SpaceLeft db 0h
dw 0AA55h ;Boot sector magic number
;===================================================================
;Compile-time directives
;
d1 = '0'+ SpaceLeft shr 8 and 0Fh
d2 = '0'+ SpaceLeft shr 4 and 0Fh
d3 = '0'+ SpaceLeft and 0Fh
if d1>'9'
d1 = d1 + 7
end if
if d2>'9'
d2 = d2 + 7
end if
if d3>'9'
d3 = d3 + 7
end if
display 'IDE bootloader',13,10
display 'Space left: ',d1,d2,d3,'h',13,10
;===================================================================
---------------------------------------------------------------------
To unsubscribe, e-mail: colorforth-unsubscribe@xxxxxxxxxxxxxxxxxx
For additional commands, e-mail: colorforth-help@xxxxxxxxxxxxxxxxxx
Main web page - http://www.colorforth.com