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" |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame^] | 20 | #include "parse_string.h" |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 21 | #include "parse_xml.h" |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 22 | #include "utils.h" |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 23 | |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 24 | #include <dirent.h> |
| 25 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 26 | #include <functional> |
| 27 | #include <memory> |
| 28 | #include <mutex> |
| 29 | |
Bowgo Tsai | 39adc14 | 2017-11-03 12:46:26 +0800 | [diff] [blame] | 30 | #ifdef LIBVINTF_TARGET |
| 31 | #include <android-base/properties.h> |
| 32 | #endif |
| 33 | |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 34 | #include <android-base/logging.h> |
| 35 | |
| 36 | using std::placeholders::_1; |
| 37 | using std::placeholders::_2; |
| 38 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 39 | namespace android { |
| 40 | namespace vintf { |
| 41 | |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 42 | using namespace details; |
| 43 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 44 | template <typename T> |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 45 | struct LockedSharedPtr { |
| 46 | std::shared_ptr<T> object; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 47 | std::mutex mutex; |
Yifan Hong | 7219ba1 | 2018-01-08 16:23:49 -0800 | [diff] [blame] | 48 | bool fetchedOnce = false; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 51 | struct LockedRuntimeInfoCache { |
| 52 | std::shared_ptr<RuntimeInfo> object; |
| 53 | std::mutex mutex; |
| 54 | RuntimeInfo::FetchFlags fetchedFlags = RuntimeInfo::FetchFlag::NONE; |
| 55 | }; |
| 56 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 57 | template <typename T, typename F> |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 58 | static std::shared_ptr<const T> Get( |
| 59 | LockedSharedPtr<T> *ptr, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 60 | bool skipCache, |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 61 | const F &fetchAllInformation) { |
| 62 | std::unique_lock<std::mutex> _lock(ptr->mutex); |
Yifan Hong | 7219ba1 | 2018-01-08 16:23:49 -0800 | [diff] [blame] | 63 | if (skipCache || !ptr->fetchedOnce) { |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 64 | ptr->object = std::make_unique<T>(); |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 65 | std::string error; |
| 66 | if (fetchAllInformation(ptr->object.get(), &error) != OK) { |
| 67 | LOG(WARNING) << error; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 68 | ptr->object = nullptr; // frees the old object |
| 69 | } |
Yifan Hong | 7219ba1 | 2018-01-08 16:23:49 -0800 | [diff] [blame] | 70 | ptr->fetchedOnce = true; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 71 | } |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 72 | return ptr->object; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // static |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 76 | std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest(bool skipCache) { |
Steven Moreland | 648a001 | 2017-10-19 21:23:41 -0700 | [diff] [blame] | 77 | static LockedSharedPtr<HalManifest> gVendorManifest; |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 78 | return Get(&gVendorManifest, skipCache, &VintfObject::FetchDeviceHalManifest); |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // static |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 82 | std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest(bool skipCache) { |
| 83 | static LockedSharedPtr<HalManifest> gFrameworkManifest; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 84 | return Get(&gFrameworkManifest, skipCache, |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 85 | std::bind(&HalManifest::fetchAllInformation, _1, kSystemManifest, _2)); |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 88 | |
| 89 | // static |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 90 | std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) { |
| 91 | static LockedSharedPtr<CompatibilityMatrix> gDeviceMatrix; |
Yifan Hong | b64ec9e | 2018-01-18 18:57:52 -0800 | [diff] [blame] | 92 | return Get(&gDeviceMatrix, skipCache, &VintfObject::FetchDeviceMatrix); |
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: |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 173 | // 1. /vendor/etc/vintf/manifest.xml + ODM manifest |
| 174 | // 2. /vendor/etc/vintf/manifest.xml |
| 175 | // 3. ODM manifest |
| 176 | // 4. /vendor/manifest.xml |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 177 | // where: |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 178 | // A + B means adding <hal> tags from B to A (so that <hal>s from B can override A) |
| 179 | status_t VintfObject::FetchDeviceHalManifest(HalManifest* out, std::string* error) { |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 180 | status_t vendorStatus = FetchOneHalManifest(kVendorManifest, out, error); |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 181 | if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) { |
| 182 | return vendorStatus; |
| 183 | } |
| 184 | |
| 185 | HalManifest odmManifest; |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 186 | status_t odmStatus = FetchOdmHalManifest(&odmManifest, error); |
| 187 | if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) { |
| 188 | return odmStatus; |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 191 | if (vendorStatus == OK) { |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 192 | if (odmStatus == OK) { |
| 193 | out->addAllHals(&odmManifest); |
| 194 | } |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 195 | return OK; |
| 196 | } |
| 197 | |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 198 | // vendorStatus != OK, "out" is not changed. |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 199 | if (odmStatus == OK) { |
| 200 | *out = std::move(odmManifest); |
| 201 | return OK; |
| 202 | } |
| 203 | |
| 204 | // Use legacy /vendor/manifest.xml |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 205 | return out->fetchAllInformation(kVendorLegacyManifest, error); |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 208 | // "out" is written to iff return status is OK. |
| 209 | // Priority: |
| 210 | // 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml |
| 211 | // 2. /odm/etc/vintf/manifest.xml |
| 212 | // 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml |
| 213 | // 4. /odm/etc/manifest.xml |
| 214 | // where: |
| 215 | // {sku} is the value of ro.boot.product.hardware.sku |
| 216 | status_t VintfObject::FetchOdmHalManifest(HalManifest* out, std::string* error) { |
| 217 | status_t status; |
| 218 | |
| 219 | #ifdef LIBVINTF_TARGET |
| 220 | std::string productModel; |
| 221 | productModel = android::base::GetProperty("ro.boot.product.hardware.sku", ""); |
| 222 | |
| 223 | if (!productModel.empty()) { |
| 224 | status = |
| 225 | FetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error); |
| 226 | if (status == OK || status != NAME_NOT_FOUND) { |
| 227 | return status; |
| 228 | } |
| 229 | } |
| 230 | #endif |
| 231 | |
| 232 | status = FetchOneHalManifest(kOdmManifest, out, error); |
| 233 | if (status == OK || status != NAME_NOT_FOUND) { |
| 234 | return status; |
| 235 | } |
| 236 | |
| 237 | #ifdef LIBVINTF_TARGET |
| 238 | if (!productModel.empty()) { |
| 239 | status = FetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out, |
| 240 | error); |
| 241 | if (status == OK || status != NAME_NOT_FOUND) { |
| 242 | return status; |
| 243 | } |
| 244 | } |
| 245 | #endif |
| 246 | |
| 247 | status = FetchOneHalManifest(kOdmLegacyManifest, out, error); |
| 248 | if (status == OK || status != NAME_NOT_FOUND) { |
| 249 | return status; |
| 250 | } |
| 251 | |
| 252 | return NAME_NOT_FOUND; |
| 253 | } |
| 254 | |
| 255 | // Fetch one manifest.xml file. "out" is written to iff return status is OK. |
| 256 | // Returns NAME_NOT_FOUND if file is missing. |
| 257 | status_t VintfObject::FetchOneHalManifest(const std::string& path, HalManifest* out, |
| 258 | std::string* error) { |
| 259 | HalManifest ret; |
| 260 | status_t status = ret.fetchAllInformation(path, error); |
| 261 | if (status == OK) { |
| 262 | *out = std::move(ret); |
| 263 | } |
| 264 | return status; |
| 265 | } |
| 266 | |
Yifan Hong | b64ec9e | 2018-01-18 18:57:52 -0800 | [diff] [blame] | 267 | status_t VintfObject::FetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) { |
| 268 | CompatibilityMatrix etcMatrix; |
| 269 | if (etcMatrix.fetchAllInformation(kVendorMatrix, error) == OK) { |
| 270 | *out = std::move(etcMatrix); |
| 271 | return OK; |
| 272 | } |
| 273 | return out->fetchAllInformation(kVendorLegacyMatrix, error); |
| 274 | } |
| 275 | |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 276 | std::vector<Named<CompatibilityMatrix>> VintfObject::GetAllFrameworkMatrixLevels( |
| 277 | std::string* error) { |
| 278 | std::vector<std::string> fileNames; |
| 279 | std::vector<Named<CompatibilityMatrix>> results; |
| 280 | |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 281 | if (details::gFetcher->listFiles(kSystemVintfDir, &fileNames, error) != OK) { |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 282 | return {}; |
| 283 | } |
| 284 | for (const std::string& fileName : fileNames) { |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 285 | std::string path = kSystemVintfDir + fileName; |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 286 | |
| 287 | std::string content; |
| 288 | std::string fetchError; |
| 289 | status_t status = details::gFetcher->fetch(path, content, &fetchError); |
| 290 | if (status != OK) { |
| 291 | if (error) { |
| 292 | *error += "Ignore file " + path + ": " + fetchError + "\n"; |
| 293 | } |
| 294 | continue; |
| 295 | } |
| 296 | |
| 297 | auto it = results.emplace(results.end()); |
| 298 | if (!gCompatibilityMatrixConverter(&it->object, content)) { |
| 299 | if (error) { |
| 300 | // TODO(b/71874788): do not use lastError() because it is not thread-safe. |
| 301 | *error += |
| 302 | "Ignore file " + path + ": " + gCompatibilityMatrixConverter.lastError() + "\n"; |
| 303 | } |
| 304 | results.erase(it); |
| 305 | continue; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | if (results.empty()) { |
| 310 | if (error) { |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 311 | *error = "No framework matrices under " + kSystemVintfDir + |
| 312 | " can be fetched or parsed.\n" + *error; |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 313 | } |
| 314 | } else { |
| 315 | if (error && !error->empty()) { |
| 316 | LOG(WARNING) << *error; |
| 317 | *error = ""; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return results; |
| 322 | } |
| 323 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 324 | // static |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 325 | std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache, |
| 326 | RuntimeInfo::FetchFlags flags) { |
| 327 | static LockedRuntimeInfoCache gDeviceRuntimeInfo; |
| 328 | std::unique_lock<std::mutex> _lock(gDeviceRuntimeInfo.mutex); |
| 329 | |
| 330 | if (!skipCache) { |
| 331 | flags &= (~gDeviceRuntimeInfo.fetchedFlags); |
| 332 | } |
| 333 | |
| 334 | if (gDeviceRuntimeInfo.object == nullptr) { |
Yifan Hong | 29bb2d4 | 2017-09-27 13:28:00 -0700 | [diff] [blame] | 335 | gDeviceRuntimeInfo.object = details::gRuntimeInfoFactory->make_shared(); |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | status_t status = gDeviceRuntimeInfo.object->fetchAllInformation(flags); |
| 339 | if (status != OK) { |
| 340 | gDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched" |
| 341 | return nullptr; |
| 342 | } |
| 343 | |
| 344 | gDeviceRuntimeInfo.fetchedFlags |= flags; |
| 345 | return gDeviceRuntimeInfo.object; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 348 | namespace details { |
| 349 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 350 | enum class ParseStatus { |
| 351 | OK, |
| 352 | PARSE_ERROR, |
| 353 | DUPLICATED_FWK_ENTRY, |
| 354 | DUPLICATED_DEV_ENTRY, |
| 355 | }; |
| 356 | |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 357 | static std::string toString(ParseStatus status) { |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 358 | switch(status) { |
| 359 | case ParseStatus::OK: return "OK"; |
| 360 | case ParseStatus::PARSE_ERROR: return "parse error"; |
| 361 | case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework"; |
| 362 | case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device"; |
| 363 | } |
| 364 | return ""; |
| 365 | } |
| 366 | |
| 367 | template<typename T> |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 368 | static ParseStatus tryParse(const std::string &xml, const XmlConverter<T> &parse, |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 369 | std::shared_ptr<T> *fwk, std::shared_ptr<T> *dev) { |
| 370 | std::shared_ptr<T> ret = std::make_shared<T>(); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 371 | if (!parse(ret.get(), xml)) { |
| 372 | return ParseStatus::PARSE_ERROR; |
| 373 | } |
| 374 | if (ret->type() == SchemaType::FRAMEWORK) { |
| 375 | if (fwk->get() != nullptr) { |
| 376 | return ParseStatus::DUPLICATED_FWK_ENTRY; |
| 377 | } |
| 378 | *fwk = std::move(ret); |
| 379 | } else if (ret->type() == SchemaType::DEVICE) { |
| 380 | if (dev->get() != nullptr) { |
| 381 | return ParseStatus::DUPLICATED_DEV_ENTRY; |
| 382 | } |
| 383 | *dev = std::move(ret); |
| 384 | } |
| 385 | return ParseStatus::OK; |
| 386 | } |
| 387 | |
| 388 | template<typename T, typename GetFunction> |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 389 | static status_t getMissing(const std::shared_ptr<T>& pkg, bool mount, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 390 | std::function<status_t(void)> mountFunction, |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 391 | std::shared_ptr<const T>* updated, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 392 | GetFunction getFunction) { |
| 393 | if (pkg != nullptr) { |
| 394 | *updated = pkg; |
| 395 | } else { |
| 396 | if (mount) { |
| 397 | (void)mountFunction(); // ignore mount errors |
| 398 | } |
| 399 | *updated = getFunction(); |
| 400 | } |
| 401 | return OK; |
| 402 | } |
| 403 | |
| 404 | #define ADD_MESSAGE(__error__) \ |
| 405 | if (error != nullptr) { \ |
| 406 | *error += (__error__); \ |
| 407 | } \ |
| 408 | |
| 409 | struct PackageInfo { |
| 410 | struct Pair { |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 411 | std::shared_ptr<HalManifest> manifest; |
| 412 | std::shared_ptr<CompatibilityMatrix> matrix; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 413 | }; |
| 414 | Pair dev; |
| 415 | Pair fwk; |
| 416 | }; |
| 417 | |
| 418 | struct UpdatedInfo { |
| 419 | struct Pair { |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 420 | std::shared_ptr<const HalManifest> manifest; |
| 421 | std::shared_ptr<const CompatibilityMatrix> matrix; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 422 | }; |
| 423 | Pair dev; |
| 424 | Pair fwk; |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 425 | std::shared_ptr<const RuntimeInfo> runtimeInfo; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 426 | }; |
| 427 | |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 428 | // Checks given compatibility info against info on the device. If no |
| 429 | // compatability info is given then the device info will be checked against |
| 430 | // itself. |
Yifan Hong | db6423e | 2017-09-11 14:38:46 -0700 | [diff] [blame] | 431 | int32_t checkCompatibility(const std::vector<std::string>& xmls, bool mount, |
| 432 | const PartitionMounter& mounter, std::string* error, |
| 433 | DisabledChecks disabledChecks) { |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 434 | status_t status; |
| 435 | ParseStatus parseStatus; |
| 436 | PackageInfo pkg; // All information from package. |
| 437 | UpdatedInfo updated; // All files and runtime info after the update. |
| 438 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 439 | // parse all information from package |
| 440 | for (const auto &xml : xmls) { |
| 441 | parseStatus = tryParse(xml, gHalManifestConverter, &pkg.fwk.manifest, &pkg.dev.manifest); |
| 442 | if (parseStatus == ParseStatus::OK) { |
| 443 | continue; // work on next one |
| 444 | } |
| 445 | if (parseStatus != ParseStatus::PARSE_ERROR) { |
| 446 | ADD_MESSAGE(toString(parseStatus) + " manifest"); |
| 447 | return ALREADY_EXISTS; |
| 448 | } |
| 449 | parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &pkg.fwk.matrix, &pkg.dev.matrix); |
| 450 | if (parseStatus == ParseStatus::OK) { |
| 451 | continue; // work on next one |
| 452 | } |
| 453 | if (parseStatus != ParseStatus::PARSE_ERROR) { |
| 454 | ADD_MESSAGE(toString(parseStatus) + " matrix"); |
| 455 | return ALREADY_EXISTS; |
| 456 | } |
| 457 | ADD_MESSAGE(toString(parseStatus)); // parse error |
| 458 | return BAD_VALUE; |
| 459 | } |
| 460 | |
| 461 | // get missing info from device |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 462 | // use functions instead of std::bind because std::bind doesn't work well with mock objects |
| 463 | auto mountSystem = [&mounter] { return mounter.mountSystem(); }; |
| 464 | auto mountVendor = [&mounter] { return mounter.mountVendor(); }; |
| 465 | if ((status = getMissing( |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 466 | pkg.fwk.manifest, mount, mountSystem, &updated.fwk.manifest, |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 467 | std::bind(VintfObject::GetFrameworkHalManifest, true /* skipCache */))) != OK) { |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 468 | return status; |
| 469 | } |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 470 | if ((status = getMissing( |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 471 | pkg.dev.manifest, mount, mountVendor, &updated.dev.manifest, |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 472 | std::bind(VintfObject::GetDeviceHalManifest, true /* skipCache */))) != OK) { |
| 473 | return status; |
| 474 | } |
| 475 | if ((status = getMissing( |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 476 | pkg.fwk.matrix, mount, mountSystem, &updated.fwk.matrix, |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 477 | std::bind(VintfObject::GetFrameworkCompatibilityMatrix, true /* skipCache */))) != |
| 478 | OK) { |
| 479 | return status; |
| 480 | } |
| 481 | if ((status = getMissing( |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 482 | pkg.dev.matrix, mount, mountVendor, &updated.dev.matrix, |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 483 | std::bind(VintfObject::GetDeviceCompatibilityMatrix, true /* skipCache */))) != OK) { |
| 484 | return status; |
| 485 | } |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 486 | |
| 487 | if (mount) { |
| 488 | (void)mounter.umountSystem(); // ignore errors |
| 489 | (void)mounter.umountVendor(); // ignore errors |
| 490 | } |
| 491 | |
| 492 | updated.runtimeInfo = VintfObject::GetRuntimeInfo(true /* skipCache */); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 493 | |
| 494 | // null checks for files and runtime info after the update |
| 495 | // TODO(b/37321309) if a compat mat is missing, it is not matched and considered compatible. |
| 496 | if (updated.fwk.manifest == nullptr) { |
| 497 | ADD_MESSAGE("No framework manifest file from device or from update package"); |
| 498 | return NO_INIT; |
| 499 | } |
| 500 | if (updated.dev.manifest == nullptr) { |
| 501 | ADD_MESSAGE("No device manifest file from device or from update package"); |
| 502 | return NO_INIT; |
| 503 | } |
| 504 | if (updated.fwk.matrix == nullptr) { |
| 505 | ADD_MESSAGE("No framework matrix, skipping;"); |
| 506 | // TODO(b/37321309) consider missing matricies as errors. |
| 507 | } |
| 508 | if (updated.dev.matrix == nullptr) { |
| 509 | ADD_MESSAGE("No device matrix, skipping;"); |
| 510 | // TODO(b/37321309) consider missing matricies as errors. |
| 511 | } |
| 512 | if (updated.runtimeInfo == nullptr) { |
| 513 | ADD_MESSAGE("No runtime info from device"); |
| 514 | return NO_INIT; |
| 515 | } |
| 516 | |
| 517 | // compatiblity check. |
| 518 | // TODO(b/37321309) outer if checks can be removed if we consider missing matrices as errors. |
| 519 | if (updated.dev.manifest && updated.fwk.matrix) { |
| 520 | if (!updated.dev.manifest->checkCompatibility(*updated.fwk.matrix, error)) { |
Yifan Hong | 0d4be12 | 2017-05-15 17:04:22 -0700 | [diff] [blame] | 521 | if (error) |
| 522 | error->insert(0, "Device manifest and framework compatibility matrix " |
| 523 | "are incompatible: "); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 524 | return INCOMPATIBLE; |
| 525 | } |
| 526 | } |
| 527 | if (updated.fwk.manifest && updated.dev.matrix) { |
| 528 | if (!updated.fwk.manifest->checkCompatibility(*updated.dev.matrix, error)) { |
Yifan Hong | 0d4be12 | 2017-05-15 17:04:22 -0700 | [diff] [blame] | 529 | if (error) |
| 530 | error->insert(0, "Framework manifest and device compatibility matrix " |
| 531 | "are incompatible: "); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 532 | return INCOMPATIBLE; |
| 533 | } |
| 534 | } |
| 535 | if (updated.runtimeInfo && updated.fwk.matrix) { |
Yifan Hong | db6423e | 2017-09-11 14:38:46 -0700 | [diff] [blame] | 536 | if (!updated.runtimeInfo->checkCompatibility(*updated.fwk.matrix, error, disabledChecks)) { |
Yifan Hong | 0d4be12 | 2017-05-15 17:04:22 -0700 | [diff] [blame] | 537 | if (error) |
| 538 | error->insert(0, "Runtime info and framework compatibility matrix " |
| 539 | "are incompatible: "); |
Yifan Hong | e8b8684 | 2017-05-25 17:59:22 +0000 | [diff] [blame] | 540 | return INCOMPATIBLE; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | return COMPATIBLE; |
| 545 | } |
| 546 | |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 547 | const std::string kSystemVintfDir = "/system/etc/vintf/"; |
Yifan Hong | ccbea05 | 2018-01-18 18:48:46 -0800 | [diff] [blame] | 548 | const std::string kVendorVintfDir = "/vendor/etc/vintf/"; |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 549 | const std::string kOdmVintfDir = "/odm/etc/vintf/"; |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 550 | |
| 551 | const std::string kVendorManifest = kVendorVintfDir + "manifest.xml"; |
Yifan Hong | a5ddddf | 2018-01-18 18:50:13 -0800 | [diff] [blame] | 552 | const std::string kSystemManifest = kSystemVintfDir + "manifest.xml"; |
Yifan Hong | b64ec9e | 2018-01-18 18:57:52 -0800 | [diff] [blame] | 553 | const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml"; |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 554 | const std::string kOdmManifest = kOdmVintfDir + "manifest.xml"; |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 555 | |
| 556 | const std::string kVendorLegacyManifest = "/vendor/manifest.xml"; |
| 557 | const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml"; |
| 558 | const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml"; |
| 559 | const std::string kOdmLegacyVintfDir = "/odm/etc/"; |
| 560 | const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml"; |
| 561 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 562 | } // namespace details |
| 563 | |
Yifan Hong | fbbf047 | 2017-04-07 18:14:18 -0700 | [diff] [blame] | 564 | // static |
Yifan Hong | db6423e | 2017-09-11 14:38:46 -0700 | [diff] [blame] | 565 | int32_t VintfObject::CheckCompatibility(const std::vector<std::string>& xmls, std::string* error, |
| 566 | DisabledChecks disabledChecks) { |
| 567 | return details::checkCompatibility(xmls, false /* mount */, *details::gPartitionMounter, error, |
| 568 | disabledChecks); |
Yifan Hong | fbbf047 | 2017-04-07 18:14:18 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame^] | 571 | bool VintfObject::isHalDeprecated(const MatrixHal& oldMatrixHal, |
| 572 | const CompatibilityMatrix& targetMatrix, |
| 573 | const IsInstanceInUse& isInstanceInUse, std::string* error) { |
| 574 | for (const VersionRange& range : oldMatrixHal.versionRanges) { |
| 575 | for (const HalInterface& interface : iterateValues(oldMatrixHal.interfaces)) { |
| 576 | for (const std::string& instance : interface.instances) { |
| 577 | if (isInstanceDeprecated(oldMatrixHal.name, range.minVer(), interface.name, |
| 578 | instance, targetMatrix, isInstanceInUse, error)) { |
| 579 | return true; |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | // If isInstanceInUse(package@x.y::interface/instance), return true iff: |
| 588 | // 1. package@x.?::interface/instance is not in targetMatrix; OR |
| 589 | // 2. package@x.z::interface/instance is in targetMatrix but |
| 590 | // !isInstanceInUse(package@x.z::interface/instance) |
| 591 | bool VintfObject::isInstanceDeprecated(const std::string& package, Version version, |
| 592 | const std::string& interface, const std::string& instance, |
| 593 | const CompatibilityMatrix& targetMatrix, |
| 594 | const IsInstanceInUse& isInstanceInUse, |
| 595 | std::string* error) { |
| 596 | bool oldVersionIsServed; |
| 597 | Version servedVersion; |
| 598 | std::tie(oldVersionIsServed, servedVersion) = |
| 599 | isInstanceInUse(package, version, interface, instance); |
| 600 | if (oldVersionIsServed) { |
| 601 | // Find any package@x.? in target matrix, and check if instance is in target matrix. |
| 602 | const MatrixHal* targetMatrixHal; |
| 603 | const VersionRange* targetMatrixRange; |
| 604 | std::tie(targetMatrixHal, targetMatrixRange) = |
| 605 | targetMatrix.getHalWithMajorVersion(package, version.majorVer); |
| 606 | if (targetMatrixHal == nullptr || targetMatrixRange == nullptr) { |
| 607 | if (error) { |
| 608 | *error = package + "@" + to_string(servedVersion) + |
| 609 | "is deprecated in compatibility matrix at FCM Version " + |
| 610 | to_string(targetMatrix.level()) + "; it should not be served."; |
| 611 | } |
| 612 | return true; |
| 613 | } |
| 614 | |
| 615 | const auto& targetMatrixInstances = targetMatrixHal->getInstances(interface); |
| 616 | if (targetMatrixInstances.find(instance) == targetMatrixInstances.end()) { |
| 617 | if (error) { |
| 618 | *error += package + "@" + to_string(servedVersion) + "::" + interface + "/" + |
| 619 | instance + " is deprecated at FCM version " + |
| 620 | to_string(targetMatrix.level()) + "; it should be not be served.\n"; |
| 621 | } |
| 622 | return true; |
| 623 | } |
| 624 | |
| 625 | // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served. |
| 626 | bool targetVersionServed; |
| 627 | std::tie(targetVersionServed, std::ignore) = |
| 628 | isInstanceInUse(package, targetMatrixRange->minVer(), interface, instance); |
| 629 | |
| 630 | if (!targetVersionServed) { |
| 631 | if (error) { |
| 632 | *error += package + "@" + to_string(servedVersion) + " is deprecated; " + |
| 633 | "require at least " + to_string(targetMatrixRange->minVer()) + "\n"; |
| 634 | } |
| 635 | return true; |
| 636 | } |
| 637 | } |
| 638 | return false; |
| 639 | } |
| 640 | |
| 641 | int32_t VintfObject::CheckDeprecation(const IsInstanceInUse& isInstanceInUse, |
| 642 | std::string* error) { |
| 643 | auto matrixFragments = GetAllFrameworkMatrixLevels(error); |
| 644 | if (matrixFragments.empty()) { |
| 645 | if (error && error->empty()) |
| 646 | *error = "Cannot get framework matrix for each FCM version for unknown error."; |
| 647 | return NAME_NOT_FOUND; |
| 648 | } |
| 649 | auto deviceManifest = GetDeviceHalManifest(); |
| 650 | if (deviceManifest == nullptr) { |
| 651 | if (error) *error = "No device manifest."; |
| 652 | return NAME_NOT_FOUND; |
| 653 | } |
| 654 | Level deviceLevel = deviceManifest->level(); |
| 655 | if (deviceLevel == Level::UNSPECIFIED) { |
| 656 | if (error) *error = "Device manifest does not specify Shipping FCM Version."; |
| 657 | return BAD_VALUE; |
| 658 | } |
| 659 | |
| 660 | const CompatibilityMatrix* targetMatrix = nullptr; |
| 661 | for (const auto& namedMatrix : matrixFragments) { |
| 662 | if (namedMatrix.object.level() == deviceLevel) { |
| 663 | targetMatrix = &namedMatrix.object; |
| 664 | } |
| 665 | } |
| 666 | if (targetMatrix == nullptr) { |
| 667 | if (error) |
| 668 | *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + "."; |
| 669 | return NAME_NOT_FOUND; |
| 670 | } |
| 671 | |
| 672 | bool hasDeprecatedHals = false; |
| 673 | for (const auto& namedMatrix : matrixFragments) { |
| 674 | if (namedMatrix.object.level() == Level::UNSPECIFIED) continue; |
| 675 | if (namedMatrix.object.level() >= deviceLevel) continue; |
| 676 | |
| 677 | const auto& oldMatrix = namedMatrix.object; |
| 678 | for (const MatrixHal& hal : oldMatrix.getHals()) { |
| 679 | hasDeprecatedHals |= isHalDeprecated(hal, *targetMatrix, isInstanceInUse, error); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS; |
| 684 | } |
| 685 | |
| 686 | int32_t VintfObject::CheckDeprecation(std::string* error) { |
| 687 | auto deviceManifest = GetDeviceHalManifest(); |
| 688 | IsInstanceInUse inManifest = [&deviceManifest](const std::string& package, Version version, |
| 689 | const std::string& interface, |
| 690 | const std::string& instance) { |
| 691 | const ManifestHal* hal = deviceManifest->getHal(package, version); |
| 692 | if (hal == nullptr) { |
| 693 | return std::make_pair(false, Version{}); |
| 694 | } |
| 695 | const auto& instances = hal->getInstances(interface); |
| 696 | if (instances.find(instance) == instances.end()) { |
| 697 | return std::make_pair(false, Version{}); |
| 698 | } |
| 699 | |
| 700 | for (Version v : hal->versions) { |
| 701 | if (v.minorAtLeast(version)) { |
| 702 | return std::make_pair(true, v); |
| 703 | } |
| 704 | } |
| 705 | return std::make_pair(false, Version{}); |
| 706 | }; |
| 707 | return CheckDeprecation(inManifest, error); |
| 708 | } |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 709 | |
| 710 | } // namespace vintf |
| 711 | } // namespace android |