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.

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