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
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;
}
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);
}
Code: Select all
menuentry "myos" {
multiboot /boot/myos.bin
}
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
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
Code: Select all
grub2-mkrescue --version
grub2-mkrescue (GRUB) 2.02~beta2