2 * Copyright (c) 2011 Martin Sucha
3 * Copyright (c) 2013 Jiri Svoboda
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
36 * @brief Tool for dumping content of block devices
46 #include <byteorder.h>
53 #define NAME "blkdump"
55 static void syntax_print(void);
56 static int print_blocks(aoff64_t block_offset
, aoff64_t block_count
, size_t block_size
);
57 static int print_toc(void);
58 static void print_hex_row(uint8_t *data
, size_t length
, size_t bytes_per_row
);
60 static bool relative
= false;
61 static service_id_t service_id
;
63 int main(int argc
, char **argv
)
70 aoff64_t block_offset
= 0;
71 aoff64_t block_count
= 1;
76 printf(NAME
": Error, argument missing.\n");
84 if (str_cmp(*argv
, "--toc") == 0) {
91 if (str_cmp(*argv
, "--relative") == 0) {
97 if (str_cmp(*argv
, "--offset") == 0) {
101 printf(NAME
": Error, argument missing (offset).\n");
106 block_offset
= strtol(*argv
, &endptr
, 10);
107 if (*endptr
!= '\0') {
108 printf(NAME
": Error, invalid argument (offset).\n");
117 if (str_cmp(*argv
, "--count") == 0) {
121 printf(NAME
": Error, argument missing (count).\n");
126 block_count
= strtol(*argv
, &endptr
, 10);
127 if (*endptr
!= '\0') {
128 printf(NAME
": Error, invalid argument (count).\n");
139 printf(NAME
": Error, unexpected argument.\n");
146 rc
= loc_service_get_id(dev_path
, &service_id
, 0);
148 printf(NAME
": Error resolving device `%s'.\n", dev_path
);
152 rc
= block_init(service_id
, 2048);
154 printf(NAME
": Error initializing libblock.\n");
158 rc
= block_get_bsize(service_id
, &block_size
);
160 printf(NAME
": Error determining device block size.\n");
164 rc
= block_get_nblocks(service_id
, &dev_nblocks
);
166 printf(NAME
": Warning, failed to obtain block device size.\n");
169 printf("Device %s has %" PRIuOFF64
" blocks, %" PRIuOFF64
" bytes each\n", dev_path
, dev_nblocks
, (aoff64_t
) block_size
);
175 ret
= print_blocks(block_offset
, block_count
, block_size
);
177 block_fini(service_id
);
182 static int print_blocks(aoff64_t block_offset
, aoff64_t block_count
, size_t block_size
)
190 data
= malloc(block_size
);
192 printf(NAME
": Error allocating data buffer of %" PRIuOFF64
" bytes", (aoff64_t
) block_size
);
196 limit
= block_offset
+ block_count
;
197 for (current
= block_offset
; current
< limit
; current
++) {
198 rc
= block_read_direct(service_id
, current
, 1, data
);
200 printf(NAME
": Error reading block at %" PRIuOFF64
" \n", current
);
205 printf("---- Block %" PRIuOFF64
" (at %" PRIuOFF64
") ----\n", current
, current
* block_size
);
207 for (data_offset
= 0; data_offset
< block_size
; data_offset
+= 16) {
209 printf("%8" PRIxOFF64
": ", (aoff64_t
) data_offset
);
211 printf("%8" PRIxOFF64
": ", current
* block_size
+ data_offset
);
213 print_hex_row(data
+ data_offset
, block_size
- data_offset
, 16);
223 static int print_toc(void)
225 scsi_toc_multisess_data_t toc
;
228 rc
= block_read_toc(service_id
, 0, &toc
, sizeof(toc
));
232 printf("Multisession Information:\n");
233 printf("\tFirst complete session: %" PRIu8
"\n", toc
.first_sess
);
234 printf("\tLast complete session: %" PRIu8
"\n", toc
.last_sess
);
235 printf("\tFirst track of last complete session:\n");
236 printf("\t\tADR / Control: 0x%" PRIx8
"\n", toc
.ftrack_lsess
.adr_control
);
237 printf("\t\tTrack number: %" PRIu8
"\n", toc
.ftrack_lsess
.track_no
);
238 printf("\t\tStart block address: %" PRIu32
"\n", toc
.ftrack_lsess
.start_addr
);
244 * Print a row of 16 bytes as commonly seen in hexadecimal dumps
246 static void print_hex_row(uint8_t *data
, size_t length
, size_t bytes_per_row
)
251 if (length
> bytes_per_row
) {
252 length
= bytes_per_row
;
255 /* Print hexadecimal values */
256 for (pos
= 0; pos
< length
; pos
++) {
257 if (pos
== length
/ 2) {
260 printf("%02hhX ", data
[pos
]);
263 /* Pad with spaces if we have less than 16 bytes */
264 for (pos
= length
; pos
< bytes_per_row
; pos
++) {
265 if (pos
== length
/ 2) {
271 /* Print printable characters */
272 for (pos
= 0; pos
< length
; pos
++) {
273 if (pos
== length
/ 2) {
277 if (b
>= 32 && b
< 128) {
285 static void syntax_print(void)
287 printf("syntax: blkdump [--toc] [--relative] [--offset <num_blocks>] "
288 "[--count <num_blocks>] <device_name>\n");