source: mainline/uspace/app/mkfat/mkfat.c@ 60c332e

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

Fix vertical spacing with new Ccheck revision.

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