flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Understanding FAT16 Sub-Directories. (This is BS btw)

Author
Thread Post new topic Reply to topic
sid123



Joined: 30 Jul 2013
Posts: 339
Location: Asia, Singapore
sid123 07 Mar 2014, 15:46
Hi,
After implementing a simple FAT16 Read FS driver (which only supports the root directory) I've planned to extend it further Smile.
Well, the next is possibly implementing directories. From what the FAT Documentation says:
sid123 randomly understanding FAT Doc wrote:

Okay a FAT File Entry is same as a Directory Entry (FAT16/12 btw)
except that the Attribute Byte (0xB I guess) has bit 4 set Smile
The directory names are different as they don't need an extension,
but are simply padded, some like 'DIR ' instead of a file like
'DIR TXT'. Cool so finding directories is same as finding files,
I have already written a dir_convert which pads the directory names.
The other difference is that the "cluster" which a file points to contains the
contents of file, while a directory's cluster points to the contents of that directory.

I understand till here. But there are 2 main doubts I have:
1. Is the cluster the LBA address of that entry? For example a root directory's LBA address is calculated by BytesPerSector * SectorsPerFAT * Number of FATs + BytesPerSector * NumberofReserved Sectors, I use this address while browsing the root directory. However, does the cluster point to the LBA address of that sub-directory? Or do I need to calculate something more? If so, what is it? Also, I've seen that a Cluster is (in FAT16) is a WORD but a LBA address is a DWORD, how's it possible? In simple words can I use the cluster as an LBA address of the sub-directory, like I use it with the root directory?
2. How do I find the address of the Parent Directory, some sources say that the '..' points to a parent directory? What do they mean by that?
I've seen some copyright and legal issues with LFN, what's it btw?
EDIT: Got the second point, the '..' is a name given to a entry in a sub directory whose cluster points to the cluster of the parent directory
Thanks,
-sid123

_________________
"Those who can make you believe in absurdities can make you commit atrocities" -- Voltaire https://github.com/Benderx2/R3X
XD
Post 07 Mar 2014, 15:46
View user's profile Send private message Reply with quote
sid123



Joined: 30 Jul 2013
Posts: 339
Location: Asia, Singapore
sid123 08 Mar 2014, 00:43
Okay, lol seems that I wasn't able to explain my doubt properly.
As I said, the offset of the root directory is calculated by 《put formula here》 similarly
how can I calculate the offset of the sub directory? It's a DWORD right?
Post 08 Mar 2014, 00:43
View user's profile Send private message Reply with quote
sid123



Joined: 30 Jul 2013
Posts: 339
Location: Asia, Singapore
sid123 08 Mar 2014, 01:18
Okay, wrote some code btw Smile
Code:
;; Change to a Directory.
;; IN : ESI - Name of Directory
;; OUT: CF on Ok, or current_directory.offset set to the new
;; directory
chdir:
        pushad  
        clc
        mov eax, esi
        ;; Convert it to FAT16 Directory filename
        call dir_convert
        mov esi, eax
        xor eax, eax
        ;; Query for existence of directory
        ;; and it's starting cluster
        call entry_exists
        ;; Good it exists
        jc .ok
        ;; Maybe not, clear carry and bailout
        clc
        popad
        ret
.ok:
        ;; AX contains starting cluster, now we need to calculate
        ;; the first sector of the cluster
        ;; Subtract 2 from
        ;; ax
        sub ax, 2
        ;; Save ECX
        push ecx
        xor ecx, ecx
        mov cl, byte [BPB.fat_16_sectors_per_cluster]
        ;; EAX now holds starting sector of cluster
        imul cx
        ;; Add Data Start region to the 
        ;; Formula:
        ;; DataRegion + ((N - 2) * SectorsPerCluster)
        add eax, [BPB.data_start]
        ;; Cool now, EAX contains the starting sector
        ;; of the sub-directory Very Happy
        ;; Update Values.
        mov dword [current_directory.offset], eax
        ;; Reload ECX
        pop ecx
        ;; Reload All registers
        popad
        ;; Set Carry Sucessful
        stc
        ;; Return
        ret
    

Is it correct? It's supposed to find a root directory, get it's cluster, and then compute the 1st sector of the sub-directory's cluster.

_________________
"Those who can make you believe in absurdities can make you commit atrocities" -- Voltaire https://github.com/Benderx2/R3X
XD
Post 08 Mar 2014, 01:18
View user's profile Send private message Reply with quote
sid123



Joined: 30 Jul 2013
Posts: 339
Location: Asia, Singapore
sid123 08 Mar 2014, 03:01
WORKS WORKS WORKS!
I solved my problem myself.
So just to tell more OSDevers about directories here you go:
1. In you file_exists/load_file/write_file NEVER use the root directory offset,
IMO on HDD Initialization calculate the root directory offset and store it in a global
variable.
2. Next create a DWORD called current_directory.offset (Choose your own name), and on HDD
Initialization set this to the root directory offset. THIS is the variable you will use in your FAT Functions.
3. Make sure you have an entry_exists function (Not tough to implement. And it should return the cluster in AX, Hint: In a FAT Root Directory entry, the 15th byte points to the start of the cluster)
4. Use the above function to compute the offset of the sub directory and set the current directory offset respectively.
5. Done.
Smile
My goals for 0.1 are almost over. I will be pushing it on github soon. Smile
These were the goals:
1. Simple PS/2 Keyboard Driver - Done
2. A simple shell - Done
3. FAT16 Read FS + Directory Support. Done
4. Homemade C Library - scanf remaining rest all printf, strlen, malloc etc. are implemented.
5. Simple API for apps - Done (chdir, file_load, file_query, printc, prints, pci read, pci write etc.)
Well my goal for 0.2 is basically a FASM port.
The only functions missing are:
file_write
file_create
lseek - No idea why fasm needs it.
Btw does fasm need malloc when running under ring 0, my apps run under ring 0 there is no need for memory allocation, fasm can use as much memory as it wants.
Post 08 Mar 2014, 03:01
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1903
DOS386 08 Mar 2014, 08:05
> lseek - No idea why fasm needs it.

RTFS. Maybe GetFileSizeEx would be sufficient (you have to know the file size BEFORE you load it).

> I've seen some copyright and legal issues with LFN, what's it btw?

YES ... the NTLFN "technology" is most likely patented.

> should return the cluster in AX

Good ... you'll need EAX or DX:AX for FAT28 and RAX for some other filesystems Smile
Post 08 Mar 2014, 08:05
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 28 Apr 2014, 16:44
Hi sorry about the delay, been away.
Here's is the fat16 driver code, to DexOS.


Description:
Download
Filename: FatDriver_2012.zip
Filesize: 152.54 KB
Downloaded: 501 Time(s)

Post 28 Apr 2014, 16:44
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.