Issue
150 January 2003
Construct
an ATA Hard Drive Controller
GETTING
FAT
All
of the reading and writing up to this point was completed
with simple C routines that were teamed together to
perform a much larger and more complex task. Believe
it or not, designing the ATA hard drive controller hardware
and finishing the C coding for the controller I/O functions
was the easy part. The Ethernet code was just as easy,
because I copied AVR code that was already written for
the Packet Whacker microcontroller NIC. The next step
in the process of assembling a microcontroller-based
networkable mass storage system was a bit more demanding.
I
completed a tremendous amount of research in preparation
for writing AVR code to implement Microsoft’s FAT32
file system. Thanks to a series of Circuit Cellar articles
written by our own Jeff Bachiochi (Circuit Cellar 143–146),
I had a good idea about the roads I will travel and
battles I will fight.
The
journey starts with the bytes in Photo 4, the master
boot record. Because I’m not executing instructions
on a legacy x86 machine and using a PC BIOS or MSDOS,
I’ll have to interpret the data and adapt it to the
AVR. For instance, there’s executable code in the MBR
that I don’t care about. The problem is that I have
to navigate through it to find markers that either give
me information about where FAT-related constants and
parameters reside or point me to places where I can
read and write my data.
In
the case of the MBR, I’m interested only in the last
66 bytes, because that’s where the partition table resides.
The fun starts at offset 0x1BE in the MBR, which is
the first partition entry. Because the drives I formatted
were secondary drives, the FDISK program couldn’t make
their partitions active. So, the first byte at offset
0x1BE was 0x00 (inactive) on my drives.
Other
interesting information resides at offset 0x1C6 in the
MBR. This is the number of sectors between the MBR and
the first sector of the first partition. As you can
see in Photo 4, WinHex shows that number as 0x3F. I
used the WinHex program to dial in 0x3F sectors beyond
the MBR and, lo and behold, there was the FAT32 boot
sector with additional fields of information to parse.
The plan is to collect documentation and use WinHex
to obtain the actual visuals concerning how data and
control areas on a FAT32 hard disk are defined and laid
out.
So,
you see that I have my work cut out for me. The good
news is that you’ll be able to share the fruits of my
labor, because I will make the ATA hard drive controller
hardware available to those of you who want one. The
code discussed in this article is available for you
to download from the Circuit Cellar ftp site. All of
the basic functions necessary to read and write to an
ATA hard drive can be found in the code I’m providing.
After
you have all of the ImageCraft C source code, a copy
of WinHex, and an ATA hard drive controller in front
of you, you’ll see that everything you’ve read about
in this article isn’t complicated, it’s simply embedded.