blob: 6b9ec1c6735b36b27e7a13dcd9b98357de7cd741 [file] [log] [blame]
Saqib Khan7254f0e2017-04-10 21:45:37 -05001#include <string>
Adriana Kobylak5ba6b102017-05-19 09:41:27 -05002#include <experimental/filesystem>
Saqib Khan7254f0e2017-04-10 21:45:37 -05003#include <fstream>
Eddie James13fc66a2017-08-31 15:36:44 -05004#include <phosphor-logging/elog-errors.hpp>
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -05005#include <phosphor-logging/log.hpp>
Eddie James13fc66a2017-08-31 15:36:44 -05006#include "xyz/openbmc_project/Common/error.hpp"
Adriana Kobylakd6a549e2017-05-10 16:23:01 -05007#include <xyz/openbmc_project/Software/Version/server.hpp>
Saqib Khan167601b2017-06-18 23:33:46 -05008#include "version.hpp"
Adriana Kobylak2d8fa222017-03-15 12:34:32 -05009#include "config.h"
10#include "item_updater.hpp"
Saqib Khana8ade7e2017-04-12 10:27:56 -050011#include "activation.hpp"
Michael Tritz60bc20f2017-07-29 23:32:21 -050012#include "serialize.hpp"
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050013
14namespace openpower
15{
16namespace software
17{
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -050018namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050019{
20
Saqib Khana8ade7e2017-04-12 10:27:56 -050021// When you see server:: you know we're referencing our base class
22namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050023namespace fs = std::experimental::filesystem;
Saqib Khana8ade7e2017-04-12 10:27:56 -050024
Eddie James13fc66a2017-08-31 15:36:44 -050025using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050026using namespace phosphor::logging;
27
Saqib Khana8ade7e2017-04-12 10:27:56 -050028constexpr auto squashFSImage = "pnor.xz.squashfs";
Eddie James13fc66a2017-08-31 15:36:44 -050029constexpr auto CHASSIS_STATE_PATH = "/xyz/openbmc_project/state/chassis0";
30constexpr auto CHASSIS_STATE_OBJ = "xyz.openbmc_project.State.Chassis";
31constexpr auto CHASSIS_STATE_OFF =
32 "xyz.openbmc_project.State.Chassis.PowerState.Off";
33constexpr auto SYSTEMD_PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
Saqib Khana8ade7e2017-04-12 10:27:56 -050034
Michael Tritzb541f1b2017-10-15 15:10:21 -050035// TODO: Change paths once openbmc/openbmc#1663 is completed.
36constexpr auto MBOXD_INTERFACE = "org.openbmc.mboxd";
37constexpr auto MBOXD_PATH = "/org/openbmc/mboxd";
38
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050039void ItemUpdater::createActivation(sdbusplus::message::message& m)
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050040{
Patrick Williamse4290942017-06-16 05:43:08 -050041 using SVersion = server::Version;
42 using VersionPurpose = SVersion::VersionPurpose;
43 namespace msg = sdbusplus::message;
44 namespace variant_ns = msg::variant_ns;
45
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050046 sdbusplus::message::object_path objPath;
47 std::map<std::string,
Patrick Williamse4290942017-06-16 05:43:08 -050048 std::map<std::string, msg::variant<std::string>>> interfaces;
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050049 m.read(objPath, interfaces);
Patrick Williamse4290942017-06-16 05:43:08 -050050
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050051 std::string path(std::move(objPath));
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050052 std::string filePath;
Patrick Williamse4290942017-06-16 05:43:08 -050053 auto purpose = VersionPurpose::Unknown;
Saqib Khance148702017-06-11 12:01:58 -050054 std::string version;
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050055
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050056 for (const auto& intf : interfaces)
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050057 {
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050058 if (intf.first == VERSION_IFACE)
59 {
60 for (const auto& property : intf.second)
61 {
62 if (property.first == "Purpose")
63 {
64 // Only process the Host and System images
Patrick Williamse4290942017-06-16 05:43:08 -050065 auto value = SVersion::convertVersionPurposeFromString(
66 variant_ns::get<std::string>(property.second));
67
68 if (value == VersionPurpose::Host ||
69 value == VersionPurpose::System)
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050070 {
Saqib Khance148702017-06-11 12:01:58 -050071 purpose = value;
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050072 }
73 }
Saqib Khance148702017-06-11 12:01:58 -050074 else if (property.first == "Version")
75 {
Patrick Williamse4290942017-06-16 05:43:08 -050076 version = variant_ns::get<std::string>(property.second);
Saqib Khance148702017-06-11 12:01:58 -050077 }
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050078 }
79 }
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050080 else if (intf.first == FILEPATH_IFACE)
81 {
82 for (const auto& property : intf.second)
83 {
84 if (property.first == "Path")
85 {
Patrick Williamse4290942017-06-16 05:43:08 -050086 filePath = variant_ns::get<std::string>(property.second);
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050087 }
88 }
89 }
90 }
Patrick Williamse4290942017-06-16 05:43:08 -050091 if ((filePath.empty()) || (purpose == VersionPurpose::Unknown))
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050092 {
93 return;
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050094 }
95
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050096 // Version id is the last item in the path
97 auto pos = path.rfind("/");
98 if (pos == std::string::npos)
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050099 {
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500100 log<level::ERR>("No version id found in object path",
101 entry("OBJPATH=%s", path));
102 return;
103 }
104
105 auto versionId = path.substr(pos + 1);
106
107 if (activations.find(versionId) == activations.end())
108 {
109 // Determine the Activation state by processing the given image dir.
110 auto activationState = server::Activation::Activations::Invalid;
Gunnar Mills3588acc2017-09-07 13:13:22 -0500111 AssociationList associations = {};
Adriana Kobylak5ba6b102017-05-19 09:41:27 -0500112 if (ItemUpdater::validateSquashFSImage(filePath) == 0)
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -0500113 {
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500114 activationState = server::Activation::Activations::Ready;
Gunnar Mills3588acc2017-09-07 13:13:22 -0500115 // Create an association to the host inventory item
116 associations.emplace_back(std::make_tuple(
117 ACTIVATION_FWD_ASSOCIATION,
118 ACTIVATION_REV_ASSOCIATION,
119 HOST_INVENTORY_PATH));
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -0500120 }
121
Adriana Kobylak5ba6b102017-05-19 09:41:27 -0500122 fs::path manifestPath(filePath);
123 manifestPath /= MANIFEST_FILE;
Saqib Khan167601b2017-06-18 23:33:46 -0500124 std::string extendedVersion = (Version::getValue(manifestPath.string(),
125 std::map<std::string, std::string>
126 {{"extended_version", ""}})).begin()->second;
Gunnar Mills3edb84b2017-08-18 15:13:15 -0500127
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500128 activations.insert(std::make_pair(
129 versionId,
130 std::make_unique<Activation>(
131 bus,
132 path,
Saqib Khan81bac882017-06-08 12:17:01 -0500133 *this,
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500134 versionId,
135 extendedVersion,
Gunnar Mills3edb84b2017-08-18 15:13:15 -0500136 activationState,
137 associations)));
Saqib Khance148702017-06-11 12:01:58 -0500138 versions.insert(std::make_pair(
139 versionId,
140 std::make_unique<Version>(
141 bus,
142 path,
143 version,
144 purpose,
Eddie Jamesfa5daf42017-09-01 11:51:28 -0500145 filePath)));
Saqib Khan00044f42017-07-10 17:24:43 -0500146 }
Patrick Williams3accb322017-05-30 16:29:52 -0500147 return;
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500148}
149
Saqib Khan167601b2017-06-18 23:33:46 -0500150void ItemUpdater::processPNORImage()
Saqib Khan7254f0e2017-04-10 21:45:37 -0500151{
Saqib Khan4c5d7442017-07-18 00:43:52 -0500152 // Read pnor.toc from folders under /media/
153 // to get Active Software Versions.
Gunnar Mills3fa70282017-08-18 15:30:42 -0500154 for (const auto& iter : fs::directory_iterator(MEDIA_DIR))
Saqib Khan4c5d7442017-07-18 00:43:52 -0500155 {
156 auto activationState = server::Activation::Activations::Active;
157
158 static const auto PNOR_RO_PREFIX_LEN = strlen(PNOR_RO_PREFIX);
Saqib Khan2be9ba92017-09-26 22:44:10 -0500159 static const auto PNOR_RW_PREFIX_LEN = strlen(PNOR_RW_PREFIX);
Saqib Khan4c5d7442017-07-18 00:43:52 -0500160
161 // Check if the PNOR_RO_PREFIX is the prefix of the iter.path
162 if (0 == iter.path().native().compare(0, PNOR_RO_PREFIX_LEN,
163 PNOR_RO_PREFIX))
164 {
Saqib Khan6a522262017-09-26 12:02:50 -0500165 // The versionId is extracted from the path
166 // for example /media/pnor-ro-2a1022fe.
167 auto id = iter.path().native().substr(PNOR_RO_PREFIX_LEN);
Saqib Khan4c5d7442017-07-18 00:43:52 -0500168 auto pnorTOC = iter.path() / PNOR_TOC_FILE;
169 if (!fs::is_regular_file(pnorTOC))
170 {
Gunnar Mills850d5f62017-10-19 17:04:38 -0500171 log<level::ERR>("Failed to read pnorTOC.",
172 entry("FILENAME=%s", pnorTOC.string()));
Saqib Khan6a522262017-09-26 12:02:50 -0500173 ItemUpdater::erase(id);
174 continue;
Saqib Khan4c5d7442017-07-18 00:43:52 -0500175 }
176 auto keyValues =
177 Version::getValue(pnorTOC,
178 {{ "version", "" },
179 { "extended_version", "" } });
180 auto& version = keyValues.at("version");
181 if (version.empty())
182 {
183 log<level::ERR>("Failed to read version from pnorTOC",
184 entry("FILENAME=%s", pnorTOC.string()));
185 activationState = server::Activation::Activations::Invalid;
186 }
187
188 auto& extendedVersion = keyValues.at("extended_version");
189 if (extendedVersion.empty())
190 {
191 log<level::ERR>("Failed to read extendedVersion from pnorTOC",
192 entry("FILENAME=%s", pnorTOC.string()));
193 activationState = server::Activation::Activations::Invalid;
194 }
195
Saqib Khan4c5d7442017-07-18 00:43:52 -0500196 auto purpose = server::Version::VersionPurpose::Host;
197 auto path = fs::path(SOFTWARE_OBJPATH) / id;
Gunnar Mills3588acc2017-09-07 13:13:22 -0500198 AssociationList associations = {};
Saqib Khan4c5d7442017-07-18 00:43:52 -0500199
Gunnar Mills3588acc2017-09-07 13:13:22 -0500200 if (activationState == server::Activation::Activations::Active)
201 {
202 // Create an association to the host inventory item
203 associations.emplace_back(std::make_tuple(
204 ACTIVATION_FWD_ASSOCIATION,
205 ACTIVATION_REV_ASSOCIATION,
206 HOST_INVENTORY_PATH));
Gunnar Mills3edb84b2017-08-18 15:13:15 -0500207
Gunnar Mills3588acc2017-09-07 13:13:22 -0500208 // Create an active association since this image is active
209 createActiveAssociation(path);
210 }
Gunnar Mills3edb84b2017-08-18 15:13:15 -0500211
Saqib Khan4c5d7442017-07-18 00:43:52 -0500212 // Create Activation instance for this version.
213 activations.insert(std::make_pair(
214 id,
215 std::make_unique<Activation>(
216 bus,
217 path,
218 *this,
219 id,
220 extendedVersion,
Gunnar Mills3edb84b2017-08-18 15:13:15 -0500221 activationState,
222 associations)));
Saqib Khan4c5d7442017-07-18 00:43:52 -0500223
224 // If Active, create RedundancyPriority instance for this version.
225 if (activationState == server::Activation::Activations::Active)
226 {
Michael Tritz36417922017-08-04 14:00:29 -0500227 uint8_t priority = std::numeric_limits<uint8_t>::max();
228 if (!restoreFromFile(id, priority))
Saqib Khan4c5d7442017-07-18 00:43:52 -0500229 {
Michael Tritz36417922017-08-04 14:00:29 -0500230 log<level::ERR>("Unable to restore priority from file.",
Gunnar Mills3fa70282017-08-18 15:30:42 -0500231 entry("VERSIONID=%s", id));
Saqib Khan4c5d7442017-07-18 00:43:52 -0500232 }
Michael Tritz36417922017-08-04 14:00:29 -0500233 activations.find(id)->second->redundancyPriority =
234 std::make_unique<RedundancyPriority>(
235 bus,
236 path,
237 *(activations.find(id)->second),
238 priority);
Saqib Khan4c5d7442017-07-18 00:43:52 -0500239 }
240
241 // Create Version instance for this version.
242 versions.insert(std::make_pair(
243 id,
244 std::make_unique<Version>(
245 bus,
246 path,
247 version,
248 purpose,
Eddie Jamesfa5daf42017-09-01 11:51:28 -0500249 "")));
Saqib Khan4c5d7442017-07-18 00:43:52 -0500250 }
Saqib Khan2be9ba92017-09-26 22:44:10 -0500251 else if (0 == iter.path().native().compare(0, PNOR_RW_PREFIX_LEN,
252 PNOR_RW_PREFIX))
253 {
254 auto id = iter.path().native().substr(PNOR_RW_PREFIX_LEN);
255 auto roDir = PNOR_RO_PREFIX + id;
256 if (!fs::is_directory(roDir))
257 {
258 log<level::ERR>("No corresponding read-only volume found.",
259 entry("DIRNAME=%s", roDir));
260 ItemUpdater::erase(id);
261 }
262 }
Saqib Khan4c5d7442017-07-18 00:43:52 -0500263 }
Gunnar Mills2badd7a2017-09-20 12:51:28 -0500264
265 // Look at the RO symlink to determine if there is a functional image
266 auto id = determineId(PNOR_RO_ACTIVE_PATH);
267 if (!id.empty())
268 {
269 updateFunctionalAssociation(std::string{SOFTWARE_OBJPATH} + '/' + id);
270 }
Saqib Khan167601b2017-06-18 23:33:46 -0500271 return;
Saqib Khan7254f0e2017-04-10 21:45:37 -0500272}
273
Adriana Kobylak5ba6b102017-05-19 09:41:27 -0500274int ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khana8ade7e2017-04-12 10:27:56 -0500275{
Saqib Khan4c5d7442017-07-18 00:43:52 -0500276 auto file = fs::path(filePath) / squashFSImage;
277 if (fs::is_regular_file(file))
Saqib Khana8ade7e2017-04-12 10:27:56 -0500278 {
279 return 0;
280 }
281 else
282 {
283 log<level::ERR>("Failed to find the SquashFS image.");
284 return -1;
285 }
286}
287
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500288void ItemUpdater::removeReadOnlyPartition(std::string versionId)
Michael Tritzdd961b62017-05-17 14:07:03 -0500289{
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500290 auto serviceFile = "obmc-flash-bios-ubiumount-ro@" + versionId +
291 ".service";
292
293 // Remove the read-only partitions.
294 auto method = bus.new_method_call(
295 SYSTEMD_BUSNAME,
296 SYSTEMD_PATH,
297 SYSTEMD_INTERFACE,
298 "StartUnit");
299 method.append(serviceFile, "replace");
300 bus.call_noreply(method);
301}
302
303void ItemUpdater::removeReadWritePartition(std::string versionId)
304{
305 auto serviceFile = "obmc-flash-bios-ubiumount-rw@" + versionId +
Michael Tritzdd961b62017-05-17 14:07:03 -0500306 ".service";
307
308 // Remove the read-write partitions.
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500309 auto method = bus.new_method_call(
Michael Tritzdd961b62017-05-17 14:07:03 -0500310 SYSTEMD_BUSNAME,
311 SYSTEMD_PATH,
312 SYSTEMD_INTERFACE,
313 "StartUnit");
314 method.append(serviceFile, "replace");
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500315 bus.call_noreply(method);
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500316}
Michael Tritzdd961b62017-05-17 14:07:03 -0500317
Michael Tritzfa7aa122017-09-22 15:16:11 -0500318void ItemUpdater::reset()
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500319{
Michael Tritzfa7aa122017-09-22 15:16:11 -0500320 for (const auto& it : activations)
321 {
322 auto serviceFile = "obmc-flash-bios-ubiclear@pnor-rw-" + it.first +
323 ".service";
324
325 // Clear the read-write partitions.
326 auto method = bus.new_method_call(
327 SYSTEMD_BUSNAME,
328 SYSTEMD_PATH,
329 SYSTEMD_INTERFACE,
330 "StartUnit");
331 method.append(serviceFile, "replace");
332 auto reply = bus.call(method);
333
334 if (reply.is_method_error())
335 {
Marri Devender Rao795d9842017-11-07 22:52:07 -0600336 log<level::ERR>("Failed to clear read-write partitions",
337 entry("SERVICE_FILE=%s", serviceFile));
Michael Tritzfa7aa122017-09-22 15:16:11 -0500338 elog<InternalFailure>();
339 }
340
341 removeFile(it.first);
342 }
Marri Devender Rao795d9842017-11-07 22:52:07 -0600343 static constexpr auto serviceFile =
344 "obmc-flash-bios-ubiclear@pnor-prsv.service";
Michael Tritzfa7aa122017-09-22 15:16:11 -0500345 // Clear the preserved partition.
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500346 auto method = bus.new_method_call(
Michael Tritzdd961b62017-05-17 14:07:03 -0500347 SYSTEMD_BUSNAME,
348 SYSTEMD_PATH,
349 SYSTEMD_INTERFACE,
350 "StartUnit");
Marri Devender Rao795d9842017-11-07 22:52:07 -0600351 method.append(serviceFile, "replace");
Michael Tritzfa7aa122017-09-22 15:16:11 -0500352 auto reply = bus.call(method);
Michael Tritzdd961b62017-05-17 14:07:03 -0500353
Michael Tritzfa7aa122017-09-22 15:16:11 -0500354 if (reply.is_method_error())
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500355 {
Marri Devender Rao795d9842017-11-07 22:52:07 -0600356 log<level::ERR>("Failed to clear preserved partition",
357 entry("SERVICE_FILE=%s", serviceFile));
Michael Tritzfa7aa122017-09-22 15:16:11 -0500358 elog<InternalFailure>();
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500359 }
Michael Tritzfa7aa122017-09-22 15:16:11 -0500360
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500361 return;
362}
363
Eddie James13fc66a2017-08-31 15:36:44 -0500364bool ItemUpdater::isVersionFunctional(std::string versionId)
365{
366 if (!fs::exists(PNOR_RO_ACTIVE_PATH))
367 {
368 return false;
369 }
370
371 fs::path activeRO = fs::read_symlink(PNOR_RO_ACTIVE_PATH);
372
373 if (!fs::is_directory(activeRO))
374 {
375 return false;
376 }
377
378 if (activeRO.string().find(versionId) == std::string::npos)
379 {
380 return false;
381 }
382
383 // active PNOR is the version we're checking
384 return true;
385}
386
387bool ItemUpdater::isChassisOn()
388{
389 auto mapperCall = bus.new_method_call(
390 MAPPER_BUSNAME,
391 MAPPER_PATH,
392 MAPPER_INTERFACE,
393 "GetObject");
394
395 mapperCall.append(CHASSIS_STATE_PATH,
396 std::vector<std::string>({CHASSIS_STATE_OBJ}));
397 auto mapperResponseMsg = bus.call(mapperCall);
398 if (mapperResponseMsg.is_method_error())
399 {
400 log<level::ERR>("Error in Mapper call");
401 elog<InternalFailure>();
402 }
403 using MapperResponseType = std::map<std::string, std::vector<std::string>>;
404 MapperResponseType mapperResponse;
405 mapperResponseMsg.read(mapperResponse);
406 if (mapperResponse.empty())
407 {
408 log<level::ERR>("Invalid Response from mapper");
409 elog<InternalFailure>();
410 }
411
412 auto method = bus.new_method_call((mapperResponse.begin()->first).c_str(),
413 CHASSIS_STATE_PATH,
414 SYSTEMD_PROPERTY_INTERFACE,
415 "Get");
416 method.append(CHASSIS_STATE_OBJ, "CurrentPowerState");
417 auto response = bus.call(method);
418 if (response.is_method_error())
419 {
420 log<level::ERR>("Error in fetching current Chassis State",
Gunnar Mills850d5f62017-10-19 17:04:38 -0500421 entry("MAPPERRESPONSE=%s",
Eddie James13fc66a2017-08-31 15:36:44 -0500422 (mapperResponse.begin()->first).c_str()));
423 elog<InternalFailure>();
424 }
425 sdbusplus::message::variant<std::string> currentChassisState;
426 response.read(currentChassisState);
427 auto strParam =
428 sdbusplus::message::variant_ns::get<std::string>(currentChassisState);
429 return (strParam != CHASSIS_STATE_OFF);
430}
431
Saqib Khanb8e7f312017-08-12 10:24:10 -0500432void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan81bac882017-06-08 12:17:01 -0500433{
434 //TODO openbmc/openbmc#1896 Improve the performance of this function
435 for (const auto& intf : activations)
436 {
Gunnar Mills3fa70282017-08-18 15:30:42 -0500437 if (intf.second->redundancyPriority)
Saqib Khan81bac882017-06-08 12:17:01 -0500438 {
Saqib Khanb8e7f312017-08-12 10:24:10 -0500439 if (intf.second->redundancyPriority.get()->priority() == value &&
440 intf.second->versionId != versionId)
Saqib Khan81bac882017-06-08 12:17:01 -0500441 {
Gunnar Mills3fa70282017-08-18 15:30:42 -0500442 intf.second->redundancyPriority.get()->priority(value + 1);
Saqib Khan81bac882017-06-08 12:17:01 -0500443 }
444 }
445 }
446}
447
Saqib Khan2af5c492017-07-17 16:15:13 -0500448bool ItemUpdater::isLowestPriority(uint8_t value)
449{
450 for (const auto& intf : activations)
451 {
Gunnar Mills3fa70282017-08-18 15:30:42 -0500452 if (intf.second->redundancyPriority)
Saqib Khan2af5c492017-07-17 16:15:13 -0500453 {
454 if (intf.second->redundancyPriority.get()->priority() < value)
455 {
456 return false;
457 }
458 }
459 }
460 return true;
461}
462
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500463void ItemUpdater::erase(std::string entryId)
464{
Eddie James13fc66a2017-08-31 15:36:44 -0500465 if (isVersionFunctional(entryId) && isChassisOn()) {
466 log<level::ERR>(("Error: Version " + entryId + \
467 " is currently active and running on the host." \
468 " Unable to remove.").c_str());
469 return;
470 }
Saqib Khanef8cd9f2017-08-16 14:20:30 -0500471 // Remove priority persistence file
472 removeFile(entryId);
473
Saqib Khan1e0aa5c2017-08-31 11:04:17 -0500474 // Removing read-only and read-write partitions
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500475 removeReadWritePartition(entryId);
476 removeReadOnlyPartition(entryId);
477
478 // Removing entry in versions map
479 auto it = versions.find(entryId);
480 if (it == versions.end())
481 {
482 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills3fa70282017-08-18 15:30:42 -0500483 " in item updater versions map." \
484 " Unable to remove.").c_str());
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500485 return;
486 }
487 versions.erase(entryId);
488
489 // Removing entry in activations map
490 auto ita = activations.find(entryId);
491 if (ita == activations.end())
492 {
493 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills3fa70282017-08-18 15:30:42 -0500494 " in item updater activations map." \
495 " Unable to remove.").c_str());
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500496 return;
497 }
498 activations.erase(entryId);
499}
500
Michael Tritz234a07e2017-09-21 00:53:06 -0500501void ItemUpdater::deleteAll()
502{
503 std::vector<std::string> deletableActivations;
504
505 for (const auto& activationIt : activations)
506 {
507 if (!isVersionFunctional(activationIt.first))
508 {
509 deletableActivations.push_back(activationIt.first);
510 }
511 }
512
513 for (const auto& deletableIt : deletableActivations)
514 {
515 ItemUpdater::erase(deletableIt);
516 }
517
518 // Remove any remaining pnor-ro- or pnor-rw- volumes that do not match
519 // the current version.
520 auto method = bus.new_method_call(
521 SYSTEMD_BUSNAME,
522 SYSTEMD_PATH,
523 SYSTEMD_INTERFACE,
524 "StartUnit");
525 method.append("obmc-flash-bios-cleanup.service", "replace");
526 bus.call_noreply(method);
527}
528
Saqib Khan2cbfa032017-08-17 14:52:37 -0500529// TODO: openbmc/openbmc#1402 Monitor flash usage
530void ItemUpdater::freeSpace()
531{
532 std::size_t count = 0;
533 decltype(activations.begin()->second->redundancyPriority.get()->priority())
534 highestPriority = 0;
535 decltype(activations.begin()->second->versionId) highestPriorityVersion;
536 for (const auto& iter : activations)
537 {
538 if (iter.second.get()->activation() == server::Activation::Activations::Active)
539 {
540 count++;
Adriana Kobylakee201a52017-11-09 15:05:04 -0600541 if (isVersionFunctional(iter.second->versionId))
542 {
543 continue;
544 }
545 if (iter.second->redundancyPriority.get()->priority() >= highestPriority)
Saqib Khan2cbfa032017-08-17 14:52:37 -0500546 {
547 highestPriority = iter.second->redundancyPriority.get()->priority();
548 highestPriorityVersion = iter.second->versionId;
549 }
550 }
551 }
552 // Remove the pnor version with highest priority since the PNOR
553 // can't hold more than 2 versions.
554 if (count >= ACTIVE_PNOR_MAX_ALLOWED)
555 {
556 erase(highestPriorityVersion);
557 }
558}
559
Gunnar Mills61010b22017-09-20 15:25:26 -0500560void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Mills9741cd12017-08-28 15:09:00 -0500561{
562 assocs.emplace_back(std::make_tuple(ACTIVE_FWD_ASSOCIATION,
563 ACTIVE_REV_ASSOCIATION,
564 path));
565 associations(assocs);
566}
567
Gunnar Mills833e4f32017-09-14 12:30:27 -0500568void ItemUpdater::updateFunctionalAssociation(const std::string& path)
569{
570 // remove all functional associations
571 for (auto iter = assocs.begin(); iter != assocs.end();)
572 {
573 if ((std::get<0>(*iter)).compare(FUNCTIONAL_FWD_ASSOCIATION) == 0)
574 {
575 iter = assocs.erase(iter);
576 }
577 else
578 {
579 ++iter;
580 }
581 }
582 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
583 FUNCTIONAL_REV_ASSOCIATION,
584 path));
585 associations(assocs);
586}
587
Gunnar Mills61010b22017-09-20 15:25:26 -0500588void ItemUpdater::removeActiveAssociation(const std::string& path)
Gunnar Mills9741cd12017-08-28 15:09:00 -0500589{
590 for (auto iter = assocs.begin(); iter != assocs.end();)
591 {
Gunnar Mills833e4f32017-09-14 12:30:27 -0500592 if ((std::get<0>(*iter)).compare(ACTIVE_FWD_ASSOCIATION) == 0 &&
593 (std::get<2>(*iter)).compare(path) == 0)
Gunnar Mills9741cd12017-08-28 15:09:00 -0500594 {
595 iter = assocs.erase(iter);
596 associations(assocs);
597 }
598 else
599 {
600 ++iter;
601 }
602 }
603}
604
Gunnar Mills2badd7a2017-09-20 12:51:28 -0500605std::string ItemUpdater::determineId(const std::string& symlinkPath)
606{
607 if (!fs::exists(symlinkPath))
608 {
609 return {};
610 }
611
612 auto target = fs::canonical(symlinkPath).string();
613
614 // check to make sure the target really exists
615 if (!fs::is_regular_file(target + "/" + PNOR_TOC_FILE))
616 {
617 return {};
618 }
619 // Get the image <id> from the symlink target
620 // for example /media/ro-2a1022fe
621 static const auto PNOR_RO_PREFIX_LEN = strlen(PNOR_RO_PREFIX);
622 return target.substr(PNOR_RO_PREFIX_LEN);
623}
624
Michael Tritzb541f1b2017-10-15 15:10:21 -0500625void GardReset::reset()
626{
627 // The GARD partition is currently misspelled "GUARD." This file path will
628 // need to be updated in the future.
629 auto path = fs::path(PNOR_PRSV_ACTIVE_PATH);
630 path /= "GUARD";
631 std::vector<uint8_t> mboxdArgs;
632
633 auto dbusCall = bus.new_method_call(
634 MBOXD_INTERFACE,
635 MBOXD_PATH,
636 MBOXD_INTERFACE,
637 "cmd");
638
639 // Suspend mboxd - no args required.
640 dbusCall.append(static_cast<uint8_t>(3), mboxdArgs);
641
642 auto responseMsg = bus.call(dbusCall);
643 if (responseMsg.is_method_error())
644 {
645 log<level::ERR>("Error in mboxd suspend call");
646 elog<InternalFailure>();
647 }
648
649 if (fs::is_regular_file(path))
650 {
651 fs::remove(path);
652 }
653
654 dbusCall = bus.new_method_call(
655 MBOXD_INTERFACE,
656 MBOXD_PATH,
657 MBOXD_INTERFACE,
658 "cmd");
659
660 // Resume mboxd with arg 1, indicating that the flash is modified.
661 mboxdArgs.push_back(1);
662 dbusCall.append(static_cast<uint8_t>(4), mboxdArgs);
663
664 responseMsg = bus.call(dbusCall);
665 if (responseMsg.is_method_error())
666 {
667 log<level::ERR>("Error in mboxd resume call");
668 elog<InternalFailure>();
669 }
670}
671
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -0500672} // namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500673} // namespace software
674} // namespace openpower