Transplantation of fatfs File System on PC (Windows/Linux) (2)

Source: Internet
Author: User

Hurry up and continue ~

Each slice is 512 bytes. I create a file of 2 MB. At the beginning, I did not know how many sectors of a cluster. Later, when I debugged the program, I saw an internal array with two sectors in one cluster. I am simulating NAND Flash. A page512 byte, each of which has 32 pages, and a file of 2 MB has 128 blocks. The first block of the file is used as the system block. Therefore, the slice size must be deducted from the system block.

After writing diskio. C, you can start to write the test code.

First, you must create a file with a certain size. I tested it through read/write.

1. Call the f_mount function. Generally, mounting will not cause any problems.

2. At the beginning, I did not call f_mkfs (0, 0, 0) to open the file directly. Opening the file will always fail.

3. perform in-depth research on fopen. The fatfs file system has the object concept. Chk_mounted is used to check whether the file system object is valid. Debugging problems are stuck in FMT = check_fs (FS, bsect = 0);/* Check sector 0 if it is a VBR */. This function is used to load the Boot Record and check whether it is a fat ing table ).

Static
Byte check_fs (/* 0: the fat Br, 1: Valid BR but not an fat, 2: Not a Br, 3: disk error */
Fatfs * FS,
/* File System Object */
DWORD sect
/* Sector # (LBA) to check if it is an fat Boot Record or not */
)
{
If (disk_read (FS-> DRV, FS-> win, sect, 1 )! = Res_ OK)/* load Boot Record */
Return 3;
If (ld_word (& FS-> win [bs_55aa])! = 0xaa55)/* check record signature (always placed at offset 510 even if the sector size is> 512 )*/
Return 2;

If (ld_dword (& FS-> win [bs_filpolicype]) & 0 xffffff) = 0x544146)/* Check "fat" string */
Return 0;
If (ld_dword (& FS-> win [bs_filpolicype32]) & 0 xffffff) = 0x544146)
Return 0;

Return 1;
}

Let's talk about two scenarios of return 3:

1) Create an empty file with no size or formatting

2) later I heard from the boss that I could create an imgfile. I went to the Internet to find relevant information. A 2 m imgfile of fat12/16 is created successfully. After testing, fopen is successfully executed. However, fwrite and fread fail. I have considered that the imgfile cannot be read and written directly during file operations.

If return 2 is used, the file is not formatted.

Is the test result.

In the test result, I used the UE tool to open the carrier file.

There is data entering at 0x0000000h.

FAT file system partition interpretation, see the link: http://199818.blog.51cto.com/189818/32679

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.