Subversion Repositories HelenOS

Rev

Rev 4420 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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