source: mainline/uspace/app/mkexfat/mkexfat.c@ 4d433fe

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

mkexfat:

  • Get the total number of blocks of the device.
  • Add function to initialize the Volume Boot Record structure.
  • Property mode set to 100644
File size: 4.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
42#define NAME "mkexfat"
43
44/** First sector of the FAT */
45#define FAT_SECTOR_START 128
46
47/** Divide and round up. */
48#define div_round_up(a, b) (((a) + (b) - 1) / (b))
49
50typedef struct exfat_cfg {
51 uint64_t volume_start;
52 uint64_t volume_count;
53 uint32_t fat_sector_count;
54 uint32_t data_start_sector;
55 uint32_t data_cluster;
56 uint32_t rootdir_cluster;
57 unsigned bytes_per_sector;
58 unsigned sec_per_cluster;
59} exfat_cfg_t;
60
61static void usage(void)
62{
63 printf("Usage: mkexfat <device>\n");
64}
65
66/** Initialize the exFAT params structure.
67 *
68 * @param cfg Pointer to the exFAT params structure to initialize.
69 */
70static void
71cfg_params_initialize(exfat_cfg_t *cfg)
72{
73}
74
75/** Initialize the Volume Boot Record fields.
76 *
77 * @param vbr Pointer to the Volume Boot Record structure.
78 * @param cfg Pointer to the exFAT configuration structure.
79 */
80static void
81vbr_initialize(exfat_bs_t *vbr, exfat_cfg_t *cfg)
82{
83 /* Fill the structure with zeroes */
84 memset(vbr, 0, sizeof(exfat_bs_t));
85
86 /* Init Jump Boot section */
87 vbr->jump[0] = 0xEB;
88 vbr->jump[1] = 0x76;
89 vbr->jump[2] = 0x90;
90
91 /* Set the filesystem name */
92 memcpy(vbr->oem_name, "EXFAT ", sizeof(vbr->oem_name));
93
94 vbr->volume_start = host2uint64_t_le(cfg->volume_start);
95 vbr->volume_count = host2uint64_t_le(cfg->volume_count);
96 vbr->fat_sector_start = host2uint32_t_le(FAT_SECTOR_START);
97 vbr->fat_sector_count = host2uint32_t_le(cfg->fat_sector_count);
98 vbr->version.major = 1;
99 vbr->version.minor = 0;
100 vbr->volume_flags = host2uint16_t_le(0);
101 vbr->bytes_per_sector = cfg->bytes_per_sectors;
102 vbr->sec_per_cluster = cfg->sec_per_cluster;
103 vbr->fat_count = 1;
104 vbr->drive_no = 0x80;
105 vbr->allocated_percent = 0;
106 vbr->signature = host2uint16_t_le(0xAA55);
107}
108
109int main (int argc, char **argv)
110{
111 exfat_cfg_t cfg;
112 aoff64_t dev_nblocks;
113 char *dev_path;
114 service_id_t service_id;
115 size_t sector_size;
116 int rc;
117
118 if (argc < 2) {
119 printf(NAME ": Error, argument missing\n");
120 usage();
121 return 1;
122 }
123
124 /* The fist sector of the partition is zero */
125 cfg.volume_start = 0;
126
127 /* TODO: Add parameters */
128
129 ++argv;
130 dev_path = *argv;
131
132 printf(NAME ": Device = %s\n", dev_path);
133
134 rc = loc_service_get_id(dev_path, &service_id, 0);
135 if (rc != EOK) {
136 printf(NAME ": Error resolving device `%s'.\n");
137 return 2;
138 }
139
140 rc = block_init(EXCHANGE_SERIALIZE, service_id, 2048);
141 if (rc != EOK) {
142 printf(NAME ": Error initializing libblock.\n");
143 return 2;
144 }
145
146 rc = block_get_bsize(service_id, &sector_size);
147 if (rc != EOK) {
148 printf(NAME ": Error determining device block size.\n");
149 return 2;
150 }
151
152 rc = block_get_nblocks(service_id, &dev_nblocks);
153 if (rc != EOK) {
154 printf(NAME ": Warning, failed to obtain device block size.\n");
155 /* FIXME: the user should specify the filesystem size */
156 return 1;
157 } else {
158 printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
159 dev_nblocks);
160
161 cfg.volume_count = dev_nblocks;
162 }
163
164 cfg_params_initialize(&cfg);
165
166
167 return 0;
168}
169
Note: See TracBrowser for help on using the repository browser.