| 1 | /*
|
|---|
| 2 | * Copyright (c) 2009 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <stdio.h>
|
|---|
| 30 | #include <stdlib.h>
|
|---|
| 31 | #include <str.h>
|
|---|
| 32 | #include "config.h"
|
|---|
| 33 | #include "util.h"
|
|---|
| 34 | #include "errors.h"
|
|---|
| 35 | #include "entry.h"
|
|---|
| 36 | #include "bdd.h"
|
|---|
| 37 | #include "cmds.h"
|
|---|
| 38 |
|
|---|
| 39 | #include <libblock.h>
|
|---|
| 40 | #include <devmap.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 | #include <assert.h>
|
|---|
| 43 |
|
|---|
| 44 | enum {
|
|---|
| 45 | /* Number of bytes per row */
|
|---|
| 46 | BPR = 16
|
|---|
| 47 | };
|
|---|
| 48 |
|
|---|
| 49 | static const char *cmdname = "bdd";
|
|---|
| 50 |
|
|---|
| 51 | /* Dispays help for bdd in various levels */
|
|---|
| 52 | void help_cmd_bdd(unsigned int level)
|
|---|
| 53 | {
|
|---|
| 54 | static char helpfmt[] =
|
|---|
| 55 | "Usage: %s <device> [<block_number> [<bytes>]]\n";
|
|---|
| 56 | if (level == HELP_SHORT) {
|
|---|
| 57 | printf("'%s' dump block device contents.\n", cmdname);
|
|---|
| 58 | } else {
|
|---|
| 59 | help_cmd_bdd(HELP_SHORT);
|
|---|
| 60 | printf(helpfmt, cmdname);
|
|---|
| 61 | }
|
|---|
| 62 | return;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /* Main entry point for bdd, accepts an array of arguments */
|
|---|
| 66 | int cmd_bdd(char **argv)
|
|---|
| 67 | {
|
|---|
| 68 | unsigned int argc;
|
|---|
| 69 | unsigned int i, j;
|
|---|
| 70 | dev_handle_t handle;
|
|---|
| 71 | uint8_t *blk;
|
|---|
| 72 | size_t size, bytes, rows;
|
|---|
| 73 | size_t block_size;
|
|---|
| 74 | int rc;
|
|---|
| 75 | aoff64_t ba;
|
|---|
| 76 | uint8_t b;
|
|---|
| 77 |
|
|---|
| 78 | /* Count the arguments */
|
|---|
| 79 | for (argc = 0; argv[argc] != NULL; argc ++);
|
|---|
| 80 |
|
|---|
| 81 | if (argc < 2 || argc > 4) {
|
|---|
| 82 | printf("%s - incorrect number of arguments.\n", cmdname);
|
|---|
| 83 | return CMD_FAILURE;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | if (argc >= 3)
|
|---|
| 87 | ba = strtol(argv[2], NULL, 0);
|
|---|
| 88 | else
|
|---|
| 89 | ba = 0;
|
|---|
| 90 |
|
|---|
| 91 | if (argc >= 4)
|
|---|
| 92 | size = strtol(argv[3], NULL, 0);
|
|---|
| 93 | else
|
|---|
| 94 | size = 256;
|
|---|
| 95 |
|
|---|
| 96 | rc = devmap_device_get_handle(argv[1], &handle, 0);
|
|---|
| 97 | if (rc != EOK) {
|
|---|
| 98 | printf("%s: Error resolving device `%s'.\n", cmdname, argv[1]);
|
|---|
| 99 | return CMD_FAILURE;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | rc = block_init(handle, 2048);
|
|---|
| 103 | if (rc != EOK) {
|
|---|
| 104 | printf("%s: Error initializing libblock.\n", cmdname);
|
|---|
| 105 | return CMD_FAILURE;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | rc = block_get_bsize(handle, &block_size);
|
|---|
| 109 | if (rc != EOK) {
|
|---|
| 110 | printf("%s: Error determining device block size.\n", cmdname);
|
|---|
| 111 | return CMD_FAILURE;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | blk = malloc(block_size);
|
|---|
| 115 | if (blk == NULL) {
|
|---|
| 116 | printf("%s: Error allocating memory.\n", cmdname);
|
|---|
| 117 | block_fini(handle);
|
|---|
| 118 | return CMD_FAILURE;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | while (size > 0) {
|
|---|
| 122 | rc = block_read_direct(handle, ba, 1, blk);
|
|---|
| 123 | if (rc != EOK) {
|
|---|
| 124 | printf("%s: Error reading block %llu\n", cmdname, ba);
|
|---|
| 125 | free(blk);
|
|---|
| 126 | block_fini(handle);
|
|---|
| 127 | return CMD_FAILURE;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | bytes = (size < block_size) ? size : block_size;
|
|---|
| 131 | rows = (bytes + BPR - 1) / BPR;
|
|---|
| 132 |
|
|---|
| 133 | for (j = 0; j < rows; j++) {
|
|---|
| 134 | for (i = 0; i < BPR; i++) {
|
|---|
| 135 | if (j * BPR + i < bytes)
|
|---|
| 136 | printf("%02x ", blk[j * BPR + i]);
|
|---|
| 137 | else
|
|---|
| 138 | printf(" ");
|
|---|
| 139 | }
|
|---|
| 140 | putchar('\t');
|
|---|
| 141 |
|
|---|
| 142 | for (i = 0; i < BPR; i++) {
|
|---|
| 143 | if (j * BPR + i < bytes) {
|
|---|
| 144 | b = blk[j * BPR + i];
|
|---|
| 145 | if (b >= 32 && b < 127)
|
|---|
| 146 | putchar(b);
|
|---|
| 147 | else
|
|---|
| 148 | putchar(' ');
|
|---|
| 149 | } else {
|
|---|
| 150 | putchar(' ');
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 | putchar('\n');
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | if (size > rows * BPR)
|
|---|
| 157 | size -= rows * BPR;
|
|---|
| 158 | else
|
|---|
| 159 | size = 0;
|
|---|
| 160 |
|
|---|
| 161 | /* Next block */
|
|---|
| 162 | ba += 1;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | free(blk);
|
|---|
| 166 | block_fini(handle);
|
|---|
| 167 |
|
|---|
| 168 | return CMD_SUCCESS;
|
|---|
| 169 | }
|
|---|