2 * devmem2.c: Simple program to read/write from/to any location in memory.
4 * Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
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/)
13 * The author can be reached at:
16 * Information and Communication Theory Group
17 * Faculty of Information Technology and Systems
18 * Delft University of Technology
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.
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.
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
49 #include <sys/types.h>
54 #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
55 __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
57 #define MAP_SIZE 4096UL
58 #define MAP_MASK (MAP_SIZE - 1)
60 int main(int argc, char **argv) {
64 void *map_base, *virt_addr;
65 unsigned long read_result, writeval;
67 int access_type = 'w';
70 fprintf(stderr, "\nUsage:\t%s { address } { file }\n"
71 "\taddress : memory address to act upon\n"
72 "\tfile : file to load\n\n", argv[0]);
76 input_fd = open(argv[2], O_RDONLY);
77 if (input_fd < 0) FATAL;
80 if (fstat(input_fd, &st) < 0) FATAL;
82 char * buf = malloc(st.st_size);
84 if (read(input_fd, buf, st.st_size) != st.st_size) FATAL;
86 target = strtoul(argv[1], 0, 0);
88 fd = open("/dev/mem", O_RDWR | O_SYNC);
91 printf("/dev/mem opened.\n");
95 map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE,
96 MAP_SHARED, fd, target & ~MAP_MASK);
98 if (map_base == (void *) -1) FATAL;
100 printf("Memory mapped at address %p.\n", map_base);
103 virt_addr = map_base + (target & MAP_MASK);
104 memcpy(virt_addr, buf, st.st_size);
106 printf("Copied %d bytes.\n", st.st_size);