From 523bff7fce18544f713a71b9419af8d4ab9fa328 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Fri, 24 Nov 2017 22:32:11 +0100 Subject: [PATCH] Put 32bits code after 1sector and load it Use makefile to build Use ld linker script to create final bin --- Makefile | 23 +++++++++++++++++++++++ Readme.md | 4 ++-- linker.ld | 26 ++++++++++++++++++++++++++ mbr.asm | 31 ++++++++++++++++++++++--------- 4 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 Makefile create mode 100644 linker.ld diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b244601 --- /dev/null +++ b/Makefile @@ -0,0 +1,23 @@ +AS=nasm +ASFLAGS += -f elf32 +LDFLAGS += -m32 -nostdlib -static -fno-common -fno-use-cxa-atexit -fno-exceptions -fno-non-call-exceptions -fno-weak -fno-rtti +CFLAGS += -m32 -Wall -Wextra -Werror -ffreestanding -fno-exceptions +CXXFLAGS += -m32 -Wall -Wextra -Werror -ffreestanding -fno-exceptions -fno-rtti + + +asmsrc=$(wildcard *.asm) +asmobj=$(asmsrc:%.asm=%.o) +csrc=$(wildcard *.c) +cobj=$(csrc:%.c=%.o) + +kernel:$(asmobj) $(cobj) linker.ld + $(CXX) $(LDFLAGS) $(cobj) $(asmobj) -o $@ -T linker.ld + +%.o:%.asm + $(AS) $(ASFLAGS) -o $@ $< + +test:kernel + qemu-system-x86_64 -fda $< + +clean: + $(RM) kernel $(asmobj) $(cobj) diff --git a/Readme.md b/Readme.md index 1b8c973..8e27a7b 100644 --- a/Readme.md +++ b/Readme.md @@ -1,10 +1,10 @@ # Instruction Build with - `nasm -f bin mbr.asm -o mbr` + `make` Run with - `qemu-system-x86_64 -boot a -fda mbr` + `make test` Debug with `qemu-system-x86_64 -boot a -fda mbr -s -S` diff --git a/linker.ld b/linker.ld new file mode 100644 index 0000000..19d67b2 --- /dev/null +++ b/linker.ld @@ -0,0 +1,26 @@ +ENTRY(boot) +OUTPUT_FORMAT("binary") +SECTIONS { + . = 0x7c00; + .text : + { + *(.boot) + *(.text) + } + + .rodata : + { + *(.rodata) + } + + .data : + { + *(.data) + } + + .bss : + { + *(.bss) + } +} + diff --git a/mbr.asm b/mbr.asm index 2060fae..bc1ff9d 100644 --- a/mbr.asm +++ b/mbr.asm @@ -1,7 +1,8 @@ -bits 16 ; mode 16bits -org 0x7C00 ; mbr ae loaded at 0x7C00 - -jmp boot +section .boot +bits 16 +global boot +boot: + jmp main display_enable: push bp @@ -57,8 +58,9 @@ println: hello db 'Hello world', 0 name db 'This is a name', 0 -boot: +main: sti ; enable virtual interupts + mov [disk],dl ; save disk used to boot by bios call display_enable @@ -81,6 +83,15 @@ boot: mov ax, 0x3 int 0x10 + ; Bios read first 512 bytes, read next disk sector + mov ah, 0x2 ;read sectors + mov al, 6 ;sectors to read + mov ch, 0 ;cylinder idx + mov dh, 0 ;head idx + mov cl, 2 ;sector idx + mov dl, [disk] ;disk idx + mov bx, copy_target;target pointer + int 0x13 cli ; clear interruption flag @@ -113,10 +124,14 @@ gdt_end: gdt_pointer: dw gdt_end - gdt_start dd gdt_start - +disk: + db 0x0 CODE_SEG equ gdt_code - gdt_start DATA_SEG equ gdt_data - gdt_start +times 510 - ($-$$) db 0 +dw 0xaa55 +copy_target: bits 32 boot2: mov ax, DATA_SEG @@ -139,6 +154,4 @@ boot2: halt: cli hlt -hello32: db "Hello world!",0 -times 510 - ($ - $$) db 0 ; fill up to 510 with 0 -dw 0xAA55 ; MBR magic number +hello32: db "Hello 32 bits world!",0