Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame^] | 1 | From 00cce57eff1a0de3b93efa5da225e9eb33d19659 Mon Sep 17 00:00:00 2001 |
| 2 | From: Sean Anderson <sean.anderson@seco.com> |
| 3 | Date: Thu, 30 Dec 2021 15:34:28 -0500 |
| 4 | Subject: [PATCH] img2simg: Add support for converting holes to "don't care" |
| 5 | chunks |
| 6 | |
| 7 | This adds support for converting files with holes to "don't care" |
| 8 | chunks. This can result in a substantial reduction in the time it takes |
| 9 | to program an image if it has many holes. |
| 10 | |
| 11 | Generally, constants compared to argc have been reduced by one, since we |
| 12 | no longer have the program name as the first argument. |
| 13 | |
| 14 | Change-Id: I00750edc07d6415dcc07ae0351e9397b0222b7ba |
| 15 | Upstream-Status: Backport [6150b00b6025918da8c28e5c2f929ecdf480a9d6] |
| 16 | Signed-off-by: Sean Anderson <sean.anderson@seco.com> |
| 17 | --- |
| 18 | libsparse/img2simg.c | 41 ++++++++++++++++++++++++++++++----------- |
| 19 | 1 file changed, 30 insertions(+), 11 deletions(-) |
| 20 | |
| 21 | diff --git a/libsparse/img2simg.c b/libsparse/img2simg.c |
| 22 | index 2e171b613..c985d5449 100644 |
| 23 | --- a/libsparse/img2simg.c |
| 24 | +++ b/libsparse/img2simg.c |
| 25 | @@ -40,25 +40,42 @@ |
| 26 | |
| 27 | void usage() |
| 28 | { |
| 29 | - fprintf(stderr, "Usage: img2simg <raw_image_file> <sparse_image_file> [<block_size>]\n"); |
| 30 | + fprintf(stderr, "Usage: img2simg [-s] <raw_image_file> <sparse_image_file> [<block_size>]\n"); |
| 31 | } |
| 32 | |
| 33 | int main(int argc, char *argv[]) |
| 34 | { |
| 35 | + char *arg_in; |
| 36 | + char *arg_out; |
| 37 | + enum sparse_read_mode mode = SPARSE_READ_MODE_NORMAL; |
| 38 | + int extra; |
| 39 | int in; |
| 40 | + int opt; |
| 41 | int out; |
| 42 | int ret; |
| 43 | struct sparse_file *s; |
| 44 | unsigned int block_size = 4096; |
| 45 | off64_t len; |
| 46 | |
| 47 | - if (argc < 3 || argc > 4) { |
| 48 | + while ((opt = getopt(argc, argv, "s")) != -1) { |
| 49 | + switch (opt) { |
| 50 | + case 's': |
| 51 | + mode = SPARSE_READ_MODE_HOLE; |
| 52 | + break; |
| 53 | + default: |
| 54 | + usage(); |
| 55 | + exit(-1); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + extra = argc - optind; |
| 60 | + if (extra < 2 || extra > 3) { |
| 61 | usage(); |
| 62 | exit(-1); |
| 63 | } |
| 64 | |
| 65 | - if (argc == 4) { |
| 66 | - block_size = atoi(argv[3]); |
| 67 | + if (extra == 3) { |
| 68 | + block_size = atoi(argv[optind + 2]); |
| 69 | } |
| 70 | |
| 71 | if (block_size < 1024 || block_size % 4 != 0) { |
| 72 | @@ -66,22 +83,24 @@ int main(int argc, char *argv[]) |
| 73 | exit(-1); |
| 74 | } |
| 75 | |
| 76 | - if (strcmp(argv[1], "-") == 0) { |
| 77 | + arg_in = argv[optind]; |
| 78 | + if (strcmp(arg_in, "-") == 0) { |
| 79 | in = STDIN_FILENO; |
| 80 | } else { |
| 81 | - in = open(argv[1], O_RDONLY | O_BINARY); |
| 82 | + in = open(arg_in, O_RDONLY | O_BINARY); |
| 83 | if (in < 0) { |
| 84 | - fprintf(stderr, "Cannot open input file %s\n", argv[1]); |
| 85 | + fprintf(stderr, "Cannot open input file %s\n", arg_in); |
| 86 | exit(-1); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | - if (strcmp(argv[2], "-") == 0) { |
| 91 | + arg_out = argv[optind + 1]; |
| 92 | + if (strcmp(arg_out, "-") == 0) { |
| 93 | out = STDOUT_FILENO; |
| 94 | } else { |
| 95 | - out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664); |
| 96 | + out = open(arg_out, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664); |
| 97 | if (out < 0) { |
| 98 | - fprintf(stderr, "Cannot open output file %s\n", argv[2]); |
| 99 | + fprintf(stderr, "Cannot open output file %s\n", arg_out); |
| 100 | exit(-1); |
| 101 | } |
| 102 | } |
| 103 | @@ -96,7 +115,7 @@ int main(int argc, char *argv[]) |
| 104 | } |
| 105 | |
| 106 | sparse_file_verbose(s); |
| 107 | - ret = sparse_file_read(s, in, SPARSE_READ_MODE_NORMAL, false); |
| 108 | + ret = sparse_file_read(s, in, mode, false); |
| 109 | if (ret) { |
| 110 | fprintf(stderr, "Failed to read file\n"); |
| 111 | exit(-1); |
| 112 | -- |
| 113 | 2.35.1.1320.gc452695387.dirty |
| 114 | |