xref: /aosp_15_r20/system/libvintf/RuntimeInfo-target.cpp (revision 70a7ec852fcefd15a4fb57f8f183a8b1c3aacb08)
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 #include <android-base/logging.h>
20 
21 #include "RuntimeInfo.h"
22 
23 #include "CompatibilityMatrix.h"
24 #include "parse_string.h"
25 
26 #include <dirent.h>
27 #include <errno.h>
28 #include <sys/utsname.h>
29 #include <unistd.h>
30 
31 #include <fstream>
32 #include <iostream>
33 #include <sstream>
34 
35 #include <android-base/properties.h>
36 #include <selinux/selinux.h>
37 
38 #include "KernelConfigs.h"
39 
40 namespace android {
41 namespace vintf {
42 
43 struct RuntimeInfoFetcher {
RuntimeInfoFetcherandroid::vintf::RuntimeInfoFetcher44     RuntimeInfoFetcher(RuntimeInfo *ki) : mRuntimeInfo(ki) { }
45     status_t fetchAllInformation(RuntimeInfo::FetchFlags flags);
46 
47    private:
48     status_t fetchVersion(RuntimeInfo::FetchFlags flags);
49     status_t fetchKernelConfigs(RuntimeInfo::FetchFlags flags);
50     status_t fetchCpuInfo(RuntimeInfo::FetchFlags flags);
51     status_t fetchKernelSepolicyVers(RuntimeInfo::FetchFlags flags);
52     status_t fetchAvb(RuntimeInfo::FetchFlags flags);
53     status_t parseKernelVersion();
54     RuntimeInfo* mRuntimeInfo;
55 };
56 
57 // decompress /proc/config.gz and read its contents.
fetchKernelConfigs(RuntimeInfo::FetchFlags)58 status_t RuntimeInfoFetcher::fetchKernelConfigs(RuntimeInfo::FetchFlags) {
59     return kernelconfigs::LoadKernelConfigs(&mRuntimeInfo->mKernel.mConfigs);
60 }
61 
fetchCpuInfo(RuntimeInfo::FetchFlags)62 status_t RuntimeInfoFetcher::fetchCpuInfo(RuntimeInfo::FetchFlags) {
63     // TODO implement this; 32-bit and 64-bit has different format.
64     std::ifstream in{"/proc/cpuinfo"};
65     if (!in.is_open()) {
66         LOG(WARNING) << "Cannot read /proc/cpuinfo";
67         return UNKNOWN_ERROR;
68     }
69     std::stringstream sstream;
70     sstream << in.rdbuf();
71     mRuntimeInfo->mCpuInfo = sstream.str();
72     return OK;
73 }
74 
fetchKernelSepolicyVers(RuntimeInfo::FetchFlags)75 status_t RuntimeInfoFetcher::fetchKernelSepolicyVers(RuntimeInfo::FetchFlags) {
76     int pv;
77 #ifdef LIBVINTF_TARGET
78     pv = security_policyvers();
79 #else
80     pv = 0;
81 #endif
82     if (pv < 0) {
83         return pv;
84     }
85     mRuntimeInfo->mKernelSepolicyVersion = pv;
86     return OK;
87 }
88 
fetchVersion(RuntimeInfo::FetchFlags flags)89 status_t RuntimeInfoFetcher::fetchVersion(RuntimeInfo::FetchFlags flags) {
90     struct utsname buf;
91     if (uname(&buf)) {
92         return -errno;
93     }
94     mRuntimeInfo->mOsName = buf.sysname;
95     mRuntimeInfo->mNodeName = buf.nodename;
96     mRuntimeInfo->mOsRelease = buf.release;
97     mRuntimeInfo->mOsVersion = buf.version;
98     mRuntimeInfo->mHardwareId = buf.machine;
99 
100     mRuntimeInfo->mIsMainline = RuntimeInfo::kernelReleaseIsMainline(mRuntimeInfo->mOsRelease);
101 
102     status_t err = RuntimeInfo::parseGkiKernelRelease(flags, mRuntimeInfo->mOsRelease,
103                                                       &mRuntimeInfo->mKernel.mVersion,
104                                                       &mRuntimeInfo->mKernel.mLevel);
105     if (err == OK) return OK;
106 
107     err = parseKernelVersion();
108     if (err != OK) {
109         LOG(ERROR) << "Could not parse kernel version from \""
110                    << mRuntimeInfo->mOsRelease << "\"";
111     }
112     return err;
113 }
114 
parseKernelVersion()115 status_t RuntimeInfoFetcher::parseKernelVersion() {
116     auto pos = mRuntimeInfo->mOsRelease.find('.');
117     if (pos == std::string::npos) {
118         return UNKNOWN_ERROR;
119     }
120     pos = mRuntimeInfo->mOsRelease.find('.', pos + 1);
121     if (pos == std::string::npos) {
122         return UNKNOWN_ERROR;
123     }
124     pos = mRuntimeInfo->mOsRelease.find_first_not_of("0123456789", pos + 1);
125     // no need to check pos == std::string::npos, because substr will handle this
126     if (!parse(mRuntimeInfo->mOsRelease.substr(0, pos), &mRuntimeInfo->mKernel.mVersion)) {
127         return UNKNOWN_ERROR;
128     }
129     return OK;
130 }
131 
fetchAvb(RuntimeInfo::FetchFlags)132 status_t RuntimeInfoFetcher::fetchAvb(RuntimeInfo::FetchFlags) {
133     std::string prop = android::base::GetProperty("ro.boot.vbmeta.avb_version", "0.0");
134     if (!parse(prop, &mRuntimeInfo->mBootVbmetaAvbVersion)) {
135         return UNKNOWN_ERROR;
136     }
137     prop = android::base::GetProperty("ro.boot.avb_version", "0.0");
138     if (!parse(prop, &mRuntimeInfo->mBootAvbVersion)) {
139         return UNKNOWN_ERROR;
140     }
141     return OK;
142 }
143 
144 struct FetchFunction {
145     RuntimeInfo::FetchFlags flags;
146     std::function<status_t(RuntimeInfoFetcher*, RuntimeInfo::FetchFlags)> fetch;
147     std::string description;
148 };
149 
fetchAllInformation(RuntimeInfo::FetchFlags flags)150 status_t RuntimeInfoFetcher::fetchAllInformation(RuntimeInfo::FetchFlags flags) {
151     using F = RuntimeInfo::FetchFlag;
152     using RF = RuntimeInfoFetcher;
153     // clang-format off
154     const static std::vector<FetchFunction> gFetchFunctions({
155         {F::CPU_VERSION | F::KERNEL_FCM, &RF::fetchVersion,            "/proc/version"},
156         {F::CONFIG_GZ,                   &RF::fetchKernelConfigs,      "/proc/config.gz"},
157         {F::CPU_INFO,                    &RF::fetchCpuInfo,            "/proc/cpuinfo"},
158         {F::POLICYVERS,                  &RF::fetchKernelSepolicyVers, "kernel sepolicy version"},
159         {F::AVB,                         &RF::fetchAvb,                "avb version"},
160     });
161     // clang-format on
162 
163     status_t err;
164     for (const auto& fetchFunction : gFetchFunctions)
165         if ((flags & fetchFunction.flags) && ((err = fetchFunction.fetch(this, flags)) != OK))
166             LOG(WARNING) << "Cannot fetch or parse " << fetchFunction.description << ": "
167                          << strerror(-err);
168 
169     return OK;
170 }
171 
fetchAllInformation(RuntimeInfo::FetchFlags flags)172 status_t RuntimeInfo::fetchAllInformation(RuntimeInfo::FetchFlags flags) {
173     return RuntimeInfoFetcher(this).fetchAllInformation(flags);
174 }
175 
176 } // namespace vintf
177 } // namespace android
178