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.

66 lines
1.7KB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include "common.h"
  7. /* Push specified file to specified device blk device, starting from blkno
  8. * and upwards.
  9. */
  10. int main(int argc, char **argv)
  11. {
  12. if (argc != 4) {
  13. fprintf(stderr, "Usage: ./blkup device blkno fname\n");
  14. return 1;
  15. }
  16. unsigned int blkno = strtol(argv[2], NULL, 10);
  17. FILE *fp = fopen(argv[3], "r");
  18. if (!fp) {
  19. fprintf(stderr, "Can't open %s.\n", argv[3]);
  20. return 1;
  21. }
  22. int fd = ttyopen(argv[1]);
  23. if (fd < 0) {
  24. fprintf(stderr, "Could not open %s\n", argv[1]);
  25. return 1;
  26. }
  27. char s[0x40];
  28. char buf[1024] = {0};
  29. sendcmdp(fd, ": _ 1024 0 DO KEY DUP .x I BLK( + C! LOOP ;");
  30. int returncode = 0;
  31. while (fread(buf, 1, 1024, fp)) {
  32. sendcmd(fd, "_");
  33. for (int i=0; i<1024; i++) {
  34. putchar('.');
  35. fflush(stdout);
  36. write(fd, &buf[i], 1);
  37. usleep(1000); // let it breathe
  38. mread(fd, s, 2); // read hex pair
  39. s[2] = 0; // null terminate
  40. unsigned char c = strtol(s, NULL, 16);
  41. if (c != buf[i]) {
  42. // mismatch!
  43. fprintf(stderr, "Mismatch at bno %d (%d) %d != %d.\n", blkno, i, buf[i], c);
  44. // we don't exit now because we need to "consume" our whole program.
  45. returncode = 1;
  46. }
  47. usleep(1000); // let it breathe
  48. }
  49. readprompt(fd);
  50. if (returncode) break;
  51. memset(buf, 0, 1024);
  52. sprintf(s, "%d BLK> ! BLK!", blkno);
  53. sendcmdp(fd, s);
  54. blkno++;
  55. }
  56. sendcmdp(fd, "FORGET _");
  57. printf("Done!\n");
  58. fclose(fp);
  59. return returncode;
  60. }