| 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 <string.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 | #define BLOCK_SIZE 512
|
|---|
| 45 | #define BPR 16
|
|---|
| 46 |
|
|---|
| 47 | static const char *cmdname = "bdd";
|
|---|
| 48 |
|
|---|
| 49 | /* Dispays help for bdd in various levels */
|
|---|
| 50 | void help_cmd_bdd(unsigned int level)
|
|---|
| 51 | {
|
|---|
| 52 | static char helpfmt[] =
|
|---|
| 53 | "Usage: %s <device> [<block_number> [<bytes>]]\n";
|
|---|
| 54 | if (level == HELP_SHORT) {
|
|---|
| 55 | printf("'%s' dump block device contents.\n", cmdname);
|
|---|
| 56 | } else {
|
|---|
| 57 | help_cmd_bdd(HELP_SHORT);
|
|---|
| 58 | printf(helpfmt, cmdname);
|
|---|
| 59 | }
|
|---|
| 60 | return;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /* Main entry point for bdd, accepts an array of arguments */
|
|---|
| 64 | int cmd_bdd(char **argv)
|
|---|
| 65 | {
|
|---|
| 66 | unsigned int argc;
|
|---|
| 67 | unsigned int i, j;
|
|---|
| 68 | dev_handle_t handle;
|
|---|
| 69 | block_t *block;
|
|---|
| 70 | uint8_t *blk;
|
|---|
| 71 | size_t size, bytes, rows;
|
|---|
| 72 | int rc;
|
|---|
| 73 | bn_t boff;
|
|---|
| 74 | uint8_t b;
|
|---|
| 75 |
|
|---|
| 76 | /* Count the arguments */
|
|---|
| 77 | for (argc = 0; argv[argc] != NULL; argc ++);
|
|---|
| 78 |
|
|---|
| 79 | if (argc < 2 || argc > 4) {
|
|---|
| 80 | printf("%s - incorrect number of arguments.\n", cmdname);
|
|---|
| 81 | return CMD_FAILURE;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | if (argc >= 3)
|
|---|
| 85 | boff = strtol(argv[2], NULL, 0);
|
|---|
| 86 | else
|
|---|
| 87 | boff = 0;
|
|---|
| 88 |
|
|---|
| 89 | if (argc >= 4)
|
|---|
| 90 | size = strtol(argv[3], NULL, 0);
|
|---|
| 91 | else
|
|---|
| 92 | size = 256;
|
|---|
| 93 |
|
|---|
| 94 | rc = devmap_device_get_handle(argv[1], &handle, 0);
|
|---|
| 95 | if (rc != EOK) {
|
|---|
| 96 | printf("Error: could not resolve device `%s'.\n", argv[1]);
|
|---|
| 97 | return CMD_FAILURE;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | rc = block_init(handle, BLOCK_SIZE);
|
|---|
| 101 | if (rc != EOK) {
|
|---|
| 102 | printf("Error: could not init libblock.\n");
|
|---|
| 103 | return CMD_FAILURE;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | rc = block_cache_init(handle, BLOCK_SIZE, 2, CACHE_MODE_WB);
|
|---|
| 107 | if (rc != EOK) {
|
|---|
| 108 | printf("Error: could not init block cache.\n");
|
|---|
| 109 | return CMD_FAILURE;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | while (size > 0) {
|
|---|
| 113 | rc = block_get(&block, handle, boff, 0);
|
|---|
| 114 | if (rc != EOK) {
|
|---|
| 115 | printf("Error: could not get block %u, device %u.\n",
|
|---|
| 116 | boff, handle);
|
|---|
| 117 | return CMD_FAILURE;
|
|---|
| 118 | }
|
|---|
| 119 | blk = (uint8_t *) block->data;
|
|---|
| 120 |
|
|---|
| 121 | bytes = (size < BLOCK_SIZE) ? size : BLOCK_SIZE;
|
|---|
| 122 | rows = (bytes + BPR - 1) / BPR;
|
|---|
| 123 |
|
|---|
| 124 | for (j = 0; j < rows; j++) {
|
|---|
| 125 | for (i = 0; i < BPR; i++) {
|
|---|
| 126 | if (j * BPR + i < bytes)
|
|---|
| 127 | printf("%02x ", blk[j * BPR + i]);
|
|---|
| 128 | else
|
|---|
| 129 | printf(" ");
|
|---|
| 130 | }
|
|---|
| 131 | putchar('\t');
|
|---|
| 132 |
|
|---|
| 133 | for (i = 0; i < BPR; i++) {
|
|---|
| 134 | if (j * BPR + i < bytes) {
|
|---|
| 135 | b = blk[j * BPR + i];
|
|---|
| 136 | if (b >= 32 && b < 127)
|
|---|
| 137 | putchar(b);
|
|---|
| 138 | else
|
|---|
| 139 | putchar(' ');
|
|---|
| 140 | } else {
|
|---|
| 141 | putchar(' ');
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 | putchar('\n');
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | rc = block_put(block);
|
|---|
| 148 | if (rc != EOK) {
|
|---|
| 149 | printf("Error: could not put block %p.\n",
|
|---|
| 150 | block);
|
|---|
| 151 | return CMD_FAILURE;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | if (size > rows * BPR)
|
|---|
| 155 | size -= rows * BPR;
|
|---|
| 156 | else
|
|---|
| 157 | size = 0;
|
|---|
| 158 |
|
|---|
| 159 | boff += rows * BPR;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | block_fini(handle);
|
|---|
| 163 |
|
|---|
| 164 | return CMD_SUCCESS;
|
|---|
| 165 | }
|
|---|