Running FAT16 File Systems and DOS Commands on SHARC® Processors
Total Page:16
File Type:pdf, Size:1020Kb
Engineer-to-Engineer Note EE-329 Technical notes on using Analog Devices DSPs, processors and development tools Visit our Web resources http://www.analog.com/ee-notes and http://www.analog.com/processors or e-mail [email protected] or [email protected] for technical support. a Running FAT16 File Systems and DOS Commands on SHARC® Processors Contributed by Mitesh Moonat, Jagadeesh Rayala and Aseem Vasudev Rev 1 – September 28, 2007 Introduction File Allocation Table 16 (FAT16) is a widely used file format implemented on mid-size memory devices (up to 2GB) such as NAND flash, Multi-Media Card (MMC), and Secure Digital (SD) cards. These memory devices are used mainly to store multimedia content and data. Therefore, it is necessary for media processing software applications to interpret the FAT16 file format in order to access and/or manipulate the content stored on such memory devices. This EE-Note discusses the implementation of various commands similar to Disk Operating System (DOS) commands to access and modify the FAT16 file system on an MMC interfaced to an ADSP-21369 SHARC® processor. This document includes details about dealing with the files stored on MMC. The source code for this interface has been developed and validated on an ADSP-21369 EZ-KIT Lite® evaluation board and is provided with this application note. Although, this document refers to the ADSP-21369 SHARC processor, the same concepts L presented here apply to ADSP-21367, ADSP-21368, and ADSP-2137x SHARC processors. The implementation of basic DOS commands that list files and folders in current directory, create a directory, copy a file, read a file, search a file, navigate through sub-folders in current directory, or delete a file or directory are discussed in detail. The code supports long file names (up to 255 bytes). Other file systems such as FAT12 and FAT32 are briefly discussed and are compared to FAT16 file system. FAT Overview FAT is a partially patented file system developed by Microsoft® for MS-DOS®. It involves the use of a table that centralizes the information about the areas belonging to files. This information helps to identify which areas are free or usable, and where each file is stored on the disk. It also identifies the areas that are unusable. To reduce the management complexity, disk space is allocated to files in contiguous groups of hardware sectors called as clusters. The maximum number of clusters possible that can be addressed by a file system depends on the number of bits being used to identify a cluster in FAT. Currently, there are three types of FAT file systems: FAT12, FAT16, and FAT32. The basic difference between them is the number of bits in the FAT entries on a disk. FAT12 uses 12-bit entries, FAT16 uses 16-bit entries, and FAT32 uses 28-bit entries. The FAT standard has also evolved in several ways, Copyright 2007, Analog Devices, Inc. All rights reserved. Analog Devices assumes no responsibility for customer product design or the use or application of customers’ products or for any infringements of patents or rights of others which may result from Analog Devices assistance. All trademarks and logos are property of their respective holders. Information furnished by Analog Devices applications and development tools engineers is believed to be accurate and reliable, however no responsibility is assumed by Analog Devices regarding technical accuracy and topicality of the content provided in Analog Devices Engineer-to-Engineer Notes. a preserving backward compatibility with existing software. Table 1 shows basic differences between the different FAT file systems. FAT is still a normal file system for removable media (with the exception of CDs and DVDs), with FAT12 used on floppies and FAT16 used on most other removable media (such as flash memory cards for digital cameras, mobile phones, and USB flash drives). Most removable media are not yet large enough to benefit from FAT32, although some larger flash drives such as Secure Digital High-Capacity (SDHC) cards use it. FAT16 is used on these drives for reasons of compatibility and size overhead. FAT12 FAT16 FAT32 Uses Floppies and small hard Small to large hard disk Medium to very large hard disk volumes volumes disk volumes Size of each FAT entry 12 bits 16 bits 28 bits Volume size (maximum) 16 Mbytes 2 Gbytes about 2^41 Table 1. Comparison between various FAT versions FAT16 on a Disk Figure 1 shows the order of structures on a disk or a FAT partition. Boot More reserved File File Root Data Region (for files and directories) sector sectors Allocation Allocation Directory (To end of partition or disk) (optional) Table 1 Table 2 Figure 1. Sections of a disk or partition Reserved Sectors The beginning sectors are reserved sectors. The first sector (sector 0), which is called the Boot Sector (or Partition Boot Record), is mandatory; the others are optional. The boot sector includes an area called the BIOS parameter block that includes the basic file system information (in particular, the FAT type and pointers to the location of the other sections). The total count of reserved sectors is indicated by a field inside the Boot Sector. FAT Region The FAT region contains two copies of FAT. Though rarely used, the second copy is helpful when the default copy gets corrupted. This region contains the mapping details of the data, region indicating the clusters are used and the clusters that are not used. Root Directory Region The root directory region contains a Directory Table that stores information about the files and directories located in the root directory region. This region has a fixed maximum size that is pre-allocated at the time that the volume is formatted. This region is only used for FAT12 and FAT16; FAT32 stores the root directory entries in the data region just as any other sub-directories. Running FAT16 File Systems and DOS Commands on SHARC® Processors (EE-329) Page 2 of 24 a Data Region The data region is the actual region where all files and directories are stored. This region occupies the majority of the partition space. Files and directories stored in this region can be made arbitrarily long simply by adding more clusters in the clusters chain of the FAT. Details of FAT16 Structure The following sections describe the entire FAT16 structure in detail. Boot Sector Table 2 lists the fields in the boot sector. Offset Description Size (bytes) 0x00 Jump code + NOP 3 0x03 OEM name 8 0x0B Bytes per sector 2 0x0D Sectors per cluster 1 0x0E Reserved sectors 2 0x10 Number of copies of FAT 1 0x11 Maximum root directory entries 2 0x13 Number of sectors in partition smaller than 32 Mbytes 2 0x15 Media descriptor (F8H for hard disks) 1 0x16 Sectors per FAT 2 0x18 Sectors per track 2 0x1A Number of heads 2 0x1C Number of hidden sectors in partition 4 0x20 Number of sectors in partition 4 0x24 Logical drive number of partition 2 0x26 Extended signature (29H) 1 0x27 Serial number of partition 4 0x2B Volume name of partition 11 0x36 FAT name (FAT16) 8 0x3E Executable code 448 0xFE Executable marker (0x55 0xAA) 2 l TOTAL 512 Table 2. Boot sector fields Running FAT16 File Systems and DOS Commands on SHARC® Processors (EE-329) Page 3 of 24 a Based on information from the boot sector, the offset locations for all the other regions are calculated as shown in Table 3. Offset Description Start of partition Boot sector Start + number of reserved sectors Fat tables Start + number of reserved sectors+ (number of sectors per FAT * 2) Root directory Start + number of reserved sectors+ (number of sectors per FAT * 2) + Data area (starts with cluster number 2) ((maximum root directory entries * 32) / bytes per sector) Table 3. Offset calculation for various regions of the disk File Allocation Table A partition is divided into identically sized clusters, small blocks of contiguous space. Cluster size may vary, depending upon the size of the partition. Typically, cluster size is between 2 Kbytes and 32 Kbytes. Each file may occupy one or more of these clusters, depending on its size. A file is represented by a chain of these clusters. The clusters in these chains are not necessarily adjacent to one another on the disk's surface and may be fragmented throughout the data region. FAT is a list of entries that map to each cluster on the partition. Each entry records one of the following information listed in Table 4. Sector Number FAT entry Description 1 0x0002 - 0xFFEF Address of the next cluster in a chain. 2 0xFFF8 - 0xFFFF Last cluster of the file (EOF). 3 0xFFF7 Bad cluster 4 0x0001 Reserved cluster 5 0x0000 Unused/Free cluster Table 4. FAT entries Figure 2 shows the mapping of three files using FAT entries. Figure 2. File allocation table Running FAT16 File Systems and DOS Commands on SHARC® Processors (EE-329) Page 4 of 24 a Directory Table A directory table is a special type of file that represents a directory (commonly called a folder). The parameters related to any file or sub-directory are represented by a 32-byte entry in the directory table, corresponding to the parent directory. Each entry records the name, extension, attributes, date and time of creation, address of the first cluster, and the size of the file or directory. The directory table for the root directory is stored in the root directory region location; the directory tables for directories other than the root directory are stored in the data region. Entries with the sub-directory flag set should have a size of 0.