help: loading kernel

Programming, for all ages and all languages.
Post Reply
User avatar
Almamu
Member
Member
Posts: 47
Joined: Wed Jul 07, 2010 9:41 am

help: loading kernel

Post 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
Image
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: help: loading kernel

Post 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).
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Almamu
Member
Member
Posts: 47
Joined: Wed Jul 07, 2010 9:41 am

Re: help: loading kernel

Post 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.
Image
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: help: loading kernel

Post 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.
If a trainstation is where trains stop, what is a workstation ?
gedd
Member
Member
Posts: 104
Joined: Thu Apr 10, 2008 1:47 am

Re: help: loading kernel

Post 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
[ Grub 2 | Visual Studio 2013 | PE File ]
The OsDev E.T.
Don't send OsDev MIB !
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: help: loading kernel

Post by gerryg400 »

3 more. (I mis-counted before)
If a trainstation is where trains stop, what is a workstation ?
User avatar
Almamu
Member
Member
Posts: 47
Joined: Wed Jul 07, 2010 9:41 am

Re: help: loading kernel

Post 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...
Image
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: help: loading kernel

Post 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.
roboman
Posts: 24
Joined: Fri Feb 27, 2009 9:41 am
Location: USA
Contact:

Re: help: loading kernel

Post 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.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: help: loading kernel

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: help: loading kernel

Post by gerryg400 »

Code: Select all

vidmem[i*2] = "H";//Print H on screen
And that the characters are characters. (and not strings).
If a trainstation is where trains stop, what is a workstation ?
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: help: loading kernel

Post 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.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: help: loading kernel

Post 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.
If a trainstation is where trains stop, what is a workstation ?
Post Reply