peek.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* peek command: hex dump of physical memory, including MMIO registers.
  2. *
  3. * peek <addr> [len] default len 64, max 256
  4. * poke <addr> <val> write one dword (e.g. an MMIO register), print
  5. * the read-back
  6. *
  7. * Addresses are physical. Lowmem is read through the identity map, RAM
  8. * above it (highmem) through a temporary kmap, and anything else (MMIO)
  9. * through its ioremap() window mapping, created on demand (4 KB,
  10. * uncached) when no driver mapped it yet.
  11. */
  12. #include "io.h"
  13. #include "paging.h"
  14. static void put_hex2(unsigned int v)
  15. {
  16. static const char hex[] = "0123456789ABCDEF";
  17. io_putchar(hex[(v >> 4) & 0xF]);
  18. io_putchar(hex[v & 0xF]);
  19. }
  20. static void put_hex8(unsigned int v)
  21. {
  22. put_hex2((v >> 24) & 0xFF);
  23. put_hex2((v >> 16) & 0xFF);
  24. put_hex2((v >> 8) & 0xFF);
  25. put_hex2(v & 0xFF);
  26. }
  27. static int hexval(char c)
  28. {
  29. if (c >= '0' && c <= '9')
  30. return c - '0';
  31. if (c >= 'a' && c <= 'f')
  32. return c - 'a' + 10;
  33. if (c >= 'A' && c <= 'F')
  34. return c - 'A' + 10;
  35. return -1;
  36. }
  37. static int parse_hex(const char *s, unsigned int *out, const char **endp)
  38. {
  39. unsigned int v = 0;
  40. int digits = 0, d;
  41. if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
  42. s += 2;
  43. while ((d = hexval(*s)) >= 0) {
  44. v = (v << 4) | (unsigned int)d;
  45. digits++;
  46. s++;
  47. }
  48. if (!digits)
  49. return -1;
  50. *out = v;
  51. *endp = s;
  52. return 0;
  53. }
  54. void program_peek_main(const char *arg)
  55. {
  56. const char *p = arg;
  57. const volatile unsigned char *mem;
  58. unsigned int addr, len = 64, i, j;
  59. if (parse_hex(p, &addr, &p) != 0) {
  60. io_putstr("peek: usage peek <hexaddr> [len<=256]\n");
  61. return;
  62. }
  63. while (*p == ' ')
  64. p++;
  65. if (*p) {
  66. if (parse_hex(p, &len, &p) != 0 || len == 0 || len > 256) {
  67. io_putstr("peek: usage peek <hexaddr> [len<=256]\n");
  68. return;
  69. }
  70. }
  71. while (*p == ' ')
  72. p++;
  73. if (*p) {
  74. io_putstr("peek: usage peek <hexaddr> [len<=256]\n");
  75. return;
  76. }
  77. for (i = 0; i < len; i += 16) {
  78. put_hex8(addr + i);
  79. io_putstr(": ");
  80. for (j = 0; j < 16 && i + j < len; j++) {
  81. void *km;
  82. mem = paging_debug_map(addr + i + j, &km);
  83. if (!mem) {
  84. io_putstr("?? ");
  85. continue;
  86. }
  87. put_hex2(*mem);
  88. if (km)
  89. kunmap(km);
  90. io_putchar(' ');
  91. }
  92. io_putchar('\n');
  93. }
  94. }
  95. /* poke <addr> <val>: write one 32-bit dword and print the read-back. Used to
  96. * drive MMIO experiments (e.g. the GPU's PIO method interface) from the
  97. * shell. */
  98. void program_poke_main(const char *arg)
  99. {
  100. const char *p = arg;
  101. volatile unsigned int *mem;
  102. unsigned int addr, val, back;
  103. if (parse_hex(p, &addr, &p) != 0) {
  104. io_putstr("poke: usage poke <hexaddr> <hexval>\n");
  105. return;
  106. }
  107. while (*p == ' ')
  108. p++;
  109. if (parse_hex(p, &val, &p) != 0) {
  110. io_putstr("poke: usage poke <hexaddr> <hexval>\n");
  111. return;
  112. }
  113. while (*p == ' ')
  114. p++;
  115. if (*p) {
  116. io_putstr("poke: usage poke <hexaddr> <hexval>\n");
  117. return;
  118. }
  119. {
  120. void *km;
  121. mem = (volatile unsigned int *)paging_debug_map(addr & ~3u, &km);
  122. if (!mem) {
  123. io_putstr("poke: cannot map that address\n");
  124. return;
  125. }
  126. *mem = val;
  127. back = *mem;
  128. if (km)
  129. kunmap(km);
  130. }
  131. io_putstr("poke ");
  132. put_hex8(addr & ~3u);
  133. io_putstr(" <- ");
  134. put_hex8(val);
  135. io_putstr(" reads ");
  136. put_hex8(back);
  137. io_putchar('\n');
  138. }