Reputation: 442
So im pretty new at OS development, right now im writing my FAT12 filesystem code. I got all the writing to floppy disk using the FDC code working but I cant seem to wrap my head around how I should proceed when writing a file to disk.
I know where my root directory is and all its related info, but how should I look for the correct sector where to write my file? And after that, how should I add that entry to the FAT?
This is the code I use to traverse the Root Directory and find a file:
FILE fsysFatDirectory (const char* DirectoryName) {
FILE file;
unsigned char* buf;
PDIRECTORY directory;
//! get 8.3 directory name
char DosFileName[11];
ToDosFileName (DirectoryName, DosFileName, 11);
DosFileName[11]=0;
//! 14 sectors per directory
int sector;
for (sector=0; sector<14; sector++) {
//! read in sector of root directory
buf = (unsigned char*) flpydsk_read_sector (_MountInfo.rootOffset + sector );
//! get directory info
directory = (PDIRECTORY) buf;
//! 16 entries per sector
int i;
for (i=0; i<16; i++) {
//! get current filename
char name[11];
kmemcpy (name, directory->Filename, 11);
name[11]=0;
//! find a match?
if (kstrcmp (DosFileName, name) == 0) {
//! found it, set up file info
kstrcpy (file.name, DirectoryName);
file.id = 0;
file.currentCluster = directory->FirstCluster;
file.fileLength = directory->FileSize;
file.eof = 0;
file.fileLength = directory->FileSize;
//! set file type
if (directory->Attrib == 0x10)
file.flags = FS_DIRECTORY;
else
file.flags = FS_FILE;
//! return file
return file;
}
//! go to next directory
directory++;
}
}
//! unable to find file
file.flags = FS_INVALID;
return file;
}
I was planning to write something similar and walk through the root directory, entry by entry until I find a spot. As for adding/walking through FAT, I know each entry represents a cluster(entry 1 = cluster 1). But I dont know if i should be walking through the FAT instead of the root directory or both.
Most of my code is based off this tutorial: http://www.brokenthorn.com/Resources/OSDev22.html But he never added the Creating/Writing a File bit, so Im doing it on my own.
Any help is appreciated.
Upvotes: 2
Views: 695
Reputation: 5960
One implementation I can think of is to use a tree-like structure wherein the root directory is the parent directory.
struct fat_component{
fat_component *parent;
/*other fat components here*/
};
Once we have that we can then invoke something like an inline assembly call to int 13h/AH=03. I'm not too familiar with it but here's a link: http://stanislavs.org/helppc/int_13-3.html.
inline_asm("mov ah, 03h");
inline_asm("mov ch, %s", fc->cylindernum);
inline_asm("mov cl, %s", fc->sectornum);
/*other int13h initializations here*/
inline_asm ("int 13h");
Upvotes: 1