Branch oldpackages for 14.07
[14.07/packages.git] / utils / devmem2 / src / devmem2.c
1 /*
2  * devmem2.c: Simple program to read/write from/to any location in memory.
3  *
4  *  Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
5  *
6  *
7  * This software has been developed for the LART computing board
8  * (http://www.lart.tudelft.nl/). The development has been sponsored by
9  * the Mobile MultiMedia Communications (http://www.mmc.tudelft.nl/)
10  * and Ubiquitous Communications (http://www.ubicom.tudelft.nl/)
11  * projects.
12  *
13  * The author can be reached at:
14  *
15  *  Jan-Derk Bakker
16  *  Information and Communication Theory Group
17  *  Faculty of Information Technology and Systems
18  *  Delft University of Technology
19  *  P.O. Box 5031
20  *  2600 GA Delft
21  *  The Netherlands
22  *
23  *
24  * This program is free software; you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation; either version 2 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  * 
34  * You should have received a copy of the GNU General Public License
35  * along with this program; if not, write to the Free Software
36  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
37  *
38  */
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <string.h>
44 #include <errno.h>
45 #include <signal.h>
46 #include <fcntl.h>
47 #include <ctype.h>
48 #include <termios.h>
49 #include <sys/types.h>
50 #include <sys/mman.h>
51   
52 #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
53   __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
54  
55 #define MAP_SIZE 4096UL
56 #define MAP_MASK (MAP_SIZE - 1)
57
58 int main(int argc, char **argv) {
59         int fd;
60         void *map_base, *virt_addr; 
61         unsigned long read_result, writeval;
62         off_t target;
63         int access_type = 'w';
64         
65         if(argc < 2) {
66                 fprintf(stderr, "\nUsage:\t%s { address } [ type [ data ] ]\n"
67                         "\taddress : memory address to act upon\n"
68                         "\ttype : access operation type : [b]yte, [h]alfword, [w]ord\n"
69                         "\tdata : data to be written\n\n",
70                         argv[0]);
71                 exit(1);
72         }
73         target = strtoul(argv[1], 0, 0);
74
75         if(argc > 2)
76                 access_type = tolower(argv[2][0]);
77
78
79         if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
80         printf("/dev/mem opened.\n"); 
81         fflush(stdout);
82         
83         /* Map one page */
84         map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
85         if(map_base == (void *) -1) FATAL;
86         printf("Memory mapped at address %p.\n", map_base); 
87         fflush(stdout);
88         
89         virt_addr = map_base + (target & MAP_MASK);
90         switch(access_type) {
91                 case 'b':
92                         read_result = *((unsigned char *) virt_addr);
93                         break;
94                 case 'h':
95                         read_result = *((unsigned short *) virt_addr);
96                         break;
97                 case 'w':
98                         read_result = *((unsigned long *) virt_addr);
99                         break;
100                 default:
101                         fprintf(stderr, "Illegal data type '%c'.\n", access_type);
102                         exit(2);
103         }
104         printf("Value at address 0x%lX (%p): 0x%lX\n", target, virt_addr, read_result); 
105         fflush(stdout);
106
107         if(argc > 3) {
108                 writeval = strtoul(argv[3], 0, 0);
109                 switch(access_type) {
110                         case 'b':
111                                 *((unsigned char *) virt_addr) = writeval;
112                                 read_result = *((unsigned char *) virt_addr);
113                                 break;
114                         case 'h':
115                                 *((unsigned short *) virt_addr) = writeval;
116                                 read_result = *((unsigned short *) virt_addr);
117                                 break;
118                         case 'w':
119                                 *((unsigned long *) virt_addr) = writeval;
120                                 read_result = *((unsigned long *) virt_addr);
121                                 break;
122                 }
123                 printf("Written 0x%lX; readback 0x%lX\n", writeval, read_result); 
124                 fflush(stdout);
125         }
126         
127         if(munmap(map_base, MAP_SIZE) == -1) FATAL;
128         close(fd);
129         return 0;
130 }
131
132