Search code examples
assemblyx86osdevmultiboot

Booting custom kernel in Grub2


I want to boot a custom kernel with Grub2. I used old (grub 1) multiboot header:

.set flags, 0x0
.set magic, 0x1badb002
.set checksum, -(magic + flags)

.align 4
.long magic
.long flags
.long checksum

...

movl %eax, magic

But it doesn't work with Grub2, so I've searched for new Multiboot specification and found it: http://bzr.savannah.gnu.org/lh/grub/branches/multiboot2/annotate/head:/doc/multiboot2.h (it's not a real specification, it's just a header file)

So, now I am using this multiboot header:

.set flags, 0x0
.set magic, 0xe85250d6
.set magic_the_second, 0x36d76289
.set checksum, -(magic + flags)

.align 4
.long magic
.long flags
.long checksum

...

movl %eax, magic_the_second

But it also doesn't work: No multiboot header

Any suggestions?

Thanks!


Solution

  • Multiboot 2 uses a different structure than the original multiboot (namely, it uses tag structures).

    Here's an example header from one of my older projects:

        # multiboot 2 header (see http://download-mirror.savannah.gnu.org/releases/grub/phcoder/multiboot.pdf)
        .balign 8
    mbhdr:
        .long 0xe85250d6 # magic
        .long 0 # architecture (i386, 32-bit)
        .long .LhdrEnd-mbhdr # header length
        .long -(.LhdrEnd-mbhdr+0xe85250d6) # checksum
        # tags
        # module align
        .word 6 # type
        .word 0 # flags
        .long 8 # size in bytes (spec says 12?)
        .balign 8
        # loader entry
        .word 3
        .word 0
        .long 12
        .long entry
        .balign 8
        # console flags
        .word 4
        .word 0
        .long 12
        .long 0x03 # EGA text support, require console
        .balign 8
        # info request 
        .word 1
        .word 0
        .long 4*6+8
        .long 5 # BIOS boot device
        .long 1 # command line
        .long 3 # modules
        .long 9 # ELF symbols
        .long 6 # memory map
        .long 10 # APM table
        .balign 8
        # address info
        .word 2 # type
        .word 0 # flags
        .long 24 # size
        .long mbhdr # header load addr
        .long 0x100000 # load addr
        .long 0 # load end addr (entire file)
        .long 0 # BSS end addr (no BSS)
        .balign 8
        # terminating tag
        .word 0
        .word 0
        .long 8
    .LhdrEnd: