blob: 2c7c9fa259d14d944d96110e85231f2c0ab966b0 [file] [log] [blame]
Yifan Hong3daec812017-02-27 18:49:11 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "VintfObject.h"
18
Yifan Hongd52bf3e2018-01-11 16:56:51 -080019#include <dirent.h>
20
Yifan Hongb712e7e2020-03-17 17:58:35 -070021#include <algorithm>
Yifan Hong3daec812017-02-27 18:49:11 -080022#include <functional>
23#include <memory>
24#include <mutex>
25
Yifan Hong60217032018-01-08 16:19:42 -080026#include <android-base/logging.h>
Yifan Hong6e32a5f2020-03-12 16:06:27 -070027#include <android-base/result.h>
Yifan Hongb02d87d2019-12-19 11:15:27 -080028#include <android-base/strings.h>
Yifan Hongff1a25c2020-03-17 14:09:10 -070029#include <hidl/metadata.h>
Yifan Hong60217032018-01-08 16:19:42 -080030
Yifan Hong12e23c22018-11-05 14:53:30 -080031#include "CompatibilityMatrix.h"
Yifan Hong12e23c22018-11-05 14:53:30 -080032#include "parse_string.h"
33#include "parse_xml.h"
34#include "utils.h"
35
Yifan Hong60217032018-01-08 16:19:42 -080036using std::placeholders::_1;
37using std::placeholders::_2;
38
Yifan Hong3daec812017-02-27 18:49:11 -080039namespace android {
40namespace vintf {
41
Yifan Hong270b5652018-01-18 18:46:01 -080042using namespace details;
43
Yifan Hong9f78c182018-07-12 14:45:52 -070044#ifdef LIBVINTF_TARGET
45static constexpr bool kIsTarget = true;
46#else
47static constexpr bool kIsTarget = false;
48#endif
Yifan Hong1fb004e2017-09-26 15:04:44 -070049
Yifan Hong3daec812017-02-27 18:49:11 -080050template <typename T, typename F>
Yifan Hongfc73edf2017-08-29 11:39:07 -070051static std::shared_ptr<const T> Get(
52 LockedSharedPtr<T> *ptr,
Yifan Hong143cfe62017-04-13 20:18:01 -070053 bool skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080054 const F &fetchAllInformation) {
55 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong7219ba12018-01-08 16:23:49 -080056 if (skipCache || !ptr->fetchedOnce) {
Yifan Hong3daec812017-02-27 18:49:11 -080057 ptr->object = std::make_unique<T>();
Yifan Hong60217032018-01-08 16:19:42 -080058 std::string error;
Steven Moreland15ccd342020-03-27 15:48:40 -070059 status_t status = fetchAllInformation(ptr->object.get(), &error);
60 if (status != OK) {
61 LOG(WARNING) << status << ": " << error;
Yifan Hong3daec812017-02-27 18:49:11 -080062 ptr->object = nullptr; // frees the old object
63 }
Yifan Hong7219ba12018-01-08 16:23:49 -080064 ptr->fetchedOnce = true;
Yifan Hong3daec812017-02-27 18:49:11 -080065 }
Yifan Hongfc73edf2017-08-29 11:39:07 -070066 return ptr->object;
Yifan Hong3daec812017-02-27 18:49:11 -080067}
68
Yifan Hong9f78c182018-07-12 14:45:52 -070069static std::unique_ptr<FileSystem> createDefaultFileSystem() {
70 std::unique_ptr<FileSystem> fileSystem;
71 if (kIsTarget) {
72 fileSystem = std::make_unique<details::FileSystemImpl>();
73 } else {
74 fileSystem = std::make_unique<details::FileSystemNoOp>();
75 }
76 return fileSystem;
77}
78
79static std::unique_ptr<PropertyFetcher> createDefaultPropertyFetcher() {
80 std::unique_ptr<PropertyFetcher> propertyFetcher;
81 if (kIsTarget) {
82 propertyFetcher = std::make_unique<details::PropertyFetcherImpl>();
83 } else {
84 propertyFetcher = std::make_unique<details::PropertyFetcherNoOp>();
85 }
86 return propertyFetcher;
87}
88
Yifan Hong9f78c182018-07-12 14:45:52 -070089std::shared_ptr<VintfObject> VintfObject::GetInstance() {
Steven Moreland954c4642020-02-26 17:03:37 -080090 static details::LockedSharedPtr<VintfObject> sInstance{};
Yifan Hong9f78c182018-07-12 14:45:52 -070091 std::unique_lock<std::mutex> lock(sInstance.mutex);
92 if (sInstance.object == nullptr) {
Yifan Hong78f5b572018-11-27 14:05:03 -080093 sInstance.object = std::shared_ptr<VintfObject>(VintfObject::Builder().build().release());
Yifan Hong9f78c182018-07-12 14:45:52 -070094 }
95 return sInstance.object;
96}
97
Yifan Hongfc73edf2017-08-29 11:39:07 -070098std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -070099 return GetInstance()->getDeviceHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -0800100}
101
Yifan Hong9f78c182018-07-12 14:45:52 -0700102std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest(bool skipCache) {
103 return Get(&mDeviceManifest, skipCache,
104 std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2));
105}
106
Yifan Hongfc73edf2017-08-29 11:39:07 -0700107std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700108 return GetInstance()->getFrameworkHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -0800109}
110
Yifan Hong9f78c182018-07-12 14:45:52 -0700111std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest(bool skipCache) {
112 return Get(&mFrameworkManifest, skipCache,
113 std::bind(&VintfObject::fetchFrameworkHalManifest, this, _1, _2));
114}
Yifan Hong2272bf82017-04-28 14:37:56 -0700115
Yifan Hongfc73edf2017-08-29 11:39:07 -0700116std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700117 return GetInstance()->getDeviceCompatibilityMatrix(skipCache);
Yifan Hong2272bf82017-04-28 14:37:56 -0700118}
119
Yifan Hong9f78c182018-07-12 14:45:52 -0700120std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix(
121 bool skipCache) {
122 return Get(&mDeviceMatrix, skipCache, std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2));
123}
124
Yifan Hongfc73edf2017-08-29 11:39:07 -0700125std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700126 return GetInstance()->getFrameworkCompatibilityMatrix(skipCache);
127}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800128
Yifan Hong9f78c182018-07-12 14:45:52 -0700129std::shared_ptr<const CompatibilityMatrix> VintfObject::getFrameworkCompatibilityMatrix(
130 bool skipCache) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800131 // To avoid deadlock, get device manifest before any locks.
Yifan Hong9f78c182018-07-12 14:45:52 -0700132 auto deviceManifest = getDeviceHalManifest();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800133
Yifan Hong9f78c182018-07-12 14:45:52 -0700134 std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800135
136 auto combined =
Yifan Hong9f78c182018-07-12 14:45:52 -0700137 Get(&mCombinedFrameworkMatrix, skipCache,
138 std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest, _1, _2));
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800139 if (combined != nullptr) {
140 return combined;
141 }
142
Yifan Hong9f78c182018-07-12 14:45:52 -0700143 return Get(&mFrameworkMatrix, skipCache,
Yifan Hong12e23c22018-11-05 14:53:30 -0800144 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(),
Yifan Hong9f78c182018-07-12 14:45:52 -0700145 kSystemLegacyMatrix, _2));
Yifan Hong2272bf82017-04-28 14:37:56 -0700146}
147
Yifan Hong9f78c182018-07-12 14:45:52 -0700148status_t VintfObject::getCombinedFrameworkMatrix(
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800149 const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out,
150 std::string* error) {
Yifan Hong73bde592019-01-22 13:30:23 -0800151 std::vector<Named<CompatibilityMatrix>> matrixFragments;
152 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
153 if (matrixFragmentsStatus != OK) {
154 return matrixFragmentsStatus;
155 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800156 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800157 if (error && error->empty()) {
158 *error = "Cannot get framework matrix for each FCM version for unknown error.";
159 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800160 return NAME_NOT_FOUND;
161 }
162
163 Level deviceLevel = Level::UNSPECIFIED;
164
165 if (deviceManifest != nullptr) {
166 deviceLevel = deviceManifest->level();
167 }
168
169 // TODO(b/70628538): Do not infer from Shipping API level.
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800170 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800171 auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800172 if (shippingApi != 0u) {
173 deviceLevel = details::convertFromApiLevel(shippingApi);
174 }
175 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800176
177 if (deviceLevel == Level::UNSPECIFIED) {
178 // Cannot infer FCM version. Combine all matrices by assuming
179 // Shipping FCM Version == min(all supported FCM Versions in the framework)
180 for (auto&& pair : matrixFragments) {
181 Level fragmentLevel = pair.object.level();
182 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
183 deviceLevel = fragmentLevel;
184 }
185 }
186 }
187
188 if (deviceLevel == Level::UNSPECIFIED) {
189 // None of the fragments specify any FCM version. Should never happen except
190 // for inconsistent builds.
191 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800192 *error = "No framework compatibility matrix files under " + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800193 " declare FCM version.";
194 }
195 return NAME_NOT_FOUND;
196 }
197
Yifan Honge7837b12018-10-11 10:38:57 -0700198 auto combined = CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800199 if (combined == nullptr) {
200 return BAD_VALUE;
201 }
202 *out = std::move(*combined);
203 return OK;
204}
205
Steven Morelandeedf2d42018-04-04 16:36:27 -0700206// Load and combine all of the manifests in a directory
Yifan Hong9f78c182018-07-12 14:45:52 -0700207status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
Steven Morelandeedf2d42018-04-04 16:36:27 -0700208 std::string* error) {
209 std::vector<std::string> fileNames;
Yifan Hong12e23c22018-11-05 14:53:30 -0800210 status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700211 // if the directory isn't there, that's okay
212 if (err == NAME_NOT_FOUND) return OK;
213 if (err != OK) return err;
214
215 for (const std::string& file : fileNames) {
216 // Only adds HALs because all other things are added by libvintf
217 // itself for now.
218 HalManifest fragmentManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700219 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700220 if (err != OK) return err;
221
Yifan Hong4c6962d2019-01-30 15:50:46 -0800222 if (!manifest->addAll(&fragmentManifest, error)) {
223 if (error) {
224 error->insert(0, "Cannot add manifest fragment " + directory + file + ":");
225 }
226 return UNKNOWN_ERROR;
227 }
Steven Morelandeedf2d42018-04-04 16:36:27 -0700228 }
229
230 return OK;
231}
232
Yifan Hong5a93bf22018-01-17 18:22:32 -0800233// Priority for loading vendor manifest:
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800234// 1. Vendor manifest + device fragments + ODM manifest (optional) + odm fragments
235// 2. Vendor manifest + device fragments
Steven Morelandeedf2d42018-04-04 16:36:27 -0700236// 3. ODM manifest (optional) + odm fragments
237// 4. /vendor/manifest.xml (legacy, no fragments)
Yifan Hong5a93bf22018-01-17 18:22:32 -0800238// where:
Steven Moreland30a532c2018-11-01 08:46:32 -0700239// A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
240// over A.
Yifan Hong9f78c182018-07-12 14:45:52 -0700241status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800242 HalManifest vendorManifest;
243 status_t vendorStatus = fetchVendorHalManifest(&vendorManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800244 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
245 return vendorStatus;
246 }
247
Steven Morelandeedf2d42018-04-04 16:36:27 -0700248 if (vendorStatus == OK) {
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800249 *out = std::move(vendorManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700250 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700251 if (fragmentStatus != OK) {
252 return fragmentStatus;
253 }
254 }
255
Yifan Hong5a93bf22018-01-17 18:22:32 -0800256 HalManifest odmManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700257 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800258 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
259 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800260 }
261
Yifan Hong5a93bf22018-01-17 18:22:32 -0800262 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800263 if (odmStatus == OK) {
Yifan Hong4c6962d2019-01-30 15:50:46 -0800264 if (!out->addAll(&odmManifest, error)) {
265 if (error) {
266 error->insert(0, "Cannot add ODM manifest :");
267 }
268 return UNKNOWN_ERROR;
269 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800270 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700271 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800272 }
273
Yifan Hong12a11ac2018-01-19 13:58:32 -0800274 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800275 if (odmStatus == OK) {
276 *out = std::move(odmManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700277 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800278 }
279
280 // Use legacy /vendor/manifest.xml
Yifan Hong12e23c22018-11-05 14:53:30 -0800281 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800282}
283
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800284// Priority:
285// 1. if {vendorSku} is defined, /vendor/etc/vintf/manifest_{vendorSku}.xml
286// 2. /vendor/etc/vintf/manifest.xml
287// where:
288// {vendorSku} is the value of ro.boot.product.vendor.sku
289status_t VintfObject::fetchVendorHalManifest(HalManifest* out, std::string* error) {
290 status_t status;
291
292 std::string vendorSku;
293 vendorSku = getPropertyFetcher()->getProperty("ro.boot.product.vendor.sku", "");
294
295 if (!vendorSku.empty()) {
296 status =
297 fetchOneHalManifest(kVendorVintfDir + "manifest_" + vendorSku + ".xml", out, error);
298 if (status == OK || status != NAME_NOT_FOUND) {
299 return status;
300 }
301 }
302
303 status = fetchOneHalManifest(kVendorManifest, out, error);
304 if (status == OK || status != NAME_NOT_FOUND) {
305 return status;
306 }
307
308 return NAME_NOT_FOUND;
309}
310
Yifan Hong12a11ac2018-01-19 13:58:32 -0800311// "out" is written to iff return status is OK.
312// Priority:
313// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
314// 2. /odm/etc/vintf/manifest.xml
315// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
316// 4. /odm/etc/manifest.xml
317// where:
318// {sku} is the value of ro.boot.product.hardware.sku
Yifan Hong9f78c182018-07-12 14:45:52 -0700319status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800320 status_t status;
321
Yifan Hong12a11ac2018-01-19 13:58:32 -0800322 std::string productModel;
Yifan Hong12e23c22018-11-05 14:53:30 -0800323 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800324
325 if (!productModel.empty()) {
326 status =
Yifan Hong9f78c182018-07-12 14:45:52 -0700327 fetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800328 if (status == OK || status != NAME_NOT_FOUND) {
329 return status;
330 }
331 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800332
Yifan Hong9f78c182018-07-12 14:45:52 -0700333 status = fetchOneHalManifest(kOdmManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800334 if (status == OK || status != NAME_NOT_FOUND) {
335 return status;
336 }
337
Yifan Hong12a11ac2018-01-19 13:58:32 -0800338 if (!productModel.empty()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700339 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800340 error);
341 if (status == OK || status != NAME_NOT_FOUND) {
342 return status;
343 }
344 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800345
Yifan Hong9f78c182018-07-12 14:45:52 -0700346 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800347 if (status == OK || status != NAME_NOT_FOUND) {
348 return status;
349 }
350
351 return NAME_NOT_FOUND;
352}
353
354// Fetch one manifest.xml file. "out" is written to iff return status is OK.
355// Returns NAME_NOT_FOUND if file is missing.
Yifan Hong9f78c182018-07-12 14:45:52 -0700356status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800357 std::string* error) {
358 HalManifest ret;
Yifan Hong12e23c22018-11-05 14:53:30 -0800359 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800360 if (status == OK) {
361 *out = std::move(ret);
362 }
363 return status;
364}
365
Yifan Hong9f78c182018-07-12 14:45:52 -0700366status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800367 CompatibilityMatrix etcMatrix;
Yifan Hong12e23c22018-11-05 14:53:30 -0800368 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800369 *out = std::move(etcMatrix);
370 return OK;
371 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800372 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800373}
374
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700375// Priority:
376// 1. /system/etc/vintf/manifest.xml
377// + /system/etc/vintf/manifest/*.xml if they exist
378// + /product/etc/vintf/manifest.xml if it exists
379// + /product/etc/vintf/manifest/*.xml if they exist
380// 2. (deprecated) /system/manifest.xml
Yifan Hong9f78c182018-07-12 14:45:52 -0700381status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700382 auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
383 if (systemEtcStatus == OK) {
384 auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out, error);
385 if (dirStatus != OK) {
386 return dirStatus;
387 }
388
Yifan Hongafb80be2020-03-19 18:09:54 -0700389 std::vector<std::pair<const std::string&, const std::string&>> extensions{
390 {kProductManifest, kProductManifestFragmentDir},
391 {kSystemExtManifest, kSystemExtManifestFragmentDir},
392 };
393 for (auto&& [manifestPath, frags] : extensions) {
394 HalManifest halManifest;
395 auto status = fetchOneHalManifest(manifestPath, &halManifest, error);
396 if (status != OK && status != NAME_NOT_FOUND) {
397 return status;
398 }
399 if (status == OK) {
400 if (!out->addAll(&halManifest, error)) {
401 if (error) {
402 error->insert(0, "Cannot add " + manifestPath + ":");
403 }
404 return UNKNOWN_ERROR;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700405 }
Yifan Hongafb80be2020-03-19 18:09:54 -0700406 }
407
408 auto fragmentStatus = addDirectoryManifests(frags, out, error);
409 if (fragmentStatus != OK) {
410 return fragmentStatus;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700411 }
412 }
Yifan Hongafb80be2020-03-19 18:09:54 -0700413 return OK;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700414 } else {
415 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
416 << (error ? *error : strerror(-systemEtcStatus));
Yifan Hongde6d00e2018-02-27 17:11:28 -0800417 }
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700418
Yifan Hong12e23c22018-11-05 14:53:30 -0800419 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800420}
421
Yifan Hong9f8f6562018-11-06 16:26:20 -0800422static void appendLine(std::string* error, const std::string& message) {
423 if (error != nullptr) {
424 if (!error->empty()) *error += "\n";
425 *error += message;
426 }
427}
428
Yifan Hong73bde592019-01-22 13:30:23 -0800429status_t VintfObject::getOneMatrix(const std::string& path, Named<CompatibilityMatrix>* out,
430 std::string* error) {
431 std::string content;
432 status_t status = getFileSystem()->fetch(path, &content, error);
433 if (status != OK) {
434 return status;
435 }
436 if (!gCompatibilityMatrixConverter(&out->object, content, error)) {
437 if (error) {
438 error->insert(0, "Cannot parse " + path + ": ");
439 }
440 return BAD_VALUE;
441 }
442 out->name = path;
443 return OK;
444}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800445
Yifan Hong73bde592019-01-22 13:30:23 -0800446status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<Named<CompatibilityMatrix>>* results,
447 std::string* error) {
Yifan Hongb02d87d2019-12-19 11:15:27 -0800448 std::vector<std::string> dirs = {
449 kSystemVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800450 kSystemExtVintfDir,
Yifan Hongb02d87d2019-12-19 11:15:27 -0800451 kProductVintfDir,
452 };
453 for (const auto& dir : dirs) {
454 std::vector<std::string> fileNames;
455 status_t listStatus = getFileSystem()->listFiles(dir, &fileNames, error);
456 if (listStatus == NAME_NOT_FOUND) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800457 continue;
458 }
Yifan Hongb02d87d2019-12-19 11:15:27 -0800459 if (listStatus != OK) {
460 return listStatus;
461 }
462 for (const std::string& fileName : fileNames) {
463 std::string path = dir + fileName;
464 Named<CompatibilityMatrix> namedMatrix;
465 std::string matrixError;
466 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
467 if (matrixStatus != OK) {
468 // Manifests and matrices share the same dir. Client may not have enough
469 // permissions to read system manifests, or may not be able to parse it.
470 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
471 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
472 continue;
473 }
474 results->emplace_back(std::move(namedMatrix));
475 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800476
Yifan Hongb02d87d2019-12-19 11:15:27 -0800477 if (dir == kSystemVintfDir && results->empty()) {
478 if (error) {
479 *error = "No framework matrices under " + dir + " can be fetched or parsed.\n";
480 }
481 return NAME_NOT_FOUND;
482 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800483 }
484
Yifan Hong73bde592019-01-22 13:30:23 -0800485 if (results->empty()) {
486 if (error) {
487 *error =
Yifan Hongb02d87d2019-12-19 11:15:27 -0800488 "No framework matrices can be fetched or parsed. "
489 "The following directories are searched:\n " +
490 android::base::Join(dirs, "\n ");
Yifan Hong73bde592019-01-22 13:30:23 -0800491 }
492 return NAME_NOT_FOUND;
493 }
494 return OK;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800495}
496
Yifan Hong1fb004e2017-09-26 15:04:44 -0700497std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
498 RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700499 return GetInstance()->getRuntimeInfo(skipCache, flags);
500}
501std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(bool skipCache,
502 RuntimeInfo::FetchFlags flags) {
503 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700504
505 if (!skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700506 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700507 }
508
Yifan Hong9f78c182018-07-12 14:45:52 -0700509 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800510 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700511 }
512
Yifan Hongf3247982019-12-12 12:11:36 -0800513 // Fetch kernel FCM version from device HAL manifest and store it in RuntimeInfo too.
514 if ((flags & RuntimeInfo::FetchFlag::KERNEL_FCM) != 0) {
515 auto manifest = getDeviceHalManifest();
516 if (!manifest) {
517 mDeviceRuntimeInfo.fetchedFlags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
518 return nullptr;
519 }
520 Level level = Level::UNSPECIFIED;
521 if (manifest->kernel().has_value()) {
522 level = manifest->kernel()->level();
523 }
524 mDeviceRuntimeInfo.object->setKernelLevel(level);
525 flags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
526 }
527
Yifan Hong9f78c182018-07-12 14:45:52 -0700528 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700529 if (status != OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700530 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
Yifan Hong1fb004e2017-09-26 15:04:44 -0700531 return nullptr;
532 }
533
Yifan Hong9f78c182018-07-12 14:45:52 -0700534 mDeviceRuntimeInfo.fetchedFlags |= flags;
535 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800536}
537
Yifan Hong12e23c22018-11-05 14:53:30 -0800538int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
539 status_t status = OK;
540 // null checks for files and runtime info
541 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700542 appendLine(error, "No framework manifest file from device or from update package");
543 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700544 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800545 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700546 appendLine(error, "No device manifest file from device or from update package");
547 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700548 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800549 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700550 appendLine(error, "No framework matrix file from device or from update package");
551 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700552 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800553 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700554 appendLine(error, "No device matrix file from device or from update package");
555 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700556 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800557
Yifan Hong072f12d2018-08-08 13:04:51 -0700558 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800559 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700560 appendLine(error, "No runtime info from device");
561 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800562 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700563 }
Yifan Hong878556e2018-07-13 13:45:30 -0700564 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700565
566 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800567 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800568 if (error) {
569 error->insert(0,
570 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700571 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800572 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700573 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800574 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800575 if (error) {
576 error->insert(0,
577 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700578 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800579 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700580 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800581
Yifan Hong072f12d2018-08-08 13:04:51 -0700582 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800583 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
Yifan Hong85e589e2019-12-18 13:12:29 -0800584 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800585 if (error) {
586 error->insert(0,
587 "Runtime info and framework compatibility matrix are incompatible: ");
588 }
589 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700590 }
591 }
592
593 return COMPATIBLE;
594}
595
Yifan Hong9f78c182018-07-12 14:45:52 -0700596namespace details {
597
Yifan Hong270b5652018-01-18 18:46:01 -0800598const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800599const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800600const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong73bde592019-01-22 13:30:23 -0800601const std::string kProductVintfDir = "/product/etc/vintf/";
Yifan Hong5bbc4ae2020-01-09 14:30:27 -0800602const std::string kSystemExtVintfDir = "/system_ext/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800603
604const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800605const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800606const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800607const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong73bde592019-01-22 13:30:23 -0800608const std::string kProductMatrix = kProductVintfDir + "compatibility_matrix.xml";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700609const std::string kProductManifest = kProductVintfDir + "manifest.xml";
Yifan Hongafb80be2020-03-19 18:09:54 -0700610const std::string kSystemExtManifest = kSystemExtVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800611
Steven Morelandeedf2d42018-04-04 16:36:27 -0700612const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/";
613const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/";
614const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700615const std::string kProductManifestFragmentDir = kProductVintfDir + "manifest/";
Yifan Hongafb80be2020-03-19 18:09:54 -0700616const std::string kSystemExtManifestFragmentDir = kSystemExtVintfDir + "manifest/";
Steven Morelandeedf2d42018-04-04 16:36:27 -0700617
Yifan Hong270b5652018-01-18 18:46:01 -0800618const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
619const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800620const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800621const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
622const std::string kOdmLegacyVintfDir = "/odm/etc/";
623const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
624
Yifan Hong69c1b112018-02-27 17:06:00 -0800625std::vector<std::string> dumpFileList() {
626 return {
Yifan Hong73bde592019-01-22 13:30:23 -0800627 // clang-format off
628 kSystemVintfDir,
629 kVendorVintfDir,
630 kOdmVintfDir,
631 kProductVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800632 kSystemExtVintfDir,
Yifan Hong73bde592019-01-22 13:30:23 -0800633 kOdmLegacyVintfDir,
634 kVendorLegacyManifest,
635 kVendorLegacyMatrix,
636 kSystemLegacyManifest,
637 kSystemLegacyMatrix,
638 // clang-format on
Yifan Hong69c1b112018-02-27 17:06:00 -0800639 };
640}
641
Yifan Hong9f78c182018-07-12 14:45:52 -0700642} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700643
Yifan Hong9f78c182018-07-12 14:45:52 -0700644bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800645 const CompatibilityMatrix& targetMatrix,
Yifan Hongb712e7e2020-03-17 17:58:35 -0700646 const ListInstances& listInstances,
647 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700648 bool isDeprecated = false;
649 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hongb712e7e2020-03-17 17:58:35 -0700650 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, childrenMap,
651 appendedError)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700652 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800653 }
Yifan Hongb712e7e2020-03-17 17:58:35 -0700654 return true; // continue to check next instance
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700655 });
656 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800657}
658
Yifan Hongb712e7e2020-03-17 17:58:35 -0700659// Let oldMatrixInstance = package@x.y-w::interface/instancePattern.
660// If any "@servedVersion::interface/servedInstance" in listInstances(package@x.y::interface)
661// matches instancePattern, return true iff for all child interfaces (from
662// GetListedInstanceInheritance), IsFqInstanceDeprecated returns false.
Yifan Hong9f78c182018-07-12 14:45:52 -0700663bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800664 const CompatibilityMatrix& targetMatrix,
Yifan Hongb712e7e2020-03-17 17:58:35 -0700665 const ListInstances& listInstances,
666 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700667 const std::string& package = oldMatrixInstance.package();
668 const Version& version = oldMatrixInstance.versionRange().minVer();
669 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700670
Yifan Honga8a8fa92018-03-20 14:42:43 -0700671 std::vector<std::string> instanceHint;
672 if (!oldMatrixInstance.isRegex()) {
673 instanceHint.push_back(oldMatrixInstance.exactInstance());
674 }
675
Yifan Hongb712e7e2020-03-17 17:58:35 -0700676 std::vector<std::string> accumulatedErrors;
Yifan Honga8a8fa92018-03-20 14:42:43 -0700677 auto list = listInstances(package, version, interface, instanceHint);
Yifan Hongb712e7e2020-03-17 17:58:35 -0700678
Yifan Honga8a8fa92018-03-20 14:42:43 -0700679 for (const auto& pair : list) {
680 const std::string& servedInstance = pair.first;
681 Version servedVersion = pair.second;
Yifan Hongb712e7e2020-03-17 17:58:35 -0700682 std::string servedFqInstanceString =
683 toFQNameString(package, servedVersion, interface, servedInstance);
Yifan Honga8a8fa92018-03-20 14:42:43 -0700684 if (!oldMatrixInstance.matchInstance(servedInstance)) {
Yifan Hongb712e7e2020-03-17 17:58:35 -0700685 // ignore unrelated instance
Yifan Honga8a8fa92018-03-20 14:42:43 -0700686 continue;
687 }
688
Yifan Hongb712e7e2020-03-17 17:58:35 -0700689 auto inheritance = GetListedInstanceInheritance(package, servedVersion, interface,
690 servedInstance, listInstances, childrenMap);
691 if (!inheritance.has_value()) {
692 accumulatedErrors.push_back(inheritance.error().message());
693 continue;
Yifan Hongf73ba512018-01-17 15:52:30 -0800694 }
695
Yifan Hongb712e7e2020-03-17 17:58:35 -0700696 std::vector<std::string> errors;
697 for (const auto& fqInstance : *inheritance) {
698 auto result = IsFqInstanceDeprecated(targetMatrix, oldMatrixInstance.format(),
699 fqInstance, listInstances);
700 if (result.ok()) {
701 errors.clear();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700702 break;
703 }
Yifan Hongb712e7e2020-03-17 17:58:35 -0700704 errors.push_back(result.error().message());
Yifan Honga8a8fa92018-03-20 14:42:43 -0700705 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800706
Yifan Hongb712e7e2020-03-17 17:58:35 -0700707 if (errors.empty()) {
708 continue;
709 }
710 accumulatedErrors.insert(accumulatedErrors.end(), errors.begin(), errors.end());
711 }
712
713 if (accumulatedErrors.empty()) {
714 return false;
715 }
716 appendLine(appendedError, android::base::Join(accumulatedErrors, "\n"));
717 return true;
718}
719
720// Check if fqInstance is listed in |listInstances|.
721bool VintfObject::IsInstanceListed(const ListInstances& listInstances,
722 const FqInstance& fqInstance) {
723 auto list =
724 listInstances(fqInstance.getPackage(), fqInstance.getVersion(), fqInstance.getInterface(),
725 {fqInstance.getInstance()} /* instanceHint*/);
726 return std::any_of(list.begin(), list.end(),
727 [&](const auto& pair) { return pair.first == fqInstance.getInstance(); });
728}
729
730// Return a list of FqInstance, where each element:
731// - is listed in |listInstances|; AND
732// - is, or inherits from, package@version::interface/instance (as specified by |childrenMap|)
733android::base::Result<std::vector<FqInstance>> VintfObject::GetListedInstanceInheritance(
734 const std::string& package, const Version& version, const std::string& interface,
735 const std::string& instance, const ListInstances& listInstances,
736 const ChildrenMap& childrenMap) {
737 FqInstance fqInstance;
738 if (!fqInstance.setTo(package, version.majorVer, version.minorVer, interface, instance)) {
739 return android::base::Error() << toFQNameString(package, version, interface, instance)
740 << " is not a valid FqInstance";
741 }
742
743 if (!IsInstanceListed(listInstances, fqInstance)) {
744 return {};
745 }
746
747 const FQName& fqName = fqInstance.getFqName();
748
749 std::vector<FqInstance> ret;
750 ret.push_back(fqInstance);
751
752 auto childRange = childrenMap.equal_range(fqName.string());
753 for (auto it = childRange.first; it != childRange.second; ++it) {
754 const auto& childFqNameString = it->second;
755 FQName childFqName;
756 if (!childFqName.setTo(childFqNameString)) {
757 return android::base::Error() << "Cannot parse " << childFqNameString << " as FQName";
758 }
759 FqInstance childFqInstance;
760 if (!childFqInstance.setTo(childFqName, fqInstance.getInstance())) {
761 return android::base::Error() << "Cannot merge " << childFqName.string() << "/"
762 << fqInstance.getInstance() << " as FqInstance";
763 continue;
764 }
765 if (!IsInstanceListed(listInstances, childFqInstance)) {
766 continue;
767 }
768 ret.push_back(childFqInstance);
769 }
770 return ret;
771}
772
773// Check if |fqInstance| is in |targetMatrix|; essentially equal to
774// targetMatrix.matchInstance(fqInstance), but provides richer error message. In details:
775// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
776// 2. package@x.z::interface/servedInstance is in targetMatrix but
777// servedInstance is not in listInstances(package@x.z::interface)
778android::base::Result<void> VintfObject::IsFqInstanceDeprecated(
779 const CompatibilityMatrix& targetMatrix, HalFormat format, const FqInstance& fqInstance,
780 const ListInstances& listInstances) {
781 // Find minimum package@x.? in target matrix, and check if instance is in target matrix.
782 bool foundInstance = false;
783 Version targetMatrixMinVer{SIZE_MAX, SIZE_MAX};
784 targetMatrix.forEachInstanceOfPackage(
785 format, fqInstance.getPackage(), [&](const auto& targetMatrixInstance) {
786 if (targetMatrixInstance.versionRange().majorVer == fqInstance.getMajorVersion() &&
787 targetMatrixInstance.interface() == fqInstance.getInterface() &&
788 targetMatrixInstance.matchInstance(fqInstance.getInstance())) {
789 targetMatrixMinVer =
790 std::min(targetMatrixMinVer, targetMatrixInstance.versionRange().minVer());
791 foundInstance = true;
792 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800793 return true;
Yifan Hongb712e7e2020-03-17 17:58:35 -0700794 });
795 if (!foundInstance) {
796 return android::base::Error()
797 << fqInstance.string() << " is deprecated in compatibility matrix at FCM Version "
798 << targetMatrix.level() << "; it should not be served.";
799 }
800
801 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
802 bool targetVersionServed = false;
803 for (const auto& newPair :
804 listInstances(fqInstance.getPackage(), targetMatrixMinVer, fqInstance.getInterface(),
805 {fqInstance.getInstance()} /* instanceHint */)) {
806 if (newPair.first == fqInstance.getInstance()) {
807 targetVersionServed = true;
808 break;
Yifan Hongf73ba512018-01-17 15:52:30 -0800809 }
810 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700811
Yifan Hongb712e7e2020-03-17 17:58:35 -0700812 if (!targetVersionServed) {
813 return android::base::Error()
814 << fqInstance.string() << " is deprecated; requires at least " << targetMatrixMinVer;
815 }
816 return {};
Yifan Hongf73ba512018-01-17 15:52:30 -0800817}
818
Yifan Hongb712e7e2020-03-17 17:58:35 -0700819int32_t VintfObject::checkDeprecation(const ListInstances& listInstances,
820 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
821 std::string* error) {
Yifan Hong73bde592019-01-22 13:30:23 -0800822 std::vector<Named<CompatibilityMatrix>> matrixFragments;
823 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
824 if (matrixFragmentsStatus != OK) {
825 return matrixFragmentsStatus;
826 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800827 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800828 if (error && error->empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800829 *error = "Cannot get framework matrix for each FCM version for unknown error.";
Yifan Hong73bde592019-01-22 13:30:23 -0800830 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800831 return NAME_NOT_FOUND;
832 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700833 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800834 if (deviceManifest == nullptr) {
835 if (error) *error = "No device manifest.";
836 return NAME_NOT_FOUND;
837 }
838 Level deviceLevel = deviceManifest->level();
839 if (deviceLevel == Level::UNSPECIFIED) {
840 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
841 return BAD_VALUE;
842 }
843
844 const CompatibilityMatrix* targetMatrix = nullptr;
845 for (const auto& namedMatrix : matrixFragments) {
846 if (namedMatrix.object.level() == deviceLevel) {
847 targetMatrix = &namedMatrix.object;
848 }
849 }
850 if (targetMatrix == nullptr) {
851 if (error)
852 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
853 return NAME_NOT_FOUND;
854 }
855
Yifan Hongb712e7e2020-03-17 17:58:35 -0700856 std::multimap<std::string, std::string> childrenMap;
857 for (const auto& child : hidlMetadata) {
858 for (const auto& parent : child.inherited) {
859 childrenMap.emplace(parent, child.name);
860 }
861 }
862
863 // Find a list of possibly deprecated HALs by comparing |listInstances| with older matrices.
864 // Matrices with unspecified level are considered "current".
865 bool isDeprecated = false;
Yifan Hongf73ba512018-01-17 15:52:30 -0800866 for (const auto& namedMatrix : matrixFragments) {
867 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
868 if (namedMatrix.object.level() >= deviceLevel) continue;
869
870 const auto& oldMatrix = namedMatrix.object;
871 for (const MatrixHal& hal : oldMatrix.getHals()) {
Yifan Hongb712e7e2020-03-17 17:58:35 -0700872 if (IsHalDeprecated(hal, *targetMatrix, listInstances, childrenMap, error)) {
873 isDeprecated = true;
874 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800875 }
876 }
877
Yifan Hongb712e7e2020-03-17 17:58:35 -0700878 return isDeprecated ? DEPRECATED : NO_DEPRECATED_HALS;
Yifan Hongf73ba512018-01-17 15:52:30 -0800879}
880
Yifan Hongb712e7e2020-03-17 17:58:35 -0700881int32_t VintfObject::checkDeprecation(const std::vector<HidlInterfaceMetadata>& hidlMetadata,
882 std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800883 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700884 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700885 ListInstances inManifest =
886 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
887 const std::vector<std::string>& /* hintInstances */) {
888 std::vector<std::pair<std::string, Version>> ret;
889 deviceManifest->forEachInstanceOfInterface(
Yifan Hongac621482019-09-10 19:27:20 -0700890 HalFormat::HIDL, package, version, interface,
891 [&ret](const ManifestInstance& manifestInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700892 ret.push_back(
893 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
894 return true;
895 });
896 return ret;
897 };
Yifan Hongb712e7e2020-03-17 17:58:35 -0700898 return checkDeprecation(inManifest, hidlMetadata, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800899}
Yifan Hong3daec812017-02-27 18:49:11 -0800900
Yifan Hong378c4082019-12-23 13:10:57 -0800901Level VintfObject::getKernelLevel(std::string* error) {
902 auto manifest = getDeviceHalManifest();
903 if (!manifest) {
904 if (error) *error = "Cannot retrieve device manifest.";
905 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700906 }
Yifan Hong378c4082019-12-23 13:10:57 -0800907 if (manifest->kernel().has_value() && manifest->kernel()->level() != Level::UNSPECIFIED) {
908 return manifest->kernel()->level();
Yifan Hong1e8febd2019-08-07 16:17:19 -0700909 }
Yifan Hong378c4082019-12-23 13:10:57 -0800910 if (error) *error = "Device manifest does not specify kernel FCM version.";
911 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700912}
913
Yifan Hong9f78c182018-07-12 14:45:52 -0700914const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
915 return mFileSystem;
916}
917
Yifan Hong9f78c182018-07-12 14:45:52 -0700918const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
919 return mPropertyFetcher;
920}
921
Yifan Hongd038b2b2018-11-27 13:57:56 -0800922const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700923 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700924}
925
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700926android::base::Result<bool> VintfObject::hasFrameworkCompatibilityMatrixExtensions() {
927 std::vector<Named<CompatibilityMatrix>> matrixFragments;
928 std::string error;
929 status_t status = getAllFrameworkMatrixLevels(&matrixFragments, &error);
930 if (status != OK) {
931 return android::base::Error(-status)
932 << "Cannot get all framework matrix fragments: " << error;
933 }
934 for (const auto& namedMatrix : matrixFragments) {
935 // Returns true if product matrix exists.
936 if (android::base::StartsWith(namedMatrix.name, kProductVintfDir)) {
937 return true;
938 }
Yifan Hong238c6dc2020-03-12 22:56:16 -0700939 // Returns true if system_ext matrix exists.
940 if (android::base::StartsWith(namedMatrix.name, kSystemExtVintfDir)) {
941 return true;
942 }
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700943 // Returns true if device system matrix exists.
944 if (android::base::StartsWith(namedMatrix.name, kSystemVintfDir) &&
945 namedMatrix.object.level() == Level::UNSPECIFIED &&
946 !namedMatrix.object.getHals().empty()) {
947 return true;
948 }
949 }
950 return false;
951}
952
Yifan Hongff1a25c2020-03-17 14:09:10 -0700953android::base::Result<void> VintfObject::checkUnusedHals(
954 const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700955 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 Hongff1a25c2020-03-17 14:09:10 -0700963 auto unused = manifest->checkUnusedHals(*matrix, hidlMetadata);
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700964 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 Hong4b52dfa2020-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 Hong6e32a5f2020-03-12 16:06:27 -0700972 << "matrices with FCM version >= " << matrix->level() << ".\n"
Yifan Hong4b52dfa2020-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 Hong6e32a5f2020-03-12 16:06:27 -0700976 << "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