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