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 | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 21 | #include <functional> |
| 22 | #include <memory> |
| 23 | #include <mutex> |
| 24 | |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 25 | #include <android-base/logging.h> |
| 26 | |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 27 | #include "CompatibilityMatrix.h" |
| 28 | #include "VintfObjectAfterUpdate.h" |
| 29 | #include "parse_string.h" |
| 30 | #include "parse_xml.h" |
| 31 | #include "utils.h" |
| 32 | |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 33 | using std::placeholders::_1; |
| 34 | using std::placeholders::_2; |
| 35 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 36 | namespace android { |
| 37 | namespace vintf { |
| 38 | |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 39 | using namespace details; |
| 40 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 41 | #ifdef LIBVINTF_TARGET |
| 42 | static constexpr bool kIsTarget = true; |
| 43 | #else |
| 44 | static constexpr bool kIsTarget = false; |
| 45 | #endif |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 46 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 47 | template <typename T, typename F> |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 48 | static std::shared_ptr<const T> Get( |
| 49 | LockedSharedPtr<T> *ptr, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 50 | bool skipCache, |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 51 | const F &fetchAllInformation) { |
| 52 | std::unique_lock<std::mutex> _lock(ptr->mutex); |
Yifan Hong | 7219ba1 | 2018-01-08 16:23:49 -0800 | [diff] [blame] | 53 | if (skipCache || !ptr->fetchedOnce) { |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 54 | ptr->object = std::make_unique<T>(); |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 55 | std::string error; |
| 56 | if (fetchAllInformation(ptr->object.get(), &error) != OK) { |
| 57 | LOG(WARNING) << error; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 58 | ptr->object = nullptr; // frees the old object |
| 59 | } |
Yifan Hong | 7219ba1 | 2018-01-08 16:23:49 -0800 | [diff] [blame] | 60 | ptr->fetchedOnce = true; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 61 | } |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 62 | return ptr->object; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 65 | static std::unique_ptr<FileSystem> createDefaultFileSystem() { |
| 66 | std::unique_ptr<FileSystem> fileSystem; |
| 67 | if (kIsTarget) { |
| 68 | fileSystem = std::make_unique<details::FileSystemImpl>(); |
| 69 | } else { |
| 70 | fileSystem = std::make_unique<details::FileSystemNoOp>(); |
| 71 | } |
| 72 | return fileSystem; |
| 73 | } |
| 74 | |
| 75 | static std::unique_ptr<PropertyFetcher> createDefaultPropertyFetcher() { |
| 76 | std::unique_ptr<PropertyFetcher> propertyFetcher; |
| 77 | if (kIsTarget) { |
| 78 | propertyFetcher = std::make_unique<details::PropertyFetcherImpl>(); |
| 79 | } else { |
| 80 | propertyFetcher = std::make_unique<details::PropertyFetcherNoOp>(); |
| 81 | } |
| 82 | return propertyFetcher; |
| 83 | } |
| 84 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 85 | details::LockedSharedPtr<VintfObject> VintfObject::sInstance{}; |
| 86 | std::shared_ptr<VintfObject> VintfObject::GetInstance() { |
| 87 | std::unique_lock<std::mutex> lock(sInstance.mutex); |
| 88 | if (sInstance.object == nullptr) { |
Yifan Hong | 78f5b57 | 2018-11-27 14:05:03 -0800 | [diff] [blame] | 89 | sInstance.object = std::shared_ptr<VintfObject>(VintfObject::Builder().build().release()); |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 90 | } |
| 91 | return sInstance.object; |
| 92 | } |
| 93 | |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 94 | std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest(bool skipCache) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 95 | return GetInstance()->getDeviceHalManifest(skipCache); |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 98 | std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest(bool skipCache) { |
| 99 | return Get(&mDeviceManifest, skipCache, |
| 100 | std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2)); |
| 101 | } |
| 102 | |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 103 | std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest(bool skipCache) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 104 | return GetInstance()->getFrameworkHalManifest(skipCache); |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 107 | std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest(bool skipCache) { |
| 108 | return Get(&mFrameworkManifest, skipCache, |
| 109 | std::bind(&VintfObject::fetchFrameworkHalManifest, this, _1, _2)); |
| 110 | } |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 111 | |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 112 | std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 113 | return GetInstance()->getDeviceCompatibilityMatrix(skipCache); |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 116 | std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix( |
| 117 | bool skipCache) { |
| 118 | return Get(&mDeviceMatrix, skipCache, std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2)); |
| 119 | } |
| 120 | |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 121 | std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 122 | return GetInstance()->getFrameworkCompatibilityMatrix(skipCache); |
| 123 | } |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 124 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 125 | std::shared_ptr<const CompatibilityMatrix> VintfObject::getFrameworkCompatibilityMatrix( |
| 126 | bool skipCache) { |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 127 | // To avoid deadlock, get device manifest before any locks. |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 128 | auto deviceManifest = getDeviceHalManifest(); |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 129 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 130 | std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex); |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 131 | |
| 132 | auto combined = |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 133 | Get(&mCombinedFrameworkMatrix, skipCache, |
| 134 | std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest, _1, _2)); |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 135 | if (combined != nullptr) { |
| 136 | return combined; |
| 137 | } |
| 138 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 139 | return Get(&mFrameworkMatrix, skipCache, |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 140 | std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(), |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 141 | kSystemLegacyMatrix, _2)); |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 144 | status_t VintfObject::getCombinedFrameworkMatrix( |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 145 | const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out, |
| 146 | std::string* error) { |
Yifan Hong | 73bde59 | 2019-01-22 13:30:23 -0800 | [diff] [blame] | 147 | std::vector<Named<CompatibilityMatrix>> matrixFragments; |
| 148 | auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error); |
| 149 | if (matrixFragmentsStatus != OK) { |
| 150 | return matrixFragmentsStatus; |
| 151 | } |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 152 | if (matrixFragments.empty()) { |
Yifan Hong | 73bde59 | 2019-01-22 13:30:23 -0800 | [diff] [blame] | 153 | if (error && error->empty()) { |
| 154 | *error = "Cannot get framework matrix for each FCM version for unknown error."; |
| 155 | } |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 156 | return NAME_NOT_FOUND; |
| 157 | } |
| 158 | |
| 159 | Level deviceLevel = Level::UNSPECIFIED; |
| 160 | |
| 161 | if (deviceManifest != nullptr) { |
| 162 | deviceLevel = deviceManifest->level(); |
| 163 | } |
| 164 | |
| 165 | // TODO(b/70628538): Do not infer from Shipping API level. |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 166 | if (deviceLevel == Level::UNSPECIFIED) { |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 167 | auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u); |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 168 | if (shippingApi != 0u) { |
| 169 | deviceLevel = details::convertFromApiLevel(shippingApi); |
| 170 | } |
| 171 | } |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 172 | |
| 173 | if (deviceLevel == Level::UNSPECIFIED) { |
| 174 | // Cannot infer FCM version. Combine all matrices by assuming |
| 175 | // Shipping FCM Version == min(all supported FCM Versions in the framework) |
| 176 | for (auto&& pair : matrixFragments) { |
| 177 | Level fragmentLevel = pair.object.level(); |
| 178 | if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) { |
| 179 | deviceLevel = fragmentLevel; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (deviceLevel == Level::UNSPECIFIED) { |
| 185 | // None of the fragments specify any FCM version. Should never happen except |
| 186 | // for inconsistent builds. |
| 187 | if (error) { |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 188 | *error = "No framework compatibility matrix files under " + kSystemVintfDir + |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 189 | " declare FCM version."; |
| 190 | } |
| 191 | return NAME_NOT_FOUND; |
| 192 | } |
| 193 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 194 | auto combined = CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error); |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 195 | if (combined == nullptr) { |
| 196 | return BAD_VALUE; |
| 197 | } |
| 198 | *out = std::move(*combined); |
| 199 | return OK; |
| 200 | } |
| 201 | |
Steven Moreland | eedf2d4 | 2018-04-04 16:36:27 -0700 | [diff] [blame] | 202 | // Load and combine all of the manifests in a directory |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 203 | status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest, |
Steven Moreland | eedf2d4 | 2018-04-04 16:36:27 -0700 | [diff] [blame] | 204 | std::string* error) { |
| 205 | std::vector<std::string> fileNames; |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 206 | status_t err = getFileSystem()->listFiles(directory, &fileNames, error); |
Steven Moreland | eedf2d4 | 2018-04-04 16:36:27 -0700 | [diff] [blame] | 207 | // if the directory isn't there, that's okay |
| 208 | if (err == NAME_NOT_FOUND) return OK; |
| 209 | if (err != OK) return err; |
| 210 | |
| 211 | for (const std::string& file : fileNames) { |
| 212 | // Only adds HALs because all other things are added by libvintf |
| 213 | // itself for now. |
| 214 | HalManifest fragmentManifest; |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 215 | err = fetchOneHalManifest(directory + file, &fragmentManifest, error); |
Steven Moreland | eedf2d4 | 2018-04-04 16:36:27 -0700 | [diff] [blame] | 216 | if (err != OK) return err; |
| 217 | |
| 218 | manifest->addAllHals(&fragmentManifest); |
| 219 | } |
| 220 | |
| 221 | return OK; |
| 222 | } |
| 223 | |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 224 | // Priority for loading vendor manifest: |
Steven Moreland | eedf2d4 | 2018-04-04 16:36:27 -0700 | [diff] [blame] | 225 | // 1. /vendor/etc/vintf/manifest.xml + device fragments + ODM manifest (optional) + odm fragments |
| 226 | // 2. /vendor/etc/vintf/manifest.xml + device fragments |
| 227 | // 3. ODM manifest (optional) + odm fragments |
| 228 | // 4. /vendor/manifest.xml (legacy, no fragments) |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 229 | // where: |
Steven Moreland | 30a532c | 2018-11-01 08:46:32 -0700 | [diff] [blame] | 230 | // A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority |
| 231 | // over A. |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 232 | status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) { |
| 233 | status_t vendorStatus = fetchOneHalManifest(kVendorManifest, out, error); |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 234 | if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) { |
| 235 | return vendorStatus; |
| 236 | } |
| 237 | |
Steven Moreland | eedf2d4 | 2018-04-04 16:36:27 -0700 | [diff] [blame] | 238 | if (vendorStatus == OK) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 239 | status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error); |
Steven Moreland | eedf2d4 | 2018-04-04 16:36:27 -0700 | [diff] [blame] | 240 | if (fragmentStatus != OK) { |
| 241 | return fragmentStatus; |
| 242 | } |
| 243 | } |
| 244 | |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 245 | HalManifest odmManifest; |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 246 | status_t odmStatus = fetchOdmHalManifest(&odmManifest, error); |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 247 | if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) { |
| 248 | return odmStatus; |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 251 | if (vendorStatus == OK) { |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 252 | if (odmStatus == OK) { |
| 253 | out->addAllHals(&odmManifest); |
| 254 | } |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 255 | return addDirectoryManifests(kOdmManifestFragmentDir, out, error); |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 258 | // vendorStatus != OK, "out" is not changed. |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 259 | if (odmStatus == OK) { |
| 260 | *out = std::move(odmManifest); |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 261 | return addDirectoryManifests(kOdmManifestFragmentDir, out, error); |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // Use legacy /vendor/manifest.xml |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 265 | return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error); |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 268 | // "out" is written to iff return status is OK. |
| 269 | // Priority: |
| 270 | // 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml |
| 271 | // 2. /odm/etc/vintf/manifest.xml |
| 272 | // 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml |
| 273 | // 4. /odm/etc/manifest.xml |
| 274 | // where: |
| 275 | // {sku} is the value of ro.boot.product.hardware.sku |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 276 | status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) { |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 277 | status_t status; |
| 278 | |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 279 | std::string productModel; |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 280 | productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", ""); |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 281 | |
| 282 | if (!productModel.empty()) { |
| 283 | status = |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 284 | fetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error); |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 285 | if (status == OK || status != NAME_NOT_FOUND) { |
| 286 | return status; |
| 287 | } |
| 288 | } |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 289 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 290 | status = fetchOneHalManifest(kOdmManifest, out, error); |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 291 | if (status == OK || status != NAME_NOT_FOUND) { |
| 292 | return status; |
| 293 | } |
| 294 | |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 295 | if (!productModel.empty()) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 296 | status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out, |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 297 | error); |
| 298 | if (status == OK || status != NAME_NOT_FOUND) { |
| 299 | return status; |
| 300 | } |
| 301 | } |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 302 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 303 | status = fetchOneHalManifest(kOdmLegacyManifest, out, error); |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 304 | if (status == OK || status != NAME_NOT_FOUND) { |
| 305 | return status; |
| 306 | } |
| 307 | |
| 308 | return NAME_NOT_FOUND; |
| 309 | } |
| 310 | |
| 311 | // Fetch one manifest.xml file. "out" is written to iff return status is OK. |
| 312 | // Returns NAME_NOT_FOUND if file is missing. |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 313 | status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out, |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 314 | std::string* error) { |
| 315 | HalManifest ret; |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 316 | status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error); |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 317 | if (status == OK) { |
| 318 | *out = std::move(ret); |
| 319 | } |
| 320 | return status; |
| 321 | } |
| 322 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 323 | status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) { |
Yifan Hong | b64ec9e | 2018-01-18 18:57:52 -0800 | [diff] [blame] | 324 | CompatibilityMatrix etcMatrix; |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 325 | if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) { |
Yifan Hong | b64ec9e | 2018-01-18 18:57:52 -0800 | [diff] [blame] | 326 | *out = std::move(etcMatrix); |
| 327 | return OK; |
| 328 | } |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 329 | return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error); |
Yifan Hong | b64ec9e | 2018-01-18 18:57:52 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 332 | status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) { |
Yifan Hong | de6d00e | 2018-02-27 17:11:28 -0800 | [diff] [blame] | 333 | HalManifest etcManifest; |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 334 | if (etcManifest.fetchAllInformation(getFileSystem().get(), kSystemManifest, error) == OK) { |
Yifan Hong | de6d00e | 2018-02-27 17:11:28 -0800 | [diff] [blame] | 335 | *out = std::move(etcManifest); |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 336 | return addDirectoryManifests(kSystemManifestFragmentDir, out, error); |
Yifan Hong | de6d00e | 2018-02-27 17:11:28 -0800 | [diff] [blame] | 337 | } |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 338 | return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error); |
Yifan Hong | de6d00e | 2018-02-27 17:11:28 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Yifan Hong | 9f8f656 | 2018-11-06 16:26:20 -0800 | [diff] [blame] | 341 | static void appendLine(std::string* error, const std::string& message) { |
| 342 | if (error != nullptr) { |
| 343 | if (!error->empty()) *error += "\n"; |
| 344 | *error += message; |
| 345 | } |
| 346 | } |
| 347 | |
Yifan Hong | 73bde59 | 2019-01-22 13:30:23 -0800 | [diff] [blame] | 348 | status_t VintfObject::getOneMatrix(const std::string& path, Named<CompatibilityMatrix>* out, |
| 349 | std::string* error) { |
| 350 | std::string content; |
| 351 | status_t status = getFileSystem()->fetch(path, &content, error); |
| 352 | if (status != OK) { |
| 353 | return status; |
| 354 | } |
| 355 | if (!gCompatibilityMatrixConverter(&out->object, content, error)) { |
| 356 | if (error) { |
| 357 | error->insert(0, "Cannot parse " + path + ": "); |
| 358 | } |
| 359 | return BAD_VALUE; |
| 360 | } |
| 361 | out->name = path; |
| 362 | return OK; |
| 363 | } |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 364 | |
Yifan Hong | 73bde59 | 2019-01-22 13:30:23 -0800 | [diff] [blame] | 365 | status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<Named<CompatibilityMatrix>>* results, |
| 366 | std::string* error) { |
| 367 | std::vector<std::string> fileNames; |
| 368 | |
| 369 | status_t listStatus = getFileSystem()->listFiles(kSystemVintfDir, &fileNames, error); |
| 370 | if (listStatus != OK) { |
| 371 | return listStatus; |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 372 | } |
| 373 | for (const std::string& fileName : fileNames) { |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 374 | std::string path = kSystemVintfDir + fileName; |
Yifan Hong | 73bde59 | 2019-01-22 13:30:23 -0800 | [diff] [blame] | 375 | Named<CompatibilityMatrix> namedMatrix; |
| 376 | std::string matrixError; |
| 377 | status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError); |
| 378 | if (matrixStatus != OK) { |
| 379 | // System manifests and matrices share the same dir. Client may not have enough |
| 380 | // permissions to read system manifests, or may not be able to parse it. |
| 381 | auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR; |
| 382 | LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError; |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 383 | continue; |
| 384 | } |
Yifan Hong | 73bde59 | 2019-01-22 13:30:23 -0800 | [diff] [blame] | 385 | results->emplace_back(std::move(namedMatrix)); |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Yifan Hong | 73bde59 | 2019-01-22 13:30:23 -0800 | [diff] [blame] | 388 | Named<CompatibilityMatrix> productMatrix; |
| 389 | std::string productError; |
| 390 | status_t productStatus = getOneMatrix(kProductMatrix, &productMatrix, &productError); |
| 391 | if (productStatus == OK) { |
| 392 | results->emplace_back(std::move(productMatrix)); |
| 393 | } else if (productStatus == NAME_NOT_FOUND) { |
| 394 | LOG(DEBUG) << "Framework Matrix: missing " << kProductMatrix; |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 395 | } else { |
Yifan Hong | 73bde59 | 2019-01-22 13:30:23 -0800 | [diff] [blame] | 396 | if (error) *error = std::move(productError); |
| 397 | return productStatus; |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Yifan Hong | 73bde59 | 2019-01-22 13:30:23 -0800 | [diff] [blame] | 400 | if (results->empty()) { |
| 401 | if (error) { |
| 402 | *error = |
| 403 | "No framework matrices under " + kSystemVintfDir + " can be fetched or parsed.\n"; |
| 404 | } |
| 405 | return NAME_NOT_FOUND; |
| 406 | } |
| 407 | return OK; |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 410 | std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache, |
| 411 | RuntimeInfo::FetchFlags flags) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 412 | return GetInstance()->getRuntimeInfo(skipCache, flags); |
| 413 | } |
| 414 | std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(bool skipCache, |
| 415 | RuntimeInfo::FetchFlags flags) { |
| 416 | std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex); |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 417 | |
| 418 | if (!skipCache) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 419 | flags &= (~mDeviceRuntimeInfo.fetchedFlags); |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 422 | if (mDeviceRuntimeInfo.object == nullptr) { |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 423 | mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared(); |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 426 | status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags); |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 427 | if (status != OK) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 428 | mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched" |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 429 | return nullptr; |
| 430 | } |
| 431 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 432 | mDeviceRuntimeInfo.fetchedFlags |= flags; |
| 433 | return mDeviceRuntimeInfo.object; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 436 | namespace details { |
| 437 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 438 | enum class ParseStatus { |
| 439 | OK, |
| 440 | PARSE_ERROR, |
| 441 | DUPLICATED_FWK_ENTRY, |
| 442 | DUPLICATED_DEV_ENTRY, |
| 443 | }; |
| 444 | |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 445 | static std::string toString(ParseStatus status) { |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 446 | switch(status) { |
| 447 | case ParseStatus::OK: return "OK"; |
| 448 | case ParseStatus::PARSE_ERROR: return "parse error"; |
| 449 | case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework"; |
| 450 | case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device"; |
| 451 | } |
| 452 | return ""; |
| 453 | } |
| 454 | |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 455 | template <typename T> |
| 456 | static ParseStatus tryParse(const std::string& xml, const XmlConverter<T>& parse, |
| 457 | VintfObjectAfterUpdate* afterUpdate) { |
Yifan Hong | fc73edf | 2017-08-29 11:39:07 -0700 | [diff] [blame] | 458 | std::shared_ptr<T> ret = std::make_shared<T>(); |
Yifan Hong | 9475706 | 2018-02-09 16:36:31 -0800 | [diff] [blame] | 459 | if (!parse(ret.get(), xml, nullptr /* error */)) { |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 460 | return ParseStatus::PARSE_ERROR; |
| 461 | } |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 462 | if (!afterUpdate->set(ret)) { |
| 463 | if (ret->type() == SchemaType::FRAMEWORK) { |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 464 | return ParseStatus::DUPLICATED_FWK_ENTRY; |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 465 | } else if (ret->type() == SchemaType::DEVICE) { |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 466 | return ParseStatus::DUPLICATED_DEV_ENTRY; |
| 467 | } |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 468 | LOG(FATAL) << "unknown SchemaType: " |
| 469 | << static_cast<std::underlying_type_t<SchemaType>>(ret->type()); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 470 | } |
| 471 | return ParseStatus::OK; |
| 472 | } |
| 473 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 474 | } // namespace details |
| 475 | |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 476 | // Simulate applying xmls to VintfObject, then checkCompatibility as usual. |
Yifan Hong | af3713e | 2018-11-05 13:59:18 -0800 | [diff] [blame] | 477 | int32_t VintfObject::checkCompatibility(const std::vector<std::string>& xmls, std::string* error, |
| 478 | CheckFlags::Type flags) { |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 479 | VintfObjectAfterUpdate afterUpdate(this); |
| 480 | ParseStatus parseStatus = ParseStatus::OK; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 481 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 482 | // parse all information from package |
| 483 | for (const auto &xml : xmls) { |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 484 | parseStatus = tryParse(xml, gHalManifestConverter, &afterUpdate); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 485 | if (parseStatus == ParseStatus::OK) { |
| 486 | continue; // work on next one |
| 487 | } |
| 488 | if (parseStatus != ParseStatus::PARSE_ERROR) { |
Yifan Hong | 878556e | 2018-07-13 13:45:30 -0700 | [diff] [blame] | 489 | appendLine(error, toString(parseStatus) + " manifest"); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 490 | return ALREADY_EXISTS; |
| 491 | } |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 492 | parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &afterUpdate); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 493 | if (parseStatus == ParseStatus::OK) { |
| 494 | continue; // work on next one |
| 495 | } |
| 496 | if (parseStatus != ParseStatus::PARSE_ERROR) { |
Yifan Hong | 878556e | 2018-07-13 13:45:30 -0700 | [diff] [blame] | 497 | appendLine(error, toString(parseStatus) + " matrix"); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 498 | return ALREADY_EXISTS; |
| 499 | } |
Yifan Hong | 878556e | 2018-07-13 13:45:30 -0700 | [diff] [blame] | 500 | appendLine(error, toString(parseStatus)); // parse error |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 501 | return BAD_VALUE; |
| 502 | } |
| 503 | |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 504 | return afterUpdate.checkCompatibility(error, flags); |
| 505 | } |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 506 | |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 507 | int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) { |
| 508 | status_t status = OK; |
| 509 | // null checks for files and runtime info |
| 510 | if (getFrameworkHalManifest() == nullptr) { |
Yifan Hong | 878556e | 2018-07-13 13:45:30 -0700 | [diff] [blame] | 511 | appendLine(error, "No framework manifest file from device or from update package"); |
| 512 | status = NO_INIT; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 513 | } |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 514 | if (getDeviceHalManifest() == nullptr) { |
Yifan Hong | 878556e | 2018-07-13 13:45:30 -0700 | [diff] [blame] | 515 | appendLine(error, "No device manifest file from device or from update package"); |
| 516 | status = NO_INIT; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 517 | } |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 518 | if (getFrameworkCompatibilityMatrix() == nullptr) { |
Yifan Hong | 878556e | 2018-07-13 13:45:30 -0700 | [diff] [blame] | 519 | appendLine(error, "No framework matrix file from device or from update package"); |
| 520 | status = NO_INIT; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 521 | } |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 522 | if (getDeviceCompatibilityMatrix() == nullptr) { |
Yifan Hong | 878556e | 2018-07-13 13:45:30 -0700 | [diff] [blame] | 523 | appendLine(error, "No device matrix file from device or from update package"); |
| 524 | status = NO_INIT; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 525 | } |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 526 | |
Yifan Hong | 072f12d | 2018-08-08 13:04:51 -0700 | [diff] [blame] | 527 | if (flags.isRuntimeInfoEnabled()) { |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 528 | if (getRuntimeInfo() == nullptr) { |
Yifan Hong | 878556e | 2018-07-13 13:45:30 -0700 | [diff] [blame] | 529 | appendLine(error, "No runtime info from device"); |
| 530 | status = NO_INIT; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 531 | } |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 532 | } |
Yifan Hong | 878556e | 2018-07-13 13:45:30 -0700 | [diff] [blame] | 533 | if (status != OK) return status; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 534 | |
| 535 | // compatiblity check. |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 536 | if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) { |
Yifan Hong | ca386fe | 2018-02-07 11:29:03 -0800 | [diff] [blame] | 537 | if (error) { |
| 538 | error->insert(0, |
| 539 | "Device manifest and framework compatibility matrix are incompatible: "); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 540 | } |
Yifan Hong | ca386fe | 2018-02-07 11:29:03 -0800 | [diff] [blame] | 541 | return INCOMPATIBLE; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 542 | } |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 543 | if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) { |
Yifan Hong | ca386fe | 2018-02-07 11:29:03 -0800 | [diff] [blame] | 544 | if (error) { |
| 545 | error->insert(0, |
| 546 | "Framework manifest and device compatibility matrix are incompatible: "); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 547 | } |
Yifan Hong | ca386fe | 2018-02-07 11:29:03 -0800 | [diff] [blame] | 548 | return INCOMPATIBLE; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 549 | } |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 550 | |
Yifan Hong | 40b7e62 | 2019-01-29 12:27:42 -0800 | [diff] [blame] | 551 | CheckFlags::Type runtimeInfoCheckFlags = flags; |
| 552 | if (!!getDeviceHalManifest()->kernel()) { |
| 553 | // Use kernel from incoming OTA package, but not on the device. |
| 554 | runtimeInfoCheckFlags = runtimeInfoCheckFlags.disableKernel(); |
| 555 | } |
| 556 | |
Yifan Hong | 072f12d | 2018-08-08 13:04:51 -0700 | [diff] [blame] | 557 | if (flags.isRuntimeInfoEnabled()) { |
Yifan Hong | 12e23c2 | 2018-11-05 14:53:30 -0800 | [diff] [blame] | 558 | if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error, |
Yifan Hong | 40b7e62 | 2019-01-29 12:27:42 -0800 | [diff] [blame] | 559 | runtimeInfoCheckFlags)) { |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 560 | if (error) { |
| 561 | error->insert(0, |
| 562 | "Runtime info and framework compatibility matrix are incompatible: "); |
| 563 | } |
| 564 | return INCOMPATIBLE; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
| 568 | return COMPATIBLE; |
| 569 | } |
| 570 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 571 | namespace details { |
| 572 | |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 573 | const std::string kSystemVintfDir = "/system/etc/vintf/"; |
Yifan Hong | ccbea05 | 2018-01-18 18:48:46 -0800 | [diff] [blame] | 574 | const std::string kVendorVintfDir = "/vendor/etc/vintf/"; |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 575 | const std::string kOdmVintfDir = "/odm/etc/vintf/"; |
Yifan Hong | 73bde59 | 2019-01-22 13:30:23 -0800 | [diff] [blame] | 576 | const std::string kProductVintfDir = "/product/etc/vintf/"; |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 577 | |
| 578 | const std::string kVendorManifest = kVendorVintfDir + "manifest.xml"; |
Yifan Hong | a5ddddf | 2018-01-18 18:50:13 -0800 | [diff] [blame] | 579 | const std::string kSystemManifest = kSystemVintfDir + "manifest.xml"; |
Yifan Hong | b64ec9e | 2018-01-18 18:57:52 -0800 | [diff] [blame] | 580 | const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml"; |
Yifan Hong | 12a11ac | 2018-01-19 13:58:32 -0800 | [diff] [blame] | 581 | const std::string kOdmManifest = kOdmVintfDir + "manifest.xml"; |
Yifan Hong | 73bde59 | 2019-01-22 13:30:23 -0800 | [diff] [blame] | 582 | const std::string kProductMatrix = kProductVintfDir + "compatibility_matrix.xml"; |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 583 | |
Steven Moreland | eedf2d4 | 2018-04-04 16:36:27 -0700 | [diff] [blame] | 584 | const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/"; |
| 585 | const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/"; |
| 586 | const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/"; |
| 587 | |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 588 | const std::string kVendorLegacyManifest = "/vendor/manifest.xml"; |
| 589 | const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml"; |
Yifan Hong | de6d00e | 2018-02-27 17:11:28 -0800 | [diff] [blame] | 590 | const std::string kSystemLegacyManifest = "/system/manifest.xml"; |
Yifan Hong | 270b565 | 2018-01-18 18:46:01 -0800 | [diff] [blame] | 591 | const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml"; |
| 592 | const std::string kOdmLegacyVintfDir = "/odm/etc/"; |
| 593 | const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml"; |
| 594 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 595 | std::vector<std::string> dumpFileList() { |
| 596 | return { |
Yifan Hong | 73bde59 | 2019-01-22 13:30:23 -0800 | [diff] [blame] | 597 | // clang-format off |
| 598 | kSystemVintfDir, |
| 599 | kVendorVintfDir, |
| 600 | kOdmVintfDir, |
| 601 | kProductVintfDir, |
| 602 | kOdmLegacyVintfDir, |
| 603 | kVendorLegacyManifest, |
| 604 | kVendorLegacyMatrix, |
| 605 | kSystemLegacyManifest, |
| 606 | kSystemLegacyMatrix, |
| 607 | // clang-format on |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 608 | }; |
| 609 | } |
| 610 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 611 | } // namespace details |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 612 | |
Yifan Hong | db6423e | 2017-09-11 14:38:46 -0700 | [diff] [blame] | 613 | int32_t VintfObject::CheckCompatibility(const std::vector<std::string>& xmls, std::string* error, |
Yifan Hong | 072f12d | 2018-08-08 13:04:51 -0700 | [diff] [blame] | 614 | CheckFlags::Type flags) { |
| 615 | return GetInstance()->checkCompatibility(xmls, error, flags); |
Yifan Hong | fbbf047 | 2017-04-07 18:14:18 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 618 | bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal, |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 619 | const CompatibilityMatrix& targetMatrix, |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 620 | const ListInstances& listInstances, std::string* error) { |
Yifan Hong | 7e9e04d | 2018-03-20 13:06:00 -0700 | [diff] [blame] | 621 | bool isDeprecated = false; |
| 622 | oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 623 | if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, error)) { |
Yifan Hong | 7e9e04d | 2018-03-20 13:06:00 -0700 | [diff] [blame] | 624 | isDeprecated = true; |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 625 | } |
Yifan Hong | 7e9e04d | 2018-03-20 13:06:00 -0700 | [diff] [blame] | 626 | return !isDeprecated; // continue if no deprecated instance is found. |
| 627 | }); |
| 628 | return isDeprecated; |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 629 | } |
| 630 | |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 631 | // Let oldMatrixInstance = package@x.y-w::interface with instancePattern. |
| 632 | // If any "servedInstance" in listInstances(package@x.y::interface) matches instancePattern, return |
| 633 | // true iff: |
| 634 | // 1. package@x.?::interface/servedInstance is not in targetMatrix; OR |
| 635 | // 2. package@x.z::interface/servedInstance is in targetMatrix but |
| 636 | // servedInstance is not in listInstances(package@x.z::interface) |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 637 | bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance, |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 638 | const CompatibilityMatrix& targetMatrix, |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 639 | const ListInstances& listInstances, std::string* error) { |
Yifan Hong | 7e9e04d | 2018-03-20 13:06:00 -0700 | [diff] [blame] | 640 | const std::string& package = oldMatrixInstance.package(); |
| 641 | const Version& version = oldMatrixInstance.versionRange().minVer(); |
| 642 | const std::string& interface = oldMatrixInstance.interface(); |
Yifan Hong | 7e9e04d | 2018-03-20 13:06:00 -0700 | [diff] [blame] | 643 | |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 644 | std::vector<std::string> instanceHint; |
| 645 | if (!oldMatrixInstance.isRegex()) { |
| 646 | instanceHint.push_back(oldMatrixInstance.exactInstance()); |
| 647 | } |
| 648 | |
| 649 | auto list = listInstances(package, version, interface, instanceHint); |
| 650 | for (const auto& pair : list) { |
| 651 | const std::string& servedInstance = pair.first; |
| 652 | Version servedVersion = pair.second; |
| 653 | if (!oldMatrixInstance.matchInstance(servedInstance)) { |
| 654 | continue; |
| 655 | } |
| 656 | |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 657 | // Find any package@x.? in target matrix, and check if instance is in target matrix. |
Yifan Hong | 217f47e | 2018-03-15 12:34:05 -0700 | [diff] [blame] | 658 | bool foundInstance = false; |
| 659 | Version targetMatrixMinVer; |
| 660 | targetMatrix.forEachInstanceOfPackage(package, [&](const auto& targetMatrixInstance) { |
| 661 | if (targetMatrixInstance.versionRange().majorVer == version.majorVer && |
| 662 | targetMatrixInstance.interface() == interface && |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 663 | targetMatrixInstance.matchInstance(servedInstance)) { |
Yifan Hong | 217f47e | 2018-03-15 12:34:05 -0700 | [diff] [blame] | 664 | targetMatrixMinVer = targetMatrixInstance.versionRange().minVer(); |
| 665 | foundInstance = true; |
| 666 | } |
| 667 | return !foundInstance; // continue if not found |
| 668 | }); |
| 669 | if (!foundInstance) { |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 670 | if (error) { |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 671 | *error = toFQNameString(package, servedVersion, interface, servedInstance) + |
Steven Moreland | a1b984c | 2018-03-09 13:09:15 -0800 | [diff] [blame] | 672 | " is deprecated in compatibility matrix at FCM Version " + |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 673 | to_string(targetMatrix.level()) + "; it should not be served."; |
| 674 | } |
| 675 | return true; |
| 676 | } |
| 677 | |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 678 | // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served. |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 679 | bool targetVersionServed = false; |
| 680 | for (const auto& newPair : |
| 681 | listInstances(package, targetMatrixMinVer, interface, instanceHint)) { |
| 682 | if (newPair.first == servedInstance) { |
| 683 | targetVersionServed = true; |
| 684 | break; |
| 685 | } |
| 686 | } |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 687 | |
| 688 | if (!targetVersionServed) { |
Yifan Hong | 9f8f656 | 2018-11-06 16:26:20 -0800 | [diff] [blame] | 689 | appendLine(error, toFQNameString(package, servedVersion, interface, servedInstance) + |
| 690 | " is deprecated; requires at least " + |
| 691 | to_string(targetMatrixMinVer)); |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 692 | return true; |
| 693 | } |
| 694 | } |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 695 | |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 696 | return false; |
| 697 | } |
| 698 | |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 699 | int32_t VintfObject::CheckDeprecation(const ListInstances& listInstances, std::string* error) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 700 | return GetInstance()->checkDeprecation(listInstances, error); |
| 701 | } |
| 702 | int32_t VintfObject::checkDeprecation(const ListInstances& listInstances, std::string* error) { |
Yifan Hong | 73bde59 | 2019-01-22 13:30:23 -0800 | [diff] [blame] | 703 | std::vector<Named<CompatibilityMatrix>> matrixFragments; |
| 704 | auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error); |
| 705 | if (matrixFragmentsStatus != OK) { |
| 706 | return matrixFragmentsStatus; |
| 707 | } |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 708 | if (matrixFragments.empty()) { |
Yifan Hong | 73bde59 | 2019-01-22 13:30:23 -0800 | [diff] [blame] | 709 | if (error && error->empty()) { |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 710 | *error = "Cannot get framework matrix for each FCM version for unknown error."; |
Yifan Hong | 73bde59 | 2019-01-22 13:30:23 -0800 | [diff] [blame] | 711 | } |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 712 | return NAME_NOT_FOUND; |
| 713 | } |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 714 | auto deviceManifest = getDeviceHalManifest(); |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 715 | if (deviceManifest == nullptr) { |
| 716 | if (error) *error = "No device manifest."; |
| 717 | return NAME_NOT_FOUND; |
| 718 | } |
| 719 | Level deviceLevel = deviceManifest->level(); |
| 720 | if (deviceLevel == Level::UNSPECIFIED) { |
| 721 | if (error) *error = "Device manifest does not specify Shipping FCM Version."; |
| 722 | return BAD_VALUE; |
| 723 | } |
| 724 | |
| 725 | const CompatibilityMatrix* targetMatrix = nullptr; |
| 726 | for (const auto& namedMatrix : matrixFragments) { |
| 727 | if (namedMatrix.object.level() == deviceLevel) { |
| 728 | targetMatrix = &namedMatrix.object; |
| 729 | } |
| 730 | } |
| 731 | if (targetMatrix == nullptr) { |
| 732 | if (error) |
| 733 | *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + "."; |
| 734 | return NAME_NOT_FOUND; |
| 735 | } |
| 736 | |
| 737 | bool hasDeprecatedHals = false; |
| 738 | for (const auto& namedMatrix : matrixFragments) { |
| 739 | if (namedMatrix.object.level() == Level::UNSPECIFIED) continue; |
| 740 | if (namedMatrix.object.level() >= deviceLevel) continue; |
| 741 | |
| 742 | const auto& oldMatrix = namedMatrix.object; |
| 743 | for (const MatrixHal& hal : oldMatrix.getHals()) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 744 | hasDeprecatedHals |= IsHalDeprecated(hal, *targetMatrix, listInstances, error); |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
| 748 | return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS; |
| 749 | } |
| 750 | |
| 751 | int32_t VintfObject::CheckDeprecation(std::string* error) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 752 | return GetInstance()->checkDeprecation(error); |
| 753 | } |
| 754 | int32_t VintfObject::checkDeprecation(std::string* error) { |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 755 | using namespace std::placeholders; |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 756 | auto deviceManifest = getDeviceHalManifest(); |
Yifan Hong | a8a8fa9 | 2018-03-20 14:42:43 -0700 | [diff] [blame] | 757 | ListInstances inManifest = |
| 758 | [&deviceManifest](const std::string& package, Version version, const std::string& interface, |
| 759 | const std::vector<std::string>& /* hintInstances */) { |
| 760 | std::vector<std::pair<std::string, Version>> ret; |
| 761 | deviceManifest->forEachInstanceOfInterface( |
| 762 | package, version, interface, [&ret](const ManifestInstance& manifestInstance) { |
| 763 | ret.push_back( |
| 764 | std::make_pair(manifestInstance.instance(), manifestInstance.version())); |
| 765 | return true; |
| 766 | }); |
| 767 | return ret; |
| 768 | }; |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 769 | return checkDeprecation(inManifest, error); |
Yifan Hong | f73ba51 | 2018-01-17 15:52:30 -0800 | [diff] [blame] | 770 | } |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 771 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 772 | const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() { |
| 773 | return mFileSystem; |
| 774 | } |
| 775 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 776 | const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() { |
| 777 | return mPropertyFetcher; |
| 778 | } |
| 779 | |
Yifan Hong | d038b2b | 2018-11-27 13:57:56 -0800 | [diff] [blame] | 780 | const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 781 | return mRuntimeInfoFactory; |
Yifan Hong | 10d8622 | 2018-04-06 15:41:05 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Yifan Hong | 78f5b57 | 2018-11-27 14:05:03 -0800 | [diff] [blame] | 784 | // make_unique does not work because VintfObject constructor is private. |
| 785 | VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {} |
| 786 | |
| 787 | VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) { |
| 788 | mObject->mFileSystem = std::move(e); |
| 789 | return *this; |
| 790 | } |
| 791 | |
| 792 | VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory( |
| 793 | std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) { |
| 794 | mObject->mRuntimeInfoFactory = std::move(e); |
| 795 | return *this; |
| 796 | } |
| 797 | |
| 798 | VintfObject::Builder& VintfObject::Builder::setPropertyFetcher( |
| 799 | std::unique_ptr<PropertyFetcher>&& e) { |
| 800 | mObject->mPropertyFetcher = std::move(e); |
| 801 | return *this; |
| 802 | } |
| 803 | |
| 804 | std::unique_ptr<VintfObject> VintfObject::Builder::build() { |
| 805 | if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem(); |
| 806 | if (!mObject->mRuntimeInfoFactory) |
| 807 | mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>(); |
| 808 | if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher(); |
| 809 | return std::move(mObject); |
| 810 | } |
| 811 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 812 | } // namespace vintf |
| 813 | } // namespace android |