Page 1 of 1

FAT booting

Posted: Sun Aug 27, 2006 8:28 am
by killedbydeath
Hi all,
i'm trying to implement fat12 in my little os. So i created a simple BPB structure and added some simple test routines below the bpb (THE BPB and the test routines are in the same file). The BPB and boot code are in absolute sector 1. dd if=kernel.bin of=/dev/fd0

After that my floppy is not recongnisable. So i reformated my floppy (i use fdformat and mkdosfs) . I've also read this thread http://www.osdev.org/phpBB2/viewtopic.php?t=2781
So i went to fdisk to see my booting partition and there was no partition. i created one and added the fat12 and bootable flag. fdisk told me that the newly created partition started at sector 18. So i did
dd if=kernel.bin obs=512 seek=17 of=/dev/fd0 . I rebooted and the computer displayed " This is not a bootable disk "etc .
How can i boot my kernel and have fat12??????

Re:FAT booting

Posted: Sun Aug 27, 2006 12:45 pm
by Habbit
Er... AFAIK, floppies don't have partitions (or maybe they have just one, I don't know). For booting from whatever medium you want, it's better to use GRUB or you will be trapped in a real nightmare. A 'standard' FAT12 floppy bootsector should have code to:
  • Access the floppy disk through the BIOS int's. This includes knowing when to turn on the drive motor and not forgetting to turn it off later, even if the read fails (kinda like try...finally but in ASM). Also, floppy reads are not very reliable. You should double-check everything or you will run a kernel-like mass of bogus code and triple-fault at the very moment you do "call _kmain"
  • Parse the directory and the FAT to find your kernel. Go for something simple like \kernel.sys or you're in for trouble. (Also don't use long file names or you'll be infringing M$'s patent!)
  • Enable protected mode and the A20 line to avoid mixing 16-bit code into your 32-bit kernel. It's better to have the mix just in one place, the bootsector, and pass control to a 32-bit kernel that is blissfully ignorant of 8086 compatibility.
All that must 1) not raise any processor exceptions, since IIRC, you have no IDT and what may be in the IVT will surely triple-fault, and 2) fit in 512 bytes, including the BPB.
See now why a lot of people uses GRUB? ;)

You can, of course, take an alternative path: just stuff the floppy access code in the bootsector and make it load a second stage loader that parses the FAT and enables protected mode and A20. But, by the way, that's what GRUB does. Instead of doing all that, just make your kernel Multiboot compliant and build onto the safe base of a known, tested bootloader.

Re:FAT booting

Posted: Mon Aug 28, 2006 4:55 am
by Pype.Clicker
http://www.mega-tokyo.com/osfaq/FAT12%20document may be of some help ...
AFAIK, floppies don't have partitions
seconding that ;) And using "fdisk" to instruct the partition is bootable will just point you into a bootsector that prints "floppy is not bootable" and waits for reset ...
After that my floppy is not recongnisable.
chances are that you trashed some crucial info in the BPB, or that you accidentally wrote _more_ than one sector.

Re:FAT booting

Posted: Mon Aug 28, 2006 6:38 am
by killedbydeath
Well I have all the required fields for the BPB and some functions to print some test strings. Should i ONLY have the BPB fields in the first sector? Cause i've seen other fat12 bpb's and they include many functions for ex. to init pmode. Currently i've put a simple console like function and a printing function below the bpb fields. But as i said there are other fa12 bootsectors that contain many functions or even a hole simple kernel in the bpb (ex bpb with many functions http://www.kenax.byethost9.com/osdev/boot/fat12bootsector.asm
Please help :-\

Re:FAT booting

Posted: Mon Aug 28, 2006 6:49 am
by Pype.Clicker
if you have nothing but the BPB, you won't be able to boot anything at all, right ? you just describe your disk to the BIOS functions ...

The bare minimum you should have, together with the BPB values, would be code to load extra sectors out of the floppy and jump to them. You can very well have whatever required to load a full binary using FAT12, setup pmode environment and display error message at every steps where things go wild. That's just terribly more complicated to fit in the 512-N bytes of your first sector.

I'd suggest you get an HEX dump of your floppy's first sector and check carefully (using the FAQ or whatever other reliable info source) that everything is in place: code, boot signature, BPB and all its parameters, etc. A good practice when you install a bootloader on a floppy consist of reading the bootsector, writing your code where it's supposed to be and then writing back the bootsector. That way, you reduce the risk of wrong parameter due to incorrect interpretation of the specs ...

Re:FAT booting

Posted: Mon Aug 28, 2006 7:40 am
by Habbit
Maybe this is stupid, but have you added the magic "bootable" flag at the end of the sector? By the way, could you post the ASM code here so we can see what's wrong with it?

Oh, and just the boot sector does not make the disk FAT-12: you have to write the boot sector over that of a preformatted disk (which you say you're doing), and you must not overwrite the FAT or the directory.

Re:FAT booting

Posted: Mon Aug 28, 2006 10:02 am
by REV
Speaking of FAT12 booting. Which sector do I copy my FAT12 bootsector onto?

Re:FAT booting

Posted: Mon Aug 28, 2006 10:24 am
by Habbit
Er... you mean the sector with the BPB and boot code? To sector 0, IIRC

Re:FAT booting

Posted: Mon Aug 28, 2006 12:15 pm
by killedbydeath
Well i've not tried yet to implement fat12 sector reading so i packed my hole little kernel in the bootsector ;D . here's the code:
(it displays a message and has one built in command.)

Code: Select all

[BITS 16]
[ORG 0]
jmp start
nop ; here starts the BPB
OEM_ID db 8
BytesPerSector db 0x0200
SectorsPerCluster db 0x01 
ReservedSectors db 0x0001
TotalFats db 0x02
MaxRootEntries dw 0x0E0
TotalSectors dw 0x0B40
MediaDescriptor dw 0xF0
SectorsPerFat dw 0x0009
SectorsPerTrack dw 0x0012
NumHeads dw 0x0002
HiddenSectors dd 0 
TotalSectorsLarge dd 0
DriveNumber db 0
Flags db 0x00
Signature db 0x29
VolumeID dd 0xFFFFFFFF
VolumeLabel db "Testdisk    "
SystemID db "FAT12   "

start:
mov ax,0x7C0
mov ds,ax
mov es,ax
mov ax,0x0000
mov ss,ax
mov sp,0xFFFF


Main:
;mov ax,0xb800
;mov bx,0xbc00
;loopl:
;mov [ax],0x0007
;cmp ax,bx ;reached 0xbc00
;je stop_filling
;inc ax
;jmp loopl
;stop_filling:
;mov [ax],0x0007 
mov si,welcome
call print

mainloop:
mov ax,0E0Ah                 
int  10h
mov si,unixpromt
call print
mov di,commandbuffer
mov dx,39
xor bx,bx
call scanf
 
print:
lodsb
cmp al,0
je printend
mov ah,0Eh
mov bh,0h
mov bl, 0Fh
int 10h
jmp print

printend:
ret



;some data
commandbuffer: times 40 db 0
cID db 'id',0
welcome db 'Welcome to my Os!',0,13,10
inv db 'Invalid Command',0
unixpromt db 13,'root@default#',0
; command tags
idtag db 'I am executing ID',0







scanf:
startscan:
xor ax,ax
int 16h
cmp al,0x0D
je end
cmp al,8
je backspace
cmp bx,dx
je startscan
mov [es:di+bx],al
push bx
mov ah,0Eh
mov bh, 0h
mov bl, 0Fh
int 10h
pop bx 
inc bx
jmp startscan
end:
cmp bx,0
je mainloop
xor al,al
mov [es:di+bx],al
xor bx,bx
jmp process
backspace:
dec bx
mov al,8
int 10h
mov al,32
int 10h
mov al,8
int 10h
jmp startscan

process:
mov si,commandbuffer
mov di,cID
mov cx,3
repe cmpsb 
jne invalid
call id
id:
xor ax,ax
mov ax,0E0Ah                 
int  10h

mov si,idtag
call print
jmp mainloop
invalid:
mov ax,0E0Ah                 
int  10h
mov si,inv
call print
jmp mainloop
times 510-($-$$) db 0
dw 0AA55h

Re:FAT booting

Posted: Mon Aug 28, 2006 2:05 pm
by Habbit
I can't be really sure about this, but I think you have some errors in your BPB that might prevent the disk from being recognized:
[tt]
OEM_ID db 8 1
BytesPerSector db 0x0200 2
SectorsPerCluster db 0x01 3
ReservedSectors db 0x0001 4
TotalFats db 0x02
MaxRootEntries dw 0x0E0
TotalSectors dw 0x0B40
MediaDescriptor dw 0xF0 5
SectorsPerFat dw 0x0009
SectorsPerTrack dw 0x0012
NumHeads dw 0x0002
HiddenSectors dd 0
TotalSectorsLarge dd 0
DriveNumber db 0
Flags db 0x00
Signature db 0x29
VolumeID dd 0xFFFFFFFF
VolumeLabel db "Testdisk "
SystemID db "FAT12 " 6
[/tt]

Red marks mean I'm pretty sure what's marked is wrong, yellow ones mean I'm not so sure.
  • 1: The OEM_ID field must be 8 bytes long. With "db 8", the assembly will emit the single byte 0x08. Insert some string 8 characters in length like in DB '8CHARSTR'
  • 2: The BytesPerSector field is a 16-bit number (a word). You seem to be trying to emit that with DB 0x0200, which will emit 0x02 0x00, as you might expect. The size is right (0x200 = 512 bytes per sector), but the BPB is - I think - little-endian, so the system will understand there are 0x0002 bytes per sector instead of 0x0200!!. You have to use DW instead of db to emit a word instead of two bytes. Thus, DW 0x0200 will emit 0x00 0x02, which will be little-endianly interpreted to 512.
  • 3: That value is wrong, IIRC it was 2 CHS sectors per DOS cluster.
  • 4: Same as 2. The field is a word and you are emitting it as two consecutive bytes with DB. The system will understand it as 0x0100: 256 reserved sectors!. Use DW 0x0001
  • 5: The MediaDescriptor field is a byte, not a word. Use DB 0xF0
  • 6: The SystemID field must be an 8-character string (in this case, 'FAT12 ', with 3 spaces after before the ending quote). I don't know if you really have that wrong or if it is just the consequence of the forum formatting, I vow for the latter since it interprets spaces as it wants to.
And... that is all, or so I think. Remember I'm just chattering about things I don't know ;D. Well, to be serious, I know a bit (but just a liiiitle bit compared to the most experienced OSdevers here in the forum): I'm no systems engineer, I just compared your BPB with my working one (or was it the non working one?).

Re:FAT booting

Posted: Tue Aug 29, 2006 1:53 am
by Pype.Clicker
another thing that you might want to check is that your "jmp start ; nop" code only takes 3 bytes. Some assembler will see that "okay, start isn't that far away, i'm gonna use 'jmp near start'", while other assembler will think "if he'd wanted a jump near, he'd written 'jmp near start', right ? he didn't so i'm using the 3-bytes opcode".

Nasm is obviously of the latest kind while masm/tasm are of the former.

Re:FAT booting

Posted: Tue Aug 29, 2006 3:39 am
by matthias
Also:

Code: Select all

[ORG 0]

shouldn't this be:

[ORG 0x7c0]
Long time since I have been programming rmode, I use GRUB now, but what I do remember is that the BIOS loads the stuff @ 0:0x7c0 or something, can someone provide some more info about this?

edit:

from the faq -> babystep: [ORG 0x7c00]

http://www.mega-tokyo.com/osfaq/BabyStep

Step 2.

Re:FAT booting

Posted: Tue Aug 29, 2006 7:30 am
by Pype.Clicker
oh dear ::) it's been so long we hadn't those questions about good-practice doing a bootsector popp'd up.

1. your bootsector, including BPB, is on "logical sector 0" of the floppy, that is, head=0, track=0, sector=1. Sectors are numbered 1..N rather than 0..N-1 when using CHS mode.

2. both [org 0x7C00] and [org 0x0] are possible. There's no guarantee on whether the BIOS will use 0x7c0:0x0000 or 0x0000:0x7C00 to get you bootstrapped, so you just pick the ORG you prefer and make a jmp far accordingly to reset CS. E.g. if you have ORG 0, you'll need a jmp 0x7c0:start .. if you have ORG 0x7C00, that will be jmp 0:start

3. if you plan to enable pmode stuff, ORG 0x7C00 is waaay easier to use than ORG 0, indeed.

PS: make sure you've been through FAQ: Rolling your own bootloader, right ?
<edit>PPS: oh, and FAQ: BootSequence can prove helpful too ;D

Re:FAT booting

Posted: Tue Aug 29, 2006 9:34 am
by killedbydeath
Thank you guys ;D :D :D :D With your help and with a look at fatgen my boot disk now has fat12 :)