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

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

Fix FAT size computation for auto-detected FAT32.

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