source: mainline/uspace/app/mkfat/mkfat.c@ 59b8639

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 59b8639 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
Line 
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 *
37 * Currently we can create 12/16/32-bit FAT.
38 */
39
40#include <ctype.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <stdint.h>
44#include <block.h>
45#include <mem.h>
46#include <loc.h>
47#include <byteorder.h>
48#include <inttypes.h>
49#include <errno.h>
50#include "fat.h"
51#include "fat_dentry.h"
52
53#define NAME "mkfat"
54
55#define LABEL_NONAME "NO NAME"
56
57/** Divide and round up. */
58#define div_round_up(a, b) (((a) + (b) - 1) / (b))
59
60/** Default file-system parameters */
61enum {
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 */
67};
68
69/** Configurable file-system parameters */
70typedef struct fat_cfg {
71 int fat_type; /* FAT12 = 12, FAT16 = 16, FAT32 = 32 */
72 size_t sector_size;
73 uint32_t total_sectors;
74 uint16_t root_ent_max;
75 uint32_t addt_res_sectors;
76 uint8_t sectors_per_cluster;
77
78 uint16_t reserved_sectors;
79 uint32_t rootdir_sectors;
80 uint32_t fat_sectors;
81 uint32_t total_clusters;
82 uint8_t fat_count;
83 const char *label;
84} fat_cfg_t;
85
86static void syntax_print(void);
87
88static int fat_params_compute(struct fat_cfg *cfg);
89static int fat_blocks_write(struct fat_cfg const *cfg, service_id_t service_id);
90static void fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs);
91
92int main(int argc, char **argv)
93{
94 struct fat_cfg cfg;
95
96 int rc;
97 char *dev_path;
98 service_id_t service_id;
99 char *endptr;
100 aoff64_t dev_nblocks;
101
102 cfg.sector_size = default_sector_size;
103 cfg.sectors_per_cluster = default_sectors_per_cluster;
104 cfg.fat_count = default_fat_count;
105 cfg.total_sectors = 0;
106 cfg.addt_res_sectors = 0;
107 cfg.root_ent_max = 128;
108 cfg.fat_type = FATAUTO;
109 cfg.label = NULL;
110
111 if (argc < 2) {
112 printf(NAME ": Error, argument missing.\n");
113 syntax_print();
114 return 1;
115 }
116
117 --argc; ++argv;
118
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;
136 }
137
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;
154 }
155
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 }
163
164 cfg.label = *argv;
165
166 --argc; ++argv;
167 }
168
169 if (str_cmp(*argv, "-") == 0) {
170 --argc; ++argv;
171 break;
172 }
173 }
174
175 if (argc != 1) {
176 printf(NAME ": Error, unexpected argument.\n");
177 syntax_print();
178 return 1;
179 }
180
181 dev_path = *argv;
182 printf("Device: %s\n", dev_path);
183
184 rc = loc_service_get_id(dev_path, &service_id, 0);
185 if (rc != EOK) {
186 printf(NAME ": Error resolving device `%s'.\n", dev_path);
187 return 2;
188 }
189
190 rc = block_init(service_id, 2048);
191 if (rc != EOK) {
192 printf(NAME ": Error initializing libblock.\n");
193 return 2;
194 }
195
196 rc = block_get_bsize(service_id, &cfg.sector_size);
197 if (rc != EOK) {
198 printf(NAME ": Error determining device block size.\n");
199 return 2;
200 }
201
202 rc = block_get_nblocks(service_id, &dev_nblocks);
203 if (rc != EOK) {
204 printf(NAME ": Warning, failed to obtain block device size.\n");
205 } else {
206 printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
207 dev_nblocks);
208 if (!cfg.total_sectors || dev_nblocks < cfg.total_sectors)
209 cfg.total_sectors = dev_nblocks;
210 }
211
212 if (cfg.total_sectors == 0) {
213 printf(NAME ": Error. You must specify filesystem size.\n");
214 return 1;
215 }
216
217 if (cfg.fat_type != FATAUTO && cfg.fat_type != FAT12 && cfg.fat_type != FAT16 &&
218 cfg.fat_type != FAT32) {
219 printf(NAME ": Error. Unknown FAT type.\n");
220 return 2;
221 }
222
223 printf(NAME ": Creating FAT filesystem on device %s.\n", dev_path);
224
225 rc = fat_params_compute(&cfg);
226 if (rc != EOK) {
227 printf(NAME ": Invalid file-system parameters.\n");
228 return 2;
229 }
230
231 printf(NAME ": Filesystem type FAT%d.\n", cfg.fat_type);
232
233 rc = fat_blocks_write(&cfg, service_id);
234 if (rc != EOK) {
235 printf(NAME ": Error writing device.\n");
236 return 2;
237 }
238
239 block_fini(service_id);
240 printf("Success.\n");
241
242 return 0;
243}
244
245static void syntax_print(void)
246{
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");
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;
279}
280
281/** Derive sizes of different filesystem structures.
282 *
283 * This function concentrates all the different computations of FAT
284 * file system params.
285 */
286static int fat_params_compute(struct fat_cfg *cfg)
287{
288 uint32_t fat_bytes;
289 uint32_t non_data_sectors_lb_16;
290 uint32_t non_data_sectors_lb;
291 uint32_t rd_sectors;
292 uint32_t tot_clust_16;
293
294 /*
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
298 * size of the data region. Also the root dir area might not
299 * need FAT entries if we decide to make a FAT32.
300 */
301
302 cfg->reserved_sectors = 1 + cfg->addt_res_sectors;
303
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,
311 cfg->sectors_per_cluster);
312
313 /* Now detect FAT type */
314 if (tot_clust_16 <= FAT12_CLST_MAX) {
315 if (cfg->fat_type == FATAUTO)
316 cfg->fat_type = FAT12;
317 else if (cfg->fat_type != FAT12)
318 return EINVAL;
319 } else if (tot_clust_16 <= FAT16_CLST_MAX) {
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 }
330
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
347 fat_bytes = div_round_up((cfg->total_clusters + 2) *
348 FAT_CLUSTER_DOUBLE_SIZE(cfg->fat_type), 2);
349 cfg->fat_sectors = div_round_up(fat_bytes, cfg->sector_size);
350
351 if (cfg->label != NULL && fat_label_encode(NULL, cfg->label) != EOK)
352 return EINVAL;
353
354 return EOK;
355}
356
357/** Create file system with the given parameters. */
358static int fat_blocks_write(struct fat_cfg const *cfg, service_id_t service_id)
359{
360 aoff64_t addr;
361 uint8_t *buffer;
362 int i;
363 uint32_t j;
364 int rc;
365 struct fat_bs bs;
366 fat_dentry_t *de;
367
368 fat_bootsec_create(cfg, &bs);
369
370 rc = block_write_direct(service_id, BS_BLOCK, 1, &bs);
371 if (rc != EOK)
372 return EIO;
373
374 addr = BS_BLOCK + 1;
375
376 buffer = calloc(cfg->sector_size, 1);
377 if (buffer == NULL)
378 return ENOMEM;
379 memset(buffer, 0, cfg->sector_size);
380
381 /* Reserved sectors */
382 for (i = 0; i < cfg->reserved_sectors - 1; ++i) {
383 rc = block_write_direct(service_id, addr, 1, buffer);
384 if (rc != EOK)
385 return EIO;
386
387 ++addr;
388 }
389
390 /* File allocation tables */
391 for (i = 0; i < cfg->fat_count; ++i) {
392 printf("Writing allocation table %d.\n", i + 1);
393
394 for (j = 0; j < cfg->fat_sectors; ++j) {
395 memset(buffer, 0, cfg->sector_size);
396 if (j == 0) {
397 buffer[0] = default_media_descriptor;
398 buffer[1] = 0xFF;
399 buffer[2] = 0xFF;
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 }
413 }
414
415 rc = block_write_direct(service_id, addr, 1, buffer);
416 if (rc != EOK)
417 return EIO;
418
419 ++addr;
420 }
421 }
422
423 /* Root directory */
424 printf("Writing root directory.\n");
425 memset(buffer, 0, cfg->sector_size);
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);
439 }
440
441 rc = block_write_direct(service_id, addr, 1, buffer);
442 if (rc != EOK)
443 return EIO;
444
445 ++addr;
446 }
447
448 free(buffer);
449
450 return EOK;
451}
452
453/** Construct boot sector with the given parameters. */
454static void fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs)
455{
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;
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 */
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 }
488
489 bs->mdesc = default_media_descriptor;
490 bs->sec_per_track = host2uint16_t_le(63);
491 bs->signature = host2uint16_t_be(0x55AA);
492 bs->headcnt = host2uint16_t_le(6);
493 bs->hidden_sec = host2uint32_t_le(0);
494
495 if (cfg->fat_type == FAT32) {
496 bs->sec_per_fat = 0;
497 bs->fat32.sectors_per_fat = host2uint32_t_le(cfg->fat_sectors);
498
499 bs->fat32.pdn = 0x80;
500 bs->fat32.ebs = 0x29;
501 bs->fat32.id = host2uint32_t_be(0x12345678);
502 bs->fat32.root_cluster = 2;
503
504 (void) fat_label_encode(&bs->fat32.label, bs_label);
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
512 (void) fat_label_encode(&bs->label, bs_label);
513 if (cfg->fat_type == FAT12)
514 memcpy(bs->type, "FAT12 ", 8);
515 else
516 memcpy(bs->type, "FAT16 ", 8);
517 }
518}
519
520/**
521 * @}
522 */
Note: See TracBrowser for help on using the repository browser.