source: mainline/uspace/app/mkexfat/mkexfat.c@ 9587b37

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9587b37 was 9587b37, checked in by Maurizio Lombardi <m.lombardi85@…>, 14 years ago

mkexfat: cstyle

  • Property mode set to 100644
File size: 11.4 KB
Line 
1/*
2 * Copyright (c) 2012 Maurizio Lombardi
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 mkexfat.c
35 * @brief Tool for creating new exFAT file systems.
36 *
37 */
38
39#include <stdio.h>
40#include <libblock.h>
41#include <assert.h>
42#include <errno.h>
43#include <malloc.h>
44#include <byteorder.h>
45#include <align.h>
46#include <sys/types.h>
47#include <sys/typefmt.h>
48#include "exfat.h"
49
50#define NAME "mkexfat"
51
52/** First sector of the FAT */
53#define FAT_SECTOR_START 128
54
55/** First sector of the Main Extended Boot Region */
56#define EBS_SECTOR_START 1
57
58/** First sector of the Main Extended Boot Region Backup */
59#define EBS_BACKUP_SECTOR_START 13
60
61/** First sector of the VBR */
62#define VBR_SECTOR 0
63
64/** First sector if the VBR Backup */
65#define VBR_BACKUP_SECTOR 12
66
67/** Size of the Main Extended Boot Region */
68#define EBS_SIZE 8
69
70/** Divide and round up. */
71#define div_round_up(a, b) (((a) + (b) - 1) / (b))
72
73/** The default size of each cluster is 4096 byte */
74#define DEFAULT_CLUSTER_SIZE 4096
75
76typedef struct exfat_cfg {
77 aoff64_t volume_start;
78 aoff64_t volume_count;
79 unsigned long fat_sector_count;
80 unsigned long data_start_sector;
81 unsigned long rootdir_cluster;
82 unsigned long total_clusters;
83 size_t sector_size;
84 size_t cluster_size;
85} exfat_cfg_t;
86
87
88static unsigned log2(unsigned n);
89
90static uint32_t
91vbr_checksum_start(void const *octets, size_t nbytes);
92
93static void
94vbr_checksum_update(void const *octets, size_t nbytes, uint32_t *checksum);
95
96static int
97ebs_write(service_id_t service_id, exfat_cfg_t *cfg,
98 int base, uint32_t *chksum);
99
100static void usage(void)
101{
102 printf("Usage: mkexfat <device>\n");
103}
104
105/** Initialize the exFAT params structure.
106 *
107 * @param cfg Pointer to the exFAT params structure to initialize.
108 */
109static void
110cfg_params_initialize(exfat_cfg_t *cfg)
111{
112 unsigned long fat_bytes;
113 aoff64_t const volume_bytes = (cfg->volume_count - FAT_SECTOR_START) *
114 cfg->sector_size;
115
116 aoff64_t n_req_clusters = volume_bytes / DEFAULT_CLUSTER_SIZE;
117 cfg->cluster_size = DEFAULT_CLUSTER_SIZE;
118
119 /* Compute the required cluster size to index
120 * the entire storage device and to keep the FAT
121 * size less or equal to 64 Mb.
122 */
123 while (n_req_clusters > 16000000ULL &&
124 (cfg->cluster_size < 32 * 1024 * 1024)) {
125
126 cfg->cluster_size <<= 1;
127 n_req_clusters = volume_bytes / cfg->cluster_size;
128 }
129
130 /* The first two clusters are reserved */
131 cfg->total_clusters = n_req_clusters + 2;
132
133 /* Compute the FAT size in sectors */
134 fat_bytes = (cfg->total_clusters + 1) * 4;
135 cfg->fat_sector_count = div_round_up(fat_bytes, cfg->sector_size);
136
137 /* Compute the number of the first data sector */
138 cfg->data_start_sector = ROUND_UP(FAT_SECTOR_START +
139 cfg->fat_sector_count, cfg->cluster_size / cfg->sector_size);
140
141 cfg->rootdir_cluster = 0;
142
143 /* The first sector of the partition is zero */
144 cfg->volume_start = 0;
145}
146
147/** Prints the exFAT structure values
148 *
149 * @param cfg Pointer to the exfat_cfg_t structure.
150 */
151static void
152cfg_print_info(exfat_cfg_t *cfg)
153{
154 printf(NAME ": Sector size: %lu\n", cfg->sector_size);
155 printf(NAME ": Cluster size: %lu\n", cfg->cluster_size);
156 printf(NAME ": FAT size in sectors: %lu\n", cfg->fat_sector_count);
157 printf(NAME ": Data start sector: %lu\n", cfg->data_start_sector);
158 printf(NAME ": Total num of clusters: %lu\n", cfg->total_clusters);
159}
160
161/** Initialize the Volume Boot Record fields.
162 *
163 * @param vbr Pointer to the Volume Boot Record structure.
164 * @param cfg Pointer to the exFAT configuration structure.
165 * @return Initial checksum value.
166 */
167static uint32_t
168vbr_initialize(exfat_bs_t *vbr, exfat_cfg_t *cfg)
169{
170 /* Fill the structure with zeroes */
171 memset(vbr, 0, sizeof(exfat_bs_t));
172
173 /* Init Jump Boot section */
174 vbr->jump[0] = 0xEB;
175 vbr->jump[1] = 0x76;
176 vbr->jump[2] = 0x90;
177
178 /* Set the filesystem name */
179 memcpy(vbr->oem_name, "EXFAT ", sizeof(vbr->oem_name));
180
181 vbr->volume_start = host2uint64_t_le(cfg->volume_start);
182 vbr->volume_count = host2uint64_t_le(cfg->volume_count);
183 vbr->fat_sector_start = host2uint32_t_le(FAT_SECTOR_START);
184 vbr->fat_sector_count = host2uint32_t_le(cfg->fat_sector_count);
185 vbr->data_start_sector = host2uint32_t_le(cfg->data_start_sector);
186
187 vbr->data_clusters = host2uint32_t_le(cfg->total_clusters -
188 div_round_up(cfg->data_start_sector, cfg->cluster_size));
189
190 vbr->rootdir_cluster = 0;
191 vbr->volume_serial = 0;
192 vbr->version.major = 1;
193 vbr->version.minor = 0;
194 vbr->volume_flags = host2uint16_t_le(0);
195 vbr->bytes_per_sector = log2(cfg->sector_size);
196 vbr->sec_per_cluster = log2(cfg->cluster_size / cfg->sector_size);
197
198 /* Maximum cluster size is 32 Mb */
199 assert((vbr->bytes_per_sector + vbr->sec_per_cluster) <= 25);
200
201 vbr->fat_count = 1;
202 vbr->drive_no = 0x80;
203 vbr->allocated_percent = 0;
204 vbr->signature = host2uint16_t_le(0xAA55);
205
206 return vbr_checksum_start(vbr, sizeof(exfat_bs_t));
207}
208
209static int
210bootsec_write(service_id_t service_id, exfat_cfg_t *cfg)
211{
212 exfat_bs_t vbr;
213 uint32_t vbr_checksum;
214 uint32_t initial_checksum;
215 int rc;
216
217 vbr_checksum = vbr_initialize(&vbr, cfg);
218 initial_checksum = vbr_checksum;
219
220 /* Write the VBR on disk */
221 rc = block_write_direct(service_id, VBR_SECTOR, 1, &vbr);
222 if (rc != EOK)
223 return rc;
224
225 /* Write the VBR backup on disk */
226 rc = block_write_direct(service_id, VBR_BACKUP_SECTOR, 1, &vbr);
227 if (rc != EOK)
228 return rc;
229
230 rc = ebs_write(service_id, cfg, EBS_SECTOR_START, &vbr_checksum);
231 if (rc != EOK)
232 return rc;
233
234 /* Restore the checksum to its initial value */
235 vbr_checksum = initial_checksum;
236
237 return ebs_write(service_id, cfg,
238 EBS_BACKUP_SECTOR_START, &vbr_checksum);
239}
240
241/** Write the Main Extended Boot Sector to disk
242 *
243 * @param service_id The service id.
244 * @param cfg Pointer to the exFAT configuration structure.
245 * @param base Base sector of the EBS.
246 * @return EOK on success or a negative error code.
247 */
248static int
249ebs_write(service_id_t service_id, exfat_cfg_t *cfg, int base, uint32_t *chksum)
250{
251 uint32_t *ebs = calloc(cfg->sector_size, sizeof(uint8_t));
252 int i, rc;
253 unsigned idx;
254
255 if (!ebs)
256 return ENOMEM;
257
258 ebs[cfg->sector_size / 4 - 1] = host2uint32_t_le(0xAA550000);
259
260 for (i = 0; i < EBS_SIZE; ++i) {
261 vbr_checksum_update(ebs, cfg->sector_size, chksum);
262
263 rc = block_write_direct(service_id,
264 i + EBS_SECTOR_START + base, 1, ebs);
265
266 if (rc != EOK)
267 goto exit;
268 }
269
270 /* The OEM record is not yet used
271 * by the official exFAT implementation, we'll fill
272 * it with zeroes.
273 */
274
275 memset(ebs, 0, cfg->sector_size);
276 vbr_checksum_update(ebs, cfg->sector_size, chksum);
277
278 rc = block_write_direct(service_id, i++ + base, 1, ebs);
279 if (rc != EOK)
280 goto exit;
281
282 /* The next sector is reserved, fill it with zeroes too */
283 vbr_checksum_update(ebs, cfg->sector_size, chksum);
284 rc = block_write_direct(service_id, i++ + base, 1, ebs);
285 if (rc != EOK)
286 goto exit;
287
288 /* Write the checksum sector */
289 for (idx = 0; idx < cfg->sector_size / sizeof(uint32_t); ++idx)
290 ebs[idx] = host2uint32_t_le(*chksum);
291
292 rc = block_write_direct(service_id, i + base, 1, ebs);
293
294exit:
295 free(ebs);
296 return rc;
297}
298
299/** Writes the FAT on disk.
300 *
301 * @param cfg Pointer to the exfat_cfg structure.
302 * @return EOK on success or a negative error code.
303 */
304static int
305fat_write(service_id_t service_id, exfat_cfg_t *cfg)
306{
307 unsigned long i;
308 uint32_t *pfat;
309 int rc;
310
311 pfat = calloc(cfg->fat_sector_count, cfg->sector_size);
312 if (!pfat)
313 return ENOMEM;
314
315 pfat[0] = host2uint32_t_le(0xFFFFFFF8);
316 pfat[1] = host2uint32_t_le(0xFFFFFFFF);
317
318 rc = block_write_direct(service_id, FAT_SECTOR_START, 1, pfat);
319 if (rc != EOK)
320 goto error;
321
322 pfat[0] = pfat[1] = 0x00000000;
323
324 for (i = 1; i < cfg->fat_sector_count; ++i) {
325 rc = block_write_direct(service_id,
326 FAT_SECTOR_START + i + 1, 1, pfat);
327 if (rc != EOK)
328 goto error;
329 }
330
331error:
332 free(pfat);
333 return rc;
334}
335
336/** Given a number (n), returns the result of log2(n).
337 *
338 * It works only if n is a power of two.
339 */
340static unsigned
341log2(unsigned n)
342{
343 unsigned r;
344
345 for (r = 0;n >> r != 1; ++r);
346
347 return r;
348}
349
350/** Initialize the VBR checksum calculation */
351static uint32_t
352vbr_checksum_start(void const *data, size_t nbytes)
353{
354 uint32_t checksum = 0;
355 size_t index;
356 uint8_t const *octets = (uint8_t *) data;
357
358 for (index = 0; index < nbytes; ++index) {
359 if (index == 106 || index == 107 || index == 112) {
360 /* Skip volume_flags and allocated_percent fields */
361 continue;
362 }
363
364 checksum = ((checksum << 31) | (checksum >> 1)) + octets[index];
365 }
366
367 return checksum;
368}
369
370/** Update the VBR checksum */
371static void
372vbr_checksum_update(void const *data, size_t nbytes, uint32_t *checksum)
373{
374 size_t index;
375 uint8_t const *octets = (uint8_t *) data;
376
377 for (index = 0; index < nbytes; ++index)
378 *checksum = ((*checksum << 31) | (*checksum >> 1)) + octets[index];
379}
380
381int main (int argc, char **argv)
382{
383 exfat_cfg_t cfg;
384 char *dev_path;
385 service_id_t service_id;
386 int rc;
387
388 if (argc < 2) {
389 printf(NAME ": Error, argument missing\n");
390 usage();
391 return 1;
392 }
393
394 /* TODO: Add parameters */
395
396 ++argv;
397 dev_path = *argv;
398
399 printf(NAME ": Device = %s\n", dev_path);
400
401 rc = loc_service_get_id(dev_path, &service_id, 0);
402 if (rc != EOK) {
403 printf(NAME ": Error resolving device `%s'.\n", dev_path);
404 return 2;
405 }
406
407 rc = block_init(EXCHANGE_SERIALIZE, service_id, 2048);
408 if (rc != EOK) {
409 printf(NAME ": Error initializing libblock.\n");
410 return 2;
411 }
412
413 rc = block_get_bsize(service_id, &cfg.sector_size);
414 if (rc != EOK) {
415 printf(NAME ": Error determining device block size.\n");
416 return 2;
417 }
418
419 if (cfg.sector_size > 4096) {
420 printf(NAME ": Error, sector size can't be greater" \
421 " than 4096 bytes.\n");
422 return 2;
423 }
424
425 rc = block_get_nblocks(service_id, &cfg.volume_count);
426 if (rc != EOK) {
427 printf(NAME ": Warning, failed to obtain" \
428 " device block size.\n");
429 /* FIXME: the user should be able to specify the filesystem size */
430 return 1;
431 } else {
432 printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
433 cfg.volume_count);
434 }
435
436 cfg_params_initialize(&cfg);
437 cfg_print_info(&cfg);
438
439 rc = bootsec_write(service_id, &cfg);
440 if (rc != EOK) {
441 printf(NAME ": Error, failed to write the VBR to disk\n");
442 return 2;
443 }
444
445 rc = fat_write(service_id, &cfg);
446 if (rc != EOK) {
447 printf(NAME ": Error, failed to write the FAT to disk\n");
448 return 2;
449 }
450
451 return 0;
452}
453
Note: See TracBrowser for help on using the repository browser.