You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. #include <string.h>
  43. #include <errno.h>
  44. #include <signal.h>
  45. #include <fcntl.h>
  46. #include <ctype.h>
  47. #include <termios.h>
  48. #include <sys/types.h>
  49. #include <sys/mman.h>
  50. #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
  51. __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
  52. #define MAP_SIZE 4096UL
  53. #define MAP_MASK (MAP_SIZE - 1)
  54. int main(int argc, char **argv) {
  55. int fd;
  56. void *map_base, *virt_addr;
  57. unsigned long read_result, writeval;
  58. off_t target;
  59. int access_type = 'w';
  60. if(argc < 2) {
  61. fprintf(stderr, "\nUsage:\t%s { address } [ type [ data ] ]\n"
  62. "\taddress : memory address to act upon\n"
  63. "\ttype : access operation type : [b]yte, [h]alfword, [w]ord\n"
  64. "\tdata : data to be written\n\n",
  65. argv[0]);
  66. exit(1);
  67. }
  68. target = strtoul(argv[1], 0, 0);
  69. if(argc > 2)
  70. access_type = tolower(argv[2][0]);
  71. if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
  72. printf("/dev/mem opened.\n");
  73. fflush(stdout);
  74. /* Map one page */
  75. map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
  76. if(map_base == (void *) -1) FATAL;
  77. printf("Memory mapped at address %p.\n", map_base);
  78. fflush(stdout);
  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. if(argc > 3) {
  97. writeval = strtoul(argv[3], 0, 0);
  98. switch(access_type) {
  99. case 'b':
  100. *((unsigned char *) virt_addr) = writeval;
  101. read_result = *((unsigned char *) virt_addr);
  102. break;
  103. case 'h':
  104. *((unsigned short *) virt_addr) = writeval;
  105. read_result = *((unsigned short *) virt_addr);
  106. break;
  107. case 'w':
  108. *((unsigned long *) virt_addr) = writeval;
  109. read_result = *((unsigned long *) virt_addr);
  110. break;
  111. }
  112. printf("Written 0x%X; readback 0x%X\n", writeval, read_result);
  113. fflush(stdout);
  114. }
  115. if(munmap(map_base, MAP_SIZE) == -1) FATAL;
  116. close(fd);
  117. return 0;
  118. }