blob: bbd399b8d43968eeeb537407f795807c66ba0d51 [file] [log] [blame]
Yifan Hong3daec812017-02-27 18:49:11 -08001/*
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 Hongd52bf3e2018-01-11 16:56:51 -080019#include <dirent.h>
20
Yifan Hong03ea4282020-03-17 17:58:35 -070021#include <algorithm>
Yifan Hong3daec812017-02-27 18:49:11 -080022#include <functional>
23#include <memory>
24#include <mutex>
25
Yifan Hongd7043972020-03-30 13:27:46 -070026#include <aidl/metadata.h>
Yifan Hong60217032018-01-08 16:19:42 -080027#include <android-base/logging.h>
Yifan Hong6b860852020-03-19 23:12:07 +000028#include <android-base/result.h>
Yifan Hongb02d87d2019-12-19 11:15:27 -080029#include <android-base/strings.h>
Yifan Hongd7924ff2020-03-17 14:09:10 -070030#include <hidl/metadata.h>
Yifan Hong60217032018-01-08 16:19:42 -080031
Yifan Hong12e23c22018-11-05 14:53:30 -080032#include "CompatibilityMatrix.h"
Yifan Hongd7043972020-03-30 13:27:46 -070033#include "constants-private.h"
Yifan Hong12e23c22018-11-05 14:53:30 -080034#include "parse_string.h"
35#include "parse_xml.h"
36#include "utils.h"
37
Yifan Hong60217032018-01-08 16:19:42 -080038using std::placeholders::_1;
39using std::placeholders::_2;
Yifan Hongc7a9a292021-02-03 17:27:58 -080040using std::string_literals::operator""s;
Yifan Hong60217032018-01-08 16:19:42 -080041
Yifan Hong3daec812017-02-27 18:49:11 -080042namespace android {
43namespace vintf {
44
Yifan Hong270b5652018-01-18 18:46:01 -080045using namespace details;
46
Yifan Hong9f78c182018-07-12 14:45:52 -070047#ifdef LIBVINTF_TARGET
48static constexpr bool kIsTarget = true;
49#else
50static constexpr bool kIsTarget = false;
51#endif
Yifan Hong1fb004e2017-09-26 15:04:44 -070052
Yifan Hong3daec812017-02-27 18:49:11 -080053template <typename T, typename F>
Yifan Hong44f611c2020-07-21 11:57:57 -070054static std::shared_ptr<const T> Get(const char* id, LockedSharedPtr<T>* ptr,
Steven Morelandec0721d2020-04-30 15:48:35 -070055 const F& fetchAllInformation) {
Yifan Hong3daec812017-02-27 18:49:11 -080056 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong44f611c2020-07-21 11:57:57 -070057 if (!ptr->fetchedOnce) {
Steven Morelandec0721d2020-04-30 15:48:35 -070058 LOG(INFO) << id << ": Reading VINTF information.";
Yifan Hong3daec812017-02-27 18:49:11 -080059 ptr->object = std::make_unique<T>();
Yifan Hong60217032018-01-08 16:19:42 -080060 std::string error;
Steven Moreland609d7ff2020-03-27 15:48:40 -070061 status_t status = fetchAllInformation(ptr->object.get(), &error);
Steven Morelandec0721d2020-04-30 15:48:35 -070062 if (status == OK) {
Steven Moreland2cc413f2020-04-30 16:42:56 -070063 ptr->fetchedOnce = true;
Steven Morelandec0721d2020-04-30 15:48:35 -070064 LOG(INFO) << id << ": Successfully processed VINTF information";
65 } else {
66 // Doubled because a malformed error std::string might cause us to
67 // lose the status.
68 LOG(ERROR) << id << ": status from fetching VINTF information: " << status;
69 LOG(ERROR) << id << ": " << status << " VINTF parse error: " << error;
Yifan Hongd7043972020-03-30 13:27:46 -070070 ptr->object = nullptr; // frees the old object
Yifan Hong3daec812017-02-27 18:49:11 -080071 }
72 }
Yifan Hongfc73edf2017-08-29 11:39:07 -070073 return ptr->object;
Yifan Hong3daec812017-02-27 18:49:11 -080074}
75
Yifan Hong9f78c182018-07-12 14:45:52 -070076static std::unique_ptr<FileSystem> createDefaultFileSystem() {
77 std::unique_ptr<FileSystem> fileSystem;
78 if (kIsTarget) {
79 fileSystem = std::make_unique<details::FileSystemImpl>();
80 } else {
81 fileSystem = std::make_unique<details::FileSystemNoOp>();
82 }
83 return fileSystem;
84}
85
86static std::unique_ptr<PropertyFetcher> createDefaultPropertyFetcher() {
87 std::unique_ptr<PropertyFetcher> propertyFetcher;
88 if (kIsTarget) {
89 propertyFetcher = std::make_unique<details::PropertyFetcherImpl>();
90 } else {
91 propertyFetcher = std::make_unique<details::PropertyFetcherNoOp>();
92 }
93 return propertyFetcher;
94}
95
Yifan Hong9f78c182018-07-12 14:45:52 -070096std::shared_ptr<VintfObject> VintfObject::GetInstance() {
Steven Morelandc16ff2b2020-02-26 17:03:37 -080097 static details::LockedSharedPtr<VintfObject> sInstance{};
Yifan Hong9f78c182018-07-12 14:45:52 -070098 std::unique_lock<std::mutex> lock(sInstance.mutex);
99 if (sInstance.object == nullptr) {
Yifan Hong78f5b572018-11-27 14:05:03 -0800100 sInstance.object = std::shared_ptr<VintfObject>(VintfObject::Builder().build().release());
Yifan Hong9f78c182018-07-12 14:45:52 -0700101 }
102 return sInstance.object;
103}
104
Yifan Hong44f611c2020-07-21 11:57:57 -0700105std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest() {
106 return GetInstance()->getDeviceHalManifest();
Yifan Hong3daec812017-02-27 18:49:11 -0800107}
108
Yifan Hong44f611c2020-07-21 11:57:57 -0700109std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest() {
110 return Get(__func__, &mDeviceManifest,
Yifan Hong9f78c182018-07-12 14:45:52 -0700111 std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2));
112}
113
Yifan Hong44f611c2020-07-21 11:57:57 -0700114std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest() {
115 return GetInstance()->getFrameworkHalManifest();
Yifan Hong3daec812017-02-27 18:49:11 -0800116}
117
Yifan Hong44f611c2020-07-21 11:57:57 -0700118std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest() {
119 return Get(__func__, &mFrameworkManifest,
Yifan Hong9f78c182018-07-12 14:45:52 -0700120 std::bind(&VintfObject::fetchFrameworkHalManifest, this, _1, _2));
121}
Yifan Hong2272bf82017-04-28 14:37:56 -0700122
Yifan Hong44f611c2020-07-21 11:57:57 -0700123std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix() {
124 return GetInstance()->getDeviceCompatibilityMatrix();
Yifan Hong2272bf82017-04-28 14:37:56 -0700125}
126
Yifan Hong44f611c2020-07-21 11:57:57 -0700127std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix() {
128 return Get(__func__, &mDeviceMatrix, std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2));
Yifan Hong9f78c182018-07-12 14:45:52 -0700129}
130
Yifan Hong44f611c2020-07-21 11:57:57 -0700131std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix() {
132 return GetInstance()->getFrameworkCompatibilityMatrix();
Yifan Hong9f78c182018-07-12 14:45:52 -0700133}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800134
Yifan Hong44f611c2020-07-21 11:57:57 -0700135std::shared_ptr<const CompatibilityMatrix> VintfObject::getFrameworkCompatibilityMatrix() {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800136 // To avoid deadlock, get device manifest before any locks.
Yifan Hong9f78c182018-07-12 14:45:52 -0700137 auto deviceManifest = getDeviceHalManifest();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800138
Yifan Hong9f78c182018-07-12 14:45:52 -0700139 std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800140
141 auto combined =
Yifan Hong44f611c2020-07-21 11:57:57 -0700142 Get(__func__, &mCombinedFrameworkMatrix,
Yifan Hong9f78c182018-07-12 14:45:52 -0700143 std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest, _1, _2));
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800144 if (combined != nullptr) {
145 return combined;
146 }
147
Yifan Hong44f611c2020-07-21 11:57:57 -0700148 return Get(__func__, &mFrameworkMatrix,
Yifan Hong12e23c22018-11-05 14:53:30 -0800149 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(),
Yifan Hong9f78c182018-07-12 14:45:52 -0700150 kSystemLegacyMatrix, _2));
Yifan Hong2272bf82017-04-28 14:37:56 -0700151}
152
Yifan Hong9f78c182018-07-12 14:45:52 -0700153status_t VintfObject::getCombinedFrameworkMatrix(
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800154 const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out,
155 std::string* error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700156 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong73bde592019-01-22 13:30:23 -0800157 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
158 if (matrixFragmentsStatus != OK) {
159 return matrixFragmentsStatus;
160 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800161 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800162 if (error && error->empty()) {
163 *error = "Cannot get framework matrix for each FCM version for unknown error.";
164 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800165 return NAME_NOT_FOUND;
166 }
167
168 Level deviceLevel = Level::UNSPECIFIED;
169
170 if (deviceManifest != nullptr) {
171 deviceLevel = deviceManifest->level();
172 }
173
174 // TODO(b/70628538): Do not infer from Shipping API level.
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800175 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800176 auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800177 if (shippingApi != 0u) {
178 deviceLevel = details::convertFromApiLevel(shippingApi);
179 }
180 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800181
182 if (deviceLevel == Level::UNSPECIFIED) {
183 // Cannot infer FCM version. Combine all matrices by assuming
184 // Shipping FCM Version == min(all supported FCM Versions in the framework)
Yifan Honga83d0e42020-04-13 13:07:31 -0700185 for (auto&& fragment : matrixFragments) {
186 Level fragmentLevel = fragment.level();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800187 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
188 deviceLevel = fragmentLevel;
189 }
190 }
191 }
192
193 if (deviceLevel == Level::UNSPECIFIED) {
194 // None of the fragments specify any FCM version. Should never happen except
195 // for inconsistent builds.
196 if (error) {
Yifan Hongc7a9a292021-02-03 17:27:58 -0800197 *error = "No framework compatibility matrix files under "s + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800198 " declare FCM version.";
199 }
200 return NAME_NOT_FOUND;
201 }
202
Yifan Honge7837b12018-10-11 10:38:57 -0700203 auto combined = CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800204 if (combined == nullptr) {
205 return BAD_VALUE;
206 }
207 *out = std::move(*combined);
208 return OK;
209}
210
Steven Morelandeedf2d42018-04-04 16:36:27 -0700211// Load and combine all of the manifests in a directory
Yifan Hong9f78c182018-07-12 14:45:52 -0700212status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
Steven Morelandeedf2d42018-04-04 16:36:27 -0700213 std::string* error) {
214 std::vector<std::string> fileNames;
Yifan Hong12e23c22018-11-05 14:53:30 -0800215 status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700216 // if the directory isn't there, that's okay
217 if (err == NAME_NOT_FOUND) return OK;
218 if (err != OK) return err;
219
220 for (const std::string& file : fileNames) {
221 // Only adds HALs because all other things are added by libvintf
222 // itself for now.
223 HalManifest fragmentManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700224 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700225 if (err != OK) return err;
226
Yifan Hong4c6962d2019-01-30 15:50:46 -0800227 if (!manifest->addAll(&fragmentManifest, error)) {
228 if (error) {
Yifan Hong9f9a3192020-04-13 16:39:45 -0700229 error->insert(0, "Cannot add manifest fragment " + directory + file + ": ");
Yifan Hong4c6962d2019-01-30 15:50:46 -0800230 }
231 return UNKNOWN_ERROR;
232 }
Steven Morelandeedf2d42018-04-04 16:36:27 -0700233 }
234
235 return OK;
236}
237
Yifan Hong5a93bf22018-01-17 18:22:32 -0800238// Priority for loading vendor manifest:
Roopesh Natarajad629d382020-02-26 09:51:38 -0800239// 1. Vendor manifest + device fragments + ODM manifest (optional) + odm fragments
240// 2. Vendor manifest + device fragments
Steven Morelandeedf2d42018-04-04 16:36:27 -0700241// 3. ODM manifest (optional) + odm fragments
242// 4. /vendor/manifest.xml (legacy, no fragments)
Yifan Hong5a93bf22018-01-17 18:22:32 -0800243// where:
Steven Moreland30a532c2018-11-01 08:46:32 -0700244// A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
245// over A.
Yifan Hong9f78c182018-07-12 14:45:52 -0700246status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
Roopesh Natarajad629d382020-02-26 09:51:38 -0800247 HalManifest vendorManifest;
248 status_t vendorStatus = fetchVendorHalManifest(&vendorManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800249 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
250 return vendorStatus;
251 }
252
Steven Morelandeedf2d42018-04-04 16:36:27 -0700253 if (vendorStatus == OK) {
Roopesh Natarajad629d382020-02-26 09:51:38 -0800254 *out = std::move(vendorManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700255 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700256 if (fragmentStatus != OK) {
257 return fragmentStatus;
258 }
259 }
260
Yifan Hong5a93bf22018-01-17 18:22:32 -0800261 HalManifest odmManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700262 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800263 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
264 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800265 }
266
Yifan Hong5a93bf22018-01-17 18:22:32 -0800267 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800268 if (odmStatus == OK) {
Yifan Hong4c6962d2019-01-30 15:50:46 -0800269 if (!out->addAll(&odmManifest, error)) {
270 if (error) {
271 error->insert(0, "Cannot add ODM manifest :");
272 }
273 return UNKNOWN_ERROR;
274 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800275 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700276 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800277 }
278
Yifan Hong12a11ac2018-01-19 13:58:32 -0800279 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800280 if (odmStatus == OK) {
281 *out = std::move(odmManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700282 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800283 }
284
285 // Use legacy /vendor/manifest.xml
Yifan Hong12e23c22018-11-05 14:53:30 -0800286 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800287}
288
Roopesh Natarajad629d382020-02-26 09:51:38 -0800289// Priority:
290// 1. if {vendorSku} is defined, /vendor/etc/vintf/manifest_{vendorSku}.xml
291// 2. /vendor/etc/vintf/manifest.xml
292// where:
293// {vendorSku} is the value of ro.boot.product.vendor.sku
294status_t VintfObject::fetchVendorHalManifest(HalManifest* out, std::string* error) {
295 status_t status;
296
297 std::string vendorSku;
298 vendorSku = getPropertyFetcher()->getProperty("ro.boot.product.vendor.sku", "");
299
300 if (!vendorSku.empty()) {
301 status =
Yifan Hongc7a9a292021-02-03 17:27:58 -0800302 fetchOneHalManifest(kVendorVintfDir + "manifest_"s + vendorSku + ".xml", out, error);
Roopesh Natarajad629d382020-02-26 09:51:38 -0800303 if (status == OK || status != NAME_NOT_FOUND) {
304 return status;
305 }
306 }
307
308 status = fetchOneHalManifest(kVendorManifest, out, error);
309 if (status == OK || status != NAME_NOT_FOUND) {
310 return status;
311 }
312
313 return NAME_NOT_FOUND;
314}
315
Yifan Hong12a11ac2018-01-19 13:58:32 -0800316// "out" is written to iff return status is OK.
317// Priority:
318// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
319// 2. /odm/etc/vintf/manifest.xml
320// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
321// 4. /odm/etc/manifest.xml
322// where:
323// {sku} is the value of ro.boot.product.hardware.sku
Yifan Hong9f78c182018-07-12 14:45:52 -0700324status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800325 status_t status;
326
Yifan Hong12a11ac2018-01-19 13:58:32 -0800327 std::string productModel;
Yifan Hong12e23c22018-11-05 14:53:30 -0800328 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800329
330 if (!productModel.empty()) {
331 status =
Yifan Hongc7a9a292021-02-03 17:27:58 -0800332 fetchOneHalManifest(kOdmVintfDir + "manifest_"s + productModel + ".xml", out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800333 if (status == OK || status != NAME_NOT_FOUND) {
334 return status;
335 }
336 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800337
Yifan Hong9f78c182018-07-12 14:45:52 -0700338 status = fetchOneHalManifest(kOdmManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800339 if (status == OK || status != NAME_NOT_FOUND) {
340 return status;
341 }
342
Yifan Hong12a11ac2018-01-19 13:58:32 -0800343 if (!productModel.empty()) {
Yifan Hongc7a9a292021-02-03 17:27:58 -0800344 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_"s + productModel + ".xml", out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800345 error);
346 if (status == OK || status != NAME_NOT_FOUND) {
347 return status;
348 }
349 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800350
Yifan Hong9f78c182018-07-12 14:45:52 -0700351 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800352 if (status == OK || status != NAME_NOT_FOUND) {
353 return status;
354 }
355
356 return NAME_NOT_FOUND;
357}
358
359// Fetch one manifest.xml file. "out" is written to iff return status is OK.
360// Returns NAME_NOT_FOUND if file is missing.
Yifan Hong9f78c182018-07-12 14:45:52 -0700361status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800362 std::string* error) {
363 HalManifest ret;
Yifan Hong12e23c22018-11-05 14:53:30 -0800364 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800365 if (status == OK) {
366 *out = std::move(ret);
367 }
368 return status;
369}
370
Yifan Hong9f78c182018-07-12 14:45:52 -0700371status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800372 CompatibilityMatrix etcMatrix;
Yifan Hong12e23c22018-11-05 14:53:30 -0800373 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800374 *out = std::move(etcMatrix);
375 return OK;
376 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800377 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800378}
379
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700380// Priority:
381// 1. /system/etc/vintf/manifest.xml
382// + /system/etc/vintf/manifest/*.xml if they exist
383// + /product/etc/vintf/manifest.xml if it exists
384// + /product/etc/vintf/manifest/*.xml if they exist
385// 2. (deprecated) /system/manifest.xml
Yifan Hongc735be92020-10-12 16:25:50 -0700386status_t VintfObject::fetchUnfilteredFrameworkHalManifest(HalManifest* out, std::string* error) {
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700387 auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
388 if (systemEtcStatus == OK) {
389 auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out, error);
390 if (dirStatus != OK) {
391 return dirStatus;
392 }
393
Yifan Hongc7a9a292021-02-03 17:27:58 -0800394 std::vector<std::pair<const char*, const char*>> extensions{
Yifan Hong659feac2020-03-19 18:09:54 -0700395 {kProductManifest, kProductManifestFragmentDir},
396 {kSystemExtManifest, kSystemExtManifestFragmentDir},
397 };
398 for (auto&& [manifestPath, frags] : extensions) {
399 HalManifest halManifest;
400 auto status = fetchOneHalManifest(manifestPath, &halManifest, error);
401 if (status != OK && status != NAME_NOT_FOUND) {
402 return status;
403 }
404 if (status == OK) {
405 if (!out->addAll(&halManifest, error)) {
406 if (error) {
Yifan Hongc7a9a292021-02-03 17:27:58 -0800407 error->insert(0, "Cannot add "s + manifestPath + ":");
Yifan Hong659feac2020-03-19 18:09:54 -0700408 }
409 return UNKNOWN_ERROR;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700410 }
Yifan Hong659feac2020-03-19 18:09:54 -0700411 }
412
413 auto fragmentStatus = addDirectoryManifests(frags, out, error);
414 if (fragmentStatus != OK) {
415 return fragmentStatus;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700416 }
417 }
Yifan Hongc735be92020-10-12 16:25:50 -0700418
Yifan Hong659feac2020-03-19 18:09:54 -0700419 return OK;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700420 } else {
421 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
422 << (error ? *error : strerror(-systemEtcStatus));
Yifan Hongde6d00e2018-02-27 17:11:28 -0800423 }
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700424
Yifan Hong12e23c22018-11-05 14:53:30 -0800425 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800426}
427
Yifan Hongc735be92020-10-12 16:25:50 -0700428status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
429 status_t status = fetchUnfilteredFrameworkHalManifest(out, error);
430 if (status != OK) {
431 return status;
432 }
433 filterHalsByDeviceManifestLevel(out);
434 return OK;
435}
436
437void VintfObject::filterHalsByDeviceManifestLevel(HalManifest* out) {
438 auto deviceManifest = getDeviceHalManifest();
439 if (deviceManifest == nullptr) {
440 LOG(WARNING) << "Cannot fetch device manifest to determine target FCM version to "
441 "filter framework manifest HALs.";
442 return;
443 }
444 Level deviceManifestLevel = deviceManifest->level();
445 if (deviceManifestLevel == Level::UNSPECIFIED) {
446 LOG(WARNING)
447 << "Not filtering framework manifest HALs because target FCM version is unspecified.";
448 return;
449 }
450 out->removeHalsIf([deviceManifestLevel](const ManifestHal& hal) {
451 if (hal.getMaxLevel() == Level::UNSPECIFIED) {
452 return false;
453 }
454 return hal.getMaxLevel() < deviceManifestLevel;
455 });
456}
457
Yifan Hong9f8f6562018-11-06 16:26:20 -0800458static void appendLine(std::string* error, const std::string& message) {
459 if (error != nullptr) {
460 if (!error->empty()) *error += "\n";
461 *error += message;
462 }
463}
464
Yifan Honga83d0e42020-04-13 13:07:31 -0700465status_t VintfObject::getOneMatrix(const std::string& path, CompatibilityMatrix* out,
Yifan Hong73bde592019-01-22 13:30:23 -0800466 std::string* error) {
467 std::string content;
468 status_t status = getFileSystem()->fetch(path, &content, error);
469 if (status != OK) {
470 return status;
471 }
Yifan Honga83d0e42020-04-13 13:07:31 -0700472 if (!gCompatibilityMatrixConverter(out, content, error)) {
Yifan Hong73bde592019-01-22 13:30:23 -0800473 if (error) {
474 error->insert(0, "Cannot parse " + path + ": ");
475 }
476 return BAD_VALUE;
477 }
Yifan Honga83d0e42020-04-13 13:07:31 -0700478 out->setFileName(path);
Yifan Hong73bde592019-01-22 13:30:23 -0800479 return OK;
480}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800481
Yifan Honga83d0e42020-04-13 13:07:31 -0700482status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<CompatibilityMatrix>* results,
Yifan Hong73bde592019-01-22 13:30:23 -0800483 std::string* error) {
Yifan Hongb02d87d2019-12-19 11:15:27 -0800484 std::vector<std::string> dirs = {
485 kSystemVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800486 kSystemExtVintfDir,
Yifan Hongb02d87d2019-12-19 11:15:27 -0800487 kProductVintfDir,
488 };
489 for (const auto& dir : dirs) {
490 std::vector<std::string> fileNames;
491 status_t listStatus = getFileSystem()->listFiles(dir, &fileNames, error);
492 if (listStatus == NAME_NOT_FOUND) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800493 continue;
494 }
Yifan Hongb02d87d2019-12-19 11:15:27 -0800495 if (listStatus != OK) {
496 return listStatus;
497 }
498 for (const std::string& fileName : fileNames) {
499 std::string path = dir + fileName;
Yifan Honga83d0e42020-04-13 13:07:31 -0700500 CompatibilityMatrix namedMatrix;
Yifan Hongb02d87d2019-12-19 11:15:27 -0800501 std::string matrixError;
502 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
503 if (matrixStatus != OK) {
504 // Manifests and matrices share the same dir. Client may not have enough
505 // permissions to read system manifests, or may not be able to parse it.
506 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
507 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
508 continue;
509 }
510 results->emplace_back(std::move(namedMatrix));
511 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800512
Yifan Hongb02d87d2019-12-19 11:15:27 -0800513 if (dir == kSystemVintfDir && results->empty()) {
514 if (error) {
515 *error = "No framework matrices under " + dir + " can be fetched or parsed.\n";
516 }
517 return NAME_NOT_FOUND;
518 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800519 }
520
Yifan Hong73bde592019-01-22 13:30:23 -0800521 if (results->empty()) {
522 if (error) {
523 *error =
Yifan Hongb02d87d2019-12-19 11:15:27 -0800524 "No framework matrices can be fetched or parsed. "
525 "The following directories are searched:\n " +
526 android::base::Join(dirs, "\n ");
Yifan Hong73bde592019-01-22 13:30:23 -0800527 }
528 return NAME_NOT_FOUND;
529 }
530 return OK;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800531}
532
Yifan Honga192fa52020-07-21 12:09:20 -0700533std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(RuntimeInfo::FetchFlags flags) {
534 return GetInstance()->getRuntimeInfo(flags);
Yifan Hong9f78c182018-07-12 14:45:52 -0700535}
Yifan Honga192fa52020-07-21 12:09:20 -0700536std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700537 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700538
Yifan Honga192fa52020-07-21 12:09:20 -0700539 // Skip fetching information that has already been fetched previously.
540 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700541
Yifan Hong9f78c182018-07-12 14:45:52 -0700542 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800543 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700544 }
545
Yifan Hong9f78c182018-07-12 14:45:52 -0700546 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700547 if (status != OK) {
Yifan Hong7becb062020-11-13 15:54:58 -0800548 // If only kernel FCM is needed, ignore errors when fetching RuntimeInfo because RuntimeInfo
549 // is not available on host. On host, the kernel level can still be inferred from device
550 // manifest.
551 // If other information is needed, flag the error by returning nullptr.
552 auto allExceptKernelFcm = RuntimeInfo::FetchFlag::ALL & ~RuntimeInfo::FetchFlag::KERNEL_FCM;
553 bool needDeviceRuntimeInfo = flags & allExceptKernelFcm;
554 if (needDeviceRuntimeInfo) {
555 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
556 return nullptr;
557 }
558 }
559
560 // To support devices without GKI, RuntimeInfo::fetchAllInformation does not report errors
561 // if kernel level cannot be retrieved. If so, fetch kernel FCM version from device HAL
562 // manifest and store it in RuntimeInfo too.
563 if (flags & RuntimeInfo::FetchFlag::KERNEL_FCM) {
564 Level deviceManifestKernelLevel = Level::UNSPECIFIED;
565 auto manifest = getDeviceHalManifest();
566 if (manifest) {
567 deviceManifestKernelLevel = manifest->inferredKernelLevel();
568 }
569 if (deviceManifestKernelLevel != Level::UNSPECIFIED) {
570 Level kernelLevel = mDeviceRuntimeInfo.object->kernelLevel();
571 if (kernelLevel == Level::UNSPECIFIED) {
572 mDeviceRuntimeInfo.object->setKernelLevel(deviceManifestKernelLevel);
573 } else if (kernelLevel != deviceManifestKernelLevel) {
574 LOG(WARNING) << "uname() reports kernel level " << kernelLevel
575 << " but device manifest sets kernel level "
576 << deviceManifestKernelLevel << ". Using kernel level " << kernelLevel;
577 }
578 }
Yifan Hong1fb004e2017-09-26 15:04:44 -0700579 }
580
Yifan Hong9f78c182018-07-12 14:45:52 -0700581 mDeviceRuntimeInfo.fetchedFlags |= flags;
582 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800583}
584
Yifan Hong12e23c22018-11-05 14:53:30 -0800585int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
586 status_t status = OK;
587 // null checks for files and runtime info
588 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700589 appendLine(error, "No framework manifest file from device or from update package");
590 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700591 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800592 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700593 appendLine(error, "No device manifest file from device or from update package");
594 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700595 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800596 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700597 appendLine(error, "No framework matrix file from device or from update package");
598 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700599 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800600 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700601 appendLine(error, "No device matrix file from device or from update package");
602 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700603 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800604
Yifan Hong072f12d2018-08-08 13:04:51 -0700605 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800606 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700607 appendLine(error, "No runtime info from device");
608 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800609 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700610 }
Yifan Hong878556e2018-07-13 13:45:30 -0700611 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700612
613 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800614 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800615 if (error) {
616 error->insert(0,
617 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700618 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800619 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700620 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800621 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800622 if (error) {
623 error->insert(0,
624 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700625 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800626 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700627 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800628
Yifan Hong072f12d2018-08-08 13:04:51 -0700629 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800630 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
Yifan Hong85e589e2019-12-18 13:12:29 -0800631 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800632 if (error) {
633 error->insert(0,
634 "Runtime info and framework compatibility matrix are incompatible: ");
635 }
636 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700637 }
638 }
639
640 return COMPATIBLE;
641}
642
Yifan Hong9f78c182018-07-12 14:45:52 -0700643namespace details {
644
Yifan Hong69c1b112018-02-27 17:06:00 -0800645std::vector<std::string> dumpFileList() {
646 return {
Yifan Hong73bde592019-01-22 13:30:23 -0800647 // clang-format off
648 kSystemVintfDir,
649 kVendorVintfDir,
650 kOdmVintfDir,
651 kProductVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800652 kSystemExtVintfDir,
Yifan Hong73bde592019-01-22 13:30:23 -0800653 kOdmLegacyVintfDir,
654 kVendorLegacyManifest,
655 kVendorLegacyMatrix,
656 kSystemLegacyManifest,
657 kSystemLegacyMatrix,
658 // clang-format on
Yifan Hong69c1b112018-02-27 17:06:00 -0800659 };
660}
661
Yifan Hong9f78c182018-07-12 14:45:52 -0700662} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700663
Yifan Hong9f78c182018-07-12 14:45:52 -0700664bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800665 const CompatibilityMatrix& targetMatrix,
Yifan Hong03ea4282020-03-17 17:58:35 -0700666 const ListInstances& listInstances,
667 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700668 bool isDeprecated = false;
669 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700670 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, childrenMap,
671 appendedError)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700672 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800673 }
Yifan Hong03ea4282020-03-17 17:58:35 -0700674 return true; // continue to check next instance
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700675 });
676 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800677}
678
Yifan Hong03ea4282020-03-17 17:58:35 -0700679// Let oldMatrixInstance = package@x.y-w::interface/instancePattern.
680// If any "@servedVersion::interface/servedInstance" in listInstances(package@x.y::interface)
681// matches instancePattern, return true iff for all child interfaces (from
682// GetListedInstanceInheritance), IsFqInstanceDeprecated returns false.
Yifan Hong9f78c182018-07-12 14:45:52 -0700683bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800684 const CompatibilityMatrix& targetMatrix,
Yifan Hong03ea4282020-03-17 17:58:35 -0700685 const ListInstances& listInstances,
686 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700687 const std::string& package = oldMatrixInstance.package();
688 const Version& version = oldMatrixInstance.versionRange().minVer();
689 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700690
Yifan Honga8a8fa92018-03-20 14:42:43 -0700691 std::vector<std::string> instanceHint;
692 if (!oldMatrixInstance.isRegex()) {
693 instanceHint.push_back(oldMatrixInstance.exactInstance());
694 }
695
Yifan Hong03ea4282020-03-17 17:58:35 -0700696 std::vector<std::string> accumulatedErrors;
Yifan Honga8a8fa92018-03-20 14:42:43 -0700697 auto list = listInstances(package, version, interface, instanceHint);
Yifan Hong03ea4282020-03-17 17:58:35 -0700698
Yifan Honga8a8fa92018-03-20 14:42:43 -0700699 for (const auto& pair : list) {
700 const std::string& servedInstance = pair.first;
701 Version servedVersion = pair.second;
Yifan Hong03ea4282020-03-17 17:58:35 -0700702 std::string servedFqInstanceString =
703 toFQNameString(package, servedVersion, interface, servedInstance);
Yifan Honga8a8fa92018-03-20 14:42:43 -0700704 if (!oldMatrixInstance.matchInstance(servedInstance)) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700705 // ignore unrelated instance
Yifan Honga8a8fa92018-03-20 14:42:43 -0700706 continue;
707 }
708
Yifan Hong03ea4282020-03-17 17:58:35 -0700709 auto inheritance = GetListedInstanceInheritance(package, servedVersion, interface,
710 servedInstance, listInstances, childrenMap);
711 if (!inheritance.has_value()) {
712 accumulatedErrors.push_back(inheritance.error().message());
713 continue;
Yifan Hongf73ba512018-01-17 15:52:30 -0800714 }
715
Yifan Hong03ea4282020-03-17 17:58:35 -0700716 std::vector<std::string> errors;
717 for (const auto& fqInstance : *inheritance) {
718 auto result = IsFqInstanceDeprecated(targetMatrix, oldMatrixInstance.format(),
719 fqInstance, listInstances);
720 if (result.ok()) {
721 errors.clear();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700722 break;
723 }
Yifan Hong03ea4282020-03-17 17:58:35 -0700724 errors.push_back(result.error().message());
Yifan Honga8a8fa92018-03-20 14:42:43 -0700725 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800726
Yifan Hong03ea4282020-03-17 17:58:35 -0700727 if (errors.empty()) {
728 continue;
729 }
730 accumulatedErrors.insert(accumulatedErrors.end(), errors.begin(), errors.end());
731 }
732
733 if (accumulatedErrors.empty()) {
734 return false;
735 }
736 appendLine(appendedError, android::base::Join(accumulatedErrors, "\n"));
737 return true;
738}
739
740// Check if fqInstance is listed in |listInstances|.
741bool VintfObject::IsInstanceListed(const ListInstances& listInstances,
742 const FqInstance& fqInstance) {
743 auto list =
744 listInstances(fqInstance.getPackage(), fqInstance.getVersion(), fqInstance.getInterface(),
745 {fqInstance.getInstance()} /* instanceHint*/);
746 return std::any_of(list.begin(), list.end(),
747 [&](const auto& pair) { return pair.first == fqInstance.getInstance(); });
748}
749
750// Return a list of FqInstance, where each element:
751// - is listed in |listInstances|; AND
752// - is, or inherits from, package@version::interface/instance (as specified by |childrenMap|)
753android::base::Result<std::vector<FqInstance>> VintfObject::GetListedInstanceInheritance(
754 const std::string& package, const Version& version, const std::string& interface,
755 const std::string& instance, const ListInstances& listInstances,
756 const ChildrenMap& childrenMap) {
757 FqInstance fqInstance;
758 if (!fqInstance.setTo(package, version.majorVer, version.minorVer, interface, instance)) {
759 return android::base::Error() << toFQNameString(package, version, interface, instance)
760 << " is not a valid FqInstance";
761 }
762
763 if (!IsInstanceListed(listInstances, fqInstance)) {
764 return {};
765 }
766
767 const FQName& fqName = fqInstance.getFqName();
768
769 std::vector<FqInstance> ret;
770 ret.push_back(fqInstance);
771
772 auto childRange = childrenMap.equal_range(fqName.string());
773 for (auto it = childRange.first; it != childRange.second; ++it) {
774 const auto& childFqNameString = it->second;
775 FQName childFqName;
776 if (!childFqName.setTo(childFqNameString)) {
777 return android::base::Error() << "Cannot parse " << childFqNameString << " as FQName";
778 }
779 FqInstance childFqInstance;
780 if (!childFqInstance.setTo(childFqName, fqInstance.getInstance())) {
781 return android::base::Error() << "Cannot merge " << childFqName.string() << "/"
782 << fqInstance.getInstance() << " as FqInstance";
783 continue;
784 }
785 if (!IsInstanceListed(listInstances, childFqInstance)) {
786 continue;
787 }
788 ret.push_back(childFqInstance);
789 }
790 return ret;
791}
792
793// Check if |fqInstance| is in |targetMatrix|; essentially equal to
794// targetMatrix.matchInstance(fqInstance), but provides richer error message. In details:
795// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
796// 2. package@x.z::interface/servedInstance is in targetMatrix but
797// servedInstance is not in listInstances(package@x.z::interface)
798android::base::Result<void> VintfObject::IsFqInstanceDeprecated(
799 const CompatibilityMatrix& targetMatrix, HalFormat format, const FqInstance& fqInstance,
800 const ListInstances& listInstances) {
801 // Find minimum package@x.? in target matrix, and check if instance is in target matrix.
802 bool foundInstance = false;
803 Version targetMatrixMinVer{SIZE_MAX, SIZE_MAX};
804 targetMatrix.forEachInstanceOfPackage(
805 format, fqInstance.getPackage(), [&](const auto& targetMatrixInstance) {
806 if (targetMatrixInstance.versionRange().majorVer == fqInstance.getMajorVersion() &&
807 targetMatrixInstance.interface() == fqInstance.getInterface() &&
808 targetMatrixInstance.matchInstance(fqInstance.getInstance())) {
809 targetMatrixMinVer =
810 std::min(targetMatrixMinVer, targetMatrixInstance.versionRange().minVer());
811 foundInstance = true;
812 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800813 return true;
Yifan Hong03ea4282020-03-17 17:58:35 -0700814 });
815 if (!foundInstance) {
816 return android::base::Error()
817 << fqInstance.string() << " is deprecated in compatibility matrix at FCM Version "
818 << targetMatrix.level() << "; it should not be served.";
819 }
820
821 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
822 bool targetVersionServed = false;
823 for (const auto& newPair :
824 listInstances(fqInstance.getPackage(), targetMatrixMinVer, fqInstance.getInterface(),
825 {fqInstance.getInstance()} /* instanceHint */)) {
826 if (newPair.first == fqInstance.getInstance()) {
827 targetVersionServed = true;
828 break;
Yifan Hongf73ba512018-01-17 15:52:30 -0800829 }
830 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700831
Yifan Hong03ea4282020-03-17 17:58:35 -0700832 if (!targetVersionServed) {
833 return android::base::Error()
834 << fqInstance.string() << " is deprecated; requires at least " << targetMatrixMinVer;
835 }
836 return {};
Yifan Hongf73ba512018-01-17 15:52:30 -0800837}
838
Yifan Hong03ea4282020-03-17 17:58:35 -0700839int32_t VintfObject::checkDeprecation(const ListInstances& listInstances,
840 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
841 std::string* error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700842 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong73bde592019-01-22 13:30:23 -0800843 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
844 if (matrixFragmentsStatus != OK) {
845 return matrixFragmentsStatus;
846 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800847 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800848 if (error && error->empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800849 *error = "Cannot get framework matrix for each FCM version for unknown error.";
Yifan Hong73bde592019-01-22 13:30:23 -0800850 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800851 return NAME_NOT_FOUND;
852 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700853 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800854 if (deviceManifest == nullptr) {
855 if (error) *error = "No device manifest.";
856 return NAME_NOT_FOUND;
857 }
858 Level deviceLevel = deviceManifest->level();
859 if (deviceLevel == Level::UNSPECIFIED) {
860 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
861 return BAD_VALUE;
862 }
863
864 const CompatibilityMatrix* targetMatrix = nullptr;
865 for (const auto& namedMatrix : matrixFragments) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700866 if (namedMatrix.level() == deviceLevel) {
867 targetMatrix = &namedMatrix;
Yifan Hongf73ba512018-01-17 15:52:30 -0800868 }
869 }
870 if (targetMatrix == nullptr) {
871 if (error)
872 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
873 return NAME_NOT_FOUND;
874 }
875
Yifan Hong03ea4282020-03-17 17:58:35 -0700876 std::multimap<std::string, std::string> childrenMap;
877 for (const auto& child : hidlMetadata) {
878 for (const auto& parent : child.inherited) {
879 childrenMap.emplace(parent, child.name);
880 }
881 }
882
883 // Find a list of possibly deprecated HALs by comparing |listInstances| with older matrices.
884 // Matrices with unspecified level are considered "current".
885 bool isDeprecated = false;
Yifan Hongf73ba512018-01-17 15:52:30 -0800886 for (const auto& namedMatrix : matrixFragments) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700887 if (namedMatrix.level() == Level::UNSPECIFIED) continue;
888 if (namedMatrix.level() >= deviceLevel) continue;
Yifan Hongf73ba512018-01-17 15:52:30 -0800889
Yifan Honga83d0e42020-04-13 13:07:31 -0700890 for (const MatrixHal& hal : namedMatrix.getHals()) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700891 if (IsHalDeprecated(hal, *targetMatrix, listInstances, childrenMap, error)) {
892 isDeprecated = true;
893 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800894 }
895 }
896
Yifan Hong03ea4282020-03-17 17:58:35 -0700897 return isDeprecated ? DEPRECATED : NO_DEPRECATED_HALS;
Yifan Hongf73ba512018-01-17 15:52:30 -0800898}
899
Yifan Hong03ea4282020-03-17 17:58:35 -0700900int32_t VintfObject::checkDeprecation(const std::vector<HidlInterfaceMetadata>& hidlMetadata,
901 std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800902 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700903 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700904 ListInstances inManifest =
905 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
906 const std::vector<std::string>& /* hintInstances */) {
907 std::vector<std::pair<std::string, Version>> ret;
908 deviceManifest->forEachInstanceOfInterface(
Yifan Hongac621482019-09-10 19:27:20 -0700909 HalFormat::HIDL, package, version, interface,
910 [&ret](const ManifestInstance& manifestInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700911 ret.push_back(
912 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
913 return true;
914 });
915 return ret;
916 };
Yifan Hong03ea4282020-03-17 17:58:35 -0700917 return checkDeprecation(inManifest, hidlMetadata, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800918}
Yifan Hong3daec812017-02-27 18:49:11 -0800919
Yifan Hong378c4082019-12-23 13:10:57 -0800920Level VintfObject::getKernelLevel(std::string* error) {
Yifan Hong2d644112020-11-12 16:11:24 -0800921 auto runtimeInfo = getRuntimeInfo(RuntimeInfo::FetchFlag::KERNEL_FCM);
922 if (!runtimeInfo) {
923 if (error) *error = "Cannot retrieve runtime info with kernel level.";
Yifan Hong378c4082019-12-23 13:10:57 -0800924 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700925 }
Yifan Hong2d644112020-11-12 16:11:24 -0800926 if (runtimeInfo->kernelLevel() != Level::UNSPECIFIED) {
927 return runtimeInfo->kernelLevel();
Yifan Hong1e8febd2019-08-07 16:17:19 -0700928 }
Yifan Hong2d644112020-11-12 16:11:24 -0800929 if (error) {
930 *error = "Both device manifest and kernel release do not specify kernel FCM version.";
931 }
Yifan Hong378c4082019-12-23 13:10:57 -0800932 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700933}
934
Yifan Hong9f78c182018-07-12 14:45:52 -0700935const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
936 return mFileSystem;
937}
938
Yifan Hong9f78c182018-07-12 14:45:52 -0700939const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
940 return mPropertyFetcher;
941}
942
Yifan Hongd038b2b2018-11-27 13:57:56 -0800943const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700944 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700945}
946
Yifan Hong6b860852020-03-19 23:12:07 +0000947android::base::Result<bool> VintfObject::hasFrameworkCompatibilityMatrixExtensions() {
Yifan Honga83d0e42020-04-13 13:07:31 -0700948 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong6b860852020-03-19 23:12:07 +0000949 std::string error;
950 status_t status = getAllFrameworkMatrixLevels(&matrixFragments, &error);
951 if (status != OK) {
952 return android::base::Error(-status)
953 << "Cannot get all framework matrix fragments: " << error;
954 }
955 for (const auto& namedMatrix : matrixFragments) {
956 // Returns true if product matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700957 if (android::base::StartsWith(namedMatrix.fileName(), kProductVintfDir)) {
Yifan Hong6b860852020-03-19 23:12:07 +0000958 return true;
959 }
Yifan Hongf6ff4272020-03-12 22:56:16 -0700960 // Returns true if system_ext matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700961 if (android::base::StartsWith(namedMatrix.fileName(), kSystemExtVintfDir)) {
Yifan Hongf6ff4272020-03-12 22:56:16 -0700962 return true;
963 }
Yifan Hong6b860852020-03-19 23:12:07 +0000964 // Returns true if device system matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700965 if (android::base::StartsWith(namedMatrix.fileName(), kSystemVintfDir) &&
966 namedMatrix.level() == Level::UNSPECIFIED && !namedMatrix.getHals().empty()) {
Yifan Hong6b860852020-03-19 23:12:07 +0000967 return true;
968 }
969 }
970 return false;
971}
972
Yifan Hongd7924ff2020-03-17 14:09:10 -0700973android::base::Result<void> VintfObject::checkUnusedHals(
974 const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
Yifan Hong6b860852020-03-19 23:12:07 +0000975 auto matrix = getFrameworkCompatibilityMatrix();
976 if (matrix == nullptr) {
977 return android::base::Error(-NAME_NOT_FOUND) << "Missing framework matrix.";
978 }
979 auto manifest = getDeviceHalManifest();
980 if (manifest == nullptr) {
981 return android::base::Error(-NAME_NOT_FOUND) << "Missing device manifest.";
982 }
Yifan Hongd7924ff2020-03-17 14:09:10 -0700983 auto unused = manifest->checkUnusedHals(*matrix, hidlMetadata);
Yifan Hong6b860852020-03-19 23:12:07 +0000984 if (!unused.empty()) {
985 return android::base::Error()
986 << "The following instances are in the device manifest but "
987 << "not specified in framework compatibility matrix: \n"
988 << " " << android::base::Join(unused, "\n ") << "\n"
989 << "Suggested fix:\n"
Yifan Hong5a220b12020-04-07 15:22:24 -0700990 << "1. Update deprecated HALs to the latest version.\n"
991 << "2. Check for any typos in device manifest or framework compatibility "
Yifan Hong6b860852020-03-19 23:12:07 +0000992 << "matrices with FCM version >= " << matrix->level() << ".\n"
Yifan Hong5a220b12020-04-07 15:22:24 -0700993 << "3. For new platform HALs, add them to any framework compatibility matrix "
994 << "with FCM version >= " << matrix->level() << " where applicable.\n"
995 << "4. For device-specific HALs, add to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
Yifan Hong6b860852020-03-19 23:12:07 +0000996 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE.";
997 }
998 return {};
999}
1000
Yifan Hongd7043972020-03-30 13:27:46 -07001001namespace {
1002
1003// Insert |name| into |ret| if shouldCheck(name).
1004void InsertIf(const std::string& name, const std::function<bool(const std::string&)>& shouldCheck,
1005 std::set<std::string>* ret) {
1006 if (shouldCheck(name)) ret->insert(name);
1007}
1008
1009std::string StripHidlInterface(const std::string& fqNameString) {
1010 FQName fqName;
1011 if (!fqName.setTo(fqNameString)) {
1012 return "";
1013 }
1014 return fqName.getPackageAndVersion().string();
1015}
1016
1017std::string StripAidlType(const std::string& type) {
1018 auto items = android::base::Split(type, ".");
1019 if (items.empty()) {
1020 return "";
1021 }
1022 items.erase(items.end() - 1);
1023 return android::base::Join(items, ".");
1024}
1025
1026std::set<std::string> HidlMetadataToSet(
1027 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1028 const std::function<bool(const std::string&)>& shouldCheck) {
1029 std::set<std::string> ret;
1030 for (const auto& item : hidlMetadata) {
1031 InsertIf(StripHidlInterface(item.name), shouldCheck, &ret);
1032 }
1033 return ret;
1034}
1035
1036std::set<std::string> AidlMetadataToSet(
1037 const std::vector<AidlInterfaceMetadata>& aidlMetadata,
1038 const std::function<bool(const std::string&)>& shouldCheck) {
1039 std::set<std::string> ret;
1040 for (const auto& item : aidlMetadata) {
1041 for (const auto& type : item.types) {
1042 InsertIf(StripAidlType(type), shouldCheck, &ret);
1043 }
1044 }
1045 return ret;
1046}
1047
1048} // anonymous namespace
1049
1050android::base::Result<void> VintfObject::checkMissingHalsInMatrices(
1051 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1052 const std::vector<AidlInterfaceMetadata>& aidlMetadata,
1053 std::function<bool(const std::string&)> shouldCheck) {
1054 if (!shouldCheck) {
1055 shouldCheck = [](const auto&) { return true; };
1056 }
1057
1058 // Get all framework matrix fragments instead of the combined framework compatibility matrix
1059 // because the latter may omit interfaces from the latest FCM if device target-level is not
1060 // the latest.
1061 std::vector<CompatibilityMatrix> matrixFragments;
1062 std::string error;
1063 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, &error);
1064 if (matrixFragmentsStatus != OK) {
1065 return android::base::Error(-matrixFragmentsStatus)
1066 << "Unable to get all framework matrix fragments: " << error;
1067 }
1068 if (matrixFragments.empty()) {
1069 if (error.empty()) {
1070 error = "Cannot get framework matrix for each FCM version for unknown error.";
1071 }
1072 return android::base::Error(-NAME_NOT_FOUND) << error;
1073 }
1074
1075 // Filter aidlMetadata and hidlMetadata with shouldCheck.
1076 auto allAidlInterfaces = AidlMetadataToSet(aidlMetadata, shouldCheck);
1077 auto allHidlInterfaces = HidlMetadataToSet(hidlMetadata, shouldCheck);
1078
1079 // Filter out instances in allAidlMetadata and allHidlMetadata that are in the matrices.
1080 std::vector<std::string> errors;
1081 for (const auto& matrix : matrixFragments) {
1082 matrix.forEachInstance([&](const MatrixInstance& matrixInstance) {
1083 switch (matrixInstance.format()) {
1084 case HalFormat::AIDL: {
1085 allAidlInterfaces.erase(matrixInstance.package());
1086 return true; // continue to next instance
1087 }
1088 case HalFormat::HIDL: {
1089 for (Version v = matrixInstance.versionRange().minVer();
1090 v <= matrixInstance.versionRange().maxVer(); ++v.minorVer) {
1091 allHidlInterfaces.erase(toFQNameString(matrixInstance.package(), v));
1092 }
1093 return true; // continue to next instance
1094 }
1095 default: {
1096 if (shouldCheck(matrixInstance.package())) {
1097 errors.push_back("HAL package " + matrixInstance.package() +
1098 " is not allowed to have format " +
1099 to_string(matrixInstance.format()) + ".");
1100 }
1101 return true; // continue to next instance
1102 }
1103 }
1104 });
1105 }
1106
1107 if (!allHidlInterfaces.empty()) {
1108 errors.push_back(
1109 "The following HIDL packages are not found in any compatibility matrix fragments:\t\n" +
1110 android::base::Join(allHidlInterfaces, "\t\n"));
1111 }
1112 if (!allAidlInterfaces.empty()) {
1113 errors.push_back(
1114 "The following AIDL packages are not found in any compatibility matrix fragments:\t\n" +
1115 android::base::Join(allAidlInterfaces, "\t\n"));
1116 }
1117
1118 if (!errors.empty()) {
1119 return android::base::Error() << android::base::Join(errors, "\n");
1120 }
1121
1122 return {};
1123}
1124
Yifan Hong78f5b572018-11-27 14:05:03 -08001125// make_unique does not work because VintfObject constructor is private.
1126VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {}
1127
1128VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
1129 mObject->mFileSystem = std::move(e);
1130 return *this;
1131}
1132
1133VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory(
1134 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
1135 mObject->mRuntimeInfoFactory = std::move(e);
1136 return *this;
1137}
1138
1139VintfObject::Builder& VintfObject::Builder::setPropertyFetcher(
1140 std::unique_ptr<PropertyFetcher>&& e) {
1141 mObject->mPropertyFetcher = std::move(e);
1142 return *this;
1143}
1144
1145std::unique_ptr<VintfObject> VintfObject::Builder::build() {
1146 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
1147 if (!mObject->mRuntimeInfoFactory)
1148 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
1149 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
1150 return std::move(mObject);
1151}
1152
Yifan Hongd7043972020-03-30 13:27:46 -07001153} // namespace vintf
1154} // namespace android