Page 1 of 1

FAT16 cluster problem?

Posted: Sun Jun 23, 2013 2:08 pm
by computertrick
Hi I have implemented FAT16 into my operating system so far all good I can calculate the root directory but when it comes to finding the physical address of the data I have a problem.

Here is more information

Reserved Blocks: 1
Total fats: 1
Total root entries: 512
Blocks per FAT: 1
Blocks per allocation unit: 128
Block size: 512

and the cluster number of the file I am trying to read is 6.

Could anyone help me with the math to calculate this I would appreciate it.

Many thanks :D

Re: FAT16 cluster problem?

Posted: Mon Jun 24, 2013 6:35 am
by egos
StartFileSector = DataStart + (SrartFileCluster-2)*SectorsPerCluster
DataStart = HiddenSectors + ReservedSectors + FATs*FATSize + RootSize

Re: FAT16 cluster problem?

Posted: Mon Jun 24, 2013 12:48 pm
by computertrick
egos wrote:StartFileSector = DataStart + (SrartFileCluster-2)*SectorsPerCluster
DataStart = HiddenSectors + ReservedSectors + FATs*FATSize + RootSize
Hi thanks for your reply using what you have given me I get pretty close to the data about 16000 bytes off. I think I am missing something here is my assembly in the boot sector for listing FAT information

Code: Select all

org 0 ; we start everything at zero
	jmp start     
MANU_DESC 			db 'mkdosfs '
BLOCK_SIZE	 		dw 512
BLOCKS_PER_ALLOCATION_UNIT 	db 128
RESERVED_BLOCKS			dw 1
TOTAL_FATS			db 1
TOTAL_ROOT_ENTRIES		dw 512
TOTAL_BLOCKS			dw 0xffff
MEDIA_DESCRIPTOR		db 0xf8
BLOCKS_PER_FAT			dw 0x01
BLOCKS_PER_TRACK		dw 18
TOTAL_HEADS			dw 0x02
HIDDEN_BLOCKS			dd 0x00
TOTAL_BLOCKS_2			dd 0x00
DRIVE_NUMBER			dw 0x00
EXTENDED_BOOT_SIGNATURE		db 0x29
VOLUME_SERIAL_NUMBER		dd 0x9d86f18c
VOLUME_LABEL			db 'SEAGULL    '
FILE_SYSTEM_IDENTIFIER		db 'FAT16   '
I am currently doing

DataStart = HIDDEN_BLOCKS + RESERVED_BLOCKS + TOTAL_FATS * BLOCKS_PER_FAT * BLOCK_SIZE

I don't know what you mean by root size??

StartFilesector = DataStart + (0x0A -2) * BLOCKS_PER_ALLOCATION_UNIT * BLOCK_SIZE


The root directory is at 0x400 which I assume is where DataStart is meant to be?

using Python3 this is the result I get

Code: Select all

> hex(0x400 + (0x0a-2) * 128 * 512)
'0x80400'
What am i doing wrong?

Re: FAT16 cluster problem?

Posted: Mon Jun 24, 2013 1:16 pm
by egos
computertrick wrote:I think I am missing something here is my assembly in the boot sector for listing FAT information
Don't think, look into the FAT manual.
I don't know what you mean by root size??
It's size of root directory in sectors.
What am i doing wrong?
* BLOCK_SIZE

Re: FAT16 cluster problem?

Posted: Mon Jun 24, 2013 1:34 pm
by computertrick
egos wrote:
computertrick wrote:I think I am missing something here is my assembly in the boot sector for listing FAT information
Don't think, look into the FAT manual.
I don't know what you mean by root size??
It's size of root directory in sectors.
What am i doing wrong?
* BLOCK_SIZE
I'm still having trouble could you please explain it more using the assembly I wrote in the last post

Re: FAT16 cluster problem?

Posted: Mon Jun 24, 2013 1:50 pm
by egos
I posted correct BPB+ structure for FAT12/16 here. Compare it to yours.

Re: FAT16 cluster problem?

Posted: Mon Jun 24, 2013 2:21 pm
by JAAman
computertrick wrote: I am currently doing

DataStart = HIDDEN_BLOCKS + RESERVED_BLOCKS + TOTAL_FATS * BLOCKS_PER_FAT * BLOCK_SIZE
this should be instead: HIDDEN_BLOCKS + RESERVED_BLOCKS + TOTAL_FATS*BLOCKS_PER_FAT
without the *BLOCK_SIZE because you are looking for a sector number, not a number of bytes
I don't know what you mean by root size??

StartFilesector = DataStart + (0x0A -2) * BLOCKS_PER_ALLOCATION_UNIT * BLOCK_SIZE

The root directory is at 0x400 which I assume is where DataStart is meant to be?
no, this is wrong -- what you are calling DataStart here is the location of the root directory, the FAT12/16 FS does not consider this to be part of the data area (FAT32 does count this, as the Root Directory is mapped to a cluster chain as a file) to find the start of the data area, you would need:
DataStart + (NumRoots/8)

so to find the location of a cluster on the disk you would need:
LBA = (cluster-2)*BLOCKS_PER_ALLOCATION_UNIT + (NumRoots/8) + (HIDDEN_BLOCKS + RESERVED_BLOCKS + TOTAL_FATS*BLOCKS_PER_FAT)

so using the specific values in your particular example from your OP this would be:
LBA = (6-2)*128 + (512/8) + (0 + 1 + 1*1)
LBA = 4*128 + 64 + 1 + 1
LBA = 512 + 64 + 2
LBA = 578

so your file starts in sector 578

that should give you LBA = sector number of the start of the cluster you are looking for (i don't know why you would want the number of bytes offset, but if you did, you could multiply LBA by BLOCK_SIZE (in this example it would be 578 * 512 = 289KB (259,936 bytes 0x048400)
egos wrote: I posted correct BPB+ structure for FAT12/16 here. Compare it to yours.
his BPB looks correct, don't think there is a problem there

Re: FAT16 cluster problem?

Posted: Mon Jun 24, 2013 2:46 pm
by egos
JAAman wrote:this should be instead: HIDDEN_BLOCKS + RESERVED_BLOCKS + TOTAL_FATS*BLOCKS_PER_FAT
Don't forget about root directory.
(NumRoots/8)
One 512-byte sector holds 512/32=16 file records, not 8.
his BPB looks correct, don't think there is a problem there
I don't know where problem is, maybe in the initial jump instruction. Actually I don't see any problems here at all. I posted the exact answer.

Re: FAT16 cluster problem?

Posted: Mon Jun 24, 2013 4:32 pm
by computertrick
Thanks for all your help ! Its working now!

Re: FAT16 cluster problem?

Posted: Mon Jun 24, 2013 4:50 pm
by computertrick
I want to learn more about FAT16 do you know any good documentation I have been using this partly http://www.tavi.co.uk/phobos/fat.html but I don't find it as useful as I should.

Also can anyone tell me what an allocation unit is? Is that the 32 bytes allocated for each file or I think thats called a directory entry right?

Re: FAT16 cluster problem?

Posted: Mon Jun 24, 2013 7:31 pm
by JAAman
computertrick wrote:I want to learn more about FAT16 do you know any good documentation I have been using this partly http://www.tavi.co.uk/phobos/fat.html but I don't find it as useful as I should.
its almost identical to FAT12, except the size of the cluster number is 16-bits instead of 12-bits (FAT32 is a little different)
best way to find information about it is by reading the actual specification -- no other file/website is a replacement for the actual specification you can also look at the FAT entry in our wiki
Also can anyone tell me what an allocation unit is? Is that the 32 bytes allocated for each file or I think thats called a directory entry right?
the term "allocation unit" refers to the "cluster", and refers to the individual chunks of the disk that may be allocated to a file -- only full clusters can be given to a file, so the cluster size (counted in sectors, must be power-of-two) is important for disk space usage (smaller cluster size means less wasted space at the end of files) -- this is why most FAT12 drives use 1-sector clusters (usually you use the smallest cluster size possible for FAT12/16)
egos wrote: One 512-byte sector holds 512/32=16 file records, not 8.
doh! your right... don't know what i was thinking... trying to calculate it in my head while writing that -- must have had 256 on my mind for some reason