blob: af075d4b3ce8d094fdbc8c237f650978b1eb4e59 [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 Hong60217032018-01-08 16:19:42 -080026#include <android-base/logging.h>
Yifan Hong6b860852020-03-19 23:12:07 +000027#include <android-base/result.h>
Yifan Hongb02d87d2019-12-19 11:15:27 -080028#include <android-base/strings.h>
Yifan Hongd7924ff2020-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 Hong44f611c2020-07-21 11:57:57 -070051static std::shared_ptr<const T> Get(const char* id, LockedSharedPtr<T>* ptr,
Steven Morelandec0721d2020-04-30 15:48:35 -070052 const F& fetchAllInformation) {
Yifan Hong3daec812017-02-27 18:49:11 -080053 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong44f611c2020-07-21 11:57:57 -070054 if (!ptr->fetchedOnce) {
Steven Morelandec0721d2020-04-30 15:48:35 -070055 LOG(INFO) << id << ": Reading VINTF information.";
Yifan Hong3daec812017-02-27 18:49:11 -080056 ptr->object = std::make_unique<T>();
Yifan Hong60217032018-01-08 16:19:42 -080057 std::string error;
Steven Moreland609d7ff2020-03-27 15:48:40 -070058 status_t status = fetchAllInformation(ptr->object.get(), &error);
Steven Morelandec0721d2020-04-30 15:48:35 -070059 if (status == OK) {
Steven Moreland2cc413f2020-04-30 16:42:56 -070060 ptr->fetchedOnce = true;
Steven Morelandec0721d2020-04-30 15:48:35 -070061 LOG(INFO) << id << ": Successfully processed VINTF information";
62 } else {
63 // Doubled because a malformed error std::string might cause us to
64 // lose the status.
65 LOG(ERROR) << id << ": status from fetching VINTF information: " << status;
66 LOG(ERROR) << id << ": " << status << " VINTF parse error: " << error;
Yifan Hong3daec812017-02-27 18:49:11 -080067 ptr->object = nullptr; // frees the old object
68 }
69 }
Yifan Hongfc73edf2017-08-29 11:39:07 -070070 return ptr->object;
Yifan Hong3daec812017-02-27 18:49:11 -080071}
72
Yifan Hong9f78c182018-07-12 14:45:52 -070073static std::unique_ptr<FileSystem> createDefaultFileSystem() {
74 std::unique_ptr<FileSystem> fileSystem;
75 if (kIsTarget) {
76 fileSystem = std::make_unique<details::FileSystemImpl>();
77 } else {
78 fileSystem = std::make_unique<details::FileSystemNoOp>();
79 }
80 return fileSystem;
81}
82
83static std::unique_ptr<PropertyFetcher> createDefaultPropertyFetcher() {
84 std::unique_ptr<PropertyFetcher> propertyFetcher;
85 if (kIsTarget) {
86 propertyFetcher = std::make_unique<details::PropertyFetcherImpl>();
87 } else {
88 propertyFetcher = std::make_unique<details::PropertyFetcherNoOp>();
89 }
90 return propertyFetcher;
91}
92
Yifan Hong9f78c182018-07-12 14:45:52 -070093std::shared_ptr<VintfObject> VintfObject::GetInstance() {
Steven Morelandc16ff2b2020-02-26 17:03:37 -080094 static details::LockedSharedPtr<VintfObject> sInstance{};
Yifan Hong9f78c182018-07-12 14:45:52 -070095 std::unique_lock<std::mutex> lock(sInstance.mutex);
96 if (sInstance.object == nullptr) {
Yifan Hong78f5b572018-11-27 14:05:03 -080097 sInstance.object = std::shared_ptr<VintfObject>(VintfObject::Builder().build().release());
Yifan Hong9f78c182018-07-12 14:45:52 -070098 }
99 return sInstance.object;
100}
101
Yifan Hong44f611c2020-07-21 11:57:57 -0700102std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest() {
103 return GetInstance()->getDeviceHalManifest();
Yifan Hong3daec812017-02-27 18:49:11 -0800104}
105
Yifan Hong44f611c2020-07-21 11:57:57 -0700106std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest() {
107 return Get(__func__, &mDeviceManifest,
Yifan Hong9f78c182018-07-12 14:45:52 -0700108 std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2));
109}
110
Yifan Hong44f611c2020-07-21 11:57:57 -0700111std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest() {
112 return GetInstance()->getFrameworkHalManifest();
Yifan Hong3daec812017-02-27 18:49:11 -0800113}
114
Yifan Hong44f611c2020-07-21 11:57:57 -0700115std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest() {
116 return Get(__func__, &mFrameworkManifest,
Yifan Hong9f78c182018-07-12 14:45:52 -0700117 std::bind(&VintfObject::fetchFrameworkHalManifest, this, _1, _2));
118}
Yifan Hong2272bf82017-04-28 14:37:56 -0700119
Yifan Hong44f611c2020-07-21 11:57:57 -0700120std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix() {
121 return GetInstance()->getDeviceCompatibilityMatrix();
Yifan Hong2272bf82017-04-28 14:37:56 -0700122}
123
Yifan Hong44f611c2020-07-21 11:57:57 -0700124std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix() {
125 return Get(__func__, &mDeviceMatrix, std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2));
Yifan Hong9f78c182018-07-12 14:45:52 -0700126}
127
Yifan Hong44f611c2020-07-21 11:57:57 -0700128std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix() {
129 return GetInstance()->getFrameworkCompatibilityMatrix();
Yifan Hong9f78c182018-07-12 14:45:52 -0700130}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800131
Yifan Hong44f611c2020-07-21 11:57:57 -0700132std::shared_ptr<const CompatibilityMatrix> VintfObject::getFrameworkCompatibilityMatrix() {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800133 // To avoid deadlock, get device manifest before any locks.
Yifan Hong9f78c182018-07-12 14:45:52 -0700134 auto deviceManifest = getDeviceHalManifest();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800135
Yifan Hong9f78c182018-07-12 14:45:52 -0700136 std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800137
138 auto combined =
Yifan Hong44f611c2020-07-21 11:57:57 -0700139 Get(__func__, &mCombinedFrameworkMatrix,
Yifan Hong9f78c182018-07-12 14:45:52 -0700140 std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest, _1, _2));
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800141 if (combined != nullptr) {
142 return combined;
143 }
144
Yifan Hong44f611c2020-07-21 11:57:57 -0700145 return Get(__func__, &mFrameworkMatrix,
Yifan Hong12e23c22018-11-05 14:53:30 -0800146 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(),
Yifan Hong9f78c182018-07-12 14:45:52 -0700147 kSystemLegacyMatrix, _2));
Yifan Hong2272bf82017-04-28 14:37:56 -0700148}
149
Yifan Hong9f78c182018-07-12 14:45:52 -0700150status_t VintfObject::getCombinedFrameworkMatrix(
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800151 const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out,
152 std::string* error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700153 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong73bde592019-01-22 13:30:23 -0800154 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
155 if (matrixFragmentsStatus != OK) {
156 return matrixFragmentsStatus;
157 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800158 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800159 if (error && error->empty()) {
160 *error = "Cannot get framework matrix for each FCM version for unknown error.";
161 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800162 return NAME_NOT_FOUND;
163 }
164
165 Level deviceLevel = Level::UNSPECIFIED;
166
167 if (deviceManifest != nullptr) {
168 deviceLevel = deviceManifest->level();
169 }
170
171 // TODO(b/70628538): Do not infer from Shipping API level.
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800172 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800173 auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800174 if (shippingApi != 0u) {
175 deviceLevel = details::convertFromApiLevel(shippingApi);
176 }
177 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800178
179 if (deviceLevel == Level::UNSPECIFIED) {
180 // Cannot infer FCM version. Combine all matrices by assuming
181 // Shipping FCM Version == min(all supported FCM Versions in the framework)
Yifan Honga83d0e42020-04-13 13:07:31 -0700182 for (auto&& fragment : matrixFragments) {
183 Level fragmentLevel = fragment.level();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800184 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
185 deviceLevel = fragmentLevel;
186 }
187 }
188 }
189
190 if (deviceLevel == Level::UNSPECIFIED) {
191 // None of the fragments specify any FCM version. Should never happen except
192 // for inconsistent builds.
193 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800194 *error = "No framework compatibility matrix files under " + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800195 " declare FCM version.";
196 }
197 return NAME_NOT_FOUND;
198 }
199
Yifan Honge7837b12018-10-11 10:38:57 -0700200 auto combined = CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800201 if (combined == nullptr) {
202 return BAD_VALUE;
203 }
204 *out = std::move(*combined);
205 return OK;
206}
207
Steven Morelandeedf2d42018-04-04 16:36:27 -0700208// Load and combine all of the manifests in a directory
Yifan Hong9f78c182018-07-12 14:45:52 -0700209status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
Steven Morelandeedf2d42018-04-04 16:36:27 -0700210 std::string* error) {
211 std::vector<std::string> fileNames;
Yifan Hong12e23c22018-11-05 14:53:30 -0800212 status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700213 // if the directory isn't there, that's okay
214 if (err == NAME_NOT_FOUND) return OK;
215 if (err != OK) return err;
216
217 for (const std::string& file : fileNames) {
218 // Only adds HALs because all other things are added by libvintf
219 // itself for now.
220 HalManifest fragmentManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700221 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700222 if (err != OK) return err;
223
Yifan Hong4c6962d2019-01-30 15:50:46 -0800224 if (!manifest->addAll(&fragmentManifest, error)) {
225 if (error) {
Yifan Hong9f9a3192020-04-13 16:39:45 -0700226 error->insert(0, "Cannot add manifest fragment " + directory + file + ": ");
Yifan Hong4c6962d2019-01-30 15:50:46 -0800227 }
228 return UNKNOWN_ERROR;
229 }
Steven Morelandeedf2d42018-04-04 16:36:27 -0700230 }
231
232 return OK;
233}
234
Yifan Hong5a93bf22018-01-17 18:22:32 -0800235// Priority for loading vendor manifest:
Roopesh Natarajad629d382020-02-26 09:51:38 -0800236// 1. Vendor manifest + device fragments + ODM manifest (optional) + odm fragments
237// 2. Vendor manifest + device fragments
Steven Morelandeedf2d42018-04-04 16:36:27 -0700238// 3. ODM manifest (optional) + odm fragments
239// 4. /vendor/manifest.xml (legacy, no fragments)
Yifan Hong5a93bf22018-01-17 18:22:32 -0800240// where:
Steven Moreland30a532c2018-11-01 08:46:32 -0700241// A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
242// over A.
Yifan Hong9f78c182018-07-12 14:45:52 -0700243status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
Roopesh Natarajad629d382020-02-26 09:51:38 -0800244 HalManifest vendorManifest;
245 status_t vendorStatus = fetchVendorHalManifest(&vendorManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800246 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
247 return vendorStatus;
248 }
249
Steven Morelandeedf2d42018-04-04 16:36:27 -0700250 if (vendorStatus == OK) {
Roopesh Natarajad629d382020-02-26 09:51:38 -0800251 *out = std::move(vendorManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700252 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700253 if (fragmentStatus != OK) {
254 return fragmentStatus;
255 }
256 }
257
Yifan Hong5a93bf22018-01-17 18:22:32 -0800258 HalManifest odmManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700259 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800260 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
261 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800262 }
263
Yifan Hong5a93bf22018-01-17 18:22:32 -0800264 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800265 if (odmStatus == OK) {
Yifan Hong4c6962d2019-01-30 15:50:46 -0800266 if (!out->addAll(&odmManifest, error)) {
267 if (error) {
268 error->insert(0, "Cannot add ODM manifest :");
269 }
270 return UNKNOWN_ERROR;
271 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800272 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700273 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800274 }
275
Yifan Hong12a11ac2018-01-19 13:58:32 -0800276 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800277 if (odmStatus == OK) {
278 *out = std::move(odmManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700279 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800280 }
281
282 // Use legacy /vendor/manifest.xml
Yifan Hong12e23c22018-11-05 14:53:30 -0800283 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800284}
285
Roopesh Natarajad629d382020-02-26 09:51:38 -0800286// Priority:
287// 1. if {vendorSku} is defined, /vendor/etc/vintf/manifest_{vendorSku}.xml
288// 2. /vendor/etc/vintf/manifest.xml
289// where:
290// {vendorSku} is the value of ro.boot.product.vendor.sku
291status_t VintfObject::fetchVendorHalManifest(HalManifest* out, std::string* error) {
292 status_t status;
293
294 std::string vendorSku;
295 vendorSku = getPropertyFetcher()->getProperty("ro.boot.product.vendor.sku", "");
296
297 if (!vendorSku.empty()) {
298 status =
299 fetchOneHalManifest(kVendorVintfDir + "manifest_" + vendorSku + ".xml", out, error);
300 if (status == OK || status != NAME_NOT_FOUND) {
301 return status;
302 }
303 }
304
305 status = fetchOneHalManifest(kVendorManifest, out, error);
306 if (status == OK || status != NAME_NOT_FOUND) {
307 return status;
308 }
309
310 return NAME_NOT_FOUND;
311}
312
Yifan Hong12a11ac2018-01-19 13:58:32 -0800313// "out" is written to iff return status is OK.
314// Priority:
315// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
316// 2. /odm/etc/vintf/manifest.xml
317// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
318// 4. /odm/etc/manifest.xml
319// where:
320// {sku} is the value of ro.boot.product.hardware.sku
Yifan Hong9f78c182018-07-12 14:45:52 -0700321status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800322 status_t status;
323
Yifan Hong12a11ac2018-01-19 13:58:32 -0800324 std::string productModel;
Yifan Hong12e23c22018-11-05 14:53:30 -0800325 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800326
327 if (!productModel.empty()) {
328 status =
Yifan Hong9f78c182018-07-12 14:45:52 -0700329 fetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800330 if (status == OK || status != NAME_NOT_FOUND) {
331 return status;
332 }
333 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800334
Yifan Hong9f78c182018-07-12 14:45:52 -0700335 status = fetchOneHalManifest(kOdmManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800336 if (status == OK || status != NAME_NOT_FOUND) {
337 return status;
338 }
339
Yifan Hong12a11ac2018-01-19 13:58:32 -0800340 if (!productModel.empty()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700341 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800342 error);
343 if (status == OK || status != NAME_NOT_FOUND) {
344 return status;
345 }
346 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800347
Yifan Hong9f78c182018-07-12 14:45:52 -0700348 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800349 if (status == OK || status != NAME_NOT_FOUND) {
350 return status;
351 }
352
353 return NAME_NOT_FOUND;
354}
355
356// Fetch one manifest.xml file. "out" is written to iff return status is OK.
357// Returns NAME_NOT_FOUND if file is missing.
Yifan Hong9f78c182018-07-12 14:45:52 -0700358status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800359 std::string* error) {
360 HalManifest ret;
Yifan Hong12e23c22018-11-05 14:53:30 -0800361 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800362 if (status == OK) {
363 *out = std::move(ret);
364 }
365 return status;
366}
367
Yifan Hong9f78c182018-07-12 14:45:52 -0700368status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800369 CompatibilityMatrix etcMatrix;
Yifan Hong12e23c22018-11-05 14:53:30 -0800370 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800371 *out = std::move(etcMatrix);
372 return OK;
373 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800374 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800375}
376
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700377// Priority:
378// 1. /system/etc/vintf/manifest.xml
379// + /system/etc/vintf/manifest/*.xml if they exist
380// + /product/etc/vintf/manifest.xml if it exists
381// + /product/etc/vintf/manifest/*.xml if they exist
382// 2. (deprecated) /system/manifest.xml
Yifan Hong9f78c182018-07-12 14:45:52 -0700383status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700384 auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
385 if (systemEtcStatus == OK) {
386 auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out, error);
387 if (dirStatus != OK) {
388 return dirStatus;
389 }
390
Yifan Hong659feac2020-03-19 18:09:54 -0700391 std::vector<std::pair<const std::string&, const std::string&>> extensions{
392 {kProductManifest, kProductManifestFragmentDir},
393 {kSystemExtManifest, kSystemExtManifestFragmentDir},
394 };
395 for (auto&& [manifestPath, frags] : extensions) {
396 HalManifest halManifest;
397 auto status = fetchOneHalManifest(manifestPath, &halManifest, error);
398 if (status != OK && status != NAME_NOT_FOUND) {
399 return status;
400 }
401 if (status == OK) {
402 if (!out->addAll(&halManifest, error)) {
403 if (error) {
404 error->insert(0, "Cannot add " + manifestPath + ":");
405 }
406 return UNKNOWN_ERROR;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700407 }
Yifan Hong659feac2020-03-19 18:09:54 -0700408 }
409
410 auto fragmentStatus = addDirectoryManifests(frags, out, error);
411 if (fragmentStatus != OK) {
412 return fragmentStatus;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700413 }
414 }
Yifan Hong659feac2020-03-19 18:09:54 -0700415 return OK;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700416 } else {
417 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
418 << (error ? *error : strerror(-systemEtcStatus));
Yifan Hongde6d00e2018-02-27 17:11:28 -0800419 }
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700420
Yifan Hong12e23c22018-11-05 14:53:30 -0800421 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800422}
423
Yifan Hong9f8f6562018-11-06 16:26:20 -0800424static void appendLine(std::string* error, const std::string& message) {
425 if (error != nullptr) {
426 if (!error->empty()) *error += "\n";
427 *error += message;
428 }
429}
430
Yifan Honga83d0e42020-04-13 13:07:31 -0700431status_t VintfObject::getOneMatrix(const std::string& path, CompatibilityMatrix* out,
Yifan Hong73bde592019-01-22 13:30:23 -0800432 std::string* error) {
433 std::string content;
434 status_t status = getFileSystem()->fetch(path, &content, error);
435 if (status != OK) {
436 return status;
437 }
Yifan Honga83d0e42020-04-13 13:07:31 -0700438 if (!gCompatibilityMatrixConverter(out, content, error)) {
Yifan Hong73bde592019-01-22 13:30:23 -0800439 if (error) {
440 error->insert(0, "Cannot parse " + path + ": ");
441 }
442 return BAD_VALUE;
443 }
Yifan Honga83d0e42020-04-13 13:07:31 -0700444 out->setFileName(path);
Yifan Hong73bde592019-01-22 13:30:23 -0800445 return OK;
446}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800447
Yifan Honga83d0e42020-04-13 13:07:31 -0700448status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<CompatibilityMatrix>* results,
Yifan Hong73bde592019-01-22 13:30:23 -0800449 std::string* error) {
Yifan Hongb02d87d2019-12-19 11:15:27 -0800450 std::vector<std::string> dirs = {
451 kSystemVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800452 kSystemExtVintfDir,
Yifan Hongb02d87d2019-12-19 11:15:27 -0800453 kProductVintfDir,
454 };
455 for (const auto& dir : dirs) {
456 std::vector<std::string> fileNames;
457 status_t listStatus = getFileSystem()->listFiles(dir, &fileNames, error);
458 if (listStatus == NAME_NOT_FOUND) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800459 continue;
460 }
Yifan Hongb02d87d2019-12-19 11:15:27 -0800461 if (listStatus != OK) {
462 return listStatus;
463 }
464 for (const std::string& fileName : fileNames) {
465 std::string path = dir + fileName;
Yifan Honga83d0e42020-04-13 13:07:31 -0700466 CompatibilityMatrix namedMatrix;
Yifan Hongb02d87d2019-12-19 11:15:27 -0800467 std::string matrixError;
468 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
469 if (matrixStatus != OK) {
470 // Manifests and matrices share the same dir. Client may not have enough
471 // permissions to read system manifests, or may not be able to parse it.
472 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
473 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
474 continue;
475 }
476 results->emplace_back(std::move(namedMatrix));
477 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800478
Yifan Hongb02d87d2019-12-19 11:15:27 -0800479 if (dir == kSystemVintfDir && results->empty()) {
480 if (error) {
481 *error = "No framework matrices under " + dir + " can be fetched or parsed.\n";
482 }
483 return NAME_NOT_FOUND;
484 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800485 }
486
Yifan Hong73bde592019-01-22 13:30:23 -0800487 if (results->empty()) {
488 if (error) {
489 *error =
Yifan Hongb02d87d2019-12-19 11:15:27 -0800490 "No framework matrices can be fetched or parsed. "
491 "The following directories are searched:\n " +
492 android::base::Join(dirs, "\n ");
Yifan Hong73bde592019-01-22 13:30:23 -0800493 }
494 return NAME_NOT_FOUND;
495 }
496 return OK;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800497}
498
Yifan Hong1fb004e2017-09-26 15:04:44 -0700499std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
500 RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700501 return GetInstance()->getRuntimeInfo(skipCache, flags);
502}
503std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(bool skipCache,
504 RuntimeInfo::FetchFlags flags) {
505 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700506
507 if (!skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700508 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700509 }
510
Yifan Hong9f78c182018-07-12 14:45:52 -0700511 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800512 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700513 }
514
Yifan Hongf3247982019-12-12 12:11:36 -0800515 // Fetch kernel FCM version from device HAL manifest and store it in RuntimeInfo too.
516 if ((flags & RuntimeInfo::FetchFlag::KERNEL_FCM) != 0) {
517 auto manifest = getDeviceHalManifest();
518 if (!manifest) {
519 mDeviceRuntimeInfo.fetchedFlags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
520 return nullptr;
521 }
522 Level level = Level::UNSPECIFIED;
523 if (manifest->kernel().has_value()) {
524 level = manifest->kernel()->level();
525 }
526 mDeviceRuntimeInfo.object->setKernelLevel(level);
527 flags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
528 }
529
Yifan Hong9f78c182018-07-12 14:45:52 -0700530 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700531 if (status != OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700532 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
Yifan Hong1fb004e2017-09-26 15:04:44 -0700533 return nullptr;
534 }
535
Yifan Hong9f78c182018-07-12 14:45:52 -0700536 mDeviceRuntimeInfo.fetchedFlags |= flags;
537 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800538}
539
Yifan Hong12e23c22018-11-05 14:53:30 -0800540int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
541 status_t status = OK;
542 // null checks for files and runtime info
543 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700544 appendLine(error, "No framework manifest file from device or from update package");
545 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700546 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800547 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700548 appendLine(error, "No device manifest file from device or from update package");
549 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700550 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800551 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700552 appendLine(error, "No framework matrix file from device or from update package");
553 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700554 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800555 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700556 appendLine(error, "No device matrix file from device or from update package");
557 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700558 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800559
Yifan Hong072f12d2018-08-08 13:04:51 -0700560 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800561 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700562 appendLine(error, "No runtime info from device");
563 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800564 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700565 }
Yifan Hong878556e2018-07-13 13:45:30 -0700566 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700567
568 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800569 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800570 if (error) {
571 error->insert(0,
572 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700573 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800574 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700575 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800576 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800577 if (error) {
578 error->insert(0,
579 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700580 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800581 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700582 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800583
Yifan Hong072f12d2018-08-08 13:04:51 -0700584 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800585 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
Yifan Hong85e589e2019-12-18 13:12:29 -0800586 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800587 if (error) {
588 error->insert(0,
589 "Runtime info and framework compatibility matrix are incompatible: ");
590 }
591 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700592 }
593 }
594
595 return COMPATIBLE;
596}
597
Yifan Hong9f78c182018-07-12 14:45:52 -0700598namespace details {
599
Yifan Hong270b5652018-01-18 18:46:01 -0800600const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800601const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800602const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong73bde592019-01-22 13:30:23 -0800603const std::string kProductVintfDir = "/product/etc/vintf/";
Yifan Hong5bbc4ae2020-01-09 14:30:27 -0800604const std::string kSystemExtVintfDir = "/system_ext/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800605
606const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800607const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800608const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800609const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong73bde592019-01-22 13:30:23 -0800610const std::string kProductMatrix = kProductVintfDir + "compatibility_matrix.xml";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700611const std::string kProductManifest = kProductVintfDir + "manifest.xml";
Yifan Hong659feac2020-03-19 18:09:54 -0700612const std::string kSystemExtManifest = kSystemExtVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800613
Steven Morelandeedf2d42018-04-04 16:36:27 -0700614const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/";
615const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/";
616const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700617const std::string kProductManifestFragmentDir = kProductVintfDir + "manifest/";
Yifan Hong659feac2020-03-19 18:09:54 -0700618const std::string kSystemExtManifestFragmentDir = kSystemExtVintfDir + "manifest/";
Steven Morelandeedf2d42018-04-04 16:36:27 -0700619
Yifan Hong270b5652018-01-18 18:46:01 -0800620const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
621const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800622const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800623const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
624const std::string kOdmLegacyVintfDir = "/odm/etc/";
625const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
626
Yifan Hong69c1b112018-02-27 17:06:00 -0800627std::vector<std::string> dumpFileList() {
628 return {
Yifan Hong73bde592019-01-22 13:30:23 -0800629 // clang-format off
630 kSystemVintfDir,
631 kVendorVintfDir,
632 kOdmVintfDir,
633 kProductVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800634 kSystemExtVintfDir,
Yifan Hong73bde592019-01-22 13:30:23 -0800635 kOdmLegacyVintfDir,
636 kVendorLegacyManifest,
637 kVendorLegacyMatrix,
638 kSystemLegacyManifest,
639 kSystemLegacyMatrix,
640 // clang-format on
Yifan Hong69c1b112018-02-27 17:06:00 -0800641 };
642}
643
Yifan Hong9f78c182018-07-12 14:45:52 -0700644} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700645
Yifan Hong9f78c182018-07-12 14:45:52 -0700646bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800647 const CompatibilityMatrix& targetMatrix,
Yifan Hong03ea4282020-03-17 17:58:35 -0700648 const ListInstances& listInstances,
649 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700650 bool isDeprecated = false;
651 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700652 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, childrenMap,
653 appendedError)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700654 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800655 }
Yifan Hong03ea4282020-03-17 17:58:35 -0700656 return true; // continue to check next instance
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700657 });
658 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800659}
660
Yifan Hong03ea4282020-03-17 17:58:35 -0700661// Let oldMatrixInstance = package@x.y-w::interface/instancePattern.
662// If any "@servedVersion::interface/servedInstance" in listInstances(package@x.y::interface)
663// matches instancePattern, return true iff for all child interfaces (from
664// GetListedInstanceInheritance), IsFqInstanceDeprecated returns false.
Yifan Hong9f78c182018-07-12 14:45:52 -0700665bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800666 const CompatibilityMatrix& targetMatrix,
Yifan Hong03ea4282020-03-17 17:58:35 -0700667 const ListInstances& listInstances,
668 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700669 const std::string& package = oldMatrixInstance.package();
670 const Version& version = oldMatrixInstance.versionRange().minVer();
671 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700672
Yifan Honga8a8fa92018-03-20 14:42:43 -0700673 std::vector<std::string> instanceHint;
674 if (!oldMatrixInstance.isRegex()) {
675 instanceHint.push_back(oldMatrixInstance.exactInstance());
676 }
677
Yifan Hong03ea4282020-03-17 17:58:35 -0700678 std::vector<std::string> accumulatedErrors;
Yifan Honga8a8fa92018-03-20 14:42:43 -0700679 auto list = listInstances(package, version, interface, instanceHint);
Yifan Hong03ea4282020-03-17 17:58:35 -0700680
Yifan Honga8a8fa92018-03-20 14:42:43 -0700681 for (const auto& pair : list) {
682 const std::string& servedInstance = pair.first;
683 Version servedVersion = pair.second;
Yifan Hong03ea4282020-03-17 17:58:35 -0700684 std::string servedFqInstanceString =
685 toFQNameString(package, servedVersion, interface, servedInstance);
Yifan Honga8a8fa92018-03-20 14:42:43 -0700686 if (!oldMatrixInstance.matchInstance(servedInstance)) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700687 // ignore unrelated instance
Yifan Honga8a8fa92018-03-20 14:42:43 -0700688 continue;
689 }
690
Yifan Hong03ea4282020-03-17 17:58:35 -0700691 auto inheritance = GetListedInstanceInheritance(package, servedVersion, interface,
692 servedInstance, listInstances, childrenMap);
693 if (!inheritance.has_value()) {
694 accumulatedErrors.push_back(inheritance.error().message());
695 continue;
Yifan Hongf73ba512018-01-17 15:52:30 -0800696 }
697
Yifan Hong03ea4282020-03-17 17:58:35 -0700698 std::vector<std::string> errors;
699 for (const auto& fqInstance : *inheritance) {
700 auto result = IsFqInstanceDeprecated(targetMatrix, oldMatrixInstance.format(),
701 fqInstance, listInstances);
702 if (result.ok()) {
703 errors.clear();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700704 break;
705 }
Yifan Hong03ea4282020-03-17 17:58:35 -0700706 errors.push_back(result.error().message());
Yifan Honga8a8fa92018-03-20 14:42:43 -0700707 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800708
Yifan Hong03ea4282020-03-17 17:58:35 -0700709 if (errors.empty()) {
710 continue;
711 }
712 accumulatedErrors.insert(accumulatedErrors.end(), errors.begin(), errors.end());
713 }
714
715 if (accumulatedErrors.empty()) {
716 return false;
717 }
718 appendLine(appendedError, android::base::Join(accumulatedErrors, "\n"));
719 return true;
720}
721
722// Check if fqInstance is listed in |listInstances|.
723bool VintfObject::IsInstanceListed(const ListInstances& listInstances,
724 const FqInstance& fqInstance) {
725 auto list =
726 listInstances(fqInstance.getPackage(), fqInstance.getVersion(), fqInstance.getInterface(),
727 {fqInstance.getInstance()} /* instanceHint*/);
728 return std::any_of(list.begin(), list.end(),
729 [&](const auto& pair) { return pair.first == fqInstance.getInstance(); });
730}
731
732// Return a list of FqInstance, where each element:
733// - is listed in |listInstances|; AND
734// - is, or inherits from, package@version::interface/instance (as specified by |childrenMap|)
735android::base::Result<std::vector<FqInstance>> VintfObject::GetListedInstanceInheritance(
736 const std::string& package, const Version& version, const std::string& interface,
737 const std::string& instance, const ListInstances& listInstances,
738 const ChildrenMap& childrenMap) {
739 FqInstance fqInstance;
740 if (!fqInstance.setTo(package, version.majorVer, version.minorVer, interface, instance)) {
741 return android::base::Error() << toFQNameString(package, version, interface, instance)
742 << " is not a valid FqInstance";
743 }
744
745 if (!IsInstanceListed(listInstances, fqInstance)) {
746 return {};
747 }
748
749 const FQName& fqName = fqInstance.getFqName();
750
751 std::vector<FqInstance> ret;
752 ret.push_back(fqInstance);
753
754 auto childRange = childrenMap.equal_range(fqName.string());
755 for (auto it = childRange.first; it != childRange.second; ++it) {
756 const auto& childFqNameString = it->second;
757 FQName childFqName;
758 if (!childFqName.setTo(childFqNameString)) {
759 return android::base::Error() << "Cannot parse " << childFqNameString << " as FQName";
760 }
761 FqInstance childFqInstance;
762 if (!childFqInstance.setTo(childFqName, fqInstance.getInstance())) {
763 return android::base::Error() << "Cannot merge " << childFqName.string() << "/"
764 << fqInstance.getInstance() << " as FqInstance";
765 continue;
766 }
767 if (!IsInstanceListed(listInstances, childFqInstance)) {
768 continue;
769 }
770 ret.push_back(childFqInstance);
771 }
772 return ret;
773}
774
775// Check if |fqInstance| is in |targetMatrix|; essentially equal to
776// targetMatrix.matchInstance(fqInstance), but provides richer error message. In details:
777// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
778// 2. package@x.z::interface/servedInstance is in targetMatrix but
779// servedInstance is not in listInstances(package@x.z::interface)
780android::base::Result<void> VintfObject::IsFqInstanceDeprecated(
781 const CompatibilityMatrix& targetMatrix, HalFormat format, const FqInstance& fqInstance,
782 const ListInstances& listInstances) {
783 // Find minimum package@x.? in target matrix, and check if instance is in target matrix.
784 bool foundInstance = false;
785 Version targetMatrixMinVer{SIZE_MAX, SIZE_MAX};
786 targetMatrix.forEachInstanceOfPackage(
787 format, fqInstance.getPackage(), [&](const auto& targetMatrixInstance) {
788 if (targetMatrixInstance.versionRange().majorVer == fqInstance.getMajorVersion() &&
789 targetMatrixInstance.interface() == fqInstance.getInterface() &&
790 targetMatrixInstance.matchInstance(fqInstance.getInstance())) {
791 targetMatrixMinVer =
792 std::min(targetMatrixMinVer, targetMatrixInstance.versionRange().minVer());
793 foundInstance = true;
794 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800795 return true;
Yifan Hong03ea4282020-03-17 17:58:35 -0700796 });
797 if (!foundInstance) {
798 return android::base::Error()
799 << fqInstance.string() << " is deprecated in compatibility matrix at FCM Version "
800 << targetMatrix.level() << "; it should not be served.";
801 }
802
803 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
804 bool targetVersionServed = false;
805 for (const auto& newPair :
806 listInstances(fqInstance.getPackage(), targetMatrixMinVer, fqInstance.getInterface(),
807 {fqInstance.getInstance()} /* instanceHint */)) {
808 if (newPair.first == fqInstance.getInstance()) {
809 targetVersionServed = true;
810 break;
Yifan Hongf73ba512018-01-17 15:52:30 -0800811 }
812 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700813
Yifan Hong03ea4282020-03-17 17:58:35 -0700814 if (!targetVersionServed) {
815 return android::base::Error()
816 << fqInstance.string() << " is deprecated; requires at least " << targetMatrixMinVer;
817 }
818 return {};
Yifan Hongf73ba512018-01-17 15:52:30 -0800819}
820
Yifan Hong03ea4282020-03-17 17:58:35 -0700821int32_t VintfObject::checkDeprecation(const ListInstances& listInstances,
822 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
823 std::string* error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700824 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong73bde592019-01-22 13:30:23 -0800825 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
826 if (matrixFragmentsStatus != OK) {
827 return matrixFragmentsStatus;
828 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800829 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800830 if (error && error->empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800831 *error = "Cannot get framework matrix for each FCM version for unknown error.";
Yifan Hong73bde592019-01-22 13:30:23 -0800832 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800833 return NAME_NOT_FOUND;
834 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700835 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800836 if (deviceManifest == nullptr) {
837 if (error) *error = "No device manifest.";
838 return NAME_NOT_FOUND;
839 }
840 Level deviceLevel = deviceManifest->level();
841 if (deviceLevel == Level::UNSPECIFIED) {
842 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
843 return BAD_VALUE;
844 }
845
846 const CompatibilityMatrix* targetMatrix = nullptr;
847 for (const auto& namedMatrix : matrixFragments) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700848 if (namedMatrix.level() == deviceLevel) {
849 targetMatrix = &namedMatrix;
Yifan Hongf73ba512018-01-17 15:52:30 -0800850 }
851 }
852 if (targetMatrix == nullptr) {
853 if (error)
854 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
855 return NAME_NOT_FOUND;
856 }
857
Yifan Hong03ea4282020-03-17 17:58:35 -0700858 std::multimap<std::string, std::string> childrenMap;
859 for (const auto& child : hidlMetadata) {
860 for (const auto& parent : child.inherited) {
861 childrenMap.emplace(parent, child.name);
862 }
863 }
864
865 // Find a list of possibly deprecated HALs by comparing |listInstances| with older matrices.
866 // Matrices with unspecified level are considered "current".
867 bool isDeprecated = false;
Yifan Hongf73ba512018-01-17 15:52:30 -0800868 for (const auto& namedMatrix : matrixFragments) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700869 if (namedMatrix.level() == Level::UNSPECIFIED) continue;
870 if (namedMatrix.level() >= deviceLevel) continue;
Yifan Hongf73ba512018-01-17 15:52:30 -0800871
Yifan Honga83d0e42020-04-13 13:07:31 -0700872 for (const MatrixHal& hal : namedMatrix.getHals()) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700873 if (IsHalDeprecated(hal, *targetMatrix, listInstances, childrenMap, error)) {
874 isDeprecated = true;
875 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800876 }
877 }
878
Yifan Hong03ea4282020-03-17 17:58:35 -0700879 return isDeprecated ? DEPRECATED : NO_DEPRECATED_HALS;
Yifan Hongf73ba512018-01-17 15:52:30 -0800880}
881
Yifan Hong03ea4282020-03-17 17:58:35 -0700882int32_t VintfObject::checkDeprecation(const std::vector<HidlInterfaceMetadata>& hidlMetadata,
883 std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800884 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700885 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700886 ListInstances inManifest =
887 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
888 const std::vector<std::string>& /* hintInstances */) {
889 std::vector<std::pair<std::string, Version>> ret;
890 deviceManifest->forEachInstanceOfInterface(
Yifan Hongac621482019-09-10 19:27:20 -0700891 HalFormat::HIDL, package, version, interface,
892 [&ret](const ManifestInstance& manifestInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700893 ret.push_back(
894 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
895 return true;
896 });
897 return ret;
898 };
Yifan Hong03ea4282020-03-17 17:58:35 -0700899 return checkDeprecation(inManifest, hidlMetadata, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800900}
Yifan Hong3daec812017-02-27 18:49:11 -0800901
Yifan Hong378c4082019-12-23 13:10:57 -0800902Level VintfObject::getKernelLevel(std::string* error) {
903 auto manifest = getDeviceHalManifest();
904 if (!manifest) {
905 if (error) *error = "Cannot retrieve device manifest.";
906 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700907 }
Yifan Hong378c4082019-12-23 13:10:57 -0800908 if (manifest->kernel().has_value() && manifest->kernel()->level() != Level::UNSPECIFIED) {
909 return manifest->kernel()->level();
Yifan Hong1e8febd2019-08-07 16:17:19 -0700910 }
Yifan Hong378c4082019-12-23 13:10:57 -0800911 if (error) *error = "Device manifest does not specify kernel FCM version.";
912 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700913}
914
Yifan Hong9f78c182018-07-12 14:45:52 -0700915const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
916 return mFileSystem;
917}
918
Yifan Hong9f78c182018-07-12 14:45:52 -0700919const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
920 return mPropertyFetcher;
921}
922
Yifan Hongd038b2b2018-11-27 13:57:56 -0800923const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700924 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700925}
926
Yifan Hong6b860852020-03-19 23:12:07 +0000927android::base::Result<bool> VintfObject::hasFrameworkCompatibilityMatrixExtensions() {
Yifan Honga83d0e42020-04-13 13:07:31 -0700928 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong6b860852020-03-19 23:12:07 +0000929 std::string error;
930 status_t status = getAllFrameworkMatrixLevels(&matrixFragments, &error);
931 if (status != OK) {
932 return android::base::Error(-status)
933 << "Cannot get all framework matrix fragments: " << error;
934 }
935 for (const auto& namedMatrix : matrixFragments) {
936 // Returns true if product matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700937 if (android::base::StartsWith(namedMatrix.fileName(), kProductVintfDir)) {
Yifan Hong6b860852020-03-19 23:12:07 +0000938 return true;
939 }
Yifan Hongf6ff4272020-03-12 22:56:16 -0700940 // Returns true if system_ext matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700941 if (android::base::StartsWith(namedMatrix.fileName(), kSystemExtVintfDir)) {
Yifan Hongf6ff4272020-03-12 22:56:16 -0700942 return true;
943 }
Yifan Hong6b860852020-03-19 23:12:07 +0000944 // Returns true if device system matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700945 if (android::base::StartsWith(namedMatrix.fileName(), kSystemVintfDir) &&
946 namedMatrix.level() == Level::UNSPECIFIED && !namedMatrix.getHals().empty()) {
Yifan Hong6b860852020-03-19 23:12:07 +0000947 return true;
948 }
949 }
950 return false;
951}
952
Yifan Hongd7924ff2020-03-17 14:09:10 -0700953android::base::Result<void> VintfObject::checkUnusedHals(
954 const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
Yifan Hong6b860852020-03-19 23:12:07 +0000955 auto matrix = getFrameworkCompatibilityMatrix();
956 if (matrix == nullptr) {
957 return android::base::Error(-NAME_NOT_FOUND) << "Missing framework matrix.";
958 }
959 auto manifest = getDeviceHalManifest();
960 if (manifest == nullptr) {
961 return android::base::Error(-NAME_NOT_FOUND) << "Missing device manifest.";
962 }
Yifan Hongd7924ff2020-03-17 14:09:10 -0700963 auto unused = manifest->checkUnusedHals(*matrix, hidlMetadata);
Yifan Hong6b860852020-03-19 23:12:07 +0000964 if (!unused.empty()) {
965 return android::base::Error()
966 << "The following instances are in the device manifest but "
967 << "not specified in framework compatibility matrix: \n"
968 << " " << android::base::Join(unused, "\n ") << "\n"
969 << "Suggested fix:\n"
Yifan Hong5a220b12020-04-07 15:22:24 -0700970 << "1. Update deprecated HALs to the latest version.\n"
971 << "2. Check for any typos in device manifest or framework compatibility "
Yifan Hong6b860852020-03-19 23:12:07 +0000972 << "matrices with FCM version >= " << matrix->level() << ".\n"
Yifan Hong5a220b12020-04-07 15:22:24 -0700973 << "3. For new platform HALs, add them to any framework compatibility matrix "
974 << "with FCM version >= " << matrix->level() << " where applicable.\n"
975 << "4. For device-specific HALs, add to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
Yifan Hong6b860852020-03-19 23:12:07 +0000976 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE.";
977 }
978 return {};
979}
980
Yifan Hong78f5b572018-11-27 14:05:03 -0800981// make_unique does not work because VintfObject constructor is private.
982VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {}
983
984VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
985 mObject->mFileSystem = std::move(e);
986 return *this;
987}
988
989VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory(
990 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
991 mObject->mRuntimeInfoFactory = std::move(e);
992 return *this;
993}
994
995VintfObject::Builder& VintfObject::Builder::setPropertyFetcher(
996 std::unique_ptr<PropertyFetcher>&& e) {
997 mObject->mPropertyFetcher = std::move(e);
998 return *this;
999}
1000
1001std::unique_ptr<VintfObject> VintfObject::Builder::build() {
1002 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
1003 if (!mObject->mRuntimeInfoFactory)
1004 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
1005 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
1006 return std::move(mObject);
1007}
1008
Yifan Hong3daec812017-02-27 18:49:11 -08001009} // namespace vintf
1010} // namespace android