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