Hello XSS community, today i want to share with you a simple method to write a bootloader
prerequisites:- A functional brain
- Some low level experience with ASM
- A desire to learn and understand
- Install linux in a virtual machine for me, I go with arch Linux: ArchWebsite and I use Virtualbox
- Install NASM https://nasm.us/ (in my case on arch linux -> sudo pacman -S nasm)
- install qemu to emulate our little bootloader https://www.qemu.org/
What is the BIOS ?
BIOS stand for BASIC INPUT/OUTPOUT SYSTEMP) in short term, it make the dev of OS easier
"provide runtime services for operating systems and programs and to perform hardware initialization during the booting process" -> https://en.wikipedia.org/wiki/BIOS
Код:
;This is ASM code
mov ax, 0x07c0
mov ds, ax
mov si, xssgreet
cld
loop:lodsb
or al, al
jz hang
mov ah, 0x0E
mov bh, 0
int 0x10
jmp loop
hang:
jmp hang
xssgreet db 'Hello XSS', 13, 10, 0
times 510-($-$$) db 0
db 0x55
db 0xAA
Explanation line by line:
line 1 -> The code in the boot sector of the disk is loaded by the BIOS at 0000:7c00, so we put 0x07c0 in the ax register
MOV copies the source operand to the destination operand without affecting the source.
line 3 ->Wee put our little message into the SI register
line 4 -> CLD clears the direction flag = to the operation DF = 0
Direction flag - Wikipedia
After each iteration, it will jump back to the "loop" label to continue the process until the loop exits.
line 6 -> if it's zero it's the end of the string
line 7->If it's zero like line 6, it's "jump" to hang
function hang: like his name said, this jump infinitely
line 8->This write a char in TTY mode
line9 ->Going to text mode
line10->call INT 10h, the BIOS video service again https://en.wikipedia.org/wiki/BIOS_interrupt_call#Invoking_an_interrupt
line11->going to the loop, again
line 15->okay a little bit more complicated. First, we declare xssgreet as a data variable (db) stand for defined byte which contain the string Hello XSS. The "13" is like \r to move the cursor to the beginning of the line. And finally, the "10" is like the \n. And "0" it's a 0 just a null byte it indicate the end of the string.
line 16->This "times" is used for repetition. In this case, this ensure that the boot sector (512 bytes) is filled with "0" except for the bootloader and the data. This ensure that the bootloader start at the byte "0" of the boot sector.
line 17 & line 18-> This is the boot signature (0xAA55) that tell the bios that the sector is bootable and can be loaded in the memory.
0x02: Compiling and testing
Okay now you need to compile it with the following command
Код:
nasm boot.asm -f bin -o boot.bin
After that you can simply run it with Qemu
Код:
qemu-system-i386 -fda boot.bin -nographic
Conclusion:
I hope you appreciated this tutorial and learning new things.