grub can't 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
Whatever5k

grub can't load my kernel

Post by Whatever5k »

I managed to build my kernel and to install grub on a floppy disk... .
But when grub tries to boot my kernel, he sais
"Invalid or unsupported executable format"
What do I do wrong?
Bart Grantham

Re: grub can't load my kernel

Post by Bart Grantham »

Did you include a multiboot header?

http://www.mcc.ac.uk/grub/multiboot_3.html#SEC11

Here's my quick and dirty approach to doing it:

Code: Select all

struct multiboot_header
{
    unsigned long magic_MBH;
    unsigned long flags_MBH;
    unsigned long cksum_MBH;
} ;

struct multiboot_header my_mbh = { 0x1BADB002, 0x00000000, 0xE4524FFE } ;
Put this at the very beginning of your code as it needs to get compiled into the first 8K of our kernel.  I strongly suggest reading the doc first before trying the code, in case it doesn't work.
Whatever5k

Re: grub can't load my kernel

Post by Whatever5k »

Thanks, I did not know that... .
But it still does not work... .
I put the multiboot_header at the beginning of main(), but GRUB doesn't want to load it... .
I compiled my kernel with
gcc -c main.c
And then I linked it with
ld -Ttext 0x100000 -oformat binary -o kernel.bin main.o
The linker sais, that he couldn't find _start and he defaulted to 00100000. What does this mean and what does the -Ttext 0x100000 mean?
The Legend

Re: grub can't load my kernel

Post by The Legend »

As you don't have the C-runtimelib the first function of your
code is not main anymore, it is void _start (void)!
K.J.

Re: grub can't load my kernel

Post by K.J. »

The linker sais, that he couldn't find _start and he defaulted to 00100000. What does this mean and what does the -Ttext 0x100000 mean?

That is because gcc -c kernel.c outputs a .o file which is in COFF format. I suggest that you output to a different output(like aout). Also, you might consider making an ASM "boiler plate" that gets loaded first and then runs your kernel's main function(I can send you a zip file with an example of this, just email me).

K.J.
Whatever5k

Re: grub can't load my kernel

Post by Whatever5k »

@K.J:
Why sould I do that? I use GRUB for loading my kernel... .

Well, even if I use _start, it does not work... .
Allright, I'll post my source code, perhaps somebody can help me then.
Here's my main.c:

Code: Select all

/*main.c*/

void write_string (int colour, char *string)
{
 ? ? ?char *video=(char*)0xB8000;
 ? ? ?while(*string!=0)
 ? ? ?{
 ? ? ? ? ? ?*video=*string;
 ? ? ? ? ? ?string++;
 ? ? ? ? ? ?video++;
 ? ? ? ? ? ?*video=colour;
 ? ? ? ? ? ?video++;
 ? ? ?}
}

typdef struct multiboot_header
{
 ? ? ?unsigned long magic_MBH;
 ? ? ?unsigned long flags_MBH;
 ? ? ?unsigned long chksum_MBH;
}MULTIBOOTHEADER;

void _start (void)
{
 ? ? ?MULTIBOOTHEADER my_mbh = {0x1BaDB002, 0x00000000, 0xE4524FFE};
 ? ? ?main();
}

int main(void)
{
 ? ? ?write_string (5, "hello, world");
 ? ? ?while(1);
 ? ? ?return 0;
}
Well, that's it and it's just a test OS.. .
I compile and link it like this:

gcc -c main.c
ld -o kernel.elf --oformat elf32-i386 -Ttext 0x100000 mail.o

Then I just copy it to the floppy:
cp kernel.elf /floppy

And I try to boot it with GRUB

Anybody now can help me?
K.J.

Re: grub can't load my kernel

Post by K.J. »

Um, I guess I didn't explain enough, you don't need to write your own _start function. Your compiler does that for you. The _start function that the compiler makes is what calls your main function.

The linker sais, that he couldn't find _start and he defaulted to 00100000. What does this mean and what does the -Ttext 0x100000 mean?

The -Ttext 0x100000 means that the code's offset stuff is setup so that it will work when the kernel is loaded to 0x100000 in RAM.

My suspician is that GRUB doesn't load the kernel to 0x100000. However I'm not totaly sure, so you should probably check out the GRUB manual.

Hope that helps,
K.J.
Whatever5k

Re: grub can't load my kernel

Post by Whatever5k »

The -Ttext 0x100000 means that the code's offset stuff is setup so that it will work when the kernel is loaded to 0x100000 in RAM
Ah, so the -Ttext 0x100000 means, that the CS (code segment) is stored at this address of the output file? Allright, but how does this help? Or do you mean, that GRUB will search for the code segment in offset 0x100000?
Thanks for replying!
Bart Grantham

Re: grub can't load my kernel

Post by Bart Grantham »

The -Ttext 0x100000 means that you've statically compiled the source so that it will run at that location and ONLY that location.  Otherwise all the symbols will be dynamic and you'll need a dynamic linker (read: a program loader) to load the program into memory and set up all the symbols properly.

I don't know about aout or coff, but if you compile to elf, the elf binary format will contain linking information that GRUB will use to load your file.  I am currently using gcc and the full bintools distro so that I can make static elf files.  Throw the file on the test boot disk and away we go....

I got your email about the boot disk, I'll send along an image later today.
Bart Grantham

Re: grub can't load my kernel

Post by Bart Grantham »

BTW, there's a lot more to this discussion, depending on what binary format you choose to go with and how you'd like to load the kernel.  Grub also supports loading a dynamicly linked kernel if you give it the address to load to in menu.lst.

I know that the options are so vague and wide-open that it's probably bewildering, especially when you're trying to write code under such circumstances.  Hang in there and be sure to read as much documentation as possible.  As soon as you have the knowledge to make decisions and focus down on the things that are important to you, it gets MUCH easier.
Post Reply