blob: 6a5685aa2090e298d80e813da83e3d5ee6ac7669 [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 Hongd038b2b2018-11-27 13:57:56 -080086 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& runtimeInfoFactory,
87 std::unique_ptr<PropertyFetcher>&& propertyFetcher)
Yifan Hong9f78c182018-07-12 14:45:52 -070088 : mFileSystem(fileSystem ? std::move(fileSystem) : createDefaultFileSystem()),
Yifan Hongd038b2b2018-11-27 13:57:56 -080089 mRuntimeInfoFactory(runtimeInfoFactory ? std::move(runtimeInfoFactory)
90 : std::make_unique<ObjectFactory<RuntimeInfo>>()),
Yifan Hong9f78c182018-07-12 14:45:52 -070091 mPropertyFetcher(propertyFetcher ? std::move(propertyFetcher)
92 : createDefaultPropertyFetcher()) {}
93
94details::LockedSharedPtr<VintfObject> VintfObject::sInstance{};
95std::shared_ptr<VintfObject> VintfObject::GetInstance() {
96 std::unique_lock<std::mutex> lock(sInstance.mutex);
97 if (sInstance.object == nullptr) {
98 sInstance.object = std::make_shared<VintfObject>();
99 }
100 return sInstance.object;
101}
102
Yifan Hongfc73edf2017-08-29 11:39:07 -0700103std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700104 return GetInstance()->getDeviceHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -0800105}
106
Yifan Hong9f78c182018-07-12 14:45:52 -0700107std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest(bool skipCache) {
108 return Get(&mDeviceManifest, skipCache,
109 std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2));
110}
111
Yifan Hongfc73edf2017-08-29 11:39:07 -0700112std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700113 return GetInstance()->getFrameworkHalManifest(skipCache);
Yifan Hong3daec812017-02-27 18:49:11 -0800114}
115
Yifan Hong9f78c182018-07-12 14:45:52 -0700116std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest(bool skipCache) {
117 return Get(&mFrameworkManifest, skipCache,
118 std::bind(&VintfObject::fetchFrameworkHalManifest, this, _1, _2));
119}
Yifan Hong2272bf82017-04-28 14:37:56 -0700120
Yifan Hongfc73edf2017-08-29 11:39:07 -0700121std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700122 return GetInstance()->getDeviceCompatibilityMatrix(skipCache);
Yifan Hong2272bf82017-04-28 14:37:56 -0700123}
124
Yifan Hong9f78c182018-07-12 14:45:52 -0700125std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix(
126 bool skipCache) {
127 return Get(&mDeviceMatrix, skipCache, std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2));
128}
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 =
Yifan Hong9f78c182018-07-12 14:45:52 -0700142 Get(&mCombinedFrameworkMatrix, skipCache,
143 std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest, _1, _2));
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800144 if (combined != nullptr) {
145 return combined;
146 }
147
Yifan Hong9f78c182018-07-12 14:45:52 -0700148 return Get(&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 Hong9f78c182018-07-12 14:45:52 -0700156 auto matrixFragments = getAllFrameworkMatrixLevels(error);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800157 if (matrixFragments.empty()) {
158 return NAME_NOT_FOUND;
159 }
160
161 Level deviceLevel = Level::UNSPECIFIED;
162
163 if (deviceManifest != nullptr) {
164 deviceLevel = deviceManifest->level();
165 }
166
167 // TODO(b/70628538): Do not infer from Shipping API level.
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800168 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800169 auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800170 if (shippingApi != 0u) {
171 deviceLevel = details::convertFromApiLevel(shippingApi);
172 }
173 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800174
175 if (deviceLevel == Level::UNSPECIFIED) {
176 // Cannot infer FCM version. Combine all matrices by assuming
177 // Shipping FCM Version == min(all supported FCM Versions in the framework)
178 for (auto&& pair : matrixFragments) {
179 Level fragmentLevel = pair.object.level();
180 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
181 deviceLevel = fragmentLevel;
182 }
183 }
184 }
185
186 if (deviceLevel == Level::UNSPECIFIED) {
187 // None of the fragments specify any FCM version. Should never happen except
188 // for inconsistent builds.
189 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800190 *error = "No framework compatibility matrix files under " + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800191 " declare FCM version.";
192 }
193 return NAME_NOT_FOUND;
194 }
195
Yifan Honge7837b12018-10-11 10:38:57 -0700196 auto combined = CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800197 if (combined == nullptr) {
198 return BAD_VALUE;
199 }
200 *out = std::move(*combined);
201 return OK;
202}
203
Steven Morelandeedf2d42018-04-04 16:36:27 -0700204// Load and combine all of the manifests in a directory
Yifan Hong9f78c182018-07-12 14:45:52 -0700205status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
Steven Morelandeedf2d42018-04-04 16:36:27 -0700206 std::string* error) {
207 std::vector<std::string> fileNames;
Yifan Hong12e23c22018-11-05 14:53:30 -0800208 status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700209 // if the directory isn't there, that's okay
210 if (err == NAME_NOT_FOUND) return OK;
211 if (err != OK) return err;
212
213 for (const std::string& file : fileNames) {
214 // Only adds HALs because all other things are added by libvintf
215 // itself for now.
216 HalManifest fragmentManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700217 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700218 if (err != OK) return err;
219
220 manifest->addAllHals(&fragmentManifest);
221 }
222
223 return OK;
224}
225
Yifan Hong5a93bf22018-01-17 18:22:32 -0800226// Priority for loading vendor manifest:
Steven Morelandeedf2d42018-04-04 16:36:27 -0700227// 1. /vendor/etc/vintf/manifest.xml + device fragments + ODM manifest (optional) + odm fragments
228// 2. /vendor/etc/vintf/manifest.xml + device fragments
229// 3. ODM manifest (optional) + odm fragments
230// 4. /vendor/manifest.xml (legacy, no fragments)
Yifan Hong5a93bf22018-01-17 18:22:32 -0800231// where:
Steven Moreland30a532c2018-11-01 08:46:32 -0700232// A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
233// over A.
Yifan Hong9f78c182018-07-12 14:45:52 -0700234status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
235 status_t vendorStatus = fetchOneHalManifest(kVendorManifest, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800236 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
237 return vendorStatus;
238 }
239
Steven Morelandeedf2d42018-04-04 16:36:27 -0700240 if (vendorStatus == OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700241 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700242 if (fragmentStatus != OK) {
243 return fragmentStatus;
244 }
245 }
246
Yifan Hong5a93bf22018-01-17 18:22:32 -0800247 HalManifest odmManifest;
Yifan Hong9f78c182018-07-12 14:45:52 -0700248 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800249 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
250 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800251 }
252
Yifan Hong5a93bf22018-01-17 18:22:32 -0800253 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800254 if (odmStatus == OK) {
255 out->addAllHals(&odmManifest);
256 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700257 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800258 }
259
Yifan Hong12a11ac2018-01-19 13:58:32 -0800260 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800261 if (odmStatus == OK) {
262 *out = std::move(odmManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700263 return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800264 }
265
266 // Use legacy /vendor/manifest.xml
Yifan Hong12e23c22018-11-05 14:53:30 -0800267 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800268}
269
Yifan Hong12a11ac2018-01-19 13:58:32 -0800270// "out" is written to iff return status is OK.
271// Priority:
272// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
273// 2. /odm/etc/vintf/manifest.xml
274// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
275// 4. /odm/etc/manifest.xml
276// where:
277// {sku} is the value of ro.boot.product.hardware.sku
Yifan Hong9f78c182018-07-12 14:45:52 -0700278status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800279 status_t status;
280
Yifan Hong12a11ac2018-01-19 13:58:32 -0800281 std::string productModel;
Yifan Hong12e23c22018-11-05 14:53:30 -0800282 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800283
284 if (!productModel.empty()) {
285 status =
Yifan Hong9f78c182018-07-12 14:45:52 -0700286 fetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800287 if (status == OK || status != NAME_NOT_FOUND) {
288 return status;
289 }
290 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800291
Yifan Hong9f78c182018-07-12 14:45:52 -0700292 status = fetchOneHalManifest(kOdmManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800293 if (status == OK || status != NAME_NOT_FOUND) {
294 return status;
295 }
296
Yifan Hong12a11ac2018-01-19 13:58:32 -0800297 if (!productModel.empty()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700298 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800299 error);
300 if (status == OK || status != NAME_NOT_FOUND) {
301 return status;
302 }
303 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800304
Yifan Hong9f78c182018-07-12 14:45:52 -0700305 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800306 if (status == OK || status != NAME_NOT_FOUND) {
307 return status;
308 }
309
310 return NAME_NOT_FOUND;
311}
312
313// Fetch one manifest.xml file. "out" is written to iff return status is OK.
314// Returns NAME_NOT_FOUND if file is missing.
Yifan Hong9f78c182018-07-12 14:45:52 -0700315status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
Yifan Hong12a11ac2018-01-19 13:58:32 -0800316 std::string* error) {
317 HalManifest ret;
Yifan Hong12e23c22018-11-05 14:53:30 -0800318 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
Yifan Hong12a11ac2018-01-19 13:58:32 -0800319 if (status == OK) {
320 *out = std::move(ret);
321 }
322 return status;
323}
324
Yifan Hong9f78c182018-07-12 14:45:52 -0700325status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800326 CompatibilityMatrix etcMatrix;
Yifan Hong12e23c22018-11-05 14:53:30 -0800327 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800328 *out = std::move(etcMatrix);
329 return OK;
330 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800331 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800332}
333
Yifan Hong9f78c182018-07-12 14:45:52 -0700334status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
Yifan Hongde6d00e2018-02-27 17:11:28 -0800335 HalManifest etcManifest;
Yifan Hong12e23c22018-11-05 14:53:30 -0800336 if (etcManifest.fetchAllInformation(getFileSystem().get(), kSystemManifest, error) == OK) {
Yifan Hongde6d00e2018-02-27 17:11:28 -0800337 *out = std::move(etcManifest);
Yifan Hong9f78c182018-07-12 14:45:52 -0700338 return addDirectoryManifests(kSystemManifestFragmentDir, out, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800339 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800340 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800341}
342
Yifan Hong9f8f6562018-11-06 16:26:20 -0800343static void appendLine(std::string* error, const std::string& message) {
344 if (error != nullptr) {
345 if (!error->empty()) *error += "\n";
346 *error += message;
347 }
348}
349
Yifan Hong9f78c182018-07-12 14:45:52 -0700350std::vector<Named<CompatibilityMatrix>> VintfObject::getAllFrameworkMatrixLevels(
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800351 std::string* error) {
352 std::vector<std::string> fileNames;
353 std::vector<Named<CompatibilityMatrix>> results;
354
Yifan Hong12e23c22018-11-05 14:53:30 -0800355 if (getFileSystem()->listFiles(kSystemVintfDir, &fileNames, error) != OK) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800356 return {};
357 }
358 for (const std::string& fileName : fileNames) {
Yifan Hong270b5652018-01-18 18:46:01 -0800359 std::string path = kSystemVintfDir + fileName;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800360
361 std::string content;
362 std::string fetchError;
Yifan Hong12e23c22018-11-05 14:53:30 -0800363 status_t status = getFileSystem()->fetch(path, &content, &fetchError);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800364 if (status != OK) {
Yifan Hong9f8f6562018-11-06 16:26:20 -0800365 appendLine(error, "Framework Matrix: Ignore file " + path + ": " + fetchError);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800366 continue;
367 }
368
369 auto it = results.emplace(results.end());
Yifan Hong9f8f6562018-11-06 16:26:20 -0800370 std::string parseError;
371 if (!gCompatibilityMatrixConverter(&it->object, content, &parseError)) {
372 appendLine(error, "Framework Matrix: Ignore file " + path + ": " + parseError);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800373 results.erase(it);
374 continue;
375 }
376 }
377
378 if (results.empty()) {
379 if (error) {
Yifan Hong9f8f6562018-11-06 16:26:20 -0800380 error->insert(0, "No framework matrices under " + kSystemVintfDir +
381 " can be fetched or parsed.\n");
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800382 }
383 } else {
384 if (error && !error->empty()) {
385 LOG(WARNING) << *error;
386 *error = "";
387 }
388 }
389
390 return results;
391}
392
Yifan Hong1fb004e2017-09-26 15:04:44 -0700393std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
394 RuntimeInfo::FetchFlags flags) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700395 return GetInstance()->getRuntimeInfo(skipCache, flags);
396}
397std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(bool skipCache,
398 RuntimeInfo::FetchFlags flags) {
399 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700400
401 if (!skipCache) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700402 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700403 }
404
Yifan Hong9f78c182018-07-12 14:45:52 -0700405 if (mDeviceRuntimeInfo.object == nullptr) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800406 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700407 }
408
Yifan Hong9f78c182018-07-12 14:45:52 -0700409 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
Yifan Hong1fb004e2017-09-26 15:04:44 -0700410 if (status != OK) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700411 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
Yifan Hong1fb004e2017-09-26 15:04:44 -0700412 return nullptr;
413 }
414
Yifan Hong9f78c182018-07-12 14:45:52 -0700415 mDeviceRuntimeInfo.fetchedFlags |= flags;
416 return mDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800417}
418
Yifan Hong143cfe62017-04-13 20:18:01 -0700419namespace details {
420
Yifan Hong143cfe62017-04-13 20:18:01 -0700421enum class ParseStatus {
422 OK,
423 PARSE_ERROR,
424 DUPLICATED_FWK_ENTRY,
425 DUPLICATED_DEV_ENTRY,
426};
427
Yifan Hong9532bd22017-04-14 15:30:52 -0700428static std::string toString(ParseStatus status) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700429 switch(status) {
430 case ParseStatus::OK: return "OK";
431 case ParseStatus::PARSE_ERROR: return "parse error";
432 case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework";
433 case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device";
434 }
435 return "";
436}
437
Yifan Hong12e23c22018-11-05 14:53:30 -0800438template <typename T>
439static ParseStatus tryParse(const std::string& xml, const XmlConverter<T>& parse,
440 VintfObjectAfterUpdate* afterUpdate) {
Yifan Hongfc73edf2017-08-29 11:39:07 -0700441 std::shared_ptr<T> ret = std::make_shared<T>();
Yifan Hong94757062018-02-09 16:36:31 -0800442 if (!parse(ret.get(), xml, nullptr /* error */)) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700443 return ParseStatus::PARSE_ERROR;
444 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800445 if (!afterUpdate->set(ret)) {
446 if (ret->type() == SchemaType::FRAMEWORK) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700447 return ParseStatus::DUPLICATED_FWK_ENTRY;
Yifan Hong12e23c22018-11-05 14:53:30 -0800448 } else if (ret->type() == SchemaType::DEVICE) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700449 return ParseStatus::DUPLICATED_DEV_ENTRY;
450 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800451 LOG(FATAL) << "unknown SchemaType: "
452 << static_cast<std::underlying_type_t<SchemaType>>(ret->type());
Yifan Hong143cfe62017-04-13 20:18:01 -0700453 }
454 return ParseStatus::OK;
455}
456
Yifan Hong9f78c182018-07-12 14:45:52 -0700457} // namespace details
458
Yifan Hong12e23c22018-11-05 14:53:30 -0800459// Simulate applying xmls to VintfObject, then checkCompatibility as usual.
Yifan Hongaf3713e2018-11-05 13:59:18 -0800460int32_t VintfObject::checkCompatibility(const std::vector<std::string>& xmls, std::string* error,
461 CheckFlags::Type flags) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800462 VintfObjectAfterUpdate afterUpdate(this);
463 ParseStatus parseStatus = ParseStatus::OK;
Yifan Hong143cfe62017-04-13 20:18:01 -0700464
Yifan Hong143cfe62017-04-13 20:18:01 -0700465 // parse all information from package
466 for (const auto &xml : xmls) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800467 parseStatus = tryParse(xml, gHalManifestConverter, &afterUpdate);
Yifan Hong143cfe62017-04-13 20:18:01 -0700468 if (parseStatus == ParseStatus::OK) {
469 continue; // work on next one
470 }
471 if (parseStatus != ParseStatus::PARSE_ERROR) {
Yifan Hong878556e2018-07-13 13:45:30 -0700472 appendLine(error, toString(parseStatus) + " manifest");
Yifan Hong143cfe62017-04-13 20:18:01 -0700473 return ALREADY_EXISTS;
474 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800475 parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &afterUpdate);
Yifan Hong143cfe62017-04-13 20:18:01 -0700476 if (parseStatus == ParseStatus::OK) {
477 continue; // work on next one
478 }
479 if (parseStatus != ParseStatus::PARSE_ERROR) {
Yifan Hong878556e2018-07-13 13:45:30 -0700480 appendLine(error, toString(parseStatus) + " matrix");
Yifan Hong143cfe62017-04-13 20:18:01 -0700481 return ALREADY_EXISTS;
482 }
Yifan Hong878556e2018-07-13 13:45:30 -0700483 appendLine(error, toString(parseStatus)); // parse error
Yifan Hong143cfe62017-04-13 20:18:01 -0700484 return BAD_VALUE;
485 }
486
Yifan Hong12e23c22018-11-05 14:53:30 -0800487 return afterUpdate.checkCompatibility(error, flags);
488}
Yifan Hong8640cd12017-05-17 12:02:28 -0700489
Yifan Hong12e23c22018-11-05 14:53:30 -0800490int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
491 status_t status = OK;
492 // null checks for files and runtime info
493 if (getFrameworkHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700494 appendLine(error, "No framework manifest file from device or from update package");
495 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700496 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800497 if (getDeviceHalManifest() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700498 appendLine(error, "No device manifest file from device or from update package");
499 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700500 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800501 if (getFrameworkCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700502 appendLine(error, "No framework matrix file from device or from update package");
503 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700504 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800505 if (getDeviceCompatibilityMatrix() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700506 appendLine(error, "No device matrix file from device or from update package");
507 status = NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700508 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800509
Yifan Hong072f12d2018-08-08 13:04:51 -0700510 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800511 if (getRuntimeInfo() == nullptr) {
Yifan Hong878556e2018-07-13 13:45:30 -0700512 appendLine(error, "No runtime info from device");
513 status = NO_INIT;
Yifan Hong69c1b112018-02-27 17:06:00 -0800514 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700515 }
Yifan Hong878556e2018-07-13 13:45:30 -0700516 if (status != OK) return status;
Yifan Hong143cfe62017-04-13 20:18:01 -0700517
518 // compatiblity check.
Yifan Hong12e23c22018-11-05 14:53:30 -0800519 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800520 if (error) {
521 error->insert(0,
522 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700523 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800524 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700525 }
Yifan Hong12e23c22018-11-05 14:53:30 -0800526 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800527 if (error) {
528 error->insert(0,
529 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700530 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800531 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700532 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800533
Yifan Hong072f12d2018-08-08 13:04:51 -0700534 if (flags.isRuntimeInfoEnabled()) {
Yifan Hong12e23c22018-11-05 14:53:30 -0800535 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
536 flags)) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800537 if (error) {
538 error->insert(0,
539 "Runtime info and framework compatibility matrix are incompatible: ");
540 }
541 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700542 }
543 }
544
545 return COMPATIBLE;
546}
547
Yifan Hong9f78c182018-07-12 14:45:52 -0700548namespace details {
549
Yifan Hong270b5652018-01-18 18:46:01 -0800550const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800551const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800552const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800553
554const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800555const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800556const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800557const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800558
Steven Morelandeedf2d42018-04-04 16:36:27 -0700559const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/";
560const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/";
561const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/";
562
Yifan Hong270b5652018-01-18 18:46:01 -0800563const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
564const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800565const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800566const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
567const std::string kOdmLegacyVintfDir = "/odm/etc/";
568const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
569
Yifan Hong69c1b112018-02-27 17:06:00 -0800570std::vector<std::string> dumpFileList() {
571 return {
572 kSystemVintfDir, kVendorVintfDir, kOdmVintfDir, kOdmLegacyVintfDir,
573
574 kVendorLegacyManifest, kVendorLegacyMatrix, kSystemLegacyManifest, kSystemLegacyMatrix,
575 };
576}
577
Yifan Hong9f78c182018-07-12 14:45:52 -0700578} // namespace details
Yifan Hong143cfe62017-04-13 20:18:01 -0700579
Yifan Hongdb6423e2017-09-11 14:38:46 -0700580int32_t VintfObject::CheckCompatibility(const std::vector<std::string>& xmls, std::string* error,
Yifan Hong072f12d2018-08-08 13:04:51 -0700581 CheckFlags::Type flags) {
582 return GetInstance()->checkCompatibility(xmls, error, flags);
Yifan Hongfbbf0472017-04-07 18:14:18 -0700583}
584
Yifan Hong9f78c182018-07-12 14:45:52 -0700585bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
Yifan Hongf73ba512018-01-17 15:52:30 -0800586 const CompatibilityMatrix& targetMatrix,
Yifan Honga8a8fa92018-03-20 14:42:43 -0700587 const ListInstances& listInstances, std::string* error) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700588 bool isDeprecated = false;
589 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700590 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, error)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700591 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800592 }
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700593 return !isDeprecated; // continue if no deprecated instance is found.
594 });
595 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800596}
597
Yifan Honga8a8fa92018-03-20 14:42:43 -0700598// Let oldMatrixInstance = package@x.y-w::interface with instancePattern.
599// If any "servedInstance" in listInstances(package@x.y::interface) matches instancePattern, return
600// true iff:
601// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
602// 2. package@x.z::interface/servedInstance is in targetMatrix but
603// servedInstance is not in listInstances(package@x.z::interface)
Yifan Hong9f78c182018-07-12 14:45:52 -0700604bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800605 const CompatibilityMatrix& targetMatrix,
Yifan Honga8a8fa92018-03-20 14:42:43 -0700606 const ListInstances& listInstances, std::string* error) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700607 const std::string& package = oldMatrixInstance.package();
608 const Version& version = oldMatrixInstance.versionRange().minVer();
609 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700610
Yifan Honga8a8fa92018-03-20 14:42:43 -0700611 std::vector<std::string> instanceHint;
612 if (!oldMatrixInstance.isRegex()) {
613 instanceHint.push_back(oldMatrixInstance.exactInstance());
614 }
615
616 auto list = listInstances(package, version, interface, instanceHint);
617 for (const auto& pair : list) {
618 const std::string& servedInstance = pair.first;
619 Version servedVersion = pair.second;
620 if (!oldMatrixInstance.matchInstance(servedInstance)) {
621 continue;
622 }
623
Yifan Hongf73ba512018-01-17 15:52:30 -0800624 // Find any package@x.? in target matrix, and check if instance is in target matrix.
Yifan Hong217f47e2018-03-15 12:34:05 -0700625 bool foundInstance = false;
626 Version targetMatrixMinVer;
627 targetMatrix.forEachInstanceOfPackage(package, [&](const auto& targetMatrixInstance) {
628 if (targetMatrixInstance.versionRange().majorVer == version.majorVer &&
629 targetMatrixInstance.interface() == interface &&
Yifan Honga8a8fa92018-03-20 14:42:43 -0700630 targetMatrixInstance.matchInstance(servedInstance)) {
Yifan Hong217f47e2018-03-15 12:34:05 -0700631 targetMatrixMinVer = targetMatrixInstance.versionRange().minVer();
632 foundInstance = true;
633 }
634 return !foundInstance; // continue if not found
635 });
636 if (!foundInstance) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800637 if (error) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700638 *error = toFQNameString(package, servedVersion, interface, servedInstance) +
Steven Morelanda1b984c2018-03-09 13:09:15 -0800639 " is deprecated in compatibility matrix at FCM Version " +
Yifan Hongf73ba512018-01-17 15:52:30 -0800640 to_string(targetMatrix.level()) + "; it should not be served.";
641 }
642 return true;
643 }
644
Yifan Hongf73ba512018-01-17 15:52:30 -0800645 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
Yifan Honga8a8fa92018-03-20 14:42:43 -0700646 bool targetVersionServed = false;
647 for (const auto& newPair :
648 listInstances(package, targetMatrixMinVer, interface, instanceHint)) {
649 if (newPair.first == servedInstance) {
650 targetVersionServed = true;
651 break;
652 }
653 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800654
655 if (!targetVersionServed) {
Yifan Hong9f8f6562018-11-06 16:26:20 -0800656 appendLine(error, toFQNameString(package, servedVersion, interface, servedInstance) +
657 " is deprecated; requires at least " +
658 to_string(targetMatrixMinVer));
Yifan Hongf73ba512018-01-17 15:52:30 -0800659 return true;
660 }
661 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700662
Yifan Hongf73ba512018-01-17 15:52:30 -0800663 return false;
664}
665
Yifan Honga8a8fa92018-03-20 14:42:43 -0700666int32_t VintfObject::CheckDeprecation(const ListInstances& listInstances, std::string* error) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700667 return GetInstance()->checkDeprecation(listInstances, error);
668}
669int32_t VintfObject::checkDeprecation(const ListInstances& listInstances, std::string* error) {
670 auto matrixFragments = getAllFrameworkMatrixLevels(error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800671 if (matrixFragments.empty()) {
672 if (error && error->empty())
673 *error = "Cannot get framework matrix for each FCM version for unknown error.";
674 return NAME_NOT_FOUND;
675 }
Yifan Hong9f78c182018-07-12 14:45:52 -0700676 auto deviceManifest = getDeviceHalManifest();
Yifan Hongf73ba512018-01-17 15:52:30 -0800677 if (deviceManifest == nullptr) {
678 if (error) *error = "No device manifest.";
679 return NAME_NOT_FOUND;
680 }
681 Level deviceLevel = deviceManifest->level();
682 if (deviceLevel == Level::UNSPECIFIED) {
683 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
684 return BAD_VALUE;
685 }
686
687 const CompatibilityMatrix* targetMatrix = nullptr;
688 for (const auto& namedMatrix : matrixFragments) {
689 if (namedMatrix.object.level() == deviceLevel) {
690 targetMatrix = &namedMatrix.object;
691 }
692 }
693 if (targetMatrix == nullptr) {
694 if (error)
695 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
696 return NAME_NOT_FOUND;
697 }
698
699 bool hasDeprecatedHals = false;
700 for (const auto& namedMatrix : matrixFragments) {
701 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
702 if (namedMatrix.object.level() >= deviceLevel) continue;
703
704 const auto& oldMatrix = namedMatrix.object;
705 for (const MatrixHal& hal : oldMatrix.getHals()) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700706 hasDeprecatedHals |= IsHalDeprecated(hal, *targetMatrix, listInstances, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800707 }
708 }
709
710 return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS;
711}
712
713int32_t VintfObject::CheckDeprecation(std::string* error) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700714 return GetInstance()->checkDeprecation(error);
715}
716int32_t VintfObject::checkDeprecation(std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800717 using namespace std::placeholders;
Yifan Hong9f78c182018-07-12 14:45:52 -0700718 auto deviceManifest = getDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700719 ListInstances inManifest =
720 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
721 const std::vector<std::string>& /* hintInstances */) {
722 std::vector<std::pair<std::string, Version>> ret;
723 deviceManifest->forEachInstanceOfInterface(
724 package, version, interface, [&ret](const ManifestInstance& manifestInstance) {
725 ret.push_back(
726 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
727 return true;
728 });
729 return ret;
730 };
Yifan Hong9f78c182018-07-12 14:45:52 -0700731 return checkDeprecation(inManifest, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800732}
Yifan Hong3daec812017-02-27 18:49:11 -0800733
Yifan Hong9f78c182018-07-12 14:45:52 -0700734const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
735 return mFileSystem;
736}
737
Yifan Hong9f78c182018-07-12 14:45:52 -0700738const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
739 return mPropertyFetcher;
740}
741
Yifan Hongd038b2b2018-11-27 13:57:56 -0800742const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
Yifan Hong9f78c182018-07-12 14:45:52 -0700743 return mRuntimeInfoFactory;
Yifan Hong10d86222018-04-06 15:41:05 -0700744}
745
Yifan Hong3daec812017-02-27 18:49:11 -0800746} // namespace vintf
747} // namespace android