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