blob: e89ca565a59ae1f18b911881090713793dc5158a [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());
Yifan Hong94757062018-02-09 16:36:31 -0800298 if (!gCompatibilityMatrixConverter(&it->object, content, error)) {
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800299 if (error) {
Yifan Hong94757062018-02-09 16:36:31 -0800300 *error += "Ignore file " + path + ": " + *error + "\n";
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800301 }
302 results.erase(it);
303 continue;
304 }
305 }
306
307 if (results.empty()) {
308 if (error) {
Yifan Hong270b5652018-01-18 18:46:01 -0800309 *error = "No framework matrices under " + kSystemVintfDir +
310 " can be fetched or parsed.\n" + *error;
Yifan Hongd52bf3e2018-01-11 16:56:51 -0800311 }
312 } else {
313 if (error && !error->empty()) {
314 LOG(WARNING) << *error;
315 *error = "";
316 }
317 }
318
319 return results;
320}
321
Yifan Hong3daec812017-02-27 18:49:11 -0800322// static
Yifan Hong1fb004e2017-09-26 15:04:44 -0700323std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
324 RuntimeInfo::FetchFlags flags) {
325 static LockedRuntimeInfoCache gDeviceRuntimeInfo;
326 std::unique_lock<std::mutex> _lock(gDeviceRuntimeInfo.mutex);
327
328 if (!skipCache) {
329 flags &= (~gDeviceRuntimeInfo.fetchedFlags);
330 }
331
332 if (gDeviceRuntimeInfo.object == nullptr) {
Yifan Hong29bb2d42017-09-27 13:28:00 -0700333 gDeviceRuntimeInfo.object = details::gRuntimeInfoFactory->make_shared();
Yifan Hong1fb004e2017-09-26 15:04:44 -0700334 }
335
336 status_t status = gDeviceRuntimeInfo.object->fetchAllInformation(flags);
337 if (status != OK) {
338 gDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
339 return nullptr;
340 }
341
342 gDeviceRuntimeInfo.fetchedFlags |= flags;
343 return gDeviceRuntimeInfo.object;
Yifan Hong3daec812017-02-27 18:49:11 -0800344}
345
Yifan Hong143cfe62017-04-13 20:18:01 -0700346namespace details {
347
Yifan Hong143cfe62017-04-13 20:18:01 -0700348enum class ParseStatus {
349 OK,
350 PARSE_ERROR,
351 DUPLICATED_FWK_ENTRY,
352 DUPLICATED_DEV_ENTRY,
353};
354
Yifan Hong9532bd22017-04-14 15:30:52 -0700355static std::string toString(ParseStatus status) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700356 switch(status) {
357 case ParseStatus::OK: return "OK";
358 case ParseStatus::PARSE_ERROR: return "parse error";
359 case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework";
360 case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device";
361 }
362 return "";
363}
364
365template<typename T>
Yifan Hong9532bd22017-04-14 15:30:52 -0700366static ParseStatus tryParse(const std::string &xml, const XmlConverter<T> &parse,
Yifan Hongfc73edf2017-08-29 11:39:07 -0700367 std::shared_ptr<T> *fwk, std::shared_ptr<T> *dev) {
368 std::shared_ptr<T> ret = std::make_shared<T>();
Yifan Hong94757062018-02-09 16:36:31 -0800369 if (!parse(ret.get(), xml, nullptr /* error */)) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700370 return ParseStatus::PARSE_ERROR;
371 }
372 if (ret->type() == SchemaType::FRAMEWORK) {
373 if (fwk->get() != nullptr) {
374 return ParseStatus::DUPLICATED_FWK_ENTRY;
375 }
376 *fwk = std::move(ret);
377 } else if (ret->type() == SchemaType::DEVICE) {
378 if (dev->get() != nullptr) {
379 return ParseStatus::DUPLICATED_DEV_ENTRY;
380 }
381 *dev = std::move(ret);
382 }
383 return ParseStatus::OK;
384}
385
386template<typename T, typename GetFunction>
Yifan Hongfc73edf2017-08-29 11:39:07 -0700387static status_t getMissing(const std::shared_ptr<T>& pkg, bool mount,
Yifan Hong143cfe62017-04-13 20:18:01 -0700388 std::function<status_t(void)> mountFunction,
Yifan Hongfc73edf2017-08-29 11:39:07 -0700389 std::shared_ptr<const T>* updated,
Yifan Hong143cfe62017-04-13 20:18:01 -0700390 GetFunction getFunction) {
391 if (pkg != nullptr) {
392 *updated = pkg;
393 } else {
394 if (mount) {
395 (void)mountFunction(); // ignore mount errors
396 }
397 *updated = getFunction();
398 }
399 return OK;
400}
401
402#define ADD_MESSAGE(__error__) \
403 if (error != nullptr) { \
404 *error += (__error__); \
405 } \
406
407struct PackageInfo {
408 struct Pair {
Yifan Hongfc73edf2017-08-29 11:39:07 -0700409 std::shared_ptr<HalManifest> manifest;
410 std::shared_ptr<CompatibilityMatrix> matrix;
Yifan Hong143cfe62017-04-13 20:18:01 -0700411 };
412 Pair dev;
413 Pair fwk;
414};
415
416struct UpdatedInfo {
417 struct Pair {
Yifan Hongfc73edf2017-08-29 11:39:07 -0700418 std::shared_ptr<const HalManifest> manifest;
419 std::shared_ptr<const CompatibilityMatrix> matrix;
Yifan Hong143cfe62017-04-13 20:18:01 -0700420 };
421 Pair dev;
422 Pair fwk;
Yifan Hongfc73edf2017-08-29 11:39:07 -0700423 std::shared_ptr<const RuntimeInfo> runtimeInfo;
Yifan Hong143cfe62017-04-13 20:18:01 -0700424};
425
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700426// Checks given compatibility info against info on the device. If no
427// compatability info is given then the device info will be checked against
428// itself.
Yifan Hongdb6423e2017-09-11 14:38:46 -0700429int32_t checkCompatibility(const std::vector<std::string>& xmls, bool mount,
430 const PartitionMounter& mounter, std::string* error,
431 DisabledChecks disabledChecks) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700432 status_t status;
433 ParseStatus parseStatus;
434 PackageInfo pkg; // All information from package.
435 UpdatedInfo updated; // All files and runtime info after the update.
436
Yifan Hong143cfe62017-04-13 20:18:01 -0700437 // parse all information from package
438 for (const auto &xml : xmls) {
439 parseStatus = tryParse(xml, gHalManifestConverter, &pkg.fwk.manifest, &pkg.dev.manifest);
440 if (parseStatus == ParseStatus::OK) {
441 continue; // work on next one
442 }
443 if (parseStatus != ParseStatus::PARSE_ERROR) {
444 ADD_MESSAGE(toString(parseStatus) + " manifest");
445 return ALREADY_EXISTS;
446 }
447 parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &pkg.fwk.matrix, &pkg.dev.matrix);
448 if (parseStatus == ParseStatus::OK) {
449 continue; // work on next one
450 }
451 if (parseStatus != ParseStatus::PARSE_ERROR) {
452 ADD_MESSAGE(toString(parseStatus) + " matrix");
453 return ALREADY_EXISTS;
454 }
455 ADD_MESSAGE(toString(parseStatus)); // parse error
456 return BAD_VALUE;
457 }
458
459 // get missing info from device
Yifan Hong8640cd12017-05-17 12:02:28 -0700460 // use functions instead of std::bind because std::bind doesn't work well with mock objects
461 auto mountSystem = [&mounter] { return mounter.mountSystem(); };
462 auto mountVendor = [&mounter] { return mounter.mountVendor(); };
463 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700464 pkg.fwk.manifest, mount, mountSystem, &updated.fwk.manifest,
Yifan Hong8640cd12017-05-17 12:02:28 -0700465 std::bind(VintfObject::GetFrameworkHalManifest, true /* skipCache */))) != OK) {
Yifan Hong143cfe62017-04-13 20:18:01 -0700466 return status;
467 }
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700468 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700469 pkg.dev.manifest, mount, mountVendor, &updated.dev.manifest,
Yifan Hong8640cd12017-05-17 12:02:28 -0700470 std::bind(VintfObject::GetDeviceHalManifest, true /* skipCache */))) != OK) {
471 return status;
472 }
473 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700474 pkg.fwk.matrix, mount, mountSystem, &updated.fwk.matrix,
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700475 std::bind(VintfObject::GetFrameworkCompatibilityMatrix, true /* skipCache */))) !=
476 OK) {
477 return status;
478 }
479 if ((status = getMissing(
Yifan Hongfc73edf2017-08-29 11:39:07 -0700480 pkg.dev.matrix, mount, mountVendor, &updated.dev.matrix,
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700481 std::bind(VintfObject::GetDeviceCompatibilityMatrix, true /* skipCache */))) != OK) {
482 return status;
483 }
Yifan Hong8640cd12017-05-17 12:02:28 -0700484
485 if (mount) {
486 (void)mounter.umountSystem(); // ignore errors
487 (void)mounter.umountVendor(); // ignore errors
488 }
489
490 updated.runtimeInfo = VintfObject::GetRuntimeInfo(true /* skipCache */);
Yifan Hong143cfe62017-04-13 20:18:01 -0700491
492 // null checks for files and runtime info after the update
Yifan Hong143cfe62017-04-13 20:18:01 -0700493 if (updated.fwk.manifest == nullptr) {
494 ADD_MESSAGE("No framework manifest file from device or from update package");
495 return NO_INIT;
496 }
497 if (updated.dev.manifest == nullptr) {
498 ADD_MESSAGE("No device manifest file from device or from update package");
499 return NO_INIT;
500 }
501 if (updated.fwk.matrix == nullptr) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800502 ADD_MESSAGE("No framework matrix file from device or from update package");
503 return NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700504 }
505 if (updated.dev.matrix == nullptr) {
Yifan Hongca386fe2018-02-07 11:29:03 -0800506 ADD_MESSAGE("No device matrix file from device or from update package");
507 return NO_INIT;
Yifan Hong143cfe62017-04-13 20:18:01 -0700508 }
509 if (updated.runtimeInfo == nullptr) {
510 ADD_MESSAGE("No runtime info from device");
511 return NO_INIT;
512 }
513
514 // compatiblity check.
Yifan Hongca386fe2018-02-07 11:29:03 -0800515 if (!updated.dev.manifest->checkCompatibility(*updated.fwk.matrix, error)) {
516 if (error) {
517 error->insert(0,
518 "Device manifest and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700519 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800520 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700521 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800522 if (!updated.fwk.manifest->checkCompatibility(*updated.dev.matrix, error)) {
523 if (error) {
524 error->insert(0,
525 "Framework manifest and device compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700526 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800527 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700528 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800529 if (!updated.runtimeInfo->checkCompatibility(*updated.fwk.matrix, error, disabledChecks)) {
530 if (error) {
531 error->insert(0, "Runtime info and framework compatibility matrix are incompatible: ");
Yifan Hong143cfe62017-04-13 20:18:01 -0700532 }
Yifan Hongca386fe2018-02-07 11:29:03 -0800533 return INCOMPATIBLE;
Yifan Hong143cfe62017-04-13 20:18:01 -0700534 }
535
536 return COMPATIBLE;
537}
538
Yifan Hong270b5652018-01-18 18:46:01 -0800539const std::string kSystemVintfDir = "/system/etc/vintf/";
Yifan Hongccbea052018-01-18 18:48:46 -0800540const std::string kVendorVintfDir = "/vendor/etc/vintf/";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800541const std::string kOdmVintfDir = "/odm/etc/vintf/";
Yifan Hong270b5652018-01-18 18:46:01 -0800542
543const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
Yifan Honga5ddddf2018-01-18 18:50:13 -0800544const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
Yifan Hongb64ec9e2018-01-18 18:57:52 -0800545const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
Yifan Hong12a11ac2018-01-19 13:58:32 -0800546const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
Yifan Hong270b5652018-01-18 18:46:01 -0800547
548const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
549const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
550const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
551const std::string kOdmLegacyVintfDir = "/odm/etc/";
552const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
553
Yifan Hong143cfe62017-04-13 20:18:01 -0700554} // namespace details
555
Yifan Hongfbbf0472017-04-07 18:14:18 -0700556// static
Yifan Hongdb6423e2017-09-11 14:38:46 -0700557int32_t VintfObject::CheckCompatibility(const std::vector<std::string>& xmls, std::string* error,
558 DisabledChecks disabledChecks) {
559 return details::checkCompatibility(xmls, false /* mount */, *details::gPartitionMounter, error,
560 disabledChecks);
Yifan Hongfbbf0472017-04-07 18:14:18 -0700561}
562
Yifan Hongf73ba512018-01-17 15:52:30 -0800563bool VintfObject::isHalDeprecated(const MatrixHal& oldMatrixHal,
564 const CompatibilityMatrix& targetMatrix,
565 const IsInstanceInUse& isInstanceInUse, std::string* error) {
566 for (const VersionRange& range : oldMatrixHal.versionRanges) {
567 for (const HalInterface& interface : iterateValues(oldMatrixHal.interfaces)) {
568 for (const std::string& instance : interface.instances) {
569 if (isInstanceDeprecated(oldMatrixHal.name, range.minVer(), interface.name,
570 instance, targetMatrix, isInstanceInUse, error)) {
571 return true;
572 }
573 }
574 }
575 }
576 return false;
577}
578
579// If isInstanceInUse(package@x.y::interface/instance), return true iff:
580// 1. package@x.?::interface/instance is not in targetMatrix; OR
581// 2. package@x.z::interface/instance is in targetMatrix but
582// !isInstanceInUse(package@x.z::interface/instance)
583bool VintfObject::isInstanceDeprecated(const std::string& package, Version version,
584 const std::string& interface, const std::string& instance,
585 const CompatibilityMatrix& targetMatrix,
586 const IsInstanceInUse& isInstanceInUse,
587 std::string* error) {
588 bool oldVersionIsServed;
589 Version servedVersion;
590 std::tie(oldVersionIsServed, servedVersion) =
591 isInstanceInUse(package, version, interface, instance);
592 if (oldVersionIsServed) {
593 // Find any package@x.? in target matrix, and check if instance is in target matrix.
594 const MatrixHal* targetMatrixHal;
595 const VersionRange* targetMatrixRange;
596 std::tie(targetMatrixHal, targetMatrixRange) =
597 targetMatrix.getHalWithMajorVersion(package, version.majorVer);
598 if (targetMatrixHal == nullptr || targetMatrixRange == nullptr) {
599 if (error) {
600 *error = package + "@" + to_string(servedVersion) +
601 "is deprecated in compatibility matrix at FCM Version " +
602 to_string(targetMatrix.level()) + "; it should not be served.";
603 }
604 return true;
605 }
606
607 const auto& targetMatrixInstances = targetMatrixHal->getInstances(interface);
608 if (targetMatrixInstances.find(instance) == targetMatrixInstances.end()) {
609 if (error) {
610 *error += package + "@" + to_string(servedVersion) + "::" + interface + "/" +
611 instance + " is deprecated at FCM version " +
612 to_string(targetMatrix.level()) + "; it should be not be served.\n";
613 }
614 return true;
615 }
616
617 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
618 bool targetVersionServed;
619 std::tie(targetVersionServed, std::ignore) =
620 isInstanceInUse(package, targetMatrixRange->minVer(), interface, instance);
621
622 if (!targetVersionServed) {
623 if (error) {
624 *error += package + "@" + to_string(servedVersion) + " is deprecated; " +
625 "require at least " + to_string(targetMatrixRange->minVer()) + "\n";
626 }
627 return true;
628 }
629 }
630 return false;
631}
632
633int32_t VintfObject::CheckDeprecation(const IsInstanceInUse& isInstanceInUse,
634 std::string* error) {
635 auto matrixFragments = GetAllFrameworkMatrixLevels(error);
636 if (matrixFragments.empty()) {
637 if (error && error->empty())
638 *error = "Cannot get framework matrix for each FCM version for unknown error.";
639 return NAME_NOT_FOUND;
640 }
641 auto deviceManifest = GetDeviceHalManifest();
642 if (deviceManifest == nullptr) {
643 if (error) *error = "No device manifest.";
644 return NAME_NOT_FOUND;
645 }
646 Level deviceLevel = deviceManifest->level();
647 if (deviceLevel == Level::UNSPECIFIED) {
648 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
649 return BAD_VALUE;
650 }
651
652 const CompatibilityMatrix* targetMatrix = nullptr;
653 for (const auto& namedMatrix : matrixFragments) {
654 if (namedMatrix.object.level() == deviceLevel) {
655 targetMatrix = &namedMatrix.object;
656 }
657 }
658 if (targetMatrix == nullptr) {
659 if (error)
660 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
661 return NAME_NOT_FOUND;
662 }
663
664 bool hasDeprecatedHals = false;
665 for (const auto& namedMatrix : matrixFragments) {
666 if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
667 if (namedMatrix.object.level() >= deviceLevel) continue;
668
669 const auto& oldMatrix = namedMatrix.object;
670 for (const MatrixHal& hal : oldMatrix.getHals()) {
671 hasDeprecatedHals |= isHalDeprecated(hal, *targetMatrix, isInstanceInUse, error);
672 }
673 }
674
675 return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS;
676}
677
678int32_t VintfObject::CheckDeprecation(std::string* error) {
679 auto deviceManifest = GetDeviceHalManifest();
680 IsInstanceInUse inManifest = [&deviceManifest](const std::string& package, Version version,
681 const std::string& interface,
682 const std::string& instance) {
683 const ManifestHal* hal = deviceManifest->getHal(package, version);
684 if (hal == nullptr) {
685 return std::make_pair(false, Version{});
686 }
687 const auto& instances = hal->getInstances(interface);
688 if (instances.find(instance) == instances.end()) {
689 return std::make_pair(false, Version{});
690 }
691
692 for (Version v : hal->versions) {
693 if (v.minorAtLeast(version)) {
694 return std::make_pair(true, v);
695 }
696 }
697 return std::make_pair(false, Version{});
698 };
699 return CheckDeprecation(inManifest, error);
700}
Yifan Hong3daec812017-02-27 18:49:11 -0800701
702} // namespace vintf
703} // namespace android