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 
18 #define LOG_TAG "libvintf"
19 
20 #include "RuntimeInfo.h"
21 
22 #include "CompatibilityMatrix.h"
23 #include "parse_string.h"
24 
25 namespace android {
26 namespace vintf {
27 
osName() const28 const std::string &RuntimeInfo::osName() const {
29     return mOsName;
30 }
31 
nodeName() const32 const std::string &RuntimeInfo::nodeName() const {
33     return mNodeName;
34 }
35 
osRelease() const36 const std::string &RuntimeInfo::osRelease() const {
37     return mOsRelease;
38 }
39 
osVersion() const40 const std::string &RuntimeInfo::osVersion() const {
41     return mOsVersion;
42 }
43 
hardwareId() const44 const std::string &RuntimeInfo::hardwareId() const {
45     return mHardwareId;
46 }
47 
kernelVersion() const48 const KernelVersion &RuntimeInfo::kernelVersion() const {
49     return mKernel.version();
50 }
51 
kernelConfigs() const52 const std::map<std::string, std::string> &RuntimeInfo::kernelConfigs() const {
53     return mKernel.configs();
54 }
55 
kernelSepolicyVersion() const56 size_t RuntimeInfo::kernelSepolicyVersion() const {
57     return mKernelSepolicyVersion;
58 }
59 
cpuInfo() const60 const std::string &RuntimeInfo::cpuInfo() const {
61     return mCpuInfo;
62 }
63 
bootVbmetaAvbVersion() const64 const Version &RuntimeInfo::bootVbmetaAvbVersion() const {
65     return mBootVbmetaAvbVersion;
66 }
67 
bootAvbVersion() const68 const Version &RuntimeInfo::bootAvbVersion() const {
69     return mBootAvbVersion;
70 }
71 
isMainlineKernel() const72 bool RuntimeInfo::isMainlineKernel() const {
73     return mIsMainline;
74 }
75 
checkCompatibility(const CompatibilityMatrix & mat,std::string * error,CheckFlags::Type flags) const76 bool RuntimeInfo::checkCompatibility(const CompatibilityMatrix& mat, std::string* error,
77                                      CheckFlags::Type flags) const {
78     if (mat.mType != SchemaType::FRAMEWORK) {
79         if (error != nullptr) {
80             *error = "Should not check runtime info against " + to_string(mat.mType)
81                     + " compatibility matrix.";
82         }
83         return false;
84     }
85     if (kernelSepolicyVersion() < mat.framework.mSepolicy.kernelSepolicyVersion()) {
86         if (error != nullptr) {
87             *error =
88                 "kernelSepolicyVersion = " + to_string(kernelSepolicyVersion()) +
89                 " but required >= " + to_string(mat.framework.mSepolicy.kernelSepolicyVersion());
90         }
91         return false;
92     }
93 
94     // mat.mSepolicy.sepolicyVersion() is checked against static
95     // HalManifest.device.mSepolicyVersion in HalManifest::checkCompatibility.
96 
97     if (flags.isKernelEnabled()) {
98         if (!isMainlineKernel() &&
99             mKernel.getMatchedKernelRequirements(mat.framework.mKernels, kernelLevel(), error)
100                 .empty()) {
101             return false;
102         }
103     }
104 
105     if (flags.isAvbEnabled()) {
106         const Version& matAvb = mat.framework.mAvbMetaVersion;
107         if (mBootAvbVersion.majorVer != matAvb.majorVer ||
108             mBootAvbVersion.minorVer < matAvb.minorVer) {
109             if (error != nullptr) {
110                 std::stringstream ss;
111                 ss << "AVB version " << mBootAvbVersion << " does not match framework matrix "
112                    << matAvb;
113                 *error = ss.str();
114             }
115             return false;
116         }
117         if (mBootVbmetaAvbVersion.majorVer != matAvb.majorVer ||
118             mBootVbmetaAvbVersion.minorVer < matAvb.minorVer) {
119             if (error != nullptr) {
120                 std::stringstream ss;
121                 ss << "Vbmeta version " << mBootVbmetaAvbVersion
122                    << " does not match framework matrix " << matAvb;
123                 *error = ss.str();
124             }
125             return false;
126         }
127     }
128 
129     return true;
130 }
131 
setKernelLevel(Level level)132 void RuntimeInfo::setKernelLevel(Level level) {
133     mKernel.mLevel = level;
134 }
135 
kernelLevel() const136 Level RuntimeInfo::kernelLevel() const {
137     return mKernel.mLevel;
138 }
139 
140 } // namespace vintf
141 } // namespace android
142