blob: a1d4a6c64f9ddaeb79ac524e6c5310f2afef5bdb [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 Hong3daec812017-02-27 18:49:11 -080021#include <functional>
22#include <memory>
23#include <mutex>
24
Yifan Hong60217032018-01-08 16:19:42 -080025#include <android-base/logging.h>
26
Yifan Hong12e23c22018-11-05 14:53:30 -080027#include "CompatibilityMatrix.h"
28#include "VintfObjectAfterUpdate.h"
29#include "parse_string.h"
30#include "parse_xml.h"
31#include "utils.h"
32
Yifan Hong60217032018-01-08 16:19:42 -080033using std::placeholders::_1;
34using std::placeholders::_2;
35
Yifan Hong3daec812017-02-27 18:49:11 -080036namespace android {
37namespace vintf {
38
Yifan Hong270b5652018-01-18 18:46:01 -080039using namespace details;
40
Yifan Hong9f78c182018-07-12 14:45:52 -070041#ifdef LIBVINTF_TARGET
42static constexpr bool kIsTarget = true;
43#else
44static constexpr bool kIsTarget = false;
45#endif
Yifan Hong1fb004e2017-09-26 15:04:44 -070046
Yifan Hong3daec812017-02-27 18:49:11 -080047template <typename T, typename F>
Yifan Hongfc73edf2017-08-29 11:39:07 -070048static std::shared_ptr<const T> Get(
49 LockedSharedPtr<T> *ptr,
Yifan Hong143cfe62017-04-13 20:18:01 -070050 bool skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080051 const F &fetchAllInformation) {
52 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong7219ba12018-01-08 16:23:49 -080053 if (skipCache || !ptr->fetchedOnce) {
Yifan Hong3daec812017-02-27 18:49:11 -080054 ptr->object = std::make_unique<T>();
Yifan Hong60217032018-01-08 16:19:42 -080055 std::string error;
56 if (fetchAllInformation(ptr->object.get(), &error) != OK) {
57 LOG(WARNING) << error;
Yifan Hong3daec812017-02-27 18:49:11 -080058 ptr->object = nullptr; // frees the old object
59 }
Yifan Hong7219ba12018-01-08 16:23:49 -080060 ptr->fetchedOnce = true;
Yifan Hong3daec812017-02-27 18:49:11 -080061 }
Yifan Hongfc73edf2017-08-29 11:39:07 -070062 return ptr->object;
Yifan Hong3daec812017-02-27 18:49:11 -080063}
64
Yifan Hong9f78c182018-07-12 14:45:52 -070065static std::unique_ptr<FileSystem> createDefaultFileSystem() {
66 std::unique_ptr<FileSystem> fileSystem;
67 if (kIsTarget) {
68 fileSystem = std::make_unique<details::FileSystemImpl>();
69 } else {
70 fileSystem = std::make_unique<details::FileSystemNoOp>();
71 }
72 return fileSystem;
73}
74
75static std::unique_ptr<PropertyFetcher> createDefaultPropertyFetcher() {
76 std::unique_ptr<PropertyFetcher> propertyFetcher;
77 if (kIsTarget) {
78 propertyFetcher = std::make_unique<details::PropertyFetcherImpl>();
79 } else {
80 propertyFetcher = std::make_unique<details::PropertyFetcherNoOp>();
81 }
82 return propertyFetcher;
83}
84
85VintfObject::VintfObject(std::unique_ptr<FileSystem>&& fileSystem,
Yifan Hong9f78c182018-07-12 14:45:52 -070086 std::unique_ptr<details::ObjectFactory<RuntimeInfo>>&& runtimeInfoFactory,
87 std::unique_ptr<details::PropertyFetcher>&& propertyFetcher)
88 : mFileSystem(fileSystem ? std::move(fileSystem) : createDefaultFileSystem()),
Yifan Hong9f78c182018-07-12 14:45:52 -070089 mRuntimeInfoFactory(runtimeInfoFactory
90 ? std::move(runtimeInfoFactory)
91 : std::make_unique<details::ObjectFactory<RuntimeInfo>>()),
92 mPropertyFetcher(propertyFetcher ? std::move(propertyFetcher)
93 : createDefaultPropertyFetcher()) {}
94
95details::LockedSharedPtr<VintfObject> VintfObject::sInstance{};
96std::shared_ptr<VintfObject> VintfObject::GetInstance() {
97 std::unique_lock<std::mutex> lock(sInstance.mutex);
98 if (sInstance.object == nullptr) {
99 sInstance.object = std::make_shared<VintfObject>();
100 }
101 return sInstance.object;
102}
103
Yifan Hongfc73edf2017-08-29 11:39:07 -0700104std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700105 return GetInstance()->getDeviceHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -0800106}
107
Yifan Hong9f78c182018-07-12 14:45:52 -0700108std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest(bool skipCache) {
109 return Get(&mDeviceManifest, skipCache,
110 std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2));
111}
112
Yifan Hongfc73edf2017-08-29 11:39:07 -0700113std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700114 return GetInstance()->getFrameworkHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -0800115}
116
Yifan Hong9f78c182018-07-12 14:45:52 -0700117std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest(bool skipCache) {
118 return Get(&mFrameworkManifest, skipCache,
119 std::bind(&VintfObject::fetchFrameworkHalManifest, this, _1, _2));
120}
Yifan Hong2272bf82017-04-28 14:37:56 -0700121
Yifan Hongfc73edf2017-08-29 11:39:07 -0700122std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700123 return GetInstance()->getDeviceCompatibilityMatrix(skipCache);
Yifan Hong2272bf82017-04-28 14:37:56 -0700124}
125
Yifan Hong9f78c182018-07-12 14:45:52 -0700126std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix(
127 bool skipCache) {
128 return Get(&mDeviceMatrix, skipCache, std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2));
129}
130
Yifan Hongfc73edf2017-08-29 11:39:07 -0700131std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700132 return GetInstance()->getFrameworkCompatibilityMatrix(skipCache);
133}
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800134
Yifan Hong9f78c182018-07-12 14:45:52 -0700135std::shared_ptr<const CompatibilityMatrix> VintfObject::getFrameworkCompatibilityMatrix(
136 bool skipCache) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800137 // To avoid deadlock, get device manifest before any locks.
Yifan Hong9f78c182018-07-12 14:45:52 -0700138 auto deviceManifest = getDeviceHalManifest();
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800139
Yifan Hong9f78c182018-07-12 14:45:52 -0700140 std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800141
142 auto combined =
Yifan Hong9f78c182018-07-12 14:45:52 -0700143 Get(&mCombinedFrameworkMatrix, skipCache,
144 std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest, _1, _2));
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800145 if (combined != nullptr) {
146 return combined;
147 }
148
Yifan Hong9f78c182018-07-12 14:45:52 -0700149 return Get(&mFrameworkMatrix, skipCache,
Yifan Hong12e23c22018-11-05 14:53:30 -0800150 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(),
Yifan Hong9f78c182018-07-12 14:45:52 -0700151 kSystemLegacyMatrix, _2));
Yifan Hong2272bf82017-04-28 14:37:56 -0700152}
153
Yifan Hong9f78c182018-07-12 14:45:52 -0700154status_t VintfObject::getCombinedFrameworkMatrix(
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800155 const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out,
156 std::string* error) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700157 auto matrixFragments = getAllFrameworkMatrixLevels(error);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800158 if (matrixFragments.empty()) {
159 return NAME_NOT_FOUND;
160 }
161
162 Level deviceLevel = Level::UNSPECIFIED;
163
164 if (deviceManifest != nullptr) {
165 deviceLevel = deviceManifest->level();
166 }
167
168 // TODO(b/70628538): Do not infer from Shipping API level.
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800169 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800170 auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800171 if (shippingApi != 0u) {
172 deviceLevel = details::convertFromApiLevel(shippingApi);
173 }
174 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800175
176 if (deviceLevel == Level::UNSPECIFIED) {
177 // Cannot infer FCM version. Combine all matrices by assuming
178 // Shipping FCM Version == min(all supported FCM Versions in the framework)
179 for (auto&& pair : matrixFragments) {
180 Level fragmentLevel = pair.object.level();
181 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
182 deviceLevel = fragmentLevel;
183 }
184 }
185 }
186
187 if (deviceLevel == Level::UNSPECIFIED) {
188 // None of the fragments specify any FCM version. Should never happen except
189 // for inconsistent builds.
190 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800191 *error = "No framework compatibility matrix files under " + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800192 " declare FCM version.";
193 }
194 return NAME_NOT_FOUND;
195 }
196
Yifan Honge7837b12018-10-11 10:38:57 -0700197 auto combined = CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800198 if (combined == nullptr) {
199 return BAD_VALUE;
200 }
201 *out = std::move(*combined);
202 return OK;
203}
204
Steven Morelandeedf2d42018-04-04 16:36:27 -0700205// Load and combine all of the manifests in a directory
Yifan Hong9f78c182018-07-12 14:45:52 -0700206status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
Steven Morelandeedf2d42018-04-04 16:36:27 -0700207 std::string* error) {
208 std::vector<std::string> fileNames;
Yifan Hong12e23c22018-11-05 14:53:30 -0800209 status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700210 // if the directory isn't there, that's okay
211 if (err == NAME_NOT_FOUND) return OK;
212 if (err != OK) return err;
213
214 for (const std::string& file : fileNames) {
215 // Only adds HALs because all other things are added by libvintf
216 // itself for now.
217 HalManifest fragmentManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700218 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700219 if (err != OK) return err;
220
221 manifest->addAllHals(&fragmentManifest);
222 }
223
224 return OK;
225}
226
Yifan Hong5a93bf22018-01-17 18:22:32 -0800227// Priority for loading vendor manifest:
Steven Morelandeedf2d42018-04-04 16:36:27 -0700228// 1. /vendor/etc/vintf/manifest.xml + device fragments + ODM manifest (optional) + odm fragments
229// 2. /vendor/etc/vintf/manifest.xml + device fragments
230// 3. ODM manifest (optional) + odm fragments
231// 4. /vendor/manifest.xml (legacy, no fragments)
Yifan Hong5a93bf22018-01-17 18:22:32 -0800232// where:
Steven Moreland30a532c2018-11-01 08:46:32 -0700233// A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
234// over A.
Yifan Hong9f78c182018-07-12 14:45:52 -0700235status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
236 status_t vendorStatus = fetchOneHalManifest(kVendorManifest, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800237 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
238 return vendorStatus;
239 }
240
Steven Morelandeedf2d42018-04-04 16:36:27 -0700241 if (vendorStatus == OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700242 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700243 if (fragmentStatus != OK) {
244 return fragmentStatus;
245 }
246 }
247
Yifan Hong5a93bf22018-01-17 18:22:32 -0800248 HalManifest odmManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700249 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800250 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
251 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800252 }
253
Yifan Hong5a93bf22018-01-17 18:22:32 -0800254 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800255 if (odmStatus == OK) {
256 out->addAllHals(&odmManifest);
257 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700258 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800259 }
260
Yifan Hong12a11ac2018-01-19 13:58:32 -0800261 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800262 if (odmStatus == OK) {
263 *out = std::move(odmManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700264 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800265 }
266
267 // Use legacy /vendor/manifest.xml
Yifan Hong12e23c22018-11-05 14:53:30 -0800268 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800269}
270
Yifan Hong12a11ac2018-01-19 13:58:32 -0800271// "out" is written to iff return status is OK.
272// Priority:
273// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
274// 2. /odm/etc/vintf/manifest.xml
275// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
276// 4. /odm/etc/manifest.xml
277// where:
278// {sku} is the value of ro.boot.product.hardware.sku
Yifan Hong9f78c182018-07-12 14:45:52 -0700279status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800280 status_t status;
281
Yifan Hong12a11ac2018-01-19 13:58:32 -0800282 std::string productModel;
Yifan Hong12e23c22018-11-05 14:53:30 -0800283 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800284
285 if (!productModel.empty()) {
286 status =
Yifan Hong9f78c182018-07-12 14:45:52 -0700287 fetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800288 if (status == OK || status != NAME_NOT_FOUND) {
289 return status;
290 }
291 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800292
Yifan Hong9f78c182018-07-12 14:45:52 -0700293 status = fetchOneHalManifest(kOdmManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800294 if (status == OK || status != NAME_NOT_FOUND) {
295 return status;
296 }
297
Yifan Hong12a11ac2018-01-19 13:58:32 -0800298 if (!productModel.empty()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700299 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800300 error);
301 if (status == OK || status != NAME_NOT_FOUND) {
302 return status;
303 }
304 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800305
Yifan Hong9f78c182018-07-12 14:45:52 -0700306 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800307 if (status == OK || status != NAME_NOT_FOUND) {
308 return status;
309 }
310
311 return NAME_NOT_FOUND;
312}
313
314// Fetch one manifest.xml file. "out" is written to iff return status is OK.
315// Returns NAME_NOT_FOUND if file is missing.
Yifan Hong9f78c182018-07-12 14:45:52 -0700316status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800317 std::string* error) {
318 HalManifest ret;
Yifan Hong12e23c22018-11-05 14:53:30 -0800319 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800320 if (status == OK) {
321 *out = std::move(ret);
322 }
323 return status;
324}
325
Yifan Hong9f78c182018-07-12 14:45:52 -0700326status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800327 CompatibilityMatrix etcMatrix;
Yifan Hong12e23c22018-11-05 14:53:30 -0800328 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800329 *out = std::move(etcMatrix);
330 return OK;
331 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800332 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800333}
334
Yifan Hong9f78c182018-07-12 14:45:52 -0700335status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
Yifan Hongde6d00e2018-02-27 17:11:28 -0800336 HalManifest etcManifest;
Yifan Hong12e23c22018-11-05 14:53:30 -0800337 if (etcManifest.fetchAllInformation(getFileSystem().get(), kSystemManifest, error) == OK) {
Yifan Hongde6d00e2018-02-27 17:11:28 -0800338 *out = std::move(etcManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700339 return addDirectoryManifests(kSystemManifestFragmentDir, out, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800340 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800341 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800342}
343
Yifan Hong9f8f6562018-11-06 16:26:20 -0800344static void appendLine(std::string* error, const std::string& message) {
345 if (error != nullptr) {
346 if (!error->empty()) *error += "\n";
347 *error += message;
348 }
349}
350
Yifan Hong9f78c182018-07-12 14:45:52 -0700351std::vector<Named<CompatibilityMatrix>> VintfObject::getAllFrameworkMatrixLevels(
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800352 std::string* error) {
353 std::vector<std::string> fileNames;
354 std::vector<Named<CompatibilityMatrix>> results;
355
Yifan Hong12e23c22018-11-05 14:53:30 -0800356 if (getFileSystem()->listFiles(kSystemVintfDir, &fileNames, error) != OK) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800357 return {};
358 }
359 for (const std::string& fileName : fileNames) {
Yifan Hong270b5652018-01-18 18:46:01 -0800360 std::string path = kSystemVintfDir + fileName;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800361
362 std::string content;
363 std::string fetchError;
Yifan Hong12e23c22018-11-05 14:53:30 -0800364 status_t status = getFileSystem()->fetch(path, &content, &fetchError);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800365 if (status != OK) {
Yifan Hong9f8f6562018-11-06 16:26:20 -0800366 appendLine(error, "Framework Matrix: Ignore file " + path + ": " + fetchError);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800367 continue;
368 }
369
370 auto it = results.emplace(results.end());
Yifan Hong9f8f6562018-11-06 16:26:20 -0800371 std::string parseError;
372 if (!gCompatibilityMatrixConverter(&it->object, content, &parseError)) {
373 appendLine(error, "Framework Matrix: Ignore file " + path + ": " + parseError);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800374 results.erase(it);
375 continue;
376 }
377 }
378
379 if (results.empty()) {
380 if (error) {
Yifan Hong9f8f6562018-11-06 16:26:20 -0800381 error->insert(0, "No framework matrices under " + kSystemVintfDir +
382 " can be fetched or parsed.\n");
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800383 }
384 } else {
385 if (error && !error->empty()) {
386 LOG(WARNING) << *error;
387 *error = "";
388 }
389 }
390
391 return results;
392}
393
Yifan Hong1fb004e2017-09-26 15:04:44 -0700394std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
395 RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700396 return GetInstance()->getRuntimeInfo(skipCache, flags);
397}
398std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(bool skipCache,
399 RuntimeInfo::FetchFlags flags) {
400 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700401
402 if (!skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700403 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700404 }
405
Yifan Hong9f78c182018-07-12 14:45:52 -0700406 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800407 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700408 }
409
Yifan Hong9f78c182018-07-12 14:45:52 -0700410 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700411 if (status != OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700412 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
Yifan Hong1fb004e2017-09-26 15:04:44 -0700413 return nullptr;
414 }
415
Yifan Hong9f78c182018-07-12 14:45:52 -0700416 mDeviceRuntimeInfo.fetchedFlags |= flags;
417 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800418}
419
Yifan Hong143cfe62017-04-13 20:18:01 -0700420namespace details {
421
Yifan Hong143cfe62017-04-13 20:18:01 -0700422enum class ParseStatus {
423 OK,
424 PARSE_ERROR,
425 DUPLICATED_FWK_ENTRY,
426 DUPLICATED_DEV_ENTRY,
427};
428
Yifan Hong9532bd22017-04-14 15:30:52 -0700429static std::string toString(ParseStatus status) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700430 switch(status) {
431 case ParseStatus::OK: return "OK";
432 case ParseStatus::PARSE_ERROR: return "parse error";
433 case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework";
434 case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device";
435 }
436 return "";
437}
438
Yifan Hong12e23c22018-11-05 14:53:30 -0800439template <typename T>
440static ParseStatus tryParse(const std::string& xml, const XmlConverter<T>& parse,
441 VintfObjectAfterUpdate* afterUpdate) {
Yifan Hongfc73edf2017-08-29 11:39:07 -0700442 std::shared_ptr<T> ret = std::make_shared<T>();
Yifan Hong94757062018-02-09 16:36:31 -0800443 if (!parse(ret.get(), xml, nullptr /* error */)) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700444 return ParseStatus::PARSE_ERROR;
445 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800446 if (!afterUpdate->set(ret)) {
447 if (ret->type() == SchemaType::FRAMEWORK) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700448 return ParseStatus::DUPLICATED_FWK_ENTRY;
Yifan Hong12e23c22018-11-05 14:53:30 -0800449 } else if (ret->type() == SchemaType::DEVICE) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700450 return ParseStatus::DUPLICATED_DEV_ENTRY;
451 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800452 LOG(FATAL) << "unknown SchemaType: "
453 << static_cast<std::underlying_type_t<SchemaType>>(ret->type());
Yifan Hong143cfe62017-04-13 20:18:01 -0700454 }
455 return ParseStatus::OK;
456}
457
Yifan Hong9f78c182018-07-12 14:45:52 -0700458} // namespace details
459
Yifan Hong12e23c22018-11-05 14:53:30 -0800460// Simulate applying xmls to VintfObject, then checkCompatibility as usual.
Yifan Hongaf3713e2018-11-05 13:59:18 -0800461int32_t VintfObject::checkCompatibility(const std::vector<std::string>& xmls, std::string* error,
462 CheckFlags::Type flags) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800463 VintfObjectAfterUpdate afterUpdate(this);
464 ParseStatus parseStatus = ParseStatus::OK;
Yifan Hong143cfe62017-04-13 20:18:01 -0700465
Yifan Hong143cfe62017-04-13 20:18:01 -0700466 // parse all information from package
467 for (const auto &xml : xmls) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800468 parseStatus = tryParse(xml, gHalManifestConverter, &afterUpdate);
Yifan Hong143cfe62017-04-13 20:18:01 -0700469 if (parseStatus == ParseStatus::OK) {
470 continue; // work on next one
471 }
472 if (parseStatus != ParseStatus::PARSE_ERROR) {
Yifan Hong878556e2018-07-13 13:45:30 -0700473 appendLine(error, toString(parseStatus) + " manifest");
Yifan Hong143cfe62017-04-13 20:18:01 -0700474 return ALREADY_EXISTS;
475 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800476 parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &afterUpdate);
Yifan Hong143cfe62017-04-13 20:18:01 -0700477 if (parseStatus == ParseStatus::OK) {
478 continue; // work on next one
479 }
480 if (parseStatus != ParseStatus::PARSE_ERROR) {
Yifan Hong878556e2018-07-13 13:45:30 -0700481 appendLine(error, toString(parseStatus) + " matrix");
Yifan Hong143cfe62017-04-13 20:18:01 -0700482 return ALREADY_EXISTS;
483 }
Yifan Hong878556e2018-07-13 13:45:30 -0700484 appendLine(error, toString(parseStatus)); // parse error
Yifan Hong143cfe62017-04-13 20:18:01 -0700485 return BAD_VALUE;
486 }
487
Yifan Hong12e23c22018-11-05 14:53:30 -0800488 return afterUpdate.checkCompatibility(error, flags);
489}
Yifan Hong8640cd12017-05-17 12:02:28 -0700490
Yifan Hong12e23c22018-11-05 14:53:30 -0800491int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
492 status_t status = OK;
493 // null checks for files and runtime info
494 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700495 appendLine(error, "No framework manifest file from device or from update package");
496 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700497 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800498 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700499 appendLine(error, "No device manifest file from device or from update package");
500 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700501 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800502 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700503 appendLine(error, "No framework matrix file from device or from update package");
504 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700505 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800506 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700507 appendLine(error, "No device matrix file from device or from update package");
508 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700509 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800510
Yifan Hong072f12d2018-08-08 13:04:51 -0700511 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800512 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700513 appendLine(error, "No runtime info from device");
514 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800515 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700516 }
Yifan Hong878556e2018-07-13 13:45:30 -0700517 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700518
519 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800520 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800521 if (error) {
522 error->insert(0,
523 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700524 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800525 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700526 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800527 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800528 if (error) {
529 error->insert(0,
530 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700531 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800532 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700533 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800534
Yifan Hong072f12d2018-08-08 13:04:51 -0700535 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800536 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
537 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800538 if (error) {
539 error->insert(0,
540 "Runtime info and framework compatibility matrix are incompatible: ");
541 }
542 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700543 }
544 }
545
546 return COMPATIBLE;
547}
548
Yifan Hong9f78c182018-07-12 14:45:52 -0700549namespace details {
550
Yifan Hong270b5652018-01-18 18:46:01 -0800551const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800552const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800553const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800554
555const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800556const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800557const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800558const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800559
Steven Morelandeedf2d42018-04-04 16:36:27 -0700560const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/";
561const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/";
562const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/";
563
Yifan Hong270b5652018-01-18 18:46:01 -0800564const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
565const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800566const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800567const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
568const std::string kOdmLegacyVintfDir = "/odm/etc/";
569const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
570
Yifan Hong69c1b112018-02-27 17:06:00 -0800571std::vector<std::string> dumpFileList() {
572 return {
573 kSystemVintfDir, kVendorVintfDir, kOdmVintfDir, kOdmLegacyVintfDir,
574
575 kVendorLegacyManifest, kVendorLegacyMatrix, kSystemLegacyManifest, kSystemLegacyMatrix,
576 };
577}
578
Yifan Hong9f78c182018-07-12 14:45:52 -0700579} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700580
Yifan Hongdb6423e2017-09-11 14:38:46 -0700581int32_t VintfObject::CheckCompatibility(const std::vector<std::string>& xmls, std::string* error,
Yifan Hong072f12d2018-08-08 13:04:51 -0700582 CheckFlags::Type flags) {
583 return GetInstance()->checkCompatibility(xmls, error, flags);
Yifan Hongfbbf0472017-04-07 18:14:18 -0700584}
585
Yifan Hong9f78c182018-07-12 14:45:52 -0700586bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800587 const CompatibilityMatrix& targetMatrix,
Yifan Honga8a8fa92018-03-20 14:42:43 -0700588 const ListInstances& listInstances, std::string* error) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700589 bool isDeprecated = false;
590 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700591 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, error)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700592 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800593 }
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700594 return !isDeprecated; // continue if no deprecated instance is found.
595 });
596 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800597}
598
Yifan Honga8a8fa92018-03-20 14:42:43 -0700599// Let oldMatrixInstance = package@x.y-w::interface with instancePattern.
600// If any "servedInstance" in listInstances(package@x.y::interface) matches instancePattern, return
601// true iff:
602// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
603// 2. package@x.z::interface/servedInstance is in targetMatrix but
604// servedInstance is not in listInstances(package@x.z::interface)
Yifan Hong9f78c182018-07-12 14:45:52 -0700605bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800606 const CompatibilityMatrix& targetMatrix,
Yifan Honga8a8fa92018-03-20 14:42:43 -0700607 const ListInstances& listInstances, std::string* error) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700608 const std::string& package = oldMatrixInstance.package();
609 const Version& version = oldMatrixInstance.versionRange().minVer();
610 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700611
Yifan Honga8a8fa92018-03-20 14:42:43 -0700612 std::vector<std::string> instanceHint;
613 if (!oldMatrixInstance.isRegex()) {
614 instanceHint.push_back(oldMatrixInstance.exactInstance());
615 }
616
617 auto list = listInstances(package, version, interface, instanceHint);
618 for (const auto& pair : list) {
619 const std::string& servedInstance = pair.first;
620 Version servedVersion = pair.second;
621 if (!oldMatrixInstance.matchInstance(servedInstance)) {
622 continue;
623 }
624
Yifan Hongf73ba512018-01-17 15:52:30 -0800625 // Find any package@x.? in target matrix, and check if instance is in target matrix.
Yifan Hong217f47e2018-03-15 12:34:05 -0700626 bool foundInstance = false;
627 Version targetMatrixMinVer;
628 targetMatrix.forEachInstanceOfPackage(package, [&](const auto& targetMatrixInstance) {
629 if (targetMatrixInstance.versionRange().majorVer == version.majorVer &&
630 targetMatrixInstance.interface() == interface &&
Yifan Honga8a8fa92018-03-20 14:42:43 -0700631 targetMatrixInstance.matchInstance(servedInstance)) {
Yifan Hong217f47e2018-03-15 12:34:05 -0700632 targetMatrixMinVer = targetMatrixInstance.versionRange().minVer();
633 foundInstance = true;
634 }
635 return !foundInstance; // continue if not found
636 });
637 if (!foundInstance) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800638 if (error) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700639 *error = toFQNameString(package, servedVersion, interface, servedInstance) +
Steven Morelanda1b984c2018-03-09 13:09:15 -0800640 " is deprecated in compatibility matrix at FCM Version " +
Yifan Hongf73ba512018-01-17 15:52:30 -0800641 to_string(targetMatrix.level()) + "; it should not be served.";
642 }
643 return true;
644 }
645
Yifan Hongf73ba512018-01-17 15:52:30 -0800646 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
Yifan Honga8a8fa92018-03-20 14:42:43 -0700647 bool targetVersionServed = false;
648 for (const auto& newPair :
649 listInstances(package, targetMatrixMinVer, interface, instanceHint)) {
650 if (newPair.first == servedInstance) {
651 targetVersionServed = true;
652 break;
653 }
654 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800655
656 if (!targetVersionServed) {
Yifan Hong9f8f6562018-11-06 16:26:20 -0800657 appendLine(error, toFQNameString(package, servedVersion, interface, servedInstance) +
658 " is deprecated; requires at least " +
659 to_string(targetMatrixMinVer));
Yifan Hongf73ba512018-01-17 15:52:30 -0800660 return true;
661 }
662 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700663
Yifan Hongf73ba512018-01-17 15:52:30 -0800664 return false;
665}
666
Yifan Honga8a8fa92018-03-20 14:42:43 -0700667int32_t VintfObject::CheckDeprecation(const ListInstances& listInstances, std::string* error) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700668 return GetInstance()->checkDeprecation(listInstances, error);
669}
670int32_t VintfObject::checkDeprecation(const ListInstances& listInstances, std::string* error) {
671 auto matrixFragments = getAllFrameworkMatrixLevels(error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800672 if (matrixFragments.empty()) {
673 if (error && error->empty())
674 *error = "Cannot get framework matrix for each FCM version for unknown error.";
675 return NAME_NOT_FOUND;
676 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700677 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800678 if (deviceManifest == nullptr) {
679 if (error) *error = "No device manifest.";
680 return NAME_NOT_FOUND;
681 }
682 Level deviceLevel = deviceManifest->level();
683 if (deviceLevel == Level::UNSPECIFIED) {
684 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
685 return BAD_VALUE;
686 }
687
688 const CompatibilityMatrix* targetMatrix = nullptr;
689 for (const auto& namedMatrix : matrixFragments) {
690 if (namedMatrix.object.level() == deviceLevel) {
691 targetMatrix = &namedMatrix.object;
692 }
693 }
694 if (targetMatrix == nullptr) {
695 if (error)
696 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
697 return NAME_NOT_FOUND;
698 }
699
700 bool hasDeprecatedHals = false;
701 for (const auto& namedMatrix : matrixFragments) {
702 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
703 if (namedMatrix.object.level() >= deviceLevel) continue;
704
705 const auto& oldMatrix = namedMatrix.object;
706 for (const MatrixHal& hal : oldMatrix.getHals()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700707 hasDeprecatedHals |= IsHalDeprecated(hal, *targetMatrix, listInstances, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800708 }
709 }
710
711 return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS;
712}
713
714int32_t VintfObject::CheckDeprecation(std::string* error) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700715 return GetInstance()->checkDeprecation(error);
716}
717int32_t VintfObject::checkDeprecation(std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800718 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700719 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700720 ListInstances inManifest =
721 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
722 const std::vector<std::string>& /* hintInstances */) {
723 std::vector<std::pair<std::string, Version>> ret;
724 deviceManifest->forEachInstanceOfInterface(
725 package, version, interface, [&ret](const ManifestInstance& manifestInstance) {
726 ret.push_back(
727 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
728 return true;
729 });
730 return ret;
731 };
Yifan Hong9f78c182018-07-12 14:45:52 -0700732 return checkDeprecation(inManifest, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800733}
Yifan Hong3daec812017-02-27 18:49:11 -0800734
Yifan Hong9f78c182018-07-12 14:45:52 -0700735const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
736 return mFileSystem;
737}
738
Yifan Hong9f78c182018-07-12 14:45:52 -0700739const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
740 return mPropertyFetcher;
741}
742
743const std::unique_ptr<details::ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
744 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700745}
746
Yifan Hong3daec812017-02-27 18:49:11 -0800747} // namespace vintf
748} // namespace android