3
0

devmem.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  3. * Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
  4. * Copyright (C) 2008, BusyBox Team. -solar 4/26/08
  5. */
  6. #include "libbb.h"
  7. int devmem_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  8. int devmem_main(int argc UNUSED_PARAM, char **argv)
  9. {
  10. void *map_base, *virt_addr;
  11. uint64_t read_result;
  12. uint64_t writeval = writeval; /* for compiler */
  13. off_t target;
  14. unsigned page_size = getpagesize();
  15. int fd;
  16. int width = 8 * sizeof(int);
  17. /* devmem ADDRESS [WIDTH [VALUE]] */
  18. // TODO: options?
  19. // -r: read and output only the value in hex, with 0x prefix
  20. // -w: write only, no reads before or after, and no output
  21. // or make this behavior default?
  22. // Let's try this and see how users react.
  23. /* ADDRESS */
  24. if (!argv[1])
  25. bb_show_usage();
  26. errno = 0;
  27. target = bb_strtoull(argv[1], NULL, 0); /* allows hex, oct etc */
  28. /* WIDTH */
  29. if (argv[2]) {
  30. if (isdigit(argv[2][0]) || argv[2][1])
  31. width = xatou(argv[2]);
  32. else {
  33. static const char bhwl[] ALIGN1 = "bhwl";
  34. static const uint8_t sizes[] ALIGN1 = {
  35. 8 * sizeof(char),
  36. 8 * sizeof(short),
  37. 8 * sizeof(int),
  38. 8 * sizeof(long),
  39. 0 /* bad */
  40. };
  41. width = strchrnul(bhwl, (argv[2][0] | 0x20)) - bhwl;
  42. width = sizes[width];
  43. }
  44. /* VALUE */
  45. if (argv[3])
  46. writeval = bb_strtoull(argv[3], NULL, 0);
  47. } else { /* argv[2] == NULL */
  48. /* make argv[3] to be a valid thing to use */
  49. argv--;
  50. }
  51. if (errno)
  52. bb_show_usage(); /* bb_strtouXX failed */
  53. fd = xopen("/dev/mem", argv[3] ? (O_RDWR | O_SYNC) : (O_RDONLY | O_SYNC));
  54. map_base = mmap(NULL,
  55. page_size * 2 /* in case value spans page */,
  56. argv[3] ? (PROT_READ | PROT_WRITE) : PROT_READ,
  57. MAP_SHARED,
  58. fd,
  59. target & ~(off_t)(page_size - 1));
  60. if (map_base == MAP_FAILED)
  61. bb_perror_msg_and_die("mmap");
  62. // printf("Memory mapped at address %p.\n", map_base);
  63. virt_addr = (char*)map_base + (target & (page_size - 1));
  64. if (!argv[3]) {
  65. switch (width) {
  66. case 8:
  67. read_result = *(volatile uint8_t*)virt_addr;
  68. break;
  69. case 16:
  70. read_result = *(volatile uint16_t*)virt_addr;
  71. break;
  72. case 32:
  73. read_result = *(volatile uint32_t*)virt_addr;
  74. break;
  75. case 64:
  76. read_result = *(volatile uint64_t*)virt_addr;
  77. break;
  78. default:
  79. bb_error_msg_and_die("bad width");
  80. }
  81. // printf("Value at address 0x%"OFF_FMT"X (%p): 0x%llX\n",
  82. // target, virt_addr,
  83. // (unsigned long long)read_result);
  84. /* Zero-padded output shows the width of access just done */
  85. printf("0x%0*llX\n", (width >> 2), (unsigned long long)read_result);
  86. } else {
  87. switch (width) {
  88. case 8:
  89. *(volatile uint8_t*)virt_addr = writeval;
  90. // read_result = *(volatile uint8_t*)virt_addr;
  91. break;
  92. case 16:
  93. *(volatile uint16_t*)virt_addr = writeval;
  94. // read_result = *(volatile uint16_t*)virt_addr;
  95. break;
  96. case 32:
  97. *(volatile uint32_t*)virt_addr = writeval;
  98. // read_result = *(volatile uint32_t*)virt_addr;
  99. break;
  100. case 64:
  101. *(volatile uint64_t*)virt_addr = writeval;
  102. // read_result = *(volatile uint64_t*)virt_addr;
  103. break;
  104. default:
  105. bb_error_msg_and_die("bad width");
  106. }
  107. // printf("Written 0x%llX; readback 0x%llX\n",
  108. // (unsigned long long)writeval,
  109. // (unsigned long long)read_result);
  110. }
  111. if (ENABLE_FEATURE_CLEAN_UP) {
  112. if (munmap(map_base, page_size * 2) == -1)
  113. bb_perror_msg_and_die("munmap");
  114. close(fd);
  115. }
  116. return EXIT_SUCCESS;
  117. }