#!/bin/bash # Complete build script for SoraOS with 2-stage bootloader set -e # Exit on any error echo "===================================" echo " SoraOS Build System v1.0" echo "===================================" echo "" # Colors GREEN='\033[0;32m' BLUE='\033[0;34m' YELLOW='\033[1;33m' NC='\033[0m' # Prepare build environment echo -e "Preparing build environment...${NC}" echo -e "Creating Folders${NC}" mkdir -p bin obj boot include echo -e "${GREEN}✓${NC} Folders created.${NC}" # Step 1: Assemble Stage 1 bootloader (MBR - 512 bytes) echo -e "${BLUE}[1/6]${NC} Assembling Stage 1 bootloader (MBR)..." nasm -f bin ./boot/boot.asm -o ./bin/boot.bin # Step 2: Assemble Stage 2 bootloader (extended loader) echo -e "${BLUE}[2/6]${NC} Assembling Stage 2 bootloader..." nasm -f bin ./boot/elevator.asm -o ./bin/elevator.bin # Step 3: Assemble kernel entry point echo -e "${BLUE}[3/6]${NC} Assembling kernel entry..." nasm -f elf64 ./boot/kernel.asm -o ./obj/kernel.asm.o # Step 4: Compile kernel C code echo -e "${BLUE}[4/6]${NC} Compiling kernel..." gcc -ffreestanding -c kernel.c -o ./obj/kernel.c.o -mcmodel=large -mno-red-zone -fno-pic -fno-pie -static -fno-stack-protector -nostdlib -mno-mmx -mno-sse -mno-sse2 -m64 # Step 5: Link kernel echo -e "${BLUE}[5/6]${NC} Linking kernel..." ld -T linker.ld -o ./bin/kernel.bin ./obj/kernel.asm.o ./obj/kernel.c.o --oformat binary --nostdlib # Step 6: Create OS image echo -e "${BLUE}[6/6]${NC} Creating OS image..." cat ./bin/boot.bin ./bin/elevator.bin ./bin/kernel.bin > os.bin # Pad to floppy size (1.44MB) # FIX: Uncomment the line below if you want to ensure the image is exactly 1.44MB # truncate -s 1440K os.bin echo "" echo -e "${GREEN}✓ Build complete!${NC}" echo "" echo "===================================" echo "File Layout:" echo "===================================" echo "Sector 1: Stage 1 (MBR)" echo "Sectors 2-6: Stage 2 (Extended Bootloader)" echo "Sectors 7+: Kernel" echo "" # Show sizes echo "File Sizes:" printf "%-15s %10s\n" "File" "Size" printf "%-15s %10s\n" "----" "----" printf "%-15s %10d bytes\n" "boot.bin" $(stat -f%z ./bin/boot.bin 2>/dev/null || stat -c%s ./bin/boot.bin 2>/dev/null) printf "%-15s %10d bytes\n" "elevator.bin" $(stat -f%z ./bin/elevator.bin 2>/dev/null || stat -c%s ./bin/elevator.bin 2>/dev/null) printf "%-15s %10d bytes\n" "kernel.bin" $(stat -f%z ./bin/kernel.bin 2>/dev/null || stat -c%s ./bin/kernel.bin 2>/dev/null) printf "%-15s %10d bytes\n" "os.bin" $(stat -f%z os.bin 2>/dev/null || stat -c%s os.bin 2>/dev/null) echo "" echo -e "${YELLOW}Running QEMU...${NC}" echo "===================================" echo "Press Ctrl+C to exit QEMU" echo "" qemu-system-x86_64 -drive format=raw,file=os.bin -no-reboot echo -e "${GREEN}Done!${NC}"