blob: 7a5d9a4277c970fc6e949083cbdc4ec4ebed8fc2 [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"
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
34using std::string;
35using std::vector;
Amin Hassani3cd4df12017-08-25 11:21:53 -070036using puffin::BitExtent;
37using puffin::ByteExtent;
Amin Hassani924183b2017-09-27 14:50:59 -070038
39namespace chromeos_update_engine {
40namespace deflate_utils {
41namespace {
42
43// The minimum size for a squashfs image to be processed.
44const 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.
48bool CopyExtentsToFile(const string& in_path,
49 const vector<Extent> extents,
50 const string& out_path,
51 size_t block_size) {
52 brillo::Blob data(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
60bool 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 BlocksInExtents(file.extents) >= kMinimumSquashfsImageSize / kBlockSize) {
65 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
76// Realigns subfiles |files| of a splitted file |file| into its correct
77// positions. This can be used for squashfs, zip, apk, etc.
78bool RealignSplittedFiles(const FilesystemInterface::File& file,
79 vector<FilesystemInterface::File>* files) {
80 // We have to shift all the Extents in |files|, based on the Extents of the
81 // |file| itself.
82 size_t num_blocks = 0;
83 for (auto& in_file : *files) { // We need to modify so no constant.
84 TEST_AND_RETURN_FALSE(
85 ShiftExtentsOverExtents(file.extents, &in_file.extents));
Amin Hassani3cd4df12017-08-25 11:21:53 -070086 TEST_AND_RETURN_FALSE(
87 ShiftBitExtentsOverExtents(file.extents, &in_file.deflates));
88
Amin Hassani924183b2017-09-27 14:50:59 -070089 in_file.name = file.name + "/" + in_file.name;
90 num_blocks += BlocksInExtents(in_file.extents);
91 }
92
93 // Check that all files in |in_files| cover the entire image.
94 TEST_AND_RETURN_FALSE(BlocksInExtents(file.extents) == num_blocks);
95 return true;
96}
97
Amin Hassani3cd4df12017-08-25 11:21:53 -070098bool IsBitExtentInExtent(const Extent& extent, const BitExtent& bit_extent) {
99 return (bit_extent.offset / 8) >= (extent.start_block() * kBlockSize) &&
100 ((bit_extent.offset + bit_extent.length + 7) / 8) <=
101 ((extent.start_block() + extent.num_blocks()) * kBlockSize);
102}
103
Amin Hassani924183b2017-09-27 14:50:59 -0700104} // namespace
105
Amin Hassani3cd4df12017-08-25 11:21:53 -0700106ByteExtent ExpandToByteExtent(const BitExtent& extent) {
107 uint64_t offset = extent.offset / 8;
108 uint64_t length = ((extent.offset + extent.length + 7) / 8) - offset;
109 return {offset, length};
110}
111
Amin Hassani924183b2017-09-27 14:50:59 -0700112bool ShiftExtentsOverExtents(const vector<Extent>& base_extents,
113 vector<Extent>* over_extents) {
114 if (BlocksInExtents(base_extents) < BlocksInExtents(*over_extents)) {
115 LOG(ERROR) << "over_extents have more blocks than base_extents! Invalid!";
116 return false;
117 }
118 for (size_t idx = 0; idx < over_extents->size(); idx++) {
119 auto over_ext = &over_extents->at(idx);
120 auto gap_blocks = base_extents[0].start_block();
121 auto last_end_block = base_extents[0].start_block();
122 for (auto base_ext : base_extents) { // We need to modify |base_ext|, so we
123 // use copy.
124 gap_blocks += base_ext.start_block() - last_end_block;
125 last_end_block = base_ext.start_block() + base_ext.num_blocks();
126 base_ext.set_start_block(base_ext.start_block() - gap_blocks);
127 if (over_ext->start_block() >= base_ext.start_block() &&
128 over_ext->start_block() <
129 base_ext.start_block() + base_ext.num_blocks()) {
130 if (over_ext->start_block() + over_ext->num_blocks() <=
131 base_ext.start_block() + base_ext.num_blocks()) {
132 // |over_ext| is inside |base_ext|, increase its start block.
133 over_ext->set_start_block(over_ext->start_block() + gap_blocks);
134 } else {
135 // |over_ext| spills over this |base_ext|, split it into two.
136 auto new_blocks = base_ext.start_block() + base_ext.num_blocks() -
137 over_ext->start_block();
138 vector<Extent> new_extents = {
139 ExtentForRange(gap_blocks + over_ext->start_block(), new_blocks),
140 ExtentForRange(over_ext->start_block() + new_blocks,
141 over_ext->num_blocks() - new_blocks)};
142 *over_ext = new_extents[0];
143 over_extents->insert(std::next(over_extents->begin(), idx + 1),
144 new_extents[1]);
145 }
146 break; // We processed |over_ext|, so break the loop;
147 }
148 }
149 }
150 return true;
151}
152
Amin Hassani3cd4df12017-08-25 11:21:53 -0700153bool ShiftBitExtentsOverExtents(const vector<Extent>& base_extents,
154 vector<BitExtent>* over_extents) {
155 if (over_extents->empty()) {
156 return true;
157 }
158
159 // This check is needed to make sure the number of bytes in |over_extents|
160 // does not exceed |base_extents|.
161 auto last_extent = ExpandToByteExtent(over_extents->back());
162 TEST_AND_RETURN_FALSE(last_extent.offset + last_extent.length <=
163 BlocksInExtents(base_extents) * kBlockSize);
164
165 for (auto o_ext = over_extents->begin(); o_ext != over_extents->end();) {
166 size_t gap_blocks = base_extents[0].start_block();
167 size_t last_end_block = base_extents[0].start_block();
168 bool o_ext_processed = false;
169 for (auto b_ext : base_extents) { // We need to modify |b_ext|, so we copy.
170 gap_blocks += b_ext.start_block() - last_end_block;
171 last_end_block = b_ext.start_block() + b_ext.num_blocks();
172 b_ext.set_start_block(b_ext.start_block() - gap_blocks);
173 auto byte_o_ext = ExpandToByteExtent(*o_ext);
174 if (byte_o_ext.offset >= b_ext.start_block() * kBlockSize &&
175 byte_o_ext.offset <
176 (b_ext.start_block() + b_ext.num_blocks()) * kBlockSize) {
177 if ((byte_o_ext.offset + byte_o_ext.length) <=
178 (b_ext.start_block() + b_ext.num_blocks()) * kBlockSize) {
179 // |o_ext| is inside |b_ext|, increase its start block.
180 o_ext->offset += gap_blocks * kBlockSize * 8;
181 ++o_ext;
182 } else {
183 // |o_ext| spills over this |b_ext|, remove it.
184 o_ext = over_extents->erase(o_ext);
185 }
186 o_ext_processed = true;
187 break; // We processed o_ext, so break the loop;
188 }
189 }
190 TEST_AND_RETURN_FALSE(o_ext_processed);
191 }
192 return true;
193}
194
195vector<BitExtent> FindDeflates(const vector<Extent>& extents,
196 const vector<BitExtent>& in_deflates) {
197 vector<BitExtent> result;
198 // TODO(ahassani): Replace this with binary_search style search.
199 for (const auto& deflate : in_deflates) {
200 for (const auto& extent : extents) {
201 if (IsBitExtentInExtent(extent, deflate)) {
202 result.push_back(deflate);
203 break;
204 }
205 }
206 }
207 return result;
208}
209
210bool CompactDeflates(const vector<Extent>& extents,
211 const vector<BitExtent>& in_deflates,
212 vector<BitExtent>* out_deflates) {
213 size_t bytes_passed = 0;
214 out_deflates->reserve(in_deflates.size());
215 for (const auto& extent : extents) {
216 size_t gap_bytes = extent.start_block() * kBlockSize - bytes_passed;
217 for (const auto& deflate : in_deflates) {
218 if (IsBitExtentInExtent(extent, deflate)) {
219 out_deflates->emplace_back(deflate.offset - (gap_bytes * 8),
220 deflate.length);
221 }
222 }
223 bytes_passed += extent.num_blocks() * kBlockSize;
224 }
225
226 // All given |in_deflates| items should've been inside one of the extents in
227 // |extents|.
228 TEST_AND_RETURN_FALSE(in_deflates.size() == out_deflates->size());
229
230 // Make sure all outgoing deflates are ordered and non-overlapping.
231 auto result = std::adjacent_find(out_deflates->begin(),
232 out_deflates->end(),
233 [](const BitExtent& a, const BitExtent& b) {
234 return (a.offset + a.length) > b.offset;
235 });
236 TEST_AND_RETURN_FALSE(result == out_deflates->end());
237 return true;
238}
239
240bool FindAndCompactDeflates(const vector<Extent>& extents,
241 const vector<BitExtent>& in_deflates,
242 vector<BitExtent>* out_deflates) {
243 auto found_deflates = FindDeflates(extents, in_deflates);
244 TEST_AND_RETURN_FALSE(CompactDeflates(extents, found_deflates, out_deflates));
245 return true;
246}
247
Amin Hassani924183b2017-09-27 14:50:59 -0700248bool PreprocessParitionFiles(const PartitionConfig& part,
Amin Hassani3cd4df12017-08-25 11:21:53 -0700249 vector<FilesystemInterface::File>* result_files,
250 bool extract_deflates) {
Amin Hassani924183b2017-09-27 14:50:59 -0700251 // Get the file system files.
252 vector<FilesystemInterface::File> tmp_files;
253 part.fs_interface->GetFiles(&tmp_files);
254 result_files->reserve(tmp_files.size());
255
256 for (const auto& file : tmp_files) {
257 if (IsSquashfsImage(part.path, file)) {
258 // Read the image into a file.
259 base::FilePath path;
260 TEST_AND_RETURN_FALSE(base::CreateTemporaryFile(&path));
261 ScopedPathUnlinker old_unlinker(path.value());
262 TEST_AND_RETURN_FALSE(
263 CopyExtentsToFile(part.path, file.extents, path.value(), kBlockSize));
264 // Test if it is actually a Squashfs file.
Amin Hassani3cd4df12017-08-25 11:21:53 -0700265 auto sqfs =
266 SquashfsFilesystem::CreateFromFile(path.value(), extract_deflates);
Amin Hassani924183b2017-09-27 14:50:59 -0700267 if (sqfs) {
268 // It is an squashfs file. Get its files to replace with itself.
269 vector<FilesystemInterface::File> files;
270 sqfs->GetFiles(&files);
271
Amin Hassani3cd4df12017-08-25 11:21:53 -0700272 // Replace squashfs file with its files only if |files| has at least two
273 // files or if it has some deflates (since it is better to replace it to
274 // take advantage of the deflates.)
275 if (files.size() > 1 ||
276 (files.size() == 1 && !files[0].deflates.empty())) {
Amin Hassani924183b2017-09-27 14:50:59 -0700277 TEST_AND_RETURN_FALSE(RealignSplittedFiles(file, &files));
278 result_files->insert(result_files->end(), files.begin(), files.end());
279 continue;
280 }
281 } else {
282 LOG(WARNING) << "We thought file: " << file.name
283 << " was a Squashfs file, but it was not.";
284 }
285 }
286 // TODO(ahassani): Process other types of files like apk, zip, etc.
287 result_files->push_back(file);
288 }
Amin Hassani924183b2017-09-27 14:50:59 -0700289 return true;
290}
291
292} // namespace deflate_utils
293} // namespace chromeos_update_engine