#!/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/