blob: 1d680372910fd73bb91de6fc993bce3f1198cc9a [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
Gunnar Mills9a782242017-08-22 16:23:15 -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;
Gunnar Mills9a782242017-08-22 16:23:15 -050035 using VersionClass = phosphor::software::manager::Version;
Saqib Khan84a0e692017-06-28 17:27:01 -050036 namespace mesg = sdbusplus::message;
37 namespace variant_ns = mesg::variant_ns;
38
39 mesg::object_path objPath;
40 auto purpose = VersionPurpose::Unknown;
Saqib Khan705f1bf2017-06-09 23:58:38 -050041 std::string version;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050042 std::map<std::string,
Patrick Williamse75d10f2017-05-30 16:56:32 -050043 std::map<std::string,
Saqib Khan84a0e692017-06-28 17:27:01 -050044 mesg::variant<std::string>>> interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050045 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050046 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050047 std::string filePath;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050048
49 for (const auto& intf : interfaces)
50 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050051 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050052 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050053 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050054 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050055 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050056 {
Saqib Khan84a0e692017-06-28 17:27:01 -050057 auto value = SVersion::convertVersionPurposeFromString(
Gunnar Mills9a782242017-08-22 16:23:15 -050058 variant_ns::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050059 if (value == VersionPurpose::BMC ||
60 value == VersionPurpose::System)
61 {
62 purpose = value;
63 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050064 }
65 else if (property.first == "Version")
66 {
Saqib Khan84a0e692017-06-28 17:27:01 -050067 version = variant_ns::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050068 }
69 }
70 }
Saqib Khan19177d32017-06-20 08:11:49 -050071 else if (intf.first == FILEPATH_IFACE)
72 {
73 for (const auto& property : intf.second)
74 {
75 if (property.first == "Path")
76 {
Saqib Khan84a0e692017-06-28 17:27:01 -050077 filePath = variant_ns::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050078 }
79 }
80 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050081 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050082 if (version.empty() ||
Saqib Khan19177d32017-06-20 08:11:49 -050083 filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -050084 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -050085 {
86 return;
87 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050088
89 // Version id is the last item in the path
90 auto pos = path.rfind("/");
91 if (pos == std::string::npos)
92 {
93 log<level::ERR>("No version id found in object path",
94 entry("OBJPATH=%s", path));
Patrick Williamse75d10f2017-05-30 16:56:32 -050095 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050096 }
97
98 auto versionId = path.substr(pos + 1);
99
Patrick Williamse75d10f2017-05-30 16:56:32 -0500100 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500101 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500102 // Determine the Activation state by processing the given image dir.
103 auto activationState = server::Activation::Activations::Invalid;
Gunnar Mills9a782242017-08-22 16:23:15 -0500104 ItemUpdater::ActivationStatus result =
105 ItemUpdater::validateSquashFSImage(filePath);
Saqib Khan35e83f32017-05-22 11:37:32 -0500106 if (result == ItemUpdater::ActivationStatus::ready)
107 {
108 activationState = server::Activation::Activations::Ready;
109 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500110 activations.insert(std::make_pair(
Saqib Khan705f1bf2017-06-09 23:58:38 -0500111 versionId,
112 std::make_unique<Activation>(
113 bus,
114 path,
Saqib Khan4c1aec02017-07-06 11:46:13 -0500115 *this,
Saqib Khan35e83f32017-05-22 11:37:32 -0500116 versionId,
Saqib Khan705f1bf2017-06-09 23:58:38 -0500117 activationState)));
118 versions.insert(std::make_pair(
119 versionId,
Gunnar Mills9a782242017-08-22 16:23:15 -0500120 std::make_unique<VersionClass>(
Saqib Khan705f1bf2017-06-09 23:58:38 -0500121 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",
Gunnar Mills9a782242017-08-22 16:23:15 -0500133 entry("VERSION_ID=%s", versionId));
Saqib Khan7b5010f2017-08-09 10:03:11 -0500134 }
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{
Gunnar Mills9a782242017-08-22 16:23:15 -0500140 using VersionClass = phosphor::software::manager::Version;
Saqib Khanba239882017-05-26 08:41:54 -0500141 auto purpose = server::Version::VersionPurpose::BMC;
142 auto version = phosphor::software::manager::Version::getBMCVersion();
143 auto id = phosphor::software::manager::Version::getId(version);
144 auto path = std::string{SOFTWARE_OBJPATH} + '/' + id;
145 activations.insert(std::make_pair(
146 id,
147 std::make_unique<Activation>(
148 bus,
149 path,
Saqib Khan4c1aec02017-07-06 11:46:13 -0500150 *this,
Saqib Khanba239882017-05-26 08:41:54 -0500151 id,
152 server::Activation::Activations::Active)));
153 versions.insert(std::make_pair(
154 id,
Gunnar Mills9a782242017-08-22 16:23:15 -0500155 std::make_unique<VersionClass>(
Saqib Khanba239882017-05-26 08:41:54 -0500156 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{
Saqib Khan0c2eb262017-08-19 11:01:18 -0500170 // Delete ReadOnly partitions
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500171 removeReadOnlyPartition(entryId);
172
173 // Removing entry in versions map
174 auto it = versions.find(entryId);
175 if (it == versions.end())
176 {
177 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills9a782242017-08-22 16:23:15 -0500178 " in item updater versions map." \
179 " Unable to remove.").c_str());
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500180 return;
181 }
182 this->versions.erase(entryId);
183
184 // Removing entry in activations map
185 auto ita = activations.find(entryId);
186 if (ita == activations.end())
187 {
188 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills9a782242017-08-22 16:23:15 -0500189 " in item updater activations map." \
190 " Unable to remove.").c_str());
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500191 return;
192 }
193 // TODO: openbmc/openbmc#1986
194 // Test if this is the currently running image
195 // If not, don't continue.
196
197 this->activations.erase(entryId);
Saqib Khan5d532672017-08-09 10:44:50 -0500198 removeFile(entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500199}
200
Saqib Khan35e83f32017-05-22 11:37:32 -0500201ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
Gunnar Mills9a782242017-08-22 16:23:15 -0500202 const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500203{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500204 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500205
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500206 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500207 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500208 fs::path file(filePath);
209 file /= bmcImage;
210 std::ifstream efile(file.c_str());
211 if (efile.good() != 1)
212 {
213 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500214 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500215 invalid = true;
216 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500217 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500218
219 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500220 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500221 return ItemUpdater::ActivationStatus::invalid;
222 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500223
224 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500225}
226
Saqib Khan4c1aec02017-07-06 11:46:13 -0500227void ItemUpdater::freePriority(uint8_t value)
228{
229 //TODO openbmc/openbmc#1896 Improve the performance of this function
230 for (const auto& intf : activations)
231 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500232 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500233 {
234 if (intf.second->redundancyPriority.get()->priority() == value)
235 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500236 intf.second->redundancyPriority.get()->priority(value + 1);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500237 }
238 }
239 }
240}
241
Michael Tritz37a59042017-07-12 13:44:53 -0500242void ItemUpdater::reset()
243{
244 // Mark the read-write partition for recreation upon reboot.
245 auto method = bus.new_method_call(
246 SYSTEMD_BUSNAME,
247 SYSTEMD_PATH,
248 SYSTEMD_INTERFACE,
249 "StartUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500250 method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace");
Michael Tritz37a59042017-07-12 13:44:53 -0500251 bus.call_noreply(method);
252
253 log<level::INFO>("BMC factory reset will take effect upon reboot.");
254
255 return;
256}
257
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500258void ItemUpdater::removeReadOnlyPartition(std::string versionId)
259{
260 auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId +
261 ".service";
262
263 // Remove the read-only partitions.
264 auto method = bus.new_method_call(
265 SYSTEMD_BUSNAME,
266 SYSTEMD_PATH,
267 SYSTEMD_INTERFACE,
268 "StartUnit");
269 method.append(serviceFile, "replace");
270 bus.call_noreply(method);
271}
272
Michael Tritz0129d922017-08-10 19:33:46 -0500273bool ItemUpdater::fieldModeEnabled(bool value)
274{
275 // enabling field mode is intended to be one way: false -> true
276 if (value && !control::FieldMode::fieldModeEnabled())
277 {
278 control::FieldMode::fieldModeEnabled(value);
279
280 auto method = bus.new_method_call(
281 SYSTEMD_BUSNAME,
282 SYSTEMD_PATH,
283 SYSTEMD_INTERFACE,
284 "StartUnit");
285 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
Gunnar Mills9a782242017-08-22 16:23:15 -0500286 "replace");
Michael Tritz0129d922017-08-10 19:33:46 -0500287 bus.call_noreply(method);
288
289 method = bus.new_method_call(
290 SYSTEMD_BUSNAME,
291 SYSTEMD_PATH,
292 SYSTEMD_INTERFACE,
293 "StopUnit");
294 method.append("usr-local.mount", "replace");
295 bus.call_noreply(method);
296
297 std::vector<std::string> usrLocal = {"usr-local.mount"};
298
299 method = bus.new_method_call(
300 SYSTEMD_BUSNAME,
301 SYSTEMD_PATH,
302 SYSTEMD_INTERFACE,
303 "MaskUnitFiles");
304 method.append(usrLocal, false, true);
305 bus.call_noreply(method);
306 }
307
308 return control::FieldMode::fieldModeEnabled();
309}
310
311void ItemUpdater::restoreFieldModeStatus()
312{
313 std::ifstream input("/run/fw_env");
314 std::string envVar;
315 std::getline(input, envVar);
316
Gunnar Mills9a782242017-08-22 16:23:15 -0500317 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500318 {
319 ItemUpdater::fieldModeEnabled(true);
320 }
321}
322
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500323} // namespace updater
324} // namespace software
325} // namespace phosphor