peek.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* peek command: hex dump of memory (identity-mapped, so this also covers
  2. * MMIO registers, e.g. a driver's BAR once mapped).
  3. *
  4. * peek <addr> [len] default len 64, max 256
  5. * poke <addr> <val> write one dword (e.g. an MMIO register), print
  6. * the read-back
  7. *
  8. * Reading an unmapped address faults; the exception handler shows EIP/CR2.
  9. */
  10. #include "io.h"
  11. static void put_hex2(unsigned int v)
  12. {
  13. static const char hex[] = "0123456789ABCDEF";
  14. io_putchar(hex[(v >> 4) & 0xF]);
  15. io_putchar(hex[v & 0xF]);
  16. }
  17. static void put_hex8(unsigned int v)
  18. {
  19. put_hex2((v >> 24) & 0xFF);
  20. put_hex2((v >> 16) & 0xFF);
  21. put_hex2((v >> 8) & 0xFF);
  22. put_hex2(v & 0xFF);
  23. }
  24. static int hexval(char c)
  25. {
  26. if (c >= '0' && c <= '9')
  27. return c - '0';
  28. if (c >= 'a' && c <= 'f')
  29. return c - 'a' + 10;
  30. if (c >= 'A' && c <= 'F')
  31. return c - 'A' + 10;
  32. return -1;
  33. }
  34. static int parse_hex(const char *s, unsigned int *out, const char **endp)
  35. {
  36. unsigned int v = 0;
  37. int digits = 0, d;
  38. if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
  39. s += 2;
  40. while ((d = hexval(*s)) >= 0) {
  41. v = (v << 4) | (unsigned int)d;
  42. digits++;
  43. s++;
  44. }
  45. if (!digits)
  46. return -1;
  47. *out = v;
  48. *endp = s;
  49. return 0;
  50. }
  51. void program_peek_main(const char *arg)
  52. {
  53. const char *p = arg;
  54. const volatile unsigned char *mem;
  55. unsigned int addr, len = 64, i, j;
  56. if (parse_hex(p, &addr, &p) != 0) {
  57. io_putstr("peek: usage peek <hexaddr> [len<=256]\n");
  58. return;
  59. }
  60. while (*p == ' ')
  61. p++;
  62. if (*p) {
  63. if (parse_hex(p, &len, &p) != 0 || len == 0 || len > 256) {
  64. io_putstr("peek: usage peek <hexaddr> [len<=256]\n");
  65. return;
  66. }
  67. }
  68. while (*p == ' ')
  69. p++;
  70. if (*p) {
  71. io_putstr("peek: usage peek <hexaddr> [len<=256]\n");
  72. return;
  73. }
  74. mem = (const volatile unsigned char *)addr;
  75. for (i = 0; i < len; i += 16) {
  76. put_hex8(addr + i);
  77. io_putstr(": ");
  78. for (j = 0; j < 16 && i + j < len; j++) {
  79. put_hex2(mem[i + j]);
  80. io_putchar(' ');
  81. }
  82. io_putchar('\n');
  83. }
  84. }
  85. /* poke <addr> <val>: write one 32-bit dword and print the read-back. Used to
  86. * drive MMIO experiments (e.g. the GPU's PIO method interface) from the
  87. * shell. */
  88. void program_poke_main(const char *arg)
  89. {
  90. const char *p = arg;
  91. volatile unsigned int *mem;
  92. unsigned int addr, val;
  93. if (parse_hex(p, &addr, &p) != 0) {
  94. io_putstr("poke: usage poke <hexaddr> <hexval>\n");
  95. return;
  96. }
  97. while (*p == ' ')
  98. p++;
  99. if (parse_hex(p, &val, &p) != 0) {
  100. io_putstr("poke: usage poke <hexaddr> <hexval>\n");
  101. return;
  102. }
  103. while (*p == ' ')
  104. p++;
  105. if (*p) {
  106. io_putstr("poke: usage poke <hexaddr> <hexval>\n");
  107. return;
  108. }
  109. mem = (volatile unsigned int *)(addr & ~3u);
  110. *mem = val;
  111. io_putstr("poke ");
  112. put_hex8(addr & ~3u);
  113. io_putstr(" <- ");
  114. put_hex8(val);
  115. io_putstr(" reads ");
  116. put_hex8(*mem);
  117. io_putchar('\n');
  118. }