1 /*
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 
19 #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 
SimgToImg(int input_fd,int output_fd)29 static 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;
53 }
54 
ParsePropertyFile(const std::string_view prop_content,std::map<std::string,std::string,std::less<>> * props_map)55 static 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 
ParseFstab(const std::string_view fstab,std::vector<FstabInfo> * fstab_info_list)72 static 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 
EntryExists(const std::string_view name) const107 bool 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 
ReadEntryToString(const std::string_view name,std::string * content) const122 bool 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   if (entry.uncompressed_length == 0) {
136     content->clear();
137     return true;
138   }
139 
140   content->resize(entry.uncompressed_length);
141   if (auto extract_err = ExtractToMemory(
142           handle_, &entry, reinterpret_cast<uint8_t*>(&content->at(0)), entry.uncompressed_length);
143       extract_err != 0) {
144     LOG(ERROR) << "failed to read " << name << " from package: " << ErrorCodeString(extract_err);
145     return false;
146   }
147 
148   return true;
149 }
150 
ExtractEntryToTempFile(const std::string_view name,TemporaryFile * temp_file) const151 bool TargetFile::ExtractEntryToTempFile(const std::string_view name,
152                                         TemporaryFile* temp_file) const {
153   if (extracted_input_) {
154     std::string entry_path = path_ + "/" + std::string(name);
155     return std::filesystem::copy_file(entry_path, temp_file->path,
156                                       std::filesystem::copy_options::overwrite_existing);
157   }
158 
159   CHECK(handle_);
160   ZipEntry entry;
161   if (auto find_err = FindEntry(handle_, name, &entry); find_err != 0) {
162     LOG(ERROR) << "failed to find " << name << " in the package: " << ErrorCodeString(find_err);
163     return false;
164   }
165 
166   if (auto status = ExtractEntryToFile(handle_, &entry, temp_file->fd); status != 0) {
167     LOG(ERROR) << "Failed to extract zip entry " << name << " : " << ErrorCodeString(status);
168     return false;
169   }
170   return true;
171 }
172 
Open()173 bool TargetFile::Open() {
174   if (!extracted_input_) {
175     if (auto ret = OpenArchive(path_.c_str(), &handle_); ret != 0) {
176       LOG(ERROR) << "failed to open source target file " << path_ << ": " << ErrorCodeString(ret);
177       return false;
178     }
179   }
180 
181   // Parse the misc info.
182   std::string misc_info_content;
183   if (!ReadEntryToString("META/misc_info.txt", &misc_info_content)) {
184     return false;
185   }
186   if (!ParsePropertyFile(misc_info_content, &misc_info_)) {
187     return false;
188   }
189 
190   return true;
191 }
192 
GetBuildProps(std::map<std::string,std::string,std::less<>> * props_map) const193 bool TargetFile::GetBuildProps(std::map<std::string, std::string, std::less<>>* props_map) const {
194   props_map->clear();
195   // Parse the source zip to mock the system props and block devices. We try all the possible
196   // locations for build props.
197   constexpr std::string_view kPropLocations[] = {
198     "SYSTEM/build.prop",
199     "VENDOR/build.prop",
200     "PRODUCT/build.prop",
201     "SYSTEM_EXT/build.prop",
202     "SYSTEM/vendor/build.prop",
203     "SYSTEM/product/build.prop",
204     "SYSTEM/system_ext/build.prop",
205     "ODM/build.prop",  // legacy
206     "ODM/etc/build.prop",
207     "VENDOR/odm/build.prop",  // legacy
208     "VENDOR/odm/etc/build.prop",
209   };
210   for (const auto& name : kPropLocations) {
211     std::string build_prop_content;
212     if (!ReadEntryToString(name, &build_prop_content)) {
213       continue;
214     }
215     std::map<std::string, std::string, std::less<>> props;
216     if (!ParsePropertyFile(build_prop_content, &props)) {
217       LOG(ERROR) << "Failed to parse build prop in " << name;
218       return false;
219     }
220     for (const auto& [key, value] : props) {
221       if (auto it = props_map->find(key); it != props_map->end() && it->second != value) {
222         LOG(WARNING) << "Property " << key << " has different values in property files, we got "
223                      << it->second << " and " << value;
224       }
225       props_map->emplace(key, value);
226     }
227   }
228 
229   return true;
230 }
231 
ExtractImage(const std::string_view entry_name,const FstabInfo & fstab_info,const std::string_view work_dir,TemporaryFile * image_file) const232 bool TargetFile::ExtractImage(const std::string_view entry_name, const FstabInfo& fstab_info,
233                               const std::string_view work_dir, TemporaryFile* image_file) const {
234   if (!EntryExists(entry_name)) {
235     return false;
236   }
237 
238   // We don't need extra work for 'emmc'; use the image file as the block device.
239   if (fstab_info.fs_type == "emmc" || misc_info_.find("extfs_sparse_flag") == misc_info_.end()) {
240     if (!ExtractEntryToTempFile(entry_name, image_file)) {
241       return false;
242     }
243   } else {  // treated as ext4 sparse image
244     TemporaryFile sparse_image{ std::string(work_dir) };
245     if (!ExtractEntryToTempFile(entry_name, &sparse_image)) {
246       return false;
247     }
248 
249     // Convert the sparse image to raw.
250     if (!SimgToImg(sparse_image.fd, image_file->fd)) {
251       LOG(ERROR) << "Failed to convert " << fstab_info.mount_point << " to raw.";
252       return false;
253     }
254   }
255 
256   return true;
257 }
258 
ParseFstabInfo(std::vector<FstabInfo> * fstab_info_list) const259 bool TargetFile::ParseFstabInfo(std::vector<FstabInfo>* fstab_info_list) const {
260   // Parse the fstab file and extract the image files. The location of the fstab actually depends
261   // on some flags e.g. "no_recovery", "recovery_as_boot". Here we just try all possibilities.
262   constexpr std::string_view kRecoveryFstabLocations[] = {
263     "RECOVERY/RAMDISK/system/etc/recovery.fstab",
264     "RECOVERY/RAMDISK/etc/recovery.fstab",
265     "BOOT/RAMDISK/system/etc/recovery.fstab",
266     "BOOT/RAMDISK/etc/recovery.fstab",
267   };
268   std::string fstab_content;
269   for (const auto& name : kRecoveryFstabLocations) {
270     if (std::string content; ReadEntryToString(name, &content)) {
271       fstab_content = std::move(content);
272       break;
273     }
274   }
275   if (fstab_content.empty()) {
276     LOG(ERROR) << "Failed to parse the recovery fstab file";
277     return false;
278   }
279 
280   // Extract the images and convert them to raw.
281   if (!ParseFstab(fstab_content, fstab_info_list)) {
282     LOG(ERROR) << "Failed to mount the block devices for source build.";
283     return false;
284   }
285 
286   return true;
287 }
288