Problem getting GRUB to load my kernel :(

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
Caleb1994
Member
Member
Posts: 83
Joined: Sun Feb 13, 2011 4:55 pm

Problem getting GRUB to load my kernel :(

Post by Caleb1994 »

I had kind of left the whole OS devel side of programming for a while because its a little confusing when you are just starting out :P but I'm back with a cleared head :D

Okay, I have a simple (one assembly source file) kernel image for GRUB to boot, but it's telling me it's not a valid executable format! I'm compiling with NASM with an output of flat binary. I have a Multiboot header (just the required values). Here is my one assembly file :P

Code: Select all

[BITS 32]
%include "grub.inc"

start:

	cli				; Block interrupts (they can awaken the CPU from halt!)
	hlt				; Halt the CPU

ALIGN 4
mboot:
	dd MULTIBOOT_HEADER_MAGIC
	dd MULTIBOOT_HEADER_FLAGS
	dd MULTIBOOT_CHECKSUM
Of course, that doesn't do anything, but everything is stripped down just to get it booted.
Here is "grub.inc"

Code: Select all

MULTIBOOT_PAGE_ALIGN	equ 1<<0
MULTIBOOT_HEADER_MAGIC	equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS	equ MULTIBOOT_PAGE_ALIGN
MULTIBOOT_CHECKSUM	equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
I compile with:

Code: Select all

nasm -f bin -o kernel.bin ./kernel_entry.asm
and then I mount my virtual hard disk using a loop back, and copy that image to "/boot/"

I use qemu to boot the hdd with this call:

Code: Select all

qemu -hda ./SimpleKernel.hdd -m 128 "$@"
The "$@" is because that is actually in a shell script. That way I can pass extra parameters to qemu if needed.

once in grub, I type "kernel /boot/kernel.bin" and I get this message:
Error 13: Invalid or unsupported executable format
I am using grub v0.97 (yes I know, it's old, but I had it laying around, and it was simple to install :P)

Any help would be greatly appreciated!

Thanks,
Caleb
Yargh
Member
Member
Posts: 56
Joined: Sat Jun 12, 2010 9:04 pm
Location: Somewhere else.

Re: Problem getting GRUB to load my kernel :(

Post by Yargh »

It would be much easier for you if you would assemble it as an ELF (not flat binary). But, if you really want to use flat binary, what you can do (iirc) is include the aout kludge in the multiboot header. This will probably require a linker script to get the values for.

Code: Select all

extern kernelCodeAddr
extern kernelBssAddr
extern kernelEndAddr

;...

mboot:
        dd MULTIBOOT_HEADER_MAGIC
        dd MULTIBOOT_HEADER_FLAGS
        dd MULTIBOOT_CHECKSUM
        dd mboot
        dd kernelCodeAddr
        dd kernelBssAddr
        dd kernelEndAddr
        dd start

Wait... What?
Caleb1994
Member
Member
Posts: 83
Joined: Sun Feb 13, 2011 4:55 pm

Re: Problem getting GRUB to load my kernel :(

Post by Caleb1994 »

I just tried compiling with ELF format, and I get the same error :/

I just changed the "-f bin" in the nasm call to "-f elf" and I got the same result. If I can use ELF format, I would assume that would be simpler. I just didn't realize I could use it.

I had originally had an example from here: http://www.osdever.net/tutorials/view/w ... e-c-kernel, but that didn't even run using grub! :O

I stripped it down to what you saw in my original post (removing the C code, and therefore the linker script and the aout kludge) to just make it a straight up assembly binary to see if that even worked.

Let's start over, since it appears my tutorial wasn't even valid. how would you (anyone really) go about compiling a simple kernel for grub?

EDIT

I was getting frustrated and starting googling (again lol) and found this tutorial: http://www.osdever.net/bkerndev/Docs/basickernel.htm his linker script was slightly different, but his multiboot header is the same. It worked! Of course, my k_printc function needs some work (it's printing characters diagonally :P) but I'm very happy since it's printing something at all! :) I now need to figure out what's wrong with the original linker script... lol
Post Reply