Page 1 of 1

help: loading kernel

Posted: Tue Jul 20, 2010 8:12 am
by Almamu
bunkdeath wrote:Hi to all those are helping someone like me,

I am new to this forum, and sorry, if this question is already there in forum. I tried to search for this question, but there was many such words so I got nothing.

I have a boot strap loader, and a simple kernel.
From tutorial i got that, kernel can be loaded in 3 different ways.

i) Put kernel in bootstrap loader.( But my kernel is bigger than 512 b)
ii) Specify the kernel file and load.
iii) Run form specified location.

The first one is not suitable for me as well as while coding OS.
If any one could give me any idea(with code) in any of two method(ii, ii) to load kernel file?

And I think platform does not matter while coding for OS(I think), but also, I am using x86 architecture Windows 7 OS

Thanks
start.asm:

Code: Select all

extern k_main
global _loader
_loader:
     call k_main
     cli
     hlt
main.c

Code: Select all

k_main(){
     char *vidmem = (char *)0xb8000;//VIDMEM pointer
     int i = 0;
     while(1){
          vidmem[i*2] = "H";//Print H on screen
          vidmem[i+1] = 0x07;//Set H color to WHITE
          i++;
     }
};
This should start the k_main function and it will print a lot of H's
Use this makefile if you have troubles:

Code: Select all

all:
     @nasm -f aout ./src/start.asm -o ./src/start.o
     @gcc -c ./src/main.c -o ./src/main.o -fno-builtin -fno-stack-protector
     ld -T link.ld -o kernel.bin ./src/start.o ./src/main.o
This should compile correctly your boot sector, but if you want to use it whit grub:
start.asm:

Code: Select all

global _loader		; making entry point visible to linker
extern k_main		; KernelMain
; setting up the Multiboot header - see GRUB docs for details
MODULEALIGN equ  1<<0                   ; align loaded modules on page boundaries
MEMINFO     equ  1<<1                   ; provide memory map
FLAGS       equ  MODULEALIGN | MEMINFO  ; this is the Multiboot 'flag' field
MAGIC       equ    0x1BADB002           ; 'magic number' lets bootloader find the header
CHECKSUM    equ -(MAGIC + FLAGS)        ; checksum required
section .text
align 4
MultiBootHeader:
	dd MAGIC
	dd FLAGS
	dd CHECKSUM
	; reserve initial kernel stack space
	STACKSIZE equ 0x4000		; that's 16k.
	_loader:
	mov esp, stack+STACKSIZE; set up the stack
	cli
	mov eax, cr0
	or al, 1
	mov cr0, eax
	push eax		; pass Multiboot magic number
	push ebx		; pass Multiboot info structure
	; Immediately after that you have to jump to the code segment in the GDT:
	JMP 08h:k_main
	hlt			; halt machine should kernel return
Its ready to hold Protected Mode...
link.ld:

Code: Select all

ENTRY (_loader)
SECTIONS{
. = 0x00100000;
.text :{
*(.text)
}
.rodata ALIGN (0x1000) : {
*(.rodata)
}
.data ALIGN (0x1000) : {
*(.data)
}
.bss : {
_sbss = .;
*(COMMON)
*(.bss)
_ebss = .;
}
}
With this you can start your kernel. It's easy to use, you only need MagicISO to replace kernel.bin in the .img file with your own kernel.bin.
grub.img: http://www.megaupload.com/?d=5J3ZFSR0

Re: help: loading kernel

Posted: Tue Jul 20, 2010 8:47 am
by Combuster
@Almamu:

Handing out code without any form of explanation is considered bad practice by many. Giving out utterly broken and braindead code is really, really bad. This is a random mix of some tutorials, does not compile, and reeks of you having absolutely no clue.

@OP:
You might want to run down a tutorial series to get some code and how it puts things together, otherwise you can just grab some known working starting code from the barebones tutorial (found on the wiki).

Re: help: loading kernel

Posted: Tue Jul 20, 2010 9:00 am
by Almamu
Combuster wrote:@Almamu:

Handing out code without any form of explanation is considered bad practice by many. Giving out utterly broken and braindead code is really, really bad. This is a random mix of some tutorials, does not compile, and reeks of you having absolutely no clue.

@OP:
You might want to run down a tutorial series to get some code and how it puts things together, otherwise you can just grab some known working starting code from the barebones tutorial (found on the wiki).
Im using this code on my OS, and it compiles and works, it has some more lines...

Okokokokok.

Re: help: loading kernel

Posted: Tue Jul 20, 2010 9:27 am
by gerryg400

Code: Select all

k_main(){
     char *vidmem = (char *)0xb8000;//VIDMEM pointer
     int i = 0;
     while(1){
          vidmem[i*2] = "H";//Print H on screen
          vidmem[i+1] = 0x07;//Set H color to WHITE
          i++;
     }
}
That so-doesn't-work. 5 obvious bugs/compile errors in a 9 line function.

Re: help: loading kernel

Posted: Tue Jul 20, 2010 9:40 am
by gedd
first change

Code: Select all

vidmem[i+1] = 0x07;//Set H color to WHITE
to

Code: Select all

vidmem[i*2+1] = 0x07;//Set H color to WHITE

Re: help: loading kernel

Posted: Tue Jul 20, 2010 9:47 am
by gerryg400
3 more. (I mis-counted before)

Re: help: loading kernel

Posted: Tue Jul 20, 2010 9:49 am
by Almamu
gedd wrote:first change

Code: Select all

vidmem[i+1] = 0x07;//Set H color to WHITE
to

Code: Select all

vidmem[i*2+1] = 0x07;//Set H color to WHITE
Ops, yeah, i have mistaked this... is the bad of write it by hand at the moment without any reference...

Re: help: loading kernel

Posted: Tue Jul 20, 2010 11:29 am
by Candy
This may still be a helpful thread if we can get the criticism to be constructive.

I prefer not handing out code samples to people asking for help with their question. Fixing problems is so much harder than taking somebody else's solution. Learning to fix your own problems should be the goal, not handing them code.

Re: help: loading kernel

Posted: Tue Jul 20, 2010 12:52 pm
by roboman
You might want to try using a simple boot loader that you already know works and just loads an exe or com file. Then you can work on getting all the fancy stuff to work in one section of code and decide late how much you want to push over to the loader and how much you want to leave in the main program. Getting a single section or function to work has always seemed to work as a better starting ground for me then tying to get several pieces to work and work together at the same time. http://alexfru.chat.ru/programming/bootprog.zip might be a loader you could try out.

Re: help: loading kernel

Posted: Tue Jul 20, 2010 4:37 pm
by neon

Code: Select all

k_main(){
     char *vidmem = (char *)0xb8000;//VIDMEM pointer
     int i = 0;
     while(1){
          vidmem[i*2] = "H";//Print H on screen
          vidmem[i+1] = 0x07;//Set H color to WHITE
          i++;
     }
}
That should eventually triple fault. You should make sure the characters being printed stay within video RAM.

Re: help: loading kernel

Posted: Tue Jul 20, 2010 5:34 pm
by gerryg400

Code: Select all

vidmem[i*2] = "H";//Print H on screen
And that the characters are characters. (and not strings).

Re: help: loading kernel

Posted: Tue Jul 20, 2010 6:45 pm
by pcmattman
while(1){
vidmem[i*2] = "H";//Print H on screen
vidmem[i+1] = 0x07;//Set H color to WHITE
i++;
}
... and that your array offsets are valid.

Re: help: loading kernel

Posted: Tue Jul 20, 2010 7:49 pm
by gerryg400

Code: Select all

     int i = 0;
     while(1){
          vidmem[i*2] = "H";//Print H on screen
          vidmem[i+1] = 0x07;//Set H color to WHITE
          i++;
     }
And that your array index can't go negative.