blob: 0e757ca04fc74e22ffd6679382a8e7689217c3d6 [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 Hong143cfe62017-04-13 20:18:01 -070019#include "CompatibilityMatrix.h"
Yifan Hongf73ba512018-01-17 15:52:30 -080020#include "parse_string.h"
Yifan Hong143cfe62017-04-13 20:18:01 -070021#include "parse_xml.h"
Yifan Hong8640cd12017-05-17 12:02:28 -070022#include "utils.h"
Yifan Hong143cfe62017-04-13 20:18:01 -070023
Yifan Hongd52bf3e2018-01-11 16:56:51 -080024#include <dirent.h>
25
Yifan Hong3daec812017-02-27 18:49:11 -080026#include <functional>
27#include <memory>
28#include <mutex>
29
Yifan Hong60217032018-01-08 16:19:42 -080030#include <android-base/logging.h>
31
32using std::placeholders::_1;
33using std::placeholders::_2;
34
Yifan Hong3daec812017-02-27 18:49:11 -080035namespace android {
36namespace vintf {
37
Yifan Hong270b5652018-01-18 18:46:01 -080038using namespace details;
39
Yifan Hong3daec812017-02-27 18:49:11 -080040template <typename T>
Yifan Hongfc73edf2017-08-29 11:39:07 -070041struct LockedSharedPtr {
42 std::shared_ptr<T> object;
Yifan Hong3daec812017-02-27 18:49:11 -080043 std::mutex mutex;
Yifan Hong7219ba12018-01-08 16:23:49 -080044 bool fetchedOnce = false;
Yifan Hong3daec812017-02-27 18:49:11 -080045};
46
Yifan Hong1fb004e2017-09-26 15:04:44 -070047struct LockedRuntimeInfoCache {
48 std::shared_ptr<RuntimeInfo> object;
49 std::mutex mutex;
50 RuntimeInfo::FetchFlags fetchedFlags = RuntimeInfo::FetchFlag::NONE;
51};
52
Yifan Hong3daec812017-02-27 18:49:11 -080053template <typename T, typename F>
Yifan Hongfc73edf2017-08-29 11:39:07 -070054static std::shared_ptr<const T> Get(
55 LockedSharedPtr<T> *ptr,
Yifan Hong143cfe62017-04-13 20:18:01 -070056 bool skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080057 const F &fetchAllInformation) {
58 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong7219ba12018-01-08 16:23:49 -080059 if (skipCache || !ptr->fetchedOnce) {
Yifan Hong3daec812017-02-27 18:49:11 -080060 ptr->object = std::make_unique<T>();
Yifan Hong60217032018-01-08 16:19:42 -080061 std::string error;
62 if (fetchAllInformation(ptr->object.get(), &error) != OK) {
63 LOG(WARNING) << error;
Yifan Hong3daec812017-02-27 18:49:11 -080064 ptr->object = nullptr; // frees the old object
65 }
Yifan Hong7219ba12018-01-08 16:23:49 -080066 ptr->fetchedOnce = true;
Yifan Hong3daec812017-02-27 18:49:11 -080067 }
Yifan Hongfc73edf2017-08-29 11:39:07 -070068 return ptr->object;
Yifan Hong3daec812017-02-27 18:49:11 -080069}
70
71// static
Yifan Hongfc73edf2017-08-29 11:39:07 -070072std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest(bool skipCache) {
Steven Moreland648a0012017-10-19 21:23:41 -070073 static LockedSharedPtr<HalManifest> gVendorManifest;
Yifan Hong5a93bf22018-01-17 18:22:32 -080074 return Get(&gVendorManifest, skipCache, &VintfObject::FetchDeviceHalManifest);
Yifan Hong3daec812017-02-27 18:49:11 -080075}
76
77// static
Yifan Hongfc73edf2017-08-29 11:39:07 -070078std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest(bool skipCache) {
79 static LockedSharedPtr<HalManifest> gFrameworkManifest;
Yifan Hongde6d00e2018-02-27 17:11:28 -080080 return Get(&gFrameworkManifest, skipCache, &VintfObject::FetchFrameworkHalManifest);
Yifan Hong3daec812017-02-27 18:49:11 -080081}
82
Yifan Hong2272bf82017-04-28 14:37:56 -070083
84// static
Yifan Hongfc73edf2017-08-29 11:39:07 -070085std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) {
86 static LockedSharedPtr<CompatibilityMatrix> gDeviceMatrix;
Yifan Hongb64ec9e2018-01-18 18:57:52 -080087 return Get(&gDeviceMatrix, skipCache, &VintfObject::FetchDeviceMatrix);
Yifan Hong2272bf82017-04-28 14:37:56 -070088}
89
90// static
Yifan Hongfc73edf2017-08-29 11:39:07 -070091std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) {
92 static LockedSharedPtr<CompatibilityMatrix> gFrameworkMatrix;
Yifan Hongd52bf3e2018-01-11 16:56:51 -080093 static LockedSharedPtr<CompatibilityMatrix> gCombinedFrameworkMatrix;
94 static std::mutex gFrameworkCompatibilityMatrixMutex;
95
96 // To avoid deadlock, get device manifest before any locks.
97 auto deviceManifest = GetDeviceHalManifest();
98
99 std::unique_lock<std::mutex> _lock(gFrameworkCompatibilityMatrixMutex);
100
101 auto combined =
102 Get(&gCombinedFrameworkMatrix, skipCache,
103 std::bind(&VintfObject::GetCombinedFrameworkMatrix, deviceManifest, _1, _2));
104 if (combined != nullptr) {
105 return combined;
106 }
107
Yifan Hong2272bf82017-04-28 14:37:56 -0700108 return Get(&gFrameworkMatrix, skipCache,
Yifan Hong270b5652018-01-18 18:46:01 -0800109 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, kSystemLegacyMatrix, _2));
Yifan Hong2272bf82017-04-28 14:37:56 -0700110}
111
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800112status_t VintfObject::GetCombinedFrameworkMatrix(
113 const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out,
114 std::string* error) {
115 auto matrixFragments = GetAllFrameworkMatrixLevels(error);
116 if (matrixFragments.empty()) {
117 return NAME_NOT_FOUND;
118 }
119
120 Level deviceLevel = Level::UNSPECIFIED;
121
122 if (deviceManifest != nullptr) {
123 deviceLevel = deviceManifest->level();
124 }
125
126 // TODO(b/70628538): Do not infer from Shipping API level.
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800127 if (deviceLevel == Level::UNSPECIFIED) {
Yifan Hongd14640a2018-02-27 18:35:39 -0800128 auto shippingApi = getPropertyFetcher().getUintProperty("ro.product.first_api_level", 0u);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800129 if (shippingApi != 0u) {
130 deviceLevel = details::convertFromApiLevel(shippingApi);
131 }
132 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800133
134 if (deviceLevel == Level::UNSPECIFIED) {
135 // Cannot infer FCM version. Combine all matrices by assuming
136 // Shipping FCM Version == min(all supported FCM Versions in the framework)
137 for (auto&& pair : matrixFragments) {
138 Level fragmentLevel = pair.object.level();
139 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
140 deviceLevel = fragmentLevel;
141 }
142 }
143 }
144
145 if (deviceLevel == Level::UNSPECIFIED) {
146 // None of the fragments specify any FCM version. Should never happen except
147 // for inconsistent builds.
148 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800149 *error = "No framework compatibility matrix files under " + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800150 " declare FCM version.";
151 }
152 return NAME_NOT_FOUND;
153 }
154
155 CompatibilityMatrix* combined =
156 CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
157 if (combined == nullptr) {
158 return BAD_VALUE;
159 }
160 *out = std::move(*combined);
161 return OK;
162}
163
Steven Morelandeedf2d42018-04-04 16:36:27 -0700164// Load and combine all of the manifests in a directory
165status_t VintfObject::AddDirectoryManifests(const std::string& directory, HalManifest* manifest,
166 std::string* error) {
167 std::vector<std::string> fileNames;
Yifan Hong10d86222018-04-06 15:41:05 -0700168 status_t err = details::getFileSystem().listFiles(directory, &fileNames, error);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700169 // if the directory isn't there, that's okay
170 if (err == NAME_NOT_FOUND) return OK;
171 if (err != OK) return err;
172
173 for (const std::string& file : fileNames) {
174 // Only adds HALs because all other things are added by libvintf
175 // itself for now.
176 HalManifest fragmentManifest;
177 err = FetchOneHalManifest(directory + file, &fragmentManifest, error);
178 if (err != OK) return err;
179
180 manifest->addAllHals(&fragmentManifest);
181 }
182
183 return OK;
184}
185
Yifan Hong5a93bf22018-01-17 18:22:32 -0800186// Priority for loading vendor manifest:
Steven Morelandeedf2d42018-04-04 16:36:27 -0700187// 1. /vendor/etc/vintf/manifest.xml + device fragments + ODM manifest (optional) + odm fragments
188// 2. /vendor/etc/vintf/manifest.xml + device fragments
189// 3. ODM manifest (optional) + odm fragments
190// 4. /vendor/manifest.xml (legacy, no fragments)
Yifan Hong5a93bf22018-01-17 18:22:32 -0800191// where:
Yifan Hong5a93bf22018-01-17 18:22:32 -0800192// A + B means adding <hal> tags from B to A (so that <hal>s from B can override A)
193status_t VintfObject::FetchDeviceHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800194 status_t vendorStatus = FetchOneHalManifest(kVendorManifest, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800195 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
196 return vendorStatus;
197 }
198
Steven Morelandeedf2d42018-04-04 16:36:27 -0700199 if (vendorStatus == OK) {
200 status_t fragmentStatus = AddDirectoryManifests(kVendorManifestFragmentDir, out, error);
201 if (fragmentStatus != OK) {
202 return fragmentStatus;
203 }
204 }
205
Yifan Hong5a93bf22018-01-17 18:22:32 -0800206 HalManifest odmManifest;
Yifan Hong12a11ac2018-01-19 13:58:32 -0800207 status_t odmStatus = FetchOdmHalManifest(&odmManifest, error);
208 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
209 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800210 }
211
Yifan Hong5a93bf22018-01-17 18:22:32 -0800212 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800213 if (odmStatus == OK) {
214 out->addAllHals(&odmManifest);
215 }
Steven Morelandeedf2d42018-04-04 16:36:27 -0700216 return AddDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800217 }
218
Yifan Hong12a11ac2018-01-19 13:58:32 -0800219 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800220 if (odmStatus == OK) {
221 *out = std::move(odmManifest);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700222 return AddDirectoryManifests(kOdmManifestFragmentDir, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800223 }
224
225 // Use legacy /vendor/manifest.xml
Yifan Hong270b5652018-01-18 18:46:01 -0800226 return out->fetchAllInformation(kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800227}
228
Yifan Hong12a11ac2018-01-19 13:58:32 -0800229// "out" is written to iff return status is OK.
230// Priority:
231// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
232// 2. /odm/etc/vintf/manifest.xml
233// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
234// 4. /odm/etc/manifest.xml
235// where:
236// {sku} is the value of ro.boot.product.hardware.sku
237status_t VintfObject::FetchOdmHalManifest(HalManifest* out, std::string* error) {
238 status_t status;
239
Yifan Hong12a11ac2018-01-19 13:58:32 -0800240 std::string productModel;
Yifan Hongd14640a2018-02-27 18:35:39 -0800241 productModel = getPropertyFetcher().getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800242
243 if (!productModel.empty()) {
244 status =
245 FetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
246 if (status == OK || status != NAME_NOT_FOUND) {
247 return status;
248 }
249 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800250
251 status = FetchOneHalManifest(kOdmManifest, out, error);
252 if (status == OK || status != NAME_NOT_FOUND) {
253 return status;
254 }
255
Yifan Hong12a11ac2018-01-19 13:58:32 -0800256 if (!productModel.empty()) {
257 status = FetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
258 error);
259 if (status == OK || status != NAME_NOT_FOUND) {
260 return status;
261 }
262 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800263
264 status = FetchOneHalManifest(kOdmLegacyManifest, out, error);
265 if (status == OK || status != NAME_NOT_FOUND) {
266 return status;
267 }
268
269 return NAME_NOT_FOUND;
270}
271
272// Fetch one manifest.xml file. "out" is written to iff return status is OK.
273// Returns NAME_NOT_FOUND if file is missing.
274status_t VintfObject::FetchOneHalManifest(const std::string& path, HalManifest* out,
275 std::string* error) {
276 HalManifest ret;
277 status_t status = ret.fetchAllInformation(path, error);
278 if (status == OK) {
279 *out = std::move(ret);
280 }
281 return status;
282}
283
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800284status_t VintfObject::FetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
285 CompatibilityMatrix etcMatrix;
286 if (etcMatrix.fetchAllInformation(kVendorMatrix, error) == OK) {
287 *out = std::move(etcMatrix);
288 return OK;
289 }
290 return out->fetchAllInformation(kVendorLegacyMatrix, error);
291}
292
Yifan Hongde6d00e2018-02-27 17:11:28 -0800293status_t VintfObject::FetchFrameworkHalManifest(HalManifest* out, std::string* error) {
294 HalManifest etcManifest;
295 if (etcManifest.fetchAllInformation(kSystemManifest, error) == OK) {
296 *out = std::move(etcManifest);
Steven Morelandeedf2d42018-04-04 16:36:27 -0700297 return AddDirectoryManifests(kSystemManifestFragmentDir, out, error);
Yifan Hongde6d00e2018-02-27 17:11:28 -0800298 }
299 return out->fetchAllInformation(kSystemLegacyManifest, error);
300}
301
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800302std::vector<Named<CompatibilityMatrix>> VintfObject::GetAllFrameworkMatrixLevels(
303 std::string* error) {
304 std::vector<std::string> fileNames;
305 std::vector<Named<CompatibilityMatrix>> results;
306
Yifan Hong10d86222018-04-06 15:41:05 -0700307 if (details::getFileSystem().listFiles(kSystemVintfDir, &fileNames, error) != OK) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800308 return {};
309 }
310 for (const std::string& fileName : fileNames) {
Yifan Hong270b5652018-01-18 18:46:01 -0800311 std::string path = kSystemVintfDir + fileName;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800312
313 std::string content;
314 std::string fetchError;
Yifan Hong10d86222018-04-06 15:41:05 -0700315 status_t status = details::getFileSystem().fetch(path, &content, &fetchError);
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800316 if (status != OK) {
317 if (error) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800318 *error += "Framework Matrix: Ignore file " + path + ": " + fetchError + "\n";
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800319 }
320 continue;
321 }
322
323 auto it = results.emplace(results.end());
Yifan Hong94757062018-02-09 16:36:31 -0800324 if (!gCompatibilityMatrixConverter(&it->object, content, error)) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800325 if (error) {
Yifan Hong69c1b112018-02-27 17:06:00 -0800326 *error += "Framework Matrix: Ignore file " + path + ": " + *error + "\n";
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800327 }
328 results.erase(it);
329 continue;
330 }
331 }
332
333 if (results.empty()) {
334 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800335 *error = "No framework matrices under " + kSystemVintfDir +
336 " can be fetched or parsed.\n" + *error;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800337 }
338 } else {
339 if (error && !error->empty()) {
340 LOG(WARNING) << *error;
341 *error = "";
342 }
343 }
344
345 return results;
346}
347
Yifan Hong3daec812017-02-27 18:49:11 -0800348// static
Yifan Hong1fb004e2017-09-26 15:04:44 -0700349std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
350 RuntimeInfo::FetchFlags flags) {
351 static LockedRuntimeInfoCache gDeviceRuntimeInfo;
352 std::unique_lock<std::mutex> _lock(gDeviceRuntimeInfo.mutex);
353
354 if (!skipCache) {
355 flags &= (~gDeviceRuntimeInfo.fetchedFlags);
356 }
357
358 if (gDeviceRuntimeInfo.object == nullptr) {
Yifan Hong29bb2d42017-09-27 13:28:00 -0700359 gDeviceRuntimeInfo.object = details::gRuntimeInfoFactory->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700360 }
361
362 status_t status = gDeviceRuntimeInfo.object->fetchAllInformation(flags);
363 if (status != OK) {
364 gDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
365 return nullptr;
366 }
367
368 gDeviceRuntimeInfo.fetchedFlags |= flags;
369 return gDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800370}
371
Yifan Hong143cfe62017-04-13 20:18:01 -0700372namespace details {
373
Yifan Hong143cfe62017-04-13 20:18:01 -0700374enum class ParseStatus {
375 OK,
376 PARSE_ERROR,
377 DUPLICATED_FWK_ENTRY,
378 DUPLICATED_DEV_ENTRY,
379};
380
Yifan Hong9532bd22017-04-14 15:30:52 -0700381static std::string toString(ParseStatus status) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700382 switch(status) {
383 case ParseStatus::OK: return "OK";
384 case ParseStatus::PARSE_ERROR: return "parse error";
385 case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework";
386 case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device";
387 }
388 return "";
389}
390
391template<typename T>
Yifan Hong9532bd22017-04-14 15:30:52 -0700392static ParseStatus tryParse(const std::string &xml, const XmlConverter<T> &parse,
Yifan Hongfc73edf2017-08-29 11:39:07 -0700393 std::shared_ptr<T> *fwk, std::shared_ptr<T> *dev) {
394 std::shared_ptr<T> ret = std::make_shared<T>();
Yifan Hong94757062018-02-09 16:36:31 -0800395 if (!parse(ret.get(), xml, nullptr /* error */)) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700396 return ParseStatus::PARSE_ERROR;
397 }
398 if (ret->type() == SchemaType::FRAMEWORK) {
399 if (fwk->get() != nullptr) {
400 return ParseStatus::DUPLICATED_FWK_ENTRY;
401 }
402 *fwk = std::move(ret);
403 } else if (ret->type() == SchemaType::DEVICE) {
404 if (dev->get() != nullptr) {
405 return ParseStatus::DUPLICATED_DEV_ENTRY;
406 }
407 *dev = std::move(ret);
408 }
409 return ParseStatus::OK;
410}
411
412template<typename T, typename GetFunction>
Yifan Hongfc73edf2017-08-29 11:39:07 -0700413static status_t getMissing(const std::shared_ptr<T>& pkg, bool mount,
Yifan Hong143cfe62017-04-13 20:18:01 -0700414 std::function<status_t(void)> mountFunction,
Yifan Hongfc73edf2017-08-29 11:39:07 -0700415 std::shared_ptr<const T>* updated,
Yifan Hong143cfe62017-04-13 20:18:01 -0700416 GetFunction getFunction) {
417 if (pkg != nullptr) {
418 *updated = pkg;
419 } else {
420 if (mount) {
421 (void)mountFunction(); // ignore mount errors
422 }
423 *updated = getFunction();
424 }
425 return OK;
426}
427
428#define ADD_MESSAGE(__error__) \
429 if (error != nullptr) { \
430 *error += (__error__); \
431 } \
432
433struct PackageInfo {
434 struct Pair {
Yifan Hongfc73edf2017-08-29 11:39:07 -0700435 std::shared_ptr<HalManifest> manifest;
436 std::shared_ptr<CompatibilityMatrix> matrix;
Yifan Hong143cfe62017-04-13 20:18:01 -0700437 };
438 Pair dev;
439 Pair fwk;
440};
441
442struct UpdatedInfo {
443 struct Pair {
Yifan Hongfc73edf2017-08-29 11:39:07 -0700444 std::shared_ptr<const HalManifest> manifest;
445 std::shared_ptr<const CompatibilityMatrix> matrix;
Yifan Hong143cfe62017-04-13 20:18:01 -0700446 };
447 Pair dev;
448 Pair fwk;
Yifan Hongfc73edf2017-08-29 11:39:07 -0700449 std::shared_ptr<const RuntimeInfo> runtimeInfo;
Yifan Hong143cfe62017-04-13 20:18:01 -0700450};
451
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700452// Checks given compatibility info against info on the device. If no
453// compatability info is given then the device info will be checked against
454// itself.
Yifan Hongdb6423e2017-09-11 14:38:46 -0700455int32_t checkCompatibility(const std::vector<std::string>& xmls, bool mount,
456 const PartitionMounter& mounter, std::string* error,
457 DisabledChecks disabledChecks) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700458 status_t status;
459 ParseStatus parseStatus;
460 PackageInfo pkg; // All information from package.
461 UpdatedInfo updated; // All files and runtime info after the update.
462
Yifan Hong143cfe62017-04-13 20:18:01 -0700463 // parse all information from package
464 for (const auto &xml : xmls) {
465 parseStatus = tryParse(xml, gHalManifestConverter, &pkg.fwk.manifest, &pkg.dev.manifest);
466 if (parseStatus == ParseStatus::OK) {
467 continue; // work on next one
468 }
469 if (parseStatus != ParseStatus::PARSE_ERROR) {
470 ADD_MESSAGE(toString(parseStatus) + " manifest");
471 return ALREADY_EXISTS;
472 }
473 parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &pkg.fwk.matrix, &pkg.dev.matrix);
474 if (parseStatus == ParseStatus::OK) {
475 continue; // work on next one
476 }
477 if (parseStatus != ParseStatus::PARSE_ERROR) {
478 ADD_MESSAGE(toString(parseStatus) + " matrix");
479 return ALREADY_EXISTS;
480 }
481 ADD_MESSAGE(toString(parseStatus)); // parse error
482 return BAD_VALUE;
483 }
484
485 // get missing info from device
Yifan Hong8640cd12017-05-17 12:02:28 -0700486 // use functions instead of std::bind because std::bind doesn't work well with mock objects
487 auto mountSystem = [&mounter] { return mounter.mountSystem(); };
488 auto mountVendor = [&mounter] { return mounter.mountVendor(); };
489 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700490 pkg.fwk.manifest, mount, mountSystem, &updated.fwk.manifest,
Yifan Hong8640cd12017-05-17 12:02:28 -0700491 std::bind(VintfObject::GetFrameworkHalManifest, true /* skipCache */))) != OK) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700492 return status;
493 }
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700494 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700495 pkg.dev.manifest, mount, mountVendor, &updated.dev.manifest,
Yifan Hong8640cd12017-05-17 12:02:28 -0700496 std::bind(VintfObject::GetDeviceHalManifest, true /* skipCache */))) != OK) {
497 return status;
498 }
499 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700500 pkg.fwk.matrix, mount, mountSystem, &updated.fwk.matrix,
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700501 std::bind(VintfObject::GetFrameworkCompatibilityMatrix, true /* skipCache */))) !=
502 OK) {
503 return status;
504 }
505 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700506 pkg.dev.matrix, mount, mountVendor, &updated.dev.matrix,
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700507 std::bind(VintfObject::GetDeviceCompatibilityMatrix, true /* skipCache */))) != OK) {
508 return status;
509 }
Yifan Hong8640cd12017-05-17 12:02:28 -0700510
511 if (mount) {
512 (void)mounter.umountSystem(); // ignore errors
513 (void)mounter.umountVendor(); // ignore errors
514 }
515
Yifan Hong69c1b112018-02-27 17:06:00 -0800516 if ((disabledChecks & DISABLE_RUNTIME_INFO) == 0) {
517 updated.runtimeInfo = VintfObject::GetRuntimeInfo(true /* skipCache */);
518 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700519
520 // null checks for files and runtime info after the update
Yifan Hong143cfe62017-04-13 20:18:01 -0700521 if (updated.fwk.manifest == nullptr) {
522 ADD_MESSAGE("No framework manifest file from device or from update package");
523 return NO_INIT;
524 }
525 if (updated.dev.manifest == nullptr) {
526 ADD_MESSAGE("No device manifest file from device or from update package");
527 return NO_INIT;
528 }
529 if (updated.fwk.matrix == nullptr) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800530 ADD_MESSAGE("No framework matrix file from device or from update package");
531 return NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700532 }
533 if (updated.dev.matrix == nullptr) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800534 ADD_MESSAGE("No device matrix file from device or from update package");
535 return NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700536 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800537
538 if ((disabledChecks & DISABLE_RUNTIME_INFO) == 0) {
539 if (updated.runtimeInfo == nullptr) {
540 ADD_MESSAGE("No runtime info from device");
541 return NO_INIT;
542 }
Yifan Hong143cfe62017-04-13 20:18:01 -0700543 }
544
545 // compatiblity check.
Yifan Hongca386fe2018-02-07 11:29:03 -0800546 if (!updated.dev.manifest->checkCompatibility(*updated.fwk.matrix, error)) {
547 if (error) {
548 error->insert(0,
549 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700550 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800551 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700552 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800553 if (!updated.fwk.manifest->checkCompatibility(*updated.dev.matrix, error)) {
554 if (error) {
555 error->insert(0,
556 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700557 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800558 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700559 }
Yifan Hong69c1b112018-02-27 17:06:00 -0800560
561 if ((disabledChecks & DISABLE_RUNTIME_INFO) == 0) {
562 if (!updated.runtimeInfo->checkCompatibility(*updated.fwk.matrix, error, disabledChecks)) {
563 if (error) {
564 error->insert(0,
565 "Runtime info and framework compatibility matrix are incompatible: ");
566 }
567 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700568 }
569 }
570
571 return COMPATIBLE;
572}
573
Yifan Hong270b5652018-01-18 18:46:01 -0800574const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800575const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800576const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800577
578const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800579const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800580const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800581const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800582
Steven Morelandeedf2d42018-04-04 16:36:27 -0700583const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/";
584const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/";
585const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/";
586
Yifan Hong270b5652018-01-18 18:46:01 -0800587const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
588const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800589const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800590const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
591const std::string kOdmLegacyVintfDir = "/odm/etc/";
592const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
593
Yifan Hong69c1b112018-02-27 17:06:00 -0800594std::vector<std::string> dumpFileList() {
595 return {
596 kSystemVintfDir, kVendorVintfDir, kOdmVintfDir, kOdmLegacyVintfDir,
597
598 kVendorLegacyManifest, kVendorLegacyMatrix, kSystemLegacyManifest, kSystemLegacyMatrix,
599 };
600}
601
Yifan Hong143cfe62017-04-13 20:18:01 -0700602} // namespace details
603
Yifan Hongfbbf0472017-04-07 18:14:18 -0700604// static
Yifan Hongdb6423e2017-09-11 14:38:46 -0700605int32_t VintfObject::CheckCompatibility(const std::vector<std::string>& xmls, std::string* error,
606 DisabledChecks disabledChecks) {
607 return details::checkCompatibility(xmls, false /* mount */, *details::gPartitionMounter, error,
608 disabledChecks);
Yifan Hongfbbf0472017-04-07 18:14:18 -0700609}
610
Yifan Hongf73ba512018-01-17 15:52:30 -0800611bool VintfObject::isHalDeprecated(const MatrixHal& oldMatrixHal,
612 const CompatibilityMatrix& targetMatrix,
Yifan Honga8a8fa92018-03-20 14:42:43 -0700613 const ListInstances& listInstances, std::string* error) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700614 bool isDeprecated = false;
615 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700616 if (isInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, error)) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700617 isDeprecated = true;
Yifan Hongf73ba512018-01-17 15:52:30 -0800618 }
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700619 return !isDeprecated; // continue if no deprecated instance is found.
620 });
621 return isDeprecated;
Yifan Hongf73ba512018-01-17 15:52:30 -0800622}
623
Yifan Honga8a8fa92018-03-20 14:42:43 -0700624// Let oldMatrixInstance = package@x.y-w::interface with instancePattern.
625// If any "servedInstance" in listInstances(package@x.y::interface) matches instancePattern, return
626// true iff:
627// 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
628// 2. package@x.z::interface/servedInstance is in targetMatrix but
629// servedInstance is not in listInstances(package@x.z::interface)
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700630bool VintfObject::isInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
Yifan Hongf73ba512018-01-17 15:52:30 -0800631 const CompatibilityMatrix& targetMatrix,
Yifan Honga8a8fa92018-03-20 14:42:43 -0700632 const ListInstances& listInstances, std::string* error) {
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700633 const std::string& package = oldMatrixInstance.package();
634 const Version& version = oldMatrixInstance.versionRange().minVer();
635 const std::string& interface = oldMatrixInstance.interface();
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700636
Yifan Honga8a8fa92018-03-20 14:42:43 -0700637 std::vector<std::string> instanceHint;
638 if (!oldMatrixInstance.isRegex()) {
639 instanceHint.push_back(oldMatrixInstance.exactInstance());
640 }
641
642 auto list = listInstances(package, version, interface, instanceHint);
643 for (const auto& pair : list) {
644 const std::string& servedInstance = pair.first;
645 Version servedVersion = pair.second;
646 if (!oldMatrixInstance.matchInstance(servedInstance)) {
647 continue;
648 }
649
Yifan Hongf73ba512018-01-17 15:52:30 -0800650 // Find any package@x.? in target matrix, and check if instance is in target matrix.
Yifan Hong217f47e2018-03-15 12:34:05 -0700651 bool foundInstance = false;
652 Version targetMatrixMinVer;
653 targetMatrix.forEachInstanceOfPackage(package, [&](const auto& targetMatrixInstance) {
654 if (targetMatrixInstance.versionRange().majorVer == version.majorVer &&
655 targetMatrixInstance.interface() == interface &&
Yifan Honga8a8fa92018-03-20 14:42:43 -0700656 targetMatrixInstance.matchInstance(servedInstance)) {
Yifan Hong217f47e2018-03-15 12:34:05 -0700657 targetMatrixMinVer = targetMatrixInstance.versionRange().minVer();
658 foundInstance = true;
659 }
660 return !foundInstance; // continue if not found
661 });
662 if (!foundInstance) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800663 if (error) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700664 *error = toFQNameString(package, servedVersion, interface, servedInstance) +
Steven Morelanda1b984c2018-03-09 13:09:15 -0800665 " is deprecated in compatibility matrix at FCM Version " +
Yifan Hongf73ba512018-01-17 15:52:30 -0800666 to_string(targetMatrix.level()) + "; it should not be served.";
667 }
668 return true;
669 }
670
Yifan Hongf73ba512018-01-17 15:52:30 -0800671 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
Yifan Honga8a8fa92018-03-20 14:42:43 -0700672 bool targetVersionServed = false;
673 for (const auto& newPair :
674 listInstances(package, targetMatrixMinVer, interface, instanceHint)) {
675 if (newPair.first == servedInstance) {
676 targetVersionServed = true;
677 break;
678 }
679 }
Yifan Hongf73ba512018-01-17 15:52:30 -0800680
681 if (!targetVersionServed) {
682 if (error) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700683 *error += toFQNameString(package, servedVersion, interface, servedInstance) +
684 " is deprecated; requires at least " + to_string(targetMatrixMinVer) +
685 "\n";
Yifan Hongf73ba512018-01-17 15:52:30 -0800686 }
687 return true;
688 }
689 }
Yifan Honga8a8fa92018-03-20 14:42:43 -0700690
Yifan Hongf73ba512018-01-17 15:52:30 -0800691 return false;
692}
693
Yifan Honga8a8fa92018-03-20 14:42:43 -0700694int32_t VintfObject::CheckDeprecation(const ListInstances& listInstances, std::string* error) {
Yifan Hongf73ba512018-01-17 15:52:30 -0800695 auto matrixFragments = GetAllFrameworkMatrixLevels(error);
696 if (matrixFragments.empty()) {
697 if (error && error->empty())
698 *error = "Cannot get framework matrix for each FCM version for unknown error.";
699 return NAME_NOT_FOUND;
700 }
701 auto deviceManifest = GetDeviceHalManifest();
702 if (deviceManifest == nullptr) {
703 if (error) *error = "No device manifest.";
704 return NAME_NOT_FOUND;
705 }
706 Level deviceLevel = deviceManifest->level();
707 if (deviceLevel == Level::UNSPECIFIED) {
708 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
709 return BAD_VALUE;
710 }
711
712 const CompatibilityMatrix* targetMatrix = nullptr;
713 for (const auto& namedMatrix : matrixFragments) {
714 if (namedMatrix.object.level() == deviceLevel) {
715 targetMatrix = &namedMatrix.object;
716 }
717 }
718 if (targetMatrix == nullptr) {
719 if (error)
720 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
721 return NAME_NOT_FOUND;
722 }
723
724 bool hasDeprecatedHals = false;
725 for (const auto& namedMatrix : matrixFragments) {
726 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
727 if (namedMatrix.object.level() >= deviceLevel) continue;
728
729 const auto& oldMatrix = namedMatrix.object;
730 for (const MatrixHal& hal : oldMatrix.getHals()) {
Yifan Honga8a8fa92018-03-20 14:42:43 -0700731 hasDeprecatedHals |= isHalDeprecated(hal, *targetMatrix, listInstances, error);
Yifan Hongf73ba512018-01-17 15:52:30 -0800732 }
733 }
734
735 return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS;
736}
737
738int32_t VintfObject::CheckDeprecation(std::string* error) {
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800739 using namespace std::placeholders;
Yifan Hongf73ba512018-01-17 15:52:30 -0800740 auto deviceManifest = GetDeviceHalManifest();
Yifan Honga8a8fa92018-03-20 14:42:43 -0700741 ListInstances inManifest =
742 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
743 const std::vector<std::string>& /* hintInstances */) {
744 std::vector<std::pair<std::string, Version>> ret;
745 deviceManifest->forEachInstanceOfInterface(
746 package, version, interface, [&ret](const ManifestInstance& manifestInstance) {
747 ret.push_back(
748 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
749 return true;
750 });
751 return ret;
752 };
Yifan Hongf73ba512018-01-17 15:52:30 -0800753 return CheckDeprecation(inManifest, error);
754}
Yifan Hong3daec812017-02-27 18:49:11 -0800755
Yifan Hong10d86222018-04-06 15:41:05 -0700756bool VintfObject::InitFileSystem(std::unique_ptr<FileSystem>&& fileSystem) {
757 return details::initFileSystem(std::move(fileSystem));
758}
759
Yifan Hong3daec812017-02-27 18:49:11 -0800760} // namespace vintf
761} // namespace android