Mirror of CollapseOS
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

66 рядки
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 (memptr+bytecount > 0xffff) {
  25. fprintf(stderr, "memptr+bytecount out of range.\n");
  26. fclose(fp);
  27. return 1;
  28. }
  29. rewind(fp);
  30. int fd = open(argv[1], O_RDWR|O_NOCTTY);
  31. char s[0x10];
  32. sprintf(s, "m=0x%04x", memptr);
  33. sendcmd(fd, s);
  34. read(fd, s, 2); // read prompt
  35. while (fread(s, 1, 1, fp)) {
  36. putchar('.');
  37. fflush(stdout);
  38. unsigned char c = s[0];
  39. sendcmd(fd, "getc");
  40. write(fd, &c, 1);
  41. read(fd, s, 2); // read prompt
  42. sendcmd(fd, "puth a");
  43. read(fd, s, 2); // read hex pair
  44. s[2] = 0; // null terminate
  45. unsigned char c2 = strtol(s, NULL, 16);
  46. read(fd, s, 2); // read prompt
  47. if (c != c2) {
  48. // mismatch!
  49. unsigned int pos = ftell(fp);
  50. fprintf(stderr, "Mismatch at byte %d! %d != %d.\n", pos, c, c2);
  51. return 1;
  52. }
  53. sendcmd(fd, "poke m a");
  54. read(fd, s, 2); // read prompt
  55. sendcmd(fd, "m=m+1");
  56. read(fd, s, 2); // read prompt
  57. }
  58. printf("Done!\n");
  59. return 0;
  60. }