blob: 7c55181dc58609a77619dd0ef09e1f694d2cbee3 [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
495 // TODO(b/37321309) if a compat mat is missing, it is not matched and considered compatible.
496 if (updated.fwk.manifest == nullptr) {
497 ADD_MESSAGE("No framework manifest file from device or from update package");
498 return NO_INIT;
499 }
500 if (updated.dev.manifest == nullptr) {
501 ADD_MESSAGE("No device manifest file from device or from update package");
502 return NO_INIT;
503 }
504 if (updated.fwk.matrix == nullptr) {
505 ADD_MESSAGE("No framework matrix, skipping;");
506 // TODO(b/37321309) consider missing matricies as errors.
507 }
508 if (updated.dev.matrix == nullptr) {
509 ADD_MESSAGE("No device matrix, skipping;");
510 // TODO(b/37321309) consider missing matricies as errors.
511 }
512 if (updated.runtimeInfo == nullptr) {
513 ADD_MESSAGE("No runtime info from device");
514 return NO_INIT;
515 }
516
517 // compatiblity check.
518 // TODO(b/37321309) outer if checks can be removed if we consider missing matrices as errors.
519 if (updated.dev.manifest && updated.fwk.matrix) {
520 if (!updated.dev.manifest->checkCompatibility(*updated.fwk.matrix, error)) {
Yifan Hong0d4be122017-05-15 17:04:22 -0700521 if (error)
522 error->insert(0, "Device manifest and framework compatibility matrix "
523 "are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700524 return INCOMPATIBLE;
525 }
526 }
527 if (updated.fwk.manifest && updated.dev.matrix) {
528 if (!updated.fwk.manifest->checkCompatibility(*updated.dev.matrix, error)) {
Yifan Hong0d4be122017-05-15 17:04:22 -0700529 if (error)
530 error->insert(0, "Framework manifest and device compatibility matrix "
531 "are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700532 return INCOMPATIBLE;
533 }
534 }
535 if (updated.runtimeInfo && updated.fwk.matrix) {
Yifan Hongdb6423e2017-09-11 14:38:46 -0700536 if (!updated.runtimeInfo->checkCompatibility(*updated.fwk.matrix, error, disabledChecks)) {
Yifan Hong0d4be122017-05-15 17:04:22 -0700537 if (error)
538 error->insert(0, "Runtime info and framework compatibility matrix "
539 "are incompatible: ");
Yifan Honge8b86842017-05-25 17:59:22 +0000540 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700541 }
542 }
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";
558const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
559const std::string kOdmLegacyVintfDir = "/odm/etc/";
560const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
561
Yifan Hong143cfe62017-04-13 20:18:01 -0700562} // namespace details
563
Yifan Hongfbbf0472017-04-07 18:14:18 -0700564// static
Yifan Hongdb6423e2017-09-11 14:38:46 -0700565int32_t VintfObject::CheckCompatibility(const std::vector<std::string>& xmls, std::string* error,
566 DisabledChecks disabledChecks) {
567 return details::checkCompatibility(xmls, false /* mount */, *details::gPartitionMounter, error,
568 disabledChecks);
Yifan Hongfbbf0472017-04-07 18:14:18 -0700569}
570
Yifan Hongf73ba512018-01-17 15:52:30 -0800571bool VintfObject::isHalDeprecated(const MatrixHal& oldMatrixHal,
572 const CompatibilityMatrix& targetMatrix,
573 const IsInstanceInUse& isInstanceInUse, std::string* error) {
574 for (const VersionRange& range : oldMatrixHal.versionRanges) {
575 for (const HalInterface& interface : iterateValues(oldMatrixHal.interfaces)) {
576 for (const std::string& instance : interface.instances) {
577 if (isInstanceDeprecated(oldMatrixHal.name, range.minVer(), interface.name,
578 instance, targetMatrix, isInstanceInUse, error)) {
579 return true;
580 }
581 }
582 }
583 }
584 return false;
585}
586
587// If isInstanceInUse(package@x.y::interface/instance), return true iff:
588// 1. package@x.?::interface/instance is not in targetMatrix; OR
589// 2. package@x.z::interface/instance is in targetMatrix but
590// !isInstanceInUse(package@x.z::interface/instance)
591bool VintfObject::isInstanceDeprecated(const std::string& package, Version version,
592 const std::string& interface, const std::string& instance,
593 const CompatibilityMatrix& targetMatrix,
594 const IsInstanceInUse& isInstanceInUse,
595 std::string* error) {
596 bool oldVersionIsServed;
597 Version servedVersion;
598 std::tie(oldVersionIsServed, servedVersion) =
599 isInstanceInUse(package, version, interface, instance);
600 if (oldVersionIsServed) {
601 // Find any package@x.? in target matrix, and check if instance is in target matrix.
602 const MatrixHal* targetMatrixHal;
603 const VersionRange* targetMatrixRange;
604 std::tie(targetMatrixHal, targetMatrixRange) =
605 targetMatrix.getHalWithMajorVersion(package, version.majorVer);
606 if (targetMatrixHal == nullptr || targetMatrixRange == nullptr) {
607 if (error) {
608 *error = package + "@" + to_string(servedVersion) +
609 "is deprecated in compatibility matrix at FCM Version " +
610 to_string(targetMatrix.level()) + "; it should not be served.";
611 }
612 return true;
613 }
614
615 const auto& targetMatrixInstances = targetMatrixHal->getInstances(interface);
616 if (targetMatrixInstances.find(instance) == targetMatrixInstances.end()) {
617 if (error) {
618 *error += package + "@" + to_string(servedVersion) + "::" + interface + "/" +
619 instance + " is deprecated at FCM version " +
620 to_string(targetMatrix.level()) + "; it should be not be served.\n";
621 }
622 return true;
623 }
624
625 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
626 bool targetVersionServed;
627 std::tie(targetVersionServed, std::ignore) =
628 isInstanceInUse(package, targetMatrixRange->minVer(), interface, instance);
629
630 if (!targetVersionServed) {
631 if (error) {
632 *error += package + "@" + to_string(servedVersion) + " is deprecated; " +
633 "require at least " + to_string(targetMatrixRange->minVer()) + "\n";
634 }
635 return true;
636 }
637 }
638 return false;
639}
640
641int32_t VintfObject::CheckDeprecation(const IsInstanceInUse& isInstanceInUse,
642 std::string* error) {
643 auto matrixFragments = GetAllFrameworkMatrixLevels(error);
644 if (matrixFragments.empty()) {
645 if (error && error->empty())
646 *error = "Cannot get framework matrix for each FCM version for unknown error.";
647 return NAME_NOT_FOUND;
648 }
649 auto deviceManifest = GetDeviceHalManifest();
650 if (deviceManifest == nullptr) {
651 if (error) *error = "No device manifest.";
652 return NAME_NOT_FOUND;
653 }
654 Level deviceLevel = deviceManifest->level();
655 if (deviceLevel == Level::UNSPECIFIED) {
656 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
657 return BAD_VALUE;
658 }
659
660 const CompatibilityMatrix* targetMatrix = nullptr;
661 for (const auto& namedMatrix : matrixFragments) {
662 if (namedMatrix.object.level() == deviceLevel) {
663 targetMatrix = &namedMatrix.object;
664 }
665 }
666 if (targetMatrix == nullptr) {
667 if (error)
668 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
669 return NAME_NOT_FOUND;
670 }
671
672 bool hasDeprecatedHals = false;
673 for (const auto& namedMatrix : matrixFragments) {
674 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
675 if (namedMatrix.object.level() >= deviceLevel) continue;
676
677 const auto& oldMatrix = namedMatrix.object;
678 for (const MatrixHal& hal : oldMatrix.getHals()) {
679 hasDeprecatedHals |= isHalDeprecated(hal, *targetMatrix, isInstanceInUse, error);
680 }
681 }
682
683 return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS;
684}
685
686int32_t VintfObject::CheckDeprecation(std::string* error) {
687 auto deviceManifest = GetDeviceHalManifest();
688 IsInstanceInUse inManifest = [&deviceManifest](const std::string& package, Version version,
689 const std::string& interface,
690 const std::string& instance) {
691 const ManifestHal* hal = deviceManifest->getHal(package, version);
692 if (hal == nullptr) {
693 return std::make_pair(false, Version{});
694 }
695 const auto& instances = hal->getInstances(interface);
696 if (instances.find(instance) == instances.end()) {
697 return std::make_pair(false, Version{});
698 }
699
700 for (Version v : hal->versions) {
701 if (v.minorAtLeast(version)) {
702 return std::make_pair(true, v);
703 }
704 }
705 return std::make_pair(false, Version{});
706 };
707 return CheckDeprecation(inManifest, error);
708}
Yifan Hong3daec812017-02-27 18:49:11 -0800709
710} // namespace vintf
711} // namespace android