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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d39c46e0 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
RevLine 
[dccf721]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 *
[602c3d8c]37 * Currently we can create 12/16/32-bit FAT.
[dccf721]38 */
39
[f4ae95a]40#include <ctype.h>
[dccf721]41#include <stdio.h>
42#include <stdlib.h>
[8d2dd7f2]43#include <stdint.h>
[f73b291]44#include <block.h>
[dccf721]45#include <mem.h>
[15f3c3f]46#include <loc.h>
[dccf721]47#include <byteorder.h>
[fb6f1a5]48#include <inttypes.h>
[dccf721]49#include <errno.h>
[1d6dd2a]50#include <str.h>
[dccf721]51#include "fat.h"
[f4ae95a]52#include "fat_dentry.h"
[dccf721]53
54#define NAME "mkfat"
55
[f4ae95a]56#define LABEL_NONAME "NO NAME"
57
[dccf721]58/** Divide and round up. */
59#define div_round_up(a, b) (((a) + (b) - 1) / (b))
60
[602c3d8c]61/** Default file-system parameters */
[dccf721]62enum {
[602c3d8c]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 */
[dccf721]68};
69
70/** Configurable file-system parameters */
71typedef struct fat_cfg {
[602c3d8c]72 int fat_type; /* FAT12 = 12, FAT16 = 16, FAT32 = 32 */
73 size_t sector_size;
[dccf721]74 uint32_t total_sectors;
75 uint16_t root_ent_max;
[602c3d8c]76 uint32_t addt_res_sectors;
77 uint8_t sectors_per_cluster;
[dccf721]78
79 uint16_t reserved_sectors;
[602c3d8c]80 uint32_t rootdir_sectors;
[dccf721]81 uint32_t fat_sectors;
[602c3d8c]82 uint32_t total_clusters;
83 uint8_t fat_count;
[f4ae95a]84 const char *label;
[602c3d8c]85} fat_cfg_t;
[dccf721]86
[9245413]87static void syntax_print(void);
88
[b7fd2a0]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);
[602c3d8c]91static void fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs);
[dccf721]92
93int main(int argc, char **argv)
94{
95 struct fat_cfg cfg;
96
[b7fd2a0]97 errno_t rc;
[dccf721]98 char *dev_path;
[15f3c3f]99 service_id_t service_id;
[dccf721]100 char *endptr;
[ed903174]101 aoff64_t dev_nblocks;
[dccf721]102
[602c3d8c]103 cfg.sector_size = default_sector_size;
104 cfg.sectors_per_cluster = default_sectors_per_cluster;
105 cfg.fat_count = default_fat_count;
[dccf721]106 cfg.total_sectors = 0;
107 cfg.addt_res_sectors = 0;
108 cfg.root_ent_max = 128;
[ed9bf14]109 cfg.fat_type = FATAUTO;
[f4ae95a]110 cfg.label = NULL;
[dccf721]111
112 if (argc < 2) {
[9245413]113 printf(NAME ": Error, argument missing.\n");
114 syntax_print();
[dccf721]115 return 1;
116 }
117
118 --argc; ++argv;
119
[2456fd0]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;
[dccf721]137 }
138
[2456fd0]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;
[602c3d8c]155 }
156
[2456fd0]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 }
[602c3d8c]164
[2456fd0]165 cfg.label = *argv;
[602c3d8c]166
[2456fd0]167 --argc; ++argv;
[f4ae95a]168 }
169
[2456fd0]170 if (str_cmp(*argv, "-") == 0) {
171 --argc; ++argv;
172 break;
173 }
[f4ae95a]174 }
175
[dccf721]176 if (argc != 1) {
[9245413]177 printf(NAME ": Error, unexpected argument.\n");
178 syntax_print();
[dccf721]179 return 1;
180 }
181
182 dev_path = *argv;
[602c3d8c]183 printf("Device: %s\n", dev_path);
[dccf721]184
[15f3c3f]185 rc = loc_service_get_id(dev_path, &service_id, 0);
[dccf721]186 if (rc != EOK) {
[9245413]187 printf(NAME ": Error resolving device `%s'.\n", dev_path);
[dccf721]188 return 2;
189 }
190
[fc22069]191 rc = block_init(service_id, 2048);
[dccf721]192 if (rc != EOK) {
193 printf(NAME ": Error initializing libblock.\n");
194 return 2;
195 }
196
[375ab5e]197 rc = block_get_bsize(service_id, &cfg.sector_size);
[dccf721]198 if (rc != EOK) {
199 printf(NAME ": Error determining device block size.\n");
200 return 2;
201 }
202
[15f3c3f]203 rc = block_get_nblocks(service_id, &dev_nblocks);
[08232ee]204 if (rc != EOK) {
205 printf(NAME ": Warning, failed to obtain block device size.\n");
206 } else {
[ed903174]207 printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
[1ccafee]208 dev_nblocks);
[81e20c7]209 if (!cfg.total_sectors || dev_nblocks < cfg.total_sectors)
[c80100f]210 cfg.total_sectors = dev_nblocks;
[08232ee]211 }
212
[dccf721]213 if (cfg.total_sectors == 0) {
214 printf(NAME ": Error. You must specify filesystem size.\n");
215 return 1;
216 }
217
[ed9bf14]218 if (cfg.fat_type != FATAUTO && cfg.fat_type != FAT12 && cfg.fat_type != FAT16 &&
219 cfg.fat_type != FAT32) {
[462b418]220 printf(NAME ": Error. Unknown FAT type.\n");
221 return 2;
222 }
223
[a927398]224 printf(NAME ": Creating FAT filesystem on device %s.\n", dev_path);
[dccf721]225
[602c3d8c]226 rc = fat_params_compute(&cfg);
[dccf721]227 if (rc != EOK) {
228 printf(NAME ": Invalid file-system parameters.\n");
229 return 2;
230 }
231
[a927398]232 printf(NAME ": Filesystem type FAT%d.\n", cfg.fat_type);
233
[375ab5e]234 rc = fat_blocks_write(&cfg, service_id);
[dccf721]235 if (rc != EOK) {
236 printf(NAME ": Error writing device.\n");
237 return 2;
238 }
239
[15f3c3f]240 block_fini(service_id);
[dccf721]241 printf("Success.\n");
242
243 return 0;
244}
245
[9245413]246static void syntax_print(void)
247{
[2456fd0]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");
[f4ae95a]253}
254
[b7fd2a0]255static errno_t fat_label_encode(void *dest, const char *src)
[f4ae95a]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;
[9245413]280}
281
[dccf721]282/** Derive sizes of different filesystem structures.
283 *
284 * This function concentrates all the different computations of FAT
285 * file system params.
286 */
[b7fd2a0]287static errno_t fat_params_compute(struct fat_cfg *cfg)
[dccf721]288{
289 uint32_t fat_bytes;
[a927398]290 uint32_t non_data_sectors_lb_16;
[dccf721]291 uint32_t non_data_sectors_lb;
[a927398]292 uint32_t rd_sectors;
293 uint32_t tot_clust_16;
[dccf721]294
295 /*
[26ad20b]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
[a927398]299 * size of the data region. Also the root dir area might not
300 * need FAT entries if we decide to make a FAT32.
[26ad20b]301 */
[602c3d8c]302
303 cfg->reserved_sectors = 1 + cfg->addt_res_sectors;
[dccf721]304
[a927398]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,
[602c3d8c]312 cfg->sectors_per_cluster);
[dccf721]313
[a927398]314 /* Now detect FAT type */
315 if (tot_clust_16 <= FAT12_CLST_MAX) {
[ed9bf14]316 if (cfg->fat_type == FATAUTO)
317 cfg->fat_type = FAT12;
318 else if (cfg->fat_type != FAT12)
319 return EINVAL;
[a927398]320 } else if (tot_clust_16 <= FAT16_CLST_MAX) {
[ed9bf14]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 }
[dccf721]331
[a927398]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
[51d0ee9]348 fat_bytes = div_round_up((cfg->total_clusters + 2) *
349 FAT_CLUSTER_DOUBLE_SIZE(cfg->fat_type), 2);
[602c3d8c]350 cfg->fat_sectors = div_round_up(fat_bytes, cfg->sector_size);
[dccf721]351
[f4ae95a]352 if (cfg->label != NULL && fat_label_encode(NULL, cfg->label) != EOK)
353 return EINVAL;
354
[dccf721]355 return EOK;
356}
357
358/** Create file system with the given parameters. */
[b7fd2a0]359static errno_t fat_blocks_write(struct fat_cfg const *cfg, service_id_t service_id)
[dccf721]360{
[ed903174]361 aoff64_t addr;
[dccf721]362 uint8_t *buffer;
363 int i;
364 uint32_t j;
[b7fd2a0]365 errno_t rc;
[dccf721]366 struct fat_bs bs;
[f4ae95a]367 fat_dentry_t *de;
[dccf721]368
[602c3d8c]369 fat_bootsec_create(cfg, &bs);
[dccf721]370
[15f3c3f]371 rc = block_write_direct(service_id, BS_BLOCK, 1, &bs);
[dccf721]372 if (rc != EOK)
373 return EIO;
374
375 addr = BS_BLOCK + 1;
376
[602c3d8c]377 buffer = calloc(cfg->sector_size, 1);
[dccf721]378 if (buffer == NULL)
379 return ENOMEM;
[602c3d8c]380 memset(buffer, 0, cfg->sector_size);
[dccf721]381
382 /* Reserved sectors */
[602c3d8c]383 for (i = 0; i < cfg->reserved_sectors - 1; ++i) {
[15f3c3f]384 rc = block_write_direct(service_id, addr, 1, buffer);
[dccf721]385 if (rc != EOK)
386 return EIO;
387
388 ++addr;
389 }
390
391 /* File allocation tables */
[602c3d8c]392 for (i = 0; i < cfg->fat_count; ++i) {
[dccf721]393 printf("Writing allocation table %d.\n", i + 1);
394
[602c3d8c]395 for (j = 0; j < cfg->fat_sectors; ++j) {
396 memset(buffer, 0, cfg->sector_size);
[dccf721]397 if (j == 0) {
[602c3d8c]398 buffer[0] = default_media_descriptor;
[dccf721]399 buffer[1] = 0xFF;
400 buffer[2] = 0xFF;
[602c3d8c]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 }
[dccf721]414 }
415
[15f3c3f]416 rc = block_write_direct(service_id, addr, 1, buffer);
[dccf721]417 if (rc != EOK)
418 return EIO;
419
420 ++addr;
421 }
422 }
423
[602c3d8c]424 /* Root directory */
[dccf721]425 printf("Writing root directory.\n");
[602c3d8c]426 memset(buffer, 0, cfg->sector_size);
[f4ae95a]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);
[602c3d8c]440 }
[dccf721]441
[f4ae95a]442 rc = block_write_direct(service_id, addr, 1, buffer);
443 if (rc != EOK)
444 return EIO;
445
446 ++addr;
[dccf721]447 }
448
449 free(buffer);
450
451 return EOK;
452}
453
454/** Construct boot sector with the given parameters. */
[602c3d8c]455static void fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs)
[dccf721]456{
[f4ae95a]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;
[dccf721]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 */
[602c3d8c]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 }
[dccf721]489
[602c3d8c]490 bs->mdesc = default_media_descriptor;
[dccf721]491 bs->sec_per_track = host2uint16_t_le(63);
[602c3d8c]492 bs->signature = host2uint16_t_be(0x55AA);
[dccf721]493 bs->headcnt = host2uint16_t_le(6);
494 bs->hidden_sec = host2uint32_t_le(0);
495
[602c3d8c]496 if (cfg->fat_type == FAT32) {
497 bs->sec_per_fat = 0;
498 bs->fat32.sectors_per_fat = host2uint32_t_le(cfg->fat_sectors);
[dccf721]499
[602c3d8c]500 bs->fat32.pdn = 0x80;
501 bs->fat32.ebs = 0x29;
502 bs->fat32.id = host2uint32_t_be(0x12345678);
503 bs->fat32.root_cluster = 2;
[dccf721]504
[f4ae95a]505 (void) fat_label_encode(&bs->fat32.label, bs_label);
[602c3d8c]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
[f4ae95a]513 (void) fat_label_encode(&bs->label, bs_label);
[58fa3e6]514 if (cfg->fat_type == FAT12)
515 memcpy(bs->type, "FAT12 ", 8);
516 else
517 memcpy(bs->type, "FAT16 ", 8);
[602c3d8c]518 }
[dccf721]519}
520
521/**
522 * @}
523 */
Note: See TracBrowser for help on using the repository browser.