Some helpful links for Floppies, HDDs, and FAT
Posted: Thu Feb 14, 2002 12:00 am
Since info on disk seems to be in high demand,
I thought I'd provid some links and a bit of
sage advice.
First, the advice. To start, well, you can learn
a lot from just a few carefully considered
Google or Yahoo searches, and by paging through
the links page for this forum. You should be
able to find several pages detailing the FAT
format, and plenty of example code (mostly in C).
Read through as many samples as you can, but
*don't* copy them; it is too easy to fall into
the trap of modifying code you don't understand.
It is better to code from scratch and be certain
of what you are really doing.
I'd also advise anyone looking to work with disks
to go step by step, in something like this order:
1) In your development environment (i.e., while
running under DOS, or Windows or Linux) code some simple
routines to write to and read from a floppy raw
- that is, something that writes a sector and
reads a sector. Test it out on some scratch
disks, and expect to have to reformat them (or
even throw them away) afterwards.
2) When you are fairly confident that you can
write to and read from a sector, and can write
part of a sector without overwriting the existing
data, try reading in a FAT table from a formatted
floppy disk, and display it's contents and the
disk's root directory. Once this works, try to
use the FAT table to locate the sectors of a file
and read them, displaying the file.
3) Then, after you are sure that you can read the
FAT table and use it to read and display files,
write something that write a new file correctly:
that is, it selects the needed free sectors from
the FAT, writes the data to those sectors in the
correct order, updates both FAT tables, and
writes an file entry in the directory. Again,
use scratch disks and junk data while testing
this.
4) Repeat steps 1 through 3 for FAT16 and FAT32,
using (and I can't strss this enough) a scratch
hard drive on an second-hand PC (you are using a
second hand PC as your target system, aren't
you?), or else under a simulator such as Bochs.
Expect to have to learn a lot more details
about logical block addressing and other
complications.
5) THROW WHAT YOU JUST WROTE AWAY AND START OVER.
Why? Because a) your first try at something
like this is almost always a messy hack,
b) you'll want to seperate the driver code from
the filesystem code, and c) you'll need the
new code to match whatever driver/resource model
you chose to use in your OS, and the test code
probably is a poor match for it. The 'build one
to throw away' principle has stood the test of
time, even if it tends to get ignored a lot.
BTW, this approach is useful for most of the major
low-level coding work on a PC, especially with
video drivers.
Oh, and you might want to iterate through 1-4
twice, once in real mode and once in p-mode, as
real-mode drivers are usually easier to write
and can use BIOS calls to do the dirty work.
Now for the links I promised:
http://www.manningjames.com/college/sem2/fat12.htm
http://www.pcguide.com/ref/hdd/file/partSizes-c.html
http://www.rose-hulman.edu/Class/cs/cs3 ... fat12.html
http://home.teleport.com/~brainy/fat32.htm
http://home.teleport.com/~brainy/fat16.htm
http://home.teleport.com/~brainy/diskaccess.htm
http://www11.ewebcity.com/nutsnduts/soyou1.htm
http://www.cs.plu.edu/pub/faculty/csce3 ... /home.html
http://www.groovyweb.uklinux.net/?page_ ... 20own%20os
HTH. HAND.
I thought I'd provid some links and a bit of
sage advice.
First, the advice. To start, well, you can learn
a lot from just a few carefully considered
Google or Yahoo searches, and by paging through
the links page for this forum. You should be
able to find several pages detailing the FAT
format, and plenty of example code (mostly in C).
Read through as many samples as you can, but
*don't* copy them; it is too easy to fall into
the trap of modifying code you don't understand.
It is better to code from scratch and be certain
of what you are really doing.
I'd also advise anyone looking to work with disks
to go step by step, in something like this order:
1) In your development environment (i.e., while
running under DOS, or Windows or Linux) code some simple
routines to write to and read from a floppy raw
- that is, something that writes a sector and
reads a sector. Test it out on some scratch
disks, and expect to have to reformat them (or
even throw them away) afterwards.
2) When you are fairly confident that you can
write to and read from a sector, and can write
part of a sector without overwriting the existing
data, try reading in a FAT table from a formatted
floppy disk, and display it's contents and the
disk's root directory. Once this works, try to
use the FAT table to locate the sectors of a file
and read them, displaying the file.
3) Then, after you are sure that you can read the
FAT table and use it to read and display files,
write something that write a new file correctly:
that is, it selects the needed free sectors from
the FAT, writes the data to those sectors in the
correct order, updates both FAT tables, and
writes an file entry in the directory. Again,
use scratch disks and junk data while testing
this.
4) Repeat steps 1 through 3 for FAT16 and FAT32,
using (and I can't strss this enough) a scratch
hard drive on an second-hand PC (you are using a
second hand PC as your target system, aren't
you?), or else under a simulator such as Bochs.
Expect to have to learn a lot more details
about logical block addressing and other
complications.
5) THROW WHAT YOU JUST WROTE AWAY AND START OVER.
Why? Because a) your first try at something
like this is almost always a messy hack,
b) you'll want to seperate the driver code from
the filesystem code, and c) you'll need the
new code to match whatever driver/resource model
you chose to use in your OS, and the test code
probably is a poor match for it. The 'build one
to throw away' principle has stood the test of
time, even if it tends to get ignored a lot.
BTW, this approach is useful for most of the major
low-level coding work on a PC, especially with
video drivers.
Oh, and you might want to iterate through 1-4
twice, once in real mode and once in p-mode, as
real-mode drivers are usually easier to write
and can use BIOS calls to do the dirty work.
Now for the links I promised:
http://www.manningjames.com/college/sem2/fat12.htm
http://www.pcguide.com/ref/hdd/file/partSizes-c.html
http://www.rose-hulman.edu/Class/cs/cs3 ... fat12.html
http://home.teleport.com/~brainy/fat32.htm
http://home.teleport.com/~brainy/fat16.htm
http://home.teleport.com/~brainy/diskaccess.htm
http://www11.ewebcity.com/nutsnduts/soyou1.htm
http://www.cs.plu.edu/pub/faculty/csce3 ... /home.html
http://www.groovyweb.uklinux.net/?page_ ... 20own%20os
HTH. HAND.