Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 91 lines (86 sloc) 2.92 kb
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
--- devmem2.c 2004-08-05 01:55:25.000000000 +0200
+++ devmem2_modif.c 2011-01-13 15:48:37.798799784 +0100
@@ -45,12 +45,16 @@
 #define MAP_SIZE 4096UL
 #define MAP_MASK (MAP_SIZE - 1)
 
+static inline void *fixup_addr(void *addr, size_t size);
+
 int main(int argc, char **argv) {
     int fd;
     void *map_base, *virt_addr;
- unsigned long read_result, writeval;
+ unsigned long read_result, write_val;
  off_t target;
  int access_type = 'w';
+ char fmt_str[128];
+ size_t data_size;
 
  if(argc < 2) {
  fprintf(stderr, "\nUsage:\t%s { address } [ type [ data ] ]\n"
@@ -79,38 +83,51 @@
     virt_addr = map_base + (target & MAP_MASK);
     switch(access_type) {
  case 'b':
+ data_size = sizeof(unsigned char);
+ virt_addr = fixup_addr(virt_addr, data_size);
  read_result = *((unsigned char *) virt_addr);
  break;
  case 'h':
+ data_size = sizeof(unsigned short);
+ virt_addr = fixup_addr(virt_addr, data_size);
  read_result = *((unsigned short *) virt_addr);
  break;
  case 'w':
+ data_size = sizeof(unsigned long);
+ virt_addr = fixup_addr(virt_addr, data_size);
  read_result = *((unsigned long *) virt_addr);
  break;
  default:
  fprintf(stderr, "Illegal data type '%c'.\n", access_type);
  exit(2);
  }
- printf("Value at address 0x%X (%p): 0x%X\n", target, virt_addr, read_result);
+ sprintf(fmt_str, "Read at address 0x%%08lX (%%p): 0x%%0%dlX\n", 2*data_size);
+ printf(fmt_str, (unsigned long)target, virt_addr, read_result);
     fflush(stdout);
 
  if(argc > 3) {
- writeval = strtoul(argv[3], 0, 0);
+ write_val = strtoul(argv[3], 0, 0);
  switch(access_type) {
  case 'b':
- *((unsigned char *) virt_addr) = writeval;
+ virt_addr = fixup_addr(virt_addr, sizeof(unsigned char));
+ *((unsigned char *) virt_addr) = write_val;
  read_result = *((unsigned char *) virt_addr);
  break;
  case 'h':
- *((unsigned short *) virt_addr) = writeval;
+ virt_addr = fixup_addr(virt_addr, sizeof(unsigned short));
+ *((unsigned short *) virt_addr) = write_val;
  read_result = *((unsigned short *) virt_addr);
  break;
  case 'w':
- *((unsigned long *) virt_addr) = writeval;
+ virt_addr = fixup_addr(virt_addr, sizeof(unsigned long));
+ *((unsigned long *) virt_addr) = write_val;
  read_result = *((unsigned long *) virt_addr);
  break;
  }
- printf("Written 0x%X; readback 0x%X\n", writeval, read_result);
+ sprintf(fmt_str, "Write at address 0x%%08lX (%%p): 0x%%0%dlX, "
+ "readback 0x%%0%dlX\n", 2*data_size, 2*data_size);
+ printf(fmt_str, (unsigned long)target, virt_addr,
+ write_val, read_result);
  fflush(stdout);
  }
 
@@ -119,3 +136,12 @@
     return 0;
 }
 
+static inline void *fixup_addr(void *addr, size_t size)
+{
+#ifdef FORCE_STRICT_ALIGNMENT
+ unsigned long aligned_addr = (unsigned long)addr;
+ aligned_addr &= ~(size - 1);
+ addr = (void *)aligned_addr;
+#endif
+ return addr;
+}
Something went wrong with that request. Please try again.