multiboot header doesn't work in boot file

341 Views Asked by At

I'm trying to write a multiboot kernel compliant using the grub (legacy) specification , here is the content of my boot.s (written with the gnu as)

#include "boot.h"
    .code32
    .globl start
    .type start, @function
    .extern kernel_main
    .type kernel_main, @function
    .bss
    .comm stack, STACK_SIZE

    .section .mboot
    .align 4
    .long MBOOT_HEADER_MAGIC
    .long MBOOT_HEADER_FLAGS
    .long MBOOT_CHECKSUM
        .text
start:
    movl $(stack + STACK_SIZE) , %esp
    cli
    call kernel_main
    sti
    hlt
    jmp .

The boot.h file content some definition for grub:

#ifndef _BOOT_H_
#define _BOOT_H_
#define MBOOT_PAGE_ALIGN 1<<0
#define MBOOT_MEM_INFO 1<<1
#define MBOOT_HEADER_MAGIC 0x1BADB002
#define MBOOT_HEADER_FLAGS MBOOT_PAGE_ALIGN | MBOOT_MEM_INFO
#define MBOOT_CHECKSUM -( MBOOT_HEADER_MAGIC + MBOOT_HEADER_FLAGS)
#define STACK_SIZE 0x4000
#endif /* _BOOT_H_ */

I also set up the linker like this:

ENTRY(start)
OUTPUT_ARCH(i386)
OUTPUT_FORMAT("elf32-i386")
SECTIONS
{
    . = 1M;
    .mboot ALIGN(4K) :
    {
        *(.mboot)

    }
    .text ALIGN(4K) :
    {
        *(.text)
        *(.rodata)
    }
    .data ALIGN(4K) :
    {
        *(.data)
    }
    .bss ALIGN(4K) :
    {
        *(.bss)
    }
}

For the moment , my kernel_main simply return the value 0x01 which i'm expecting in the %eax register, however when i execute the kernel with:

qemu -kernel kernel.bin -monitor stdio

and then issue the command info registers in qemu , i always find a value of 18 for %eax. So my questions are: is my boot.s file correctly defined to be grub compliant? and why the value 18 in %eax instead of 0x01?

1

There are 1 best solutions below

3
On

Your image is most likely correct - QEMU will throw an error if it's not multiboot-compliant. eax is probably used by an interrupt handler after your call to kernel_main, because you enable interrupts with sti.