Lab 9: File Systems
Mar. 7/8, 2007
[153 Home]     [Format]     [Sector 0]     [Dir Entry]     [Byte Offset]     [Implementation]     [Resources]

The MS-DOS Disk Format



The Boot Sector

Byte Offset Length Contents Typical Value (FAT12)
0x00 3 Jump instruction to boot code eb ?? 90
0x03 8 What system formated the disk mkdosfs
0x0b 2 Number of bytes per sector 0x0200 = 512
0x0d 1 Number of sectors per cluster 0x01
0x0e 2 Number of reserved sectors 0x0001
0x10 1 Number of FATs 0x02
0x11 2 Number of root directory entries 0x00e0 = 224
0x13 2 Number of sectors 0x0b40 = 2880
0x15 1 Medium descriptor 0xf0 (removable media)
0x16 2 Number of sectors per FAT 0x0009
0x18 2 Number of sectors per track 0x0012 = 18
0x1a 2 Number of heads 0x0002
0x1c 4 Number of hidden sectors 0x00000000
0x1fe 2 Boot signature 0xaa55


The Directory Entry (32 bytes)

Byte Offset Length Contents
0x00 8 Filename
0x08 3 Extension
0x0b 1 Attribute
0x0c 2 Reserved
0x0e 2 Time file was created
0x10 2 Date file was created
0x12 2 Last access date
0x14 2 Reserved
0x16 2 Time of last write
0x18 2 Date of last write
0x1a 2 Starting cluster number
0x1c 4 File size in bytes

  • First Byte of the Filename
    Value Description
    0x00 Unused entry
    0xe5 File/Dir has been deleted
    0x2e current dir (.), or parent dir (..)
    if the second byte is also 2e
    0x41 The next 32-byte entry is a valid entry
    (the current entry starting with 0x41 is invalid)

  • Directory Entry Attributes
    Mask Attribute
    0x10 Directory
    0x20 Archive


Convert Clusters to Byte Offset
  • byte offset = (starting cluster number + 31) * bytesPerSector
  • Example: Calculate the starting position of /dir1/file1
    1. Search from the first root directory entry @ 0x2600 (sector #19)
    2. The first 32-byte entry @ 0x2600 starts with 0x41, so it is not a valid entry, but the next 32-byte entry @ 0x2620 is a valid entry
    3. The first 8 bytes starting @ 0x2620 is the directory name (DIR1), while the 2 bytes starting at relative position @ 0x1A is the starting cluster number of DIR1, which is 0x0002
    4. Calculate the starting position of DIR1: (2 + 31) * 512 = 0x4200, i.e., DIR1 starts @ 0x4200
    5. The first 32-byte entry @ 0x4200 starts with 0x2e, so it points to the current directory, the 2 bytes starting at relative position @ 0x1A should be the same as 0x0002
    6. The next 32-byte entry @ 0x4220 starts with 0x2e2e, so it points to the parent directory, the 2 bytes starting at relative position @ 0x1A should be the same as the starting cluster number of its parent (in this example, parent is root, so it is 0)
    7. The next three 32-byte entries starts with 0xe5, so they are invalid entries, we can simply skip them
    8. The next 32-byte entry @ 0x42a0 starts with 0x41, so it is not a valid entry, but the next 32-byte entry @ 0x42c0 is a valid entry
    9. The first 8 bytes starting @ 0x42c0 is the file name(file1), the next 3 bytes is the extension (txt), while the 2 bytes starting at relative position @ 0x1A is the starting cluster number of file1.txt, which is 0x0015
    10. Calculate the starting position of file1.txt: (15 + 31) * 512 = 0x6800, i.e., file1.txt starts @ 0x6800

Implementation

Command Implementation
load floppy.img Read the boot sector to get bytesPerSector
ls while (the first byte of a 32-byte directory entry is not 0)
{ Read all valid directory entries of the current directory and print their names; }
cd dir1 push the starting position of the current directory to the stack;
find the directory entry of dir1 and make dir1 the current directory;
rm file1.txt find the directory entry for the file/dir to be deleted;
set the first byte to be 0xe5;


Resources
  1. Lab manual: p.179-204
  2. KDE hex editor: /usr/bin/khexedit
  3. FAT32 File System Specification
  4. A Description of the DOS File System