OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Mar 29, 2024 12:14 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: [SOLVED] FAT32 write support
PostPosted: Thu Aug 24, 2017 9:58 am 
Offline
Member
Member

Joined: Sun Aug 20, 2017 10:59 am
Posts: 43
Hello everyone,
i implemented fat32 read support in my kernel, and it is now working fine (except that i did not implemented reading a file at a specific offset, but anyway).
I wanted to fully implement fat32 support, so i need file and directory creation, file writing, and file and directory deletion.
I read the FAT article on the wiki, and the FAT32 one by Requimrar, but i did not find them useful for write support.
I have read the microsoft "fatgen103.doc" document, but again even if read support, and lfn stuff is really clear i could not find useful information for write support.
I have 3 questions :
- How do i find an 'empty' or free to use cluster on the drive ? Do i need to look in the FAT, and where ?
- How do i create a directory or a file ? I am supposed to create a new entry in the FAT, right ? but how do i do that ? (where to write it, with what data)
- How do i allocate a cluster for a file or a directory, if the file data becomes too big for the current cluster ? (that cluster address must then be written in the FAT, right ? so that we can follow the cluster chain)

I tried to understand the concepts behind the file system, but i can't deny that i don't really know what are the data in the FAT itself, as all i've done for now using the fat is following cluster chain.

Thanks for using your time for me (and sorry if my english is bad) !


Last edited by vhaudiquet on Wed Sep 13, 2017 1:51 pm, edited 3 times in total.

Top
 Profile  
 
 Post subject: Re: FAT32 write support
PostPosted: Thu Aug 24, 2017 11:12 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 27, 2004 11:00 pm
Posts: 874
Location: WA
Quote:
- How do i find an 'empty' or free to use cluster on the drive ? Do i need to look in the FAT, and where ?

yes, any FAT entry marked "free" is free, empty, and can be used for storing a file

where doesn't matter (that is a matter of implementation and optimization)

just to note: a 0 value means the cluster is free to be used

Quote:
- How do i create a directory or a file ? I am supposed to create a new entry in the FAT, right ? but how do i do that ? (where to write it, with what data)

a directory is just a file that contains other files (really very simple)
for both these are the things that need to be done:

find a free cluster to use in the FAT table, if you need more than 1 cluster, find more clusters
mark the last cluster in the chain as EOF (iirc that should be 0xFFFFFF8... but check the docs and don't rely on me to be correct)
if you are using more than 1 cluster, make all other clusters point to the next cluster in the list
find an available free slot in the parent directory list, and fill it with the proper name, size, attributes, and starting cluster


Quote:
- How do i allocate a cluster for a file or a directory, if the file data becomes too big for the current cluster ? (that cluster address must then be written in the FAT, right ? so that we can follow the cluster chain)

your directory entry points to the first cluster (this is how you mark which file the cluster is used by), and mark each cluster in the FAT table with the next cluster used by the file, the last cluster should be marked as EOF

if you need to grow a file larger, it is really easy:
just get a new free cluster (any cluster previously marked 0) and mark that as EOF, and point the previous final cluster to the new cluster (don't forget to update the file size in the directory)

Quote:
I tried to understand the concepts behind the file system, but i can't deny that i don't really know what are the data in the FAT itself, as all i've done for now using the fat is following cluster chain.

FAT tables are very simple: its a linked list, where the directory entry points to the start, and each entry then points to the next entry
0 means unused (free, available for use)
any cluster > EOF means bad cluster (probably the disk is damaged in that spot and cannot be reliably read/written)

_________________
## ---- ----- ------ Intel Manuals
OSdev wiki


Top
 Profile  
 
 Post subject: Re: FAT32 write support
PostPosted: Thu Aug 24, 2017 11:27 am 
Offline
Member
Member

Joined: Sat Feb 27, 2010 8:55 pm
Posts: 147
JAAman wrote:

Quote:
- How do i create a directory or a file ? I am supposed to create a new entry in the FAT, right ? but how do i do that ? (where to write it, with what data)

a directory is just a file that contains other files (really very simple)
for both these are the things that need to be done:

@OP: Remember to add the "." and ".." entries

JAAman wrote:
mark the last cluster in the chain as EOF (iirc that should be 0xFFFFFF8... but check the docs and don't rely on me to be correct)

I believe FFFFFF8 - FFFFFFE are bad clusters and FFFFFFF is EOF, but I must admit I'm not sure either.

if you are using more than 1 cluster, make all other clusters point to the next cluster in the list
find an available free slot in the parent directory list, and fill it with the proper name, size, attributes, and starting cluster


Quote:
- How do i allocate a cluster for a file or a directory, if the file data becomes too big for the current cluster ? (that cluster address must then be written in the FAT, right ? so that we can follow the cluster chain)

You find free clusters and mark them either as EOF (if they're the last one) or mark them with the number of the next cluster


Quote:
I tried to understand the concepts behind the file system, but i can't deny that i don't really know what are the data in the FAT itself, as all i've done for now using the fat is following cluster chain.

I think you're 99% of the way there! You already know how to read a FAT volume; just think about what steps had to be taken to get the data you're reading onto the disk.


Top
 Profile  
 
 Post subject: Re: FAT32 write support
PostPosted: Thu Aug 24, 2017 11:39 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 27, 2004 11:00 pm
Posts: 874
Location: WA
just checked FATgen and we were both wrong...

0xFFFFFF8 - 0xFFFFFFF are all EOF (or the spec calls it EOC -- End Of Clusterchain)
0xFFFFFF7 == BAD_CLUSTER

_________________
## ---- ----- ------ Intel Manuals
OSdev wiki


Top
 Profile  
 
 Post subject: Re: FAT32 write support
PostPosted: Fri Aug 25, 2017 3:22 am 
Offline
Member
Member

Joined: Sun Aug 20, 2017 10:59 am
Posts: 43
Thanks to both of you guys !
I started implementing that and that was actually easier than i thought ; with your explanations, i even rewritten some part of the read process to cache the FAT table entirely (to improve performance)... :D :D


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 79 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group