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