blob: 115081fe40065173d7dae21d07b0c8e276d2b7e3 [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
388 HalManifest productManifest;
389 auto productStatus = fetchOneHalManifest(kProductManifest, &productManifest, error);
390 if (productStatus != OK && productStatus != NAME_NOT_FOUND) {
391 return productStatus;
392 }
393 if (productStatus == OK) {
394 if (!out->addAll(&productManifest, error)) {
395 if (error) {
396 error->insert(0, "Cannot add " + kProductManifest + ":");
397 }
398 return UNKNOWN_ERROR;
399 }
400 }
401
402 return addDirectoryManifests(kProductManifestFragmentDir, out, error);
403 } else {
404 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
405 << (error ? *error : strerror(-systemEtcStatus));
Yifan Hongde6d00e2018-02-27 17:11:28 -0800406 }
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700407
Yifan Hong12e23c22018-11-05 14:53:30 -0800408 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800409}
410
Yifan Hong9f8f6562018-11-06 16:26:20 -0800411static void appendLine(std::string* error, const std::string& message) {
412 if (error != nullptr) {
413 if (!error->empty()) *error += "\n";
414 *error += message;
415 }
416}
417
Yifan Hong73bde592019-01-22 13:30:23 -0800418status_t VintfObject::getOneMatrix(const std::string& path, Named<CompatibilityMatrix>* out,
419 std::string* error) {
420 std::string content;
421 status_t status = getFileSystem()->fetch(path, &content, error);
422 if (status != OK) {
423 return status;
424 }
425 if (!gCompatibilityMatrixConverter(&out->object, content, error)) {
426 if (error) {
427 error->insert(0, "Cannot parse " + path + ": ");
428 }
429 return BAD_VALUE;
430 }
431 out->name = path;
432 return OK;
433}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800434
Yifan Hong73bde592019-01-22 13:30:23 -0800435status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<Named<CompatibilityMatrix>>* results,
436 std::string* error) {
Yifan Hongb02d87d2019-12-19 11:15:27 -0800437 std::vector<std::string> dirs = {
438 kSystemVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800439 kSystemExtVintfDir,
Yifan Hongb02d87d2019-12-19 11:15:27 -0800440 kProductVintfDir,
441 };
442 for (const auto& dir : dirs) {
443 std::vector<std::string> fileNames;
444 status_t listStatus = getFileSystem()->listFiles(dir, &fileNames, error);
445 if (listStatus == NAME_NOT_FOUND) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800446 continue;
447 }
Yifan Hongb02d87d2019-12-19 11:15:27 -0800448 if (listStatus != OK) {
449 return listStatus;
450 }
451 for (const std::string& fileName : fileNames) {
452 std::string path = dir + fileName;
453 Named<CompatibilityMatrix> namedMatrix;
454 std::string matrixError;
455 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
456 if (matrixStatus != OK) {
457 // Manifests and matrices share the same dir. Client may not have enough
458 // permissions to read system manifests, or may not be able to parse it.
459 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
460 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
461 continue;
462 }
463 results->emplace_back(std::move(namedMatrix));
464 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800465
Yifan Hongb02d87d2019-12-19 11:15:27 -0800466 if (dir == kSystemVintfDir && results->empty()) {
467 if (error) {
468 *error = "No framework matrices under " + dir + " can be fetched or parsed.\n";
469 }
470 return NAME_NOT_FOUND;
471 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800472 }
473
Yifan Hong73bde592019-01-22 13:30:23 -0800474 if (results->empty()) {
475 if (error) {
476 *error =
Yifan Hongb02d87d2019-12-19 11:15:27 -0800477 "No framework matrices can be fetched or parsed. "
478 "The following directories are searched:\n " +
479 android::base::Join(dirs, "\n ");
Yifan Hong73bde592019-01-22 13:30:23 -0800480 }
481 return NAME_NOT_FOUND;
482 }
483 return OK;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800484}
485
Yifan Hong1fb004e2017-09-26 15:04:44 -0700486std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
487 RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700488 return GetInstance()->getRuntimeInfo(skipCache, flags);
489}
490std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(bool skipCache,
491 RuntimeInfo::FetchFlags flags) {
492 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700493
494 if (!skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700495 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700496 }
497
Yifan Hong9f78c182018-07-12 14:45:52 -0700498 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800499 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700500 }
501
Yifan Hongf3247982019-12-12 12:11:36 -0800502 // Fetch kernel FCM version from device HAL manifest and store it in RuntimeInfo too.
503 if ((flags & RuntimeInfo::FetchFlag::KERNEL_FCM) != 0) {
504 auto manifest = getDeviceHalManifest();
505 if (!manifest) {
506 mDeviceRuntimeInfo.fetchedFlags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
507 return nullptr;
508 }
509 Level level = Level::UNSPECIFIED;
510 if (manifest->kernel().has_value()) {
511 level = manifest->kernel()->level();
512 }
513 mDeviceRuntimeInfo.object->setKernelLevel(level);
514 flags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
515 }
516
Yifan Hong9f78c182018-07-12 14:45:52 -0700517 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700518 if (status != OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700519 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
Yifan Hong1fb004e2017-09-26 15:04:44 -0700520 return nullptr;
521 }
522
Yifan Hong9f78c182018-07-12 14:45:52 -0700523 mDeviceRuntimeInfo.fetchedFlags |= flags;
524 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800525}
526
Yifan Hong12e23c22018-11-05 14:53:30 -0800527int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
528 status_t status = OK;
529 // null checks for files and runtime info
530 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700531 appendLine(error, "No framework manifest file from device or from update package");
532 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700533 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800534 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700535 appendLine(error, "No device manifest file from device or from update package");
536 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700537 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800538 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700539 appendLine(error, "No framework matrix file from device or from update package");
540 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700541 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800542 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700543 appendLine(error, "No device matrix file from device or from update package");
544 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700545 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800546
Yifan Hong072f12d2018-08-08 13:04:51 -0700547 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800548 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700549 appendLine(error, "No runtime info from device");
550 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800551 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700552 }
Yifan Hong878556e2018-07-13 13:45:30 -0700553 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700554
555 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800556 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800557 if (error) {
558 error->insert(0,
559 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700560 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800561 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700562 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800563 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800564 if (error) {
565 error->insert(0,
566 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700567 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800568 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700569 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800570
Yifan Hong072f12d2018-08-08 13:04:51 -0700571 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800572 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
Yifan Hong85e589e2019-12-18 13:12:29 -0800573 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800574 if (error) {
575 error->insert(0,
576 "Runtime info and framework compatibility matrix are incompatible: ");
577 }
578 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700579 }
580 }
581
582 return COMPATIBLE;
583}
584
Yifan Hong9f78c182018-07-12 14:45:52 -0700585namespace details {
586
Yifan Hong270b5652018-01-18 18:46:01 -0800587const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800588const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800589const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong73bde592019-01-22 13:30:23 -0800590const std::string kProductVintfDir = "/product/etc/vintf/";
Yifan Hong5bbc4ae2020-01-09 14:30:27 -0800591const std::string kSystemExtVintfDir = "/system_ext/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800592
593const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800594const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800595const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800596const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong73bde592019-01-22 13:30:23 -0800597const std::string kProductMatrix = kProductVintfDir + "compatibility_matrix.xml";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700598const std::string kProductManifest = kProductVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800599
Steven Morelandeedf2d42018-04-04 16:36:27 -0700600const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/";
601const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/";
602const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700603const std::string kProductManifestFragmentDir = kProductVintfDir + "manifest/";
Steven Morelandeedf2d42018-04-04 16:36:27 -0700604
Yifan Hong270b5652018-01-18 18:46:01 -0800605const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
606const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800607const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800608const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
609const std::string kOdmLegacyVintfDir = "/odm/etc/";
610const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
611
Yifan Hong69c1b112018-02-27 17:06:00 -0800612std::vector<std::string> dumpFileList() {
613 return {
Yifan Hong73bde592019-01-22 13:30:23 -0800614 // clang-format off
615 kSystemVintfDir,
616 kVendorVintfDir,
617 kOdmVintfDir,
618 kProductVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800619 kSystemExtVintfDir,
Yifan Hong73bde592019-01-22 13:30:23 -0800620 kOdmLegacyVintfDir,
621 kVendorLegacyManifest,
622 kVendorLegacyMatrix,
623 kSystemLegacyManifest,
624 kSystemLegacyMatrix,
625 // clang-format on
Yifan Hong69c1b112018-02-27 17:06:00 -0800626 };
627}
628
Yifan Hong9f78c182018-07-12 14:45:52 -0700629} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700630
Yifan Hong9f78c182018-07-12 14:45:52 -0700631bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800632 const CompatibilityMatrix& targetMatrix,
Yifan Hongb712e7e2020-03-17 17:58:35 -0700633 const ListInstances& listInstances,
634 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700635 bool isDeprecated = false;
636 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hongb712e7e2020-03-17 17:58:35 -0700637 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, childrenMap,
638 appendedError)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700639 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800640 }
Yifan Hongb712e7e2020-03-17 17:58:35 -0700641 return true; // continue to check next instance
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700642 });
643 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800644}
645
Yifan Hongb712e7e2020-03-17 17:58:35 -0700646// Let oldMatrixInstance = package@x.y-w::interface/instancePattern.
647// If any "@servedVersion::interface/servedInstance" in listInstances(package@x.y::interface)
648// matches instancePattern, return true iff for all child interfaces (from
649// GetListedInstanceInheritance), IsFqInstanceDeprecated returns false.
Yifan Hong9f78c182018-07-12 14:45:52 -0700650bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800651 const CompatibilityMatrix& targetMatrix,
Yifan Hongb712e7e2020-03-17 17:58:35 -0700652 const ListInstances& listInstances,
653 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700654 const std::string& package = oldMatrixInstance.package();
655 const Version& version = oldMatrixInstance.versionRange().minVer();
656 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700657
Yifan Honga8a8fa92018-03-20 14:42:43 -0700658 std::vector<std::string> instanceHint;
659 if (!oldMatrixInstance.isRegex()) {
660 instanceHint.push_back(oldMatrixInstance.exactInstance());
661 }
662
Yifan Hongb712e7e2020-03-17 17:58:35 -0700663 std::vector<std::string> accumulatedErrors;
Yifan Honga8a8fa92018-03-20 14:42:43 -0700664 auto list = listInstances(package, version, interface, instanceHint);
Yifan Hongb712e7e2020-03-17 17:58:35 -0700665
Yifan Honga8a8fa92018-03-20 14:42:43 -0700666 for (const auto& pair : list) {
667 const std::string& servedInstance = pair.first;
668 Version servedVersion = pair.second;
Yifan Hongb712e7e2020-03-17 17:58:35 -0700669 std::string servedFqInstanceString =
670 toFQNameString(package, servedVersion, interface, servedInstance);
Yifan Honga8a8fa92018-03-20 14:42:43 -0700671 if (!oldMatrixInstance.matchInstance(servedInstance)) {
Yifan Hongb712e7e2020-03-17 17:58:35 -0700672 // ignore unrelated instance
Yifan Honga8a8fa92018-03-20 14:42:43 -0700673 continue;
674 }
675
Yifan Hongb712e7e2020-03-17 17:58:35 -0700676 auto inheritance = GetListedInstanceInheritance(package, servedVersion, interface,
677 servedInstance, listInstances, childrenMap);
678 if (!inheritance.has_value()) {
679 accumulatedErrors.push_back(inheritance.error().message());
680 continue;
Yifan Hongf73ba512018-01-17 15:52:30 -0800681 }
682
Yifan Hongb712e7e2020-03-17 17:58:35 -0700683 std::vector<std::string> errors;
684 for (const auto& fqInstance : *inheritance) {
685 auto result = IsFqInstanceDeprecated(targetMatrix, oldMatrixInstance.format(),
686 fqInstance, listInstances);
687 if (result.ok()) {
688 errors.clear();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700689 break;
690 }
Yifan Hongb712e7e2020-03-17 17:58:35 -0700691 errors.push_back(result.error().message());
Yifan Honga8a8fa92018-03-20 14:42:43 -0700692 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800693
Yifan Hongb712e7e2020-03-17 17:58:35 -0700694 if (errors.empty()) {
695 continue;
696 }
697 accumulatedErrors.insert(accumulatedErrors.end(), errors.begin(), errors.end());
698 }
699
700 if (accumulatedErrors.empty()) {
701 return false;
702 }
703 appendLine(appendedError, android::base::Join(accumulatedErrors, "\n"));
704 return true;
705}
706
707// Check if fqInstance is listed in |listInstances|.
708bool VintfObject::IsInstanceListed(const ListInstances& listInstances,
709 const FqInstance& fqInstance) {
710 auto list =
711 listInstances(fqInstance.getPackage(), fqInstance.getVersion(), fqInstance.getInterface(),
712 {fqInstance.getInstance()} /* instanceHint*/);
713 return std::any_of(list.begin(), list.end(),
714 [&](const auto& pair) { return pair.first == fqInstance.getInstance(); });
715}
716
717// Return a list of FqInstance, where each element:
718// - is listed in |listInstances|; AND
719// - is, or inherits from, package@version::interface/instance (as specified by |childrenMap|)
720android::base::Result<std::vector<FqInstance>> VintfObject::GetListedInstanceInheritance(
721 const std::string& package, const Version& version, const std::string& interface,
722 const std::string& instance, const ListInstances& listInstances,
723 const ChildrenMap& childrenMap) {
724 FqInstance fqInstance;
725 if (!fqInstance.setTo(package, version.majorVer, version.minorVer, interface, instance)) {
726 return android::base::Error() << toFQNameString(package, version, interface, instance)
727 << " is not a valid FqInstance";
728 }
729
730 if (!IsInstanceListed(listInstances, fqInstance)) {
731 return {};
732 }
733
734 const FQName& fqName = fqInstance.getFqName();
735
736 std::vector<FqInstance> ret;
737 ret.push_back(fqInstance);
738
739 auto childRange = childrenMap.equal_range(fqName.string());
740 for (auto it = childRange.first; it != childRange.second; ++it) {
741 const auto& childFqNameString = it->second;
742 FQName childFqName;
743 if (!childFqName.setTo(childFqNameString)) {
744 return android::base::Error() << "Cannot parse " << childFqNameString << " as FQName";
745 }
746 FqInstance childFqInstance;
747 if (!childFqInstance.setTo(childFqName, fqInstance.getInstance())) {
748 return android::base::Error() << "Cannot merge " << childFqName.string() << "/"
749 << fqInstance.getInstance() << " as FqInstance";
750 continue;
751 }
752 if (!IsInstanceListed(listInstances, childFqInstance)) {
753 continue;
754 }
755 ret.push_back(childFqInstance);
756 }
757 return ret;
758}
759
760// Check if |fqInstance| is in |targetMatrix|; essentially equal to
761// targetMatrix.matchInstance(fqInstance), but provides richer error message. In details:
762// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
763// 2. package@x.z::interface/servedInstance is in targetMatrix but
764// servedInstance is not in listInstances(package@x.z::interface)
765android::base::Result<void> VintfObject::IsFqInstanceDeprecated(
766 const CompatibilityMatrix& targetMatrix, HalFormat format, const FqInstance& fqInstance,
767 const ListInstances& listInstances) {
768 // Find minimum package@x.? in target matrix, and check if instance is in target matrix.
769 bool foundInstance = false;
770 Version targetMatrixMinVer{SIZE_MAX, SIZE_MAX};
771 targetMatrix.forEachInstanceOfPackage(
772 format, fqInstance.getPackage(), [&](const auto& targetMatrixInstance) {
773 if (targetMatrixInstance.versionRange().majorVer == fqInstance.getMajorVersion() &&
774 targetMatrixInstance.interface() == fqInstance.getInterface() &&
775 targetMatrixInstance.matchInstance(fqInstance.getInstance())) {
776 targetMatrixMinVer =
777 std::min(targetMatrixMinVer, targetMatrixInstance.versionRange().minVer());
778 foundInstance = true;
779 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800780 return true;
Yifan Hongb712e7e2020-03-17 17:58:35 -0700781 });
782 if (!foundInstance) {
783 return android::base::Error()
784 << fqInstance.string() << " is deprecated in compatibility matrix at FCM Version "
785 << targetMatrix.level() << "; it should not be served.";
786 }
787
788 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
789 bool targetVersionServed = false;
790 for (const auto& newPair :
791 listInstances(fqInstance.getPackage(), targetMatrixMinVer, fqInstance.getInterface(),
792 {fqInstance.getInstance()} /* instanceHint */)) {
793 if (newPair.first == fqInstance.getInstance()) {
794 targetVersionServed = true;
795 break;
Yifan Hongf73ba512018-01-17 15:52:30 -0800796 }
797 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700798
Yifan Hongb712e7e2020-03-17 17:58:35 -0700799 if (!targetVersionServed) {
800 return android::base::Error()
801 << fqInstance.string() << " is deprecated; requires at least " << targetMatrixMinVer;
802 }
803 return {};
Yifan Hongf73ba512018-01-17 15:52:30 -0800804}
805
Yifan Hongb712e7e2020-03-17 17:58:35 -0700806int32_t VintfObject::checkDeprecation(const ListInstances& listInstances,
807 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
808 std::string* error) {
Yifan Hong73bde592019-01-22 13:30:23 -0800809 std::vector<Named<CompatibilityMatrix>> matrixFragments;
810 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
811 if (matrixFragmentsStatus != OK) {
812 return matrixFragmentsStatus;
813 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800814 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800815 if (error && error->empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800816 *error = "Cannot get framework matrix for each FCM version for unknown error.";
Yifan Hong73bde592019-01-22 13:30:23 -0800817 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800818 return NAME_NOT_FOUND;
819 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700820 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800821 if (deviceManifest == nullptr) {
822 if (error) *error = "No device manifest.";
823 return NAME_NOT_FOUND;
824 }
825 Level deviceLevel = deviceManifest->level();
826 if (deviceLevel == Level::UNSPECIFIED) {
827 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
828 return BAD_VALUE;
829 }
830
831 const CompatibilityMatrix* targetMatrix = nullptr;
832 for (const auto& namedMatrix : matrixFragments) {
833 if (namedMatrix.object.level() == deviceLevel) {
834 targetMatrix = &namedMatrix.object;
835 }
836 }
837 if (targetMatrix == nullptr) {
838 if (error)
839 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
840 return NAME_NOT_FOUND;
841 }
842
Yifan Hongb712e7e2020-03-17 17:58:35 -0700843 std::multimap<std::string, std::string> childrenMap;
844 for (const auto& child : hidlMetadata) {
845 for (const auto& parent : child.inherited) {
846 childrenMap.emplace(parent, child.name);
847 }
848 }
849
850 // Find a list of possibly deprecated HALs by comparing |listInstances| with older matrices.
851 // Matrices with unspecified level are considered "current".
852 bool isDeprecated = false;
Yifan Hongf73ba512018-01-17 15:52:30 -0800853 for (const auto& namedMatrix : matrixFragments) {
854 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
855 if (namedMatrix.object.level() >= deviceLevel) continue;
856
857 const auto& oldMatrix = namedMatrix.object;
858 for (const MatrixHal& hal : oldMatrix.getHals()) {
Yifan Hongb712e7e2020-03-17 17:58:35 -0700859 if (IsHalDeprecated(hal, *targetMatrix, listInstances, childrenMap, error)) {
860 isDeprecated = true;
861 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800862 }
863 }
864
Yifan Hongb712e7e2020-03-17 17:58:35 -0700865 return isDeprecated ? DEPRECATED : NO_DEPRECATED_HALS;
Yifan Hongf73ba512018-01-17 15:52:30 -0800866}
867
Yifan Hongb712e7e2020-03-17 17:58:35 -0700868int32_t VintfObject::checkDeprecation(const std::vector<HidlInterfaceMetadata>& hidlMetadata,
869 std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800870 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700871 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700872 ListInstances inManifest =
873 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
874 const std::vector<std::string>& /* hintInstances */) {
875 std::vector<std::pair<std::string, Version>> ret;
876 deviceManifest->forEachInstanceOfInterface(
Yifan Hongac621482019-09-10 19:27:20 -0700877 HalFormat::HIDL, package, version, interface,
878 [&ret](const ManifestInstance& manifestInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700879 ret.push_back(
880 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
881 return true;
882 });
883 return ret;
884 };
Yifan Hongb712e7e2020-03-17 17:58:35 -0700885 return checkDeprecation(inManifest, hidlMetadata, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800886}
Yifan Hong3daec812017-02-27 18:49:11 -0800887
Yifan Hong378c4082019-12-23 13:10:57 -0800888Level VintfObject::getKernelLevel(std::string* error) {
889 auto manifest = getDeviceHalManifest();
890 if (!manifest) {
891 if (error) *error = "Cannot retrieve device manifest.";
892 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700893 }
Yifan Hong378c4082019-12-23 13:10:57 -0800894 if (manifest->kernel().has_value() && manifest->kernel()->level() != Level::UNSPECIFIED) {
895 return manifest->kernel()->level();
Yifan Hong1e8febd2019-08-07 16:17:19 -0700896 }
Yifan Hong378c4082019-12-23 13:10:57 -0800897 if (error) *error = "Device manifest does not specify kernel FCM version.";
898 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700899}
900
Yifan Hong9f78c182018-07-12 14:45:52 -0700901const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
902 return mFileSystem;
903}
904
Yifan Hong9f78c182018-07-12 14:45:52 -0700905const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
906 return mPropertyFetcher;
907}
908
Yifan Hongd038b2b2018-11-27 13:57:56 -0800909const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700910 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700911}
912
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700913android::base::Result<bool> VintfObject::hasFrameworkCompatibilityMatrixExtensions() {
914 std::vector<Named<CompatibilityMatrix>> matrixFragments;
915 std::string error;
916 status_t status = getAllFrameworkMatrixLevels(&matrixFragments, &error);
917 if (status != OK) {
918 return android::base::Error(-status)
919 << "Cannot get all framework matrix fragments: " << error;
920 }
921 for (const auto& namedMatrix : matrixFragments) {
922 // Returns true if product matrix exists.
923 if (android::base::StartsWith(namedMatrix.name, kProductVintfDir)) {
924 return true;
925 }
Yifan Hong238c6dc2020-03-12 22:56:16 -0700926 // Returns true if system_ext matrix exists.
927 if (android::base::StartsWith(namedMatrix.name, kSystemExtVintfDir)) {
928 return true;
929 }
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700930 // Returns true if device system matrix exists.
931 if (android::base::StartsWith(namedMatrix.name, kSystemVintfDir) &&
932 namedMatrix.object.level() == Level::UNSPECIFIED &&
933 !namedMatrix.object.getHals().empty()) {
934 return true;
935 }
936 }
937 return false;
938}
939
Yifan Hongff1a25c2020-03-17 14:09:10 -0700940android::base::Result<void> VintfObject::checkUnusedHals(
941 const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700942 auto matrix = getFrameworkCompatibilityMatrix();
943 if (matrix == nullptr) {
944 return android::base::Error(-NAME_NOT_FOUND) << "Missing framework matrix.";
945 }
946 auto manifest = getDeviceHalManifest();
947 if (manifest == nullptr) {
948 return android::base::Error(-NAME_NOT_FOUND) << "Missing device manifest.";
949 }
Yifan Hongff1a25c2020-03-17 14:09:10 -0700950 auto unused = manifest->checkUnusedHals(*matrix, hidlMetadata);
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700951 if (!unused.empty()) {
952 return android::base::Error()
953 << "The following instances are in the device manifest but "
954 << "not specified in framework compatibility matrix: \n"
955 << " " << android::base::Join(unused, "\n ") << "\n"
956 << "Suggested fix:\n"
957 << "1. Check for any typos in device manifest or framework compatibility "
958 << "matrices with FCM version >= " << matrix->level() << ".\n"
959 << "2. Add them to any framework compatibility matrix with FCM "
960 << "version >= " << matrix->level() << " where applicable.\n"
961 << "3. Add them to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
962 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE.";
963 }
964 return {};
965}
966
Yifan Hong78f5b572018-11-27 14:05:03 -0800967// make_unique does not work because VintfObject constructor is private.
968VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {}
969
970VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
971 mObject->mFileSystem = std::move(e);
972 return *this;
973}
974
975VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory(
976 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
977 mObject->mRuntimeInfoFactory = std::move(e);
978 return *this;
979}
980
981VintfObject::Builder& VintfObject::Builder::setPropertyFetcher(
982 std::unique_ptr<PropertyFetcher>&& e) {
983 mObject->mPropertyFetcher = std::move(e);
984 return *this;
985}
986
987std::unique_ptr<VintfObject> VintfObject::Builder::build() {
988 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
989 if (!mObject->mRuntimeInfoFactory)
990 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
991 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
992 return std::move(mObject);
993}
994
Yifan Hong3daec812017-02-27 18:49:11 -0800995} // namespace vintf
996} // namespace android