blob: d1967991d230bfd8602bfaa28671decebe38fd36 [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"
Kelvin Zhang7949fce2024-03-12 10:17:40 -070030#include "update_engine/payload_generator/extent_utils.h"
Amin Hassani924183b2017-09-27 14:50:59 -070031#include "update_engine/payload_generator/squashfs_filesystem.h"
32#include "update_engine/update_metadata.pb.h"
33
Amin Hassani3cd4df12017-08-25 11:21:53 -070034using puffin::BitExtent;
35using puffin::ByteExtent;
Amin Hassani232f8f92019-01-14 16:15:31 -080036using std::string;
37using std::vector;
Amin Hassani924183b2017-09-27 14:50:59 -070038
39namespace chromeos_update_engine {
40namespace deflate_utils {
41namespace {
42
Kelvin Zhang7949fce2024-03-12 10:17:40 -070043constexpr std::ostream& operator<<(std::ostream& out,
44 const puffin::BitExtent& ext) {
45 out << "BitExtent(" << ext.offset << "," << ext.length << ")";
46 return out;
47}
48
Amin Hassani924183b2017-09-27 14:50:59 -070049// The minimum size for a squashfs image to be processed.
50const uint64_t kMinimumSquashfsImageSize = 1 * 1024 * 1024; // bytes
51
52// TODO(*): Optimize this so we don't have to read all extents into memory in
53// case it is large.
54bool CopyExtentsToFile(const string& in_path,
Andrew9d5a61d2020-03-26 13:40:37 -070055 const vector<Extent>& extents,
Amin Hassani924183b2017-09-27 14:50:59 -070056 const string& out_path,
57 size_t block_size) {
Amin Hassanid8b67f42017-12-06 13:47:52 -080058 brillo::Blob data(utils::BlocksInExtents(extents) * block_size);
Amin Hassani924183b2017-09-27 14:50:59 -070059 TEST_AND_RETURN_FALSE(
60 utils::ReadExtents(in_path, extents, &data, data.size(), block_size));
61 TEST_AND_RETURN_FALSE(
62 utils::WriteFile(out_path.c_str(), data.data(), data.size()));
63 return true;
64}
65
66bool IsSquashfsImage(const string& part_path,
67 const FilesystemInterface::File& file) {
68 // Only check for files with img postfix.
69 if (base::EndsWith(file.name, ".img", base::CompareCase::SENSITIVE) &&
Amin Hassanid8b67f42017-12-06 13:47:52 -080070 utils::BlocksInExtents(file.extents) >=
71 kMinimumSquashfsImageSize / kBlockSize) {
Amin Hassani924183b2017-09-27 14:50:59 -070072 brillo::Blob super_block;
73 TEST_AND_RETURN_FALSE(
74 utils::ReadFileChunk(part_path,
75 file.extents[0].start_block() * kBlockSize,
76 100,
77 &super_block));
78 return SquashfsFilesystem::IsSquashfsImage(super_block);
79 }
80 return false;
81}
82
Håkan Kviste4d414e2019-06-28 08:05:06 +020083bool IsRegularFile(const FilesystemInterface::File& file) {
84 // If inode is 0, then stat information is invalid for some psuedo files
85 if (file.file_stat.st_ino != 0 &&
86 (file.file_stat.st_mode & S_IFMT) == S_IFREG) {
87 return true;
88 }
89 return false;
90}
91
Amin Hassani924183b2017-09-27 14:50:59 -070092// Realigns subfiles |files| of a splitted file |file| into its correct
93// positions. This can be used for squashfs, zip, apk, etc.
94bool RealignSplittedFiles(const FilesystemInterface::File& file,
95 vector<FilesystemInterface::File>* files) {
96 // We have to shift all the Extents in |files|, based on the Extents of the
97 // |file| itself.
98 size_t num_blocks = 0;
99 for (auto& in_file : *files) { // We need to modify so no constant.
100 TEST_AND_RETURN_FALSE(
101 ShiftExtentsOverExtents(file.extents, &in_file.extents));
Amin Hassani3cd4df12017-08-25 11:21:53 -0700102 TEST_AND_RETURN_FALSE(
103 ShiftBitExtentsOverExtents(file.extents, &in_file.deflates));
104
Amin Hassani924183b2017-09-27 14:50:59 -0700105 in_file.name = file.name + "/" + in_file.name;
Amin Hassanid8b67f42017-12-06 13:47:52 -0800106 num_blocks += utils::BlocksInExtents(in_file.extents);
Amin Hassani924183b2017-09-27 14:50:59 -0700107 }
108
109 // Check that all files in |in_files| cover the entire image.
Amin Hassanid8b67f42017-12-06 13:47:52 -0800110 TEST_AND_RETURN_FALSE(utils::BlocksInExtents(file.extents) == num_blocks);
Amin Hassani924183b2017-09-27 14:50:59 -0700111 return true;
112}
113
Amin Hassani3cd4df12017-08-25 11:21:53 -0700114bool IsBitExtentInExtent(const Extent& extent, const BitExtent& bit_extent) {
115 return (bit_extent.offset / 8) >= (extent.start_block() * kBlockSize) &&
116 ((bit_extent.offset + bit_extent.length + 7) / 8) <=
117 ((extent.start_block() + extent.num_blocks()) * kBlockSize);
118}
119
Sen Jiang23bae402018-11-13 11:27:29 -0800120// Returns whether the given file |name| has an extension listed in
121// |extensions|.
Kelvin Zhang0af01222021-10-05 13:36:47 -0700122
123} // namespace
124
Kelvin Zhangcb2dc882021-11-22 09:26:50 -0800125constexpr base::StringPiece ToStringPiece(std::string_view s) {
126 return base::StringPiece(s.data(), s.length());
127}
128
129bool IsFileExtensions(
130 const std::string_view name,
131 const std::initializer_list<std::string_view>& extensions) {
132 return any_of(extensions.begin(),
133 extensions.end(),
134 [name = ToStringPiece(name)](const auto& ext) {
135 return base::EndsWith(name,
136 ToStringPiece(ext),
137 base::CompareCase::INSENSITIVE_ASCII);
138 });
Sen Jiang23bae402018-11-13 11:27:29 -0800139}
140
Amin Hassani3cd4df12017-08-25 11:21:53 -0700141ByteExtent ExpandToByteExtent(const BitExtent& extent) {
142 uint64_t offset = extent.offset / 8;
143 uint64_t length = ((extent.offset + extent.length + 7) / 8) - offset;
144 return {offset, length};
145}
146
Amin Hassani924183b2017-09-27 14:50:59 -0700147bool ShiftExtentsOverExtents(const vector<Extent>& base_extents,
148 vector<Extent>* over_extents) {
Amin Hassanid8b67f42017-12-06 13:47:52 -0800149 if (utils::BlocksInExtents(base_extents) <
150 utils::BlocksInExtents(*over_extents)) {
Amin Hassani924183b2017-09-27 14:50:59 -0700151 LOG(ERROR) << "over_extents have more blocks than base_extents! Invalid!";
152 return false;
153 }
154 for (size_t idx = 0; idx < over_extents->size(); idx++) {
155 auto over_ext = &over_extents->at(idx);
156 auto gap_blocks = base_extents[0].start_block();
157 auto last_end_block = base_extents[0].start_block();
158 for (auto base_ext : base_extents) { // We need to modify |base_ext|, so we
159 // use copy.
160 gap_blocks += base_ext.start_block() - last_end_block;
161 last_end_block = base_ext.start_block() + base_ext.num_blocks();
162 base_ext.set_start_block(base_ext.start_block() - gap_blocks);
163 if (over_ext->start_block() >= base_ext.start_block() &&
164 over_ext->start_block() <
165 base_ext.start_block() + base_ext.num_blocks()) {
166 if (over_ext->start_block() + over_ext->num_blocks() <=
167 base_ext.start_block() + base_ext.num_blocks()) {
168 // |over_ext| is inside |base_ext|, increase its start block.
169 over_ext->set_start_block(over_ext->start_block() + gap_blocks);
170 } else {
171 // |over_ext| spills over this |base_ext|, split it into two.
172 auto new_blocks = base_ext.start_block() + base_ext.num_blocks() -
173 over_ext->start_block();
174 vector<Extent> new_extents = {
175 ExtentForRange(gap_blocks + over_ext->start_block(), new_blocks),
176 ExtentForRange(over_ext->start_block() + new_blocks,
177 over_ext->num_blocks() - new_blocks)};
178 *over_ext = new_extents[0];
179 over_extents->insert(std::next(over_extents->begin(), idx + 1),
180 new_extents[1]);
181 }
182 break; // We processed |over_ext|, so break the loop;
183 }
184 }
185 }
186 return true;
187}
188
Amin Hassani3cd4df12017-08-25 11:21:53 -0700189bool ShiftBitExtentsOverExtents(const vector<Extent>& base_extents,
190 vector<BitExtent>* over_extents) {
191 if (over_extents->empty()) {
192 return true;
193 }
194
195 // This check is needed to make sure the number of bytes in |over_extents|
196 // does not exceed |base_extents|.
197 auto last_extent = ExpandToByteExtent(over_extents->back());
Kelvin Zhangcb2dc882021-11-22 09:26:50 -0800198 TEST_LE(last_extent.offset + last_extent.length,
199 utils::BlocksInExtents(base_extents) * kBlockSize);
Amin Hassani3cd4df12017-08-25 11:21:53 -0700200
201 for (auto o_ext = over_extents->begin(); o_ext != over_extents->end();) {
202 size_t gap_blocks = base_extents[0].start_block();
203 size_t last_end_block = base_extents[0].start_block();
204 bool o_ext_processed = false;
205 for (auto b_ext : base_extents) { // We need to modify |b_ext|, so we copy.
206 gap_blocks += b_ext.start_block() - last_end_block;
207 last_end_block = b_ext.start_block() + b_ext.num_blocks();
208 b_ext.set_start_block(b_ext.start_block() - gap_blocks);
209 auto byte_o_ext = ExpandToByteExtent(*o_ext);
210 if (byte_o_ext.offset >= b_ext.start_block() * kBlockSize &&
211 byte_o_ext.offset <
212 (b_ext.start_block() + b_ext.num_blocks()) * kBlockSize) {
213 if ((byte_o_ext.offset + byte_o_ext.length) <=
214 (b_ext.start_block() + b_ext.num_blocks()) * kBlockSize) {
215 // |o_ext| is inside |b_ext|, increase its start block.
216 o_ext->offset += gap_blocks * kBlockSize * 8;
217 ++o_ext;
218 } else {
219 // |o_ext| spills over this |b_ext|, remove it.
220 o_ext = over_extents->erase(o_ext);
221 }
222 o_ext_processed = true;
223 break; // We processed o_ext, so break the loop;
224 }
225 }
226 TEST_AND_RETURN_FALSE(o_ext_processed);
227 }
228 return true;
229}
230
231vector<BitExtent> FindDeflates(const vector<Extent>& extents,
232 const vector<BitExtent>& in_deflates) {
233 vector<BitExtent> result;
234 // TODO(ahassani): Replace this with binary_search style search.
235 for (const auto& deflate : in_deflates) {
236 for (const auto& extent : extents) {
237 if (IsBitExtentInExtent(extent, deflate)) {
238 result.push_back(deflate);
239 break;
240 }
241 }
242 }
243 return result;
244}
245
246bool CompactDeflates(const vector<Extent>& extents,
247 const vector<BitExtent>& in_deflates,
248 vector<BitExtent>* out_deflates) {
249 size_t bytes_passed = 0;
250 out_deflates->reserve(in_deflates.size());
251 for (const auto& extent : extents) {
252 size_t gap_bytes = extent.start_block() * kBlockSize - bytes_passed;
253 for (const auto& deflate : in_deflates) {
254 if (IsBitExtentInExtent(extent, deflate)) {
255 out_deflates->emplace_back(deflate.offset - (gap_bytes * 8),
256 deflate.length);
257 }
258 }
259 bytes_passed += extent.num_blocks() * kBlockSize;
260 }
261
262 // All given |in_deflates| items should've been inside one of the extents in
263 // |extents|.
Kelvin Zhang7949fce2024-03-12 10:17:40 -0700264 TEST_EQ(in_deflates.size(), out_deflates->size());
265 Dedup(out_deflates);
Amin Hassani3cd4df12017-08-25 11:21:53 -0700266
267 // Make sure all outgoing deflates are ordered and non-overlapping.
268 auto result = std::adjacent_find(out_deflates->begin(),
269 out_deflates->end(),
270 [](const BitExtent& a, const BitExtent& b) {
271 return (a.offset + a.length) > b.offset;
272 });
Kelvin Zhang7949fce2024-03-12 10:17:40 -0700273 if (result != out_deflates->end()) {
274 LOG(ERROR) << "out_deflate is overlapped " << (*result) << ", "
275 << *(++result);
276 return false;
277 }
Amin Hassani3cd4df12017-08-25 11:21:53 -0700278 return true;
279}
280
281bool FindAndCompactDeflates(const vector<Extent>& extents,
282 const vector<BitExtent>& in_deflates,
283 vector<BitExtent>* out_deflates) {
284 auto found_deflates = FindDeflates(extents, in_deflates);
285 TEST_AND_RETURN_FALSE(CompactDeflates(extents, found_deflates, out_deflates));
286 return true;
287}
288
Kelvin Zhangcb2dc882021-11-22 09:26:50 -0800289bool DeflatePreprocessFileData(const std::string_view filename,
290 const brillo::Blob& data,
291 vector<puffin::BitExtent>* deflates) {
292 bool is_zip = IsFileExtensions(
293 filename, {".apk", ".zip", ".jar", ".zvoice", ".apex", "capex"});
294 bool is_gzip = IsFileExtensions(filename, {".gz", ".gzip", ".tgz"});
295 if (is_zip) {
296 if (!puffin::LocateDeflatesInZipArchive(data, deflates)) {
297 LOG(ERROR) << "Failed to locate deflates in zip file " << filename;
298 deflates->clear();
299 return false;
300 }
301 } else if (is_gzip) {
302 if (!puffin::LocateDeflatesInGzip(data, deflates)) {
303 LOG(ERROR) << "Failed to locate deflates in gzip file " << filename;
304 deflates->clear();
305 return false;
306 }
307 }
308 return true;
309}
310
Sen Jiangce39e672018-11-28 16:43:00 -0800311bool PreprocessPartitionFiles(const PartitionConfig& part,
312 vector<FilesystemInterface::File>* result_files,
313 bool extract_deflates) {
Amin Hassani924183b2017-09-27 14:50:59 -0700314 // Get the file system files.
315 vector<FilesystemInterface::File> tmp_files;
316 part.fs_interface->GetFiles(&tmp_files);
317 result_files->reserve(tmp_files.size());
318
Tianjie Xu1a7bb2c2018-01-22 17:56:57 -0800319 for (auto& file : tmp_files) {
Håkan Kviste4d414e2019-06-28 08:05:06 +0200320 auto is_regular_file = IsRegularFile(file);
321
322 if (is_regular_file && IsSquashfsImage(part.path, file)) {
Amin Hassani924183b2017-09-27 14:50:59 -0700323 // Read the image into a file.
324 base::FilePath path;
325 TEST_AND_RETURN_FALSE(base::CreateTemporaryFile(&path));
326 ScopedPathUnlinker old_unlinker(path.value());
327 TEST_AND_RETURN_FALSE(
328 CopyExtentsToFile(part.path, file.extents, path.value(), kBlockSize));
329 // Test if it is actually a Squashfs file.
Kelvin Zhanged9b2082023-05-25 13:58:19 -0700330 auto sqfs =
331 SquashfsFilesystem::CreateFromFile(path.value(), extract_deflates);
Amin Hassani924183b2017-09-27 14:50:59 -0700332 if (sqfs) {
333 // It is an squashfs file. Get its files to replace with itself.
334 vector<FilesystemInterface::File> files;
335 sqfs->GetFiles(&files);
336
Amin Hassani3cd4df12017-08-25 11:21:53 -0700337 // Replace squashfs file with its files only if |files| has at least two
338 // files or if it has some deflates (since it is better to replace it to
339 // take advantage of the deflates.)
340 if (files.size() > 1 ||
341 (files.size() == 1 && !files[0].deflates.empty())) {
Amin Hassani924183b2017-09-27 14:50:59 -0700342 TEST_AND_RETURN_FALSE(RealignSplittedFiles(file, &files));
343 result_files->insert(result_files->end(), files.begin(), files.end());
344 continue;
345 }
346 } else {
347 LOG(WARNING) << "We thought file: " << file.name
348 << " was a Squashfs file, but it was not.";
349 }
350 }
Tianjie Xu1a7bb2c2018-01-22 17:56:57 -0800351
Tianjie55abd3c2020-06-19 00:22:59 -0700352 if (is_regular_file && extract_deflates && !file.is_compressed) {
Sen Jiang23bae402018-11-13 11:27:29 -0800353 // Search for deflates if the file is in zip or gzip format.
354 // .zvoice files may eventually move out of rootfs. If that happens,
355 // remove ".zvoice" (crbug.com/782918).
Sen Jiang812e9d12018-11-21 16:40:37 -0800356 bool is_zip = IsFileExtensions(
Kelvin Zhangdc1f2582021-09-13 16:43:41 -0700357 file.name, {".apk", ".zip", ".jar", ".zvoice", ".apex", "capex"});
Sen Jiang23bae402018-11-13 11:27:29 -0800358 bool is_gzip = IsFileExtensions(file.name, {".gz", ".gzip", ".tgz"});
359 if (is_zip || is_gzip) {
360 brillo::Blob data;
361 TEST_AND_RETURN_FALSE(utils::ReadExtents(
362 part.path,
363 file.extents,
364 &data,
365 kBlockSize * utils::BlocksInExtents(file.extents),
366 kBlockSize));
Kelvin Zhang6fbf5942021-12-09 14:06:25 -0800367 // |data| read from disk always has size multiple of kBlockSize. So it
368 // might contain trailing garbage data and confuse the gzip/zip
369 // processors. Trim them.
370 if (file.file_stat.st_size > 0 &&
371 static_cast<size_t>(file.file_stat.st_size) < data.size()) {
372 data.resize(file.file_stat.st_size);
373 }
Sen Jiang23bae402018-11-13 11:27:29 -0800374 vector<puffin::BitExtent> deflates;
Kelvin Zhangcb2dc882021-11-22 09:26:50 -0800375 if (!DeflatePreprocessFileData(file.name, data, &deflates)) {
376 LOG(ERROR) << "Failed to preprocess deflate data in partition "
377 << part.name;
378 return false;
Sen Jiang23bae402018-11-13 11:27:29 -0800379 }
380 // Shift the deflate's extent to the offset starting from the beginning
381 // of the current partition; and the delta processor will align the
382 // extents in a continuous buffer later.
383 TEST_AND_RETURN_FALSE(
384 ShiftBitExtentsOverExtents(file.extents, &deflates));
385 file.deflates = std::move(deflates);
386 }
Tianjie Xu1a7bb2c2018-01-22 17:56:57 -0800387 }
388
Amin Hassani924183b2017-09-27 14:50:59 -0700389 result_files->push_back(file);
390 }
Amin Hassani924183b2017-09-27 14:50:59 -0700391 return true;
392}
393
394} // namespace deflate_utils
395} // namespace chromeos_update_engine