Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "item_updater.hpp" |
| 4 | |
| 5 | #include "images.hpp" |
| 6 | #include "serialize.hpp" |
| 7 | #include "version.hpp" |
| 8 | #include "xyz/openbmc_project/Software/Version/server.hpp" |
| 9 | |
| 10 | #include <elog-errors.hpp> |
| 11 | #include <experimental/filesystem> |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 12 | #include <fstream> |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 13 | #include <phosphor-logging/elog.hpp> |
| 14 | #include <phosphor-logging/log.hpp> |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 15 | #include <queue> |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 16 | #include <set> |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 17 | #include <string> |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 18 | #include <xyz/openbmc_project/Software/Version/error.hpp> |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 19 | |
| 20 | namespace phosphor |
| 21 | { |
| 22 | namespace software |
| 23 | { |
| 24 | namespace updater |
| 25 | { |
| 26 | |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 27 | // When you see server:: you know we're referencing our base class |
| 28 | namespace server = sdbusplus::xyz::openbmc_project::Software::server; |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 29 | namespace control = sdbusplus::xyz::openbmc_project::Control::server; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 30 | |
| 31 | using namespace phosphor::logging; |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 32 | using namespace sdbusplus::xyz::openbmc_project::Software::Version::Error; |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 33 | using namespace phosphor::software::image; |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 34 | namespace fs = std::experimental::filesystem; |
| 35 | |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 36 | void ItemUpdater::createActivation(sdbusplus::message::message& msg) |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 37 | { |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 38 | |
| 39 | using SVersion = server::Version; |
| 40 | using VersionPurpose = SVersion::VersionPurpose; |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 41 | using VersionClass = phosphor::software::manager::Version; |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 42 | namespace mesg = sdbusplus::message; |
| 43 | namespace variant_ns = mesg::variant_ns; |
| 44 | |
| 45 | mesg::object_path objPath; |
| 46 | auto purpose = VersionPurpose::Unknown; |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 47 | std::string version; |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 48 | std::map<std::string, std::map<std::string, mesg::variant<std::string>>> |
| 49 | interfaces; |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 50 | msg.read(objPath, interfaces); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 51 | std::string path(std::move(objPath)); |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 52 | std::string filePath; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 53 | |
| 54 | for (const auto& intf : interfaces) |
| 55 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 56 | if (intf.first == VERSION_IFACE) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 57 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 58 | for (const auto& property : intf.second) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 59 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 60 | if (property.first == "Purpose") |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 61 | { |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 62 | auto value = SVersion::convertVersionPurposeFromString( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 63 | variant_ns::get<std::string>(property.second)); |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 64 | if (value == VersionPurpose::BMC || |
| 65 | value == VersionPurpose::System) |
| 66 | { |
| 67 | purpose = value; |
| 68 | } |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 69 | } |
| 70 | else if (property.first == "Version") |
| 71 | { |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 72 | version = variant_ns::get<std::string>(property.second); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | } |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 76 | else if (intf.first == FILEPATH_IFACE) |
| 77 | { |
| 78 | for (const auto& property : intf.second) |
| 79 | { |
| 80 | if (property.first == "Path") |
| 81 | { |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 82 | filePath = variant_ns::get<std::string>(property.second); |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 86 | } |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 87 | if (version.empty() || filePath.empty() || |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 88 | purpose == VersionPurpose::Unknown) |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 89 | { |
| 90 | return; |
| 91 | } |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 92 | |
| 93 | // Version id is the last item in the path |
| 94 | auto pos = path.rfind("/"); |
| 95 | if (pos == std::string::npos) |
| 96 | { |
| 97 | log<level::ERR>("No version id found in object path", |
Adriana Kobylak | 596466b | 2018-02-13 14:48:53 -0600 | [diff] [blame] | 98 | entry("OBJPATH=%s", path.c_str())); |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 99 | return; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | auto versionId = path.substr(pos + 1); |
| 103 | |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 104 | if (activations.find(versionId) == activations.end()) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 105 | { |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 106 | // Determine the Activation state by processing the given image dir. |
| 107 | auto activationState = server::Activation::Activations::Invalid; |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 108 | ItemUpdater::ActivationStatus result = |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 109 | ItemUpdater::validateSquashFSImage(filePath); |
Gunnar Mills | 43b25cd | 2017-09-07 13:19:34 -0500 | [diff] [blame] | 110 | AssociationList associations = {}; |
| 111 | |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 112 | if (result == ItemUpdater::ActivationStatus::ready) |
| 113 | { |
| 114 | activationState = server::Activation::Activations::Ready; |
Gunnar Mills | 43b25cd | 2017-09-07 13:19:34 -0500 | [diff] [blame] | 115 | // Create an association to the BMC inventory item |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 116 | associations.emplace_back( |
| 117 | std::make_tuple(ACTIVATION_FWD_ASSOCIATION, |
| 118 | ACTIVATION_REV_ASSOCIATION, bmcInventoryPath)); |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 119 | } |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 120 | |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 121 | activations.insert(std::make_pair( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 122 | versionId, |
| 123 | std::make_unique<Activation>(bus, path, *this, versionId, |
| 124 | activationState, associations))); |
Michael Tritz | 4254bec | 2017-10-03 17:18:22 -0500 | [diff] [blame] | 125 | |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 126 | auto versionPtr = std::make_unique<VersionClass>( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 127 | bus, path, version, purpose, filePath, |
| 128 | std::bind(&ItemUpdater::erase, this, std::placeholders::_1)); |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 129 | versionPtr->deleteObject = |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 130 | std::make_unique<phosphor::software::manager::Delete>(bus, path, |
| 131 | *versionPtr); |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 132 | versions.insert(std::make_pair(versionId, std::move(versionPtr))); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 133 | } |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 134 | return; |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 137 | void ItemUpdater::processBMCImage() |
| 138 | { |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 139 | using VersionClass = phosphor::software::manager::Version; |
Lei YU | 269bff3 | 2018-08-21 15:21:40 +0800 | [diff] [blame] | 140 | |
| 141 | // Check MEDIA_DIR and create if it does not exist |
| 142 | try |
| 143 | { |
| 144 | if (!fs::is_directory(MEDIA_DIR)) |
| 145 | { |
| 146 | fs::create_directory(MEDIA_DIR); |
| 147 | } |
| 148 | } |
| 149 | catch (const fs::filesystem_error& e) |
| 150 | { |
| 151 | log<level::ERR>("Failed to prepare dir", entry("ERR=%s", e.what())); |
| 152 | return; |
| 153 | } |
| 154 | |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 155 | // Read os-release from /etc/ to get the functional BMC version |
| 156 | auto functionalVersion = VersionClass::getBMCVersion(OS_RELEASE_FILE); |
| 157 | |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 158 | // Read os-release from folders under /media/ to get |
| 159 | // BMC Software Versions. |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 160 | for (const auto& iter : fs::directory_iterator(MEDIA_DIR)) |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 161 | { |
| 162 | auto activationState = server::Activation::Activations::Active; |
Saqib Khan | 6fab70d | 2017-09-07 00:13:50 -0500 | [diff] [blame] | 163 | static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 164 | |
| 165 | // Check if the BMC_RO_PREFIXis the prefix of the iter.path |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 166 | if (0 == |
| 167 | iter.path().native().compare(0, BMC_RO_PREFIX_LEN, BMC_ROFS_PREFIX)) |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 168 | { |
Saqib Khan | 021c365 | 2017-09-26 12:11:02 -0500 | [diff] [blame] | 169 | // The versionId is extracted from the path |
| 170 | // for example /media/ro-2a1022fe. |
| 171 | auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 172 | auto osRelease = iter.path() / OS_RELEASE_FILE; |
| 173 | if (!fs::is_regular_file(osRelease)) |
| 174 | { |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 175 | log<level::ERR>( |
| 176 | "Failed to read osRelease", |
| 177 | entry("FILENAME=%s", osRelease.string().c_str())); |
Saqib Khan | 021c365 | 2017-09-26 12:11:02 -0500 | [diff] [blame] | 178 | ItemUpdater::erase(id); |
| 179 | continue; |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 180 | } |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 181 | auto version = VersionClass::getBMCVersion(osRelease); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 182 | if (version.empty()) |
| 183 | { |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 184 | log<level::ERR>( |
| 185 | "Failed to read version from osRelease", |
| 186 | entry("FILENAME=%s", osRelease.string().c_str())); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 187 | activationState = server::Activation::Activations::Invalid; |
| 188 | } |
Saqib Khan | 021c365 | 2017-09-26 12:11:02 -0500 | [diff] [blame] | 189 | |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 190 | auto purpose = server::Version::VersionPurpose::BMC; |
| 191 | auto path = fs::path(SOFTWARE_OBJPATH) / id; |
| 192 | |
Lei YU | 269bff3 | 2018-08-21 15:21:40 +0800 | [diff] [blame] | 193 | // Create functional association if this is the functional |
| 194 | // version |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 195 | if (version.compare(functionalVersion) == 0) |
| 196 | { |
| 197 | createFunctionalAssociation(path); |
| 198 | } |
| 199 | |
Gunnar Mills | 43b25cd | 2017-09-07 13:19:34 -0500 | [diff] [blame] | 200 | AssociationList associations = {}; |
| 201 | |
| 202 | if (activationState == server::Activation::Activations::Active) |
| 203 | { |
| 204 | // Create an association to the BMC inventory item |
| 205 | associations.emplace_back(std::make_tuple( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 206 | ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION, |
| 207 | bmcInventoryPath)); |
Gunnar Mills | 43b25cd | 2017-09-07 13:19:34 -0500 | [diff] [blame] | 208 | |
| 209 | // Create an active association since this image is active |
| 210 | createActiveAssociation(path); |
| 211 | } |
| 212 | |
Adriana Kobylak | ee590c7 | 2017-09-26 15:16:06 -0500 | [diff] [blame] | 213 | // Create Version instance for this version. |
| 214 | auto versionPtr = std::make_unique<VersionClass>( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 215 | bus, path, version, purpose, "", |
| 216 | std::bind(&ItemUpdater::erase, this, std::placeholders::_1)); |
Adriana Kobylak | ee590c7 | 2017-09-26 15:16:06 -0500 | [diff] [blame] | 217 | auto isVersionFunctional = versionPtr->isFunctional(); |
Michael Tritz | 4254bec | 2017-10-03 17:18:22 -0500 | [diff] [blame] | 218 | if (!isVersionFunctional) |
| 219 | { |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 220 | versionPtr->deleteObject = |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 221 | std::make_unique<phosphor::software::manager::Delete>( |
| 222 | bus, path, *versionPtr); |
Michael Tritz | 4254bec | 2017-10-03 17:18:22 -0500 | [diff] [blame] | 223 | } |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 224 | versions.insert(std::make_pair(id, std::move(versionPtr))); |
Michael Tritz | 4254bec | 2017-10-03 17:18:22 -0500 | [diff] [blame] | 225 | |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 226 | // Create Activation instance for this version. |
| 227 | activations.insert(std::make_pair( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 228 | id, std::make_unique<Activation>( |
| 229 | bus, path, *this, id, activationState, associations))); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 230 | |
Lei YU | 269bff3 | 2018-08-21 15:21:40 +0800 | [diff] [blame] | 231 | // If Active, create RedundancyPriority instance for this |
| 232 | // version. |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 233 | if (activationState == server::Activation::Activations::Active) |
| 234 | { |
| 235 | uint8_t priority = std::numeric_limits<uint8_t>::max(); |
| 236 | if (!restoreFromFile(id, priority)) |
| 237 | { |
Adriana Kobylak | ee590c7 | 2017-09-26 15:16:06 -0500 | [diff] [blame] | 238 | if (isVersionFunctional) |
| 239 | { |
| 240 | priority = 0; |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | log<level::ERR>("Unable to restore priority from file.", |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 245 | entry("VERSIONID=%s", id.c_str())); |
Adriana Kobylak | ee590c7 | 2017-09-26 15:16:06 -0500 | [diff] [blame] | 246 | } |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 247 | } |
| 248 | activations.find(id)->second->redundancyPriority = |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 249 | std::make_unique<RedundancyPriority>( |
| 250 | bus, path, *(activations.find(id)->second), priority, |
| 251 | false); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 252 | } |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 253 | } |
| 254 | } |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 255 | |
| 256 | // If there is no ubi volume for bmc version then read the /etc/os-release |
| 257 | // and create rofs-<versionId> under /media |
| 258 | if (activations.size() == 0) |
| 259 | { |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 260 | auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE); |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 261 | auto id = phosphor::software::manager::Version::getId(version); |
| 262 | auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/"; |
| 263 | try |
| 264 | { |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 265 | if (!fs::is_directory(versionFileDir)) |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 266 | { |
| 267 | fs::create_directories(versionFileDir); |
| 268 | } |
| 269 | auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE; |
| 270 | fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath); |
| 271 | ItemUpdater::processBMCImage(); |
| 272 | } |
| 273 | catch (const std::exception& e) |
| 274 | { |
| 275 | log<level::ERR>(e.what()); |
| 276 | } |
| 277 | } |
Eddie James | eaa1ee0 | 2018-03-01 10:09:10 -0600 | [diff] [blame] | 278 | |
| 279 | mirrorUbootToAlt(); |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 280 | return; |
| 281 | } |
| 282 | |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 283 | void ItemUpdater::erase(std::string entryId) |
| 284 | { |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 285 | // Find entry in versions map |
| 286 | auto it = versions.find(entryId); |
| 287 | if (it != versions.end()) |
| 288 | { |
Lei YU | 0f88b5a | 2018-08-21 15:28:53 +0800 | [diff] [blame] | 289 | if (it->second->isFunctional() && ACTIVE_BMC_MAX_ALLOWED > 1) |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 290 | { |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 291 | log<level::ERR>("Error: Version is currently running on the BMC. " |
| 292 | "Unable to remove.", |
| 293 | entry("VERSIONID=%s", entryId.c_str())); |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 294 | return; |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // Delete ReadOnly partitions if it's not active |
| 298 | removeReadOnlyPartition(entryId); |
| 299 | removeFile(entryId); |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 300 | |
| 301 | // Removing entry in versions map |
| 302 | this->versions.erase(entryId); |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 303 | } |
| 304 | else |
| 305 | { |
| 306 | // Delete ReadOnly partitions even if we can't find the version |
| 307 | removeReadOnlyPartition(entryId); |
| 308 | removeFile(entryId); |
| 309 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 310 | log<level::ERR>("Error: Failed to find version in item updater " |
| 311 | "versions map. Unable to remove.", |
| 312 | entry("VERSIONID=%s", entryId.c_str())); |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 313 | } |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 314 | |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 315 | helper.clearEntry(entryId); |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 316 | |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 317 | // Removing entry in activations map |
| 318 | auto ita = activations.find(entryId); |
| 319 | if (ita == activations.end()) |
| 320 | { |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 321 | log<level::ERR>("Error: Failed to find version in item updater " |
| 322 | "activations map. Unable to remove.", |
| 323 | entry("VERSIONID=%s", entryId.c_str())); |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 324 | } |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 325 | else |
| 326 | { |
| 327 | this->activations.erase(entryId); |
| 328 | } |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 329 | ItemUpdater::resetUbootEnvVars(); |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 330 | return; |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 331 | } |
| 332 | |
Michael Tritz | bc1bf3a | 2017-09-18 16:38:23 -0500 | [diff] [blame] | 333 | void ItemUpdater::deleteAll() |
| 334 | { |
Adriana Kobylak | 83cd21f | 2018-02-28 15:48:48 -0600 | [diff] [blame] | 335 | std::vector<std::string> deletableVersions; |
| 336 | |
Michael Tritz | bc1bf3a | 2017-09-18 16:38:23 -0500 | [diff] [blame] | 337 | for (const auto& versionIt : versions) |
| 338 | { |
| 339 | if (!versionIt.second->isFunctional()) |
| 340 | { |
Adriana Kobylak | 83cd21f | 2018-02-28 15:48:48 -0600 | [diff] [blame] | 341 | deletableVersions.push_back(versionIt.first); |
Michael Tritz | bc1bf3a | 2017-09-18 16:38:23 -0500 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
Adriana Kobylak | 83cd21f | 2018-02-28 15:48:48 -0600 | [diff] [blame] | 345 | for (const auto& deletableIt : deletableVersions) |
| 346 | { |
| 347 | ItemUpdater::erase(deletableIt); |
| 348 | } |
| 349 | |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 350 | helper.cleanup(); |
Michael Tritz | bc1bf3a | 2017-09-18 16:38:23 -0500 | [diff] [blame] | 351 | } |
| 352 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 353 | ItemUpdater::ActivationStatus |
| 354 | ItemUpdater::validateSquashFSImage(const std::string& filePath) |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 355 | { |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 356 | bool invalid = false; |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 357 | |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 358 | for (auto& bmcImage : bmcImages) |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 359 | { |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 360 | fs::path file(filePath); |
| 361 | file /= bmcImage; |
| 362 | std::ifstream efile(file.c_str()); |
| 363 | if (efile.good() != 1) |
| 364 | { |
| 365 | log<level::ERR>("Failed to find the BMC image.", |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 366 | entry("IMAGE=%s", bmcImage.c_str())); |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 367 | invalid = true; |
| 368 | } |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 369 | } |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 370 | |
| 371 | if (invalid) |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 372 | { |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 373 | return ItemUpdater::ActivationStatus::invalid; |
| 374 | } |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 375 | |
| 376 | return ItemUpdater::ActivationStatus::ready; |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 377 | } |
| 378 | |
Adriana Kobylak | bbcb7be | 2018-07-17 15:47:34 -0500 | [diff] [blame] | 379 | void ItemUpdater::savePriority(const std::string& versionId, uint8_t value) |
| 380 | { |
| 381 | storeToFile(versionId, value); |
| 382 | helper.setEntry(versionId, value); |
| 383 | } |
| 384 | |
Saqib Khan | b9da663 | 2017-09-13 09:48:37 -0500 | [diff] [blame] | 385 | void ItemUpdater::freePriority(uint8_t value, const std::string& versionId) |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 386 | { |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 387 | std::map<std::string, uint8_t> priorityMap; |
| 388 | |
| 389 | // Insert the requested version and priority, it may not exist yet. |
| 390 | priorityMap.insert(std::make_pair(versionId, value)); |
| 391 | |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 392 | for (const auto& intf : activations) |
| 393 | { |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 394 | if (intf.second->redundancyPriority) |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 395 | { |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 396 | priorityMap.insert(std::make_pair( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 397 | intf.first, intf.second->redundancyPriority.get()->priority())); |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 398 | } |
| 399 | } |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 400 | |
| 401 | // Lambda function to compare 2 priority values, use <= to allow duplicates |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 402 | typedef std::function<bool(std::pair<std::string, uint8_t>, |
| 403 | std::pair<std::string, uint8_t>)> |
| 404 | cmpPriority; |
| 405 | cmpPriority cmpPriorityFunc = |
| 406 | [](std::pair<std::string, uint8_t> priority1, |
| 407 | std::pair<std::string, uint8_t> priority2) { |
| 408 | return priority1.second <= priority2.second; |
| 409 | }; |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 410 | |
| 411 | // Sort versions by ascending priority |
| 412 | std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 413 | priorityMap.begin(), priorityMap.end(), cmpPriorityFunc); |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 414 | |
| 415 | auto freePriorityValue = value; |
| 416 | for (auto& element : prioritySet) |
| 417 | { |
| 418 | if (element.first == versionId) |
| 419 | { |
| 420 | continue; |
| 421 | } |
| 422 | if (element.second == freePriorityValue) |
| 423 | { |
| 424 | ++freePriorityValue; |
| 425 | auto it = activations.find(element.first); |
| 426 | it->second->redundancyPriority.get()->sdbusPriority( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 427 | freePriorityValue); |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | auto lowestVersion = prioritySet.begin()->first; |
| 432 | if (value == prioritySet.begin()->second) |
| 433 | { |
| 434 | lowestVersion = versionId; |
| 435 | } |
| 436 | updateUbootEnvVars(lowestVersion); |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 437 | } |
| 438 | |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 439 | void ItemUpdater::reset() |
| 440 | { |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 441 | helper.factoryReset(); |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 442 | |
| 443 | log<level::INFO>("BMC factory reset will take effect upon reboot."); |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 444 | } |
| 445 | |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 446 | void ItemUpdater::removeReadOnlyPartition(std::string versionId) |
| 447 | { |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 448 | helper.removeVersion(versionId); |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 449 | } |
| 450 | |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 451 | bool ItemUpdater::fieldModeEnabled(bool value) |
| 452 | { |
| 453 | // enabling field mode is intended to be one way: false -> true |
| 454 | if (value && !control::FieldMode::fieldModeEnabled()) |
| 455 | { |
| 456 | control::FieldMode::fieldModeEnabled(value); |
| 457 | |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 458 | helper.enableFieldMode(); |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | return control::FieldMode::fieldModeEnabled(); |
| 462 | } |
| 463 | |
| 464 | void ItemUpdater::restoreFieldModeStatus() |
| 465 | { |
Michael Tritz | ff0b421 | 2017-10-24 17:38:09 -0500 | [diff] [blame] | 466 | std::ifstream input("/dev/mtd/u-boot-env"); |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 467 | std::string envVar; |
| 468 | std::getline(input, envVar); |
| 469 | |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 470 | if (envVar.find("fieldmode=true") != std::string::npos) |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 471 | { |
| 472 | ItemUpdater::fieldModeEnabled(true); |
| 473 | } |
| 474 | } |
| 475 | |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 476 | void ItemUpdater::setBMCInventoryPath() |
| 477 | { |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 478 | auto depth = 0; |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 479 | auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 480 | MAPPER_INTERFACE, "GetSubTreePaths"); |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 481 | |
Adriana Kobylak | 1254c62 | 2017-12-07 12:24:56 -0600 | [diff] [blame] | 482 | mapperCall.append(INVENTORY_PATH); |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 483 | mapperCall.append(depth); |
Adriana Kobylak | 1254c62 | 2017-12-07 12:24:56 -0600 | [diff] [blame] | 484 | std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE}; |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 485 | mapperCall.append(filter); |
| 486 | |
Ed Tanous | 87c7817 | 2018-08-10 12:51:53 -0700 | [diff] [blame] | 487 | try |
| 488 | { |
| 489 | auto response = bus.call(mapperCall); |
| 490 | |
| 491 | using ObjectPaths = std::vector<std::string>; |
| 492 | ObjectPaths result; |
| 493 | response.read(result); |
| 494 | |
| 495 | if (!result.empty()) |
| 496 | { |
| 497 | bmcInventoryPath = result.front(); |
| 498 | } |
| 499 | } |
| 500 | catch (const sdbusplus::exception::SdBusError& e) |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 501 | { |
| 502 | log<level::ERR>("Error in mapper GetSubTreePath"); |
| 503 | return; |
| 504 | } |
| 505 | |
Adriana Kobylak | 1254c62 | 2017-12-07 12:24:56 -0600 | [diff] [blame] | 506 | return; |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 507 | } |
| 508 | |
Gunnar Mills | f10b232 | 2017-09-21 15:31:55 -0500 | [diff] [blame] | 509 | void ItemUpdater::createActiveAssociation(const std::string& path) |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 510 | { |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 511 | assocs.emplace_back( |
| 512 | std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path)); |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 513 | associations(assocs); |
| 514 | } |
| 515 | |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 516 | void ItemUpdater::createFunctionalAssociation(const std::string& path) |
| 517 | { |
| 518 | assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION, |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 519 | FUNCTIONAL_REV_ASSOCIATION, path)); |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 520 | associations(assocs); |
| 521 | } |
| 522 | |
Gunnar Mills | f10b232 | 2017-09-21 15:31:55 -0500 | [diff] [blame] | 523 | void ItemUpdater::removeActiveAssociation(const std::string& path) |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 524 | { |
| 525 | for (auto iter = assocs.begin(); iter != assocs.end();) |
| 526 | { |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 527 | // Since there could be multiple associations to the same path, |
| 528 | // only remove ones that have an active forward association. |
| 529 | if ((std::get<0>(*iter)).compare(ACTIVE_FWD_ASSOCIATION) == 0 && |
| 530 | (std::get<2>(*iter)).compare(path) == 0) |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 531 | { |
| 532 | iter = assocs.erase(iter); |
| 533 | associations(assocs); |
| 534 | } |
| 535 | else |
| 536 | { |
| 537 | ++iter; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
Saqib Khan | b9da663 | 2017-09-13 09:48:37 -0500 | [diff] [blame] | 542 | bool ItemUpdater::isLowestPriority(uint8_t value) |
| 543 | { |
| 544 | for (const auto& intf : activations) |
| 545 | { |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 546 | if (intf.second->redundancyPriority) |
Saqib Khan | b9da663 | 2017-09-13 09:48:37 -0500 | [diff] [blame] | 547 | { |
| 548 | if (intf.second->redundancyPriority.get()->priority() < value) |
| 549 | { |
| 550 | return false; |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | return true; |
| 555 | } |
| 556 | |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 557 | void ItemUpdater::updateUbootEnvVars(const std::string& versionId) |
| 558 | { |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 559 | helper.updateUbootVersionId(versionId); |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 560 | } |
| 561 | |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 562 | void ItemUpdater::resetUbootEnvVars() |
| 563 | { |
| 564 | decltype(activations.begin()->second->redundancyPriority.get()->priority()) |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 565 | lowestPriority = std::numeric_limits<uint8_t>::max(); |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 566 | decltype(activations.begin()->second->versionId) lowestPriorityVersion; |
| 567 | for (const auto& intf : activations) |
| 568 | { |
| 569 | if (!intf.second->redundancyPriority.get()) |
| 570 | { |
| 571 | // Skip this version if the redundancyPriority is not initialized. |
| 572 | continue; |
| 573 | } |
| 574 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 575 | if (intf.second->redundancyPriority.get()->priority() <= lowestPriority) |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 576 | { |
| 577 | lowestPriority = intf.second->redundancyPriority.get()->priority(); |
| 578 | lowestPriorityVersion = intf.second->versionId; |
| 579 | } |
| 580 | } |
| 581 | |
Saqib Khan | f0382c3 | 2017-10-24 13:36:22 -0500 | [diff] [blame] | 582 | // Update the U-boot environment variable to point to the lowest priority |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 583 | updateUbootEnvVars(lowestPriorityVersion); |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 584 | } |
| 585 | |
Adriana Kobylak | a696359 | 2018-09-07 14:13:29 -0500 | [diff] [blame^] | 586 | void ItemUpdater::freeSpace(Activation& caller) |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 587 | { |
| 588 | // Versions with the highest priority in front |
| 589 | std::priority_queue<std::pair<int, std::string>, |
| 590 | std::vector<std::pair<int, std::string>>, |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 591 | std::less<std::pair<int, std::string>>> |
| 592 | versionsPQ; |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 593 | |
| 594 | std::size_t count = 0; |
| 595 | for (const auto& iter : activations) |
| 596 | { |
| 597 | if ((iter.second.get()->activation() == |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 598 | server::Activation::Activations::Active) || |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 599 | (iter.second.get()->activation() == |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 600 | server::Activation::Activations::Failed)) |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 601 | { |
| 602 | count++; |
| 603 | // Don't put the functional version on the queue since we can't |
| 604 | // remove the "running" BMC version. |
Lei YU | 0f88b5a | 2018-08-21 15:28:53 +0800 | [diff] [blame] | 605 | // If ACTIVE_BMC_MAX_ALLOWED <= 1, there is only one active BMC, |
| 606 | // so remove functional version as well. |
Adriana Kobylak | a696359 | 2018-09-07 14:13:29 -0500 | [diff] [blame^] | 607 | // Don't delete the the Activation object that called this function. |
| 608 | if ((versions.find(iter.second->versionId) |
| 609 | ->second->isFunctional() && |
| 610 | ACTIVE_BMC_MAX_ALLOWED > 1) || |
| 611 | (iter.second->versionId == caller.versionId)) |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 612 | { |
| 613 | continue; |
| 614 | } |
Adriana Kobylak | a696359 | 2018-09-07 14:13:29 -0500 | [diff] [blame^] | 615 | |
| 616 | // Failed activations don't have priority, assign them a large value |
| 617 | // for sorting purposes. |
| 618 | auto priority = 999; |
| 619 | if (iter.second.get()->activation() == |
| 620 | server::Activation::Activations::Active) |
| 621 | { |
| 622 | priority = iter.second->redundancyPriority.get()->priority(); |
| 623 | } |
| 624 | |
| 625 | versionsPQ.push(std::make_pair(priority, iter.second->versionId)); |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
| 629 | // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1, |
| 630 | // remove the highest priority one(s). |
| 631 | while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty())) |
| 632 | { |
| 633 | erase(versionsPQ.top().second); |
| 634 | versionsPQ.pop(); |
| 635 | count--; |
| 636 | } |
| 637 | } |
| 638 | |
Eddie James | eaa1ee0 | 2018-03-01 10:09:10 -0600 | [diff] [blame] | 639 | void ItemUpdater::mirrorUbootToAlt() |
| 640 | { |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 641 | helper.mirrorAlt(); |
Eddie James | eaa1ee0 | 2018-03-01 10:09:10 -0600 | [diff] [blame] | 642 | } |
| 643 | |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 644 | } // namespace updater |
| 645 | } // namespace software |
| 646 | } // namespace phosphor |