summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Anderson <sean.anderson@seco.com>2022-08-12 15:44:04 -0400
committerKhem Raj <raj.khem@gmail.com>2022-08-13 07:05:31 -0700
commit9862a017fa7f88424f0670ba89af58e5051550b0 (patch)
treebd02990b51040f62df11ffacfc82aec4ff588153
parentfb331cb62eafd1e534dee292525084ccee0ef3e1 (diff)
downloadmeta-openembedded-9862a017fa7f88424f0670ba89af58e5051550b0.tar.gz
image_types_sparse: Generate "don't care" chunks
By default, img2simg will only generate raw and fill chunks. This adds support for "don't care" chunks, based on file holes. This is similar to how bmaptool works. "don't care" chunks do not need to be written, speeding up flashing time. Signed-off-by: Sean Anderson <sean.anderson@seco.com> Signed-off-by: Khem Raj <raj.khem@gmail.com>
-rw-r--r--meta-oe/classes/image_types_sparse.bbclass2
-rw-r--r--meta-oe/recipes-devtools/android-tools/android-tools/core/0015-libsparse-Split-off-most-of-sparse_file_read_normal-.patch60
-rw-r--r--meta-oe/recipes-devtools/android-tools/android-tools/core/0016-libsparse-Add-hole-mode-to-sparse_file_read.patch188
-rw-r--r--meta-oe/recipes-devtools/android-tools/android-tools/core/0017-img2simg-Add-support-for-converting-holes-to-don-t-c.patch114
-rw-r--r--meta-oe/recipes-devtools/android-tools/android-tools_5.1.1.r37.bb3
5 files changed, 366 insertions, 1 deletions
diff --git a/meta-oe/classes/image_types_sparse.bbclass b/meta-oe/classes/image_types_sparse.bbclass
index 355a3dafc2..69e24cbb79 100644
--- a/meta-oe/classes/image_types_sparse.bbclass
+++ b/meta-oe/classes/image_types_sparse.bbclass
@@ -11,6 +11,6 @@ CONVERSIONTYPES += "sparse"
11CONVERSION_CMD:sparse() { 11CONVERSION_CMD:sparse() {
12 INPUT="${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.${type}" 12 INPUT="${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.${type}"
13 truncate --no-create --size=%${SPARSE_BLOCK_SIZE} "$INPUT" 13 truncate --no-create --size=%${SPARSE_BLOCK_SIZE} "$INPUT"
14 img2simg "$INPUT" "$INPUT.sparse" ${SPARSE_BLOCK_SIZE} 14 img2simg -s "$INPUT" "$INPUT.sparse" ${SPARSE_BLOCK_SIZE}
15} 15}
16CONVERSION_DEPENDS_sparse = "android-tools-native" 16CONVERSION_DEPENDS_sparse = "android-tools-native"
diff --git a/meta-oe/recipes-devtools/android-tools/android-tools/core/0015-libsparse-Split-off-most-of-sparse_file_read_normal-.patch b/meta-oe/recipes-devtools/android-tools/android-tools/core/0015-libsparse-Split-off-most-of-sparse_file_read_normal-.patch
new file mode 100644
index 0000000000..b3893f0c80
--- /dev/null
+++ b/meta-oe/recipes-devtools/android-tools/android-tools/core/0015-libsparse-Split-off-most-of-sparse_file_read_normal-.patch
@@ -0,0 +1,60 @@
1From 7b74d23ed955206a789a96bdc3288593e702afac Mon Sep 17 00:00:00 2001
2From: Sean Anderson <sean.anderson@seco.com>
3Date: Thu, 30 Dec 2021 15:16:08 -0500
4Subject: [PATCH] libsparse: Split off most of sparse_file_read_normal into a
5 helper function
6
7This carves out the core of sparse_file_read_normal and splits it off so
8it can be reused in the next patch. No functional change intended.
9
10Change-Id: Id00491fd7e5bb6fa28c517a0bb32b8b506539d4d
11Upstream-status: Backport [95657f3e5976d96073f7bbfe3a49192509999d1d]
12Signed-off-by: Sean Anderson <sean.anderson@seco.com>
13---
14 libsparse/sparse_read.c | 21 ++++++++++++++++-----
15 1 file changed, 16 insertions(+), 5 deletions(-)
16
17diff --git a/libsparse/sparse_read.c b/libsparse/sparse_read.c
18index 8e188e9a4..ee4abd86a 100644
19--- a/libsparse/sparse_read.c
20+++ b/libsparse/sparse_read.c
21@@ -353,13 +353,11 @@ static int sparse_file_read_sparse(struct sparse_file *s, int fd, bool crc)
22 return 0;
23 }
24
25-static int sparse_file_read_normal(struct sparse_file *s, int fd)
26+static int do_sparse_file_read_normal(struct sparse_file *s, int fd, uint32_t* buf, int64_t offset,
27+ int64_t remain)
28 {
29 int ret;
30- uint32_t *buf = malloc(s->block_size);
31- unsigned int block = 0;
32- int64_t remain = s->len;
33- int64_t offset = 0;
34+ unsigned int block = offset / s->block_size;
35 unsigned int to_read;
36 unsigned int i;
37 bool sparse_block;
38@@ -403,6 +401,19 @@ static int sparse_file_read_normal(struct sparse_file *s, int fd)
39 return 0;
40 }
41
42+static int sparse_file_read_normal(struct sparse_file* s, int fd)
43+{
44+ int ret;
45+ uint32_t* buf = (uint32_t*)malloc(s->block_size);
46+
47+ if (!buf)
48+ return -ENOMEM;
49+
50+ ret = do_sparse_file_read_normal(s, fd, buf, 0, s->len);
51+ free(buf);
52+ return ret;
53+}
54+
55 int sparse_file_read(struct sparse_file *s, int fd, bool sparse, bool crc)
56 {
57 if (crc && !sparse) {
58--
592.35.1.1320.gc452695387.dirty
60
diff --git a/meta-oe/recipes-devtools/android-tools/android-tools/core/0016-libsparse-Add-hole-mode-to-sparse_file_read.patch b/meta-oe/recipes-devtools/android-tools/android-tools/core/0016-libsparse-Add-hole-mode-to-sparse_file_read.patch
new file mode 100644
index 0000000000..e5221d2b4c
--- /dev/null
+++ b/meta-oe/recipes-devtools/android-tools/android-tools/core/0016-libsparse-Add-hole-mode-to-sparse_file_read.patch
@@ -0,0 +1,188 @@
1From 41574b628ec4229c24dfe289af7b6978edcca4ed Mon Sep 17 00:00:00 2001
2From: Sean Anderson <sean.anderson@seco.com>
3Date: Thu, 30 Dec 2021 15:19:41 -0500
4Subject: [PATCH] libsparse: Add "hole" mode to sparse_file_read
5
6This adds support for filesystem-level sparse files. These files have
7holes which are not stored in the filesystem and when read are full of
8zeros. While these zeros may be significant in some types of files,
9other types of files may not care about the contents of holes. For
10example, most filesystem creation tools write to all the blocks they
11care about. Those blocks not written to will remain holes, and can be
12safely represented by "don't care" chunks. Using "don't care" chunks
13instead of fill chunks can result in a substantial reduction of the time
14it takes to program a sparse image.
15
16To accomplish this, we extend the existing "sparse" boolean parameter to
17be an enum of mode types. This enum represents the strategy we take when
18reading in a file. For the most part the implementation is
19straightforward. We use lseek to determine where the holes in the file
20are, and then use do_sparse_file_read_normal to create chunks for the
21data section. Note that every file has an implicit hole at its end.
22
23Change-Id: I0cfbf08886fca9a91cb753ec8734c84fcbe52c9f
24Upstream-Status: Backport [f96466b05543b984ef7315d830bab4a409228d35]
25Signed-off-by: Sean Anderson <sean.anderson@seco.com>
26---
27 libsparse/img2simg.c | 2 +-
28 libsparse/include/sparse/sparse.h | 32 +++++++++++---
29 libsparse/sparse_read.c | 71 +++++++++++++++++++++++++++++--
30 3 files changed, 93 insertions(+), 12 deletions(-)
31
32diff --git a/libsparse/img2simg.c b/libsparse/img2simg.c
33index a0db36f45..2e171b613 100644
34--- a/libsparse/img2simg.c
35+++ b/libsparse/img2simg.c
36@@ -96,7 +96,7 @@ int main(int argc, char *argv[])
37 }
38
39 sparse_file_verbose(s);
40- ret = sparse_file_read(s, in, false, false);
41+ ret = sparse_file_read(s, in, SPARSE_READ_MODE_NORMAL, false);
42 if (ret) {
43 fprintf(stderr, "Failed to read file\n");
44 exit(-1);
45diff --git a/libsparse/include/sparse/sparse.h b/libsparse/include/sparse/sparse.h
46index 8b757d22a..b68aa21a8 100644
47--- a/libsparse/include/sparse/sparse.h
48+++ b/libsparse/include/sparse/sparse.h
49@@ -196,23 +196,41 @@ int64_t sparse_file_len(struct sparse_file *s, bool sparse, bool crc);
50 int sparse_file_callback(struct sparse_file *s, bool sparse, bool crc,
51 int (*write)(void *priv, const void *data, int len), void *priv);
52
53+/**
54+ * enum sparse_read_mode - The method to use when reading in files
55+ * @SPARSE_READ_MODE_NORMAL: The input is a regular file. Constant chunks of
56+ * data (including holes) will be be converted to
57+ * fill chunks.
58+ * @SPARSE_READ_MODE_SPARSE: The input is an Android sparse file.
59+ * @SPARSE_READ_MODE_HOLE: The input is a regular file. Holes will be converted
60+ * to "don't care" chunks. Other constant chunks will
61+ * be converted to fill chunks.
62+ */
63+enum sparse_read_mode {
64+ SPARSE_READ_MODE_NORMAL = false,
65+ SPARSE_READ_MODE_SPARSE = true,
66+ SPARSE_READ_MODE_HOLE,
67+};
68+
69 /**
70 * sparse_file_read - read a file into a sparse file cookie
71 *
72 * @s - sparse file cookie
73 * @fd - file descriptor to read from
74- * @sparse - read a file in the Android sparse file format
75+ * @mode - mode to use when reading the input file
76 * @crc - verify the crc of a file in the Android sparse file format
77 *
78- * Reads a file into a sparse file cookie. If sparse is true, the file is
79- * assumed to be in the Android sparse file format. If sparse is false, the
80- * file will be sparsed by looking for block aligned chunks of all zeros or
81- * another 32 bit value. If crc is true, the crc of the sparse file will be
82- * verified.
83+ * Reads a file into a sparse file cookie. If @mode is
84+ * %SPARSE_READ_MODE_SPARSE, the file is assumed to be in the Android sparse
85+ * file format. If @mode is %SPARSE_READ_MODE_NORMAL, the file will be sparsed
86+ * by looking for block aligned chunks of all zeros or another 32 bit value. If
87+ * @mode is %SPARSE_READ_MODE_HOLE, the file will be sparsed like
88+ * %SPARSE_READ_MODE_NORMAL, but holes in the file will be converted to "don't
89+ * care" chunks. If crc is true, the crc of the sparse file will be verified.
90 *
91 * Returns 0 on success, negative errno on error.
92 */
93-int sparse_file_read(struct sparse_file *s, int fd, bool sparse, bool crc);
94+int sparse_file_read(struct sparse_file *s, int fd, enum sparse_read_mode mode, bool crc);
95
96 /**
97 * sparse_file_import - import an existing sparse file
98diff --git a/libsparse/sparse_read.c b/libsparse/sparse_read.c
99index ee4abd86a..81f48cc13 100644
100--- a/libsparse/sparse_read.c
101+++ b/libsparse/sparse_read.c
102@@ -414,16 +414,79 @@ static int sparse_file_read_normal(struct sparse_file* s, int fd)
103 return ret;
104 }
105
106-int sparse_file_read(struct sparse_file *s, int fd, bool sparse, bool crc)
107+#ifdef __linux__
108+static int sparse_file_read_hole(struct sparse_file* s, int fd)
109 {
110- if (crc && !sparse) {
111+ int ret;
112+ uint32_t* buf = (uint32_t*)malloc(s->block_size);
113+ int64_t end = 0;
114+ int64_t start = 0;
115+
116+ if (!buf) {
117+ return -ENOMEM;
118+ }
119+
120+ do {
121+ start = lseek(fd, end, SEEK_DATA);
122+ if (start < 0) {
123+ if (errno == ENXIO)
124+ /* The rest of the file is a hole */
125+ break;
126+
127+ error("could not seek to data");
128+ free(buf);
129+ return -errno;
130+ } else if (start > s->len) {
131+ break;
132+ }
133+
134+ end = lseek(fd, start, SEEK_HOLE);
135+ if (end < 0) {
136+ error("could not seek to end");
137+ free(buf);
138+ return -errno;
139+ }
140+ end = min(end, s->len);
141+
142+ start = ALIGN_DOWN(start, s->block_size);
143+ end = ALIGN(end, s->block_size);
144+ if (lseek(fd, start, SEEK_SET) < 0) {
145+ free(buf);
146+ return -errno;
147+ }
148+
149+ ret = do_sparse_file_read_normal(s, fd, buf, start, end - start);
150+ if (ret) {
151+ free(buf);
152+ return ret;
153+ }
154+ } while (end < s->len);
155+
156+ free(buf);
157+ return 0;
158+}
159+#else
160+static int sparse_file_read_hole(struct sparse_file* s __unused, int fd __unused)
161+{
162+ return -ENOTSUP;
163+}
164+#endif
165+
166+int sparse_file_read(struct sparse_file *s, int fd, enum sparse_read_mode mode, bool crc)
167+{
168+ if (crc && mode != SPARSE_READ_MODE_SPARSE) {
169 return -EINVAL;
170 }
171
172- if (sparse) {
173+ switch (mode) {
174+ case SPARSE_READ_MODE_SPARSE:
175 return sparse_file_read_sparse(s, fd, crc);
176- } else {
177+ case SPARSE_READ_MODE_NORMAL:
178 return sparse_file_read_normal(s, fd);
179+ case SPARSE_READ_MODE_HOLE:
180+ return sparse_file_read_hole(s, fd);
181+ default:
182+ return -EINVAL;
183 }
184 }
185
186--
1872.35.1.1320.gc452695387.dirty
188
diff --git a/meta-oe/recipes-devtools/android-tools/android-tools/core/0017-img2simg-Add-support-for-converting-holes-to-don-t-c.patch b/meta-oe/recipes-devtools/android-tools/android-tools/core/0017-img2simg-Add-support-for-converting-holes-to-don-t-c.patch
new file mode 100644
index 0000000000..9d19f58095
--- /dev/null
+++ b/meta-oe/recipes-devtools/android-tools/android-tools/core/0017-img2simg-Add-support-for-converting-holes-to-don-t-c.patch
@@ -0,0 +1,114 @@
1From 00cce57eff1a0de3b93efa5da225e9eb33d19659 Mon Sep 17 00:00:00 2001
2From: Sean Anderson <sean.anderson@seco.com>
3Date: Thu, 30 Dec 2021 15:34:28 -0500
4Subject: [PATCH] img2simg: Add support for converting holes to "don't care"
5 chunks
6
7This adds support for converting files with holes to "don't care"
8chunks. This can result in a substantial reduction in the time it takes
9to program an image if it has many holes.
10
11Generally, constants compared to argc have been reduced by one, since we
12no longer have the program name as the first argument.
13
14Change-Id: I00750edc07d6415dcc07ae0351e9397b0222b7ba
15Upstream-Status: Backport [6150b00b6025918da8c28e5c2f929ecdf480a9d6]
16Signed-off-by: Sean Anderson <sean.anderson@seco.com>
17---
18 libsparse/img2simg.c | 41 ++++++++++++++++++++++++++++++-----------
19 1 file changed, 30 insertions(+), 11 deletions(-)
20
21diff --git a/libsparse/img2simg.c b/libsparse/img2simg.c
22index 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--
1132.35.1.1320.gc452695387.dirty
114
diff --git a/meta-oe/recipes-devtools/android-tools/android-tools_5.1.1.r37.bb b/meta-oe/recipes-devtools/android-tools/android-tools_5.1.1.r37.bb
index 8f28abbd19..26393605d4 100644
--- a/meta-oe/recipes-devtools/android-tools/android-tools_5.1.1.r37.bb
+++ b/meta-oe/recipes-devtools/android-tools/android-tools_5.1.1.r37.bb
@@ -41,6 +41,9 @@ SRC_URI = " \
41 file://core/adb_libssl_11.diff;patchdir=system/core \ 41 file://core/adb_libssl_11.diff;patchdir=system/core \
42 file://core/0013-adb-Support-riscv64.patch;patchdir=system/core \ 42 file://core/0013-adb-Support-riscv64.patch;patchdir=system/core \
43 file://core/0014-add-u3-ss-descriptor-support-for-adb.patch;patchdir=system/core \ 43 file://core/0014-add-u3-ss-descriptor-support-for-adb.patch;patchdir=system/core \
44 file://core/0015-libsparse-Split-off-most-of-sparse_file_read_normal-.patch;patchdir=system/core \
45 file://core/0016-libsparse-Add-hole-mode-to-sparse_file_read.patch;patchdir=system/core \
46 file://core/0017-img2simg-Add-support-for-converting-holes-to-don-t-c.patch;patchdir=system/core \
44 file://extras/0001-ext4_utils-remove-selinux-extensions.patch;patchdir=system/extras \ 47 file://extras/0001-ext4_utils-remove-selinux-extensions.patch;patchdir=system/extras \
45 file://extras/0002-ext4_utils-add-o-argument-to-preserve-ownership.patch;patchdir=system/extras \ 48 file://extras/0002-ext4_utils-add-o-argument-to-preserve-ownership.patch;patchdir=system/extras \
46 file://libselinux/0001-Remove-bionic-specific-calls.patch;patchdir=external/libselinux \ 49 file://libselinux/0001-Remove-bionic-specific-calls.patch;patchdir=external/libselinux \