Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.8KB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. /* Read specified number of bytes at specified memory address through a BASIC
  6. * remote shell and dump it to stdout.
  7. */
  8. void sendcmd(int fd, char *cmd)
  9. {
  10. char junk[2];
  11. while (*cmd) {
  12. write(fd, cmd, 1);
  13. read(fd, &junk, 1);
  14. cmd++;
  15. // The other side is sometimes much slower than us and if we don't let
  16. // it breathe, it can choke.
  17. usleep(1000);
  18. }
  19. write(fd, "\n", 1);
  20. read(fd, &junk, 2); // sends back \r\n
  21. usleep(1000);
  22. }
  23. int main(int argc, char **argv)
  24. {
  25. if (argc != 4) {
  26. fprintf(stderr, "Usage: ./memdump device memptr bytecount\n");
  27. return 1;
  28. }
  29. unsigned int memptr = strtol(argv[2], NULL, 16);
  30. unsigned int bytecount = strtol(argv[3], NULL, 16);
  31. fprintf(stderr, "memptr: 0x%04x bytecount: 0x%04x.\n", memptr, bytecount);
  32. if (memptr+bytecount > 0xffff) {
  33. fprintf(stderr, "memptr+bytecount out of range.\n");
  34. return 1;
  35. }
  36. if (!bytecount) {
  37. // nothing to spit
  38. return 0;
  39. }
  40. int fd = open(argv[1], O_RDWR|O_NOCTTY);
  41. char s[0x20];
  42. sprintf(s, "m=0x%04x", memptr);
  43. sendcmd(fd, s);
  44. read(fd, s, 2); // read prompt
  45. for (int i=0; i<bytecount; i++) {
  46. sendcmd(fd, "peek m");
  47. read(fd, s, 2); // read prompt
  48. sendcmd(fd, "print a");
  49. for (int j=0; j<3; j++) {
  50. read(fd, s+j, 1);
  51. s[j+1] = 0; // always null-terminate
  52. if (s[j] < '0') {
  53. break;
  54. }
  55. }
  56. unsigned char c = strtol(s, NULL, 10);
  57. putchar(c);
  58. read(fd, s, 3); // read \r\n + prompt - last char
  59. sendcmd(fd, "m=m+1");
  60. read(fd, s, 2); // read prompt
  61. }
  62. return 0;
  63. }