Call C function from bootloader

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
remy
Posts: 23
Joined: Thu Nov 20, 2014 4:06 pm

Call C function from bootloader

Post by remy »

Hi,

I'm looking for a minimalist way to call a C function from my own bootloader (here from protected mode).
I give you here my bootloader, do you have any workable example to do so ?

Bootloader :

Code: Select all

[org 0x7C00]

cli
lgdt [gdt_descriptor] 

; Enter PM
mov eax, cr0
or	eax, 0x1
mov	cr0, eax

jmp 0x8:init_pm    ;jmp to the GDT code descriptor


[bits 32]
init_pm :
	mov ax, 0x10      ;load the GDT data descriptor
	mov ds, ax
	mov ss, ax
	mov es, ax
	mov fs, ax
	mov gs, ax

	;Any idea how to call my C function from here ?

	jmp $


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[bits 16]

GDT:
;null : 
	dd 0x0 
	dd 0x0

;code : 
	dw 0xffff		        ;Limit
	dw 0x0			;Base
	db 0x0			;Base
	db 0b10011010 	       ;1st flag, Type flag
	db 0b11001111 	       ;2nd flag, Limit
	db 0x0			;Base

;data : 
	dw 0xffff		
	dw 0x0			
	db 0x0
	db 0b10010010 
	db 0b11001111 
	db 0x0

gdt_descriptor :
	dw $ - GDT - 1 		        ;16-bit size
	dd GDT				;32-bit start address


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Bootsector padding
times 510-($-$$) db 0
dw 0xaa55

Here my simple kernel code :

Code: Select all

void main() {
	char * vga = (char *) 0xb8000 ;
	*vga = "X";
}
Many thanks for your solution.
NB: I rode the GRUB tutorial to load a C kernel, but I don't get a thing...!
brunexgeek
Member
Member
Posts: 45
Joined: Wed Dec 25, 2013 11:51 am

Re: Call C function from bootloader

Post by brunexgeek »

I assume you are not familiar with bootloaders. Basically, you need to load your kernel to a specific address and then jump to it.

Some usefull links:
http://wiki.osdev.org/Boot_Sequence#Loading
http://www.brokenthorn.com/Resources/OSDev3.html
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Call C function from bootloader

Post by neon »

Depending on your executable image format, scan the headers for the entry point or function symbol and call it. The only requirement is that you must have a way to pre-load the executable C code into memory with the boot loader or just load and execute it as an external program.

This is as simple as it gets. However, with that in mind, what you presented is not a boot loader -- its a boot record. It is responsible for loading and executing a boot loader or boot manager. I suggest saving the C code for the boot loader or kernel - not the boot record.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
b.zaar
Member
Member
Posts: 294
Joined: Wed May 21, 2008 4:33 am
Location: Mars MTC +6:00
Contact:

Re: Call C function from bootloader

Post by b.zaar »

This post might help to understand the separation between the bootloader and the C kernel with an assembly stub.

http://forum.osdev.org/viewtopic.php?f= ... 70#p242060
"God! Not Unix" - Richard Stallman

Website: venom Dev
OS project: venom OS
Hexadecimal Editor: hexed
remy
Posts: 23
Joined: Thu Nov 20, 2014 4:06 pm

Re: Call C function from bootloader

Post by remy »

I merged with this post which is the same topic : http://forum.osdev.org/viewtopic.php?f=1&t=28670
Post Reply