blob: 0bcb3632930e848a25a3833464da960c5749b386 [file] [log] [blame]
Yifan Hongccf967b2017-01-18 11:04:19 -08001/*
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
18#define LOG_TAG "libvintf"
19
Yifan Honga7201e72017-02-17 10:09:59 -080020#include "RuntimeInfo.h"
Yifan Hongccf967b2017-01-18 11:04:19 -080021
Yifan Hongc66ad1e2017-02-08 20:19:45 -080022#include "CompatibilityMatrix.h"
23#include "parse_string.h"
24
Yifan Hongccf967b2017-01-18 11:04:19 -080025namespace android {
26namespace vintf {
27
Yifan Honga7201e72017-02-17 10:09:59 -080028const std::string &RuntimeInfo::osName() const {
Yifan Hongccf967b2017-01-18 11:04:19 -080029 return mOsName;
30}
31
Yifan Honga7201e72017-02-17 10:09:59 -080032const std::string &RuntimeInfo::nodeName() const {
Yifan Hongccf967b2017-01-18 11:04:19 -080033 return mNodeName;
34}
35
Yifan Honga7201e72017-02-17 10:09:59 -080036const std::string &RuntimeInfo::osRelease() const {
Yifan Hongccf967b2017-01-18 11:04:19 -080037 return mOsRelease;
38}
39
Yifan Honga7201e72017-02-17 10:09:59 -080040const std::string &RuntimeInfo::osVersion() const {
Yifan Hongccf967b2017-01-18 11:04:19 -080041 return mOsVersion;
42}
43
Yifan Honga7201e72017-02-17 10:09:59 -080044const std::string &RuntimeInfo::hardwareId() const {
Yifan Hongccf967b2017-01-18 11:04:19 -080045 return mHardwareId;
46}
47
Yifan Hongab0e6e72017-04-26 11:43:47 -070048const KernelVersion &RuntimeInfo::kernelVersion() const {
49 return mKernelVersion;
50}
51
Yifan Hongab0e6e72017-04-26 11:43:47 -070052const std::map<std::string, std::string> &RuntimeInfo::kernelConfigs() const {
53 return mKernelConfigs;
54}
55
Yifan Honga7201e72017-02-17 10:09:59 -080056size_t RuntimeInfo::kernelSepolicyVersion() const {
Yifan Hongccf967b2017-01-18 11:04:19 -080057 return mKernelSepolicyVersion;
58}
59
Yifan Hong242eabf2017-04-20 14:06:26 -070060const std::string &RuntimeInfo::cpuInfo() const {
61 return mCpuInfo;
62}
63
Yifan Hongab0e6e72017-04-26 11:43:47 -070064const Version &RuntimeInfo::bootVbmetaAvbVersion() const {
65 return mBootVbmetaAvbVersion;
66}
67
68const Version &RuntimeInfo::bootAvbVersion() const {
69 return mBootAvbVersion;
70}
71
Yifan Hongd776d202017-08-23 12:56:31 -070072bool RuntimeInfo::matchKernelConfigs(const std::vector<KernelConfig>& matrixConfigs,
73 std::string* error) const {
74 for (const KernelConfig& matrixConfig : matrixConfigs) {
75 const std::string& key = matrixConfig.first;
76 auto it = this->mKernelConfigs.find(key);
77 if (it == this->mKernelConfigs.end()) {
78 // special case: <value type="tristate">n</value> matches if the config doesn't exist.
79 if (matrixConfig.second == KernelConfigTypedValue::gMissingConfig) {
80 continue;
81 }
82 if (error != nullptr) {
83 *error = "Missing config " + key;
84 }
85 return false;
86 }
87 const std::string& kernelValue = it->second;
88 if (!matrixConfig.second.matchValue(kernelValue)) {
89 if (error != nullptr) {
90 *error = "For config " + key + ", value = " + kernelValue + " but required " +
91 to_string(matrixConfig.second);
92 }
93 return false;
94 }
95 }
96 return true;
97}
98
99bool RuntimeInfo::matchKernelVersion(const KernelVersion& minLts) const {
100 return minLts.version == mKernelVersion.version && minLts.majorRev == mKernelVersion.majorRev &&
101 minLts.minorRev <= mKernelVersion.minorRev;
102}
103
Yifan Honga7201e72017-02-17 10:09:59 -0800104bool RuntimeInfo::checkCompatibility(const CompatibilityMatrix &mat,
Yifan Hongc66ad1e2017-02-08 20:19:45 -0800105 std::string *error) const {
Yifan Hong7c7d7062017-04-04 16:26:51 -0700106 if (mat.mType != SchemaType::FRAMEWORK) {
107 if (error != nullptr) {
108 *error = "Should not check runtime info against " + to_string(mat.mType)
109 + " compatibility matrix.";
110 }
111 return false;
112 }
113 if (kernelSepolicyVersion() != mat.framework.mSepolicy.kernelSepolicyVersion()) {
Yifan Hongc66ad1e2017-02-08 20:19:45 -0800114 if (error != nullptr) {
115 *error = "kernelSepolicyVersion = " + to_string(kernelSepolicyVersion())
Yifan Hong7c7d7062017-04-04 16:26:51 -0700116 + " but required " + to_string(mat.framework.mSepolicy.kernelSepolicyVersion());
Yifan Hongc66ad1e2017-02-08 20:19:45 -0800117 }
118 return false;
119 }
120
Yifan Hongd776d202017-08-23 12:56:31 -0700121 // mat.mSepolicy.sepolicyVersion() is checked against static
122 // HalManifest.device.mSepolicyVersion in HalManifest::checkCompatibility.
Yifan Hongc66ad1e2017-02-08 20:19:45 -0800123
Yifan Hongd776d202017-08-23 12:56:31 -0700124 bool foundMatchedKernelVersion = false;
125 bool foundMatchedConditions = false;
126 for (const MatrixKernel& matrixKernel : mat.framework.mKernels) {
127 if (!matchKernelVersion(matrixKernel.minLts())) {
128 continue;
129 }
130 foundMatchedKernelVersion = true;
131 // ignore this fragment if not all conditions are met.
132 if (!matchKernelConfigs(matrixKernel.conditions(), error)) {
133 continue;
134 }
135 foundMatchedConditions = true;
136 if (!matchKernelConfigs(matrixKernel.configs(), error)) {
137 return false;
138 }
139 }
140 if (!foundMatchedKernelVersion) {
Yifan Hongc66ad1e2017-02-08 20:19:45 -0800141 if (error != nullptr) {
Yifan Hongd776d202017-08-23 12:56:31 -0700142 std::stringstream ss;
143 ss << "Framework is incompatible with kernel version " << mKernelVersion
144 << ", compatible kernel versions are";
145 for (const MatrixKernel& matrixKernel : mat.framework.mKernels)
146 ss << " " << matrixKernel.minLts();
147 *error = ss.str();
Yifan Hongc66ad1e2017-02-08 20:19:45 -0800148 }
149 return false;
150 }
Yifan Hongd776d202017-08-23 12:56:31 -0700151 if (!foundMatchedConditions) {
152 // This should not happen because first <conditions> for each <kernel> must be
153 // empty. Reject here for inconsistency.
154 if (error != nullptr) {
155 error->insert(0, "Framework match kernel version with unmet conditions:");
Yifan Hongc66ad1e2017-02-08 20:19:45 -0800156 }
Yifan Hongd776d202017-08-23 12:56:31 -0700157 return false;
158 }
159 if (error != nullptr) {
160 error->clear();
Yifan Hongc66ad1e2017-02-08 20:19:45 -0800161 }
Yifan Hongf3029302017-04-12 17:23:49 -0700162
163 const Version &matAvb = mat.framework.mAvbMetaVersion;
Michael Schwartz5cfbc922017-05-08 14:06:49 -0700164 if (mBootAvbVersion.majorVer != matAvb.majorVer || mBootAvbVersion.minorVer < matAvb.minorVer) {
165 if (error != nullptr) {
166 std::stringstream ss;
167 ss << "AVB version " << mBootAvbVersion << " does not match framework matrix "
168 << matAvb;
169 *error = ss.str();
170 }
171 return false;
172 }
173 if (mBootVbmetaAvbVersion.majorVer != matAvb.majorVer ||
174 mBootVbmetaAvbVersion.minorVer < matAvb.minorVer) {
175 if (error != nullptr) {
176 std::stringstream ss;
177 ss << "Vbmeta version " << mBootVbmetaAvbVersion << " does not match framework matrix "
178 << matAvb;
179 *error = ss.str();
180 }
Yifan Hongf3029302017-04-12 17:23:49 -0700181 return false;
182 }
183
Yifan Hongc66ad1e2017-02-08 20:19:45 -0800184 return true;
185}
186
Yifan Hongccf967b2017-01-18 11:04:19 -0800187} // namespace vintf
188} // namespace android