blob: 3f097f7c2a07976f955e7941c7c4a48376210d0b [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 Hong143cfe62017-04-13 20:18:01 -070084 return Get(&gFrameworkManifest, skipCache,
Yifan Hong270b5652018-01-18 18:46:01 -080085 std::bind(&HalManifest::fetchAllInformation, _1, kSystemManifest, _2));
Yifan Hong3daec812017-02-27 18:49:11 -080086}
87
Yifan Hong2272bf82017-04-28 14:37:56 -070088
89// static
Yifan Hongfc73edf2017-08-29 11:39:07 -070090std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) {
91 static LockedSharedPtr<CompatibilityMatrix> gDeviceMatrix;
Yifan Hongb64ec9e2018-01-18 18:57:52 -080092 return Get(&gDeviceMatrix, skipCache, &VintfObject::FetchDeviceMatrix);
Yifan Hong2272bf82017-04-28 14:37:56 -070093}
94
95// static
Yifan Hongfc73edf2017-08-29 11:39:07 -070096std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) {
97 static LockedSharedPtr<CompatibilityMatrix> gFrameworkMatrix;
Yifan Hongd52bf3e2018-01-11 16:56:51 -080098 static LockedSharedPtr<CompatibilityMatrix> gCombinedFrameworkMatrix;
99 static std::mutex gFrameworkCompatibilityMatrixMutex;
100
101 // To avoid deadlock, get device manifest before any locks.
102 auto deviceManifest = GetDeviceHalManifest();
103
104 std::unique_lock<std::mutex> _lock(gFrameworkCompatibilityMatrixMutex);
105
106 auto combined =
107 Get(&gCombinedFrameworkMatrix, skipCache,
108 std::bind(&VintfObject::GetCombinedFrameworkMatrix, deviceManifest, _1, _2));
109 if (combined != nullptr) {
110 return combined;
111 }
112
Yifan Hong2272bf82017-04-28 14:37:56 -0700113 return Get(&gFrameworkMatrix, skipCache,
Yifan Hong270b5652018-01-18 18:46:01 -0800114 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, kSystemLegacyMatrix, _2));
Yifan Hong2272bf82017-04-28 14:37:56 -0700115}
116
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800117status_t VintfObject::GetCombinedFrameworkMatrix(
118 const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out,
119 std::string* error) {
120 auto matrixFragments = GetAllFrameworkMatrixLevels(error);
121 if (matrixFragments.empty()) {
122 return NAME_NOT_FOUND;
123 }
124
125 Level deviceLevel = Level::UNSPECIFIED;
126
127 if (deviceManifest != nullptr) {
128 deviceLevel = deviceManifest->level();
129 }
130
131 // TODO(b/70628538): Do not infer from Shipping API level.
132#ifdef LIBVINTF_TARGET
133 if (deviceLevel == Level::UNSPECIFIED) {
134 auto shippingApi =
135 android::base::GetUintProperty<uint64_t>("ro.product.first_api_level", 0u);
136 if (shippingApi != 0u) {
137 deviceLevel = details::convertFromApiLevel(shippingApi);
138 }
139 }
140#endif
141
142 if (deviceLevel == Level::UNSPECIFIED) {
143 // Cannot infer FCM version. Combine all matrices by assuming
144 // Shipping FCM Version == min(all supported FCM Versions in the framework)
145 for (auto&& pair : matrixFragments) {
146 Level fragmentLevel = pair.object.level();
147 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
148 deviceLevel = fragmentLevel;
149 }
150 }
151 }
152
153 if (deviceLevel == Level::UNSPECIFIED) {
154 // None of the fragments specify any FCM version. Should never happen except
155 // for inconsistent builds.
156 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800157 *error = "No framework compatibility matrix files under " + kSystemVintfDir +
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800158 " declare FCM version.";
159 }
160 return NAME_NOT_FOUND;
161 }
162
163 CompatibilityMatrix* combined =
164 CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
165 if (combined == nullptr) {
166 return BAD_VALUE;
167 }
168 *out = std::move(*combined);
169 return OK;
170}
171
Yifan Hong5a93bf22018-01-17 18:22:32 -0800172// Priority for loading vendor manifest:
Yifan Hong12a11ac2018-01-19 13:58:32 -0800173// 1. /vendor/etc/vintf/manifest.xml + ODM manifest
174// 2. /vendor/etc/vintf/manifest.xml
175// 3. ODM manifest
176// 4. /vendor/manifest.xml
Yifan Hong5a93bf22018-01-17 18:22:32 -0800177// where:
Yifan Hong5a93bf22018-01-17 18:22:32 -0800178// A + B means adding <hal> tags from B to A (so that <hal>s from B can override A)
179status_t VintfObject::FetchDeviceHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800180 status_t vendorStatus = FetchOneHalManifest(kVendorManifest, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800181 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
182 return vendorStatus;
183 }
184
185 HalManifest odmManifest;
Yifan Hong12a11ac2018-01-19 13:58:32 -0800186 status_t odmStatus = FetchOdmHalManifest(&odmManifest, error);
187 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
188 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800189 }
190
Yifan Hong5a93bf22018-01-17 18:22:32 -0800191 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800192 if (odmStatus == OK) {
193 out->addAllHals(&odmManifest);
194 }
Yifan Hong5a93bf22018-01-17 18:22:32 -0800195 return OK;
196 }
197
Yifan Hong12a11ac2018-01-19 13:58:32 -0800198 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800199 if (odmStatus == OK) {
200 *out = std::move(odmManifest);
201 return OK;
202 }
203
204 // Use legacy /vendor/manifest.xml
Yifan Hong270b5652018-01-18 18:46:01 -0800205 return out->fetchAllInformation(kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800206}
207
Yifan Hong12a11ac2018-01-19 13:58:32 -0800208// "out" is written to iff return status is OK.
209// Priority:
210// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
211// 2. /odm/etc/vintf/manifest.xml
212// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
213// 4. /odm/etc/manifest.xml
214// where:
215// {sku} is the value of ro.boot.product.hardware.sku
216status_t VintfObject::FetchOdmHalManifest(HalManifest* out, std::string* error) {
217 status_t status;
218
219#ifdef LIBVINTF_TARGET
220 std::string productModel;
221 productModel = android::base::GetProperty("ro.boot.product.hardware.sku", "");
222
223 if (!productModel.empty()) {
224 status =
225 FetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
226 if (status == OK || status != NAME_NOT_FOUND) {
227 return status;
228 }
229 }
230#endif
231
232 status = FetchOneHalManifest(kOdmManifest, out, error);
233 if (status == OK || status != NAME_NOT_FOUND) {
234 return status;
235 }
236
237#ifdef LIBVINTF_TARGET
238 if (!productModel.empty()) {
239 status = FetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
240 error);
241 if (status == OK || status != NAME_NOT_FOUND) {
242 return status;
243 }
244 }
245#endif
246
247 status = FetchOneHalManifest(kOdmLegacyManifest, out, error);
248 if (status == OK || status != NAME_NOT_FOUND) {
249 return status;
250 }
251
252 return NAME_NOT_FOUND;
253}
254
255// Fetch one manifest.xml file. "out" is written to iff return status is OK.
256// Returns NAME_NOT_FOUND if file is missing.
257status_t VintfObject::FetchOneHalManifest(const std::string& path, HalManifest* out,
258 std::string* error) {
259 HalManifest ret;
260 status_t status = ret.fetchAllInformation(path, error);
261 if (status == OK) {
262 *out = std::move(ret);
263 }
264 return status;
265}
266
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800267status_t VintfObject::FetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
268 CompatibilityMatrix etcMatrix;
269 if (etcMatrix.fetchAllInformation(kVendorMatrix, error) == OK) {
270 *out = std::move(etcMatrix);
271 return OK;
272 }
273 return out->fetchAllInformation(kVendorLegacyMatrix, error);
274}
275
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800276std::vector<Named<CompatibilityMatrix>> VintfObject::GetAllFrameworkMatrixLevels(
277 std::string* error) {
278 std::vector<std::string> fileNames;
279 std::vector<Named<CompatibilityMatrix>> results;
280
Yifan Hong270b5652018-01-18 18:46:01 -0800281 if (details::gFetcher->listFiles(kSystemVintfDir, &fileNames, error) != OK) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800282 return {};
283 }
284 for (const std::string& fileName : fileNames) {
Yifan Hong270b5652018-01-18 18:46:01 -0800285 std::string path = kSystemVintfDir + fileName;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800286
287 std::string content;
288 std::string fetchError;
289 status_t status = details::gFetcher->fetch(path, content, &fetchError);
290 if (status != OK) {
291 if (error) {
292 *error += "Ignore file " + path + ": " + fetchError + "\n";
293 }
294 continue;
295 }
296
297 auto it = results.emplace(results.end());
298 if (!gCompatibilityMatrixConverter(&it->object, content)) {
299 if (error) {
300 // TODO(b/71874788): do not use lastError() because it is not thread-safe.
301 *error +=
302 "Ignore file " + path + ": " + gCompatibilityMatrixConverter.lastError() + "\n";
303 }
304 results.erase(it);
305 continue;
306 }
307 }
308
309 if (results.empty()) {
310 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800311 *error = "No framework matrices under " + kSystemVintfDir +
312 " can be fetched or parsed.\n" + *error;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800313 }
314 } else {
315 if (error && !error->empty()) {
316 LOG(WARNING) << *error;
317 *error = "";
318 }
319 }
320
321 return results;
322}
323
Yifan Hong3daec812017-02-27 18:49:11 -0800324// static
Yifan Hong1fb004e2017-09-26 15:04:44 -0700325std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
326 RuntimeInfo::FetchFlags flags) {
327 static LockedRuntimeInfoCache gDeviceRuntimeInfo;
328 std::unique_lock<std::mutex> _lock(gDeviceRuntimeInfo.mutex);
329
330 if (!skipCache) {
331 flags &= (~gDeviceRuntimeInfo.fetchedFlags);
332 }
333
334 if (gDeviceRuntimeInfo.object == nullptr) {
Yifan Hong29bb2d42017-09-27 13:28:00 -0700335 gDeviceRuntimeInfo.object = details::gRuntimeInfoFactory->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700336 }
337
338 status_t status = gDeviceRuntimeInfo.object->fetchAllInformation(flags);
339 if (status != OK) {
340 gDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
341 return nullptr;
342 }
343
344 gDeviceRuntimeInfo.fetchedFlags |= flags;
345 return gDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800346}
347
Yifan Hong143cfe62017-04-13 20:18:01 -0700348namespace details {
349
Yifan Hong143cfe62017-04-13 20:18:01 -0700350enum class ParseStatus {
351 OK,
352 PARSE_ERROR,
353 DUPLICATED_FWK_ENTRY,
354 DUPLICATED_DEV_ENTRY,
355};
356
Yifan Hong9532bd22017-04-14 15:30:52 -0700357static std::string toString(ParseStatus status) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700358 switch(status) {
359 case ParseStatus::OK: return "OK";
360 case ParseStatus::PARSE_ERROR: return "parse error";
361 case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework";
362 case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device";
363 }
364 return "";
365}
366
367template<typename T>
Yifan Hong9532bd22017-04-14 15:30:52 -0700368static ParseStatus tryParse(const std::string &xml, const XmlConverter<T> &parse,
Yifan Hongfc73edf2017-08-29 11:39:07 -0700369 std::shared_ptr<T> *fwk, std::shared_ptr<T> *dev) {
370 std::shared_ptr<T> ret = std::make_shared<T>();
Yifan Hong143cfe62017-04-13 20:18:01 -0700371 if (!parse(ret.get(), xml)) {
372 return ParseStatus::PARSE_ERROR;
373 }
374 if (ret->type() == SchemaType::FRAMEWORK) {
375 if (fwk->get() != nullptr) {
376 return ParseStatus::DUPLICATED_FWK_ENTRY;
377 }
378 *fwk = std::move(ret);
379 } else if (ret->type() == SchemaType::DEVICE) {
380 if (dev->get() != nullptr) {
381 return ParseStatus::DUPLICATED_DEV_ENTRY;
382 }
383 *dev = std::move(ret);
384 }
385 return ParseStatus::OK;
386}
387
388template<typename T, typename GetFunction>
Yifan Hongfc73edf2017-08-29 11:39:07 -0700389static status_t getMissing(const std::shared_ptr<T>& pkg, bool mount,
Yifan Hong143cfe62017-04-13 20:18:01 -0700390 std::function<status_t(void)> mountFunction,
Yifan Hongfc73edf2017-08-29 11:39:07 -0700391 std::shared_ptr<const T>* updated,
Yifan Hong143cfe62017-04-13 20:18:01 -0700392 GetFunction getFunction) {
393 if (pkg != nullptr) {
394 *updated = pkg;
395 } else {
396 if (mount) {
397 (void)mountFunction(); // ignore mount errors
398 }
399 *updated = getFunction();
400 }
401 return OK;
402}
403
404#define ADD_MESSAGE(__error__) \
405 if (error != nullptr) { \
406 *error += (__error__); \
407 } \
408
409struct PackageInfo {
410 struct Pair {
Yifan Hongfc73edf2017-08-29 11:39:07 -0700411 std::shared_ptr<HalManifest> manifest;
412 std::shared_ptr<CompatibilityMatrix> matrix;
Yifan Hong143cfe62017-04-13 20:18:01 -0700413 };
414 Pair dev;
415 Pair fwk;
416};
417
418struct UpdatedInfo {
419 struct Pair {
Yifan Hongfc73edf2017-08-29 11:39:07 -0700420 std::shared_ptr<const HalManifest> manifest;
421 std::shared_ptr<const CompatibilityMatrix> matrix;
Yifan Hong143cfe62017-04-13 20:18:01 -0700422 };
423 Pair dev;
424 Pair fwk;
Yifan Hongfc73edf2017-08-29 11:39:07 -0700425 std::shared_ptr<const RuntimeInfo> runtimeInfo;
Yifan Hong143cfe62017-04-13 20:18:01 -0700426};
427
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700428// Checks given compatibility info against info on the device. If no
429// compatability info is given then the device info will be checked against
430// itself.
Yifan Hongdb6423e2017-09-11 14:38:46 -0700431int32_t checkCompatibility(const std::vector<std::string>& xmls, bool mount,
432 const PartitionMounter& mounter, std::string* error,
433 DisabledChecks disabledChecks) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700434 status_t status;
435 ParseStatus parseStatus;
436 PackageInfo pkg; // All information from package.
437 UpdatedInfo updated; // All files and runtime info after the update.
438
Yifan Hong143cfe62017-04-13 20:18:01 -0700439 // parse all information from package
440 for (const auto &xml : xmls) {
441 parseStatus = tryParse(xml, gHalManifestConverter, &pkg.fwk.manifest, &pkg.dev.manifest);
442 if (parseStatus == ParseStatus::OK) {
443 continue; // work on next one
444 }
445 if (parseStatus != ParseStatus::PARSE_ERROR) {
446 ADD_MESSAGE(toString(parseStatus) + " manifest");
447 return ALREADY_EXISTS;
448 }
449 parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &pkg.fwk.matrix, &pkg.dev.matrix);
450 if (parseStatus == ParseStatus::OK) {
451 continue; // work on next one
452 }
453 if (parseStatus != ParseStatus::PARSE_ERROR) {
454 ADD_MESSAGE(toString(parseStatus) + " matrix");
455 return ALREADY_EXISTS;
456 }
457 ADD_MESSAGE(toString(parseStatus)); // parse error
458 return BAD_VALUE;
459 }
460
461 // get missing info from device
Yifan Hong8640cd12017-05-17 12:02:28 -0700462 // use functions instead of std::bind because std::bind doesn't work well with mock objects
463 auto mountSystem = [&mounter] { return mounter.mountSystem(); };
464 auto mountVendor = [&mounter] { return mounter.mountVendor(); };
465 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700466 pkg.fwk.manifest, mount, mountSystem, &updated.fwk.manifest,
Yifan Hong8640cd12017-05-17 12:02:28 -0700467 std::bind(VintfObject::GetFrameworkHalManifest, true /* skipCache */))) != OK) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700468 return status;
469 }
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700470 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700471 pkg.dev.manifest, mount, mountVendor, &updated.dev.manifest,
Yifan Hong8640cd12017-05-17 12:02:28 -0700472 std::bind(VintfObject::GetDeviceHalManifest, true /* skipCache */))) != OK) {
473 return status;
474 }
475 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700476 pkg.fwk.matrix, mount, mountSystem, &updated.fwk.matrix,
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700477 std::bind(VintfObject::GetFrameworkCompatibilityMatrix, true /* skipCache */))) !=
478 OK) {
479 return status;
480 }
481 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700482 pkg.dev.matrix, mount, mountVendor, &updated.dev.matrix,
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700483 std::bind(VintfObject::GetDeviceCompatibilityMatrix, true /* skipCache */))) != OK) {
484 return status;
485 }
Yifan Hong8640cd12017-05-17 12:02:28 -0700486
487 if (mount) {
488 (void)mounter.umountSystem(); // ignore errors
489 (void)mounter.umountVendor(); // ignore errors
490 }
491
492 updated.runtimeInfo = VintfObject::GetRuntimeInfo(true /* skipCache */);
Yifan Hong143cfe62017-04-13 20:18:01 -0700493
494 // null checks for files and runtime info after the update
Yifan Hong143cfe62017-04-13 20:18:01 -0700495 if (updated.fwk.manifest == nullptr) {
496 ADD_MESSAGE("No framework manifest file from device or from update package");
497 return NO_INIT;
498 }
499 if (updated.dev.manifest == nullptr) {
500 ADD_MESSAGE("No device manifest file from device or from update package");
501 return NO_INIT;
502 }
503 if (updated.fwk.matrix == nullptr) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800504 ADD_MESSAGE("No framework matrix file from device or from update package");
505 return NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700506 }
507 if (updated.dev.matrix == nullptr) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800508 ADD_MESSAGE("No device matrix file from device or from update package");
509 return NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700510 }
511 if (updated.runtimeInfo == nullptr) {
512 ADD_MESSAGE("No runtime info from device");
513 return NO_INIT;
514 }
515
516 // compatiblity check.
Yifan Hongca386fe2018-02-07 11:29:03 -0800517 if (!updated.dev.manifest->checkCompatibility(*updated.fwk.matrix, error)) {
518 if (error) {
519 error->insert(0,
520 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700521 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800522 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700523 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800524 if (!updated.fwk.manifest->checkCompatibility(*updated.dev.matrix, error)) {
525 if (error) {
526 error->insert(0,
527 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700528 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800529 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700530 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800531 if (!updated.runtimeInfo->checkCompatibility(*updated.fwk.matrix, error, disabledChecks)) {
532 if (error) {
533 error->insert(0, "Runtime info and framework 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 }
537
538 return COMPATIBLE;
539}
540
Yifan Hong270b5652018-01-18 18:46:01 -0800541const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800542const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800543const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800544
545const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800546const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800547const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800548const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800549
550const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
551const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
552const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
553const std::string kOdmLegacyVintfDir = "/odm/etc/";
554const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
555
Yifan Hong143cfe62017-04-13 20:18:01 -0700556} // namespace details
557
Yifan Hongfbbf0472017-04-07 18:14:18 -0700558// static
Yifan Hongdb6423e2017-09-11 14:38:46 -0700559int32_t VintfObject::CheckCompatibility(const std::vector<std::string>& xmls, std::string* error,
560 DisabledChecks disabledChecks) {
561 return details::checkCompatibility(xmls, false /* mount */, *details::gPartitionMounter, error,
562 disabledChecks);
Yifan Hongfbbf0472017-04-07 18:14:18 -0700563}
564
Yifan Hongf73ba512018-01-17 15:52:30 -0800565bool VintfObject::isHalDeprecated(const MatrixHal& oldMatrixHal,
566 const CompatibilityMatrix& targetMatrix,
567 const IsInstanceInUse& isInstanceInUse, std::string* error) {
568 for (const VersionRange& range : oldMatrixHal.versionRanges) {
569 for (const HalInterface& interface : iterateValues(oldMatrixHal.interfaces)) {
570 for (const std::string& instance : interface.instances) {
571 if (isInstanceDeprecated(oldMatrixHal.name, range.minVer(), interface.name,
572 instance, targetMatrix, isInstanceInUse, error)) {
573 return true;
574 }
575 }
576 }
577 }
578 return false;
579}
580
581// If isInstanceInUse(package@x.y::interface/instance), return true iff:
582// 1. package@x.?::interface/instance is not in targetMatrix; OR
583// 2. package@x.z::interface/instance is in targetMatrix but
584// !isInstanceInUse(package@x.z::interface/instance)
585bool VintfObject::isInstanceDeprecated(const std::string& package, Version version,
586 const std::string& interface, const std::string& instance,
587 const CompatibilityMatrix& targetMatrix,
588 const IsInstanceInUse& isInstanceInUse,
589 std::string* error) {
590 bool oldVersionIsServed;
591 Version servedVersion;
592 std::tie(oldVersionIsServed, servedVersion) =
593 isInstanceInUse(package, version, interface, instance);
594 if (oldVersionIsServed) {
595 // Find any package@x.? in target matrix, and check if instance is in target matrix.
596 const MatrixHal* targetMatrixHal;
597 const VersionRange* targetMatrixRange;
598 std::tie(targetMatrixHal, targetMatrixRange) =
599 targetMatrix.getHalWithMajorVersion(package, version.majorVer);
600 if (targetMatrixHal == nullptr || targetMatrixRange == nullptr) {
601 if (error) {
602 *error = package + "@" + to_string(servedVersion) +
603 "is deprecated in compatibility matrix at FCM Version " +
604 to_string(targetMatrix.level()) + "; it should not be served.";
605 }
606 return true;
607 }
608
609 const auto& targetMatrixInstances = targetMatrixHal->getInstances(interface);
610 if (targetMatrixInstances.find(instance) == targetMatrixInstances.end()) {
611 if (error) {
612 *error += package + "@" + to_string(servedVersion) + "::" + interface + "/" +
613 instance + " is deprecated at FCM version " +
614 to_string(targetMatrix.level()) + "; it should be not be served.\n";
615 }
616 return true;
617 }
618
619 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
620 bool targetVersionServed;
621 std::tie(targetVersionServed, std::ignore) =
622 isInstanceInUse(package, targetMatrixRange->minVer(), interface, instance);
623
624 if (!targetVersionServed) {
625 if (error) {
626 *error += package + "@" + to_string(servedVersion) + " is deprecated; " +
627 "require at least " + to_string(targetMatrixRange->minVer()) + "\n";
628 }
629 return true;
630 }
631 }
632 return false;
633}
634
635int32_t VintfObject::CheckDeprecation(const IsInstanceInUse& isInstanceInUse,
636 std::string* error) {
637 auto matrixFragments = GetAllFrameworkMatrixLevels(error);
638 if (matrixFragments.empty()) {
639 if (error && error->empty())
640 *error = "Cannot get framework matrix for each FCM version for unknown error.";
641 return NAME_NOT_FOUND;
642 }
643 auto deviceManifest = GetDeviceHalManifest();
644 if (deviceManifest == nullptr) {
645 if (error) *error = "No device manifest.";
646 return NAME_NOT_FOUND;
647 }
648 Level deviceLevel = deviceManifest->level();
649 if (deviceLevel == Level::UNSPECIFIED) {
650 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
651 return BAD_VALUE;
652 }
653
654 const CompatibilityMatrix* targetMatrix = nullptr;
655 for (const auto& namedMatrix : matrixFragments) {
656 if (namedMatrix.object.level() == deviceLevel) {
657 targetMatrix = &namedMatrix.object;
658 }
659 }
660 if (targetMatrix == nullptr) {
661 if (error)
662 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
663 return NAME_NOT_FOUND;
664 }
665
666 bool hasDeprecatedHals = false;
667 for (const auto& namedMatrix : matrixFragments) {
668 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
669 if (namedMatrix.object.level() >= deviceLevel) continue;
670
671 const auto& oldMatrix = namedMatrix.object;
672 for (const MatrixHal& hal : oldMatrix.getHals()) {
673 hasDeprecatedHals |= isHalDeprecated(hal, *targetMatrix, isInstanceInUse, error);
674 }
675 }
676
677 return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS;
678}
679
680int32_t VintfObject::CheckDeprecation(std::string* error) {
681 auto deviceManifest = GetDeviceHalManifest();
682 IsInstanceInUse inManifest = [&deviceManifest](const std::string& package, Version version,
683 const std::string& interface,
684 const std::string& instance) {
685 const ManifestHal* hal = deviceManifest->getHal(package, version);
686 if (hal == nullptr) {
687 return std::make_pair(false, Version{});
688 }
689 const auto& instances = hal->getInstances(interface);
690 if (instances.find(instance) == instances.end()) {
691 return std::make_pair(false, Version{});
692 }
693
694 for (Version v : hal->versions) {
695 if (v.minorAtLeast(version)) {
696 return std::make_pair(true, v);
697 }
698 }
699 return std::make_pair(false, Version{});
700 };
701 return CheckDeprecation(inManifest, error);
702}
Yifan Hong3daec812017-02-27 18:49:11 -0800703
704} // namespace vintf
705} // namespace android