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 <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 = open(argv[1], O_RDWR|O_NOCTTY|O_SYNC);
  23. if (fd < 0) {
  24. fprintf(stderr, "Could not open %s\n", argv[1]);
  25. return 1;
  26. }
  27. set_interface_attribs(fd, 0, 0);
  28. set_blocking(fd, 1);
  29. char s[0x40];
  30. char buf[1024] = {0};
  31. sendcmdp(fd, ": _ 1024 0 DO KEY DUP .x I BLK( + C! LOOP ;");
  32. int returncode = 0;
  33. while (fread(buf, 1, 1024, fp)) {
  34. sendcmd(fd, "_");
  35. for (int i=0; i<1024; i++) {
  36. putchar('.');
  37. fflush(stdout);
  38. write(fd, &buf[i], 1);
  39. usleep(1000); // let it breathe
  40. mread(fd, s, 2); // read hex pair
  41. s[2] = 0; // null terminate
  42. unsigned char c = strtol(s, NULL, 16);
  43. if (c != buf[i]) {
  44. // mismatch!
  45. fprintf(stderr, "Mismatch at bno %d (%d) %d != %d.\n", blkno, i, buf[i], c);
  46. // we don't exit now because we need to "consume" our whole program.
  47. returncode = 1;
  48. }
  49. usleep(1000); // let it breathe
  50. }
  51. readprompt(fd);
  52. if (returncode) break;
  53. memset(buf, 0, 1024);
  54. sprintf(s, "%d BLK> ! BLK!", blkno);
  55. sendcmdp(fd, s);
  56. blkno++;
  57. }
  58. sendcmdp(fd, "FORGET _");
  59. printf("Done!\n");
  60. fclose(fp);
  61. return returncode;
  62. }