[32d3ebf] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Martin Sucha
|
---|
[7901ac8] | 3 | * Copyright (c) 2013 Jiri Svoboda
|
---|
[32d3ebf] | 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>
|
---|
[8d2dd7f2] | 42 | #include <stdint.h>
|
---|
[f73b291] | 43 | #include <block.h>
|
---|
[32d3ebf] | 44 | #include <mem.h>
|
---|
[15f3c3f] | 45 | #include <loc.h>
|
---|
[32d3ebf] | 46 | #include <byteorder.h>
|
---|
[3abf70c7] | 47 | #include <scsi/mmc.h>
|
---|
[7354b5e] | 48 | #include <offset.h>
|
---|
[32d3ebf] | 49 | #include <inttypes.h>
|
---|
| 50 | #include <errno.h>
|
---|
| 51 |
|
---|
| 52 | #define NAME "blkdump"
|
---|
| 53 |
|
---|
| 54 | static void syntax_print(void);
|
---|
[7901ac8] | 55 | static int print_blocks(aoff64_t block_offset, aoff64_t block_count, size_t block_size);
|
---|
| 56 | static int print_toc(void);
|
---|
[32d3ebf] | 57 | static void print_hex_row(uint8_t *data, size_t length, size_t bytes_per_row);
|
---|
| 58 |
|
---|
[7901ac8] | 59 | static bool relative = false;
|
---|
| 60 | static service_id_t service_id;
|
---|
| 61 |
|
---|
[32d3ebf] | 62 | int main(int argc, char **argv)
|
---|
| 63 | {
|
---|
| 64 |
|
---|
[b7fd2a0] | 65 | errno_t rc;
|
---|
[32d3ebf] | 66 | char *dev_path;
|
---|
| 67 | size_t block_size;
|
---|
| 68 | char *endptr;
|
---|
| 69 | aoff64_t block_offset = 0;
|
---|
| 70 | aoff64_t block_count = 1;
|
---|
| 71 | aoff64_t dev_nblocks;
|
---|
[7901ac8] | 72 | bool toc = false;
|
---|
[32d3ebf] | 73 |
|
---|
| 74 | if (argc < 2) {
|
---|
| 75 | printf(NAME ": Error, argument missing.\n");
|
---|
| 76 | syntax_print();
|
---|
| 77 | return 1;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | --argc; ++argv;
|
---|
| 81 |
|
---|
[7901ac8] | 82 | if (str_cmp(*argv, "--toc") == 0) {
|
---|
| 83 | --argc; ++argv;
|
---|
| 84 | toc = true;
|
---|
| 85 | goto devname;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[d3842e0] | 88 | if (str_cmp(*argv, "--relative") == 0) {
|
---|
| 89 | --argc; ++argv;
|
---|
| 90 | relative = true;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[32d3ebf] | 93 | if (str_cmp(*argv, "--offset") == 0) {
|
---|
| 94 | --argc; ++argv;
|
---|
| 95 | if (*argv == NULL) {
|
---|
| 96 | printf(NAME ": Error, argument missing (offset).\n");
|
---|
| 97 | syntax_print();
|
---|
| 98 | return 1;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | block_offset = strtol(*argv, &endptr, 10);
|
---|
| 102 | if (*endptr != '\0') {
|
---|
| 103 | printf(NAME ": Error, invalid argument (offset).\n");
|
---|
| 104 | syntax_print();
|
---|
| 105 | return 1;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | --argc; ++argv;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | if (str_cmp(*argv, "--count") == 0) {
|
---|
| 112 | --argc; ++argv;
|
---|
| 113 | if (*argv == NULL) {
|
---|
| 114 | printf(NAME ": Error, argument missing (count).\n");
|
---|
| 115 | syntax_print();
|
---|
| 116 | return 1;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | block_count = strtol(*argv, &endptr, 10);
|
---|
| 120 | if (*endptr != '\0') {
|
---|
| 121 | printf(NAME ": Error, invalid argument (count).\n");
|
---|
| 122 | syntax_print();
|
---|
| 123 | return 1;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | --argc; ++argv;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[7901ac8] | 129 | devname:
|
---|
[32d3ebf] | 130 | if (argc != 1) {
|
---|
| 131 | printf(NAME ": Error, unexpected argument.\n");
|
---|
| 132 | syntax_print();
|
---|
| 133 | return 1;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | dev_path = *argv;
|
---|
| 137 |
|
---|
[15f3c3f] | 138 | rc = loc_service_get_id(dev_path, &service_id, 0);
|
---|
[32d3ebf] | 139 | if (rc != EOK) {
|
---|
| 140 | printf(NAME ": Error resolving device `%s'.\n", dev_path);
|
---|
| 141 | return 2;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[fc22069] | 144 | rc = block_init(service_id, 2048);
|
---|
[32d3ebf] | 145 | if (rc != EOK) {
|
---|
| 146 | printf(NAME ": Error initializing libblock.\n");
|
---|
| 147 | return 2;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[15f3c3f] | 150 | rc = block_get_bsize(service_id, &block_size);
|
---|
[32d3ebf] | 151 | if (rc != EOK) {
|
---|
| 152 | printf(NAME ": Error determining device block size.\n");
|
---|
| 153 | return 2;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[15f3c3f] | 156 | rc = block_get_nblocks(service_id, &dev_nblocks);
|
---|
[32d3ebf] | 157 | if (rc != EOK) {
|
---|
| 158 | printf(NAME ": Warning, failed to obtain block device size.\n");
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[ec534b4] | 161 | printf("Device %s has %" PRIuOFF64 " blocks, %" PRIuOFF64 " bytes each\n", dev_path, dev_nblocks, (aoff64_t) block_size);
|
---|
[32d3ebf] | 162 |
|
---|
[d5c1051] | 163 | int ret;
|
---|
[7901ac8] | 164 | if (toc)
|
---|
[d5c1051] | 165 | ret = print_toc();
|
---|
[7901ac8] | 166 | else
|
---|
[d5c1051] | 167 | ret = print_blocks(block_offset, block_count, block_size);
|
---|
[7901ac8] | 168 |
|
---|
| 169 | block_fini(service_id);
|
---|
| 170 |
|
---|
[d5c1051] | 171 | return ret;
|
---|
[7901ac8] | 172 | }
|
---|
| 173 |
|
---|
| 174 | static int print_blocks(aoff64_t block_offset, aoff64_t block_count, size_t block_size)
|
---|
| 175 | {
|
---|
| 176 | uint8_t *data;
|
---|
| 177 | aoff64_t current;
|
---|
| 178 | aoff64_t limit;
|
---|
| 179 | size_t data_offset;
|
---|
[b7fd2a0] | 180 | errno_t rc;
|
---|
[7901ac8] | 181 |
|
---|
[32d3ebf] | 182 | data = malloc(block_size);
|
---|
| 183 | if (data == NULL) {
|
---|
[ec534b4] | 184 | printf(NAME ": Error allocating data buffer of %" PRIuOFF64 " bytes", (aoff64_t) block_size);
|
---|
[32d3ebf] | 185 | return 3;
|
---|
| 186 | }
|
---|
[7901ac8] | 187 |
|
---|
[32d3ebf] | 188 | limit = block_offset + block_count;
|
---|
| 189 | for (current = block_offset; current < limit; current++) {
|
---|
[15f3c3f] | 190 | rc = block_read_direct(service_id, current, 1, data);
|
---|
[32d3ebf] | 191 | if (rc != EOK) {
|
---|
| 192 | printf(NAME ": Error reading block at %" PRIuOFF64 " \n", current);
|
---|
| 193 | free(data);
|
---|
| 194 | return 3;
|
---|
| 195 | }
|
---|
[7901ac8] | 196 |
|
---|
[32d3ebf] | 197 | printf("---- Block %" PRIuOFF64 " (at %" PRIuOFF64 ") ----\n", current, current*block_size);
|
---|
[7901ac8] | 198 |
|
---|
[32d3ebf] | 199 | for (data_offset = 0; data_offset < block_size; data_offset += 16) {
|
---|
[d3842e0] | 200 | if (relative) {
|
---|
| 201 | printf("%8" PRIxOFF64 ": ", (aoff64_t) data_offset);
|
---|
| 202 | }
|
---|
| 203 | else {
|
---|
| 204 | printf("%8" PRIxOFF64 ": ", current*block_size + data_offset);
|
---|
| 205 | }
|
---|
[32d3ebf] | 206 | print_hex_row(data+data_offset, block_size-data_offset, 16);
|
---|
| 207 | printf("\n");
|
---|
| 208 | }
|
---|
| 209 | printf("\n");
|
---|
| 210 | }
|
---|
[7901ac8] | 211 |
|
---|
[32d3ebf] | 212 | free(data);
|
---|
[7901ac8] | 213 | return 0;
|
---|
| 214 | }
|
---|
[32d3ebf] | 215 |
|
---|
[7901ac8] | 216 | static int print_toc(void)
|
---|
| 217 | {
|
---|
[3abf70c7] | 218 | scsi_toc_multisess_data_t toc;
|
---|
[b7fd2a0] | 219 | errno_t rc;
|
---|
[7901ac8] | 220 |
|
---|
[3abf70c7] | 221 | rc = block_read_toc(service_id, 0, &toc, sizeof(toc));
|
---|
| 222 | if (rc != EOK)
|
---|
[7901ac8] | 223 | return 1;
|
---|
| 224 |
|
---|
[da79df42] | 225 | printf("Multisession Information:\n");
|
---|
| 226 | printf("\tFirst complete session: %" PRIu8 "\n", toc.first_sess);
|
---|
| 227 | printf("\tLast complete session: %" PRIu8 "\n", toc.last_sess);
|
---|
| 228 | printf("\tFirst track of last complete session:\n");
|
---|
| 229 | printf("\t\tADR / Control: 0x%" PRIx8 "\n", toc.ftrack_lsess.adr_control);
|
---|
| 230 | printf("\t\tTrack number: %" PRIu8 "\n", toc.ftrack_lsess.track_no);
|
---|
| 231 | printf("\t\tStart block address: %" PRIu32 "\n", toc.ftrack_lsess.start_addr);
|
---|
[32d3ebf] | 232 |
|
---|
| 233 | return 0;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /**
|
---|
| 237 | * Print a row of 16 bytes as commonly seen in hexadecimal dumps
|
---|
| 238 | */
|
---|
| 239 | static void print_hex_row(uint8_t *data, size_t length, size_t bytes_per_row) {
|
---|
| 240 | size_t pos;
|
---|
| 241 | uint8_t b;
|
---|
| 242 |
|
---|
| 243 | if (length > bytes_per_row) {
|
---|
| 244 | length = bytes_per_row;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[1a86c50] | 247 | /* Print hexadecimal values */
|
---|
[32d3ebf] | 248 | for (pos = 0; pos < length; pos++) {
|
---|
| 249 | if (pos == length/2) {
|
---|
| 250 | printf(" ");
|
---|
| 251 | }
|
---|
[cd5816d6] | 252 | printf("%02hhX ", data[pos]);
|
---|
[32d3ebf] | 253 | }
|
---|
| 254 |
|
---|
[1a86c50] | 255 | /* Pad with spaces if we have less than 16 bytes */
|
---|
[32d3ebf] | 256 | for (pos = length; pos < bytes_per_row; pos++) {
|
---|
| 257 | if (pos == length/2) {
|
---|
| 258 | printf(" ");
|
---|
| 259 | }
|
---|
[cd5816d6] | 260 | printf(" ");
|
---|
[32d3ebf] | 261 | }
|
---|
| 262 |
|
---|
[1a86c50] | 263 | /* Print printable characters */
|
---|
[32d3ebf] | 264 | for (pos = 0; pos < length; pos++) {
|
---|
[cd5816d6] | 265 | if (pos == length/2) {
|
---|
| 266 | printf(" ");
|
---|
| 267 | }
|
---|
[32d3ebf] | 268 | b = data[pos];
|
---|
| 269 | if (b >= 32 && b < 128) {
|
---|
| 270 | putchar(b);
|
---|
| 271 | }
|
---|
| 272 | else {
|
---|
| 273 | putchar('.');
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | static void syntax_print(void)
|
---|
| 279 | {
|
---|
[da79df42] | 280 | printf("syntax: blkdump [--toc] [--relative] [--offset <num_blocks>] "
|
---|
| 281 | "[--count <num_blocks>] <device_name>\n");
|
---|
[32d3ebf] | 282 | }
|
---|
| 283 |
|
---|
| 284 | /**
|
---|
| 285 | * @}
|
---|
| 286 | */
|
---|