[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>
|
---|
[f73b291] | 42 | #include <block.h>
|
---|
[32d3ebf] | 43 | #include <mem.h>
|
---|
[15f3c3f] | 44 | #include <loc.h>
|
---|
[32d3ebf] | 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);
|
---|
[7901ac8] | 54 | static int print_blocks(aoff64_t block_offset, aoff64_t block_count, size_t block_size);
|
---|
| 55 | static int print_toc(void);
|
---|
[32d3ebf] | 56 | static void print_hex_row(uint8_t *data, size_t length, size_t bytes_per_row);
|
---|
| 57 |
|
---|
[7901ac8] | 58 | static bool relative = false;
|
---|
| 59 | static service_id_t service_id;
|
---|
| 60 |
|
---|
[32d3ebf] | 61 | int 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;
|
---|
[7901ac8] | 71 | bool toc = false;
|
---|
[32d3ebf] | 72 |
|
---|
| 73 | if (argc < 2) {
|
---|
| 74 | printf(NAME ": Error, argument missing.\n");
|
---|
| 75 | syntax_print();
|
---|
| 76 | return 1;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | --argc; ++argv;
|
---|
| 80 |
|
---|
[7901ac8] | 81 | if (str_cmp(*argv, "--toc") == 0) {
|
---|
| 82 | --argc; ++argv;
|
---|
| 83 | toc = true;
|
---|
| 84 | goto devname;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[d3842e0] | 87 | if (str_cmp(*argv, "--relative") == 0) {
|
---|
| 88 | --argc; ++argv;
|
---|
| 89 | relative = true;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[32d3ebf] | 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 |
|
---|
[7901ac8] | 128 | devname:
|
---|
[32d3ebf] | 129 | if (argc != 1) {
|
---|
| 130 | printf(NAME ": Error, unexpected argument.\n");
|
---|
| 131 | syntax_print();
|
---|
| 132 | return 1;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | dev_path = *argv;
|
---|
| 136 |
|
---|
[15f3c3f] | 137 | rc = loc_service_get_id(dev_path, &service_id, 0);
|
---|
[32d3ebf] | 138 | if (rc != EOK) {
|
---|
| 139 | printf(NAME ": Error resolving device `%s'.\n", dev_path);
|
---|
| 140 | return 2;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[15f3c3f] | 143 | rc = block_init(EXCHANGE_SERIALIZE, service_id, 2048);
|
---|
[32d3ebf] | 144 | if (rc != EOK) {
|
---|
| 145 | printf(NAME ": Error initializing libblock.\n");
|
---|
| 146 | return 2;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[15f3c3f] | 149 | rc = block_get_bsize(service_id, &block_size);
|
---|
[32d3ebf] | 150 | if (rc != EOK) {
|
---|
| 151 | printf(NAME ": Error determining device block size.\n");
|
---|
| 152 | return 2;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[15f3c3f] | 155 | rc = block_get_nblocks(service_id, &dev_nblocks);
|
---|
[32d3ebf] | 156 | if (rc != EOK) {
|
---|
| 157 | printf(NAME ": Warning, failed to obtain block device size.\n");
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[ec534b4] | 160 | printf("Device %s has %" PRIuOFF64 " blocks, %" PRIuOFF64 " bytes each\n", dev_path, dev_nblocks, (aoff64_t) block_size);
|
---|
[32d3ebf] | 161 |
|
---|
[7901ac8] | 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 |
|
---|
| 172 | static 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 |
|
---|
[32d3ebf] | 180 | data = malloc(block_size);
|
---|
| 181 | if (data == NULL) {
|
---|
[ec534b4] | 182 | printf(NAME ": Error allocating data buffer of %" PRIuOFF64 " bytes", (aoff64_t) block_size);
|
---|
[32d3ebf] | 183 | return 3;
|
---|
| 184 | }
|
---|
[7901ac8] | 185 |
|
---|
[32d3ebf] | 186 | limit = block_offset + block_count;
|
---|
| 187 | for (current = block_offset; current < limit; current++) {
|
---|
[15f3c3f] | 188 | rc = block_read_direct(service_id, current, 1, data);
|
---|
[32d3ebf] | 189 | if (rc != EOK) {
|
---|
| 190 | printf(NAME ": Error reading block at %" PRIuOFF64 " \n", current);
|
---|
| 191 | free(data);
|
---|
| 192 | return 3;
|
---|
| 193 | }
|
---|
[7901ac8] | 194 |
|
---|
[32d3ebf] | 195 | printf("---- Block %" PRIuOFF64 " (at %" PRIuOFF64 ") ----\n", current, current*block_size);
|
---|
[7901ac8] | 196 |
|
---|
[32d3ebf] | 197 | for (data_offset = 0; data_offset < block_size; data_offset += 16) {
|
---|
[d3842e0] | 198 | if (relative) {
|
---|
| 199 | printf("%8" PRIxOFF64 ": ", (aoff64_t) data_offset);
|
---|
| 200 | }
|
---|
| 201 | else {
|
---|
| 202 | printf("%8" PRIxOFF64 ": ", current*block_size + data_offset);
|
---|
| 203 | }
|
---|
[32d3ebf] | 204 | print_hex_row(data+data_offset, block_size-data_offset, 16);
|
---|
| 205 | printf("\n");
|
---|
| 206 | }
|
---|
| 207 | printf("\n");
|
---|
| 208 | }
|
---|
[7901ac8] | 209 |
|
---|
[32d3ebf] | 210 | free(data);
|
---|
[7901ac8] | 211 | return 0;
|
---|
| 212 | }
|
---|
[32d3ebf] | 213 |
|
---|
[7901ac8] | 214 | static 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);
|
---|
[32d3ebf] | 225 |
|
---|
| 226 | return 0;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | /**
|
---|
| 230 | * Print a row of 16 bytes as commonly seen in hexadecimal dumps
|
---|
| 231 | */
|
---|
| 232 | static 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 |
|
---|
[1a86c50] | 240 | /* Print hexadecimal values */
|
---|
[32d3ebf] | 241 | for (pos = 0; pos < length; pos++) {
|
---|
| 242 | if (pos == length/2) {
|
---|
| 243 | printf(" ");
|
---|
| 244 | }
|
---|
[cd5816d6] | 245 | printf("%02hhX ", data[pos]);
|
---|
[32d3ebf] | 246 | }
|
---|
| 247 |
|
---|
[1a86c50] | 248 | /* Pad with spaces if we have less than 16 bytes */
|
---|
[32d3ebf] | 249 | for (pos = length; pos < bytes_per_row; pos++) {
|
---|
| 250 | if (pos == length/2) {
|
---|
| 251 | printf(" ");
|
---|
| 252 | }
|
---|
[cd5816d6] | 253 | printf(" ");
|
---|
[32d3ebf] | 254 | }
|
---|
| 255 |
|
---|
[1a86c50] | 256 | /* Print printable characters */
|
---|
[32d3ebf] | 257 | for (pos = 0; pos < length; pos++) {
|
---|
[cd5816d6] | 258 | if (pos == length/2) {
|
---|
| 259 | printf(" ");
|
---|
| 260 | }
|
---|
[32d3ebf] | 261 | b = data[pos];
|
---|
| 262 | if (b >= 32 && b < 128) {
|
---|
| 263 | putchar(b);
|
---|
| 264 | }
|
---|
| 265 | else {
|
---|
| 266 | putchar('.');
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | static void syntax_print(void)
|
---|
| 272 | {
|
---|
[d3842e0] | 273 | printf("syntax: blkdump [--relative] [--offset <num_blocks>] [--count <num_blocks>] <device_name>\n");
|
---|
[32d3ebf] | 274 | }
|
---|
| 275 |
|
---|
| 276 | /**
|
---|
| 277 | * @}
|
---|
| 278 | */
|
---|