blob: 55fa7a3c1244932f62479381ea3d49aa9f35e9f3 [file] [log] [blame]
Yifan Hong3daec812017-02-27 18:49:11 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "VintfObject.h"
18
Yifan Hongd52bf3e2018-01-11 16:56:51 -080019#include <dirent.h>
20
Yifan Hong03ea4282020-03-17 17:58:35 -070021#include <algorithm>
Yifan Hong3daec812017-02-27 18:49:11 -080022#include <functional>
23#include <memory>
24#include <mutex>
25
Yifan Hongd7043972020-03-30 13:27:46 -070026#include <aidl/metadata.h>
Yifan Hong60217032018-01-08 16:19:42 -080027#include <android-base/logging.h>
Yifan Hong6b860852020-03-19 23:12:07 +000028#include <android-base/result.h>
Yifan Hongb02d87d2019-12-19 11:15:27 -080029#include <android-base/strings.h>
Yifan Hongd7924ff2020-03-17 14:09:10 -070030#include <hidl/metadata.h>
Yifan Hong60217032018-01-08 16:19:42 -080031
Yifan Hong12e23c22018-11-05 14:53:30 -080032#include "CompatibilityMatrix.h"
Yifan Hongd7043972020-03-30 13:27:46 -070033#include "constants-private.h"
Yifan Hong12e23c22018-11-05 14:53:30 -080034#include "parse_string.h"
35#include "parse_xml.h"
36#include "utils.h"
37
Yifan Hong60217032018-01-08 16:19:42 -080038using std::placeholders::_1;
39using std::placeholders::_2;
40
Yifan Hong3daec812017-02-27 18:49:11 -080041namespace android {
42namespace vintf {
43
Yifan Hong270b5652018-01-18 18:46:01 -080044using namespace details;
45
Yifan Hong9f78c182018-07-12 14:45:52 -070046#ifdef LIBVINTF_TARGET
47static constexpr bool kIsTarget = true;
48#else
49static constexpr bool kIsTarget = false;
50#endif
Yifan Hong1fb004e2017-09-26 15:04:44 -070051
Yifan Hong3daec812017-02-27 18:49:11 -080052template <typename T, typename F>
Yifan Hong44f611c2020-07-21 11:57:57 -070053static std::shared_ptr<const T> Get(const char* id, LockedSharedPtr<T>* ptr,
Steven Morelandec0721d2020-04-30 15:48:35 -070054 const F& fetchAllInformation) {
Yifan Hong3daec812017-02-27 18:49:11 -080055 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong44f611c2020-07-21 11:57:57 -070056 if (!ptr->fetchedOnce) {
Steven Morelandec0721d2020-04-30 15:48:35 -070057 LOG(INFO) << id << ": Reading VINTF information.";
Yifan Hong3daec812017-02-27 18:49:11 -080058 ptr->object = std::make_unique<T>();
Yifan Hong60217032018-01-08 16:19:42 -080059 std::string error;
Steven Moreland609d7ff2020-03-27 15:48:40 -070060 status_t status = fetchAllInformation(ptr->object.get(), &error);
Steven Morelandec0721d2020-04-30 15:48:35 -070061 if (status == OK) {
Steven Moreland2cc413f2020-04-30 16:42:56 -070062 ptr->fetchedOnce = true;
Steven Morelandec0721d2020-04-30 15:48:35 -070063 LOG(INFO) << id << ": Successfully processed VINTF information";
64 } else {
65 // Doubled because a malformed error std::string might cause us to
66 // lose the status.
67 LOG(ERROR) << id << ": status from fetching VINTF information: " << status;
68 LOG(ERROR) << id << ": " << status << " VINTF parse error: " << error;
Yifan Hongd7043972020-03-30 13:27:46 -070069 ptr->object = nullptr; // frees the old object
Yifan Hong3daec812017-02-27 18:49:11 -080070 }
71 }
Yifan Hongfc73edf2017-08-29 11:39:07 -070072 return ptr->object;
Yifan Hong3daec812017-02-27 18:49:11 -080073}
74
Yifan Hong9f78c182018-07-12 14:45:52 -070075static std::unique_ptr<FileSystem> createDefaultFileSystem() {
76 std::unique_ptr<FileSystem> fileSystem;
77 if (kIsTarget) {
78 fileSystem = std::make_unique<details::FileSystemImpl>();
79 } else {
80 fileSystem = std::make_unique<details::FileSystemNoOp>();
81 }
82 return fileSystem;
83}
84
85static std::unique_ptr<PropertyFetcher> createDefaultPropertyFetcher() {
86 std::unique_ptr<PropertyFetcher> propertyFetcher;
87 if (kIsTarget) {
88 propertyFetcher = std::make_unique<details::PropertyFetcherImpl>();
89 } else {
90 propertyFetcher = std::make_unique<details::PropertyFetcherNoOp>();
91 }
92 return propertyFetcher;
93}
94
Yifan Hong9f78c182018-07-12 14:45:52 -070095std::shared_ptr<VintfObject> VintfObject::GetInstance() {
Steven Morelandc16ff2b2020-02-26 17:03:37 -080096 static details::LockedSharedPtr<VintfObject> sInstance{};
Yifan Hong9f78c182018-07-12 14:45:52 -070097 std::unique_lock<std::mutex> lock(sInstance.mutex);
98 if (sInstance.object == nullptr) {
Yifan Hong78f5b572018-11-27 14:05:03 -080099 sInstance.object = std::shared_ptr<VintfObject>(VintfObject::Builder().build().release());
Yifan Hong9f78c182018-07-12 14:45:52 -0700100 }
101 return sInstance.object;
102}
103
Yifan Hong44f611c2020-07-21 11:57:57 -0700104std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest() {
105 return GetInstance()->getDeviceHalManifest();
Yifan Hong3daec812017-02-27 18:49:11 -0800106}
107
Yifan Hong44f611c2020-07-21 11:57:57 -0700108std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest() {
109 return Get(__func__, &mDeviceManifest,
Yifan Hong9f78c182018-07-12 14:45:52 -0700110 std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2));
111}
112
Yifan Hong44f611c2020-07-21 11:57:57 -0700113std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest() {
114 return GetInstance()->getFrameworkHalManifest();
Yifan Hong3daec812017-02-27 18:49:11 -0800115}
116
Yifan Hong44f611c2020-07-21 11:57:57 -0700117std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest() {
118 return Get(__func__, &mFrameworkManifest,
Yifan Hong9f78c182018-07-12 14:45:52 -0700119 std::bind(&VintfObject::fetchFrameworkHalManifest, this, _1, _2));
120}
Yifan Hong2272bf82017-04-28 14:37:56 -0700121
Yifan Hong44f611c2020-07-21 11:57:57 -0700122std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix() {
123 return GetInstance()->getDeviceCompatibilityMatrix();
Yifan Hong2272bf82017-04-28 14:37:56 -0700124}
125
Yifan Hong44f611c2020-07-21 11:57:57 -0700126std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix() {
127 return Get(__func__, &mDeviceMatrix, std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2));
Yifan Hong9f78c182018-07-12 14:45:52 -0700128}
129
Yifan Hong44f611c2020-07-21 11:57:57 -0700130std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix() {
131 return GetInstance()->getFrameworkCompatibilityMatrix();
Yifan Hong9f78c182018-07-12 14:45:52 -0700132}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800133
Yifan Hong44f611c2020-07-21 11:57:57 -0700134std::shared_ptr<const CompatibilityMatrix> VintfObject::getFrameworkCompatibilityMatrix() {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800135 // To avoid deadlock, get device manifest before any locks.
Yifan Hong9f78c182018-07-12 14:45:52 -0700136 auto deviceManifest = getDeviceHalManifest();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800137
Yifan Hong9f78c182018-07-12 14:45:52 -0700138 std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800139
140 auto combined =
Yifan Hong44f611c2020-07-21 11:57:57 -0700141 Get(__func__, &mCombinedFrameworkMatrix,
Yifan Hong9f78c182018-07-12 14:45:52 -0700142 std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest, _1, _2));
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800143 if (combined != nullptr) {
144 return combined;
145 }
146
Yifan Hong44f611c2020-07-21 11:57:57 -0700147 return Get(__func__, &mFrameworkMatrix,
Yifan Hong12e23c22018-11-05 14:53:30 -0800148 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(),
Yifan Hong9f78c182018-07-12 14:45:52 -0700149 kSystemLegacyMatrix, _2));
Yifan Hong2272bf82017-04-28 14:37:56 -0700150}
151
Yifan Hong9f78c182018-07-12 14:45:52 -0700152status_t VintfObject::getCombinedFrameworkMatrix(
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800153 const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out,
154 std::string* error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700155 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong73bde592019-01-22 13:30:23 -0800156 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
157 if (matrixFragmentsStatus != OK) {
158 return matrixFragmentsStatus;
159 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800160 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800161 if (error && error->empty()) {
162 *error = "Cannot get framework matrix for each FCM version for unknown error.";
163 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800164 return NAME_NOT_FOUND;
165 }
166
167 Level deviceLevel = Level::UNSPECIFIED;
168
169 if (deviceManifest != nullptr) {
170 deviceLevel = deviceManifest->level();
171 }
172
173 // TODO(b/70628538): Do not infer from Shipping API level.
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800174 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800175 auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800176 if (shippingApi != 0u) {
177 deviceLevel = details::convertFromApiLevel(shippingApi);
178 }
179 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800180
181 if (deviceLevel == Level::UNSPECIFIED) {
182 // Cannot infer FCM version. Combine all matrices by assuming
183 // Shipping FCM Version == min(all supported FCM Versions in the framework)
Yifan Honga83d0e42020-04-13 13:07:31 -0700184 for (auto&& fragment : matrixFragments) {
185 Level fragmentLevel = fragment.level();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800186 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
187 deviceLevel = fragmentLevel;
188 }
189 }
190 }
191
192 if (deviceLevel == Level::UNSPECIFIED) {
193 // None of the fragments specify any FCM version. Should never happen except
194 // for inconsistent builds.
195 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800196 *error = "No framework compatibility matrix files under " + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800197 " declare FCM version.";
198 }
199 return NAME_NOT_FOUND;
200 }
201
Yifan Honge7837b12018-10-11 10:38:57 -0700202 auto combined = CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800203 if (combined == nullptr) {
204 return BAD_VALUE;
205 }
206 *out = std::move(*combined);
207 return OK;
208}
209
Steven Morelandeedf2d42018-04-04 16:36:27 -0700210// Load and combine all of the manifests in a directory
Yifan Hong9f78c182018-07-12 14:45:52 -0700211status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
Steven Morelandeedf2d42018-04-04 16:36:27 -0700212 std::string* error) {
213 std::vector<std::string> fileNames;
Yifan Hong12e23c22018-11-05 14:53:30 -0800214 status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700215 // if the directory isn't there, that's okay
216 if (err == NAME_NOT_FOUND) return OK;
217 if (err != OK) return err;
218
219 for (const std::string& file : fileNames) {
220 // Only adds HALs because all other things are added by libvintf
221 // itself for now.
222 HalManifest fragmentManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700223 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700224 if (err != OK) return err;
225
Yifan Hong4c6962d2019-01-30 15:50:46 -0800226 if (!manifest->addAll(&fragmentManifest, error)) {
227 if (error) {
Yifan Hong9f9a3192020-04-13 16:39:45 -0700228 error->insert(0, "Cannot add manifest fragment " + directory + file + ": ");
Yifan Hong4c6962d2019-01-30 15:50:46 -0800229 }
230 return UNKNOWN_ERROR;
231 }
Steven Morelandeedf2d42018-04-04 16:36:27 -0700232 }
233
234 return OK;
235}
236
Yifan Hong5a93bf22018-01-17 18:22:32 -0800237// Priority for loading vendor manifest:
Roopesh Natarajad629d382020-02-26 09:51:38 -0800238// 1. Vendor manifest + device fragments + ODM manifest (optional) + odm fragments
239// 2. Vendor manifest + device fragments
Steven Morelandeedf2d42018-04-04 16:36:27 -0700240// 3. ODM manifest (optional) + odm fragments
241// 4. /vendor/manifest.xml (legacy, no fragments)
Yifan Hong5a93bf22018-01-17 18:22:32 -0800242// where:
Steven Moreland30a532c2018-11-01 08:46:32 -0700243// A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
244// over A.
Yifan Hong9f78c182018-07-12 14:45:52 -0700245status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
Roopesh Natarajad629d382020-02-26 09:51:38 -0800246 HalManifest vendorManifest;
247 status_t vendorStatus = fetchVendorHalManifest(&vendorManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800248 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
249 return vendorStatus;
250 }
251
Steven Morelandeedf2d42018-04-04 16:36:27 -0700252 if (vendorStatus == OK) {
Roopesh Natarajad629d382020-02-26 09:51:38 -0800253 *out = std::move(vendorManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700254 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700255 if (fragmentStatus != OK) {
256 return fragmentStatus;
257 }
258 }
259
Yifan Hong5a93bf22018-01-17 18:22:32 -0800260 HalManifest odmManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700261 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800262 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
263 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800264 }
265
Yifan Hong5a93bf22018-01-17 18:22:32 -0800266 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800267 if (odmStatus == OK) {
Yifan Hong4c6962d2019-01-30 15:50:46 -0800268 if (!out->addAll(&odmManifest, error)) {
269 if (error) {
270 error->insert(0, "Cannot add ODM manifest :");
271 }
272 return UNKNOWN_ERROR;
273 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800274 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700275 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800276 }
277
Yifan Hong12a11ac2018-01-19 13:58:32 -0800278 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800279 if (odmStatus == OK) {
280 *out = std::move(odmManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700281 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800282 }
283
284 // Use legacy /vendor/manifest.xml
Yifan Hong12e23c22018-11-05 14:53:30 -0800285 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800286}
287
Roopesh Natarajad629d382020-02-26 09:51:38 -0800288// Priority:
289// 1. if {vendorSku} is defined, /vendor/etc/vintf/manifest_{vendorSku}.xml
290// 2. /vendor/etc/vintf/manifest.xml
291// where:
292// {vendorSku} is the value of ro.boot.product.vendor.sku
293status_t VintfObject::fetchVendorHalManifest(HalManifest* out, std::string* error) {
294 status_t status;
295
296 std::string vendorSku;
297 vendorSku = getPropertyFetcher()->getProperty("ro.boot.product.vendor.sku", "");
298
299 if (!vendorSku.empty()) {
300 status =
301 fetchOneHalManifest(kVendorVintfDir + "manifest_" + vendorSku + ".xml", out, error);
302 if (status == OK || status != NAME_NOT_FOUND) {
303 return status;
304 }
305 }
306
307 status = fetchOneHalManifest(kVendorManifest, out, error);
308 if (status == OK || status != NAME_NOT_FOUND) {
309 return status;
310 }
311
312 return NAME_NOT_FOUND;
313}
314
Yifan Hong12a11ac2018-01-19 13:58:32 -0800315// "out" is written to iff return status is OK.
316// Priority:
317// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
318// 2. /odm/etc/vintf/manifest.xml
319// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
320// 4. /odm/etc/manifest.xml
321// where:
322// {sku} is the value of ro.boot.product.hardware.sku
Yifan Hong9f78c182018-07-12 14:45:52 -0700323status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800324 status_t status;
325
Yifan Hong12a11ac2018-01-19 13:58:32 -0800326 std::string productModel;
Yifan Hong12e23c22018-11-05 14:53:30 -0800327 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800328
329 if (!productModel.empty()) {
330 status =
Yifan Hong9f78c182018-07-12 14:45:52 -0700331 fetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800332 if (status == OK || status != NAME_NOT_FOUND) {
333 return status;
334 }
335 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800336
Yifan Hong9f78c182018-07-12 14:45:52 -0700337 status = fetchOneHalManifest(kOdmManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800338 if (status == OK || status != NAME_NOT_FOUND) {
339 return status;
340 }
341
Yifan Hong12a11ac2018-01-19 13:58:32 -0800342 if (!productModel.empty()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700343 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800344 error);
345 if (status == OK || status != NAME_NOT_FOUND) {
346 return status;
347 }
348 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800349
Yifan Hong9f78c182018-07-12 14:45:52 -0700350 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800351 if (status == OK || status != NAME_NOT_FOUND) {
352 return status;
353 }
354
355 return NAME_NOT_FOUND;
356}
357
358// Fetch one manifest.xml file. "out" is written to iff return status is OK.
359// Returns NAME_NOT_FOUND if file is missing.
Yifan Hong9f78c182018-07-12 14:45:52 -0700360status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800361 std::string* error) {
362 HalManifest ret;
Yifan Hong12e23c22018-11-05 14:53:30 -0800363 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800364 if (status == OK) {
365 *out = std::move(ret);
366 }
367 return status;
368}
369
Yifan Hong9f78c182018-07-12 14:45:52 -0700370status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800371 CompatibilityMatrix etcMatrix;
Yifan Hong12e23c22018-11-05 14:53:30 -0800372 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800373 *out = std::move(etcMatrix);
374 return OK;
375 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800376 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800377}
378
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700379// Priority:
380// 1. /system/etc/vintf/manifest.xml
381// + /system/etc/vintf/manifest/*.xml if they exist
382// + /product/etc/vintf/manifest.xml if it exists
383// + /product/etc/vintf/manifest/*.xml if they exist
384// 2. (deprecated) /system/manifest.xml
Yifan Hongc735be92020-10-12 16:25:50 -0700385status_t VintfObject::fetchUnfilteredFrameworkHalManifest(HalManifest* out, std::string* error) {
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700386 auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
387 if (systemEtcStatus == OK) {
388 auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out, error);
389 if (dirStatus != OK) {
390 return dirStatus;
391 }
392
Yifan Hong659feac2020-03-19 18:09:54 -0700393 std::vector<std::pair<const std::string&, const std::string&>> extensions{
394 {kProductManifest, kProductManifestFragmentDir},
395 {kSystemExtManifest, kSystemExtManifestFragmentDir},
396 };
397 for (auto&& [manifestPath, frags] : extensions) {
398 HalManifest halManifest;
399 auto status = fetchOneHalManifest(manifestPath, &halManifest, error);
400 if (status != OK && status != NAME_NOT_FOUND) {
401 return status;
402 }
403 if (status == OK) {
404 if (!out->addAll(&halManifest, error)) {
405 if (error) {
406 error->insert(0, "Cannot add " + manifestPath + ":");
407 }
408 return UNKNOWN_ERROR;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700409 }
Yifan Hong659feac2020-03-19 18:09:54 -0700410 }
411
412 auto fragmentStatus = addDirectoryManifests(frags, out, error);
413 if (fragmentStatus != OK) {
414 return fragmentStatus;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700415 }
416 }
Yifan Hongc735be92020-10-12 16:25:50 -0700417
Yifan Hong659feac2020-03-19 18:09:54 -0700418 return OK;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700419 } else {
420 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
421 << (error ? *error : strerror(-systemEtcStatus));
Yifan Hongde6d00e2018-02-27 17:11:28 -0800422 }
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700423
Yifan Hong12e23c22018-11-05 14:53:30 -0800424 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800425}
426
Yifan Hongc735be92020-10-12 16:25:50 -0700427status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
428 status_t status = fetchUnfilteredFrameworkHalManifest(out, error);
429 if (status != OK) {
430 return status;
431 }
432 filterHalsByDeviceManifestLevel(out);
433 return OK;
434}
435
436void VintfObject::filterHalsByDeviceManifestLevel(HalManifest* out) {
437 auto deviceManifest = getDeviceHalManifest();
438 if (deviceManifest == nullptr) {
439 LOG(WARNING) << "Cannot fetch device manifest to determine target FCM version to "
440 "filter framework manifest HALs.";
441 return;
442 }
443 Level deviceManifestLevel = deviceManifest->level();
444 if (deviceManifestLevel == Level::UNSPECIFIED) {
445 LOG(WARNING)
446 << "Not filtering framework manifest HALs because target FCM version is unspecified.";
447 return;
448 }
449 out->removeHalsIf([deviceManifestLevel](const ManifestHal& hal) {
450 if (hal.getMaxLevel() == Level::UNSPECIFIED) {
451 return false;
452 }
453 return hal.getMaxLevel() < deviceManifestLevel;
454 });
455}
456
Yifan Hong9f8f6562018-11-06 16:26:20 -0800457static void appendLine(std::string* error, const std::string& message) {
458 if (error != nullptr) {
459 if (!error->empty()) *error += "\n";
460 *error += message;
461 }
462}
463
Yifan Honga83d0e42020-04-13 13:07:31 -0700464status_t VintfObject::getOneMatrix(const std::string& path, CompatibilityMatrix* out,
Yifan Hong73bde592019-01-22 13:30:23 -0800465 std::string* error) {
466 std::string content;
467 status_t status = getFileSystem()->fetch(path, &content, error);
468 if (status != OK) {
469 return status;
470 }
Yifan Honga83d0e42020-04-13 13:07:31 -0700471 if (!gCompatibilityMatrixConverter(out, content, error)) {
Yifan Hong73bde592019-01-22 13:30:23 -0800472 if (error) {
473 error->insert(0, "Cannot parse " + path + ": ");
474 }
475 return BAD_VALUE;
476 }
Yifan Honga83d0e42020-04-13 13:07:31 -0700477 out->setFileName(path);
Yifan Hong73bde592019-01-22 13:30:23 -0800478 return OK;
479}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800480
Yifan Honga83d0e42020-04-13 13:07:31 -0700481status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<CompatibilityMatrix>* results,
Yifan Hong73bde592019-01-22 13:30:23 -0800482 std::string* error) {
Yifan Hongb02d87d2019-12-19 11:15:27 -0800483 std::vector<std::string> dirs = {
484 kSystemVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800485 kSystemExtVintfDir,
Yifan Hongb02d87d2019-12-19 11:15:27 -0800486 kProductVintfDir,
487 };
488 for (const auto& dir : dirs) {
489 std::vector<std::string> fileNames;
490 status_t listStatus = getFileSystem()->listFiles(dir, &fileNames, error);
491 if (listStatus == NAME_NOT_FOUND) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800492 continue;
493 }
Yifan Hongb02d87d2019-12-19 11:15:27 -0800494 if (listStatus != OK) {
495 return listStatus;
496 }
497 for (const std::string& fileName : fileNames) {
498 std::string path = dir + fileName;
Yifan Honga83d0e42020-04-13 13:07:31 -0700499 CompatibilityMatrix namedMatrix;
Yifan Hongb02d87d2019-12-19 11:15:27 -0800500 std::string matrixError;
501 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
502 if (matrixStatus != OK) {
503 // Manifests and matrices share the same dir. Client may not have enough
504 // permissions to read system manifests, or may not be able to parse it.
505 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
506 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
507 continue;
508 }
509 results->emplace_back(std::move(namedMatrix));
510 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800511
Yifan Hongb02d87d2019-12-19 11:15:27 -0800512 if (dir == kSystemVintfDir && results->empty()) {
513 if (error) {
514 *error = "No framework matrices under " + dir + " can be fetched or parsed.\n";
515 }
516 return NAME_NOT_FOUND;
517 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800518 }
519
Yifan Hong73bde592019-01-22 13:30:23 -0800520 if (results->empty()) {
521 if (error) {
522 *error =
Yifan Hongb02d87d2019-12-19 11:15:27 -0800523 "No framework matrices can be fetched or parsed. "
524 "The following directories are searched:\n " +
525 android::base::Join(dirs, "\n ");
Yifan Hong73bde592019-01-22 13:30:23 -0800526 }
527 return NAME_NOT_FOUND;
528 }
529 return OK;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800530}
531
Yifan Honga192fa52020-07-21 12:09:20 -0700532std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(RuntimeInfo::FetchFlags flags) {
533 return GetInstance()->getRuntimeInfo(flags);
Yifan Hong9f78c182018-07-12 14:45:52 -0700534}
Yifan Honga192fa52020-07-21 12:09:20 -0700535std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700536 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700537
Yifan Honga192fa52020-07-21 12:09:20 -0700538 // Skip fetching information that has already been fetched previously.
539 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700540
Yifan Hong9f78c182018-07-12 14:45:52 -0700541 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800542 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700543 }
544
Yifan Hongf3247982019-12-12 12:11:36 -0800545 // Fetch kernel FCM version from device HAL manifest and store it in RuntimeInfo too.
546 if ((flags & RuntimeInfo::FetchFlag::KERNEL_FCM) != 0) {
547 auto manifest = getDeviceHalManifest();
548 if (!manifest) {
549 mDeviceRuntimeInfo.fetchedFlags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
550 return nullptr;
551 }
Yifan Hongd942aae2020-11-12 16:48:12 -0800552 mDeviceRuntimeInfo.object->setKernelLevel(manifest->inferredKernelLevel());
Yifan Hongf3247982019-12-12 12:11:36 -0800553 flags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
554 }
555
Yifan Hong9f78c182018-07-12 14:45:52 -0700556 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700557 if (status != OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700558 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
Yifan Hong1fb004e2017-09-26 15:04:44 -0700559 return nullptr;
560 }
561
Yifan Hong9f78c182018-07-12 14:45:52 -0700562 mDeviceRuntimeInfo.fetchedFlags |= flags;
563 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800564}
565
Yifan Hong12e23c22018-11-05 14:53:30 -0800566int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
567 status_t status = OK;
568 // null checks for files and runtime info
569 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700570 appendLine(error, "No framework manifest file from device or from update package");
571 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700572 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800573 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700574 appendLine(error, "No device manifest file from device or from update package");
575 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700576 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800577 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700578 appendLine(error, "No framework matrix file from device or from update package");
579 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700580 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800581 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700582 appendLine(error, "No device matrix file from device or from update package");
583 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700584 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800585
Yifan Hong072f12d2018-08-08 13:04:51 -0700586 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800587 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700588 appendLine(error, "No runtime info from device");
589 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800590 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700591 }
Yifan Hong878556e2018-07-13 13:45:30 -0700592 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700593
594 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800595 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800596 if (error) {
597 error->insert(0,
598 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700599 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800600 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700601 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800602 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800603 if (error) {
604 error->insert(0,
605 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700606 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800607 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700608 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800609
Yifan Hong072f12d2018-08-08 13:04:51 -0700610 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800611 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
Yifan Hong85e589e2019-12-18 13:12:29 -0800612 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800613 if (error) {
614 error->insert(0,
615 "Runtime info and framework compatibility matrix are incompatible: ");
616 }
617 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700618 }
619 }
620
621 return COMPATIBLE;
622}
623
Yifan Hong9f78c182018-07-12 14:45:52 -0700624namespace details {
625
Yifan Hong270b5652018-01-18 18:46:01 -0800626const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800627const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800628const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong73bde592019-01-22 13:30:23 -0800629const std::string kProductVintfDir = "/product/etc/vintf/";
Yifan Hong5bbc4ae2020-01-09 14:30:27 -0800630const std::string kSystemExtVintfDir = "/system_ext/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800631
632const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800633const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800634const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800635const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong73bde592019-01-22 13:30:23 -0800636const std::string kProductMatrix = kProductVintfDir + "compatibility_matrix.xml";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700637const std::string kProductManifest = kProductVintfDir + "manifest.xml";
Yifan Hong659feac2020-03-19 18:09:54 -0700638const std::string kSystemExtManifest = kSystemExtVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800639
Steven Morelandeedf2d42018-04-04 16:36:27 -0700640const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/";
641const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/";
642const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700643const std::string kProductManifestFragmentDir = kProductVintfDir + "manifest/";
Yifan Hong659feac2020-03-19 18:09:54 -0700644const std::string kSystemExtManifestFragmentDir = kSystemExtVintfDir + "manifest/";
Steven Morelandeedf2d42018-04-04 16:36:27 -0700645
Yifan Hong270b5652018-01-18 18:46:01 -0800646const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
647const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800648const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800649const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
650const std::string kOdmLegacyVintfDir = "/odm/etc/";
651const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
652
Yifan Hong69c1b112018-02-27 17:06:00 -0800653std::vector<std::string> dumpFileList() {
654 return {
Yifan Hong73bde592019-01-22 13:30:23 -0800655 // clang-format off
656 kSystemVintfDir,
657 kVendorVintfDir,
658 kOdmVintfDir,
659 kProductVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800660 kSystemExtVintfDir,
Yifan Hong73bde592019-01-22 13:30:23 -0800661 kOdmLegacyVintfDir,
662 kVendorLegacyManifest,
663 kVendorLegacyMatrix,
664 kSystemLegacyManifest,
665 kSystemLegacyMatrix,
666 // clang-format on
Yifan Hong69c1b112018-02-27 17:06:00 -0800667 };
668}
669
Yifan Hong9f78c182018-07-12 14:45:52 -0700670} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700671
Yifan Hong9f78c182018-07-12 14:45:52 -0700672bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800673 const CompatibilityMatrix& targetMatrix,
Yifan Hong03ea4282020-03-17 17:58:35 -0700674 const ListInstances& listInstances,
675 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700676 bool isDeprecated = false;
677 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700678 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, childrenMap,
679 appendedError)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700680 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800681 }
Yifan Hong03ea4282020-03-17 17:58:35 -0700682 return true; // continue to check next instance
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700683 });
684 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800685}
686
Yifan Hong03ea4282020-03-17 17:58:35 -0700687// Let oldMatrixInstance = package@x.y-w::interface/instancePattern.
688// If any "@servedVersion::interface/servedInstance" in listInstances(package@x.y::interface)
689// matches instancePattern, return true iff for all child interfaces (from
690// GetListedInstanceInheritance), IsFqInstanceDeprecated returns false.
Yifan Hong9f78c182018-07-12 14:45:52 -0700691bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800692 const CompatibilityMatrix& targetMatrix,
Yifan Hong03ea4282020-03-17 17:58:35 -0700693 const ListInstances& listInstances,
694 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700695 const std::string& package = oldMatrixInstance.package();
696 const Version& version = oldMatrixInstance.versionRange().minVer();
697 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700698
Yifan Honga8a8fa92018-03-20 14:42:43 -0700699 std::vector<std::string> instanceHint;
700 if (!oldMatrixInstance.isRegex()) {
701 instanceHint.push_back(oldMatrixInstance.exactInstance());
702 }
703
Yifan Hong03ea4282020-03-17 17:58:35 -0700704 std::vector<std::string> accumulatedErrors;
Yifan Honga8a8fa92018-03-20 14:42:43 -0700705 auto list = listInstances(package, version, interface, instanceHint);
Yifan Hong03ea4282020-03-17 17:58:35 -0700706
Yifan Honga8a8fa92018-03-20 14:42:43 -0700707 for (const auto& pair : list) {
708 const std::string& servedInstance = pair.first;
709 Version servedVersion = pair.second;
Yifan Hong03ea4282020-03-17 17:58:35 -0700710 std::string servedFqInstanceString =
711 toFQNameString(package, servedVersion, interface, servedInstance);
Yifan Honga8a8fa92018-03-20 14:42:43 -0700712 if (!oldMatrixInstance.matchInstance(servedInstance)) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700713 // ignore unrelated instance
Yifan Honga8a8fa92018-03-20 14:42:43 -0700714 continue;
715 }
716
Yifan Hong03ea4282020-03-17 17:58:35 -0700717 auto inheritance = GetListedInstanceInheritance(package, servedVersion, interface,
718 servedInstance, listInstances, childrenMap);
719 if (!inheritance.has_value()) {
720 accumulatedErrors.push_back(inheritance.error().message());
721 continue;
Yifan Hongf73ba512018-01-17 15:52:30 -0800722 }
723
Yifan Hong03ea4282020-03-17 17:58:35 -0700724 std::vector<std::string> errors;
725 for (const auto& fqInstance : *inheritance) {
726 auto result = IsFqInstanceDeprecated(targetMatrix, oldMatrixInstance.format(),
727 fqInstance, listInstances);
728 if (result.ok()) {
729 errors.clear();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700730 break;
731 }
Yifan Hong03ea4282020-03-17 17:58:35 -0700732 errors.push_back(result.error().message());
Yifan Honga8a8fa92018-03-20 14:42:43 -0700733 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800734
Yifan Hong03ea4282020-03-17 17:58:35 -0700735 if (errors.empty()) {
736 continue;
737 }
738 accumulatedErrors.insert(accumulatedErrors.end(), errors.begin(), errors.end());
739 }
740
741 if (accumulatedErrors.empty()) {
742 return false;
743 }
744 appendLine(appendedError, android::base::Join(accumulatedErrors, "\n"));
745 return true;
746}
747
748// Check if fqInstance is listed in |listInstances|.
749bool VintfObject::IsInstanceListed(const ListInstances& listInstances,
750 const FqInstance& fqInstance) {
751 auto list =
752 listInstances(fqInstance.getPackage(), fqInstance.getVersion(), fqInstance.getInterface(),
753 {fqInstance.getInstance()} /* instanceHint*/);
754 return std::any_of(list.begin(), list.end(),
755 [&](const auto& pair) { return pair.first == fqInstance.getInstance(); });
756}
757
758// Return a list of FqInstance, where each element:
759// - is listed in |listInstances|; AND
760// - is, or inherits from, package@version::interface/instance (as specified by |childrenMap|)
761android::base::Result<std::vector<FqInstance>> VintfObject::GetListedInstanceInheritance(
762 const std::string& package, const Version& version, const std::string& interface,
763 const std::string& instance, const ListInstances& listInstances,
764 const ChildrenMap& childrenMap) {
765 FqInstance fqInstance;
766 if (!fqInstance.setTo(package, version.majorVer, version.minorVer, interface, instance)) {
767 return android::base::Error() << toFQNameString(package, version, interface, instance)
768 << " is not a valid FqInstance";
769 }
770
771 if (!IsInstanceListed(listInstances, fqInstance)) {
772 return {};
773 }
774
775 const FQName& fqName = fqInstance.getFqName();
776
777 std::vector<FqInstance> ret;
778 ret.push_back(fqInstance);
779
780 auto childRange = childrenMap.equal_range(fqName.string());
781 for (auto it = childRange.first; it != childRange.second; ++it) {
782 const auto& childFqNameString = it->second;
783 FQName childFqName;
784 if (!childFqName.setTo(childFqNameString)) {
785 return android::base::Error() << "Cannot parse " << childFqNameString << " as FQName";
786 }
787 FqInstance childFqInstance;
788 if (!childFqInstance.setTo(childFqName, fqInstance.getInstance())) {
789 return android::base::Error() << "Cannot merge " << childFqName.string() << "/"
790 << fqInstance.getInstance() << " as FqInstance";
791 continue;
792 }
793 if (!IsInstanceListed(listInstances, childFqInstance)) {
794 continue;
795 }
796 ret.push_back(childFqInstance);
797 }
798 return ret;
799}
800
801// Check if |fqInstance| is in |targetMatrix|; essentially equal to
802// targetMatrix.matchInstance(fqInstance), but provides richer error message. In details:
803// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
804// 2. package@x.z::interface/servedInstance is in targetMatrix but
805// servedInstance is not in listInstances(package@x.z::interface)
806android::base::Result<void> VintfObject::IsFqInstanceDeprecated(
807 const CompatibilityMatrix& targetMatrix, HalFormat format, const FqInstance& fqInstance,
808 const ListInstances& listInstances) {
809 // Find minimum package@x.? in target matrix, and check if instance is in target matrix.
810 bool foundInstance = false;
811 Version targetMatrixMinVer{SIZE_MAX, SIZE_MAX};
812 targetMatrix.forEachInstanceOfPackage(
813 format, fqInstance.getPackage(), [&](const auto& targetMatrixInstance) {
814 if (targetMatrixInstance.versionRange().majorVer == fqInstance.getMajorVersion() &&
815 targetMatrixInstance.interface() == fqInstance.getInterface() &&
816 targetMatrixInstance.matchInstance(fqInstance.getInstance())) {
817 targetMatrixMinVer =
818 std::min(targetMatrixMinVer, targetMatrixInstance.versionRange().minVer());
819 foundInstance = true;
820 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800821 return true;
Yifan Hong03ea4282020-03-17 17:58:35 -0700822 });
823 if (!foundInstance) {
824 return android::base::Error()
825 << fqInstance.string() << " is deprecated in compatibility matrix at FCM Version "
826 << targetMatrix.level() << "; it should not be served.";
827 }
828
829 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
830 bool targetVersionServed = false;
831 for (const auto& newPair :
832 listInstances(fqInstance.getPackage(), targetMatrixMinVer, fqInstance.getInterface(),
833 {fqInstance.getInstance()} /* instanceHint */)) {
834 if (newPair.first == fqInstance.getInstance()) {
835 targetVersionServed = true;
836 break;
Yifan Hongf73ba512018-01-17 15:52:30 -0800837 }
838 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700839
Yifan Hong03ea4282020-03-17 17:58:35 -0700840 if (!targetVersionServed) {
841 return android::base::Error()
842 << fqInstance.string() << " is deprecated; requires at least " << targetMatrixMinVer;
843 }
844 return {};
Yifan Hongf73ba512018-01-17 15:52:30 -0800845}
846
Yifan Hong03ea4282020-03-17 17:58:35 -0700847int32_t VintfObject::checkDeprecation(const ListInstances& listInstances,
848 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
849 std::string* error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700850 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong73bde592019-01-22 13:30:23 -0800851 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
852 if (matrixFragmentsStatus != OK) {
853 return matrixFragmentsStatus;
854 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800855 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800856 if (error && error->empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800857 *error = "Cannot get framework matrix for each FCM version for unknown error.";
Yifan Hong73bde592019-01-22 13:30:23 -0800858 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800859 return NAME_NOT_FOUND;
860 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700861 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800862 if (deviceManifest == nullptr) {
863 if (error) *error = "No device manifest.";
864 return NAME_NOT_FOUND;
865 }
866 Level deviceLevel = deviceManifest->level();
867 if (deviceLevel == Level::UNSPECIFIED) {
868 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
869 return BAD_VALUE;
870 }
871
872 const CompatibilityMatrix* targetMatrix = nullptr;
873 for (const auto& namedMatrix : matrixFragments) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700874 if (namedMatrix.level() == deviceLevel) {
875 targetMatrix = &namedMatrix;
Yifan Hongf73ba512018-01-17 15:52:30 -0800876 }
877 }
878 if (targetMatrix == nullptr) {
879 if (error)
880 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
881 return NAME_NOT_FOUND;
882 }
883
Yifan Hong03ea4282020-03-17 17:58:35 -0700884 std::multimap<std::string, std::string> childrenMap;
885 for (const auto& child : hidlMetadata) {
886 for (const auto& parent : child.inherited) {
887 childrenMap.emplace(parent, child.name);
888 }
889 }
890
891 // Find a list of possibly deprecated HALs by comparing |listInstances| with older matrices.
892 // Matrices with unspecified level are considered "current".
893 bool isDeprecated = false;
Yifan Hongf73ba512018-01-17 15:52:30 -0800894 for (const auto& namedMatrix : matrixFragments) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700895 if (namedMatrix.level() == Level::UNSPECIFIED) continue;
896 if (namedMatrix.level() >= deviceLevel) continue;
Yifan Hongf73ba512018-01-17 15:52:30 -0800897
Yifan Honga83d0e42020-04-13 13:07:31 -0700898 for (const MatrixHal& hal : namedMatrix.getHals()) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700899 if (IsHalDeprecated(hal, *targetMatrix, listInstances, childrenMap, error)) {
900 isDeprecated = true;
901 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800902 }
903 }
904
Yifan Hong03ea4282020-03-17 17:58:35 -0700905 return isDeprecated ? DEPRECATED : NO_DEPRECATED_HALS;
Yifan Hongf73ba512018-01-17 15:52:30 -0800906}
907
Yifan Hong03ea4282020-03-17 17:58:35 -0700908int32_t VintfObject::checkDeprecation(const std::vector<HidlInterfaceMetadata>& hidlMetadata,
909 std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800910 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700911 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700912 ListInstances inManifest =
913 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
914 const std::vector<std::string>& /* hintInstances */) {
915 std::vector<std::pair<std::string, Version>> ret;
916 deviceManifest->forEachInstanceOfInterface(
Yifan Hongac621482019-09-10 19:27:20 -0700917 HalFormat::HIDL, package, version, interface,
918 [&ret](const ManifestInstance& manifestInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700919 ret.push_back(
920 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
921 return true;
922 });
923 return ret;
924 };
Yifan Hong03ea4282020-03-17 17:58:35 -0700925 return checkDeprecation(inManifest, hidlMetadata, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800926}
Yifan Hong3daec812017-02-27 18:49:11 -0800927
Yifan Hong378c4082019-12-23 13:10:57 -0800928Level VintfObject::getKernelLevel(std::string* error) {
929 auto manifest = getDeviceHalManifest();
930 if (!manifest) {
931 if (error) *error = "Cannot retrieve device manifest.";
932 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700933 }
Yifan Hongd942aae2020-11-12 16:48:12 -0800934 // TODO(b/161317193): read kernel level from RuntimeInfo
Yifan Hong378c4082019-12-23 13:10:57 -0800935 if (manifest->kernel().has_value() && manifest->kernel()->level() != Level::UNSPECIFIED) {
936 return manifest->kernel()->level();
Yifan Hong1e8febd2019-08-07 16:17:19 -0700937 }
Yifan Hong378c4082019-12-23 13:10:57 -0800938 if (error) *error = "Device manifest does not specify kernel FCM version.";
939 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700940}
941
Yifan Hong9f78c182018-07-12 14:45:52 -0700942const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
943 return mFileSystem;
944}
945
Yifan Hong9f78c182018-07-12 14:45:52 -0700946const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
947 return mPropertyFetcher;
948}
949
Yifan Hongd038b2b2018-11-27 13:57:56 -0800950const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700951 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700952}
953
Yifan Hong6b860852020-03-19 23:12:07 +0000954android::base::Result<bool> VintfObject::hasFrameworkCompatibilityMatrixExtensions() {
Yifan Honga83d0e42020-04-13 13:07:31 -0700955 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong6b860852020-03-19 23:12:07 +0000956 std::string error;
957 status_t status = getAllFrameworkMatrixLevels(&matrixFragments, &error);
958 if (status != OK) {
959 return android::base::Error(-status)
960 << "Cannot get all framework matrix fragments: " << error;
961 }
962 for (const auto& namedMatrix : matrixFragments) {
963 // Returns true if product matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700964 if (android::base::StartsWith(namedMatrix.fileName(), kProductVintfDir)) {
Yifan Hong6b860852020-03-19 23:12:07 +0000965 return true;
966 }
Yifan Hongf6ff4272020-03-12 22:56:16 -0700967 // Returns true if system_ext matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700968 if (android::base::StartsWith(namedMatrix.fileName(), kSystemExtVintfDir)) {
Yifan Hongf6ff4272020-03-12 22:56:16 -0700969 return true;
970 }
Yifan Hong6b860852020-03-19 23:12:07 +0000971 // Returns true if device system matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700972 if (android::base::StartsWith(namedMatrix.fileName(), kSystemVintfDir) &&
973 namedMatrix.level() == Level::UNSPECIFIED && !namedMatrix.getHals().empty()) {
Yifan Hong6b860852020-03-19 23:12:07 +0000974 return true;
975 }
976 }
977 return false;
978}
979
Yifan Hongd7924ff2020-03-17 14:09:10 -0700980android::base::Result<void> VintfObject::checkUnusedHals(
981 const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
Yifan Hong6b860852020-03-19 23:12:07 +0000982 auto matrix = getFrameworkCompatibilityMatrix();
983 if (matrix == nullptr) {
984 return android::base::Error(-NAME_NOT_FOUND) << "Missing framework matrix.";
985 }
986 auto manifest = getDeviceHalManifest();
987 if (manifest == nullptr) {
988 return android::base::Error(-NAME_NOT_FOUND) << "Missing device manifest.";
989 }
Yifan Hongd7924ff2020-03-17 14:09:10 -0700990 auto unused = manifest->checkUnusedHals(*matrix, hidlMetadata);
Yifan Hong6b860852020-03-19 23:12:07 +0000991 if (!unused.empty()) {
992 return android::base::Error()
993 << "The following instances are in the device manifest but "
994 << "not specified in framework compatibility matrix: \n"
995 << " " << android::base::Join(unused, "\n ") << "\n"
996 << "Suggested fix:\n"
Yifan Hong5a220b12020-04-07 15:22:24 -0700997 << "1. Update deprecated HALs to the latest version.\n"
998 << "2. Check for any typos in device manifest or framework compatibility "
Yifan Hong6b860852020-03-19 23:12:07 +0000999 << "matrices with FCM version >= " << matrix->level() << ".\n"
Yifan Hong5a220b12020-04-07 15:22:24 -07001000 << "3. For new platform HALs, add them to any framework compatibility matrix "
1001 << "with FCM version >= " << matrix->level() << " where applicable.\n"
1002 << "4. For device-specific HALs, add to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
Yifan Hong6b860852020-03-19 23:12:07 +00001003 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE.";
1004 }
1005 return {};
1006}
1007
Yifan Hongd7043972020-03-30 13:27:46 -07001008namespace {
1009
1010// Insert |name| into |ret| if shouldCheck(name).
1011void InsertIf(const std::string& name, const std::function<bool(const std::string&)>& shouldCheck,
1012 std::set<std::string>* ret) {
1013 if (shouldCheck(name)) ret->insert(name);
1014}
1015
1016std::string StripHidlInterface(const std::string& fqNameString) {
1017 FQName fqName;
1018 if (!fqName.setTo(fqNameString)) {
1019 return "";
1020 }
1021 return fqName.getPackageAndVersion().string();
1022}
1023
1024std::string StripAidlType(const std::string& type) {
1025 auto items = android::base::Split(type, ".");
1026 if (items.empty()) {
1027 return "";
1028 }
1029 items.erase(items.end() - 1);
1030 return android::base::Join(items, ".");
1031}
1032
1033std::set<std::string> HidlMetadataToSet(
1034 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1035 const std::function<bool(const std::string&)>& shouldCheck) {
1036 std::set<std::string> ret;
1037 for (const auto& item : hidlMetadata) {
1038 InsertIf(StripHidlInterface(item.name), shouldCheck, &ret);
1039 }
1040 return ret;
1041}
1042
1043std::set<std::string> AidlMetadataToSet(
1044 const std::vector<AidlInterfaceMetadata>& aidlMetadata,
1045 const std::function<bool(const std::string&)>& shouldCheck) {
1046 std::set<std::string> ret;
1047 for (const auto& item : aidlMetadata) {
1048 for (const auto& type : item.types) {
1049 InsertIf(StripAidlType(type), shouldCheck, &ret);
1050 }
1051 }
1052 return ret;
1053}
1054
1055} // anonymous namespace
1056
1057android::base::Result<void> VintfObject::checkMissingHalsInMatrices(
1058 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1059 const std::vector<AidlInterfaceMetadata>& aidlMetadata,
1060 std::function<bool(const std::string&)> shouldCheck) {
1061 if (!shouldCheck) {
1062 shouldCheck = [](const auto&) { return true; };
1063 }
1064
1065 // Get all framework matrix fragments instead of the combined framework compatibility matrix
1066 // because the latter may omit interfaces from the latest FCM if device target-level is not
1067 // the latest.
1068 std::vector<CompatibilityMatrix> matrixFragments;
1069 std::string error;
1070 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, &error);
1071 if (matrixFragmentsStatus != OK) {
1072 return android::base::Error(-matrixFragmentsStatus)
1073 << "Unable to get all framework matrix fragments: " << error;
1074 }
1075 if (matrixFragments.empty()) {
1076 if (error.empty()) {
1077 error = "Cannot get framework matrix for each FCM version for unknown error.";
1078 }
1079 return android::base::Error(-NAME_NOT_FOUND) << error;
1080 }
1081
1082 // Filter aidlMetadata and hidlMetadata with shouldCheck.
1083 auto allAidlInterfaces = AidlMetadataToSet(aidlMetadata, shouldCheck);
1084 auto allHidlInterfaces = HidlMetadataToSet(hidlMetadata, shouldCheck);
1085
1086 // Filter out instances in allAidlMetadata and allHidlMetadata that are in the matrices.
1087 std::vector<std::string> errors;
1088 for (const auto& matrix : matrixFragments) {
1089 matrix.forEachInstance([&](const MatrixInstance& matrixInstance) {
1090 switch (matrixInstance.format()) {
1091 case HalFormat::AIDL: {
1092 allAidlInterfaces.erase(matrixInstance.package());
1093 return true; // continue to next instance
1094 }
1095 case HalFormat::HIDL: {
1096 for (Version v = matrixInstance.versionRange().minVer();
1097 v <= matrixInstance.versionRange().maxVer(); ++v.minorVer) {
1098 allHidlInterfaces.erase(toFQNameString(matrixInstance.package(), v));
1099 }
1100 return true; // continue to next instance
1101 }
1102 default: {
1103 if (shouldCheck(matrixInstance.package())) {
1104 errors.push_back("HAL package " + matrixInstance.package() +
1105 " is not allowed to have format " +
1106 to_string(matrixInstance.format()) + ".");
1107 }
1108 return true; // continue to next instance
1109 }
1110 }
1111 });
1112 }
1113
1114 if (!allHidlInterfaces.empty()) {
1115 errors.push_back(
1116 "The following HIDL packages are not found in any compatibility matrix fragments:\t\n" +
1117 android::base::Join(allHidlInterfaces, "\t\n"));
1118 }
1119 if (!allAidlInterfaces.empty()) {
1120 errors.push_back(
1121 "The following AIDL packages are not found in any compatibility matrix fragments:\t\n" +
1122 android::base::Join(allAidlInterfaces, "\t\n"));
1123 }
1124
1125 if (!errors.empty()) {
1126 return android::base::Error() << android::base::Join(errors, "\n");
1127 }
1128
1129 return {};
1130}
1131
Yifan Hong78f5b572018-11-27 14:05:03 -08001132// make_unique does not work because VintfObject constructor is private.
1133VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {}
1134
1135VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
1136 mObject->mFileSystem = std::move(e);
1137 return *this;
1138}
1139
1140VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory(
1141 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
1142 mObject->mRuntimeInfoFactory = std::move(e);
1143 return *this;
1144}
1145
1146VintfObject::Builder& VintfObject::Builder::setPropertyFetcher(
1147 std::unique_ptr<PropertyFetcher>&& e) {
1148 mObject->mPropertyFetcher = std::move(e);
1149 return *this;
1150}
1151
1152std::unique_ptr<VintfObject> VintfObject::Builder::build() {
1153 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
1154 if (!mObject->mRuntimeInfoFactory)
1155 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
1156 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
1157 return std::move(mObject);
1158}
1159
Yifan Hongd7043972020-03-30 13:27:46 -07001160} // namespace vintf
1161} // namespace android