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