blob: 13e2f5ce234997ee537f8ce497e3620e4d5c9854 [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>
Steven Morelandbf91ee22020-04-30 15:48:35 -070051static std::shared_ptr<const T> Get(const char* id, LockedSharedPtr<T>* ptr, bool skipCache,
52 const F& fetchAllInformation) {
Yifan Hong3daec812017-02-27 18:49:11 -080053 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong7219ba12018-01-08 16:23:49 -080054 if (skipCache || !ptr->fetchedOnce) {
Steven Morelandbf91ee22020-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 Moreland15ccd342020-03-27 15:48:40 -070058 status_t status = fetchAllInformation(ptr->object.get(), &error);
Steven Morelandbf91ee22020-04-30 15:48:35 -070059 if (status == OK) {
Steven Morelandea1b8972020-04-30 16:42:56 -070060 ptr->fetchedOnce = true;
Steven Morelandbf91ee22020-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 Moreland954c4642020-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 Hongfc73edf2017-08-29 11:39:07 -0700102std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700103 return GetInstance()->getDeviceHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -0800104}
105
Yifan Hong9f78c182018-07-12 14:45:52 -0700106std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest(bool skipCache) {
Steven Morelandbf91ee22020-04-30 15:48:35 -0700107 return Get(__func__, &mDeviceManifest, skipCache,
Yifan Hong9f78c182018-07-12 14:45:52 -0700108 std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2));
109}
110
Yifan Hongfc73edf2017-08-29 11:39:07 -0700111std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700112 return GetInstance()->getFrameworkHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -0800113}
114
Yifan Hong9f78c182018-07-12 14:45:52 -0700115std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest(bool skipCache) {
Steven Morelandbf91ee22020-04-30 15:48:35 -0700116 return Get(__func__, &mFrameworkManifest, skipCache,
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 Hongfc73edf2017-08-29 11:39:07 -0700120std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700121 return GetInstance()->getDeviceCompatibilityMatrix(skipCache);
Yifan Hong2272bf82017-04-28 14:37:56 -0700122}
123
Yifan Hong9f78c182018-07-12 14:45:52 -0700124std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix(
125 bool skipCache) {
Steven Morelandbf91ee22020-04-30 15:48:35 -0700126 return Get(__func__, &mDeviceMatrix, skipCache,
127 std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2));
Yifan Hong9f78c182018-07-12 14:45:52 -0700128}
129
Yifan Hongfc73edf2017-08-29 11:39:07 -0700130std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700131 return GetInstance()->getFrameworkCompatibilityMatrix(skipCache);
132}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800133
Yifan Hong9f78c182018-07-12 14:45:52 -0700134std::shared_ptr<const CompatibilityMatrix> VintfObject::getFrameworkCompatibilityMatrix(
135 bool skipCache) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800136 // To avoid deadlock, get device manifest before any locks.
Yifan Hong9f78c182018-07-12 14:45:52 -0700137 auto deviceManifest = getDeviceHalManifest();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800138
Yifan Hong9f78c182018-07-12 14:45:52 -0700139 std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800140
141 auto combined =
Steven Morelandbf91ee22020-04-30 15:48:35 -0700142 Get(__func__, &mCombinedFrameworkMatrix, skipCache,
Yifan Hong9f78c182018-07-12 14:45:52 -0700143 std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest, _1, _2));
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800144 if (combined != nullptr) {
145 return combined;
146 }
147
Steven Morelandbf91ee22020-04-30 15:48:35 -0700148 return Get(__func__, &mFrameworkMatrix, skipCache,
Yifan Hong12e23c22018-11-05 14:53:30 -0800149 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(),
Yifan Hong9f78c182018-07-12 14:45:52 -0700150 kSystemLegacyMatrix, _2));
Yifan Hong2272bf82017-04-28 14:37:56 -0700151}
152
Yifan Hong9f78c182018-07-12 14:45:52 -0700153status_t VintfObject::getCombinedFrameworkMatrix(
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800154 const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out,
155 std::string* error) {
Yifan Hong73bde592019-01-22 13:30:23 -0800156 std::vector<Named<CompatibilityMatrix>> matrixFragments;
157 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
158 if (matrixFragmentsStatus != OK) {
159 return matrixFragmentsStatus;
160 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800161 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800162 if (error && error->empty()) {
163 *error = "Cannot get framework matrix for each FCM version for unknown error.";
164 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800165 return NAME_NOT_FOUND;
166 }
167
168 Level deviceLevel = Level::UNSPECIFIED;
169
170 if (deviceManifest != nullptr) {
171 deviceLevel = deviceManifest->level();
172 }
173
174 // TODO(b/70628538): Do not infer from Shipping API level.
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800175 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800176 auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800177 if (shippingApi != 0u) {
178 deviceLevel = details::convertFromApiLevel(shippingApi);
179 }
180 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800181
182 if (deviceLevel == Level::UNSPECIFIED) {
183 // Cannot infer FCM version. Combine all matrices by assuming
184 // Shipping FCM Version == min(all supported FCM Versions in the framework)
185 for (auto&& pair : matrixFragments) {
186 Level fragmentLevel = pair.object.level();
187 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
188 deviceLevel = fragmentLevel;
189 }
190 }
191 }
192
193 if (deviceLevel == Level::UNSPECIFIED) {
194 // None of the fragments specify any FCM version. Should never happen except
195 // for inconsistent builds.
196 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800197 *error = "No framework compatibility matrix files under " + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800198 " declare FCM version.";
199 }
200 return NAME_NOT_FOUND;
201 }
202
Yifan Honge7837b12018-10-11 10:38:57 -0700203 auto combined = CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800204 if (combined == nullptr) {
205 return BAD_VALUE;
206 }
207 *out = std::move(*combined);
208 return OK;
209}
210
Steven Morelandeedf2d42018-04-04 16:36:27 -0700211// Load and combine all of the manifests in a directory
Yifan Hong9f78c182018-07-12 14:45:52 -0700212status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
Steven Morelandeedf2d42018-04-04 16:36:27 -0700213 std::string* error) {
214 std::vector<std::string> fileNames;
Yifan Hong12e23c22018-11-05 14:53:30 -0800215 status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700216 // if the directory isn't there, that's okay
217 if (err == NAME_NOT_FOUND) return OK;
218 if (err != OK) return err;
219
220 for (const std::string& file : fileNames) {
221 // Only adds HALs because all other things are added by libvintf
222 // itself for now.
223 HalManifest fragmentManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700224 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700225 if (err != OK) return err;
226
Yifan Hong4c6962d2019-01-30 15:50:46 -0800227 if (!manifest->addAll(&fragmentManifest, error)) {
228 if (error) {
229 error->insert(0, "Cannot add manifest fragment " + directory + file + ":");
230 }
231 return UNKNOWN_ERROR;
232 }
Steven Morelandeedf2d42018-04-04 16:36:27 -0700233 }
234
235 return OK;
236}
237
Yifan Hong5a93bf22018-01-17 18:22:32 -0800238// Priority for loading vendor manifest:
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800239// 1. Vendor manifest + device fragments + ODM manifest (optional) + odm fragments
240// 2. Vendor manifest + device fragments
Steven Morelandeedf2d42018-04-04 16:36:27 -0700241// 3. ODM manifest (optional) + odm fragments
242// 4. /vendor/manifest.xml (legacy, no fragments)
Yifan Hong5a93bf22018-01-17 18:22:32 -0800243// where:
Steven Moreland30a532c2018-11-01 08:46:32 -0700244// A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
245// over A.
Yifan Hong9f78c182018-07-12 14:45:52 -0700246status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800247 HalManifest vendorManifest;
248 status_t vendorStatus = fetchVendorHalManifest(&vendorManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800249 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
250 return vendorStatus;
251 }
252
Steven Morelandeedf2d42018-04-04 16:36:27 -0700253 if (vendorStatus == OK) {
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800254 *out = std::move(vendorManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700255 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700256 if (fragmentStatus != OK) {
257 return fragmentStatus;
258 }
259 }
260
Yifan Hong5a93bf22018-01-17 18:22:32 -0800261 HalManifest odmManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700262 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800263 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
264 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800265 }
266
Yifan Hong5a93bf22018-01-17 18:22:32 -0800267 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800268 if (odmStatus == OK) {
Yifan Hong4c6962d2019-01-30 15:50:46 -0800269 if (!out->addAll(&odmManifest, error)) {
270 if (error) {
271 error->insert(0, "Cannot add ODM manifest :");
272 }
273 return UNKNOWN_ERROR;
274 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800275 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700276 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800277 }
278
Yifan Hong12a11ac2018-01-19 13:58:32 -0800279 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800280 if (odmStatus == OK) {
281 *out = std::move(odmManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700282 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800283 }
284
285 // Use legacy /vendor/manifest.xml
Yifan Hong12e23c22018-11-05 14:53:30 -0800286 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800287}
288
Roopesh Natarajafe7068d2020-02-26 09:51:38 -0800289// Priority:
290// 1. if {vendorSku} is defined, /vendor/etc/vintf/manifest_{vendorSku}.xml
291// 2. /vendor/etc/vintf/manifest.xml
292// where:
293// {vendorSku} is the value of ro.boot.product.vendor.sku
294status_t VintfObject::fetchVendorHalManifest(HalManifest* out, std::string* error) {
295 status_t status;
296
297 std::string vendorSku;
298 vendorSku = getPropertyFetcher()->getProperty("ro.boot.product.vendor.sku", "");
299
300 if (!vendorSku.empty()) {
301 status =
302 fetchOneHalManifest(kVendorVintfDir + "manifest_" + vendorSku + ".xml", out, error);
303 if (status == OK || status != NAME_NOT_FOUND) {
304 return status;
305 }
306 }
307
308 status = fetchOneHalManifest(kVendorManifest, out, error);
309 if (status == OK || status != NAME_NOT_FOUND) {
310 return status;
311 }
312
313 return NAME_NOT_FOUND;
314}
315
Yifan Hong12a11ac2018-01-19 13:58:32 -0800316// "out" is written to iff return status is OK.
317// Priority:
318// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
319// 2. /odm/etc/vintf/manifest.xml
320// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
321// 4. /odm/etc/manifest.xml
322// where:
323// {sku} is the value of ro.boot.product.hardware.sku
Yifan Hong9f78c182018-07-12 14:45:52 -0700324status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800325 status_t status;
326
Yifan Hong12a11ac2018-01-19 13:58:32 -0800327 std::string productModel;
Yifan Hong12e23c22018-11-05 14:53:30 -0800328 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800329
330 if (!productModel.empty()) {
331 status =
Yifan Hong9f78c182018-07-12 14:45:52 -0700332 fetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800333 if (status == OK || status != NAME_NOT_FOUND) {
334 return status;
335 }
336 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800337
Yifan Hong9f78c182018-07-12 14:45:52 -0700338 status = fetchOneHalManifest(kOdmManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800339 if (status == OK || status != NAME_NOT_FOUND) {
340 return status;
341 }
342
Yifan Hong12a11ac2018-01-19 13:58:32 -0800343 if (!productModel.empty()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700344 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800345 error);
346 if (status == OK || status != NAME_NOT_FOUND) {
347 return status;
348 }
349 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800350
Yifan Hong9f78c182018-07-12 14:45:52 -0700351 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800352 if (status == OK || status != NAME_NOT_FOUND) {
353 return status;
354 }
355
356 return NAME_NOT_FOUND;
357}
358
359// Fetch one manifest.xml file. "out" is written to iff return status is OK.
360// Returns NAME_NOT_FOUND if file is missing.
Yifan Hong9f78c182018-07-12 14:45:52 -0700361status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800362 std::string* error) {
363 HalManifest ret;
Yifan Hong12e23c22018-11-05 14:53:30 -0800364 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800365 if (status == OK) {
366 *out = std::move(ret);
367 }
368 return status;
369}
370
Yifan Hong9f78c182018-07-12 14:45:52 -0700371status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800372 CompatibilityMatrix etcMatrix;
Yifan Hong12e23c22018-11-05 14:53:30 -0800373 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800374 *out = std::move(etcMatrix);
375 return OK;
376 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800377 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800378}
379
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700380// Priority:
381// 1. /system/etc/vintf/manifest.xml
382// + /system/etc/vintf/manifest/*.xml if they exist
383// + /product/etc/vintf/manifest.xml if it exists
384// + /product/etc/vintf/manifest/*.xml if they exist
385// 2. (deprecated) /system/manifest.xml
Yifan Hong9f78c182018-07-12 14:45:52 -0700386status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700387 auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
388 if (systemEtcStatus == OK) {
389 auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out, error);
390 if (dirStatus != OK) {
391 return dirStatus;
392 }
393
Yifan Hongafb80be2020-03-19 18:09:54 -0700394 std::vector<std::pair<const std::string&, const std::string&>> extensions{
395 {kProductManifest, kProductManifestFragmentDir},
396 {kSystemExtManifest, kSystemExtManifestFragmentDir},
397 };
398 for (auto&& [manifestPath, frags] : extensions) {
399 HalManifest halManifest;
400 auto status = fetchOneHalManifest(manifestPath, &halManifest, error);
401 if (status != OK && status != NAME_NOT_FOUND) {
402 return status;
403 }
404 if (status == OK) {
405 if (!out->addAll(&halManifest, error)) {
406 if (error) {
407 error->insert(0, "Cannot add " + manifestPath + ":");
408 }
409 return UNKNOWN_ERROR;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700410 }
Yifan Hongafb80be2020-03-19 18:09:54 -0700411 }
412
413 auto fragmentStatus = addDirectoryManifests(frags, out, error);
414 if (fragmentStatus != OK) {
415 return fragmentStatus;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700416 }
417 }
Yifan Hongafb80be2020-03-19 18:09:54 -0700418 return OK;
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700419 } else {
420 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
421 << (error ? *error : strerror(-systemEtcStatus));
Yifan Hongde6d00e2018-02-27 17:11:28 -0800422 }
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700423
Yifan Hong12e23c22018-11-05 14:53:30 -0800424 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800425}
426
Yifan Hong9f8f6562018-11-06 16:26:20 -0800427static void appendLine(std::string* error, const std::string& message) {
428 if (error != nullptr) {
429 if (!error->empty()) *error += "\n";
430 *error += message;
431 }
432}
433
Yifan Hong73bde592019-01-22 13:30:23 -0800434status_t VintfObject::getOneMatrix(const std::string& path, Named<CompatibilityMatrix>* out,
435 std::string* error) {
436 std::string content;
437 status_t status = getFileSystem()->fetch(path, &content, error);
438 if (status != OK) {
439 return status;
440 }
441 if (!gCompatibilityMatrixConverter(&out->object, content, error)) {
442 if (error) {
443 error->insert(0, "Cannot parse " + path + ": ");
444 }
445 return BAD_VALUE;
446 }
447 out->name = path;
448 return OK;
449}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800450
Yifan Hong73bde592019-01-22 13:30:23 -0800451status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<Named<CompatibilityMatrix>>* results,
452 std::string* error) {
Yifan Hongb02d87d2019-12-19 11:15:27 -0800453 std::vector<std::string> dirs = {
454 kSystemVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800455 kSystemExtVintfDir,
Yifan Hongb02d87d2019-12-19 11:15:27 -0800456 kProductVintfDir,
457 };
458 for (const auto& dir : dirs) {
459 std::vector<std::string> fileNames;
460 status_t listStatus = getFileSystem()->listFiles(dir, &fileNames, error);
461 if (listStatus == NAME_NOT_FOUND) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800462 continue;
463 }
Yifan Hongb02d87d2019-12-19 11:15:27 -0800464 if (listStatus != OK) {
465 return listStatus;
466 }
467 for (const std::string& fileName : fileNames) {
468 std::string path = dir + fileName;
469 Named<CompatibilityMatrix> namedMatrix;
470 std::string matrixError;
471 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
472 if (matrixStatus != OK) {
473 // Manifests and matrices share the same dir. Client may not have enough
474 // permissions to read system manifests, or may not be able to parse it.
475 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
476 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
477 continue;
478 }
479 results->emplace_back(std::move(namedMatrix));
480 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800481
Yifan Hongb02d87d2019-12-19 11:15:27 -0800482 if (dir == kSystemVintfDir && results->empty()) {
483 if (error) {
484 *error = "No framework matrices under " + dir + " can be fetched or parsed.\n";
485 }
486 return NAME_NOT_FOUND;
487 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800488 }
489
Yifan Hong73bde592019-01-22 13:30:23 -0800490 if (results->empty()) {
491 if (error) {
492 *error =
Yifan Hongb02d87d2019-12-19 11:15:27 -0800493 "No framework matrices can be fetched or parsed. "
494 "The following directories are searched:\n " +
495 android::base::Join(dirs, "\n ");
Yifan Hong73bde592019-01-22 13:30:23 -0800496 }
497 return NAME_NOT_FOUND;
498 }
499 return OK;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800500}
501
Yifan Hong1fb004e2017-09-26 15:04:44 -0700502std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
503 RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700504 return GetInstance()->getRuntimeInfo(skipCache, flags);
505}
506std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(bool skipCache,
507 RuntimeInfo::FetchFlags flags) {
508 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700509
510 if (!skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700511 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700512 }
513
Yifan Hong9f78c182018-07-12 14:45:52 -0700514 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800515 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700516 }
517
Yifan Hongf3247982019-12-12 12:11:36 -0800518 // Fetch kernel FCM version from device HAL manifest and store it in RuntimeInfo too.
519 if ((flags & RuntimeInfo::FetchFlag::KERNEL_FCM) != 0) {
520 auto manifest = getDeviceHalManifest();
521 if (!manifest) {
522 mDeviceRuntimeInfo.fetchedFlags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
523 return nullptr;
524 }
525 Level level = Level::UNSPECIFIED;
526 if (manifest->kernel().has_value()) {
527 level = manifest->kernel()->level();
528 }
529 mDeviceRuntimeInfo.object->setKernelLevel(level);
530 flags &= ~RuntimeInfo::FetchFlag::KERNEL_FCM;
531 }
532
Yifan Hong9f78c182018-07-12 14:45:52 -0700533 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700534 if (status != OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700535 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
Yifan Hong1fb004e2017-09-26 15:04:44 -0700536 return nullptr;
537 }
538
Yifan Hong9f78c182018-07-12 14:45:52 -0700539 mDeviceRuntimeInfo.fetchedFlags |= flags;
540 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800541}
542
Yifan Hong12e23c22018-11-05 14:53:30 -0800543int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
544 status_t status = OK;
545 // null checks for files and runtime info
546 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700547 appendLine(error, "No framework manifest file from device or from update package");
548 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700549 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800550 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700551 appendLine(error, "No device manifest file from device or from update package");
552 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700553 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800554 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700555 appendLine(error, "No framework matrix file from device or from update package");
556 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700557 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800558 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700559 appendLine(error, "No device matrix file from device or from update package");
560 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700561 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800562
Yifan Hong072f12d2018-08-08 13:04:51 -0700563 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800564 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700565 appendLine(error, "No runtime info from device");
566 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800567 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700568 }
Yifan Hong878556e2018-07-13 13:45:30 -0700569 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700570
571 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800572 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800573 if (error) {
574 error->insert(0,
575 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700576 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800577 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700578 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800579 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800580 if (error) {
581 error->insert(0,
582 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700583 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800584 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700585 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800586
Yifan Hong072f12d2018-08-08 13:04:51 -0700587 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800588 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
Yifan Hong85e589e2019-12-18 13:12:29 -0800589 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800590 if (error) {
591 error->insert(0,
592 "Runtime info and framework compatibility matrix are incompatible: ");
593 }
594 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700595 }
596 }
597
598 return COMPATIBLE;
599}
600
Yifan Hong9f78c182018-07-12 14:45:52 -0700601namespace details {
602
Yifan Hong270b5652018-01-18 18:46:01 -0800603const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800604const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800605const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong73bde592019-01-22 13:30:23 -0800606const std::string kProductVintfDir = "/product/etc/vintf/";
Yifan Hong5bbc4ae2020-01-09 14:30:27 -0800607const std::string kSystemExtVintfDir = "/system_ext/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800608
609const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800610const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800611const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800612const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong73bde592019-01-22 13:30:23 -0800613const std::string kProductMatrix = kProductVintfDir + "compatibility_matrix.xml";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700614const std::string kProductManifest = kProductVintfDir + "manifest.xml";
Yifan Hongafb80be2020-03-19 18:09:54 -0700615const std::string kSystemExtManifest = kSystemExtVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800616
Steven Morelandeedf2d42018-04-04 16:36:27 -0700617const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/";
618const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/";
619const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/";
Yifan Hong5f4e57e2019-04-23 15:16:25 -0700620const std::string kProductManifestFragmentDir = kProductVintfDir + "manifest/";
Yifan Hongafb80be2020-03-19 18:09:54 -0700621const std::string kSystemExtManifestFragmentDir = kSystemExtVintfDir + "manifest/";
Steven Morelandeedf2d42018-04-04 16:36:27 -0700622
Yifan Hong270b5652018-01-18 18:46:01 -0800623const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
624const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800625const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800626const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
627const std::string kOdmLegacyVintfDir = "/odm/etc/";
628const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
629
Yifan Hong69c1b112018-02-27 17:06:00 -0800630std::vector<std::string> dumpFileList() {
631 return {
Yifan Hong73bde592019-01-22 13:30:23 -0800632 // clang-format off
633 kSystemVintfDir,
634 kVendorVintfDir,
635 kOdmVintfDir,
636 kProductVintfDir,
Yifan Honga0968e82019-12-19 14:08:13 -0800637 kSystemExtVintfDir,
Yifan Hong73bde592019-01-22 13:30:23 -0800638 kOdmLegacyVintfDir,
639 kVendorLegacyManifest,
640 kVendorLegacyMatrix,
641 kSystemLegacyManifest,
642 kSystemLegacyMatrix,
643 // clang-format on
Yifan Hong69c1b112018-02-27 17:06:00 -0800644 };
645}
646
Yifan Hong9f78c182018-07-12 14:45:52 -0700647} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700648
Yifan Hong9f78c182018-07-12 14:45:52 -0700649bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800650 const CompatibilityMatrix& targetMatrix,
Yifan Hongb712e7e2020-03-17 17:58:35 -0700651 const ListInstances& listInstances,
652 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700653 bool isDeprecated = false;
654 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hongb712e7e2020-03-17 17:58:35 -0700655 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, childrenMap,
656 appendedError)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700657 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800658 }
Yifan Hongb712e7e2020-03-17 17:58:35 -0700659 return true; // continue to check next instance
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700660 });
661 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800662}
663
Yifan Hongb712e7e2020-03-17 17:58:35 -0700664// Let oldMatrixInstance = package@x.y-w::interface/instancePattern.
665// If any "@servedVersion::interface/servedInstance" in listInstances(package@x.y::interface)
666// matches instancePattern, return true iff for all child interfaces (from
667// GetListedInstanceInheritance), IsFqInstanceDeprecated returns false.
Yifan Hong9f78c182018-07-12 14:45:52 -0700668bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800669 const CompatibilityMatrix& targetMatrix,
Yifan Hongb712e7e2020-03-17 17:58:35 -0700670 const ListInstances& listInstances,
671 const ChildrenMap& childrenMap, std::string* appendedError) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700672 const std::string& package = oldMatrixInstance.package();
673 const Version& version = oldMatrixInstance.versionRange().minVer();
674 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700675
Yifan Honga8a8fa92018-03-20 14:42:43 -0700676 std::vector<std::string> instanceHint;
677 if (!oldMatrixInstance.isRegex()) {
678 instanceHint.push_back(oldMatrixInstance.exactInstance());
679 }
680
Yifan Hongb712e7e2020-03-17 17:58:35 -0700681 std::vector<std::string> accumulatedErrors;
Yifan Honga8a8fa92018-03-20 14:42:43 -0700682 auto list = listInstances(package, version, interface, instanceHint);
Yifan Hongb712e7e2020-03-17 17:58:35 -0700683
Yifan Honga8a8fa92018-03-20 14:42:43 -0700684 for (const auto& pair : list) {
685 const std::string& servedInstance = pair.first;
686 Version servedVersion = pair.second;
Yifan Hongb712e7e2020-03-17 17:58:35 -0700687 std::string servedFqInstanceString =
688 toFQNameString(package, servedVersion, interface, servedInstance);
Yifan Honga8a8fa92018-03-20 14:42:43 -0700689 if (!oldMatrixInstance.matchInstance(servedInstance)) {
Yifan Hongb712e7e2020-03-17 17:58:35 -0700690 // ignore unrelated instance
Yifan Honga8a8fa92018-03-20 14:42:43 -0700691 continue;
692 }
693
Yifan Hongb712e7e2020-03-17 17:58:35 -0700694 auto inheritance = GetListedInstanceInheritance(package, servedVersion, interface,
695 servedInstance, listInstances, childrenMap);
696 if (!inheritance.has_value()) {
697 accumulatedErrors.push_back(inheritance.error().message());
698 continue;
Yifan Hongf73ba512018-01-17 15:52:30 -0800699 }
700
Yifan Hongb712e7e2020-03-17 17:58:35 -0700701 std::vector<std::string> errors;
702 for (const auto& fqInstance : *inheritance) {
703 auto result = IsFqInstanceDeprecated(targetMatrix, oldMatrixInstance.format(),
704 fqInstance, listInstances);
705 if (result.ok()) {
706 errors.clear();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700707 break;
708 }
Yifan Hongb712e7e2020-03-17 17:58:35 -0700709 errors.push_back(result.error().message());
Yifan Honga8a8fa92018-03-20 14:42:43 -0700710 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800711
Yifan Hongb712e7e2020-03-17 17:58:35 -0700712 if (errors.empty()) {
713 continue;
714 }
715 accumulatedErrors.insert(accumulatedErrors.end(), errors.begin(), errors.end());
716 }
717
718 if (accumulatedErrors.empty()) {
719 return false;
720 }
721 appendLine(appendedError, android::base::Join(accumulatedErrors, "\n"));
722 return true;
723}
724
725// Check if fqInstance is listed in |listInstances|.
726bool VintfObject::IsInstanceListed(const ListInstances& listInstances,
727 const FqInstance& fqInstance) {
728 auto list =
729 listInstances(fqInstance.getPackage(), fqInstance.getVersion(), fqInstance.getInterface(),
730 {fqInstance.getInstance()} /* instanceHint*/);
731 return std::any_of(list.begin(), list.end(),
732 [&](const auto& pair) { return pair.first == fqInstance.getInstance(); });
733}
734
735// Return a list of FqInstance, where each element:
736// - is listed in |listInstances|; AND
737// - is, or inherits from, package@version::interface/instance (as specified by |childrenMap|)
738android::base::Result<std::vector<FqInstance>> VintfObject::GetListedInstanceInheritance(
739 const std::string& package, const Version& version, const std::string& interface,
740 const std::string& instance, const ListInstances& listInstances,
741 const ChildrenMap& childrenMap) {
742 FqInstance fqInstance;
743 if (!fqInstance.setTo(package, version.majorVer, version.minorVer, interface, instance)) {
744 return android::base::Error() << toFQNameString(package, version, interface, instance)
745 << " is not a valid FqInstance";
746 }
747
748 if (!IsInstanceListed(listInstances, fqInstance)) {
749 return {};
750 }
751
752 const FQName& fqName = fqInstance.getFqName();
753
754 std::vector<FqInstance> ret;
755 ret.push_back(fqInstance);
756
757 auto childRange = childrenMap.equal_range(fqName.string());
758 for (auto it = childRange.first; it != childRange.second; ++it) {
759 const auto& childFqNameString = it->second;
760 FQName childFqName;
761 if (!childFqName.setTo(childFqNameString)) {
762 return android::base::Error() << "Cannot parse " << childFqNameString << " as FQName";
763 }
764 FqInstance childFqInstance;
765 if (!childFqInstance.setTo(childFqName, fqInstance.getInstance())) {
766 return android::base::Error() << "Cannot merge " << childFqName.string() << "/"
767 << fqInstance.getInstance() << " as FqInstance";
768 continue;
769 }
770 if (!IsInstanceListed(listInstances, childFqInstance)) {
771 continue;
772 }
773 ret.push_back(childFqInstance);
774 }
775 return ret;
776}
777
778// Check if |fqInstance| is in |targetMatrix|; essentially equal to
779// targetMatrix.matchInstance(fqInstance), but provides richer error message. In details:
780// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
781// 2. package@x.z::interface/servedInstance is in targetMatrix but
782// servedInstance is not in listInstances(package@x.z::interface)
783android::base::Result<void> VintfObject::IsFqInstanceDeprecated(
784 const CompatibilityMatrix& targetMatrix, HalFormat format, const FqInstance& fqInstance,
785 const ListInstances& listInstances) {
786 // Find minimum package@x.? in target matrix, and check if instance is in target matrix.
787 bool foundInstance = false;
788 Version targetMatrixMinVer{SIZE_MAX, SIZE_MAX};
789 targetMatrix.forEachInstanceOfPackage(
790 format, fqInstance.getPackage(), [&](const auto& targetMatrixInstance) {
791 if (targetMatrixInstance.versionRange().majorVer == fqInstance.getMajorVersion() &&
792 targetMatrixInstance.interface() == fqInstance.getInterface() &&
793 targetMatrixInstance.matchInstance(fqInstance.getInstance())) {
794 targetMatrixMinVer =
795 std::min(targetMatrixMinVer, targetMatrixInstance.versionRange().minVer());
796 foundInstance = true;
797 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800798 return true;
Yifan Hongb712e7e2020-03-17 17:58:35 -0700799 });
800 if (!foundInstance) {
801 return android::base::Error()
802 << fqInstance.string() << " is deprecated in compatibility matrix at FCM Version "
803 << targetMatrix.level() << "; it should not be served.";
804 }
805
806 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
807 bool targetVersionServed = false;
808 for (const auto& newPair :
809 listInstances(fqInstance.getPackage(), targetMatrixMinVer, fqInstance.getInterface(),
810 {fqInstance.getInstance()} /* instanceHint */)) {
811 if (newPair.first == fqInstance.getInstance()) {
812 targetVersionServed = true;
813 break;
Yifan Hongf73ba512018-01-17 15:52:30 -0800814 }
815 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700816
Yifan Hongb712e7e2020-03-17 17:58:35 -0700817 if (!targetVersionServed) {
818 return android::base::Error()
819 << fqInstance.string() << " is deprecated; requires at least " << targetMatrixMinVer;
820 }
821 return {};
Yifan Hongf73ba512018-01-17 15:52:30 -0800822}
823
Yifan Hongb712e7e2020-03-17 17:58:35 -0700824int32_t VintfObject::checkDeprecation(const ListInstances& listInstances,
825 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
826 std::string* error) {
Yifan Hong73bde592019-01-22 13:30:23 -0800827 std::vector<Named<CompatibilityMatrix>> matrixFragments;
828 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
829 if (matrixFragmentsStatus != OK) {
830 return matrixFragmentsStatus;
831 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800832 if (matrixFragments.empty()) {
Yifan Hong73bde592019-01-22 13:30:23 -0800833 if (error && error->empty()) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800834 *error = "Cannot get framework matrix for each FCM version for unknown error.";
Yifan Hong73bde592019-01-22 13:30:23 -0800835 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800836 return NAME_NOT_FOUND;
837 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700838 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800839 if (deviceManifest == nullptr) {
840 if (error) *error = "No device manifest.";
841 return NAME_NOT_FOUND;
842 }
843 Level deviceLevel = deviceManifest->level();
844 if (deviceLevel == Level::UNSPECIFIED) {
845 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
846 return BAD_VALUE;
847 }
848
849 const CompatibilityMatrix* targetMatrix = nullptr;
850 for (const auto& namedMatrix : matrixFragments) {
851 if (namedMatrix.object.level() == deviceLevel) {
852 targetMatrix = &namedMatrix.object;
853 }
854 }
855 if (targetMatrix == nullptr) {
856 if (error)
857 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
858 return NAME_NOT_FOUND;
859 }
860
Yifan Hongb712e7e2020-03-17 17:58:35 -0700861 std::multimap<std::string, std::string> childrenMap;
862 for (const auto& child : hidlMetadata) {
863 for (const auto& parent : child.inherited) {
864 childrenMap.emplace(parent, child.name);
865 }
866 }
867
868 // Find a list of possibly deprecated HALs by comparing |listInstances| with older matrices.
869 // Matrices with unspecified level are considered "current".
870 bool isDeprecated = false;
Yifan Hongf73ba512018-01-17 15:52:30 -0800871 for (const auto& namedMatrix : matrixFragments) {
872 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
873 if (namedMatrix.object.level() >= deviceLevel) continue;
874
875 const auto& oldMatrix = namedMatrix.object;
876 for (const MatrixHal& hal : oldMatrix.getHals()) {
Yifan Hongb712e7e2020-03-17 17:58:35 -0700877 if (IsHalDeprecated(hal, *targetMatrix, listInstances, childrenMap, error)) {
878 isDeprecated = true;
879 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800880 }
881 }
882
Yifan Hongb712e7e2020-03-17 17:58:35 -0700883 return isDeprecated ? DEPRECATED : NO_DEPRECATED_HALS;
Yifan Hongf73ba512018-01-17 15:52:30 -0800884}
885
Yifan Hongb712e7e2020-03-17 17:58:35 -0700886int32_t VintfObject::checkDeprecation(const std::vector<HidlInterfaceMetadata>& hidlMetadata,
887 std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800888 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700889 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700890 ListInstances inManifest =
891 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
892 const std::vector<std::string>& /* hintInstances */) {
893 std::vector<std::pair<std::string, Version>> ret;
894 deviceManifest->forEachInstanceOfInterface(
Yifan Hongac621482019-09-10 19:27:20 -0700895 HalFormat::HIDL, package, version, interface,
896 [&ret](const ManifestInstance& manifestInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700897 ret.push_back(
898 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
899 return true;
900 });
901 return ret;
902 };
Yifan Hongb712e7e2020-03-17 17:58:35 -0700903 return checkDeprecation(inManifest, hidlMetadata, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800904}
Yifan Hong3daec812017-02-27 18:49:11 -0800905
Yifan Hong378c4082019-12-23 13:10:57 -0800906Level VintfObject::getKernelLevel(std::string* error) {
907 auto manifest = getDeviceHalManifest();
908 if (!manifest) {
909 if (error) *error = "Cannot retrieve device manifest.";
910 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700911 }
Yifan Hong378c4082019-12-23 13:10:57 -0800912 if (manifest->kernel().has_value() && manifest->kernel()->level() != Level::UNSPECIFIED) {
913 return manifest->kernel()->level();
Yifan Hong1e8febd2019-08-07 16:17:19 -0700914 }
Yifan Hong378c4082019-12-23 13:10:57 -0800915 if (error) *error = "Device manifest does not specify kernel FCM version.";
916 return Level::UNSPECIFIED;
Yifan Hong1e8febd2019-08-07 16:17:19 -0700917}
918
Yifan Hong9f78c182018-07-12 14:45:52 -0700919const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
920 return mFileSystem;
921}
922
Yifan Hong9f78c182018-07-12 14:45:52 -0700923const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
924 return mPropertyFetcher;
925}
926
Yifan Hongd038b2b2018-11-27 13:57:56 -0800927const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700928 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700929}
930
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700931android::base::Result<bool> VintfObject::hasFrameworkCompatibilityMatrixExtensions() {
932 std::vector<Named<CompatibilityMatrix>> matrixFragments;
933 std::string error;
934 status_t status = getAllFrameworkMatrixLevels(&matrixFragments, &error);
935 if (status != OK) {
936 return android::base::Error(-status)
937 << "Cannot get all framework matrix fragments: " << error;
938 }
939 for (const auto& namedMatrix : matrixFragments) {
940 // Returns true if product matrix exists.
941 if (android::base::StartsWith(namedMatrix.name, kProductVintfDir)) {
942 return true;
943 }
Yifan Hong238c6dc2020-03-12 22:56:16 -0700944 // Returns true if system_ext matrix exists.
945 if (android::base::StartsWith(namedMatrix.name, kSystemExtVintfDir)) {
946 return true;
947 }
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700948 // Returns true if device system matrix exists.
949 if (android::base::StartsWith(namedMatrix.name, kSystemVintfDir) &&
950 namedMatrix.object.level() == Level::UNSPECIFIED &&
951 !namedMatrix.object.getHals().empty()) {
952 return true;
953 }
954 }
955 return false;
956}
957
Yifan Hongff1a25c2020-03-17 14:09:10 -0700958android::base::Result<void> VintfObject::checkUnusedHals(
959 const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700960 auto matrix = getFrameworkCompatibilityMatrix();
961 if (matrix == nullptr) {
962 return android::base::Error(-NAME_NOT_FOUND) << "Missing framework matrix.";
963 }
964 auto manifest = getDeviceHalManifest();
965 if (manifest == nullptr) {
966 return android::base::Error(-NAME_NOT_FOUND) << "Missing device manifest.";
967 }
Yifan Hongff1a25c2020-03-17 14:09:10 -0700968 auto unused = manifest->checkUnusedHals(*matrix, hidlMetadata);
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700969 if (!unused.empty()) {
970 return android::base::Error()
971 << "The following instances are in the device manifest but "
972 << "not specified in framework compatibility matrix: \n"
973 << " " << android::base::Join(unused, "\n ") << "\n"
974 << "Suggested fix:\n"
Yifan Hong4b52dfa2020-04-07 15:22:24 -0700975 << "1. Update deprecated HALs to the latest version.\n"
976 << "2. Check for any typos in device manifest or framework compatibility "
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700977 << "matrices with FCM version >= " << matrix->level() << ".\n"
Yifan Hong4b52dfa2020-04-07 15:22:24 -0700978 << "3. For new platform HALs, add them to any framework compatibility matrix "
979 << "with FCM version >= " << matrix->level() << " where applicable.\n"
980 << "4. For device-specific HALs, add to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700981 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE.";
982 }
983 return {};
984}
985
Yifan Hong78f5b572018-11-27 14:05:03 -0800986// make_unique does not work because VintfObject constructor is private.
987VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {}
988
989VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
990 mObject->mFileSystem = std::move(e);
991 return *this;
992}
993
994VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory(
995 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
996 mObject->mRuntimeInfoFactory = std::move(e);
997 return *this;
998}
999
1000VintfObject::Builder& VintfObject::Builder::setPropertyFetcher(
1001 std::unique_ptr<PropertyFetcher>&& e) {
1002 mObject->mPropertyFetcher = std::move(e);
1003 return *this;
1004}
1005
1006std::unique_ptr<VintfObject> VintfObject::Builder::build() {
1007 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
1008 if (!mObject->mRuntimeInfoFactory)
1009 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
1010 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
1011 return std::move(mObject);
1012}
1013
Yifan Hong3daec812017-02-27 18:49:11 -08001014} // namespace vintf
1015} // namespace android