1 /*
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
19 #include <dirent.h>
20
21 #include <algorithm>
22 #include <functional>
23 #include <memory>
24 #include <mutex>
25
26 #include <aidl/metadata.h>
27 #include <android-base/logging.h>
28 #include <android-base/result.h>
29 #include <android-base/strings.h>
30 #include <hidl/metadata.h>
31
32 #include "CompatibilityMatrix.h"
33 #include "VintfObjectUtils.h"
34 #include "constants-private.h"
35 #include "parse_string.h"
36 #include "parse_xml.h"
37 #include "utils.h"
38
39 using std::placeholders::_1;
40 using std::placeholders::_2;
41 using std::string_literals::operator""s;
42
43 namespace android {
44 namespace vintf {
45
46 using namespace details;
47
48 #ifdef LIBVINTF_TARGET
49 static constexpr bool kIsTarget = true;
50 #else
51 static constexpr bool kIsTarget = false;
52 #endif
53
createDefaultFileSystem()54 static std::unique_ptr<FileSystem> createDefaultFileSystem() {
55 std::unique_ptr<FileSystem> fileSystem;
56 if (kIsTarget) {
57 fileSystem = std::make_unique<details::FileSystemImpl>();
58 } else {
59 fileSystem = std::make_unique<details::FileSystemNoOp>();
60 }
61 return fileSystem;
62 }
63
createDefaultPropertyFetcher()64 static std::unique_ptr<PropertyFetcher> createDefaultPropertyFetcher() {
65 std::unique_ptr<PropertyFetcher> propertyFetcher;
66 if (kIsTarget) {
67 propertyFetcher = std::make_unique<details::PropertyFetcherImpl>();
68 } else {
69 propertyFetcher = std::make_unique<details::PropertyFetcherNoOp>();
70 }
71 return propertyFetcher;
72 }
73
GetInstance()74 std::shared_ptr<VintfObject> VintfObject::GetInstance() {
75 static details::LockedSharedPtr<VintfObject> sInstance{};
76 std::unique_lock<std::mutex> lock(sInstance.mutex);
77 if (sInstance.object == nullptr) {
78 sInstance.object = std::shared_ptr<VintfObject>(VintfObject::Builder().build().release());
79 }
80 return sInstance.object;
81 }
82
GetDeviceHalManifest()83 std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest() {
84 return GetInstance()->getDeviceHalManifest();
85 }
86
getDeviceHalManifest()87 std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest() {
88 return Get(__func__, &mDeviceManifest,
89 std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2));
90 }
91
GetFrameworkHalManifest()92 std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest() {
93 return GetInstance()->getFrameworkHalManifest();
94 }
95
getFrameworkHalManifest()96 std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest() {
97 return Get(__func__, &mFrameworkManifest,
98 std::bind(&VintfObject::fetchFrameworkHalManifest, this, _1, _2));
99 }
100
GetDeviceCompatibilityMatrix()101 std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix() {
102 return GetInstance()->getDeviceCompatibilityMatrix();
103 }
104
getDeviceCompatibilityMatrix()105 std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix() {
106 return Get(__func__, &mDeviceMatrix, std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2));
107 }
108
GetFrameworkCompatibilityMatrix()109 std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix() {
110 return GetInstance()->getFrameworkCompatibilityMatrix();
111 }
112
getFrameworkCompatibilityMatrix()113 std::shared_ptr<const CompatibilityMatrix> VintfObject::getFrameworkCompatibilityMatrix() {
114 // To avoid deadlock, get device manifest before any locks.
115 auto deviceManifest = getDeviceHalManifest();
116
117 std::string error;
118 auto kernelLevel = getKernelLevel(&error);
119 if (kernelLevel == Level::UNSPECIFIED) {
120 LOG(WARNING) << "getKernelLevel: " << error;
121 }
122
123 std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex);
124
125 auto combined = Get(__func__, &mCombinedFrameworkMatrix,
126 std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest,
127 kernelLevel, _1, _2));
128 if (combined != nullptr) {
129 return combined;
130 }
131
132 return Get(__func__, &mFrameworkMatrix,
133 std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(),
134 kSystemLegacyMatrix, _2));
135 }
136
getCombinedFrameworkMatrix(const std::shared_ptr<const HalManifest> & deviceManifest,Level kernelLevel,CompatibilityMatrix * out,std::string * error)137 status_t VintfObject::getCombinedFrameworkMatrix(
138 const std::shared_ptr<const HalManifest>& deviceManifest, Level kernelLevel,
139 CompatibilityMatrix* out, std::string* error) {
140 std::vector<CompatibilityMatrix> matrixFragments;
141 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
142 if (matrixFragmentsStatus != OK) {
143 return matrixFragmentsStatus;
144 }
145 if (matrixFragments.empty()) {
146 if (error && error->empty()) {
147 *error = "Cannot get framework matrix for each FCM version for unknown error.";
148 }
149 return NAME_NOT_FOUND;
150 }
151
152 Level deviceLevel = Level::UNSPECIFIED;
153
154 if (deviceManifest != nullptr) {
155 deviceLevel = deviceManifest->level();
156 }
157
158 // TODO(b/70628538): Do not infer from Shipping API level.
159 if (deviceLevel == Level::UNSPECIFIED) {
160 auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u);
161 if (shippingApi != 0u) {
162 deviceLevel = details::convertFromApiLevel(shippingApi);
163 }
164 }
165
166 if (deviceLevel == Level::UNSPECIFIED) {
167 // Cannot infer FCM version. Combine all matrices by assuming
168 // Shipping FCM Version == min(all supported FCM Versions in the framework)
169 for (auto&& fragment : matrixFragments) {
170 Level fragmentLevel = fragment.level();
171 if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
172 deviceLevel = fragmentLevel;
173 }
174 }
175 }
176
177 if (deviceLevel == Level::UNSPECIFIED) {
178 // None of the fragments specify any FCM version. Should never happen except
179 // for inconsistent builds.
180 if (error) {
181 *error = "No framework compatibility matrix files under "s + kSystemVintfDir +
182 " declare FCM version.";
183 }
184 return NAME_NOT_FOUND;
185 }
186
187 auto combined = CompatibilityMatrix::combine(deviceLevel, kernelLevel, &matrixFragments, error);
188 if (combined == nullptr) {
189 return BAD_VALUE;
190 }
191 *out = std::move(*combined);
192 return OK;
193 }
194
195 // Load and combine all of the manifests in a directory
196 // If forceSchemaType, all fragment manifests are coerced into manifest->type().
addDirectoryManifests(const std::string & directory,HalManifest * manifest,bool forceSchemaType,std::string * error)197 status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
198 bool forceSchemaType, std::string* error) {
199 std::vector<std::string> fileNames;
200 status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
201 // if the directory isn't there, that's okay
202 if (err == NAME_NOT_FOUND) {
203 if (error) {
204 error->clear();
205 }
206 return OK;
207 }
208 if (err != OK) return err;
209
210 for (const std::string& file : fileNames) {
211 // Only adds HALs because all other things are added by libvintf
212 // itself for now.
213 HalManifest fragmentManifest;
214 err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
215 if (err != OK) return err;
216
217 if (forceSchemaType) {
218 fragmentManifest.setType(manifest->type());
219 }
220
221 if (!manifest->addAll(&fragmentManifest, error)) {
222 if (error) {
223 error->insert(0, "Cannot add manifest fragment " + directory + file + ": ");
224 }
225 return UNKNOWN_ERROR;
226 }
227 }
228
229 return OK;
230 }
231
232 // Priority for loading vendor manifest:
233 // 1. Vendor manifest + device fragments + ODM manifest (optional) + odm fragments
234 // 2. Vendor manifest + device fragments
235 // 3. ODM manifest (optional) + odm fragments
236 // 4. /vendor/manifest.xml (legacy, no fragments)
237 // where:
238 // A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
239 // over A.
fetchDeviceHalManifest(HalManifest * out,std::string * error)240 status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
241 HalManifest vendorManifest;
242 status_t vendorStatus = fetchVendorHalManifest(&vendorManifest, error);
243 if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
244 return vendorStatus;
245 }
246
247 if (vendorStatus == OK) {
248 *out = std::move(vendorManifest);
249 status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out,
250 false /* forceSchemaType*/, error);
251 if (fragmentStatus != OK) {
252 return fragmentStatus;
253 }
254 }
255
256 HalManifest odmManifest;
257 status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
258 if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
259 return odmStatus;
260 }
261
262 if (vendorStatus == OK) {
263 if (odmStatus == OK) {
264 if (!out->addAll(&odmManifest, error)) {
265 if (error) {
266 error->insert(0, "Cannot add ODM manifest :");
267 }
268 return UNKNOWN_ERROR;
269 }
270 }
271 return addDirectoryManifests(kOdmManifestFragmentDir, out, false /* forceSchemaType */,
272 error);
273 }
274
275 // vendorStatus != OK, "out" is not changed.
276 if (odmStatus == OK) {
277 *out = std::move(odmManifest);
278 return addDirectoryManifests(kOdmManifestFragmentDir, out, false /* forceSchemaType */,
279 error);
280 }
281
282 // Use legacy /vendor/manifest.xml
283 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
284 }
285
286 // Priority:
287 // 1. if {vendorSku} is defined, /vendor/etc/vintf/manifest_{vendorSku}.xml
288 // 2. /vendor/etc/vintf/manifest.xml
289 // where:
290 // {vendorSku} is the value of ro.boot.product.vendor.sku
fetchVendorHalManifest(HalManifest * out,std::string * error)291 status_t VintfObject::fetchVendorHalManifest(HalManifest* out, std::string* error) {
292 status_t status;
293
294 std::string vendorSku;
295 vendorSku = getPropertyFetcher()->getProperty("ro.boot.product.vendor.sku", "");
296
297 if (!vendorSku.empty()) {
298 status =
299 fetchOneHalManifest(kVendorVintfDir + "manifest_"s + vendorSku + ".xml", out, error);
300 if (status == OK || status != NAME_NOT_FOUND) {
301 return status;
302 }
303 }
304
305 status = fetchOneHalManifest(kVendorManifest, out, error);
306 if (status == OK || status != NAME_NOT_FOUND) {
307 return status;
308 }
309
310 return NAME_NOT_FOUND;
311 }
312
313 // "out" is written to iff return status is OK.
314 // Priority:
315 // 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
316 // 2. /odm/etc/vintf/manifest.xml
317 // 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
318 // 4. /odm/etc/manifest.xml
319 // where:
320 // {sku} is the value of ro.boot.product.hardware.sku
fetchOdmHalManifest(HalManifest * out,std::string * error)321 status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
322 status_t status;
323
324 std::string productModel;
325 productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
326
327 if (!productModel.empty()) {
328 status =
329 fetchOneHalManifest(kOdmVintfDir + "manifest_"s + productModel + ".xml", out, error);
330 if (status == OK || status != NAME_NOT_FOUND) {
331 return status;
332 }
333 }
334
335 status = fetchOneHalManifest(kOdmManifest, out, error);
336 if (status == OK || status != NAME_NOT_FOUND) {
337 return status;
338 }
339
340 if (!productModel.empty()) {
341 status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_"s + productModel + ".xml", out,
342 error);
343 if (status == OK || status != NAME_NOT_FOUND) {
344 return status;
345 }
346 }
347
348 status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
349 if (status == OK || status != NAME_NOT_FOUND) {
350 return status;
351 }
352
353 return NAME_NOT_FOUND;
354 }
355
356 // Fetch one manifest.xml file. "out" is written to iff return status is OK.
357 // Returns NAME_NOT_FOUND if file is missing.
fetchOneHalManifest(const std::string & path,HalManifest * out,std::string * error)358 status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
359 std::string* error) {
360 HalManifest ret;
361 status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
362 if (status == OK) {
363 *out = std::move(ret);
364 }
365 return status;
366 }
367
fetchDeviceMatrix(CompatibilityMatrix * out,std::string * error)368 status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
369 CompatibilityMatrix etcMatrix;
370 if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
371 *out = std::move(etcMatrix);
372 return OK;
373 }
374 return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
375 }
376
377 // Priority:
378 // 1. /system/etc/vintf/manifest.xml
379 // + /system/etc/vintf/manifest/*.xml if they exist
380 // + /product/etc/vintf/manifest.xml if it exists
381 // + /product/etc/vintf/manifest/*.xml if they exist
382 // 2. (deprecated) /system/manifest.xml
fetchUnfilteredFrameworkHalManifest(HalManifest * out,std::string * error)383 status_t VintfObject::fetchUnfilteredFrameworkHalManifest(HalManifest* out, std::string* error) {
384 auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
385 if (systemEtcStatus == OK) {
386 auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out,
387 false /* forceSchemaType */, error);
388 if (dirStatus != OK) {
389 return dirStatus;
390 }
391
392 std::vector<std::pair<const char*, const char*>> extensions{
393 {kProductManifest, kProductManifestFragmentDir},
394 {kSystemExtManifest, kSystemExtManifestFragmentDir},
395 };
396 for (auto&& [manifestPath, frags] : extensions) {
397 HalManifest halManifest;
398 auto status = fetchOneHalManifest(manifestPath, &halManifest, error);
399 if (status != OK && status != NAME_NOT_FOUND) {
400 return status;
401 }
402 if (status == OK) {
403 if (!out->addAll(&halManifest, error)) {
404 if (error) {
405 error->insert(0, "Cannot add "s + manifestPath + ":");
406 }
407 return UNKNOWN_ERROR;
408 }
409 }
410
411 auto fragmentStatus =
412 addDirectoryManifests(frags, out, false /* forceSchemaType */, error);
413 if (fragmentStatus != OK) {
414 return fragmentStatus;
415 }
416 }
417
418 return OK;
419 } else {
420 LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
421 << (error ? *error : strerror(-systemEtcStatus));
422 }
423
424 return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
425 }
426
fetchFrameworkHalManifest(HalManifest * out,std::string * error)427 status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
428 status_t status = fetchUnfilteredFrameworkHalManifest(out, error);
429 if (status != OK) {
430 return status;
431 }
432 filterHalsByDeviceManifestLevel(out);
433 return OK;
434 }
435
filterHalsByDeviceManifestLevel(HalManifest * out)436 void VintfObject::filterHalsByDeviceManifestLevel(HalManifest* out) {
437 auto deviceManifest = getDeviceHalManifest();
438 if (deviceManifest == nullptr) {
439 LOG(WARNING) << "Cannot fetch device manifest to determine target FCM version to "
440 "filter framework manifest HALs.";
441 return;
442 }
443 Level deviceManifestLevel = deviceManifest->level();
444 if (deviceManifestLevel == Level::UNSPECIFIED) {
445 LOG(WARNING)
446 << "Not filtering framework manifest HALs because target FCM version is unspecified.";
447 return;
448 }
449 out->removeHalsIf([deviceManifestLevel](const ManifestHal& hal) {
450 if (hal.getMaxLevel() == Level::UNSPECIFIED) {
451 return false;
452 }
453 return hal.getMaxLevel() < deviceManifestLevel;
454 });
455 }
456
appendLine(std::string * error,const std::string & message)457 static void appendLine(std::string* error, const std::string& message) {
458 if (error != nullptr) {
459 if (!error->empty()) *error += "\n";
460 *error += message;
461 }
462 }
463
getOneMatrix(const std::string & path,CompatibilityMatrix * out,std::string * error)464 status_t VintfObject::getOneMatrix(const std::string& path, CompatibilityMatrix* out,
465 std::string* error) {
466 std::string content;
467 status_t status = getFileSystem()->fetch(path, &content, error);
468 if (status != OK) {
469 return status;
470 }
471 if (!fromXml(out, content, error)) {
472 if (error) {
473 error->insert(0, "Cannot parse " + path + ": ");
474 }
475 return BAD_VALUE;
476 }
477 out->setFileName(path);
478 return OK;
479 }
480
getAllFrameworkMatrixLevels(std::vector<CompatibilityMatrix> * results,std::string * error)481 status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<CompatibilityMatrix>* results,
482 std::string* error) {
483 std::vector<std::string> dirs = {
484 kSystemVintfDir,
485 kSystemExtVintfDir,
486 kProductVintfDir,
487 };
488 for (const auto& dir : dirs) {
489 std::vector<std::string> fileNames;
490 status_t listStatus = getFileSystem()->listFiles(dir, &fileNames, error);
491 if (listStatus == NAME_NOT_FOUND) {
492 if (error) {
493 error->clear();
494 }
495 continue;
496 }
497 if (listStatus != OK) {
498 return listStatus;
499 }
500 for (const std::string& fileName : fileNames) {
501 std::string path = dir + fileName;
502 CompatibilityMatrix namedMatrix;
503 std::string matrixError;
504 status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
505 if (matrixStatus != OK) {
506 // Manifests and matrices share the same dir. Client may not have enough
507 // permissions to read system manifests, or may not be able to parse it.
508 auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
509 LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
510 continue;
511 }
512 results->emplace_back(std::move(namedMatrix));
513 }
514
515 if (dir == kSystemVintfDir && results->empty()) {
516 if (error) {
517 *error = "No framework matrices under " + dir + " can be fetched or parsed.\n";
518 }
519 return NAME_NOT_FOUND;
520 }
521 }
522
523 if (results->empty()) {
524 if (error) {
525 *error =
526 "No framework matrices can be fetched or parsed. "
527 "The following directories are searched:\n " +
528 android::base::Join(dirs, "\n ");
529 }
530 return NAME_NOT_FOUND;
531 }
532 return OK;
533 }
534
GetRuntimeInfo(RuntimeInfo::FetchFlags flags)535 std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(RuntimeInfo::FetchFlags flags) {
536 return GetInstance()->getRuntimeInfo(flags);
537 }
getRuntimeInfo(RuntimeInfo::FetchFlags flags)538 std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(RuntimeInfo::FetchFlags flags) {
539 std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
540
541 // Skip fetching information that has already been fetched previously.
542 flags &= (~mDeviceRuntimeInfo.fetchedFlags);
543
544 if (mDeviceRuntimeInfo.object == nullptr) {
545 mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
546 }
547
548 status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
549 if (status != OK) {
550 // If only kernel FCM is needed, ignore errors when fetching RuntimeInfo because RuntimeInfo
551 // is not available on host. On host, the kernel level can still be inferred from device
552 // manifest.
553 // If other information is needed, flag the error by returning nullptr.
554 auto allExceptKernelFcm = RuntimeInfo::FetchFlag::ALL & ~RuntimeInfo::FetchFlag::KERNEL_FCM;
555 bool needDeviceRuntimeInfo = flags & allExceptKernelFcm;
556 if (needDeviceRuntimeInfo) {
557 mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
558 return nullptr;
559 }
560 }
561
562 // To support devices without GKI, RuntimeInfo::fetchAllInformation does not report errors
563 // if kernel level cannot be retrieved. If so, fetch kernel FCM version from device HAL
564 // manifest and store it in RuntimeInfo too.
565 if (flags & RuntimeInfo::FetchFlag::KERNEL_FCM) {
566 Level deviceManifestKernelLevel = Level::UNSPECIFIED;
567 auto manifest = getDeviceHalManifest();
568 if (manifest) {
569 deviceManifestKernelLevel = manifest->inferredKernelLevel();
570 }
571 if (deviceManifestKernelLevel != Level::UNSPECIFIED) {
572 Level kernelLevel = mDeviceRuntimeInfo.object->kernelLevel();
573 if (kernelLevel == Level::UNSPECIFIED) {
574 mDeviceRuntimeInfo.object->setKernelLevel(deviceManifestKernelLevel);
575 } else if (kernelLevel != deviceManifestKernelLevel) {
576 LOG(WARNING) << "uname() reports kernel level " << kernelLevel
577 << " but device manifest sets kernel level "
578 << deviceManifestKernelLevel << ". Using kernel level " << kernelLevel;
579 }
580 }
581 }
582
583 mDeviceRuntimeInfo.fetchedFlags |= flags;
584 return mDeviceRuntimeInfo.object;
585 }
586
checkCompatibility(std::string * error,CheckFlags::Type flags)587 int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
588 status_t status = OK;
589 // null checks for files and runtime info
590 if (getFrameworkHalManifest() == nullptr) {
591 appendLine(error, "No framework manifest file from device or from update package");
592 status = NO_INIT;
593 }
594 if (getDeviceHalManifest() == nullptr) {
595 appendLine(error, "No device manifest file from device or from update package");
596 status = NO_INIT;
597 }
598 if (getFrameworkCompatibilityMatrix() == nullptr) {
599 appendLine(error, "No framework matrix file from device or from update package");
600 status = NO_INIT;
601 }
602 if (getDeviceCompatibilityMatrix() == nullptr) {
603 appendLine(error, "No device matrix file from device or from update package");
604 status = NO_INIT;
605 }
606
607 if (flags.isRuntimeInfoEnabled()) {
608 if (getRuntimeInfo() == nullptr) {
609 appendLine(error, "No runtime info from device");
610 status = NO_INIT;
611 }
612 }
613 if (status != OK) return status;
614
615 // compatiblity check.
616 if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
617 if (error) {
618 error->insert(0,
619 "Device manifest and framework compatibility matrix are incompatible: ");
620 }
621 return INCOMPATIBLE;
622 }
623 if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
624 if (error) {
625 error->insert(0,
626 "Framework manifest and device compatibility matrix are incompatible: ");
627 }
628 return INCOMPATIBLE;
629 }
630
631 if (flags.isRuntimeInfoEnabled()) {
632 if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
633 flags)) {
634 if (error) {
635 error->insert(0,
636 "Runtime info and framework compatibility matrix are incompatible: ");
637 }
638 return INCOMPATIBLE;
639 }
640 }
641
642 return COMPATIBLE;
643 }
644
645 namespace details {
646
dumpFileList()647 std::vector<std::string> dumpFileList() {
648 return {
649 // clang-format off
650 kSystemVintfDir,
651 kVendorVintfDir,
652 kOdmVintfDir,
653 kProductVintfDir,
654 kSystemExtVintfDir,
655 kOdmLegacyVintfDir,
656 kVendorLegacyManifest,
657 kVendorLegacyMatrix,
658 kSystemLegacyManifest,
659 kSystemLegacyMatrix,
660 // clang-format on
661 };
662 }
663
664 } // namespace details
665
IsHalDeprecated(const MatrixHal & oldMatrixHal,const CompatibilityMatrix & targetMatrix,const ListInstances & listInstances,const ChildrenMap & childrenMap,std::string * appendedError)666 bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
667 const CompatibilityMatrix& targetMatrix,
668 const ListInstances& listInstances,
669 const ChildrenMap& childrenMap, std::string* appendedError) {
670 bool isDeprecated = false;
671 oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
672 if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, childrenMap,
673 appendedError)) {
674 isDeprecated = true;
675 }
676 return true; // continue to check next instance
677 });
678 return isDeprecated;
679 }
680
681 // Let oldMatrixInstance = package@x.y-w::interface/instancePattern.
682 // If any "@servedVersion::interface/servedInstance" in listInstances(package@x.y::interface)
683 // matches instancePattern, return true iff for all child interfaces (from
684 // GetListedInstanceInheritance), IsFqInstanceDeprecated returns false.
IsInstanceDeprecated(const MatrixInstance & oldMatrixInstance,const CompatibilityMatrix & targetMatrix,const ListInstances & listInstances,const ChildrenMap & childrenMap,std::string * appendedError)685 bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
686 const CompatibilityMatrix& targetMatrix,
687 const ListInstances& listInstances,
688 const ChildrenMap& childrenMap, std::string* appendedError) {
689 const std::string& package = oldMatrixInstance.package();
690 const Version& version = oldMatrixInstance.versionRange().minVer();
691 const std::string& interface = oldMatrixInstance.interface();
692
693 std::vector<std::string> instanceHint;
694 if (!oldMatrixInstance.isRegex()) {
695 instanceHint.push_back(oldMatrixInstance.exactInstance());
696 }
697
698 std::vector<std::string> accumulatedErrors;
699 auto list = listInstances(package, version, interface, instanceHint);
700
701 for (const auto& pair : list) {
702 const std::string& servedInstance = pair.first;
703 Version servedVersion = pair.second;
704 std::string servedFqInstanceString =
705 toFQNameString(package, servedVersion, interface, servedInstance);
706 if (!oldMatrixInstance.matchInstance(servedInstance)) {
707 // ignore unrelated instance
708 continue;
709 }
710
711 auto inheritance = GetListedInstanceInheritance(package, servedVersion, interface,
712 servedInstance, listInstances, childrenMap);
713 if (!inheritance.has_value()) {
714 accumulatedErrors.push_back(inheritance.error().message());
715 continue;
716 }
717
718 std::vector<std::string> errors;
719 for (const auto& fqInstance : *inheritance) {
720 auto result = IsFqInstanceDeprecated(targetMatrix, oldMatrixInstance.format(),
721 fqInstance, listInstances);
722 if (result.ok()) {
723 errors.clear();
724 break;
725 }
726 errors.push_back(result.error().message());
727 }
728
729 if (errors.empty()) {
730 continue;
731 }
732 accumulatedErrors.insert(accumulatedErrors.end(), errors.begin(), errors.end());
733 }
734
735 if (accumulatedErrors.empty()) {
736 return false;
737 }
738 appendLine(appendedError, android::base::Join(accumulatedErrors, "\n"));
739 return true;
740 }
741
742 // Check if fqInstance is listed in |listInstances|.
IsInstanceListed(const ListInstances & listInstances,const FqInstance & fqInstance)743 bool VintfObject::IsInstanceListed(const ListInstances& listInstances,
744 const FqInstance& fqInstance) {
745 auto list =
746 listInstances(fqInstance.getPackage(), fqInstance.getVersion(), fqInstance.getInterface(),
747 {fqInstance.getInstance()} /* instanceHint*/);
748 return std::any_of(list.begin(), list.end(),
749 [&](const auto& pair) { return pair.first == fqInstance.getInstance(); });
750 }
751
752 // Return a list of FqInstance, where each element:
753 // - is listed in |listInstances|; AND
754 // - is, or inherits from, package@version::interface/instance (as specified by |childrenMap|)
GetListedInstanceInheritance(const std::string & package,const Version & version,const std::string & interface,const std::string & instance,const ListInstances & listInstances,const ChildrenMap & childrenMap)755 android::base::Result<std::vector<FqInstance>> VintfObject::GetListedInstanceInheritance(
756 const std::string& package, const Version& version, const std::string& interface,
757 const std::string& instance, const ListInstances& listInstances,
758 const ChildrenMap& childrenMap) {
759 FqInstance fqInstance;
760 if (!fqInstance.setTo(package, version.majorVer, version.minorVer, interface, instance)) {
761 return android::base::Error() << toFQNameString(package, version, interface, instance)
762 << " is not a valid FqInstance";
763 }
764
765 if (!IsInstanceListed(listInstances, fqInstance)) {
766 return {};
767 }
768
769 const FQName& fqName = fqInstance.getFqName();
770
771 std::vector<FqInstance> ret;
772 ret.push_back(fqInstance);
773
774 auto childRange = childrenMap.equal_range(fqName.string());
775 for (auto it = childRange.first; it != childRange.second; ++it) {
776 const auto& childFqNameString = it->second;
777 FQName childFqName;
778 if (!childFqName.setTo(childFqNameString)) {
779 return android::base::Error() << "Cannot parse " << childFqNameString << " as FQName";
780 }
781 FqInstance childFqInstance;
782 if (!childFqInstance.setTo(childFqName, fqInstance.getInstance())) {
783 return android::base::Error() << "Cannot merge " << childFqName.string() << "/"
784 << fqInstance.getInstance() << " as FqInstance";
785 continue;
786 }
787 if (!IsInstanceListed(listInstances, childFqInstance)) {
788 continue;
789 }
790 ret.push_back(childFqInstance);
791 }
792 return ret;
793 }
794
795 // Check if |fqInstance| is in |targetMatrix|; essentially equal to
796 // targetMatrix.matchInstance(fqInstance), but provides richer error message. In details:
797 // 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
798 // 2. package@x.z::interface/servedInstance is in targetMatrix but
799 // servedInstance is not in listInstances(package@x.z::interface)
IsFqInstanceDeprecated(const CompatibilityMatrix & targetMatrix,HalFormat format,const FqInstance & fqInstance,const ListInstances & listInstances)800 android::base::Result<void> VintfObject::IsFqInstanceDeprecated(
801 const CompatibilityMatrix& targetMatrix, HalFormat format, const FqInstance& fqInstance,
802 const ListInstances& listInstances) {
803 // Find minimum package@x.? in target matrix, and check if instance is in target matrix.
804 bool foundInstance = false;
805 Version targetMatrixMinVer{SIZE_MAX, SIZE_MAX};
806 targetMatrix.forEachInstanceOfPackage(
807 format, fqInstance.getPackage(), [&](const auto& targetMatrixInstance) {
808 if (targetMatrixInstance.versionRange().majorVer == fqInstance.getMajorVersion() &&
809 targetMatrixInstance.interface() == fqInstance.getInterface() &&
810 targetMatrixInstance.matchInstance(fqInstance.getInstance())) {
811 targetMatrixMinVer =
812 std::min(targetMatrixMinVer, targetMatrixInstance.versionRange().minVer());
813 foundInstance = true;
814 }
815 return true;
816 });
817 if (!foundInstance) {
818 return android::base::Error()
819 << fqInstance.string() << " is deprecated in compatibility matrix at FCM Version "
820 << targetMatrix.level() << "; it should not be served.";
821 }
822
823 // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
824 bool targetVersionServed = false;
825 for (const auto& newPair :
826 listInstances(fqInstance.getPackage(), targetMatrixMinVer, fqInstance.getInterface(),
827 {fqInstance.getInstance()} /* instanceHint */)) {
828 if (newPair.first == fqInstance.getInstance()) {
829 targetVersionServed = true;
830 break;
831 }
832 }
833
834 if (!targetVersionServed) {
835 return android::base::Error()
836 << fqInstance.string() << " is deprecated; requires at least " << targetMatrixMinVer;
837 }
838 return {};
839 }
840
checkDeprecation(const ListInstances & listInstances,const std::vector<HidlInterfaceMetadata> & hidlMetadata,std::string * error)841 int32_t VintfObject::checkDeprecation(const ListInstances& listInstances,
842 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
843 std::string* error) {
844 std::vector<CompatibilityMatrix> matrixFragments;
845 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
846 if (matrixFragmentsStatus != OK) {
847 return matrixFragmentsStatus;
848 }
849 if (matrixFragments.empty()) {
850 if (error && error->empty()) {
851 *error = "Cannot get framework matrix for each FCM version for unknown error.";
852 }
853 return NAME_NOT_FOUND;
854 }
855 auto deviceManifest = getDeviceHalManifest();
856 if (deviceManifest == nullptr) {
857 if (error) *error = "No device manifest.";
858 return NAME_NOT_FOUND;
859 }
860 Level deviceLevel = deviceManifest->level();
861 if (deviceLevel == Level::UNSPECIFIED) {
862 if (error) *error = "Device manifest does not specify Shipping FCM Version.";
863 return BAD_VALUE;
864 }
865 std::string kernelLevelError;
866 Level kernelLevel = getKernelLevel(&kernelLevelError);
867 if (kernelLevel == Level::UNSPECIFIED) {
868 LOG(WARNING) << kernelLevelError;
869 }
870
871 std::vector<CompatibilityMatrix> targetMatrices;
872 // Partition matrixFragments into two groups, where the second group
873 // contains all matrices whose level == deviceLevel.
874 auto targetMatricesPartition = std::partition(
875 matrixFragments.begin(), matrixFragments.end(),
876 [&](const CompatibilityMatrix& matrix) { return matrix.level() != deviceLevel; });
877 // Move these matrices into the targetMatrices vector...
878 std::move(targetMatricesPartition, matrixFragments.end(), std::back_inserter(targetMatrices));
879 if (targetMatrices.empty()) {
880 if (error)
881 *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
882 return NAME_NOT_FOUND;
883 }
884 // so that they can be combined into one matrix for deprecation checking.
885 auto targetMatrix =
886 CompatibilityMatrix::combine(deviceLevel, kernelLevel, &targetMatrices, error);
887 if (targetMatrix == nullptr) {
888 return BAD_VALUE;
889 }
890
891 std::multimap<std::string, std::string> childrenMap;
892 for (const auto& child : hidlMetadata) {
893 for (const auto& parent : child.inherited) {
894 childrenMap.emplace(parent, child.name);
895 }
896 }
897
898 // Find a list of possibly deprecated HALs by comparing |listInstances| with older matrices.
899 // Matrices with unspecified level are considered "current".
900 bool isDeprecated = false;
901 for (auto it = matrixFragments.begin(); it < targetMatricesPartition; ++it) {
902 const auto& namedMatrix = *it;
903 if (namedMatrix.level() == Level::UNSPECIFIED) continue;
904 if (namedMatrix.level() > deviceLevel) continue;
905 for (const MatrixHal& hal : namedMatrix.getHals()) {
906 if (IsHalDeprecated(hal, *targetMatrix, listInstances, childrenMap, error)) {
907 isDeprecated = true;
908 }
909 }
910 }
911
912 return isDeprecated ? DEPRECATED : NO_DEPRECATED_HALS;
913 }
914
checkDeprecation(const std::vector<HidlInterfaceMetadata> & hidlMetadata,std::string * error)915 int32_t VintfObject::checkDeprecation(const std::vector<HidlInterfaceMetadata>& hidlMetadata,
916 std::string* error) {
917 using namespace std::placeholders;
918 auto deviceManifest = getDeviceHalManifest();
919 ListInstances inManifest =
920 [&deviceManifest](const std::string& package, Version version, const std::string& interface,
921 const std::vector<std::string>& /* hintInstances */) {
922 std::vector<std::pair<std::string, Version>> ret;
923 deviceManifest->forEachInstanceOfInterface(
924 HalFormat::HIDL, package, version, interface,
925 [&ret](const ManifestInstance& manifestInstance) {
926 ret.push_back(
927 std::make_pair(manifestInstance.instance(), manifestInstance.version()));
928 return true;
929 });
930 return ret;
931 };
932 return checkDeprecation(inManifest, hidlMetadata, error);
933 }
934
getKernelLevel(std::string * error)935 Level VintfObject::getKernelLevel(std::string* error) {
936 auto runtimeInfo = getRuntimeInfo(RuntimeInfo::FetchFlag::KERNEL_FCM);
937 if (!runtimeInfo) {
938 if (error) *error = "Cannot retrieve runtime info with kernel level.";
939 return Level::UNSPECIFIED;
940 }
941 if (runtimeInfo->kernelLevel() != Level::UNSPECIFIED) {
942 return runtimeInfo->kernelLevel();
943 }
944 if (error) {
945 *error = "Both device manifest and kernel release do not specify kernel FCM version.";
946 }
947 return Level::UNSPECIFIED;
948 }
949
getFileSystem()950 const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
951 return mFileSystem;
952 }
953
getPropertyFetcher()954 const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
955 return mPropertyFetcher;
956 }
957
getRuntimeInfoFactory()958 const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
959 return mRuntimeInfoFactory;
960 }
961
hasFrameworkCompatibilityMatrixExtensions()962 android::base::Result<bool> VintfObject::hasFrameworkCompatibilityMatrixExtensions() {
963 std::vector<CompatibilityMatrix> matrixFragments;
964 std::string error;
965 status_t status = getAllFrameworkMatrixLevels(&matrixFragments, &error);
966 if (status != OK) {
967 return android::base::Error(-status)
968 << "Cannot get all framework matrix fragments: " << error;
969 }
970 for (const auto& namedMatrix : matrixFragments) {
971 // Returns true if product matrix exists.
972 if (android::base::StartsWith(namedMatrix.fileName(), kProductVintfDir)) {
973 return true;
974 }
975 // Returns true if system_ext matrix exists.
976 if (android::base::StartsWith(namedMatrix.fileName(), kSystemExtVintfDir)) {
977 return true;
978 }
979 // Returns true if device system matrix exists.
980 if (android::base::StartsWith(namedMatrix.fileName(), kSystemVintfDir) &&
981 namedMatrix.level() == Level::UNSPECIFIED && !namedMatrix.getHals().empty()) {
982 return true;
983 }
984 }
985 return false;
986 }
987
checkUnusedHals(const std::vector<HidlInterfaceMetadata> & hidlMetadata)988 android::base::Result<void> VintfObject::checkUnusedHals(
989 const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
990 auto matrix = getFrameworkCompatibilityMatrix();
991 if (matrix == nullptr) {
992 return android::base::Error(-NAME_NOT_FOUND) << "Missing framework matrix.";
993 }
994 auto manifest = getDeviceHalManifest();
995 if (manifest == nullptr) {
996 return android::base::Error(-NAME_NOT_FOUND) << "Missing device manifest.";
997 }
998 auto unused = manifest->checkUnusedHals(*matrix, hidlMetadata);
999 if (!unused.empty()) {
1000 return android::base::Error()
1001 << "The following instances are in the device manifest but "
1002 << "not specified in framework compatibility matrix: \n"
1003 << " " << android::base::Join(unused, "\n ") << "\n"
1004 << "Suggested fix:\n"
1005 << "1. Update deprecated HALs to the latest version.\n"
1006 << "2. Check for any typos in device manifest or framework compatibility "
1007 << "matrices with FCM version >= " << matrix->level() << ".\n"
1008 << "3. For new platform HALs, add them to any framework compatibility matrix "
1009 << "with FCM version >= " << matrix->level() << " where applicable.\n"
1010 << "4. For device-specific HALs, add to DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE "
1011 << "or DEVICE_PRODUCT_COMPATIBILITY_MATRIX_FILE.";
1012 }
1013 return {};
1014 }
1015
1016 namespace {
1017
1018 // Insert |name| into |ret| if shouldCheck(name).
InsertIf(const std::string & name,const std::function<bool (const std::string &)> & shouldCheck,std::set<std::string> * ret)1019 void InsertIf(const std::string& name, const std::function<bool(const std::string&)>& shouldCheck,
1020 std::set<std::string>* ret) {
1021 if (shouldCheck(name)) ret->insert(name);
1022 }
1023
StripHidlInterface(const std::string & fqNameString)1024 std::string StripHidlInterface(const std::string& fqNameString) {
1025 FQName fqName;
1026 if (!fqName.setTo(fqNameString)) {
1027 return "";
1028 }
1029 return fqName.getPackageAndVersion().string();
1030 }
1031
StripAidlType(const std::string & type)1032 std::string StripAidlType(const std::string& type) {
1033 auto items = android::base::Split(type, ".");
1034 if (items.empty()) {
1035 return "";
1036 }
1037 items.erase(items.end() - 1);
1038 return android::base::Join(items, ".");
1039 }
1040
1041 // android.hardware.foo@1.0
HidlMetadataToPackagesAndVersions(const std::vector<HidlInterfaceMetadata> & hidlMetadata,const std::function<bool (const std::string &)> & shouldCheck)1042 std::set<std::string> HidlMetadataToPackagesAndVersions(
1043 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1044 const std::function<bool(const std::string&)>& shouldCheck) {
1045 std::set<std::string> ret;
1046 for (const auto& item : hidlMetadata) {
1047 InsertIf(StripHidlInterface(item.name), shouldCheck, &ret);
1048 }
1049 return ret;
1050 }
1051
1052 // android.hardware.foo
AidlMetadataToPackages(const std::vector<AidlInterfaceMetadata> & aidlMetadata,const std::function<bool (const std::string &)> & shouldCheck)1053 std::set<std::string> AidlMetadataToPackages(
1054 const std::vector<AidlInterfaceMetadata>& aidlMetadata,
1055 const std::function<bool(const std::string&)>& shouldCheck) {
1056 std::set<std::string> ret;
1057 for (const auto& item : aidlMetadata) {
1058 for (const auto& type : item.types) {
1059 InsertIf(StripAidlType(type), shouldCheck, &ret);
1060 }
1061 }
1062 return ret;
1063 }
1064
1065 // android.hardware.foo@1.0::IFoo.
1066 // Note that UDTs are not filtered out, so there might be non-interface types.
HidlMetadataToNames(const std::vector<HidlInterfaceMetadata> & hidlMetadata)1067 std::set<std::string> HidlMetadataToNames(const std::vector<HidlInterfaceMetadata>& hidlMetadata) {
1068 std::set<std::string> ret;
1069 for (const auto& item : hidlMetadata) {
1070 ret.insert(item.name);
1071 }
1072 return ret;
1073 }
1074
1075 // android.hardware.foo.IFoo
1076 // Note that UDTs are not filtered out, so there might be non-interface types.
AidlMetadataToNames(const std::vector<AidlInterfaceMetadata> & aidlMetadata)1077 std::set<std::string> AidlMetadataToNames(const std::vector<AidlInterfaceMetadata>& aidlMetadata) {
1078 std::set<std::string> ret;
1079 for (const auto& item : aidlMetadata) {
1080 for (const auto& type : item.types) {
1081 ret.insert(type);
1082 }
1083 }
1084 return ret;
1085 }
1086
1087 } // anonymous namespace
1088
getAllFrameworkMatrixLevels()1089 android::base::Result<std::vector<CompatibilityMatrix>> VintfObject::getAllFrameworkMatrixLevels() {
1090 // Get all framework matrix fragments instead of the combined framework compatibility matrix
1091 // because the latter may omit interfaces from the latest FCM if device target-level is not
1092 // the latest.
1093 std::vector<CompatibilityMatrix> matrixFragments;
1094 std::string error;
1095 auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, &error);
1096 if (matrixFragmentsStatus != OK) {
1097 return android::base::Error(-matrixFragmentsStatus)
1098 << "Unable to get all framework matrix fragments: " << error;
1099 }
1100 if (matrixFragments.empty()) {
1101 if (error.empty()) {
1102 error = "Cannot get framework matrix for each FCM version for unknown error.";
1103 }
1104 return android::base::Error(-NAME_NOT_FOUND) << error;
1105 }
1106 return matrixFragments;
1107 }
1108
checkMissingHalsInMatrices(const std::vector<HidlInterfaceMetadata> & hidlMetadata,const std::vector<AidlInterfaceMetadata> & aidlMetadata,std::function<bool (const std::string &)> shouldCheck)1109 android::base::Result<void> VintfObject::checkMissingHalsInMatrices(
1110 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1111 const std::vector<AidlInterfaceMetadata>& aidlMetadata,
1112 std::function<bool(const std::string&)> shouldCheck) {
1113 if (!shouldCheck) {
1114 shouldCheck = [](const auto&) { return true; };
1115 }
1116
1117 auto matrixFragments = getAllFrameworkMatrixLevels();
1118 if (!matrixFragments.ok()) return matrixFragments.error();
1119
1120 // Filter aidlMetadata and hidlMetadata with shouldCheck.
1121 auto allAidlPackages = AidlMetadataToPackages(aidlMetadata, shouldCheck);
1122 auto allHidlPackagesAndVersions = HidlMetadataToPackagesAndVersions(hidlMetadata, shouldCheck);
1123
1124 // Filter out instances in allAidlMetadata and allHidlMetadata that are in the matrices.
1125 std::vector<std::string> errors;
1126 for (const auto& matrix : matrixFragments.value()) {
1127 matrix.forEachInstance([&](const MatrixInstance& matrixInstance) {
1128 switch (matrixInstance.format()) {
1129 case HalFormat::AIDL: {
1130 allAidlPackages.erase(matrixInstance.package());
1131 return true; // continue to next instance
1132 }
1133 case HalFormat::HIDL: {
1134 for (Version v = matrixInstance.versionRange().minVer();
1135 v <= matrixInstance.versionRange().maxVer(); ++v.minorVer) {
1136 allHidlPackagesAndVersions.erase(
1137 toFQNameString(matrixInstance.package(), v));
1138 }
1139 return true; // continue to next instance
1140 }
1141 default: {
1142 if (shouldCheck(matrixInstance.package())) {
1143 errors.push_back("HAL package " + matrixInstance.package() +
1144 " is not allowed to have format " +
1145 to_string(matrixInstance.format()) + ".");
1146 }
1147 return true; // continue to next instance
1148 }
1149 }
1150 });
1151 }
1152
1153 if (!allHidlPackagesAndVersions.empty()) {
1154 errors.push_back(
1155 "The following HIDL packages are not found in any compatibility matrix fragments:\t\n" +
1156 android::base::Join(allHidlPackagesAndVersions, "\t\n"));
1157 }
1158 if (!allAidlPackages.empty()) {
1159 errors.push_back(
1160 "The following AIDL packages are not found in any compatibility matrix fragments:\t\n" +
1161 android::base::Join(allAidlPackages, "\t\n"));
1162 }
1163
1164 if (!errors.empty()) {
1165 return android::base::Error() << android::base::Join(errors, "\n");
1166 }
1167
1168 return {};
1169 }
1170
checkMatrixHalsHasDefinition(const std::vector<HidlInterfaceMetadata> & hidlMetadata,const std::vector<AidlInterfaceMetadata> & aidlMetadata)1171 android::base::Result<void> VintfObject::checkMatrixHalsHasDefinition(
1172 const std::vector<HidlInterfaceMetadata>& hidlMetadata,
1173 const std::vector<AidlInterfaceMetadata>& aidlMetadata) {
1174 auto matrixFragments = getAllFrameworkMatrixLevels();
1175 if (!matrixFragments.ok()) return matrixFragments.error();
1176
1177 auto allAidlNames = AidlMetadataToNames(aidlMetadata);
1178 auto allHidlNames = HidlMetadataToNames(hidlMetadata);
1179 std::set<std::string> badAidlInterfaces;
1180 std::set<std::string> badHidlInterfaces;
1181
1182 std::vector<std::string> errors;
1183 for (const auto& matrix : matrixFragments.value()) {
1184 if (matrix.level() == Level::UNSPECIFIED) {
1185 LOG(INFO) << "Skip checkMatrixHalsHasDefinition() on " << matrix.fileName()
1186 << " with no level.";
1187 continue;
1188 }
1189
1190 matrix.forEachInstance([&](const MatrixInstance& matrixInstance) {
1191 switch (matrixInstance.format()) {
1192 case HalFormat::AIDL: {
1193 auto matrixInterface =
1194 toAidlFqnameString(matrixInstance.package(), matrixInstance.interface());
1195 if (allAidlNames.find(matrixInterface) == allAidlNames.end()) {
1196 errors.push_back(
1197 "AIDL interface " + matrixInterface + " is referenced in " +
1198 matrix.fileName() +
1199 ", but there is no corresponding .aidl definition associated with an "
1200 "aidl_interface module in this build. Typo?");
1201 }
1202 return true; // continue to next instance
1203 }
1204 case HalFormat::HIDL: {
1205 for (Version v = matrixInstance.versionRange().minVer();
1206 v <= matrixInstance.versionRange().maxVer(); ++v.minorVer) {
1207 auto matrixInterface = matrixInstance.interfaceDescription(v);
1208 if (allHidlNames.find(matrixInterface) == allHidlNames.end()) {
1209 errors.push_back(
1210 "HIDL interface " + matrixInterface + " is referenced in " +
1211 matrix.fileName() +
1212 ", but there is no corresponding .hal definition associated with "
1213 "a hidl_interface module in this build. Typo?");
1214 }
1215 }
1216 return true; // continue to next instance
1217 }
1218 default: {
1219 // We do not have data for native HALs.
1220 return true; // continue to next instance
1221 }
1222 }
1223 });
1224 }
1225
1226 if (!errors.empty()) {
1227 return android::base::Error() << android::base::Join(errors, "\n");
1228 }
1229
1230 return {};
1231 }
1232
1233 // make_unique does not work because VintfObject constructor is private.
Builder()1234 VintfObject::Builder::Builder()
1235 : VintfObjectBuilder(std::unique_ptr<VintfObject>(new VintfObject())) {}
1236
1237 namespace details {
1238
~VintfObjectBuilder()1239 VintfObjectBuilder::~VintfObjectBuilder() {}
1240
setFileSystem(std::unique_ptr<FileSystem> && e)1241 VintfObjectBuilder& VintfObjectBuilder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
1242 mObject->mFileSystem = std::move(e);
1243 return *this;
1244 }
1245
setRuntimeInfoFactory(std::unique_ptr<ObjectFactory<RuntimeInfo>> && e)1246 VintfObjectBuilder& VintfObjectBuilder::setRuntimeInfoFactory(
1247 std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
1248 mObject->mRuntimeInfoFactory = std::move(e);
1249 return *this;
1250 }
1251
setPropertyFetcher(std::unique_ptr<PropertyFetcher> && e)1252 VintfObjectBuilder& VintfObjectBuilder::setPropertyFetcher(std::unique_ptr<PropertyFetcher>&& e) {
1253 mObject->mPropertyFetcher = std::move(e);
1254 return *this;
1255 }
1256
buildInternal()1257 std::unique_ptr<VintfObject> VintfObjectBuilder::buildInternal() {
1258 if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
1259 if (!mObject->mRuntimeInfoFactory)
1260 mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
1261 if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
1262 return std::move(mObject);
1263 }
1264
1265 } // namespace details
1266
1267 } // namespace vintf
1268 } // namespace android
1269