Page 1 of 1

Grub Error 13 has me at wits' end...

Posted: Tue Mar 25, 2008 7:40 pm
by marius__
Hello everyone,

I am a young programmer in way over my head. With that out of the way, let me explain my predicament:

I decided to start simple, using the barebones kernel provided in the OSDev Wiki (very useful for a beginner like me; kudos to whoever wrote that article)

Anyway, I copied the code in the tutorial, except that since I run a 64-bit OS (i.e, GAS and GCC both like to produce ELF64 output) and I plan to build a 64 bit OS anyway, I dedided to change the program so it would be 64-bit compatible by simply replacing the %exx registers with %rxx. I changed nothing else.

I built the code as follows:


$as loader.s -o loader.o
$gcc -o kernel.o -c kernel.c -Wall -Werror -nostdlib -nostartfiles -nodefaultlibs
$ld -T linker.ld loader.o kernel.o


When I try to emulate the produced a.out file on a GRUB boot-floppy image, GRUB gives me the infamous error 13 (Invalid or unsupported executable format)

Reading the wiki page more closely, I saw that this could be a symptom of a bad multiboot header. I used mbchk, and:

$mbchk a.out
a.out: No Multiboot header.

it's worth noting that:

$mbchk loader.o
loader.o: The Multiboot header is found at the offset 64.
loader.o: Page alignment is turned on.
loader.o: Memory information is turned on.
loader.o: Address fields is turned off.
loader.o: All checks passed.


Why is this? Can anyone help? Is it an issue w/ my linker.ld script??

P.S: I'm sorry to post such a n00bish question, but I couldn't find an answer in the wiki, or the forums

Re: Grub Error 13 has me at wits' end...

Posted: Tue Mar 25, 2008 8:00 pm
by JJeronimo
marius__ wrote:Anyway, I copied the code in the tutorial, except that since I run a 64-bit OS (i.e, GAS and GCC both like to produce ELF64 output) and I plan to build a 64 bit OS anyway, I dedided to change the program so it would be 64-bit compatible by simply replacing the %exx registers with %rxx. I changed nothing else.
It's not enough replacing the 32-bit register names by 64-bit ones. You need to setup 64-bit Long Mode (which is a bit complicated) before actually being able to write 64-bit code... For now, I would suggest that you do your experiences in 32-bit Protected mode since it's much more documented than 64-bit mode, and move later to long mode, when you have a broader overview of what it involves.

Anyway, 64-bit information on:
http://www.osdev.org/wiki/X86-64

JJ

Posted: Tue Mar 25, 2008 8:39 pm
by piranha
I don't think that grub supports 64-bit.
Try compiling with:
gcc -m32
ld -m elf_i386
nasm -f elf32
gas --32
And add your flags. That compiles into 32bit, just make your kernel 32-bit and it should work.

-JL

Posted: Wed Mar 26, 2008 5:35 am
by marius__
Thanks for your help. Next time I won't be so stupid and hasty when I try to do something I don't understand.

While I'll use 32bit mode for now, when I do use 64 bit mode in the future, how will I get GRUB to support it? Will I need the AOUT kludge or some other code change or a totally different bootloader?

Thanks again. I'll be more careful next time.

EDIT EDIT: I got the chance to test this out & thought it didn't work. It turned out I was using an old floppy image containing my 64-bit kernel. When I used the right img, it worked like a charm :)

Posted: Wed Mar 26, 2008 6:04 am
by JamesM
EDIT: Got the chance to test what you recommended, compiled the code (this time taken directly from the barebones tutorial) using the same options as before plus the options for 32 bit output files. mbchk this time gives a more favorable output (similar to the one I posted above for loader.o) but GRUB still refuses the new file, giving error 13.
What format is the file in? pure binary? ELF? or... are you using cygwin's tools and haven't realised that it outputs a PE file?

objdump it next and check the multiboot header is in there properly.
Could there be something wrong with my GRUB bootdisk-image, and, if so, where could I find a better one?
Probably not. Try downloading an image that works (you'll find some at www.jamesmolloy.co.uk) and try that - put your kernel on that disk or the kernel you find there on your disk, and see what happens.

Posted: Wed Mar 26, 2008 8:59 am
by JAAman
when I do use 64 bit mode in the future, how will I get GRUB to support it? Will I need the AOUT kludge or some other code change or a totally different bootloader?
when you move to LMode later, you will continue to let GRUB boot a PMode portion, which will then switch into LMode