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