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