blob: 9f3a176df4f07d74841b2716831dcc2bfa4d5b34 [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
Bowgo Tsai39adc142017-11-03 12:46:26 +080030#ifdef LIBVINTF_TARGET
31#include <android-base/properties.h>
32#endif
33
Yifan Hong60217032018-01-08 16:19:42 -080034#include <android-base/logging.h>
35
36using std::placeholders::_1;
37using std::placeholders::_2;
38
Yifan Hong3daec812017-02-27 18:49:11 -080039namespace android {
40namespace vintf {
41
Yifan Hong270b5652018-01-18 18:46:01 -080042using namespace details;
43
Yifan Hong3daec812017-02-27 18:49:11 -080044template <typename T>
Yifan Hongfc73edf2017-08-29 11:39:07 -070045struct LockedSharedPtr {
46 std::shared_ptr<T> object;
Yifan Hong3daec812017-02-27 18:49:11 -080047 std::mutex mutex;
Yifan Hong7219ba12018-01-08 16:23:49 -080048 bool fetchedOnce = false;
Yifan Hong3daec812017-02-27 18:49:11 -080049};
50
Yifan Hong1fb004e2017-09-26 15:04:44 -070051struct LockedRuntimeInfoCache {
52 std::shared_ptr<RuntimeInfo> object;
53 std::mutex mutex;
54 RuntimeInfo::FetchFlags fetchedFlags = RuntimeInfo::FetchFlag::NONE;
55};
56
Yifan Hong3daec812017-02-27 18:49:11 -080057template <typename T, typename F>
Yifan Hongfc73edf2017-08-29 11:39:07 -070058static std::shared_ptr<const T> Get(
59 LockedSharedPtr<T> *ptr,
Yifan Hong143cfe62017-04-13 20:18:01 -070060 bool skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080061 const F &fetchAllInformation) {
62 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong7219ba12018-01-08 16:23:49 -080063 if (skipCache || !ptr->fetchedOnce) {
Yifan Hong3daec812017-02-27 18:49:11 -080064 ptr->object = std::make_unique<T>();
Yifan Hong60217032018-01-08 16:19:42 -080065 std::string error;
66 if (fetchAllInformation(ptr->object.get(), &error) != OK) {
67 LOG(WARNING) << error;
Yifan Hong3daec812017-02-27 18:49:11 -080068 ptr->object = nullptr; // frees the old object
69 }
Yifan Hong7219ba12018-01-08 16:23:49 -080070 ptr->fetchedOnce = true;
Yifan Hong3daec812017-02-27 18:49:11 -080071 }
Yifan Hongfc73edf2017-08-29 11:39:07 -070072 return ptr->object;
Yifan Hong3daec812017-02-27 18:49:11 -080073}
74
75// static
Yifan Hongfc73edf2017-08-29 11:39:07 -070076std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest(bool skipCache) {
Steven Moreland648a0012017-10-19 21:23:41 -070077 static LockedSharedPtr<HalManifest> gVendorManifest;
Yifan Hong5a93bf22018-01-17 18:22:32 -080078 return Get(&gVendorManifest, skipCache, &VintfObject::FetchDeviceHalManifest);
Yifan Hong3daec812017-02-27 18:49:11 -080079}
80
81// static
Yifan Hongfc73edf2017-08-29 11:39:07 -070082std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest(bool skipCache) {
83 static LockedSharedPtr<HalManifest> gFrameworkManifest;
Yifan Hongde6d00e2018-02-27 17:11:28 -080084 return Get(&gFrameworkManifest, skipCache, &VintfObject::FetchFrameworkHalManifest);
Yifan Hong3daec812017-02-27 18:49:11 -080085}
86
Yifan Hong2272bf82017-04-28 14:37:56 -070087
88// static
Yifan Hongfc73edf2017-08-29 11:39:07 -070089std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) {
90 static LockedSharedPtr<CompatibilityMatrix> gDeviceMatrix;
Yifan Hongb64ec9e2018-01-18 18:57:52 -080091 return Get(&gDeviceMatrix, skipCache, &VintfObject::FetchDeviceMatrix);
Yifan Hong2272bf82017-04-28 14:37:56 -070092}
93
94// static
Yifan Hongfc73edf2017-08-29 11:39:07 -070095std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) {
96 static LockedSharedPtr<CompatibilityMatrix> gFrameworkMatrix;
Yifan Hongd52bf3e2018-01-11 16:56:51 -080097 static LockedSharedPtr<CompatibilityMatrix> gCombinedFrameworkMatrix;
98 static std::mutex gFrameworkCompatibilityMatrixMutex;
99
100 // To avoid deadlock, get device manifest before any locks.
101 auto deviceManifest = GetDeviceHalManifest();
102
103 std::unique_lock<std::mutex> _lock(gFrameworkCompatibilityMatrixMutex);
104
105 auto combined =
106 Get(&gCombinedFrameworkMatrix, skipCache,
107 std::bind(&VintfObject::GetCombinedFrameworkMatrix, deviceManifest, _1, _2));
108 if (combined != nullptr) {
109 return combined;
110 }
111
Yifan Hong2272bf82017-04-28 14:37:56 -0700112 return Get(&gFrameworkMatrix, skipCache,
Yifan Hong270b5652018-01-18 18:46:01 -0800113 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, kSystemLegacyMatrix, _2));
Yifan Hong2272bf82017-04-28 14:37:56 -0700114}
115
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800116status_t VintfObject::GetCombinedFrameworkMatrix(
117 const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out,
118 std::string* error) {
119 auto matrixFragments = GetAllFrameworkMatrixLevels(error);
120 if (matrixFragments.empty()) {
121 return NAME_NOT_FOUND;
122 }
123
124 Level deviceLevel = Level::UNSPECIFIED;
125
126 if (deviceManifest != nullptr) {
127 deviceLevel = deviceManifest->level();
128 }
129
130 // TODO(b/70628538): Do not infer from Shipping API level.
131#ifdef LIBVINTF_TARGET
132 if (deviceLevel == Level::UNSPECIFIED) {
133 auto shippingApi =
134 android::base::GetUintProperty<uint64_t>("ro.product.first_api_level", 0u);
135 if (shippingApi != 0u) {
136 deviceLevel = details::convertFromApiLevel(shippingApi);
137 }
138 }
139#endif
140
141 if (deviceLevel == Level::UNSPECIFIED) {
142 // Cannot infer FCM version. Combine all matrices by assuming
143 // Shipping FCM Version == min(all supported FCM Versions in the framework)
144 for (auto&& pair : matrixFragments) {
145 Level fragmentLevel = pair.object.level();
146 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
147 deviceLevel = fragmentLevel;
148 }
149 }
150 }
151
152 if (deviceLevel == Level::UNSPECIFIED) {
153 // None of the fragments specify any FCM version. Should never happen except
154 // for inconsistent builds.
155 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800156 *error = "No framework compatibility matrix files under " + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800157 " declare FCM version.";
158 }
159 return NAME_NOT_FOUND;
160 }
161
162 CompatibilityMatrix* combined =
163 CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
164 if (combined == nullptr) {
165 return BAD_VALUE;
166 }
167 *out = std::move(*combined);
168 return OK;
169}
170
Yifan Hong5a93bf22018-01-17 18:22:32 -0800171// Priority for loading vendor manifest:
Yifan Hong12a11ac2018-01-19 13:58:32 -0800172// 1. /vendor/etc/vintf/manifest.xml + ODM manifest
173// 2. /vendor/etc/vintf/manifest.xml
174// 3. ODM manifest
175// 4. /vendor/manifest.xml
Yifan Hong5a93bf22018-01-17 18:22:32 -0800176// where:
Yifan Hong5a93bf22018-01-17 18:22:32 -0800177// A + B means adding <hal> tags from B to A (so that <hal>s from B can override A)
178status_t VintfObject::FetchDeviceHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800179 status_t vendorStatus = FetchOneHalManifest(kVendorManifest, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800180 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
181 return vendorStatus;
182 }
183
184 HalManifest odmManifest;
Yifan Hong12a11ac2018-01-19 13:58:32 -0800185 status_t odmStatus = FetchOdmHalManifest(&odmManifest, error);
186 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
187 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800188 }
189
Yifan Hong5a93bf22018-01-17 18:22:32 -0800190 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800191 if (odmStatus == OK) {
192 out->addAllHals(&odmManifest);
193 }
Yifan Hong5a93bf22018-01-17 18:22:32 -0800194 return OK;
195 }
196
Yifan Hong12a11ac2018-01-19 13:58:32 -0800197 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800198 if (odmStatus == OK) {
199 *out = std::move(odmManifest);
200 return OK;
201 }
202
203 // Use legacy /vendor/manifest.xml
Yifan Hong270b5652018-01-18 18:46:01 -0800204 return out->fetchAllInformation(kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800205}
206
Yifan Hong12a11ac2018-01-19 13:58:32 -0800207// "out" is written to iff return status is OK.
208// Priority:
209// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
210// 2. /odm/etc/vintf/manifest.xml
211// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
212// 4. /odm/etc/manifest.xml
213// where:
214// {sku} is the value of ro.boot.product.hardware.sku
215status_t VintfObject::FetchOdmHalManifest(HalManifest* out, std::string* error) {
216 status_t status;
217
218#ifdef LIBVINTF_TARGET
219 std::string productModel;
220 productModel = android::base::GetProperty("ro.boot.product.hardware.sku", "");
221
222 if (!productModel.empty()) {
223 status =
224 FetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
225 if (status == OK || status != NAME_NOT_FOUND) {
226 return status;
227 }
228 }
229#endif
230
231 status = FetchOneHalManifest(kOdmManifest, out, error);
232 if (status == OK || status != NAME_NOT_FOUND) {
233 return status;
234 }
235
236#ifdef LIBVINTF_TARGET
237 if (!productModel.empty()) {
238 status = FetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
239 error);
240 if (status == OK || status != NAME_NOT_FOUND) {
241 return status;
242 }
243 }
244#endif
245
246 status = FetchOneHalManifest(kOdmLegacyManifest, out, error);
247 if (status == OK || status != NAME_NOT_FOUND) {
248 return status;
249 }
250
251 return NAME_NOT_FOUND;
252}
253
254// Fetch one manifest.xml file. "out" is written to iff return status is OK.
255// Returns NAME_NOT_FOUND if file is missing.
256status_t VintfObject::FetchOneHalManifest(const std::string& path, HalManifest* out,
257 std::string* error) {
258 HalManifest ret;
259 status_t status = ret.fetchAllInformation(path, error);
260 if (status == OK) {
261 *out = std::move(ret);
262 }
263 return status;
264}
265
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800266status_t VintfObject::FetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
267 CompatibilityMatrix etcMatrix;
268 if (etcMatrix.fetchAllInformation(kVendorMatrix, error) == OK) {
269 *out = std::move(etcMatrix);
270 return OK;
271 }
272 return out->fetchAllInformation(kVendorLegacyMatrix, error);
273}
274
Yifan Hongde6d00e2018-02-27 17:11:28 -0800275status_t VintfObject::FetchFrameworkHalManifest(HalManifest* out, std::string* error) {
276 HalManifest etcManifest;
277 if (etcManifest.fetchAllInformation(kSystemManifest, error) == OK) {
278 *out = std::move(etcManifest);
279 return OK;
280 }
281 return out->fetchAllInformation(kSystemLegacyManifest, error);
282}
283
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800284std::vector<Named<CompatibilityMatrix>> VintfObject::GetAllFrameworkMatrixLevels(
285 std::string* error) {
286 std::vector<std::string> fileNames;
287 std::vector<Named<CompatibilityMatrix>> results;
288
Yifan Hong270b5652018-01-18 18:46:01 -0800289 if (details::gFetcher->listFiles(kSystemVintfDir, &fileNames, error) != OK) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800290 return {};
291 }
292 for (const std::string& fileName : fileNames) {
Yifan Hong270b5652018-01-18 18:46:01 -0800293 std::string path = kSystemVintfDir + fileName;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800294
295 std::string content;
296 std::string fetchError;
297 status_t status = details::gFetcher->fetch(path, content, &fetchError);
298 if (status != OK) {
299 if (error) {
300 *error += "Ignore file " + path + ": " + fetchError + "\n";
301 }
302 continue;
303 }
304
305 auto it = results.emplace(results.end());
Yifan Hong94757062018-02-09 16:36:31 -0800306 if (!gCompatibilityMatrixConverter(&it->object, content, error)) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800307 if (error) {
Yifan Hong94757062018-02-09 16:36:31 -0800308 *error += "Ignore file " + path + ": " + *error + "\n";
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800309 }
310 results.erase(it);
311 continue;
312 }
313 }
314
315 if (results.empty()) {
316 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800317 *error = "No framework matrices under " + kSystemVintfDir +
318 " can be fetched or parsed.\n" + *error;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800319 }
320 } else {
321 if (error && !error->empty()) {
322 LOG(WARNING) << *error;
323 *error = "";
324 }
325 }
326
327 return results;
328}
329
Yifan Hong3daec812017-02-27 18:49:11 -0800330// static
Yifan Hong1fb004e2017-09-26 15:04:44 -0700331std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
332 RuntimeInfo::FetchFlags flags) {
333 static LockedRuntimeInfoCache gDeviceRuntimeInfo;
334 std::unique_lock<std::mutex> _lock(gDeviceRuntimeInfo.mutex);
335
336 if (!skipCache) {
337 flags &= (~gDeviceRuntimeInfo.fetchedFlags);
338 }
339
340 if (gDeviceRuntimeInfo.object == nullptr) {
Yifan Hong29bb2d42017-09-27 13:28:00 -0700341 gDeviceRuntimeInfo.object = details::gRuntimeInfoFactory->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700342 }
343
344 status_t status = gDeviceRuntimeInfo.object->fetchAllInformation(flags);
345 if (status != OK) {
346 gDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
347 return nullptr;
348 }
349
350 gDeviceRuntimeInfo.fetchedFlags |= flags;
351 return gDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800352}
353
Yifan Hong143cfe62017-04-13 20:18:01 -0700354namespace details {
355
Yifan Hong143cfe62017-04-13 20:18:01 -0700356enum class ParseStatus {
357 OK,
358 PARSE_ERROR,
359 DUPLICATED_FWK_ENTRY,
360 DUPLICATED_DEV_ENTRY,
361};
362
Yifan Hong9532bd22017-04-14 15:30:52 -0700363static std::string toString(ParseStatus status) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700364 switch(status) {
365 case ParseStatus::OK: return "OK";
366 case ParseStatus::PARSE_ERROR: return "parse error";
367 case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework";
368 case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device";
369 }
370 return "";
371}
372
373template<typename T>
Yifan Hong9532bd22017-04-14 15:30:52 -0700374static ParseStatus tryParse(const std::string &xml, const XmlConverter<T> &parse,
Yifan Hongfc73edf2017-08-29 11:39:07 -0700375 std::shared_ptr<T> *fwk, std::shared_ptr<T> *dev) {
376 std::shared_ptr<T> ret = std::make_shared<T>();
Yifan Hong94757062018-02-09 16:36:31 -0800377 if (!parse(ret.get(), xml, nullptr /* error */)) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700378 return ParseStatus::PARSE_ERROR;
379 }
380 if (ret->type() == SchemaType::FRAMEWORK) {
381 if (fwk->get() != nullptr) {
382 return ParseStatus::DUPLICATED_FWK_ENTRY;
383 }
384 *fwk = std::move(ret);
385 } else if (ret->type() == SchemaType::DEVICE) {
386 if (dev->get() != nullptr) {
387 return ParseStatus::DUPLICATED_DEV_ENTRY;
388 }
389 *dev = std::move(ret);
390 }
391 return ParseStatus::OK;
392}
393
394template<typename T, typename GetFunction>
Yifan Hongfc73edf2017-08-29 11:39:07 -0700395static status_t getMissing(const std::shared_ptr<T>& pkg, bool mount,
Yifan Hong143cfe62017-04-13 20:18:01 -0700396 std::function<status_t(void)> mountFunction,
Yifan Hongfc73edf2017-08-29 11:39:07 -0700397 std::shared_ptr<const T>* updated,
Yifan Hong143cfe62017-04-13 20:18:01 -0700398 GetFunction getFunction) {
399 if (pkg != nullptr) {
400 *updated = pkg;
401 } else {
402 if (mount) {
403 (void)mountFunction(); // ignore mount errors
404 }
405 *updated = getFunction();
406 }
407 return OK;
408}
409
410#define ADD_MESSAGE(__error__) \
411 if (error != nullptr) { \
412 *error += (__error__); \
413 } \
414
415struct PackageInfo {
416 struct Pair {
Yifan Hongfc73edf2017-08-29 11:39:07 -0700417 std::shared_ptr<HalManifest> manifest;
418 std::shared_ptr<CompatibilityMatrix> matrix;
Yifan Hong143cfe62017-04-13 20:18:01 -0700419 };
420 Pair dev;
421 Pair fwk;
422};
423
424struct UpdatedInfo {
425 struct Pair {
Yifan Hongfc73edf2017-08-29 11:39:07 -0700426 std::shared_ptr<const HalManifest> manifest;
427 std::shared_ptr<const CompatibilityMatrix> matrix;
Yifan Hong143cfe62017-04-13 20:18:01 -0700428 };
429 Pair dev;
430 Pair fwk;
Yifan Hongfc73edf2017-08-29 11:39:07 -0700431 std::shared_ptr<const RuntimeInfo> runtimeInfo;
Yifan Hong143cfe62017-04-13 20:18:01 -0700432};
433
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700434// Checks given compatibility info against info on the device. If no
435// compatability info is given then the device info will be checked against
436// itself.
Yifan Hongdb6423e2017-09-11 14:38:46 -0700437int32_t checkCompatibility(const std::vector<std::string>& xmls, bool mount,
438 const PartitionMounter& mounter, std::string* error,
439 DisabledChecks disabledChecks) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700440 status_t status;
441 ParseStatus parseStatus;
442 PackageInfo pkg; // All information from package.
443 UpdatedInfo updated; // All files and runtime info after the update.
444
Yifan Hong143cfe62017-04-13 20:18:01 -0700445 // parse all information from package
446 for (const auto &xml : xmls) {
447 parseStatus = tryParse(xml, gHalManifestConverter, &pkg.fwk.manifest, &pkg.dev.manifest);
448 if (parseStatus == ParseStatus::OK) {
449 continue; // work on next one
450 }
451 if (parseStatus != ParseStatus::PARSE_ERROR) {
452 ADD_MESSAGE(toString(parseStatus) + " manifest");
453 return ALREADY_EXISTS;
454 }
455 parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &pkg.fwk.matrix, &pkg.dev.matrix);
456 if (parseStatus == ParseStatus::OK) {
457 continue; // work on next one
458 }
459 if (parseStatus != ParseStatus::PARSE_ERROR) {
460 ADD_MESSAGE(toString(parseStatus) + " matrix");
461 return ALREADY_EXISTS;
462 }
463 ADD_MESSAGE(toString(parseStatus)); // parse error
464 return BAD_VALUE;
465 }
466
467 // get missing info from device
Yifan Hong8640cd12017-05-17 12:02:28 -0700468 // use functions instead of std::bind because std::bind doesn't work well with mock objects
469 auto mountSystem = [&mounter] { return mounter.mountSystem(); };
470 auto mountVendor = [&mounter] { return mounter.mountVendor(); };
471 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700472 pkg.fwk.manifest, mount, mountSystem, &updated.fwk.manifest,
Yifan Hong8640cd12017-05-17 12:02:28 -0700473 std::bind(VintfObject::GetFrameworkHalManifest, true /* skipCache */))) != OK) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700474 return status;
475 }
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700476 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700477 pkg.dev.manifest, mount, mountVendor, &updated.dev.manifest,
Yifan Hong8640cd12017-05-17 12:02:28 -0700478 std::bind(VintfObject::GetDeviceHalManifest, true /* skipCache */))) != OK) {
479 return status;
480 }
481 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700482 pkg.fwk.matrix, mount, mountSystem, &updated.fwk.matrix,
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700483 std::bind(VintfObject::GetFrameworkCompatibilityMatrix, true /* skipCache */))) !=
484 OK) {
485 return status;
486 }
487 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700488 pkg.dev.matrix, mount, mountVendor, &updated.dev.matrix,
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700489 std::bind(VintfObject::GetDeviceCompatibilityMatrix, true /* skipCache */))) != OK) {
490 return status;
491 }
Yifan Hong8640cd12017-05-17 12:02:28 -0700492
493 if (mount) {
494 (void)mounter.umountSystem(); // ignore errors
495 (void)mounter.umountVendor(); // ignore errors
496 }
497
498 updated.runtimeInfo = VintfObject::GetRuntimeInfo(true /* skipCache */);
Yifan Hong143cfe62017-04-13 20:18:01 -0700499
500 // null checks for files and runtime info after the update
Yifan Hong143cfe62017-04-13 20:18:01 -0700501 if (updated.fwk.manifest == nullptr) {
502 ADD_MESSAGE("No framework manifest file from device or from update package");
503 return NO_INIT;
504 }
505 if (updated.dev.manifest == nullptr) {
506 ADD_MESSAGE("No device manifest file from device or from update package");
507 return NO_INIT;
508 }
509 if (updated.fwk.matrix == nullptr) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800510 ADD_MESSAGE("No framework matrix file from device or from update package");
511 return NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700512 }
513 if (updated.dev.matrix == nullptr) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800514 ADD_MESSAGE("No device matrix file from device or from update package");
515 return NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700516 }
517 if (updated.runtimeInfo == nullptr) {
518 ADD_MESSAGE("No runtime info from device");
519 return NO_INIT;
520 }
521
522 // compatiblity check.
Yifan Hongca386fe2018-02-07 11:29:03 -0800523 if (!updated.dev.manifest->checkCompatibility(*updated.fwk.matrix, error)) {
524 if (error) {
525 error->insert(0,
526 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700527 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800528 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700529 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800530 if (!updated.fwk.manifest->checkCompatibility(*updated.dev.matrix, error)) {
531 if (error) {
532 error->insert(0,
533 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700534 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800535 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700536 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800537 if (!updated.runtimeInfo->checkCompatibility(*updated.fwk.matrix, error, disabledChecks)) {
538 if (error) {
539 error->insert(0, "Runtime info and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700540 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800541 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700542 }
543
544 return COMPATIBLE;
545}
546
Yifan Hong270b5652018-01-18 18:46:01 -0800547const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800548const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800549const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800550
551const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800552const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800553const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800554const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800555
556const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
557const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800558const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800559const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
560const std::string kOdmLegacyVintfDir = "/odm/etc/";
561const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
562
Yifan Hong143cfe62017-04-13 20:18:01 -0700563} // namespace details
564
Yifan Hongfbbf0472017-04-07 18:14:18 -0700565// static
Yifan Hongdb6423e2017-09-11 14:38:46 -0700566int32_t VintfObject::CheckCompatibility(const std::vector<std::string>& xmls, std::string* error,
567 DisabledChecks disabledChecks) {
568 return details::checkCompatibility(xmls, false /* mount */, *details::gPartitionMounter, error,
569 disabledChecks);
Yifan Hongfbbf0472017-04-07 18:14:18 -0700570}
571
Yifan Hongf73ba512018-01-17 15:52:30 -0800572bool VintfObject::isHalDeprecated(const MatrixHal& oldMatrixHal,
573 const CompatibilityMatrix& targetMatrix,
574 const IsInstanceInUse& isInstanceInUse, std::string* error) {
575 for (const VersionRange& range : oldMatrixHal.versionRanges) {
576 for (const HalInterface& interface : iterateValues(oldMatrixHal.interfaces)) {
577 for (const std::string& instance : interface.instances) {
578 if (isInstanceDeprecated(oldMatrixHal.name, range.minVer(), interface.name,
579 instance, targetMatrix, isInstanceInUse, error)) {
580 return true;
581 }
582 }
583 }
584 }
585 return false;
586}
587
588// If isInstanceInUse(package@x.y::interface/instance), return true iff:
589// 1. package@x.?::interface/instance is not in targetMatrix; OR
590// 2. package@x.z::interface/instance is in targetMatrix but
591// !isInstanceInUse(package@x.z::interface/instance)
592bool VintfObject::isInstanceDeprecated(const std::string& package, Version version,
593 const std::string& interface, const std::string& instance,
594 const CompatibilityMatrix& targetMatrix,
595 const IsInstanceInUse& isInstanceInUse,
596 std::string* error) {
597 bool oldVersionIsServed;
598 Version servedVersion;
599 std::tie(oldVersionIsServed, servedVersion) =
600 isInstanceInUse(package, version, interface, instance);
601 if (oldVersionIsServed) {
602 // Find any package@x.? in target matrix, and check if instance is in target matrix.
603 const MatrixHal* targetMatrixHal;
604 const VersionRange* targetMatrixRange;
605 std::tie(targetMatrixHal, targetMatrixRange) =
606 targetMatrix.getHalWithMajorVersion(package, version.majorVer);
607 if (targetMatrixHal == nullptr || targetMatrixRange == nullptr) {
608 if (error) {
Yifan Hongb6c7f492018-02-27 14:07:57 -0800609 *error = toFQNameString(package, servedVersion) +
Yifan Hongf73ba512018-01-17 15:52:30 -0800610 "is deprecated in compatibility matrix at FCM Version " +
611 to_string(targetMatrix.level()) + "; it should not be served.";
612 }
613 return true;
614 }
615
616 const auto& targetMatrixInstances = targetMatrixHal->getInstances(interface);
617 if (targetMatrixInstances.find(instance) == targetMatrixInstances.end()) {
618 if (error) {
Yifan Hongb6c7f492018-02-27 14:07:57 -0800619 *error += toFQNameString(package, servedVersion, interface, instance) +
620 " is deprecated at FCM version " + to_string(targetMatrix.level()) +
621 "; it should be not be served.\n";
Yifan Hongf73ba512018-01-17 15:52:30 -0800622 }
623 return true;
624 }
625
626 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
627 bool targetVersionServed;
628 std::tie(targetVersionServed, std::ignore) =
629 isInstanceInUse(package, targetMatrixRange->minVer(), interface, instance);
630
631 if (!targetVersionServed) {
632 if (error) {
Yifan Hongb6c7f492018-02-27 14:07:57 -0800633 *error += toFQNameString(package, servedVersion) + " is deprecated; " +
Yifan Hongf73ba512018-01-17 15:52:30 -0800634 "require at least " + to_string(targetMatrixRange->minVer()) + "\n";
635 }
636 return true;
637 }
638 }
639 return false;
640}
641
642int32_t VintfObject::CheckDeprecation(const IsInstanceInUse& isInstanceInUse,
643 std::string* error) {
644 auto matrixFragments = GetAllFrameworkMatrixLevels(error);
645 if (matrixFragments.empty()) {
646 if (error && error->empty())
647 *error = "Cannot get framework matrix for each FCM version for unknown error.";
648 return NAME_NOT_FOUND;
649 }
650 auto deviceManifest = GetDeviceHalManifest();
651 if (deviceManifest == nullptr) {
652 if (error) *error = "No device manifest.";
653 return NAME_NOT_FOUND;
654 }
655 Level deviceLevel = deviceManifest->level();
656 if (deviceLevel == Level::UNSPECIFIED) {
657 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
658 return BAD_VALUE;
659 }
660
661 const CompatibilityMatrix* targetMatrix = nullptr;
662 for (const auto& namedMatrix : matrixFragments) {
663 if (namedMatrix.object.level() == deviceLevel) {
664 targetMatrix = &namedMatrix.object;
665 }
666 }
667 if (targetMatrix == nullptr) {
668 if (error)
669 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
670 return NAME_NOT_FOUND;
671 }
672
673 bool hasDeprecatedHals = false;
674 for (const auto& namedMatrix : matrixFragments) {
675 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
676 if (namedMatrix.object.level() >= deviceLevel) continue;
677
678 const auto& oldMatrix = namedMatrix.object;
679 for (const MatrixHal& hal : oldMatrix.getHals()) {
680 hasDeprecatedHals |= isHalDeprecated(hal, *targetMatrix, isInstanceInUse, error);
681 }
682 }
683
684 return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS;
685}
686
687int32_t VintfObject::CheckDeprecation(std::string* error) {
688 auto deviceManifest = GetDeviceHalManifest();
689 IsInstanceInUse inManifest = [&deviceManifest](const std::string& package, Version version,
690 const std::string& interface,
691 const std::string& instance) {
692 const ManifestHal* hal = deviceManifest->getHal(package, version);
693 if (hal == nullptr) {
694 return std::make_pair(false, Version{});
695 }
696 const auto& instances = hal->getInstances(interface);
697 if (instances.find(instance) == instances.end()) {
698 return std::make_pair(false, Version{});
699 }
700
701 for (Version v : hal->versions) {
702 if (v.minorAtLeast(version)) {
703 return std::make_pair(true, v);
704 }
705 }
706 return std::make_pair(false, Version{});
707 };
708 return CheckDeprecation(inManifest, error);
709}
Yifan Hong3daec812017-02-27 18:49:11 -0800710
711} // namespace vintf
712} // namespace android