From b83b28b21571f9b1fadd251295f5ca03d9dfedc7 Mon Sep 17 00:00:00 2001 From: Xander Date: Wed, 28 Jun 2023 17:20:46 +0200 Subject: Booting to protected mode --- .gitignore | 1 + Makefile | 36 +++++++++++++++++++++ flake.lock | 61 ++++++++++++++++++++++++++++++++++++ src/arch/x86_64/boot.asm | 8 +++++ src/arch/x86_64/grub.cfg | 7 +++++ src/arch/x86_64/linker.ld | 16 ++++++++++ src/arch/x86_64/multiboot_header.asm | 15 +++++++++ 7 files changed, 144 insertions(+) create mode 100644 Makefile create mode 100644 flake.lock create mode 100644 src/arch/x86_64/boot.asm create mode 100644 src/arch/x86_64/grub.cfg create mode 100644 src/arch/x86_64/linker.ld create mode 100644 src/arch/x86_64/multiboot_header.asm diff --git a/.gitignore b/.gitignore index 567609b..ff72c83 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build/ +.ccls-cache/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6d4c9a8 --- /dev/null +++ b/Makefile @@ -0,0 +1,36 @@ +arch ?= x86_64 +kernel := build/kernel-$(arch).bin +iso := build/os-$(arch).iso + +linker_script := src/arch/$(arch)/linker.ld +grub_cfg := src/arch/$(arch)/grub.cfg +assembly_source_files := $(wildcard src/arch/$(arch)/*.asm) +assembly_object_files := $(patsubst src/arch/$(arch)/%.asm, \ + build/arch/$(arch)/%.o, $(assembly_source_files)) + +.PHONY: all clean run iso + +all: $(kernel) + +clean: + @rm -r build + +run: $(iso) + @qemu-system-x86_64 -cdrom $(iso) + +iso: $(iso) + +$(iso): $(kernel) $(grub_cfg) + @mkdir -p build/isofiles/boot/grub + @cp $(kernel) build/isofiles/boot/kernel.bin + @cp $(grub_cfg) build/isofiles/boot/grub + @grub-mkrescue -o $(iso) build/isofiles 2> /dev/null + @rm -r build/isofiles + +$(kernel): $(assembly_object_files) $(linker_script) + @ld -n -T $(linker_script) -o $(kernel) $(assembly_object_files) + +# compile assembly files +build/arch/$(arch)/%.o: src/arch/$(arch)/%.asm + @mkdir -p $(shell dirname $@) + @nasm -felf64 $< -o $@ diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..35ff5fb --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1687709756, + "narHash": "sha256-Y5wKlQSkgEK2weWdOu4J3riRd+kV/VCgHsqLNTTWQ/0=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "dbabf0ca0c0c4bce6ea5eaf65af5cb694d2082c7", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1687886075, + "narHash": "sha256-PeayJDDDy+uw1Ats4moZnRdL1OFuZm1Tj+KiHlD67+o=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "a565059a348422af5af9026b5174dc5c0dcefdae", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/src/arch/x86_64/boot.asm b/src/arch/x86_64/boot.asm new file mode 100644 index 0000000..1a531c0 --- /dev/null +++ b/src/arch/x86_64/boot.asm @@ -0,0 +1,8 @@ +global start + +section .text +bits 32 +start: + ; print `OK` to screen + mov dword [0xb8000], 0x2f4b2f4f + hlt diff --git a/src/arch/x86_64/grub.cfg b/src/arch/x86_64/grub.cfg new file mode 100644 index 0000000..7c34c9c --- /dev/null +++ b/src/arch/x86_64/grub.cfg @@ -0,0 +1,7 @@ +set timeout=0 +set default=0 + +menuentry "ats-os" { + multiboot2 /boot/kernel.bin + boot +} diff --git a/src/arch/x86_64/linker.ld b/src/arch/x86_64/linker.ld new file mode 100644 index 0000000..5d788f1 --- /dev/null +++ b/src/arch/x86_64/linker.ld @@ -0,0 +1,16 @@ +ENTRY(start) + +SECTIONS { + . = 1M; + + .boot : + { + /* ensure that the multiboot header is at the beginning */ + *(.multiboot_header) + } + + .text : + { + *(.text) + } +} diff --git a/src/arch/x86_64/multiboot_header.asm b/src/arch/x86_64/multiboot_header.asm new file mode 100644 index 0000000..9a9289c --- /dev/null +++ b/src/arch/x86_64/multiboot_header.asm @@ -0,0 +1,15 @@ +section .multiboot_header +header_start: + dd 0xe85250d6 ; magic number (multiboot 2) + dd 0 ; architecture 0 (protected mode i386) + dd header_end - header_start ; header length + ; checksum + dd 0x100000000 - (0xe85250d6 + 0 + (header_end - header_start)) + + ; insert optional multiboot tags here + + ; required end tag + dw 0 ; type + dw 0 ; flags + dd 8 ; size +header_end: -- cgit v1.2.3