blob: b74f6e9490977b9bff54bc0c2ca8198349566e28 [file] [log] [blame]
Patrick Williams6a85e8a2021-08-27 08:22:29 -05001#include "config.h"
2
3#include "msl_verify.hpp"
4
5#include "version.hpp"
6
Patrick Williamsc9bb6422021-08-27 06:18:35 -05007#include <phosphor-logging/lg2.hpp>
8#include <xyz/openbmc_project/Software/Version/server.hpp>
Patrick Williams6a85e8a2021-08-27 08:22:29 -05009
10#include <regex>
11
Patrick Williamsc9bb6422021-08-27 06:18:35 -050012PHOSPHOR_LOG2_USING;
Patrick Williams6a85e8a2021-08-27 08:22:29 -050013
14int minimum_ship_level::compare(const Version& versionToCompare,
15 const Version& mslVersion)
16{
17 if (versionToCompare.major > mslVersion.major)
18 return (1);
19 if (versionToCompare.major < mslVersion.major)
20 return (-1);
21
22 if (versionToCompare.minor > mslVersion.minor)
23 return (1);
24 if (versionToCompare.minor < mslVersion.minor)
25 return (-1);
26
27 if (versionToCompare.rev > mslVersion.rev)
28 return (1);
29 if (versionToCompare.rev < mslVersion.rev)
30 return (-1);
31
32 // Both string are equal and there is no need to make an upgrade return 0.
33 return 0;
34}
35
36// parse Function copy inpVersion onto outVersion in Version format
37// {major,minor,rev}.
38void minimum_ship_level::parse(const std::string& inpVersion,
39 Version& outVersion)
40{
41 std::smatch match;
42 outVersion = {0, 0, 0};
43
44 std::regex rx{REGEX_BMC_MSL, std::regex::extended};
45
46 if (!std::regex_search(inpVersion, match, rx))
47 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -050048 error("Unable to parse BMC version: {VERSION}", "VERSION", inpVersion);
Patrick Williams6a85e8a2021-08-27 08:22:29 -050049 return;
50 }
51
52 outVersion.major = std::stoi(match[2]);
53 outVersion.minor = std::stoi(match[3]);
54 outVersion.rev = std::stoi(match[4]);
55}
56
57bool minimum_ship_level::verify(const std::string& versionManifest)
58{
59
60 // If there is no msl or mslRegex return upgrade is needed.
61 std::string msl{BMC_MSL};
62 std::string mslRegex{REGEX_BMC_MSL};
63 if (msl.empty() || mslRegex.empty())
64 {
65 return true;
66 }
67
68 // Define mslVersion variable and populate in Version format
69 // {major,minor,rev} using parse function.
70
71 Version mslVersion = {0, 0, 0};
72 parse(msl, mslVersion);
73
74 // Define actualVersion variable and populate in Version format
75 // {major,minor,rev} using parse function.
76 std::string tmpStr{};
77
78 tmpStr = versionManifest;
79 Version actualVersion = {0, 0, 0};
80 parse(versionManifest, actualVersion);
81
82 // Compare actualVersion vs MSL.
83 auto rc = compare(actualVersion, mslVersion);
84 if (rc < 0)
85 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -050086 auto purpose = sdbusplus::xyz::openbmc_project::Software::server::
87 Version::VersionPurpose::BMC;
88 error(
89 "BMC Minimum Ship Level ({MIN_VERSION}) NOT met by {ACTUAL_VERSION}",
90 "MIN_VERSION", msl, "ACTUAL_VERSION", tmpStr, "VERSION_PURPOSE",
91 purpose);
Patrick Williams6a85e8a2021-08-27 08:22:29 -050092 return false;
93 }
94
95 return true;
96}