1 // 2 // Copyright (C) 2020 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 #pragma once 18 19 #include <stdint.h> 20 21 #include <optional> 22 #include <string> 23 24 #include <kver/kmi_version.h> 25 26 namespace android::kver { 27 28 // Kernel release is a unique identifier of a GKI binary. 29 // Example: 5.4.42-android12-0 30 class KernelRelease { 31 public: 32 // Parse a string like "5.4.42-android12-0" to a KernelRelease object. 33 // If allow_suffix is set, allow strings like "5.4.42-android12-0-something". 34 // Suffix is discarded and not stored in this object. 35 // Return nullopt if any error. 36 static std::optional<KernelRelease> Parse(const std::string& s, bool allow_suffix = false); 37 38 // Return string representation of the kernel release object, excluding the suffix. 39 // e.g. "5.4.42-android12-0". 40 // To get the KMI version string, see kmi_version().string(). 41 std::string string() const; 42 43 // Return the KMI version corresponding to this KernelRelease. 44 const KmiVersion& kmi_version() const { return kmi_version_; } 45 46 // Getters of each field. 47 uint64_t version() const { return kmi_version().version(); } 48 uint64_t patch_level() const { return kmi_version().patch_level(); } 49 uint64_t sub_level() const { return sub_level_; } 50 uint64_t android_release() const { return kmi_version().android_release(); } 51 uint64_t generation() const { return kmi_version().generation(); } 52 53 // Let a kernel release be w.x.y-androidz-k. 54 // Returns (w, x, y) 55 std::tuple<uint64_t, uint64_t, uint64_t> kernel_version_tuple() const; 56 57 private: 58 KernelRelease() = default; 59 KmiVersion kmi_version_; 60 uint64_t sub_level_ = 0; 61 }; 62 63 } // namespace android::kver 64