blob: 71588af2791e7cc0c723931b1105f19e05c8d505 [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 Honga192fa52020-07-21 12:09:20 -0700499std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(RuntimeInfo::FetchFlags flags) {
500 return GetInstance()->getRuntimeInfo(flags);
Yifan Hong9f78c182018-07-12 14:45:52 -0700501}
Yifan Honga192fa52020-07-21 12:09:20 -0700502std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700503 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700504
Yifan Honga192fa52020-07-21 12:09:20 -0700505 // Skip fetching information that has already been fetched previously.
506 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700507
Yifan Hong9f78c182018-07-12 14:45:52 -0700508 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800509 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700510 }
511
Yifan Hongf3247982019-12-12 12:11:36 -0800512 // Fetch kernel FCM version from device HAL manifest and store it in RuntimeInfo too.
513 if ((flags & RuntimeInfo::FetchFlag::KERNEL_FCM) != 0) {
514 auto manifest = getDeviceHalManifest();
515 if (!manifest) {
516 mDeviceRuntimeInfo.fetchedFlags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
517 return nullptr;
518 }
519 Level level = Level::UNSPECIFIED;
520 if (manifest->kernel().has_value()) {
521 level = manifest->kernel()->level();
522 }
523 mDeviceRuntimeInfo.object->setKernelLevel(level);
524 flags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
525 }
526
Yifan Hong9f78c182018-07-12 14:45:52 -0700527 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700528 if (status != OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700529 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
Yifan Hong1fb004e2017-09-26 15:04:44 -0700530 return nullptr;
531 }
532
Yifan Hong9f78c182018-07-12 14:45:52 -0700533 mDeviceRuntimeInfo.fetchedFlags |= flags;
534 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800535}
536
Yifan Hong12e23c22018-11-05 14:53:30 -0800537int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
538 status_t status = OK;
539 // null checks for files and runtime info
540 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700541 appendLine(error, "No framework manifest file from device or from update package");
542 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700543 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800544 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700545 appendLine(error, "No device manifest file from device or from update package");
546 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700547 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800548 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700549 appendLine(error, "No framework matrix file from device or from update package");
550 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700551 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800552 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700553 appendLine(error, "No device matrix file from device or from update package");
554 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700555 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800556
Yifan Hong072f12d2018-08-08 13:04:51 -0700557 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800558 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700559 appendLine(error, "No runtime info from device");
560 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800561 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700562 }
Yifan Hong878556e2018-07-13 13:45:30 -0700563 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700564
565 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800566 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800567 if (error) {
568 error->insert(0,
569 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700570 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800571 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700572 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800573 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800574 if (error) {
575 error->insert(0,
576 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700577 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800578 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700579 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800580
Yifan Hong072f12d2018-08-08 13:04:51 -0700581 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800582 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
Yifan Hong85e589e2019-12-18 13:12:29 -0800583 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800584 if (error) {
585 error->insert(0,
586 "Runtime info and framework compatibility matrix are incompatible: ");
587 }
588 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700589 }
590 }
591
592 return COMPATIBLE;
593}
594
Yifan Hong9f78c182018-07-12 14:45:52 -0700595namespace details {
596
Yifan Hong270b5652018-01-18 18:46:01 -0800597const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800598const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800599const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong73bde592019-01-22 13:30:23 -0800600const std::string kProductVintfDir = "/product/etc/vintf/";
Yifan Hong5bbc4ae2020-01-09 14:30:27 -0800601const std::string kSystemExtVintfDir = "/system_ext/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800602
603const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800604const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800605const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800606const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong73bde592019-01-22 13:30:23 -0800607const std::string kProductMatrix = kProductVintfDir + "compatibility_matrix.xml";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700608const std::string kProductManifest = kProductVintfDir + "manifest.xml";
Yifan Hong659feac2020-03-19 18:09:54 -0700609const std::string kSystemExtManifest = kSystemExtVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800610
Steven Morelandeedf2d42018-04-04 16:36:27 -0700611const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/";
612const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/";
613const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700614const std::string kProductManifestFragmentDir = kProductVintfDir + "manifest/";
Yifan Hong659feac2020-03-19 18:09:54 -0700615const std::string kSystemExtManifestFragmentDir = kSystemExtVintfDir + "manifest/";
Steven Morelandeedf2d42018-04-04 16:36:27 -0700616
Yifan Hong270b5652018-01-18 18:46:01 -0800617const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
618const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800619const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800620const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
621const std::string kOdmLegacyVintfDir = "/odm/etc/";
622const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
623
Yifan Hong69c1b112018-02-27 17:06:00 -0800624std::vector<std::string> dumpFileList() {
625 return {
Yifan Hong73bde592019-01-22 13:30:23 -0800626 // clang-format off
627 kSystemVintfDir,
628 kVendorVintfDir,
629 kOdmVintfDir,
630 kProductVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800631 kSystemExtVintfDir,
Yifan Hong73bde592019-01-22 13:30:23 -0800632 kOdmLegacyVintfDir,
633 kVendorLegacyManifest,
634 kVendorLegacyMatrix,
635 kSystemLegacyManifest,
636 kSystemLegacyMatrix,
637 // clang-format on
Yifan Hong69c1b112018-02-27 17:06:00 -0800638 };
639}
640
Yifan Hong9f78c182018-07-12 14:45:52 -0700641} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700642
Yifan Hong9f78c182018-07-12 14:45:52 -0700643bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800644 const CompatibilityMatrix& targetMatrix,
Yifan Hong03ea4282020-03-17 17:58:35 -0700645 const ListInstances& listInstances,
646 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700647 bool isDeprecated = false;
648 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700649 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, childrenMap,
650 appendedError)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700651 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800652 }
Yifan Hong03ea4282020-03-17 17:58:35 -0700653 return true; // continue to check next instance
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700654 });
655 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800656}
657
Yifan Hong03ea4282020-03-17 17:58:35 -0700658// Let oldMatrixInstance = package@x.y-w::interface/instancePattern.
659// If any "@servedVersion::interface/servedInstance" in listInstances(package@x.y::interface)
660// matches instancePattern, return true iff for all child interfaces (from
661// GetListedInstanceInheritance), IsFqInstanceDeprecated returns false.
Yifan Hong9f78c182018-07-12 14:45:52 -0700662bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800663 const CompatibilityMatrix& targetMatrix,
Yifan Hong03ea4282020-03-17 17:58:35 -0700664 const ListInstances& listInstances,
665 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700666 const std::string& package = oldMatrixInstance.package();
667 const Version& version = oldMatrixInstance.versionRange().minVer();
668 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700669
Yifan Honga8a8fa92018-03-20 14:42:43 -0700670 std::vector<std::string> instanceHint;
671 if (!oldMatrixInstance.isRegex()) {
672 instanceHint.push_back(oldMatrixInstance.exactInstance());
673 }
674
Yifan Hong03ea4282020-03-17 17:58:35 -0700675 std::vector<std::string> accumulatedErrors;
Yifan Honga8a8fa92018-03-20 14:42:43 -0700676 auto list = listInstances(package, version, interface, instanceHint);
Yifan Hong03ea4282020-03-17 17:58:35 -0700677
Yifan Honga8a8fa92018-03-20 14:42:43 -0700678 for (const auto& pair : list) {
679 const std::string& servedInstance = pair.first;
680 Version servedVersion = pair.second;
Yifan Hong03ea4282020-03-17 17:58:35 -0700681 std::string servedFqInstanceString =
682 toFQNameString(package, servedVersion, interface, servedInstance);
Yifan Honga8a8fa92018-03-20 14:42:43 -0700683 if (!oldMatrixInstance.matchInstance(servedInstance)) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700684 // ignore unrelated instance
Yifan Honga8a8fa92018-03-20 14:42:43 -0700685 continue;
686 }
687
Yifan Hong03ea4282020-03-17 17:58:35 -0700688 auto inheritance = GetListedInstanceInheritance(package, servedVersion, interface,
689 servedInstance, listInstances, childrenMap);
690 if (!inheritance.has_value()) {
691 accumulatedErrors.push_back(inheritance.error().message());
692 continue;
Yifan Hongf73ba512018-01-17 15:52:30 -0800693 }
694
Yifan Hong03ea4282020-03-17 17:58:35 -0700695 std::vector<std::string> errors;
696 for (const auto& fqInstance : *inheritance) {
697 auto result = IsFqInstanceDeprecated(targetMatrix, oldMatrixInstance.format(),
698 fqInstance, listInstances);
699 if (result.ok()) {
700 errors.clear();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700701 break;
702 }
Yifan Hong03ea4282020-03-17 17:58:35 -0700703 errors.push_back(result.error().message());
Yifan Honga8a8fa92018-03-20 14:42:43 -0700704 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800705
Yifan Hong03ea4282020-03-17 17:58:35 -0700706 if (errors.empty()) {
707 continue;
708 }
709 accumulatedErrors.insert(accumulatedErrors.end(), errors.begin(), errors.end());
710 }
711
712 if (accumulatedErrors.empty()) {
713 return false;
714 }
715 appendLine(appendedError, android::base::Join(accumulatedErrors, "\n"));
716 return true;
717}
718
719// Check if fqInstance is listed in |listInstances|.
720bool VintfObject::IsInstanceListed(const ListInstances& listInstances,
721 const FqInstance& fqInstance) {
722 auto list =
723 listInstances(fqInstance.getPackage(), fqInstance.getVersion(), fqInstance.getInterface(),
724 {fqInstance.getInstance()} /* instanceHint*/);
725 return std::any_of(list.begin(), list.end(),
726 [&](const auto& pair) { return pair.first == fqInstance.getInstance(); });
727}
728
729// Return a list of FqInstance, where each element:
730// - is listed in |listInstances|; AND
731// - is, or inherits from, package@version::interface/instance (as specified by |childrenMap|)
732android::base::Result<std::vector<FqInstance>> VintfObject::GetListedInstanceInheritance(
733 const std::string& package, const Version& version, const std::string& interface,
734 const std::string& instance, const ListInstances& listInstances,
735 const ChildrenMap& childrenMap) {
736 FqInstance fqInstance;
737 if (!fqInstance.setTo(package, version.majorVer, version.minorVer, interface, instance)) {
738 return android::base::Error() << toFQNameString(package, version, interface, instance)
739 << " is not a valid FqInstance";
740 }
741
742 if (!IsInstanceListed(listInstances, fqInstance)) {
743 return {};
744 }
745
746 const FQName& fqName = fqInstance.getFqName();
747
748 std::vector<FqInstance> ret;
749 ret.push_back(fqInstance);
750
751 auto childRange = childrenMap.equal_range(fqName.string());
752 for (auto it = childRange.first; it != childRange.second; ++it) {
753 const auto& childFqNameString = it->second;
754 FQName childFqName;
755 if (!childFqName.setTo(childFqNameString)) {
756 return android::base::Error() << "Cannot parse " << childFqNameString << " as FQName";
757 }
758 FqInstance childFqInstance;
759 if (!childFqInstance.setTo(childFqName, fqInstance.getInstance())) {
760 return android::base::Error() << "Cannot merge " << childFqName.string() << "/"
761 << fqInstance.getInstance() << " as FqInstance";
762 continue;
763 }
764 if (!IsInstanceListed(listInstances, childFqInstance)) {
765 continue;
766 }
767 ret.push_back(childFqInstance);
768 }
769 return ret;
770}
771
772// Check if |fqInstance| is in |targetMatrix|; essentially equal to
773// targetMatrix.matchInstance(fqInstance), but provides richer error message. In details:
774// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
775// 2. package@x.z::interface/servedInstance is in targetMatrix but
776// servedInstance is not in listInstances(package@x.z::interface)
777android::base::Result<void> VintfObject::IsFqInstanceDeprecated(
778 const CompatibilityMatrix& targetMatrix, HalFormat format, const FqInstance& fqInstance,
779 const ListInstances& listInstances) {
780 // Find minimum package@x.? in target matrix, and check if instance is in target matrix.
781 bool foundInstance = false;
782 Version targetMatrixMinVer{SIZE_MAX, SIZE_MAX};
783 targetMatrix.forEachInstanceOfPackage(
784 format, fqInstance.getPackage(), [&](const auto& targetMatrixInstance) {
785 if (targetMatrixInstance.versionRange().majorVer == fqInstance.getMajorVersion() &&
786 targetMatrixInstance.interface() == fqInstance.getInterface() &&
787 targetMatrixInstance.matchInstance(fqInstance.getInstance())) {
788 targetMatrixMinVer =
789 std::min(targetMatrixMinVer, targetMatrixInstance.versionRange().minVer());
790 foundInstance = true;
791 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800792 return true;
Yifan Hong03ea4282020-03-17 17:58:35 -0700793 });
794 if (!foundInstance) {
795 return android::base::Error()
796 << fqInstance.string() << " is deprecated in compatibility matrix at FCM Version "
797 << targetMatrix.level() << "; it should not be served.";
798 }
799
800 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
801 bool targetVersionServed = false;
802 for (const auto& newPair :
803 listInstances(fqInstance.getPackage(), targetMatrixMinVer, fqInstance.getInterface(),
804 {fqInstance.getInstance()} /* instanceHint */)) {
805 if (newPair.first == fqInstance.getInstance()) {
806 targetVersionServed = true;
807 break;
Yifan Hongf73ba512018-01-17 15:52:30 -0800808 }
809 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700810
Yifan Hong03ea4282020-03-17 17:58:35 -0700811 if (!targetVersionServed) {
812 return android::base::Error()
813 << fqInstance.string() << " is deprecated; requires at least " << targetMatrixMinVer;
814 }
815 return {};
Yifan Hongf73ba512018-01-17 15:52:30 -0800816}
817
Yifan Hong03ea4282020-03-17 17:58:35 -0700818int32_t VintfObject::checkDeprecation(const ListInstances& listInstances,
819 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
820 std::string* error) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700821 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong73bde592019-01-22 13:30:23 -0800822 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
823 if (matrixFragmentsStatus != OK) {
824 return matrixFragmentsStatus;
825 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800826 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800827 if (error && error->empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800828 *error = "Cannot get framework matrix for each FCM version for unknown error.";
Yifan Hong73bde592019-01-22 13:30:23 -0800829 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800830 return NAME_NOT_FOUND;
831 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700832 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800833 if (deviceManifest == nullptr) {
834 if (error) *error = "No device manifest.";
835 return NAME_NOT_FOUND;
836 }
837 Level deviceLevel = deviceManifest->level();
838 if (deviceLevel == Level::UNSPECIFIED) {
839 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
840 return BAD_VALUE;
841 }
842
843 const CompatibilityMatrix* targetMatrix = nullptr;
844 for (const auto& namedMatrix : matrixFragments) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700845 if (namedMatrix.level() == deviceLevel) {
846 targetMatrix = &namedMatrix;
Yifan Hongf73ba512018-01-17 15:52:30 -0800847 }
848 }
849 if (targetMatrix == nullptr) {
850 if (error)
851 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
852 return NAME_NOT_FOUND;
853 }
854
Yifan Hong03ea4282020-03-17 17:58:35 -0700855 std::multimap<std::string, std::string> childrenMap;
856 for (const auto& child : hidlMetadata) {
857 for (const auto& parent : child.inherited) {
858 childrenMap.emplace(parent, child.name);
859 }
860 }
861
862 // Find a list of possibly deprecated HALs by comparing |listInstances| with older matrices.
863 // Matrices with unspecified level are considered "current".
864 bool isDeprecated = false;
Yifan Hongf73ba512018-01-17 15:52:30 -0800865 for (const auto& namedMatrix : matrixFragments) {
Yifan Honga83d0e42020-04-13 13:07:31 -0700866 if (namedMatrix.level() == Level::UNSPECIFIED) continue;
867 if (namedMatrix.level() >= deviceLevel) continue;
Yifan Hongf73ba512018-01-17 15:52:30 -0800868
Yifan Honga83d0e42020-04-13 13:07:31 -0700869 for (const MatrixHal& hal : namedMatrix.getHals()) {
Yifan Hong03ea4282020-03-17 17:58:35 -0700870 if (IsHalDeprecated(hal, *targetMatrix, listInstances, childrenMap, error)) {
871 isDeprecated = true;
872 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800873 }
874 }
875
Yifan Hong03ea4282020-03-17 17:58:35 -0700876 return isDeprecated ? DEPRECATED : NO_DEPRECATED_HALS;
Yifan Hongf73ba512018-01-17 15:52:30 -0800877}
878
Yifan Hong03ea4282020-03-17 17:58:35 -0700879int32_t VintfObject::checkDeprecation(const std::vector<HidlInterfaceMetadata>& hidlMetadata,
880 std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800881 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700882 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700883 ListInstances inManifest =
884 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
885 const std::vector<std::string>& /* hintInstances */) {
886 std::vector<std::pair<std::string, Version>> ret;
887 deviceManifest->forEachInstanceOfInterface(
Yifan Hongac621482019-09-10 19:27:20 -0700888 HalFormat::HIDL, package, version, interface,
889 [&ret](const ManifestInstance& manifestInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700890 ret.push_back(
891 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
892 return true;
893 });
894 return ret;
895 };
Yifan Hong03ea4282020-03-17 17:58:35 -0700896 return checkDeprecation(inManifest, hidlMetadata, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800897}
Yifan Hong3daec812017-02-27 18:49:11 -0800898
Yifan Hong378c4082019-12-23 13:10:57 -0800899Level VintfObject::getKernelLevel(std::string* error) {
900 auto manifest = getDeviceHalManifest();
901 if (!manifest) {
902 if (error) *error = "Cannot retrieve device manifest.";
903 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700904 }
Yifan Hong378c4082019-12-23 13:10:57 -0800905 if (manifest->kernel().has_value() && manifest->kernel()->level() != Level::UNSPECIFIED) {
906 return manifest->kernel()->level();
Yifan Hong1e8febd2019-08-07 16:17:19 -0700907 }
Yifan Hong378c4082019-12-23 13:10:57 -0800908 if (error) *error = "Device manifest does not specify kernel FCM version.";
909 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700910}
911
Yifan Hong9f78c182018-07-12 14:45:52 -0700912const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
913 return mFileSystem;
914}
915
Yifan Hong9f78c182018-07-12 14:45:52 -0700916const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
917 return mPropertyFetcher;
918}
919
Yifan Hongd038b2b2018-11-27 13:57:56 -0800920const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700921 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700922}
923
Yifan Hong6b860852020-03-19 23:12:07 +0000924android::base::Result<bool> VintfObject::hasFrameworkCompatibilityMatrixExtensions() {
Yifan Honga83d0e42020-04-13 13:07:31 -0700925 std::vector<CompatibilityMatrix> matrixFragments;
Yifan Hong6b860852020-03-19 23:12:07 +0000926 std::string error;
927 status_t status = getAllFrameworkMatrixLevels(&matrixFragments, &error);
928 if (status != OK) {
929 return android::base::Error(-status)
930 << "Cannot get all framework matrix fragments: " << error;
931 }
932 for (const auto& namedMatrix : matrixFragments) {
933 // Returns true if product matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700934 if (android::base::StartsWith(namedMatrix.fileName(), kProductVintfDir)) {
Yifan Hong6b860852020-03-19 23:12:07 +0000935 return true;
936 }
Yifan Hongf6ff4272020-03-12 22:56:16 -0700937 // Returns true if system_ext matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700938 if (android::base::StartsWith(namedMatrix.fileName(), kSystemExtVintfDir)) {
Yifan Hongf6ff4272020-03-12 22:56:16 -0700939 return true;
940 }
Yifan Hong6b860852020-03-19 23:12:07 +0000941 // Returns true if device system matrix exists.
Yifan Honga83d0e42020-04-13 13:07:31 -0700942 if (android::base::StartsWith(namedMatrix.fileName(), kSystemVintfDir) &&
943 namedMatrix.level() == Level::UNSPECIFIED && !namedMatrix.getHals().empty()) {
Yifan Hong6b860852020-03-19 23:12:07 +0000944 return true;
945 }
946 }
947 return false;
948}
949
Yifan Hongd7924ff2020-03-17 14:09:10 -0700950android::base::Result<void> VintfObject::checkUnusedHals(
951 const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
Yifan Hong6b860852020-03-19 23:12:07 +0000952 auto matrix = getFrameworkCompatibilityMatrix();
953 if (matrix == nullptr) {
954 return android::base::Error(-NAME_NOT_FOUND) << "Missing framework matrix.";
955 }
956 auto manifest = getDeviceHalManifest();
957 if (manifest == nullptr) {
958 return android::base::Error(-NAME_NOT_FOUND) << "Missing device manifest.";
959 }
Yifan Hongd7924ff2020-03-17 14:09:10 -0700960 auto unused = manifest->checkUnusedHals(*matrix, hidlMetadata);
Yifan Hong6b860852020-03-19 23:12:07 +0000961 if (!unused.empty()) {
962 return android::base::Error()
963 << "The following instances are in the device manifest but "
964 << "not specified in framework compatibility matrix: \n"
965 << " " << android::base::Join(unused, "\n ") << "\n"
966 << "Suggested fix:\n"
Yifan Hong5a220b12020-04-07 15:22:24 -0700967 << "1. Update deprecated HALs to the latest version.\n"
968 << "2. Check for any typos in device manifest or framework compatibility "
Yifan Hong6b860852020-03-19 23:12:07 +0000969 << "matrices with FCM version >= " << matrix->level() << ".\n"
Yifan Hong5a220b12020-04-07 15:22:24 -0700970 << "3. For new platform HALs, add them to any framework compatibility matrix "
971 << "with FCM version >= " << matrix->level() << " where applicable.\n"
972 << "4. For device-specific HALs, add to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
Yifan Hong6b860852020-03-19 23:12:07 +0000973 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE.";
974 }
975 return {};
976}
977
Yifan Hong78f5b572018-11-27 14:05:03 -0800978// make_unique does not work because VintfObject constructor is private.
979VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {}
980
981VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
982 mObject->mFileSystem = std::move(e);
983 return *this;
984}
985
986VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory(
987 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
988 mObject->mRuntimeInfoFactory = std::move(e);
989 return *this;
990}
991
992VintfObject::Builder& VintfObject::Builder::setPropertyFetcher(
993 std::unique_ptr<PropertyFetcher>&& e) {
994 mObject->mPropertyFetcher = std::move(e);
995 return *this;
996}
997
998std::unique_ptr<VintfObject> VintfObject::Builder::build() {
999 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
1000 if (!mObject->mRuntimeInfoFactory)
1001 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
1002 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
1003 return std::move(mObject);
1004}
1005
Yifan Hong3daec812017-02-27 18:49:11 -08001006} // namespace vintf
1007} // namespace android