wrote a basic bootloader

This commit is contained in:
2025-02-25 01:14:52 +00:00
commit 012b0454ed
36 changed files with 1667 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
#!/bin/bash
set -e
# Set root directory
cd "$(dirname "$0")/.."
# Create build directory if it doesn't exist
mkdir -p build/bios
# Assemble stage1
nasm -f elf32 boot/bios/stage1/stage1.s -o build/bios/stage1.o
# Link to binary
ld -m elf_i386 -T boot/bios/stage1/stage1.ld build/bios/stage1.o -o build/bios/stage1.bin
# Verify size
size=$(wc -c < build/bios/stage1.bin)
if [ "$size" -gt 446 ]; then
echo "Error: stage1.bin is larger than 446 bytes ($size bytes)"
exit 1
fi
echo "Successfully built stage1.bin ($size bytes)"
+29
View File
@@ -0,0 +1,29 @@
#!/bin/bash
set -e
# Set root directory
cd "$(dirname "$0")/.."
# Create build directory if it doesn't exist
mkdir -p build/bios
# Compile C code
gcc -m32 -ffreestanding -fno-pie -fno-stack-protector -nostdlib -nodefaultlibs \
-Wall -Wextra -O2 -c boot/bios/stage2/stage2.c -o build/bios/stage2.o
# Assemble boot code
nasm -f elf32 boot/bios/stage2/boot.s -o build/bios/boot.o
# Link to binary
ld -m elf_i386 -T boot/bios/stage2/stage2.ld \
build/bios/boot.o \
build/bios/stage2.o \
-o build/bios/stage2.bin
# Verify size (stage2 can be multiple sectors, but let's warn if it's too large)
size=$(wc -c < build/bios/stage2.bin)
if [ "$size" -gt 32768 ]; then # 64 sectors (32KB) should be plenty
echo "Warning: stage2.bin is larger than 32KB ($size bytes)"
fi
echo "Successfully built stage2.bin ($size bytes)"
+63
View File
@@ -0,0 +1,63 @@
#!/bin/bash
set -e
# Size in MB
DISK_SIZE=64
ESP_SIZE=100 # Size of EFI System Partition in sectors (100 sectors = ~50KB, enough for our bootloader)
# Create build directory if it doesn't exist
mkdir -p build/bios
# Build stage 1/2 bootloader
./scripts/build_stage1.sh
./scripts/build_stage2.sh
# Create an empty disk image
dd if=/dev/zero of=disk.img bs=1M count=$DISK_SIZE
# Create a partition table
(
echo o # Create a new empty DOS partition table
echo n # Add a new partition (EFI System Partition)
echo p # Primary partition
echo 1 # Partition number 1
echo 2048 # First sector (leaving room for bootloader)
echo +${ESP_SIZE} # Size in sectors
echo t # Change partition type
echo ef # EFI System Partition type
echo n # Add a new partition (Main partition)
echo p # Primary partition
echo 2 # Partition number 2
echo # Default first sector (right after ESP)
echo # Use rest of disk
echo t # Change partition type
echo 2 # Select second partition
echo 0c # FAT32 LBA type
echo a # Make bootable
echo 2 # Select second partition
echo w # Write changes
) | fdisk disk.img > /dev/null 2>&1
# Copy bootloader to disk image
# First stage (MBR)
dd if=build/bios/stage1.bin of=disk.img conv=notrunc bs=446 count=1
# Second stage (right after MBR)
dd if=build/bios/stage2.bin of=disk.img conv=notrunc bs=512 seek=1
# Format ESP partition
ESP_OFFSET=$((2048 * 512)) # First partition starts at sector 2048
mformat -i disk.img@@${ESP_OFFSET} -F -h 255 -t 1 -s 63 -c 1 ::
# Format main partition
MAIN_OFFSET=$(( (2048 + ESP_SIZE) * 512 )) # Second partition starts after ESP
mformat -i disk.img@@${MAIN_OFFSET} -F -h 255 -t 1 -s 63 -c 1 ::
# Create EFI directory structure in ESP
mmd -i disk.img@@${ESP_OFFSET} ::/EFI
mmd -i disk.img@@${ESP_OFFSET} ::/EFI/BOOT
# When you have the UEFI bootloader ready, uncomment this:
# mcopy -i disk.img@@${ESP_OFFSET} boot/uefi/BOOTX64.EFI ::/EFI/BOOT/
Executable
+26
View File
@@ -0,0 +1,26 @@
#!/bin/bash
set -e
# set root directory of project
cd "$(dirname "$0")/.."
# Build the bootloader
./scripts/create_disk.sh
# Default to KVM if available
KVM_FLAGS=""
if [ -c /dev/kvm ]; then
KVM_FLAGS="-enable-kvm -cpu host"
fi
# Run QEMU with appropriate flags
qemu-system-x86_64 \
$KVM_FLAGS \
-m 4G \
-drive file=disk.img,format=raw \
-monitor stdio \
-no-reboot \
-no-shutdown \
-smp 4 \
-serial file:serial.log