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