ext4_mbr: multiple changes related to MBR parsing
[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/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 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         ext4_filedev_filename(input_name);
69         bd = ext4_filedev_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         ext4_io_raw_filename(input_name);
81         bd = ext4_io_raw_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         if (winpart) {
97                 if (!open_windows())
98                         return false;
99         } else {
100                 if (!open_linux())
101                         return false;
102         }
103
104         return true;
105 }
106
107 static bool parse_opt(int argc, char **argv)
108 {
109         int option_index = 0;
110         int c;
111
112         static struct option long_options[] = {
113             {"input", required_argument, 0, 'i'},
114             {"wpart", no_argument, 0, 'w'},
115             {"verbose", no_argument, 0, 'v'},
116             {0, 0, 0, 0}};
117
118         while (-1 != (c = getopt_long(argc, argv, "i:wv",
119                                       long_options, &option_index))) {
120
121                 switch (c) {
122                 case 'i':
123                         input_name = optarg;
124                         break;
125                 case 'w':
126                         winpart = true;
127                         break;
128                 case 'v':
129                         verbose = true;
130                         break;
131                 default:
132                         printf("%s", usage);
133                         return false;
134                 }
135         }
136
137         return true;
138 }
139
140 int main(int argc, char **argv)
141 {
142         int r;
143         if (!parse_opt(argc, argv)){
144                 printf("parse_opt error\n");
145                 return EXIT_FAILURE;
146         }
147
148         if (!open_filedev()) {
149                 printf("open_filedev error\n");
150                 return EXIT_FAILURE;
151         }
152
153         if (verbose)
154                 ext4_dmask_set(DEBUG_ALL);
155
156         printf("ext4_mbr\n");
157         struct ext4_mbr_bdevs bdevs;
158         r = ext4_mbr_scan(bd, &bdevs);
159         if (r != EOK) {
160                 printf("ext4_mbr_scan error\n");
161                 return EXIT_FAILURE;
162         }
163
164         int i;
165         printf("ext4_mbr_scan:\n");
166         for (i = 0; i < 4; i++) {
167                 printf("mbr_entry %d:\n", i);
168                 if (!bdevs.partitions[i].bdif) {
169                         printf("\tempty/unknown\n");
170                         continue;
171                 }
172
173                 printf("\toffeset: 0x%llx, %lluMB\n",
174                         bdevs.partitions[i].part_offset,
175                         bdevs.partitions[i].part_offset / (1024 * 1024));
176                 printf("\tsize: 0x%llx, %lluMB\n",
177                         bdevs.partitions[i].part_size,
178                         bdevs.partitions[i].part_size / (1024 * 1024));
179         }
180
181
182         return EXIT_SUCCESS;
183 }