/* * Copyright (c) 2010 Jiri Svoboda * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * - The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** @addtogroup mkfat * @{ */ /** * @file mkfat.c * @brief Tool for creating new FAT file systems. * * Currently we can create 12/16/32-bit FAT. */ #include #include #include #include #include #include #include #include #include #include #include #include #include "fat.h" #include "fat_dentry.h" #define NAME "mkfat" #define LABEL_NONAME "NO NAME" /** Divide and round up. */ #define div_round_up(a, b) (((a) + (b) - 1) / (b)) /** Default file-system parameters */ enum { default_sector_size = 512, default_sectors_per_cluster = 4, default_fat_count = 2, default_reserved_clusters = 2, default_media_descriptor = 0xF8 /**< fixed disk */ }; /** Configurable file-system parameters */ typedef struct fat_cfg { int fat_type; /* FAT12 = 12, FAT16 = 16, FAT32 = 32 */ size_t sector_size; uint32_t total_sectors; uint16_t root_ent_max; uint32_t addt_res_sectors; uint8_t sectors_per_cluster; uint16_t reserved_sectors; uint32_t rootdir_sectors; uint32_t fat_sectors; uint32_t total_clusters; uint8_t fat_count; const char *label; } fat_cfg_t; static void syntax_print(void); static errno_t fat_params_compute(struct fat_cfg *cfg); static errno_t fat_blocks_write(struct fat_cfg const *cfg, service_id_t service_id); static errno_t fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs); int main(int argc, char **argv) { struct fat_cfg cfg; errno_t rc; char *dev_path; service_id_t service_id; char *endptr; aoff64_t dev_nblocks; cfg.sector_size = default_sector_size; cfg.sectors_per_cluster = default_sectors_per_cluster; cfg.fat_count = default_fat_count; cfg.total_sectors = 0; cfg.addt_res_sectors = 0; cfg.root_ent_max = 128; cfg.fat_type = FATAUTO; cfg.label = NULL; if (argc < 2) { printf(NAME ": Error, argument missing.\n"); syntax_print(); return 1; } --argc; ++argv; while (*argv[0] == '-') { if (str_cmp(*argv, "--size") == 0) { --argc; ++argv; if (*argv == NULL) { printf(NAME ": Error, argument missing.\n"); syntax_print(); return 1; } cfg.total_sectors = strtol(*argv, &endptr, 10); if (*endptr != '\0') { printf(NAME ": Error, invalid argument.\n"); syntax_print(); return 1; } --argc; ++argv; } if (str_cmp(*argv, "--type") == 0) { --argc; ++argv; if (*argv == NULL) { printf(NAME ": Error, argument missing.\n"); syntax_print(); return 1; } cfg.fat_type = strtol(*argv, &endptr, 10); if (*endptr != '\0') { printf(NAME ": Error, invalid argument.\n"); syntax_print(); return 1; } --argc; ++argv; } if (str_cmp(*argv, "--label") == 0) { --argc; ++argv; if (*argv == NULL) { printf(NAME ": Error, argument missing.\n"); syntax_print(); return 1; } cfg.label = *argv; --argc; ++argv; } if (str_cmp(*argv, "-") == 0) { --argc; ++argv; break; } } if (argc != 1) { printf(NAME ": Error, unexpected argument.\n"); syntax_print(); return 1; } dev_path = *argv; printf("Device: %s\n", dev_path); rc = loc_service_get_id(dev_path, &service_id, 0); if (rc != EOK) { printf(NAME ": Error resolving device `%s'.\n", dev_path); return 2; } rc = block_init(service_id, 2048); if (rc != EOK) { printf(NAME ": Error initializing libblock.\n"); return 2; } rc = block_get_bsize(service_id, &cfg.sector_size); if (rc != EOK) { printf(NAME ": Error determining device block size.\n"); return 2; } rc = block_get_nblocks(service_id, &dev_nblocks); if (rc != EOK) { printf(NAME ": Warning, failed to obtain block device size.\n"); } else { printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n", dev_nblocks); if (!cfg.total_sectors || dev_nblocks < cfg.total_sectors) cfg.total_sectors = dev_nblocks; } if (cfg.total_sectors == 0) { printf(NAME ": Error. You must specify filesystem size.\n"); return 1; } if (cfg.fat_type != FATAUTO && cfg.fat_type != FAT12 && cfg.fat_type != FAT16 && cfg.fat_type != FAT32) { printf(NAME ": Error. Unknown FAT type.\n"); return 2; } printf(NAME ": Creating FAT filesystem on device %s.\n", dev_path); rc = fat_params_compute(&cfg); if (rc != EOK) { printf(NAME ": Invalid file-system parameters.\n"); return 2; } printf(NAME ": Filesystem type FAT%d.\n", cfg.fat_type); rc = fat_blocks_write(&cfg, service_id); if (rc != EOK) { printf(NAME ": Error writing device.\n"); return 2; } block_fini(service_id); printf("Success.\n"); return 0; } static void syntax_print(void) { printf("syntax: mkfat [...] \n"); printf("options:\n" "\t--size Filesystem size, overrides device size\n" "\t--type 12|16|32 FAT type (auto-detected by default)\n" "\t--label