Flash drives and hard drives have different hardware but use many of the same logical structures for storing and managing data. To write firmwarethat formats the media or reads and writes files in formatted media, you need to understand these structures and how to use the information in them.
This chapter explains how information is organized in the storage media and what information is stored in the master boot record sector that is the first sector in most storage devices.
A Look Inside
The logical structures, addressing methods, and file systems described in this book have their origins in the IBM PC, its derivatives, and the Microsoft operating systems developed for use on these computers. Over the years, the software components have evolved to support media with larger capacities and new capabilities. Some of the developments, such as the logical block addressing method, simplify the job of the mass-storage host and make it easier for embedded systems to support storage media. Other developments add support for capacities and abilities that embedded systems are unlikely to need. Windows and other operating systems support a variety of file systems, including options suitable for small embedded systems, so PCs and embedded systems can access the same media without problems.
Figure 7-1: Storage media can be formatted with a master boot record and up to four primary partitions.
Components of Formatted Media
As explained in Chapter 1, bytes in storage media are grouped in blocks called sectors. All of the sectors in the media have the same capacity, typically 512. Some file-system drivers support sector sizes that are multiples of 512.
Low-level formatting code allocates most of the sectors to one or more logical partitions, or volumes. (Figure 7-1) Formatting can be done by a PC, an embedded system, or another computer that interfaces to the media.
In most storage devices, the first sector in the media (sector zero) is the master boot record (MBR) sector. The sector contains an MBR structure, which in turn contains a partition table that defines the locations of up to four partitions. Under Windows, each partition appears as a separate volume, or logical drive with its own drive letter. The MBR sector also has an area that can contain executable code. A computer that boots from the storage device runs the executable code on boot up. In Windows systems, the Fdisk tool can perform the low-level formatting that stores the MBR structure in the first sector. Fdisk deletes all programs and data previously stored in the media.
Each volume begins with a boot sector that contains information specific to the volume’s file system. (Don’t confuse this boot sector with the master- boot-record sector.) File-system drivers in embedded systems often use a FAT file system. The term FAT refers to a family of file systems as well as the file allocation tables that every FAT system contains. The two most common FAT file systems are FAT16 and FAT32. A third FAT system, FAT12, is suitable only for smaller-capacity media and is uncommon these days even in embedded systems. (Floppy drives use FAT12.)
Every FAT volume contains the following elements: a reserved region that includes a boot sector, a FAT region that stores the FATs, and a file and directory data region that contains data clusters for storing files and directories. FAT12 and FAT16 volumes store the root directory in a dedicated root-directory region that precedes the data clusters, while FAT32 volumes store the root directory in any available data clusters.
A word about licensing: due to patent protections, manufacturers of some types of devices that implement FAT file systems may need to obtain a license from Microsoft. The devices include removable solid-state media and some consumer electronics devices. Microsoft provides source code and test specifications to licensees. For more information, see www.microsoft.com/mscorp/ip/tech/fat.asp.
Microsoft’s NTFS is an alternative to FAT file systems for PCs. Because Microsoft hasn’t publicly released a specification for NTFS, the file system isn’t practical for use in embedded systems that don’t use an operating system with NTFS support.
FAT file systems store files and directories in data clusters. Each cluster consists of one or more sectors. All clusters in a volume are the same size. Data clusters can provide larger-capacity units for program code to work with when storing and retrieving files.
The boot sector specifies what sectors are available for storing files and directories, what sectors contain the file allocation tables, and how many sectors are in a data cluster. The boot sector can also contain program code used in booting the computer.The FATs hold a record of the data clusters used by files.
The document that defines all three FAT file systems is a hardware white paper titled FAT32 File System Specification from Microsoft. Chapter 8 has more about FAT file systems.