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