Just as a preface I've spent the last several hours looking at linker script documentation and examples, but to no avail.
I'm currently writing a bootloader and have gotten so far as to start writing the code to look through a FAT16 filesystem to load a file that contains the rest of my bootloader. The boot process is as follows: stage one, which resides in the boot sector, loads stage two, which resides in the bootstrap section of the FAT16 filesystem, which loads the binary containing the rest of the bootloader.
Stage one, which loads stage two, works just fine. I write an MBR partition table to a raw disk image (using fdisk) and then use dd to insert the stage one binary into the sector where the MBR partition table is.
Stage one consists of multiple source files. Keep in mind stage one works perfectly, reading from the partition table and jumping to stage two .
I'm not including the stage one code here, as I don't think it's relevant. Below are my two assembly files for stage two and the linker script.
Code: Select all
# linker.ld
OUTPUT_FORMAT(binary)
ENTRY(_start)
SECTIONS
{
. = 0x7E3E; /* 0x7E00 (load location) + 0x003E (offset to boot code). */
.text : {
*(.text*)
}
}
Code: Select all
# bootstrap.S
.code16
.text
.global _start
_start:
call foo
hlt
Code: Select all
# fat.S
.text
.global foo
foo:
mov $0x2, %dx
cli
jmp .
Code: Select all
# Build
x86_64-elf-as -o bootstrap.o bootstrap.S
x86_64-elf-as -o fat.o fat.S
x86_64-elf-gcc -T linker.ld -o bootstrap.bin -ffreestanding -O2 -nostdlib bootloader.o fat.o -lgcc
# Make the disk
dd if=/dev/zero of=$(_ROOT)/target/lasv.dd bs=1M count=16
sfdisk lasv.dd < .sfdiskrc
# Make filesystem
sudo losetup -o $$[2048*512] --sizelimit $$[8*1024*1024] -f lasv.dd
sudo mkfs.fat -v -F 12 -n "BOOT" /dev/loop0
# Insert bootstrap code into FAT16
sudo dd if=bootstrap.bin of=/dev/loop0 conv=notrunc bs=1 count=448 seek=62
If you need/want to build the project, you can find the code here