ext4_mbr: multiple changes related to MBR parsing
[lwext4.git] / fs_test / lwext4_mkfs.c
1 /*
2  * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
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 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <getopt.h>
34 #include <stdbool.h>
35 #include <inttypes.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <sys/time.h>
39
40 #include <ext4.h>
41 #include <ext4_mkfs.h>
42 #include "../blockdev/linux/ext4_filedev.h"
43 #include "../blockdev/windows/io_raw.h"
44
45 /**@brief   Input stream name.*/
46 const char *input_name = NULL;
47
48 /**@brief   Block device handle.*/
49 static struct ext4_blockdev *bd;
50
51 /**@brief   Indicates that input is windows partition.*/
52 static bool winpart = false;
53
54 static int fs_type = F_SET_EXT4;
55
56 static struct ext4_fs fs;
57 static struct ext4_mkfs_info info = {
58         .block_size = 1024,
59 };
60
61 static bool verbose = false;
62
63 static const char *usage = "                                    \n\
64 Welcome in lwext4_mkfs tool .                                   \n\
65 Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)  \n\
66 Usage:                                                          \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\
72 \n";
73
74
75 static bool open_linux(void)
76 {
77         ext4_filedev_filename(input_name);
78         bd = ext4_filedev_get();
79         if (!bd) {
80                 printf("open_filedev: fail\n");
81                 return false;
82         }
83         return true;
84 }
85
86 static bool open_windows(void)
87 {
88 #ifdef WIN32
89         ext4_io_raw_filename(input_name);
90         bd = ext4_io_raw_dev_get();
91         if (!bd) {
92                 printf("open_winpartition: fail\n");
93                 return false;
94         }
95         return true;
96 #else
97         printf("open_winpartition: this mode should be used only under windows "
98                "!\n");
99         return false;
100 #endif
101 }
102
103 static bool open_filedev(void)
104 {
105         if (winpart) {
106                 if (!open_windows())
107                         return false;
108         } else {
109                 if (!open_linux())
110                         return false;
111         }
112
113         return true;
114 }
115
116 static bool parse_opt(int argc, char **argv)
117 {
118         int option_index = 0;
119         int c;
120
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'},
127             {0, 0, 0, 0}};
128
129         while (-1 != (c = getopt_long(argc, argv, "i:b:e:wv",
130                                       long_options, &option_index))) {
131
132                 switch (c) {
133                 case 'i':
134                         input_name = optarg;
135                         break;
136                 case 'b':
137                         info.block_size = atoi(optarg);
138                         break;
139                 case 'e':
140                         fs_type = atoi(optarg);
141                         break;
142                 case 'w':
143                         winpart = true;
144                         break;
145                 case 'v':
146                         verbose = true;
147                         break;
148                 default:
149                         printf("%s", usage);
150                         return false;
151                 }
152         }
153
154         switch (info.block_size) {
155         case 1024:
156         case 2048:
157         case 4096:
158                 break;
159         default:
160                 printf("parse_opt: block_size = %"PRIu32" unsupported\n",
161                                 info.block_size);
162                 return false;
163         }
164
165         switch (fs_type) {
166         case F_SET_EXT2:
167         case F_SET_EXT3:
168         case F_SET_EXT4:
169                 break;
170         default:
171                 printf("parse_opt: fs_type = %"PRIu32" unsupported\n", fs_type);
172                 return false;
173         }
174
175         return true;
176 }
177
178 int main(int argc, char **argv)
179 {
180         int r;
181         if (!parse_opt(argc, argv)){
182                 printf("parse_opt error\n");
183                 return EXIT_FAILURE;
184         }
185
186         if (!open_filedev()) {
187                 printf("open_filedev error\n");
188                 return EXIT_FAILURE;
189         }
190
191         if (verbose)
192                 ext4_dmask_set(DEBUG_ALL);
193
194         printf("ext4_mkfs: ext%d\n", fs_type);
195         r = ext4_mkfs(&fs, bd, &info, fs_type);
196         if (r != EOK) {
197                 printf("ext4_mkfs error: %d\n", r);
198                 return EXIT_FAILURE;
199         }
200
201         memset(&info, 0, sizeof(struct ext4_mkfs_info));
202         r = ext4_mkfs_read_info(bd, &info);
203         if (r != EOK) {
204                 printf("ext4_mkfs_read_info error: %d\n", r);
205                 return EXIT_FAILURE;
206         }
207
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);
222
223         printf("\nDone ...\n");
224         return EXIT_SUCCESS;
225 }