Skip to contentSkip to conversation

multiboot header error

DISCUSSION Constantine Kousoulos 1 participant 3 messages May 29, 2007 – May 31, 2007

lore
Share & tools
lore
#0 lore raw

After successfully installing GRUB2 from cvs (thanks Jerone!), i decided to further test it on my amd64 machine.

At first, i built the sample kernel from http://www.gnu.org/software/grub/manual/multiboot/multiboot.html#Example-OS-code using the '-m32' gcc flag and by marking boot.S with the '.code32' directive. All these effectively created an elf32 image on my amd64 machine which built and booted without any trouble. :)

Then i proceeded to check if i can make GRUB2 boot the same kernel as a native elf64 image. So, i removed the above mentioned flag and directive, renamed any 'pushl' instruction to 'pushq', renamed registers 'eax' to 'rex' and 'ebx' to 'rbx', modified the magic number (0xe85250d6) and alingned it to 64-bits. When i tried to boot it i got: "error: No multiboot header found". :(

Is there something i did or didn't do correctly? I would greatly appreciate any feedback!

Thanks,
Constantine

#1 lore raw

Here is a sample kernel, built as elf64, that simply refuses to boot. It consists of only two files: boot.S and multiboot.h. It does absolutely nothing and it is a cut-down version of the kernel found at http://www.gnu.org/software/grub/manual/multiboot/multiboot.html#Example-OS-code or in grub-0.97/docs.

boot.S:
#include "multiboot.h"
.text
.globl start, _start
start:
_start:

	jmp	multiboot_entry

	/* Align 64 bits boundary.  */
	.align	8

multiboot_header:
.long MULTIBOOT_HEADER_MAGIC
.long MULTIBOOT_HEADER_FLAGS
/* .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)*/

multiboot_entry:

	/* Initialize the stack pointer.  */
	movq	$(stack + STACK_SIZE), %rsp

	/* Reset EFLAGS.  */
	pushq	$0
	popf

	/* Push the pointer to the Multiboot information structure.  */
	pushq	%rbx
	/* Push the magic value.  */
	pushq	%rax

loop: hlt

	jmp	loop

	/* Our stack area.  */
	.comm	stack, STACK_SIZE

multiboot.h:
#define MULTIBOOT_HEADER_MAGIC 0xe85250d6
#define MULTIBOOT_HEADER_FLAGS 0x00000000
#define STACK_SIZE 0x4000

Makefile.am:
noinst_PROGRAMS = kernel
kernel_SOURCES = boot.S multiboot.h kernel_CFLAGS = -fno-builtin -nostdinc -O -g -Wall kernel_LDFLAGS = -nostdlib

All i get from GRUB2 is "error: No multiboot header found". I have tried changing the magic number to 0xd65052e8 to make sure i got it ok, but it still doesn't boot. The flags are all zero according to the next version of the Multiboot Spec. The same document doesn't say anything about a checksum in the multiboot header, so i commented it out.

What do you think is to blame for this behaviour?

Thanks,
Constantine

#2 lore raw

Since i didn't get any feedback, i was forced to use the force and go to the source. I found out a few amazing things.

First of all, there is a serious inconsistency between the next version of the Multiboot Specification and the current code. The spec mentions: "[..] The header must start with a 32-bit magic number 0xe85250d6 in native endian [..]'. However, there is no such magic number defined in the source code!! No wonder i got "error: No multiboot header found". Grub2 always checks for the standard magic number 0x1badb002.

So, after i changed the multiboot header to the old magic number i got over the annoying error message and stumbled onto another. The cause of this error is this piece of code from grub2/loader/i386/pc/multiboot.c (line 210, inside grub_multiboot_load_elf64() ):

	  if (grub_file_read (file, (void *) ((grub_uint32_t) phdr->p_paddr),
			      phdr->p_filesz)
              != (grub_ssize_t) phdr->p_filesz)
	    return grub_error (GRUB_ERR_BAD_OS,
			       "couldn't read segment from file");

That's right, i get "error: couldn't read segment from file".

For once more i call upon the experience of the Grub2 developers. Is this a bug of grub_read_file()? The loaded elf64 file is so small that it should have no problem fitting into physical memory. In addition to that, 'objdump -D mykernel' shows that the kernel is loaded at address 0x400120 which is within the usable physical RAM map.

Sorry for troubling you with all these emails, but i need to load
an elf64 kernel for an os project i'm working on.

Thanks,
Constantine