12 Replies - 3815 Views - Last Post: 12 March 2012 - 09:55 AM

#1 Sinned  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 206
  • Joined: 13-October 10

[ASM] Load kernel from bootloader

Posted 14 January 2012 - 04:12 AM

Hello everyone,

I'm trying to write an "operating system" from scratch.
I use a floppy to boot.

Stage 1: - complete
I wrote a bootloader, which prints "Hello world". (BITS 16 - INT 0x10 - BIOS Print interrupt)

Stage 2: - in progress
There have to be 2 sectors on the floppy, both sectors have a size of 512 bytes:
- sector 1: bootloader
- sector 2: kernel
I want to let the bootloader load the kernel on the 2nd sector.
The kernel has to print "Hello world".

I just don't know how to start with "stage 2", because I have no idea how to load the 2nd sector on a floppy and how to call its main function.

My code is inserted on the bottom of the post.

I hope someone could help me with booting my OS.

Thanks in advance,

Sinned

Stage 1 - bootloader.asm:
[BITS 16] ;protected mode
[ORG 0x7C00] ;Bootloader base - loaded by kernel

;Set DS to 0
MOV AX, 0x0000
MOV DS, AX

;init print colors
MOV AH, 0x0E
MOV BH, 0x00
MOV BL, 0x07

MOV SI, msg ;Set Hello World message in SI
CALL PrintString

JMP $ ;hang

PrintString:
.next_char:
MOV AL, [SI] ;load current character in string
OR AL, AL ;see if AL is zero
JZ .char_done ;if AL is zero, jump to end (end of string reached)
INT 0x10 ;call print character command
INC SI ;increase SI, next character
JMP .next_char
.char_done:
RET

;Data section - never reaced
msg db 'Hello world', 13, 10, 0

TIMES 510 - ($ - $$) db 0 ;fill rest of file (needs a size of 512 bytes)
DW 0xAA55 ;end


Stage2 (plan) - bootloader.asm:
[BITS 16] ;protected mode
[ORG 0x7C00]

;these two calls still doesn't exist because I don't know how to make them
CALL LoadKernel
CALL CallKernelMain

TIMES 510 - ($ - $$) db 0 ;fill rest of file (needs a size of 512 bytes)
DW 0xAA55 ;end


Stage 2 (plan) - kernel.asm:
;don't know if headers are needed here
kernelMain:
;Set DS to 0
MOV AX, 0x0000
MOV DS, AX

;init print colors
MOV AH, 0x0E
MOV BH, 0x00
MOV BL, 0x07

MOV SI, msg ;Set Hello World message in SI
CALL PrintString

JMP $ ;hang

PrintString:
.next_char:
MOV AL, [SI] ;load current character in string
OR AL, AL ;see if AL is zero
JZ .char_done ;if AL is zero, jump to end (end of string reached)
INT 0x10 ;call print character command
INC SI ;increase SI, next character
JMP .next_char
.char_done:
RET

;Data sector
msg db 'Hello world', 13, 10, 0

TIMES 510 - ($ - $$) db 0 ;fill rest of file (needs a size of 512 bytes)
DW 0xAA55 ;end - don't know if this is needed here


Is This A Good Question/Topic? 1
  • +

Replies To: [ASM] Load kernel from bootloader

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1530
  • View blog
  • Posts: 5,519
  • Joined: 03-August 09

Re: [ASM] Load kernel from bootloader

Posted 14 January 2012 - 11:30 AM

the goal of a boot loader is to load the kernel. if the boot-loader has to load the kernel image from a complex file system then you generally need a 2 stage boot-loader(the first loads a second bigger one that loads the kernel and calls main in protected mode). were and how you store your kernel change how you load the kernel. if you store it on a hard drive then you need to know were on the hard drive; is it a regular file on a file system? or is it in it's own partition? or is a special set of sectors reserved explicitly for the kernel?

say you use FAT16 and you store your kernel in "system/kernel.bin" then you would most likely need a 2 stage boot loader. the first stage could parse the header and load the second stage while the second stage loaded kernel at a specific location in memory, gathered necessary information, entered protected mode, then called the first byte in the executable.

OSDev.org is great for this sort of stuff btw
Was This Post Helpful? 0
  • +
  • -

#3 Sinned  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 206
  • Joined: 13-October 10

Re: [ASM] Load kernel from bootloader

Posted 15 January 2012 - 07:05 AM

Yes... I already knew I had to let the bootloader load the kernel.
My question was just, how to do that. :P

But thanks for the link. (OSDev.org)
Was This Post Helpful? 0
  • +
  • -

#4 Shane Hudson  Icon User is offline

  • D.I.C Technophile
  • member icon

Reputation: 341
  • View blog
  • Posts: 1,281
  • Joined: 06-December 09

Re: [ASM] Load kernel from bootloader

Posted 15 January 2012 - 08:49 AM

It is nice to see a thread about building an operating system that actually has code in it! Good luck and I look forward to seeing how it goes, I wouldn't know where to start!
Was This Post Helpful? 0
  • +
  • -

#5 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1530
  • View blog
  • Posts: 5,519
  • Joined: 03-August 09

Re: [ASM] Load kernel from bootloader

Posted 15 January 2012 - 02:32 PM

Quote

My question was just, how to do that.


where are you storing it? hard drive? floppy? what file system?
what format is the kernel in? ELF, MZ, PE, raw?
where dose the kernel expect to be loaded?
what information dose the kernel expect to have upon start up?

you need to know ALL of these things first to load a kernel.

edit:
also, have you considered using a boot loader like GRUB?

This post has been edited by ishkabible: 15 January 2012 - 02:34 PM

Was This Post Helpful? 1
  • +
  • -

#6 Sinned  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 206
  • Joined: 13-October 10

Re: [ASM] Load kernel from bootloader

Posted 19 January 2012 - 11:48 AM

View Postishkabible, on 15 January 2012 - 02:32 PM, said:

also, have you considered using a boot loader like GRUB?

I did, but I'm not going to use it.
For my PC running Linux I use GRUB to boot, but when I write a own OS I do also like a own boot loader.
Also, I'm not very experienced with ASM, so the boot loader was a nice thing to do.

I found the solution for my problem, now I know how to let the bootloader load the (small) kernel.
The rest of my project is NOT going to be open-source, but I will post my current "Hello world" code.
Here it is:
bootloader.asm
[BITS 16]
[ORG 0x7C00]

MOV DL, 0x0 ;drive 0 = floppy 1
MOV DH, 0x0 ;head (0=base)
MOV CH, 0x0 ;track/cylinder
MOV CL, 0x02 ;sector (1=bootloader, apparently sectors starts counting at 1 instead of 0)
MOV BX, 0x1000 ;place in RAM for kernel - I suppose randomly chosen on examples
MOV ES, BX ;place BX in pointer ES
MOV BX, 0x0 ;back to zero - also has something to do with RAM position

ReadFloppy:
MOV AH, 0x02
MOV AL, 0x01
INT 0x13
JC ReadFloppy ;if it went wrong, try again

;pointers to RAM position (0x1000)
MOV AX, 0x1000
MOV DS, AX
MOV ES, AX
MOV FS, AX
MOV GS, AX
MOV SS, AX

JMP 0x1000:0x0

;assuming we get never back here again, so no further coding needed (kernel handles everything now)

TIMES 510 - ($ - $$) db 0 ;fill resting bytes with zero
DW 0xAA55 ;end of bootloader (2 bytes)


kernel.asm
;set print-registers
MOV AH, 0x0E ;function nr
MOV BH, 0x00 ;page
MOV BL, 0x07 ;color

MOV SI, msg ;move msg to SI-pointer
CALL PrintString ;call function to print SI (msg)

JMP $ ;hang

PrintString:
.next_char:
MOV AL, [SI] ;current character
OR AL, AL
JZ .print_done ;if current char is zero, go to end
INT 0x10 ;print character
INC SI ;increase pointer to msg (next character)
JMP .next_char
.exit_char
RET

msg db 'Hello world from the kernel!', 13, 10, 0

TIMES 512 - ($ - $$) db 0 ;fill the rest


For the ones who don't know how to assemble - and put on floppy (code is written for floppy - and read sector from floppy) - here the assemble and write commands are (for Linux):
nasm bootloader.asm -f bin -o boot.bin
nasm kernel.asm -f bin -o kernel.bin
dd if=boot.bin of=/dev/fd0 bs=512 count=1
dd if=kernel.asm of=/dev/fd0 bs=512 count=1 seek=1


So, that's it.

Everyone who tried to help, or giving support, many thanks!

Sinned

This post has been edited by Sinned: 20 January 2012 - 07:36 AM

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,551
  • Joined: 27-December 08

Re: [ASM] Load kernel from bootloader

Posted 19 January 2012 - 11:49 AM

Please post Assembly questions in the Assembly forum. Moved to Assembly.
Was This Post Helpful? 0
  • +
  • -

#8 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 719
  • View blog
  • Posts: 1,978
  • Joined: 28-March 11

Re: [ASM] Load kernel from bootloader

Posted 19 January 2012 - 04:35 PM

View PostSinned, on 19 January 2012 - 01:48 PM, said:

I'm not very experienced with ASM

Quote

For the ones who don't know how to compile

That is nice, but get rid of the word compile if you are going to use Assembly. We do not and never have - compiled code, we assemble and or link code.
Was This Post Helpful? 0
  • +
  • -

#9 Sinned  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 206
  • Joined: 13-October 10

Re: [ASM] Load kernel from bootloader

Posted 20 January 2012 - 07:35 AM

View PostGunnerInc, on 19 January 2012 - 04:35 PM, said:

Quote

For the ones who don't know how to compile

That is nice, but get rid of the word compile if you are going to use Assembly. We do not and never have - compiled code, we assemble and or link code.

Aargh! :P
I know Assembly doesn't get compiled, but I automatically this word.
Every time I say "compiled" for Assembly, someone remembers me to stop using that word.

But thanks for remember me again, I'll edit the post.
Was This Post Helpful? 0
  • +
  • -

#10 uautkarsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-March 12

Re: [ASM] Load kernel from bootloader

Posted 11 March 2012 - 04:02 AM

Dude.. In this you made the kernel the same size as the bootloader(512 kbytes, a necessity of bootloader).. But kernels can also be put directly in to the floppy by simple copy and paste.. In you case, its kind of a second bootloader only i guess..
(I am still a beginner in the OS Development)
Was This Post Helpful? 0
  • +
  • -

#11 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1530
  • View blog
  • Posts: 5,519
  • Joined: 03-August 09

Re: [ASM] Load kernel from bootloader

Posted 11 March 2012 - 09:59 AM

512 kbytes? I wish, there wouldn't be a need for bootloaders if you had that much space; you would just write your kernel there :P it's actually only 512 bytes which is crazy small.

This post has been edited by ishkabible: 11 March 2012 - 10:04 AM

Was This Post Helpful? 0
  • +
  • -

#12 uautkarsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-March 12

Re: [ASM] Load kernel from bootloader

Posted 11 March 2012 - 11:55 AM

Oh.. yea.. Am sorry for the :gun_bandana: "k" there.. :tt2:
So still.. How can i load my own file (which is suppose on the floppy disk) using the bootloader..???
Was This Post Helpful? 0
  • +
  • -

#13 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1530
  • View blog
  • Posts: 5,519
  • Joined: 03-August 09

Re: [ASM] Load kernel from bootloader

Posted 12 March 2012 - 09:55 AM

is that a question? well you have to use the in/out ports to access the sectors of the floppy and read them into memory. depending on your kernel's format this could mean deciphering the file into executable code(reallocation tables and what not)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1