blob: cd12688623cf6cba064e38c3948bda959870ed55 [file] [log] [blame]
Saqib Khan35e83f32017-05-22 11:37:32 -05001#include <fstream>
Gunnar Millsec1b41c2017-05-02 12:20:36 -05002#include <string>
Gunnar Mills2ce7da22017-05-04 15:37:56 -05003#include <phosphor-logging/log.hpp>
Gunnar Millsec1b41c2017-05-02 12:20:36 -05004#include "config.h"
Gunnar Mills2ce7da22017-05-04 15:37:56 -05005#include "item_updater.hpp"
6#include "xyz/openbmc_project/Software/Version/server.hpp"
Saqib Khan35e83f32017-05-22 11:37:32 -05007#include <experimental/filesystem>
Saqib Khan705f1bf2017-06-09 23:58:38 -05008#include "version.hpp"
Gunnar Millsec1b41c2017-05-02 12:20:36 -05009
10namespace phosphor
11{
12namespace software
13{
14namespace updater
15{
16
Gunnar Mills2ce7da22017-05-04 15:37:56 -050017// When you see server:: you know we're referencing our base class
18namespace server = sdbusplus::xyz::openbmc_project::Software::server;
19
20using namespace phosphor::logging;
Saqib Khan35e83f32017-05-22 11:37:32 -050021namespace fs = std::experimental::filesystem;
22
23constexpr auto bmcImage = "image-rofs";
Gunnar Mills2ce7da22017-05-04 15:37:56 -050024
Patrick Williamse75d10f2017-05-30 16:56:32 -050025void ItemUpdater::createActivation(sdbusplus::message::message& msg)
Gunnar Millsec1b41c2017-05-02 12:20:36 -050026{
Gunnar Mills2ce7da22017-05-04 15:37:56 -050027 sdbusplus::message::object_path objPath;
Saqib Khan705f1bf2017-06-09 23:58:38 -050028 auto purpose = server::Version::VersionPurpose::Unknown;
29 std::string version;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050030 std::map<std::string,
Patrick Williamse75d10f2017-05-30 16:56:32 -050031 std::map<std::string,
32 sdbusplus::message::variant<std::string>>> interfaces;
33 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050034 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050035 std::string filePath;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050036
37 for (const auto& intf : interfaces)
38 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050039 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050040 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050041 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050042 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050043 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050044 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050045 std::string str = sdbusplus::message::variant_ns::
46 get<std::string>(property.second);
47 purpose = server::Version::
48 convertVersionPurposeFromString(str);
49 }
50 else if (property.first == "Version")
51 {
52 version = sdbusplus::message::variant_ns::
53 get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050054 }
55 }
56 }
Saqib Khan19177d32017-06-20 08:11:49 -050057 else if (intf.first == FILEPATH_IFACE)
58 {
59 for (const auto& property : intf.second)
60 {
61 if (property.first == "Path")
62 {
63 filePath = sdbusplus::message::variant_ns::get<
64 std::string>(property.second);
65 }
66 }
67 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050068 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050069 if (version.empty() ||
Saqib Khan19177d32017-06-20 08:11:49 -050070 filePath.empty() ||
Saqib Khan705f1bf2017-06-09 23:58:38 -050071 (purpose != server::Version::VersionPurpose::BMC &&
72 purpose != server::Version::VersionPurpose::System))
73 {
74 return;
75 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050076
77 // Version id is the last item in the path
78 auto pos = path.rfind("/");
79 if (pos == std::string::npos)
80 {
81 log<level::ERR>("No version id found in object path",
82 entry("OBJPATH=%s", path));
Patrick Williamse75d10f2017-05-30 16:56:32 -050083 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050084 }
85
86 auto versionId = path.substr(pos + 1);
87
Patrick Williamse75d10f2017-05-30 16:56:32 -050088 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -050089 {
Saqib Khan35e83f32017-05-22 11:37:32 -050090 // Determine the Activation state by processing the given image dir.
91 auto activationState = server::Activation::Activations::Invalid;
92 ItemUpdater::ActivationStatus result = ItemUpdater::
Saqib Khan19177d32017-06-20 08:11:49 -050093 validateSquashFSImage(filePath);
Saqib Khan35e83f32017-05-22 11:37:32 -050094 if (result == ItemUpdater::ActivationStatus::ready)
95 {
96 activationState = server::Activation::Activations::Ready;
97 }
Saqib Khan35e83f32017-05-22 11:37:32 -050098 activations.insert(std::make_pair(
Saqib Khan705f1bf2017-06-09 23:58:38 -050099 versionId,
100 std::make_unique<Activation>(
101 bus,
102 path,
Saqib Khan35e83f32017-05-22 11:37:32 -0500103 versionId,
Saqib Khan705f1bf2017-06-09 23:58:38 -0500104 activationState)));
105 versions.insert(std::make_pair(
106 versionId,
107 std::make_unique<phosphor::software::
108 manager::Version>(
109 bus,
110 path,
111 version,
112 purpose,
Saqib Khan19177d32017-06-20 08:11:49 -0500113 filePath)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500114 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500115 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500116}
117
Saqib Khanba239882017-05-26 08:41:54 -0500118void ItemUpdater::processBMCImage()
119{
120 auto purpose = server::Version::VersionPurpose::BMC;
121 auto version = phosphor::software::manager::Version::getBMCVersion();
122 auto id = phosphor::software::manager::Version::getId(version);
123 auto path = std::string{SOFTWARE_OBJPATH} + '/' + id;
124 activations.insert(std::make_pair(
125 id,
126 std::make_unique<Activation>(
127 bus,
128 path,
129 id,
130 server::Activation::Activations::Active)));
131 versions.insert(std::make_pair(
132 id,
133 std::make_unique<phosphor::software::
134 manager::Version>(
135 bus,
136 path,
137 version,
138 purpose,
139 "")));
140 return;
141}
142
Saqib Khan35e83f32017-05-22 11:37:32 -0500143ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
Saqib Khan19177d32017-06-20 08:11:49 -0500144 const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500145{
146
Saqib Khan19177d32017-06-20 08:11:49 -0500147 fs::path file(filePath);
Saqib Khan35e83f32017-05-22 11:37:32 -0500148 file /= bmcImage;
149 std::ifstream efile(file.c_str());
150
151 if (efile.good() == 1)
152 {
153 return ItemUpdater::ActivationStatus::ready;
154 }
155 else
156 {
Saqib Khan19177d32017-06-20 08:11:49 -0500157 log<level::ERR>("Failed to find the BMC image.");
Saqib Khan35e83f32017-05-22 11:37:32 -0500158 return ItemUpdater::ActivationStatus::invalid;
159 }
160}
161
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500162} // namespace updater
163} // namespace software
164} // namespace phosphor