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