blob: f15141282ec33f02147ac7ca07467a54d0ea5d43 [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"
Saqib Khan5d532672017-08-09 10:44:50 -05009#include "serialize.hpp"
Gunnar Millsec1b41c2017-05-02 12:20:36 -050010
11namespace phosphor
12{
13namespace software
14{
15namespace updater
16{
17
Gunnar Mills2ce7da22017-05-04 15:37:56 -050018// When you see server:: you know we're referencing our base class
19namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Michael Tritz0129d922017-08-10 19:33:46 -050020namespace control = sdbusplus::xyz::openbmc_project::Control::server;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050021
22using namespace phosphor::logging;
Saqib Khan35e83f32017-05-22 11:37:32 -050023namespace fs = std::experimental::filesystem;
24
Michael Tritzb1cfdf92017-08-14 14:33:30 -050025const std::vector<std::string> bmcImages = {"image-kernel",
26 "image-rofs",
27 "image-rwfs",
28 "image-u-boot"};
Gunnar Mills2ce7da22017-05-04 15:37:56 -050029
Patrick Williamse75d10f2017-05-30 16:56:32 -050030void ItemUpdater::createActivation(sdbusplus::message::message& msg)
Gunnar Millsec1b41c2017-05-02 12:20:36 -050031{
Saqib Khan84a0e692017-06-28 17:27:01 -050032
33 using SVersion = server::Version;
34 using VersionPurpose = SVersion::VersionPurpose;
35 namespace mesg = sdbusplus::message;
36 namespace variant_ns = mesg::variant_ns;
37
38 mesg::object_path objPath;
39 auto purpose = VersionPurpose::Unknown;
Saqib Khan705f1bf2017-06-09 23:58:38 -050040 std::string version;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050041 std::map<std::string,
Patrick Williamse75d10f2017-05-30 16:56:32 -050042 std::map<std::string,
Saqib Khan84a0e692017-06-28 17:27:01 -050043 mesg::variant<std::string>>> interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050044 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050045 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050046 std::string filePath;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050047
48 for (const auto& intf : interfaces)
49 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050050 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050051 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050052 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050053 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050054 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050055 {
Saqib Khan84a0e692017-06-28 17:27:01 -050056 auto value = SVersion::convertVersionPurposeFromString(
57 variant_ns::get<std::string>(property.second));
58 if (value == VersionPurpose::BMC ||
59 value == VersionPurpose::System)
60 {
61 purpose = value;
62 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050063 }
64 else if (property.first == "Version")
65 {
Saqib Khan84a0e692017-06-28 17:27:01 -050066 version = variant_ns::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050067 }
68 }
69 }
Saqib Khan19177d32017-06-20 08:11:49 -050070 else if (intf.first == FILEPATH_IFACE)
71 {
72 for (const auto& property : intf.second)
73 {
74 if (property.first == "Path")
75 {
Saqib Khan84a0e692017-06-28 17:27:01 -050076 filePath = variant_ns::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050077 }
78 }
79 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050080 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050081 if (version.empty() ||
Saqib Khan19177d32017-06-20 08:11:49 -050082 filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -050083 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -050084 {
85 return;
86 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050087
88 // Version id is the last item in the path
89 auto pos = path.rfind("/");
90 if (pos == std::string::npos)
91 {
92 log<level::ERR>("No version id found in object path",
93 entry("OBJPATH=%s", path));
Patrick Williamse75d10f2017-05-30 16:56:32 -050094 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050095 }
96
97 auto versionId = path.substr(pos + 1);
98
Patrick Williamse75d10f2017-05-30 16:56:32 -050099 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500100 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500101 // Determine the Activation state by processing the given image dir.
102 auto activationState = server::Activation::Activations::Invalid;
103 ItemUpdater::ActivationStatus result = ItemUpdater::
Saqib Khan19177d32017-06-20 08:11:49 -0500104 validateSquashFSImage(filePath);
Saqib Khan35e83f32017-05-22 11:37:32 -0500105 if (result == ItemUpdater::ActivationStatus::ready)
106 {
107 activationState = server::Activation::Activations::Ready;
108 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500109 activations.insert(std::make_pair(
Saqib Khan705f1bf2017-06-09 23:58:38 -0500110 versionId,
111 std::make_unique<Activation>(
112 bus,
113 path,
Saqib Khan4c1aec02017-07-06 11:46:13 -0500114 *this,
Saqib Khan35e83f32017-05-22 11:37:32 -0500115 versionId,
Saqib Khan705f1bf2017-06-09 23:58:38 -0500116 activationState)));
117 versions.insert(std::make_pair(
118 versionId,
119 std::make_unique<phosphor::software::
120 manager::Version>(
121 bus,
122 path,
123 version,
124 purpose,
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500125 filePath,
126 std::bind(&ItemUpdater::erase,
127 this,
128 std::placeholders::_1))));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500129 }
Saqib Khan7b5010f2017-08-09 10:03:11 -0500130 else
131 {
132 log<level::INFO>("Software Object with the same version already exists",
133 entry("VERSION_ID=%s", versionId));
134 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500135 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500136}
137
Saqib Khanba239882017-05-26 08:41:54 -0500138void ItemUpdater::processBMCImage()
139{
140 auto purpose = server::Version::VersionPurpose::BMC;
141 auto version = phosphor::software::manager::Version::getBMCVersion();
142 auto id = phosphor::software::manager::Version::getId(version);
143 auto path = std::string{SOFTWARE_OBJPATH} + '/' + id;
144 activations.insert(std::make_pair(
145 id,
146 std::make_unique<Activation>(
147 bus,
148 path,
Saqib Khan4c1aec02017-07-06 11:46:13 -0500149 *this,
Saqib Khanba239882017-05-26 08:41:54 -0500150 id,
151 server::Activation::Activations::Active)));
152 versions.insert(std::make_pair(
153 id,
154 std::make_unique<phosphor::software::
155 manager::Version>(
156 bus,
157 path,
158 version,
159 purpose,
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500160 "",
161 std::bind(&ItemUpdater::erase,
162 this,
163 std::placeholders::_1))));
164
Saqib Khanba239882017-05-26 08:41:54 -0500165 return;
166}
167
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500168void ItemUpdater::erase(std::string entryId)
169{
170 // Delete ReadWrite and ReadOnly partitions
171 removeReadWritePartition(entryId);
172 removeReadOnlyPartition(entryId);
173
174 // Removing entry in versions map
175 auto it = versions.find(entryId);
176 if (it == versions.end())
177 {
178 log<level::ERR>(("Error: Failed to find version " + entryId + \
179 " in item updater versions map." \
180 " Unable to remove.").c_str());
181 return;
182 }
183 this->versions.erase(entryId);
184
185 // Removing entry in activations map
186 auto ita = activations.find(entryId);
187 if (ita == activations.end())
188 {
189 log<level::ERR>(("Error: Failed to find version " + entryId + \
190 " in item updater activations map." \
191 " Unable to remove.").c_str());
192 return;
193 }
194 // TODO: openbmc/openbmc#1986
195 // Test if this is the currently running image
196 // If not, don't continue.
197
198 this->activations.erase(entryId);
Saqib Khan5d532672017-08-09 10:44:50 -0500199 removeFile(entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500200}
201
Saqib Khan35e83f32017-05-22 11:37:32 -0500202ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
Saqib Khan19177d32017-06-20 08:11:49 -0500203 const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500204{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500205 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500206
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500207 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500208 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500209 fs::path file(filePath);
210 file /= bmcImage;
211 std::ifstream efile(file.c_str());
212 if (efile.good() != 1)
213 {
214 log<level::ERR>("Failed to find the BMC image.",
215 entry("IMAGE=%s", bmcImage.c_str()));
216 invalid = true;
217 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500218 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500219
220 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500221 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500222 return ItemUpdater::ActivationStatus::invalid;
223 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500224
225 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500226}
227
Saqib Khan4c1aec02017-07-06 11:46:13 -0500228void ItemUpdater::freePriority(uint8_t value)
229{
230 //TODO openbmc/openbmc#1896 Improve the performance of this function
231 for (const auto& intf : activations)
232 {
233 if(intf.second->redundancyPriority)
234 {
235 if (intf.second->redundancyPriority.get()->priority() == value)
236 {
237 intf.second->redundancyPriority.get()->priority(value+1);
238 }
239 }
240 }
241}
242
Michael Tritz37a59042017-07-12 13:44:53 -0500243void ItemUpdater::reset()
244{
245 // Mark the read-write partition for recreation upon reboot.
246 auto method = bus.new_method_call(
247 SYSTEMD_BUSNAME,
248 SYSTEMD_PATH,
249 SYSTEMD_INTERFACE,
250 "StartUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500251 method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace");
Michael Tritz37a59042017-07-12 13:44:53 -0500252 bus.call_noreply(method);
253
254 log<level::INFO>("BMC factory reset will take effect upon reboot.");
255
256 return;
257}
258
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500259void ItemUpdater::removeReadOnlyPartition(std::string versionId)
260{
261 auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId +
262 ".service";
263
264 // Remove the read-only partitions.
265 auto method = bus.new_method_call(
266 SYSTEMD_BUSNAME,
267 SYSTEMD_PATH,
268 SYSTEMD_INTERFACE,
269 "StartUnit");
270 method.append(serviceFile, "replace");
271 bus.call_noreply(method);
272}
273
274void ItemUpdater::removeReadWritePartition(std::string versionId)
275{
276 auto serviceFile = "obmc-flash-bmc-ubirw-remove.service";
277
278 // Remove the read-write partitions.
279 auto method = bus.new_method_call(
280 SYSTEMD_BUSNAME,
281 SYSTEMD_PATH,
282 SYSTEMD_INTERFACE,
283 "StartUnit");
284 method.append(serviceFile, "replace");
285 bus.call_noreply(method);
286}
287
Michael Tritz0129d922017-08-10 19:33:46 -0500288bool ItemUpdater::fieldModeEnabled(bool value)
289{
290 // enabling field mode is intended to be one way: false -> true
291 if (value && !control::FieldMode::fieldModeEnabled())
292 {
293 control::FieldMode::fieldModeEnabled(value);
294
295 auto method = bus.new_method_call(
296 SYSTEMD_BUSNAME,
297 SYSTEMD_PATH,
298 SYSTEMD_INTERFACE,
299 "StartUnit");
300 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
301 "replace");
302 bus.call_noreply(method);
303
304 method = bus.new_method_call(
305 SYSTEMD_BUSNAME,
306 SYSTEMD_PATH,
307 SYSTEMD_INTERFACE,
308 "StopUnit");
309 method.append("usr-local.mount", "replace");
310 bus.call_noreply(method);
311
312 std::vector<std::string> usrLocal = {"usr-local.mount"};
313
314 method = bus.new_method_call(
315 SYSTEMD_BUSNAME,
316 SYSTEMD_PATH,
317 SYSTEMD_INTERFACE,
318 "MaskUnitFiles");
319 method.append(usrLocal, false, true);
320 bus.call_noreply(method);
321 }
322
323 return control::FieldMode::fieldModeEnabled();
324}
325
326void ItemUpdater::restoreFieldModeStatus()
327{
328 std::ifstream input("/run/fw_env");
329 std::string envVar;
330 std::getline(input, envVar);
331
332 if(envVar.find("fieldmode=true") != std::string::npos)
333 {
334 ItemUpdater::fieldModeEnabled(true);
335 }
336}
337
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500338} // namespace updater
339} // namespace software
340} // namespace phosphor