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