1
0
tetros/bootloader/Makefile
2025-02-17 10:45:02 -08:00

83 lines
2.1 KiB
Makefile

# This compiles our bootloader as a static library,
# and wraps it in a multistage loader.
BUILD = ./build
.PHONY: all
all: $(BUILD)/mbr.bin $(BUILD)/512.bin $(BUILD)/stage2.bin
.PHONY: clean
clean:
rm -drf $(BUILD)
cd bootloader; cargo clean
# Compile bootloader as library
LIB_SRC = ./bootloader/Cargo.toml ./bootloader/Cargo.lock $(shell find ./bootloader/src -type f)
$(BUILD)/bootloader.lib: $(LIB_SRC)
@mkdir -p $(BUILD)
cd bootloader && \
env RUSTFLAGS="-C soft-float" \
cargo rustc \
--manifest-path="./Cargo.toml" \
-Z build-std=core,alloc \
-Z build-std-features=compiler-builtins-mem \
--target "./targets/x86-unknown-none.json" \
--lib \
--release \
-- \
--emit link="$(CURDIR)/$@"
# Link bootloader
BIOS_LD = ./bootloader/linkers/x86-unknown-none.ld
$(BUILD)/bootloader.elf: $(BUILD)/bootloader.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
# Parameters:
# - BIOS_SRC: source directory of bios assembly
# - STAGE3: path to linked stage 3 binary
# - STAGE2_SECTOR: the index of the first sector of the stage 2 binary on the disk
BIOS_SRC = ./bios
STAGE2_SECTOR = 5
STAGE3 = $(BUILD)/bootloader.elf
$(BUILD)/bios.bin: $(wildcard $(BIOS_SRC)/*.asm) $(STAGE3)
@mkdir -p "$(BUILD)"
nasm \
-f bin \
-D STAGE3=$(STAGE3) \
-D STAGE2_SECTOR=$(STAGE2_SECTOR) \
-o "$@" \
-l "$@.lst" \
-i "$(BIOS_SRC)" \
"$(BIOS_SRC)/main.asm"
# Extract MBR code (first 440 bytes)
# This can be used to embed this mbr in gpt-partitioned disks
$(BUILD)/mbr.bin: $(BUILD)/bios.bin
@mkdir -p "$(BUILD)"
@echo ""
dd if="$<" bs=440 count=1 of="$@"
# Extract full mbr (first 512 bytes)
# This can be used to make raw boot disks
$(BUILD)/512.bin: $(BUILD)/bios.bin
@mkdir -p "$(BUILD)"
@echo ""
dd if="$<" bs=512 count=1 of="$@"
# Extract stage 2 (rest of file)
$(BUILD)/stage2.bin: $(BUILD)/bios.bin
@mkdir -p "$(BUILD)"
@echo ""
dd if="$<" bs=512 skip=1 of="$@"