Page 1 of 1

Grub Multiboot with PE file

Posted: Sat Nov 03, 2007 1:05 pm
by hstagni
I was able to boot a "hello world" C kernel using GRUB, following a tutorial and I had to copy a small code in ASM that contains the header required for GRUB to boot it. Also, I had to compile the 'kernel' to ELF. Everything worked fine!(ok, not everything, i didn´t saw hello world on the screen, just a few letters with different collors but I fixed it already)

I was trying to see Multiboot Specification and it says I can boot a kernel in any format, it doesn´t need to be ELF. So, I recompiled the kernel with MINGW but it didn´t work. I was getting ERROR 13 in GRUB" Invalid or unsupported executable format ".

So, How can I compile the kernel using MinGW and make GRUB boot it?

Posted: Sat Nov 03, 2007 2:38 pm
by frank
The only problem with loading the PE format is that GRUB doesn't know how to parse it and you will have to provide it quite a bit of information on how to load it and where to jump too.

You can make grub load any file format by using the aout kludge. This uses additional fields at the end of the
Multiboot header, like this:

Code: Select all

    MULTIBOOT_PAGE_ALIGN   equ 1<<0
    MULTIBOOT_MEMORY_INFO  equ 1<<1
    MULTIBOOT_AOUT_KLUDGE  equ 1<<16

    MULTIBOOT_HEADER_MAGIC equ 0x1BADB002
    MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
    CHECKSUM               equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)

    ; The Multiboot header
        align 4
    mboot:
        dd MULTIBOOT_HEADER_MAGIC
        dd MULTIBOOT_HEADER_FLAGS
        dd CHECKSUM
    ; fields used if MULTIBOOT_AOUT_KLUDGE is set in MULTIBOOT_HEADER_FLAGS
        dd mboot ; these are PHYSICAL addresses
        dd code  ; start of kernel .text (code) section
        dd edata ; end of kernel .data section
        dd end   ; end of kernel BSS
        dd start ; kernel entry point (initial EIP)

NOTE: The "aout kludge" works with binary and other file
formats, too.   (xxx - untested; should be correct)
I'm sorry if this is a little confusing. If you have any more questions just ask.

EDIT: This tutorial also goes over the concept. You may have to read through some of the pages. http://www.osdever.net/bkerndev/index.php

Posted: Sun Nov 04, 2007 7:00 am
by hstagni
Thanks a lot for answering it.

I used this header and the tutorial´s linker script and I got this:

(kernel.o is the output of compiling kernel.c)
nasm -f aout loader.asm -o loader.o
ld -T link.ld -o kernel.exe loader.o kernel.o
----> ERROR: loader.o: file format not recognized

Then I changed the output format of loader.o. I tried all the formats nasm provides and I got the following error:
----->ERROR: cannot perform PE operations on non PE output file: "kernel.exe"

Posted: Sun Nov 04, 2007 7:28 am
by Craze Frog
hstagni wrote:Then I changed the output format of loader.o. I tried all the formats nasm provides and I got the following error:
----->ERROR: cannot perform PE operations on non PE output file: "kernel.exe"
The mingw port of ld doesn't work correctly with non-PE files, you will need to build a cross compiler toolchain. It's very easy, look at the guide in the wiki.

Posted: Sun Nov 04, 2007 8:53 am
by hstagni
OK, I used the wiki´s linker script(I had to add somethings of course) , and it worked! I saw something like HLOWRD with different colors that was supposed to be HELLO WORLD!

Thank you all!