summaryrefslogtreecommitdiffstats
path: root/getmem.c
blob: 160125a0461ab7dd8a0e9dda1fe1a3c033499e48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2005-2010 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

#include <fcntl.h>
#include <getopt.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>

#include "version.h"

struct option long_options[] = {
  { "help",        0, 0, 'h' },
  { "version",     0, 0, 'V' },
  { "quad",        0, 0, 'q' },
  { "long",        0, 0, 'l' },
  { "dword",       0, 0, 'd' },
  { "short",       0, 0, 's' },
  { "word",        0, 0, 'w' },
  { "byte",        0, 0, 'b' },
  { "memcpy",      0, 0, 'm' },
  { 0, 0, 0, 0 }
};

const char *program;

void usage(void)
{
  fprintf(stderr, "Usage: %s [options] address bytes\n"
	  "  --help         -h  Print this help\n"
	  "  --version      -V  Print current version\n"
	  "  --quad         -q  Read quadwords (qword)\n"
	  "  --long         -l  Read longwords (dword)\n"
	  "  --dword        -d  Read longwords (dword)\n"
	  "  --short        -s  Read shortwords (word)\n"
	  "  --word         -w  Read shortwords (word)\n"
	  "  --byte         -b  Read bytes\n"
	  "  --memcpy       -m  Use memcpy (default)\n"
	  , program);
}

int main(int argc, char *argv[])
{
  uintptr_t start, mapstart, len, left, maplen;
  char *mem, *buffer;
  volatile char *ptr;
  int fd;
  int c;
  int size = 0;

  program = argv[0];

  while ( (c = getopt_long(argc,argv,"hVqldswbm",long_options,NULL)) != - 1 ) {
    switch( c ) {
    case 'h':
      usage();
      exit(0);
    case 'V':
      fprintf(stderr, "%s: version %s\n", program, VERSION_STRING);
      exit(0);
    case 'q':
      size = 8;
      break;
    case 'l':
    case 'd':
      size = 4;
      break;
    case 'w':
    case 's':
      size = 2;
      break;
    case 'b':
      size = 1;
      break;
    case 'm':
      size = 0;
      break;
    default:
      usage();
      exit(EX_USAGE);
    }
  }

  if ( argc-optind != 2 ) {
    usage();
    exit(EX_USAGE);
  }

  fd = open("/dev/mem", O_RDONLY);
  if ( fd < 0 ) {
    perror("/dev/mem");
    exit(EX_OSFILE);
  }

  start = (uintptr_t)strtoumax(argv[1], NULL, 0);
  len   = (uintptr_t)strtoumax(argv[2], NULL, 0);

  buffer = malloc(len);
  if ( !buffer ) {
    perror("malloc");
    exit(EX_OSERR);
  }

  mapstart = start & ~0xffff;
  maplen   = (len + (start-mapstart) + 0xffff) & ~0xffff;

  mem = mmap(NULL, maplen, PROT_READ, MAP_SHARED, fd, mapstart);
  if ( mem == MAP_FAILED ) {
    perror("mmap");
    exit(EX_OSERR);
  }

  if (size == 0) {
    memcpy(buffer, mem+(start-mapstart), len);
  } else {
    ptr = mem;
    left = len;
    
    while (left) {
      static const int next_lower_power_of_2[8] =
	{ 0, 1, 2, 2, 4, 4, 4, 4 };

      if (left < size)
	size = next_lower_power_of_2[left];

      switch (size) {
      case 1:
	*(uint8_t *)buffer = *(volatile uint8_t *)ptr;
	break;
      case 2:
	*(uint16_t *)buffer = *(volatile uint16_t *)ptr;
	break;
      case 4:
	*(uint32_t *)buffer = *(volatile uint32_t *)ptr;
	break;
      case 8:
	*(uint64_t *)buffer = *(volatile uint64_t *)ptr;
	break;
      default:
	abort();
      }
      buffer += size;
      left -= size;
    }
  }

  fwrite(buffer, len, 1, stdout);

  return 0;
}