blob: c68de414b55adefc7183ec825dd9365c589a7e6b [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
Yifan Hong5a93bf22018-01-17 18:22:32 -0800164// Priority for loading vendor manifest:
Yifan Hong12a11ac2018-01-19 13:58:32 -0800165// 1. /vendor/etc/vintf/manifest.xml + ODM manifest
166// 2. /vendor/etc/vintf/manifest.xml
167// 3. ODM manifest
168// 4. /vendor/manifest.xml
Yifan Hong5a93bf22018-01-17 18:22:32 -0800169// where:
Yifan Hong5a93bf22018-01-17 18:22:32 -0800170// A + B means adding <hal> tags from B to A (so that <hal>s from B can override A)
171status_t VintfObject::FetchDeviceHalManifest(HalManifest* out, std::string* error) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800172 status_t vendorStatus = FetchOneHalManifest(kVendorManifest, out, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800173 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
174 return vendorStatus;
175 }
176
177 HalManifest odmManifest;
Yifan Hong12a11ac2018-01-19 13:58:32 -0800178 status_t odmStatus = FetchOdmHalManifest(&odmManifest, error);
179 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
180 return odmStatus;
Yifan Hong5a93bf22018-01-17 18:22:32 -0800181 }
182
Yifan Hong5a93bf22018-01-17 18:22:32 -0800183 if (vendorStatus == OK) {
Yifan Hong12a11ac2018-01-19 13:58:32 -0800184 if (odmStatus == OK) {
185 out->addAllHals(&odmManifest);
186 }
Yifan Hong5a93bf22018-01-17 18:22:32 -0800187 return OK;
188 }
189
Yifan Hong12a11ac2018-01-19 13:58:32 -0800190 // vendorStatus != OK, "out" is not changed.
Yifan Hong5a93bf22018-01-17 18:22:32 -0800191 if (odmStatus == OK) {
192 *out = std::move(odmManifest);
193 return OK;
194 }
195
196 // Use legacy /vendor/manifest.xml
Yifan Hong270b5652018-01-18 18:46:01 -0800197 return out->fetchAllInformation(kVendorLegacyManifest, error);
Yifan Hong5a93bf22018-01-17 18:22:32 -0800198}
199
Yifan Hong12a11ac2018-01-19 13:58:32 -0800200// "out" is written to iff return status is OK.
201// Priority:
202// 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
203// 2. /odm/etc/vintf/manifest.xml
204// 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
205// 4. /odm/etc/manifest.xml
206// where:
207// {sku} is the value of ro.boot.product.hardware.sku
208status_t VintfObject::FetchOdmHalManifest(HalManifest* out, std::string* error) {
209 status_t status;
210
Yifan Hong12a11ac2018-01-19 13:58:32 -0800211 std::string productModel;
Yifan Hongd14640a2018-02-27 18:35:39 -0800212 productModel = getPropertyFetcher().getProperty("ro.boot.product.hardware.sku", "");
Yifan Hong12a11ac2018-01-19 13:58:32 -0800213
214 if (!productModel.empty()) {
215 status =
216 FetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
217 if (status == OK || status != NAME_NOT_FOUND) {
218 return status;
219 }
220 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800221
222 status = FetchOneHalManifest(kOdmManifest, out, error);
223 if (status == OK || status != NAME_NOT_FOUND) {
224 return status;
225 }
226
Yifan Hong12a11ac2018-01-19 13:58:32 -0800227 if (!productModel.empty()) {
228 status = FetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
229 error);
230 if (status == OK || status != NAME_NOT_FOUND) {
231 return status;
232 }
233 }
Yifan Hong12a11ac2018-01-19 13:58:32 -0800234
235 status = FetchOneHalManifest(kOdmLegacyManifest, out, error);
236 if (status == OK || status != NAME_NOT_FOUND) {
237 return status;
238 }
239
240 return NAME_NOT_FOUND;
241}
242
243// Fetch one manifest.xml file. "out" is written to iff return status is OK.
244// Returns NAME_NOT_FOUND if file is missing.
245status_t VintfObject::FetchOneHalManifest(const std::string& path, HalManifest* out,
246 std::string* error) {
247 HalManifest ret;
248 status_t status = ret.fetchAllInformation(path, error);
249 if (status == OK) {
250 *out = std::move(ret);
251 }
252 return status;
253}
254
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800255status_t VintfObject::FetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
256 CompatibilityMatrix etcMatrix;
257 if (etcMatrix.fetchAllInformation(kVendorMatrix, error) == OK) {
258 *out = std::move(etcMatrix);
259 return OK;
260 }
261 return out->fetchAllInformation(kVendorLegacyMatrix, error);
262}
263
Yifan Hongde6d00e2018-02-27 17:11:28 -0800264status_t VintfObject::FetchFrameworkHalManifest(HalManifest* out, std::string* error) {
265 HalManifest etcManifest;
266 if (etcManifest.fetchAllInformation(kSystemManifest, error) == OK) {
267 *out = std::move(etcManifest);
268 return OK;
269 }
270 return out->fetchAllInformation(kSystemLegacyManifest, error);
271}
272
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800273std::vector<Named<CompatibilityMatrix>> VintfObject::GetAllFrameworkMatrixLevels(
274 std::string* error) {
275 std::vector<std::string> fileNames;
276 std::vector<Named<CompatibilityMatrix>> results;
277
Yifan Hong270b5652018-01-18 18:46:01 -0800278 if (details::gFetcher->listFiles(kSystemVintfDir, &fileNames, error) != OK) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800279 return {};
280 }
281 for (const std::string& fileName : fileNames) {
Yifan Hong270b5652018-01-18 18:46:01 -0800282 std::string path = kSystemVintfDir + fileName;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800283
284 std::string content;
285 std::string fetchError;
286 status_t status = details::gFetcher->fetch(path, content, &fetchError);
287 if (status != OK) {
288 if (error) {
289 *error += "Ignore file " + path + ": " + fetchError + "\n";
290 }
291 continue;
292 }
293
294 auto it = results.emplace(results.end());
Yifan Hong94757062018-02-09 16:36:31 -0800295 if (!gCompatibilityMatrixConverter(&it->object, content, error)) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800296 if (error) {
Yifan Hong94757062018-02-09 16:36:31 -0800297 *error += "Ignore file " + path + ": " + *error + "\n";
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800298 }
299 results.erase(it);
300 continue;
301 }
302 }
303
304 if (results.empty()) {
305 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800306 *error = "No framework matrices under " + kSystemVintfDir +
307 " can be fetched or parsed.\n" + *error;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800308 }
309 } else {
310 if (error && !error->empty()) {
311 LOG(WARNING) << *error;
312 *error = "";
313 }
314 }
315
316 return results;
317}
318
Yifan Hong3daec812017-02-27 18:49:11 -0800319// static
Yifan Hong1fb004e2017-09-26 15:04:44 -0700320std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
321 RuntimeInfo::FetchFlags flags) {
322 static LockedRuntimeInfoCache gDeviceRuntimeInfo;
323 std::unique_lock<std::mutex> _lock(gDeviceRuntimeInfo.mutex);
324
325 if (!skipCache) {
326 flags &= (~gDeviceRuntimeInfo.fetchedFlags);
327 }
328
329 if (gDeviceRuntimeInfo.object == nullptr) {
Yifan Hong29bb2d42017-09-27 13:28:00 -0700330 gDeviceRuntimeInfo.object = details::gRuntimeInfoFactory->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700331 }
332
333 status_t status = gDeviceRuntimeInfo.object->fetchAllInformation(flags);
334 if (status != OK) {
335 gDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
336 return nullptr;
337 }
338
339 gDeviceRuntimeInfo.fetchedFlags |= flags;
340 return gDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800341}
342
Yifan Hong143cfe62017-04-13 20:18:01 -0700343namespace details {
344
Yifan Hong143cfe62017-04-13 20:18:01 -0700345enum class ParseStatus {
346 OK,
347 PARSE_ERROR,
348 DUPLICATED_FWK_ENTRY,
349 DUPLICATED_DEV_ENTRY,
350};
351
Yifan Hong9532bd22017-04-14 15:30:52 -0700352static std::string toString(ParseStatus status) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700353 switch(status) {
354 case ParseStatus::OK: return "OK";
355 case ParseStatus::PARSE_ERROR: return "parse error";
356 case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework";
357 case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device";
358 }
359 return "";
360}
361
362template<typename T>
Yifan Hong9532bd22017-04-14 15:30:52 -0700363static ParseStatus tryParse(const std::string &xml, const XmlConverter<T> &parse,
Yifan Hongfc73edf2017-08-29 11:39:07 -0700364 std::shared_ptr<T> *fwk, std::shared_ptr<T> *dev) {
365 std::shared_ptr<T> ret = std::make_shared<T>();
Yifan Hong94757062018-02-09 16:36:31 -0800366 if (!parse(ret.get(), xml, nullptr /* error */)) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700367 return ParseStatus::PARSE_ERROR;
368 }
369 if (ret->type() == SchemaType::FRAMEWORK) {
370 if (fwk->get() != nullptr) {
371 return ParseStatus::DUPLICATED_FWK_ENTRY;
372 }
373 *fwk = std::move(ret);
374 } else if (ret->type() == SchemaType::DEVICE) {
375 if (dev->get() != nullptr) {
376 return ParseStatus::DUPLICATED_DEV_ENTRY;
377 }
378 *dev = std::move(ret);
379 }
380 return ParseStatus::OK;
381}
382
383template<typename T, typename GetFunction>
Yifan Hongfc73edf2017-08-29 11:39:07 -0700384static status_t getMissing(const std::shared_ptr<T>& pkg, bool mount,
Yifan Hong143cfe62017-04-13 20:18:01 -0700385 std::function<status_t(void)> mountFunction,
Yifan Hongfc73edf2017-08-29 11:39:07 -0700386 std::shared_ptr<const T>* updated,
Yifan Hong143cfe62017-04-13 20:18:01 -0700387 GetFunction getFunction) {
388 if (pkg != nullptr) {
389 *updated = pkg;
390 } else {
391 if (mount) {
392 (void)mountFunction(); // ignore mount errors
393 }
394 *updated = getFunction();
395 }
396 return OK;
397}
398
399#define ADD_MESSAGE(__error__) \
400 if (error != nullptr) { \
401 *error += (__error__); \
402 } \
403
404struct PackageInfo {
405 struct Pair {
Yifan Hongfc73edf2017-08-29 11:39:07 -0700406 std::shared_ptr<HalManifest> manifest;
407 std::shared_ptr<CompatibilityMatrix> matrix;
Yifan Hong143cfe62017-04-13 20:18:01 -0700408 };
409 Pair dev;
410 Pair fwk;
411};
412
413struct UpdatedInfo {
414 struct Pair {
Yifan Hongfc73edf2017-08-29 11:39:07 -0700415 std::shared_ptr<const HalManifest> manifest;
416 std::shared_ptr<const CompatibilityMatrix> matrix;
Yifan Hong143cfe62017-04-13 20:18:01 -0700417 };
418 Pair dev;
419 Pair fwk;
Yifan Hongfc73edf2017-08-29 11:39:07 -0700420 std::shared_ptr<const RuntimeInfo> runtimeInfo;
Yifan Hong143cfe62017-04-13 20:18:01 -0700421};
422
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700423// Checks given compatibility info against info on the device. If no
424// compatability info is given then the device info will be checked against
425// itself.
Yifan Hongdb6423e2017-09-11 14:38:46 -0700426int32_t checkCompatibility(const std::vector<std::string>& xmls, bool mount,
427 const PartitionMounter& mounter, std::string* error,
428 DisabledChecks disabledChecks) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700429 status_t status;
430 ParseStatus parseStatus;
431 PackageInfo pkg; // All information from package.
432 UpdatedInfo updated; // All files and runtime info after the update.
433
Yifan Hong143cfe62017-04-13 20:18:01 -0700434 // parse all information from package
435 for (const auto &xml : xmls) {
436 parseStatus = tryParse(xml, gHalManifestConverter, &pkg.fwk.manifest, &pkg.dev.manifest);
437 if (parseStatus == ParseStatus::OK) {
438 continue; // work on next one
439 }
440 if (parseStatus != ParseStatus::PARSE_ERROR) {
441 ADD_MESSAGE(toString(parseStatus) + " manifest");
442 return ALREADY_EXISTS;
443 }
444 parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &pkg.fwk.matrix, &pkg.dev.matrix);
445 if (parseStatus == ParseStatus::OK) {
446 continue; // work on next one
447 }
448 if (parseStatus != ParseStatus::PARSE_ERROR) {
449 ADD_MESSAGE(toString(parseStatus) + " matrix");
450 return ALREADY_EXISTS;
451 }
452 ADD_MESSAGE(toString(parseStatus)); // parse error
453 return BAD_VALUE;
454 }
455
456 // get missing info from device
Yifan Hong8640cd12017-05-17 12:02:28 -0700457 // use functions instead of std::bind because std::bind doesn't work well with mock objects
458 auto mountSystem = [&mounter] { return mounter.mountSystem(); };
459 auto mountVendor = [&mounter] { return mounter.mountVendor(); };
460 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700461 pkg.fwk.manifest, mount, mountSystem, &updated.fwk.manifest,
Yifan Hong8640cd12017-05-17 12:02:28 -0700462 std::bind(VintfObject::GetFrameworkHalManifest, true /* skipCache */))) != OK) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700463 return status;
464 }
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700465 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700466 pkg.dev.manifest, mount, mountVendor, &updated.dev.manifest,
Yifan Hong8640cd12017-05-17 12:02:28 -0700467 std::bind(VintfObject::GetDeviceHalManifest, true /* skipCache */))) != OK) {
468 return status;
469 }
470 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700471 pkg.fwk.matrix, mount, mountSystem, &updated.fwk.matrix,
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700472 std::bind(VintfObject::GetFrameworkCompatibilityMatrix, true /* skipCache */))) !=
473 OK) {
474 return status;
475 }
476 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700477 pkg.dev.matrix, mount, mountVendor, &updated.dev.matrix,
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700478 std::bind(VintfObject::GetDeviceCompatibilityMatrix, true /* skipCache */))) != OK) {
479 return status;
480 }
Yifan Hong8640cd12017-05-17 12:02:28 -0700481
482 if (mount) {
483 (void)mounter.umountSystem(); // ignore errors
484 (void)mounter.umountVendor(); // ignore errors
485 }
486
487 updated.runtimeInfo = VintfObject::GetRuntimeInfo(true /* skipCache */);
Yifan Hong143cfe62017-04-13 20:18:01 -0700488
489 // null checks for files and runtime info after the update
Yifan Hong143cfe62017-04-13 20:18:01 -0700490 if (updated.fwk.manifest == nullptr) {
491 ADD_MESSAGE("No framework manifest file from device or from update package");
492 return NO_INIT;
493 }
494 if (updated.dev.manifest == nullptr) {
495 ADD_MESSAGE("No device manifest file from device or from update package");
496 return NO_INIT;
497 }
498 if (updated.fwk.matrix == nullptr) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800499 ADD_MESSAGE("No framework matrix file from device or from update package");
500 return NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700501 }
502 if (updated.dev.matrix == nullptr) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800503 ADD_MESSAGE("No device matrix file from device or from update package");
504 return NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700505 }
506 if (updated.runtimeInfo == nullptr) {
507 ADD_MESSAGE("No runtime info from device");
508 return NO_INIT;
509 }
510
511 // compatiblity check.
Yifan Hongca386fe2018-02-07 11:29:03 -0800512 if (!updated.dev.manifest->checkCompatibility(*updated.fwk.matrix, error)) {
513 if (error) {
514 error->insert(0,
515 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700516 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800517 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700518 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800519 if (!updated.fwk.manifest->checkCompatibility(*updated.dev.matrix, error)) {
520 if (error) {
521 error->insert(0,
522 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700523 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800524 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700525 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800526 if (!updated.runtimeInfo->checkCompatibility(*updated.fwk.matrix, error, disabledChecks)) {
527 if (error) {
528 error->insert(0, "Runtime info and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700529 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800530 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700531 }
532
533 return COMPATIBLE;
534}
535
Yifan Hong270b5652018-01-18 18:46:01 -0800536const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800537const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800538const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800539
540const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800541const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800542const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800543const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800544
545const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
546const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
Yifan Hongde6d00e2018-02-27 17:11:28 -0800547const std::string kSystemLegacyManifest = "/system/manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800548const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
549const std::string kOdmLegacyVintfDir = "/odm/etc/";
550const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
551
Yifan Hong143cfe62017-04-13 20:18:01 -0700552} // namespace details
553
Yifan Hongfbbf0472017-04-07 18:14:18 -0700554// static
Yifan Hongdb6423e2017-09-11 14:38:46 -0700555int32_t VintfObject::CheckCompatibility(const std::vector<std::string>& xmls, std::string* error,
556 DisabledChecks disabledChecks) {
557 return details::checkCompatibility(xmls, false /* mount */, *details::gPartitionMounter, error,
558 disabledChecks);
Yifan Hongfbbf0472017-04-07 18:14:18 -0700559}
560
Yifan Hongf73ba512018-01-17 15:52:30 -0800561bool VintfObject::isHalDeprecated(const MatrixHal& oldMatrixHal,
562 const CompatibilityMatrix& targetMatrix,
563 const IsInstanceInUse& isInstanceInUse, std::string* error) {
564 for (const VersionRange& range : oldMatrixHal.versionRanges) {
565 for (const HalInterface& interface : iterateValues(oldMatrixHal.interfaces)) {
566 for (const std::string& instance : interface.instances) {
567 if (isInstanceDeprecated(oldMatrixHal.name, range.minVer(), interface.name,
568 instance, targetMatrix, isInstanceInUse, error)) {
569 return true;
570 }
571 }
572 }
573 }
574 return false;
575}
576
577// If isInstanceInUse(package@x.y::interface/instance), return true iff:
578// 1. package@x.?::interface/instance is not in targetMatrix; OR
579// 2. package@x.z::interface/instance is in targetMatrix but
580// !isInstanceInUse(package@x.z::interface/instance)
581bool VintfObject::isInstanceDeprecated(const std::string& package, Version version,
582 const std::string& interface, const std::string& instance,
583 const CompatibilityMatrix& targetMatrix,
584 const IsInstanceInUse& isInstanceInUse,
585 std::string* error) {
586 bool oldVersionIsServed;
587 Version servedVersion;
588 std::tie(oldVersionIsServed, servedVersion) =
589 isInstanceInUse(package, version, interface, instance);
590 if (oldVersionIsServed) {
591 // Find any package@x.? in target matrix, and check if instance is in target matrix.
592 const MatrixHal* targetMatrixHal;
593 const VersionRange* targetMatrixRange;
594 std::tie(targetMatrixHal, targetMatrixRange) =
595 targetMatrix.getHalWithMajorVersion(package, version.majorVer);
596 if (targetMatrixHal == nullptr || targetMatrixRange == nullptr) {
597 if (error) {
Yifan Hongb6c7f492018-02-27 14:07:57 -0800598 *error = toFQNameString(package, servedVersion) +
Yifan Hongf73ba512018-01-17 15:52:30 -0800599 "is deprecated in compatibility matrix at FCM Version " +
600 to_string(targetMatrix.level()) + "; it should not be served.";
601 }
602 return true;
603 }
604
605 const auto& targetMatrixInstances = targetMatrixHal->getInstances(interface);
606 if (targetMatrixInstances.find(instance) == targetMatrixInstances.end()) {
607 if (error) {
Yifan Hongb6c7f492018-02-27 14:07:57 -0800608 *error += toFQNameString(package, servedVersion, interface, instance) +
609 " is deprecated at FCM version " + to_string(targetMatrix.level()) +
610 "; it should be not be served.\n";
Yifan Hongf73ba512018-01-17 15:52:30 -0800611 }
612 return true;
613 }
614
615 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
616 bool targetVersionServed;
617 std::tie(targetVersionServed, std::ignore) =
618 isInstanceInUse(package, targetMatrixRange->minVer(), interface, instance);
619
620 if (!targetVersionServed) {
621 if (error) {
Yifan Hongb6c7f492018-02-27 14:07:57 -0800622 *error += toFQNameString(package, servedVersion) + " is deprecated; " +
Yifan Hongf73ba512018-01-17 15:52:30 -0800623 "require at least " + to_string(targetMatrixRange->minVer()) + "\n";
624 }
625 return true;
626 }
627 }
628 return false;
629}
630
631int32_t VintfObject::CheckDeprecation(const IsInstanceInUse& isInstanceInUse,
632 std::string* error) {
633 auto matrixFragments = GetAllFrameworkMatrixLevels(error);
634 if (matrixFragments.empty()) {
635 if (error && error->empty())
636 *error = "Cannot get framework matrix for each FCM version for unknown error.";
637 return NAME_NOT_FOUND;
638 }
639 auto deviceManifest = GetDeviceHalManifest();
640 if (deviceManifest == nullptr) {
641 if (error) *error = "No device manifest.";
642 return NAME_NOT_FOUND;
643 }
644 Level deviceLevel = deviceManifest->level();
645 if (deviceLevel == Level::UNSPECIFIED) {
646 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
647 return BAD_VALUE;
648 }
649
650 const CompatibilityMatrix* targetMatrix = nullptr;
651 for (const auto& namedMatrix : matrixFragments) {
652 if (namedMatrix.object.level() == deviceLevel) {
653 targetMatrix = &namedMatrix.object;
654 }
655 }
656 if (targetMatrix == nullptr) {
657 if (error)
658 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
659 return NAME_NOT_FOUND;
660 }
661
662 bool hasDeprecatedHals = false;
663 for (const auto& namedMatrix : matrixFragments) {
664 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
665 if (namedMatrix.object.level() >= deviceLevel) continue;
666
667 const auto& oldMatrix = namedMatrix.object;
668 for (const MatrixHal& hal : oldMatrix.getHals()) {
669 hasDeprecatedHals |= isHalDeprecated(hal, *targetMatrix, isInstanceInUse, error);
670 }
671 }
672
673 return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS;
674}
675
676int32_t VintfObject::CheckDeprecation(std::string* error) {
677 auto deviceManifest = GetDeviceHalManifest();
678 IsInstanceInUse inManifest = [&deviceManifest](const std::string& package, Version version,
679 const std::string& interface,
680 const std::string& instance) {
681 const ManifestHal* hal = deviceManifest->getHal(package, version);
682 if (hal == nullptr) {
683 return std::make_pair(false, Version{});
684 }
685 const auto& instances = hal->getInstances(interface);
686 if (instances.find(instance) == instances.end()) {
687 return std::make_pair(false, Version{});
688 }
689
690 for (Version v : hal->versions) {
691 if (v.minorAtLeast(version)) {
692 return std::make_pair(true, v);
693 }
694 }
695 return std::make_pair(false, Version{});
696 };
697 return CheckDeprecation(inManifest, error);
698}
Yifan Hong3daec812017-02-27 18:49:11 -0800699
700} // namespace vintf
701} // namespace android