devmem.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
  3. * Copyright (C) 2008, BusyBox Team. -solar 4/26/08
  4. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  5. */
  6. //config:config DEVMEM
  7. //config: bool "devmem (2.5 kb)"
  8. //config: default y
  9. //config: help
  10. //config: devmem is a small program that reads and writes from physical
  11. //config: memory using /dev/mem.
  12. //applet:IF_DEVMEM(APPLET(devmem, BB_DIR_SBIN, BB_SUID_DROP))
  13. //kbuild:lib-$(CONFIG_DEVMEM) += devmem.o
  14. //usage:#define devmem_trivial_usage
  15. //usage: "ADDRESS [WIDTH [VALUE]]"
  16. //usage:#define devmem_full_usage "\n\n"
  17. //usage: "Read/write from physical address\n"
  18. //usage: "\n ADDRESS Address to act upon"
  19. //usage: "\n WIDTH Width (8/16/...)"
  20. //usage: "\n VALUE Data to be written"
  21. #include "libbb.h"
  22. int devmem_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  23. int devmem_main(int argc UNUSED_PARAM, char **argv)
  24. {
  25. void *map_base, *virt_addr;
  26. uint64_t read_result;
  27. off_t target;
  28. unsigned page_size, mapped_size, offset_in_page;
  29. int fd;
  30. unsigned width = 8 * sizeof(int);
  31. /* devmem ADDRESS [WIDTH [VALUE]] */
  32. // TODO: options?
  33. // -r: read and output only the value in hex, with 0x prefix
  34. // -w: write only, no reads before or after, and no output
  35. // or make this behavior default?
  36. // Let's try this and see how users react.
  37. /* ADDRESS */
  38. if (!argv[1])
  39. bb_show_usage();
  40. errno = 0;
  41. target = bb_strtoull(argv[1], NULL, 0); /* allows hex, oct etc */
  42. /* WIDTH */
  43. if (argv[2]) {
  44. if (isdigit(argv[2][0]) || argv[2][1])
  45. width = xatou(argv[2]);
  46. else {
  47. static const char bhwl[] ALIGN1 = "bhwl";
  48. static const uint8_t sizes[] ALIGN1 = {
  49. 8 * sizeof(char),
  50. 8 * sizeof(short),
  51. 8 * sizeof(int),
  52. 8 * sizeof(long),
  53. 0 /* bad */
  54. };
  55. width = strchrnul(bhwl, (argv[2][0] | 0x20)) - bhwl;
  56. width = sizes[width];
  57. }
  58. } else { /* argv[2] == NULL */
  59. /* make argv[3] to be a valid thing to fetch */
  60. argv--;
  61. }
  62. if (errno)
  63. bb_show_usage(); /* one of bb_strtouXX failed */
  64. fd = xopen("/dev/mem", argv[3] ? (O_RDWR | O_SYNC) : (O_RDONLY | O_SYNC));
  65. mapped_size = page_size = bb_getpagesize();
  66. offset_in_page = (unsigned)target & (page_size - 1);
  67. if (offset_in_page + width > page_size) {
  68. /* This access spans pages.
  69. * Must map two pages to make it possible: */
  70. mapped_size *= 2;
  71. }
  72. map_base = mmap(NULL,
  73. mapped_size,
  74. argv[3] ? (PROT_READ | PROT_WRITE) : PROT_READ,
  75. MAP_SHARED,
  76. fd,
  77. target & ~(off_t)(page_size - 1));
  78. if (map_base == MAP_FAILED)
  79. bb_simple_perror_msg_and_die("mmap");
  80. // printf("Memory mapped at address %p.\n", map_base);
  81. virt_addr = (char*)map_base + offset_in_page;
  82. if (!argv[3]) {
  83. #ifdef __SIZEOF_INT128__
  84. if (width == 128) {
  85. unsigned __int128 rd =
  86. *(volatile unsigned __int128 *)virt_addr;
  87. printf("0x%016llX%016llX\n",
  88. (unsigned long long)(uint64_t)(rd >> 64),
  89. (unsigned long long)(uint64_t)rd
  90. );
  91. } else
  92. #endif
  93. {
  94. switch (width) {
  95. case 8:
  96. read_result = *(volatile uint8_t*)virt_addr;
  97. break;
  98. case 16:
  99. read_result = *(volatile uint16_t*)virt_addr;
  100. break;
  101. case 32:
  102. read_result = *(volatile uint32_t*)virt_addr;
  103. break;
  104. case 64:
  105. read_result = *(volatile uint64_t*)virt_addr;
  106. break;
  107. default:
  108. bb_simple_error_msg_and_die("bad width");
  109. }
  110. // printf("Value at address 0x%"OFF_FMT"X (%p): 0x%llX\n",
  111. // target, virt_addr,
  112. // (unsigned long long)read_result);
  113. /* Zero-padded output shows the width of access just done */
  114. printf("0x%0*llX\n", (width >> 2), (unsigned long long)read_result);
  115. }
  116. } else {
  117. /* parse VALUE */
  118. #ifdef __SIZEOF_INT128__
  119. unsigned __int128 writeval = strtoumax(argv[3], NULL, 0);
  120. #else
  121. uint64_t writeval = bb_strtoull(argv[3], NULL, 0);
  122. #endif
  123. switch (width) {
  124. case 8:
  125. *(volatile uint8_t*)virt_addr = writeval;
  126. // read_result = *(volatile uint8_t*)virt_addr;
  127. break;
  128. case 16:
  129. *(volatile uint16_t*)virt_addr = writeval;
  130. // read_result = *(volatile uint16_t*)virt_addr;
  131. break;
  132. case 32:
  133. *(volatile uint32_t*)virt_addr = writeval;
  134. // read_result = *(volatile uint32_t*)virt_addr;
  135. break;
  136. case 64:
  137. *(volatile uint64_t*)virt_addr = writeval;
  138. // read_result = *(volatile uint64_t*)virt_addr;
  139. break;
  140. #ifdef __SIZEOF_INT128__
  141. case 128:
  142. *(volatile unsigned __int128 *)virt_addr = writeval;
  143. // read_result = *(volatile uint64_t*)virt_addr;
  144. break;
  145. #endif
  146. default:
  147. bb_simple_error_msg_and_die("bad width");
  148. }
  149. // printf("Written 0x%llX; readback 0x%llX\n",
  150. // (unsigned long long)writeval,
  151. // (unsigned long long)read_result);
  152. }
  153. if (ENABLE_FEATURE_CLEAN_UP) {
  154. if (munmap(map_base, mapped_size) == -1)
  155. bb_simple_perror_msg_and_die("munmap");
  156. close(fd);
  157. }
  158. return EXIT_SUCCESS;
  159. }