blob: a19d3e1f3d73715c184764904ab1cca59d1f0b7f [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 Hongb712e7e2020-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 Hong60217032018-01-08 16:19:42 -080026#include <android-base/logging.h>
Yifan Hong6e32a5f2020-03-12 16:06:27 -070027#include <android-base/result.h>
Yifan Hongb02d87d2019-12-19 11:15:27 -080028#include <android-base/strings.h>
Yifan Hongff1a25c2020-03-17 14:09:10 -070029#include <hidl/metadata.h>
Yifan Hong60217032018-01-08 16:19:42 -080030
Yifan Hong12e23c22018-11-05 14:53:30 -080031#include "CompatibilityMatrix.h"
Yifan Hong12e23c22018-11-05 14:53:30 -080032#include "parse_string.h"
33#include "parse_xml.h"
34#include "utils.h"
35
Yifan Hong60217032018-01-08 16:19:42 -080036using std::placeholders::_1;
37using std::placeholders::_2;
38
Yifan Hong3daec812017-02-27 18:49:11 -080039namespace android {
40namespace vintf {
41
Yifan Hong270b5652018-01-18 18:46:01 -080042using namespace details;
43
Yifan Hong9f78c182018-07-12 14:45:52 -070044#ifdef LIBVINTF_TARGET
45static constexpr bool kIsTarget = true;
46#else
47static constexpr bool kIsTarget = false;
48#endif
Yifan Hong1fb004e2017-09-26 15:04:44 -070049
Yifan Hong3daec812017-02-27 18:49:11 -080050template <typename T, typename F>
Yifan Hongfc73edf2017-08-29 11:39:07 -070051static std::shared_ptr<const T> Get(
52 LockedSharedPtr<T> *ptr,
Yifan Hong143cfe62017-04-13 20:18:01 -070053 bool skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080054 const F &fetchAllInformation) {
55 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong7219ba12018-01-08 16:23:49 -080056 if (skipCache || !ptr->fetchedOnce) {
Yifan Hong3daec812017-02-27 18:49:11 -080057 ptr->object = std::make_unique<T>();
Yifan Hong60217032018-01-08 16:19:42 -080058 std::string error;
59 if (fetchAllInformation(ptr->object.get(), &error) != OK) {
60 LOG(WARNING) << error;
Yifan Hong3daec812017-02-27 18:49:11 -080061 ptr->object = nullptr; // frees the old object
62 }
Yifan Hong7219ba12018-01-08 16:23:49 -080063 ptr->fetchedOnce = true;
Yifan Hong3daec812017-02-27 18:49:11 -080064 }
Yifan Hongfc73edf2017-08-29 11:39:07 -070065 return ptr->object;
Yifan Hong3daec812017-02-27 18:49:11 -080066}
67
Yifan Hong9f78c182018-07-12 14:45:52 -070068static std::unique_ptr<FileSystem> createDefaultFileSystem() {
69 std::unique_ptr<FileSystem> fileSystem;
70 if (kIsTarget) {
71 fileSystem = std::make_unique<details::FileSystemImpl>();
72 } else {
73 fileSystem = std::make_unique<details::FileSystemNoOp>();
74 }
75 return fileSystem;
76}
77
78static std::unique_ptr<PropertyFetcher> createDefaultPropertyFetcher() {
79 std::unique_ptr<PropertyFetcher> propertyFetcher;
80 if (kIsTarget) {
81 propertyFetcher = std::make_unique<details::PropertyFetcherImpl>();
82 } else {
83 propertyFetcher = std::make_unique<details::PropertyFetcherNoOp>();
84 }
85 return propertyFetcher;
86}
87
Yifan Hong9f78c182018-07-12 14:45:52 -070088std::shared_ptr<VintfObject> VintfObject::GetInstance() {
Steven Moreland954c4642020-02-26 17:03:37 -080089 static details::LockedSharedPtr<VintfObject> sInstance{};
Yifan Hong9f78c182018-07-12 14:45:52 -070090 std::unique_lock<std::mutex> lock(sInstance.mutex);
91 if (sInstance.object == nullptr) {
Yifan Hong78f5b572018-11-27 14:05:03 -080092 sInstance.object = std::shared_ptr<VintfObject>(VintfObject::Builder().build().release());
Yifan Hong9f78c182018-07-12 14:45:52 -070093 }
94 return sInstance.object;
95}
96
Yifan Hongfc73edf2017-08-29 11:39:07 -070097std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -070098 return GetInstance()->getDeviceHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -080099}
100
Yifan Hong9f78c182018-07-12 14:45:52 -0700101std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest(bool skipCache) {
102 return Get(&mDeviceManifest, skipCache,
103 std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2));
104}
105
Yifan Hongfc73edf2017-08-29 11:39:07 -0700106std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700107 return GetInstance()->getFrameworkHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -0800108}
109
Yifan Hong9f78c182018-07-12 14:45:52 -0700110std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest(bool skipCache) {
111 return Get(&mFrameworkManifest, skipCache,
112 std::bind(&VintfObject::fetchFrameworkHalManifest, this, _1, _2));
113}
Yifan Hong2272bf82017-04-28 14:37:56 -0700114
Yifan Hongfc73edf2017-08-29 11:39:07 -0700115std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700116 return GetInstance()->getDeviceCompatibilityMatrix(skipCache);
Yifan Hong2272bf82017-04-28 14:37:56 -0700117}
118
Yifan Hong9f78c182018-07-12 14:45:52 -0700119std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix(
120 bool skipCache) {
121 return Get(&mDeviceMatrix, skipCache, std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2));
122}
123
Yifan Hongfc73edf2017-08-29 11:39:07 -0700124std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700125 return GetInstance()->getFrameworkCompatibilityMatrix(skipCache);
126}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800127
Yifan Hong9f78c182018-07-12 14:45:52 -0700128std::shared_ptr<const CompatibilityMatrix> VintfObject::getFrameworkCompatibilityMatrix(
129 bool skipCache) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800130 // To avoid deadlock, get device manifest before any locks.
Yifan Hong9f78c182018-07-12 14:45:52 -0700131 auto deviceManifest = getDeviceHalManifest();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800132
Yifan Hong9f78c182018-07-12 14:45:52 -0700133 std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800134
135 auto combined =
Yifan Hong9f78c182018-07-12 14:45:52 -0700136 Get(&mCombinedFrameworkMatrix, skipCache,
137 std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest, _1, _2));
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800138 if (combined != nullptr) {
139 return combined;
140 }
141
Yifan Hong9f78c182018-07-12 14:45:52 -0700142 return Get(&mFrameworkMatrix, skipCache,
Yifan Hong12e23c22018-11-05 14:53:30 -0800143 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(),
Yifan Hong9f78c182018-07-12 14:45:52 -0700144 kSystemLegacyMatrix, _2));
Yifan Hong2272bf82017-04-28 14:37:56 -0700145}
146
Yifan Hong9f78c182018-07-12 14:45:52 -0700147status_t VintfObject::getCombinedFrameworkMatrix(
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800148 const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out,
149 std::string* error) {
Yifan Hong73bde592019-01-22 13:30:23 -0800150 std::vector<Named<CompatibilityMatrix>> matrixFragments;
151 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
152 if (matrixFragmentsStatus != OK) {
153 return matrixFragmentsStatus;
154 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800155 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800156 if (error && error->empty()) {
157 *error = "Cannot get framework matrix for each FCM version for unknown error.";
158 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800159 return NAME_NOT_FOUND;
160 }
161
162 Level deviceLevel = Level::UNSPECIFIED;
163
164 if (deviceManifest != nullptr) {
165 deviceLevel = deviceManifest->level();
166 }
167
168 // TODO(b/70628538): Do not infer from Shipping API level.
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800169 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800170 auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800171 if (shippingApi != 0u) {
172 deviceLevel = details::convertFromApiLevel(shippingApi);
173 }
174 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800175
176 if (deviceLevel == Level::UNSPECIFIED) {
177 // Cannot infer FCM version. Combine all matrices by assuming
178 // Shipping FCM Version == min(all supported FCM Versions in the framework)
179 for (auto&& pair : matrixFragments) {
180 Level fragmentLevel = pair.object.level();
181 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
182 deviceLevel = fragmentLevel;
183 }
184 }
185 }
186
187 if (deviceLevel == Level::UNSPECIFIED) {
188 // None of the fragments specify any FCM version. Should never happen except
189 // for inconsistent builds.
190 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800191 *error = "No framework compatibility matrix files under " + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800192 " declare FCM version.";
193 }
194 return NAME_NOT_FOUND;
195 }
196
Yifan Honge7837b12018-10-11 10:38:57 -0700197 auto combined = CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800198 if (combined == nullptr) {
199 return BAD_VALUE;
200 }
201 *out = std::move(*combined);
202 return OK;
203}
204
Steven Morelandeedf2d42018-04-04 16:36:27 -0700205// Load and combine all of the manifests in a directory
Yifan Hong9f78c182018-07-12 14:45:52 -0700206status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
Steven Morelandeedf2d42018-04-04 16:36:27 -0700207 std::string* error) {
208 std::vector<std::string> fileNames;
Yifan Hong12e23c22018-11-05 14:53:30 -0800209 status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700210 // if the directory isn't there, that's okay
211 if (err == NAME_NOT_FOUND) return OK;
212 if (err != OK) return err;
213
214 for (const std::string& file : fileNames) {
215 // Only adds HALs because all other things are added by libvintf
216 // itself for now.
217 HalManifest fragmentManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700218 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700219 if (err != OK) return err;
220
Yifan Hong4c6962d2019-01-30 15:50:46 -0800221 if (!manifest->addAll(&fragmentManifest, error)) {
222 if (error) {
223 error->insert(0, "Cannot add manifest fragment " + directory + file + ":");
224 }
225 return UNKNOWN_ERROR;
226 }
Steven Morelandeedf2d42018-04-04 16:36:27 -0700227 }
228
229 return OK;
230}
231
Yifan Hong5a93bf22018-01-17 18:22:32 -0800232// Priority for loading vendor manifest:
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800233// 1. Vendor manifest + device fragments + ODM manifest (optional) + odm fragments
234// 2. Vendor manifest + device fragments
Steven Morelandeedf2d42018-04-04 16:36:27 -0700235// 3. ODM manifest (optional) + odm fragments
236// 4. /vendor/manifest.xml (legacy, no fragments)
Yifan Hong5a93bf22018-01-17 18:22:32 -0800237// where:
Steven Moreland30a532c2018-11-01 08:46:32 -0700238// A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
239// over A.
Yifan Hong9f78c182018-07-12 14:45:52 -0700240status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800241 HalManifest vendorManifest;
242 status_t vendorStatus = fetchVendorHalManifest(&vendorManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800243 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
244 return vendorStatus;
245 }
246
Steven Morelandeedf2d42018-04-04 16:36:27 -0700247 if (vendorStatus == OK) {
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800248 *out = std::move(vendorManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700249 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700250 if (fragmentStatus != OK) {
251 return fragmentStatus;
252 }
253 }
254
Yifan Hong5a93bf22018-01-17 18:22:32 -0800255 HalManifest odmManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700256 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800257 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
258 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800259 }
260
Yifan Hong5a93bf22018-01-17 18:22:32 -0800261 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800262 if (odmStatus == OK) {
Yifan Hong4c6962d2019-01-30 15:50:46 -0800263 if (!out->addAll(&odmManifest, error)) {
264 if (error) {
265 error->insert(0, "Cannot add ODM manifest :");
266 }
267 return UNKNOWN_ERROR;
268 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800269 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700270 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800271 }
272
Yifan Hong12a11ac2018-01-19 13:58:32 -0800273 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800274 if (odmStatus == OK) {
275 *out = std::move(odmManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700276 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800277 }
278
279 // Use legacy /vendor/manifest.xml
Yifan Hong12e23c22018-11-05 14:53:30 -0800280 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800281}
282
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800283// Priority:
284// 1. if {vendorSku} is defined, /vendor/etc/vintf/manifest_{vendorSku}.xml
285// 2. /vendor/etc/vintf/manifest.xml
286// where:
287// {vendorSku} is the value of ro.boot.product.vendor.sku
288status_t VintfObject::fetchVendorHalManifest(HalManifest* out, std::string* error) {
289 status_t status;
290
291 std::string vendorSku;
292 vendorSku = getPropertyFetcher()->getProperty("ro.boot.product.vendor.sku", "");
293
294 if (!vendorSku.empty()) {
295 status =
296 fetchOneHalManifest(kVendorVintfDir + "manifest_" + vendorSku + ".xml", out, error);
297 if (status == OK || status != NAME_NOT_FOUND) {
298 return status;
299 }
300 }
301
302 status = fetchOneHalManifest(kVendorManifest, out, error);
303 if (status == OK || status != NAME_NOT_FOUND) {
304 return status;
305 }
306
307 return NAME_NOT_FOUND;
308}
309
Yifan Hong12a11ac2018-01-19 13:58:32 -0800310// "out" is written to iff return status is OK.
311// Priority:
312// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
313// 2. /odm/etc/vintf/manifest.xml
314// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
315// 4. /odm/etc/manifest.xml
316// where:
317// {sku} is the value of ro.boot.product.hardware.sku
Yifan Hong9f78c182018-07-12 14:45:52 -0700318status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800319 status_t status;
320
Yifan Hong12a11ac2018-01-19 13:58:32 -0800321 std::string productModel;
Yifan Hong12e23c22018-11-05 14:53:30 -0800322 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800323
324 if (!productModel.empty()) {
325 status =
Yifan Hong9f78c182018-07-12 14:45:52 -0700326 fetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800327 if (status == OK || status != NAME_NOT_FOUND) {
328 return status;
329 }
330 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800331
Yifan Hong9f78c182018-07-12 14:45:52 -0700332 status = fetchOneHalManifest(kOdmManifest, 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 if (!productModel.empty()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700338 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800339 error);
340 if (status == OK || status != NAME_NOT_FOUND) {
341 return status;
342 }
343 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800344
Yifan Hong9f78c182018-07-12 14:45:52 -0700345 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800346 if (status == OK || status != NAME_NOT_FOUND) {
347 return status;
348 }
349
350 return NAME_NOT_FOUND;
351}
352
353// Fetch one manifest.xml file. "out" is written to iff return status is OK.
354// Returns NAME_NOT_FOUND if file is missing.
Yifan Hong9f78c182018-07-12 14:45:52 -0700355status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800356 std::string* error) {
357 HalManifest ret;
Yifan Hong12e23c22018-11-05 14:53:30 -0800358 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800359 if (status == OK) {
360 *out = std::move(ret);
361 }
362 return status;
363}
364
Yifan Hong9f78c182018-07-12 14:45:52 -0700365status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800366 CompatibilityMatrix etcMatrix;
Yifan Hong12e23c22018-11-05 14:53:30 -0800367 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800368 *out = std::move(etcMatrix);
369 return OK;
370 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800371 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800372}
373
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700374// Priority:
375// 1. /system/etc/vintf/manifest.xml
376// + /system/etc/vintf/manifest/*.xml if they exist
377// + /product/etc/vintf/manifest.xml if it exists
378// + /product/etc/vintf/manifest/*.xml if they exist
379// 2. (deprecated) /system/manifest.xml
Yifan Hong9f78c182018-07-12 14:45:52 -0700380status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700381 auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
382 if (systemEtcStatus == OK) {
383 auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out, error);
384 if (dirStatus != OK) {
385 return dirStatus;
386 }
387
Yifan Hongafb80be2020-03-19 18:09:54 -0700388 std::vector<std::pair<const std::string&, const std::string&>> extensions{
389 {kProductManifest, kProductManifestFragmentDir},
390 {kSystemExtManifest, kSystemExtManifestFragmentDir},
391 };
392 for (auto&& [manifestPath, frags] : extensions) {
393 HalManifest halManifest;
394 auto status = fetchOneHalManifest(manifestPath, &halManifest, error);
395 if (status != OK && status != NAME_NOT_FOUND) {
396 return status;
397 }
398 if (status == OK) {
399 if (!out->addAll(&halManifest, error)) {
400 if (error) {
401 error->insert(0, "Cannot add " + manifestPath + ":");
402 }
403 return UNKNOWN_ERROR;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700404 }
Yifan Hongafb80be2020-03-19 18:09:54 -0700405 }
406
407 auto fragmentStatus = addDirectoryManifests(frags, out, error);
408 if (fragmentStatus != OK) {
409 return fragmentStatus;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700410 }
411 }
Yifan Hongafb80be2020-03-19 18:09:54 -0700412 return OK;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700413 } else {
414 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
415 << (error ? *error : strerror(-systemEtcStatus));
Yifan Hongde6d00e2018-02-27 17:11:28 -0800416 }
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700417
Yifan Hong12e23c22018-11-05 14:53:30 -0800418 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800419}
420
Yifan Hong9f8f6562018-11-06 16:26:20 -0800421static void appendLine(std::string* error, const std::string& message) {
422 if (error != nullptr) {
423 if (!error->empty()) *error += "\n";
424 *error += message;
425 }
426}
427
Yifan Hong73bde592019-01-22 13:30:23 -0800428status_t VintfObject::getOneMatrix(const std::string& path, Named<CompatibilityMatrix>* out,
429 std::string* error) {
430 std::string content;
431 status_t status = getFileSystem()->fetch(path, &content, error);
432 if (status != OK) {
433 return status;
434 }
435 if (!gCompatibilityMatrixConverter(&out->object, content, error)) {
436 if (error) {
437 error->insert(0, "Cannot parse " + path + ": ");
438 }
439 return BAD_VALUE;
440 }
441 out->name = path;
442 return OK;
443}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800444
Yifan Hong73bde592019-01-22 13:30:23 -0800445status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<Named<CompatibilityMatrix>>* results,
446 std::string* error) {
Yifan Hongb02d87d2019-12-19 11:15:27 -0800447 std::vector<std::string> dirs = {
448 kSystemVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800449 kSystemExtVintfDir,
Yifan Hongb02d87d2019-12-19 11:15:27 -0800450 kProductVintfDir,
451 };
452 for (const auto& dir : dirs) {
453 std::vector<std::string> fileNames;
454 status_t listStatus = getFileSystem()->listFiles(dir, &fileNames, error);
455 if (listStatus == NAME_NOT_FOUND) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800456 continue;
457 }
Yifan Hongb02d87d2019-12-19 11:15:27 -0800458 if (listStatus != OK) {
459 return listStatus;
460 }
461 for (const std::string& fileName : fileNames) {
462 std::string path = dir + fileName;
463 Named<CompatibilityMatrix> namedMatrix;
464 std::string matrixError;
465 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
466 if (matrixStatus != OK) {
467 // Manifests and matrices share the same dir. Client may not have enough
468 // permissions to read system manifests, or may not be able to parse it.
469 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
470 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
471 continue;
472 }
473 results->emplace_back(std::move(namedMatrix));
474 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800475
Yifan Hongb02d87d2019-12-19 11:15:27 -0800476 if (dir == kSystemVintfDir && results->empty()) {
477 if (error) {
478 *error = "No framework matrices under " + dir + " can be fetched or parsed.\n";
479 }
480 return NAME_NOT_FOUND;
481 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800482 }
483
Yifan Hong73bde592019-01-22 13:30:23 -0800484 if (results->empty()) {
485 if (error) {
486 *error =
Yifan Hongb02d87d2019-12-19 11:15:27 -0800487 "No framework matrices can be fetched or parsed. "
488 "The following directories are searched:\n " +
489 android::base::Join(dirs, "\n ");
Yifan Hong73bde592019-01-22 13:30:23 -0800490 }
491 return NAME_NOT_FOUND;
492 }
493 return OK;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800494}
495
Yifan Hong1fb004e2017-09-26 15:04:44 -0700496std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
497 RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700498 return GetInstance()->getRuntimeInfo(skipCache, flags);
499}
500std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(bool skipCache,
501 RuntimeInfo::FetchFlags flags) {
502 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700503
504 if (!skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700505 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700506 }
507
Yifan Hong9f78c182018-07-12 14:45:52 -0700508 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800509 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700510 }
511
Yifan Hongf3247982019-12-12 12:11:36 -0800512 // Fetch kernel FCM version from device HAL manifest and store it in RuntimeInfo too.
513 if ((flags & RuntimeInfo::FetchFlag::KERNEL_FCM) != 0) {
514 auto manifest = getDeviceHalManifest();
515 if (!manifest) {
516 mDeviceRuntimeInfo.fetchedFlags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
517 return nullptr;
518 }
519 Level level = Level::UNSPECIFIED;
520 if (manifest->kernel().has_value()) {
521 level = manifest->kernel()->level();
522 }
523 mDeviceRuntimeInfo.object->setKernelLevel(level);
524 flags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
525 }
526
Yifan Hong9f78c182018-07-12 14:45:52 -0700527 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700528 if (status != OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700529 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
Yifan Hong1fb004e2017-09-26 15:04:44 -0700530 return nullptr;
531 }
532
Yifan Hong9f78c182018-07-12 14:45:52 -0700533 mDeviceRuntimeInfo.fetchedFlags |= flags;
534 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800535}
536
Yifan Hong12e23c22018-11-05 14:53:30 -0800537int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
538 status_t status = OK;
539 // null checks for files and runtime info
540 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700541 appendLine(error, "No framework manifest file from device or from update package");
542 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700543 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800544 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700545 appendLine(error, "No device manifest file from device or from update package");
546 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700547 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800548 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700549 appendLine(error, "No framework matrix file from device or from update package");
550 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700551 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800552 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700553 appendLine(error, "No device matrix file from device or from update package");
554 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700555 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800556
Yifan Hong072f12d2018-08-08 13:04:51 -0700557 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800558 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700559 appendLine(error, "No runtime info from device");
560 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800561 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700562 }
Yifan Hong878556e2018-07-13 13:45:30 -0700563 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700564
565 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800566 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800567 if (error) {
568 error->insert(0,
569 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700570 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800571 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700572 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800573 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800574 if (error) {
575 error->insert(0,
576 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700577 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800578 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700579 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800580
Yifan Hong072f12d2018-08-08 13:04:51 -0700581 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800582 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
Yifan Hong85e589e2019-12-18 13:12:29 -0800583 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800584 if (error) {
585 error->insert(0,
586 "Runtime info and framework compatibility matrix are incompatible: ");
587 }
588 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700589 }
590 }
591
592 return COMPATIBLE;
593}
594
Yifan Hong9f78c182018-07-12 14:45:52 -0700595namespace details {
596
Yifan Hong270b5652018-01-18 18:46:01 -0800597const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800598const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800599const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong73bde592019-01-22 13:30:23 -0800600const std::string kProductVintfDir = "/product/etc/vintf/";
Yifan Hong5bbc4ae2020-01-09 14:30:27 -0800601const std::string kSystemExtVintfDir = "/system_ext/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800602
603const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800604const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800605const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800606const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong73bde592019-01-22 13:30:23 -0800607const std::string kProductMatrix = kProductVintfDir + "compatibility_matrix.xml";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700608const std::string kProductManifest = kProductVintfDir + "manifest.xml";
Yifan Hongafb80be2020-03-19 18:09:54 -0700609const std::string kSystemExtManifest = kSystemExtVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800610
Steven Morelandeedf2d42018-04-04 16:36:27 -0700611const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/";
612const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/";
613const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700614const std::string kProductManifestFragmentDir = kProductVintfDir + "manifest/";
Yifan Hongafb80be2020-03-19 18:09:54 -0700615const std::string kSystemExtManifestFragmentDir = kSystemExtVintfDir + "manifest/";
Steven Morelandeedf2d42018-04-04 16:36:27 -0700616
Yifan Hong270b5652018-01-18 18:46:01 -0800617const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
618const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800619const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800620const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
621const std::string kOdmLegacyVintfDir = "/odm/etc/";
622const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
623
Yifan Hong69c1b112018-02-27 17:06:00 -0800624std::vector<std::string> dumpFileList() {
625 return {
Yifan Hong73bde592019-01-22 13:30:23 -0800626 // clang-format off
627 kSystemVintfDir,
628 kVendorVintfDir,
629 kOdmVintfDir,
630 kProductVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800631 kSystemExtVintfDir,
Yifan Hong73bde592019-01-22 13:30:23 -0800632 kOdmLegacyVintfDir,
633 kVendorLegacyManifest,
634 kVendorLegacyMatrix,
635 kSystemLegacyManifest,
636 kSystemLegacyMatrix,
637 // clang-format on
Yifan Hong69c1b112018-02-27 17:06:00 -0800638 };
639}
640
Yifan Hong9f78c182018-07-12 14:45:52 -0700641} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700642
Yifan Hong9f78c182018-07-12 14:45:52 -0700643bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800644 const CompatibilityMatrix& targetMatrix,
Yifan Hongb712e7e2020-03-17 17:58:35 -0700645 const ListInstances& listInstances,
646 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700647 bool isDeprecated = false;
648 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hongb712e7e2020-03-17 17:58:35 -0700649 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, childrenMap,
650 appendedError)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700651 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800652 }
Yifan Hongb712e7e2020-03-17 17:58:35 -0700653 return true; // continue to check next instance
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700654 });
655 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800656}
657
Yifan Hongb712e7e2020-03-17 17:58:35 -0700658// Let oldMatrixInstance = package@x.y-w::interface/instancePattern.
659// If any "@servedVersion::interface/servedInstance" in listInstances(package@x.y::interface)
660// matches instancePattern, return true iff for all child interfaces (from
661// GetListedInstanceInheritance), IsFqInstanceDeprecated returns false.
Yifan Hong9f78c182018-07-12 14:45:52 -0700662bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800663 const CompatibilityMatrix& targetMatrix,
Yifan Hongb712e7e2020-03-17 17:58:35 -0700664 const ListInstances& listInstances,
665 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700666 const std::string& package = oldMatrixInstance.package();
667 const Version& version = oldMatrixInstance.versionRange().minVer();
668 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700669
Yifan Honga8a8fa92018-03-20 14:42:43 -0700670 std::vector<std::string> instanceHint;
671 if (!oldMatrixInstance.isRegex()) {
672 instanceHint.push_back(oldMatrixInstance.exactInstance());
673 }
674
Yifan Hongb712e7e2020-03-17 17:58:35 -0700675 std::vector<std::string> accumulatedErrors;
Yifan Honga8a8fa92018-03-20 14:42:43 -0700676 auto list = listInstances(package, version, interface, instanceHint);
Yifan Hongb712e7e2020-03-17 17:58:35 -0700677
Yifan Honga8a8fa92018-03-20 14:42:43 -0700678 for (const auto& pair : list) {
679 const std::string& servedInstance = pair.first;
680 Version servedVersion = pair.second;
Yifan Hongb712e7e2020-03-17 17:58:35 -0700681 std::string servedFqInstanceString =
682 toFQNameString(package, servedVersion, interface, servedInstance);
Yifan Honga8a8fa92018-03-20 14:42:43 -0700683 if (!oldMatrixInstance.matchInstance(servedInstance)) {
Yifan Hongb712e7e2020-03-17 17:58:35 -0700684 // ignore unrelated instance
Yifan Honga8a8fa92018-03-20 14:42:43 -0700685 continue;
686 }
687
Yifan Hongb712e7e2020-03-17 17:58:35 -0700688 auto inheritance = GetListedInstanceInheritance(package, servedVersion, interface,
689 servedInstance, listInstances, childrenMap);
690 if (!inheritance.has_value()) {
691 accumulatedErrors.push_back(inheritance.error().message());
692 continue;
Yifan Hongf73ba512018-01-17 15:52:30 -0800693 }
694
Yifan Hongb712e7e2020-03-17 17:58:35 -0700695 std::vector<std::string> errors;
696 for (const auto& fqInstance : *inheritance) {
697 auto result = IsFqInstanceDeprecated(targetMatrix, oldMatrixInstance.format(),
698 fqInstance, listInstances);
699 if (result.ok()) {
700 errors.clear();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700701 break;
702 }
Yifan Hongb712e7e2020-03-17 17:58:35 -0700703 errors.push_back(result.error().message());
Yifan Honga8a8fa92018-03-20 14:42:43 -0700704 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800705
Yifan Hongb712e7e2020-03-17 17:58:35 -0700706 if (errors.empty()) {
707 continue;
708 }
709 accumulatedErrors.insert(accumulatedErrors.end(), errors.begin(), errors.end());
710 }
711
712 if (accumulatedErrors.empty()) {
713 return false;
714 }
715 appendLine(appendedError, android::base::Join(accumulatedErrors, "\n"));
716 return true;
717}
718
719// Check if fqInstance is listed in |listInstances|.
720bool VintfObject::IsInstanceListed(const ListInstances& listInstances,
721 const FqInstance& fqInstance) {
722 auto list =
723 listInstances(fqInstance.getPackage(), fqInstance.getVersion(), fqInstance.getInterface(),
724 {fqInstance.getInstance()} /* instanceHint*/);
725 return std::any_of(list.begin(), list.end(),
726 [&](const auto& pair) { return pair.first == fqInstance.getInstance(); });
727}
728
729// Return a list of FqInstance, where each element:
730// - is listed in |listInstances|; AND
731// - is, or inherits from, package@version::interface/instance (as specified by |childrenMap|)
732android::base::Result<std::vector<FqInstance>> VintfObject::GetListedInstanceInheritance(
733 const std::string& package, const Version& version, const std::string& interface,
734 const std::string& instance, const ListInstances& listInstances,
735 const ChildrenMap& childrenMap) {
736 FqInstance fqInstance;
737 if (!fqInstance.setTo(package, version.majorVer, version.minorVer, interface, instance)) {
738 return android::base::Error() << toFQNameString(package, version, interface, instance)
739 << " is not a valid FqInstance";
740 }
741
742 if (!IsInstanceListed(listInstances, fqInstance)) {
743 return {};
744 }
745
746 const FQName& fqName = fqInstance.getFqName();
747
748 std::vector<FqInstance> ret;
749 ret.push_back(fqInstance);
750
751 auto childRange = childrenMap.equal_range(fqName.string());
752 for (auto it = childRange.first; it != childRange.second; ++it) {
753 const auto& childFqNameString = it->second;
754 FQName childFqName;
755 if (!childFqName.setTo(childFqNameString)) {
756 return android::base::Error() << "Cannot parse " << childFqNameString << " as FQName";
757 }
758 FqInstance childFqInstance;
759 if (!childFqInstance.setTo(childFqName, fqInstance.getInstance())) {
760 return android::base::Error() << "Cannot merge " << childFqName.string() << "/"
761 << fqInstance.getInstance() << " as FqInstance";
762 continue;
763 }
764 if (!IsInstanceListed(listInstances, childFqInstance)) {
765 continue;
766 }
767 ret.push_back(childFqInstance);
768 }
769 return ret;
770}
771
772// Check if |fqInstance| is in |targetMatrix|; essentially equal to
773// targetMatrix.matchInstance(fqInstance), but provides richer error message. In details:
774// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
775// 2. package@x.z::interface/servedInstance is in targetMatrix but
776// servedInstance is not in listInstances(package@x.z::interface)
777android::base::Result<void> VintfObject::IsFqInstanceDeprecated(
778 const CompatibilityMatrix& targetMatrix, HalFormat format, const FqInstance& fqInstance,
779 const ListInstances& listInstances) {
780 // Find minimum package@x.? in target matrix, and check if instance is in target matrix.
781 bool foundInstance = false;
782 Version targetMatrixMinVer{SIZE_MAX, SIZE_MAX};
783 targetMatrix.forEachInstanceOfPackage(
784 format, fqInstance.getPackage(), [&](const auto& targetMatrixInstance) {
785 if (targetMatrixInstance.versionRange().majorVer == fqInstance.getMajorVersion() &&
786 targetMatrixInstance.interface() == fqInstance.getInterface() &&
787 targetMatrixInstance.matchInstance(fqInstance.getInstance())) {
788 targetMatrixMinVer =
789 std::min(targetMatrixMinVer, targetMatrixInstance.versionRange().minVer());
790 foundInstance = true;
791 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800792 return true;
Yifan Hongb712e7e2020-03-17 17:58:35 -0700793 });
794 if (!foundInstance) {
795 return android::base::Error()
796 << fqInstance.string() << " is deprecated in compatibility matrix at FCM Version "
797 << targetMatrix.level() << "; it should not be served.";
798 }
799
800 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
801 bool targetVersionServed = false;
802 for (const auto& newPair :
803 listInstances(fqInstance.getPackage(), targetMatrixMinVer, fqInstance.getInterface(),
804 {fqInstance.getInstance()} /* instanceHint */)) {
805 if (newPair.first == fqInstance.getInstance()) {
806 targetVersionServed = true;
807 break;
Yifan Hongf73ba512018-01-17 15:52:30 -0800808 }
809 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700810
Yifan Hongb712e7e2020-03-17 17:58:35 -0700811 if (!targetVersionServed) {
812 return android::base::Error()
813 << fqInstance.string() << " is deprecated; requires at least " << targetMatrixMinVer;
814 }
815 return {};
Yifan Hongf73ba512018-01-17 15:52:30 -0800816}
817
Yifan Hongb712e7e2020-03-17 17:58:35 -0700818int32_t VintfObject::checkDeprecation(const ListInstances& listInstances,
819 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
820 std::string* error) {
Yifan Hong73bde592019-01-22 13:30:23 -0800821 std::vector<Named<CompatibilityMatrix>> matrixFragments;
822 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
823 if (matrixFragmentsStatus != OK) {
824 return matrixFragmentsStatus;
825 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800826 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800827 if (error && error->empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800828 *error = "Cannot get framework matrix for each FCM version for unknown error.";
Yifan Hong73bde592019-01-22 13:30:23 -0800829 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800830 return NAME_NOT_FOUND;
831 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700832 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800833 if (deviceManifest == nullptr) {
834 if (error) *error = "No device manifest.";
835 return NAME_NOT_FOUND;
836 }
837 Level deviceLevel = deviceManifest->level();
838 if (deviceLevel == Level::UNSPECIFIED) {
839 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
840 return BAD_VALUE;
841 }
842
843 const CompatibilityMatrix* targetMatrix = nullptr;
844 for (const auto& namedMatrix : matrixFragments) {
845 if (namedMatrix.object.level() == deviceLevel) {
846 targetMatrix = &namedMatrix.object;
847 }
848 }
849 if (targetMatrix == nullptr) {
850 if (error)
851 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
852 return NAME_NOT_FOUND;
853 }
854
Yifan Hongb712e7e2020-03-17 17:58:35 -0700855 std::multimap<std::string, std::string> childrenMap;
856 for (const auto& child : hidlMetadata) {
857 for (const auto& parent : child.inherited) {
858 childrenMap.emplace(parent, child.name);
859 }
860 }
861
862 // Find a list of possibly deprecated HALs by comparing |listInstances| with older matrices.
863 // Matrices with unspecified level are considered "current".
864 bool isDeprecated = false;
Yifan Hongf73ba512018-01-17 15:52:30 -0800865 for (const auto& namedMatrix : matrixFragments) {
866 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
867 if (namedMatrix.object.level() >= deviceLevel) continue;
868
869 const auto& oldMatrix = namedMatrix.object;
870 for (const MatrixHal& hal : oldMatrix.getHals()) {
Yifan Hongb712e7e2020-03-17 17:58:35 -0700871 if (IsHalDeprecated(hal, *targetMatrix, listInstances, childrenMap, error)) {
872 isDeprecated = true;
873 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800874 }
875 }
876
Yifan Hongb712e7e2020-03-17 17:58:35 -0700877 return isDeprecated ? DEPRECATED : NO_DEPRECATED_HALS;
Yifan Hongf73ba512018-01-17 15:52:30 -0800878}
879
Yifan Hongb712e7e2020-03-17 17:58:35 -0700880int32_t VintfObject::checkDeprecation(const std::vector<HidlInterfaceMetadata>& hidlMetadata,
881 std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800882 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700883 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700884 ListInstances inManifest =
885 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
886 const std::vector<std::string>& /* hintInstances */) {
887 std::vector<std::pair<std::string, Version>> ret;
888 deviceManifest->forEachInstanceOfInterface(
Yifan Hongac621482019-09-10 19:27:20 -0700889 HalFormat::HIDL, package, version, interface,
890 [&ret](const ManifestInstance& manifestInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700891 ret.push_back(
892 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
893 return true;
894 });
895 return ret;
896 };
Yifan Hongb712e7e2020-03-17 17:58:35 -0700897 return checkDeprecation(inManifest, hidlMetadata, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800898}
Yifan Hong3daec812017-02-27 18:49:11 -0800899
Yifan Hong378c4082019-12-23 13:10:57 -0800900Level VintfObject::getKernelLevel(std::string* error) {
901 auto manifest = getDeviceHalManifest();
902 if (!manifest) {
903 if (error) *error = "Cannot retrieve device manifest.";
904 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700905 }
Yifan Hong378c4082019-12-23 13:10:57 -0800906 if (manifest->kernel().has_value() && manifest->kernel()->level() != Level::UNSPECIFIED) {
907 return manifest->kernel()->level();
Yifan Hong1e8febd2019-08-07 16:17:19 -0700908 }
Yifan Hong378c4082019-12-23 13:10:57 -0800909 if (error) *error = "Device manifest does not specify kernel FCM version.";
910 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700911}
912
Yifan Hong9f78c182018-07-12 14:45:52 -0700913const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
914 return mFileSystem;
915}
916
Yifan Hong9f78c182018-07-12 14:45:52 -0700917const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
918 return mPropertyFetcher;
919}
920
Yifan Hongd038b2b2018-11-27 13:57:56 -0800921const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700922 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700923}
924
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700925android::base::Result<bool> VintfObject::hasFrameworkCompatibilityMatrixExtensions() {
926 std::vector<Named<CompatibilityMatrix>> matrixFragments;
927 std::string error;
928 status_t status = getAllFrameworkMatrixLevels(&matrixFragments, &error);
929 if (status != OK) {
930 return android::base::Error(-status)
931 << "Cannot get all framework matrix fragments: " << error;
932 }
933 for (const auto& namedMatrix : matrixFragments) {
934 // Returns true if product matrix exists.
935 if (android::base::StartsWith(namedMatrix.name, kProductVintfDir)) {
936 return true;
937 }
Yifan Hong238c6dc2020-03-12 22:56:16 -0700938 // Returns true if system_ext matrix exists.
939 if (android::base::StartsWith(namedMatrix.name, kSystemExtVintfDir)) {
940 return true;
941 }
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700942 // Returns true if device system matrix exists.
943 if (android::base::StartsWith(namedMatrix.name, kSystemVintfDir) &&
944 namedMatrix.object.level() == Level::UNSPECIFIED &&
945 !namedMatrix.object.getHals().empty()) {
946 return true;
947 }
948 }
949 return false;
950}
951
Yifan Hongff1a25c2020-03-17 14:09:10 -0700952android::base::Result<void> VintfObject::checkUnusedHals(
953 const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700954 auto matrix = getFrameworkCompatibilityMatrix();
955 if (matrix == nullptr) {
956 return android::base::Error(-NAME_NOT_FOUND) << "Missing framework matrix.";
957 }
958 auto manifest = getDeviceHalManifest();
959 if (manifest == nullptr) {
960 return android::base::Error(-NAME_NOT_FOUND) << "Missing device manifest.";
961 }
Yifan Hongff1a25c2020-03-17 14:09:10 -0700962 auto unused = manifest->checkUnusedHals(*matrix, hidlMetadata);
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700963 if (!unused.empty()) {
964 return android::base::Error()
965 << "The following instances are in the device manifest but "
966 << "not specified in framework compatibility matrix: \n"
967 << " " << android::base::Join(unused, "\n ") << "\n"
968 << "Suggested fix:\n"
969 << "1. Check for any typos in device manifest or framework compatibility "
970 << "matrices with FCM version >= " << matrix->level() << ".\n"
971 << "2. Add them to any framework compatibility matrix with FCM "
972 << "version >= " << matrix->level() << " where applicable.\n"
973 << "3. Add them to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
974 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE.";
975 }
976 return {};
977}
978
Yifan Hong78f5b572018-11-27 14:05:03 -0800979// make_unique does not work because VintfObject constructor is private.
980VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {}
981
982VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
983 mObject->mFileSystem = std::move(e);
984 return *this;
985}
986
987VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory(
988 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
989 mObject->mRuntimeInfoFactory = std::move(e);
990 return *this;
991}
992
993VintfObject::Builder& VintfObject::Builder::setPropertyFetcher(
994 std::unique_ptr<PropertyFetcher>&& e) {
995 mObject->mPropertyFetcher = std::move(e);
996 return *this;
997}
998
999std::unique_ptr<VintfObject> VintfObject::Builder::build() {
1000 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
1001 if (!mObject->mRuntimeInfoFactory)
1002 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
1003 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
1004 return std::move(mObject);
1005}
1006
Yifan Hong3daec812017-02-27 18:49:11 -08001007} // namespace vintf
1008} // namespace android