Subversion Repositories HelenOS

Rev

Rev 4496 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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