blob: cc2e4d6447197c8768e8e3a2d9b91e650d753af8 [file] [log] [blame]
Amin Hassani924183b2017-09-27 14:50:59 -07001//
2// Copyright (C) 2017 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#include "update_engine/payload_generator/deflate_utils.h"
18
19#include <algorithm>
20#include <string>
21#include <utility>
22
23#include <base/files/file_util.h>
24#include <base/logging.h>
25#include <base/strings/string_util.h>
26
27#include "update_engine/common/utils.h"
28#include "update_engine/payload_generator/delta_diff_generator.h"
29#include "update_engine/payload_generator/extent_ranges.h"
Amin Hassani924183b2017-09-27 14:50:59 -070030#include "update_engine/payload_generator/squashfs_filesystem.h"
31#include "update_engine/update_metadata.pb.h"
32
Amin Hassani3cd4df12017-08-25 11:21:53 -070033using puffin::BitExtent;
34using puffin::ByteExtent;
Amin Hassani232f8f92019-01-14 16:15:31 -080035using std::string;
36using std::vector;
Amin Hassani924183b2017-09-27 14:50:59 -070037
38namespace chromeos_update_engine {
39namespace deflate_utils {
40namespace {
41
42// The minimum size for a squashfs image to be processed.
43const uint64_t kMinimumSquashfsImageSize = 1 * 1024 * 1024; // bytes
44
45// TODO(*): Optimize this so we don't have to read all extents into memory in
46// case it is large.
47bool CopyExtentsToFile(const string& in_path,
Andrew9d5a61d2020-03-26 13:40:37 -070048 const vector<Extent>& extents,
Amin Hassani924183b2017-09-27 14:50:59 -070049 const string& out_path,
50 size_t block_size) {
Amin Hassanid8b67f42017-12-06 13:47:52 -080051 brillo::Blob data(utils::BlocksInExtents(extents) * block_size);
Amin Hassani924183b2017-09-27 14:50:59 -070052 TEST_AND_RETURN_FALSE(
53 utils::ReadExtents(in_path, extents, &data, data.size(), block_size));
54 TEST_AND_RETURN_FALSE(
55 utils::WriteFile(out_path.c_str(), data.data(), data.size()));
56 return true;
57}
58
59bool IsSquashfsImage(const string& part_path,
60 const FilesystemInterface::File& file) {
61 // Only check for files with img postfix.
62 if (base::EndsWith(file.name, ".img", base::CompareCase::SENSITIVE) &&
Amin Hassanid8b67f42017-12-06 13:47:52 -080063 utils::BlocksInExtents(file.extents) >=
64 kMinimumSquashfsImageSize / kBlockSize) {
Amin Hassani924183b2017-09-27 14:50:59 -070065 brillo::Blob super_block;
66 TEST_AND_RETURN_FALSE(
67 utils::ReadFileChunk(part_path,
68 file.extents[0].start_block() * kBlockSize,
69 100,
70 &super_block));
71 return SquashfsFilesystem::IsSquashfsImage(super_block);
72 }
73 return false;
74}
75
Håkan Kviste4d414e2019-06-28 08:05:06 +020076bool IsRegularFile(const FilesystemInterface::File& file) {
77 // If inode is 0, then stat information is invalid for some psuedo files
78 if (file.file_stat.st_ino != 0 &&
79 (file.file_stat.st_mode & S_IFMT) == S_IFREG) {
80 return true;
81 }
82 return false;
83}
84
Amin Hassani924183b2017-09-27 14:50:59 -070085// Realigns subfiles |files| of a splitted file |file| into its correct
86// positions. This can be used for squashfs, zip, apk, etc.
87bool RealignSplittedFiles(const FilesystemInterface::File& file,
88 vector<FilesystemInterface::File>* files) {
89 // We have to shift all the Extents in |files|, based on the Extents of the
90 // |file| itself.
91 size_t num_blocks = 0;
92 for (auto& in_file : *files) { // We need to modify so no constant.
93 TEST_AND_RETURN_FALSE(
94 ShiftExtentsOverExtents(file.extents, &in_file.extents));
Amin Hassani3cd4df12017-08-25 11:21:53 -070095 TEST_AND_RETURN_FALSE(
96 ShiftBitExtentsOverExtents(file.extents, &in_file.deflates));
97
Amin Hassani924183b2017-09-27 14:50:59 -070098 in_file.name = file.name + "/" + in_file.name;
Amin Hassanid8b67f42017-12-06 13:47:52 -080099 num_blocks += utils::BlocksInExtents(in_file.extents);
Amin Hassani924183b2017-09-27 14:50:59 -0700100 }
101
102 // Check that all files in |in_files| cover the entire image.
Amin Hassanid8b67f42017-12-06 13:47:52 -0800103 TEST_AND_RETURN_FALSE(utils::BlocksInExtents(file.extents) == num_blocks);
Amin Hassani924183b2017-09-27 14:50:59 -0700104 return true;
105}
106
Amin Hassani3cd4df12017-08-25 11:21:53 -0700107bool IsBitExtentInExtent(const Extent& extent, const BitExtent& bit_extent) {
108 return (bit_extent.offset / 8) >= (extent.start_block() * kBlockSize) &&
109 ((bit_extent.offset + bit_extent.length + 7) / 8) <=
110 ((extent.start_block() + extent.num_blocks()) * kBlockSize);
111}
112
Sen Jiang23bae402018-11-13 11:27:29 -0800113// Returns whether the given file |name| has an extension listed in
114// |extensions|.
Kelvin Zhang0af01222021-10-05 13:36:47 -0700115
116} // namespace
117
Kelvin Zhangcb2dc882021-11-22 09:26:50 -0800118constexpr base::StringPiece ToStringPiece(std::string_view s) {
119 return base::StringPiece(s.data(), s.length());
120}
121
122bool IsFileExtensions(
123 const std::string_view name,
124 const std::initializer_list<std::string_view>& extensions) {
125 return any_of(extensions.begin(),
126 extensions.end(),
127 [name = ToStringPiece(name)](const auto& ext) {
128 return base::EndsWith(name,
129 ToStringPiece(ext),
130 base::CompareCase::INSENSITIVE_ASCII);
131 });
Sen Jiang23bae402018-11-13 11:27:29 -0800132}
133
Amin Hassani3cd4df12017-08-25 11:21:53 -0700134ByteExtent ExpandToByteExtent(const BitExtent& extent) {
135 uint64_t offset = extent.offset / 8;
136 uint64_t length = ((extent.offset + extent.length + 7) / 8) - offset;
137 return {offset, length};
138}
139
Amin Hassani924183b2017-09-27 14:50:59 -0700140bool ShiftExtentsOverExtents(const vector<Extent>& base_extents,
141 vector<Extent>* over_extents) {
Amin Hassanid8b67f42017-12-06 13:47:52 -0800142 if (utils::BlocksInExtents(base_extents) <
143 utils::BlocksInExtents(*over_extents)) {
Amin Hassani924183b2017-09-27 14:50:59 -0700144 LOG(ERROR) << "over_extents have more blocks than base_extents! Invalid!";
145 return false;
146 }
147 for (size_t idx = 0; idx < over_extents->size(); idx++) {
148 auto over_ext = &over_extents->at(idx);
149 auto gap_blocks = base_extents[0].start_block();
150 auto last_end_block = base_extents[0].start_block();
151 for (auto base_ext : base_extents) { // We need to modify |base_ext|, so we
152 // use copy.
153 gap_blocks += base_ext.start_block() - last_end_block;
154 last_end_block = base_ext.start_block() + base_ext.num_blocks();
155 base_ext.set_start_block(base_ext.start_block() - gap_blocks);
156 if (over_ext->start_block() >= base_ext.start_block() &&
157 over_ext->start_block() <
158 base_ext.start_block() + base_ext.num_blocks()) {
159 if (over_ext->start_block() + over_ext->num_blocks() <=
160 base_ext.start_block() + base_ext.num_blocks()) {
161 // |over_ext| is inside |base_ext|, increase its start block.
162 over_ext->set_start_block(over_ext->start_block() + gap_blocks);
163 } else {
164 // |over_ext| spills over this |base_ext|, split it into two.
165 auto new_blocks = base_ext.start_block() + base_ext.num_blocks() -
166 over_ext->start_block();
167 vector<Extent> new_extents = {
168 ExtentForRange(gap_blocks + over_ext->start_block(), new_blocks),
169 ExtentForRange(over_ext->start_block() + new_blocks,
170 over_ext->num_blocks() - new_blocks)};
171 *over_ext = new_extents[0];
172 over_extents->insert(std::next(over_extents->begin(), idx + 1),
173 new_extents[1]);
174 }
175 break; // We processed |over_ext|, so break the loop;
176 }
177 }
178 }
179 return true;
180}
181
Amin Hassani3cd4df12017-08-25 11:21:53 -0700182bool ShiftBitExtentsOverExtents(const vector<Extent>& base_extents,
183 vector<BitExtent>* over_extents) {
184 if (over_extents->empty()) {
185 return true;
186 }
187
188 // This check is needed to make sure the number of bytes in |over_extents|
189 // does not exceed |base_extents|.
190 auto last_extent = ExpandToByteExtent(over_extents->back());
Kelvin Zhangcb2dc882021-11-22 09:26:50 -0800191 TEST_LE(last_extent.offset + last_extent.length,
192 utils::BlocksInExtents(base_extents) * kBlockSize);
Amin Hassani3cd4df12017-08-25 11:21:53 -0700193
194 for (auto o_ext = over_extents->begin(); o_ext != over_extents->end();) {
195 size_t gap_blocks = base_extents[0].start_block();
196 size_t last_end_block = base_extents[0].start_block();
197 bool o_ext_processed = false;
198 for (auto b_ext : base_extents) { // We need to modify |b_ext|, so we copy.
199 gap_blocks += b_ext.start_block() - last_end_block;
200 last_end_block = b_ext.start_block() + b_ext.num_blocks();
201 b_ext.set_start_block(b_ext.start_block() - gap_blocks);
202 auto byte_o_ext = ExpandToByteExtent(*o_ext);
203 if (byte_o_ext.offset >= b_ext.start_block() * kBlockSize &&
204 byte_o_ext.offset <
205 (b_ext.start_block() + b_ext.num_blocks()) * kBlockSize) {
206 if ((byte_o_ext.offset + byte_o_ext.length) <=
207 (b_ext.start_block() + b_ext.num_blocks()) * kBlockSize) {
208 // |o_ext| is inside |b_ext|, increase its start block.
209 o_ext->offset += gap_blocks * kBlockSize * 8;
210 ++o_ext;
211 } else {
212 // |o_ext| spills over this |b_ext|, remove it.
213 o_ext = over_extents->erase(o_ext);
214 }
215 o_ext_processed = true;
216 break; // We processed o_ext, so break the loop;
217 }
218 }
219 TEST_AND_RETURN_FALSE(o_ext_processed);
220 }
221 return true;
222}
223
224vector<BitExtent> FindDeflates(const vector<Extent>& extents,
225 const vector<BitExtent>& in_deflates) {
226 vector<BitExtent> result;
227 // TODO(ahassani): Replace this with binary_search style search.
228 for (const auto& deflate : in_deflates) {
229 for (const auto& extent : extents) {
230 if (IsBitExtentInExtent(extent, deflate)) {
231 result.push_back(deflate);
232 break;
233 }
234 }
235 }
236 return result;
237}
238
239bool CompactDeflates(const vector<Extent>& extents,
240 const vector<BitExtent>& in_deflates,
241 vector<BitExtent>* out_deflates) {
242 size_t bytes_passed = 0;
243 out_deflates->reserve(in_deflates.size());
244 for (const auto& extent : extents) {
245 size_t gap_bytes = extent.start_block() * kBlockSize - bytes_passed;
246 for (const auto& deflate : in_deflates) {
247 if (IsBitExtentInExtent(extent, deflate)) {
248 out_deflates->emplace_back(deflate.offset - (gap_bytes * 8),
249 deflate.length);
250 }
251 }
252 bytes_passed += extent.num_blocks() * kBlockSize;
253 }
254
255 // All given |in_deflates| items should've been inside one of the extents in
256 // |extents|.
257 TEST_AND_RETURN_FALSE(in_deflates.size() == out_deflates->size());
258
259 // Make sure all outgoing deflates are ordered and non-overlapping.
260 auto result = std::adjacent_find(out_deflates->begin(),
261 out_deflates->end(),
262 [](const BitExtent& a, const BitExtent& b) {
263 return (a.offset + a.length) > b.offset;
264 });
265 TEST_AND_RETURN_FALSE(result == out_deflates->end());
266 return true;
267}
268
269bool FindAndCompactDeflates(const vector<Extent>& extents,
270 const vector<BitExtent>& in_deflates,
271 vector<BitExtent>* out_deflates) {
272 auto found_deflates = FindDeflates(extents, in_deflates);
273 TEST_AND_RETURN_FALSE(CompactDeflates(extents, found_deflates, out_deflates));
274 return true;
275}
276
Kelvin Zhangcb2dc882021-11-22 09:26:50 -0800277bool DeflatePreprocessFileData(const std::string_view filename,
278 const brillo::Blob& data,
279 vector<puffin::BitExtent>* deflates) {
280 bool is_zip = IsFileExtensions(
281 filename, {".apk", ".zip", ".jar", ".zvoice", ".apex", "capex"});
282 bool is_gzip = IsFileExtensions(filename, {".gz", ".gzip", ".tgz"});
283 if (is_zip) {
284 if (!puffin::LocateDeflatesInZipArchive(data, deflates)) {
285 LOG(ERROR) << "Failed to locate deflates in zip file " << filename;
286 deflates->clear();
287 return false;
288 }
289 } else if (is_gzip) {
290 if (!puffin::LocateDeflatesInGzip(data, deflates)) {
291 LOG(ERROR) << "Failed to locate deflates in gzip file " << filename;
292 deflates->clear();
293 return false;
294 }
295 }
296 return true;
297}
298
Sen Jiangce39e672018-11-28 16:43:00 -0800299bool PreprocessPartitionFiles(const PartitionConfig& part,
300 vector<FilesystemInterface::File>* result_files,
301 bool extract_deflates) {
Amin Hassani924183b2017-09-27 14:50:59 -0700302 // Get the file system files.
303 vector<FilesystemInterface::File> tmp_files;
304 part.fs_interface->GetFiles(&tmp_files);
305 result_files->reserve(tmp_files.size());
306
Tianjie Xu1a7bb2c2018-01-22 17:56:57 -0800307 for (auto& file : tmp_files) {
Håkan Kviste4d414e2019-06-28 08:05:06 +0200308 auto is_regular_file = IsRegularFile(file);
309
310 if (is_regular_file && IsSquashfsImage(part.path, file)) {
Amin Hassani924183b2017-09-27 14:50:59 -0700311 // Read the image into a file.
312 base::FilePath path;
313 TEST_AND_RETURN_FALSE(base::CreateTemporaryFile(&path));
314 ScopedPathUnlinker old_unlinker(path.value());
315 TEST_AND_RETURN_FALSE(
316 CopyExtentsToFile(part.path, file.extents, path.value(), kBlockSize));
317 // Test if it is actually a Squashfs file.
Kelvin Zhanged9b2082023-05-25 13:58:19 -0700318 auto sqfs =
319 SquashfsFilesystem::CreateFromFile(path.value(), extract_deflates);
Amin Hassani924183b2017-09-27 14:50:59 -0700320 if (sqfs) {
321 // It is an squashfs file. Get its files to replace with itself.
322 vector<FilesystemInterface::File> files;
323 sqfs->GetFiles(&files);
324
Amin Hassani3cd4df12017-08-25 11:21:53 -0700325 // Replace squashfs file with its files only if |files| has at least two
326 // files or if it has some deflates (since it is better to replace it to
327 // take advantage of the deflates.)
328 if (files.size() > 1 ||
329 (files.size() == 1 && !files[0].deflates.empty())) {
Amin Hassani924183b2017-09-27 14:50:59 -0700330 TEST_AND_RETURN_FALSE(RealignSplittedFiles(file, &files));
331 result_files->insert(result_files->end(), files.begin(), files.end());
332 continue;
333 }
334 } else {
335 LOG(WARNING) << "We thought file: " << file.name
336 << " was a Squashfs file, but it was not.";
337 }
338 }
Tianjie Xu1a7bb2c2018-01-22 17:56:57 -0800339
Tianjie55abd3c2020-06-19 00:22:59 -0700340 if (is_regular_file && extract_deflates && !file.is_compressed) {
Sen Jiang23bae402018-11-13 11:27:29 -0800341 // Search for deflates if the file is in zip or gzip format.
342 // .zvoice files may eventually move out of rootfs. If that happens,
343 // remove ".zvoice" (crbug.com/782918).
Sen Jiang812e9d12018-11-21 16:40:37 -0800344 bool is_zip = IsFileExtensions(
Kelvin Zhangdc1f2582021-09-13 16:43:41 -0700345 file.name, {".apk", ".zip", ".jar", ".zvoice", ".apex", "capex"});
Sen Jiang23bae402018-11-13 11:27:29 -0800346 bool is_gzip = IsFileExtensions(file.name, {".gz", ".gzip", ".tgz"});
347 if (is_zip || is_gzip) {
348 brillo::Blob data;
349 TEST_AND_RETURN_FALSE(utils::ReadExtents(
350 part.path,
351 file.extents,
352 &data,
353 kBlockSize * utils::BlocksInExtents(file.extents),
354 kBlockSize));
Kelvin Zhang6fbf5942021-12-09 14:06:25 -0800355 // |data| read from disk always has size multiple of kBlockSize. So it
356 // might contain trailing garbage data and confuse the gzip/zip
357 // processors. Trim them.
358 if (file.file_stat.st_size > 0 &&
359 static_cast<size_t>(file.file_stat.st_size) < data.size()) {
360 data.resize(file.file_stat.st_size);
361 }
Sen Jiang23bae402018-11-13 11:27:29 -0800362 vector<puffin::BitExtent> deflates;
Kelvin Zhangcb2dc882021-11-22 09:26:50 -0800363 if (!DeflatePreprocessFileData(file.name, data, &deflates)) {
364 LOG(ERROR) << "Failed to preprocess deflate data in partition "
365 << part.name;
366 return false;
Sen Jiang23bae402018-11-13 11:27:29 -0800367 }
368 // Shift the deflate's extent to the offset starting from the beginning
369 // of the current partition; and the delta processor will align the
370 // extents in a continuous buffer later.
371 TEST_AND_RETURN_FALSE(
372 ShiftBitExtentsOverExtents(file.extents, &deflates));
373 file.deflates = std::move(deflates);
374 }
Tianjie Xu1a7bb2c2018-01-22 17:56:57 -0800375 }
376
Amin Hassani924183b2017-09-27 14:50:59 -0700377 result_files->push_back(file);
378 }
Amin Hassani924183b2017-09-27 14:50:59 -0700379 return true;
380}
381
382} // namespace deflate_utils
383} // namespace chromeos_update_engine