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