Question about FAT12

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
GenX

Question about FAT12

Post by GenX »

Hi everyone,

Ok, I have been on and off playing around with creating my own OS, I am trying to understand how FAT12 works but something doesn't seem right, I am pretty sure I am missing something, but I just want some clarification so that I understand it properly.

The numbers below represent the first 10 bytes of the FAT organized into their 12 bit entries, so 001 is FAT(0), 122 is FAT(1), etc...

001 122 334 455 667 788 99

If I look at a directory entry and get a first cluster number of 2 then I first read the corresponding cluster, this is fine, I understand this but when it comes to getting the next cluster number from the FAT I don't understand something, I will explain that now.

The process to get the next cluster number is as follows:

EntryLocation = (ClusterNumber / 2) + ClusterNumber

Viewing the FAT as an array of bytes I would read a word, the word I would read is in italics in the above numbers

word FatEntry = FAT[EntryLocation]

now I have to adjust this value to get the 12 bits I am after, here is the problem:

If ClusterNumber is even, I am supposed to clear the highest nibble which would leave me with 344, but a ClusterNumber of 2 means I should end up with 334... It seems to me that I should be shifting FatEntry right 4 bits if its even and clear the high 4 bits if its odd which is opposite to what the documentation says...

I hope you can understand this post, I am really confused about this ???

Cheers. :)
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Question about FAT12

Post by Candy »

GenX wrote: Hi everyone,

Ok, I have been on and off playing around with creating my own OS, I am trying to understand how FAT12 works but something doesn't seem right, I am pretty sure I am missing something, but I just want some clarification so that I understand it properly.

The numbers below represent the first 10 bytes of the FAT organized into their 12 bit entries, so 001 is FAT(0), 122 is FAT(1), etc...

001 122 334 455 667 788 99

If I look at a directory entry and get a first cluster number of 2 then I first read the corresponding cluster, this is fine, I understand this but when it comes to getting the next cluster number from the FAT I don't understand something, I will explain that now.

The process to get the next cluster number is as follows:

EntryLocation = (ClusterNumber / 2) + ClusterNumber

Viewing the FAT as an array of bytes I would read a word, the word I would read is in italics in the above numbers

word FatEntry = FAT[EntryLocation]

now I have to adjust this value to get the 12 bits I am after, here is the problem:

If ClusterNumber is even, I am supposed to clear the highest nibble which would leave me with 344, but a ClusterNumber of 2 means I should end up with 334... It seems to me that I should be shifting FatEntry right 4 bits if its even and clear the high 4 bits if its odd which is opposite to what the documentation says...

I hope you can understand this post, I am really confused about this ???

Cheers. :)

That's little endian done wrong.


The bytes are like:

00 11 22 33 44 55 66 77 88 -> 100 221 433 554 766 887
so, if you want the 1st entry, load a 16-bit word from address 0. That gives you (after little endian conversion) 1100. Since it's even, mask out the upper 4 bits and you got it.

For the second, load byte 1 and 2 as word. That gives 2211. Shift it right by 1 nibble to get 221, which is the right one.
GenX

Re:Question about FAT12

Post by GenX »

:-[ Now thats embarassing!!

Thanks a bundle. :)
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Question about FAT12

Post by Candy »

GenX wrote: :-[ Now thats embarassing!!

Thanks a bundle. :)
Don't mind it, most people (including me) mess up little-endian and big-endian stuff regularly.

On a sidenote, try to use name each nibble, it makes it clearer which bits you want. This is for FAT12:

ab cd ef => dab efc
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Question about FAT12

Post by Solar »

And then there are people who really believe that little-endian is "correct". :-D
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Question about FAT12

Post by Candy »

Solar wrote: And then there are people who really believe that little-endian is "correct". :-D
Oh yes, most certainly it is. I usually mess up the big endian stuff :)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Question about FAT12

Post by Solar »

Ahem... *cough* seethedoc *cough*... :-D
Every good solution is obvious once you've found it.
Kemp

Re:Question about FAT12

Post by Kemp »

I have to say that I can never remember which way round it goes, it's usually a case of

1) Try it the way that seems right
2) Check the result and find out it's wrong
3) Flip it backwards
GenX

Re:Question about FAT12

Post by GenX »

Phew!! I am not alone. ;D
ab cd ef => dab efc
I think i'll write this down and stick it to my monitor... ;)

Thanks everyone.
Post Reply