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