Name refactiring inside file_windows module
[lwext4.git] / fs_test / lwext4_mbr.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_mbr.h>
42 #include "../blockdev/linux/file_dev.h"
43 #include "../blockdev/windows/file_windows.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 bool verbose = false;
55
56 static const char *usage = "                                    \n\
57 Welcome in lwext4_mbr tool.                                     \n\
58 Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)  \n\
59 Usage:                                                          \n\
60 [-i] --input   - input file name (or blockdevice)               \n\
61 [-w] --wpart   - windows partition mode                         \n\
62 [-v] --verbose - verbose mode                                   \n\
63 \n";
64
65
66 static bool open_linux(void)
67 {
68         file_dev_name_set(input_name);
69         bd = file_dev_get();
70         if (!bd) {
71                 printf("open_filedev: fail\n");
72                 return false;
73         }
74         return true;
75 }
76
77 static bool open_windows(void)
78 {
79 #ifdef WIN32
80         file_windows_name_set(input_name);
81         bd = file_windows_dev_get();
82         if (!bd) {
83                 printf("open_winpartition: fail\n");
84                 return false;
85         }
86         return true;
87 #else
88         printf("open_winpartition: this mode should be used only under windows "
89                "!\n");
90         return false;
91 #endif
92 }
93
94 static bool open_filedev(void)
95 {
96         return winpart ? open_windows() : open_linux();
97 }
98
99 static bool parse_opt(int argc, char **argv)
100 {
101         int option_index = 0;
102         int c;
103
104         static struct option long_options[] = {
105             {"input", required_argument, 0, 'i'},
106             {"wpart", no_argument, 0, 'w'},
107             {"verbose", no_argument, 0, 'v'},
108             {"version", no_argument, 0, 'x'},
109             {0, 0, 0, 0}};
110
111         while (-1 != (c = getopt_long(argc, argv, "i:wvx",
112                                       long_options, &option_index))) {
113
114                 switch (c) {
115                 case 'i':
116                         input_name = optarg;
117                         break;
118                 case 'w':
119                         winpart = true;
120                         break;
121                 case 'v':
122                         verbose = true;
123                         break;
124                 case 'x':
125                         puts(VERSION);
126                         exit(0);
127                         break;
128                 default:
129                         printf("%s", usage);
130                         return false;
131                 }
132         }
133
134         return true;
135 }
136
137 int main(int argc, char **argv)
138 {
139         int r;
140         if (!parse_opt(argc, argv)){
141                 printf("parse_opt error\n");
142                 return EXIT_FAILURE;
143         }
144
145         if (!open_filedev()) {
146                 printf("open_filedev error\n");
147                 return EXIT_FAILURE;
148         }
149
150         if (verbose)
151                 ext4_dmask_set(DEBUG_ALL);
152
153         printf("ext4_mbr\n");
154         struct ext4_mbr_bdevs bdevs;
155         r = ext4_mbr_scan(bd, &bdevs);
156         if (r != EOK) {
157                 printf("ext4_mbr_scan error\n");
158                 return EXIT_FAILURE;
159         }
160
161         int i;
162         printf("ext4_mbr_scan:\n");
163         for (i = 0; i < 4; i++) {
164                 printf("mbr_entry %d:\n", i);
165                 if (!bdevs.partitions[i].bdif) {
166                         printf("\tempty/unknown\n");
167                         continue;
168                 }
169
170                 printf("\toffeset: 0x%"PRIx64", %"PRIu64"MB\n",
171                         bdevs.partitions[i].part_offset,
172                         bdevs.partitions[i].part_offset / (1024 * 1024));
173                 printf("\tsize:    0x%"PRIx64", %"PRIu64"MB\n",
174                         bdevs.partitions[i].part_size,
175                         bdevs.partitions[i].part_size / (1024 * 1024));
176         }
177
178
179         return EXIT_SUCCESS;
180 }