dbg.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* Serial debug monitor. Entered via the `dbg` command and driven from the host
  2. * with `make monitor` (picocom). ASCII commands, newline-terminated, hex args:
  3. *
  4. * r <addr> <len> read len bytes at addr -> hex dump
  5. * w <addr> <b0> [b1 ...] write bytes at addr
  6. * i <port> inb (read I/O port)
  7. * o <port> <val> outb (write I/O port)
  8. * h help
  9. * q quit
  10. *
  11. * Runs inline (blocks the terminal), so it owns the serial ring buffer and
  12. * doesn't conflict with the kexec auto-detect in the terminal idle loop.
  13. */
  14. #include "dbg.h"
  15. #include "serial.h"
  16. #define DBG_LINE_MAX 64
  17. #define DBG_NARGS 32
  18. #define DBG_READ_MAX 256
  19. static inline unsigned char dbg_inb(unsigned short port)
  20. {
  21. unsigned char v;
  22. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  23. return v;
  24. }
  25. static inline void dbg_outb(unsigned short port, unsigned char v)
  26. {
  27. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  28. }
  29. static void dbg_putbyte(unsigned char b)
  30. {
  31. static const char hex[] = "0123456789ABCDEF";
  32. serial_putc(SERIAL_COM1, hex[b >> 4]);
  33. serial_putc(SERIAL_COM1, hex[b & 0xF]);
  34. }
  35. /* Block until a byte arrives (echoing it so picocom works without local echo). */
  36. static void dbg_getc(unsigned char *c)
  37. {
  38. for (;;) {
  39. if (!serial_ring_wait(0xFFFFFFFFu, c))
  40. continue; /* timeout (effectively never) */
  41. if (*c == '\r')
  42. ; /* ignore bare CR */
  43. else if (*c == '\n')
  44. serial_puts(SERIAL_COM1, "\n");
  45. else
  46. serial_putc(SERIAL_COM1, (char)*c);
  47. return;
  48. }
  49. }
  50. static void dbg_read_line(char *buf, unsigned int max)
  51. {
  52. unsigned int i = 0;
  53. unsigned char c;
  54. for (;;) {
  55. dbg_getc(&c);
  56. if (c == '\n' || c == '\r')
  57. break;
  58. if (i + 1 < max)
  59. buf[i++] = (char)c;
  60. }
  61. buf[i] = '\0';
  62. }
  63. /* Parse a hex number at *s (optional 0x prefix); advance *s past it. */
  64. static int dbg_parse_hex(const char **s, unsigned int *out)
  65. {
  66. const char *p = *s;
  67. unsigned int v = 0;
  68. int digits = 0;
  69. if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
  70. p += 2;
  71. for (;; p++) {
  72. unsigned int d;
  73. char c = *p;
  74. if (c >= '0' && c <= '9') d = (unsigned int)(c - '0');
  75. else if (c >= 'a' && c <= 'f') d = (unsigned int)(c - 'a' + 10);
  76. else if (c >= 'A' && c <= 'F') d = (unsigned int)(c - 'A' + 10);
  77. else break;
  78. v = (v << 4) | d;
  79. digits++;
  80. }
  81. if (!digits)
  82. return 0;
  83. *out = v;
  84. *s = p;
  85. return 1;
  86. }
  87. /* Returns 1 to quit. */
  88. static int dbg_command(const char *line)
  89. {
  90. const char *p = line;
  91. unsigned int args[DBG_NARGS];
  92. unsigned int nargs = 0;
  93. char cmd;
  94. while (*p == ' ')
  95. p++;
  96. if (!*p)
  97. return 0;
  98. cmd = *p++;
  99. while (nargs < DBG_NARGS) {
  100. while (*p == ' ')
  101. p++;
  102. if (!*p)
  103. break;
  104. if (!dbg_parse_hex(&p, &args[nargs]))
  105. break;
  106. nargs++;
  107. }
  108. switch (cmd) {
  109. case 'r':
  110. if (nargs < 2) {
  111. serial_puts(SERIAL_COM1, "usage: r <addr> <len>\n");
  112. } else {
  113. unsigned int addr = args[0];
  114. unsigned int len = args[1];
  115. unsigned int i;
  116. if (len > DBG_READ_MAX)
  117. len = DBG_READ_MAX;
  118. serial_puthex(SERIAL_COM1, addr);
  119. serial_putc(SERIAL_COM1, ':');
  120. for (i = 0; i < len; i++) {
  121. serial_putc(SERIAL_COM1, ' ');
  122. dbg_putbyte(((volatile unsigned char *)addr)[i]);
  123. }
  124. serial_putc(SERIAL_COM1, '\n');
  125. }
  126. return 0;
  127. case 'w':
  128. if (nargs < 2) {
  129. serial_puts(SERIAL_COM1, "usage: w <addr> <b0> [b1 ...]\n");
  130. } else {
  131. unsigned int addr = args[0];
  132. unsigned int i;
  133. for (i = 1; i < nargs; i++)
  134. ((volatile unsigned char *)addr)[i - 1] = (unsigned char)args[i];
  135. serial_puts(SERIAL_COM1, "ok\n");
  136. }
  137. return 0;
  138. case 'i':
  139. if (nargs < 1) {
  140. serial_puts(SERIAL_COM1, "usage: i <port>\n");
  141. } else {
  142. serial_puthex(SERIAL_COM1, args[0]);
  143. serial_puts(SERIAL_COM1, " = ");
  144. dbg_putbyte(dbg_inb((unsigned short)args[0]));
  145. serial_putc(SERIAL_COM1, '\n');
  146. }
  147. return 0;
  148. case 'o':
  149. if (nargs < 2) {
  150. serial_puts(SERIAL_COM1, "usage: o <port> <val>\n");
  151. } else {
  152. dbg_outb((unsigned short)args[0], (unsigned char)args[1]);
  153. serial_puts(SERIAL_COM1, "ok\n");
  154. }
  155. return 0;
  156. case 'h':
  157. serial_puts(SERIAL_COM1,
  158. "r <addr> <len> read memory (hex dump)\n"
  159. "w <addr> <b>... write bytes\n"
  160. "i <port> inb\n"
  161. "o <port> <val> outb\n"
  162. "h help\n"
  163. "q quit\n");
  164. return 0;
  165. case 'q':
  166. return 1;
  167. default:
  168. serial_puts(SERIAL_COM1, "unknown cmd (h for help)\n");
  169. return 0;
  170. }
  171. }
  172. void program_dbg_main(void)
  173. {
  174. char line[DBG_LINE_MAX];
  175. serial_puts(SERIAL_COM1, "\ndbg: ready (h for help)\n");
  176. for (;;) {
  177. serial_puts(SERIAL_COM1, "dbg> ");
  178. dbg_read_line(line, DBG_LINE_MAX);
  179. if (dbg_command(line))
  180. break;
  181. }
  182. serial_puts(SERIAL_COM1, "dbg: bye\n");
  183. }