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) |
| 20 | return (1); |
| 21 | if (versionToCompare.major < mslVersion.major) |
| 22 | return (-1); |
| 23 | |
| 24 | if (versionToCompare.minor > mslVersion.minor) |
| 25 | return (1); |
| 26 | if (versionToCompare.minor < mslVersion.minor) |
| 27 | return (-1); |
| 28 | |
| 29 | if (versionToCompare.rev > mslVersion.rev) |
| 30 | return (1); |
| 31 | if (versionToCompare.rev < mslVersion.rev) |
| 32 | return (-1); |
| 33 | |
| 34 | // Both string are equal and there is no need to make an upgrade return 0. |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | // parse Function copy inpVersion onto outVersion in Version format |
| 39 | // {major,minor,rev}. |
| 40 | void minimum_ship_level::parse(const std::string& inpVersion, |
| 41 | Version& outVersion) |
| 42 | { |
| 43 | std::smatch match; |
| 44 | outVersion = {0, 0, 0}; |
| 45 | |
| 46 | std::regex rx{REGEX_BMC_MSL, std::regex::extended}; |
| 47 | |
| 48 | if (!std::regex_search(inpVersion, match, rx)) |
| 49 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 50 | error("Unable to parse BMC version: {VERSION}", "VERSION", inpVersion); |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 51 | return; |
| 52 | } |
| 53 | |
| 54 | outVersion.major = std::stoi(match[2]); |
| 55 | outVersion.minor = std::stoi(match[3]); |
| 56 | outVersion.rev = std::stoi(match[4]); |
| 57 | } |
| 58 | |
| 59 | bool minimum_ship_level::verify(const std::string& versionManifest) |
| 60 | { |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 61 | // If there is no msl or mslRegex return upgrade is needed. |
| 62 | std::string msl{BMC_MSL}; |
| 63 | std::string mslRegex{REGEX_BMC_MSL}; |
| 64 | if (msl.empty() || mslRegex.empty()) |
| 65 | { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | // Define mslVersion variable and populate in Version format |
| 70 | // {major,minor,rev} using parse function. |
| 71 | |
| 72 | Version mslVersion = {0, 0, 0}; |
| 73 | parse(msl, mslVersion); |
| 74 | |
| 75 | // Define actualVersion variable and populate in Version format |
| 76 | // {major,minor,rev} using parse function. |
| 77 | std::string tmpStr{}; |
| 78 | |
| 79 | tmpStr = versionManifest; |
| 80 | Version actualVersion = {0, 0, 0}; |
| 81 | parse(versionManifest, actualVersion); |
| 82 | |
| 83 | // Compare actualVersion vs MSL. |
| 84 | auto rc = compare(actualVersion, mslVersion); |
| 85 | if (rc < 0) |
| 86 | { |
zamiseck | 10ec975 | 2022-04-27 12:39:50 -0500 | [diff] [blame] | 87 | using namespace phosphor::logging; |
| 88 | using IncompatibleErr = sdbusplus::xyz::openbmc_project::Software:: |
| 89 | Version::Error::Incompatible; |
| 90 | using Incompatible = |
| 91 | xyz::openbmc_project::Software::Version::Incompatible; |
| 92 | std::string purpose = |
| 93 | "xyz.openbmc_project.Software.Version.VersionPurpose.BMC"; |
| 94 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 95 | error( |
| 96 | "BMC Minimum Ship Level ({MIN_VERSION}) NOT met by {ACTUAL_VERSION}", |
| 97 | "MIN_VERSION", msl, "ACTUAL_VERSION", tmpStr, "VERSION_PURPOSE", |
| 98 | purpose); |
zamiseck | 10ec975 | 2022-04-27 12:39:50 -0500 | [diff] [blame] | 99 | report<IncompatibleErr>(Incompatible::MIN_VERSION(msl.c_str()), |
| 100 | Incompatible::ACTUAL_VERSION(tmpStr.c_str()), |
| 101 | Incompatible::VERSION_PURPOSE(purpose.c_str())); |
Patrick Williams | 6a85e8a | 2021-08-27 08:22:29 -0500 | [diff] [blame] | 102 | return false; |
| 103 | } |
| 104 | |
| 105 | return true; |
| 106 | } |