blob: f2a239af218a9dce7763a720c54e7b5481dfe5d3 [file] [log] [blame]
Yifan Honga72bde72017-09-28 13:36:48 -07001/*
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
Yifan Hong69c1b112018-02-27 17:06:00 -080017#include <getopt.h>
Yifan Hong62503e12019-08-26 12:47:41 -070018#include <sysexits.h>
Yifan Hong69c1b112018-02-27 17:06:00 -080019#include <unistd.h>
Yifan Honga72bde72017-09-28 13:36:48 -070020
Yifan Hong69c1b112018-02-27 17:06:00 -080021#include <iostream>
22#include <map>
23
Yifan Hong07292852019-08-21 15:40:30 -070024#include <android-base/file.h>
Yifan Hong6aef2fc2020-01-09 14:41:11 -080025#include <android-base/logging.h>
Yifan Hong69c1b112018-02-27 17:06:00 -080026#include <android-base/parseint.h>
Yifan Hong6e32a5f2020-03-12 16:06:27 -070027#include <android-base/result.h>
Yifan Hong07292852019-08-21 15:40:30 -070028#include <android-base/strings.h>
Yifan Hong69c1b112018-02-27 17:06:00 -080029#include <utils/Errors.h>
Yifan Hong07292852019-08-21 15:40:30 -070030#include <vintf/KernelConfigParser.h>
Yifan Hong69c1b112018-02-27 17:06:00 -080031#include <vintf/VintfObject.h>
Yifan Hong07292852019-08-21 15:40:30 -070032#include <vintf/parse_string.h>
Yifan Honga72bde72017-09-28 13:36:48 -070033#include <vintf/parse_xml.h>
34#include "utils.h"
35
36namespace android {
37namespace vintf {
Yifan Hong69c1b112018-02-27 17:06:00 -080038namespace details {
Yifan Honga72bde72017-09-28 13:36:48 -070039
Yifan Hong69c1b112018-02-27 17:06:00 -080040// fake sysprops
41using Properties = std::map<std::string, std::string>;
42
Yifan Hong07292852019-08-21 15:40:30 -070043using Dirmap = std::map<std::string, std::string>;
44
Yifan Hong69c1b112018-02-27 17:06:00 -080045enum Option : int {
Yifan Hong0dfc3d52020-01-09 14:30:02 -080046 // Modes
Yifan Hong69c1b112018-02-27 17:06:00 -080047 HELP,
Yifan Hong0dfc3d52020-01-09 14:30:02 -080048 DUMP_FILE_LIST = 1,
Yifan Hong69c1b112018-02-27 17:06:00 -080049 CHECK_COMPAT,
Yifan Hong0dfc3d52020-01-09 14:30:02 -080050 CHECK_ONE,
51
52 // Options
53 ROOTDIR,
54 PROPERTY,
Yifan Hong07292852019-08-21 15:40:30 -070055 DIR_MAP,
56 KERNEL,
Yifan Hong69c1b112018-02-27 17:06:00 -080057};
58// command line arguments
59using Args = std::multimap<Option, std::string>;
60
Yifan Hong07292852019-08-21 15:40:30 -070061class HostFileSystem : public details::FileSystemImpl {
Yifan Hong69c1b112018-02-27 17:06:00 -080062 public:
Yifan Hong0dfc3d52020-01-09 14:30:02 -080063 HostFileSystem(const Dirmap& dirmap, status_t missingError)
64 : mDirMap(dirmap), mMissingError(missingError) {}
Yifan Hong10d86222018-04-06 15:41:05 -070065 status_t fetch(const std::string& path, std::string* fetched,
66 std::string* error) const override {
Yifan Hong6aef2fc2020-01-09 14:41:11 -080067 auto resolved = resolve(path, error);
Yifan Hong07292852019-08-21 15:40:30 -070068 if (resolved.empty()) {
Yifan Hong0dfc3d52020-01-09 14:30:02 -080069 return mMissingError;
Yifan Hong07292852019-08-21 15:40:30 -070070 }
71 status_t status = details::FileSystemImpl::fetch(resolved, fetched, error);
Yifan Hong6aef2fc2020-01-09 14:41:11 -080072 LOG(INFO) << "Fetch '" << resolved << "': " << toString(status);
Yifan Hong10d86222018-04-06 15:41:05 -070073 return status;
Yifan Hong69c1b112018-02-27 17:06:00 -080074 }
Yifan Hong10d86222018-04-06 15:41:05 -070075 status_t listFiles(const std::string& path, std::vector<std::string>* out,
76 std::string* error) const override {
Yifan Hong6aef2fc2020-01-09 14:41:11 -080077 auto resolved = resolve(path, error);
Yifan Hong07292852019-08-21 15:40:30 -070078 if (resolved.empty()) {
Yifan Hong0dfc3d52020-01-09 14:30:02 -080079 return mMissingError;
Yifan Hong07292852019-08-21 15:40:30 -070080 }
81 status_t status = details::FileSystemImpl::listFiles(resolved, out, error);
Yifan Hong6aef2fc2020-01-09 14:41:11 -080082 LOG(INFO) << "List '" << resolved << "': " << toString(status);
Yifan Hong69c1b112018-02-27 17:06:00 -080083 return status;
84 }
85
86 private:
Yifan Hong69c1b112018-02-27 17:06:00 -080087 static std::string toString(status_t status) {
88 return status == OK ? "SUCCESS" : strerror(-status);
89 }
Yifan Hong6aef2fc2020-01-09 14:41:11 -080090 std::string resolve(const std::string& path, std::string* error) const {
Yifan Hong07292852019-08-21 15:40:30 -070091 for (auto [prefix, mappedPath] : mDirMap) {
92 if (path == prefix) {
93 return mappedPath;
94 }
95 if (android::base::StartsWith(path, prefix + "/")) {
96 return mappedPath + "/" + path.substr(prefix.size() + 1);
97 }
98 }
Yifan Hong6aef2fc2020-01-09 14:41:11 -080099 if (error) {
100 *error = "Cannot resolve path " + path;
101 } else {
Yifan Hong0dfc3d52020-01-09 14:30:02 -0800102 LOG(mMissingError == NAME_NOT_FOUND ? INFO : ERROR) << "Cannot resolve path " << path;
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800103 }
Yifan Hong07292852019-08-21 15:40:30 -0700104 return "";
105 }
106
107 Dirmap mDirMap;
Yifan Hong0dfc3d52020-01-09 14:30:02 -0800108 status_t mMissingError;
Yifan Hong69c1b112018-02-27 17:06:00 -0800109};
110
111class PresetPropertyFetcher : public PropertyFetcher {
112 public:
113 std::string getProperty(const std::string& key,
114 const std::string& defaultValue) const override {
115 auto it = mProps.find(key);
116 if (it == mProps.end()) {
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800117 LOG(INFO) << "Sysprop " << key << " is missing, default to '" << defaultValue << "'";
Yifan Hong69c1b112018-02-27 17:06:00 -0800118 return defaultValue;
119 }
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800120 LOG(INFO) << "Sysprop " << key << "=" << it->second;
Yifan Hong69c1b112018-02-27 17:06:00 -0800121 return it->second;
122 }
123 uint64_t getUintProperty(const std::string& key, uint64_t defaultValue,
124 uint64_t max) const override {
125 uint64_t result;
126 std::string value = getProperty(key, "");
127 if (!value.empty() && android::base::ParseUint(value, &result, max)) return result;
128 return defaultValue;
129 }
130 bool getBoolProperty(const std::string& key, bool defaultValue) const override {
131 std::string value = getProperty(key, "");
132 if (value == "1" || value == "true") {
133 return true;
134 } else if (value == "0" || value == "false") {
135 return false;
136 }
137 return defaultValue;
138 }
139 void setProperties(const Properties& props) { mProps.insert(props.begin(), props.end()); }
140
141 private:
142 std::map<std::string, std::string> mProps;
143};
144
Yifan Hong07292852019-08-21 15:40:30 -0700145struct StaticRuntimeInfo : public RuntimeInfo {
146 KernelVersion kernelVersion;
147 std::string kernelConfigFile;
148
149 status_t fetchAllInformation(FetchFlags flags) override {
150 if (flags & RuntimeInfo::FetchFlag::CPU_VERSION) {
151 mKernel.mVersion = kernelVersion;
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800152 LOG(INFO) << "fetched kernel version " << kernelVersion;
Yifan Hong07292852019-08-21 15:40:30 -0700153 }
154 if (flags & RuntimeInfo::FetchFlag::CONFIG_GZ) {
155 std::string content;
156 if (!android::base::ReadFileToString(kernelConfigFile, &content)) {
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800157 LOG(ERROR) << "Cannot read " << kernelConfigFile;
Yifan Hong07292852019-08-21 15:40:30 -0700158 return UNKNOWN_ERROR;
159 }
160 KernelConfigParser parser;
161 auto status = parser.processAndFinish(content);
162 if (status != OK) {
163 return status;
164 }
165 mKernel.mConfigs = std::move(parser.configs());
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800166 LOG(INFO) << "read kernel configs from " << kernelConfigFile;
Yifan Hong07292852019-08-21 15:40:30 -0700167 }
168 if (flags & RuntimeInfo::FetchFlag::POLICYVERS) {
169 mKernelSepolicyVersion = SIZE_MAX;
170 }
171 return OK;
172 }
173};
174
175struct StubRuntimeInfo : public RuntimeInfo {
176 status_t fetchAllInformation(FetchFlags) override { return UNKNOWN_ERROR; }
177};
178
179struct StaticRuntimeInfoFactory : public ObjectFactory<RuntimeInfo> {
180 std::shared_ptr<RuntimeInfo> info;
181 StaticRuntimeInfoFactory(std::shared_ptr<RuntimeInfo> i) : info(i) {}
182 std::shared_ptr<RuntimeInfo> make_shared() const override {
183 if (info) return info;
184 return std::make_shared<StubRuntimeInfo>();
185 }
186};
187
Yifan Hong69c1b112018-02-27 17:06:00 -0800188// helper functions
Yifan Honga72bde72017-09-28 13:36:48 -0700189template <typename T>
Yifan Hong9f78c182018-07-12 14:45:52 -0700190std::unique_ptr<T> readObject(FileSystem* fileSystem, const std::string& path,
191 const XmlConverter<T>& converter) {
Yifan Honga72bde72017-09-28 13:36:48 -0700192 std::string xml;
Yifan Hong60217032018-01-08 16:19:42 -0800193 std::string error;
Yifan Hong9f78c182018-07-12 14:45:52 -0700194 status_t err = fileSystem->fetch(path, &xml, &error);
Yifan Honga72bde72017-09-28 13:36:48 -0700195 if (err != OK) {
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800196 LOG(ERROR) << "Cannot read '" << path << "' (" << strerror(-err) << "): " << error;
Yifan Honga72bde72017-09-28 13:36:48 -0700197 return nullptr;
198 }
199 auto ret = std::make_unique<T>();
Yifan Hong94757062018-02-09 16:36:31 -0800200 if (!converter(ret.get(), xml, &error)) {
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800201 LOG(ERROR) << "Cannot parse '" << path << "': " << error;
Yifan Honga72bde72017-09-28 13:36:48 -0700202 return nullptr;
203 }
204 return ret;
205}
206
Yifan Hong69c1b112018-02-27 17:06:00 -0800207int checkCompatibilityForFiles(const std::string& manifestPath, const std::string& matrixPath) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700208 auto fileSystem = std::make_unique<FileSystemImpl>();
209 auto manifest = readObject(fileSystem.get(), manifestPath, gHalManifestConverter);
210 auto matrix = readObject(fileSystem.get(), matrixPath, gCompatibilityMatrixConverter);
Yifan Honga72bde72017-09-28 13:36:48 -0700211 if (manifest == nullptr || matrix == nullptr) {
212 return -1;
213 }
214
215 std::string error;
216 if (!manifest->checkCompatibility(*matrix, &error)) {
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800217 LOG(ERROR) << "Incompatible: " << error;
Yifan Honga72bde72017-09-28 13:36:48 -0700218 std::cout << "false" << std::endl;
219 return 1;
220 }
221
222 std::cout << "true" << std::endl;
223 return 0;
224}
Yifan Hong69c1b112018-02-27 17:06:00 -0800225
226Args parseArgs(int argc, char** argv) {
227 int longOptFlag;
228 int optionIndex;
229 Args ret;
230 std::vector<struct option> longopts{
Yifan Hong0dfc3d52020-01-09 14:30:02 -0800231 // Modes
Yifan Hong69c1b112018-02-27 17:06:00 -0800232 {"help", no_argument, &longOptFlag, HELP},
Yifan Hong0dfc3d52020-01-09 14:30:02 -0800233 {"dump-file-list", no_argument, &longOptFlag, DUMP_FILE_LIST},
Yifan Hong69c1b112018-02-27 17:06:00 -0800234 {"check-compat", no_argument, &longOptFlag, CHECK_COMPAT},
Yifan Hong0dfc3d52020-01-09 14:30:02 -0800235 {"check-one", no_argument, &longOptFlag, CHECK_ONE},
236 // Options
237 {"rootdir", required_argument, &longOptFlag, ROOTDIR},
238 {"property", required_argument, &longOptFlag, PROPERTY},
Yifan Hong07292852019-08-21 15:40:30 -0700239 {"dirmap", required_argument, &longOptFlag, DIR_MAP},
240 {"kernel", required_argument, &longOptFlag, KERNEL},
Yifan Hong69c1b112018-02-27 17:06:00 -0800241 {0, 0, 0, 0}};
242 std::map<int, Option> shortopts{
243 {'h', HELP}, {'D', PROPERTY}, {'c', CHECK_COMPAT},
244 };
245 for (;;) {
246 int c = getopt_long(argc, argv, "hcD:", longopts.data(), &optionIndex);
247 if (c == -1) {
248 break;
249 }
250 std::string argValue = optarg ? optarg : std::string{};
251 if (c == 0) {
252 ret.emplace(static_cast<Option>(longOptFlag), std::move(argValue));
253 } else {
254 ret.emplace(shortopts[c], std::move(argValue));
255 }
256 }
257 if (optind < argc) {
258 // see non option
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800259 LOG(ERROR) << "unrecognized option `" << argv[optind] << "'";
Yifan Hong69c1b112018-02-27 17:06:00 -0800260 return {{HELP, ""}};
261 }
262 return ret;
263}
264
265template <typename T>
Yifan Hong07292852019-08-21 15:40:30 -0700266std::map<std::string, std::string> splitArgs(const T& args, char split) {
267 std::map<std::string, std::string> ret;
Yifan Hong69c1b112018-02-27 17:06:00 -0800268 for (const auto& arg : args) {
Yifan Hong07292852019-08-21 15:40:30 -0700269 auto pos = arg.find(split);
Yifan Hong69c1b112018-02-27 17:06:00 -0800270 auto key = arg.substr(0, pos);
271 auto value = pos == std::string::npos ? std::string{} : arg.substr(pos + 1);
272 ret[key] = value;
273 }
274 return ret;
275}
Yifan Hong07292852019-08-21 15:40:30 -0700276template <typename T>
277Properties getProperties(const T& args) {
278 return splitArgs(args, '=');
279}
280
281template <typename T>
282Dirmap getDirmap(const T& args) {
283 return splitArgs(args, ':');
284}
285
286template <typename T>
287std::shared_ptr<StaticRuntimeInfo> getRuntimeInfo(const T& args) {
288 auto ret = std::make_shared<StaticRuntimeInfo>();
289 if (std::distance(args.begin(), args.end()) > 1) {
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800290 LOG(ERROR) << "Can't have multiple --kernel options";
Yifan Hong07292852019-08-21 15:40:30 -0700291 return nullptr;
292 }
293 auto pair = android::base::Split(*args.begin(), ":");
294 if (pair.size() != 2) {
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800295 LOG(ERROR) << "Invalid --kernel";
Yifan Hong07292852019-08-21 15:40:30 -0700296 return nullptr;
297 }
298 if (!parse(pair[0], &ret->kernelVersion)) {
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800299 LOG(ERROR) << "Cannot parse " << pair[0] << " as kernel version";
Yifan Hong07292852019-08-21 15:40:30 -0700300 return nullptr;
301 }
302 ret->kernelConfigFile = std::move(pair[1]);
303 return ret;
304}
Yifan Hong69c1b112018-02-27 17:06:00 -0800305
306int usage(const char* me) {
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800307 LOG(ERROR)
Yifan Hong69c1b112018-02-27 17:06:00 -0800308 << me << ": check VINTF metadata." << std::endl
Yifan Hong0dfc3d52020-01-09 14:30:02 -0800309 << " Modes:" << std::endl
Yifan Hong69c1b112018-02-27 17:06:00 -0800310 << " --dump-file-list: Dump a list of directories / files on device" << std::endl
311 << " that is required to be used by --check-compat." << std::endl
312 << " -c, --check-compat: check compatibility for files under the root" << std::endl
313 << " directory specified by --root-dir." << std::endl
Yifan Hong0dfc3d52020-01-09 14:30:02 -0800314 << " --check-one: check consistency of VINTF metadata for a single partition."
315 << std::endl
316 << std::endl
317 << " Options:" << std::endl
Yifan Hong07292852019-08-21 15:40:30 -0700318 << " --rootdir=<dir>: specify root directory for all metadata. Same as " << std::endl
319 << " --dirmap /:<dir>" << std::endl
Yifan Hong69c1b112018-02-27 17:06:00 -0800320 << " -D, --property <key>=<value>: specify sysprops." << std::endl
Yifan Hong07292852019-08-21 15:40:30 -0700321 << " --dirmap </system:/dir/to/system> [--dirmap </vendor:/dir/to/vendor>[...]]"
322 << std::endl
323 << " Map partitions to directories. Cannot be specified with --rootdir."
324 << " --kernel <x.y.z:path/to/config>" << std::endl
325 << " Use the given kernel version and config to check. If" << std::endl
326 << " unspecified, kernel requirements are skipped." << std::endl
327 << std::endl
Yifan Hong69c1b112018-02-27 17:06:00 -0800328 << " --help: show this message." << std::endl
329 << std::endl
330 << " Example:" << std::endl
331 << " # Get the list of required files." << std::endl
332 << " " << me << " --dump-file-list > /tmp/files.txt" << std::endl
333 << " # Pull from ADB, or use your own command to extract files from images"
334 << std::endl
335 << " ROOTDIR=/tmp/device/" << std::endl
336 << " cat /tmp/files.txt | xargs -I{} bash -c \"mkdir -p $ROOTDIR`dirname {}` && adb "
337 "pull {} $ROOTDIR{}\""
338 << std::endl
339 << " # Check compatibility." << std::endl
340 << " " << me << " --check-compat --rootdir=$ROOTDIR \\" << std::endl
341 << " --property ro.product.first_api_level=`adb shell getprop "
342 "ro.product.first_api_level` \\"
343 << std::endl
344 << " --property ro.boot.product.hardware.sku=`adb shell getprop "
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800345 "ro.boot.product.hardware.sku`";
Yifan Hong62503e12019-08-26 12:47:41 -0700346 return EX_USAGE;
Yifan Hong69c1b112018-02-27 17:06:00 -0800347}
348
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700349android::base::Result<void> checkAllFiles(const Dirmap& dirmap, const Properties& props,
350 std::shared_ptr<StaticRuntimeInfo> runtimeInfo) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700351 auto hostPropertyFetcher = std::make_unique<PresetPropertyFetcher>();
352 hostPropertyFetcher->setProperties(props);
Yifan Hong07292852019-08-21 15:40:30 -0700353
354 CheckFlags::Type flags = CheckFlags::DEFAULT;
355 if (!runtimeInfo) flags = flags.disableRuntimeInfo();
356
357 auto vintfObject =
358 VintfObject::Builder()
Yifan Hong0dfc3d52020-01-09 14:30:02 -0800359 .setFileSystem(std::make_unique<HostFileSystem>(dirmap, UNKNOWN_ERROR))
Yifan Hong07292852019-08-21 15:40:30 -0700360 .setPropertyFetcher(std::move(hostPropertyFetcher))
361 .setRuntimeInfoFactory(std::make_unique<StaticRuntimeInfoFactory>(runtimeInfo))
362 .build();
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700363
364 std::string error;
365 int compatibleResult = vintfObject->checkCompatibility(&error, flags);
366 if (compatibleResult == INCOMPATIBLE) {
367 return android::base::Error() << error;
368 }
369 if (compatibleResult != COMPATIBLE) {
370 return android::base::Error(-compatibleResult) << error;
371 }
372
373 auto hasFcmExt = vintfObject->hasFrameworkCompatibilityMatrixExtensions();
374 if (!hasFcmExt.has_value()) {
375 return hasFcmExt.error();
376 }
Yifan Hong76f85852020-03-23 16:32:40 +0000377 auto deviceManifest = vintfObject->getDeviceHalManifest();
378 if (deviceManifest == nullptr) {
379 return android::base::Error(-NAME_NOT_FOUND) << "No device HAL manifest";
380 }
381 auto targetFcm = deviceManifest->level();
382 if (*hasFcmExt || (targetFcm != Level::UNSPECIFIED && targetFcm >= Level::R)) {
Ryanne Chengd324f842020-03-23 01:39:13 +0000383 return vintfObject->checkUnusedHals();
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700384 }
385 LOG(INFO) << "Skip checking unused HALs.";
386 return {};
Yifan Hong69c1b112018-02-27 17:06:00 -0800387}
388
Yifan Hong0dfc3d52020-01-09 14:30:02 -0800389int checkDirmaps(const Dirmap& dirmap) {
390 auto exitCode = EX_OK;
391 for (auto&& [prefix, mappedPath] : dirmap) {
392 auto vintfObject =
393 VintfObject::Builder()
394 .setFileSystem(std::make_unique<HostFileSystem>(dirmap, NAME_NOT_FOUND))
395 .setPropertyFetcher(std::make_unique<PropertyFetcherNoOp>())
396 .setRuntimeInfoFactory(std::make_unique<StaticRuntimeInfoFactory>(nullptr))
397 .build();
398
399 if (android::base::StartsWith(prefix, "/system")) {
400 LOG(INFO) << "Checking system manifest.";
401 auto manifest = vintfObject->getFrameworkHalManifest();
402 if (!manifest) {
403 LOG(ERROR) << "Cannot fetch system manifest.";
404 exitCode = EX_SOFTWARE;
405 }
406 LOG(INFO) << "Checking system matrix.";
407 auto matrix = vintfObject->getFrameworkCompatibilityMatrix();
408 if (!matrix) {
409 LOG(ERROR) << "Cannot fetch system matrix.";
410 exitCode = EX_SOFTWARE;
411 }
412 continue;
413 }
414
415 if (android::base::StartsWith(prefix, "/vendor")) {
416 LOG(INFO) << "Checking vendor manifest.";
417 auto manifest = vintfObject->getDeviceHalManifest();
418 if (!manifest) {
419 LOG(ERROR) << "Cannot fetch vendor manifest.";
420 exitCode = EX_SOFTWARE;
421 }
422 LOG(INFO) << "Checking vendor matrix.";
423 auto matrix = vintfObject->getDeviceCompatibilityMatrix();
424 if (!matrix) {
425 LOG(ERROR) << "Cannot fetch vendor matrix.";
426 exitCode = EX_SOFTWARE;
427 }
428 continue;
429 }
430
431 LOG(ERROR) << "--check-one does not work with --dirmap " << prefix;
432 exitCode = EX_SOFTWARE;
433 }
434 return exitCode;
435}
436
Yifan Hong69c1b112018-02-27 17:06:00 -0800437} // namespace details
438} // namespace vintf
439} // namespace android
440
441int main(int argc, char** argv) {
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800442 android::base::SetLogger(android::base::StderrLogger);
443
Yifan Hong69c1b112018-02-27 17:06:00 -0800444 using namespace android::vintf;
445 using namespace android::vintf::details;
446 // legacy usage: check_vintf <manifest.xml> <matrix.xml>
Yifan Hong0dfc3d52020-01-09 14:30:02 -0800447 if (argc == 3 && *argv[1] != '-' && *argv[2] != '-') {
Yifan Hong69c1b112018-02-27 17:06:00 -0800448 int ret = checkCompatibilityForFiles(argv[1], argv[2]);
449 if (ret >= 0) return ret;
450 }
451
452 Args args = parseArgs(argc, argv);
453
454 if (!iterateValues(args, HELP).empty()) {
455 return usage(argv[0]);
456 }
457
458 if (!iterateValues(args, DUMP_FILE_LIST).empty()) {
459 for (const auto& file : dumpFileList()) {
460 std::cout << file << std::endl;
461 }
462 return 0;
463 }
464
Yifan Hong0dfc3d52020-01-09 14:30:02 -0800465 auto dirmap = getDirmap(iterateValues(args, DIR_MAP));
466
467 if (!iterateValues(args, CHECK_ONE).empty()) {
468 return checkDirmaps(dirmap);
469 }
470
Yifan Hong69c1b112018-02-27 17:06:00 -0800471 auto checkCompat = iterateValues(args, CHECK_COMPAT);
Yifan Hong07292852019-08-21 15:40:30 -0700472 if (checkCompat.empty()) {
473 return usage(argv[0]);
Yifan Hong69c1b112018-02-27 17:06:00 -0800474 }
475
Yifan Hong07292852019-08-21 15:40:30 -0700476 auto rootdirs = iterateValues(args, ROOTDIR);
477 if (!rootdirs.empty()) {
478 if (std::distance(rootdirs.begin(), rootdirs.end()) > 1) {
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800479 LOG(ERROR) << "Can't have multiple --rootdir options";
Yifan Hong07292852019-08-21 15:40:30 -0700480 return usage(argv[0]);
481 }
482 args.emplace(DIR_MAP, "/:" + *rootdirs.begin());
483 }
484
485 auto properties = getProperties(iterateValues(args, PROPERTY));
Yifan Hong07292852019-08-21 15:40:30 -0700486
487 std::shared_ptr<StaticRuntimeInfo> runtimeInfo;
488 auto kernelArgs = iterateValues(args, KERNEL);
489 if (!kernelArgs.empty()) {
490 runtimeInfo = getRuntimeInfo(kernelArgs);
491 if (runtimeInfo == nullptr) {
492 return usage(argv[0]);
493 }
494 }
495
Yifan Hong07292852019-08-21 15:40:30 -0700496 if (dirmap.empty()) {
Yifan Hong6aef2fc2020-01-09 14:41:11 -0800497 LOG(ERROR) << "Missing --rootdir or --dirmap option.";
Yifan Hong07292852019-08-21 15:40:30 -0700498 return usage(argv[0]);
499 }
500
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700501 auto compat = checkAllFiles(dirmap, properties, runtimeInfo);
Yifan Hong62503e12019-08-26 12:47:41 -0700502
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700503 if (compat.ok()) {
Yifan Hong62503e12019-08-26 12:47:41 -0700504 std::cout << "COMPATIBLE" << std::endl;
505 return EX_OK;
506 }
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700507 if (compat.error().code() == 0) {
508 LOG(ERROR) << "files are incompatible: " << compat.error();
Yifan Hong62503e12019-08-26 12:47:41 -0700509 std::cout << "INCOMPATIBLE" << std::endl;
510 return EX_DATAERR;
511 }
Yifan Hong6e32a5f2020-03-12 16:06:27 -0700512 LOG(ERROR) << strerror(compat.error().code()) << ": " << compat.error();
Yifan Hong62503e12019-08-26 12:47:41 -0700513 return EX_SOFTWARE;
Yifan Hong69c1b112018-02-27 17:06:00 -0800514}