From 379d8dbcdc35a7cf027be0eb76bd1d481b235497 Mon Sep 17 00:00:00 2001 From: Mark Date: Thu, 12 Dec 2024 07:14:44 -0800 Subject: [PATCH] Clean up build process --- Makefile | 30 +++++++++++++---- bios/{bootloader.asm => main.asm} | 0 make/bios | 54 ++++++++++++++++++++++++++++--- make/bootloader | 29 ----------------- 4 files changed, 72 insertions(+), 41 deletions(-) rename bios/{bootloader.asm => main.asm} (100%) delete mode 100644 make/bootloader diff --git a/Makefile b/Makefile index 847008d..113ef89 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,27 @@ BUILD_DIR=./build +export QEMU?=qemu-system-x86_64 + + +.PHONY: bios +bios: $(BUILD_DIR)/bios.bin $(BUILD_DIR)/bios.img .PHONY: clean - -all: $(BUILD_DIR)/bootloader.bin - -include make/bios -include make/bootloader - clean: - rm -drf $(BUILD_DIR) \ No newline at end of file + rm -drf $(BUILD_DIR) + cd bootloader; cargo clean + +qemu: $(BUILD_DIR)/bios.img + $(QEMU) \ + -d cpu_reset \ + -no-reboot \ + -smp 4 -m 2048 \ + -chardev stdio,id=debug,signal=off,mux=on \ + -serial chardev:debug \ + -mon chardev=debug \ + -machine q35 \ + -net none \ + -enable-kvm \ + -cpu host \ + -drive file="$<",format=raw + +include make/bios \ No newline at end of file diff --git a/bios/bootloader.asm b/bios/main.asm similarity index 100% rename from bios/bootloader.asm rename to bios/main.asm diff --git a/make/bios b/make/bios index 17ea865..a9dc9f8 100644 --- a/make/bios +++ b/make/bios @@ -1,12 +1,56 @@ -BIOS_SRC = ./bios -STAGE3_BIN = $(BUILD_DIR)/stage3.elf +# Build script for BIOS (legacy) boot. +# +# This compiles our bootloader as a static library, +# and wraps it in a multistage loader. -$(BUILD_DIR)/bootloader.bin: $(wildcard $(BIOS_SRC)/*.asm) $(STAGE3_BIN) + +# Compile bootloader as library +LIB_SRC = ./bootloader/Cargo.toml ./bootloader/Cargo.lock $(shell find ./bootloader/src -type f) +$(BUILD_DIR)/bios.lib: $(LIB_SRC) + @mkdir -p $(BUILD_DIR) + env RUSTFLAGS="-C soft-float" \ + cargo rustc \ + --manifest-path="./bootloader/Cargo.toml" \ + -Z build-std=core,alloc \ + -Z build-std-features=compiler-builtins-mem \ + --target "./bootloader/targets/x86-unknown-none.json" \ + --lib \ + --release \ + -- \ + --emit link="$(CURDIR)/$@" + +# Link bootloader +BIOS_LD = ./bootloader/linkers/x86-unknown-none.ld +$(BUILD_DIR)/bios.elf: $(BUILD_DIR)/bios.lib $(BIOS_LD) + ld \ + -m elf_i386 \ + --gc-sections \ + -z max-page-size=0x1000 \ + -T "$(BIOS_LD)" \ + -o "$@" \ + "$<" + + objcopy --only-keep-debug "$@" "$@.sym" + objcopy --strip-debug "$@" + +# Wrap bootloader in three-stage BIOS loader +BIOS_SRC = ./bios +$(BUILD_DIR)/bios.bin: $(wildcard $(BIOS_SRC)/*.asm) $(BUILD_DIR)/bios.elf @mkdir -p "$(BUILD_DIR)" nasm \ -f bin \ -o "$@" \ -l "$@.lst" \ - -D STAGE3="$(STAGE3_BIN)" \ + -D STAGE3="$(BUILD_DIR)/bios.elf" \ -i "$(BIOS_SRC)" \ - "$(BIOS_SRC)/bootloader.asm" \ No newline at end of file + "$(BIOS_SRC)/main.asm" + +# Build demo disk image +$(BUILD_DIR)/bios.img: $(BUILD_DIR)/bios.bin + rm -f "$@.partial" + fallocate -l 256MiB "$@.partial" + parted -s -a minimal "$@.partial" mklabel msdos + parted -s -a minimal "$@.partial" mkpart primary 2MiB 100% + dd if="$<" of="$@.partial" bs=1 count=512 conv=notrunc + dd if="$<" of="$@.partial" bs=512 skip=1 seek=1 conv=notrunc + mv "$@.partial" "$@" \ No newline at end of file diff --git a/make/bootloader b/make/bootloader deleted file mode 100644 index 73d68f9..0000000 --- a/make/bootloader +++ /dev/null @@ -1,29 +0,0 @@ -STAGE3_SRC = ./bootloader/Cargo.toml ./bootloader/Cargo.lock $(shell find ./bootloader/src -type f) -STAGE3_LD = ./bootloader/linkers/x86-unknown-none.ld - -# Compile stage 3 bootloader -$(BUILD_DIR)/stage3.lib.a: $(STAGE3_SRC) - @mkdir -p $(BUILD_DIR) - env RUSTFLAGS="-C soft-float" \ - cargo rustc \ - --manifest-path="./bootloader/Cargo.toml" \ - -Z build-std=core,alloc \ - -Z build-std-features=compiler-builtins-mem \ - --target "./bootloader/targets/x86-unknown-none.json" \ - --lib \ - --release \ - -- \ - --emit link="$(CURDIR)/$@" - -# Link stage 3 bootloader -$(BUILD_DIR)/stage3.elf: $(BUILD_DIR)/stage3.lib.a $(STAGE3_LD) - ld \ - -m elf_i386 \ - --gc-sections \ - -z max-page-size=0x1000 \ - -T "$(STAGE3_LD)" \ - -o "$@" \ - "$<" - - objcopy --only-keep-debug "$@" "$@.sym" - objcopy --strip-debug "$@" \ No newline at end of file