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


Ignore:
Timestamp:
2012-02-26T19:35:13Z (12 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
00af658
Parents:
e04bfbf0
Message:

mkexfat: Improvements and bug fixes

  • The user now can specify the filesystem size and the cluster size he wants.
  • Fix a bug when calculating the size of the bitmap in cfg_params_initialize().
File:
1 edited

Legend:

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

    re04bfbf0 ref144ef  
    4848#include <bool.h>
    4949#include <str.h>
     50#include <getopt.h>
    5051#include "exfat.h"
    5152#include "upcase.h"
     
    122123upcase_table_checksum(void const *data, size_t nbytes);
    123124
     125static struct option const long_options[] = {
     126        {"help", no_argument, 0, 'h'},
     127        {"cluster-size", required_argument, 0, 'c'},
     128        {"fs-size", required_argument, 0, 's'},
     129};
     130
    124131static void usage(void)
    125132{
    126         printf("Usage: mkexfat <device>\n");
     133        printf("Usage: mkexfat [options] <device>\n"
     134            "-c, --cluster-size ## Specify the cluster size\n"
     135            "-s, --fs-size ##      Specify the filesystem size\n");
    127136}
    128137
     
    137146        aoff64_t const volume_bytes = (cfg->volume_count - FAT_SECTOR_START) *
    138147            cfg->sector_size;
    139 
    140         aoff64_t n_req_clusters = volume_bytes / DEFAULT_CLUSTER_SIZE;
     148        aoff64_t n_req_clusters;
     149
     150        if (cfg->cluster_size != 0) {
     151                /* The user already choose the cluster size he wants */
     152                n_req_clusters = volume_bytes / cfg->cluster_size;
     153                goto skip_cluster_size_set;
     154        }
     155
     156        n_req_clusters = volume_bytes / DEFAULT_CLUSTER_SIZE;
    141157        cfg->cluster_size = DEFAULT_CLUSTER_SIZE;
    142158
     
    149165
    150166                cfg->cluster_size <<= 1;
    151                 n_req_clusters = volume_bytes / cfg->cluster_size;
    152         }
     167                n_req_clusters = div_round_up(volume_bytes, cfg->cluster_size);
     168        }
     169
     170skip_cluster_size_set:
    153171
    154172        /* The first two clusters are reserved */
     
    164182
    165183        /* Compute the bitmap size */
    166         cfg->bitmap_size = n_req_clusters / 8;
     184        cfg->bitmap_size = div_round_up(n_req_clusters, 8);
    167185
    168186        /* Compute the number of clusters reserved to the bitmap */
     
    678696}
    679697
     698static bool
     699is_power_of_two(unsigned long n)
     700{
     701        if (n == 0)
     702                return false;
     703
     704        return (n & (n - 1)) == 0;
     705}
     706
    680707int main (int argc, char **argv)
    681708{
     
    684711        char *dev_path;
    685712        service_id_t service_id;
    686         int rc;
     713        int rc, c, opt_ind;
     714        aoff64_t user_fs_size = 0;
    687715
    688716        if (argc < 2) {
     
    692720        }
    693721
    694         /* TODO: Add parameters */
    695 
    696         ++argv;
     722        cfg.cluster_size = 0;
     723
     724        for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
     725                c = getopt_long(argc, argv, "hs:c:",
     726                    long_options, &opt_ind);
     727                switch (c) {
     728                case 'h':
     729                        usage();
     730                        return 0;
     731                case 's':
     732                        user_fs_size = (aoff64_t) strtol(optarg, NULL, 10);
     733                        if (user_fs_size < 1024 * 1024) {
     734                                printf(NAME ": Error, fs size can't be less"
     735                                    " than 1 Mb.\n");
     736                                return 1;
     737                        }
     738                        break;
     739
     740                case 'c':
     741                        cfg.cluster_size = strtol(optarg, NULL, 10);
     742                        if (cfg.cluster_size < 4096) {
     743                                printf(NAME ": Error, cluster size can't"
     744                                    " be less than 4096 byte.\n");
     745                                return 1;
     746                        } else if (cfg.cluster_size > 32 * 1024 * 1024) {
     747                                printf(NAME ": Error, cluster size can't"
     748                                    " be greater than 32 Mb");
     749                                return 1;
     750                        }
     751
     752                        if (!is_power_of_two(cfg.cluster_size)) {
     753                                printf(NAME ": Error, the size of the cluster"
     754                                    " must be a power of two.\n");
     755                                return 1;
     756                        }
     757                        break;
     758                }
     759        }
     760
     761        argv += optind;
    697762        dev_path = *argv;
    698763
     
    727792                printf(NAME ": Warning, failed to obtain" \
    728793                    " device block size.\n");
    729                 /* FIXME: the user should be able to specify the filesystem size */
    730794                return 1;
    731795        } else {
     
    734798        }
    735799
     800        if (user_fs_size != 0) {
     801                if (user_fs_size > cfg.volume_count * cfg.sector_size) {
     802                        printf(NAME ": Error, the device is not big enough"
     803                            " to create the filesystem a filesystem of"
     804                            " the specified size.\n");
     805                        return 1;
     806                }
     807
     808                cfg.volume_count = user_fs_size / cfg.sector_size;
     809        }
     810
    736811        cfg_params_initialize(&cfg);
    737812        cfg_print_info(&cfg);
Note: See TracChangeset for help on using the changeset viewer.