otrx: support TRX from stdin when extracting
authorRafał Miłecki <rafal@milecki.pl>
Sat, 27 Nov 2021 15:31:04 +0000 (16:31 +0100)
committerRafał Miłecki <rafal@milecki.pl>
Thu, 2 Dec 2021 07:33:30 +0000 (08:33 +0100)
It allows e.g.
dd if=firmware.chk skip=1 bs=58 | otrx extract - -1 kernel.bin

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
src/otrx.c

index 5ee310cff9c86c880dec5ce6982c560ae701b67c..4ea19aaa04d656854e7c8dc57e71a6c85e5db49d 100644 (file)
@@ -558,7 +558,7 @@ static void otrx_extract_parse_options(int argc, char **argv) {
        }
 }
 
-static int otrx_extract_copy(FILE *trx, size_t offset, size_t length, char *out_path) {
+static int otrx_extract_copy(struct otrx_ctx *otrx, size_t length, char *out_path) {
        FILE *out;
        size_t bytes;
        uint8_t *buf;
@@ -578,8 +578,7 @@ static int otrx_extract_copy(FILE *trx, size_t offset, size_t length, char *out_
                goto err_close;
        }
 
-       fseek(trx, offset, SEEK_SET);
-       bytes = fread(buf, 1, length, trx);
+       bytes = fread(buf, 1, length, otrx->fp);
        if (bytes != length) {
                fprintf(stderr, "Couldn't read %zu B of data from %s\n", length, trx_path);
                err =  -ENOMEM;
@@ -631,9 +630,9 @@ static int otrx_extract(int argc, char **argv) {
                if (!part->offset && partition[part->idx])
                        printf("TRX doesn't contain partition %d, can't extract %s\n", part->idx + 1, partition[part->idx]);
                if (!part->offset || !partition[part->idx])
-                       continue;
+                       otrx_skip(otrx.fp, part->length);
                else
-                       otrx_extract_copy(otrx.fp, trx_offset + part->offset, part->length, partition[part->idx]);
+                       otrx_extract_copy(&otrx, part->length, partition[part->idx]);
        }
 
        otrx_close(otrx.fp);