~vdupras/collapseos

ref: ac3629b817115b1af75096856ee9499cfbbd3392 collapseos/tools/blkdown.c -rw-r--r-- 1.6 KiB
ac3629b8Virgil Dupras Make BLK@* and BLK!* into ialiases 1 year, 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#include "common.h"

/* Pull specified block no "srcblk" from "device" into block no "dstblk" in
 * local "blkfs". If "dstblk" is omitted, it is the same as "srcblk"
 */

int main(int argc, char **argv)
{
    if ((argc != 5) && (argc != 4)) {
        fprintf(stderr, "Usage: ./blkdown device srcblk blkfs [dstblk]\n");
        return 1;
    }
    unsigned int srcblk = strtol(argv[2], NULL, 10);
    unsigned int dstblk = srcblk;
    if (argc == 5) {
        dstblk = strtol(argv[4], NULL, 10);
    }
    FILE *fp = fopen(argv[3], "r+");
    if (!fp) {
        fprintf(stderr, "Can't open %s.\n", argv[3]);
        return 1;
    }
    int fd = ttyopen(argv[1]);
    if (fd < 0) {
        fprintf(stderr, "Could not open %s\n", argv[1]);
        return 1;
    }
    fseek(fp, 1024 * dstblk, SEEK_SET);
    char s[0x40];
    sprintf(s, ": _ %d BLK@ 1024 0 DO I BLK( + C@ .x LOOP ; _", srcblk);
    sendcmd(fd, s);

    int returncode = 0;
    for (int i=0; i<1024; i++) {
        usleep(1000); // let it breathe
        mread(fd, s, 2); // read hex pair
        s[2] = 0; // null terminate
        char *endptr = NULL;
        unsigned char c = strtol(s, &endptr, 16);
        if (endptr != &s[2]) {
            fprintf(stderr, "Non-hex value (%s) at index %d.\n", s, i);
            // we don't exit now because we need to "consume" our whole program.
            returncode = 1;
        }
        if (returncode == 0) {
            fwrite(&c, 1, 1, fp);
        }
    }
    readprompt(fd);
    sendcmdp(fd, "FORGET _");
    printf("Done!\n");
    fclose(fp);
    return returncode;
}