Changes in uspace/app/mkfat/mkfat.c [991f645:51d0ee9] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/mkfat/mkfat.c
r991f645 r51d0ee9 35 35 * @brief Tool for creating new FAT file systems. 36 36 * 37 * Currently we can only create 16-bit FAT.37 * Currently we can create 12/16/32-bit FAT. 38 38 */ 39 39 … … 42 42 #include <libblock.h> 43 43 #include <mem.h> 44 #include < devmap.h>44 #include <loc.h> 45 45 #include <byteorder.h> 46 46 #include <sys/types.h> … … 55 55 #define div_round_up(a, b) (((a) + (b) - 1) / (b)) 56 56 57 /** Predefinedfile-system parameters */57 /** Default file-system parameters */ 58 58 enum { 59 sector_size = 512,60 sectors_per_cluster = 8,61 fat_count = 2,62 reserved_clusters = 2,63 media_descriptor = 0xF8 /**< fixed disk */59 default_sector_size = 512, 60 default_sectors_per_cluster = 4, 61 default_fat_count = 2, 62 default_reserved_clusters = 2, 63 default_media_descriptor = 0xF8 /**< fixed disk */ 64 64 }; 65 65 66 66 /** Configurable file-system parameters */ 67 67 typedef struct fat_cfg { 68 int fat_type; /* FAT12 = 12, FAT16 = 16, FAT32 = 32 */ 69 size_t sector_size; 68 70 uint32_t total_sectors; 69 71 uint16_t root_ent_max; 70 uint16_t addt_res_sectors; 72 uint32_t addt_res_sectors; 73 uint8_t sectors_per_cluster; 74 75 uint16_t reserved_sectors; 76 uint32_t rootdir_sectors; 77 uint32_t fat_sectors; 78 uint32_t total_clusters; 79 uint8_t fat_count; 71 80 } fat_cfg_t; 72 81 73 /** Derived file-system parameters */74 typedef struct fat_params {75 struct fat_cfg cfg;76 uint16_t reserved_sectors;77 uint16_t rootdir_sectors;78 uint32_t fat_sectors;79 uint16_t total_clusters;80 } fat_params_t;81 82 82 static void syntax_print(void); 83 83 84 static int fat_params_compute(struct fat_cfg const *cfg, 85 struct fat_params *par); 86 static int fat_blocks_write(struct fat_params const *par, 87 devmap_handle_t handle); 88 static void fat_bootsec_create(struct fat_params const *par, struct fat_bs *bs); 84 static int fat_params_compute(struct fat_cfg *cfg); 85 static int fat_blocks_write(struct fat_cfg const *cfg, service_id_t service_id); 86 static void fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs); 89 87 90 88 int main(int argc, char **argv) 91 89 { 92 struct fat_params par;93 90 struct fat_cfg cfg; 94 91 95 92 int rc; 96 93 char *dev_path; 97 devmap_handle_t handle; 98 size_t block_size; 94 service_id_t service_id; 99 95 char *endptr; 100 96 aoff64_t dev_nblocks; 101 97 98 cfg.sector_size = default_sector_size; 99 cfg.sectors_per_cluster = default_sectors_per_cluster; 100 cfg.fat_count = default_fat_count; 102 101 cfg.total_sectors = 0; 103 102 cfg.addt_res_sectors = 0; 104 103 cfg.root_ent_max = 128; 104 cfg.fat_type = FAT16; 105 105 106 106 if (argc < 2) { … … 111 111 112 112 --argc; ++argv; 113 114 113 if (str_cmp(*argv, "--size") == 0) { 115 114 --argc; ++argv; … … 130 129 } 131 130 131 if (str_cmp(*argv, "--type") == 0) { 132 --argc; ++argv; 133 if (*argv == NULL) { 134 printf(NAME ": Error, argument missing.\n"); 135 syntax_print(); 136 return 1; 137 } 138 139 cfg.fat_type = strtol(*argv, &endptr, 10); 140 if (*endptr != '\0') { 141 printf(NAME ": Error, invalid argument.\n"); 142 syntax_print(); 143 return 1; 144 } 145 146 --argc; ++argv; 147 } 148 132 149 if (argc != 1) { 133 150 printf(NAME ": Error, unexpected argument.\n"); … … 137 154 138 155 dev_path = *argv; 139 140 rc = devmap_device_get_handle(dev_path, &handle, 0); 156 printf("Device: %s\n", dev_path); 157 158 rc = loc_service_get_id(dev_path, &service_id, 0); 141 159 if (rc != EOK) { 142 160 printf(NAME ": Error resolving device `%s'.\n", dev_path); … … 144 162 } 145 163 146 rc = block_init( handle, 2048);164 rc = block_init(EXCHANGE_SERIALIZE, service_id, 2048); 147 165 if (rc != EOK) { 148 166 printf(NAME ": Error initializing libblock.\n"); … … 150 168 } 151 169 152 rc = block_get_bsize( handle, &block_size);170 rc = block_get_bsize(service_id, &cfg.sector_size); 153 171 if (rc != EOK) { 154 172 printf(NAME ": Error determining device block size.\n"); … … 156 174 } 157 175 158 rc = block_get_nblocks( handle, &dev_nblocks);176 rc = block_get_nblocks(service_id, &dev_nblocks); 159 177 if (rc != EOK) { 160 178 printf(NAME ": Warning, failed to obtain block device size.\n"); … … 162 180 printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n", 163 181 dev_nblocks); 164 cfg.total_sectors = dev_nblocks; 165 } 166 167 if (block_size != 512) { 168 printf(NAME ": Error. Device block size is not 512 bytes.\n"); 169 return 2; 182 if (!cfg.total_sectors || dev_nblocks < cfg.total_sectors) 183 cfg.total_sectors = dev_nblocks; 170 184 } 171 185 … … 175 189 } 176 190 177 printf(NAME ": Creating FAT filesystem on device %s.\n", dev_path); 178 179 rc = fat_params_compute(&cfg, &par); 191 if (cfg.fat_type != FAT12 && cfg.fat_type != FAT16 && cfg.fat_type != FAT32) { 192 printf(NAME ": Error. Unknown FAT type.\n"); 193 return 2; 194 } 195 196 printf(NAME ": Creating FAT%d filesystem on device %s.\n", cfg.fat_type, dev_path); 197 198 rc = fat_params_compute(&cfg); 180 199 if (rc != EOK) { 181 200 printf(NAME ": Invalid file-system parameters.\n"); … … 183 202 } 184 203 185 rc = fat_blocks_write(& par, handle);204 rc = fat_blocks_write(&cfg, service_id); 186 205 if (rc != EOK) { 187 206 printf(NAME ": Error writing device.\n"); … … 189 208 } 190 209 191 block_fini( handle);210 block_fini(service_id); 192 211 printf("Success.\n"); 193 212 … … 197 216 static void syntax_print(void) 198 217 { 199 printf("syntax: mkfat [--size < num_blocks>] <device_name>\n");218 printf("syntax: mkfat [--size <sectors>] [--type 12|16|32] <device_name>\n"); 200 219 } 201 220 … … 205 224 * file system params. 206 225 */ 207 static int fat_params_compute(struct fat_cfg const *cfg, struct fat_params *par)226 static int fat_params_compute(struct fat_cfg *cfg) 208 227 { 209 228 uint32_t fat_bytes; … … 211 230 212 231 /* 213 * Make a conservative guess on the FAT size needed for the file 214 * system. The optimum could be potentially smaller since we 215 * do not subtract size of the FAT itself when computing the 216 * size of the data region. 217 */ 218 219 par->reserved_sectors = 1 + cfg->addt_res_sectors; 220 par->rootdir_sectors = div_round_up(cfg->root_ent_max * DIRENT_SIZE, 221 sector_size); 222 non_data_sectors_lb = par->reserved_sectors + par->rootdir_sectors; 223 224 par->total_clusters = div_round_up(cfg->total_sectors - non_data_sectors_lb, 225 sectors_per_cluster); 226 227 fat_bytes = (par->total_clusters + 2) * 2; 228 par->fat_sectors = div_round_up(fat_bytes, sector_size); 229 230 par->cfg = *cfg; 232 * Make a conservative guess on the FAT size needed for the file 233 * system. The optimum could be potentially smaller since we 234 * do not subtract size of the FAT itself when computing the 235 * size of the data region. 236 */ 237 238 cfg->reserved_sectors = 1 + cfg->addt_res_sectors; 239 if (cfg->fat_type != FAT32) { 240 cfg->rootdir_sectors = div_round_up(cfg->root_ent_max * DIRENT_SIZE, 241 cfg->sector_size); 242 } else 243 cfg->rootdir_sectors = 0; 244 non_data_sectors_lb = cfg->reserved_sectors + cfg->rootdir_sectors; 245 246 cfg->total_clusters = div_round_up(cfg->total_sectors - non_data_sectors_lb, 247 cfg->sectors_per_cluster); 248 249 if ((cfg->fat_type == FAT12 && cfg->total_clusters > FAT12_CLST_MAX) || 250 (cfg->fat_type == FAT16 && (cfg->total_clusters <= FAT12_CLST_MAX || 251 cfg->total_clusters > FAT16_CLST_MAX)) || 252 (cfg->fat_type == FAT32 && cfg->total_clusters <= FAT16_CLST_MAX)) 253 return ENOSPC; 254 255 fat_bytes = div_round_up((cfg->total_clusters + 2) * 256 FAT_CLUSTER_DOUBLE_SIZE(cfg->fat_type), 2); 257 cfg->fat_sectors = div_round_up(fat_bytes, cfg->sector_size); 231 258 232 259 return EOK; … … 234 261 235 262 /** Create file system with the given parameters. */ 236 static int fat_blocks_write(struct fat_ params const *par, devmap_handle_t handle)263 static int fat_blocks_write(struct fat_cfg const *cfg, service_id_t service_id) 237 264 { 238 265 aoff64_t addr; … … 243 270 struct fat_bs bs; 244 271 245 fat_bootsec_create( par, &bs);246 247 rc = block_write_direct( handle, BS_BLOCK, 1, &bs);272 fat_bootsec_create(cfg, &bs); 273 274 rc = block_write_direct(service_id, BS_BLOCK, 1, &bs); 248 275 if (rc != EOK) 249 276 return EIO; … … 251 278 addr = BS_BLOCK + 1; 252 279 253 buffer = calloc( sector_size, 1);280 buffer = calloc(cfg->sector_size, 1); 254 281 if (buffer == NULL) 255 282 return ENOMEM; 283 memset(buffer, 0, cfg->sector_size); 256 284 257 285 /* Reserved sectors */ 258 for (i = 0; i < par->reserved_sectors - 1; ++i) {259 rc = block_write_direct( handle, addr, 1, buffer);286 for (i = 0; i < cfg->reserved_sectors - 1; ++i) { 287 rc = block_write_direct(service_id, addr, 1, buffer); 260 288 if (rc != EOK) 261 289 return EIO; … … 265 293 266 294 /* File allocation tables */ 267 for (i = 0; i < fat_count; ++i) {295 for (i = 0; i < cfg->fat_count; ++i) { 268 296 printf("Writing allocation table %d.\n", i + 1); 269 297 270 for (j = 0; j < par->fat_sectors; ++j) {271 memset(buffer, 0, sector_size);298 for (j = 0; j < cfg->fat_sectors; ++j) { 299 memset(buffer, 0, cfg->sector_size); 272 300 if (j == 0) { 273 buffer[0] = media_descriptor;301 buffer[0] = default_media_descriptor; 274 302 buffer[1] = 0xFF; 275 303 buffer[2] = 0xFF; 276 buffer[3] = 0xFF; 304 if (cfg->fat_type == FAT16) { 305 buffer[3] = 0xFF; 306 } else if (cfg->fat_type == FAT32) { 307 buffer[3] = 0x0F; 308 buffer[4] = 0xFF; 309 buffer[5] = 0xFF; 310 buffer[6] = 0xFF; 311 buffer[7] = 0x0F; 312 buffer[8] = 0xF8; 313 buffer[9] = 0xFF; 314 buffer[10] = 0xFF; 315 buffer[11] = 0x0F; 316 } 277 317 } 278 318 279 rc = block_write_direct( handle, addr, 1, buffer);319 rc = block_write_direct(service_id, addr, 1, buffer); 280 320 if (rc != EOK) 281 321 return EIO; … … 285 325 } 286 326 327 /* Root directory */ 287 328 printf("Writing root directory.\n"); 288 289 memset(buffer, 0, sector_size); 290 291 /* Root directory */ 292 for (i = 0; i < par->rootdir_sectors; ++i) { 293 rc = block_write_direct(handle, addr, 1, buffer); 294 if (rc != EOK) 295 return EIO; 296 297 ++addr; 329 memset(buffer, 0, cfg->sector_size); 330 if (cfg->fat_type != FAT32) { 331 size_t idx; 332 for (idx = 0; idx < cfg->rootdir_sectors; ++idx) { 333 rc = block_write_direct(service_id, addr, 1, buffer); 334 if (rc != EOK) 335 return EIO; 336 337 ++addr; 338 } 339 } else { 340 for (i = 0; i < cfg->sectors_per_cluster; i++) { 341 rc = block_write_direct(service_id, addr, 1, buffer); 342 if (rc != EOK) 343 return EIO; 344 345 ++addr; 346 } 298 347 } 299 348 … … 304 353 305 354 /** Construct boot sector with the given parameters. */ 306 static void fat_bootsec_create(struct fat_ params const *par, struct fat_bs *bs)355 static void fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs) 307 356 { 308 357 memset(bs, 0, sizeof(*bs)); … … 315 364 316 365 /* BIOS Parameter Block */ 317 bs->bps = host2uint16_t_le(sector_size); 318 bs->spc = sectors_per_cluster; 319 bs->rscnt = host2uint16_t_le(par->reserved_sectors); 320 bs->fatcnt = fat_count; 321 bs->root_ent_max = host2uint16_t_le(par->cfg.root_ent_max); 322 323 if (par->cfg.total_sectors < 0x10000) 324 bs->totsec16 = host2uint16_t_le(par->cfg.total_sectors); 325 else 326 bs->totsec16 = host2uint16_t_le(0); 327 328 bs->mdesc = media_descriptor; 329 bs->sec_per_fat = host2uint16_t_le(par->fat_sectors); 366 bs->bps = host2uint16_t_le(cfg->sector_size); 367 bs->spc = cfg->sectors_per_cluster; 368 bs->rscnt = host2uint16_t_le(cfg->reserved_sectors); 369 bs->fatcnt = cfg->fat_count; 370 bs->root_ent_max = host2uint16_t_le(cfg->root_ent_max); 371 372 if (cfg->total_sectors < 0x10000) { 373 bs->totsec16 = host2uint16_t_le(cfg->total_sectors); 374 bs->totsec32 = 0; 375 } else { 376 bs->totsec16 = 0; 377 bs->totsec32 = host2uint32_t_le(cfg->total_sectors); 378 } 379 380 bs->mdesc = default_media_descriptor; 330 381 bs->sec_per_track = host2uint16_t_le(63); 382 bs->signature = host2uint16_t_be(0x55AA); 331 383 bs->headcnt = host2uint16_t_le(6); 332 384 bs->hidden_sec = host2uint32_t_le(0); 333 385 334 if (par->cfg.total_sectors >= 0x10000) 335 bs->totsec32 = host2uint32_t_le(par->cfg.total_sectors); 336 else 337 bs->totsec32 = host2uint32_t_le(0); 338 339 /* Extended BPB */ 340 bs->pdn = 0x80; 341 bs->ebs = 0x29; 342 bs->id = host2uint32_t_be(0x12345678); 343 344 memcpy(bs->label, "HELENOS_NEW", 11); 345 memcpy(bs->type, "FAT16 ", 8); 346 bs->signature = host2uint16_t_be(0x55AA); 386 if (cfg->fat_type == FAT32) { 387 bs->sec_per_fat = 0; 388 bs->fat32.sectors_per_fat = host2uint32_t_le(cfg->fat_sectors); 389 390 bs->fat32.pdn = 0x80; 391 bs->fat32.ebs = 0x29; 392 bs->fat32.id = host2uint32_t_be(0x12345678); 393 bs->fat32.root_cluster = 2; 394 395 memcpy(bs->fat32.label, "HELENOS_NEW", 11); 396 memcpy(bs->fat32.type, "FAT32 ", 8); 397 } else { 398 bs->sec_per_fat = host2uint16_t_le(cfg->fat_sectors); 399 bs->pdn = 0x80; 400 bs->ebs = 0x29; 401 bs->id = host2uint32_t_be(0x12345678); 402 403 memcpy(bs->label, "HELENOS_NEW", 11); 404 memcpy(bs->type, "FAT ", 8); 405 } 347 406 } 348 407
Note:
See TracChangeset
for help on using the changeset viewer.