Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 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> |
Yifan Hong | 2350269 | 2020-04-06 18:46:20 -0700 | [diff] [blame] | 20 | #include <android-base/strings.h> |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 21 | |
| 22 | #include "RuntimeInfo.h" |
| 23 | |
| 24 | #include "CompatibilityMatrix.h" |
Yifan Hong | e4959a1 | 2017-07-05 16:05:09 -0700 | [diff] [blame] | 25 | #include "KernelConfigParser.h" |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 26 | #include "parse_string.h" |
| 27 | |
Yifan Hong | 5075f45 | 2017-04-20 14:00:24 -0700 | [diff] [blame] | 28 | #include <dirent.h> |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 29 | #include <errno.h> |
| 30 | #include <sys/utsname.h> |
| 31 | #include <unistd.h> |
| 32 | |
Yifan Hong | 242eabf | 2017-04-20 14:06:26 -0700 | [diff] [blame] | 33 | #include <fstream> |
| 34 | #include <iostream> |
| 35 | #include <sstream> |
| 36 | |
Yifan Hong | e621fb1 | 2018-02-28 14:41:38 -0800 | [diff] [blame] | 37 | #include <android-base/properties.h> |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 38 | #include <selinux/selinux.h> |
| 39 | #include <zlib.h> |
| 40 | |
| 41 | #define PROC_CONFIG "/proc/config.gz" |
| 42 | #define BUFFER_SIZE sysconf(_SC_PAGESIZE) |
| 43 | |
Yifan Hong | 2350269 | 2020-04-06 18:46:20 -0700 | [diff] [blame] | 44 | static constexpr char kMainline[] = "-mainline-"; |
Yifan Hong | e769d1b | 2021-10-01 15:26:40 -0700 | [diff] [blame] | 45 | static constexpr char kMainlineSuffix[] = "-mainline"; |
Yifan Hong | 2350269 | 2020-04-06 18:46:20 -0700 | [diff] [blame] | 46 | |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 47 | namespace android { |
| 48 | namespace vintf { |
| 49 | |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 50 | struct RuntimeInfoFetcher { |
| 51 | RuntimeInfoFetcher(RuntimeInfo *ki) : mRuntimeInfo(ki) { } |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 52 | status_t fetchAllInformation(RuntimeInfo::FetchFlags flags); |
| 53 | |
| 54 | private: |
Yifan Hong | 5a5dbef | 2020-11-10 17:31:47 -0800 | [diff] [blame] | 55 | status_t fetchVersion(RuntimeInfo::FetchFlags flags); |
| 56 | status_t fetchKernelConfigs(RuntimeInfo::FetchFlags flags); |
| 57 | status_t fetchCpuInfo(RuntimeInfo::FetchFlags flags); |
| 58 | status_t fetchKernelSepolicyVers(RuntimeInfo::FetchFlags flags); |
| 59 | status_t fetchAvb(RuntimeInfo::FetchFlags flags); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 60 | status_t parseKernelVersion(); |
| 61 | RuntimeInfo *mRuntimeInfo; |
Yifan Hong | e4959a1 | 2017-07-05 16:05:09 -0700 | [diff] [blame] | 62 | KernelConfigParser mConfigParser; |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | // decompress /proc/config.gz and read its contents. |
Yifan Hong | 5a5dbef | 2020-11-10 17:31:47 -0800 | [diff] [blame] | 66 | status_t RuntimeInfoFetcher::fetchKernelConfigs(RuntimeInfo::FetchFlags) { |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 67 | gzFile f = gzopen(PROC_CONFIG, "rb"); |
| 68 | if (f == NULL) { |
| 69 | LOG(ERROR) << "Could not open /proc/config.gz: " << errno; |
| 70 | return -errno; |
| 71 | } |
| 72 | |
| 73 | char buf[BUFFER_SIZE]; |
| 74 | int len; |
| 75 | while ((len = gzread(f, buf, sizeof buf)) > 0) { |
Yifan Hong | e4959a1 | 2017-07-05 16:05:09 -0700 | [diff] [blame] | 76 | mConfigParser.process(buf, len); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 77 | } |
| 78 | status_t err = OK; |
| 79 | if (len < 0) { |
| 80 | int errnum; |
| 81 | const char *errmsg = gzerror(f, &errnum); |
| 82 | LOG(ERROR) << "Could not read /proc/config.gz: " << errmsg; |
| 83 | err = (errnum == Z_ERRNO ? -errno : errnum); |
| 84 | } |
Yifan Hong | e4959a1 | 2017-07-05 16:05:09 -0700 | [diff] [blame] | 85 | mConfigParser.finish(); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 86 | gzclose(f); |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 87 | mRuntimeInfo->mKernel.mConfigs = std::move(mConfigParser.configs()); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 88 | return err; |
| 89 | } |
| 90 | |
Yifan Hong | 5a5dbef | 2020-11-10 17:31:47 -0800 | [diff] [blame] | 91 | status_t RuntimeInfoFetcher::fetchCpuInfo(RuntimeInfo::FetchFlags) { |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 92 | // TODO implement this; 32-bit and 64-bit has different format. |
Yifan Hong | 242eabf | 2017-04-20 14:06:26 -0700 | [diff] [blame] | 93 | std::ifstream in{"/proc/cpuinfo"}; |
| 94 | if (!in.is_open()) { |
| 95 | LOG(WARNING) << "Cannot read /proc/cpuinfo"; |
| 96 | return UNKNOWN_ERROR; |
| 97 | } |
| 98 | std::stringstream sstream; |
| 99 | sstream << in.rdbuf(); |
| 100 | mRuntimeInfo->mCpuInfo = sstream.str(); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 101 | return OK; |
| 102 | } |
| 103 | |
Yifan Hong | 5a5dbef | 2020-11-10 17:31:47 -0800 | [diff] [blame] | 104 | status_t RuntimeInfoFetcher::fetchKernelSepolicyVers(RuntimeInfo::FetchFlags) { |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 105 | int pv; |
| 106 | #ifdef LIBVINTF_TARGET |
| 107 | pv = security_policyvers(); |
| 108 | #else |
| 109 | pv = 0; |
| 110 | #endif |
| 111 | if (pv < 0) { |
| 112 | return pv; |
| 113 | } |
| 114 | mRuntimeInfo->mKernelSepolicyVersion = pv; |
| 115 | return OK; |
| 116 | } |
| 117 | |
Yifan Hong | e3c3d53 | 2020-11-10 17:34:24 -0800 | [diff] [blame] | 118 | status_t RuntimeInfoFetcher::fetchVersion(RuntimeInfo::FetchFlags flags) { |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 119 | struct utsname buf; |
| 120 | if (uname(&buf)) { |
| 121 | return -errno; |
| 122 | } |
| 123 | mRuntimeInfo->mOsName = buf.sysname; |
| 124 | mRuntimeInfo->mNodeName = buf.nodename; |
| 125 | mRuntimeInfo->mOsRelease = buf.release; |
| 126 | mRuntimeInfo->mOsVersion = buf.version; |
| 127 | mRuntimeInfo->mHardwareId = buf.machine; |
| 128 | |
Yifan Hong | e769d1b | 2021-10-01 15:26:40 -0700 | [diff] [blame] | 129 | mRuntimeInfo->mIsMainline = mRuntimeInfo->mOsRelease.find(kMainline) != std::string::npos || |
| 130 | android::base::EndsWith(mRuntimeInfo->mOsRelease, kMainlineSuffix); |
Yifan Hong | 5a5dbef | 2020-11-10 17:31:47 -0800 | [diff] [blame] | 131 | |
Yifan Hong | e3c3d53 | 2020-11-10 17:34:24 -0800 | [diff] [blame] | 132 | status_t err = RuntimeInfo::parseGkiKernelRelease(flags, mRuntimeInfo->mOsRelease, |
| 133 | &mRuntimeInfo->mKernel.mVersion, |
| 134 | &mRuntimeInfo->mKernel.mLevel); |
| 135 | if (err == OK) return OK; |
| 136 | |
| 137 | err = parseKernelVersion(); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 138 | if (err != OK) { |
| 139 | LOG(ERROR) << "Could not parse kernel version from \"" |
| 140 | << mRuntimeInfo->mOsRelease << "\""; |
| 141 | } |
| 142 | return err; |
| 143 | } |
| 144 | |
| 145 | status_t RuntimeInfoFetcher::parseKernelVersion() { |
| 146 | auto pos = mRuntimeInfo->mOsRelease.find('.'); |
| 147 | if (pos == std::string::npos) { |
| 148 | return UNKNOWN_ERROR; |
| 149 | } |
| 150 | pos = mRuntimeInfo->mOsRelease.find('.', pos + 1); |
| 151 | if (pos == std::string::npos) { |
| 152 | return UNKNOWN_ERROR; |
| 153 | } |
| 154 | pos = mRuntimeInfo->mOsRelease.find_first_not_of("0123456789", pos + 1); |
| 155 | // no need to check pos == std::string::npos, because substr will handle this |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 156 | if (!parse(mRuntimeInfo->mOsRelease.substr(0, pos), &mRuntimeInfo->mKernel.mVersion)) { |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 157 | return UNKNOWN_ERROR; |
| 158 | } |
| 159 | return OK; |
| 160 | } |
| 161 | |
Yifan Hong | 5a5dbef | 2020-11-10 17:31:47 -0800 | [diff] [blame] | 162 | status_t RuntimeInfoFetcher::fetchAvb(RuntimeInfo::FetchFlags) { |
Yifan Hong | e621fb1 | 2018-02-28 14:41:38 -0800 | [diff] [blame] | 163 | std::string prop = android::base::GetProperty("ro.boot.vbmeta.avb_version", "0.0"); |
Yifan Hong | 881a9e45 | 2017-04-27 19:31:13 -0700 | [diff] [blame] | 164 | if (!parse(prop, &mRuntimeInfo->mBootVbmetaAvbVersion)) { |
Yifan Hong | f302930 | 2017-04-12 17:23:49 -0700 | [diff] [blame] | 165 | return UNKNOWN_ERROR; |
| 166 | } |
Yifan Hong | e621fb1 | 2018-02-28 14:41:38 -0800 | [diff] [blame] | 167 | prop = android::base::GetProperty("ro.boot.avb_version", "0.0"); |
Yifan Hong | 881a9e45 | 2017-04-27 19:31:13 -0700 | [diff] [blame] | 168 | if (!parse(prop, &mRuntimeInfo->mBootAvbVersion)) { |
Yifan Hong | f302930 | 2017-04-12 17:23:49 -0700 | [diff] [blame] | 169 | return UNKNOWN_ERROR; |
| 170 | } |
| 171 | return OK; |
| 172 | } |
| 173 | |
Yifan Hong | 5a5dbef | 2020-11-10 17:31:47 -0800 | [diff] [blame] | 174 | struct FetchFunction { |
| 175 | RuntimeInfo::FetchFlags flags; |
| 176 | std::function<status_t(RuntimeInfoFetcher*, RuntimeInfo::FetchFlags)> fetch; |
| 177 | std::string description; |
| 178 | }; |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 179 | |
Yifan Hong | 5a5dbef | 2020-11-10 17:31:47 -0800 | [diff] [blame] | 180 | status_t RuntimeInfoFetcher::fetchAllInformation(RuntimeInfo::FetchFlags flags) { |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 181 | using F = RuntimeInfo::FetchFlag; |
| 182 | using RF = RuntimeInfoFetcher; |
Yifan Hong | 5a5dbef | 2020-11-10 17:31:47 -0800 | [diff] [blame] | 183 | // clang-format off |
| 184 | const static std::vector<FetchFunction> gFetchFunctions({ |
Yifan Hong | e3c3d53 | 2020-11-10 17:34:24 -0800 | [diff] [blame] | 185 | {F::CPU_VERSION | F::KERNEL_FCM, &RF::fetchVersion, "/proc/version"}, |
Yifan Hong | 5a5dbef | 2020-11-10 17:31:47 -0800 | [diff] [blame] | 186 | {F::CONFIG_GZ, &RF::fetchKernelConfigs, "/proc/config.gz"}, |
| 187 | {F::CPU_INFO, &RF::fetchCpuInfo, "/proc/cpuinfo"}, |
| 188 | {F::POLICYVERS, &RF::fetchKernelSepolicyVers, "kernel sepolicy version"}, |
| 189 | {F::AVB, &RF::fetchAvb, "avb version"}, |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 190 | }); |
Yifan Hong | 5a5dbef | 2020-11-10 17:31:47 -0800 | [diff] [blame] | 191 | // clang-format on |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 192 | |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 193 | status_t err; |
Yifan Hong | 5a5dbef | 2020-11-10 17:31:47 -0800 | [diff] [blame] | 194 | for (const auto& fetchFunction : gFetchFunctions) |
Greg Kaiser | 8b7fabd | 2020-11-17 09:01:28 -0800 | [diff] [blame] | 195 | if ((flags & fetchFunction.flags) && ((err = fetchFunction.fetch(this, flags)) != OK)) |
Yifan Hong | 5a5dbef | 2020-11-10 17:31:47 -0800 | [diff] [blame] | 196 | LOG(WARNING) << "Cannot fetch or parse " << fetchFunction.description << ": " |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 197 | << strerror(-err); |
| 198 | |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 199 | return OK; |
| 200 | } |
| 201 | |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 202 | status_t RuntimeInfo::fetchAllInformation(RuntimeInfo::FetchFlags flags) { |
| 203 | return RuntimeInfoFetcher(this).fetchAllInformation(flags); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | } // namespace vintf |
| 207 | } // namespace android |