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 <sys/typefmt.h>
|
---|
33 | #include "config.h"
|
---|
34 | #include "util.h"
|
---|
35 | #include "errors.h"
|
---|
36 | #include "entry.h"
|
---|
37 | #include "bdd.h"
|
---|
38 | #include "cmds.h"
|
---|
39 |
|
---|
40 | #include <libblock.h>
|
---|
41 | #include <loc.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <assert.h>
|
---|
44 |
|
---|
45 | enum {
|
---|
46 | /* Number of bytes per row */
|
---|
47 | BPR = 16
|
---|
48 | };
|
---|
49 |
|
---|
50 | static const char *cmdname = "bdd";
|
---|
51 |
|
---|
52 | /* Dispays help for bdd in various levels */
|
---|
53 | void help_cmd_bdd(unsigned int level)
|
---|
54 | {
|
---|
55 | static char helpfmt[] =
|
---|
56 | "Usage: %s <device> [<block_number> [<bytes>]]\n";
|
---|
57 | if (level == HELP_SHORT) {
|
---|
58 | printf("'%s' dump block device contents.\n", cmdname);
|
---|
59 | } else {
|
---|
60 | help_cmd_bdd(HELP_SHORT);
|
---|
61 | printf(helpfmt, cmdname);
|
---|
62 | }
|
---|
63 | return;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /* Main entry point for bdd, accepts an array of arguments */
|
---|
67 | int cmd_bdd(char **argv)
|
---|
68 | {
|
---|
69 | unsigned int argc;
|
---|
70 | unsigned int i, j;
|
---|
71 | service_id_t service_id;
|
---|
72 | aoff64_t offset;
|
---|
73 | uint8_t *blk;
|
---|
74 | size_t size, bytes, rows;
|
---|
75 | size_t block_size;
|
---|
76 | int rc;
|
---|
77 | aoff64_t ba;
|
---|
78 | uint8_t b;
|
---|
79 |
|
---|
80 | /* Count the arguments */
|
---|
81 | for (argc = 0; argv[argc] != NULL; argc ++);
|
---|
82 |
|
---|
83 | if (argc < 2 || argc > 4) {
|
---|
84 | printf("%s - incorrect number of arguments.\n", cmdname);
|
---|
85 | return CMD_FAILURE;
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (argc >= 3)
|
---|
89 | ba = strtol(argv[2], NULL, 0);
|
---|
90 | else
|
---|
91 | ba = 0;
|
---|
92 |
|
---|
93 | if (argc >= 4)
|
---|
94 | size = strtol(argv[3], NULL, 0);
|
---|
95 | else
|
---|
96 | size = 256;
|
---|
97 |
|
---|
98 | rc = loc_service_get_id(argv[1], &service_id, 0);
|
---|
99 | if (rc != EOK) {
|
---|
100 | printf("%s: Error resolving device `%s'.\n", cmdname, argv[1]);
|
---|
101 | return CMD_FAILURE;
|
---|
102 | }
|
---|
103 |
|
---|
104 | rc = block_init(EXCHANGE_SERIALIZE, service_id, 2048);
|
---|
105 | if (rc != EOK) {
|
---|
106 | printf("%s: Error initializing libblock.\n", cmdname);
|
---|
107 | return CMD_FAILURE;
|
---|
108 | }
|
---|
109 |
|
---|
110 | rc = block_get_bsize(service_id, &block_size);
|
---|
111 | if (rc != EOK) {
|
---|
112 | printf("%s: Error determining device block size.\n", cmdname);
|
---|
113 | return CMD_FAILURE;
|
---|
114 | }
|
---|
115 |
|
---|
116 | blk = malloc(block_size);
|
---|
117 | if (blk == NULL) {
|
---|
118 | printf("%s: Error allocating memory.\n", cmdname);
|
---|
119 | block_fini(service_id);
|
---|
120 | return CMD_FAILURE;
|
---|
121 | }
|
---|
122 |
|
---|
123 | offset = ba * block_size;
|
---|
124 |
|
---|
125 | while (size > 0) {
|
---|
126 | rc = block_read_direct(service_id, ba, 1, blk);
|
---|
127 | if (rc != EOK) {
|
---|
128 | printf("%s: Error reading block %" PRIuOFF64 "\n", cmdname, ba);
|
---|
129 | free(blk);
|
---|
130 | block_fini(service_id);
|
---|
131 | return CMD_FAILURE;
|
---|
132 | }
|
---|
133 |
|
---|
134 | bytes = (size < block_size) ? size : block_size;
|
---|
135 | rows = (bytes + BPR - 1) / BPR;
|
---|
136 |
|
---|
137 | for (j = 0; j < rows; j++) {
|
---|
138 | printf("[%06" PRIxOFF64 "] ", offset);
|
---|
139 | for (i = 0; i < BPR; i++) {
|
---|
140 | if (j * BPR + i < bytes)
|
---|
141 | printf("%02x ", blk[j * BPR + i]);
|
---|
142 | else
|
---|
143 | printf(" ");
|
---|
144 | }
|
---|
145 | putchar('\t');
|
---|
146 |
|
---|
147 | for (i = 0; i < BPR; i++) {
|
---|
148 | if (j * BPR + i < bytes) {
|
---|
149 | b = blk[j * BPR + i];
|
---|
150 | if (b >= 32 && b < 127)
|
---|
151 | putchar(b);
|
---|
152 | else
|
---|
153 | putchar(' ');
|
---|
154 | } else {
|
---|
155 | putchar(' ');
|
---|
156 | }
|
---|
157 | }
|
---|
158 | offset += BPR;
|
---|
159 | putchar('\n');
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (size > rows * BPR)
|
---|
163 | size -= rows * BPR;
|
---|
164 | else
|
---|
165 | size = 0;
|
---|
166 |
|
---|
167 | /* Next block */
|
---|
168 | ba += 1;
|
---|
169 | }
|
---|
170 |
|
---|
171 | free(blk);
|
---|
172 | block_fini(service_id);
|
---|
173 |
|
---|
174 | return CMD_SUCCESS;
|
---|
175 | }
|
---|