Wednesday, March 12, 2008

Writing bootsector - The GNU "as" (aka gas) way

My friend Haynes pointed me to this quick hands on tutorial..

Simple, brief and neatly explained tutorial on how to write bootsectors with GNU tools for the x86 architecture.

Also, objcopy can be used to convert between binary formats.

--------- --------- --------- --------- --------- --------- --------- ---------
Sunday 2008-03-30 02:19 UTC+5:30

I used the snippet of code given at the above webpage
# Author: Susam Pal 
# To assemble and link this code, execute the following commands:-
# as -o char.o char.s
# ld --oformat binary -o char char.o
# To write this code into the boot sector of a device, say /dev/sdb:-
# dd if=char of=/dev/sdb
# echo -ne "\x55\xaa" | dd seek=510 bs=1 of=/dev/sdb

.section .text
.globl _start
mov $0xb800, %ax
mov %ax, %ds
movb $'A', 0
movb $0x1e, 1
jmp idle
Assembled and linked the same using the commands given in the comment part of the code.
Then, had the ".com" file pushed into a floppy image file. And it worked !

When I pushed the binary into a floppy image formatted as "mkfs -t vfat", the filesystem went corrupt
even though the booting was successful. When I tried formatting the floppy as ext3, everything worked
and the filesystem was also preserved. Wondering why writing the bootsector corrupted the vfat filesystem.

Used qemu to test the stuff !!


No comments: