Tianjie Xu | 74b0f7c | 2019-05-22 13:59:57 -0700 | [diff] [blame] | 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/build_info.h" |
| 18 | |
Tianjie Xu | 60b242c | 2019-07-30 16:48:52 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | |
Tianjie Xu | 74b0f7c | 2019-05-22 13:59:57 -0700 | [diff] [blame] | 21 | #include <set> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include <android-base/logging.h> |
| 25 | #include <android-base/stringprintf.h> |
| 26 | #include <android-base/strings.h> |
| 27 | |
| 28 | #include "updater/target_files.h" |
| 29 | |
| 30 | bool BuildInfo::ParseTargetFile(const std::string_view target_file_path, bool extracted_input) { |
| 31 | TargetFile target_file(std::string(target_file_path), extracted_input); |
| 32 | if (!target_file.Open()) { |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | if (!target_file.GetBuildProps(&build_props_)) { |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | std::vector<FstabInfo> fstab_info_list; |
| 41 | if (!target_file.ParseFstabInfo(&fstab_info_list)) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | for (const auto& fstab_info : fstab_info_list) { |
| 46 | for (const auto& directory : { "IMAGES", "RADIO" }) { |
| 47 | std::string entry_name = directory + fstab_info.mount_point + ".img"; |
| 48 | if (!target_file.EntryExists(entry_name)) { |
| 49 | LOG(WARNING) << "Failed to find the image entry in the target file: " << entry_name; |
| 50 | continue; |
| 51 | } |
| 52 | |
| 53 | temp_files_.emplace_back(work_dir_); |
| 54 | auto& image_file = temp_files_.back(); |
| 55 | if (!target_file.ExtractImage(entry_name, fstab_info, work_dir_, &image_file)) { |
| 56 | LOG(ERROR) << "Failed to set up source image files."; |
| 57 | return false; |
| 58 | } |
| 59 | |
Tianjie Xu | 60b242c | 2019-07-30 16:48:52 -0700 | [diff] [blame] | 60 | std::string mapped_path = image_file.path; |
| 61 | // Rename the images to more readable ones if we want to keep the image. |
| 62 | if (keep_images_) { |
| 63 | mapped_path = work_dir_ + fstab_info.mount_point + ".img"; |
| 64 | image_file.release(); |
| 65 | if (rename(image_file.path, mapped_path.c_str()) != 0) { |
| 66 | PLOG(ERROR) << "Failed to rename " << image_file.path << " to " << mapped_path; |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | |
Tianjie Xu | 74b0f7c | 2019-05-22 13:59:57 -0700 | [diff] [blame] | 71 | LOG(INFO) << "Mounted " << fstab_info.mount_point << "\nMapping: " << fstab_info.blockdev_name |
Tianjie Xu | 60b242c | 2019-07-30 16:48:52 -0700 | [diff] [blame] | 72 | << " to " << mapped_path; |
Tianjie Xu | 74b0f7c | 2019-05-22 13:59:57 -0700 | [diff] [blame] | 73 | |
| 74 | blockdev_map_.emplace( |
| 75 | fstab_info.blockdev_name, |
Tianjie Xu | 60b242c | 2019-07-30 16:48:52 -0700 | [diff] [blame] | 76 | FakeBlockDevice(fstab_info.blockdev_name, fstab_info.mount_point, mapped_path)); |
Tianjie Xu | 74b0f7c | 2019-05-22 13:59:57 -0700 | [diff] [blame] | 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | std::string BuildInfo::GetProperty(const std::string_view key, |
| 85 | const std::string_view default_value) const { |
| 86 | // The logic to parse the ro.product properties should be in line with the generation script. |
| 87 | // More details in common.py BuildInfo.GetBuildProp. |
| 88 | // TODO(xunchang) handle the oem property and the source order defined in |
| 89 | // ro.product.property_source_order |
| 90 | const std::set<std::string, std::less<>> ro_product_props = { |
| 91 | "ro.product.brand", "ro.product.device", "ro.product.manufacturer", "ro.product.model", |
| 92 | "ro.product.name" |
| 93 | }; |
| 94 | const std::vector<std::string> source_order = { |
Justin Yun | 7ba8f18 | 2019-06-28 16:17:26 +0900 | [diff] [blame] | 95 | "product", "odm", "vendor", "system_ext", "system", |
Tianjie Xu | 74b0f7c | 2019-05-22 13:59:57 -0700 | [diff] [blame] | 96 | }; |
| 97 | if (ro_product_props.find(key) != ro_product_props.end()) { |
| 98 | std::string_view key_suffix(key); |
| 99 | CHECK(android::base::ConsumePrefix(&key_suffix, "ro.product")); |
| 100 | for (const auto& source : source_order) { |
| 101 | std::string resolved_key = "ro.product." + source + std::string(key_suffix); |
| 102 | if (auto entry = build_props_.find(resolved_key); entry != build_props_.end()) { |
| 103 | return entry->second; |
| 104 | } |
| 105 | } |
| 106 | LOG(WARNING) << "Failed to find property: " << key; |
| 107 | return std::string(default_value); |
| 108 | } else if (key == "ro.build.fingerprint") { |
| 109 | // clang-format off |
| 110 | return android::base::StringPrintf("%s/%s/%s:%s/%s/%s:%s/%s", |
| 111 | GetProperty("ro.product.brand", "").c_str(), |
| 112 | GetProperty("ro.product.name", "").c_str(), |
| 113 | GetProperty("ro.product.device", "").c_str(), |
| 114 | GetProperty("ro.build.version.release", "").c_str(), |
| 115 | GetProperty("ro.build.id", "").c_str(), |
| 116 | GetProperty("ro.build.version.incremental", "").c_str(), |
| 117 | GetProperty("ro.build.type", "").c_str(), |
| 118 | GetProperty("ro.build.tags", "").c_str()); |
| 119 | // clang-format on |
| 120 | } |
| 121 | |
| 122 | auto entry = build_props_.find(key); |
| 123 | if (entry == build_props_.end()) { |
| 124 | LOG(WARNING) << "Failed to find property: " << key; |
| 125 | return std::string(default_value); |
| 126 | } |
| 127 | |
| 128 | return entry->second; |
| 129 | } |
| 130 | |
| 131 | std::string BuildInfo::FindBlockDeviceName(const std::string_view name) const { |
| 132 | auto entry = blockdev_map_.find(name); |
| 133 | if (entry == blockdev_map_.end()) { |
| 134 | LOG(WARNING) << "Failed to find path to block device " << name; |
| 135 | return ""; |
| 136 | } |
| 137 | |
| 138 | return entry->second.mounted_file_path; |
| 139 | } |