Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 1 | /* |
| 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 "VintfObject.h" |
| 18 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 19 | #include "CompatibilityMatrix.h" |
| 20 | #include "parse_xml.h" |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 21 | #include "utils.h" |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 22 | |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 23 | #include <dirent.h> |
| 24 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 25 | #include <functional> |
| 26 | #include <memory> |
| 27 | #include <mutex> |
| 28 | |
Bowgo Tsai | 39adc14 | 2017-11-03 12:46:26 +0800 | [diff] [blame] | 29 | #ifdef LIBVINTF_TARGET |
| 30 | #include <android-base/properties.h> |
| 31 | #endif |
| 32 | |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 33 | #include <android-base/logging.h> |
| 34 | |
| 35 | using std::placeholders::_1; |
| 36 | using std::placeholders::_2; |
| 37 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 38 | namespace android { |
| 39 | namespace vintf { |
| 40 | |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame^] | 41 | using namespace details; |
| 42 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 43 | template <typename T> |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 44 | struct LockedSharedPtr { |
| 45 | std::shared_ptr<T> object; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 46 | std::mutex mutex; |
Yifan Hong | 7219ba1 | 2018-01-08 16:23:49 -0800 | [diff] [blame] | 47 | bool fetchedOnce = false; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 48 | }; |
| 49 | |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 50 | struct LockedRuntimeInfoCache { |
| 51 | std::shared_ptr<RuntimeInfo> object; |
| 52 | std::mutex mutex; |
| 53 | RuntimeInfo::FetchFlags fetchedFlags = RuntimeInfo::FetchFlag::NONE; |
| 54 | }; |
| 55 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 56 | template <typename T, typename F> |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 57 | static std::shared_ptr<const T> Get( |
| 58 | LockedSharedPtr<T> *ptr, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 59 | bool skipCache, |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 60 | const F &fetchAllInformation) { |
| 61 | std::unique_lock<std::mutex> _lock(ptr->mutex); |
Yifan Hong | 7219ba1 | 2018-01-08 16:23:49 -0800 | [diff] [blame] | 62 | if (skipCache || !ptr->fetchedOnce) { |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 63 | ptr->object = std::make_unique<T>(); |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 64 | std::string error; |
| 65 | if (fetchAllInformation(ptr->object.get(), &error) != OK) { |
| 66 | LOG(WARNING) << error; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 67 | ptr->object = nullptr; // frees the old object |
| 68 | } |
Yifan Hong | 7219ba1 | 2018-01-08 16:23:49 -0800 | [diff] [blame] | 69 | ptr->fetchedOnce = true; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 70 | } |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 71 | return ptr->object; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // static |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 75 | std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest(bool skipCache) { |
Steven Moreland | 648a001 | 2017-10-19 21:23:41 -0700 | [diff] [blame] | 76 | static LockedSharedPtr<HalManifest> gVendorManifest; |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 77 | return Get(&gVendorManifest, skipCache, &VintfObject::FetchDeviceHalManifest); |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // static |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 81 | std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest(bool skipCache) { |
| 82 | static LockedSharedPtr<HalManifest> gFrameworkManifest; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 83 | return Get(&gFrameworkManifest, skipCache, |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame^] | 84 | std::bind(&HalManifest::fetchAllInformation, _1, kSystemManifest, _2)); |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 87 | |
| 88 | // static |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 89 | std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) { |
| 90 | static LockedSharedPtr<CompatibilityMatrix> gDeviceMatrix; |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 91 | return Get(&gDeviceMatrix, skipCache, |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame^] | 92 | std::bind(&CompatibilityMatrix::fetchAllInformation, _1, kVendorLegacyMatrix, _2)); |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // static |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 96 | std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) { |
| 97 | static LockedSharedPtr<CompatibilityMatrix> gFrameworkMatrix; |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 98 | static LockedSharedPtr<CompatibilityMatrix> gCombinedFrameworkMatrix; |
| 99 | static std::mutex gFrameworkCompatibilityMatrixMutex; |
| 100 | |
| 101 | // To avoid deadlock, get device manifest before any locks. |
| 102 | auto deviceManifest = GetDeviceHalManifest(); |
| 103 | |
| 104 | std::unique_lock<std::mutex> _lock(gFrameworkCompatibilityMatrixMutex); |
| 105 | |
| 106 | auto combined = |
| 107 | Get(&gCombinedFrameworkMatrix, skipCache, |
| 108 | std::bind(&VintfObject::GetCombinedFrameworkMatrix, deviceManifest, _1, _2)); |
| 109 | if (combined != nullptr) { |
| 110 | return combined; |
| 111 | } |
| 112 | |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 113 | return Get(&gFrameworkMatrix, skipCache, |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame^] | 114 | std::bind(&CompatibilityMatrix::fetchAllInformation, _1, kSystemLegacyMatrix, _2)); |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 117 | status_t VintfObject::GetCombinedFrameworkMatrix( |
| 118 | const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out, |
| 119 | std::string* error) { |
| 120 | auto matrixFragments = GetAllFrameworkMatrixLevels(error); |
| 121 | if (matrixFragments.empty()) { |
| 122 | return NAME_NOT_FOUND; |
| 123 | } |
| 124 | |
| 125 | Level deviceLevel = Level::UNSPECIFIED; |
| 126 | |
| 127 | if (deviceManifest != nullptr) { |
| 128 | deviceLevel = deviceManifest->level(); |
| 129 | } |
| 130 | |
| 131 | // TODO(b/70628538): Do not infer from Shipping API level. |
| 132 | #ifdef LIBVINTF_TARGET |
| 133 | if (deviceLevel == Level::UNSPECIFIED) { |
| 134 | auto shippingApi = |
| 135 | android::base::GetUintProperty<uint64_t>("ro.product.first_api_level", 0u); |
| 136 | if (shippingApi != 0u) { |
| 137 | deviceLevel = details::convertFromApiLevel(shippingApi); |
| 138 | } |
| 139 | } |
| 140 | #endif |
| 141 | |
| 142 | if (deviceLevel == Level::UNSPECIFIED) { |
| 143 | // Cannot infer FCM version. Combine all matrices by assuming |
| 144 | // Shipping FCM Version == min(all supported FCM Versions in the framework) |
| 145 | for (auto&& pair : matrixFragments) { |
| 146 | Level fragmentLevel = pair.object.level(); |
| 147 | if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) { |
| 148 | deviceLevel = fragmentLevel; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if (deviceLevel == Level::UNSPECIFIED) { |
| 154 | // None of the fragments specify any FCM version. Should never happen except |
| 155 | // for inconsistent builds. |
| 156 | if (error) { |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame^] | 157 | *error = "No framework compatibility matrix files under " + kSystemVintfDir + |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 158 | " declare FCM version."; |
| 159 | } |
| 160 | return NAME_NOT_FOUND; |
| 161 | } |
| 162 | |
| 163 | CompatibilityMatrix* combined = |
| 164 | CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error); |
| 165 | if (combined == nullptr) { |
| 166 | return BAD_VALUE; |
| 167 | } |
| 168 | *out = std::move(*combined); |
| 169 | return OK; |
| 170 | } |
| 171 | |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 172 | // Priority for loading vendor manifest: |
| 173 | // 1. If {sku} sysprop is set and both files exist, |
| 174 | // /vendor/etc/manifest.xml + /odm/etc/manifest_{sku}.xml |
| 175 | // 2. If both files exist, |
| 176 | // /vendor/etc/manifest.xml + /odm/etc/manifest.xml |
| 177 | // 3. If file exists, /vendor/etc/manifest.xml |
| 178 | // 4. If {sku} sysprop is set and file exists, |
| 179 | // /odm/etc/manifest_{sku}.xml |
| 180 | // 5. If file exists, /odm/etc/manifest.xml |
| 181 | // 6. If file exists, /vendor/manifest.xml |
| 182 | // where: |
| 183 | // {sku} is the value of ro.boot.product.hardware.sku |
| 184 | // A + B means adding <hal> tags from B to A (so that <hal>s from B can override A) |
| 185 | status_t VintfObject::FetchDeviceHalManifest(HalManifest* out, std::string* error) { |
| 186 | // fetchAllInformation returns NAME_NOT_FOUND if file is missing. |
| 187 | HalManifest vendorManifest; |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame^] | 188 | status_t vendorStatus = vendorManifest.fetchAllInformation(kVendorManifest, error); |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 189 | if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) { |
| 190 | return vendorStatus; |
| 191 | } |
| 192 | |
| 193 | HalManifest odmManifest; |
| 194 | status_t odmStatus = NAME_NOT_FOUND; |
| 195 | |
| 196 | #ifdef LIBVINTF_TARGET |
| 197 | std::string productModel = android::base::GetProperty("ro.boot.product.hardware.sku", ""); |
| 198 | if (!productModel.empty()) { |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame^] | 199 | odmStatus = odmManifest.fetchAllInformation( |
| 200 | kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", error); |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 201 | if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) { |
| 202 | return odmStatus; |
| 203 | } |
| 204 | } |
| 205 | #endif |
| 206 | |
| 207 | if (odmStatus == NAME_NOT_FOUND) { |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame^] | 208 | odmStatus = odmManifest.fetchAllInformation(kOdmLegacyManifest, error); |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 209 | if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) { |
| 210 | return odmStatus; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // Both files exist. Use vendor manifest as base manifest and let ODM manifest override it. |
| 215 | if (vendorStatus == OK && odmStatus == OK) { |
| 216 | *out = std::move(vendorManifest); |
| 217 | out->addAllHals(&odmManifest); |
| 218 | return OK; |
| 219 | } |
| 220 | |
| 221 | // Only vendor manifest exists. Use it. |
| 222 | if (vendorStatus == OK) { |
| 223 | *out = std::move(vendorManifest); |
| 224 | return OK; |
| 225 | } |
| 226 | |
| 227 | // Only ODM manifest exists. use it. |
| 228 | if (odmStatus == OK) { |
| 229 | *out = std::move(odmManifest); |
| 230 | return OK; |
| 231 | } |
| 232 | |
| 233 | // Use legacy /vendor/manifest.xml |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame^] | 234 | return out->fetchAllInformation(kVendorLegacyManifest, error); |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 237 | std::vector<Named<CompatibilityMatrix>> VintfObject::GetAllFrameworkMatrixLevels( |
| 238 | std::string* error) { |
| 239 | std::vector<std::string> fileNames; |
| 240 | std::vector<Named<CompatibilityMatrix>> results; |
| 241 | |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame^] | 242 | if (details::gFetcher->listFiles(kSystemVintfDir, &fileNames, error) != OK) { |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 243 | return {}; |
| 244 | } |
| 245 | for (const std::string& fileName : fileNames) { |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame^] | 246 | std::string path = kSystemVintfDir + fileName; |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 247 | |
| 248 | std::string content; |
| 249 | std::string fetchError; |
| 250 | status_t status = details::gFetcher->fetch(path, content, &fetchError); |
| 251 | if (status != OK) { |
| 252 | if (error) { |
| 253 | *error += "Ignore file " + path + ": " + fetchError + "\n"; |
| 254 | } |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | auto it = results.emplace(results.end()); |
| 259 | if (!gCompatibilityMatrixConverter(&it->object, content)) { |
| 260 | if (error) { |
| 261 | // TODO(b/71874788): do not use lastError() because it is not thread-safe. |
| 262 | *error += |
| 263 | "Ignore file " + path + ": " + gCompatibilityMatrixConverter.lastError() + "\n"; |
| 264 | } |
| 265 | results.erase(it); |
| 266 | continue; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | if (results.empty()) { |
| 271 | if (error) { |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame^] | 272 | *error = "No framework matrices under " + kSystemVintfDir + |
| 273 | " can be fetched or parsed.\n" + *error; |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 274 | } |
| 275 | } else { |
| 276 | if (error && !error->empty()) { |
| 277 | LOG(WARNING) << *error; |
| 278 | *error = ""; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return results; |
| 283 | } |
| 284 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 285 | // static |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 286 | std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache, |
| 287 | RuntimeInfo::FetchFlags flags) { |
| 288 | static LockedRuntimeInfoCache gDeviceRuntimeInfo; |
| 289 | std::unique_lock<std::mutex> _lock(gDeviceRuntimeInfo.mutex); |
| 290 | |
| 291 | if (!skipCache) { |
| 292 | flags &= (~gDeviceRuntimeInfo.fetchedFlags); |
| 293 | } |
| 294 | |
| 295 | if (gDeviceRuntimeInfo.object == nullptr) { |
Yifan Hong | 29bb2d4 | 2017-09-27 13:28:00 -0700 | [diff] [blame] | 296 | gDeviceRuntimeInfo.object = details::gRuntimeInfoFactory->make_shared(); |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | status_t status = gDeviceRuntimeInfo.object->fetchAllInformation(flags); |
| 300 | if (status != OK) { |
| 301 | gDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched" |
| 302 | return nullptr; |
| 303 | } |
| 304 | |
| 305 | gDeviceRuntimeInfo.fetchedFlags |= flags; |
| 306 | return gDeviceRuntimeInfo.object; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 309 | namespace details { |
| 310 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 311 | enum class ParseStatus { |
| 312 | OK, |
| 313 | PARSE_ERROR, |
| 314 | DUPLICATED_FWK_ENTRY, |
| 315 | DUPLICATED_DEV_ENTRY, |
| 316 | }; |
| 317 | |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 318 | static std::string toString(ParseStatus status) { |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 319 | switch(status) { |
| 320 | case ParseStatus::OK: return "OK"; |
| 321 | case ParseStatus::PARSE_ERROR: return "parse error"; |
| 322 | case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework"; |
| 323 | case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device"; |
| 324 | } |
| 325 | return ""; |
| 326 | } |
| 327 | |
| 328 | template<typename T> |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 329 | static ParseStatus tryParse(const std::string &xml, const XmlConverter<T> &parse, |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 330 | std::shared_ptr<T> *fwk, std::shared_ptr<T> *dev) { |
| 331 | std::shared_ptr<T> ret = std::make_shared<T>(); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 332 | if (!parse(ret.get(), xml)) { |
| 333 | return ParseStatus::PARSE_ERROR; |
| 334 | } |
| 335 | if (ret->type() == SchemaType::FRAMEWORK) { |
| 336 | if (fwk->get() != nullptr) { |
| 337 | return ParseStatus::DUPLICATED_FWK_ENTRY; |
| 338 | } |
| 339 | *fwk = std::move(ret); |
| 340 | } else if (ret->type() == SchemaType::DEVICE) { |
| 341 | if (dev->get() != nullptr) { |
| 342 | return ParseStatus::DUPLICATED_DEV_ENTRY; |
| 343 | } |
| 344 | *dev = std::move(ret); |
| 345 | } |
| 346 | return ParseStatus::OK; |
| 347 | } |
| 348 | |
| 349 | template<typename T, typename GetFunction> |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 350 | static status_t getMissing(const std::shared_ptr<T>& pkg, bool mount, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 351 | std::function<status_t(void)> mountFunction, |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 352 | std::shared_ptr<const T>* updated, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 353 | GetFunction getFunction) { |
| 354 | if (pkg != nullptr) { |
| 355 | *updated = pkg; |
| 356 | } else { |
| 357 | if (mount) { |
| 358 | (void)mountFunction(); // ignore mount errors |
| 359 | } |
| 360 | *updated = getFunction(); |
| 361 | } |
| 362 | return OK; |
| 363 | } |
| 364 | |
| 365 | #define ADD_MESSAGE(__error__) \ |
| 366 | if (error != nullptr) { \ |
| 367 | *error += (__error__); \ |
| 368 | } \ |
| 369 | |
| 370 | struct PackageInfo { |
| 371 | struct Pair { |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 372 | std::shared_ptr<HalManifest> manifest; |
| 373 | std::shared_ptr<CompatibilityMatrix> matrix; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 374 | }; |
| 375 | Pair dev; |
| 376 | Pair fwk; |
| 377 | }; |
| 378 | |
| 379 | struct UpdatedInfo { |
| 380 | struct Pair { |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 381 | std::shared_ptr<const HalManifest> manifest; |
| 382 | std::shared_ptr<const CompatibilityMatrix> matrix; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 383 | }; |
| 384 | Pair dev; |
| 385 | Pair fwk; |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 386 | std::shared_ptr<const RuntimeInfo> runtimeInfo; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 387 | }; |
| 388 | |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 389 | // Checks given compatibility info against info on the device. If no |
| 390 | // compatability info is given then the device info will be checked against |
| 391 | // itself. |
Yifan Hong | db6423e | 2017-09-11 14:38:46 -0700 | [diff] [blame] | 392 | int32_t checkCompatibility(const std::vector<std::string>& xmls, bool mount, |
| 393 | const PartitionMounter& mounter, std::string* error, |
| 394 | DisabledChecks disabledChecks) { |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 395 | status_t status; |
| 396 | ParseStatus parseStatus; |
| 397 | PackageInfo pkg; // All information from package. |
| 398 | UpdatedInfo updated; // All files and runtime info after the update. |
| 399 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 400 | // parse all information from package |
| 401 | for (const auto &xml : xmls) { |
| 402 | parseStatus = tryParse(xml, gHalManifestConverter, &pkg.fwk.manifest, &pkg.dev.manifest); |
| 403 | if (parseStatus == ParseStatus::OK) { |
| 404 | continue; // work on next one |
| 405 | } |
| 406 | if (parseStatus != ParseStatus::PARSE_ERROR) { |
| 407 | ADD_MESSAGE(toString(parseStatus) + " manifest"); |
| 408 | return ALREADY_EXISTS; |
| 409 | } |
| 410 | parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &pkg.fwk.matrix, &pkg.dev.matrix); |
| 411 | if (parseStatus == ParseStatus::OK) { |
| 412 | continue; // work on next one |
| 413 | } |
| 414 | if (parseStatus != ParseStatus::PARSE_ERROR) { |
| 415 | ADD_MESSAGE(toString(parseStatus) + " matrix"); |
| 416 | return ALREADY_EXISTS; |
| 417 | } |
| 418 | ADD_MESSAGE(toString(parseStatus)); // parse error |
| 419 | return BAD_VALUE; |
| 420 | } |
| 421 | |
| 422 | // get missing info from device |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 423 | // use functions instead of std::bind because std::bind doesn't work well with mock objects |
| 424 | auto mountSystem = [&mounter] { return mounter.mountSystem(); }; |
| 425 | auto mountVendor = [&mounter] { return mounter.mountVendor(); }; |
| 426 | if ((status = getMissing( |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 427 | pkg.fwk.manifest, mount, mountSystem, &updated.fwk.manifest, |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 428 | std::bind(VintfObject::GetFrameworkHalManifest, true /* skipCache */))) != OK) { |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 429 | return status; |
| 430 | } |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 431 | if ((status = getMissing( |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 432 | pkg.dev.manifest, mount, mountVendor, &updated.dev.manifest, |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 433 | std::bind(VintfObject::GetDeviceHalManifest, true /* skipCache */))) != OK) { |
| 434 | return status; |
| 435 | } |
| 436 | if ((status = getMissing( |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 437 | pkg.fwk.matrix, mount, mountSystem, &updated.fwk.matrix, |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 438 | std::bind(VintfObject::GetFrameworkCompatibilityMatrix, true /* skipCache */))) != |
| 439 | OK) { |
| 440 | return status; |
| 441 | } |
| 442 | if ((status = getMissing( |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 443 | pkg.dev.matrix, mount, mountVendor, &updated.dev.matrix, |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 444 | std::bind(VintfObject::GetDeviceCompatibilityMatrix, true /* skipCache */))) != OK) { |
| 445 | return status; |
| 446 | } |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 447 | |
| 448 | if (mount) { |
| 449 | (void)mounter.umountSystem(); // ignore errors |
| 450 | (void)mounter.umountVendor(); // ignore errors |
| 451 | } |
| 452 | |
| 453 | updated.runtimeInfo = VintfObject::GetRuntimeInfo(true /* skipCache */); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 454 | |
| 455 | // null checks for files and runtime info after the update |
| 456 | // TODO(b/37321309) if a compat mat is missing, it is not matched and considered compatible. |
| 457 | if (updated.fwk.manifest == nullptr) { |
| 458 | ADD_MESSAGE("No framework manifest file from device or from update package"); |
| 459 | return NO_INIT; |
| 460 | } |
| 461 | if (updated.dev.manifest == nullptr) { |
| 462 | ADD_MESSAGE("No device manifest file from device or from update package"); |
| 463 | return NO_INIT; |
| 464 | } |
| 465 | if (updated.fwk.matrix == nullptr) { |
| 466 | ADD_MESSAGE("No framework matrix, skipping;"); |
| 467 | // TODO(b/37321309) consider missing matricies as errors. |
| 468 | } |
| 469 | if (updated.dev.matrix == nullptr) { |
| 470 | ADD_MESSAGE("No device matrix, skipping;"); |
| 471 | // TODO(b/37321309) consider missing matricies as errors. |
| 472 | } |
| 473 | if (updated.runtimeInfo == nullptr) { |
| 474 | ADD_MESSAGE("No runtime info from device"); |
| 475 | return NO_INIT; |
| 476 | } |
| 477 | |
| 478 | // compatiblity check. |
| 479 | // TODO(b/37321309) outer if checks can be removed if we consider missing matrices as errors. |
| 480 | if (updated.dev.manifest && updated.fwk.matrix) { |
| 481 | if (!updated.dev.manifest->checkCompatibility(*updated.fwk.matrix, error)) { |
Yifan Hong | 0d4be12 | 2017-05-15 17:04:22 -0700 | [diff] [blame] | 482 | if (error) |
| 483 | error->insert(0, "Device manifest and framework compatibility matrix " |
| 484 | "are incompatible: "); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 485 | return INCOMPATIBLE; |
| 486 | } |
| 487 | } |
| 488 | if (updated.fwk.manifest && updated.dev.matrix) { |
| 489 | if (!updated.fwk.manifest->checkCompatibility(*updated.dev.matrix, error)) { |
Yifan Hong | 0d4be12 | 2017-05-15 17:04:22 -0700 | [diff] [blame] | 490 | if (error) |
| 491 | error->insert(0, "Framework manifest and device compatibility matrix " |
| 492 | "are incompatible: "); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 493 | return INCOMPATIBLE; |
| 494 | } |
| 495 | } |
| 496 | if (updated.runtimeInfo && updated.fwk.matrix) { |
Yifan Hong | db6423e | 2017-09-11 14:38:46 -0700 | [diff] [blame] | 497 | if (!updated.runtimeInfo->checkCompatibility(*updated.fwk.matrix, error, disabledChecks)) { |
Yifan Hong | 0d4be12 | 2017-05-15 17:04:22 -0700 | [diff] [blame] | 498 | if (error) |
| 499 | error->insert(0, "Runtime info and framework compatibility matrix " |
| 500 | "are incompatible: "); |
Yifan Hong | e8b8684 | 2017-05-25 17:59:22 +0000 | [diff] [blame] | 501 | return INCOMPATIBLE; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
| 505 | return COMPATIBLE; |
| 506 | } |
| 507 | |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame^] | 508 | const std::string kSystemVintfDir = "/system/etc/vintf/"; |
| 509 | const std::string kVendorVintfDir = "/vendor/etc/"; |
| 510 | |
| 511 | const std::string kVendorManifest = kVendorVintfDir + "manifest.xml"; |
| 512 | const std::string kSystemManifest = "/system/manifest.xml"; |
| 513 | |
| 514 | const std::string kVendorLegacyManifest = "/vendor/manifest.xml"; |
| 515 | const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml"; |
| 516 | const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml"; |
| 517 | const std::string kOdmLegacyVintfDir = "/odm/etc/"; |
| 518 | const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml"; |
| 519 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 520 | } // namespace details |
| 521 | |
Yifan Hong | fbbf047 | 2017-04-07 18:14:18 -0700 | [diff] [blame] | 522 | // static |
Yifan Hong | db6423e | 2017-09-11 14:38:46 -0700 | [diff] [blame] | 523 | int32_t VintfObject::CheckCompatibility(const std::vector<std::string>& xmls, std::string* error, |
| 524 | DisabledChecks disabledChecks) { |
| 525 | return details::checkCompatibility(xmls, false /* mount */, *details::gPartitionMounter, error, |
| 526 | disabledChecks); |
Yifan Hong | fbbf047 | 2017-04-07 18:14:18 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 529 | |
| 530 | } // namespace vintf |
| 531 | } // namespace android |