source: mainline/uspace/app/blkdump/blkdump.c@ cd5816d6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cd5816d6 was cd5816d6, checked in by Martin Sucha <sucha14@…>, 14 years ago

Fix displaying address and space delimiters in blkdump

  • Property mode set to 100644
File size: 5.5 KB
Line 
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 <devmap.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 void print_hex_row(uint8_t *data, size_t length, size_t bytes_per_row);
55
56int main(int argc, char **argv)
57{
58
59 int rc;
60 char *dev_path;
61 devmap_handle_t handle;
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
72 if (argc < 2) {
73 printf(NAME ": Error, argument missing.\n");
74 syntax_print();
75 return 1;
76 }
77
78 --argc; ++argv;
79
80 if (str_cmp(*argv, "--offset") == 0) {
81 --argc; ++argv;
82 if (*argv == NULL) {
83 printf(NAME ": Error, argument missing (offset).\n");
84 syntax_print();
85 return 1;
86 }
87
88 block_offset = strtol(*argv, &endptr, 10);
89 if (*endptr != '\0') {
90 printf(NAME ": Error, invalid argument (offset).\n");
91 syntax_print();
92 return 1;
93 }
94
95 --argc; ++argv;
96 }
97
98 if (str_cmp(*argv, "--count") == 0) {
99 --argc; ++argv;
100 if (*argv == NULL) {
101 printf(NAME ": Error, argument missing (count).\n");
102 syntax_print();
103 return 1;
104 }
105
106 block_count = strtol(*argv, &endptr, 10);
107 if (*endptr != '\0') {
108 printf(NAME ": Error, invalid argument (count).\n");
109 syntax_print();
110 return 1;
111 }
112
113 --argc; ++argv;
114 }
115
116 if (argc != 1) {
117 printf(NAME ": Error, unexpected argument.\n");
118 syntax_print();
119 return 1;
120 }
121
122 dev_path = *argv;
123
124 rc = devmap_device_get_handle(dev_path, &handle, 0);
125 if (rc != EOK) {
126 printf(NAME ": Error resolving device `%s'.\n", dev_path);
127 return 2;
128 }
129
130 rc = block_init(handle, 2048);
131 if (rc != EOK) {
132 printf(NAME ": Error initializing libblock.\n");
133 return 2;
134 }
135
136 rc = block_get_bsize(handle, &block_size);
137 if (rc != EOK) {
138 printf(NAME ": Error determining device block size.\n");
139 return 2;
140 }
141
142 rc = block_get_nblocks(handle, &dev_nblocks);
143 if (rc != EOK) {
144 printf(NAME ": Warning, failed to obtain block device size.\n");
145 }
146
147 printf("Device %s has %" PRIuOFF64 " blocks, %" PRIuOFF64 " bytes each\n", dev_path, dev_nblocks, (aoff64_t) block_size);
148
149 data = malloc(block_size);
150 if (data == NULL) {
151 printf(NAME ": Error allocating data buffer of %" PRIuOFF64 " bytes", (aoff64_t) block_size);
152 block_fini(handle);
153 return 3;
154 }
155
156 limit = block_offset + block_count;
157 for (current = block_offset; current < limit; current++) {
158 rc = block_read_direct(handle, current, 1, data);
159 if (rc != EOK) {
160 printf(NAME ": Error reading block at %" PRIuOFF64 " \n", current);
161 free(data);
162 return 3;
163 }
164
165 printf("---- Block %" PRIuOFF64 " (at %" PRIuOFF64 ") ----\n", current, current*block_size);
166
167 for (data_offset = 0; data_offset < block_size; data_offset += 16) {
168 printf("%8" PRIxOFF64 ": ", current*block_size + data_offset);
169 print_hex_row(data+data_offset, block_size-data_offset, 16);
170 printf("\n");
171 }
172 printf("\n");
173 }
174
175 free(data);
176
177 block_fini(handle);
178
179 return 0;
180}
181
182/**
183 * Print a row of 16 bytes as commonly seen in hexadecimal dumps
184 */
185static void print_hex_row(uint8_t *data, size_t length, size_t bytes_per_row) {
186 size_t pos;
187 uint8_t b;
188
189 if (length > bytes_per_row) {
190 length = bytes_per_row;
191 }
192
193 // Print hexadecimal values
194 for (pos = 0; pos < length; pos++) {
195 if (pos == length/2) {
196 printf(" ");
197 }
198 printf("%02hhX ", data[pos]);
199 }
200
201 // pad with spaces if we have less than 16 bytes
202 for (pos = length; pos < bytes_per_row; pos++) {
203 if (pos == length/2) {
204 printf(" ");
205 }
206 printf(" ");
207 }
208
209 // Print printable characters
210 for (pos = 0; pos < length; pos++) {
211 if (pos == length/2) {
212 printf(" ");
213 }
214 b = data[pos];
215 if (b >= 32 && b < 128) {
216 putchar(b);
217 }
218 else {
219 putchar('.');
220 }
221 }
222}
223
224static void syntax_print(void)
225{
226 printf("syntax: blkdump [--offset <num_blocks>] [--count <num_blocks>] <device_name>\n");
227}
228
229/**
230 * @}
231 */
Note: See TracBrowser for help on using the repository browser.