source: mainline/uspace/app/bdsh/cmds/modules/bdd/bdd.c@ 15f3c3f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 15f3c3f was 15f3c3f, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Rename devmap to loc, devfs to locfs.

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[27fd651]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>
[19f857a]31#include <str.h>
[7e752b2]32#include <sys/typefmt.h>
[27fd651]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>
[15f3c3f]41#include <loc.h>
[27fd651]42#include <errno.h>
[c91f2d1b]43#include <assert.h>
[27fd651]44
[309ede1]45enum {
46 /* Number of bytes per row */
47 BPR = 16
48};
[27fd651]49
50static const char *cmdname = "bdd";
51
52/* Dispays help for bdd in various levels */
53void 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 */
67int cmd_bdd(char **argv)
68{
69 unsigned int argc;
70 unsigned int i, j;
[15f3c3f]71 service_id_t service_id;
[7308e84]72 aoff64_t offset;
[27fd651]73 uint8_t *blk;
74 size_t size, bytes, rows;
[309ede1]75 size_t block_size;
[27fd651]76 int rc;
[ed903174]77 aoff64_t ba;
[27fd651]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)
[309ede1]89 ba = strtol(argv[2], NULL, 0);
[27fd651]90 else
[309ede1]91 ba = 0;
[27fd651]92
93 if (argc >= 4)
94 size = strtol(argv[3], NULL, 0);
95 else
96 size = 256;
97
[15f3c3f]98 rc = loc_service_get_id(argv[1], &service_id, 0);
[27fd651]99 if (rc != EOK) {
[309ede1]100 printf("%s: Error resolving device `%s'.\n", cmdname, argv[1]);
[27fd651]101 return CMD_FAILURE;
102 }
103
[15f3c3f]104 rc = block_init(EXCHANGE_SERIALIZE, service_id, 2048);
[27fd651]105 if (rc != EOK) {
[309ede1]106 printf("%s: Error initializing libblock.\n", cmdname);
[27fd651]107 return CMD_FAILURE;
108 }
109
[15f3c3f]110 rc = block_get_bsize(service_id, &block_size);
[27fd651]111 if (rc != EOK) {
[309ede1]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);
[15f3c3f]119 block_fini(service_id);
[27fd651]120 return CMD_FAILURE;
121 }
122
[7308e84]123 offset = ba * block_size;
124
[27fd651]125 while (size > 0) {
[15f3c3f]126 rc = block_read_direct(service_id, ba, 1, blk);
[cffce57]127 if (rc != EOK) {
[7e752b2]128 printf("%s: Error reading block %" PRIuOFF64 "\n", cmdname, ba);
[309ede1]129 free(blk);
[15f3c3f]130 block_fini(service_id);
[cffce57]131 return CMD_FAILURE;
132 }
[27fd651]133
[309ede1]134 bytes = (size < block_size) ? size : block_size;
[27fd651]135 rows = (bytes + BPR - 1) / BPR;
136
137 for (j = 0; j < rows; j++) {
[7308e84]138 printf("[%06" PRIxOFF64 "] ", offset);
[27fd651]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 }
[7308e84]158 offset += BPR;
[27fd651]159 putchar('\n');
160 }
161
162 if (size > rows * BPR)
163 size -= rows * BPR;
164 else
165 size = 0;
166
[309ede1]167 /* Next block */
168 ba += 1;
[27fd651]169 }
170
[309ede1]171 free(blk);
[15f3c3f]172 block_fini(service_id);
[27fd651]173
174 return CMD_SUCCESS;
175}
Note: See TracBrowser for help on using the repository browser.