source: mainline/uspace/app/mkfat/mkfat.c@ a927398

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a927398 was a927398, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Fix FAT size computation for auto-detected FAT32.

  • Property mode set to 100644
File size: 12.3 KB
RevLine 
[dccf721]1/*
2 * Copyright (c) 2010 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup fs
30 * @{
31 */
32
33/**
34 * @file mkfat.c
35 * @brief Tool for creating new FAT file systems.
36 *
[602c3d8c]37 * Currently we can create 12/16/32-bit FAT.
[dccf721]38 */
39
[f4ae95a]40#include <ctype.h>
[dccf721]41#include <stdio.h>
42#include <stdlib.h>
[8d2dd7f2]43#include <stdint.h>
[f73b291]44#include <block.h>
[dccf721]45#include <mem.h>
[15f3c3f]46#include <loc.h>
[dccf721]47#include <byteorder.h>
[fb6f1a5]48#include <inttypes.h>
[dccf721]49#include <errno.h>
50#include "fat.h"
[f4ae95a]51#include "fat_dentry.h"
[dccf721]52
53#define NAME "mkfat"
54
[f4ae95a]55#define LABEL_NONAME "NO NAME"
56
[dccf721]57/** Divide and round up. */
58#define div_round_up(a, b) (((a) + (b) - 1) / (b))
59
[602c3d8c]60/** Default file-system parameters */
[dccf721]61enum {
[602c3d8c]62 default_sector_size = 512,
63 default_sectors_per_cluster = 4,
64 default_fat_count = 2,
65 default_reserved_clusters = 2,
66 default_media_descriptor = 0xF8 /**< fixed disk */
[dccf721]67};
68
69/** Configurable file-system parameters */
70typedef struct fat_cfg {
[602c3d8c]71 int fat_type; /* FAT12 = 12, FAT16 = 16, FAT32 = 32 */
72 size_t sector_size;
[dccf721]73 uint32_t total_sectors;
74 uint16_t root_ent_max;
[602c3d8c]75 uint32_t addt_res_sectors;
76 uint8_t sectors_per_cluster;
[dccf721]77
78 uint16_t reserved_sectors;
[602c3d8c]79 uint32_t rootdir_sectors;
[dccf721]80 uint32_t fat_sectors;
[602c3d8c]81 uint32_t total_clusters;
82 uint8_t fat_count;
[f4ae95a]83 const char *label;
[602c3d8c]84} fat_cfg_t;
[dccf721]85
[9245413]86static void syntax_print(void);
87
[602c3d8c]88static int fat_params_compute(struct fat_cfg *cfg);
[375ab5e]89static int fat_blocks_write(struct fat_cfg const *cfg, service_id_t service_id);
[602c3d8c]90static void fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs);
[dccf721]91
92int main(int argc, char **argv)
93{
94 struct fat_cfg cfg;
95
96 int rc;
97 char *dev_path;
[15f3c3f]98 service_id_t service_id;
[dccf721]99 char *endptr;
[ed903174]100 aoff64_t dev_nblocks;
[dccf721]101
[602c3d8c]102 cfg.sector_size = default_sector_size;
103 cfg.sectors_per_cluster = default_sectors_per_cluster;
104 cfg.fat_count = default_fat_count;
[dccf721]105 cfg.total_sectors = 0;
106 cfg.addt_res_sectors = 0;
107 cfg.root_ent_max = 128;
[ed9bf14]108 cfg.fat_type = FATAUTO;
[f4ae95a]109 cfg.label = NULL;
[dccf721]110
111 if (argc < 2) {
[9245413]112 printf(NAME ": Error, argument missing.\n");
113 syntax_print();
[dccf721]114 return 1;
115 }
116
117 --argc; ++argv;
118 if (str_cmp(*argv, "--size") == 0) {
119 --argc; ++argv;
120 if (*argv == NULL) {
[9245413]121 printf(NAME ": Error, argument missing.\n");
122 syntax_print();
[dccf721]123 return 1;
124 }
125
126 cfg.total_sectors = strtol(*argv, &endptr, 10);
127 if (*endptr != '\0') {
[9245413]128 printf(NAME ": Error, invalid argument.\n");
129 syntax_print();
[dccf721]130 return 1;
131 }
132
133 --argc; ++argv;
134 }
135
[602c3d8c]136 if (str_cmp(*argv, "--type") == 0) {
137 --argc; ++argv;
138 if (*argv == NULL) {
139 printf(NAME ": Error, argument missing.\n");
140 syntax_print();
141 return 1;
142 }
143
144 cfg.fat_type = strtol(*argv, &endptr, 10);
145 if (*endptr != '\0') {
146 printf(NAME ": Error, invalid argument.\n");
147 syntax_print();
148 return 1;
149 }
150
151 --argc; ++argv;
152 }
153
[f4ae95a]154 if (str_cmp(*argv, "--label") == 0) {
155 --argc; ++argv;
156 if (*argv == NULL) {
157 printf(NAME ": Error, argument missing.\n");
158 syntax_print();
159 return 1;
160 }
161
162 cfg.label = *argv;
163
164 --argc; ++argv;
165 }
166
[dccf721]167 if (argc != 1) {
[9245413]168 printf(NAME ": Error, unexpected argument.\n");
169 syntax_print();
[dccf721]170 return 1;
171 }
172
173 dev_path = *argv;
[602c3d8c]174 printf("Device: %s\n", dev_path);
[dccf721]175
[15f3c3f]176 rc = loc_service_get_id(dev_path, &service_id, 0);
[dccf721]177 if (rc != EOK) {
[9245413]178 printf(NAME ": Error resolving device `%s'.\n", dev_path);
[dccf721]179 return 2;
180 }
181
[fc22069]182 rc = block_init(service_id, 2048);
[dccf721]183 if (rc != EOK) {
184 printf(NAME ": Error initializing libblock.\n");
185 return 2;
186 }
187
[375ab5e]188 rc = block_get_bsize(service_id, &cfg.sector_size);
[dccf721]189 if (rc != EOK) {
190 printf(NAME ": Error determining device block size.\n");
191 return 2;
192 }
193
[15f3c3f]194 rc = block_get_nblocks(service_id, &dev_nblocks);
[08232ee]195 if (rc != EOK) {
196 printf(NAME ": Warning, failed to obtain block device size.\n");
197 } else {
[ed903174]198 printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
[1ccafee]199 dev_nblocks);
[81e20c7]200 if (!cfg.total_sectors || dev_nblocks < cfg.total_sectors)
[c80100f]201 cfg.total_sectors = dev_nblocks;
[08232ee]202 }
203
[dccf721]204 if (cfg.total_sectors == 0) {
205 printf(NAME ": Error. You must specify filesystem size.\n");
206 return 1;
207 }
208
[ed9bf14]209 if (cfg.fat_type != FATAUTO && cfg.fat_type != FAT12 && cfg.fat_type != FAT16 &&
210 cfg.fat_type != FAT32) {
[462b418]211 printf(NAME ": Error. Unknown FAT type.\n");
212 return 2;
213 }
214
[a927398]215 printf(NAME ": Creating FAT filesystem on device %s.\n", dev_path);
[dccf721]216
[602c3d8c]217 rc = fat_params_compute(&cfg);
[dccf721]218 if (rc != EOK) {
219 printf(NAME ": Invalid file-system parameters.\n");
220 return 2;
221 }
222
[a927398]223 printf(NAME ": Filesystem type FAT%d.\n", cfg.fat_type);
224
[375ab5e]225 rc = fat_blocks_write(&cfg, service_id);
[dccf721]226 if (rc != EOK) {
227 printf(NAME ": Error writing device.\n");
228 return 2;
229 }
230
[15f3c3f]231 block_fini(service_id);
[dccf721]232 printf("Success.\n");
233
234 return 0;
235}
236
[9245413]237static void syntax_print(void)
238{
[f4ae95a]239 printf("syntax: mkfat [--size <sectors>] [--type 12|16|32] "
240 "[--label <label>] <device_name>\n");
241}
242
243static int fat_label_encode(void *dest, const char *src)
244{
245 int i;
246 const char *sp;
247 uint8_t *dp;
248
249 i = 0;
250 sp = src;
251 dp = (uint8_t *)dest;
252
253 while (*sp != '\0' && i < FAT_VOLLABEL_LEN) {
254 if (!ascii_check(*sp))
255 return EINVAL;
256 if (dp != NULL)
257 dp[i] = toupper(*sp);
258 ++i; ++sp;
259 }
260
261 while (i < FAT_VOLLABEL_LEN) {
262 if (dp != NULL)
263 dp[i] = FAT_PAD;
264 ++i;
265 }
266
267 return EOK;
[9245413]268}
269
[dccf721]270/** Derive sizes of different filesystem structures.
271 *
272 * This function concentrates all the different computations of FAT
273 * file system params.
274 */
[602c3d8c]275static int fat_params_compute(struct fat_cfg *cfg)
[dccf721]276{
277 uint32_t fat_bytes;
[a927398]278 uint32_t non_data_sectors_lb_16;
[dccf721]279 uint32_t non_data_sectors_lb;
[a927398]280 uint32_t rd_sectors;
281 uint32_t tot_clust_16;
[dccf721]282
283 /*
[26ad20b]284 * Make a conservative guess on the FAT size needed for the file
285 * system. The optimum could be potentially smaller since we
286 * do not subtract size of the FAT itself when computing the
[a927398]287 * size of the data region. Also the root dir area might not
288 * need FAT entries if we decide to make a FAT32.
[26ad20b]289 */
[602c3d8c]290
291 cfg->reserved_sectors = 1 + cfg->addt_res_sectors;
[dccf721]292
[a927398]293 /* Only correct for FAT12/16 (FAT32 has root dir stored in clusters */
294 rd_sectors = div_round_up(cfg->root_ent_max * DIRENT_SIZE,
295 cfg->sector_size);
296 non_data_sectors_lb_16 = cfg->reserved_sectors + rd_sectors;
297
298 /* Only correct for FAT12/16 */
299 tot_clust_16 = div_round_up(cfg->total_sectors - non_data_sectors_lb_16,
[602c3d8c]300 cfg->sectors_per_cluster);
[dccf721]301
[a927398]302 /* Now detect FAT type */
303 if (tot_clust_16 <= FAT12_CLST_MAX) {
[ed9bf14]304 if (cfg->fat_type == FATAUTO)
305 cfg->fat_type = FAT12;
306 else if (cfg->fat_type != FAT12)
307 return EINVAL;
[a927398]308 } else if (tot_clust_16 <= FAT16_CLST_MAX) {
[ed9bf14]309 if (cfg->fat_type == FATAUTO)
310 cfg->fat_type = FAT16;
311 else if (cfg->fat_type != FAT16)
312 return EINVAL;
313 } else {
314 if (cfg->fat_type == FATAUTO)
315 cfg->fat_type = FAT32;
316 else if (cfg->fat_type != FAT32)
317 return EINVAL;
318 }
[dccf721]319
[a927398]320 /* Actual root directory size, non-data sectors */
321 if (cfg->fat_type != FAT32) {
322 cfg->rootdir_sectors = div_round_up(cfg->root_ent_max * DIRENT_SIZE,
323 cfg->sector_size);
324 non_data_sectors_lb = cfg->reserved_sectors + cfg->rootdir_sectors;
325
326 } else {
327 /* We create a single-cluster root dir */
328 cfg->rootdir_sectors = cfg->sectors_per_cluster;
329 non_data_sectors_lb = cfg->reserved_sectors;
330 }
331
332 /* Actual total number of clusters */
333 cfg->total_clusters = div_round_up(cfg->total_sectors - non_data_sectors_lb,
334 cfg->sectors_per_cluster);
335
[51d0ee9]336 fat_bytes = div_round_up((cfg->total_clusters + 2) *
337 FAT_CLUSTER_DOUBLE_SIZE(cfg->fat_type), 2);
[602c3d8c]338 cfg->fat_sectors = div_round_up(fat_bytes, cfg->sector_size);
[dccf721]339
[f4ae95a]340 if (cfg->label != NULL && fat_label_encode(NULL, cfg->label) != EOK)
341 return EINVAL;
342
[dccf721]343 return EOK;
344}
345
346/** Create file system with the given parameters. */
[375ab5e]347static int fat_blocks_write(struct fat_cfg const *cfg, service_id_t service_id)
[dccf721]348{
[ed903174]349 aoff64_t addr;
[dccf721]350 uint8_t *buffer;
351 int i;
352 uint32_t j;
353 int rc;
354 struct fat_bs bs;
[f4ae95a]355 fat_dentry_t *de;
[dccf721]356
[602c3d8c]357 fat_bootsec_create(cfg, &bs);
[dccf721]358
[15f3c3f]359 rc = block_write_direct(service_id, BS_BLOCK, 1, &bs);
[dccf721]360 if (rc != EOK)
361 return EIO;
362
363 addr = BS_BLOCK + 1;
364
[602c3d8c]365 buffer = calloc(cfg->sector_size, 1);
[dccf721]366 if (buffer == NULL)
367 return ENOMEM;
[602c3d8c]368 memset(buffer, 0, cfg->sector_size);
[dccf721]369
370 /* Reserved sectors */
[602c3d8c]371 for (i = 0; i < cfg->reserved_sectors - 1; ++i) {
[15f3c3f]372 rc = block_write_direct(service_id, addr, 1, buffer);
[dccf721]373 if (rc != EOK)
374 return EIO;
375
376 ++addr;
377 }
378
379 /* File allocation tables */
[602c3d8c]380 for (i = 0; i < cfg->fat_count; ++i) {
[dccf721]381 printf("Writing allocation table %d.\n", i + 1);
382
[602c3d8c]383 for (j = 0; j < cfg->fat_sectors; ++j) {
384 memset(buffer, 0, cfg->sector_size);
[dccf721]385 if (j == 0) {
[602c3d8c]386 buffer[0] = default_media_descriptor;
[dccf721]387 buffer[1] = 0xFF;
388 buffer[2] = 0xFF;
[602c3d8c]389 if (cfg->fat_type == FAT16) {
390 buffer[3] = 0xFF;
391 } else if (cfg->fat_type == FAT32) {
392 buffer[3] = 0x0F;
393 buffer[4] = 0xFF;
394 buffer[5] = 0xFF;
395 buffer[6] = 0xFF;
396 buffer[7] = 0x0F;
397 buffer[8] = 0xF8;
398 buffer[9] = 0xFF;
399 buffer[10] = 0xFF;
400 buffer[11] = 0x0F;
401 }
[dccf721]402 }
403
[15f3c3f]404 rc = block_write_direct(service_id, addr, 1, buffer);
[dccf721]405 if (rc != EOK)
406 return EIO;
407
408 ++addr;
409 }
410 }
411
[602c3d8c]412 /* Root directory */
[dccf721]413 printf("Writing root directory.\n");
[602c3d8c]414 memset(buffer, 0, cfg->sector_size);
[f4ae95a]415 de = (fat_dentry_t *)buffer;
416 size_t idx;
417 for (idx = 0; idx < cfg->rootdir_sectors; ++idx) {
418
419 if (idx == 0 && cfg->label != NULL) {
420 /* Set up volume label entry */
421 (void) fat_label_encode(&de->name, cfg->label);
422 de->attr = FAT_ATTR_VOLLABEL;
423 de->mtime = 0x1234;
424 de->mdate = 0x1234;
425 } else if (idx == 1) {
426 /* Clear volume label entry */
427 memset(buffer, 0, cfg->sector_size);
[602c3d8c]428 }
[dccf721]429
[f4ae95a]430 rc = block_write_direct(service_id, addr, 1, buffer);
431 if (rc != EOK)
432 return EIO;
433
434 ++addr;
[dccf721]435 }
436
437 free(buffer);
438
439 return EOK;
440}
441
442/** Construct boot sector with the given parameters. */
[602c3d8c]443static void fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs)
[dccf721]444{
[f4ae95a]445 const char *bs_label;
446
447 /*
448 * The boot sector must always contain a valid label. If there
449 * is no label, there should be 'NO NAME'
450 */
451 if (cfg->label != NULL)
452 bs_label = cfg->label;
453 else
454 bs_label = LABEL_NONAME;
[dccf721]455 memset(bs, 0, sizeof(*bs));
456
457 bs->ji[0] = 0xEB;
458 bs->ji[1] = 0x3C;
459 bs->ji[2] = 0x90;
460
461 memcpy(bs->oem_name, "HELENOS ", 8);
462
463 /* BIOS Parameter Block */
[602c3d8c]464 bs->bps = host2uint16_t_le(cfg->sector_size);
465 bs->spc = cfg->sectors_per_cluster;
466 bs->rscnt = host2uint16_t_le(cfg->reserved_sectors);
467 bs->fatcnt = cfg->fat_count;
468 bs->root_ent_max = host2uint16_t_le(cfg->root_ent_max);
469
470 if (cfg->total_sectors < 0x10000) {
471 bs->totsec16 = host2uint16_t_le(cfg->total_sectors);
472 bs->totsec32 = 0;
473 } else {
474 bs->totsec16 = 0;
475 bs->totsec32 = host2uint32_t_le(cfg->total_sectors);
476 }
[dccf721]477
[602c3d8c]478 bs->mdesc = default_media_descriptor;
[dccf721]479 bs->sec_per_track = host2uint16_t_le(63);
[602c3d8c]480 bs->signature = host2uint16_t_be(0x55AA);
[dccf721]481 bs->headcnt = host2uint16_t_le(6);
482 bs->hidden_sec = host2uint32_t_le(0);
483
[602c3d8c]484 if (cfg->fat_type == FAT32) {
485 bs->sec_per_fat = 0;
486 bs->fat32.sectors_per_fat = host2uint32_t_le(cfg->fat_sectors);
[dccf721]487
[602c3d8c]488 bs->fat32.pdn = 0x80;
489 bs->fat32.ebs = 0x29;
490 bs->fat32.id = host2uint32_t_be(0x12345678);
491 bs->fat32.root_cluster = 2;
[dccf721]492
[f4ae95a]493 (void) fat_label_encode(&bs->fat32.label, bs_label);
[602c3d8c]494 memcpy(bs->fat32.type, "FAT32 ", 8);
495 } else {
496 bs->sec_per_fat = host2uint16_t_le(cfg->fat_sectors);
497 bs->pdn = 0x80;
498 bs->ebs = 0x29;
499 bs->id = host2uint32_t_be(0x12345678);
500
[f4ae95a]501 (void) fat_label_encode(&bs->label, bs_label);
[602c3d8c]502 memcpy(bs->type, "FAT ", 8);
503 }
[dccf721]504}
505
506/**
507 * @}
508 */
Note: See TracBrowser for help on using the repository browser.