blob: 7692293af930894f1901dbecd05f19f282f9ad17 [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
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050035void ItemUpdater::createActivation(sdbusplus::message::message& m)
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050036{
Patrick Williamse4290942017-06-16 05:43:08 -050037 using SVersion = server::Version;
38 using VersionPurpose = SVersion::VersionPurpose;
39 namespace msg = sdbusplus::message;
40 namespace variant_ns = msg::variant_ns;
41
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050042 sdbusplus::message::object_path objPath;
43 std::map<std::string,
Patrick Williamse4290942017-06-16 05:43:08 -050044 std::map<std::string, msg::variant<std::string>>> interfaces;
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050045 m.read(objPath, interfaces);
Patrick Williamse4290942017-06-16 05:43:08 -050046
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050047 std::string path(std::move(objPath));
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050048 std::string filePath;
Patrick Williamse4290942017-06-16 05:43:08 -050049 auto purpose = VersionPurpose::Unknown;
Saqib Khance148702017-06-11 12:01:58 -050050 std::string version;
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050051
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050052 for (const auto& intf : interfaces)
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050053 {
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050054 if (intf.first == VERSION_IFACE)
55 {
56 for (const auto& property : intf.second)
57 {
58 if (property.first == "Purpose")
59 {
60 // Only process the Host and System images
Patrick Williamse4290942017-06-16 05:43:08 -050061 auto value = SVersion::convertVersionPurposeFromString(
62 variant_ns::get<std::string>(property.second));
63
64 if (value == VersionPurpose::Host ||
65 value == VersionPurpose::System)
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050066 {
Saqib Khance148702017-06-11 12:01:58 -050067 purpose = value;
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050068 }
69 }
Saqib Khance148702017-06-11 12:01:58 -050070 else if (property.first == "Version")
71 {
Patrick Williamse4290942017-06-16 05:43:08 -050072 version = variant_ns::get<std::string>(property.second);
Saqib Khance148702017-06-11 12:01:58 -050073 }
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050074 }
75 }
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050076 else if (intf.first == FILEPATH_IFACE)
77 {
78 for (const auto& property : intf.second)
79 {
80 if (property.first == "Path")
81 {
Patrick Williamse4290942017-06-16 05:43:08 -050082 filePath = variant_ns::get<std::string>(property.second);
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050083 }
84 }
85 }
86 }
Patrick Williamse4290942017-06-16 05:43:08 -050087 if ((filePath.empty()) || (purpose == VersionPurpose::Unknown))
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050088 {
89 return;
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050090 }
91
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050092 // Version id is the last item in the path
93 auto pos = path.rfind("/");
94 if (pos == std::string::npos)
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050095 {
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050096 log<level::ERR>("No version id found in object path",
97 entry("OBJPATH=%s", path));
98 return;
99 }
100
101 auto versionId = path.substr(pos + 1);
102
103 if (activations.find(versionId) == activations.end())
104 {
105 // Determine the Activation state by processing the given image dir.
106 auto activationState = server::Activation::Activations::Invalid;
Gunnar Mills3588acc2017-09-07 13:13:22 -0500107 AssociationList associations = {};
Adriana Kobylak5ba6b102017-05-19 09:41:27 -0500108 if (ItemUpdater::validateSquashFSImage(filePath) == 0)
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -0500109 {
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500110 activationState = server::Activation::Activations::Ready;
Gunnar Mills3588acc2017-09-07 13:13:22 -0500111 // Create an association to the host inventory item
112 associations.emplace_back(std::make_tuple(
113 ACTIVATION_FWD_ASSOCIATION,
114 ACTIVATION_REV_ASSOCIATION,
115 HOST_INVENTORY_PATH));
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -0500116 }
117
Adriana Kobylak5ba6b102017-05-19 09:41:27 -0500118 fs::path manifestPath(filePath);
119 manifestPath /= MANIFEST_FILE;
Saqib Khan167601b2017-06-18 23:33:46 -0500120 std::string extendedVersion = (Version::getValue(manifestPath.string(),
121 std::map<std::string, std::string>
122 {{"extended_version", ""}})).begin()->second;
Gunnar Mills3edb84b2017-08-18 15:13:15 -0500123
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500124 activations.insert(std::make_pair(
125 versionId,
126 std::make_unique<Activation>(
127 bus,
128 path,
Saqib Khan81bac882017-06-08 12:17:01 -0500129 *this,
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500130 versionId,
131 extendedVersion,
Gunnar Mills3edb84b2017-08-18 15:13:15 -0500132 activationState,
133 associations)));
Saqib Khance148702017-06-11 12:01:58 -0500134 versions.insert(std::make_pair(
135 versionId,
136 std::make_unique<Version>(
137 bus,
138 path,
139 version,
140 purpose,
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500141 filePath,
142 *this)));
Saqib Khan00044f42017-07-10 17:24:43 -0500143 }
Saqib Khan3fb2d172017-08-07 12:14:03 -0500144 else
145 {
146 log<level::INFO>("Software Object with the same version already exists",
Gunnar Mills3fa70282017-08-18 15:30:42 -0500147 entry("VERSION_ID=%s", versionId));
Saqib Khan3fb2d172017-08-07 12:14:03 -0500148 }
Patrick Williams3accb322017-05-30 16:29:52 -0500149 return;
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500150}
151
Saqib Khan167601b2017-06-18 23:33:46 -0500152void ItemUpdater::processPNORImage()
Saqib Khan7254f0e2017-04-10 21:45:37 -0500153{
Saqib Khan4c5d7442017-07-18 00:43:52 -0500154 // Read pnor.toc from folders under /media/
155 // to get Active Software Versions.
Gunnar Mills3fa70282017-08-18 15:30:42 -0500156 for (const auto& iter : fs::directory_iterator(MEDIA_DIR))
Saqib Khan4c5d7442017-07-18 00:43:52 -0500157 {
158 auto activationState = server::Activation::Activations::Active;
159
160 static const auto PNOR_RO_PREFIX_LEN = strlen(PNOR_RO_PREFIX);
Saqib Khan2be9ba92017-09-26 22:44:10 -0500161 static const auto PNOR_RW_PREFIX_LEN = strlen(PNOR_RW_PREFIX);
Saqib Khan4c5d7442017-07-18 00:43:52 -0500162
163 // Check if the PNOR_RO_PREFIX is the prefix of the iter.path
164 if (0 == iter.path().native().compare(0, PNOR_RO_PREFIX_LEN,
165 PNOR_RO_PREFIX))
166 {
Saqib Khan6a522262017-09-26 12:02:50 -0500167 // The versionId is extracted from the path
168 // for example /media/pnor-ro-2a1022fe.
169 auto id = iter.path().native().substr(PNOR_RO_PREFIX_LEN);
Saqib Khan4c5d7442017-07-18 00:43:52 -0500170 auto pnorTOC = iter.path() / PNOR_TOC_FILE;
171 if (!fs::is_regular_file(pnorTOC))
172 {
Saqib Khan6a522262017-09-26 12:02:50 -0500173 log<level::ERR>("Failed to read pnorTOC.\n",
Saqib Khan4c5d7442017-07-18 00:43:52 -0500174 entry("FileName=%s", pnorTOC.string()));
Saqib Khan6a522262017-09-26 12:02:50 -0500175 ItemUpdater::erase(id);
176 continue;
Saqib Khan4c5d7442017-07-18 00:43:52 -0500177 }
178 auto keyValues =
179 Version::getValue(pnorTOC,
180 {{ "version", "" },
181 { "extended_version", "" } });
182 auto& version = keyValues.at("version");
183 if (version.empty())
184 {
185 log<level::ERR>("Failed to read version from pnorTOC",
186 entry("FILENAME=%s", pnorTOC.string()));
187 activationState = server::Activation::Activations::Invalid;
188 }
189
190 auto& extendedVersion = keyValues.at("extended_version");
191 if (extendedVersion.empty())
192 {
193 log<level::ERR>("Failed to read extendedVersion from pnorTOC",
194 entry("FILENAME=%s", pnorTOC.string()));
195 activationState = server::Activation::Activations::Invalid;
196 }
197
Saqib Khan4c5d7442017-07-18 00:43:52 -0500198 auto purpose = server::Version::VersionPurpose::Host;
199 auto path = fs::path(SOFTWARE_OBJPATH) / id;
Gunnar Mills3588acc2017-09-07 13:13:22 -0500200 AssociationList associations = {};
Saqib Khan4c5d7442017-07-18 00:43:52 -0500201
Gunnar Mills3588acc2017-09-07 13:13:22 -0500202 if (activationState == server::Activation::Activations::Active)
203 {
204 // Create an association to the host inventory item
205 associations.emplace_back(std::make_tuple(
206 ACTIVATION_FWD_ASSOCIATION,
207 ACTIVATION_REV_ASSOCIATION,
208 HOST_INVENTORY_PATH));
Gunnar Mills3edb84b2017-08-18 15:13:15 -0500209
Gunnar Mills3588acc2017-09-07 13:13:22 -0500210 // Create an active association since this image is active
211 createActiveAssociation(path);
212 }
Gunnar Mills3edb84b2017-08-18 15:13:15 -0500213
Saqib Khan4c5d7442017-07-18 00:43:52 -0500214 // Create Activation instance for this version.
215 activations.insert(std::make_pair(
216 id,
217 std::make_unique<Activation>(
218 bus,
219 path,
220 *this,
221 id,
222 extendedVersion,
Gunnar Mills3edb84b2017-08-18 15:13:15 -0500223 activationState,
224 associations)));
Saqib Khan4c5d7442017-07-18 00:43:52 -0500225
226 // If Active, create RedundancyPriority instance for this version.
227 if (activationState == server::Activation::Activations::Active)
228 {
Michael Tritz36417922017-08-04 14:00:29 -0500229 uint8_t priority = std::numeric_limits<uint8_t>::max();
230 if (!restoreFromFile(id, priority))
Saqib Khan4c5d7442017-07-18 00:43:52 -0500231 {
Michael Tritz36417922017-08-04 14:00:29 -0500232 log<level::ERR>("Unable to restore priority from file.",
Gunnar Mills3fa70282017-08-18 15:30:42 -0500233 entry("VERSIONID=%s", id));
Saqib Khan4c5d7442017-07-18 00:43:52 -0500234 }
Michael Tritz36417922017-08-04 14:00:29 -0500235 activations.find(id)->second->redundancyPriority =
236 std::make_unique<RedundancyPriority>(
237 bus,
238 path,
239 *(activations.find(id)->second),
240 priority);
Saqib Khan4c5d7442017-07-18 00:43:52 -0500241 }
242
243 // Create Version instance for this version.
244 versions.insert(std::make_pair(
245 id,
246 std::make_unique<Version>(
247 bus,
248 path,
249 version,
250 purpose,
251 "",
252 *this)));
253 }
Saqib Khan2be9ba92017-09-26 22:44:10 -0500254 else if (0 == iter.path().native().compare(0, PNOR_RW_PREFIX_LEN,
255 PNOR_RW_PREFIX))
256 {
257 auto id = iter.path().native().substr(PNOR_RW_PREFIX_LEN);
258 auto roDir = PNOR_RO_PREFIX + id;
259 if (!fs::is_directory(roDir))
260 {
261 log<level::ERR>("No corresponding read-only volume found.",
262 entry("DIRNAME=%s", roDir));
263 ItemUpdater::erase(id);
264 }
265 }
Saqib Khan4c5d7442017-07-18 00:43:52 -0500266 }
Gunnar Mills2badd7a2017-09-20 12:51:28 -0500267
268 // Look at the RO symlink to determine if there is a functional image
269 auto id = determineId(PNOR_RO_ACTIVE_PATH);
270 if (!id.empty())
271 {
272 updateFunctionalAssociation(std::string{SOFTWARE_OBJPATH} + '/' + id);
273 }
Saqib Khan167601b2017-06-18 23:33:46 -0500274 return;
Saqib Khan7254f0e2017-04-10 21:45:37 -0500275}
276
Adriana Kobylak5ba6b102017-05-19 09:41:27 -0500277int ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khana8ade7e2017-04-12 10:27:56 -0500278{
Saqib Khan4c5d7442017-07-18 00:43:52 -0500279 auto file = fs::path(filePath) / squashFSImage;
280 if (fs::is_regular_file(file))
Saqib Khana8ade7e2017-04-12 10:27:56 -0500281 {
282 return 0;
283 }
284 else
285 {
286 log<level::ERR>("Failed to find the SquashFS image.");
287 return -1;
288 }
289}
290
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500291void ItemUpdater::removeReadOnlyPartition(std::string versionId)
Michael Tritzdd961b62017-05-17 14:07:03 -0500292{
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500293 auto serviceFile = "obmc-flash-bios-ubiumount-ro@" + versionId +
294 ".service";
295
296 // Remove the read-only partitions.
297 auto method = bus.new_method_call(
298 SYSTEMD_BUSNAME,
299 SYSTEMD_PATH,
300 SYSTEMD_INTERFACE,
301 "StartUnit");
302 method.append(serviceFile, "replace");
303 bus.call_noreply(method);
304}
305
306void ItemUpdater::removeReadWritePartition(std::string versionId)
307{
308 auto serviceFile = "obmc-flash-bios-ubiumount-rw@" + versionId +
Michael Tritzdd961b62017-05-17 14:07:03 -0500309 ".service";
310
311 // Remove the read-write partitions.
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500312 auto method = bus.new_method_call(
Michael Tritzdd961b62017-05-17 14:07:03 -0500313 SYSTEMD_BUSNAME,
314 SYSTEMD_PATH,
315 SYSTEMD_INTERFACE,
316 "StartUnit");
317 method.append(serviceFile, "replace");
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500318 bus.call_noreply(method);
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500319}
Michael Tritzdd961b62017-05-17 14:07:03 -0500320
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500321void ItemUpdater::removePreservedPartition()
322{
Michael Tritzdd961b62017-05-17 14:07:03 -0500323 // Remove the preserved partition.
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500324 auto method = bus.new_method_call(
Michael Tritzdd961b62017-05-17 14:07:03 -0500325 SYSTEMD_BUSNAME,
326 SYSTEMD_PATH,
327 SYSTEMD_INTERFACE,
328 "StartUnit");
329 method.append("obmc-flash-bios-ubiumount-prsv.service", "replace");
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500330 bus.call_noreply(method);
Michael Tritzdd961b62017-05-17 14:07:03 -0500331
332 return;
333}
334
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500335void ItemUpdater::reset()
336{
Gunnar Mills3fa70282017-08-18 15:30:42 -0500337 for (const auto& it : activations)
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500338 {
339 removeReadWritePartition(it.first);
Michael Tritz60bc20f2017-07-29 23:32:21 -0500340 removeFile(it.first);
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500341 }
342 removePreservedPartition();
343 return;
344}
345
Eddie James13fc66a2017-08-31 15:36:44 -0500346bool ItemUpdater::isVersionFunctional(std::string versionId)
347{
348 if (!fs::exists(PNOR_RO_ACTIVE_PATH))
349 {
350 return false;
351 }
352
353 fs::path activeRO = fs::read_symlink(PNOR_RO_ACTIVE_PATH);
354
355 if (!fs::is_directory(activeRO))
356 {
357 return false;
358 }
359
360 if (activeRO.string().find(versionId) == std::string::npos)
361 {
362 return false;
363 }
364
365 // active PNOR is the version we're checking
366 return true;
367}
368
369bool ItemUpdater::isChassisOn()
370{
371 auto mapperCall = bus.new_method_call(
372 MAPPER_BUSNAME,
373 MAPPER_PATH,
374 MAPPER_INTERFACE,
375 "GetObject");
376
377 mapperCall.append(CHASSIS_STATE_PATH,
378 std::vector<std::string>({CHASSIS_STATE_OBJ}));
379 auto mapperResponseMsg = bus.call(mapperCall);
380 if (mapperResponseMsg.is_method_error())
381 {
382 log<level::ERR>("Error in Mapper call");
383 elog<InternalFailure>();
384 }
385 using MapperResponseType = std::map<std::string, std::vector<std::string>>;
386 MapperResponseType mapperResponse;
387 mapperResponseMsg.read(mapperResponse);
388 if (mapperResponse.empty())
389 {
390 log<level::ERR>("Invalid Response from mapper");
391 elog<InternalFailure>();
392 }
393
394 auto method = bus.new_method_call((mapperResponse.begin()->first).c_str(),
395 CHASSIS_STATE_PATH,
396 SYSTEMD_PROPERTY_INTERFACE,
397 "Get");
398 method.append(CHASSIS_STATE_OBJ, "CurrentPowerState");
399 auto response = bus.call(method);
400 if (response.is_method_error())
401 {
402 log<level::ERR>("Error in fetching current Chassis State",
403 entry("MapperResponse=%s",
404 (mapperResponse.begin()->first).c_str()));
405 elog<InternalFailure>();
406 }
407 sdbusplus::message::variant<std::string> currentChassisState;
408 response.read(currentChassisState);
409 auto strParam =
410 sdbusplus::message::variant_ns::get<std::string>(currentChassisState);
411 return (strParam != CHASSIS_STATE_OFF);
412}
413
Saqib Khanb8e7f312017-08-12 10:24:10 -0500414void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan81bac882017-06-08 12:17:01 -0500415{
416 //TODO openbmc/openbmc#1896 Improve the performance of this function
417 for (const auto& intf : activations)
418 {
Gunnar Mills3fa70282017-08-18 15:30:42 -0500419 if (intf.second->redundancyPriority)
Saqib Khan81bac882017-06-08 12:17:01 -0500420 {
Saqib Khanb8e7f312017-08-12 10:24:10 -0500421 if (intf.second->redundancyPriority.get()->priority() == value &&
422 intf.second->versionId != versionId)
Saqib Khan81bac882017-06-08 12:17:01 -0500423 {
Gunnar Mills3fa70282017-08-18 15:30:42 -0500424 intf.second->redundancyPriority.get()->priority(value + 1);
Saqib Khan81bac882017-06-08 12:17:01 -0500425 }
426 }
427 }
428}
429
Saqib Khan2af5c492017-07-17 16:15:13 -0500430bool ItemUpdater::isLowestPriority(uint8_t value)
431{
432 for (const auto& intf : activations)
433 {
Gunnar Mills3fa70282017-08-18 15:30:42 -0500434 if (intf.second->redundancyPriority)
Saqib Khan2af5c492017-07-17 16:15:13 -0500435 {
436 if (intf.second->redundancyPriority.get()->priority() < value)
437 {
438 return false;
439 }
440 }
441 }
442 return true;
443}
444
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500445void ItemUpdater::erase(std::string entryId)
446{
Eddie James13fc66a2017-08-31 15:36:44 -0500447 if (isVersionFunctional(entryId) && isChassisOn()) {
448 log<level::ERR>(("Error: Version " + entryId + \
449 " is currently active and running on the host." \
450 " Unable to remove.").c_str());
451 return;
452 }
Saqib Khanef8cd9f2017-08-16 14:20:30 -0500453 // Remove priority persistence file
454 removeFile(entryId);
455
Saqib Khan1e0aa5c2017-08-31 11:04:17 -0500456 // Removing read-only and read-write partitions
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500457 removeReadWritePartition(entryId);
458 removeReadOnlyPartition(entryId);
459
460 // Removing entry in versions map
461 auto it = versions.find(entryId);
462 if (it == versions.end())
463 {
464 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills3fa70282017-08-18 15:30:42 -0500465 " in item updater versions map." \
466 " Unable to remove.").c_str());
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500467 return;
468 }
469 versions.erase(entryId);
470
471 // Removing entry in activations map
472 auto ita = activations.find(entryId);
473 if (ita == activations.end())
474 {
475 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills3fa70282017-08-18 15:30:42 -0500476 " in item updater activations map." \
477 " Unable to remove.").c_str());
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500478 return;
479 }
480 activations.erase(entryId);
481}
482
Michael Tritz234a07e2017-09-21 00:53:06 -0500483void ItemUpdater::deleteAll()
484{
485 std::vector<std::string> deletableActivations;
486
487 for (const auto& activationIt : activations)
488 {
489 if (!isVersionFunctional(activationIt.first))
490 {
491 deletableActivations.push_back(activationIt.first);
492 }
493 }
494
495 for (const auto& deletableIt : deletableActivations)
496 {
497 ItemUpdater::erase(deletableIt);
498 }
499
500 // Remove any remaining pnor-ro- or pnor-rw- volumes that do not match
501 // the current version.
502 auto method = bus.new_method_call(
503 SYSTEMD_BUSNAME,
504 SYSTEMD_PATH,
505 SYSTEMD_INTERFACE,
506 "StartUnit");
507 method.append("obmc-flash-bios-cleanup.service", "replace");
508 bus.call_noreply(method);
509}
510
Saqib Khan2cbfa032017-08-17 14:52:37 -0500511// TODO: openbmc/openbmc#1402 Monitor flash usage
512void ItemUpdater::freeSpace()
513{
514 std::size_t count = 0;
515 decltype(activations.begin()->second->redundancyPriority.get()->priority())
516 highestPriority = 0;
517 decltype(activations.begin()->second->versionId) highestPriorityVersion;
518 for (const auto& iter : activations)
519 {
520 if (iter.second.get()->activation() == server::Activation::Activations::Active)
521 {
522 count++;
523 if (iter.second->redundancyPriority.get()->priority() > highestPriority)
524 {
525 highestPriority = iter.second->redundancyPriority.get()->priority();
526 highestPriorityVersion = iter.second->versionId;
527 }
528 }
529 }
530 // Remove the pnor version with highest priority since the PNOR
531 // can't hold more than 2 versions.
532 if (count >= ACTIVE_PNOR_MAX_ALLOWED)
533 {
534 erase(highestPriorityVersion);
535 }
536}
537
Gunnar Mills61010b22017-09-20 15:25:26 -0500538void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Mills9741cd12017-08-28 15:09:00 -0500539{
540 assocs.emplace_back(std::make_tuple(ACTIVE_FWD_ASSOCIATION,
541 ACTIVE_REV_ASSOCIATION,
542 path));
543 associations(assocs);
544}
545
Gunnar Mills833e4f32017-09-14 12:30:27 -0500546void ItemUpdater::updateFunctionalAssociation(const std::string& path)
547{
548 // remove all functional associations
549 for (auto iter = assocs.begin(); iter != assocs.end();)
550 {
551 if ((std::get<0>(*iter)).compare(FUNCTIONAL_FWD_ASSOCIATION) == 0)
552 {
553 iter = assocs.erase(iter);
554 }
555 else
556 {
557 ++iter;
558 }
559 }
560 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
561 FUNCTIONAL_REV_ASSOCIATION,
562 path));
563 associations(assocs);
564}
565
Gunnar Mills61010b22017-09-20 15:25:26 -0500566void ItemUpdater::removeActiveAssociation(const std::string& path)
Gunnar Mills9741cd12017-08-28 15:09:00 -0500567{
568 for (auto iter = assocs.begin(); iter != assocs.end();)
569 {
Gunnar Mills833e4f32017-09-14 12:30:27 -0500570 if ((std::get<0>(*iter)).compare(ACTIVE_FWD_ASSOCIATION) == 0 &&
571 (std::get<2>(*iter)).compare(path) == 0)
Gunnar Mills9741cd12017-08-28 15:09:00 -0500572 {
573 iter = assocs.erase(iter);
574 associations(assocs);
575 }
576 else
577 {
578 ++iter;
579 }
580 }
581}
582
Gunnar Mills2badd7a2017-09-20 12:51:28 -0500583std::string ItemUpdater::determineId(const std::string& symlinkPath)
584{
585 if (!fs::exists(symlinkPath))
586 {
587 return {};
588 }
589
590 auto target = fs::canonical(symlinkPath).string();
591
592 // check to make sure the target really exists
593 if (!fs::is_regular_file(target + "/" + PNOR_TOC_FILE))
594 {
595 return {};
596 }
597 // Get the image <id> from the symlink target
598 // for example /media/ro-2a1022fe
599 static const auto PNOR_RO_PREFIX_LEN = strlen(PNOR_RO_PREFIX);
600 return target.substr(PNOR_RO_PREFIX_LEN);
601}
602
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -0500603} // namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500604} // namespace software
605} // namespace openpower