Debug Notes:
First step: Read the physical sector 0.
The four bytes starting from offset 0X1CA (458) are: 0x7,0x5b,0x1e,0x00;
And the number of sectors is: 0x1e5b07=. Each sector is 512 bytes. So the SD card capacity is: *512/1024/1024mbyte
The 4 bytes starting from offset 0x1c6=454 are: 0xf9,0x00,0x00,0x00, which indicates that the boot sector is in the 0xf9=249 sector. Get the SD card more detailed information, 0 sectors will not be the tube.
/*
unsigned long range=0; You can find the BPB area in two ways.
while (Range<find_bpb_up_range)
{ How to search for a string in a sector
Fat32_readsector (Range,fat32_buffer); It is better to have a BPB sector number in the 454 position with a read from the physical 0 sector
if (Fat32_buffer[0]==0xeb && fat32_buffer[2]==0x90)
Break
range++;
}
Return range;*/
Step Two:
To enter the boot sector:
Front 0x03 to 0xA content should be initialized for the machine, my card is not shown above, ignore
The two bytes starting at 0xB are 0x00 0x02 and represent 0x0200=512 bytes. 512 bytes per sector
The next 0xD position is 0x20, representing a cluster with 32 sectors
The next two positions in 0xE are: 0x01 0x00 stands for 1 (0x0001) reserved sectors, that is, each fat table is in the same sector as the boot sector (97) +1=98
The next 0x02 represents a two fat table with a position number of 16 and an actual offset of 0--16
The next 00 02 and 0x0200=512. Represents FDT (Directory area) has 512 registrations
Next 0x16 (22) begins with two bytes: F3 00 means that 0xf3=243 represents 243 sectors per fat table
0x36 to 0x3d represented by: 0x46 0x41 0x54 0x31 0x36 0x20 0x20 0x20 1G card for FAT16 format
Change to 2G card: (FAT32)
The four bytes starting from offset 0X1CA (458) are: 0x77,0x9f,0x3a,0x00;
And the number of sectors is: 0x3a9f77=. Each sector is 512 bytes. So the SD card capacity is: *512/1024/1024mbyte
The 4 bytes starting from offset ox1c6=454 are: 0x89,0x00,0x00,0x00, which indicates that the boot sector is in the 0x89=137 sector. Get the SD card more detailed information, 0 sectors will not be the tube.
Step Two:
To enter the boot sector:
Front 0x03 to 0xA content should be initialized for the machine, 2G card shown above as MSDOS5.0.
0xB (11, the number above my background debugging interface) starts with two bytes for 0x00 0x02 for 0x0200=512 bytes. 512 bytes per sector
The next 0xD position is 0x08, representing a cluster with 8 sectors
The next two positions in 0xE are: 0x26 0x00 stands for 38 reserved sectors, that is, each fat table is in the same sector as the boot sector (97) +38
The next 0x02 represents a two fat table with a position number of 16 and an actual offset of 0--16
The next 00 00 and 0x0200=512. Represents FDT (Directory area) has 512 registrations
Next 0x16 (22) begins with two bytes: 00 00 i.e. 0XF3=243 represents 243 sectors per fat table
0x52 to 0x58 represented by: 0x46 0x41 0x54 0x33 0x32 0x20 0x20 0x20 2G card for FAT32 format
(SD card BPB in small-end mode, when reading a continuous number of bytes, you need to handle it yourself)
2GSDcard Parameters:
BPB Num 137 BPB is in the same number
Total_size 1875M Total Capacity
Firstdircluster 2: Root cluster number
Firstdatasector 7665 First Data sector
bytespersector:512 bytes Number of bytes per sector
Fatsectors 3745 Fat Table occupies the number of sectors, this must be fixed, can be calculated by the total capacity and number of clusters
Sectorspercluster:8 number of sectors per cluster
Firstfatsector 175 The first Fat table sector area code, is not fixed.
Firstdirsector 7665 First Directory sector
Rootdirsectors 0 The number of sectors occupied by the root directory, in FAT32, because the root zone is documented, that is, in the data area, so there is no fixed root zone consumption sector number, all 0
Rootdircount 0 root entries, fixed to 0
Follow up to 175 sectors to see the data inside the first fat:
F8 FF FF 0F FF FF FF FF-FF FF 0F FF FF 0F (top 8 for header tag)
1e XX FF FF FF 0f FF FF FF 0F FF FF 0F
FF FF FF 0F FF FF FF 0F FF FF 0F
(There will be a valid number in some of the following locations, first analyze the data I read earlier)
register int ntmp;
Continue: Read the root area of the file:
4f 4e (44) (CC)
F O U N D 0 0 0
D9 3e d9 3e d9 3e xx xx CC D9
/* Clock hopping condition, 24m->8m unstable
Turning off cop watchdog can be solved. You can turn the watchdog off first when you debug the program.
You need to feed the dog constantly in places where there is a need for cyclic delays.
FAT32:
Physical Sector & Logical sector
The CMD24 command writes data to the inside, knowing the physical sector address.
OK: The MBR is located in the physical sector 0 sector, where the physical sector address of the DBR sector is stored.
DBR in Logical sector 0 sector
Read-Write physical sector is the physical address, the logical address is the file system, if only the SD card as a flash ROM to use
There is no concept of logical address.
In the file system, the logical address is for the DBR (Dos Boot Record) of a partition, if the DBR is in the 100 sector, then
The physical sector is in 150 sectors, and the logical sector address is 50. If the logical sector and the physical sector are consistent after formatting, that is, DBR is in the 0 sector, then in Winhex
Do not display physical sectors
NOTES: Multilevel directory structure. FAT 32 The following main structure: Boot area, file system area, data area
Under FAT32, the root directory is also attributed to the data area
After a subdirectory has been created, a subdirectory sector is seen in 7552 sectors, and a first-level file name is seen in the 7528 sectors (the root area), followed by a detailed study
Another major reform of FAT32 is the file of the root directory, which is the same as the normal file, which can be distributed anywhere in the data area, and the root directory in FAT16 is
Rigidly fixed, immediately following the Fat table, not part of the data area.
Such a root directory will not have the FAT16 in the 512 directory entry restrictions, not enough time to increase the cluster chain, allocate empty clusters.
Also, the location of the root directory is no longer rigidly fixed and can be stored in any cluster that can be addressed within the partition, but usually
The root directory is the first (formatted) Table of contents that is created. So what we're seeing is basically the 1th cluster in the root cluster sequence.
FAT32 the number of clusters is still the same as FAT16. The 1th cluster in the sequence is still numbered as the 2nd cluster, usually used in the root directory (which is different from the FAT16, the root of FAT16
The directory does not occupy the cluster space, and the root of 32 sectors is the 1th cluster in the cluster area.
Key:1, the first cluster number in the sequence is the second cluster, so the data area is found as the second cluster
2, data area, followed by the Fat table, you can find the beginning of the data area
3, the beginning of the general data area is the root directory area, so usually also find the root directory area, found the desired location.
Information that can be obtained directly:
Where the MBR:BPB is located (so-called logical offset position)
BPB: Number of shadow sectors fat table number of sectors per fat table
This data is sufficient to determine the physical location of the data area.