1 ///////////////////////////////////////////////////////////////////////////
2 // Workfile: devmem2.c (Implementation)
3 // Author: Jan-Derk Bakker, Daniel Giritzer
5 // Description: Simple module to read/write from/to any location in memory.
6 // Remarks: assumes a 4k page size and that sizeof(unsigned long) == 4
8 // Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
9 // Copyright (C) 2017, Daniel Giritzer (giri@nwrk.biz)
10 ///////////////////////////////////////////////////////////////////////////
21 #include <sys/types.h>
26 #define MAP_SIZE 4096UL
27 #define MAP_MASK (MAP_SIZE - 1)
30 /////////////////////////////////////////////////
31 /// \brief This function is called by the module
32 /// if an error occurs.
34 /// \param char* Custom error Message.
35 /////////////////////////////////////////////////
36 static void ERROR(char* custom)
38 //print custom error message
39 fprintf(stderr, "Error: %s \n", custom);
41 //print further information
42 fprintf(stderr, "Info: %d, %s\n",
43 errno, strerror(errno));
46 /////////////////////////////////////////////////
47 /// \brief This function allows writing to memory
48 /// on the specified address.
50 /// \param char* Address to write to
51 /// \param char* Value to write
52 /// \param int Access type, should be 'w' (word),
53 /// 'h' (halfword), 'b'(byte)
54 /////////////////////////////////////////////////
55 int writemem(char* address, char* value, int access_type)
60 unsigned long writeval = 0;
62 off_t target = strtoul(address, 0, 0);
65 if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
67 ERROR("Opening /dev/mem/ failed!");
72 map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
74 if(map_base == (void *) -1)
76 ERROR("Mapping Memory Page Failed!");
81 writeval = strtoul(value, 0, 0);
85 *((unsigned char *) virt_addr) = writeval;
88 *((unsigned short *) virt_addr) = writeval;
91 *((unsigned long *) virt_addr) = writeval;
94 ERROR("Wrong access_type set!");
99 if(munmap(map_base, MAP_SIZE) == -1)
101 ERROR("Unmapping Memory Page Failed!");
109 /////////////////////////////////////////////////
110 /// \brief This function allows reading the memory
111 /// on the specified address.
113 /// \param char* Address to read from
114 /// \param int Access type, should be 'w' (word),
115 /// 'h' (halfword), 'b'(byte)
116 /// \param unsigned long* pointer result variable
117 /////////////////////////////////////////////////
118 int readmem(char* address, int access_type, unsigned long *result)
121 void *map_base, *virt_addr;
123 unsigned long read_result = 0;
125 target = strtoul(address, 0, 0);
129 //Print error on failure
130 ERROR("Result Parameter is a nullpointer!");
135 // Try to open /dev/mem
136 if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
138 //Print error on failure
139 ERROR("Opening /dev/mem/ failed!");
144 map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
146 // Check if mapping was successful
147 if(map_base == (void *) -1)
149 ERROR("Mapping Memory Page Failed!");
153 virt_addr = map_base + (target & MAP_MASK);
157 read_result = *((unsigned char *) virt_addr);
160 read_result = *((unsigned short *) virt_addr);
163 read_result = *((unsigned long *) virt_addr);
166 ERROR("Wrong access_type set!");
170 if(munmap(map_base, MAP_SIZE) == -1)
172 ERROR("Unmapping Memory Page Failed!");
176 *result = read_result;