blob: 93540b2e54a660638d951e542afe5fb80a7a1e8d [file] [log] [blame]
Tianjie Xuc1a5e262019-05-22 14:34:12 -07001/*
2 * Copyright (C) 2019 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 "updater/target_files.h"
18
Tianjie Xu74b0f7c2019-05-22 13:59:57 -070019#include <unistd.h>
20
21#include <algorithm>
22#include <filesystem>
23#include <memory>
24
25#include <android-base/logging.h>
26#include <android-base/strings.h>
27#include <sparse/sparse.h>
28
29static bool SimgToImg(int input_fd, int output_fd) {
30 if (lseek64(input_fd, 0, SEEK_SET) == -1) {
31 PLOG(ERROR) << "Failed to lseek64 on the input sparse image";
32 return false;
33 }
34
35 if (lseek64(output_fd, 0, SEEK_SET) == -1) {
36 PLOG(ERROR) << "Failed to lseek64 on the output raw image";
37 return false;
38 }
39
40 std::unique_ptr<sparse_file, decltype(&sparse_file_destroy)> s_file(
41 sparse_file_import(input_fd, true, false), sparse_file_destroy);
42 if (!s_file) {
43 LOG(ERROR) << "Failed to import the sparse image.";
44 return false;
45 }
46
47 if (sparse_file_write(s_file.get(), output_fd, false, false, false) < 0) {
48 PLOG(ERROR) << "Failed to output the raw image file.";
49 return false;
50 }
51
52 return true;
Tianjie Xuc1a5e262019-05-22 14:34:12 -070053}
54
Tianjie Xu74b0f7c2019-05-22 13:59:57 -070055static bool ParsePropertyFile(const std::string_view prop_content,
56 std::map<std::string, std::string, std::less<>>* props_map) {
57 LOG(INFO) << "Start parsing build property\n";
58 std::vector<std::string> lines = android::base::Split(std::string(prop_content), "\n");
59 for (const auto& line : lines) {
60 if (line.empty() || line[0] == '#') continue;
61 auto pos = line.find('=');
62 if (pos == std::string::npos) continue;
63 std::string key = line.substr(0, pos);
64 std::string value = line.substr(pos + 1);
65 LOG(INFO) << key << ": " << value;
66 props_map->emplace(key, value);
67 }
68
69 return true;
70}
71
72static bool ParseFstab(const std::string_view fstab, std::vector<FstabInfo>* fstab_info_list) {
73 LOG(INFO) << "parsing fstab\n";
74 std::vector<std::string> lines = android::base::Split(std::string(fstab), "\n");
75 for (const auto& line : lines) {
76 if (line.empty() || line[0] == '#') continue;
77
78 // <block_device> <mount_point> <fs_type> <mount_flags> optional:<fs_mgr_flags>
79 std::vector<std::string> tokens = android::base::Split(line, " ");
80 tokens.erase(std::remove(tokens.begin(), tokens.end(), ""), tokens.end());
81 if (tokens.size() != 4 && tokens.size() != 5) {
82 LOG(ERROR) << "Unexpected token size: " << tokens.size() << std::endl
83 << "Error parsing fstab line: " << line;
84 return false;
85 }
86
87 const auto& blockdev = tokens[0];
88 const auto& mount_point = tokens[1];
89 const auto& fs_type = tokens[2];
90 if (!android::base::StartsWith(mount_point, "/")) {
91 LOG(WARNING) << "mount point '" << mount_point << "' does not start with '/'";
92 continue;
93 }
94
95 // The simulator only supports ext4 and emmc for now.
96 if (fs_type != "ext4" && fs_type != "emmc") {
97 LOG(WARNING) << "Unsupported fs_type in " << line;
98 continue;
99 }
100
101 fstab_info_list->emplace_back(blockdev, mount_point, fs_type);
102 }
103
104 return true;
105}
106
107bool TargetFile::EntryExists(const std::string_view name) const {
108 if (extracted_input_) {
109 std::string entry_path = path_ + "/" + std::string(name);
110 if (access(entry_path.c_str(), O_RDONLY) != 0) {
111 PLOG(WARNING) << "Failed to access " << entry_path;
112 return false;
113 }
114 return true;
115 }
116
117 CHECK(handle_);
118 ZipEntry img_entry;
119 return FindEntry(handle_, name, &img_entry) == 0;
120}
121
122bool TargetFile::ReadEntryToString(const std::string_view name, std::string* content) const {
123 if (extracted_input_) {
124 std::string entry_path = path_ + "/" + std::string(name);
125 return android::base::ReadFileToString(entry_path, content);
126 }
127
128 CHECK(handle_);
129 ZipEntry entry;
130 if (auto find_err = FindEntry(handle_, name, &entry); find_err != 0) {
131 LOG(ERROR) << "failed to find " << name << " in the package: " << ErrorCodeString(find_err);
132 return false;
133 }
134
135 content->resize(entry.uncompressed_length);
136 if (auto extract_err = ExtractToMemory(
137 handle_, &entry, reinterpret_cast<uint8_t*>(&content->at(0)), entry.uncompressed_length);
138 extract_err != 0) {
139 LOG(ERROR) << "failed to read " << name << " from package: " << ErrorCodeString(extract_err);
140 return false;
141 }
142
143 return true;
144}
145
146bool TargetFile::ExtractEntryToTempFile(const std::string_view name,
147 TemporaryFile* temp_file) const {
148 if (extracted_input_) {
149 std::string entry_path = path_ + "/" + std::string(name);
150 return std::filesystem::copy_file(entry_path, temp_file->path,
151 std::filesystem::copy_options::overwrite_existing);
152 }
153
154 CHECK(handle_);
155 ZipEntry entry;
156 if (auto find_err = FindEntry(handle_, name, &entry); find_err != 0) {
157 LOG(ERROR) << "failed to find " << name << " in the package: " << ErrorCodeString(find_err);
158 return false;
159 }
160
161 if (auto status = ExtractEntryToFile(handle_, &entry, temp_file->fd); status != 0) {
162 LOG(ERROR) << "Failed to extract zip entry " << name << " : " << ErrorCodeString(status);
163 return false;
164 }
165 return true;
166}
167
168bool TargetFile::Open() {
169 if (!extracted_input_) {
170 if (auto ret = OpenArchive(path_.c_str(), &handle_); ret != 0) {
171 LOG(ERROR) << "failed to open source target file " << path_ << ": " << ErrorCodeString(ret);
172 return false;
173 }
174 }
175
176 // Parse the misc info.
177 std::string misc_info_content;
178 if (!ReadEntryToString("META/misc_info.txt", &misc_info_content)) {
179 return false;
180 }
181 if (!ParsePropertyFile(misc_info_content, &misc_info_)) {
182 return false;
183 }
184
185 return true;
186}
187
188bool TargetFile::GetBuildProps(std::map<std::string, std::string, std::less<>>* props_map) const {
189 props_map->clear();
190 // Parse the source zip to mock the system props and block devices. We try all the possible
191 // locations for build props.
192 constexpr std::string_view kPropLocations[] = {
193 "SYSTEM/build.prop",
194 "VENDOR/build.prop",
195 "PRODUCT/build.prop",
196 "PRODUCT_SERVICES/build.prop",
197 "SYSTEM/vendor/build.prop",
198 "SYSTEM/product/build.prop",
199 "SYSTEM/product_services/build.prop",
200 "ODM/build.prop", // legacy
201 "ODM/etc/build.prop",
202 "VENDOR/odm/build.prop", // legacy
203 "VENDOR/odm/etc/build.prop",
204 };
205 for (const auto& name : kPropLocations) {
206 std::string build_prop_content;
207 if (!ReadEntryToString(name, &build_prop_content)) {
208 continue;
209 }
210 std::map<std::string, std::string, std::less<>> props;
211 if (!ParsePropertyFile(build_prop_content, &props)) {
212 LOG(ERROR) << "Failed to parse build prop in " << name;
213 return false;
214 }
215 for (const auto& [key, value] : props) {
216 if (auto it = props_map->find(key); it != props_map->end() && it->second != value) {
217 LOG(WARNING) << "Property " << key << " has different values in property files, we got "
218 << it->second << " and " << value;
219 }
220 props_map->emplace(key, value);
221 }
222 }
223
224 return true;
225}
226
227bool TargetFile::ExtractImage(const std::string_view entry_name, const FstabInfo& fstab_info,
228 const std::string_view work_dir, TemporaryFile* image_file) const {
229 if (!EntryExists(entry_name)) {
230 return false;
231 }
232
233 // We don't need extra work for 'emmc'; use the image file as the block device.
234 if (fstab_info.fs_type == "emmc" || misc_info_.find("extfs_sparse_flag") == misc_info_.end()) {
235 if (!ExtractEntryToTempFile(entry_name, image_file)) {
236 return false;
237 }
238 } else { // treated as ext4 sparse image
239 TemporaryFile sparse_image{ std::string(work_dir) };
240 if (!ExtractEntryToTempFile(entry_name, &sparse_image)) {
241 return false;
242 }
243
244 // Convert the sparse image to raw.
245 if (!SimgToImg(sparse_image.fd, image_file->fd)) {
246 LOG(ERROR) << "Failed to convert " << fstab_info.mount_point << " to raw.";
247 return false;
248 }
249 }
250
251 return true;
252}
253
254bool TargetFile::ParseFstabInfo(std::vector<FstabInfo>* fstab_info_list) const {
255 // Parse the fstab file and extract the image files. The location of the fstab actually depends
256 // on some flags e.g. "no_recovery", "recovery_as_boot". Here we just try all possibilities.
257 constexpr std::string_view kRecoveryFstabLocations[] = {
258 "RECOVERY/RAMDISK/system/etc/recovery.fstab",
259 "RECOVERY/RAMDISK/etc/recovery.fstab",
260 "BOOT/RAMDISK/system/etc/recovery.fstab",
261 "BOOT/RAMDISK/etc/recovery.fstab",
262 };
263 std::string fstab_content;
264 for (const auto& name : kRecoveryFstabLocations) {
265 if (std::string content; ReadEntryToString(name, &content)) {
266 fstab_content = std::move(content);
267 break;
268 }
269 }
270 if (fstab_content.empty()) {
271 LOG(ERROR) << "Failed to parse the recovery fstab file";
272 return false;
273 }
274
275 // Extract the images and convert them to raw.
276 if (!ParseFstab(fstab_content, fstab_info_list)) {
277 LOG(ERROR) << "Failed to mount the block devices for source build.";
278 return false;
279 }
280
281 return true;
Tianjie Xuc1a5e262019-05-22 14:34:12 -0700282}