blob: 3cd2278bd9f4f8eda51ae976e473eb4020c65afd [file] [log] [blame]
Miguel Gomez21dad042020-06-26 20:54:48 +00001#include "config.h"
2
3#include "msl_verify.hpp"
4
5#include "version.hpp"
6
7#include <phosphor-logging/log.hpp>
8
9#include <regex>
10
11using namespace phosphor::logging;
12
13int minimum_ship_level::compare(const Version& versionToCompare,
14 const Version& mslVersion)
15{
16 if (versionToCompare.major > mslVersion.major)
17 return (1);
18 if (versionToCompare.major < mslVersion.major)
19 return (-1);
20
21 if (versionToCompare.minor > mslVersion.minor)
22 return (1);
23 if (versionToCompare.minor < mslVersion.minor)
24 return (-1);
25
26 if (versionToCompare.rev > mslVersion.rev)
27 return (1);
28 if (versionToCompare.rev < mslVersion.rev)
29 return (-1);
30
31 // Both string are equal and there is no need to make an upgrade return 0.
32 return 0;
33}
34
35// parse Function copy inpVersion onto outVersion in Version format
36// {major,minor,rev}.
37void minimum_ship_level::parse(const std::string& inpVersion,
38 Version& outVersion)
39{
40 std::smatch match;
41 outVersion = {0, 0, 0};
42
43 std::regex rx{REGEX_BMC_MSL, std::regex::extended};
44
45 if (!std::regex_search(inpVersion, match, rx))
46 {
47 log<level::ERR>("Unable to parse BMC version",
48 entry("VERSION=%s", inpVersion.c_str()));
49 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 {
86 log<level::ERR>(
87 "BMC Minimum Ship Level NOT met",
88 entry("MIN_VERSION=%s", msl.c_str()),
89 entry("ACTUAL_VERSION=%s", tmpStr.c_str()),
90 entry("VERSION_PURPOSE=%s",
91 "xyz.openbmc_project.Software.Version.VersionPurpose.BMC"));
92 return false;
93 }
94
95 return true;
96}