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 | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 29 | #include <utils/Errors.h> |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 30 | #include <vintf/KernelConfigParser.h> |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 31 | #include <vintf/VintfObject.h> |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 32 | #include <vintf/parse_string.h> |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 33 | #include <vintf/parse_xml.h> |
| 34 | #include "utils.h" |
| 35 | |
| 36 | namespace android { |
| 37 | namespace vintf { |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 38 | namespace details { |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 39 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 40 | // fake sysprops |
| 41 | using Properties = std::map<std::string, std::string>; |
| 42 | |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 43 | using Dirmap = std::map<std::string, std::string>; |
| 44 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 45 | enum Option : int { |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 46 | // Modes |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 47 | HELP, |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 48 | DUMP_FILE_LIST = 1, |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 49 | CHECK_COMPAT, |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 50 | CHECK_ONE, |
| 51 | |
| 52 | // Options |
| 53 | ROOTDIR, |
| 54 | PROPERTY, |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 55 | DIR_MAP, |
| 56 | KERNEL, |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 57 | }; |
| 58 | // command line arguments |
| 59 | using Args = std::multimap<Option, std::string>; |
| 60 | |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 61 | class HostFileSystem : public details::FileSystemImpl { |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 62 | public: |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 63 | HostFileSystem(const Dirmap& dirmap, status_t missingError) |
| 64 | : mDirMap(dirmap), mMissingError(missingError) {} |
Yifan Hong | 10d8622 | 2018-04-06 15:41:05 -0700 | [diff] [blame] | 65 | status_t fetch(const std::string& path, std::string* fetched, |
| 66 | std::string* error) const override { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 67 | auto resolved = resolve(path, error); |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 68 | if (resolved.empty()) { |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 69 | return mMissingError; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 70 | } |
| 71 | status_t status = details::FileSystemImpl::fetch(resolved, fetched, error); |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 72 | LOG(INFO) << "Fetch '" << resolved << "': " << toString(status); |
Yifan Hong | 10d8622 | 2018-04-06 15:41:05 -0700 | [diff] [blame] | 73 | return status; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 74 | } |
Yifan Hong | 10d8622 | 2018-04-06 15:41:05 -0700 | [diff] [blame] | 75 | status_t listFiles(const std::string& path, std::vector<std::string>* out, |
| 76 | std::string* error) const override { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 77 | auto resolved = resolve(path, error); |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 78 | if (resolved.empty()) { |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 79 | return mMissingError; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 80 | } |
| 81 | status_t status = details::FileSystemImpl::listFiles(resolved, out, error); |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 82 | LOG(INFO) << "List '" << resolved << "': " << toString(status); |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 83 | return status; |
| 84 | } |
| 85 | |
| 86 | private: |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 87 | static std::string toString(status_t status) { |
| 88 | return status == OK ? "SUCCESS" : strerror(-status); |
| 89 | } |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 90 | std::string resolve(const std::string& path, std::string* error) const { |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 91 | 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 Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 99 | if (error) { |
| 100 | *error = "Cannot resolve path " + path; |
| 101 | } else { |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 102 | LOG(mMissingError == NAME_NOT_FOUND ? INFO : ERROR) << "Cannot resolve path " << path; |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 103 | } |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 104 | return ""; |
| 105 | } |
| 106 | |
| 107 | Dirmap mDirMap; |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 108 | status_t mMissingError; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | class 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 Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 117 | LOG(INFO) << "Sysprop " << key << " is missing, default to '" << defaultValue << "'"; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 118 | return defaultValue; |
| 119 | } |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 120 | LOG(INFO) << "Sysprop " << key << "=" << it->second; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 121 | 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 Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 145 | struct 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 Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 152 | LOG(INFO) << "fetched kernel version " << kernelVersion; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 153 | } |
| 154 | if (flags & RuntimeInfo::FetchFlag::CONFIG_GZ) { |
| 155 | std::string content; |
| 156 | if (!android::base::ReadFileToString(kernelConfigFile, &content)) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 157 | LOG(ERROR) << "Cannot read " << kernelConfigFile; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 158 | 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 Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 166 | LOG(INFO) << "read kernel configs from " << kernelConfigFile; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 167 | } |
| 168 | if (flags & RuntimeInfo::FetchFlag::POLICYVERS) { |
| 169 | mKernelSepolicyVersion = SIZE_MAX; |
| 170 | } |
| 171 | return OK; |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | struct StubRuntimeInfo : public RuntimeInfo { |
| 176 | status_t fetchAllInformation(FetchFlags) override { return UNKNOWN_ERROR; } |
| 177 | }; |
| 178 | |
| 179 | struct 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 Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 188 | // helper functions |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 189 | template <typename T> |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 190 | std::unique_ptr<T> readObject(FileSystem* fileSystem, const std::string& path, |
| 191 | const XmlConverter<T>& converter) { |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 192 | std::string xml; |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 193 | std::string error; |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 194 | status_t err = fileSystem->fetch(path, &xml, &error); |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 195 | if (err != OK) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 196 | LOG(ERROR) << "Cannot read '" << path << "' (" << strerror(-err) << "): " << error; |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 197 | return nullptr; |
| 198 | } |
| 199 | auto ret = std::make_unique<T>(); |
Yifan Hong | 9475706 | 2018-02-09 16:36:31 -0800 | [diff] [blame] | 200 | if (!converter(ret.get(), xml, &error)) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 201 | LOG(ERROR) << "Cannot parse '" << path << "': " << error; |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 202 | return nullptr; |
| 203 | } |
| 204 | return ret; |
| 205 | } |
| 206 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 207 | int checkCompatibilityForFiles(const std::string& manifestPath, const std::string& matrixPath) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 208 | auto fileSystem = std::make_unique<FileSystemImpl>(); |
| 209 | auto manifest = readObject(fileSystem.get(), manifestPath, gHalManifestConverter); |
| 210 | auto matrix = readObject(fileSystem.get(), matrixPath, gCompatibilityMatrixConverter); |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 211 | if (manifest == nullptr || matrix == nullptr) { |
| 212 | return -1; |
| 213 | } |
| 214 | |
| 215 | std::string error; |
| 216 | if (!manifest->checkCompatibility(*matrix, &error)) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 217 | LOG(ERROR) << "Incompatible: " << error; |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 218 | std::cout << "false" << std::endl; |
| 219 | return 1; |
| 220 | } |
| 221 | |
| 222 | std::cout << "true" << std::endl; |
| 223 | return 0; |
| 224 | } |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 225 | |
| 226 | Args parseArgs(int argc, char** argv) { |
| 227 | int longOptFlag; |
| 228 | int optionIndex; |
| 229 | Args ret; |
| 230 | std::vector<struct option> longopts{ |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 231 | // Modes |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 232 | {"help", no_argument, &longOptFlag, HELP}, |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 233 | {"dump-file-list", no_argument, &longOptFlag, DUMP_FILE_LIST}, |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 234 | {"check-compat", no_argument, &longOptFlag, CHECK_COMPAT}, |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 235 | {"check-one", no_argument, &longOptFlag, CHECK_ONE}, |
| 236 | // Options |
| 237 | {"rootdir", required_argument, &longOptFlag, ROOTDIR}, |
| 238 | {"property", required_argument, &longOptFlag, PROPERTY}, |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 239 | {"dirmap", required_argument, &longOptFlag, DIR_MAP}, |
| 240 | {"kernel", required_argument, &longOptFlag, KERNEL}, |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 241 | {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 Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 259 | LOG(ERROR) << "unrecognized option `" << argv[optind] << "'"; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 260 | return {{HELP, ""}}; |
| 261 | } |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | template <typename T> |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 266 | std::map<std::string, std::string> splitArgs(const T& args, char split) { |
| 267 | std::map<std::string, std::string> ret; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 268 | for (const auto& arg : args) { |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 269 | auto pos = arg.find(split); |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 270 | 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 Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 276 | template <typename T> |
| 277 | Properties getProperties(const T& args) { |
| 278 | return splitArgs(args, '='); |
| 279 | } |
| 280 | |
| 281 | template <typename T> |
| 282 | Dirmap getDirmap(const T& args) { |
| 283 | return splitArgs(args, ':'); |
| 284 | } |
| 285 | |
| 286 | template <typename T> |
| 287 | std::shared_ptr<StaticRuntimeInfo> getRuntimeInfo(const T& args) { |
| 288 | auto ret = std::make_shared<StaticRuntimeInfo>(); |
| 289 | if (std::distance(args.begin(), args.end()) > 1) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 290 | LOG(ERROR) << "Can't have multiple --kernel options"; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 291 | return nullptr; |
| 292 | } |
| 293 | auto pair = android::base::Split(*args.begin(), ":"); |
| 294 | if (pair.size() != 2) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 295 | LOG(ERROR) << "Invalid --kernel"; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 296 | return nullptr; |
| 297 | } |
| 298 | if (!parse(pair[0], &ret->kernelVersion)) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 299 | LOG(ERROR) << "Cannot parse " << pair[0] << " as kernel version"; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 300 | return nullptr; |
| 301 | } |
| 302 | ret->kernelConfigFile = std::move(pair[1]); |
| 303 | return ret; |
| 304 | } |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 305 | |
| 306 | int usage(const char* me) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 307 | LOG(ERROR) |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 308 | << me << ": check VINTF metadata." << std::endl |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 309 | << " Modes:" << std::endl |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 310 | << " --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 Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 314 | << " --check-one: check consistency of VINTF metadata for a single partition." |
| 315 | << std::endl |
| 316 | << std::endl |
| 317 | << " Options:" << std::endl |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 318 | << " --rootdir=<dir>: specify root directory for all metadata. Same as " << std::endl |
| 319 | << " --dirmap /:<dir>" << std::endl |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 320 | << " -D, --property <key>=<value>: specify sysprops." << std::endl |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 321 | << " --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 Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 328 | << " --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 Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 345 | "ro.boot.product.hardware.sku`"; |
Yifan Hong | 62503e1 | 2019-08-26 12:47:41 -0700 | [diff] [blame] | 346 | return EX_USAGE; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Yifan Hong | 6e32a5f | 2020-03-12 16:06:27 -0700 | [diff] [blame] | 349 | android::base::Result<void> checkAllFiles(const Dirmap& dirmap, const Properties& props, |
| 350 | std::shared_ptr<StaticRuntimeInfo> runtimeInfo) { |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 351 | auto hostPropertyFetcher = std::make_unique<PresetPropertyFetcher>(); |
| 352 | hostPropertyFetcher->setProperties(props); |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 353 | |
| 354 | CheckFlags::Type flags = CheckFlags::DEFAULT; |
| 355 | if (!runtimeInfo) flags = flags.disableRuntimeInfo(); |
| 356 | |
| 357 | auto vintfObject = |
| 358 | VintfObject::Builder() |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 359 | .setFileSystem(std::make_unique<HostFileSystem>(dirmap, UNKNOWN_ERROR)) |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 360 | .setPropertyFetcher(std::move(hostPropertyFetcher)) |
| 361 | .setRuntimeInfoFactory(std::make_unique<StaticRuntimeInfoFactory>(runtimeInfo)) |
| 362 | .build(); |
Yifan Hong | 6e32a5f | 2020-03-12 16:06:27 -0700 | [diff] [blame] | 363 | |
| 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 Hong | 76f8585 | 2020-03-23 16:32:40 +0000 | [diff] [blame^] | 377 | 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 Cheng | d324f84 | 2020-03-23 01:39:13 +0000 | [diff] [blame] | 383 | return vintfObject->checkUnusedHals(); |
Yifan Hong | 6e32a5f | 2020-03-12 16:06:27 -0700 | [diff] [blame] | 384 | } |
| 385 | LOG(INFO) << "Skip checking unused HALs."; |
| 386 | return {}; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 389 | int 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 Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 437 | } // namespace details |
| 438 | } // namespace vintf |
| 439 | } // namespace android |
| 440 | |
| 441 | int main(int argc, char** argv) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 442 | android::base::SetLogger(android::base::StderrLogger); |
| 443 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 444 | using namespace android::vintf; |
| 445 | using namespace android::vintf::details; |
| 446 | // legacy usage: check_vintf <manifest.xml> <matrix.xml> |
Yifan Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 447 | if (argc == 3 && *argv[1] != '-' && *argv[2] != '-') { |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 448 | 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 Hong | 0dfc3d5 | 2020-01-09 14:30:02 -0800 | [diff] [blame] | 465 | auto dirmap = getDirmap(iterateValues(args, DIR_MAP)); |
| 466 | |
| 467 | if (!iterateValues(args, CHECK_ONE).empty()) { |
| 468 | return checkDirmaps(dirmap); |
| 469 | } |
| 470 | |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 471 | auto checkCompat = iterateValues(args, CHECK_COMPAT); |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 472 | if (checkCompat.empty()) { |
| 473 | return usage(argv[0]); |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 474 | } |
| 475 | |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 476 | auto rootdirs = iterateValues(args, ROOTDIR); |
| 477 | if (!rootdirs.empty()) { |
| 478 | if (std::distance(rootdirs.begin(), rootdirs.end()) > 1) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 479 | LOG(ERROR) << "Can't have multiple --rootdir options"; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 480 | return usage(argv[0]); |
| 481 | } |
| 482 | args.emplace(DIR_MAP, "/:" + *rootdirs.begin()); |
| 483 | } |
| 484 | |
| 485 | auto properties = getProperties(iterateValues(args, PROPERTY)); |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 486 | |
| 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 Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 496 | if (dirmap.empty()) { |
Yifan Hong | 6aef2fc | 2020-01-09 14:41:11 -0800 | [diff] [blame] | 497 | LOG(ERROR) << "Missing --rootdir or --dirmap option."; |
Yifan Hong | 0729285 | 2019-08-21 15:40:30 -0700 | [diff] [blame] | 498 | return usage(argv[0]); |
| 499 | } |
| 500 | |
Yifan Hong | 6e32a5f | 2020-03-12 16:06:27 -0700 | [diff] [blame] | 501 | auto compat = checkAllFiles(dirmap, properties, runtimeInfo); |
Yifan Hong | 62503e1 | 2019-08-26 12:47:41 -0700 | [diff] [blame] | 502 | |
Yifan Hong | 6e32a5f | 2020-03-12 16:06:27 -0700 | [diff] [blame] | 503 | if (compat.ok()) { |
Yifan Hong | 62503e1 | 2019-08-26 12:47:41 -0700 | [diff] [blame] | 504 | std::cout << "COMPATIBLE" << std::endl; |
| 505 | return EX_OK; |
| 506 | } |
Yifan Hong | 6e32a5f | 2020-03-12 16:06:27 -0700 | [diff] [blame] | 507 | if (compat.error().code() == 0) { |
| 508 | LOG(ERROR) << "files are incompatible: " << compat.error(); |
Yifan Hong | 62503e1 | 2019-08-26 12:47:41 -0700 | [diff] [blame] | 509 | std::cout << "INCOMPATIBLE" << std::endl; |
| 510 | return EX_DATAERR; |
| 511 | } |
Yifan Hong | 6e32a5f | 2020-03-12 16:06:27 -0700 | [diff] [blame] | 512 | LOG(ERROR) << strerror(compat.error().code()) << ": " << compat.error(); |
Yifan Hong | 62503e1 | 2019-08-26 12:47:41 -0700 | [diff] [blame] | 513 | return EX_SOFTWARE; |
Yifan Hong | 69c1b11 | 2018-02-27 17:06:00 -0800 | [diff] [blame] | 514 | } |