Simple Bootloader to Load Bin

Simple Bootloader to Load Bin

I am new to things like OS dev, but I would like to ask you if there is any simple bootloader or tutorial how to load bin file (which contains custom OS).

I read lot of tutorials about bootloaders, but mostly they end with Hello World and doesn't say how to load some bin file.

I have kernel.bin which contains:

kernel.asm:

bits    32
section         .text
        align   4
        dd      0x1BADB002
        dd      0x00
        dd      - (0x1BADB002+0x00)

global start
extern kernel
start:
        cli
        call kernel
        hlt

and kernel.c:

kernel()
{
        char* VideoMemory = (char*)0xb8000;
        VideoMemory[0] = 'M';
        VideoMemory[2] = 'y';
        VideoMemory[4] = ' ';
        VideoMemory[6] = 'O';
        VideoMemory[8] = 'S';
}

It uses grub as bootloader and kernel.bin is saved in /boot/ directory, but I would like to create custom bootloader which loads that kernel.bin

I know that if I create custom bootloader I can use only first 510 bytes, because 511th and 512th byte contains magic number for booting.

I know that I have to use:

times 510 - ($ - $$) db 0
db 55h
db 0AAh

But I don't know how to tell to that bootloader to load that kernel.bin

2

3 Answers

There's 3 things you need to know before you can write a boot loader:

a) Which environment will the boot loader execute in (BIOS, UEFI, something else?)

b) What kind of device will the boot loader be booted from. Booting from CD is often very different to booting from hard drive; and booting from network is often very different to both CD and hard drive.

c) Where is the "stuff" (kernel, or initial RAM disk, or...) that the boot loader is supposed to load. Assuming you're booting from hard drive; this can involve dealing with partitions ("MBR partitions", UEFI/GPT) and understanding some kind of file system (FAT? Ext2? Something designed for the OS?).

I know that I have to use:

times 510 - ($ - $$) db 0
db 55h
db 0AAh

That implies "BIOS, booting from some kind of disk" (maybe floppy with no partitions, but maybe hard disk with some kind of partitioning scheme); which is about half of what you need to know before writing a boot loader.

How to make a simple bootloader to load bin, implies a lot of work, but I try to be as concise as I can. I describe how I made my bootloader for a 100 MB partition on my Hard Disk, formatted with FAT16.

Step one:

I created a 100 MB partition on my HDD and I formatted in FAT16. I configured GRUB in such a way that I could select to boot from this partition. Once done, I booted from the 100 MB partition and I got back on screen the message "No system disk. Press any key to reboot..." which told me that I successfully manage to create a partition of the Hard Disk that I could use as if it was a Floppy Drive. You can find all the details about this first step here:

Step two:

I created my bootloader which searched the ROOT directory of the partition looking for a specific file. In my case, the bootloader was looking for "SOFTWARE.BIG" which is a plain binary file. "SOFTWARE.BIG" is just a name I decided to give to the file to be loaded in RAM by the bootloader, but when you adapt my bootloader to make yours, you can change it to be MYKERNEL.BIN if you like. You can find all the details about this second step here:

Step three:

Each partition is different even if they all use FAT16 as a file system. Things like the number of bytes per sector, the number of sectors per cluster of file, etc. they make the partitions different one from the other but all those relevant information are saved in the BIOS Parameter Block. I created a program that adapted my bootloader depending on the details that the same program read from the BIOS Parameter Block. In this way, you can make the bootloader fit your own FAT16 partition. You can find all the details about this third step here:

Step four:

Here you can find how I tested my bootloader to be sure that it was really working always:

1

Get a copy of Emu8086, you will find some good examples of boot loader, mini kernel and how to test it all. emu8086 is discontinued so try some safe sites to get it from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.