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 
101     // GKI kernel release string specifies the kernel level using a string like
102     // "android12". This function converts the trailing number of this string to
103     // a Level. For example, androidReleaseToLevel(12) -> Level::S.
104     // Abort if the value of |androidRelease| is higher than supported values
105     // specified in Level.
106     static Level gkiAndroidReleaseToLevel(uint64_t androidRelease);
107 
108    protected:
109     virtual status_t fetchAllInformation(FetchFlags flags);
110 
111     void setKernelLevel(Level level);
112     Level kernelLevel() const;
113 
114     // Helper function to parse kernel release string as a GKI kernel release string.
115     // Return error if:
116     // - it is not a GKI kernel release string
117     // - kernel level is not recognized by libvintf.
118     static status_t parseGkiKernelRelease(RuntimeInfo::FetchFlags flags,
119                                           const std::string& kernelReleaseString,
120                                           KernelVersion* version, Level* kernelLevel);
121 
122     friend struct RuntimeInfoFetcher;
123     friend class VintfObject;
124     friend struct LibVintfTest;
125     friend std::string dump(const RuntimeInfo& ki, bool);
126     friend class testing::VintfObjectRuntimeInfoTest;
127 
128     // /proc/config.gz
129     // Key: CONFIG_xxx; Value: the value after = sign.
130     KernelInfo mKernel;
131     std::string mOsName;
132     std::string mNodeName;
133     std::string mOsRelease;
134     std::string mOsVersion;
135     std::string mHardwareId;
136 
137     std::vector<std::string> mSepolicyFilePaths;
138     std::string mCpuInfo;
139     Version mBootVbmetaAvbVersion;
140     Version mBootAvbVersion;
141 
142     size_t mKernelSepolicyVersion = 0u;
143 
144     bool mIsMainline = false;
145 };
146 
147 } // namespace vintf
148 } // namespace android
149 
150 #endif // ANDROID_VINTF_RUNTIME_INFO_H
151