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