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

Last change on this file was df1b4a8, checked in by Jiri Svoboda <jiri@…>, 12 months ago

Fix volume label entry / rootdir creation in mkfat for FAT32

With FAT32 the root directory is stored in clusters (like any
other directory). Its start address must be adjusted for
root_cluster (which is 2).

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