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

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
marius__
Posts: 2
Joined: Mon Mar 24, 2008 11:25 am

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

Post 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
JJeronimo
Member
Member
Posts: 202
Joined: Wed Oct 18, 2006 3:29 pm

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

Post 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
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Post 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
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
marius__
Posts: 2
Joined: Mon Mar 24, 2008 11:25 am

Post 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 :)
Last edited by marius__ on Wed Mar 26, 2008 6:17 am, edited 1 time in total.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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.
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post 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
Post Reply