Changes in uspace/app/mkfat/mkfat.c [81e20c7:15f3c3f] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/mkfat/mkfat.c
r81e20c7 r15f3c3f 35 35 * @brief Tool for creating new FAT file systems. 36 36 * 37 * Currently we can create 12/16/32-bit FAT.37 * Currently we can only create 16-bit FAT. 38 38 */ 39 39 … … 55 55 #define div_round_up(a, b) (((a) + (b) - 1) / (b)) 56 56 57 /** Defaultfile-system parameters */57 /** Predefined file-system parameters */ 58 58 enum { 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 */59 sector_size = 512, 60 sectors_per_cluster = 8, 61 fat_count = 2, 62 reserved_clusters = 2, 63 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;70 68 uint32_t total_sectors; 71 69 uint16_t root_ent_max; 72 uint32_t addt_res_sectors; 73 uint8_t sectors_per_cluster; 74 70 uint16_t addt_res_sectors; 71 } fat_cfg_t; 72 73 /** Derived file-system parameters */ 74 typedef struct fat_params { 75 struct fat_cfg cfg; 75 76 uint16_t reserved_sectors; 76 uint 32_t rootdir_sectors;77 uint16_t rootdir_sectors; 77 78 uint32_t fat_sectors; 78 uint32_t total_clusters; 79 uint8_t fat_count; 80 } fat_cfg_t; 79 uint16_t total_clusters; 80 } fat_params_t; 81 81 82 82 static void syntax_print(void); 83 83 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); 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 service_id_t service_id); 88 static void fat_bootsec_create(struct fat_params const *par, struct fat_bs *bs); 87 89 88 90 int main(int argc, char **argv) 89 91 { 92 struct fat_params par; 90 93 struct fat_cfg cfg; 91 94 … … 93 96 char *dev_path; 94 97 service_id_t service_id; 98 size_t block_size; 95 99 char *endptr; 96 100 aoff64_t dev_nblocks; 97 101 98 cfg.sector_size = default_sector_size;99 cfg.sectors_per_cluster = default_sectors_per_cluster;100 cfg.fat_count = default_fat_count;101 102 cfg.total_sectors = 0; 102 103 cfg.addt_res_sectors = 0; 103 104 cfg.root_ent_max = 128; 104 cfg.fat_type = FAT16;105 105 106 106 if (argc < 2) { … … 111 111 112 112 --argc; ++argv; 113 113 114 if (str_cmp(*argv, "--size") == 0) { 114 115 --argc; ++argv; … … 129 130 } 130 131 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 149 132 if (argc != 1) { 150 133 printf(NAME ": Error, unexpected argument.\n"); … … 154 137 155 138 dev_path = *argv; 156 printf("Device: %s\n", dev_path);157 139 158 140 rc = loc_service_get_id(dev_path, &service_id, 0); … … 168 150 } 169 151 170 rc = block_get_bsize(service_id, & cfg.sector_size);152 rc = block_get_bsize(service_id, &block_size); 171 153 if (rc != EOK) { 172 154 printf(NAME ": Error determining device block size.\n"); … … 180 162 printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n", 181 163 dev_nblocks); 182 if (!cfg.total_sectors || dev_nblocks < cfg.total_sectors) 183 cfg.total_sectors = 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; 184 170 } 185 171 … … 189 175 } 190 176 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); 177 printf(NAME ": Creating FAT filesystem on device %s.\n", dev_path); 178 179 rc = fat_params_compute(&cfg, &par); 199 180 if (rc != EOK) { 200 181 printf(NAME ": Invalid file-system parameters.\n"); … … 202 183 } 203 184 204 rc = fat_blocks_write(& cfg, service_id);185 rc = fat_blocks_write(&par, service_id); 205 186 if (rc != EOK) { 206 187 printf(NAME ": Error writing device.\n"); … … 216 197 static void syntax_print(void) 217 198 { 218 printf("syntax: mkfat [--size < sectors>] [--type 12|16|32] <device_name>\n");199 printf("syntax: mkfat [--size <num_blocks>] <device_name>\n"); 219 200 } 220 201 … … 224 205 * file system params. 225 206 */ 226 static int fat_params_compute(struct fat_cfg *cfg)207 static int fat_params_compute(struct fat_cfg const *cfg, struct fat_params *par) 227 208 { 228 209 uint32_t fat_bytes; … … 230 211 231 212 /* 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 = (cfg->total_clusters + 2) * FAT_SIZE(cfg->fat_type); 256 cfg->fat_sectors = div_round_up(fat_bytes, cfg->sector_size); 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; 257 231 258 232 return EOK; … … 260 234 261 235 /** Create file system with the given parameters. */ 262 static int fat_blocks_write(struct fat_ cfg const *cfg, service_id_t service_id)236 static int fat_blocks_write(struct fat_params const *par, service_id_t service_id) 263 237 { 264 238 aoff64_t addr; … … 269 243 struct fat_bs bs; 270 244 271 fat_bootsec_create( cfg, &bs);245 fat_bootsec_create(par, &bs); 272 246 273 247 rc = block_write_direct(service_id, BS_BLOCK, 1, &bs); … … 277 251 addr = BS_BLOCK + 1; 278 252 279 buffer = calloc( cfg->sector_size, 1);253 buffer = calloc(sector_size, 1); 280 254 if (buffer == NULL) 281 255 return ENOMEM; 282 memset(buffer, 0, cfg->sector_size);283 256 284 257 /* Reserved sectors */ 285 for (i = 0; i < cfg->reserved_sectors - 1; ++i) {258 for (i = 0; i < par->reserved_sectors - 1; ++i) { 286 259 rc = block_write_direct(service_id, addr, 1, buffer); 287 260 if (rc != EOK) … … 292 265 293 266 /* File allocation tables */ 294 for (i = 0; i < cfg->fat_count; ++i) {267 for (i = 0; i < fat_count; ++i) { 295 268 printf("Writing allocation table %d.\n", i + 1); 296 269 297 for (j = 0; j < cfg->fat_sectors; ++j) {298 memset(buffer, 0, cfg->sector_size);270 for (j = 0; j < par->fat_sectors; ++j) { 271 memset(buffer, 0, sector_size); 299 272 if (j == 0) { 300 buffer[0] = default_media_descriptor;273 buffer[0] = media_descriptor; 301 274 buffer[1] = 0xFF; 302 275 buffer[2] = 0xFF; 303 if (cfg->fat_type == FAT16) { 304 buffer[3] = 0xFF; 305 } else if (cfg->fat_type == FAT32) { 306 buffer[3] = 0x0F; 307 buffer[4] = 0xFF; 308 buffer[5] = 0xFF; 309 buffer[6] = 0xFF; 310 buffer[7] = 0x0F; 311 buffer[8] = 0xF8; 312 buffer[9] = 0xFF; 313 buffer[10] = 0xFF; 314 buffer[11] = 0x0F; 315 } 276 buffer[3] = 0xFF; 316 277 } 317 278 … … 324 285 } 325 286 287 printf("Writing root directory.\n"); 288 289 memset(buffer, 0, sector_size); 290 326 291 /* Root directory */ 327 printf("Writing root directory.\n"); 328 memset(buffer, 0, cfg->sector_size); 329 if (cfg->fat_type != FAT32) { 330 size_t idx; 331 for (idx = 0; idx < cfg->rootdir_sectors; ++idx) { 332 rc = block_write_direct(service_id, addr, 1, buffer); 333 if (rc != EOK) 334 return EIO; 335 336 ++addr; 337 } 338 } else { 339 for (i = 0; i < cfg->sectors_per_cluster; i++) { 340 rc = block_write_direct(service_id, addr, 1, buffer); 341 if (rc != EOK) 342 return EIO; 343 344 ++addr; 345 } 292 for (i = 0; i < par->rootdir_sectors; ++i) { 293 rc = block_write_direct(service_id, addr, 1, buffer); 294 if (rc != EOK) 295 return EIO; 296 297 ++addr; 346 298 } 347 299 … … 352 304 353 305 /** Construct boot sector with the given parameters. */ 354 static void fat_bootsec_create(struct fat_ cfg const *cfg, struct fat_bs *bs)306 static void fat_bootsec_create(struct fat_params const *par, struct fat_bs *bs) 355 307 { 356 308 memset(bs, 0, sizeof(*bs)); … … 363 315 364 316 /* BIOS Parameter Block */ 365 bs->bps = host2uint16_t_le(cfg->sector_size); 366 bs->spc = cfg->sectors_per_cluster; 367 bs->rscnt = host2uint16_t_le(cfg->reserved_sectors); 368 bs->fatcnt = cfg->fat_count; 369 bs->root_ent_max = host2uint16_t_le(cfg->root_ent_max); 370 371 if (cfg->total_sectors < 0x10000) { 372 bs->totsec16 = host2uint16_t_le(cfg->total_sectors); 373 bs->totsec32 = 0; 374 } else { 375 bs->totsec16 = 0; 376 bs->totsec32 = host2uint32_t_le(cfg->total_sectors); 377 } 378 379 bs->mdesc = default_media_descriptor; 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); 380 330 bs->sec_per_track = host2uint16_t_le(63); 381 bs->signature = host2uint16_t_be(0x55AA);382 331 bs->headcnt = host2uint16_t_le(6); 383 332 bs->hidden_sec = host2uint32_t_le(0); 384 333 385 if (cfg->fat_type == FAT32) { 386 bs->sec_per_fat = 0; 387 bs->fat32.sectors_per_fat = host2uint32_t_le(cfg->fat_sectors); 388 389 bs->fat32.pdn = 0x80; 390 bs->fat32.ebs = 0x29; 391 bs->fat32.id = host2uint32_t_be(0x12345678); 392 bs->fat32.root_cluster = 2; 393 394 memcpy(bs->fat32.label, "HELENOS_NEW", 11); 395 memcpy(bs->fat32.type, "FAT32 ", 8); 396 } else { 397 bs->sec_per_fat = host2uint16_t_le(cfg->fat_sectors); 398 bs->pdn = 0x80; 399 bs->ebs = 0x29; 400 bs->id = host2uint32_t_be(0x12345678); 401 402 memcpy(bs->label, "HELENOS_NEW", 11); 403 memcpy(bs->type, "FAT ", 8); 404 } 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); 405 347 } 406 348
Note:
See TracChangeset
for help on using the changeset viewer.