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
17 #include <stdlib.h>
18 #include <unistd.h>
19
20 #include <fstream>
21 #include <iostream>
22 #include <sstream>
23 #include <string>
24 #include <unordered_map>
25
26 #include <android-base/file.h>
27 #include <android-base/parseint.h>
28 #include <android-base/strings.h>
29 #include <libvts_vintf_test_common/common.h>
30 #include <vintf/AssembleVintf.h>
31 #include <vintf/KernelConfigParser.h>
32 #include <vintf/parse_string.h>
33 #include <vintf/parse_xml.h>
34 #include "utils.h"
35
36 #define BUFFER_SIZE sysconf(_SC_PAGESIZE)
37
38 namespace android {
39 namespace vintf {
40
41 static const std::string gConfigPrefix = "android-base-";
42 static const std::string gConfigSuffix = ".config";
43 static const std::string gBaseConfig = "android-base.config";
44
45 // An input stream with a name.
46 // The input stream may be an actual file, or a stringstream for testing.
47 // It takes ownership on the istream.
48 class NamedIstream {
49 public:
50 NamedIstream() = default;
NamedIstream(const std::string & name,std::unique_ptr<std::istream> && stream)51 NamedIstream(const std::string& name, std::unique_ptr<std::istream>&& stream)
52 : mName(name), mStream(std::move(stream)) {}
name() const53 const std::string& name() const { return mName; }
stream()54 std::istream& stream() { return *mStream; }
hasStream()55 bool hasStream() { return mStream != nullptr; }
56
57 private:
58 std::string mName;
59 std::unique_ptr<std::istream> mStream;
60 };
61
62 /**
63 * Slurps the device manifest file and add build time flag to it.
64 */
65 class AssembleVintfImpl : public AssembleVintf {
66 using Condition = std::unique_ptr<KernelConfig>;
67 using ConditionedConfig = std::pair<Condition, std::vector<KernelConfig> /* configs */>;
68
69 public:
setFakeEnv(const std::string & key,const std::string & value)70 void setFakeEnv(const std::string& key, const std::string& value) { mFakeEnv[key] = value; }
71
getEnv(const std::string & key) const72 std::string getEnv(const std::string& key) const {
73 auto it = mFakeEnv.find(key);
74 if (it != mFakeEnv.end()) {
75 return it->second;
76 }
77 const char* envValue = getenv(key.c_str());
78 return envValue != nullptr ? std::string(envValue) : std::string();
79 }
80
81 // Get environment variable and split with space.
getEnvList(const std::string & key) const82 std::vector<std::string> getEnvList(const std::string& key) const {
83 std::vector<std::string> ret;
84 for (auto&& v : base::Split(getEnv(key), " ")) {
85 v = base::Trim(v);
86 if (!v.empty()) {
87 ret.push_back(v);
88 }
89 }
90 return ret;
91 }
92
93 template <typename T>
getFlag(const std::string & key,T * value,bool log=true) const94 bool getFlag(const std::string& key, T* value, bool log = true) const {
95 std::string envValue = getEnv(key);
96 if (envValue.empty()) {
97 if (log) {
98 std::cerr << "Warning: " << key << " is missing, defaulted to " << (*value) << "."
99 << std::endl;
100 }
101 return true;
102 }
103
104 if (!parse(envValue, value)) {
105 std::cerr << "Cannot parse " << envValue << "." << std::endl;
106 return false;
107 }
108 return true;
109 }
110
111 /**
112 * Set *out to environment variable only if *out is default constructed.
113 * Return false if a fatal error has occurred:
114 * - The environment variable has an unknown format
115 * - The value of the environment variable does not match a predefined variable in the files
116 */
117 template <typename T>
getFlagIfUnset(const std::string & envKey,T * out) const118 bool getFlagIfUnset(const std::string& envKey, T* out) const {
119 bool hasExistingValue = !(*out == T{});
120
121 bool hasEnvValue = false;
122 T envValue;
123 std::string envStrValue = getEnv(envKey);
124 if (!envStrValue.empty()) {
125 if (!parse(envStrValue, &envValue)) {
126 std::cerr << "Cannot parse " << envValue << "." << std::endl;
127 return false;
128 }
129 hasEnvValue = true;
130 }
131
132 if (hasExistingValue) {
133 if (hasEnvValue && (*out != envValue)) {
134 std::cerr << "Cannot override existing value " << *out << " with " << envKey
135 << " (which is " << envValue << ")." << std::endl;
136 return false;
137 }
138 return true;
139 }
140 if (hasEnvValue) {
141 *out = envValue;
142 }
143 return true;
144 }
145
getBooleanFlag(const std::string & key) const146 bool getBooleanFlag(const std::string& key) const { return getEnv(key) == std::string("true"); }
147
getIntegerFlag(const std::string & key,size_t defaultValue=0) const148 size_t getIntegerFlag(const std::string& key, size_t defaultValue = 0) const {
149 std::string envValue = getEnv(key);
150 if (envValue.empty()) {
151 return defaultValue;
152 }
153 size_t value;
154 if (!base::ParseUint(envValue, &value)) {
155 std::cerr << "Error: " << key << " must be a number." << std::endl;
156 return defaultValue;
157 }
158 return value;
159 }
160
read(std::basic_istream<char> & is)161 static std::string read(std::basic_istream<char>& is) {
162 std::stringstream ss;
163 ss << is.rdbuf();
164 return ss.str();
165 }
166
167 // Return true if name of file is "android-base.config". This file must be specified
168 // exactly once for each kernel version. These requirements do not have any conditions.
isCommonConfig(const std::string & path)169 static bool isCommonConfig(const std::string& path) {
170 return ::android::base::Basename(path) == gBaseConfig;
171 }
172
173 // Return true if name of file matches "android-base-foo.config".
174 // Zero or more conditional configs may be specified for each kernel version. These
175 // requirements are conditional on CONFIG_FOO=y.
isConditionalConfig(const std::string & path)176 static bool isConditionalConfig(const std::string& path) {
177 auto fname = ::android::base::Basename(path);
178 return ::android::base::StartsWith(fname, gConfigPrefix) &&
179 ::android::base::EndsWith(fname, gConfigSuffix);
180 }
181
182 // Return true for all other file names (i.e. not android-base.config, and not conditional
183 // configs.)
184 // Zero or more conditional configs may be specified for each kernel version.
185 // These requirements do not have any conditions.
isExtraCommonConfig(const std::string & path)186 static bool isExtraCommonConfig(const std::string& path) {
187 return !isCommonConfig(path) && !isConditionalConfig(path);
188 }
189
190 // nullptr on any error, otherwise the condition.
generateCondition(const std::string & path)191 static Condition generateCondition(const std::string& path) {
192 if (!isConditionalConfig(path)) {
193 return nullptr;
194 }
195 auto fname = ::android::base::Basename(path);
196 std::string sub = fname.substr(gConfigPrefix.size(),
197 fname.size() - gConfigPrefix.size() - gConfigSuffix.size());
198 if (sub.empty()) {
199 return nullptr; // should not happen
200 }
201 for (size_t i = 0; i < sub.size(); ++i) {
202 if (sub[i] == '-') {
203 sub[i] = '_';
204 continue;
205 }
206 if (isalnum(sub[i])) {
207 sub[i] = toupper(sub[i]);
208 continue;
209 }
210 std::cerr << "'" << fname << "' (in " << path
211 << ") is not a valid kernel config file name. Must match regex: "
212 << "android-base(-[0-9a-zA-Z-]+)?\\" << gConfigSuffix
213 << std::endl;
214 return nullptr;
215 }
216 sub.insert(0, "CONFIG_");
217 return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES);
218 }
219
parseFileForKernelConfigs(std::basic_istream<char> & stream,std::vector<KernelConfig> * out)220 static bool parseFileForKernelConfigs(std::basic_istream<char>& stream,
221 std::vector<KernelConfig>* out) {
222 KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */);
223 status_t err = parser.processAndFinish(read(stream));
224 if (err != OK) {
225 std::cerr << parser.error();
226 return false;
227 }
228
229 for (auto& configPair : parser.configs()) {
230 out->push_back({});
231 KernelConfig& config = out->back();
232 config.first = std::move(configPair.first);
233 if (!parseKernelConfigTypedValue(configPair.second, &config.second)) {
234 std::cerr << "Unknown value type for key = '" << config.first << "', value = '"
235 << configPair.second << "'\n";
236 return false;
237 }
238 }
239 return true;
240 }
241
parseFilesForKernelConfigs(std::vector<NamedIstream> * streams,std::vector<ConditionedConfig> * out)242 static bool parseFilesForKernelConfigs(std::vector<NamedIstream>* streams,
243 std::vector<ConditionedConfig>* out) {
244 out->clear();
245 ConditionedConfig commonConfig;
246 bool foundCommonConfig = false;
247 bool ret = true;
248
249 for (auto& namedStream : *streams) {
250 if (isCommonConfig(namedStream.name()) || isExtraCommonConfig(namedStream.name())) {
251 if (!parseFileForKernelConfigs(namedStream.stream(), &commonConfig.second)) {
252 std::cerr << "Failed to generate common configs for file "
253 << namedStream.name();
254 ret = false;
255 }
256 if (isCommonConfig(namedStream.name())) {
257 foundCommonConfig = true;
258 }
259 } else {
260 Condition condition = generateCondition(namedStream.name());
261 if (condition == nullptr) {
262 std::cerr << "Failed to generate conditional configs for file "
263 << namedStream.name();
264 ret = false;
265 }
266
267 std::vector<KernelConfig> kernelConfigs;
268 if ((ret &= parseFileForKernelConfigs(namedStream.stream(), &kernelConfigs)))
269 out->emplace_back(std::move(condition), std::move(kernelConfigs));
270 }
271 }
272
273 if (!foundCommonConfig) {
274 std::cerr << "No " << gBaseConfig << " is found in these paths:" << std::endl;
275 for (auto& namedStream : *streams) {
276 std::cerr << " " << namedStream.name() << std::endl;
277 }
278 ret = false;
279 }
280 // first element is always common configs (no conditions).
281 out->insert(out->begin(), std::move(commonConfig));
282 return ret;
283 }
284
out() const285 std::basic_ostream<char>& out() const { return mOutRef == nullptr ? std::cout : *mOutRef; }
286
287 // If -c is provided, check it.
checkDualFile(const HalManifest & manifest,const CompatibilityMatrix & matrix)288 bool checkDualFile(const HalManifest& manifest, const CompatibilityMatrix& matrix) {
289 if (getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
290 std::string error;
291 if (!manifest.checkCompatibility(matrix, &error, mCheckFlags)) {
292 std::cerr << "Not compatible: " << error << std::endl;
293 return false;
294 }
295 }
296 return true;
297 }
298
299 using HalManifests = std::vector<HalManifest>;
300 using CompatibilityMatrices = std::vector<CompatibilityMatrix>;
301
302 template <typename M>
outputInputs(const std::vector<M> & inputs)303 void outputInputs(const std::vector<M>& inputs) {
304 out() << "<!--" << std::endl;
305 out() << " Input:" << std::endl;
306 for (const auto& e : inputs) {
307 if (!e.fileName().empty()) {
308 out() << " " << base::Basename(e.fileName()) << std::endl;
309 }
310 }
311 out() << "-->" << std::endl;
312 }
313
314 // Parse --kernel arguments and write to output manifest.
setDeviceManifestKernel(HalManifest * manifest)315 bool setDeviceManifestKernel(HalManifest* manifest) {
316 if (mKernels.empty()) {
317 return true;
318 }
319 if (mKernels.size() > 1) {
320 std::cerr << "Warning: multiple --kernel is specified when building device manifest. "
321 << "Only the first one will be used." << std::endl;
322 }
323 auto& kernelArg = *mKernels.begin();
324 const auto& kernelVer = kernelArg.first;
325 auto& kernelConfigFiles = kernelArg.second;
326 // addKernel() guarantees that !kernelConfigFiles.empty().
327 if (kernelConfigFiles.size() > 1) {
328 std::cerr << "Warning: multiple config files are specified in --kernel when building "
329 << "device manfiest. Only the first one will be used." << std::endl;
330 }
331
332 KernelConfigParser parser(true /* processComments */, false /* relaxedFormat */);
333 status_t err = parser.processAndFinish(read(kernelConfigFiles[0].stream()));
334 if (err != OK) {
335 std::cerr << parser.error();
336 return false;
337 }
338
339 // Set version and configs in manifest.
340 auto kernel_info = std::make_optional<KernelInfo>();
341 kernel_info->mVersion = kernelVer;
342 kernel_info->mConfigs = parser.configs();
343 std::string error;
344 if (!manifest->mergeKernel(&kernel_info, &error)) {
345 std::cerr << error << "\n";
346 return false;
347 }
348
349 return true;
350 }
351
inferDeviceManifestKernelFcm(HalManifest * manifest)352 void inferDeviceManifestKernelFcm(HalManifest* manifest) {
353 // No target FCM version.
354 if (manifest->level() == Level::UNSPECIFIED) return;
355 // target FCM version < R: leave value untouched.
356 if (manifest->level() < Level::R) return;
357 // Inject empty <kernel> tag if missing.
358 if (!manifest->kernel().has_value()) {
359 manifest->device.mKernel = std::make_optional<KernelInfo>();
360 }
361 // Kernel FCM already set.
362 if (manifest->kernel()->level() != Level::UNSPECIFIED) return;
363
364 manifest->device.mKernel->mLevel = manifest->level();
365 }
366
assembleHalManifest(HalManifests * halManifests)367 bool assembleHalManifest(HalManifests* halManifests) {
368 std::string error;
369 HalManifest* halManifest = &halManifests->front();
370 for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) {
371 const std::string& path = it->fileName();
372 HalManifest& manifestToAdd = *it;
373
374 if (manifestToAdd.level() != Level::UNSPECIFIED) {
375 if (halManifest->level() == Level::UNSPECIFIED) {
376 halManifest->mLevel = manifestToAdd.level();
377 } else if (halManifest->level() != manifestToAdd.level()) {
378 std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl
379 << " File '" << halManifests->front().fileName() << "' has level "
380 << halManifest->level() << std::endl
381 << " File '" << path << "' has level " << manifestToAdd.level()
382 << std::endl;
383 return false;
384 }
385 }
386
387 if (!halManifest->addAll(&manifestToAdd, &error)) {
388 std::cerr << "File \"" << path << "\" cannot be added: " << error << std::endl;
389 return false;
390 }
391 }
392
393 if (halManifest->mType == SchemaType::DEVICE) {
394 if (!getFlagIfUnset("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion)) {
395 return false;
396 }
397
398 if (!setDeviceFcmVersion(halManifest)) {
399 return false;
400 }
401
402 if (!setDeviceManifestKernel(halManifest)) {
403 return false;
404 }
405
406 inferDeviceManifestKernelFcm(halManifest);
407 }
408
409 if (halManifest->mType == SchemaType::FRAMEWORK) {
410 for (auto&& v : getEnvList("PROVIDED_VNDK_VERSIONS")) {
411 halManifest->framework.mVendorNdks.emplace_back(std::move(v));
412 }
413
414 for (auto&& v : getEnvList("PLATFORM_SYSTEMSDK_VERSIONS")) {
415 halManifest->framework.mSystemSdk.mVersions.emplace(std::move(v));
416 }
417 }
418
419 outputInputs(*halManifests);
420
421 if (mOutputMatrix) {
422 CompatibilityMatrix generatedMatrix = halManifest->generateCompatibleMatrix();
423 if (!halManifest->checkCompatibility(generatedMatrix, &error, mCheckFlags)) {
424 std::cerr << "FATAL ERROR: cannot generate a compatible matrix: " << error
425 << std::endl;
426 }
427 out() << "<!-- \n"
428 " Autogenerated skeleton compatibility matrix. \n"
429 " Use with caution. Modify it to suit your needs.\n"
430 " All HALs are set to optional.\n"
431 " Many entries other than HALs are zero-filled and\n"
432 " require human attention. \n"
433 "-->\n"
434 << gCompatibilityMatrixConverter(generatedMatrix, mSerializeFlags);
435 } else {
436 out() << gHalManifestConverter(*halManifest, mSerializeFlags);
437 }
438 out().flush();
439
440 if (mCheckFile.hasStream()) {
441 CompatibilityMatrix checkMatrix;
442 checkMatrix.setFileName(mCheckFile.name());
443 if (!gCompatibilityMatrixConverter(&checkMatrix, read(mCheckFile.stream()), &error)) {
444 std::cerr << "Cannot parse check file as a compatibility matrix: " << error
445 << std::endl;
446 return false;
447 }
448 if (!checkDualFile(*halManifest, checkMatrix)) {
449 return false;
450 }
451 }
452
453 return true;
454 }
455
456 // Parse --kernel arguments and write to output matrix.
assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix * matrix)457 bool assembleFrameworkCompatibilityMatrixKernels(CompatibilityMatrix* matrix) {
458 for (auto& pair : mKernels) {
459 std::vector<ConditionedConfig> conditionedConfigs;
460 if (!parseFilesForKernelConfigs(&pair.second, &conditionedConfigs)) {
461 return false;
462 }
463 for (ConditionedConfig& conditionedConfig : conditionedConfigs) {
464 MatrixKernel kernel(KernelVersion{pair.first}, std::move(conditionedConfig.second));
465 if (conditionedConfig.first != nullptr)
466 kernel.mConditions.push_back(std::move(*conditionedConfig.first));
467 std::string error;
468 if (!matrix->addKernel(std::move(kernel), &error)) {
469 std::cerr << "Error:" << error << std::endl;
470 return false;
471 };
472 }
473 }
474 return true;
475 }
476
setDeviceFcmVersion(HalManifest * manifest)477 bool setDeviceFcmVersion(HalManifest* manifest) {
478 // Not needed for generating empty manifest for DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE.
479 if (getBooleanFlag("VINTF_IGNORE_TARGET_FCM_VERSION")) {
480 return true;
481 }
482
483 size_t shippingApiLevel = getIntegerFlag("PRODUCT_SHIPPING_API_LEVEL");
484
485 if (manifest->level() != Level::UNSPECIFIED) {
486 if (shippingApiLevel != 0) {
487 auto res = android::vintf::testing::TestTargetFcmVersion(manifest->level(),
488 shippingApiLevel);
489 if (!res.ok()) std::cerr << "Warning: " << res.error() << std::endl;
490 }
491 return true;
492 }
493 if (!getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
494 manifest->mLevel = Level::LEGACY;
495 return true;
496 }
497 // TODO(b/70628538): Do not infer from Shipping API level.
498 if (shippingApiLevel) {
499 std::cerr << "Warning: Shipping FCM Version is inferred from Shipping API level. "
500 << "Declare Shipping FCM Version in device manifest directly." << std::endl;
501 manifest->mLevel = details::convertFromApiLevel(shippingApiLevel);
502 if (manifest->mLevel == Level::UNSPECIFIED) {
503 std::cerr << "Error: Shipping FCM Version cannot be inferred from Shipping API "
504 << "level " << shippingApiLevel << "."
505 << "Declare Shipping FCM Version in device manifest directly."
506 << std::endl;
507 return false;
508 }
509 return true;
510 }
511 // TODO(b/69638851): should be an error if Shipping API level is not defined.
512 // For now, just leave it empty; when framework compatibility matrix is built,
513 // lowest FCM Version is assumed.
514 std::cerr << "Warning: Shipping FCM Version cannot be inferred, because:" << std::endl
515 << " (1) It is not explicitly declared in device manifest;" << std::endl
516 << " (2) PRODUCT_ENFORCE_VINTF_MANIFEST is set to true;" << std::endl
517 << " (3) PRODUCT_SHIPPING_API_LEVEL is undefined." << std::endl
518 << "Assuming 'unspecified' Shipping FCM Version. " << std::endl
519 << "To remove this warning, define 'level' attribute in device manifest."
520 << std::endl;
521 return true;
522 }
523
getLowestFcmVersion(const CompatibilityMatrices & matrices)524 Level getLowestFcmVersion(const CompatibilityMatrices& matrices) {
525 Level ret = Level::UNSPECIFIED;
526 for (const auto& e : matrices) {
527 if (ret == Level::UNSPECIFIED || ret > e.level()) {
528 ret = e.level();
529 }
530 }
531 return ret;
532 }
533
assembleCompatibilityMatrix(CompatibilityMatrices * matrices)534 bool assembleCompatibilityMatrix(CompatibilityMatrices* matrices) {
535 std::string error;
536 CompatibilityMatrix* matrix = nullptr;
537 std::unique_ptr<HalManifest> checkManifest;
538 std::unique_ptr<CompatibilityMatrix> builtMatrix;
539
540 if (mCheckFile.hasStream()) {
541 checkManifest = std::make_unique<HalManifest>();
542 checkManifest->setFileName(mCheckFile.name());
543 if (!gHalManifestConverter(checkManifest.get(), read(mCheckFile.stream()), &error)) {
544 std::cerr << "Cannot parse check file as a HAL manifest: " << error << std::endl;
545 return false;
546 }
547 }
548
549 if (matrices->front().mType == SchemaType::DEVICE) {
550 builtMatrix = CompatibilityMatrix::combineDeviceMatrices(matrices, &error);
551 matrix = builtMatrix.get();
552
553 if (matrix == nullptr) {
554 std::cerr << error << std::endl;
555 return false;
556 }
557
558 auto vndkVersion = base::Trim(getEnv("REQUIRED_VNDK_VERSION"));
559 if (!vndkVersion.empty()) {
560 auto& valueInMatrix = matrix->device.mVendorNdk;
561 if (!valueInMatrix.version().empty() && valueInMatrix.version() != vndkVersion) {
562 std::cerr << "Hard-coded <vendor-ndk> version in device compatibility matrix ("
563 << matrices->front().fileName() << "), '" << valueInMatrix.version()
564 << "', does not match value inferred "
565 << "from BOARD_VNDK_VERSION '" << vndkVersion << "'" << std::endl;
566 return false;
567 }
568 valueInMatrix = VendorNdk{std::move(vndkVersion)};
569 }
570
571 for (auto&& v : getEnvList("BOARD_SYSTEMSDK_VERSIONS")) {
572 matrix->device.mSystemSdk.mVersions.emplace(std::move(v));
573 }
574 }
575
576 if (matrices->front().mType == SchemaType::FRAMEWORK) {
577 Level deviceLevel =
578 checkManifest != nullptr ? checkManifest->level() : Level::UNSPECIFIED;
579 if (deviceLevel == Level::UNSPECIFIED) {
580 deviceLevel = getLowestFcmVersion(*matrices);
581 if (checkManifest != nullptr && deviceLevel != Level::UNSPECIFIED) {
582 std::cerr << "Warning: No Target FCM Version for device. Assuming \""
583 << to_string(deviceLevel)
584 << "\" when building final framework compatibility matrix."
585 << std::endl;
586 }
587 }
588 builtMatrix = CompatibilityMatrix::combine(deviceLevel, matrices, &error);
589 matrix = builtMatrix.get();
590
591 if (matrix == nullptr) {
592 std::cerr << error << std::endl;
593 return false;
594 }
595
596 if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) {
597 return false;
598 }
599
600 // Add PLATFORM_SEPOLICY_* to sepolicy.sepolicy-version. Remove dupes.
601 std::set<Version> sepolicyVersions;
602 auto sepolicyVersionStrings = getEnvList("PLATFORM_SEPOLICY_COMPAT_VERSIONS");
603 auto currentSepolicyVersionString = getEnv("PLATFORM_SEPOLICY_VERSION");
604 if (!currentSepolicyVersionString.empty()) {
605 sepolicyVersionStrings.push_back(currentSepolicyVersionString);
606 }
607 for (auto&& s : sepolicyVersionStrings) {
608 Version v;
609 if (!parse(s, &v)) {
610 std::cerr << "Error: unknown sepolicy version '" << s << "' specified by "
611 << (s == currentSepolicyVersionString
612 ? "PLATFORM_SEPOLICY_VERSION"
613 : "PLATFORM_SEPOLICY_COMPAT_VERSIONS")
614 << ".";
615 return false;
616 }
617 sepolicyVersions.insert(v);
618 }
619 for (auto&& v : sepolicyVersions) {
620 matrix->framework.mSepolicy.mSepolicyVersionRanges.emplace_back(v.majorVer,
621 v.minorVer);
622 }
623
624 if (!getFlagIfUnset("POLICYVERS",
625 &matrix->framework.mSepolicy.mKernelSepolicyVersion)) {
626 return false;
627 }
628 if (!getFlagIfUnset("FRAMEWORK_VBMETA_VERSION", &matrix->framework.mAvbMetaVersion)) {
629 return false;
630 }
631 // Hard-override existing AVB version
632 getFlag("FRAMEWORK_VBMETA_VERSION_OVERRIDE", &matrix->framework.mAvbMetaVersion,
633 false /* log */);
634 }
635 outputInputs(*matrices);
636 out() << gCompatibilityMatrixConverter(*matrix, mSerializeFlags);
637 out().flush();
638
639 if (checkManifest != nullptr && !checkDualFile(*checkManifest, *matrix)) {
640 return false;
641 }
642
643 return true;
644 }
645
646 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
647 template <typename Schema, typename AssembleFunc>
tryAssemble(const XmlConverter<Schema> & converter,const std::string & schemaName,AssembleFunc assemble,std::string * error)648 AssembleStatus tryAssemble(const XmlConverter<Schema>& converter, const std::string& schemaName,
649 AssembleFunc assemble, std::string* error) {
650 std::vector<Schema> schemas;
651 Schema schema;
652 schema.setFileName(mInFiles.front().name());
653 if (!converter(&schema, read(mInFiles.front().stream()), error)) {
654 return TRY_NEXT;
655 }
656 auto firstType = schema.type();
657 schemas.emplace_back(std::move(schema));
658
659 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
660 Schema additionalSchema;
661 const std::string& fileName = it->name();
662 additionalSchema.setFileName(fileName);
663 if (!converter(&additionalSchema, read(it->stream()), error)) {
664 std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " "
665 << schemaName << " (but the first file is a valid " << firstType << " "
666 << schemaName << "). Error: " << *error << std::endl;
667 return FAIL_AND_EXIT;
668 }
669 if (additionalSchema.type() != firstType) {
670 std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " "
671 << schemaName << " (but a " << firstType << " " << schemaName
672 << " is expected)." << std::endl;
673 return FAIL_AND_EXIT;
674 }
675
676 schemas.emplace_back(std::move(additionalSchema));
677 }
678 return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT;
679 }
680
assemble()681 bool assemble() override {
682 using std::placeholders::_1;
683 if (mInFiles.empty()) {
684 std::cerr << "Missing input file." << std::endl;
685 return false;
686 }
687
688 std::string manifestError;
689 auto status = tryAssemble(gHalManifestConverter, "manifest",
690 std::bind(&AssembleVintfImpl::assembleHalManifest, this, _1),
691 &manifestError);
692 if (status == SUCCESS) return true;
693 if (status == FAIL_AND_EXIT) return false;
694
695 resetInFiles();
696
697 std::string matrixError;
698 status = tryAssemble(gCompatibilityMatrixConverter, "compatibility matrix",
699 std::bind(&AssembleVintfImpl::assembleCompatibilityMatrix, this, _1),
700 &matrixError);
701 if (status == SUCCESS) return true;
702 if (status == FAIL_AND_EXIT) return false;
703
704 std::cerr << "Input file has unknown format." << std::endl
705 << "Error when attempting to convert to manifest: " << manifestError << std::endl
706 << "Error when attempting to convert to compatibility matrix: " << matrixError
707 << std::endl;
708 return false;
709 }
710
setOutputStream(Ostream && out)711 std::ostream& setOutputStream(Ostream&& out) override {
712 mOutRef = std::move(out);
713 return *mOutRef;
714 }
715
addInputStream(const std::string & name,Istream && in)716 std::istream& addInputStream(const std::string& name, Istream&& in) override {
717 auto it = mInFiles.emplace(mInFiles.end(), name, std::move(in));
718 return it->stream();
719 }
720
setCheckInputStream(const std::string & name,Istream && in)721 std::istream& setCheckInputStream(const std::string& name, Istream&& in) override {
722 mCheckFile = NamedIstream(name, std::move(in));
723 return mCheckFile.stream();
724 }
725
hasKernelVersion(const KernelVersion & kernelVer) const726 bool hasKernelVersion(const KernelVersion& kernelVer) const override {
727 return mKernels.find(kernelVer) != mKernels.end();
728 }
729
addKernelConfigInputStream(const KernelVersion & kernelVer,const std::string & name,Istream && in)730 std::istream& addKernelConfigInputStream(const KernelVersion& kernelVer,
731 const std::string& name, Istream&& in) override {
732 auto&& kernel = mKernels[kernelVer];
733 auto it = kernel.emplace(kernel.end(), name, std::move(in));
734 return it->stream();
735 }
736
resetInFiles()737 void resetInFiles() {
738 for (auto& inFile : mInFiles) {
739 inFile.stream().clear();
740 inFile.stream().seekg(0);
741 }
742 }
743
setOutputMatrix()744 void setOutputMatrix() override { mOutputMatrix = true; }
745
setHalsOnly()746 bool setHalsOnly() override {
747 if (mHasSetHalsOnlyFlag) {
748 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
749 return false;
750 }
751 // Just override it with HALS_ONLY because other flags that modify mSerializeFlags
752 // does not interfere with this (except --no-hals).
753 mSerializeFlags = SerializeFlags::HALS_ONLY;
754 mHasSetHalsOnlyFlag = true;
755 return true;
756 }
757
setNoHals()758 bool setNoHals() override {
759 if (mHasSetHalsOnlyFlag) {
760 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
761 return false;
762 }
763 mSerializeFlags = mSerializeFlags.disableHals();
764 mHasSetHalsOnlyFlag = true;
765 return true;
766 }
767
setNoKernelRequirements()768 bool setNoKernelRequirements() override {
769 mSerializeFlags = mSerializeFlags.disableKernelConfigs().disableKernelMinorRevision();
770 mCheckFlags = mCheckFlags.disableKernel();
771 return true;
772 }
773
774 private:
775 std::vector<NamedIstream> mInFiles;
776 Ostream mOutRef;
777 NamedIstream mCheckFile;
778 bool mOutputMatrix = false;
779 bool mHasSetHalsOnlyFlag = false;
780 SerializeFlags::Type mSerializeFlags = SerializeFlags::EVERYTHING;
781 std::map<KernelVersion, std::vector<NamedIstream>> mKernels;
782 std::map<std::string, std::string> mFakeEnv;
783 CheckFlags::Type mCheckFlags = CheckFlags::DEFAULT;
784 };
785
openOutFile(const std::string & path)786 bool AssembleVintf::openOutFile(const std::string& path) {
787 return static_cast<std::ofstream&>(setOutputStream(std::make_unique<std::ofstream>(path)))
788 .is_open();
789 }
790
openInFile(const std::string & path)791 bool AssembleVintf::openInFile(const std::string& path) {
792 return static_cast<std::ifstream&>(addInputStream(path, std::make_unique<std::ifstream>(path)))
793 .is_open();
794 }
795
openCheckFile(const std::string & path)796 bool AssembleVintf::openCheckFile(const std::string& path) {
797 return static_cast<std::ifstream&>(
798 setCheckInputStream(path, std::make_unique<std::ifstream>(path)))
799 .is_open();
800 }
801
addKernel(const std::string & kernelArg)802 bool AssembleVintf::addKernel(const std::string& kernelArg) {
803 auto tokens = base::Split(kernelArg, ":");
804 if (tokens.size() <= 1) {
805 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
806 return false;
807 }
808 KernelVersion kernelVer;
809 if (!parse(tokens.front(), &kernelVer)) {
810 std::cerr << "Unrecognized kernel version '" << tokens.front() << "'" << std::endl;
811 return false;
812 }
813 if (hasKernelVersion(kernelVer)) {
814 std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl;
815 return false;
816 }
817 for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) {
818 bool opened =
819 static_cast<std::ifstream&>(
820 addKernelConfigInputStream(kernelVer, *it, std::make_unique<std::ifstream>(*it)))
821 .is_open();
822 if (!opened) {
823 std::cerr << "Cannot open file '" << *it << "'." << std::endl;
824 return false;
825 }
826 }
827 return true;
828 }
829
newInstance()830 std::unique_ptr<AssembleVintf> AssembleVintf::newInstance() {
831 return std::make_unique<AssembleVintfImpl>();
832 }
833
834 } // namespace vintf
835 } // namespace android
836