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

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

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

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