source: mainline/uspace/app/blkdump/blkdump.c@ 7901ac8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7901ac8 was 7901ac8, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Rudimentary support for dumping TOC in blkdump.

  • Property mode set to 100644
File size: 6.5 KB
Line 
1/*
2 * Copyright (c) 2011 Martin Sucha
3 * Copyright (c) 2013 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 <block.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
53static void syntax_print(void);
54static int print_blocks(aoff64_t block_offset, aoff64_t block_count, size_t block_size);
55static int print_toc(void);
56static void print_hex_row(uint8_t *data, size_t length, size_t bytes_per_row);
57
58static bool relative = false;
59static service_id_t service_id;
60
61int main(int argc, char **argv)
62{
63
64 int rc;
65 char *dev_path;
66 size_t block_size;
67 char *endptr;
68 aoff64_t block_offset = 0;
69 aoff64_t block_count = 1;
70 aoff64_t dev_nblocks;
71 bool toc = 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, "--toc") == 0) {
82 --argc; ++argv;
83 toc = true;
84 goto devname;
85 }
86
87 if (str_cmp(*argv, "--relative") == 0) {
88 --argc; ++argv;
89 relative = true;
90 }
91
92 if (str_cmp(*argv, "--offset") == 0) {
93 --argc; ++argv;
94 if (*argv == NULL) {
95 printf(NAME ": Error, argument missing (offset).\n");
96 syntax_print();
97 return 1;
98 }
99
100 block_offset = strtol(*argv, &endptr, 10);
101 if (*endptr != '\0') {
102 printf(NAME ": Error, invalid argument (offset).\n");
103 syntax_print();
104 return 1;
105 }
106
107 --argc; ++argv;
108 }
109
110 if (str_cmp(*argv, "--count") == 0) {
111 --argc; ++argv;
112 if (*argv == NULL) {
113 printf(NAME ": Error, argument missing (count).\n");
114 syntax_print();
115 return 1;
116 }
117
118 block_count = strtol(*argv, &endptr, 10);
119 if (*endptr != '\0') {
120 printf(NAME ": Error, invalid argument (count).\n");
121 syntax_print();
122 return 1;
123 }
124
125 --argc; ++argv;
126 }
127
128devname:
129 if (argc != 1) {
130 printf(NAME ": Error, unexpected argument.\n");
131 syntax_print();
132 return 1;
133 }
134
135 dev_path = *argv;
136
137 rc = loc_service_get_id(dev_path, &service_id, 0);
138 if (rc != EOK) {
139 printf(NAME ": Error resolving device `%s'.\n", dev_path);
140 return 2;
141 }
142
143 rc = block_init(EXCHANGE_SERIALIZE, service_id, 2048);
144 if (rc != EOK) {
145 printf(NAME ": Error initializing libblock.\n");
146 return 2;
147 }
148
149 rc = block_get_bsize(service_id, &block_size);
150 if (rc != EOK) {
151 printf(NAME ": Error determining device block size.\n");
152 return 2;
153 }
154
155 rc = block_get_nblocks(service_id, &dev_nblocks);
156 if (rc != EOK) {
157 printf(NAME ": Warning, failed to obtain block device size.\n");
158 }
159
160 printf("Device %s has %" PRIuOFF64 " blocks, %" PRIuOFF64 " bytes each\n", dev_path, dev_nblocks, (aoff64_t) block_size);
161
162 if (toc)
163 rc = print_toc();
164 else
165 rc = print_blocks(block_offset, block_count, block_size);
166
167 block_fini(service_id);
168
169 return rc;
170}
171
172static int print_blocks(aoff64_t block_offset, aoff64_t block_count, size_t block_size)
173{
174 uint8_t *data;
175 aoff64_t current;
176 aoff64_t limit;
177 size_t data_offset;
178 int rc;
179
180 data = malloc(block_size);
181 if (data == NULL) {
182 printf(NAME ": Error allocating data buffer of %" PRIuOFF64 " bytes", (aoff64_t) block_size);
183 return 3;
184 }
185
186 limit = block_offset + block_count;
187 for (current = block_offset; current < limit; current++) {
188 rc = block_read_direct(service_id, current, 1, data);
189 if (rc != EOK) {
190 printf(NAME ": Error reading block at %" PRIuOFF64 " \n", current);
191 free(data);
192 return 3;
193 }
194
195 printf("---- Block %" PRIuOFF64 " (at %" PRIuOFF64 ") ----\n", current, current*block_size);
196
197 for (data_offset = 0; data_offset < block_size; data_offset += 16) {
198 if (relative) {
199 printf("%8" PRIxOFF64 ": ", (aoff64_t) data_offset);
200 }
201 else {
202 printf("%8" PRIxOFF64 ": ", current*block_size + data_offset);
203 }
204 print_hex_row(data+data_offset, block_size-data_offset, 16);
205 printf("\n");
206 }
207 printf("\n");
208 }
209
210 free(data);
211 return 0;
212}
213
214static int print_toc(void)
215{
216 toc_block_t *toc;
217
218 toc = block_get_toc(service_id, 0);
219 if (toc == NULL)
220 return 1;
221
222 printf("TOC size: %" PRIu16 " bytes\n", toc->size);
223 printf("First session: %" PRIu8 "\n", toc->first_session);
224 printf("Last_session: %" PRIu8 "\n", toc->last_session);
225
226 return 0;
227}
228
229/**
230 * Print a row of 16 bytes as commonly seen in hexadecimal dumps
231 */
232static void print_hex_row(uint8_t *data, size_t length, size_t bytes_per_row) {
233 size_t pos;
234 uint8_t b;
235
236 if (length > bytes_per_row) {
237 length = bytes_per_row;
238 }
239
240 /* Print hexadecimal values */
241 for (pos = 0; pos < length; pos++) {
242 if (pos == length/2) {
243 printf(" ");
244 }
245 printf("%02hhX ", data[pos]);
246 }
247
248 /* Pad with spaces if we have less than 16 bytes */
249 for (pos = length; pos < bytes_per_row; pos++) {
250 if (pos == length/2) {
251 printf(" ");
252 }
253 printf(" ");
254 }
255
256 /* Print printable characters */
257 for (pos = 0; pos < length; pos++) {
258 if (pos == length/2) {
259 printf(" ");
260 }
261 b = data[pos];
262 if (b >= 32 && b < 128) {
263 putchar(b);
264 }
265 else {
266 putchar('.');
267 }
268 }
269}
270
271static void syntax_print(void)
272{
273 printf("syntax: blkdump [--relative] [--offset <num_blocks>] [--count <num_blocks>] <device_name>\n");
274}
275
276/**
277 * @}
278 */
Note: See TracBrowser for help on using the repository browser.