Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 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 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 17 | #include <getopt.h> |
Yifan Hong | 62503e1 | 2019-08-26 12:47:41 -0700 | [diff] [blame] | 18 | #include <sysexits.h> |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 19 | #include <unistd.h> |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 20 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 21 | #include <iostream> |
| 22 | #include <map> |
| 23 | |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 24 | #include <android-base/file.h> |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 25 | #include <android-base/logging.h> |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 26 | #include <android-base/parseint.h> |
Yifan Hong | 6e32a5f | 2020-03-12 16:06:27 -0700 | [diff] [blame] | 27 | #include <android-base/result.h> |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 28 | #include <android-base/strings.h> |
Yifan Hong | 28414a1 | 2020-03-23 16:32:40 +0000 | [diff] [blame] | 29 | #include <hidl/metadata.h> |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 30 | #include <utils/Errors.h> |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 31 | #include <vintf/KernelConfigParser.h> |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 32 | #include <vintf/VintfObject.h> |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 33 | #include <vintf/parse_string.h> |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 34 | #include <vintf/parse_xml.h> |
| 35 | #include "utils.h" |
| 36 | |
| 37 | namespace android { |
| 38 | namespace vintf { |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 39 | namespace details { |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 40 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 41 | // fake sysprops |
| 42 | using Properties = std::map<std::string, std::string>; |
| 43 | |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 44 | using Dirmap = std::map<std::string, std::string>; |
| 45 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 46 | enum Option : int { |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 47 | // Modes |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 48 | HELP, |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 49 | DUMP_FILE_LIST = 1, |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 50 | CHECK_COMPAT, |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 51 | CHECK_ONE, |
| 52 | |
| 53 | // Options |
| 54 | ROOTDIR, |
| 55 | PROPERTY, |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 56 | DIR_MAP, |
| 57 | KERNEL, |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 58 | }; |
| 59 | // command line arguments |
| 60 | using Args = std::multimap<Option, std::string>; |
| 61 | |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 62 | class HostFileSystem : public details::FileSystemImpl { |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 63 | public: |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 64 | HostFileSystem(const Dirmap& dirmap, status_t missingError) |
| 65 | : mDirMap(dirmap), mMissingError(missingError) {} |
Yifan Hong | 10d8622 | 2018-04-06 15:41:05 -0700 | [diff] [blame] | 66 | status_t fetch(const std::string& path, std::string* fetched, |
| 67 | std::string* error) const override { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 68 | auto resolved = resolve(path, error); |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 69 | if (resolved.empty()) { |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 70 | return mMissingError; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 71 | } |
| 72 | status_t status = details::FileSystemImpl::fetch(resolved, fetched, error); |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 73 | LOG(INFO) << "Fetch '" << resolved << "': " << toString(status); |
Yifan Hong | 10d8622 | 2018-04-06 15:41:05 -0700 | [diff] [blame] | 74 | return status; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 75 | } |
Yifan Hong | 10d8622 | 2018-04-06 15:41:05 -0700 | [diff] [blame] | 76 | status_t listFiles(const std::string& path, std::vector<std::string>* out, |
| 77 | std::string* error) const override { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 78 | auto resolved = resolve(path, error); |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 79 | if (resolved.empty()) { |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 80 | return mMissingError; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 81 | } |
| 82 | status_t status = details::FileSystemImpl::listFiles(resolved, out, error); |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 83 | LOG(INFO) << "List '" << resolved << "': " << toString(status); |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 84 | return status; |
| 85 | } |
| 86 | |
| 87 | private: |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 88 | static std::string toString(status_t status) { |
| 89 | return status == OK ? "SUCCESS" : strerror(-status); |
| 90 | } |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 91 | std::string resolve(const std::string& path, std::string* error) const { |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 92 | for (auto [prefix, mappedPath] : mDirMap) { |
| 93 | if (path == prefix) { |
| 94 | return mappedPath; |
| 95 | } |
| 96 | if (android::base::StartsWith(path, prefix + "/")) { |
| 97 | return mappedPath + "/" + path.substr(prefix.size() + 1); |
| 98 | } |
| 99 | } |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 100 | if (error) { |
| 101 | *error = "Cannot resolve path " + path; |
| 102 | } else { |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 103 | LOG(mMissingError == NAME_NOT_FOUND ? INFO : ERROR) << "Cannot resolve path " << path; |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 104 | } |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 105 | return ""; |
| 106 | } |
| 107 | |
| 108 | Dirmap mDirMap; |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 109 | status_t mMissingError; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | class PresetPropertyFetcher : public PropertyFetcher { |
| 113 | public: |
| 114 | std::string getProperty(const std::string& key, |
| 115 | const std::string& defaultValue) const override { |
| 116 | auto it = mProps.find(key); |
| 117 | if (it == mProps.end()) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 118 | LOG(INFO) << "Sysprop " << key << " is missing, default to '" << defaultValue << "'"; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 119 | return defaultValue; |
| 120 | } |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 121 | LOG(INFO) << "Sysprop " << key << "=" << it->second; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 122 | return it->second; |
| 123 | } |
| 124 | uint64_t getUintProperty(const std::string& key, uint64_t defaultValue, |
| 125 | uint64_t max) const override { |
| 126 | uint64_t result; |
| 127 | std::string value = getProperty(key, ""); |
| 128 | if (!value.empty() && android::base::ParseUint(value, &result, max)) return result; |
| 129 | return defaultValue; |
| 130 | } |
| 131 | bool getBoolProperty(const std::string& key, bool defaultValue) const override { |
| 132 | std::string value = getProperty(key, ""); |
| 133 | if (value == "1" || value == "true") { |
| 134 | return true; |
| 135 | } else if (value == "0" || value == "false") { |
| 136 | return false; |
| 137 | } |
| 138 | return defaultValue; |
| 139 | } |
| 140 | void setProperties(const Properties& props) { mProps.insert(props.begin(), props.end()); } |
| 141 | |
| 142 | private: |
| 143 | std::map<std::string, std::string> mProps; |
| 144 | }; |
| 145 | |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 146 | struct StaticRuntimeInfo : public RuntimeInfo { |
| 147 | KernelVersion kernelVersion; |
| 148 | std::string kernelConfigFile; |
| 149 | |
| 150 | status_t fetchAllInformation(FetchFlags flags) override { |
| 151 | if (flags & RuntimeInfo::FetchFlag::CPU_VERSION) { |
| 152 | mKernel.mVersion = kernelVersion; |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 153 | LOG(INFO) << "fetched kernel version " << kernelVersion; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 154 | } |
| 155 | if (flags & RuntimeInfo::FetchFlag::CONFIG_GZ) { |
| 156 | std::string content; |
| 157 | if (!android::base::ReadFileToString(kernelConfigFile, &content)) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 158 | LOG(ERROR) << "Cannot read " << kernelConfigFile; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 159 | return UNKNOWN_ERROR; |
| 160 | } |
| 161 | KernelConfigParser parser; |
| 162 | auto status = parser.processAndFinish(content); |
| 163 | if (status != OK) { |
| 164 | return status; |
| 165 | } |
| 166 | mKernel.mConfigs = std::move(parser.configs()); |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 167 | LOG(INFO) << "read kernel configs from " << kernelConfigFile; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 168 | } |
| 169 | if (flags & RuntimeInfo::FetchFlag::POLICYVERS) { |
| 170 | mKernelSepolicyVersion = SIZE_MAX; |
| 171 | } |
| 172 | return OK; |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | struct StubRuntimeInfo : public RuntimeInfo { |
| 177 | status_t fetchAllInformation(FetchFlags) override { return UNKNOWN_ERROR; } |
| 178 | }; |
| 179 | |
| 180 | struct StaticRuntimeInfoFactory : public ObjectFactory<RuntimeInfo> { |
| 181 | std::shared_ptr<RuntimeInfo> info; |
| 182 | StaticRuntimeInfoFactory(std::shared_ptr<RuntimeInfo> i) : info(i) {} |
| 183 | std::shared_ptr<RuntimeInfo> make_shared() const override { |
| 184 | if (info) return info; |
| 185 | return std::make_shared<StubRuntimeInfo>(); |
| 186 | } |
| 187 | }; |
| 188 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 189 | // helper functions |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 190 | template <typename T> |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 191 | std::unique_ptr<T> readObject(FileSystem* fileSystem, const std::string& path, |
| 192 | const XmlConverter<T>& converter) { |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 193 | std::string xml; |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 194 | std::string error; |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 195 | status_t err = fileSystem->fetch(path, &xml, &error); |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 196 | if (err != OK) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 197 | LOG(ERROR) << "Cannot read '" << path << "' (" << strerror(-err) << "): " << error; |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 198 | return nullptr; |
| 199 | } |
| 200 | auto ret = std::make_unique<T>(); |
Yifan Hong | 9475706 | 2018-02-09 16:36:31 -0800 | [diff] [blame] | 201 | if (!converter(ret.get(), xml, &error)) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 202 | LOG(ERROR) << "Cannot parse '" << path << "': " << error; |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 203 | return nullptr; |
| 204 | } |
| 205 | return ret; |
| 206 | } |
| 207 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 208 | int checkCompatibilityForFiles(const std::string& manifestPath, const std::string& matrixPath) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 209 | auto fileSystem = std::make_unique<FileSystemImpl>(); |
| 210 | auto manifest = readObject(fileSystem.get(), manifestPath, gHalManifestConverter); |
| 211 | auto matrix = readObject(fileSystem.get(), matrixPath, gCompatibilityMatrixConverter); |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 212 | if (manifest == nullptr || matrix == nullptr) { |
| 213 | return -1; |
| 214 | } |
| 215 | |
| 216 | std::string error; |
| 217 | if (!manifest->checkCompatibility(*matrix, &error)) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 218 | LOG(ERROR) << "Incompatible: " << error; |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 219 | std::cout << "false" << std::endl; |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | std::cout << "true" << std::endl; |
| 224 | return 0; |
| 225 | } |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 226 | |
| 227 | Args parseArgs(int argc, char** argv) { |
| 228 | int longOptFlag; |
| 229 | int optionIndex; |
| 230 | Args ret; |
| 231 | std::vector<struct option> longopts{ |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 232 | // Modes |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 233 | {"help", no_argument, &longOptFlag, HELP}, |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 234 | {"dump-file-list", no_argument, &longOptFlag, DUMP_FILE_LIST}, |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 235 | {"check-compat", no_argument, &longOptFlag, CHECK_COMPAT}, |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 236 | {"check-one", no_argument, &longOptFlag, CHECK_ONE}, |
| 237 | // Options |
| 238 | {"rootdir", required_argument, &longOptFlag, ROOTDIR}, |
| 239 | {"property", required_argument, &longOptFlag, PROPERTY}, |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 240 | {"dirmap", required_argument, &longOptFlag, DIR_MAP}, |
| 241 | {"kernel", required_argument, &longOptFlag, KERNEL}, |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 242 | {0, 0, 0, 0}}; |
| 243 | std::map<int, Option> shortopts{ |
| 244 | {'h', HELP}, {'D', PROPERTY}, {'c', CHECK_COMPAT}, |
| 245 | }; |
| 246 | for (;;) { |
| 247 | int c = getopt_long(argc, argv, "hcD:", longopts.data(), &optionIndex); |
| 248 | if (c == -1) { |
| 249 | break; |
| 250 | } |
| 251 | std::string argValue = optarg ? optarg : std::string{}; |
| 252 | if (c == 0) { |
| 253 | ret.emplace(static_cast<Option>(longOptFlag), std::move(argValue)); |
| 254 | } else { |
| 255 | ret.emplace(shortopts[c], std::move(argValue)); |
| 256 | } |
| 257 | } |
| 258 | if (optind < argc) { |
| 259 | // see non option |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 260 | LOG(ERROR) << "unrecognized option `" << argv[optind] << "'"; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 261 | return {{HELP, ""}}; |
| 262 | } |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | template <typename T> |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 267 | std::map<std::string, std::string> splitArgs(const T& args, char split) { |
| 268 | std::map<std::string, std::string> ret; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 269 | for (const auto& arg : args) { |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 270 | auto pos = arg.find(split); |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 271 | auto key = arg.substr(0, pos); |
| 272 | auto value = pos == std::string::npos ? std::string{} : arg.substr(pos + 1); |
| 273 | ret[key] = value; |
| 274 | } |
| 275 | return ret; |
| 276 | } |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 277 | template <typename T> |
| 278 | Properties getProperties(const T& args) { |
| 279 | return splitArgs(args, '='); |
| 280 | } |
| 281 | |
| 282 | template <typename T> |
| 283 | Dirmap getDirmap(const T& args) { |
| 284 | return splitArgs(args, ':'); |
| 285 | } |
| 286 | |
| 287 | template <typename T> |
| 288 | std::shared_ptr<StaticRuntimeInfo> getRuntimeInfo(const T& args) { |
| 289 | auto ret = std::make_shared<StaticRuntimeInfo>(); |
| 290 | if (std::distance(args.begin(), args.end()) > 1) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 291 | LOG(ERROR) << "Can't have multiple --kernel options"; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 292 | return nullptr; |
| 293 | } |
| 294 | auto pair = android::base::Split(*args.begin(), ":"); |
| 295 | if (pair.size() != 2) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 296 | LOG(ERROR) << "Invalid --kernel"; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 297 | return nullptr; |
| 298 | } |
| 299 | if (!parse(pair[0], &ret->kernelVersion)) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 300 | LOG(ERROR) << "Cannot parse " << pair[0] << " as kernel version"; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 301 | return nullptr; |
| 302 | } |
| 303 | ret->kernelConfigFile = std::move(pair[1]); |
| 304 | return ret; |
| 305 | } |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 306 | |
| 307 | int usage(const char* me) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 308 | LOG(ERROR) |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 309 | << me << ": check VINTF metadata." << std::endl |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 310 | << " Modes:" << std::endl |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 311 | << " --dump-file-list: Dump a list of directories / files on device" << std::endl |
| 312 | << " that is required to be used by --check-compat." << std::endl |
| 313 | << " -c, --check-compat: check compatibility for files under the root" << std::endl |
| 314 | << " directory specified by --root-dir." << std::endl |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 315 | << " --check-one: check consistency of VINTF metadata for a single partition." |
| 316 | << std::endl |
| 317 | << std::endl |
| 318 | << " Options:" << std::endl |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 319 | << " --rootdir=<dir>: specify root directory for all metadata. Same as " << std::endl |
| 320 | << " --dirmap /:<dir>" << std::endl |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 321 | << " -D, --property <key>=<value>: specify sysprops." << std::endl |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 322 | << " --dirmap </system:/dir/to/system> [--dirmap </vendor:/dir/to/vendor>[...]]" |
| 323 | << std::endl |
| 324 | << " Map partitions to directories. Cannot be specified with --rootdir." |
| 325 | << " --kernel <x.y.z:path/to/config>" << std::endl |
| 326 | << " Use the given kernel version and config to check. If" << std::endl |
| 327 | << " unspecified, kernel requirements are skipped." << std::endl |
| 328 | << std::endl |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 329 | << " --help: show this message." << std::endl |
| 330 | << std::endl |
| 331 | << " Example:" << std::endl |
| 332 | << " # Get the list of required files." << std::endl |
| 333 | << " " << me << " --dump-file-list > /tmp/files.txt" << std::endl |
| 334 | << " # Pull from ADB, or use your own command to extract files from images" |
| 335 | << std::endl |
| 336 | << " ROOTDIR=/tmp/device/" << std::endl |
| 337 | << " cat /tmp/files.txt | xargs -I{} bash -c \"mkdir -p $ROOTDIR`dirname {}` && adb " |
| 338 | "pull {} $ROOTDIR{}\"" |
| 339 | << std::endl |
| 340 | << " # Check compatibility." << std::endl |
| 341 | << " " << me << " --check-compat --rootdir=$ROOTDIR \\" << std::endl |
| 342 | << " --property ro.product.first_api_level=`adb shell getprop " |
| 343 | "ro.product.first_api_level` \\" |
| 344 | << std::endl |
| 345 | << " --property ro.boot.product.hardware.sku=`adb shell getprop " |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 346 | "ro.boot.product.hardware.sku`"; |
Yifan Hong | 62503e1 | 2019-08-26 12:47:41 -0700 | [diff] [blame] | 347 | return EX_USAGE; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Yifan Hong | 6e32a5f | 2020-03-12 16:06:27 -0700 | [diff] [blame] | 350 | android::base::Result<void> checkAllFiles(const Dirmap& dirmap, const Properties& props, |
| 351 | std::shared_ptr<StaticRuntimeInfo> runtimeInfo) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 352 | auto hostPropertyFetcher = std::make_unique<PresetPropertyFetcher>(); |
| 353 | hostPropertyFetcher->setProperties(props); |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 354 | |
| 355 | CheckFlags::Type flags = CheckFlags::DEFAULT; |
| 356 | if (!runtimeInfo) flags = flags.disableRuntimeInfo(); |
| 357 | |
| 358 | auto vintfObject = |
| 359 | VintfObject::Builder() |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 360 | .setFileSystem(std::make_unique<HostFileSystem>(dirmap, UNKNOWN_ERROR)) |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 361 | .setPropertyFetcher(std::move(hostPropertyFetcher)) |
| 362 | .setRuntimeInfoFactory(std::make_unique<StaticRuntimeInfoFactory>(runtimeInfo)) |
| 363 | .build(); |
Yifan Hong | 6e32a5f | 2020-03-12 16:06:27 -0700 | [diff] [blame] | 364 | |
| 365 | std::string error; |
| 366 | int compatibleResult = vintfObject->checkCompatibility(&error, flags); |
| 367 | if (compatibleResult == INCOMPATIBLE) { |
| 368 | return android::base::Error() << error; |
| 369 | } |
| 370 | if (compatibleResult != COMPATIBLE) { |
| 371 | return android::base::Error(-compatibleResult) << error; |
| 372 | } |
| 373 | |
| 374 | auto hasFcmExt = vintfObject->hasFrameworkCompatibilityMatrixExtensions(); |
| 375 | if (!hasFcmExt.has_value()) { |
| 376 | return hasFcmExt.error(); |
| 377 | } |
Yifan Hong | 76f8585 | 2020-03-23 16:32:40 +0000 | [diff] [blame] | 378 | auto deviceManifest = vintfObject->getDeviceHalManifest(); |
| 379 | if (deviceManifest == nullptr) { |
| 380 | return android::base::Error(-NAME_NOT_FOUND) << "No device HAL manifest"; |
| 381 | } |
| 382 | auto targetFcm = deviceManifest->level(); |
| 383 | if (*hasFcmExt || (targetFcm != Level::UNSPECIFIED && targetFcm >= Level::R)) { |
Yifan Hong | 28414a1 | 2020-03-23 16:32:40 +0000 | [diff] [blame] | 384 | auto hidlMetadata = HidlInterfaceMetadata::all(); |
| 385 | return vintfObject->checkUnusedHals(hidlMetadata); |
Yifan Hong | 6e32a5f | 2020-03-12 16:06:27 -0700 | [diff] [blame] | 386 | } |
| 387 | LOG(INFO) << "Skip checking unused HALs."; |
| 388 | return {}; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Yifan Hong | 3b4a3ea | 2020-03-13 18:44:22 -0700 | [diff] [blame] | 391 | int checkDirmaps(const Dirmap& dirmap, const Properties& props) { |
| 392 | auto hostPropertyFetcher = std::make_unique<PresetPropertyFetcher>(); |
| 393 | hostPropertyFetcher->setProperties(props); |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 394 | auto exitCode = EX_OK; |
| 395 | for (auto&& [prefix, mappedPath] : dirmap) { |
| 396 | auto vintfObject = |
| 397 | VintfObject::Builder() |
| 398 | .setFileSystem(std::make_unique<HostFileSystem>(dirmap, NAME_NOT_FOUND)) |
Yifan Hong | 3b4a3ea | 2020-03-13 18:44:22 -0700 | [diff] [blame] | 399 | .setPropertyFetcher(std::move(hostPropertyFetcher)) |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 400 | .setRuntimeInfoFactory(std::make_unique<StaticRuntimeInfoFactory>(nullptr)) |
| 401 | .build(); |
| 402 | |
| 403 | if (android::base::StartsWith(prefix, "/system")) { |
| 404 | LOG(INFO) << "Checking system manifest."; |
| 405 | auto manifest = vintfObject->getFrameworkHalManifest(); |
| 406 | if (!manifest) { |
| 407 | LOG(ERROR) << "Cannot fetch system manifest."; |
| 408 | exitCode = EX_SOFTWARE; |
| 409 | } |
| 410 | LOG(INFO) << "Checking system matrix."; |
| 411 | auto matrix = vintfObject->getFrameworkCompatibilityMatrix(); |
| 412 | if (!matrix) { |
| 413 | LOG(ERROR) << "Cannot fetch system matrix."; |
| 414 | exitCode = EX_SOFTWARE; |
| 415 | } |
| 416 | continue; |
| 417 | } |
| 418 | |
| 419 | if (android::base::StartsWith(prefix, "/vendor")) { |
| 420 | LOG(INFO) << "Checking vendor manifest."; |
| 421 | auto manifest = vintfObject->getDeviceHalManifest(); |
| 422 | if (!manifest) { |
| 423 | LOG(ERROR) << "Cannot fetch vendor manifest."; |
| 424 | exitCode = EX_SOFTWARE; |
| 425 | } |
| 426 | LOG(INFO) << "Checking vendor matrix."; |
| 427 | auto matrix = vintfObject->getDeviceCompatibilityMatrix(); |
| 428 | if (!matrix) { |
| 429 | LOG(ERROR) << "Cannot fetch vendor matrix."; |
| 430 | exitCode = EX_SOFTWARE; |
| 431 | } |
| 432 | continue; |
| 433 | } |
| 434 | |
| 435 | LOG(ERROR) << "--check-one does not work with --dirmap " << prefix; |
| 436 | exitCode = EX_SOFTWARE; |
| 437 | } |
| 438 | return exitCode; |
| 439 | } |
| 440 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 441 | } // namespace details |
| 442 | } // namespace vintf |
| 443 | } // namespace android |
| 444 | |
| 445 | int main(int argc, char** argv) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 446 | android::base::SetLogger(android::base::StderrLogger); |
| 447 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 448 | using namespace android::vintf; |
| 449 | using namespace android::vintf::details; |
| 450 | // legacy usage: check_vintf <manifest.xml> <matrix.xml> |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 451 | if (argc == 3 && *argv[1] != '-' && *argv[2] != '-') { |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 452 | int ret = checkCompatibilityForFiles(argv[1], argv[2]); |
| 453 | if (ret >= 0) return ret; |
| 454 | } |
| 455 | |
| 456 | Args args = parseArgs(argc, argv); |
| 457 | |
| 458 | if (!iterateValues(args, HELP).empty()) { |
| 459 | return usage(argv[0]); |
| 460 | } |
| 461 | |
| 462 | if (!iterateValues(args, DUMP_FILE_LIST).empty()) { |
| 463 | for (const auto& file : dumpFileList()) { |
| 464 | std::cout << file << std::endl; |
| 465 | } |
| 466 | return 0; |
| 467 | } |
| 468 | |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 469 | auto dirmap = getDirmap(iterateValues(args, DIR_MAP)); |
Yifan Hong | 3b4a3ea | 2020-03-13 18:44:22 -0700 | [diff] [blame] | 470 | auto properties = getProperties(iterateValues(args, PROPERTY)); |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 471 | |
| 472 | if (!iterateValues(args, CHECK_ONE).empty()) { |
Yifan Hong | 3b4a3ea | 2020-03-13 18:44:22 -0700 | [diff] [blame] | 473 | return checkDirmaps(dirmap, properties); |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 474 | } |
| 475 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 476 | auto checkCompat = iterateValues(args, CHECK_COMPAT); |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 477 | if (checkCompat.empty()) { |
| 478 | return usage(argv[0]); |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 481 | auto rootdirs = iterateValues(args, ROOTDIR); |
| 482 | if (!rootdirs.empty()) { |
| 483 | if (std::distance(rootdirs.begin(), rootdirs.end()) > 1) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 484 | LOG(ERROR) << "Can't have multiple --rootdir options"; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 485 | return usage(argv[0]); |
| 486 | } |
| 487 | args.emplace(DIR_MAP, "/:" + *rootdirs.begin()); |
| 488 | } |
| 489 | |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 490 | std::shared_ptr<StaticRuntimeInfo> runtimeInfo; |
| 491 | auto kernelArgs = iterateValues(args, KERNEL); |
| 492 | if (!kernelArgs.empty()) { |
| 493 | runtimeInfo = getRuntimeInfo(kernelArgs); |
| 494 | if (runtimeInfo == nullptr) { |
| 495 | return usage(argv[0]); |
| 496 | } |
| 497 | } |
| 498 | |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 499 | if (dirmap.empty()) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 500 | LOG(ERROR) << "Missing --rootdir or --dirmap option."; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 501 | return usage(argv[0]); |
| 502 | } |
| 503 | |
Yifan Hong | 6e32a5f | 2020-03-12 16:06:27 -0700 | [diff] [blame] | 504 | auto compat = checkAllFiles(dirmap, properties, runtimeInfo); |
Yifan Hong | 62503e1 | 2019-08-26 12:47:41 -0700 | [diff] [blame] | 505 | |
Yifan Hong | 6e32a5f | 2020-03-12 16:06:27 -0700 | [diff] [blame] | 506 | if (compat.ok()) { |
Yifan Hong | 62503e1 | 2019-08-26 12:47:41 -0700 | [diff] [blame] | 507 | std::cout << "COMPATIBLE" << std::endl; |
| 508 | return EX_OK; |
| 509 | } |
Yifan Hong | 6e32a5f | 2020-03-12 16:06:27 -0700 | [diff] [blame] | 510 | if (compat.error().code() == 0) { |
| 511 | LOG(ERROR) << "files are incompatible: " << compat.error(); |
Yifan Hong | 62503e1 | 2019-08-26 12:47:41 -0700 | [diff] [blame] | 512 | std::cout << "INCOMPATIBLE" << std::endl; |
| 513 | return EX_DATAERR; |
| 514 | } |
Yifan Hong | 6e32a5f | 2020-03-12 16:06:27 -0700 | [diff] [blame] | 515 | LOG(ERROR) << strerror(compat.error().code()) << ": " << compat.error(); |
Yifan Hong | 62503e1 | 2019-08-26 12:47:41 -0700 | [diff] [blame] | 516 | return EX_SOFTWARE; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 517 | } |