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 unsigned long Address to write to
51 /// \param unsigned long Value to write
52 /// \param int Access type, should be 'w' (word),
53 /// 'h' (halfword), 'b'(byte)
54 /////////////////////////////////////////////////
55 int writemem(unsigned long address, unsigned long writeval, int access_type)
61 off_t target = address;
63 // Try to open /dev/mem
64 if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
66 ERROR("Opening /dev/mem/ failed!");
71 map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
73 // Check if mapping was successful
74 if(map_base == (void *) -1)
76 //Print error on failure
77 ERROR("Mapping Memory Page Failed!");
81 //calculate virtual address
82 virt_addr = map_base + (target & MAP_MASK);
84 //write value to memory
88 *((unsigned char *) virt_addr) = writeval;
91 *((unsigned short *) virt_addr) = writeval;
94 *((unsigned long *) virt_addr) = writeval;
97 ERROR("Wrong access_type set!");
102 if(munmap(map_base, MAP_SIZE) == -1)
104 ERROR("Unmapping Memory Page Failed!");
112 /////////////////////////////////////////////////
113 /// \brief This function allows reading the memory
114 /// on the specified address.
116 /// \param char* Address to read from
117 /// \param int Access type, should be 'w' (word),
118 /// 'h' (halfword), 'b'(byte)
119 /// \param unsigned long* pointer result variable
120 /////////////////////////////////////////////////
121 int readmem(unsigned long address, int access_type, unsigned long *result)
124 void *map_base, *virt_addr;
125 unsigned long read_result = 0;
127 off_t target = address;
129 //check if result variable is a nullpointer
132 //Print error on failure
133 ERROR("Result Parameter is a nullpointer!");
138 // Try to open /dev/mem
139 if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
141 //Print error on failure
142 ERROR("Opening /dev/mem/ failed!");
147 map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
149 // Check if mapping was successful
150 if(map_base == (void *) -1)
152 ERROR("Mapping Memory Page Failed!");
156 //calculate virtual address
157 virt_addr = map_base + (target & MAP_MASK);
163 read_result = *((unsigned char *) virt_addr);
166 read_result = *((unsigned short *) virt_addr);
169 read_result = *((unsigned long *) virt_addr);
172 ERROR("Wrong access_type set!");
177 if(munmap(map_base, MAP_SIZE) == -1)
179 ERROR("Unmapping Memory Page Failed!");
184 *result = read_result;