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.

68 lines
1.8KB

  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 the BASIC shell 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);
  36. char s[0x40];
  37. sprintf(s, "m=0x%04x", memptr);
  38. sendcmdp(fd, s);
  39. sprintf(s, "while m<0x%04x getc:puth a:poke m a:m=m+1", memptr+bytecount);
  40. sendcmd(fd, s);
  41. int returncode = 0;
  42. while (fread(s, 1, 1, fp)) {
  43. putchar('.');
  44. fflush(stdout);
  45. unsigned char c = s[0];
  46. write(fd, &c, 1);
  47. usleep(1000); // let it breathe
  48. read(fd, s, 2); // read hex pair
  49. s[2] = 0; // null terminate
  50. unsigned char c2 = strtol(s, NULL, 16);
  51. if (c != c2) {
  52. // mismatch!
  53. unsigned int pos = ftell(fp);
  54. fprintf(stderr, "Mismatch at byte %d! %d != %d.\n", pos, c, c2);
  55. // we don't exit now because we need to "consume" our whole program.
  56. returncode = 1;
  57. }
  58. }
  59. printf("Done!\n");
  60. fclose(fp);
  61. return returncode;
  62. }