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

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

Improve mkfat option parsing.

  • Property mode set to 100644
File size: 12.6 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
[2456fd0]119 while (*argv[0] == '-') {
120 if (str_cmp(*argv, "--size") == 0) {
121 --argc; ++argv;
122 if (*argv == NULL) {
123 printf(NAME ": Error, argument missing.\n");
124 syntax_print();
125 return 1;
126 }
127
128 cfg.total_sectors = strtol(*argv, &endptr, 10);
129 if (*endptr != '\0') {
130 printf(NAME ": Error, invalid argument.\n");
131 syntax_print();
132 return 1;
133 }
134
135 --argc; ++argv;
[dccf721]136 }
137
[2456fd0]138 if (str_cmp(*argv, "--type") == 0) {
139 --argc; ++argv;
140 if (*argv == NULL) {
141 printf(NAME ": Error, argument missing.\n");
142 syntax_print();
143 return 1;
144 }
145
146 cfg.fat_type = strtol(*argv, &endptr, 10);
147 if (*endptr != '\0') {
148 printf(NAME ": Error, invalid argument.\n");
149 syntax_print();
150 return 1;
151 }
152
153 --argc; ++argv;
[602c3d8c]154 }
155
[2456fd0]156 if (str_cmp(*argv, "--label") == 0) {
157 --argc; ++argv;
158 if (*argv == NULL) {
159 printf(NAME ": Error, argument missing.\n");
160 syntax_print();
161 return 1;
162 }
[602c3d8c]163
[2456fd0]164 cfg.label = *argv;
[602c3d8c]165
[2456fd0]166 --argc; ++argv;
[f4ae95a]167 }
168
[2456fd0]169 if (str_cmp(*argv, "-") == 0) {
170 --argc; ++argv;
171 break;
172 }
[f4ae95a]173 }
174
[dccf721]175 if (argc != 1) {
[9245413]176 printf(NAME ": Error, unexpected argument.\n");
177 syntax_print();
[dccf721]178 return 1;
179 }
180
181 dev_path = *argv;
[602c3d8c]182 printf("Device: %s\n", dev_path);
[dccf721]183
[15f3c3f]184 rc = loc_service_get_id(dev_path, &service_id, 0);
[dccf721]185 if (rc != EOK) {
[9245413]186 printf(NAME ": Error resolving device `%s'.\n", dev_path);
[dccf721]187 return 2;
188 }
189
[fc22069]190 rc = block_init(service_id, 2048);
[dccf721]191 if (rc != EOK) {
192 printf(NAME ": Error initializing libblock.\n");
193 return 2;
194 }
195
[375ab5e]196 rc = block_get_bsize(service_id, &cfg.sector_size);
[dccf721]197 if (rc != EOK) {
198 printf(NAME ": Error determining device block size.\n");
199 return 2;
200 }
201
[15f3c3f]202 rc = block_get_nblocks(service_id, &dev_nblocks);
[08232ee]203 if (rc != EOK) {
204 printf(NAME ": Warning, failed to obtain block device size.\n");
205 } else {
[ed903174]206 printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
[1ccafee]207 dev_nblocks);
[81e20c7]208 if (!cfg.total_sectors || dev_nblocks < cfg.total_sectors)
[c80100f]209 cfg.total_sectors = dev_nblocks;
[08232ee]210 }
211
[dccf721]212 if (cfg.total_sectors == 0) {
213 printf(NAME ": Error. You must specify filesystem size.\n");
214 return 1;
215 }
216
[ed9bf14]217 if (cfg.fat_type != FATAUTO && cfg.fat_type != FAT12 && cfg.fat_type != FAT16 &&
218 cfg.fat_type != FAT32) {
[462b418]219 printf(NAME ": Error. Unknown FAT type.\n");
220 return 2;
221 }
222
[a927398]223 printf(NAME ": Creating FAT filesystem on device %s.\n", dev_path);
[dccf721]224
[602c3d8c]225 rc = fat_params_compute(&cfg);
[dccf721]226 if (rc != EOK) {
227 printf(NAME ": Invalid file-system parameters.\n");
228 return 2;
229 }
230
[a927398]231 printf(NAME ": Filesystem type FAT%d.\n", cfg.fat_type);
232
[375ab5e]233 rc = fat_blocks_write(&cfg, service_id);
[dccf721]234 if (rc != EOK) {
235 printf(NAME ": Error writing device.\n");
236 return 2;
237 }
238
[15f3c3f]239 block_fini(service_id);
[dccf721]240 printf("Success.\n");
241
242 return 0;
243}
244
[9245413]245static void syntax_print(void)
246{
[2456fd0]247 printf("syntax: mkfat [<options>...] <device_name>\n");
248 printf("options:\n"
249 "\t--size <sectors> Filesystem size, overrides device size\n"
250 "\t--type 12|16|32 FAT type (auto-detected by default)\n"
251 "\t--label <label> Volume label\n");
[f4ae95a]252}
253
254static int fat_label_encode(void *dest, const char *src)
255{
256 int i;
257 const char *sp;
258 uint8_t *dp;
259
260 i = 0;
261 sp = src;
262 dp = (uint8_t *)dest;
263
264 while (*sp != '\0' && i < FAT_VOLLABEL_LEN) {
265 if (!ascii_check(*sp))
266 return EINVAL;
267 if (dp != NULL)
268 dp[i] = toupper(*sp);
269 ++i; ++sp;
270 }
271
272 while (i < FAT_VOLLABEL_LEN) {
273 if (dp != NULL)
274 dp[i] = FAT_PAD;
275 ++i;
276 }
277
278 return EOK;
[9245413]279}
280
[dccf721]281/** Derive sizes of different filesystem structures.
282 *
283 * This function concentrates all the different computations of FAT
284 * file system params.
285 */
[602c3d8c]286static int fat_params_compute(struct fat_cfg *cfg)
[dccf721]287{
288 uint32_t fat_bytes;
[a927398]289 uint32_t non_data_sectors_lb_16;
[dccf721]290 uint32_t non_data_sectors_lb;
[a927398]291 uint32_t rd_sectors;
292 uint32_t tot_clust_16;
[dccf721]293
294 /*
[26ad20b]295 * Make a conservative guess on the FAT size needed for the file
296 * system. The optimum could be potentially smaller since we
297 * do not subtract size of the FAT itself when computing the
[a927398]298 * size of the data region. Also the root dir area might not
299 * need FAT entries if we decide to make a FAT32.
[26ad20b]300 */
[602c3d8c]301
302 cfg->reserved_sectors = 1 + cfg->addt_res_sectors;
[dccf721]303
[a927398]304 /* Only correct for FAT12/16 (FAT32 has root dir stored in clusters */
305 rd_sectors = div_round_up(cfg->root_ent_max * DIRENT_SIZE,
306 cfg->sector_size);
307 non_data_sectors_lb_16 = cfg->reserved_sectors + rd_sectors;
308
309 /* Only correct for FAT12/16 */
310 tot_clust_16 = div_round_up(cfg->total_sectors - non_data_sectors_lb_16,
[602c3d8c]311 cfg->sectors_per_cluster);
[dccf721]312
[a927398]313 /* Now detect FAT type */
314 if (tot_clust_16 <= FAT12_CLST_MAX) {
[ed9bf14]315 if (cfg->fat_type == FATAUTO)
316 cfg->fat_type = FAT12;
317 else if (cfg->fat_type != FAT12)
318 return EINVAL;
[a927398]319 } else if (tot_clust_16 <= FAT16_CLST_MAX) {
[ed9bf14]320 if (cfg->fat_type == FATAUTO)
321 cfg->fat_type = FAT16;
322 else if (cfg->fat_type != FAT16)
323 return EINVAL;
324 } else {
325 if (cfg->fat_type == FATAUTO)
326 cfg->fat_type = FAT32;
327 else if (cfg->fat_type != FAT32)
328 return EINVAL;
329 }
[dccf721]330
[a927398]331 /* Actual root directory size, non-data sectors */
332 if (cfg->fat_type != FAT32) {
333 cfg->rootdir_sectors = div_round_up(cfg->root_ent_max * DIRENT_SIZE,
334 cfg->sector_size);
335 non_data_sectors_lb = cfg->reserved_sectors + cfg->rootdir_sectors;
336
337 } else {
338 /* We create a single-cluster root dir */
339 cfg->rootdir_sectors = cfg->sectors_per_cluster;
340 non_data_sectors_lb = cfg->reserved_sectors;
341 }
342
343 /* Actual total number of clusters */
344 cfg->total_clusters = div_round_up(cfg->total_sectors - non_data_sectors_lb,
345 cfg->sectors_per_cluster);
346
[51d0ee9]347 fat_bytes = div_round_up((cfg->total_clusters + 2) *
348 FAT_CLUSTER_DOUBLE_SIZE(cfg->fat_type), 2);
[602c3d8c]349 cfg->fat_sectors = div_round_up(fat_bytes, cfg->sector_size);
[dccf721]350
[f4ae95a]351 if (cfg->label != NULL && fat_label_encode(NULL, cfg->label) != EOK)
352 return EINVAL;
353
[dccf721]354 return EOK;
355}
356
357/** Create file system with the given parameters. */
[375ab5e]358static int fat_blocks_write(struct fat_cfg const *cfg, service_id_t service_id)
[dccf721]359{
[ed903174]360 aoff64_t addr;
[dccf721]361 uint8_t *buffer;
362 int i;
363 uint32_t j;
364 int rc;
365 struct fat_bs bs;
[f4ae95a]366 fat_dentry_t *de;
[dccf721]367
[602c3d8c]368 fat_bootsec_create(cfg, &bs);
[dccf721]369
[15f3c3f]370 rc = block_write_direct(service_id, BS_BLOCK, 1, &bs);
[dccf721]371 if (rc != EOK)
372 return EIO;
373
374 addr = BS_BLOCK + 1;
375
[602c3d8c]376 buffer = calloc(cfg->sector_size, 1);
[dccf721]377 if (buffer == NULL)
378 return ENOMEM;
[602c3d8c]379 memset(buffer, 0, cfg->sector_size);
[dccf721]380
381 /* Reserved sectors */
[602c3d8c]382 for (i = 0; i < cfg->reserved_sectors - 1; ++i) {
[15f3c3f]383 rc = block_write_direct(service_id, addr, 1, buffer);
[dccf721]384 if (rc != EOK)
385 return EIO;
386
387 ++addr;
388 }
389
390 /* File allocation tables */
[602c3d8c]391 for (i = 0; i < cfg->fat_count; ++i) {
[dccf721]392 printf("Writing allocation table %d.\n", i + 1);
393
[602c3d8c]394 for (j = 0; j < cfg->fat_sectors; ++j) {
395 memset(buffer, 0, cfg->sector_size);
[dccf721]396 if (j == 0) {
[602c3d8c]397 buffer[0] = default_media_descriptor;
[dccf721]398 buffer[1] = 0xFF;
399 buffer[2] = 0xFF;
[602c3d8c]400 if (cfg->fat_type == FAT16) {
401 buffer[3] = 0xFF;
402 } else if (cfg->fat_type == FAT32) {
403 buffer[3] = 0x0F;
404 buffer[4] = 0xFF;
405 buffer[5] = 0xFF;
406 buffer[6] = 0xFF;
407 buffer[7] = 0x0F;
408 buffer[8] = 0xF8;
409 buffer[9] = 0xFF;
410 buffer[10] = 0xFF;
411 buffer[11] = 0x0F;
412 }
[dccf721]413 }
414
[15f3c3f]415 rc = block_write_direct(service_id, addr, 1, buffer);
[dccf721]416 if (rc != EOK)
417 return EIO;
418
419 ++addr;
420 }
421 }
422
[602c3d8c]423 /* Root directory */
[dccf721]424 printf("Writing root directory.\n");
[602c3d8c]425 memset(buffer, 0, cfg->sector_size);
[f4ae95a]426 de = (fat_dentry_t *)buffer;
427 size_t idx;
428 for (idx = 0; idx < cfg->rootdir_sectors; ++idx) {
429
430 if (idx == 0 && cfg->label != NULL) {
431 /* Set up volume label entry */
432 (void) fat_label_encode(&de->name, cfg->label);
433 de->attr = FAT_ATTR_VOLLABEL;
434 de->mtime = 0x1234;
435 de->mdate = 0x1234;
436 } else if (idx == 1) {
437 /* Clear volume label entry */
438 memset(buffer, 0, cfg->sector_size);
[602c3d8c]439 }
[dccf721]440
[f4ae95a]441 rc = block_write_direct(service_id, addr, 1, buffer);
442 if (rc != EOK)
443 return EIO;
444
445 ++addr;
[dccf721]446 }
447
448 free(buffer);
449
450 return EOK;
451}
452
453/** Construct boot sector with the given parameters. */
[602c3d8c]454static void fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs)
[dccf721]455{
[f4ae95a]456 const char *bs_label;
457
458 /*
459 * The boot sector must always contain a valid label. If there
460 * is no label, there should be 'NO NAME'
461 */
462 if (cfg->label != NULL)
463 bs_label = cfg->label;
464 else
465 bs_label = LABEL_NONAME;
[dccf721]466 memset(bs, 0, sizeof(*bs));
467
468 bs->ji[0] = 0xEB;
469 bs->ji[1] = 0x3C;
470 bs->ji[2] = 0x90;
471
472 memcpy(bs->oem_name, "HELENOS ", 8);
473
474 /* BIOS Parameter Block */
[602c3d8c]475 bs->bps = host2uint16_t_le(cfg->sector_size);
476 bs->spc = cfg->sectors_per_cluster;
477 bs->rscnt = host2uint16_t_le(cfg->reserved_sectors);
478 bs->fatcnt = cfg->fat_count;
479 bs->root_ent_max = host2uint16_t_le(cfg->root_ent_max);
480
481 if (cfg->total_sectors < 0x10000) {
482 bs->totsec16 = host2uint16_t_le(cfg->total_sectors);
483 bs->totsec32 = 0;
484 } else {
485 bs->totsec16 = 0;
486 bs->totsec32 = host2uint32_t_le(cfg->total_sectors);
487 }
[dccf721]488
[602c3d8c]489 bs->mdesc = default_media_descriptor;
[dccf721]490 bs->sec_per_track = host2uint16_t_le(63);
[602c3d8c]491 bs->signature = host2uint16_t_be(0x55AA);
[dccf721]492 bs->headcnt = host2uint16_t_le(6);
493 bs->hidden_sec = host2uint32_t_le(0);
494
[602c3d8c]495 if (cfg->fat_type == FAT32) {
496 bs->sec_per_fat = 0;
497 bs->fat32.sectors_per_fat = host2uint32_t_le(cfg->fat_sectors);
[dccf721]498
[602c3d8c]499 bs->fat32.pdn = 0x80;
500 bs->fat32.ebs = 0x29;
501 bs->fat32.id = host2uint32_t_be(0x12345678);
502 bs->fat32.root_cluster = 2;
[dccf721]503
[f4ae95a]504 (void) fat_label_encode(&bs->fat32.label, bs_label);
[602c3d8c]505 memcpy(bs->fat32.type, "FAT32 ", 8);
506 } else {
507 bs->sec_per_fat = host2uint16_t_le(cfg->fat_sectors);
508 bs->pdn = 0x80;
509 bs->ebs = 0x29;
510 bs->id = host2uint32_t_be(0x12345678);
511
[f4ae95a]512 (void) fat_label_encode(&bs->label, bs_label);
[58fa3e6]513 if (cfg->fat_type == FAT12)
514 memcpy(bs->type, "FAT12 ", 8);
515 else
516 memcpy(bs->type, "FAT16 ", 8);
[602c3d8c]517 }
[dccf721]518}
519
520/**
521 * @}
522 */
Note: See TracBrowser for help on using the repository browser.