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.

78 lines
2.0KB

  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. sprintf(s,
  42. ": _ 0x%04x 0x%04x DO KEY DUP .x I C! LOOP ; _",
  43. memptr+bytecount, memptr);
  44. sendcmd(fd, s);
  45. int returncode = 0;
  46. while (fread(s, 1, 1, fp)) {
  47. putc('.', stderr);
  48. fflush(stderr);
  49. unsigned char c = s[0];
  50. write(fd, &c, 1);
  51. usleep(1000); // let it breathe
  52. mread(fd, s, 2); // read hex pair
  53. s[2] = 0; // null terminate
  54. unsigned char c2 = strtol(s, NULL, 16);
  55. if (c != c2) {
  56. // mismatch!
  57. unsigned int pos = ftell(fp);
  58. fprintf(stderr, "Mismatch at byte %d! %d != %d.\n", pos, c, c2);
  59. // we don't exit now because we need to "consume" our whole program.
  60. returncode = 1;
  61. }
  62. usleep(1000); // let it breathe
  63. }
  64. readprompt(fd);
  65. sendcmdp(fd, "FORGET _");
  66. fprintf(stderr, "Done!\n");
  67. fclose(fp);
  68. if (fd > 0) {
  69. close(fd);
  70. }
  71. return returncode;
  72. }