blob: adc6f7cab730c57435cde300d95da8e57ff89c99 [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 {
336 elog<InternalFailure>();
337 }
338
339 removeFile(it.first);
340 }
341 // Clear the preserved partition.
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500342 auto method = bus.new_method_call(
Michael Tritzdd961b62017-05-17 14:07:03 -0500343 SYSTEMD_BUSNAME,
344 SYSTEMD_PATH,
345 SYSTEMD_INTERFACE,
346 "StartUnit");
Michael Tritzfa7aa122017-09-22 15:16:11 -0500347 method.append("obmc-flash-bios-ubiclear@pnor-prsv.service", "replace");
348 auto reply = bus.call(method);
Michael Tritzdd961b62017-05-17 14:07:03 -0500349
Michael Tritzfa7aa122017-09-22 15:16:11 -0500350 if (reply.is_method_error())
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500351 {
Michael Tritzfa7aa122017-09-22 15:16:11 -0500352 elog<InternalFailure>();
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500353 }
Michael Tritzfa7aa122017-09-22 15:16:11 -0500354
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500355 return;
356}
357
Eddie James13fc66a2017-08-31 15:36:44 -0500358bool ItemUpdater::isVersionFunctional(std::string versionId)
359{
360 if (!fs::exists(PNOR_RO_ACTIVE_PATH))
361 {
362 return false;
363 }
364
365 fs::path activeRO = fs::read_symlink(PNOR_RO_ACTIVE_PATH);
366
367 if (!fs::is_directory(activeRO))
368 {
369 return false;
370 }
371
372 if (activeRO.string().find(versionId) == std::string::npos)
373 {
374 return false;
375 }
376
377 // active PNOR is the version we're checking
378 return true;
379}
380
381bool ItemUpdater::isChassisOn()
382{
383 auto mapperCall = bus.new_method_call(
384 MAPPER_BUSNAME,
385 MAPPER_PATH,
386 MAPPER_INTERFACE,
387 "GetObject");
388
389 mapperCall.append(CHASSIS_STATE_PATH,
390 std::vector<std::string>({CHASSIS_STATE_OBJ}));
391 auto mapperResponseMsg = bus.call(mapperCall);
392 if (mapperResponseMsg.is_method_error())
393 {
394 log<level::ERR>("Error in Mapper call");
395 elog<InternalFailure>();
396 }
397 using MapperResponseType = std::map<std::string, std::vector<std::string>>;
398 MapperResponseType mapperResponse;
399 mapperResponseMsg.read(mapperResponse);
400 if (mapperResponse.empty())
401 {
402 log<level::ERR>("Invalid Response from mapper");
403 elog<InternalFailure>();
404 }
405
406 auto method = bus.new_method_call((mapperResponse.begin()->first).c_str(),
407 CHASSIS_STATE_PATH,
408 SYSTEMD_PROPERTY_INTERFACE,
409 "Get");
410 method.append(CHASSIS_STATE_OBJ, "CurrentPowerState");
411 auto response = bus.call(method);
412 if (response.is_method_error())
413 {
414 log<level::ERR>("Error in fetching current Chassis State",
Gunnar Mills850d5f62017-10-19 17:04:38 -0500415 entry("MAPPERRESPONSE=%s",
Eddie James13fc66a2017-08-31 15:36:44 -0500416 (mapperResponse.begin()->first).c_str()));
417 elog<InternalFailure>();
418 }
419 sdbusplus::message::variant<std::string> currentChassisState;
420 response.read(currentChassisState);
421 auto strParam =
422 sdbusplus::message::variant_ns::get<std::string>(currentChassisState);
423 return (strParam != CHASSIS_STATE_OFF);
424}
425
Saqib Khanb8e7f312017-08-12 10:24:10 -0500426void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan81bac882017-06-08 12:17:01 -0500427{
428 //TODO openbmc/openbmc#1896 Improve the performance of this function
429 for (const auto& intf : activations)
430 {
Gunnar Mills3fa70282017-08-18 15:30:42 -0500431 if (intf.second->redundancyPriority)
Saqib Khan81bac882017-06-08 12:17:01 -0500432 {
Saqib Khanb8e7f312017-08-12 10:24:10 -0500433 if (intf.second->redundancyPriority.get()->priority() == value &&
434 intf.second->versionId != versionId)
Saqib Khan81bac882017-06-08 12:17:01 -0500435 {
Gunnar Mills3fa70282017-08-18 15:30:42 -0500436 intf.second->redundancyPriority.get()->priority(value + 1);
Saqib Khan81bac882017-06-08 12:17:01 -0500437 }
438 }
439 }
440}
441
Saqib Khan2af5c492017-07-17 16:15:13 -0500442bool ItemUpdater::isLowestPriority(uint8_t value)
443{
444 for (const auto& intf : activations)
445 {
Gunnar Mills3fa70282017-08-18 15:30:42 -0500446 if (intf.second->redundancyPriority)
Saqib Khan2af5c492017-07-17 16:15:13 -0500447 {
448 if (intf.second->redundancyPriority.get()->priority() < value)
449 {
450 return false;
451 }
452 }
453 }
454 return true;
455}
456
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500457void ItemUpdater::erase(std::string entryId)
458{
Eddie James13fc66a2017-08-31 15:36:44 -0500459 if (isVersionFunctional(entryId) && isChassisOn()) {
460 log<level::ERR>(("Error: Version " + entryId + \
461 " is currently active and running on the host." \
462 " Unable to remove.").c_str());
463 return;
464 }
Saqib Khanef8cd9f2017-08-16 14:20:30 -0500465 // Remove priority persistence file
466 removeFile(entryId);
467
Saqib Khan1e0aa5c2017-08-31 11:04:17 -0500468 // Removing read-only and read-write partitions
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500469 removeReadWritePartition(entryId);
470 removeReadOnlyPartition(entryId);
471
472 // Removing entry in versions map
473 auto it = versions.find(entryId);
474 if (it == versions.end())
475 {
476 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills3fa70282017-08-18 15:30:42 -0500477 " in item updater versions map." \
478 " Unable to remove.").c_str());
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500479 return;
480 }
481 versions.erase(entryId);
482
483 // Removing entry in activations map
484 auto ita = activations.find(entryId);
485 if (ita == activations.end())
486 {
487 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills3fa70282017-08-18 15:30:42 -0500488 " in item updater activations map." \
489 " Unable to remove.").c_str());
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500490 return;
491 }
492 activations.erase(entryId);
493}
494
Michael Tritz234a07e2017-09-21 00:53:06 -0500495void ItemUpdater::deleteAll()
496{
497 std::vector<std::string> deletableActivations;
498
499 for (const auto& activationIt : activations)
500 {
501 if (!isVersionFunctional(activationIt.first))
502 {
503 deletableActivations.push_back(activationIt.first);
504 }
505 }
506
507 for (const auto& deletableIt : deletableActivations)
508 {
509 ItemUpdater::erase(deletableIt);
510 }
511
512 // Remove any remaining pnor-ro- or pnor-rw- volumes that do not match
513 // the current version.
514 auto method = bus.new_method_call(
515 SYSTEMD_BUSNAME,
516 SYSTEMD_PATH,
517 SYSTEMD_INTERFACE,
518 "StartUnit");
519 method.append("obmc-flash-bios-cleanup.service", "replace");
520 bus.call_noreply(method);
521}
522
Saqib Khan2cbfa032017-08-17 14:52:37 -0500523// TODO: openbmc/openbmc#1402 Monitor flash usage
524void ItemUpdater::freeSpace()
525{
526 std::size_t count = 0;
527 decltype(activations.begin()->second->redundancyPriority.get()->priority())
528 highestPriority = 0;
529 decltype(activations.begin()->second->versionId) highestPriorityVersion;
530 for (const auto& iter : activations)
531 {
532 if (iter.second.get()->activation() == server::Activation::Activations::Active)
533 {
534 count++;
Adriana Kobylakee201a52017-11-09 15:05:04 -0600535 if (isVersionFunctional(iter.second->versionId))
536 {
537 continue;
538 }
539 if (iter.second->redundancyPriority.get()->priority() >= highestPriority)
Saqib Khan2cbfa032017-08-17 14:52:37 -0500540 {
541 highestPriority = iter.second->redundancyPriority.get()->priority();
542 highestPriorityVersion = iter.second->versionId;
543 }
544 }
545 }
546 // Remove the pnor version with highest priority since the PNOR
547 // can't hold more than 2 versions.
548 if (count >= ACTIVE_PNOR_MAX_ALLOWED)
549 {
550 erase(highestPriorityVersion);
551 }
552}
553
Gunnar Mills61010b22017-09-20 15:25:26 -0500554void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Mills9741cd12017-08-28 15:09:00 -0500555{
556 assocs.emplace_back(std::make_tuple(ACTIVE_FWD_ASSOCIATION,
557 ACTIVE_REV_ASSOCIATION,
558 path));
559 associations(assocs);
560}
561
Gunnar Mills833e4f32017-09-14 12:30:27 -0500562void ItemUpdater::updateFunctionalAssociation(const std::string& path)
563{
564 // remove all functional associations
565 for (auto iter = assocs.begin(); iter != assocs.end();)
566 {
567 if ((std::get<0>(*iter)).compare(FUNCTIONAL_FWD_ASSOCIATION) == 0)
568 {
569 iter = assocs.erase(iter);
570 }
571 else
572 {
573 ++iter;
574 }
575 }
576 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
577 FUNCTIONAL_REV_ASSOCIATION,
578 path));
579 associations(assocs);
580}
581
Gunnar Mills61010b22017-09-20 15:25:26 -0500582void ItemUpdater::removeActiveAssociation(const std::string& path)
Gunnar Mills9741cd12017-08-28 15:09:00 -0500583{
584 for (auto iter = assocs.begin(); iter != assocs.end();)
585 {
Gunnar Mills833e4f32017-09-14 12:30:27 -0500586 if ((std::get<0>(*iter)).compare(ACTIVE_FWD_ASSOCIATION) == 0 &&
587 (std::get<2>(*iter)).compare(path) == 0)
Gunnar Mills9741cd12017-08-28 15:09:00 -0500588 {
589 iter = assocs.erase(iter);
590 associations(assocs);
591 }
592 else
593 {
594 ++iter;
595 }
596 }
597}
598
Gunnar Mills2badd7a2017-09-20 12:51:28 -0500599std::string ItemUpdater::determineId(const std::string& symlinkPath)
600{
601 if (!fs::exists(symlinkPath))
602 {
603 return {};
604 }
605
606 auto target = fs::canonical(symlinkPath).string();
607
608 // check to make sure the target really exists
609 if (!fs::is_regular_file(target + "/" + PNOR_TOC_FILE))
610 {
611 return {};
612 }
613 // Get the image <id> from the symlink target
614 // for example /media/ro-2a1022fe
615 static const auto PNOR_RO_PREFIX_LEN = strlen(PNOR_RO_PREFIX);
616 return target.substr(PNOR_RO_PREFIX_LEN);
617}
618
Michael Tritzb541f1b2017-10-15 15:10:21 -0500619void GardReset::reset()
620{
621 // The GARD partition is currently misspelled "GUARD." This file path will
622 // need to be updated in the future.
623 auto path = fs::path(PNOR_PRSV_ACTIVE_PATH);
624 path /= "GUARD";
625 std::vector<uint8_t> mboxdArgs;
626
627 auto dbusCall = bus.new_method_call(
628 MBOXD_INTERFACE,
629 MBOXD_PATH,
630 MBOXD_INTERFACE,
631 "cmd");
632
633 // Suspend mboxd - no args required.
634 dbusCall.append(static_cast<uint8_t>(3), mboxdArgs);
635
636 auto responseMsg = bus.call(dbusCall);
637 if (responseMsg.is_method_error())
638 {
639 log<level::ERR>("Error in mboxd suspend call");
640 elog<InternalFailure>();
641 }
642
643 if (fs::is_regular_file(path))
644 {
645 fs::remove(path);
646 }
647
648 dbusCall = bus.new_method_call(
649 MBOXD_INTERFACE,
650 MBOXD_PATH,
651 MBOXD_INTERFACE,
652 "cmd");
653
654 // Resume mboxd with arg 1, indicating that the flash is modified.
655 mboxdArgs.push_back(1);
656 dbusCall.append(static_cast<uint8_t>(4), mboxdArgs);
657
658 responseMsg = bus.call(dbusCall);
659 if (responseMsg.is_method_error())
660 {
661 log<level::ERR>("Error in mboxd resume call");
662 elog<InternalFailure>();
663 }
664}
665
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -0500666} // namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500667} // namespace software
668} // namespace openpower