FAT32 - creating a text file

Hello everyone!

Recently I try to save some data on a SD card using my microcontroller.
In order to understand how FAT32 works I use 512 MB SD card and DMDE program and of course s simply txt file.

First of all I formatted my SD card with 512B allocation unit size FAT32 by Windows 7.
Secondly I pasted there a simply txt file as follows:
name: "file0001.txt"
content: "file0001"
so the size of this file is 8 bytes (it is clear - 8 characters).

I opened it in the DMDE program and I found all proper data as follows:

Boot Sector (block 0):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Jump+NOP Instruction

MSDOS5.0 - OEM Name
512 - Bytes per Sector
1 - Sector per Cluster
1006 - Reserved Sectors
2 - Copies of FAT
0xF8 - Media Descriptor
63 - Sectors per Track
255 - Heads
0 - Hidden Sectors on Partition
1000448 - Sectors of Partition
7689 - Sectors of FAT
0 - Flags
0 - Version of FAT32 Driver
2 - Cluster Number of Start Root Directory
1 - Sector Number of File Information System Sector
6 - Sector Number of Backup Boot Sector
128 - Logical Driver Number of Partition
41 - Extended Signature
2858294644 - Serial Number of Partition
NO NAME - Volume Name of Partition
FAT32 - FAT Name
Executable code rest 420 bytes
0x55 0xAA - Boot Sector Signature

File Information System Sector (block 1):

1
2
3
4
5
6
7
0x52 0x52 0x61 0x41 - First Signature
00 00 00 ... 00 - zeros
0x72 0x72 0x41 0x61 - Signature of FSI Sector
FE 03 0F 00 - Number of Free Clusters
04 00 00 00 - Number of First Free Cluster
00 00 00 ... 00 - zeros
0x55 0xAA - Boot Sector Signature


Boot Sector copy (block 6):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
MSDOS5.0 - OEM Name
512 - Bytes per Sector
1 - Sector per Cluster
1006 - Reserved Sectors
2 - Copies of FAT
0xF8 - Media Descriptor
63 - Sectors per Track
255 - Heads
0 - Hidden Sectors on Partition
1000448 - Sectors of Partition
7689 - Sectors of FAT
0 - Flags
0 - Version of FAT32 Driver
2 - Cluster Number of Start Root Directory
1 - Sector Number of File Information System Sector
6 - Sector Number of Backup Boot Sector
128 - Logical Driver Number of Partition
41 - Extended Signature
2858294644 - Serial Number of Partition
NO NAME - Volume Name of Partition
FAT32 - FAT Name
Executable code rest 420 bytes
0x55 0xAA - Boot Sector Signature


File Information System Sector copy (block 7):

1
2
3
4
5
6
7
0x52 0x52 0x61 0x41 - First Signature
00 00 00 ... 00 - zeros
0x72 0x72 0x41 0x61 - Signature of FSI Sector
FE 03 0F 00 - Number of Free Clusters
04 00 00 00 - Number of First Free Cluster
00 00 00 ... 00 - zeros
0x55 0xAA - Boot Sector Signature


FAT (block 1006):

1
2
3
4
F8 FF FF FF  FF FF FF 0F  FF FF FF F0  FF FF FF F0 - F8 FF FF FF: a Media Descriptor, FF FF FF 0F - End Of cluster Chain mark
00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  
00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  
...


FAT copy (block 8695):
1
2
3
4
F8 FF FF FF  FF FF FF FF  FF FF FF F0  FF FF FF F0 - F8 FF FF FF: a Media Descriptor, FF FF FF 0F - End Of cluster Chain mark
00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  
00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  
...


Root Directory entry (block 16384):

1
2
3
4
5
54 45 53 54  5F 4C 41 42  45 4C 33 08  00 00 00 00 - TEST_LABEL3 (rest nothing special)
00 00 00 00  00 00 DA 7B  41 40 00 00  00 00 00 00
46 49 4C 45  30 30 30 31  54 58 54 20  18 49 EC 7B - FILE0001TXT (file attributes, dates and times)
41 40 41 40  00 00 E3 7B  41 40 03 00  08 00 00 00 - Start Cluster Number - 3, Size of File - 8 bytes
00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  


Data region (block 16385):
1
2
3
66 69 6C 65  30 30 30 31  00 00 00 00  00 00 00 00 - FILE0001
00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00
...


Then I try to manually add (create) a new txt file as follows:
name: "file0002.txt"
content: "file0002"
so the size of this file is 8 bytes (it is clear - 8 characters).

So I made some changes as follows:

File Information System Sector (block 1):
1
2
3
4
5
6
7
0x52 0x52 0x61 0x41 - First Signature
00 00 00 ... 00 - zeros
0x72 0x72 0x41 0x61 - Signature of FSI Sector
FD 03 0F 00 - Number of Free Clusters	// was FE 03 0F 00
05 00 00 00 - Number of First Free Cluster	// was 04 00 00 00
00 00 00 ... 00 - zeros
0x55 0xAA - Boot Sector Signature


FAT (block 1006):

1
2
3
4
F8 FF FF FF  FF FF FF 0F  FF FF FF F0  FF FF FF F0 - F8 FF FF FF: a Media Descriptor, FF FF FF 0F - End Of cluster Chain mark
FF FF FF F0  00 00 00 00  00 00 00 00  00 00 00 00 - FF FF FF 0F - End Of cluster Chain mark 
00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  
...


FAT copy (block 8695):
1
2
3
4
F8 FF FF FF  FF FF FF FF  FF FF FF F0  FF FF FF F0 - F8 FF FF FF: a Media Descriptor, FF FF FF 0F - End Of cluster Chain mark
FF FF FF F0  00 00 00 00  00 00 00 00  00 00 00 00 - FF FF FF 0F - End Of cluster Chain mark 
00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  
...


Root Directory entry (block 16384):

1
2
3
4
5
6
54 45 53 54  5F 4C 41 42  45 4C 33 08  00 00 00 00 - TEST_LABEL3 (rest nothing special)
00 00 00 00  00 00 DA 7B  41 40 00 00  00 00 00 00
46 49 4C 45  30 30 30 31  54 58 54 20  18 49 EC 7B - FILE0001TXT (file attributes, dates and times)
41 40 41 40  00 00 E3 7B  41 40 03 00  08 00 00 00 - Start Cluster Number - 3, Size of File - 8 bytes
46 49 4C 45  30 30 30 32  54 58 54 20  18 49 EC 7B - FILE0001TXT (file attributes, dates and times)
41 40 41 40  00 00 E3 7B  41 40 04 00  08 00 00 00 - Start Cluster Number - 4, Size of File - 8 bytes


Data region (block 16385):
1
2
3
66 69 6C 65  30 30 30 31  00 00 00 00  00 00 00 00 - FILE0001
00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00
...


Data region (block 16386):
1
2
3
66 69 6C 65  30 30 30 32  00 00 00 00  00 00 00 00 - FILE0002
00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00
...


I saved all changes on the SD card and I opened SD in Windows Explorer. The "file0002.txt" does exist but when I double-

click on it to open the file and check the content there occured an error as follows:

"The file or directory is corrupted and unreadable."

and the file is empty.

What I did wrong that there is no effect on the SD card with FAT32? Do I forget about something in the FAT32 changes?

Best regards,
Kuba
chkdsk might tell you what's wrong.

I can't comment on what you've done because I can't quite follow it. For example, I can't see how you found the root block, data region ... I could probably work it out from the spec, but not from what you've shown.
Topic archived. No new replies allowed.