source: mainline/uspace/app/mkexfat/mkexfat.c@ dabe1664

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

mkexfat: Initialize the main extended boot sector and the checksum block

  • 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, EBS_BACKUP_SECTOR_START, &vbr_checksum);
238}
239
240/** Write the Main Extended Boot Sector to disk
241 *
242 * @param service_id The service id.
243 * @param cfg Pointer to the exFAT configuration structure.
244 * @param base Base sector of the EBS.
245 * @return EOK on success or a negative error code.
246 */
247static int
248ebs_write(service_id_t service_id, exfat_cfg_t *cfg, int base, uint32_t *chksum)
249{
250 uint32_t *ebs = calloc(cfg->sector_size, sizeof(uint8_t));
251 int i, rc;
252 unsigned idx;
253
254 if (!ebs)
255 return ENOMEM;
256
257 ebs[cfg->sector_size / 4 - 1] = host2uint32_t_le(0xAA550000);
258
259 for (i = 0; i < EBS_SIZE; ++i) {
260 vbr_checksum_update(ebs, cfg->sector_size, chksum);
261
262 rc = block_write_direct(service_id,
263 i + EBS_SECTOR_START + base, 1, ebs);
264
265 if (rc != EOK)
266 goto exit;
267 }
268
269 /* The OEM record is not yet used
270 * by the official exFAT implementation, we'll fill
271 * it with zeroes.
272 */
273
274 memset(ebs, 0, cfg->sector_size);
275 vbr_checksum_update(ebs, cfg->sector_size, chksum);
276
277 rc = block_write_direct(service_id, i++ + base, 1, ebs);
278 if (rc != EOK)
279 goto exit;
280
281 /* The next sector is reserved, fill it with zeroes too */
282 vbr_checksum_update(ebs, cfg->sector_size, chksum);
283 rc = block_write_direct(service_id, i++ + base, 1, ebs);
284 if (rc != EOK)
285 goto exit;
286
287 /* Write the checksum sector */
288 for (idx = 0; idx < cfg->sector_size / sizeof(uint32_t); ++idx)
289 ebs[idx] = host2uint32_t_le(*chksum);
290
291 rc = block_write_direct(service_id, i + base, 1, ebs);
292
293exit:
294 free(ebs);
295 return rc;
296}
297
298/** Writes the FAT on disk.
299 *
300 * @param cfg Pointer to the exfat_cfg structure.
301 * @return EOK on success or a negative error code.
302 */
303static int
304fat_write(service_id_t service_id, exfat_cfg_t *cfg)
305{
306 unsigned long i;
307 uint32_t *pfat;
308 int rc;
309
310 pfat = calloc(cfg->fat_sector_count, cfg->sector_size);
311 if (!pfat)
312 return ENOMEM;
313
314 pfat[0] = host2uint32_t_le(0xFFFFFFF8);
315 pfat[1] = host2uint32_t_le(0xFFFFFFFF);
316
317 rc = block_write_direct(service_id, FAT_SECTOR_START, 1, pfat);
318 if (rc != EOK)
319 goto error;
320
321 pfat[0] = pfat[1] = 0x00000000;
322
323 for (i = 1; i < cfg->fat_sector_count; ++i) {
324 rc = block_write_direct(service_id,
325 FAT_SECTOR_START + i + 1, 1, pfat);
326 if (rc != EOK)
327 goto error;
328 }
329
330error:
331 free(pfat);
332 return rc;
333}
334
335/** Given a number (n), returns the result of log2(n).
336 *
337 * It works only if n is a power of two.
338 */
339static unsigned
340log2(unsigned n)
341{
342 unsigned r;
343
344 for (r = 0;n >> r != 1; ++r);
345
346 return r;
347}
348
349/** Initialize the VBR checksum calculation */
350static uint32_t
351vbr_checksum_start(void const *data, size_t nbytes)
352{
353 uint32_t checksum = 0;
354 size_t index;
355 uint8_t const *octets = (uint8_t *) data;
356
357 for (index = 0; index < nbytes; ++index) {
358 if (index == 106 || index == 107 || index == 112) {
359 /* Skip volume_flags and allocated_percent fields */
360 continue;
361 }
362
363 checksum = ((checksum << 31) | (checksum >> 1)) + octets[index];
364 }
365
366 return checksum;
367}
368
369/** Update the VBR checksum */
370static void
371vbr_checksum_update(void const *data, size_t nbytes, uint32_t *checksum)
372{
373 size_t index;
374 uint8_t const *octets = (uint8_t *) data;
375
376 for (index = 0; index < nbytes; ++index)
377 *checksum = ((*checksum << 31) | (*checksum >> 1)) + octets[index];
378}
379
380int main (int argc, char **argv)
381{
382 exfat_cfg_t cfg;
383 char *dev_path;
384 service_id_t service_id;
385 int rc;
386
387 if (argc < 2) {
388 printf(NAME ": Error, argument missing\n");
389 usage();
390 return 1;
391 }
392
393 /* TODO: Add parameters */
394
395 ++argv;
396 dev_path = *argv;
397
398 printf(NAME ": Device = %s\n", dev_path);
399
400 rc = loc_service_get_id(dev_path, &service_id, 0);
401 if (rc != EOK) {
402 printf(NAME ": Error resolving device `%s'.\n", dev_path);
403 return 2;
404 }
405
406 rc = block_init(EXCHANGE_SERIALIZE, service_id, 2048);
407 if (rc != EOK) {
408 printf(NAME ": Error initializing libblock.\n");
409 return 2;
410 }
411
412 rc = block_get_bsize(service_id, &cfg.sector_size);
413 if (rc != EOK) {
414 printf(NAME ": Error determining device block size.\n");
415 return 2;
416 }
417
418 if (cfg.sector_size > 4096) {
419 printf(NAME ": Error, sector size can't be greater" \
420 " than 4096 bytes.\n");
421 return 2;
422 }
423
424 rc = block_get_nblocks(service_id, &cfg.volume_count);
425 if (rc != EOK) {
426 printf(NAME ": Warning, failed to obtain" \
427 " device block size.\n");
428 /* FIXME: the user should be able to specify the filesystem size */
429 return 1;
430 } else {
431 printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
432 cfg.volume_count);
433 }
434
435 cfg_params_initialize(&cfg);
436 cfg_print_info(&cfg);
437
438 rc = bootsec_write(service_id, &cfg);
439 if (rc != EOK) {
440 printf(NAME ": Error, failed to write the VBR to disk\n");
441 return 2;
442 }
443
444 rc = fat_write(service_id, &cfg);
445 if (rc != EOK) {
446 printf(NAME ": Error, failed to write the FAT to disk\n");
447 return 2;
448 }
449
450 return 0;
451}
452
Note: See TracBrowser for help on using the repository browser.