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.
42 #include "../blockdev/linux/file_dev.h"
43 #include "../blockdev/windows/file_windows.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 bool verbose = false;
56 static const char *usage = " \n\
57 Welcome in lwext4_mbr tool. \n\
58 Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) \n\
60 [-i] --input - input file name (or blockdevice) \n\
61 [-w] --wpart - windows partition mode \n\
62 [-v] --verbose - verbose mode \n\
66 static bool open_linux(void)
68 file_dev_name_set(input_name);
71 printf("open_filedev: fail\n");
77 static bool open_windows(void)
80 file_windows_name_set(input_name);
81 bd = file_windows_dev_get();
83 printf("open_winpartition: fail\n");
88 printf("open_winpartition: this mode should be used only under windows "
94 static bool open_filedev(void)
96 return winpart ? open_windows() : open_linux();
99 static bool parse_opt(int argc, char **argv)
101 int option_index = 0;
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'},
111 while (-1 != (c = getopt_long(argc, argv, "i:wvx",
112 long_options, &option_index))) {
137 int main(int argc, char **argv)
140 if (!parse_opt(argc, argv)){
141 printf("parse_opt error\n");
145 if (!open_filedev()) {
146 printf("open_filedev error\n");
151 ext4_dmask_set(DEBUG_ALL);
153 printf("ext4_mbr\n");
154 struct ext4_mbr_bdevs bdevs;
155 r = ext4_mbr_scan(bd, &bdevs);
157 printf("ext4_mbr_scan error\n");
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");
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));