VintfObject.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. #include "VintfObject.h"
  17. #include <dirent.h>
  18. #include <functional>
  19. #include <memory>
  20. #include <mutex>
  21. #include <android-base/logging.h>
  22. #include "CompatibilityMatrix.h"
  23. #include "VintfObjectAfterUpdate.h"
  24. #include "parse_string.h"
  25. #include "parse_xml.h"
  26. #include "utils.h"
  27. using std::placeholders::_1;
  28. using std::placeholders::_2;
  29. namespace android {
  30. namespace vintf {
  31. using namespace details;
  32. #ifdef LIBVINTF_TARGET
  33. static constexpr bool kIsTarget = true;
  34. #else
  35. static constexpr bool kIsTarget = false;
  36. #endif
  37. template <typename T, typename F>
  38. static std::shared_ptr<const T> Get(
  39. LockedSharedPtr<T> *ptr,
  40. bool skipCache,
  41. const F &fetchAllInformation) {
  42. std::unique_lock<std::mutex> _lock(ptr->mutex);
  43. if (skipCache || !ptr->fetchedOnce) {
  44. ptr->object = std::make_unique<T>();
  45. std::string error;
  46. if (fetchAllInformation(ptr->object.get(), &error) != OK) {
  47. LOG(WARNING) << error;
  48. ptr->object = nullptr; // frees the old object
  49. }
  50. ptr->fetchedOnce = true;
  51. }
  52. return ptr->object;
  53. }
  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. static std::unique_ptr<PropertyFetcher> createDefaultPropertyFetcher() {
  64. std::unique_ptr<PropertyFetcher> propertyFetcher;
  65. if (kIsTarget) {
  66. propertyFetcher = std::make_unique<details::PropertyFetcherImpl>();
  67. } else {
  68. propertyFetcher = std::make_unique<details::PropertyFetcherNoOp>();
  69. }
  70. return propertyFetcher;
  71. }
  72. details::LockedSharedPtr<VintfObject> VintfObject::sInstance{};
  73. std::shared_ptr<VintfObject> VintfObject::GetInstance() {
  74. std::unique_lock<std::mutex> lock(sInstance.mutex);
  75. if (sInstance.object == nullptr) {
  76. sInstance.object = std::shared_ptr<VintfObject>(VintfObject::Builder().build().release());
  77. }
  78. return sInstance.object;
  79. }
  80. std::shared_ptr<const HalManifest> VintfObject::GetDeviceHalManifest(bool skipCache) {
  81. return GetInstance()->getDeviceHalManifest(skipCache);
  82. }
  83. std::shared_ptr<const HalManifest> VintfObject::getDeviceHalManifest(bool skipCache) {
  84. return Get(&mDeviceManifest, skipCache,
  85. std::bind(&VintfObject::fetchDeviceHalManifest, this, _1, _2));
  86. }
  87. std::shared_ptr<const HalManifest> VintfObject::GetFrameworkHalManifest(bool skipCache) {
  88. return GetInstance()->getFrameworkHalManifest(skipCache);
  89. }
  90. std::shared_ptr<const HalManifest> VintfObject::getFrameworkHalManifest(bool skipCache) {
  91. return Get(&mFrameworkManifest, skipCache,
  92. std::bind(&VintfObject::fetchFrameworkHalManifest, this, _1, _2));
  93. }
  94. std::shared_ptr<const CompatibilityMatrix> VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) {
  95. return GetInstance()->getDeviceCompatibilityMatrix(skipCache);
  96. }
  97. std::shared_ptr<const CompatibilityMatrix> VintfObject::getDeviceCompatibilityMatrix(
  98. bool skipCache) {
  99. return Get(&mDeviceMatrix, skipCache, std::bind(&VintfObject::fetchDeviceMatrix, this, _1, _2));
  100. }
  101. std::shared_ptr<const CompatibilityMatrix> VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) {
  102. return GetInstance()->getFrameworkCompatibilityMatrix(skipCache);
  103. }
  104. std::shared_ptr<const CompatibilityMatrix> VintfObject::getFrameworkCompatibilityMatrix(
  105. bool skipCache) {
  106. // To avoid deadlock, get device manifest before any locks.
  107. auto deviceManifest = getDeviceHalManifest();
  108. std::unique_lock<std::mutex> _lock(mFrameworkCompatibilityMatrixMutex);
  109. auto combined =
  110. Get(&mCombinedFrameworkMatrix, skipCache,
  111. std::bind(&VintfObject::getCombinedFrameworkMatrix, this, deviceManifest, _1, _2));
  112. if (combined != nullptr) {
  113. return combined;
  114. }
  115. return Get(&mFrameworkMatrix, skipCache,
  116. std::bind(&CompatibilityMatrix::fetchAllInformation, _1, getFileSystem().get(),
  117. kSystemLegacyMatrix, _2));
  118. }
  119. status_t VintfObject::getCombinedFrameworkMatrix(
  120. const std::shared_ptr<const HalManifest>& deviceManifest, CompatibilityMatrix* out,
  121. std::string* error) {
  122. std::vector<Named<CompatibilityMatrix>> matrixFragments;
  123. auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
  124. if (matrixFragmentsStatus != OK) {
  125. return matrixFragmentsStatus;
  126. }
  127. if (matrixFragments.empty()) {
  128. if (error && error->empty()) {
  129. *error = "Cannot get framework matrix for each FCM version for unknown error.";
  130. }
  131. return NAME_NOT_FOUND;
  132. }
  133. Level deviceLevel = Level::UNSPECIFIED;
  134. if (deviceManifest != nullptr) {
  135. deviceLevel = deviceManifest->level();
  136. }
  137. // TODO(b/70628538): Do not infer from Shipping API level.
  138. if (deviceLevel == Level::UNSPECIFIED) {
  139. auto shippingApi = getPropertyFetcher()->getUintProperty("ro.product.first_api_level", 0u);
  140. if (shippingApi != 0u) {
  141. deviceLevel = details::convertFromApiLevel(shippingApi);
  142. }
  143. }
  144. if (deviceLevel == Level::UNSPECIFIED) {
  145. // Cannot infer FCM version. Combine all matrices by assuming
  146. // Shipping FCM Version == min(all supported FCM Versions in the framework)
  147. for (auto&& pair : matrixFragments) {
  148. Level fragmentLevel = pair.object.level();
  149. if (fragmentLevel != Level::UNSPECIFIED && deviceLevel > fragmentLevel) {
  150. deviceLevel = fragmentLevel;
  151. }
  152. }
  153. }
  154. if (deviceLevel == Level::UNSPECIFIED) {
  155. // None of the fragments specify any FCM version. Should never happen except
  156. // for inconsistent builds.
  157. if (error) {
  158. *error = "No framework compatibility matrix files under " + kSystemVintfDir +
  159. " declare FCM version.";
  160. }
  161. return NAME_NOT_FOUND;
  162. }
  163. auto combined = CompatibilityMatrix::combine(deviceLevel, &matrixFragments, error);
  164. if (combined == nullptr) {
  165. return BAD_VALUE;
  166. }
  167. *out = std::move(*combined);
  168. return OK;
  169. }
  170. // Load and combine all of the manifests in a directory
  171. status_t VintfObject::addDirectoryManifests(const std::string& directory, HalManifest* manifest,
  172. std::string* error) {
  173. std::vector<std::string> fileNames;
  174. status_t err = getFileSystem()->listFiles(directory, &fileNames, error);
  175. // if the directory isn't there, that's okay
  176. if (err == NAME_NOT_FOUND) return OK;
  177. if (err != OK) return err;
  178. for (const std::string& file : fileNames) {
  179. // Only adds HALs because all other things are added by libvintf
  180. // itself for now.
  181. HalManifest fragmentManifest;
  182. err = fetchOneHalManifest(directory + file, &fragmentManifest, error);
  183. if (err != OK) return err;
  184. if (!manifest->addAll(&fragmentManifest, error)) {
  185. if (error) {
  186. error->insert(0, "Cannot add manifest fragment " + directory + file + ":");
  187. }
  188. return UNKNOWN_ERROR;
  189. }
  190. }
  191. return OK;
  192. }
  193. // Priority for loading vendor manifest:
  194. // 1. /vendor/etc/vintf/manifest.xml + device fragments + ODM manifest (optional) + odm fragments
  195. // 2. /vendor/etc/vintf/manifest.xml + device fragments
  196. // 3. ODM manifest (optional) + odm fragments
  197. // 4. /vendor/manifest.xml (legacy, no fragments)
  198. // where:
  199. // A + B means unioning <hal> tags from A and B. If B declares an override, then this takes priority
  200. // over A.
  201. status_t VintfObject::fetchDeviceHalManifest(HalManifest* out, std::string* error) {
  202. status_t vendorStatus = fetchOneHalManifest(kVendorManifest, out, error);
  203. if (vendorStatus != OK && vendorStatus != NAME_NOT_FOUND) {
  204. return vendorStatus;
  205. }
  206. if (vendorStatus == OK) {
  207. status_t fragmentStatus = addDirectoryManifests(kVendorManifestFragmentDir, out, error);
  208. if (fragmentStatus != OK) {
  209. return fragmentStatus;
  210. }
  211. }
  212. HalManifest odmManifest;
  213. status_t odmStatus = fetchOdmHalManifest(&odmManifest, error);
  214. if (odmStatus != OK && odmStatus != NAME_NOT_FOUND) {
  215. return odmStatus;
  216. }
  217. if (vendorStatus == OK) {
  218. if (odmStatus == OK) {
  219. if (!out->addAll(&odmManifest, error)) {
  220. if (error) {
  221. error->insert(0, "Cannot add ODM manifest :");
  222. }
  223. return UNKNOWN_ERROR;
  224. }
  225. }
  226. return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
  227. }
  228. // vendorStatus != OK, "out" is not changed.
  229. if (odmStatus == OK) {
  230. *out = std::move(odmManifest);
  231. return addDirectoryManifests(kOdmManifestFragmentDir, out, error);
  232. }
  233. // Use legacy /vendor/manifest.xml
  234. return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyManifest, error);
  235. }
  236. // "out" is written to iff return status is OK.
  237. // Priority:
  238. // 1. if {sku} is defined, /odm/etc/vintf/manifest_{sku}.xml
  239. // 2. /odm/etc/vintf/manifest.xml
  240. // 3. if {sku} is defined, /odm/etc/manifest_{sku}.xml
  241. // 4. /odm/etc/manifest.xml
  242. // where:
  243. // {sku} is the value of ro.boot.product.hardware.sku
  244. status_t VintfObject::fetchOdmHalManifest(HalManifest* out, std::string* error) {
  245. status_t status;
  246. std::string productModel;
  247. productModel = getPropertyFetcher()->getProperty("ro.boot.product.hardware.sku", "");
  248. if (!productModel.empty()) {
  249. status =
  250. fetchOneHalManifest(kOdmVintfDir + "manifest_" + productModel + ".xml", out, error);
  251. if (status == OK || status != NAME_NOT_FOUND) {
  252. return status;
  253. }
  254. }
  255. status = fetchOneHalManifest(kOdmManifest, out, error);
  256. if (status == OK || status != NAME_NOT_FOUND) {
  257. return status;
  258. }
  259. if (!productModel.empty()) {
  260. status = fetchOneHalManifest(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", out,
  261. error);
  262. if (status == OK || status != NAME_NOT_FOUND) {
  263. return status;
  264. }
  265. }
  266. status = fetchOneHalManifest(kOdmLegacyManifest, out, error);
  267. if (status == OK || status != NAME_NOT_FOUND) {
  268. return status;
  269. }
  270. return NAME_NOT_FOUND;
  271. }
  272. // Fetch one manifest.xml file. "out" is written to iff return status is OK.
  273. // Returns NAME_NOT_FOUND if file is missing.
  274. status_t VintfObject::fetchOneHalManifest(const std::string& path, HalManifest* out,
  275. std::string* error) {
  276. HalManifest ret;
  277. status_t status = ret.fetchAllInformation(getFileSystem().get(), path, error);
  278. if (status == OK) {
  279. *out = std::move(ret);
  280. }
  281. return status;
  282. }
  283. status_t VintfObject::fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error) {
  284. CompatibilityMatrix etcMatrix;
  285. if (etcMatrix.fetchAllInformation(getFileSystem().get(), kVendorMatrix, error) == OK) {
  286. *out = std::move(etcMatrix);
  287. return OK;
  288. }
  289. return out->fetchAllInformation(getFileSystem().get(), kVendorLegacyMatrix, error);
  290. }
  291. // Priority:
  292. // 1. /system/etc/vintf/manifest.xml
  293. // + /system/etc/vintf/manifest/*.xml if they exist
  294. // + /product/etc/vintf/manifest.xml if it exists
  295. // + /product/etc/vintf/manifest/*.xml if they exist
  296. // 2. (deprecated) /system/manifest.xml
  297. status_t VintfObject::fetchFrameworkHalManifest(HalManifest* out, std::string* error) {
  298. auto systemEtcStatus = fetchOneHalManifest(kSystemManifest, out, error);
  299. if (systemEtcStatus == OK) {
  300. auto dirStatus = addDirectoryManifests(kSystemManifestFragmentDir, out, error);
  301. if (dirStatus != OK) {
  302. return dirStatus;
  303. }
  304. HalManifest productManifest;
  305. auto productStatus = fetchOneHalManifest(kProductManifest, &productManifest, error);
  306. if (productStatus != OK && productStatus != NAME_NOT_FOUND) {
  307. return productStatus;
  308. }
  309. if (productStatus == OK) {
  310. if (!out->addAll(&productManifest, error)) {
  311. if (error) {
  312. error->insert(0, "Cannot add " + kProductManifest + ":");
  313. }
  314. return UNKNOWN_ERROR;
  315. }
  316. }
  317. return addDirectoryManifests(kProductManifestFragmentDir, out, error);
  318. } else {
  319. LOG(WARNING) << "Cannot fetch " << kSystemManifest << ": "
  320. << (error ? *error : strerror(-systemEtcStatus));
  321. }
  322. return out->fetchAllInformation(getFileSystem().get(), kSystemLegacyManifest, error);
  323. }
  324. static void appendLine(std::string* error, const std::string& message) {
  325. if (error != nullptr) {
  326. if (!error->empty()) *error += "\n";
  327. *error += message;
  328. }
  329. }
  330. status_t VintfObject::getOneMatrix(const std::string& path, Named<CompatibilityMatrix>* out,
  331. std::string* error) {
  332. std::string content;
  333. status_t status = getFileSystem()->fetch(path, &content, error);
  334. if (status != OK) {
  335. return status;
  336. }
  337. if (!gCompatibilityMatrixConverter(&out->object, content, error)) {
  338. if (error) {
  339. error->insert(0, "Cannot parse " + path + ": ");
  340. }
  341. return BAD_VALUE;
  342. }
  343. out->name = path;
  344. return OK;
  345. }
  346. status_t VintfObject::getAllFrameworkMatrixLevels(std::vector<Named<CompatibilityMatrix>>* results,
  347. std::string* error) {
  348. std::vector<std::string> fileNames;
  349. status_t listStatus = getFileSystem()->listFiles(kSystemVintfDir, &fileNames, error);
  350. if (listStatus != OK) {
  351. return listStatus;
  352. }
  353. for (const std::string& fileName : fileNames) {
  354. std::string path = kSystemVintfDir + fileName;
  355. Named<CompatibilityMatrix> namedMatrix;
  356. std::string matrixError;
  357. status_t matrixStatus = getOneMatrix(path, &namedMatrix, &matrixError);
  358. if (matrixStatus != OK) {
  359. // System manifests and matrices share the same dir. Client may not have enough
  360. // permissions to read system manifests, or may not be able to parse it.
  361. auto logLevel = matrixStatus == BAD_VALUE ? base::DEBUG : base::ERROR;
  362. LOG(logLevel) << "Framework Matrix: Ignore file " << path << ": " << matrixError;
  363. continue;
  364. }
  365. results->emplace_back(std::move(namedMatrix));
  366. }
  367. Named<CompatibilityMatrix> productMatrix;
  368. std::string productError;
  369. status_t productStatus = getOneMatrix(kProductMatrix, &productMatrix, &productError);
  370. if (productStatus == OK) {
  371. results->emplace_back(std::move(productMatrix));
  372. } else if (productStatus == NAME_NOT_FOUND) {
  373. LOG(DEBUG) << "Framework Matrix: missing " << kProductMatrix;
  374. } else {
  375. if (error) *error = std::move(productError);
  376. return productStatus;
  377. }
  378. if (results->empty()) {
  379. if (error) {
  380. *error =
  381. "No framework matrices under " + kSystemVintfDir + " can be fetched or parsed.\n";
  382. }
  383. return NAME_NOT_FOUND;
  384. }
  385. return OK;
  386. }
  387. std::shared_ptr<const RuntimeInfo> VintfObject::GetRuntimeInfo(bool skipCache,
  388. RuntimeInfo::FetchFlags flags) {
  389. return GetInstance()->getRuntimeInfo(skipCache, flags);
  390. }
  391. std::shared_ptr<const RuntimeInfo> VintfObject::getRuntimeInfo(bool skipCache,
  392. RuntimeInfo::FetchFlags flags) {
  393. std::unique_lock<std::mutex> _lock(mDeviceRuntimeInfo.mutex);
  394. if (!skipCache) {
  395. flags &= (~mDeviceRuntimeInfo.fetchedFlags);
  396. }
  397. if (mDeviceRuntimeInfo.object == nullptr) {
  398. mDeviceRuntimeInfo.object = getRuntimeInfoFactory()->make_shared();
  399. }
  400. status_t status = mDeviceRuntimeInfo.object->fetchAllInformation(flags);
  401. if (status != OK) {
  402. mDeviceRuntimeInfo.fetchedFlags &= (~flags); // mark the fields as "not fetched"
  403. return nullptr;
  404. }
  405. mDeviceRuntimeInfo.fetchedFlags |= flags;
  406. return mDeviceRuntimeInfo.object;
  407. }
  408. namespace details {
  409. enum class ParseStatus {
  410. OK,
  411. PARSE_ERROR,
  412. DUPLICATED_FWK_ENTRY,
  413. DUPLICATED_DEV_ENTRY,
  414. };
  415. static std::string toString(ParseStatus status) {
  416. switch(status) {
  417. case ParseStatus::OK: return "OK";
  418. case ParseStatus::PARSE_ERROR: return "parse error";
  419. case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework";
  420. case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device";
  421. }
  422. return "";
  423. }
  424. template <typename T>
  425. static ParseStatus tryParse(const std::string& xml, const XmlConverter<T>& parse,
  426. VintfObjectAfterUpdate* afterUpdate) {
  427. std::shared_ptr<T> ret = std::make_shared<T>();
  428. if (!parse(ret.get(), xml, nullptr /* error */)) {
  429. return ParseStatus::PARSE_ERROR;
  430. }
  431. if (!afterUpdate->set(ret)) {
  432. if (ret->type() == SchemaType::FRAMEWORK) {
  433. return ParseStatus::DUPLICATED_FWK_ENTRY;
  434. } else if (ret->type() == SchemaType::DEVICE) {
  435. return ParseStatus::DUPLICATED_DEV_ENTRY;
  436. }
  437. LOG(FATAL) << "unknown SchemaType: "
  438. << static_cast<std::underlying_type_t<SchemaType>>(ret->type());
  439. }
  440. return ParseStatus::OK;
  441. }
  442. } // namespace details
  443. // Simulate applying xmls to VintfObject, then checkCompatibility as usual.
  444. int32_t VintfObject::checkCompatibility(const std::vector<std::string>& xmls, std::string* error,
  445. CheckFlags::Type flags) {
  446. VintfObjectAfterUpdate afterUpdate(this);
  447. ParseStatus parseStatus = ParseStatus::OK;
  448. // parse all information from package
  449. for (const auto &xml : xmls) {
  450. parseStatus = tryParse(xml, gHalManifestConverter, &afterUpdate);
  451. if (parseStatus == ParseStatus::OK) {
  452. continue; // work on next one
  453. }
  454. if (parseStatus != ParseStatus::PARSE_ERROR) {
  455. appendLine(error, toString(parseStatus) + " manifest");
  456. return ALREADY_EXISTS;
  457. }
  458. parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &afterUpdate);
  459. if (parseStatus == ParseStatus::OK) {
  460. continue; // work on next one
  461. }
  462. if (parseStatus != ParseStatus::PARSE_ERROR) {
  463. appendLine(error, toString(parseStatus) + " matrix");
  464. return ALREADY_EXISTS;
  465. }
  466. appendLine(error, toString(parseStatus)); // parse error
  467. return BAD_VALUE;
  468. }
  469. return afterUpdate.checkCompatibility(error, flags);
  470. }
  471. int32_t VintfObject::checkCompatibility(std::string* error, CheckFlags::Type flags) {
  472. status_t status = OK;
  473. // null checks for files and runtime info
  474. if (getFrameworkHalManifest() == nullptr) {
  475. appendLine(error, "No framework manifest file from device or from update package");
  476. status = NO_INIT;
  477. }
  478. if (getDeviceHalManifest() == nullptr) {
  479. appendLine(error, "No device manifest file from device or from update package");
  480. status = NO_INIT;
  481. }
  482. if (getFrameworkCompatibilityMatrix() == nullptr) {
  483. appendLine(error, "No framework matrix file from device or from update package");
  484. status = NO_INIT;
  485. }
  486. if (getDeviceCompatibilityMatrix() == nullptr) {
  487. appendLine(error, "No device matrix file from device or from update package");
  488. status = NO_INIT;
  489. }
  490. if (flags.isRuntimeInfoEnabled()) {
  491. if (getRuntimeInfo() == nullptr) {
  492. appendLine(error, "No runtime info from device");
  493. status = NO_INIT;
  494. }
  495. }
  496. if (status != OK) return status;
  497. // compatiblity check.
  498. if (!getDeviceHalManifest()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error)) {
  499. if (error) {
  500. error->insert(0,
  501. "Device manifest and framework compatibility matrix are incompatible: ");
  502. }
  503. return INCOMPATIBLE;
  504. }
  505. if (!getFrameworkHalManifest()->checkCompatibility(*getDeviceCompatibilityMatrix(), error)) {
  506. if (error) {
  507. error->insert(0,
  508. "Framework manifest and device compatibility matrix are incompatible: ");
  509. }
  510. return INCOMPATIBLE;
  511. }
  512. CheckFlags::Type runtimeInfoCheckFlags = flags;
  513. if (!!getDeviceHalManifest()->kernel()) {
  514. // Use kernel from incoming OTA package, but not on the device.
  515. runtimeInfoCheckFlags = runtimeInfoCheckFlags.disableKernel();
  516. }
  517. if (flags.isRuntimeInfoEnabled()) {
  518. if (!getRuntimeInfo()->checkCompatibility(*getFrameworkCompatibilityMatrix(), error,
  519. runtimeInfoCheckFlags)) {
  520. if (error) {
  521. error->insert(0,
  522. "Runtime info and framework compatibility matrix are incompatible: ");
  523. }
  524. return INCOMPATIBLE;
  525. }
  526. }
  527. return COMPATIBLE;
  528. }
  529. namespace details {
  530. const std::string kSystemVintfDir = "/system/etc/vintf/";
  531. const std::string kVendorVintfDir = "/vendor/etc/vintf/";
  532. const std::string kOdmVintfDir = "/odm/etc/vintf/";
  533. const std::string kProductVintfDir = "/product/etc/vintf/";
  534. const std::string kVendorManifest = kVendorVintfDir + "manifest.xml";
  535. const std::string kSystemManifest = kSystemVintfDir + "manifest.xml";
  536. const std::string kVendorMatrix = kVendorVintfDir + "compatibility_matrix.xml";
  537. const std::string kOdmManifest = kOdmVintfDir + "manifest.xml";
  538. const std::string kProductMatrix = kProductVintfDir + "compatibility_matrix.xml";
  539. const std::string kProductManifest = kProductVintfDir + "manifest.xml";
  540. const std::string kVendorManifestFragmentDir = kVendorVintfDir + "manifest/";
  541. const std::string kSystemManifestFragmentDir = kSystemVintfDir + "manifest/";
  542. const std::string kOdmManifestFragmentDir = kOdmVintfDir + "manifest/";
  543. const std::string kProductManifestFragmentDir = kProductVintfDir + "manifest/";
  544. const std::string kVendorLegacyManifest = "/vendor/manifest.xml";
  545. const std::string kVendorLegacyMatrix = "/vendor/compatibility_matrix.xml";
  546. const std::string kSystemLegacyManifest = "/system/manifest.xml";
  547. const std::string kSystemLegacyMatrix = "/system/compatibility_matrix.xml";
  548. const std::string kOdmLegacyVintfDir = "/odm/etc/";
  549. const std::string kOdmLegacyManifest = kOdmLegacyVintfDir + "manifest.xml";
  550. std::vector<std::string> dumpFileList() {
  551. return {
  552. // clang-format off
  553. kSystemVintfDir,
  554. kVendorVintfDir,
  555. kOdmVintfDir,
  556. kProductVintfDir,
  557. kOdmLegacyVintfDir,
  558. kVendorLegacyManifest,
  559. kVendorLegacyMatrix,
  560. kSystemLegacyManifest,
  561. kSystemLegacyMatrix,
  562. // clang-format on
  563. };
  564. }
  565. } // namespace details
  566. int32_t VintfObject::CheckCompatibility(const std::vector<std::string>& xmls, std::string* error,
  567. CheckFlags::Type flags) {
  568. return GetInstance()->checkCompatibility(xmls, error, flags);
  569. }
  570. bool VintfObject::IsHalDeprecated(const MatrixHal& oldMatrixHal,
  571. const CompatibilityMatrix& targetMatrix,
  572. const ListInstances& listInstances, std::string* error) {
  573. bool isDeprecated = false;
  574. oldMatrixHal.forEachInstance([&](const MatrixInstance& oldMatrixInstance) {
  575. if (IsInstanceDeprecated(oldMatrixInstance, targetMatrix, listInstances, error)) {
  576. isDeprecated = true;
  577. }
  578. return !isDeprecated; // continue if no deprecated instance is found.
  579. });
  580. return isDeprecated;
  581. }
  582. // Let oldMatrixInstance = [email protected]::interface with instancePattern.
  583. // If any "servedInstance" in listInstances([email protected]::interface) matches instancePattern, return
  584. // true iff:
  585. // 1. package@x.?::interface/servedInstance is not in targetMatrix; OR
  586. // 2. [email protected]::interface/servedInstance is in targetMatrix but
  587. // servedInstance is not in listInstances([email protected]::interface)
  588. bool VintfObject::IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance,
  589. const CompatibilityMatrix& targetMatrix,
  590. const ListInstances& listInstances, std::string* error) {
  591. const std::string& package = oldMatrixInstance.package();
  592. const Version& version = oldMatrixInstance.versionRange().minVer();
  593. const std::string& interface = oldMatrixInstance.interface();
  594. std::vector<std::string> instanceHint;
  595. if (!oldMatrixInstance.isRegex()) {
  596. instanceHint.push_back(oldMatrixInstance.exactInstance());
  597. }
  598. auto list = listInstances(package, version, interface, instanceHint);
  599. for (const auto& pair : list) {
  600. const std::string& servedInstance = pair.first;
  601. Version servedVersion = pair.second;
  602. if (!oldMatrixInstance.matchInstance(servedInstance)) {
  603. continue;
  604. }
  605. // Find any package@x.? in target matrix, and check if instance is in target matrix.
  606. bool foundInstance = false;
  607. Version targetMatrixMinVer;
  608. targetMatrix.forEachInstanceOfPackage(package, [&](const auto& targetMatrixInstance) {
  609. if (targetMatrixInstance.versionRange().majorVer == version.majorVer &&
  610. targetMatrixInstance.interface() == interface &&
  611. targetMatrixInstance.matchInstance(servedInstance)) {
  612. targetMatrixMinVer = targetMatrixInstance.versionRange().minVer();
  613. foundInstance = true;
  614. }
  615. return !foundInstance; // continue if not found
  616. });
  617. if (!foundInstance) {
  618. if (error) {
  619. *error = toFQNameString(package, servedVersion, interface, servedInstance) +
  620. " is deprecated in compatibility matrix at FCM Version " +
  621. to_string(targetMatrix.level()) + "; it should not be served.";
  622. }
  623. return true;
  624. }
  625. // Assuming that targetMatrix requires @x.u-v, require that at least @x.u is served.
  626. bool targetVersionServed = false;
  627. for (const auto& newPair :
  628. listInstances(package, targetMatrixMinVer, interface, instanceHint)) {
  629. if (newPair.first == servedInstance) {
  630. targetVersionServed = true;
  631. break;
  632. }
  633. }
  634. if (!targetVersionServed) {
  635. appendLine(error, toFQNameString(package, servedVersion, interface, servedInstance) +
  636. " is deprecated; requires at least " +
  637. to_string(targetMatrixMinVer));
  638. return true;
  639. }
  640. }
  641. return false;
  642. }
  643. int32_t VintfObject::CheckDeprecation(const ListInstances& listInstances, std::string* error) {
  644. return GetInstance()->checkDeprecation(listInstances, error);
  645. }
  646. int32_t VintfObject::checkDeprecation(const ListInstances& listInstances, std::string* error) {
  647. std::vector<Named<CompatibilityMatrix>> matrixFragments;
  648. auto matrixFragmentsStatus = getAllFrameworkMatrixLevels(&matrixFragments, error);
  649. if (matrixFragmentsStatus != OK) {
  650. return matrixFragmentsStatus;
  651. }
  652. if (matrixFragments.empty()) {
  653. if (error && error->empty()) {
  654. *error = "Cannot get framework matrix for each FCM version for unknown error.";
  655. }
  656. return NAME_NOT_FOUND;
  657. }
  658. auto deviceManifest = getDeviceHalManifest();
  659. if (deviceManifest == nullptr) {
  660. if (error) *error = "No device manifest.";
  661. return NAME_NOT_FOUND;
  662. }
  663. Level deviceLevel = deviceManifest->level();
  664. if (deviceLevel == Level::UNSPECIFIED) {
  665. if (error) *error = "Device manifest does not specify Shipping FCM Version.";
  666. return BAD_VALUE;
  667. }
  668. const CompatibilityMatrix* targetMatrix = nullptr;
  669. for (const auto& namedMatrix : matrixFragments) {
  670. if (namedMatrix.object.level() == deviceLevel) {
  671. targetMatrix = &namedMatrix.object;
  672. }
  673. }
  674. if (targetMatrix == nullptr) {
  675. if (error)
  676. *error = "Cannot find framework matrix at FCM version " + to_string(deviceLevel) + ".";
  677. return NAME_NOT_FOUND;
  678. }
  679. bool hasDeprecatedHals = false;
  680. for (const auto& namedMatrix : matrixFragments) {
  681. if (namedMatrix.object.level() == Level::UNSPECIFIED) continue;
  682. if (namedMatrix.object.level() >= deviceLevel) continue;
  683. const auto& oldMatrix = namedMatrix.object;
  684. for (const MatrixHal& hal : oldMatrix.getHals()) {
  685. hasDeprecatedHals |= IsHalDeprecated(hal, *targetMatrix, listInstances, error);
  686. }
  687. }
  688. return hasDeprecatedHals ? DEPRECATED : NO_DEPRECATED_HALS;
  689. }
  690. int32_t VintfObject::CheckDeprecation(std::string* error) {
  691. return GetInstance()->checkDeprecation(error);
  692. }
  693. int32_t VintfObject::checkDeprecation(std::string* error) {
  694. using namespace std::placeholders;
  695. auto deviceManifest = getDeviceHalManifest();
  696. ListInstances inManifest =
  697. [&deviceManifest](const std::string& package, Version version, const std::string& interface,
  698. const std::vector<std::string>& /* hintInstances */) {
  699. std::vector<std::pair<std::string, Version>> ret;
  700. deviceManifest->forEachInstanceOfInterface(
  701. package, version, interface, [&ret](const ManifestInstance& manifestInstance) {
  702. ret.push_back(
  703. std::make_pair(manifestInstance.instance(), manifestInstance.version()));
  704. return true;
  705. });
  706. return ret;
  707. };
  708. return checkDeprecation(inManifest, error);
  709. }
  710. const std::unique_ptr<FileSystem>& VintfObject::getFileSystem() {
  711. return mFileSystem;
  712. }
  713. const std::unique_ptr<PropertyFetcher>& VintfObject::getPropertyFetcher() {
  714. return mPropertyFetcher;
  715. }
  716. const std::unique_ptr<ObjectFactory<RuntimeInfo>>& VintfObject::getRuntimeInfoFactory() {
  717. return mRuntimeInfoFactory;
  718. }
  719. // make_unique does not work because VintfObject constructor is private.
  720. VintfObject::Builder::Builder() : mObject(std::unique_ptr<VintfObject>(new VintfObject())) {}
  721. VintfObject::Builder& VintfObject::Builder::setFileSystem(std::unique_ptr<FileSystem>&& e) {
  722. mObject->mFileSystem = std::move(e);
  723. return *this;
  724. }
  725. VintfObject::Builder& VintfObject::Builder::setRuntimeInfoFactory(
  726. std::unique_ptr<ObjectFactory<RuntimeInfo>>&& e) {
  727. mObject->mRuntimeInfoFactory = std::move(e);
  728. return *this;
  729. }
  730. VintfObject::Builder& VintfObject::Builder::setPropertyFetcher(
  731. std::unique_ptr<PropertyFetcher>&& e) {
  732. mObject->mPropertyFetcher = std::move(e);
  733. return *this;
  734. }
  735. std::unique_ptr<VintfObject> VintfObject::Builder::build() {
  736. if (!mObject->mFileSystem) mObject->mFileSystem = createDefaultFileSystem();
  737. if (!mObject->mRuntimeInfoFactory)
  738. mObject->mRuntimeInfoFactory = std::make_unique<ObjectFactory<RuntimeInfo>>();
  739. if (!mObject->mPropertyFetcher) mObject->mPropertyFetcher = createDefaultPropertyFetcher();
  740. return std::move(mObject);
  741. }
  742. } // namespace vintf
  743. } // namespace android