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