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