source: mainline/uspace/app/mkfat/mkfat.c@ 81b1db8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 81b1db8 was 1d6dd2a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Remove unnecessary includes from <stdio.h>.

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