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.

61 lines
1.6KB

  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. /* Pull specified block no "srcblk" from "device" into block no "dstblk" in
  8. * local "blkfs". If "dstblk" is omitted, it is the same as "srcblk"
  9. */
  10. int main(int argc, char **argv)
  11. {
  12. if ((argc != 5) && (argc != 4)) {
  13. fprintf(stderr, "Usage: ./blkdown device srcblk blkfs [dstblk]\n");
  14. return 1;
  15. }
  16. unsigned int srcblk = strtol(argv[2], NULL, 10);
  17. unsigned int dstblk = srcblk;
  18. if (argc == 5) {
  19. dstblk = strtol(argv[4], NULL, 10);
  20. }
  21. FILE *fp = fopen(argv[3], "r+");
  22. if (!fp) {
  23. fprintf(stderr, "Can't open %s.\n", argv[3]);
  24. return 1;
  25. }
  26. int fd = ttyopen(argv[1]);
  27. if (fd < 0) {
  28. fprintf(stderr, "Could not open %s\n", argv[1]);
  29. return 1;
  30. }
  31. fseek(fp, 1024 * dstblk, SEEK_SET);
  32. char s[0x40];
  33. sprintf(s, ": _ %d BLK@ 1024 0 DO I BLK( + C@ .x LOOP ; _", srcblk);
  34. sendcmd(fd, s);
  35. int returncode = 0;
  36. for (int i=0; i<1024; i++) {
  37. usleep(1000); // let it breathe
  38. mread(fd, s, 2); // read hex pair
  39. s[2] = 0; // null terminate
  40. char *endptr = NULL;
  41. unsigned char c = strtol(s, &endptr, 16);
  42. if (endptr != &s[2]) {
  43. fprintf(stderr, "Non-hex value (%s) at index %d.\n", s, i);
  44. // we don't exit now because we need to "consume" our whole program.
  45. returncode = 1;
  46. }
  47. if (returncode == 0) {
  48. fwrite(&c, 1, 1, fp);
  49. }
  50. }
  51. readprompt(fd);
  52. sendcmdp(fd, "FORGET _");
  53. printf("Done!\n");
  54. fclose(fp);
  55. return returncode;
  56. }