blob: a41e2830dad8b2f34144384f427d35859d25f9a6 [file] [log] [blame]
Amin Hassanid7da8f42017-08-23 14:29:40 -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/squashfs_filesystem.h"
18
19#include <fcntl.h>
20
21#include <algorithm>
22#include <string>
Amin Hassani3cd4df12017-08-25 11:21:53 -070023#include <utility>
Amin Hassanid7da8f42017-08-23 14:29:40 -070024
25#include <base/files/file_util.h>
Amin Hassani77c25fc2019-01-29 10:24:19 -080026#include <base/files/scoped_temp_dir.h>
Amin Hassanid7da8f42017-08-23 14:29:40 -070027#include <base/logging.h>
28#include <base/strings/string_number_conversions.h>
29#include <base/strings/string_split.h>
30#include <brillo/streams/file_stream.h>
31
32#include "update_engine/common/subprocess.h"
33#include "update_engine/common/utils.h"
Amin Hassani3cd4df12017-08-25 11:21:53 -070034#include "update_engine/payload_generator/deflate_utils.h"
Amin Hassanid7da8f42017-08-23 14:29:40 -070035#include "update_engine/payload_generator/delta_diff_generator.h"
36#include "update_engine/payload_generator/extent_ranges.h"
37#include "update_engine/payload_generator/extent_utils.h"
38#include "update_engine/update_metadata.pb.h"
39
Amin Hassani77c25fc2019-01-29 10:24:19 -080040using base::FilePath;
41using base::ScopedTempDir;
Amin Hassanid7da8f42017-08-23 14:29:40 -070042using std::string;
43using std::unique_ptr;
44using std::vector;
45
46namespace chromeos_update_engine {
47
48namespace {
49
Amin Hassanid7da8f42017-08-23 14:29:40 -070050// The size of the squashfs super block.
51constexpr size_t kSquashfsSuperBlockSize = 96;
52constexpr uint64_t kSquashfsCompressedBit = 1 << 24;
Amin Hassani3cd4df12017-08-25 11:21:53 -070053constexpr uint32_t kSquashfsZlibCompression = 1;
Amin Hassanid7da8f42017-08-23 14:29:40 -070054
Amin Hassani77c25fc2019-01-29 10:24:19 -080055constexpr char kUpdateEngineConf[] = "etc/update_engine.conf";
56
Amin Hassanid7da8f42017-08-23 14:29:40 -070057bool ReadSquashfsHeader(const brillo::Blob blob,
58 SquashfsFilesystem::SquashfsHeader* header) {
59 if (blob.size() < kSquashfsSuperBlockSize) {
60 return false;
61 }
62
63 memcpy(&header->magic, blob.data(), 4);
64 memcpy(&header->block_size, blob.data() + 12, 4);
65 memcpy(&header->compression_type, blob.data() + 20, 2);
66 memcpy(&header->major_version, blob.data() + 28, 2);
67 return true;
68}
69
70bool CheckHeader(const SquashfsFilesystem::SquashfsHeader& header) {
71 return header.magic == 0x73717368 && header.major_version == 4;
72}
73
74bool GetFileMapContent(const string& sqfs_path, string* map) {
Amin Hassanied03b442020-10-26 17:21:29 -070075 ScopedTempFile map_file("squashfs_file_map.XXXXXX");
Amin Hassanid7da8f42017-08-23 14:29:40 -070076 // Run unsquashfs to get the system file map.
77 // unsquashfs -m <map-file> <squashfs-file>
Amin Hassanied03b442020-10-26 17:21:29 -070078 vector<string> cmd = {"unsquashfs", "-m", map_file.path(), sqfs_path};
Amin Hassani3a4caa12019-11-06 11:12:28 -080079 string stdout, stderr;
Amin Hassanid7da8f42017-08-23 14:29:40 -070080 int exit_code;
Amin Hassani3a4caa12019-11-06 11:12:28 -080081 if (!Subprocess::SynchronousExec(cmd, &exit_code, &stdout, &stderr) ||
Amin Hassanid7da8f42017-08-23 14:29:40 -070082 exit_code != 0) {
Amin Hassani3a4caa12019-11-06 11:12:28 -080083 LOG(ERROR) << "Failed to run `unsquashfs -m` with stdout content: "
84 << stdout << " and stderr content: " << stderr;
Amin Hassanid7da8f42017-08-23 14:29:40 -070085 return false;
86 }
Amin Hassanied03b442020-10-26 17:21:29 -070087 TEST_AND_RETURN_FALSE(utils::ReadFile(map_file.path(), map));
Amin Hassanid7da8f42017-08-23 14:29:40 -070088 return true;
89}
90
Amin Hassani77c25fc2019-01-29 10:24:19 -080091bool GetUpdateEngineConfig(const std::string& sqfs_path, string* config) {
92 ScopedTempDir unsquash_dir;
93 if (!unsquash_dir.CreateUniqueTempDir()) {
94 PLOG(ERROR) << "Failed to create a temporary directory.";
95 return false;
96 }
97
98 // Run unsquashfs to extract update_engine.conf
99 // -f: To force overriding if the target directory exists.
100 // -d: The directory to unsquash the files.
101 vector<string> cmd = {"unsquashfs",
102 "-f",
103 "-d",
104 unsquash_dir.GetPath().value(),
105 sqfs_path,
106 kUpdateEngineConf};
Amin Hassani3a4caa12019-11-06 11:12:28 -0800107 string stdout, stderr;
Amin Hassani77c25fc2019-01-29 10:24:19 -0800108 int exit_code;
Amin Hassani3a4caa12019-11-06 11:12:28 -0800109 if (!Subprocess::SynchronousExec(cmd, &exit_code, &stdout, &stderr) ||
Amin Hassani77c25fc2019-01-29 10:24:19 -0800110 exit_code != 0) {
Amin Hassani3a4caa12019-11-06 11:12:28 -0800111 PLOG(ERROR) << "Failed to unsquashfs etc/update_engine.conf with stdout: "
112 << stdout << " and stderr: " << stderr;
Amin Hassani77c25fc2019-01-29 10:24:19 -0800113 return false;
114 }
115
116 auto config_path = unsquash_dir.GetPath().Append(kUpdateEngineConf);
117 string config_content;
118 if (!utils::ReadFile(config_path.value(), &config_content)) {
119 PLOG(ERROR) << "Failed to read " << config_path.value();
120 return false;
121 }
122
123 if (config_content.empty()) {
124 LOG(ERROR) << "update_engine config file was empty!!";
125 return false;
126 }
127
128 *config = std::move(config_content);
129 return true;
130}
131
Amin Hassanid7da8f42017-08-23 14:29:40 -0700132} // namespace
133
134bool SquashfsFilesystem::Init(const string& map,
Amin Hassani3cd4df12017-08-25 11:21:53 -0700135 const string& sqfs_path,
Amin Hassanid7da8f42017-08-23 14:29:40 -0700136 size_t size,
Amin Hassani3cd4df12017-08-25 11:21:53 -0700137 const SquashfsHeader& header,
138 bool extract_deflates) {
Amin Hassanid7da8f42017-08-23 14:29:40 -0700139 size_ = size;
Amin Hassani3cd4df12017-08-25 11:21:53 -0700140
141 bool is_zlib = header.compression_type == kSquashfsZlibCompression;
142 if (!is_zlib) {
143 LOG(WARNING) << "Filesystem is not Gzipped. Not filling deflates!";
144 }
145 vector<puffin::ByteExtent> zlib_blks;
146
Amin Hassanid7da8f42017-08-23 14:29:40 -0700147 // Reading files map. For the format of the file map look at the comments for
148 // |CreateFromFileMap()|.
149 auto lines = base::SplitStringPiece(map,
150 "\n",
151 base::WhitespaceHandling::KEEP_WHITESPACE,
152 base::SplitResult::SPLIT_WANT_NONEMPTY);
153 for (const auto& line : lines) {
154 auto splits =
155 base::SplitStringPiece(line,
156 " \t",
157 base::WhitespaceHandling::TRIM_WHITESPACE,
158 base::SplitResult::SPLIT_WANT_NONEMPTY);
159 // Only filename is invalid.
160 TEST_AND_RETURN_FALSE(splits.size() > 1);
161 uint64_t start;
162 TEST_AND_RETURN_FALSE(base::StringToUint64(splits[1], &start));
163 uint64_t cur_offset = start;
Amin Hassani1a200c12020-02-26 14:47:23 -0800164 bool is_compressed = false;
Amin Hassanid7da8f42017-08-23 14:29:40 -0700165 for (size_t i = 2; i < splits.size(); ++i) {
166 uint64_t blk_size;
167 TEST_AND_RETURN_FALSE(base::StringToUint64(splits[i], &blk_size));
168 // TODO(ahassani): For puffin push it into a proper list if uncompressed.
169 auto new_blk_size = blk_size & ~kSquashfsCompressedBit;
170 TEST_AND_RETURN_FALSE(new_blk_size <= header.block_size);
Amin Hassani3cd4df12017-08-25 11:21:53 -0700171 if (new_blk_size > 0 && !(blk_size & kSquashfsCompressedBit)) {
Amin Hassani1a200c12020-02-26 14:47:23 -0800172 // It is a compressed block.
Amin Hassani3cd4df12017-08-25 11:21:53 -0700173 if (is_zlib && extract_deflates) {
174 zlib_blks.emplace_back(cur_offset, new_blk_size);
175 }
Amin Hassani1a200c12020-02-26 14:47:23 -0800176 is_compressed = true;
Amin Hassani3cd4df12017-08-25 11:21:53 -0700177 }
Amin Hassanid7da8f42017-08-23 14:29:40 -0700178 cur_offset += new_blk_size;
179 }
180
181 // If size is zero do not add the file.
182 if (cur_offset - start > 0) {
183 File file;
184 file.name = splits[0].as_string();
185 file.extents = {ExtentForBytes(kBlockSize, start, cur_offset - start)};
Amin Hassani1a200c12020-02-26 14:47:23 -0800186 file.is_compressed = is_compressed;
Amin Hassanid7da8f42017-08-23 14:29:40 -0700187 files_.emplace_back(file);
188 }
189 }
190
191 // Sort all files by their offset in the squashfs.
192 std::sort(files_.begin(), files_.end(), [](const File& a, const File& b) {
193 return a.extents[0].start_block() < b.extents[0].start_block();
194 });
195 // If there is any overlap between two consecutive extents, remove them. Here
196 // we are assuming all files have exactly one extent. If this assumption
197 // changes then this implementation needs to change too.
Jae Hoon Kim3f894a82020-05-20 19:26:19 -0700198 for (auto first = files_.begin(),
199 second = first + (first == files_.end() ? 0 : 1);
Amin Hassanid7da8f42017-08-23 14:29:40 -0700200 first != files_.end() && second != files_.end();
201 second = first + 1) {
202 auto first_begin = first->extents[0].start_block();
203 auto first_end = first_begin + first->extents[0].num_blocks();
204 auto second_begin = second->extents[0].start_block();
205 auto second_end = second_begin + second->extents[0].num_blocks();
206 // Remove the first file if the size is zero.
207 if (first_end == first_begin) {
208 first = files_.erase(first);
209 } else if (first_end > second_begin) { // We found a collision.
210 if (second_end <= first_end) {
211 // Second file is inside the first file, remove the second file.
212 second = files_.erase(second);
213 } else if (first_begin == second_begin) {
214 // First file is inside the second file, remove the first file.
215 first = files_.erase(first);
216 } else {
217 // Remove overlapping extents from the first file.
218 first->extents[0].set_num_blocks(second_begin - first_begin);
219 ++first;
220 }
221 } else {
222 ++first;
223 }
224 }
225
226 // Find all the metadata including superblock and add them to the list of
227 // files.
228 ExtentRanges file_extents;
229 for (const auto& file : files_) {
230 file_extents.AddExtents(file.extents);
231 }
Sen Jiang0a582fb2018-06-26 19:27:21 -0700232 vector<Extent> full = {ExtentForBytes(kBlockSize, 0, size_)};
Amin Hassanid7da8f42017-08-23 14:29:40 -0700233 auto metadata_extents = FilterExtentRanges(full, file_extents);
234 // For now there should be at most two extents. One for superblock and one for
235 // metadata at the end. Just create appropriate files with <metadata-i> name.
236 // We can add all these extents as one metadata too, but that violates the
237 // contiguous write optimization.
238 for (size_t i = 0; i < metadata_extents.size(); i++) {
239 File file;
240 file.name = "<metadata-" + std::to_string(i) + ">";
241 file.extents = {metadata_extents[i]};
242 files_.emplace_back(file);
243 }
244
245 // Do one last sort before returning.
246 std::sort(files_.begin(), files_.end(), [](const File& a, const File& b) {
247 return a.extents[0].start_block() < b.extents[0].start_block();
248 });
Amin Hassani3cd4df12017-08-25 11:21:53 -0700249
250 if (is_zlib && extract_deflates) {
251 // If it is infact gzipped, then the sqfs_path should be valid to read its
252 // content.
253 TEST_AND_RETURN_FALSE(!sqfs_path.empty());
254 if (zlib_blks.empty()) {
255 return true;
256 }
257
258 // Sort zlib blocks.
259 std::sort(zlib_blks.begin(),
260 zlib_blks.end(),
261 [](const puffin::ByteExtent& a, const puffin::ByteExtent& b) {
262 return a.offset < b.offset;
263 });
264
Amin Hassani5d185052019-04-23 07:28:30 -0700265 // Sometimes a squashfs can have a two files that are hard linked. In this
266 // case both files will have the same starting offset in the image and hence
267 // the same zlib blocks. So we need to remove these duplicates to eliminate
268 // further potential probems. As a matter of fact the next statement will
269 // fail if there are duplicates (there will be overlap between two blocks).
270 auto last = std::unique(zlib_blks.begin(), zlib_blks.end());
271 zlib_blks.erase(last, zlib_blks.end());
272
Tianjiee283ce42020-07-29 11:37:51 -0700273 // Make sure zlib blocks are not overlapping.
Amin Hassani3cd4df12017-08-25 11:21:53 -0700274 auto result = std::adjacent_find(
275 zlib_blks.begin(),
276 zlib_blks.end(),
277 [](const puffin::ByteExtent& a, const puffin::ByteExtent& b) {
278 return (a.offset + a.length) > b.offset;
279 });
280 TEST_AND_RETURN_FALSE(result == zlib_blks.end());
281
282 vector<puffin::BitExtent> deflates;
283 TEST_AND_RETURN_FALSE(
284 puffin::LocateDeflatesInZlibBlocks(sqfs_path, zlib_blks, &deflates));
285
286 // Add deflates for each file.
287 for (auto& file : files_) {
288 file.deflates = deflate_utils::FindDeflates(file.extents, deflates);
289 }
290 }
Amin Hassanid7da8f42017-08-23 14:29:40 -0700291 return true;
292}
293
294unique_ptr<SquashfsFilesystem> SquashfsFilesystem::CreateFromFile(
Amin Hassani77c25fc2019-01-29 10:24:19 -0800295 const string& sqfs_path, bool extract_deflates, bool load_settings) {
Amin Hassanid7da8f42017-08-23 14:29:40 -0700296 if (sqfs_path.empty())
297 return nullptr;
298
299 brillo::StreamPtr sqfs_file =
Amin Hassani77c25fc2019-01-29 10:24:19 -0800300 brillo::FileStream::Open(FilePath(sqfs_path),
Amin Hassanid7da8f42017-08-23 14:29:40 -0700301 brillo::Stream::AccessMode::READ,
302 brillo::FileStream::Disposition::OPEN_EXISTING,
303 nullptr);
304 if (!sqfs_file) {
305 LOG(ERROR) << "Unable to open " << sqfs_path << " for reading.";
306 return nullptr;
307 }
308
309 SquashfsHeader header;
310 brillo::Blob blob(kSquashfsSuperBlockSize);
311 if (!sqfs_file->ReadAllBlocking(blob.data(), blob.size(), nullptr)) {
312 LOG(ERROR) << "Unable to read from file: " << sqfs_path;
313 return nullptr;
314 }
315 if (!ReadSquashfsHeader(blob, &header) || !CheckHeader(header)) {
316 // This is not necessary an error.
317 return nullptr;
318 }
319
320 // Read the map file.
321 string filemap;
322 if (!GetFileMapContent(sqfs_path, &filemap)) {
323 LOG(ERROR) << "Failed to produce squashfs map file: " << sqfs_path;
324 return nullptr;
325 }
326
327 unique_ptr<SquashfsFilesystem> sqfs(new SquashfsFilesystem());
Amin Hassani3cd4df12017-08-25 11:21:53 -0700328 if (!sqfs->Init(
329 filemap, sqfs_path, sqfs_file->GetSize(), header, extract_deflates)) {
Amin Hassanid7da8f42017-08-23 14:29:40 -0700330 LOG(ERROR) << "Failed to initialized the Squashfs file system";
331 return nullptr;
332 }
Amin Hassani3cd4df12017-08-25 11:21:53 -0700333
Amin Hassani77c25fc2019-01-29 10:24:19 -0800334 if (load_settings) {
335 if (!GetUpdateEngineConfig(sqfs_path, &sqfs->update_engine_config_)) {
336 return nullptr;
337 }
338 }
339
Amin Hassanid7da8f42017-08-23 14:29:40 -0700340 return sqfs;
341}
342
343unique_ptr<SquashfsFilesystem> SquashfsFilesystem::CreateFromFileMap(
344 const string& filemap, size_t size, const SquashfsHeader& header) {
345 if (!CheckHeader(header)) {
346 LOG(ERROR) << "Invalid Squashfs super block!";
347 return nullptr;
348 }
349
350 unique_ptr<SquashfsFilesystem> sqfs(new SquashfsFilesystem());
Amin Hassani3cd4df12017-08-25 11:21:53 -0700351 if (!sqfs->Init(filemap, "", size, header, false)) {
Amin Hassanid7da8f42017-08-23 14:29:40 -0700352 LOG(ERROR) << "Failed to initialize the Squashfs file system using filemap";
353 return nullptr;
354 }
355 // TODO(ahassani): Add a function that initializes the puffin related extents.
356 return sqfs;
357}
358
359size_t SquashfsFilesystem::GetBlockSize() const {
360 return kBlockSize;
361}
362
363size_t SquashfsFilesystem::GetBlockCount() const {
364 return size_ / kBlockSize;
365}
366
367bool SquashfsFilesystem::GetFiles(vector<File>* files) const {
368 files->insert(files->end(), files_.begin(), files_.end());
369 return true;
370}
371
372bool SquashfsFilesystem::LoadSettings(brillo::KeyValueStore* store) const {
Amin Hassani77c25fc2019-01-29 10:24:19 -0800373 if (!store->LoadFromString(update_engine_config_)) {
374 LOG(ERROR) << "Failed to load the settings with config: "
375 << update_engine_config_;
376 return false;
377 }
378 return true;
Amin Hassanid7da8f42017-08-23 14:29:40 -0700379}
380
381bool SquashfsFilesystem::IsSquashfsImage(const brillo::Blob& blob) {
382 SquashfsHeader header;
383 return ReadSquashfsHeader(blob, &header) && CheckHeader(header);
384}
Amin Hassanid7da8f42017-08-23 14:29:40 -0700385} // namespace chromeos_update_engine