fd0(tutorial) fd0(tutorial) ffdd00 wwiitthh GGNNUU//LLiinnuuxx The tutorial explains in detail about what happens when filesystem utilites are used with /dev/fd0. It also explains briefly about the msdos filesystem with GNU/Linux when used with the 3-1/2 inch floppy disk. AAuutthhoorr Written by Shakthi Kannan . DDaattee October 2006. SStteepp 11:: FFoorrmmaattttiinngg //ddeevv//ffdd00 fdformat /dev/fd0 0000000 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 * 0168000 fdformat dumps "f6" to /dev/fd0 as done by format_disk() in util- linux/disk-utils/fdformat.c (util-linux package). f6 is defined as FD_FILL_BYTE in include/linux/fs.h. It is used by setup_format_params() in drivers/block/floppy.c. The floppy disk characteristics: Size: 3-1/2 inch. High density. Two sides. Each side has 1440 sectors. So, totally 2880 sectors. 512 bytes/sector. Total bytes = 2880 sectors x 512 bytes/sector = 1474560 bytes = 1440 KB = 1.44 MB. Two copies of File Allocation Table (FAT). One reserved sector (logical sector 0 or bootstrap sector). One sector per cluster. 224 entries in root directory. 9 sectors per File Allocation Table. So, each FAT has 9 sectors * 512 bytes/sector = 4608 bytes or 0x1200 bytes. 18 sectors per track. Number of tracks = 2880 sectors/track / 18 sectors/track = 160 tracks. 80 tracks/side. Zero hidden sectors. SStteepp 22:: mmkkffss..mmssddooss mkfs.msdos /dev/fd0 0000000 3ceb 6d90 646b 736f 7366 0000 0102 0001 0000010 e002 4000 f00b 0009 0012 0002 0000 0000 0000020 0000 0000 0000 6529 192d 2045 2020 2020 0000030 2020 2020 2020 4146 3154 2032 2020 1f0e 0000040 5bbe ac7c c022 0b74 b456 bb0e 0007 10cd 0000050 eb5e 32f0 cde4 cd16 eb19 54fe 6968 2073 0000060 7369 6e20 746f 6120 6220 6f6f 6174 6c62 0000070 2065 6964 6b73 202e 5020 656c 7361 2065 0000080 6e69 6573 7472 6120 6220 6f6f 6174 6c62 0000090 2065 6c66 706f 7970 6120 646e 0a0d 7270 00000a0 7365 2073 6e61 2079 656b 2079 6f74 7420 00000b0 7972 6120 6167 6e69 2e20 2e2e 0d20 000a 00000c0 0000 0000 0000 0000 0000 0000 0000 0000 * 00001f0 0000 0000 0000 0000 0000 0000 0000 aa55 0000200 fff0 00ff 0000 0000 0000 0000 0000 0000 0000210 0000 0000 0000 0000 0000 0000 0000 0000 * 0001400 fff0 00ff 0000 0000 0000 0000 0000 0000 0001410 0000 0000 0000 0000 0000 0000 0000 0000 * 0004200 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 * 0168000 The first reserved sector is logical sector 0 or the bootstrap sector of size 512 bytes (0x200). [Byte 1 0 3 2 5 4 7 6 9 8 1110 1312 1514] 0000000 3ceb 6d90 646b 736f 7366 0000 0102 0001 90_3ceb (0-2 bytes) - first instruction of bootstrap routine. On i86 it is "JMP 003e NOP"). 0073_6673_6f64_6b6d (3-10 bytes) - OEM name ( sfsodkm) (strcpy (bs.sys- tem_id, "mkdosfs") in setup_tables() in dosfstools/mkdosfs/mkdosfs.c 0200 (11-12 bytes) - number of bytes/sector (512) 01 (13th byte) - number of sectors/cluster (1) 0001 (14-15 bytes) - number of reserved sectors (1) [Byte 1716 1918 2120 2322 2524 2726 2928 3130] 0000010 e002 4000 f00b 0009 0012 0002 0000 0000 02 (16th byte) - number of copies of FAT (2) 00e0 (17-18 bytes) - number of entries in root directory (224) 0b40 (19-20 bytes) - total number of sectors (2880) f0 (21st byte) - media descriptor type (3-1/2 inch) 0009 (22-23 bytes) - number of sectors in each copy of FAT (9) 0012 (24-25 bytes) - number of sectors/track (18) 0002 (26-27 bytes) - number of sides (2) 0000_0000 (28-31 bytes) - number of hidden sectors (0) [Byte 3332 3534 3736 3938 4140 4342 4544 4746] 0000020 0000 0000 0000 6529 192d 2045 2020 2020 0000_0000 (32-35) - total number of sectors The following are part of struct msdos_volume_info in dosfs- tools/mkdosfs/mkdosfs.c: 00 (36th byte) - drive number 00 (37th byte) - RESERVED 29 (38th byte) - ext_boot_sign (0x29 if fields below exist) 65 (39th byte) - volume_id[0] 2d (40th byte) - volume_id[1] 19 (41th byte) - volume_id[2] 45 (42th byte) - volume_id[3] volume_id is created initially with time in main() in mkdosfs.c and hence changes on every run of mkdosfs. [Byte 4948 5150 5352 5554 5756 5958 6160 6362] 0000030 2020 2020 2020 4146 3154 2032 2020 20_2020_2020_2020_2020_2020 (43-53 bytes) - Volume label (blank space) 4641_5431_3220_2020 (54-61) - fs_type ("FAT12 ") fs_type is set to MSDOS_FAT12_SIGN ("FAT12 ") after size_fat is set to 12 in setup_tables() in mkdosfs.c. [Byte 6362] 0000030 1f0e [Byte 6564 6766 6968 7170 7372 7574 7776 7978] 0000040 5bbe ac7c c022 0b74 b456 bb0e 0007 10cd [Byte 1 0 3 2 5 4 7 6 9 8 1110 1312 1514] 0000050 eb5e 32f0 cde4 cd16 eb19 54fe 6968 2073 eb5e 32f0 cde4 cd16 eb19 Tfe i h s [Byte 1 0 3 2 5 4 7 6 9 8 1110 1312 1514] 0000060 7369 6e20 746f 6120 6220 6f6f 6174 6c62 s i n t o a b o o a t l b [Byte 1 0 3 2 5 4 7 6 9 8 1110 1312 1514] 0000070 2065 6964 6b73 202e 5020 656c 7361 2065 e i d k s . p e l s a e [Byte 1 0 3 2 5 4 7 6 9 8 1110 1312 1514] 0000080 6e69 6573 7472 6120 6220 6f6f 6174 6c62 n i e s t r a b o o a t l b [Byte 1 0 3 2 5 4 7 6 9 8 1110 1312 1514] 0000090 2065 6c66 706f 7970 6120 646e 0a0d 7270 e l f p o y p a d n \n\r r p [Byte 1 0 3 2 5 4 7 6 9 8 1110 1312 1514] 00000a0 7365 2073 6e61 2079 656b 2079 6f74 7420 s e s n a y e k y o t t [Byte 1 0 3 2 5 4 7 6 9 8 1110 1312 1514] 00000b0 7972 6120 6167 6e69 2e20 2e2e 0d20 000a y r a a g n i . . . \r \n 00000c0 0000 0000 0000 0000 0000 0000 0000 0000 * 00001f0 0000 0000 0000 0000 0000 0000 0000 aa55 Filled by dummy_boot_code[BOOTCODE_SIZE] from dosfs- tools/mkdosfs/mkdosfs.c. BOOTCODE_SIZE is defined as 448 bytes and ends at 0000_01fd. char dummy_boot_code[BOOTCODE_SIZE] = "\x0e" /* push cs */ "\x1f" /* pop ds */ "\xbe\x5b\x7c" /* mov si, offset message_txt */ /* write_msg: */ "\xac" /* lodsb */ "\x22\xc0" /* and al, al */ "\x74\x0b" /* jz key_press */ "\x56" /* push si */ "\xb4\x0e" /* mov ah, 0eh */ "\xbb\x07\x00" /* mov bx, 0007h */ "\xcd\x10" /* int 10h */ "\x5e" /* pop si */ "\xeb\xf0" /* jmp write_msg */ /* key_press: */ "\x32\xe4" /* xor ah, ah */ "\xcd\x16" /* int 16h */ "\xcd\x19" /* int 19h */ "\xeb\xfe" /* foo: jmp foo */ /* message_txt: */ "This is not a bootable disk. Please insert a bootable floppy and\r\n" "press any key to try again ... \r\n"; 00000c0 0000 0000 0000 0000 0000 0000 0000 0000 * 00001f0 0000 0000 0000 0000 0000 0000 0000 aa55 510th byte is hexadecimal 55. 511th byte is hexadecimal aa. The general structure of the disk is: ______________ | Boot sector| (512 bytes) | FAT #1 | (4608 bytes) (0200-1400) | FAT #2 | (4608 bytes) (1400-2600) | Data | (cluster #2 onwards ...) -------------- 0000200 fff0 00ff 0000 0000 0000 0000 0000 0000 0000210 0000 0000 0000 0000 0000 0000 0000 0000 * 0001400 fff0 00ff 0000 0000 0000 0000 0000 0000 0001410 0000 0000 0000 0000 0000 0000 0000 0000 * 0004200 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 f0 in 0x0000_0200 is the first byte and contains the media descriptor type value. It is added in setup_tables(). ffff is the endmarker cluster value set in setup_tables() by mark_FAT_cluster(). Data consists of root_directories and file contents. root_directories are calculated using: root_directories = root_directory_entries * 32 / bytes_per_sector Hence, there are 14 sectors for root_directories and 19 sectors for boot sector, FAT #1, and FAT #2. So, totally 2880-33=2847 sectors are available for data. SStteepp 33:: MMoouunnttiinngg //ddeevv//ffdd00 mount -t msdos /dev/fd0 /mnt/floppy 0000000 3ceb 6d90 646b 736f 7366 0000 0102 0001 0000010 e002 4000 f00b 0009 0012 0002 0000 0000 0000020 0000 0000 0000 6529 192d 2045 2020 2020 0000030 2020 2020 2020 4146 3154 2032 2020 1f0e 0000040 5bbe ac7c c022 0b74 b456 bb0e 0007 10cd 0000050 eb5e 32f0 cde4 cd16 eb19 54fe 6968 2073 0000060 7369 6e20 746f 6120 6220 6f6f 6174 6c62 0000070 2065 6964 6b73 202e 5020 656c 7361 2065 0000080 6e69 6573 7472 6120 6220 6f6f 6174 6c62 0000090 2065 6c66 706f 7970 6120 646e 0a0d 7270 00000a0 7365 2073 6e61 2079 656b 2079 6f74 7420 00000b0 7972 6120 6167 6e69 2e20 2e2e 0d20 000a 00000c0 0000 0000 0000 0000 0000 0000 0000 0000 * 00001f0 0000 0000 0000 0000 0000 0000 0000 aa55 0000200 fff0 00ff 0000 0000 0000 0000 0000 0000 0000210 0000 0000 0000 0000 0000 0000 0000 0000 * 0001400 fff0 00ff 0000 0000 0000 0000 0000 0000 0001410 0000 0000 0000 0000 0000 0000 0000 0000 * 0004200 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 * 0168000 There is no effect on the disk when mounted. Only the kernel data structures are updated. SStteepp 44:: CCrreeaattiinngg aa ffiillee aabbccddeeffgghh touch /mnt/floppy/abcdefgh 0000000 3ceb 6d90 646b 736f 7366 0000 0102 0001 0000010 e002 4000 f00b 0009 0012 0002 0000 0000 0000020 0000 0000 0000 6529 192d 2045 2020 2020 0000030 2020 2020 2020 4146 3154 2032 2020 1f0e 0000040 5bbe ac7c c022 0b74 b456 bb0e 0007 10cd 0000050 eb5e 32f0 cde4 cd16 eb19 54fe 6968 2073 0000060 7369 6e20 746f 6120 6220 6f6f 6174 6c62 0000070 2065 6964 6b73 202e 5020 656c 7361 2065 0000080 6e69 6573 7472 6120 6220 6f6f 6174 6c62 0000090 2065 6c66 706f 7970 6120 646e 0a0d 7270 00000a0 7365 2073 6e61 2079 656b 2079 6f74 7420 00000b0 7972 6120 6167 6e69 2e20 2e2e 0d20 000a 00000c0 0000 0000 0000 0000 0000 0000 0000 0000 * 00001f0 0000 0000 0000 0000 0000 0000 0000 aa55 0000200 fff0 00ff 0000 0000 0000 0000 0000 0000 0000210 0000 0000 0000 0000 0000 0000 0000 0000 * 0001400 fff0 00ff 0000 0000 0000 0000 0000 0000 0001410 0000 0000 0000 0000 0000 0000 0000 0000 * 0002600 4241 4443 4645 4847 2020 2020 0000 0000 0002610 0000 0000 0000 9a06 353a 0000 0000 0000 0002620 0000 0000 0000 0000 0000 0000 0000 0000 * 0004200 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 * 0168000 The directory structure is given as follows: Byte(s) Contents 0-7 file name or first 8 characters of volume name 8-10 file extension or last 3 characters of volume name 11 attribute byte 12-21 unused 22-23 time 24-25 date 26-27 number of first cluster 28-31 number of bytes in file, or zero for subdirectory/volume label The attribute byte bit definitions are as follows: Bit Meaning 7 unused 6 unused 5 file has been changed since last backup 4 is a directory 3 is a volume label 2 system file 1 hidden file 0 read-only [Byte 1 0 3 2 5 4 7 6 9 8 1110 1312 1514] 0002600 4241 4443 4645 4847 2020 2020 0000 0000 B A D C F E H G 20 abcdefgh is the filename without any extension. Bit 5 has been set in attribute byte - the file has been changed. [Byte 1716 1918 2120 2322 2524 2726 2928 3130] 0002610 0000 0000 0000 9a06 353a 0000 0000 0000 The time bytes (22-23 bytes, 5/6/5 bit split-up) have the following meaning: Bits Contents 15-11 hour (0-23) 10-5 minute (0-59) 4-0 double seconds (0-29) The abcdefgh file was created at 19 hours, 16 minutes, and 6 seconds (10011_010000_00110). The date bytes (24-25 bytes, 7/4/5 bit split-up) have the following meaning: Bits Contents 15-9 years elapsed since 1980 (0-127) 8-5 month (1=January, 2=February, ...) 4-0 day (1-31) The abcdefgh file was created on 26, September, 26 years after 1980 = 2006 (00110101_1001_11010). SStteepp 55:: WWrriittiinngg tthhee wwoorrdd eecchhoo ttoo //mmnntt//ffllooppppyy//aabbccddeeffgghh echo "hello" > /mnt/floppy/abcdefgh 0000000 3ceb 6d90 646b 736f 7366 0000 0102 0001 0000010 e002 4000 f00b 0009 0012 0002 0000 0000 0000020 0000 0000 0000 6529 192d 2045 2020 2020 0000030 2020 2020 2020 4146 3154 2032 2020 1f0e 0000040 5bbe ac7c c022 0b74 b456 bb0e 0007 10cd 0000050 eb5e 32f0 cde4 cd16 eb19 54fe 6968 2073 0000060 7369 6e20 746f 6120 6220 6f6f 6174 6c62 0000070 2065 6964 6b73 202e 5020 656c 7361 2065 0000080 6e69 6573 7472 6120 6220 6f6f 6174 6c62 0000090 2065 6c66 706f 7970 6120 646e 0a0d 7270 00000a0 7365 2073 6e61 2079 656b 2079 6f74 7420 00000b0 7972 6120 6167 6e69 2e20 2e2e 0d20 000a 00000c0 0000 0000 0000 0000 0000 0000 0000 0000 * 00001f0 0000 0000 0000 0000 0000 0000 0000 aa55 0000200 fff0 ffff 000f 0000 0000 0000 0000 0000 0000210 0000 0000 0000 0000 0000 0000 0000 0000 * 0001400 fff0 ffff 000f 0000 0000 0000 0000 0000 0001410 0000 0000 0000 0000 0000 0000 0000 0000 * 0002600 4241 4443 4645 4847 2020 2020 0000 0000 0002610 0000 0000 0000 9a06 353a 0000 0000 0000 0002620 0000 0000 0000 0000 0000 0000 0000 0000 * 0004200 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 * 0168000 The data is cached and not written to disk until unmount. SStteepp 66:: UUnnmmoouunnttiinngg tthhee ffllooppppyy umount /mnt/floppy 0000000 3ceb 6d90 646b 736f 7366 0000 0102 0001 0000010 e002 4000 f00b 0009 0012 0002 0000 0000 0000020 0000 0000 0000 6529 192d 2045 2020 2020 0000030 2020 2020 2020 4146 3154 2032 2020 1f0e 0000040 5bbe ac7c c022 0b74 b456 bb0e 0007 10cd 0000050 eb5e 32f0 cde4 cd16 eb19 54fe 6968 2073 0000060 7369 6e20 746f 6120 6220 6f6f 6174 6c62 0000070 2065 6964 6b73 202e 5020 656c 7361 2065 0000080 6e69 6573 7472 6120 6220 6f6f 6174 6c62 0000090 2065 6c66 706f 7970 6120 646e 0a0d 7270 00000a0 7365 2073 6e61 2079 656b 2079 6f74 7420 00000b0 7972 6120 6167 6e69 2e20 2e2e 0d20 000a 00000c0 0000 0000 0000 0000 0000 0000 0000 0000 * 00001f0 0000 0000 0000 0000 0000 0000 0000 aa55 0000200 fff0 ffff 000f 0000 0000 0000 0000 0000 0000210 0000 0000 0000 0000 0000 0000 0000 0000 * 0001400 fff0 ffff 000f 0000 0000 0000 0000 0000 0001410 0000 0000 0000 0000 0000 0000 0000 0000 * 0002600 4241 4443 4645 4847 2020 2020 0000 0000 0002610 0000 0000 0000 9a45 353a 0002 0006 0000 0002620 0000 0000 0000 0000 0000 0000 0000 0000 * 0004200 6568 6c6c 0a6f 0000 0000 0000 0000 0000 0004210 0000 0000 0000 0000 0000 0000 0000 0000 * 0004400 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 f6f6 * 0168000 The time when the update occurred has been set at 19 hours, 18 minutes, and 5 seconds (10011_010010_00101). 0004200 6568 6c6c 0a6f 0000 0000 0000 0000 0000 e h l l \n o Data is stored at 0000_4200. RReeffeerreenncceess [1] Philip J. Erdelsky. 1993. A description of the DOS file system. http://alumnus.caltech.edu/~pje/dosfiles.html [2] Andries Brouwer. 2002. The FAT filesystem. http://www.win.tue.nl/~aeb/linux/fs/fat/fat-1.html [3] dosfstools. ftp://ftp.uni-erlangen.de/pub/Linux/LOCAL/dosfstools. [4] util-linux. http://freshmeat.net/projects/util-linux. [5] Thomas Kjoernes. May 11, 2000. File Allocation Table. http://home.no.net/tkos/info/fat.html fd0(tutorial)