1 //
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"
30 #include "update_engine/payload_generator/extent_utils.h"
31 #include "update_engine/payload_generator/squashfs_filesystem.h"
32 #include "update_engine/update_metadata.pb.h"
33
34 using puffin::BitExtent;
35 using puffin::ByteExtent;
36 using std::string;
37 using std::vector;
38
39 namespace chromeos_update_engine {
40 namespace deflate_utils {
41 namespace {
42
operator <<(std::ostream & out,const puffin::BitExtent & ext)43 constexpr std::ostream& operator<<(std::ostream& out,
44 const puffin::BitExtent& ext) {
45 out << "BitExtent(" << ext.offset << "," << ext.length << ")";
46 return out;
47 }
48
49 // The minimum size for a squashfs image to be processed.
50 const 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.
CopyExtentsToFile(const string & in_path,const vector<Extent> & extents,const string & out_path,size_t block_size)54 bool CopyExtentsToFile(const string& in_path,
55 const vector<Extent>& extents,
56 const string& out_path,
57 size_t block_size) {
58 brillo::Blob data(utils::BlocksInExtents(extents) * block_size);
59 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
IsSquashfsImage(const string & part_path,const FilesystemInterface::File & file)66 bool 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) &&
70 utils::BlocksInExtents(file.extents) >=
71 kMinimumSquashfsImageSize / kBlockSize) {
72 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
IsRegularFile(const FilesystemInterface::File & file)83 bool 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
92 // Realigns subfiles |files| of a splitted file |file| into its correct
93 // positions. This can be used for squashfs, zip, apk, etc.
RealignSplittedFiles(const FilesystemInterface::File & file,vector<FilesystemInterface::File> * files)94 bool 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));
102 TEST_AND_RETURN_FALSE(
103 ShiftBitExtentsOverExtents(file.extents, &in_file.deflates));
104
105 in_file.name = file.name + "/" + in_file.name;
106 num_blocks += utils::BlocksInExtents(in_file.extents);
107 }
108
109 // Check that all files in |in_files| cover the entire image.
110 TEST_AND_RETURN_FALSE(utils::BlocksInExtents(file.extents) == num_blocks);
111 return true;
112 }
113
IsBitExtentInExtent(const Extent & extent,const BitExtent & bit_extent)114 bool 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
120 // Returns whether the given file |name| has an extension listed in
121 // |extensions|.
122
123 } // namespace
124
ToStringPiece(std::string_view s)125 constexpr base::StringPiece ToStringPiece(std::string_view s) {
126 return base::StringPiece(s.data(), s.length());
127 }
128
IsFileExtensions(const std::string_view name,const std::initializer_list<std::string_view> & extensions)129 bool 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 });
139 }
140
ExpandToByteExtent(const BitExtent & extent)141 ByteExtent 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
ShiftExtentsOverExtents(const vector<Extent> & base_extents,vector<Extent> * over_extents)147 bool ShiftExtentsOverExtents(const vector<Extent>& base_extents,
148 vector<Extent>* over_extents) {
149 if (utils::BlocksInExtents(base_extents) <
150 utils::BlocksInExtents(*over_extents)) {
151 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
ShiftBitExtentsOverExtents(const vector<Extent> & base_extents,vector<BitExtent> * over_extents)189 bool 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());
198 TEST_LE(last_extent.offset + last_extent.length,
199 utils::BlocksInExtents(base_extents) * kBlockSize);
200
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
FindDeflates(const vector<Extent> & extents,const vector<BitExtent> & in_deflates)231 vector<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
CompactDeflates(const vector<Extent> & extents,const vector<BitExtent> & in_deflates,vector<BitExtent> * out_deflates)246 bool 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|.
264 TEST_EQ(in_deflates.size(), out_deflates->size());
265 Dedup(out_deflates);
266
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 });
273 if (result != out_deflates->end()) {
274 LOG(ERROR) << "out_deflate is overlapped " << (*result) << ", "
275 << *(++result);
276 return false;
277 }
278 return true;
279 }
280
FindAndCompactDeflates(const vector<Extent> & extents,const vector<BitExtent> & in_deflates,vector<BitExtent> * out_deflates)281 bool 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
DeflatePreprocessFileData(const std::string_view filename,const brillo::Blob & data,vector<puffin::BitExtent> * deflates)289 bool 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
PreprocessPartitionFiles(const PartitionConfig & part,vector<FilesystemInterface::File> * result_files,bool extract_deflates)311 bool PreprocessPartitionFiles(const PartitionConfig& part,
312 vector<FilesystemInterface::File>* result_files,
313 bool extract_deflates) {
314 // 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
319 for (auto& file : tmp_files) {
320 auto is_regular_file = IsRegularFile(file);
321
322 if (is_regular_file && IsSquashfsImage(part.path, file)) {
323 // 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.
330 auto sqfs =
331 SquashfsFilesystem::CreateFromFile(path.value(), extract_deflates);
332 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
337 // 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())) {
342 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 }
351
352 if (is_regular_file && extract_deflates && !file.is_compressed) {
353 // 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).
356 bool is_zip = IsFileExtensions(
357 file.name, {".apk", ".zip", ".jar", ".zvoice", ".apex", "capex"});
358 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));
367 // |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 }
374 vector<puffin::BitExtent> deflates;
375 if (!DeflatePreprocessFileData(file.name, data, &deflates)) {
376 LOG(ERROR) << "Failed to preprocess deflate data in partition "
377 << part.name;
378 return false;
379 }
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 }
387 }
388
389 result_files->push_back(file);
390 }
391 return true;
392 }
393
394 } // namespace deflate_utils
395 } // namespace chromeos_update_engine
396