Jump to content
xisto Community
Sign in to follow this  
AdityaKar

The Fat File System - Part 2 Processing of Files

Recommended Posts

Hello again friends!

 

In my previous tutorial The FAT File System - Part 1 I had explained the internal storage structure of FAT FS. Though I was a little disappointed to see only 20 views till now. Neverthless, here I am with the next(and probably last) tutorial on this topic.

This time we are going to talk about the processing of files on FAT file system. But first let me clear any doubts you have in understanding some things.

 

Boot Sector AREA:

 

I know you must be wondering what’s this area on the disk is about and what can I get from this area. Ok first let me tell you this area is located on the floppy disk in the very first sector. Same for HDDs but on primary partition. When a computer first boots up the BIOS loads the first sector of the disk automatically into the memory and transfers control to it. This loading of the data is done at location 0x7C00 in memory. Normally a sector is 512 bytes therefore this first 512 constitutes the BOOT SECTOR.

 

A boot sector has information about the format of the disk for example the number of clusters, size of a sector and many other things but it also has a very small assembly code written to load the OS into memory. Remember since we only have 512 bytes we have to be very careful with what we want in the boot loader. The boot loader usually isn’t complete in just 1 sector and if other sectors have to be loaded also then provision has to be made in the boot sector‘s bootstrap routine to load these sectors.

 

Normally these are system files which are located on the first two clusters. The clusters are numbered after the system area so they all are actually part of the data area. However when system files are present the user data normally starts from cluster 2 (clusters are numbered from 0). The Boot Sector also provides information about the kind of files system we have on the disk. Although this can be done with the fields in which strings such as FAT12 ,FAT16, FAT32 is present but however Microsoft doesn’t pay attention to these fields and calculates the type of FAT with the number of clusters we have on the disk. I’m not going to discuss the math involved with that you people look that up somewhere. I’m just trying to provide you basic knowledge so you won’t get confused.

 

If you want to know how to create a boot loader (YES a real one) take a look at the end of this tutorial.

 

 

The Directory Entry

 

Well this one really had me confused. Because of the name I thought it was used to make entry for the directories but soon I was about to find out that this was just the beginning of the end. I had started looking for it in some C books but that was a mistake so I want to make sure that you completely understand this.

 

Directory entry is not just for the maintaining directories it’s also used for files. Remember that the directory entry is for the files /directories under the root directory. Now with this info we calculated the number of files that can be made on a standard floppy under a root directory. This number came out to be 224 files / directories under root.

 

But we have created a larger number of files than this. Its true but can’t be done without creating subdirectories. Just by toggling a bit a file can be made into a subdirectory. Subdirectories are not special yet they are in the manner they are treated by the file system driver. First of all the size of directory is left 0. It can be calculated very easily by looking at the files present in that directory since files do have a field about their size.

 

Note that this size is different from the one on the disk, this is because if you create a file and put a character in it its size will be 1 byte. Now when you store this file on the disk the size on the disk will be 512 bytes!! That’s a waste of 511 bytes! This is because the OS allocated a full cluster to it regardless of the size of the file. If the file can’t be allocated on a single cluster than more clusters are allocated for it. This is done to increase efficiency of the disk and to avoid a read every time something is needed from the disk. Also in case of floppy this is 511 bytes since 1 sector / cluster. In case of HDD more than 1 sectors are present / cluster and this can lead to a major waste of space. Also this feature allows the file to grow freely as long as it doesn’t need another cluster so FAT entries need not be changed every time a modification to the file is made.

 

Now what happens to files present in subdirectories? Well they act as FAT entries for the files / data present under them. That is if we created a subdirectory mydir under root (i.e. now we have c:\mydir) and now we create a file named myfile under this directory then the entry for this file will NOT be recorded in the system DIRECTORY ENTRY ( the one for root) instead its entry will be recorded as contents of the cluster allocated to the directory mydir. When we need to read a file from this directory the system will search in the contents of this directory to load the file (FAT will be needed to locate the cluster where this directory‘s contents are present). Since contents of the file can be spread over the clusters therefore more entries can be recorded than in the directory entry itself.

 

However it must be noted that now the clusters are being used for data as well as directory entries therefore you can create a large number of small files but can’t create large number of large files. So if are having any bright ideas of using this way to store data on disk well just forget it.

 

 

User DATA AREA

 

This area appears right after the Directory Entry. The first two clusters contain the OS files needed for system startup. Therefore user data begins from cluster number 2. Every cluster has a corresponding entry in the FAT. Remember that a FAT is just a linked list which points to the next cluster or indicates the EOF. There are various values assigned to FAT entries for each cluster when they are free, contain some data, erased. These values are 0, some other cluster number, and 0xE5.

 

When a file/ directory is erased its contents are not actually erased they are present on the disk. So it’s possible to recover them but before any write operation is performed on the disk. The first byte of the name of file / directory is marked with a special value 0xE5. This value is seen by the driver of the file system and it interprets that the cluster entry in FAT is erased. Therefore since it’s a linked list if we lose 1 entry the whole file / directory. To again recover the file / directory we just need to change the value of this byte to something else and WALAH! We have a file recovery utility!! Its not easy though getting hands on the HDD with XP watching over you. Try out some functions of bios.h like the one named biosdisk you will know what I’m talking about.

 

1 more thing it seems to me that VPC uses a rather strange kind of BOOT SECTOR we tried to read its contents but couldn’t get anything everything was just so something else. If you can get the same info as the BOOT SECTOR described above please let me know.

A very small BOOT LOADER

 

Ok let’s try our hands on writing a boot loader. Well this boot loader will print a character on the screen when the system starts up and then sits idle doing nothing.( REBOOT to your OS fun is OVER ha ha ). You can extend this to anything you want to do.

 

The Basics

The boot loader is exactly 512 bytes. No more no less. Now creating our boot loader you must be wondering how we fill up 512 bytes! Well its easy just pad it up with Zeros when you are done. The last 2 bytes i.e. boot sector [510]=0x55 and boot sector[511]=0xAA. Actually since all PCs are Little Endian architecture based therefore the actual signature of boot loader is 0xAA55. Time for the CODE u will need nasm to assemble this code into pure binary file.(no obj /exe)

 

[bITS 16] ;We are in real mode which is 16 bits when system starts up.

 

[ORG 0x7c00] ; since BIOS loads the first sector at location 0x7C00 we assemble according to it.

 

mov ah,0eh ; setting up registers for interrupt 0x10(print in text mode)

mov al,'P' ;character to print.

mov bh,0fh ;Color of the Character

mov bl,0

int 10h ; generate interrupt

 

hang:

jmp hang

 

times 510-($-$$) db 0 ; pad up the rest of bytes uptil 510 to zero

dw 55aah ; define word 0xAA55 in reverse order boot loader signature

assemble this using this command on the command prompt

nasm –f bin boot.asm –o boot.bin

 

boot is the asm file we created and –o specifies the output bin file.

 

There are various options take a look at them. Now write this bin file on the first sector of the floppy or u can create a virtual floppy (that day isn’t far when people will have virtual notebooks!!) and boot from it.

 

You can try to jump continuously to the print section and print the same or some different character such as your name etc. This was just an attempt to give you an idea of what exactly goes behind the scenes which we take for granted!!

 

There’s way too much in boot sector writing itself I only know this much about it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.