Embedded Systems Interfacing
Overview
                                                                                  DOS Data Structure
          File I/O                                                                        Sectors and Clusters
                                                                                          Master Boot Records/Boot Record
                                                                                          File Allocation Table
                Embedded Systems Interfacing                                              Root Directory
                        Interfacing
                                                                                          Subdirectory File
                                                                                  File IO Module
                                                                 1                                                                                              2
           Sectors and Clusters                                                SD Card Map Structure
              Sectors are hardware units with 512 bytes                          Partition Table (divides media into
               per sector                                                          drives)
              Cluster is an file system unit with 2nx512                         Boot Record (divides drive into data
               bytes per cluster                                                   structures)
              Each cluster numbers start at a value of 2 at                      16-Bit File Allocation Table 1 (FAT1)
               start of the data space (see future slide)
                                                                                  16-Bit File Allocation Table 2 (FAT2)
               Each cluster is an allocation unit in the File
                                                                                   Root Directory (fixed number of files)
           
                                                                               
               Allocation Table or FAT (see future slide)
                                                                                  Data Area (files and subdirectories)
                                                                 3                                                                                              4
           File System Layout                                                  512 MB SD Card Map
                   Master Boot Record
                    (Partition Table)
                                         • Cluster #1 used for       Address       Sector     Cluster   Description
                      Boot Record        Disk Type                   0:0000        0          N.A.      Master Boot Record which contains the Partition table
                                                                     1:DA00        0ED        N.A       Boot Record
                        FAT #1
                                         •MBR, BR, FATs and          1:DC00        0EE        N.A       File Allocation Table 1 with 16-bit FAT entries
                        FAT #2           Root not covered by         2:CE00        167        N.A.      File Allocation Table 2 with 16-bit FAT entries
                                                                     3:C000        1E0        N.A.      Root Directory with VFAT and directory entries
                     Root Directory      FAT Cluster Numbers
                                                                     4:0000        200        2         Bill of Right.txt
                       Cluster #2        •FAT-16 uses two            4:4000        220        3         MyFile.txt
                                         bytes per cluster
                       Cluster #3
                                                                               512 Bytes = 1 Sector1
                     Cluster #nnnn                                                                                          1   Hardware Term
                                                                 5                                                                                              6
Copyright James Grover, 2008                                                                                                                                        1
     Embedded Systems Interfacing
                   Partition Table Image                                                               SD Card Partition Table
                                                                                                  Offset          Size                Data                             Description
       Contains                                                                                   0x1BE           1 Byte              0x00                             Not bootable media
       Code for
                                                                                                  0x01BF          1 Byte              0x03                             Starting Head (Boot Record)
       Bootable                                                                                   0x1C0           6 Bits              0B110001                         Starting Sector (Boot Record)
       Media                                                                                      0x1C0           10 Bits             0B000000000000                   Staring Cylinder (Boot Record)
                                                                                                  0x1C2           1 Byte              0x06                             System ID: BIGDOS FAT16 partition
                                                                                                  0x1C3           1 Byte              0x0F                             Ending Head
                                                                                                  0x1C4           6 bits              0B111111                         Ending Sector
                                                                                                  0x1C4           10 bits             0B1101011011                     Ending Cylinder
                                                                                                  0x1C6           4 Bytes             0x000000ED                       Relative Sector
                               Read/Executed By B.I.O.S.                                          0x1CA           4 Bytes             0X000F1DA3                       Total Sectors
                                                                                             7
                                                                                                  Go to LBA 0x000000ED for Boot Record                                                                     8
                   Boot Record Image                                                                   SD Card Boot Record
                                                                                                           Offset           Size               Data                Description
                                                                                                           0x0B             2 Bytes            0x200               Bytes per sector (512)
                                                                                                           0x0D             1 Byte             0x20                Sectors per cluster (32)
        Contains                                                                                           0x0E             2 Bytes            0x0001              Reserved sectors
                                                                                                           0x10             1 Byte             0x02                Number of File Allocation Tables
        Code for                                                                                           0x11             2 Bytes            0x0200              Root entries (512)
        Bootable                                                                                           0x13             2 Bytes            0x0000              Small sectors
        Media
                                                                                                             Standard Information in BIOS Parameter Block
                                                                                                                • 512 Bytes/Sector
                                Read/Executed By B.I.O.S.                                                       • 32 Sectors/Cluster
                                                                                                                • 1 Reserved Sector
                                                                                                                • 2 FATs
                                                                                                                • 200 Directory Entries (x 32 Bytes/Entry)
                                                                                             9                                                                                                             10
                   SD Card Boot Record                                                                 FAT Entry Reference
          Offset     Size         Data             Description
          0x15       1 Byte       0xF8             Media type                                                                         Entry             Description
          0x16       2 Bytes      0x0079           Sectors per File Allocation Table (121)                                            0x0000            Free cluster
          0x18       2 Bytes      0x003F           Sectors per track (63)                                                             0x0001            Reserved cluster
                                                                                                                               0x0002 - 0xFFEF          Used cluster; value points to next cluster
          0x1A       2 Bytes      0x0010           Number of heads (16)
                                                                                                                               0xFFF0 - 0xFFF6          Reserved values
          0x1C       4 Bytes      0x000000ED       Hidden sectors (237)
                                                                                                                                      0xFFF7            Bad cluster
          0x20       4 Bytes      0x000F1DA3       Large sectors (990627)
                                                                                                                               0xFFF8 - 0xFFFF          Last cluster in file
          0x24       1 Bytes      0x80             Physical drive number
          0x25       1 Byte       0x00             Current head
          0x26       1 Byte       0x29             Signature                                               Maximum of 65520 Clusters in FAT-16
          0x27       4 bytes      0x63343362       Volume serial number
          0x2B       11 Bytes     "NO NAME     "   Volumn label
                                                                                                           Cluster is Software (OS) Term
          0x36       8 Bytes      "FAT16 "         System ID
                                                                                             11                                                                                                            12
Copyright James Grover, 2008                                                                                                                                                                                    2
     Embedded Systems Interfacing
             FAT1 Image                                                                              FAT2 Image
                                                                                                13                                                                                          14
             Root Directory Image                                                                    Bill of Rights Directory
                                                                                                      Offset   Size      Data         Description
       •VFAT entries                                                                                  0x00
                                                                                                      0x08
                                                                                                               8 Bytes
                                                                                                               3 Bytes
                                                                                                                         "BILLOF~1
                                                                                                                         "TXT
                                                                                                                                      8.3 Filename.
                                                                                                                                      8.3 File extension
       •Directory entries                                                                             0x0B     1 Byte    0x20         Attribute
                                                                                                      0x0C     1 Byte    0x00         Reserved
                                                                                                      0x0D     1 Byte    0x68         Creation time milliseconds
                                                                                                      0x0E     2 Bytes   0x4209       Creation time in hours, minutes and seconds
                                                                                                      0x10     2 Bytes   0x3574       Creation date in years since 1980, months, and day.
                                                                                                      0x12     2 Bytes   0x3574       Last accessed date
                                                                                                      0x14     2 Bytes   0x0000       EA-index
                                                                                                      0x16     2 Bytes   0x42EF       Last modified time
                                                                                                      0x18     2 Bytes   0x3571       Last modified date
                                                                                                      0x1A     2 Bytes   0x0002       First cluster
                                                                                                      0x1C     4 Bytes   0x00000F23   File size in bytes.
                                                                                                15                                                                                          16
                                                                                                               First (and only) Sector of
             Long File Reference (VFAT)                                                                               Bill of Rights
                   Offset   Size       Data         Description
                   0x00     1 Byte     01           Sequence number
                   0x01     10 Bytes   "Bill "      Name characters as five UTF-16 characters
                   0x0B     1 Byte     0x0F         Attribute
                   0x0C     1 Byte     0x00         Reserved
                   0x0D     1 Byte     0x2A         Checksum of DOS file name
                   0x0E     12 Bytes   "of Right"   Name characters as six UTF-16 characters
                   0x1A     2 Bytes                 First cluster which is always 0x0000
                                                                                                17                                                                                          18
Copyright James Grover, 2008                                                                                                                                                                     3
     Embedded Systems Interfacing
            File Allocation Table                                              FAT and Directory Interaction
               FAT12 used for floppy disk
                    Dreamed up by Bill Gates over a weekend
                    Very cryptic and atypical of other OS allocation
                     methods
                
                           FF 00 10          0FF 100
               FAT16 used for small hard drives and SD
                Cards
               FAT32 used for large hard drives
                                                                        19                                                          20
            Get Out Calculator                                                 Get Out Calculator
          Find Partition Table 0?                                            Find Root Directory?
                Sector 0, Offset 1BE                                                0x176 + Sector/FATBR = 0x00000001E0
          Find Boot Record?                                                  Data Area?
                0 + (1C6)DWORD = 0x000000ED                                         0x1E0 + Root EntriesBR*(Bytes/Entry)std/(Bytes/Sec)BR
                                                                                    = 0x0000000200
          Find FAT 1?
                0x000000ED + ReservedBR = 0x000000EE                         Cluster n
          Find FAT 2?                                                               0x200 + (n-2)FAT *Sector/ClusterBR
                0xEE + Sec/FATBR = 0x00000176                                       0x200 + (3-2)FAT *0x20 = 0x220
                                                                        21                                                          22
            Get Out Calculator                                                 Structure Review
          First Cluster of File?                                                  Template of memory
                                                                                  struct structTag {
                    0x200+(File EntryDirectory Entry-2)* Sector/ClusterBR              dataType variable name;
          Next Cluster of File?
                                                                                       ●●●
                                                                                       };
                    0x200 + (n)FAT* Sector/ClusterBR                              Example
                                                                                       struct myStructTag{
                                                                                                   int myNum;
                                                                                                   char myLetter[4];
                                                                                                   int * myPointer;
                                                                                                   }
                                                                        23                                                          24
Copyright James Grover, 2008                                                                                                                4
     Embedded Systems Interfacing
           Structure Review                                     Structure Review
                                                                    Access Array Element
              Declaration                                      
                                                                        ch=myStruct.myLetter[3];
                  struct structTag structureName;                       ch=myStructPtr->myLetter[3];
                                                                   Access Scalar
              Examples                                                 num=myStruct.myNum;
                                                                        num=myStructPtr->myNumber;
                  struct myStructTag myStruct =                    Access Pointer
                       {34512,’A’,’Y’,’8’,9,&Buffer[0]};                ptr=myStruct.myPointer;
                                                                        ptr=myStructPtr->myPointer;
                  struct myStructTag * myStructPtr;                Access Data Pointer To
                                                                        ch=*(myStruct.(myPointer+5));
                                                                        ch=*(myStruct->(myPointer+5));
                                                      25                                                                       26
           Data types                                           Partition Information
                                                           struct PARTITIONINFO {
                                                           BYTE bootid; /* bootable? 0=no, 128=yes */
                typedef unsigned char BYTE;
                                                           BYTE beghead; /* beginning head number */
                typedef unsigned int WORD;                 BYTE begsect; /* beginning sector number */
                typedef unsigned long DWORD;               BYTE begcyl; /* 10 bit nmbr, with high 2 bits put in begsect */
                                                           BYTE systid; /* Operating System type indicator code */
                enum BOOL {TRUE=0,FALSE=1};                BYTE endhead; /* ending head number */
                                                           BYTE endsect; /* ending sector number */
                                                           BYTE endcyl; /* also a 10 bit nmbr, with same high 2 bit trick */
                                                           DWORD relsect; /* first sector relative to start of disk */
                                                           DWORD numsect; /* number of sectors in partition */
                                                           };
                                                                      How much space allocated?
                                                      27                                                                       28
           Master Boot Record                                   Access MBR Data
           struct MBR{                                     struct MBR * p=(struct MBR *)&buffer[0];
           BYTE codes[446];
                                                           BYTE BOOTID,BEGHEAD,BEGSECT,SYSTID,ENDHEAD,ENDSECT;
           struct PARTITIONINFO partition[4];              WORD BEGCYL,ENDCYL,SIGNATURE;
           WORD mbrid;                                     DWORD RELSECT,NUMSECT;
           };
                                                           BOOTID=p->partition[0].bootid;
                                                           BEGHEAD=p->partition[0].beghead;
                                                           BEGSECT=(p->partition[0].begsect)&0x3F;
                How much space allocated?                  BEGCYL=(((p->partition[0].begsect)&0xC0)<<8)+p->partition[0].begcyl;
                                                           SYSTID=p->partition[0].systid;
                                                           ENDHEAD=p->partition[0].endhead;
                                                           ENDSECT=(p->partition[0].endsect)&0x3F;
                                                           ENDCYL=(((p->partition[0].endsect)&0xC0)<<8)+p->partition[0].endcyl;
                                                      29                                                                       30
Copyright James Grover, 2008                                                                                                        5
     Embedded Systems Interfacing
             Access MBR Data                                                MEDIA Structure
                                                                                 typedef unsigned long LBA;
         RELSECT=p->partition[0].relsect;
         NUMSECT=p->partition[0].numsect;                                        typedef struct {
         SIGNATURE=p->mbrid;                                                           LBA fat;
                                                                                       LBA root;
                                                                                       LBA data;
                                                                                       unsigned maxroot;
                                                                                       unsigned maxcls;
                                                                                       unsigned fatsize;
                                                                                       unsigned char fatcopy;
                                                                                       unsigned char sxc;
                                                                                       } MEDIA;
                                                                  31                                                              32
             MFILE Structure                                                MFILE Structure
        typedef struct {                                                 unsigned time;      // last update time
           MEDIA * mda;            // media structure pointer            unsigned date;      // last update date
           unsigned char * buffer; // sector buffer                      char   name[11];     // file name
           unsigned cluster;      // first cluster                       char   chk;      // checksum = ~( entry + name[0])
           unsigned ccls;       // current cluster in file               unsigned entry;     // entry position in cur directory
           unsigned sec;         // sector in current cluster            char   mode;       // mode 'r', 'w'
           unsigned pos;         // position in current sector           } MFILE;
           unsigned top;         // number of data bytes in the buffer
           long    seek;       // position in the file
           long    size;      // file size
                                                                  33                                                              34
             Directory Entries                                              Directory Entries
                Filename @ 0x00 for 8 bytes                                   Reserved @ 0x0C for 1 byte
                     0x00 Unused
                 
                                                                               Time Created @ 0x0D for 3 bytes
                    0xE5 erased entry
                    0x2E Dot entry (. or ..)                                  Dated Created @ 0x10 for 2 bytes
                File extension @ 0x08 for 3 bytes, padded                     Date Last Accessed @ 0x12 for 2 bytes
                 with spaces                                                   Extended Attribute @ 0x14 for 2 bytes
                 File Attribute @ 0x0B for 1 byte
                                                                                Last Modify Time @ 0x16 for 2 bytes
             
                                                                            
                    VFAT has read only, hidden, system and volumn
                     label or 0x0F attribute                                   Last Modify Date @ 0x18 for 2 bytes
                                                                  35                                                              36
Copyright James Grover, 2008                                                                                                           6
     Embedded Systems Interfacing
           Directory Entries                                FAT Tread
              First Cluster in FAT-16 @ 0x1B for 2            Get current cluster, ccls
               bytes                                           Determine FAT sector p=ccls>>8
              File Size in bytes @ 0x1C for 4 bytes           Check if cached
                  Should be 0 for Volume label or             Else read FAT sector
                   subdirectory
                                                               Get LBA of next FAT entry
              Long File Name overlays directory entry
                  See web
                                                       37                                                      38
           File I/O Support                                 File I/O Support
              Fileio.h contains:                                  Function Prototypes
                                                                         readDIR
                   Error Codes
                                                                     
               
                                                                        writeDIR
                  Media Structure                                      newDIR
                  Mfile Structure                                      mount
                  File Attributes                                      unmount
                                                                        fopenM
                  Function Prototypes                                  freadM
                       nextFAT                                         fwriteM
                       newFAT                                          fcloseM
                                                       39                                                      40
           File I/O Support                                 Homework
              Fileio.c contains:                              Chapter 14 Handout
                  Offset values for items in:                     Exercise 1: LBA of Boot Record, FAT1,
                        Master Boot Record
                    
                                                                    FAT2, Root Directory, Data Area.
                       Partition Table
                       Directroy Entries                          Exercise 2: Given Directory and FAT find
                  Global Vasiables                                 LBA of file sectors.
                       FERROR                                     Exercise 3: Write structure for decoding
                        Media Structure Instance
                    
                                                                    directory entry
                  Code for Prototypes in fileio.h
                  Code for helper functions
                                                       41                                                      43
Copyright James Grover, 2008                                                                                        7