Page 1 of 1

newbie needs help with c kernel

Posted: Fri Sep 27, 2002 11:22 am
by skullz
Hi I am a newbie at os development and my c kernel wount boot, it only freezes.
c kernel
----------
#define WHITE 0x07
void clrscr();
unsigned int printf(char *text,unsigned int ln);

int _start() {

clrscr();
printf("test\nOS test",0);
return 0;
}

void clrscr()
{
char *video = (char *)0xb8000;
unsigned int nr = 0;
while (nr< (80*25*2))
{
video[nr]=' ';
nr++;
video[nr]= WHITE;
nr++;
};
};

unsigned int printf(char *text,unsigned int ln)
{
char *video = (char *)0xb8000;
unsigned int nr = 0;
nr = (ln*80*2);
while(*text!=0)
{
if(*text=='\n')
{
ln++;
nr= (ln*80*2);
*text++;
}
else
{
video[nr]=*text;
*text++;
nr++;
video[nr]= WHITE;
nr++;
};
};
return(1);
};

boot.asm (form bootf02)
------------

jmp 07C0h:start ; Goto start

;_______________________________________


; Here is where the boot program starts.
start:
; Update the segment registers
mov ax, cs ; Copy cs into ax
mov ds, ax ; cs->ax->ds
mov es, ax ; cs->ax->es

jmp Running ; Go to the running part of the bootsector

;____________________________________________
; This is where the boot program runs.
Running:

; Load the second stage loader.
reset: ; Reset the floppy drive
mov ax, 0 ;
mov dl, 0 ; Drive=0 (=A)
int 13h ;
jc reset
read:
mov ax, 1000h ; ES:BX = 1000:0000
mov es, ax ;
mov bx, 0 ;

mov ah, 2 ; Load disk data to ES:BX
mov al, 5 ; Load 5 sectors
mov ch, 0 ; Cylinder=0
mov cl, 2 ; Sector=2
mov dh, 0 ; Head=0
mov dl, 0 ; Drive=0
int 13h ; Read!

jc read

cli ; Stop BIOS interrups - because PMode can't use them in this manner

; Set Protected Mode ( PMode )
mov eax, CR0
or eax, 0x1
mov CR0, eax

; Start A20
.1: in al, 0x64
test al, 2
jnz .1
mov al, 0xD1
out 0x64, al
.2: in al, 0x64
and ax, byte 2
jnz .2
mov al, 0xDF
out 0x60, al

; PMode needs a jmp, so here it is
jmp pmode

;____________________________________________
; We are in protected mode here:
[bits 32]
pmode:

; jmp 1000h:0000 ; Jump to the C kernel

; This part makes sure the bootsector is 512 bytes.

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

; End of boot.asm
;____________________________________________

compiled as followed
--------------------------
nasm -f bin -o boot.bin boot.asm
gcc -c kernel.c -o kernel.o
ld -Ttext=0x100000 -o kernel.bin kernel.o -e 0x0
objcopy -R .note -R .comment -S -O binary kernel kernel.bin
PARTCOPY boot.bin 0 3 -f0 0
PARTCOPY boot.bin 3E 1C2 -f0 3E

Re:newbie needs help with c kernel

Posted: Fri Sep 27, 2002 12:01 pm
by Slasher
Hi,
you have to define a Global Descriptor Table (GDT), load it with the LGDT command before you switch to Protected mode!!! Thats why its freezing!!!
Try to search for a tutorial on protected mode and study it in detail. also get the intel 386+ programmers manual!!
;D

Re:newbie needs help with c kernel

Posted: Fri Sep 27, 2002 12:34 pm
by frank

Code: Select all

;____________________________________________
; We are in protected mode here:
[bits 32]
pmode:

;              jmp 1000h:0000      ; Jump to the C kernel
delete the ; and change it to
jmp 0x1000:0x0000 or 1000h:0000h

Re:newbie needs help with c kernel

Posted: Sat Sep 28, 2002 8:27 pm
by Tom
you can just use my whole source code...and even using some...please put the Copyright (C) 2002 Tom Fritz there ;)

Re:newbie needs help with c kernel

Posted: Sat Sep 28, 2002 9:42 pm
by Warmaster199
Here's a good hintage:

Make a small assmebler stub that you tack onto the front of your kernel when compiling. This small stub should be ORG 0x1000(Because your code starts there). Get it to call start...

------------------start.asm------------------
ORG 0x1000

start:
mov ax, cs ; Get CS and make all the seg-regs
mov ds, ax ; the same. Change later if needed.
mov es, ax
mov fs, ax
jmp k_start

EXTERN __start ; Declares int _start(); Note the
; 2 '_''s infront of _start(); You need one extra
; '_' infront to call a C function;
; CCode void main() would be ASMCode _main

k_start:
call __start
jmp $ ; This will freeze the machine incase
_start returns...

Re:newbie needs help with c kernel

Posted: Sun Sep 29, 2002 8:31 am
by skullz
Sorry about that Tom
This is about the 3rd kernel & bootloader i have tried to boot but all i get is bullock

Re:newbie needs help with c kernel

Posted: Sun Sep 29, 2002 12:01 pm
by Tom
my code is not programed for a second bootloader even...it is programmed for the bootsector, then the kernel.

You can make a 3rd bootloader easily with my code.

If you want to, here's an outline:

your bootsector:
read disk, goto second bootloader

second bootloader:
read disk, set PMode, go to my C kernel

C Kernel( 3rd bootloader ):
do stuff, use my code untill you get yours working