source: mainline/uspace/app/blkdump/blkdump.c

Last change on this file was 0460377, checked in by GitHub <noreply@…>, 3 months ago

uspace/app/blkdump: fix argv null deref (#251)

  • Property mode set to 100644
File size: 7.0 KB
Line 
1/*
2 * Copyright (c) 2024 Jiri Svoboda
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
30/** @addtogroup blkdump
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>
42#include <stdint.h>
43#include <block.h>
44#include <mem.h>
45#include <loc.h>
46#include <byteorder.h>
47#include <scsi/mmc.h>
48#include <offset.h>
49#include <inttypes.h>
50#include <errno.h>
51#include <str.h>
52
53#define NAME "blkdump"
54
55static void syntax_print(void);
56static int print_blocks(aoff64_t block_offset, aoff64_t block_count, size_t block_size);
57static int print_toc(void);
58static void print_hex_row(uint8_t *data, size_t length, size_t bytes_per_row);
59
60static bool relative = false;
61static service_id_t service_id;
62
63int main(int argc, char **argv)
64{
65
66 errno_t rc;
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;
73 bool toc = false;
74
75 if (argc < 2) {
76 printf(NAME ": Error, argument missing.\n");
77 syntax_print();
78 return 1;
79 }
80
81 --argc;
82 ++argv;
83
84 if (str_cmp(*argv, "--toc") == 0) {
85 --argc;
86 ++argv;
87 toc = true;
88 goto devname;
89 }
90
91 if (str_cmp(*argv, "--relative") == 0) {
92 --argc;
93 ++argv;
94 relative = true;
95 }
96
97 if (*argv && str_cmp(*argv, "--offset") == 0) {
98 --argc;
99 ++argv;
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
113 --argc;
114 ++argv;
115 }
116
117 if (*argv && str_cmp(*argv, "--count") == 0) {
118 --argc;
119 ++argv;
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
133 --argc;
134 ++argv;
135 }
136
137devname:
138 if (*argv == NULL) {
139 printf(NAME ": Error, argument missing (device_name).\n");
140 syntax_print();
141 return 1;
142 }
143
144 if (argc != 1) {
145 printf(NAME ": Error, unexpected argument.\n");
146 syntax_print();
147 return 1;
148 }
149
150 dev_path = *argv;
151
152 rc = loc_service_get_id(dev_path, &service_id, 0);
153 if (rc != EOK) {
154 printf(NAME ": Error resolving device `%s'.\n", dev_path);
155 return 2;
156 }
157
158 rc = block_init(service_id);
159 if (rc != EOK) {
160 printf(NAME ": Error initializing libblock.\n");
161 return 2;
162 }
163
164 rc = block_get_bsize(service_id, &block_size);
165 if (rc != EOK) {
166 printf(NAME ": Error determining device block size.\n");
167 return 2;
168 }
169
170 rc = block_get_nblocks(service_id, &dev_nblocks);
171 if (rc != EOK) {
172 printf(NAME ": Warning, failed to obtain block device size.\n");
173 }
174
175 printf("Device %s has %" PRIuOFF64 " blocks, %" PRIuOFF64 " bytes each\n", dev_path, dev_nblocks, (aoff64_t) block_size);
176
177 int ret;
178 if (toc)
179 ret = print_toc();
180 else
181 ret = print_blocks(block_offset, block_count, block_size);
182
183 block_fini(service_id);
184
185 return ret;
186}
187
188static int print_blocks(aoff64_t block_offset, aoff64_t block_count, size_t block_size)
189{
190 uint8_t *data;
191 aoff64_t current;
192 aoff64_t limit;
193 size_t data_offset;
194 errno_t rc;
195
196 data = malloc(block_size);
197 if (data == NULL) {
198 printf(NAME ": Error allocating data buffer of %" PRIuOFF64 " bytes", (aoff64_t) block_size);
199 return 3;
200 }
201
202 limit = block_offset + block_count;
203 for (current = block_offset; current < limit; current++) {
204 rc = block_read_direct(service_id, current, 1, data);
205 if (rc != EOK) {
206 printf(NAME ": Error reading block at %" PRIuOFF64 " \n", current);
207 free(data);
208 return 3;
209 }
210
211 printf("---- Block %" PRIuOFF64 " (at %" PRIuOFF64 ") ----\n", current, current * block_size);
212
213 for (data_offset = 0; data_offset < block_size; data_offset += 16) {
214 if (relative) {
215 printf("%8" PRIxOFF64 ": ", (aoff64_t) data_offset);
216 } else {
217 printf("%8" PRIxOFF64 ": ", current * block_size + data_offset);
218 }
219 print_hex_row(data + data_offset, block_size - data_offset, 16);
220 printf("\n");
221 }
222 printf("\n");
223 }
224
225 free(data);
226 return 0;
227}
228
229static int print_toc(void)
230{
231 scsi_toc_multisess_data_t toc;
232 errno_t rc;
233
234 rc = block_read_toc(service_id, 0, &toc, sizeof(toc));
235 if (rc != EOK)
236 return 1;
237
238 printf("Multisession Information:\n");
239 printf("\tFirst complete session: %" PRIu8 "\n", toc.first_sess);
240 printf("\tLast complete session: %" PRIu8 "\n", toc.last_sess);
241 printf("\tFirst track of last complete session:\n");
242 printf("\t\tADR / Control: 0x%" PRIx8 "\n", toc.ftrack_lsess.adr_control);
243 printf("\t\tTrack number: %" PRIu8 "\n", toc.ftrack_lsess.track_no);
244 printf("\t\tStart block address: %" PRIu32 "\n", toc.ftrack_lsess.start_addr);
245
246 return 0;
247}
248
249/**
250 * Print a row of 16 bytes as commonly seen in hexadecimal dumps
251 */
252static void print_hex_row(uint8_t *data, size_t length, size_t bytes_per_row)
253{
254 size_t pos;
255 uint8_t b;
256
257 if (length > bytes_per_row) {
258 length = bytes_per_row;
259 }
260
261 /* Print hexadecimal values */
262 for (pos = 0; pos < length; pos++) {
263 if (pos == length / 2) {
264 printf(" ");
265 }
266 printf("%02hhX ", data[pos]);
267 }
268
269 /* Pad with spaces if we have less than 16 bytes */
270 for (pos = length; pos < bytes_per_row; pos++) {
271 if (pos == length / 2) {
272 printf(" ");
273 }
274 printf(" ");
275 }
276
277 /* Print printable characters */
278 for (pos = 0; pos < length; pos++) {
279 if (pos == length / 2) {
280 printf(" ");
281 }
282 b = data[pos];
283 if (b >= 32 && b < 128) {
284 putchar(b);
285 } else {
286 putchar('.');
287 }
288 }
289}
290
291static void syntax_print(void)
292{
293 printf("syntax: blkdump [--toc] [--relative] [--offset <num_blocks>] "
294 "[--count <num_blocks>] <device_name>\n");
295}
296
297/**
298 * @}
299 */
Note: See TracBrowser for help on using the repository browser.