FAT Boot Sector

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
perlpower

FAT Boot Sector

Post by perlpower »

Hey,

I've looked around the web and cant find a tutorial on how to do this. I was wanting to make a bootsector that is fat compatable so windows can read/write files to it and be able to boot a kernel called kernel.bin

perlpower

[move][tt]PERLPOWER currently trying to develop Pelos : POKE EXECUTE LOWLEVEL OPERATING SYSTEM[/tt][/move]
[hr]
[shadow=red,left]There are 10 types of people in this world those who under stand binary and those who dont.[/shadow]
User avatar
Candy
Member
Member
Posts: 3883
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:FAT Boot Sector

Post by Candy »

DOS compatible boot sectors need a jump (2 or 3 bytes) at the start, a BPB that starts at byte 4 and that describes the volume. That keeps your volume DOS-accessible. Of course, you don't pass the end of the boot sector (or you'd wreck the disk identifier and the FAT - invalidating the disk) and you finish up with a nice 0x55 0xAA at the end of your sector.

Of course there are authorities on FAT. The most important one that will ask you money for pre-formatting media with it is of course Microsoft, who explains the FAT boot sector requirements (although they didn't intend it as a requirements list) on this web page.

Googling for FAT boot sector gives quite a lot of interesting links as well. There are a few sites I'm aware of that disassemble a FAT bootsector (believe it's the 98 boot sector) with quite a detailed explanation on everything it does. Have also seen an NT series boot sector disassembled once, although I believe I did that for myself (and threw away the result - main result was, there's a small bit of SMC that changes int13 calls to int13 BIOS Ext calls).
Ryu

Re:FAT Boot Sector

Post by Ryu »

In that Microsoft link that Candy provided: "Because the MBR transfers CPU execution to the boot sector, the first few bytes of the FAT boot sector must be valid executable instructions for an 80x86 CPU. In practice these first instructions constitute a "jump" instruction and occupy the first 3 bytes of the boot sector."

From my experience, inorder to make it compatiable with Windows (XP specifically), it must begin with a jmp instruction and following a nop when jmp short is used. I've tried using cli then jmp short, XP didn't recognise the floppy. So its quite an old peice of document, which only applies to several older NT families (3.1 & 3.51).
User avatar
Candy
Member
Member
Posts: 3883
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:FAT Boot Sector

Post by Candy »

Ryu wrote: In that Microsoft link that Candy provided: "Because the MBR transfers CPU execution to the boot sector, the first few bytes of the FAT boot sector must be valid executable instructions for an 80x86 CPU. In practice these first instructions constitute a "jump" instruction and occupy the first 3 bytes of the boot sector."

From my experience, inorder to make it compatiable with Windows (XP specifically), it must begin with a jmp instruction and following a nop when jmp short is used. I've tried using cli then jmp short, XP didn't recognise the floppy. So its quite an old peice of document, which only applies to several older NT families (3.1 & 3.51).
That needn't be true, however, in lots of cases common practice is extended to requirement because some idiot hardcoded it into something that became oft used.

See also the FAT document from Microsoft that states for about half a dozen things that were dynamic in FAT16 that they shouldn't ever change because somebody hardcoded it and that that code would break. Serves them right for not releasing docs in the first place, but still...
perl

Re:FAT Boot Sector

Post by perl »

??? ??? ???
I got the disasmbled source code from a freshly formated FAT12 Floppy. I copyed the first 512 bytes and disasmbled them to get this
jmp short 0x3e
nop
dec bp
push bx
inc sp
dec di
push bx
xor ax,0x302e
add [bp+si],al
add [bx+di],ax
add [bp+si],al
loopne 0x13
inc ax
or si,ax
or [bx+si],ax
adc al,[bx+si]
add al,[bx+si]
add [bx+si],al
add [bx+si],al
add [bx+si],al
add [bx+si],al
add [bx+si],al
sub [bx],cx
test [bp+si+0x28],bx
dec si
dec di
and [bp+0x41],cl
dec bp
inc bp
and [bx+si],ah
and [bx+si],ah
inc si
inc cx
push sp
xor [bp+si],si
and [bx+si],ah
and [bp+di],dh
leave
mov ss,cx
mov sp,0x7bf0
mov ds,cx
mov ax,0x2000
mov es,ax
cld
mov bp,0x7c00
cmp [bp+0x24],cl
jnl 0x79
mov ax,cx
cwd
call 0x197
jc 0x79
sub bx,byte +0x3a
mov eax,[0x7c1c]
cmp eax,[es:bx]
mov dl,[es:bx-0x4]
jnz 0x74
or dl,0x2
mov [bp+0x2],dl
add bl,0x10
jnc 0x64
xor cx,cx
mov al,[bp+0x10]
cbw
mul word [bp+0x16]
add ax,[bp+0x1c]
adc dx,[bp+0x1e]
add ax,[bp+0xe]
adc dx,cx
mov si,[bp+0x11]
pusha
mov [bp-0x4],ax
mov [bp-0x2],dx
mov ax,0x20
mul si
mov bx,[bp+0xb]
add ax,bx
dec ax
div bx
add [bp-0x4],ax
adc [bp-0x2],cx
popa
mov di,0x0
call 0x197
jc 0xec
cmp [es:di],ch
jz 0xcf
pusha
mov cl,0xb
mov si,0x7da1
repe cmpsb
popa
jz 0xf5
dec si
jz 0xcf
add di,byte +0x20
cmp di,bx
jc 0xb3
jmp short 0xab
mov al,[0x7dfb]
mov ah,0x7d
mov si,ax
lodsb
cbw
inc ax
jz 0xe7
dec ax
jz 0xf1
mov ah,0xe
mov bx,0x7
int 0x10
jmp short 0xd6
mov al,[0x7dfd]
jmp short 0xd2
mov al,[0x7dfc]
jmp short 0xd2
int 0x16
int 0x19
mov dx,[es:di+0x1a]
push dx
mov al,0x1
mov bx,0x0
call 0x13d
jc 0xec
pop bx
mov dl,[bp+0x24]
mov si,0x7c0b
mov di,sp
mov word [bp-0x10],0x7d3d
mov word [bp-0xc],0x7d29
mov cx,ds
mov [bp-0xe],cx
mov [bp-0xa],cx
mov byte [0x7d96],0xcb
jmp 0x2000:0x3
movzx cx,al
mov eax,[bp-0x8]
add eax,[bp+0x1c]
mov edx,eax
shr edx,0x10
jmp short 0x19b
movzx cx,al
dec dx
dec dx
mov al,[bp+0xd]
xor ah,ah
mul dx
add ax,[bp-0x4]
adc dx,[bp-0x2]
jmp short 0x19b
push dx
push ax
push es
push bx
push byte +0x1
push byte +0x10
xchg ax,cx
mov ax,[bp+0x18]
xchg ax,si
xchg ax,dx
xor dx,dx
div si
xchg ax,cx
div si
inc dx
xchg cx,dx
div word [bp+0x1a]
mov dh,dl
mov ch,al
ror ah,0x2
or cl,ah
mov ax,0x201
cmp byte [bp+0x2],0xe
jnz 0x182
mov ah,0x42
mov si,sp
mov dl,[bp+0x24]
int 0x13
popa
popa
jc 0x196
inc ax
jnz 0x18f
inc dx
add bx,[bp+0xb]
dec cx
jnz 0x19b
clc
ret
inc cx
mov bx,0x0
pusha
o32 push byte +0x0
jmp short 0x151
dec si
push sp
dec sp
inc sp
push dx
and [bx+si],ah
and [bx+si],ah
and [bx+si],ah
or ax,0x520a
gs insw
outsw
jna 0x219
and [si+0x69],ah
jnc 0x224
jnc 0x1db
outsw
jc 0x1de
outsw
jz 0x229
gs jc 0x1e4
insw
imul sp,[fs:bx+di+0x2e],word 0xdff
or al,[si+0x69]
jnc 0x23c
and [di+0x72],ah
jc 0x245
jc 0x1d7
or ax,0x500a
jc 0x242
jnc 0x252
and [bx+di+0x6e],ah
jns 0x204
imul sp,[di+0x79],byte +0x20
jz 0x259
and [bp+si+0x65],dh
jnc 0x263
popa
jc 0x266
or ax,0xa
add [bx+si],al
add [bx+si],al
add [bx+si],al
lodsb
retf
fcom dword [di-0x56]
add [bx+si],al
db 0x00

Sry About the long post.
i was wondering how to modify this code and make it load kernal.bin into memory and jmp to it ? ???
Kemp

Re:FAT Boot Sector

Post by Kemp »

Well for a start, most of that is not code but is in fact information about the disk. Next up, undocumented disassembled code does not lend itself to being used for anything other than debugging.

My advice is to read the documentation for FAT boot sectors along with some tutorials and write one based on those. You could also look up GRUB, which will prevent you having to write your own.
User avatar
Candy
Member
Member
Posts: 3883
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:FAT Boot Sector

Post by Candy »

Kemp wrote: Well for a start, most of that is not code but is in fact information about the disk. Next up, undocumented disassembled code does not lend itself to being used for anything other than debugging.

My advice is to read the documentation for FAT boot sectors along with some tutorials and write one based on those. You could also look up GRUB, which will prevent you having to write your own.
There are also numerous people who have done this whilst using common sense (since all you see is bytes, not all is code). They have determined how it works and commented on it. Read one of their commentaries to learn about the FAT boot sector.

If you're not interested in learning but only in hacking on something, try changing the bytes that say "IO SYS" and "MSDOS SYS" to something else. In case of an NT sector, change "NTLDR " to something else.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re:FAT Boot Sector

Post by Combuster »

well, if you have a dos floppy it should read somewhere io.sys/dos.sys/ibmbio.com (not sure which one, just look for a filename (the dot'll be missing)
just fire up your favourite hex editor and change it with your kernel's value - easy

otherwise you could use my first stage bootloader (attachment), which does about the same thing (replace the "STAGE2 BIN" with whatever you want, that's my bootstrap image).

Be aware, fat IS case sensitive although Windows won't admit it. (filenames appear all-caps, padded with spaces if the name or extension is not 8.3: Kernel.asm will read as KERNEL..ASM where dots denote spaces)

p.s. I sense somebody beating me to this point :(
[edit] attachment getting refused - link instead [/edit]
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
chasetec

Re:FAT Boot Sector

Post by chasetec »

perl wrote: ??? ??? ???
I got the disasmbled source code from a freshly formated FAT12 Floppy. I copyed the first 512 bytes and disasmbled them to get this

*snip*

Sry About the long post.
i was wondering how to modify this code and make it load kernal.bin into memory and jmp to it ? ???
ouch, do you like pain for some reason? As Candy said the fat12 boot sector is documented in lots of places, just google search it for better examples with comments. I have a decent amount of comments in mine if you want to take a look, http://www.osdev.org/f12boot.asm
Ryu

Re:FAT Boot Sector

Post by Ryu »

Combuster wrote: well, if you have a dos floppy it should read somewhere io.sys/dos.sys/ibmbio.com (not sure which one, just look for a filename (the dot'll be missing)
just fire up your favourite hex editor and change it with your kernel's value - easy
Well if you do it this way you also need to know where exactly in memory the bootsector loads io.sys/dos.sys/ibmbio.com and what code segment its suppose to run on (or manually change CS segment first thing in your binary). This would be needed to compile your binary to run properly.
User avatar
bubach
Member
Member
Posts: 1224
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:FAT Boot Sector

Post by bubach »

"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
Post Reply