Changeset d7f09583 in mainline for uspace/app/mkexfat/mkexfat.c


Ignore:
Timestamp:
2012-02-27T19:48:09Z (12 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2601383
Parents:
9ce1acf
Message:

mkexfat: try to prevent the user from specifying a cluster size too big for the device.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/mkexfat/mkexfat.c

    r9ce1acf rd7f09583  
    146146        aoff64_t const volume_bytes = (cfg->volume_count - FAT_SECTOR_START) *
    147147            cfg->sector_size;
    148         aoff64_t n_req_clusters;
    149148
    150149        if (cfg->cluster_size != 0) {
    151150                /* The user already choose the cluster size he wants */
    152                 n_req_clusters = volume_bytes / cfg->cluster_size;
     151                cfg->total_clusters = volume_bytes / cfg->cluster_size;
    153152                goto skip_cluster_size_set;
    154153        }
    155154
    156         n_req_clusters = volume_bytes / DEFAULT_CLUSTER_SIZE;
     155        cfg->total_clusters = volume_bytes / DEFAULT_CLUSTER_SIZE;
    157156        cfg->cluster_size = DEFAULT_CLUSTER_SIZE;
    158157
     
    161160         * size less or equal to 64 Mb.
    162161         */
    163         while (n_req_clusters > 16000000ULL &&
     162        while (cfg->total_clusters > 16000000ULL &&
    164163            (cfg->cluster_size < 32 * 1024 * 1024)) {
    165164
    166165                cfg->cluster_size <<= 1;
    167                 n_req_clusters = div_round_up(volume_bytes, cfg->cluster_size);
     166                cfg->total_clusters = div_round_up(volume_bytes,
     167                    cfg->cluster_size);
    168168        }
    169169
    170170skip_cluster_size_set:
    171171
    172         /* The first two clusters are reserved */
    173         cfg->total_clusters = n_req_clusters + 2;
    174 
    175172        /* Compute the FAT size in sectors */
    176         fat_bytes = (cfg->total_clusters + 1) * 4;
     173        fat_bytes = (cfg->total_clusters + 3) * sizeof(uint32_t);
    177174        cfg->fat_sector_count = div_round_up(fat_bytes, cfg->sector_size);
    178175
     
    182179
    183180        /* Compute the bitmap size */
    184         cfg->bitmap_size = div_round_up(n_req_clusters, 8);
     181        cfg->bitmap_size = div_round_up(cfg->total_clusters, 8);
    185182
    186183        /* Compute the number of clusters reserved to the bitmap */
     
    194191        cfg->allocated_clusters += div_round_up(sizeof(upcase_table),
    195192            cfg->cluster_size);
     193
     194        /* Subtract the FAT and bootsector space from the total
     195         * number of available clusters.
     196         */
     197        cfg->total_clusters -= div_round_up(cfg->data_start_sector *
     198            cfg->sector_size, cfg->cluster_size);
    196199
    197200        /* Will be set later */
     
    219222        printf(NAME ": Data start sector:     %lu\n", cfg->data_start_sector);
    220223        printf(NAME ": Total num of clusters: %lu\n", cfg->total_clusters);
     224        printf(NAME ": Total used clusters:   %lu\n", cfg->allocated_clusters);
    221225        printf(NAME ": Bitmap size:           %lu\n", (unsigned long)
    222226            div_round_up(cfg->bitmap_size, cfg->cluster_size));
     
    251255        mbs->data_start_sector = host2uint32_t_le(cfg->data_start_sector);
    252256
    253         mbs->data_clusters = host2uint32_t_le(cfg->total_clusters -
    254             div_round_up(cfg->data_start_sector * cfg->sector_size,
    255             cfg->cluster_size));
     257        mbs->data_clusters = host2uint32_t_le(cfg->total_clusters);
    256258
    257259        mbs->rootdir_cluster = host2uint32_t_le(cfg->rootdir_cluster);
     
    818820        cfg_print_info(&cfg);
    819821
     822        if ((cfg.total_clusters - cfg.allocated_clusters) <= 2) {
     823                printf(NAME ": Error, insufficient disk space on device.\n");
     824                return 2;
     825        }
     826
    820827        printf(NAME ": Writing the allocation table.\n");
    821828
Note: See TracChangeset for help on using the changeset viewer.