Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "msl_verify.hpp" |
| 4 | |
| 5 | #include "version.hpp" |
| 6 | |
zamiseck | 10ec975 | 2022-04-27 12:39:50 -0500 | [diff] [blame] | 7 | #include <phosphor-logging/elog-errors.hpp> |
| 8 | #include <phosphor-logging/elog.hpp> |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 9 | #include <phosphor-logging/lg2.hpp> |
zamiseck | 10ec975 | 2022-04-27 12:39:50 -0500 | [diff] [blame] | 10 | #include <xyz/openbmc_project/Software/Version/error.hpp> |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 11 | |
| 12 | #include <regex> |
| 13 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 14 | PHOSPHOR_LOG2_USING; |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 15 | |
| 16 | int minimum_ship_level::compare(const Version& versionToCompare, |
| 17 | const Version& mslVersion) |
| 18 | { |
| 19 | if (versionToCompare.major > mslVersion.major) |
Pavithra Barithaya | ce9a5c9 | 2024-06-25 02:24:07 -0500 | [diff] [blame] | 20 | { |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 21 | return (1); |
Pavithra Barithaya | ce9a5c9 | 2024-06-25 02:24:07 -0500 | [diff] [blame] | 22 | } |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 23 | if (versionToCompare.major < mslVersion.major) |
Pavithra Barithaya | ce9a5c9 | 2024-06-25 02:24:07 -0500 | [diff] [blame] | 24 | { |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 25 | return (-1); |
Pavithra Barithaya | ce9a5c9 | 2024-06-25 02:24:07 -0500 | [diff] [blame] | 26 | } |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 27 | |
| 28 | if (versionToCompare.minor > mslVersion.minor) |
Pavithra Barithaya | ce9a5c9 | 2024-06-25 02:24:07 -0500 | [diff] [blame] | 29 | { |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 30 | return (1); |
Pavithra Barithaya | ce9a5c9 | 2024-06-25 02:24:07 -0500 | [diff] [blame] | 31 | } |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 32 | if (versionToCompare.minor < mslVersion.minor) |
Pavithra Barithaya | ce9a5c9 | 2024-06-25 02:24:07 -0500 | [diff] [blame] | 33 | { |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 34 | return (-1); |
Pavithra Barithaya | ce9a5c9 | 2024-06-25 02:24:07 -0500 | [diff] [blame] | 35 | } |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 36 | |
| 37 | if (versionToCompare.rev > mslVersion.rev) |
Pavithra Barithaya | ce9a5c9 | 2024-06-25 02:24:07 -0500 | [diff] [blame] | 38 | { |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 39 | return (1); |
Pavithra Barithaya | ce9a5c9 | 2024-06-25 02:24:07 -0500 | [diff] [blame] | 40 | } |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 41 | if (versionToCompare.rev < mslVersion.rev) |
Pavithra Barithaya | ce9a5c9 | 2024-06-25 02:24:07 -0500 | [diff] [blame] | 42 | { |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 43 | return (-1); |
Pavithra Barithaya | ce9a5c9 | 2024-06-25 02:24:07 -0500 | [diff] [blame] | 44 | } |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 45 | |
| 46 | // Both string are equal and there is no need to make an upgrade return 0. |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | // parse Function copy inpVersion onto outVersion in Version format |
| 51 | // {major,minor,rev}. |
| 52 | void minimum_ship_level::parse(const std::string& inpVersion, |
| 53 | Version& outVersion) |
| 54 | { |
| 55 | std::smatch match; |
| 56 | outVersion = {0, 0, 0}; |
| 57 | |
| 58 | std::regex rx{REGEX_BMC_MSL, std::regex::extended}; |
| 59 | |
| 60 | if (!std::regex_search(inpVersion, match, rx)) |
| 61 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 62 | error("Unable to parse BMC version: {VERSION}", "VERSION", inpVersion); |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 63 | return; |
| 64 | } |
| 65 | |
| 66 | outVersion.major = std::stoi(match[2]); |
| 67 | outVersion.minor = std::stoi(match[3]); |
| 68 | outVersion.rev = std::stoi(match[4]); |
| 69 | } |
| 70 | |
| 71 | bool minimum_ship_level::verify(const std::string& versionManifest) |
| 72 | { |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 73 | // If there is no msl or mslRegex return upgrade is needed. |
Adriana Kobylak | 30352a6 | 2024-04-09 09:25:36 -0500 | [diff] [blame] | 74 | if (!enabled()) |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 75 | { |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | // Define mslVersion variable and populate in Version format |
| 80 | // {major,minor,rev} using parse function. |
| 81 | |
Adriana Kobylak | 30352a6 | 2024-04-09 09:25:36 -0500 | [diff] [blame] | 82 | std::string msl = getMinimumVersion(); |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 83 | Version mslVersion = {0, 0, 0}; |
| 84 | parse(msl, mslVersion); |
| 85 | |
| 86 | // Define actualVersion variable and populate in Version format |
| 87 | // {major,minor,rev} using parse function. |
| 88 | std::string tmpStr{}; |
| 89 | |
| 90 | tmpStr = versionManifest; |
| 91 | Version actualVersion = {0, 0, 0}; |
| 92 | parse(versionManifest, actualVersion); |
| 93 | |
| 94 | // Compare actualVersion vs MSL. |
| 95 | auto rc = compare(actualVersion, mslVersion); |
| 96 | if (rc < 0) |
| 97 | { |
zamiseck | 10ec975 | 2022-04-27 12:39:50 -0500 | [diff] [blame] | 98 | using namespace phosphor::logging; |
Adriana Kobylak | ce82de5 | 2024-01-16 13:56:38 -0600 | [diff] [blame] | 99 | using IncompatibleErr = sdbusplus::error::xyz::openbmc_project:: |
| 100 | software::version::Incompatible; |
zamiseck | 10ec975 | 2022-04-27 12:39:50 -0500 | [diff] [blame] | 101 | using Incompatible = |
Adriana Kobylak | ce82de5 | 2024-01-16 13:56:38 -0600 | [diff] [blame] | 102 | xyz::openbmc_project::software::version::Incompatible; |
zamiseck | 10ec975 | 2022-04-27 12:39:50 -0500 | [diff] [blame] | 103 | std::string purpose = |
| 104 | "xyz.openbmc_project.Software.Version.VersionPurpose.BMC"; |
| 105 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 106 | error( |
| 107 | "BMC Minimum Ship Level ({MIN_VERSION}) NOT met by {ACTUAL_VERSION}", |
| 108 | "MIN_VERSION", msl, "ACTUAL_VERSION", tmpStr, "VERSION_PURPOSE", |
| 109 | purpose); |
zamiseck | 10ec975 | 2022-04-27 12:39:50 -0500 | [diff] [blame] | 110 | report<IncompatibleErr>(Incompatible::MIN_VERSION(msl.c_str()), |
| 111 | Incompatible::ACTUAL_VERSION(tmpStr.c_str()), |
| 112 | Incompatible::VERSION_PURPOSE(purpose.c_str())); |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 113 | return false; |
| 114 | } |
| 115 | |
| 116 | return true; |
| 117 | } |
Adriana Kobylak | 30352a6 | 2024-04-09 09:25:36 -0500 | [diff] [blame] | 118 | |
| 119 | bool minimum_ship_level::enabled() |
| 120 | { |
| 121 | std::string msl = getMinimumVersion(); |
| 122 | std::string mslRegex{REGEX_BMC_MSL}; |
| 123 | if (!msl.empty() && !mslRegex.empty()) |
| 124 | { |
| 125 | return true; |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | std::string minimum_ship_level::getMinimumVersion() |
| 131 | { |
| 132 | return BMC_MSL; |
| 133 | } |