FAT16

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
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

FAT16

Post by BMW »

Hi all,

I decided it was time to implement the FAT16 filesystem in my OS. I have setup the BPB successfully.

I dd my bootsector (followed by 999.5KiB of zeroes) to a usb flash drive, then set the word directly after the bootsector (first FAT entry) to 0xFFF0 and the word at the second FAT entry to 0xFFFF (end of file). The first part of the flash drive then looks like this:

Code: Select all

e9 ab 00 53 6b 75 78 4f 53 00 00 00 02 01 01 00 02 00 02 00 08 f0 08 00 00 00 00 00 00

00 00 00 00 00 00 00 80 00 29 0f a7 01 53 53 6b 75 78 4f 53 20 44 69 73 6b 46 41 54 31

36 00 00 00 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 82 00 07 00 82 80 45 72

72 6f 72 20 6c 6f 61 64 69 6e 67 20 73 65 63 74 6f 72 28 73 29 0d 0a 00 53 54 41 47 45

32 20 20 42 49 4e 4b 45 52 4e 45 4c 20 20 42 49 4e 53 79 73 74 65 6d 20 66 69 6c 65 73

20 6d 69 73 73 69 6e 67 0d 0a 00 53 74 61 67 65 32 20 4c 6f 61 64 65 64 00 00 00 00 00

88 16 54 7c b8 e0 07 fa 8e d0 bc 00 04 fb 31 c0 8e d8 b8 20 00 f7 26 11 7c f7 36 0b 7c

89 c1 a1 10 7c f7 26 16 7c 03 06 0e 7c a3 ac 7c 31 d2 8b 1e 52 7c 86 c1 e8 9e 00 8b 0e

11 7c 8b 3e 52 7c 51 b9 0b 00 be 6f 7c 57 f3 a6 5f 74 08 59 83 c7 20 e2 ed eb 6d 8b 45

1a a3 aa 7c 8b 0e 0e 7c 31 c0 a0 10 7c f7 26 16 7c 31 d2 8b 1e 52 7c e8 65 00 8b 1e 50

7c a1 aa 7c 83 e8 02 0f b6 16 0d 7c f7 e2 03 06 ac 7c 89 c1 31 d2 a0 0d 7c e8 46 00 a1

aa 7c d1 e0 03 06 52 7c 89 c3 8b 17 83 fa f8 7d 11 89 16 aa 7c a1 0d 7c f7 26 0b 7c 01

06 50 7c eb be be 9c 7c e8 11 00 fa f4 ea 00 07 00 00 fa f4 be 85 7c e8 02 00 fa f4 b4

0e ac 3c 00 74 04 cd 10 eb f5 c3 a2 40 7c 89 1e 42 7c 89 16 44 7c 88 0e 46 7c 8a 16 54

7c be 3e 7c b4 42 cd 13 72 01 c3 be 55 7c e8 d1 ff fa f4 00 00 00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa f0 ff ff ff 00 00 00 00 00 00....... (zeroes)
Then I mount the device in ubuntu, copy my stage 2 "STAGE2.BIN", which is 457 bytes in size, to the drive. This results in the third entry of the FAT being set to 0xFFF0???? What is going on here?

This is my BPB (set up for a 1MiB volume):

Code: Select all

db 'SkuxOS',0x00,0x00 ;OEM identifier
bpbBytesPerSector dw 512 ;bytes per sector
bpbSectorsPerCluster db 1 ;sectors per cluster
bpbReservedSectors dw 1 ;number of reserved sectors
bpbNumberOfFATS db 2 ;number of FATs
bpbRootDirectoryEntries dw 512 ;number of root directory entries
dw 2048 ;number of sectors (1 MiB disk = 512*2048 bytes)
db 0xF0 ;media descriptor
bpbSectorsPerFAT dw 8 ;sectors per FAT
dw 0 ;sectors per track
dw 0 ;number of heads
dd 0 ;hidden sectors
dd 0 ;number of sectors (only used if disk size > 32MiB)
db 0x80 ;drive number
db 0 ;reserved
db 0x29 ;extended boot signature
dd 0x5301A70F ;serial number
db 'SkuxOS Disk' ;volume label
db 'FAT16', 0x00, 0x00, 0x00
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
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: FAT16

Post by Combuster »

Why are you expecting that a medium smaller than a floppy can be sanely formatted as FAT16?

You're violating the specification for FAT. Expect breakage.
"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 ]
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: FAT16

Post by Mikemk »

When you copied the file, ubuntu saw that clusters 1 and 2 were occupied, so it was placed at cluster 3
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: FAT16

Post by egos »

Combuster is right. Ubuntu thinks that it's FAT12. BMW, increase values of TotalSectors and bpbSectorsPerFAT.

m12, there is no cluster 1 on FAT volumes (FAT cells 0, 1 are reserved). The first data cluster is 2. So Ubuntu sees that only cluster 2 is occupied.
If you have seen bad English in my words, tell me what's wrong, please.
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: FAT16

Post by Mikemk »

egos wrote:Combuster is right. Ubuntu thinks that it's FAT12. BMW, increase values of TotalSectors and bpbSectorsPerFAT.

m12, there is no cluster 1 on FAT volumes (FAT cells 0, 1 are reserved). The first data cluster is 2. So Ubuntu sees that only cluster 2 is occupied.
Oh, I thought the FAT started at the first available cluster. I havent messed with filesystems much doto nodisk driveryet
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: FAT16

Post by BMW »

Combuster wrote:Why are you expecting that a medium smaller than a floppy can be sanely formatted as FAT16?

You're violating the specification for FAT. Expect breakage.
Ah, thanks.
egos wrote:Combuster is right. Ubuntu thinks that it's FAT12.
Wow, an established system such as linux doesn't even have a decent FAT driver? Assuming its FAT12... honestly!!! Surely it's not that hard to check the file system type from the BPB.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: FAT16

Post by jnc100 »

BMW wrote:
egos wrote:Combuster is right. Ubuntu thinks that it's FAT12.
Wow, an established system such as linux doesn't even have a decent FAT driver? Assuming its FAT12... honestly!!! Surely it's not that hard to check the file system type from the BPB.
The type of FAT is determined by the total number of data clusters available, in your case this is 1999, which implies FAT12. The BS_FilSysType field in the BPB has no bearing on what type of filesystem is actually in the partition. See the wiki page or the FAT32 docs from Microsoft for more information.

Regards,
John.
HugeCode
Member
Member
Posts: 112
Joined: Mon Dec 17, 2012 9:12 am

Re: FAT16

Post by HugeCode »

BMW:
Microsoft EFI FAT32 File System Specification
In the following example, when it says <, it does not mean <=. Note also that the numbers are correct. The first number for FAT12 is 4085; the second number for FAT16 is 65525. These numbers and the ‘<’ signs are not wrong.

Code: Select all

If(CountofClusters < 4085) {
/* Volume is FAT12 */
} else if(CountofClusters < 65525) {
    /* Volume is FAT16 */
} else {
    /* Volume is FAT32 */
}
This is the one and only way that FAT type is determined. There is no such thing as a FAT12 volume that has more than 4084 clusters. There is no such thing as a FAT16 volume that has less than 4085 clusters or more than 65,524 clusters.
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: FAT16

Post by BMW »

HugeCode wrote:BMW:
Microsoft EFI FAT32 File System Specification
In the following example, when it says <, it does not mean <=. Note also that the numbers are correct. The first number for FAT12 is 4085; the second number for FAT16 is 65525. These numbers and the ‘<’ signs are not wrong.

Code: Select all

If(CountofClusters < 4085) {
/* Volume is FAT12 */
} else if(CountofClusters < 65525) {
    /* Volume is FAT16 */
} else {
    /* Volume is FAT32 */
}
This is the one and only way that FAT type is determined. There is no such thing as a FAT12 volume that has more than 4084 clusters. There is no such thing as a FAT16 volume that has less than 4085 clusters or more than 65,524 clusters.
Oh, thanks. I have made the volume 16MB and it seems to have fixed the problem.... except for Ubuntu assuming there was only 1 FAT when I put 2 in the BPB. Or is this the same sort of idea, does the number of FATS depend on the type of media? E.g. 2 FATS for floppy 1 for hard disk?
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
HugeCode
Member
Member
Posts: 112
Joined: Mon Dec 17, 2012 9:12 am

Re: FAT16

Post by HugeCode »

FAT tables also have their own format for first two cluster entries. Check the specification (2000, 2005).
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: FAT16

Post by BMW »

HugeCode wrote:Check the specification (2000, 2005).
Ahh, thank you so much. I was going off some tutorials (not always a good idea). I guess I should have looked for that myself, but thanks anyway. That has cleared up a few things.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Post Reply