2 * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
41 #include <ext4_mkfs.h>
42 #include "../blockdev/linux/ext4_filedev.h"
43 #include "../blockdev/windows/io_raw.h"
45 /**@brief Input stream name.*/
46 const char *input_name = NULL;
48 /**@brief Block device handle.*/
49 static struct ext4_blockdev *bd;
51 /**@brief Indicates that input is windows partition.*/
52 static bool winpart = false;
54 static int fs_type = F_SET_EXT4;
56 static struct ext4_fs fs;
57 static struct ext4_mkfs_info info = {
61 static bool verbose = false;
63 static const char *usage = " \n\
64 Welcome in lwext4_mkfs tool . \n\
65 Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) \n\
67 [-i] --input - input file name (or blockdevice) \n\
68 [-w] --wpart - windows partition mode \n\
69 [-v] --verbose - verbose mode \n\
70 [-b] --block - block size: 1024, 2048, 4096 (default 1024) \n\
71 [-e] --ext - fs type (ext2: 2, ext3: 3 ext4: 4)) \n\
75 static bool open_linux(void)
77 ext4_filedev_filename(input_name);
78 bd = ext4_filedev_get();
80 printf("open_filedev: fail\n");
86 static bool open_windows(void)
89 ext4_io_raw_filename(input_name);
90 bd = ext4_io_raw_dev_get();
92 printf("open_winpartition: fail\n");
97 printf("open_winpartition: this mode should be used only under windows "
103 static bool open_filedev(void)
116 static bool parse_opt(int argc, char **argv)
118 int option_index = 0;
121 static struct option long_options[] = {
122 {"input", required_argument, 0, 'i'},
123 {"block", required_argument, 0, 'b'},
124 {"ext", required_argument, 0, 'e'},
125 {"wpart", no_argument, 0, 'w'},
126 {"verbose", no_argument, 0, 'v'},
129 while (-1 != (c = getopt_long(argc, argv, "i:b:e:wv",
130 long_options, &option_index))) {
137 info.block_size = atoi(optarg);
140 fs_type = atoi(optarg);
154 switch (info.block_size) {
160 printf("parse_opt: block_size = %"PRIu32" unsupported\n",
171 printf("parse_opt: fs_type = %"PRIu32" unsupported\n", fs_type);
178 int main(int argc, char **argv)
181 if (!parse_opt(argc, argv)){
182 printf("parse_opt error\n");
186 if (!open_filedev()) {
187 printf("open_filedev error\n");
192 ext4_dmask_set(DEBUG_ALL);
194 printf("ext4_mkfs: ext%d\n", fs_type);
195 r = ext4_mkfs(&fs, bd, &info, fs_type);
197 printf("ext4_mkfs error: %d\n", r);
201 memset(&info, 0, sizeof(struct ext4_mkfs_info));
202 r = ext4_mkfs_read_info(bd, &info);
204 printf("ext4_mkfs_read_info error: %d\n", r);
208 printf("Created filesystem with parameters:\n");
209 printf("Size: %"PRIu64"\n", info.len);
210 printf("Block size: %"PRIu32"\n", info.block_size);
211 printf("Blocks per group: %"PRIu32"\n", info.blocks_per_group);
212 printf("Inodes per group: %"PRIu32"\n", info.inodes_per_group);
213 printf("Inode size: %"PRIu32"\n", info.inode_size);
214 printf("Inodes: %"PRIu32"\n", info.inodes);
215 printf("Journal blocks: %"PRIu32"\n", info.journal_blocks);
216 printf("Features ro_compat: 0x%x\n", info.feat_ro_compat);
217 printf("Features compat: 0x%x\n", info.feat_compat);
218 printf("Features incompat: 0x%x\n", info.feat_incompat);
219 printf("BG desc reserve: %"PRIu32"\n", info.bg_desc_reserve_blocks);
220 printf("Descriptor size: %"PRIu32"\n",info.dsc_size);
221 printf("Label: %s\n", info.label);
223 printf("\nDone ...\n");