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 "constants-private.h"
35 #include "utils.h"
36
37 #define BUFFER_SIZE sysconf(_SC_PAGESIZE)
38
39 namespace android {
40 namespace vintf {
41
42 static const std::string gConfigPrefix = "android-base-";
43 static const std::string gConfigSuffix = ".config";
44 static const std::string gBaseConfig = "android-base.config";
45
46 // An input stream with a name.
47 // The input stream may be an actual file, or a stringstream for testing.
48 // It takes ownership on the istream.
49 class NamedIstream {
50 public:
51 NamedIstream() = default;
NamedIstream(const std::string & name,std::unique_ptr<std::istream> && stream)52 NamedIstream(const std::string& name, std::unique_ptr<std::istream>&& stream)
53 : mName(name), mStream(std::move(stream)) {}
name() const54 const std::string& name() const { return mName; }
stream()55 std::istream& stream() { return *mStream; }
hasStream()56 bool hasStream() { return mStream != nullptr; }
57
58 private:
59 std::string mName;
60 std::unique_ptr<std::istream> mStream;
61 };
62
63 /**
64 * Slurps the device manifest file and add build time flag to it.
65 */
66 class AssembleVintfImpl : public AssembleVintf {
67 using Condition = std::unique_ptr<KernelConfig>;
68 using ConditionedConfig = std::pair<Condition, std::vector<KernelConfig> /* configs */>;
69
70 public:
setFakeEnv(const std::string & key,const std::string & value)71 void setFakeEnv(const std::string& key, const std::string& value) { mFakeEnv[key] = value; }
72
getEnv(const std::string & key) const73 std::string getEnv(const std::string& key) const {
74 auto it = mFakeEnv.find(key);
75 if (it != mFakeEnv.end()) {
76 return it->second;
77 }
78 const char* envValue = getenv(key.c_str());
79 return envValue != nullptr ? std::string(envValue) : std::string();
80 }
81
82 // Get environment variable and split with space.
getEnvList(const std::string & key) const83 std::vector<std::string> getEnvList(const std::string& key) const {
84 std::vector<std::string> ret;
85 for (auto&& v : base::Split(getEnv(key), " ")) {
86 v = base::Trim(v);
87 if (!v.empty()) {
88 ret.push_back(v);
89 }
90 }
91 return ret;
92 }
93
94 template <typename T>
getFlag(const std::string & key,T * value,bool log=true) const95 bool getFlag(const std::string& key, T* value, bool log = true) const {
96 std::string envValue = getEnv(key);
97 if (envValue.empty()) {
98 if (log) {
99 std::cerr << "Warning: " << key << " is missing, defaulted to " << (*value) << "."
100 << std::endl;
101 }
102 return true;
103 }
104
105 if (!parse(envValue, value)) {
106 std::cerr << "Cannot parse " << envValue << "." << std::endl;
107 return false;
108 }
109 return true;
110 }
111
112 /**
113 * Set *out to environment variable only if *out is default constructed.
114 * Return false if a fatal error has occurred:
115 * - The environment variable has an unknown format
116 * - The value of the environment variable does not match a predefined variable in the files
117 */
118 template <typename T>
getFlagIfUnset(const std::string & envKey,T * out) const119 bool getFlagIfUnset(const std::string& envKey, T* out) const {
120 bool hasExistingValue = !(*out == T{});
121
122 bool hasEnvValue = false;
123 T envValue;
124 std::string envStrValue = getEnv(envKey);
125 if (!envStrValue.empty()) {
126 if (!parse(envStrValue, &envValue)) {
127 std::cerr << "Cannot parse " << envValue << "." << std::endl;
128 return false;
129 }
130 hasEnvValue = true;
131 }
132
133 if (hasExistingValue) {
134 if (hasEnvValue && (*out != envValue)) {
135 std::cerr << "Cannot override existing value " << *out << " with " << envKey
136 << " (which is " << envValue << ")." << std::endl;
137 return false;
138 }
139 return true;
140 }
141 if (hasEnvValue) {
142 *out = envValue;
143 }
144 return true;
145 }
146
getBooleanFlag(const std::string & key) const147 bool getBooleanFlag(const std::string& key) const { return getEnv(key) == std::string("true"); }
148
getIntegerFlag(const std::string & key,size_t defaultValue=0) const149 size_t getIntegerFlag(const std::string& key, size_t defaultValue = 0) const {
150 std::string envValue = getEnv(key);
151 if (envValue.empty()) {
152 return defaultValue;
153 }
154 size_t value;
155 if (!base::ParseUint(envValue, &value)) {
156 std::cerr << "Error: " << key << " must be a number." << std::endl;
157 return defaultValue;
158 }
159 return value;
160 }
161
read(std::basic_istream<char> & is)162 static std::string read(std::basic_istream<char>& is) {
163 std::stringstream ss;
164 ss << is.rdbuf();
165 return ss.str();
166 }
167
168 // Return true if name of file is "android-base.config". This file must be specified
169 // exactly once for each kernel version. These requirements do not have any conditions.
isCommonConfig(const std::string & path)170 static bool isCommonConfig(const std::string& path) {
171 return ::android::base::Basename(path) == gBaseConfig;
172 }
173
174 // Return true if name of file matches "android-base-foo.config".
175 // Zero or more conditional configs may be specified for each kernel version. These
176 // requirements are conditional on CONFIG_FOO=y.
isConditionalConfig(const std::string & path)177 static bool isConditionalConfig(const std::string& path) {
178 auto fname = ::android::base::Basename(path);
179 return ::android::base::StartsWith(fname, gConfigPrefix) &&
180 ::android::base::EndsWith(fname, gConfigSuffix);
181 }
182
183 // Return true for all other file names (i.e. not android-base.config, and not conditional
184 // configs.)
185 // Zero or more conditional configs may be specified for each kernel version.
186 // These requirements do not have any conditions.
isExtraCommonConfig(const std::string & path)187 static bool isExtraCommonConfig(const std::string& path) {
188 return !isCommonConfig(path) && !isConditionalConfig(path);
189 }
190
191 // nullptr on any error, otherwise the condition.
generateCondition(const std::string & path)192 static Condition generateCondition(const std::string& path) {
193 if (!isConditionalConfig(path)) {
194 return nullptr;
195 }
196 auto fname = ::android::base::Basename(path);
197 std::string sub = fname.substr(gConfigPrefix.size(),
198 fname.size() - gConfigPrefix.size() - gConfigSuffix.size());
199 if (sub.empty()) {
200 return nullptr; // should not happen
201 }
202 for (size_t i = 0; i < sub.size(); ++i) {
203 if (sub[i] == '-') {
204 sub[i] = '_';
205 continue;
206 }
207 if (isalnum(sub[i])) {
208 sub[i] = toupper(sub[i]);
209 continue;
210 }
211 std::cerr << "'" << fname << "' (in " << path
212 << ") is not a valid kernel config file name. Must match regex: "
213 << "android-base(-[0-9a-zA-Z-]+)?\\" << gConfigSuffix
214 << std::endl;
215 return nullptr;
216 }
217 sub.insert(0, "CONFIG_");
218 return std::make_unique<KernelConfig>(std::move(sub), Tristate::YES);
219 }
220
parseFileForKernelConfigs(std::basic_istream<char> & stream,std::vector<KernelConfig> * out)221 static bool parseFileForKernelConfigs(std::basic_istream<char>& stream,
222 std::vector<KernelConfig>* out) {
223 KernelConfigParser parser(true /* processComments */, true /* relaxedFormat */);
224 status_t err = parser.processAndFinish(read(stream));
225 if (err != OK) {
226 std::cerr << parser.error();
227 return false;
228 }
229
230 for (auto& configPair : parser.configs()) {
231 out->push_back({});
232 KernelConfig& config = out->back();
233 config.first = std::move(configPair.first);
234 if (!parseKernelConfigTypedValue(configPair.second, &config.second)) {
235 std::cerr << "Unknown value type for key = '" << config.first << "', value = '"
236 << configPair.second << "'\n";
237 return false;
238 }
239 }
240 return true;
241 }
242
parseFilesForKernelConfigs(std::vector<NamedIstream> * streams,std::vector<ConditionedConfig> * out)243 static bool parseFilesForKernelConfigs(std::vector<NamedIstream>* streams,
244 std::vector<ConditionedConfig>* out) {
245 out->clear();
246 ConditionedConfig commonConfig;
247 bool foundCommonConfig = false;
248 bool ret = true;
249
250 for (auto& namedStream : *streams) {
251 if (isCommonConfig(namedStream.name()) || isExtraCommonConfig(namedStream.name())) {
252 if (!parseFileForKernelConfigs(namedStream.stream(), &commonConfig.second)) {
253 std::cerr << "Failed to generate common configs for file "
254 << namedStream.name();
255 ret = false;
256 }
257 if (isCommonConfig(namedStream.name())) {
258 foundCommonConfig = true;
259 }
260 } else {
261 Condition condition = generateCondition(namedStream.name());
262 if (condition == nullptr) {
263 std::cerr << "Failed to generate conditional configs for file "
264 << namedStream.name();
265 ret = false;
266 }
267
268 std::vector<KernelConfig> kernelConfigs;
269 if ((ret &= parseFileForKernelConfigs(namedStream.stream(), &kernelConfigs)))
270 out->emplace_back(std::move(condition), std::move(kernelConfigs));
271 }
272 }
273
274 if (!foundCommonConfig) {
275 std::cerr << "No " << gBaseConfig << " is found in these paths:" << std::endl;
276 for (auto& namedStream : *streams) {
277 std::cerr << " " << namedStream.name() << std::endl;
278 }
279 ret = false;
280 }
281 // first element is always common configs (no conditions).
282 out->insert(out->begin(), std::move(commonConfig));
283 return ret;
284 }
285
out() const286 std::basic_ostream<char>& out() const { return mOutRef == nullptr ? std::cout : *mOutRef; }
287
288 // If -c is provided, check it.
checkDualFile(const HalManifest & manifest,const CompatibilityMatrix & matrix)289 bool checkDualFile(const HalManifest& manifest, const CompatibilityMatrix& matrix) {
290 if (getBooleanFlag("PRODUCT_ENFORCE_VINTF_MANIFEST")) {
291 std::string error;
292 if (!manifest.checkCompatibility(matrix, &error, mCheckFlags)) {
293 std::cerr << "Not compatible: " << error << std::endl;
294 return false;
295 }
296 }
297 return true;
298 }
299
300 using HalManifests = std::vector<HalManifest>;
301 using CompatibilityMatrices = std::vector<CompatibilityMatrix>;
302
303 template <typename M>
outputInputs(const std::vector<M> & inputs)304 void outputInputs(const std::vector<M>& inputs) {
305 out() << "<!--" << std::endl;
306 out() << " Input:" << std::endl;
307 for (const auto& e : inputs) {
308 if (!e.fileName().empty()) {
309 out() << " " << base::Basename(e.fileName()) << std::endl;
310 }
311 }
312 out() << "-->" << std::endl;
313 }
314
315 // Parse --kernel arguments and write to output manifest.
setDeviceManifestKernel(HalManifest * manifest)316 bool setDeviceManifestKernel(HalManifest* manifest) {
317 if (mKernels.empty()) {
318 return true;
319 }
320 if (mKernels.size() > 1) {
321 std::cerr << "Warning: multiple --kernel is specified when building device manifest. "
322 << "Only the first one will be used." << std::endl;
323 }
324 auto& kernelArg = *mKernels.begin();
325 const auto& kernelVer = kernelArg.first;
326 auto& kernelConfigFiles = kernelArg.second;
327 // addKernel() guarantees that !kernelConfigFiles.empty().
328 if (kernelConfigFiles.size() > 1) {
329 std::cerr << "Warning: multiple config files are specified in --kernel when building "
330 << "device manfiest. Only the first one will be used." << std::endl;
331 }
332
333 KernelConfigParser parser(true /* processComments */, false /* relaxedFormat */);
334 status_t err = parser.processAndFinish(read(kernelConfigFiles[0].stream()));
335 if (err != OK) {
336 std::cerr << parser.error();
337 return false;
338 }
339
340 // Set version and configs in manifest.
341 auto kernel_info = std::make_optional<KernelInfo>();
342 kernel_info->mVersion = kernelVer;
343 kernel_info->mConfigs = parser.configs();
344 std::string error;
345 if (!manifest->mergeKernel(&kernel_info, &error)) {
346 std::cerr << error << "\n";
347 return false;
348 }
349 return true;
350 }
351
checkDeviceManifestNoKernelLevel(const HalManifest & manifest)352 bool checkDeviceManifestNoKernelLevel(const HalManifest& manifest) {
353 if (manifest.level() != Level::UNSPECIFIED &&
354 manifest.level() >= details::kEnforceDeviceManifestNoKernelLevel &&
355 // Use manifest.kernel()->level() directly because inferredKernelLevel()
356 // reads manifest.level().
357 manifest.kernel().has_value() && manifest.kernel()->level() != Level::UNSPECIFIED) {
358 std::cerr << "Error: Device manifest with level " << manifest.level()
359 << " must not set kernel level " << manifest.kernel()->level() << std::endl;
360 return false;
361 }
362 return true;
363 }
364
assembleHalManifest(HalManifests * halManifests)365 bool assembleHalManifest(HalManifests* halManifests) {
366 std::string error;
367 HalManifest* halManifest = &halManifests->front();
368 for (auto it = halManifests->begin() + 1; it != halManifests->end(); ++it) {
369 const std::string& path = it->fileName();
370 HalManifest& manifestToAdd = *it;
371
372 if (manifestToAdd.level() != Level::UNSPECIFIED) {
373 if (halManifest->level() == Level::UNSPECIFIED) {
374 halManifest->mLevel = manifestToAdd.level();
375 } else if (halManifest->level() != manifestToAdd.level()) {
376 std::cerr << "Inconsistent FCM Version in HAL manifests:" << std::endl
377 << " File '" << halManifests->front().fileName() << "' has level "
378 << halManifest->level() << std::endl
379 << " File '" << path << "' has level " << manifestToAdd.level()
380 << std::endl;
381 return false;
382 }
383 }
384
385 if (!halManifest->addAll(&manifestToAdd, &error)) {
386 std::cerr << "File \"" << path << "\" cannot be added: " << error << std::endl;
387 return false;
388 }
389 }
390
391 if (halManifest->mType == SchemaType::DEVICE) {
392 if (!getFlagIfUnset("BOARD_SEPOLICY_VERS", &halManifest->device.mSepolicyVersion)) {
393 return false;
394 }
395
396 if (!setDeviceFcmVersion(halManifest)) {
397 return false;
398 }
399
400 if (!setDeviceManifestKernel(halManifest)) {
401 return false;
402 }
403
404 if (!checkDeviceManifestNoKernelLevel(*halManifest)) {
405 return false;
406 }
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 << toXml(generatedMatrix, mSerializeFlags);
435 } else {
436 out() << toXml(*halManifest, mSerializeFlags);
437 }
438 out().flush();
439
440 if (mCheckFile.hasStream()) {
441 CompatibilityMatrix checkMatrix;
442 checkMatrix.setFileName(mCheckFile.name());
443 if (!fromXml(&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 (!fromXml(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 // No <kernel> tags to assemble at this point
589 const auto kernelLevel = Level::UNSPECIFIED;
590 builtMatrix = CompatibilityMatrix::combine(deviceLevel, kernelLevel, matrices, &error);
591 matrix = builtMatrix.get();
592
593 if (matrix == nullptr) {
594 std::cerr << error << std::endl;
595 return false;
596 }
597
598 if (!assembleFrameworkCompatibilityMatrixKernels(matrix)) {
599 return false;
600 }
601
602 // Add PLATFORM_SEPOLICY_* to sepolicy.sepolicy-version. Remove dupes.
603 std::set<Version> sepolicyVersions;
604 auto sepolicyVersionStrings = getEnvList("PLATFORM_SEPOLICY_COMPAT_VERSIONS");
605 auto currentSepolicyVersionString = getEnv("PLATFORM_SEPOLICY_VERSION");
606 if (!currentSepolicyVersionString.empty()) {
607 sepolicyVersionStrings.push_back(currentSepolicyVersionString);
608 }
609 for (auto&& s : sepolicyVersionStrings) {
610 Version v;
611 if (!parse(s, &v)) {
612 std::cerr << "Error: unknown sepolicy version '" << s << "' specified by "
613 << (s == currentSepolicyVersionString
614 ? "PLATFORM_SEPOLICY_VERSION"
615 : "PLATFORM_SEPOLICY_COMPAT_VERSIONS")
616 << ".";
617 return false;
618 }
619 sepolicyVersions.insert(v);
620 }
621 for (auto&& v : sepolicyVersions) {
622 matrix->framework.mSepolicy.mSepolicyVersionRanges.emplace_back(v.majorVer,
623 v.minorVer);
624 }
625
626 if (!getFlagIfUnset("POLICYVERS",
627 &matrix->framework.mSepolicy.mKernelSepolicyVersion)) {
628 return false;
629 }
630 if (!getFlagIfUnset("FRAMEWORK_VBMETA_VERSION", &matrix->framework.mAvbMetaVersion)) {
631 return false;
632 }
633 // Hard-override existing AVB version
634 getFlag("FRAMEWORK_VBMETA_VERSION_OVERRIDE", &matrix->framework.mAvbMetaVersion,
635 false /* log */);
636 }
637 outputInputs(*matrices);
638 out() << toXml(*matrix, mSerializeFlags);
639 out().flush();
640
641 if (checkManifest != nullptr && !checkDualFile(*checkManifest, *matrix)) {
642 return false;
643 }
644
645 return true;
646 }
647
648 enum AssembleStatus { SUCCESS, FAIL_AND_EXIT, TRY_NEXT };
649 template <typename Schema, typename AssembleFunc>
tryAssemble(const std::string & schemaName,AssembleFunc assemble,std::string * error)650 AssembleStatus tryAssemble(const std::string& schemaName, AssembleFunc assemble,
651 std::string* error) {
652 std::vector<Schema> schemas;
653 Schema schema;
654 schema.setFileName(mInFiles.front().name());
655 if (!fromXml(&schema, read(mInFiles.front().stream()), error)) {
656 return TRY_NEXT;
657 }
658 auto firstType = schema.type();
659 schemas.emplace_back(std::move(schema));
660
661 for (auto it = mInFiles.begin() + 1; it != mInFiles.end(); ++it) {
662 Schema additionalSchema;
663 const std::string& fileName = it->name();
664 additionalSchema.setFileName(fileName);
665 if (!fromXml(&additionalSchema, read(it->stream()), error)) {
666 std::cerr << "File \"" << fileName << "\" is not a valid " << firstType << " "
667 << schemaName << " (but the first file is a valid " << firstType << " "
668 << schemaName << "). Error: " << *error << std::endl;
669 return FAIL_AND_EXIT;
670 }
671 if (additionalSchema.type() != firstType) {
672 std::cerr << "File \"" << fileName << "\" is a " << additionalSchema.type() << " "
673 << schemaName << " (but a " << firstType << " " << schemaName
674 << " is expected)." << std::endl;
675 return FAIL_AND_EXIT;
676 }
677
678 schemas.emplace_back(std::move(additionalSchema));
679 }
680 return assemble(&schemas) ? SUCCESS : FAIL_AND_EXIT;
681 }
682
assemble()683 bool assemble() override {
684 using std::placeholders::_1;
685 if (mInFiles.empty()) {
686 std::cerr << "Missing input file." << std::endl;
687 return false;
688 }
689
690 std::string manifestError;
691 auto status = tryAssemble<HalManifest>(
692 "manifest", std::bind(&AssembleVintfImpl::assembleHalManifest, this, _1),
693 &manifestError);
694 if (status == SUCCESS) return true;
695 if (status == FAIL_AND_EXIT) return false;
696
697 resetInFiles();
698
699 std::string matrixError;
700 status = tryAssemble<CompatibilityMatrix>(
701 "compatibility matrix",
702 std::bind(&AssembleVintfImpl::assembleCompatibilityMatrix, this, _1), &matrixError);
703 if (status == SUCCESS) return true;
704 if (status == FAIL_AND_EXIT) return false;
705
706 std::cerr << "Input file has unknown format." << std::endl
707 << "Error when attempting to convert to manifest: " << manifestError << std::endl
708 << "Error when attempting to convert to compatibility matrix: " << matrixError
709 << std::endl;
710 return false;
711 }
712
setOutputStream(Ostream && out)713 std::ostream& setOutputStream(Ostream&& out) override {
714 mOutRef = std::move(out);
715 return *mOutRef;
716 }
717
addInputStream(const std::string & name,Istream && in)718 std::istream& addInputStream(const std::string& name, Istream&& in) override {
719 auto it = mInFiles.emplace(mInFiles.end(), name, std::move(in));
720 return it->stream();
721 }
722
setCheckInputStream(const std::string & name,Istream && in)723 std::istream& setCheckInputStream(const std::string& name, Istream&& in) override {
724 mCheckFile = NamedIstream(name, std::move(in));
725 return mCheckFile.stream();
726 }
727
hasKernelVersion(const KernelVersion & kernelVer) const728 bool hasKernelVersion(const KernelVersion& kernelVer) const override {
729 return mKernels.find(kernelVer) != mKernels.end();
730 }
731
addKernelConfigInputStream(const KernelVersion & kernelVer,const std::string & name,Istream && in)732 std::istream& addKernelConfigInputStream(const KernelVersion& kernelVer,
733 const std::string& name, Istream&& in) override {
734 auto&& kernel = mKernels[kernelVer];
735 auto it = kernel.emplace(kernel.end(), name, std::move(in));
736 return it->stream();
737 }
738
resetInFiles()739 void resetInFiles() {
740 for (auto& inFile : mInFiles) {
741 inFile.stream().clear();
742 inFile.stream().seekg(0);
743 }
744 }
745
setOutputMatrix()746 void setOutputMatrix() override { mOutputMatrix = true; }
747
setHalsOnly()748 bool setHalsOnly() override {
749 if (mHasSetHalsOnlyFlag) {
750 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
751 return false;
752 }
753 // Just override it with HALS_ONLY because other flags that modify mSerializeFlags
754 // does not interfere with this (except --no-hals).
755 mSerializeFlags = SerializeFlags::HALS_ONLY;
756 mHasSetHalsOnlyFlag = true;
757 return true;
758 }
759
setNoHals()760 bool setNoHals() override {
761 if (mHasSetHalsOnlyFlag) {
762 std::cerr << "Error: Cannot set --hals-only with --no-hals." << std::endl;
763 return false;
764 }
765 mSerializeFlags = mSerializeFlags.disableHals();
766 mHasSetHalsOnlyFlag = true;
767 return true;
768 }
769
setNoKernelRequirements()770 bool setNoKernelRequirements() override {
771 mSerializeFlags = mSerializeFlags.disableKernelConfigs().disableKernelMinorRevision();
772 mCheckFlags = mCheckFlags.disableKernel();
773 return true;
774 }
775
776 private:
777 std::vector<NamedIstream> mInFiles;
778 Ostream mOutRef;
779 NamedIstream mCheckFile;
780 bool mOutputMatrix = false;
781 bool mHasSetHalsOnlyFlag = false;
782 SerializeFlags::Type mSerializeFlags = SerializeFlags::EVERYTHING;
783 std::map<KernelVersion, std::vector<NamedIstream>> mKernels;
784 std::map<std::string, std::string> mFakeEnv;
785 CheckFlags::Type mCheckFlags = CheckFlags::DEFAULT;
786 };
787
openOutFile(const std::string & path)788 bool AssembleVintf::openOutFile(const std::string& path) {
789 return static_cast<std::ofstream&>(setOutputStream(std::make_unique<std::ofstream>(path)))
790 .is_open();
791 }
792
openInFile(const std::string & path)793 bool AssembleVintf::openInFile(const std::string& path) {
794 return static_cast<std::ifstream&>(addInputStream(path, std::make_unique<std::ifstream>(path)))
795 .is_open();
796 }
797
openCheckFile(const std::string & path)798 bool AssembleVintf::openCheckFile(const std::string& path) {
799 return static_cast<std::ifstream&>(
800 setCheckInputStream(path, std::make_unique<std::ifstream>(path)))
801 .is_open();
802 }
803
addKernel(const std::string & kernelArg)804 bool AssembleVintf::addKernel(const std::string& kernelArg) {
805 auto tokens = base::Split(kernelArg, ":");
806 if (tokens.size() <= 1) {
807 std::cerr << "Unrecognized --kernel option '" << kernelArg << "'" << std::endl;
808 return false;
809 }
810 KernelVersion kernelVer;
811 if (!parse(tokens.front(), &kernelVer)) {
812 std::cerr << "Unrecognized kernel version '" << tokens.front() << "'" << std::endl;
813 return false;
814 }
815 if (hasKernelVersion(kernelVer)) {
816 std::cerr << "Multiple --kernel for " << kernelVer << " is specified." << std::endl;
817 return false;
818 }
819 for (auto it = tokens.begin() + 1; it != tokens.end(); ++it) {
820 bool opened =
821 static_cast<std::ifstream&>(
822 addKernelConfigInputStream(kernelVer, *it, std::make_unique<std::ifstream>(*it)))
823 .is_open();
824 if (!opened) {
825 std::cerr << "Cannot open file '" << *it << "'." << std::endl;
826 return false;
827 }
828 }
829 return true;
830 }
831
newInstance()832 std::unique_ptr<AssembleVintf> AssembleVintf::newInstance() {
833 return std::make_unique<AssembleVintfImpl>();
834 }
835
836 } // namespace vintf
837 } // namespace android
838