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