Multiboot 2

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
iamnoob
Posts: 13
Joined: Mon Oct 19, 2015 12:36 pm

Multiboot 2

Post by iamnoob »

I'm trying to use Multiboot 2 (http://nongnu.askapache.com/grub/phcoder/multiboot.pdf) but I'm getting a "no multiboot header" found error.

boot.S

Code: Select all

.extern kernel_main

.section .multiboot2

multiboot2_header_start:
/* magic number */
.long	0xe85250d6
/* architecture */
.long	0
/* header size */
.long	multiboot2_header_end - multiboot2_header_start
/* checksum */
.long	-(0xe85250d6 + (multiboot2_header_end - multiboot2_header_start))

/* tag end */
.align 8
.short	0
.short	0
.long	8
multiboot2_header_end:

# Reserve a stack for the initial thread.
.section .bootstrap_stack, "aw", @nobits
stack_bottom:
.skip 16384 # 16 KiB
stack_top:

.section .text.init

.global _start
.type _start, @function
_start:
	movl $stack_top, %esp

	/* Reset EFLAGS. */
	pushl   $0
	popf

	pushl %ebx
	pushl %eax

	call kernel_main

	cli
	hlt
.Lhang:
	jmp .Lhang

# Set the size of the _start symbol to the current location '.' minus its start.
# This is useful when debugging or when you implement call tracing.
.size _start, . - _start	
linker.ld

Code: Select all

BOOT_ADDRESS	= 0x00100000;

PAGE_SIZE		= 0x1000;

ENTRY(_start)
SECTIONS {
	. = BOOT_ADDRESS;
	.text.init : {
		. = ALIGN(8);
		*(.multiboot2)
		*(.text.init)
	} : boot

	.data.init : {
		*(.data.init)
	}

	.bss.init : {
		*(.bss.init)
	}

	.text ALIGN(PAGE_SIZE) : AT(ADDR(.text)) {
		*(.text)
	} : text

	.rodata ALIGN(PAGE_SIZE) : AT(ADDR(.rodata)) {
		*(.rodata)
	} : rodata

	.data ALIGN(PAGE_SIZE) : AT(ADDR(.data)) {
		*(.data)
	} : data

	.bss ALIGN(PAGE_SIZE) : AT(ADDR(.bss)) {
		*(.bss)
		*(.bootstrap_stack)
	}

	kernel_image_size = .;
}

PHDRS {
	headers	PT_PHDR FILEHDR PHDRS;
	boot	PT_LOAD;
	text	PT_LOAD;
	rodata	PT_LOAD;
	data	PT_LOAD;
}
multiboot.h

http://fossies.org/linux/grub/include/multiboot2.h

main.cpp

Code: Select all

#include <stddef.h>
#include <stdint.h>

void WriteCharacter(unsigned char c, unsigned char forecolour, unsigned char backcolour, int x, int y)
{
     uint16_t attrib = (backcolour << 4) | (forecolour & 0x0F);
     volatile uint16_t * where;
     where = (volatile uint16_t *)0xB8000 + (y * 80 + x) ;
     *where = c | (attrib << 8);
}

extern "C" 
void kernel_main(uint32_t magic, uint32_t addr)
{
	WriteCharacter('A', 15, 0, 0, 0);
}
grub.cfg

Code: Select all

menuentry "myos" {
	multiboot /boot/myos.bin
}
mkiso.sh

Code: Select all

#!/bin/bash
mkdir -p isodir/boot/grub
cp myos.bin isodir/boot/myos.bin
cp grub.cfg isodir/boot/grub/grub.cfg
grub2-mkrescue -o myos.iso isodir
Compile commands

Code: Select all

i686-elf-as boot.S -o boot.o
i686-elf-g++ -std=c++14 -Wall -Wextra -pedantic -O2 -ffreestanding -fno-exceptions -fno-rtti -c main.cpp -o main.o
i686-elf-g++ -T linker.ld -o myos.bin -ffreestanding -O2 -nostdlib boot.o main.o -lgcc
And I think I have the right grub version:

Code: Select all

grub2-mkrescue --version
grub2-mkrescue (GRUB) 2.02~beta2
Most examples I've seen using Multiboot 2 are targetted for 64-bit and use their own bootloader. I'm starting to think either I have the wrong version of Grub or Grub doesn't implement Multiboot 2.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Multiboot 2

Post by kzinti »

Ya, I lost an hour on that one myself.

Change:

Code: Select all

menuentry "myos" {
   multiboot /boot/myos.bin
}
To:

Code: Select all

menuentry "myos" {
   multiboot2 /boot/myos.bin
}
Grub... Seriously...
iamnoob
Posts: 13
Joined: Mon Oct 19, 2015 12:36 pm

Re: Multiboot 2

Post by iamnoob »

kiznit wrote:Ya, I lost an hour on that one myself..
HAHAHA. Thank you so much.
JackieWhite
Posts: 1
Joined: Fri Nov 01, 2019 9:37 pm
Libera.chat IRC: Jackie365

Re: Multiboot 2

Post by JackieWhite »

Thanks a lot!
I almost gave up on multiboot2
Post Reply