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.

81 lines
2.2KB

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