source: mainline/uspace/app/mkfat/mkfat.c@ 03333bc

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

Add syntax help for mkfat.

  • Property mode set to 100644
File size: 8.2 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 only create 16-bit FAT.
38 */
39
40#include <stdio.h>
41#include <stdlib.h>
42#include <libblock.h>
43#include <mem.h>
44#include <devmap.h>
45#include <byteorder.h>
46#include <errno.h>
47#include "fat.h"
48
49#define NAME "mkfat"
50
51/** Divide and round up. */
52#define div_round_up(a, b) (((a) + (b) - 1) / (b))
53
54/** Predefined file-system parameters */
55enum {
56 sector_size = 512,
57 sectors_per_cluster = 8,
58 fat_count = 2,
59 reserved_clusters = 2,
60 media_descriptor = 0xF8 /**< fixed disk */
61};
62
63/** Configurable file-system parameters */
64typedef struct fat_cfg {
65 uint32_t total_sectors;
66 uint16_t root_ent_max;
67 uint16_t addt_res_sectors;
68} fat_cfg_t;
69
70/** Derived file-system parameters */
71typedef struct fat_params {
72 struct fat_cfg cfg;
73 uint16_t reserved_sectors;
74 uint16_t rootdir_sectors;
75 uint32_t fat_sectors;
76 uint16_t total_clusters;
77} fat_params_t;
78
79static void syntax_print(void);
80
81static int fat_params_compute(struct fat_cfg const *cfg,
82 struct fat_params *par);
83static int fat_blocks_write(struct fat_params const *par,
84 dev_handle_t handle);
85static void fat_bootsec_create(struct fat_params const *par, struct fat_bs *bs);
86
87int main(int argc, char **argv)
88{
89 struct fat_params par;
90 struct fat_cfg cfg;
91
92 int rc;
93 char *dev_path;
94 dev_handle_t handle;
95 size_t block_size;
96 char *endptr;
97 bn_t dev_nblocks;
98
99 cfg.total_sectors = 0;
100 cfg.addt_res_sectors = 0;
101 cfg.root_ent_max = 128;
102
103 if (argc < 2) {
104 printf(NAME ": Error, argument missing.\n");
105 syntax_print();
106 return 1;
107 }
108
109 --argc; ++argv;
110
111 if (str_cmp(*argv, "--size") == 0) {
112 --argc; ++argv;
113 if (*argv == NULL) {
114 printf(NAME ": Error, argument missing.\n");
115 syntax_print();
116 return 1;
117 }
118
119 cfg.total_sectors = strtol(*argv, &endptr, 10);
120 if (*endptr != '\0') {
121 printf(NAME ": Error, invalid argument.\n");
122 syntax_print();
123 return 1;
124 }
125
126 --argc; ++argv;
127 }
128
129 if (argc != 1) {
130 printf(NAME ": Error, unexpected argument.\n");
131 syntax_print();
132 return 1;
133 }
134
135 dev_path = *argv;
136
137 rc = devmap_device_get_handle(dev_path, &handle, 0);
138 if (rc != EOK) {
139 printf(NAME ": Error resolving device `%s'.\n", dev_path);
140 return 2;
141 }
142
143 rc = block_init(handle, 2048);
144 if (rc != EOK) {
145 printf(NAME ": Error initializing libblock.\n");
146 return 2;
147 }
148
149 rc = block_get_bsize(handle, &block_size);
150 if (rc != EOK) {
151 printf(NAME ": Error determining device block size.\n");
152 return 2;
153 }
154
155 rc = block_get_nblocks(handle, &dev_nblocks);
156 if (rc != EOK) {
157 printf(NAME ": Warning, failed to obtain block device size.\n");
158 } else {
159 printf(NAME ": Block device has %llu blocks.\n", dev_nblocks);
160 cfg.total_sectors = dev_nblocks;
161 }
162
163 if (block_size != 512) {
164 printf(NAME ": Error. Device block size is not 512 bytes.\n");
165 return 2;
166 }
167
168 if (cfg.total_sectors == 0) {
169 printf(NAME ": Error. You must specify filesystem size.\n");
170 return 1;
171 }
172
173 printf(NAME ": Creating FAT filesystem on device %s.\n", dev_path);
174
175 rc = fat_params_compute(&cfg, &par);
176 if (rc != EOK) {
177 printf(NAME ": Invalid file-system parameters.\n");
178 return 2;
179 }
180
181 rc = fat_blocks_write(&par, handle);
182 if (rc != EOK) {
183 printf(NAME ": Error writing device.\n");
184 return 2;
185 }
186
187 block_fini(handle);
188 printf("Success.\n");
189
190 return 0;
191}
192
193static void syntax_print(void)
194{
195 printf("syntax: mkfat [--size <num_blocks>] <device_name>\n");
196}
197
198/** Derive sizes of different filesystem structures.
199 *
200 * This function concentrates all the different computations of FAT
201 * file system params.
202 */
203static int fat_params_compute(struct fat_cfg const *cfg, struct fat_params *par)
204{
205 uint32_t fat_bytes;
206 uint32_t non_data_sectors_lb;
207
208 /*
209 * Make a conservative guess on the FAT size needed for the file
210 * system. The optimum could be potentially smaller since we
211 * do not subtract size of the FAT itself when computing the
212 * size of the data region.
213 */
214
215 par->reserved_sectors = 1 + cfg->addt_res_sectors;
216 par->rootdir_sectors = div_round_up(cfg->root_ent_max * DIRENT_SIZE,
217 sector_size);
218 non_data_sectors_lb = par->reserved_sectors + par->rootdir_sectors;
219
220 par->total_clusters = div_round_up(cfg->total_sectors - non_data_sectors_lb,
221 sectors_per_cluster);
222
223 fat_bytes = (par->total_clusters + 2) * 2;
224 par->fat_sectors = div_round_up(fat_bytes, sector_size);
225
226 par->cfg = *cfg;
227
228 return EOK;
229}
230
231/** Create file system with the given parameters. */
232static int fat_blocks_write(struct fat_params const *par, dev_handle_t handle)
233{
234 bn_t addr;
235 uint8_t *buffer;
236 int i;
237 uint32_t j;
238 int rc;
239 struct fat_bs bs;
240
241 fat_bootsec_create(par, &bs);
242
243 rc = block_write_direct(handle, BS_BLOCK, 1, &bs);
244 if (rc != EOK)
245 return EIO;
246
247 addr = BS_BLOCK + 1;
248
249 buffer = calloc(sector_size, 1);
250 if (buffer == NULL)
251 return ENOMEM;
252
253 /* Reserved sectors */
254 for (i = 0; i < par->reserved_sectors - 1; ++i) {
255 rc = block_write_direct(handle, addr, 1, buffer);
256 if (rc != EOK)
257 return EIO;
258
259 ++addr;
260 }
261
262 /* File allocation tables */
263 for (i = 0; i < fat_count; ++i) {
264 printf("Writing allocation table %d.\n", i + 1);
265
266 for (j = 0; j < par->fat_sectors; ++j) {
267 memset(buffer, 0, sector_size);
268 if (j == 0) {
269 buffer[0] = media_descriptor;
270 buffer[1] = 0xFF;
271 buffer[2] = 0xFF;
272 buffer[3] = 0xFF;
273 }
274
275 rc = block_write_direct(handle, addr, 1, buffer);
276 if (rc != EOK)
277 return EIO;
278
279 ++addr;
280 }
281 }
282
283 printf("Writing root directory.\n");
284
285 memset(buffer, 0, sector_size);
286
287 /* Root directory */
288 for (i = 0; i < par->rootdir_sectors; ++i) {
289 rc = block_write_direct(handle, addr, 1, buffer);
290 if (rc != EOK)
291 return EIO;
292
293 ++addr;
294 }
295
296 free(buffer);
297
298 return EOK;
299}
300
301/** Construct boot sector with the given parameters. */
302static void fat_bootsec_create(struct fat_params const *par, struct fat_bs *bs)
303{
304 memset(bs, 0, sizeof(*bs));
305
306 bs->ji[0] = 0xEB;
307 bs->ji[1] = 0x3C;
308 bs->ji[2] = 0x90;
309
310 memcpy(bs->oem_name, "HELENOS ", 8);
311
312 /* BIOS Parameter Block */
313 bs->bps = host2uint16_t_le(sector_size);
314 bs->spc = sectors_per_cluster;
315 bs->rscnt = host2uint16_t_le(par->reserved_sectors);
316 bs->fatcnt = fat_count;
317 bs->root_ent_max = host2uint16_t_le(par->cfg.root_ent_max);
318
319 if (par->cfg.total_sectors < 0x10000)
320 bs->totsec16 = host2uint16_t_le(par->cfg.total_sectors);
321 else
322 bs->totsec16 = host2uint16_t_le(0);
323
324 bs->mdesc = media_descriptor;
325 bs->sec_per_fat = host2uint16_t_le(par->fat_sectors);
326 bs->sec_per_track = host2uint16_t_le(63);
327 bs->headcnt = host2uint16_t_le(6);
328 bs->hidden_sec = host2uint32_t_le(0);
329
330 if (par->cfg.total_sectors >= 0x10000)
331 bs->totsec32 = host2uint32_t_le(par->cfg.total_sectors);
332 else
333 bs->totsec32 = host2uint32_t_le(0);
334
335 /* Extended BPB */
336 bs->pdn = 0x80;
337 bs->ebs = 0x29;
338 bs->id = host2uint32_t_be(0x12345678);
339
340 memcpy(bs->label, "HELENOS_NEW", 11);
341 memcpy(bs->type, "FAT16 ", 8);
342 bs->signature = host2uint16_t_be(0x55AA);
343}
344
345/**
346 * @}
347 */
Note: See TracBrowser for help on using the repository browser.