📄 paul's 8051 code library understanding the fat32 filesystem.htm
字号:
href="file:///E:/IDE-MP3s/pjrc_8051/serial-eeprom.html"
tppabs="http://www.pjrc.com/tech/8051/serial-eeprom.html">Serial
EEPROM</A><BR><IMG height=10 alt=""
src="Paul's 8051 Code Library Understanding the FAT32 Filesystem.files/a.gif"
width=10 tppabs="http://www.pjrc.com/img/a.gif"> <A
href="file:///E:/IDE-MP3s/pjrc_8051/amd-flash-rom.html"
tppabs="http://www.pjrc.com/tech/8051/amd-flash-rom.html">AMD 28F256
Flash</A><BR><IMG height=10 alt=""
src="Paul's 8051 Code Library Understanding the FAT32 Filesystem.files/a.gif"
width=10 tppabs="http://www.pjrc.com/img/a.gif"> <A
href="file:///E:/IDE-MP3s/pjrc_8051/xilinx-program.html"
tppabs="http://www.pjrc.com/tech/8051/xilinx-program.html">Xilinx
3000</A><BR><IMG height=10 alt=""
src="Paul's 8051 Code Library Understanding the FAT32 Filesystem.files/b.gif"
width=10 tppabs="http://www.pjrc.com/img/b.gif"> <B>IDE Hard
Drive</B><BR></SMALL>
<DIV style="PADDING-LEFT: 1.2em"><SMALL><IMG height=10 alt=""
src="Paul's 8051 Code Library Understanding the FAT32 Filesystem.files/a.gif"
width=10 tppabs="http://www.pjrc.com/img/a.gif"> <A
href="file:///E:/IDE-MP3s/pjrc_8051/index-5.html"
tppabs="http://www.pjrc.com/tech/8051/ide/index.html">Main
Page</A><BR><IMG height=10 alt=selected
src="Paul's 8051 Code Library Understanding the FAT32 Filesystem.files/b.gif"
width=10 tppabs="http://www.pjrc.com/img/b.gif"> <SPAN
style="BACKGROUND-COLOR: #b0e4a8">FAT32 Info</SPAN><BR><IMG
height=10 alt=""
src="Paul's 8051 Code Library Understanding the FAT32 Filesystem.files/a.gif"
width=10 tppabs="http://www.pjrc.com/img/a.gif"> <A
href="file:///E:/IDE-MP3s/pjrc_8051/wesley.html"
tppabs="http://www.pjrc.com/tech/8051/ide/wesley.html">Peter
Faasse</A><BR></SMALL></DIV><SMALL><IMG height=10 alt=""
src="Paul's 8051 Code Library Understanding the FAT32 Filesystem.files/a.gif"
width=10 tppabs="http://www.pjrc.com/img/a.gif"> <A
href="file:///E:/IDE-MP3s/pjrc_8051/index-3.html"
tppabs="http://www.pjrc.com/tech/8051/contrib/tb51/index.html">Tiny
Basic</A><BR></SMALL></DIV><SMALL><A
href="file:///E:/IDE-MP3s/pjrc_8051/aicp-schematic.html"
tppabs="http://www.pjrc.com/tech/8051/aicp-schematic.html"><IMG
height=10 alt=""
src="Paul's 8051 Code Library Understanding the FAT32 Filesystem.files/c.gif"
width=10 border=0 tppabs="http://www.pjrc.com/img/c.gif"> <B>89C2051
Programmer</B></A><BR><A
href="file:///E:/IDE-MP3s/pjrc_8051/free.html"
tppabs="http://www.pjrc.com/tech/8051/free.html"><IMG height=10
alt=""
src="Paul's 8051 Code Library Understanding the FAT32 Filesystem.files/c.gif"
width=10 border=0 tppabs="http://www.pjrc.com/img/c.gif"> <B>Other
Resources</B></A><BR></SMALL></TD></TR>
<TR>
<TD height=60></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><A
name=navend></A><!--/htdig_noindex--><!--nav end-->
<H2>Understanding FAT32 Filesystems</H2>This page is intended to help you
understand how to access data on Microsoft FAT32 filesystems, commonly used on
hard drives ranging in size from 500 megs to hundreds of gigabytes. FAT is a
relatively simple and unsophisticated filesystem that is understood by nearly
all operating systems, including Linux and MacOS, so it's usually a common
choice for firmware-based projects that need to access hard drives. FAT16 and
FAT12 are very similar and used on smaller disks. This page will concentrate on
FAT32 only (to keep it simple), and briefly mention where these other two are
different.
<P>The <A
href="javascript:if(confirm('http://www.microsoft.com/whdc/hwdev/hardware/fatgen.mspx%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.microsoft.com/whdc/hwdev/hardware/fatgen.mspx'"
tppabs="http://www.microsoft.com/whdc/hwdev/hardware/fatgen.mspx">official FAT
specification is available from Microsoft</A>, complete with a software "license
agreement". Saddly, the document from Microsoft is hard to read if you do not
already understand the FAT filesystem structure, and it lacks information about
disk partitioning which also must be dealt with to properly use standard hard
drives. While you may find the Microsoft spec useful, this page is meant to
"stand alone"... and you can simply read it without suffering through 3 pages of
legalese!
<P>However, this page will intentionally "gloss over" many small details and
omit many of the finer points, in an attempt to keep it simple and easy to read
for anyone faced with learning FAT32 without any previous exposure.
<H2>Where To Start... How About At The Beginning?</H2>The first sector of the
drive is called the Master Boot Record (MBR). You can read it with LBA = 0. For
any new projects, you should not even worry about accessing a drive in CHS mode,
as LBA just numbers the sectors sequentially starting at zero, which is much
simpler. All IDE drives support accessing sectors using LBA. Also, all IDE
drives use sectors that are 512 bytes. Recent Microsoft operating systems refer
to using larger sectors, but the drives still use 512 bytes per sector and MS is
just treating multiple sectors as if they were one sector. The remainder of this
page will only refer to LBA address of 512 byte sectors.
<P>The first 446 bytes of the MBR are code that boots the computer. This is
followed by a 64 byte partition table, and the last two bytes are always 0x55
and 0xAA. You should always check these last two bytes, as a simple "sanity
check" that the MBR is ok.
<P>
<TABLE width=262 align=center>
<TBODY>
<TR>
<TD><IMG height=300 alt="MBR Diagrag"
src="Paul's 8051 Code Library Understanding the FAT32 Filesystem.files/mbr.gif"
width=250
tppabs="http://www.pjrc.com/tech/8051/ide/mbr.gif"><BR><SMALL><B>Figure
1</B>: MBR (first sector) layout</SMALL></TD></TR></TBODY></TABLE>
<P>The MBR can only represent four partitions. A technique called "extended"
partitioning is used to allow more than four, and often times it is used when
there are more than two partitions. All we're going to say about extended
partitions is that they appear in this table just like a normal partition, and
their first sector has another partition table that describes the partitions
within its space. But for the sake of simply getting some code to work, we're
going to not worry about extended partitions (and repartition and reformat any
drive that has them....) The most common scenario is only one partition using
the whole drive, with partitions 2, 3 and 4 blank.
<P>Each partition description is just 16 bytes, and the good news is that you
can usually just ignore most of them. The fifth byte is a <I>Type Code</I> that
tells what type of filesystem is supposed to be contained within the partition,
and the ninth through twelth bytes indicate the <I>LBA Begin</I> address where
that partition begins on the disk.
<P>
<TABLE width=480 align=center>
<TBODY>
<TR>
<TD><IMG height=42 alt="Partition Entry"
src="Paul's 8051 Code Library Understanding the FAT32 Filesystem.files/partition.gif"
width=469
tppabs="http://www.pjrc.com/tech/8051/ide/partition.gif"><BR><SMALL><B>Figure
2</B>: 16-byte partition entry</SMALL></TD></TR></TBODY></TABLE>
<P>Normally you only need to check the <I>Type Code</I> of each entry, looking
for either 0x0B or 0x0C (the two that are used for FAT32), and then read the
<I>LBA Begin</I> to learn where the FAT32 filesystem is located on the disk.
<P>TODO: add a table of known type codes, with the FAT32 ones colored
<P>The <I>Number of Sectors</I> field can be checked to make sure you do not
access (particularly write) beyond the end of the space that is allocated for
the parition. However, the FAT32 filesystem itself contains information about
its size, so this <I>Number of Sectors</I> field is redundant. Several of
Microsoft's operating systems ignore it and instead rely on the size information
embedded within the first sector of the filesystem. (yes, I have experimentally
verified this, though unintentionally :) Linux checks the <I>Number of
Sectors</I> field and properly prevents access beyond the allocated space. Most
firmware will probably ignore it.
<P>The Boot Flag, CHS Begin, and CHS End fields should be ignored.
<H2>FAT32 Volume ID... Yet Another First Sector</H2>The first step to reading
the FAT32 filesystem is the read its first sector, called the Volume ID. The
Volume ID is read using the LBA Begin address found from the partition table.
From this sector, you will extract information that tells you everything you
need to know about the physical layout of the FAT32 filesystem.
<P>Microsoft's specification lists many variables, and the FAT32 Volume ID is
slightly different than the older ones used for FAT16 and FAT12. Fortunately,
most of the information is not needed for simple code. Only four variables are
required, and three others should be checked to make sure they have the expected
values.
<P>
<TABLE width=262 align=center>
<TBODY>
<TR>
<TD><IMG height=300 alt="Volume ID Diagram"
src="Paul's 8051 Code Library Understanding the FAT32 Filesystem.files/volume_id.gif"
width=250
tppabs="http://www.pjrc.com/tech/8051/ide/volume_id.gif"><BR><SMALL><B>Figure
4</B>: FAT32 Volume ID, critical fields</SMALL></TD></TR></TBODY></TABLE>
<P>
<TABLE cellSpacing=0 cellPadding=4 align=center border=1>
<TBODY>
<TR>
<TH>Field</TH>
<TH>Microsoft's Name</TH>
<TH>Offset</TH>
<TH>Size</TH>
<TH>Value</TH></TR>
<TR>
<TD>Bytes Per Sector</TD>
<TD>BPB_BytsPerSec</TD>
<TD>0x0B</TD>
<TD align=right>16 Bits</TD>
<TD>Always 512 Bytes</TD></TR>
<TR>
<TD>Sectors Per Cluster</TD>
<TD>BPB_SecPerClus</TD>
<TD>0x0D</TD>
<TD align=right>8 Bits</TD>
<TD>1,2,4,8,16,32,64,128</TD></TR>
<TR>
<TD>Number of Reserved Sectors</TD>
<TD>BPB_RsvdSecCnt</TD>
<TD>0x0E</TD>
<TD align=right>16 Bits</TD>
<TD>Usually 0x20</TD></TR>
<TR>
<TD>Number of FATs</TD>
<TD>BPB_NumFATs</TD>
<TD>0x10</TD>
<TD align=right>8 Bits</TD>
<TD>Always 2</TD></TR>
<TR>
<TD>Sectors Per FAT</TD>
<TD>BPB_FATSz32</TD>
<TD>0x24</TD>
<TD>32 Bits</TD>
<TD align=right>Depends on disk size</TD></TR>
<TR>
<TD>Root Directory First Cluster</TD>
<TD>BPB_RootClus</TD>
<TD>0x2C</TD>
<TD align=right>32 Bits</TD>
<TD>Usually 0x00000002</TD></TR>
<TR>
<TD>Signature</TD>
<TD>(none)</TD>
<TD>0x1FE</TD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -