📄 fs_configfat.c
字号:
{ denom = fmt->ff_nfats + (var->fv_sectorsize << (fmt->ff_clustshift - 2)); numer = navailsects + (1 << (fmt->ff_clustshift + 1)) + (1 << fmt->ff_clustshift); } return (uint32)((numer + denom - 1) / denom);}/**************************************************************************** * Name: mkfatfs_clustersearchlimits * * Description: * Pick the starting and ending cluster size to use in the search for the * the optimal cluster size. * * Input: * fmt - Caller specified format parameters * var - Other format parameters that are not caller specifiable. (Most * set by mkfatfs_configfatfs()). * * Return: * Starting cluster size is set in fmt->ff_clustshift; Final cluster * size is the returned value. * ****************************************************************************/static inline ubytemkfatfs_clustersearchlimits(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var){ ubyte mxclustshift; /* Did the caller already pick the cluster size? If not, the clustshift value * will be 0xff */ if (fmt->ff_clustshift == 0xff) { /* Pick a starting size based on the number of sectors on the device */ if (fmt->ff_nsectors < 2048) { /* 2k sectors, start wit 1 sector/cluster. */ fmt->ff_clustshift = 0; } else if (fmt->ff_nsectors < 4096) { /* 4k sectors, start with 2 sector/cluster. */ fmt->ff_clustshift = 1; } else if (fmt->ff_nsectors < 8192) { /* 8k sectors, start with 4 sector/cluster. */ fmt->ff_clustshift = 2; } else if (fmt->ff_nsectors < 16384) { /* 16k sectors, start with 8 sector/cluster. */ fmt->ff_clustshift = 3; } else if (fmt->ff_nsectors < 32768) { /* 32k sectors, start with 16 sector/cluster. */ fmt->ff_clustshift = 4; } else { /* Otherwise, 32 sector/cluster. */ fmt->ff_clustshift = 5; } /* Wherever the search starts, it will end with the maximum of * 128 sectors per cluster */ mxclustshift = 7; } else { /* The caller has selected a cluster size. There will be no search! * Just set the maximum to the caller specificed value. */ mxclustshift = fmt->ff_clustshift; } return mxclustshift;}/**************************************************************************** * Name: mkfatfs_try12 * * Description: * Try to define a FAT12 filesystem on the device using the candidate * sectors per cluster * * Input: * fmt - Caller specified format parameters * var - Other format parameters that are not caller specifiable. (Most * set by mkfatfs_configfatfs()). * fatconfig - FAT12-specific configuration * * Return: * Zero on success configuration of a FAT12 file system; negated errno * on failure * ****************************************************************************/static inline intmkfatfs_tryfat12(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, FAR struct fat_config_s *config){ uint32 maxnclusters; /* Calculate the number sectors in one FAT required to access all of the * available sectors. */ config->fc_nfatsects = mkfatfs_nfatsect12(fmt, var, config->fc_navailsects); if (config->fc_nfatsects > 0) { /* Calculate the number of clusters available given the number of available * sectors and the number of those that will be used for FAT: */ config->fc_nclusters = (config->fc_navailsects - fmt->ff_nfats * config->fc_nfatsects) >> fmt->ff_clustshift; /* Calculate the maximum number of clusters that could be supported by a * FAT of this size. * * maxnclusters = nfatsects * sectorsize / 1.5 - 2 */ maxnclusters = (config->fc_nfatsects >> (var->fv_sectshift + 1)) / 3; if (maxnclusters > FAT_MAXCLUST12) { maxnclusters = FAT_MAXCLUST12; } fvdbg("nfatsects=%u nclusters=%u (max=%u)\n", config->fc_nfatsects, config->fc_nclusters, maxnclusters); /* Check if this number of clusters would overflow the maximum for * FAT12 (remembering that two FAT cluster slots are reserved). */ if (config->fc_nclusters > maxnclusters - 2) { fvdbg("Too many clusters for FAT12\n"); return -ENFILE; } } return 0;} /**************************************************************************** * Name: mkfatfs_try16 * * Description: * Try to define a FAT16 filesystem on the device using the candidate * sectors per cluster * * Input: * fmt - Caller specified format parameters * var - Other format parameters that are not caller specifiable. (Most * set by mkfatfs_configfatfs()). * fatconfig - FAT16-specific configuration * * Return: * Zero on success configuration of a FAT16 file system; negated errno * on failure * ****************************************************************************/static inline intmkfatfs_tryfat16(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, FAR struct fat_config_s *config){ uint32 maxnclusters; /* Calculate the number sectors in one FAT required to access all of the * available sectors. */ config->fc_nfatsects = mkfatfs_nfatsect16(fmt, var, config->fc_navailsects); if (config->fc_nfatsects > 0) { /* Calculate the number of clusters available given the number of available * sectors and the number of those that will be used for FAT: */ config->fc_nclusters = (config->fc_navailsects - fmt->ff_nfats * config->fc_nfatsects) >> fmt->ff_clustshift; /* Calculate the maximum number of clusters that could be supported by a * FAT of this size. * * maxnclusters = nfatsects * sectorsize / 2 - 2 */ maxnclusters = config->fc_nfatsects << (var->fv_sectorsize - 1); if (maxnclusters > FAT_MAXCLUST16) { maxnclusters = FAT_MAXCLUST16; } fvdbg("nfatsects=%u nclusters=%u (min=%u max=%u)\n", config->fc_nfatsects, config->fc_nclusters, FAT_MINCLUST16, maxnclusters); /* Check if this number of clusters would overflow the maximum for * FAT16 (remembering that two FAT cluster slots are reserved). * Check the lower limit as well. The FAT12 is distinguished from FAT16 * by comparing the number of clusters on the device agains a known * threshold. If a small FAT16 file system were created, then it would * be confused as a FAT12 at mount time. */ if ((config->fc_nclusters > maxnclusters - 2) || (config->fc_nclusters < FAT_MINCLUST16)) { fvdbg("Too few or too many clusters for FAT16\n"); return -ENFILE; } } return 0;}/**************************************************************************** * Name: mkfatfs_try32 * * Description: * Try to define a FAT32 filesystem on the device using the candidate * sectors per cluster * * Input: * fmt - Caller specified format parameters * var - Other format parameters that are not caller specifiable. (Most * set by mkfatfs_configfatfs()). * fatconfig - FAT32-specific configuration * * Return: * Zero on success configuration of a FAT32 file system; negated errno * on failure * ****************************************************************************/static inline intmkfatfs_tryfat32(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, FAR struct fat_config_s *config){ uint32 maxnclusters; /* Calculate the number sectors in one FAT required to access all of the * available sectors. */ config->fc_nfatsects = mkfatfs_nfatsect32(fmt, var, config->fc_navailsects); if (config->fc_nfatsects > 0) { /* Calculate the number of clusters available given the number of available * sectors and the number of those that will be used for FAT: */ config->fc_nclusters = (config->fc_navailsects - fmt->ff_nfats * config->fc_nfatsects) >> fmt->ff_clustshift; /* Calculate the maximum number of clusters that could be supported by a * FAT of this size. * * maxnclusters = nfatsects * sectorsize / 4 - 2 */ maxnclusters = (config->fc_nfatsects >> (var->fv_sectshift - 2)); if (maxnclusters > FAT_MAXCLUST32) { maxnclusters = FAT_MAXCLUST32; } fvdbg("nfatsects=%u nclusters=%u (max=%u)\n", config->fc_nfatsects, config->fc_nclusters, maxnclusters); /* Check if this number of clusters would overflow the maximum for * FAT32 (remembering that two FAT cluster slots are reserved). */ if ((config->fc_nclusters > maxnclusters - 3) || (config->fc_nclusters < FAT_MINCLUST32 && fmt->ff_fattype != 32)) { fvdbg("Too few or too many clusters for FAT32\n"); return -ENFILE; } } return 0;}/**************************************************************************** * Name: mkfatfs_selectfat * * Description: * The cluster search has succeeded, select the specified FAT FS * * Input: * fattype - The FAT size selected * fmt - Caller specified format parameters * var - Format parameters that are not caller specifiable. * * Return: * None * ****************************************************************************/static inline voidmkfatfs_selectfat(int fattype, FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, FAR struct fat_config_s *config){ /* Return the appropriate information about the selected file system. */ fdbg("Selected FAT%d\n", fattype); var->fv_fattype = fattype; var->fv_nclusters = config->fc_nclusters; var->fv_nfatsects = config->fc_nfatsects; fmt->ff_rsvdseccount = config->fc_rsvdseccount;}/**************************************************************************** * Name: mkfatfs_clustersearch * * Description:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -