1 | /*
|
---|
2 | * Copyright (c) 2011 Martin Sucha
|
---|
3 | * Copyright (c) 2010 Jiri Svoboda
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup fs
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * @file blockdump.c
|
---|
36 | * @brief Tool for dumping content of block devices
|
---|
37 | *
|
---|
38 | */
|
---|
39 |
|
---|
40 | #include <stdio.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <libblock.h>
|
---|
43 | #include <mem.h>
|
---|
44 | #include <loc.h>
|
---|
45 | #include <byteorder.h>
|
---|
46 | #include <sys/types.h>
|
---|
47 | #include <sys/typefmt.h>
|
---|
48 | #include <inttypes.h>
|
---|
49 | #include <errno.h>
|
---|
50 |
|
---|
51 | #define NAME "blkdump"
|
---|
52 |
|
---|
53 | static void syntax_print(void);
|
---|
54 | static void print_hex_row(uint8_t *data, size_t length, size_t bytes_per_row);
|
---|
55 |
|
---|
56 | int main(int argc, char **argv)
|
---|
57 | {
|
---|
58 |
|
---|
59 | int rc;
|
---|
60 | char *dev_path;
|
---|
61 | service_id_t service_id;
|
---|
62 | size_t block_size;
|
---|
63 | char *endptr;
|
---|
64 | aoff64_t block_offset = 0;
|
---|
65 | aoff64_t block_count = 1;
|
---|
66 | aoff64_t dev_nblocks;
|
---|
67 | uint8_t *data;
|
---|
68 | size_t data_offset;
|
---|
69 | aoff64_t current;
|
---|
70 | aoff64_t limit;
|
---|
71 | bool relative = false;
|
---|
72 |
|
---|
73 | if (argc < 2) {
|
---|
74 | printf(NAME ": Error, argument missing.\n");
|
---|
75 | syntax_print();
|
---|
76 | return 1;
|
---|
77 | }
|
---|
78 |
|
---|
79 | --argc; ++argv;
|
---|
80 |
|
---|
81 | if (str_cmp(*argv, "--relative") == 0) {
|
---|
82 | --argc; ++argv;
|
---|
83 | relative = true;
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (str_cmp(*argv, "--offset") == 0) {
|
---|
87 | --argc; ++argv;
|
---|
88 | if (*argv == NULL) {
|
---|
89 | printf(NAME ": Error, argument missing (offset).\n");
|
---|
90 | syntax_print();
|
---|
91 | return 1;
|
---|
92 | }
|
---|
93 |
|
---|
94 | block_offset = strtol(*argv, &endptr, 10);
|
---|
95 | if (*endptr != '\0') {
|
---|
96 | printf(NAME ": Error, invalid argument (offset).\n");
|
---|
97 | syntax_print();
|
---|
98 | return 1;
|
---|
99 | }
|
---|
100 |
|
---|
101 | --argc; ++argv;
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (str_cmp(*argv, "--count") == 0) {
|
---|
105 | --argc; ++argv;
|
---|
106 | if (*argv == NULL) {
|
---|
107 | printf(NAME ": Error, argument missing (count).\n");
|
---|
108 | syntax_print();
|
---|
109 | return 1;
|
---|
110 | }
|
---|
111 |
|
---|
112 | block_count = strtol(*argv, &endptr, 10);
|
---|
113 | if (*endptr != '\0') {
|
---|
114 | printf(NAME ": Error, invalid argument (count).\n");
|
---|
115 | syntax_print();
|
---|
116 | return 1;
|
---|
117 | }
|
---|
118 |
|
---|
119 | --argc; ++argv;
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (argc != 1) {
|
---|
123 | printf(NAME ": Error, unexpected argument.\n");
|
---|
124 | syntax_print();
|
---|
125 | return 1;
|
---|
126 | }
|
---|
127 |
|
---|
128 | dev_path = *argv;
|
---|
129 |
|
---|
130 | rc = loc_service_get_id(dev_path, &service_id, 0);
|
---|
131 | if (rc != EOK) {
|
---|
132 | printf(NAME ": Error resolving device `%s'.\n", dev_path);
|
---|
133 | return 2;
|
---|
134 | }
|
---|
135 |
|
---|
136 | rc = block_init(EXCHANGE_SERIALIZE, service_id, 2048);
|
---|
137 | if (rc != EOK) {
|
---|
138 | printf(NAME ": Error initializing libblock.\n");
|
---|
139 | return 2;
|
---|
140 | }
|
---|
141 |
|
---|
142 | rc = block_get_bsize(service_id, &block_size);
|
---|
143 | if (rc != EOK) {
|
---|
144 | printf(NAME ": Error determining device block size.\n");
|
---|
145 | return 2;
|
---|
146 | }
|
---|
147 |
|
---|
148 | rc = block_get_nblocks(service_id, &dev_nblocks);
|
---|
149 | if (rc != EOK) {
|
---|
150 | printf(NAME ": Warning, failed to obtain block device size.\n");
|
---|
151 | }
|
---|
152 |
|
---|
153 | printf("Device %s has %" PRIuOFF64 " blocks, %" PRIuOFF64 " bytes each\n", dev_path, dev_nblocks, (aoff64_t) block_size);
|
---|
154 |
|
---|
155 | data = malloc(block_size);
|
---|
156 | if (data == NULL) {
|
---|
157 | printf(NAME ": Error allocating data buffer of %" PRIuOFF64 " bytes", (aoff64_t) block_size);
|
---|
158 | block_fini(service_id);
|
---|
159 | return 3;
|
---|
160 | }
|
---|
161 |
|
---|
162 | limit = block_offset + block_count;
|
---|
163 | for (current = block_offset; current < limit; current++) {
|
---|
164 | rc = block_read_direct(service_id, current, 1, data);
|
---|
165 | if (rc != EOK) {
|
---|
166 | printf(NAME ": Error reading block at %" PRIuOFF64 " \n", current);
|
---|
167 | free(data);
|
---|
168 | return 3;
|
---|
169 | }
|
---|
170 |
|
---|
171 | printf("---- Block %" PRIuOFF64 " (at %" PRIuOFF64 ") ----\n", current, current*block_size);
|
---|
172 |
|
---|
173 | for (data_offset = 0; data_offset < block_size; data_offset += 16) {
|
---|
174 | if (relative) {
|
---|
175 | printf("%8" PRIxOFF64 ": ", (aoff64_t) data_offset);
|
---|
176 | }
|
---|
177 | else {
|
---|
178 | printf("%8" PRIxOFF64 ": ", current*block_size + data_offset);
|
---|
179 | }
|
---|
180 | print_hex_row(data+data_offset, block_size-data_offset, 16);
|
---|
181 | printf("\n");
|
---|
182 | }
|
---|
183 | printf("\n");
|
---|
184 | }
|
---|
185 |
|
---|
186 | free(data);
|
---|
187 |
|
---|
188 | block_fini(service_id);
|
---|
189 |
|
---|
190 | return 0;
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Print a row of 16 bytes as commonly seen in hexadecimal dumps
|
---|
195 | */
|
---|
196 | static void print_hex_row(uint8_t *data, size_t length, size_t bytes_per_row) {
|
---|
197 | size_t pos;
|
---|
198 | uint8_t b;
|
---|
199 |
|
---|
200 | if (length > bytes_per_row) {
|
---|
201 | length = bytes_per_row;
|
---|
202 | }
|
---|
203 |
|
---|
204 | /* Print hexadecimal values */
|
---|
205 | for (pos = 0; pos < length; pos++) {
|
---|
206 | if (pos == length/2) {
|
---|
207 | printf(" ");
|
---|
208 | }
|
---|
209 | printf("%02hhX ", data[pos]);
|
---|
210 | }
|
---|
211 |
|
---|
212 | /* Pad with spaces if we have less than 16 bytes */
|
---|
213 | for (pos = length; pos < bytes_per_row; pos++) {
|
---|
214 | if (pos == length/2) {
|
---|
215 | printf(" ");
|
---|
216 | }
|
---|
217 | printf(" ");
|
---|
218 | }
|
---|
219 |
|
---|
220 | /* Print printable characters */
|
---|
221 | for (pos = 0; pos < length; pos++) {
|
---|
222 | if (pos == length/2) {
|
---|
223 | printf(" ");
|
---|
224 | }
|
---|
225 | b = data[pos];
|
---|
226 | if (b >= 32 && b < 128) {
|
---|
227 | putchar(b);
|
---|
228 | }
|
---|
229 | else {
|
---|
230 | putchar('.');
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | static void syntax_print(void)
|
---|
236 | {
|
---|
237 | printf("syntax: blkdump [--relative] [--offset <num_blocks>] [--count <num_blocks>] <device_name>\n");
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * @}
|
---|
242 | */
|
---|