Page 1 of 1

Boot sector

Posted: Sat Jan 08, 2005 12:00 am
by onizuka
Hi,
I'm trying to put up a minimal protected mode bootsector to launch a little OS after.
Could somebody tell me what the problem is ? I get 3 exceptions => reboot.
Here's the code :

************************myboot.asm*********************************************

[ORG 7C00h]
[BITS 16]
;------------------DEFS--------------------------------------;

BIOS_DROP equ 0x07C0 ; Where the BIOS dropped us
KSTART equ 2 ;
KSIZE equ 1 ;
KSEG equ 1000h ; Where we'll throw the kernel

;-----------------CODE---------------------------------------;


;Set up our stack
;mov ax, BIOS_DROP ;
;mov ds, ax ;AX = DS = ES = BIOS_DROP
;mov es, ax ;
;mov ax, 0x8000
;mov ss, ax ; let's set up our stack from 0x8000
;mov sp, 0xf000 ; to 0xf000

mov si,takeoff
call bprint ; Greetings !!

;Copy the kernel
mov ax, 0x200 + KSIZE ; service: AH=0x2 (copy), AL=KERNEL_SIZE sectors
;mov es, word [KSEG]
push word KSEG ; ES <- KERNEL_SEG (copy destination)
pop es ;
xor bx, bx ; BX <- 0 (dunno if that has to be...)
mov cx, KSTART ; CX <- KERNEL_START (copy source)
xor dx, dx ; DX <- 0
int 13h
jnc done

mov si, error ; error
call bprint
jmp $

done:
mov si, success
call bprint

cli ; Disable interrupts
xor ax, ax ; ax = ds = 0 (used by lgdt)
mov ds, ax
lgdt [gdtr] ; Load the GDT (segmentation)
mov eax, cr0
or al, 1 ; Protected mode bit <- 0
mov cr0, eax
;jmp $
jmp SYS_CODE_SEL:clear_pipe

;---------------------- PM--------------------------------------------;
[BITS 32]
clear_pipe:
mov ax, SYS_DATA_SEL ; Update DATA Segment
mov ds, ax
mov ss, ax
mov esp, 9000h

k_launch:
;jmp $ ; Uncomment this for testing
;jmp KSEG:0 ; give the hand to the kernel
jmp 1000h


;-----------------VARS---------------------------------------------------;
; Should always be declared after the stack's up !! (I think at least =0)
takeoff db "HI !!",13,10,0
success db "Bootsector: Kernel copy successful !!",13,10,0
error db "Bootsector: Kernel copy error >O<",13,10,0

;GDT Table Definition for segmentation
[BITS 32]

gdtr:
dw gdt_end - gdt - 1 ; GDT limit
dd gdt ; GDT base

gdt:
times 8 db 0 ; NULL Descriptor

SYS_CODE_SEL equ $-gdt
dw 0xFFFF ; limit 15:0
dw 0 ; base 15:0
db 0 ; base 23:16
db 0x9A ; type = present, ring 0, code, non-conforming, readable
db 0xCF ; page granular, 32-bit
db 0 ; base 31:24

SYS_DATA_SEL equ $-gdt
dw 0xFFFF ; limit 15:0
dw 0 ; base 15:0
db 0 ; base 23:16
db 0x92 ; type = present, ring 0, data, expand-up, writable
db 0xCF ; page granular, 32-bit
db 0 ; base 31:24

gdt_end:

;-------------------MISC FUNCS-------------------------------------------;

;Very simplified print in real mode
bprint:
push ax
push bx

.next:
lodsb ; Load ds:si word to al
cmp al, 0 ; if 0x0 => end of string
jz .end
mov ah, 0x0E ; 0x0e BIOS service -> print to screen
mov bx, 0x0004 ; bx -> attribut (red), al -> caractere ascii
int 0x10 ; INT !!
jmp .next

.end:
pop bx
pop ax
ret


;-------------------Boot sector signature-------------------------------;

times 510 - ($ - $$) db 0
dw 0xAA55

******************************kern.c******************************************************
void main(void) {
for (;;);
}

******************************BOCHS output********************************************
00300095414e[CPU ] exception(): 3rd (13) exception with no resolution, shutdow


I'm a bit short on time. Thanks a lot.

Re: Boot sector

Posted: Sun Jan 09, 2005 12:00 am
by blackcatcoder
try this, hope it will help

BITS 32]
clear_pipe:
mov ax, SYS_DATA_SEL ; Update DATA Segment
mov ds, ax
mov ss, ax
mov esp, 9000h

k_launch:
;jmp $ ; Uncomment this for testing
;jmp KSEG:0 ; give the hand to the kernel
jmp 10000h // <---

Re: Boot sector

Posted: Mon Jan 10, 2005 12:00 am
by onizuka
Worked !!
Thanks a lot blackcatcoder !! :) This s*** has been annoying me for a while !!
Why's that 10000h ?

Thanks again

Re: Boot sector

Posted: Mon Jan 10, 2005 12:00 am
by JAAman
10000h is the same as rm: 1000:0000 == 64k

Re: Boot sector

Posted: Fri Jan 14, 2005 12:00 am
by blackcatcoder
no prob ;-)