138 lines
4.7 KiB
Bash
Executable File
138 lines
4.7 KiB
Bash
Executable File
#!/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'
|
|
|
|
# Shared compiler flags (single source of truth for both real compiles AND compile_commands.json)
|
|
CFLAGS="-ffreestanding -mcmodel=large -mno-red-zone -fno-pic -fno-pie -static -fno-stack-protector -nostdlib -mno-mmx -mno-sse -mno-sse2 -m64"
|
|
INCLUDE="-I./include"
|
|
|
|
# compile_commands.json setup (for clangd/nvim LSP support)
|
|
CCJ_OUTPUT="compile_commands.json"
|
|
PROJECT_DIR="$(pwd)"
|
|
|
|
append_ccj_entry() {
|
|
local src_file="$1"
|
|
local full_cmd="$2"
|
|
if [ "$ccj_first" = true ]; then
|
|
ccj_first=false
|
|
else
|
|
echo "," >> "$CCJ_OUTPUT"
|
|
fi
|
|
printf ' {\n' >> "$CCJ_OUTPUT"
|
|
printf ' "directory": "%s",\n' "$PROJECT_DIR" >> "$CCJ_OUTPUT"
|
|
printf ' "file": "%s",\n' "$src_file" >> "$CCJ_OUTPUT"
|
|
printf ' "command": "%s"\n' "$full_cmd" >> "$CCJ_OUTPUT"
|
|
printf ' }' >> "$CCJ_OUTPUT"
|
|
}
|
|
|
|
echo "[" > "$CCJ_OUTPUT"
|
|
ccj_first=true
|
|
|
|
# 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/7]${NC} Assembling Stage 1 bootloader (MBR)..."
|
|
nasm -f bin ./boot/boot.asm -o ./bin/boot.bin
|
|
|
|
# Step 1.5: Compile C source files
|
|
echo -e "${BLUE}[2/7]${NC} Compiling C source files..."
|
|
COUNT=$(ls src/*.c 2>/dev/null | wc -l)
|
|
if [ "$COUNT" -eq 0 ]; then
|
|
echo -e "${YELLOW}No C source files found in src/. Skipping compilation.${NC}"
|
|
else
|
|
COUNTER=1
|
|
for file in src/*.c; do
|
|
echo -e "${BLUE}[$COUNTER/$COUNT]${NC} Compiling $file..."
|
|
((COUNTER++))
|
|
name=$(basename "$file")
|
|
gcc $CFLAGS $INCLUDE -c "$file" -o "obj/${name}.o"
|
|
append_ccj_entry "$file" "gcc $CFLAGS $INCLUDE -c $file -o obj/${name}.o"
|
|
done
|
|
echo -e "${GREEN}✓${NC} C source files compiled.${NC}"
|
|
fi
|
|
|
|
|
|
# Step 2: Compile kernel C code
|
|
echo -e "${BLUE}[3/7]${NC} Compiling kernel..."
|
|
gcc $CFLAGS $INCLUDE -c kernel.c -o ./obj/kernel.c.o
|
|
append_ccj_entry "kernel.c" "gcc $CFLAGS $INCLUDE -c kernel.c -o ./obj/kernel.c.o"
|
|
|
|
# Close compile_commands.json now that all C compiling is done
|
|
echo "" >> "$CCJ_OUTPUT"
|
|
echo "]" >> "$CCJ_OUTPUT"
|
|
|
|
# Step 3: Assemble kernel entry point
|
|
echo -e "${BLUE}[4/7]${NC} Assembling kernel entry..."
|
|
nasm -f elf64 ./boot/kernel.asm -o ./obj/kernel.asm.o
|
|
|
|
# Step 4: Link kernel
|
|
echo -e "${BLUE}[5/7]${NC} Linking kernel..."
|
|
ld -T linker.ld -o ./bin/kernel.bin ./obj/*.o --oformat binary --nostdlib
|
|
# Debug build (optional)
|
|
ld -T linker.ld -o ./bin/kernel_debug.elf ./obj/*.o --oformat elf64-x86-64 --nostdlib
|
|
|
|
# Step 4.5: Calculate kernel size in sectors
|
|
KERNEL_SIZE=$(stat -f%z ./bin/kernel.bin 2>/dev/null || stat -c%s ./bin/kernel.bin 2>/dev/null)
|
|
SECTORS=$(( (KERNEL_SIZE + 511) / 512 )) #Round up to nearest sector
|
|
echo -e "${YELLOW}Kernel size: ${KERNEL_SIZE} bytes, which is ${SECTORS} sectors.${NC}"
|
|
|
|
# Step 5: Assemble Stage 2 bootloader (extended loader)
|
|
echo -e "${BLUE}[6/7]${NC} Assembling Stage 2 bootloader..."
|
|
nasm -f bin ./boot/elevator.asm -D KERNEL_SECTORS=$SECTORS -o ./bin/elevator.bin
|
|
|
|
# Step 6: Create OS image
|
|
echo -e "${BLUE}[7/7]${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}"
|