fatfs icon indicating copy to clipboard operation
fatfs copied to clipboard

fatfs can't recognize FAT32 image (32MB) created by shell command

Open haorongLemon opened this issue 3 years ago • 0 comments

i just create empty FAT32 image file with raspberry pi, the command bellow:

$ dd if=/dev/zero of=img.bin bs=1M count=32
$ mkfs -t vfat -F 32 -n BOOT img.bin > /dev/null 2>&1
$ mount -t vfat img.bin  /mnt                # mount it sucessfully!

then, i modify diskio.c to read image, the code just like that:

DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
	BYTE *buff,		/* Data buffer to store read data */
	LBA_t sector,	/* Start sector in LBA */
	UINT count		/* Number of sectors to read */
)
{
    FILE* fp = fopen("img.bin", "rb");
    fseek(fp, sector * FF_MIN_SS, SEEK_SET);
    fread(buff, FF_MIN_SS, count, fp);
    fclose(fp);
    return 0;
}

when i call “f_mount(&fatfs, "0:", 1);”,it return 13, so i debug it with codeblocks. the source code bellow make me confused:

static FRESULT mount_volume (){    # file: ff.c,  line: 33323
    ...
		nclst = (tsect - sysect) / fs->csize;			/* Number of clusters */
		if (nclst == 0) return FR_NO_FILESYSTEM;
		fmt = 0;
		if (nclst <= MAX_FAT32) fmt = FS_FAT32;        
		if (nclst <= MAX_FAT16) fmt = FS_FAT16;   // my image size  is 32MB, it set fmt equal to FS_FAT16
		if (nclst <= MAX_FAT12) fmt = FS_FAT12;
		if (fmt == 0) return FR_NO_FILESYSTEM;
               ...
		if (fmt == FS_FAT32) {
			if (ld_word(fs->win + BPB_FSVer32) != 0) return FR_NO_FILESYSTEM;
			if (fs->n_rootdir != 0) return FR_NO_FILESYSTEM;    // logic different, in other cases(FAT16?)
			fs->dirbase = ld_dword(fs->win + BPB_RootClus32);	
			szbfat = fs->n_fatent * 4;					
		} else {
			if (fs->n_rootdir == 0)	return FR_NO_FILESYSTEM;   // touch here, then return 13, means mount failed 
			fs->dirbase = fs->fatbase + fasize;		
			szbfat = (fmt == FS_FAT16) ?				
				fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1);
		}
    ...
}

my image file is 32MB, i guess this procedure recognize type with img size?howerver, the logic "fs->n_rootdir == 0" is different between FS_FAT32 case and NON FS_FAT32 cases。

haorongLemon avatar Jul 07 '21 10:07 haorongLemon