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