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 #ifndef ANDROID_VINTF_RUNTIME_INFO_H
18 #define ANDROID_VINTF_RUNTIME_INFO_H
19 
20 #include "Version.h"
21 
22 #include <map>
23 #include <string>
24 #include <vector>
25 
26 #include <utils/Errors.h>
27 
28 #include "CheckFlags.h"
29 #include "KernelInfo.h"
30 #include "MatrixKernel.h"
31 #include "Version.h"
32 
33 namespace android {
34 namespace vintf {
35 
36 namespace testing {
37 class VintfObjectRuntimeInfoTest;
38 }  // namespace testing
39 
40 struct CompatibilityMatrix;
41 
42 // Runtime Info sent to OTA server
43 struct RuntimeInfo {
44 
RuntimeInfoRuntimeInfo45     RuntimeInfo() {}
46     virtual ~RuntimeInfo() = default;
47 
48     // /proc/version
49     // utsname.sysname
50     const std::string &osName() const;
51     // utsname.nodename
52     const std::string &nodeName() const;
53     // utsname.release
54     const std::string &osRelease() const;
55     // utsname.version
56     const std::string &osVersion() const;
57     // utsname.machine
58     const std::string &hardwareId() const;
59     // extract from utsname.release
60     const KernelVersion &kernelVersion() const;
61 
62     const std::map<std::string, std::string> &kernelConfigs() const;
63 
64     const Version &bootVbmetaAvbVersion() const;
65     const Version &bootAvbVersion() const;
66 
67     // /proc/cpuinfo
68     const std::string &cpuInfo() const;
69 
70     // /sys/fs/selinux/policyvers
71     size_t kernelSepolicyVersion() const;
72 
73     bool isMainlineKernel() const;
74 
75     // Return whether this RuntimeInfo works with the given compatibility matrix. Return true if:
76     // - mat is a framework compat-mat
77     // - sepolicy.kernel-sepolicy-version == kernelSepolicyVersion()
78     // - /proc/config.gz matches the requirements. Note that /proc/config.gz is read when the
79     //   RuntimeInfo object is created (the first time VintfObject::GetRuntimeInfo is called),
80     //   not when RuntimeInfo::checkCompatibility is called.
81     // - avb-vbmetaversion matches related sysprops
82     bool checkCompatibility(const CompatibilityMatrix& mat, std::string* error = nullptr,
83                             CheckFlags::Type flags = CheckFlags::DEFAULT) const;
84 
85 
86     using FetchFlags = uint32_t;
87     enum FetchFlag : FetchFlags {
88         CPU_VERSION = 1 << 0,
89         CONFIG_GZ = 1 << 1,
90         CPU_INFO = 1 << 2,
91         POLICYVERS = 1 << 3,
92         AVB = 1 << 4,
93         KERNEL_FCM = 1 << 5,
94         LAST_PLUS_ONE,
95 
96         NONE = 0,
97         ALL = ((LAST_PLUS_ONE - 1) << 1) - 1,
98     };
99 
100    protected:
101     virtual status_t fetchAllInformation(FetchFlags flags);
102 
103     void setKernelLevel(Level level);
104     Level kernelLevel() const;
105 
106     friend struct RuntimeInfoFetcher;
107     friend class VintfObject;
108     friend struct LibVintfTest;
109     friend std::string dump(const RuntimeInfo& ki, bool);
110     friend class testing::VintfObjectRuntimeInfoTest;
111 
112     // /proc/config.gz
113     // Key: CONFIG_xxx; Value: the value after = sign.
114     KernelInfo mKernel;
115     std::string mOsName;
116     std::string mNodeName;
117     std::string mOsRelease;
118     std::string mOsVersion;
119     std::string mHardwareId;
120 
121     std::vector<std::string> mSepolicyFilePaths;
122     std::string mCpuInfo;
123     Version mBootVbmetaAvbVersion;
124     Version mBootAvbVersion;
125 
126     size_t mKernelSepolicyVersion = 0u;
127 
128     bool mIsMainline = false;
129 };
130 
131 } // namespace vintf
132 } // namespace android
133 
134 #endif // ANDROID_VINTF_RUNTIME_INFO_H
135