blob: e4941bd646a1ed3f5353a3abb5999c529305b01c [file] [log] [blame]
Yifan Honga72bde72017-09-28 13:36:48 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yifan Hong69c1b112018-02-27 17:06:00 -080017#include <getopt.h>
18#include <unistd.h>
Yifan Honga72bde72017-09-28 13:36:48 -070019
Yifan Hong69c1b112018-02-27 17:06:00 -080020#include <iostream>
21#include <map>
22
23#include <android-base/parseint.h>
24#include <utils/Errors.h>
25#include <vintf/VintfObject.h>
Yifan Honga72bde72017-09-28 13:36:48 -070026#include <vintf/parse_xml.h>
27#include "utils.h"
28
29namespace android {
30namespace vintf {
Yifan Hong69c1b112018-02-27 17:06:00 -080031namespace details {
Yifan Honga72bde72017-09-28 13:36:48 -070032
Yifan Hong69c1b112018-02-27 17:06:00 -080033// fake sysprops
34using Properties = std::map<std::string, std::string>;
35
36enum Option : int {
37 DUMP_FILE_LIST = 1,
38 ROOTDIR,
39 HELP,
40 PROPERTY,
41 CHECK_COMPAT,
42};
43// command line arguments
44using Args = std::multimap<Option, std::string>;
45
Yifan Hong10d86222018-04-06 15:41:05 -070046class HostFileSystem : public FileSystem {
Yifan Hong69c1b112018-02-27 17:06:00 -080047 public:
Yifan Hong10d86222018-04-06 15:41:05 -070048 HostFileSystem(const std::string& rootdir) {
Yifan Hong69c1b112018-02-27 17:06:00 -080049 mRootDir = rootdir;
50 if (!mRootDir.empty() && mRootDir.back() != '/') {
51 mRootDir.push_back('/');
52 }
53 }
Yifan Hong10d86222018-04-06 15:41:05 -070054 status_t fetch(const std::string& path, std::string* fetched,
55 std::string* error) const override {
56 status_t status = mImpl.fetch(mRootDir + path, fetched, error);
57 std::cerr << "Debug: Fetch '" << mRootDir << path << "': " << toString(status) << std::endl;
58 return status;
Yifan Hong69c1b112018-02-27 17:06:00 -080059 }
Yifan Hong10d86222018-04-06 15:41:05 -070060 status_t listFiles(const std::string& path, std::vector<std::string>* out,
61 std::string* error) const override {
62 status_t status = mImpl.listFiles(mRootDir + path, out, error);
Yifan Hong69c1b112018-02-27 17:06:00 -080063 std::cerr << "Debug: List '" << mRootDir << path << "': " << toString(status) << std::endl;
64 return status;
65 }
66
67 private:
Yifan Hong69c1b112018-02-27 17:06:00 -080068 static std::string toString(status_t status) {
69 return status == OK ? "SUCCESS" : strerror(-status);
70 }
71 std::string mRootDir;
Yifan Hong10d86222018-04-06 15:41:05 -070072 FileSystemImpl mImpl;
Yifan Hong69c1b112018-02-27 17:06:00 -080073};
74
75class PresetPropertyFetcher : public PropertyFetcher {
76 public:
77 std::string getProperty(const std::string& key,
78 const std::string& defaultValue) const override {
79 auto it = mProps.find(key);
80 if (it == mProps.end()) {
81 std::cerr << "Debug: Sysprop " << key << " is missing, default to '" << defaultValue
82 << "'" << std::endl;
83 return defaultValue;
84 }
85 std::cerr << "Debug: Sysprop " << key << "=" << it->second << std::endl;
86 return it->second;
87 }
88 uint64_t getUintProperty(const std::string& key, uint64_t defaultValue,
89 uint64_t max) const override {
90 uint64_t result;
91 std::string value = getProperty(key, "");
92 if (!value.empty() && android::base::ParseUint(value, &result, max)) return result;
93 return defaultValue;
94 }
95 bool getBoolProperty(const std::string& key, bool defaultValue) const override {
96 std::string value = getProperty(key, "");
97 if (value == "1" || value == "true") {
98 return true;
99 } else if (value == "0" || value == "false") {
100 return false;
101 }
102 return defaultValue;
103 }
104 void setProperties(const Properties& props) { mProps.insert(props.begin(), props.end()); }
105
106 private:
107 std::map<std::string, std::string> mProps;
108};
109
Yifan Hong69c1b112018-02-27 17:06:00 -0800110// helper functions
Yifan Honga72bde72017-09-28 13:36:48 -0700111template <typename T>
Yifan Hong9f78c182018-07-12 14:45:52 -0700112std::unique_ptr<T> readObject(FileSystem* fileSystem, const std::string& path,
113 const XmlConverter<T>& converter) {
Yifan Honga72bde72017-09-28 13:36:48 -0700114 std::string xml;
Yifan Hong60217032018-01-08 16:19:42 -0800115 std::string error;
Yifan Hong9f78c182018-07-12 14:45:52 -0700116 status_t err = fileSystem->fetch(path, &xml, &error);
Yifan Honga72bde72017-09-28 13:36:48 -0700117 if (err != OK) {
Yifan Hong60217032018-01-08 16:19:42 -0800118 std::cerr << "Error: Cannot read '" << path << "' (" << strerror(-err) << "): " << error
119 << std::endl;
Yifan Honga72bde72017-09-28 13:36:48 -0700120 return nullptr;
121 }
122 auto ret = std::make_unique<T>();
Yifan Hong94757062018-02-09 16:36:31 -0800123 if (!converter(ret.get(), xml, &error)) {
124 std::cerr << "Error: Cannot parse '" << path << "': " << error << std::endl;
Yifan Honga72bde72017-09-28 13:36:48 -0700125 return nullptr;
126 }
127 return ret;
128}
129
Yifan Hong69c1b112018-02-27 17:06:00 -0800130int checkCompatibilityForFiles(const std::string& manifestPath, const std::string& matrixPath) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700131 auto fileSystem = std::make_unique<FileSystemImpl>();
132 auto manifest = readObject(fileSystem.get(), manifestPath, gHalManifestConverter);
133 auto matrix = readObject(fileSystem.get(), matrixPath, gCompatibilityMatrixConverter);
Yifan Honga72bde72017-09-28 13:36:48 -0700134 if (manifest == nullptr || matrix == nullptr) {
135 return -1;
136 }
137
138 std::string error;
139 if (!manifest->checkCompatibility(*matrix, &error)) {
140 std::cerr << "Error: Incompatible: " << error << std::endl;
141 std::cout << "false" << std::endl;
142 return 1;
143 }
144
145 std::cout << "true" << std::endl;
146 return 0;
147}
Yifan Hong69c1b112018-02-27 17:06:00 -0800148
149Args parseArgs(int argc, char** argv) {
150 int longOptFlag;
151 int optionIndex;
152 Args ret;
153 std::vector<struct option> longopts{
154 {"dump-file-list", no_argument, &longOptFlag, DUMP_FILE_LIST},
155 {"rootdir", required_argument, &longOptFlag, ROOTDIR},
156 {"help", no_argument, &longOptFlag, HELP},
157 {"property", required_argument, &longOptFlag, PROPERTY},
158 {"check-compat", no_argument, &longOptFlag, CHECK_COMPAT},
159 {0, 0, 0, 0}};
160 std::map<int, Option> shortopts{
161 {'h', HELP}, {'D', PROPERTY}, {'c', CHECK_COMPAT},
162 };
163 for (;;) {
164 int c = getopt_long(argc, argv, "hcD:", longopts.data(), &optionIndex);
165 if (c == -1) {
166 break;
167 }
168 std::string argValue = optarg ? optarg : std::string{};
169 if (c == 0) {
170 ret.emplace(static_cast<Option>(longOptFlag), std::move(argValue));
171 } else {
172 ret.emplace(shortopts[c], std::move(argValue));
173 }
174 }
175 if (optind < argc) {
176 // see non option
177 std::cerr << "unrecognized option `" << argv[optind] << "'" << std::endl;
178 return {{HELP, ""}};
179 }
180 return ret;
181}
182
183template <typename T>
184Properties getProperties(const T& args) {
185 Properties ret;
186 for (const auto& arg : args) {
187 auto pos = arg.find('=');
188 auto key = arg.substr(0, pos);
189 auto value = pos == std::string::npos ? std::string{} : arg.substr(pos + 1);
190 ret[key] = value;
191 }
192 return ret;
193}
194
195int usage(const char* me) {
196 std::cerr
197 << me << ": check VINTF metadata." << std::endl
198 << " Options:" << std::endl
199 << " --dump-file-list: Dump a list of directories / files on device" << std::endl
200 << " that is required to be used by --check-compat." << std::endl
201 << " -c, --check-compat: check compatibility for files under the root" << std::endl
202 << " directory specified by --root-dir." << std::endl
203 << " --rootdir=<dir>: specify root directory for all metadata." << std::endl
204 << " -D, --property <key>=<value>: specify sysprops." << std::endl
205 << " --help: show this message." << std::endl
206 << std::endl
207 << " Example:" << std::endl
208 << " # Get the list of required files." << std::endl
209 << " " << me << " --dump-file-list > /tmp/files.txt" << std::endl
210 << " # Pull from ADB, or use your own command to extract files from images"
211 << std::endl
212 << " ROOTDIR=/tmp/device/" << std::endl
213 << " cat /tmp/files.txt | xargs -I{} bash -c \"mkdir -p $ROOTDIR`dirname {}` && adb "
214 "pull {} $ROOTDIR{}\""
215 << std::endl
216 << " # Check compatibility." << std::endl
217 << " " << me << " --check-compat --rootdir=$ROOTDIR \\" << std::endl
218 << " --property ro.product.first_api_level=`adb shell getprop "
219 "ro.product.first_api_level` \\"
220 << std::endl
221 << " --property ro.boot.product.hardware.sku=`adb shell getprop "
222 "ro.boot.product.hardware.sku`"
223 << std::endl;
224 return 1;
225}
226
227int checkAllFiles(const std::string& rootdir, const Properties& props, std::string* error) {
Yifan Hong9f78c182018-07-12 14:45:52 -0700228 auto hostPropertyFetcher = std::make_unique<PresetPropertyFetcher>();
229 hostPropertyFetcher->setProperties(props);
230 VintfObject vintfObject(std::make_unique<HostFileSystem>(rootdir),
231 nullptr /* partition mounter */, nullptr /* runtime info factory */,
232 std::move(hostPropertyFetcher));
233 return vintfObject.checkCompatibility({} /* packageInfo */, error, DISABLE_RUNTIME_INFO);
Yifan Hong69c1b112018-02-27 17:06:00 -0800234}
235
236} // namespace details
237} // namespace vintf
238} // namespace android
239
240int main(int argc, char** argv) {
241 using namespace android::vintf;
242 using namespace android::vintf::details;
243 // legacy usage: check_vintf <manifest.xml> <matrix.xml>
244 if (argc == 3) {
245 int ret = checkCompatibilityForFiles(argv[1], argv[2]);
246 if (ret >= 0) return ret;
247 }
248
249 Args args = parseArgs(argc, argv);
250
251 if (!iterateValues(args, HELP).empty()) {
252 return usage(argv[0]);
253 }
254
255 if (!iterateValues(args, DUMP_FILE_LIST).empty()) {
256 for (const auto& file : dumpFileList()) {
257 std::cout << file << std::endl;
258 }
259 return 0;
260 }
261
262 auto rootdirs = iterateValues(args, ROOTDIR);
263 auto properties = getProperties(iterateValues(args, PROPERTY));
264
265 auto checkCompat = iterateValues(args, CHECK_COMPAT);
266 if (!checkCompat.empty()) {
267 if (rootdirs.empty()) {
268 std::cerr << "Missing --rootdir option." << std::endl;
269 return usage(argv[0]);
270 }
271 int ret = COMPATIBLE;
272 for (const auto& rootdir : rootdirs) {
273 std::cerr << "Debug: checking files under " << rootdir << "..." << std::endl;
274 std::string error;
275 int compat = checkAllFiles(rootdir, properties, &error);
276 std::cerr << "Debug: files under " << rootdir
277 << (compat == COMPATIBLE
278 ? " is compatible"
279 : compat == INCOMPATIBLE ? " are incompatible"
280 : (" has encountered an error: " + error))
281 << std::endl;
282 }
283 if (ret == COMPATIBLE) {
284 std::cout << "true" << std::endl;
285 }
286 return ret;
287 }
288
289 return usage(argv[0]);
290}