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.

85 lines
2.3KB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include "common.h"
  6. /* Push specified file to specified device running Forth and verify
  7. * that the sent contents is correct.
  8. */
  9. int main(int argc, char **argv)
  10. {
  11. if (argc != 4) {
  12. fprintf(stderr, "Usage: ./upload device memptr fname\n");
  13. return 1;
  14. }
  15. unsigned int memptr = strtol(argv[2], NULL, 16);
  16. FILE *fp = fopen(argv[3], "r");
  17. if (!fp) {
  18. fprintf(stderr, "Can't open %s.\n", argv[3]);
  19. return 1;
  20. }
  21. fseek(fp, 0, SEEK_END);
  22. unsigned int bytecount = ftell(fp);
  23. fprintf(stderr, "memptr: 0x%04x bytecount: 0x%04x.\n", memptr, bytecount);
  24. if (!bytecount) {
  25. // Nothing to read
  26. fclose(fp);
  27. return 0;
  28. }
  29. if (memptr+bytecount > 0xffff) {
  30. fprintf(stderr, "memptr+bytecount out of range.\n");
  31. fclose(fp);
  32. return 1;
  33. }
  34. rewind(fp);
  35. int fd = ttyopen(argv[1]);
  36. if (fd < 0) {
  37. fprintf(stderr, "Could not open %s\n", argv[1]);
  38. return 1;
  39. }
  40. char s[0x40];
  41. sendcmdp(fd, ": K KEY DUP EMIT ;");
  42. // P: parse digit. We assume '0-9' or 'a-f' range and return a 0-15 value.
  43. sendcmdp(fd, ": P DUP '0' '9' =><= IF '0' ELSE 0x57 THEN - ;");
  44. // R: receive hex pairs. we receive values in hex pairs, re-emit them upon
  45. // reception, and then write them to memory.
  46. sprintf(s,
  47. ": R %d %d DO K P 16 * K P OR I C! LOOP ; R",
  48. memptr+bytecount, memptr);
  49. sendcmd(fd, s);
  50. int returncode = 0;
  51. while (fread(s, 1, 1, fp)) {
  52. unsigned char c1, c2;
  53. c1 = s[0];
  54. sprintf(s, "%02x", c1);
  55. for (int i=0; i<2; i++) {
  56. c1 = s[i];
  57. write(fd, &c1, 1);
  58. usleep(1000); // let it breathe
  59. mread(fd, &c2, 1); // read ping back
  60. if (c1 != c2) {
  61. // mismatch!
  62. unsigned int pos = ftell(fp);
  63. fprintf(stderr, "Mismatch at byte %d! %d != %d.\n", pos, c1, c2);
  64. // we don't exit now because we need to "consume" our whole program.
  65. returncode = 1;
  66. }
  67. }
  68. putc('.', stderr);
  69. fflush(stderr);
  70. }
  71. readprompt(fd);
  72. sendcmdp(fd, "FORGET K");
  73. fprintf(stderr, "Done!\n");
  74. fclose(fp);
  75. if (fd > 0) {
  76. close(fd);
  77. }
  78. return returncode;
  79. }