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 | |
Adriana Kobylak | d5b8f75 | 2019-05-01 11:52:35 -0500 | [diff] [blame] | 10 | #include <phosphor-logging/elog-errors.hpp> |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 11 | #include <phosphor-logging/elog.hpp> |
| 12 | #include <phosphor-logging/log.hpp> |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 13 | #include <xyz/openbmc_project/Common/error.hpp> |
| 14 | #include <xyz/openbmc_project/Software/Image/error.hpp> |
| 15 | |
| 16 | #include <filesystem> |
| 17 | #include <fstream> |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 18 | #include <queue> |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 19 | #include <set> |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 20 | #include <string> |
Gunnar Mills | 60f5ccf | 2020-05-28 12:26:39 -0500 | [diff] [blame] | 21 | #include <thread> |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 22 | |
| 23 | namespace phosphor |
| 24 | { |
| 25 | namespace software |
| 26 | { |
| 27 | namespace updater |
| 28 | { |
| 29 | |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 30 | // When you see server:: you know we're referencing our base class |
| 31 | namespace server = sdbusplus::xyz::openbmc_project::Software::server; |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 32 | namespace control = sdbusplus::xyz::openbmc_project::Control::server; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 33 | |
| 34 | using namespace phosphor::logging; |
Adriana Kobylak | 43699ca | 2018-10-17 14:56:29 -0500 | [diff] [blame] | 35 | using namespace sdbusplus::xyz::openbmc_project::Software::Image::Error; |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 36 | using namespace phosphor::software::image; |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame] | 37 | namespace fs = std::filesystem; |
Adriana Kobylak | d5b8f75 | 2019-05-01 11:52:35 -0500 | [diff] [blame] | 38 | using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 39 | |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 40 | void ItemUpdater::createActivation(sdbusplus::message::message& msg) |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 41 | { |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 42 | |
| 43 | using SVersion = server::Version; |
| 44 | using VersionPurpose = SVersion::VersionPurpose; |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 45 | using VersionClass = phosphor::software::manager::Version; |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 46 | |
Patrick Williams | bc1facd | 2020-06-03 05:58:27 -0500 | [diff] [blame] | 47 | sdbusplus::message::object_path objPath; |
Saqib Khan | 84a0e69 | 2017-06-28 17:27:01 -0500 | [diff] [blame] | 48 | auto purpose = VersionPurpose::Unknown; |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 49 | std::string version; |
Patrick Williams | bc1facd | 2020-06-03 05:58:27 -0500 | [diff] [blame] | 50 | std::map<std::string, std::map<std::string, std::variant<std::string>>> |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 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( |
Patrick Williams | e883fb8 | 2020-05-13 11:38:55 -0500 | [diff] [blame] | 65 | std::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 | { |
Patrick Williams | e883fb8 | 2020-05-13 11:38:55 -0500 | [diff] [blame] | 77 | version = std::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 | { |
Patrick Williams | e883fb8 | 2020-05-13 11:38:55 -0500 | [diff] [blame] | 87 | filePath = std::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 | { |
Adriana Kobylak | 716cd78 | 2020-06-08 13:27:43 -0500 | [diff] [blame] | 178 | // Get the version to calculate the id |
Adriana Kobylak | 24a8d83 | 2020-06-10 08:29:36 -0500 | [diff] [blame] | 179 | fs::path releaseFile(OS_RELEASE_FILE); |
| 180 | auto osRelease = iter.path() / releaseFile.relative_path(); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 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())); |
Adriana Kobylak | 716cd78 | 2020-06-08 13:27:43 -0500 | [diff] [blame] | 186 | |
| 187 | // Try to get the version id from the mount directory name and |
| 188 | // call to delete it as this version may be corrupted. Dynamic |
| 189 | // volumes created by the UBI layout for example have the id in |
| 190 | // the mount directory name. The worst that can happen is that |
| 191 | // erase() is called with an non-existent id and returns. |
| 192 | auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN); |
Saqib Khan | 021c365 | 2017-09-26 12:11:02 -0500 | [diff] [blame] | 193 | ItemUpdater::erase(id); |
Adriana Kobylak | 716cd78 | 2020-06-08 13:27:43 -0500 | [diff] [blame] | 194 | |
Saqib Khan | 021c365 | 2017-09-26 12:11:02 -0500 | [diff] [blame] | 195 | continue; |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 196 | } |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 197 | auto version = VersionClass::getBMCVersion(osRelease); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 198 | if (version.empty()) |
| 199 | { |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 200 | log<level::ERR>( |
| 201 | "Failed to read version from osRelease", |
| 202 | entry("FILENAME=%s", osRelease.string().c_str())); |
Adriana Kobylak | 716cd78 | 2020-06-08 13:27:43 -0500 | [diff] [blame] | 203 | |
| 204 | // Try to delete the version, same as above if the |
| 205 | // OS_RELEASE_FILE does not exist. |
| 206 | auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN); |
| 207 | ItemUpdater::erase(id); |
| 208 | |
| 209 | continue; |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 210 | } |
Saqib Khan | 021c365 | 2017-09-26 12:11:02 -0500 | [diff] [blame] | 211 | |
Adriana Kobylak | 716cd78 | 2020-06-08 13:27:43 -0500 | [diff] [blame] | 212 | auto id = VersionClass::getId(version); |
| 213 | |
Adriana Kobylak | f383d27 | 2020-06-16 15:17:22 -0500 | [diff] [blame] | 214 | // Check if the id has already been added. This can happen if the |
| 215 | // BMC partitions / devices were manually flashed with the same |
| 216 | // image. |
| 217 | if (versions.find(id) != versions.end()) |
| 218 | { |
| 219 | continue; |
| 220 | } |
| 221 | |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 222 | auto purpose = server::Version::VersionPurpose::BMC; |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 223 | restorePurpose(id, purpose); |
| 224 | |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 225 | auto path = fs::path(SOFTWARE_OBJPATH) / id; |
| 226 | |
Lei YU | 269bff3 | 2018-08-21 15:21:40 +0800 | [diff] [blame] | 227 | // Create functional association if this is the functional |
| 228 | // version |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 229 | if (version.compare(functionalVersion) == 0) |
| 230 | { |
| 231 | createFunctionalAssociation(path); |
| 232 | } |
| 233 | |
Gunnar Mills | 43b25cd | 2017-09-07 13:19:34 -0500 | [diff] [blame] | 234 | AssociationList associations = {}; |
| 235 | |
| 236 | if (activationState == server::Activation::Activations::Active) |
| 237 | { |
| 238 | // Create an association to the BMC inventory item |
| 239 | associations.emplace_back(std::make_tuple( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 240 | ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION, |
| 241 | bmcInventoryPath)); |
Gunnar Mills | 43b25cd | 2017-09-07 13:19:34 -0500 | [diff] [blame] | 242 | |
| 243 | // Create an active association since this image is active |
| 244 | createActiveAssociation(path); |
| 245 | } |
| 246 | |
AppaRao Puli | bbebec7 | 2020-01-28 23:57:41 +0530 | [diff] [blame] | 247 | // All updateable firmware components must expose the updateable |
| 248 | // association. |
| 249 | createUpdateableAssociation(path); |
| 250 | |
Adriana Kobylak | ee590c7 | 2017-09-26 15:16:06 -0500 | [diff] [blame] | 251 | // Create Version instance for this version. |
| 252 | auto versionPtr = std::make_unique<VersionClass>( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 253 | bus, path, version, purpose, "", |
| 254 | std::bind(&ItemUpdater::erase, this, std::placeholders::_1)); |
Adriana Kobylak | ee590c7 | 2017-09-26 15:16:06 -0500 | [diff] [blame] | 255 | auto isVersionFunctional = versionPtr->isFunctional(); |
Michael Tritz | 4254bec | 2017-10-03 17:18:22 -0500 | [diff] [blame] | 256 | if (!isVersionFunctional) |
| 257 | { |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 258 | versionPtr->deleteObject = |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 259 | std::make_unique<phosphor::software::manager::Delete>( |
| 260 | bus, path, *versionPtr); |
Michael Tritz | 4254bec | 2017-10-03 17:18:22 -0500 | [diff] [blame] | 261 | } |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 262 | versions.insert(std::make_pair(id, std::move(versionPtr))); |
Michael Tritz | 4254bec | 2017-10-03 17:18:22 -0500 | [diff] [blame] | 263 | |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 264 | // Create Activation instance for this version. |
| 265 | activations.insert(std::make_pair( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 266 | id, std::make_unique<Activation>( |
| 267 | bus, path, *this, id, activationState, associations))); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 268 | |
Lei YU | 269bff3 | 2018-08-21 15:21:40 +0800 | [diff] [blame] | 269 | // If Active, create RedundancyPriority instance for this |
| 270 | // version. |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 271 | if (activationState == server::Activation::Activations::Active) |
| 272 | { |
| 273 | uint8_t priority = std::numeric_limits<uint8_t>::max(); |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 274 | if (!restorePriority(id, priority)) |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 275 | { |
Adriana Kobylak | ee590c7 | 2017-09-26 15:16:06 -0500 | [diff] [blame] | 276 | if (isVersionFunctional) |
| 277 | { |
| 278 | priority = 0; |
| 279 | } |
| 280 | else |
| 281 | { |
| 282 | log<level::ERR>("Unable to restore priority from file.", |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 283 | entry("VERSIONID=%s", id.c_str())); |
Adriana Kobylak | ee590c7 | 2017-09-26 15:16:06 -0500 | [diff] [blame] | 284 | } |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 285 | } |
| 286 | activations.find(id)->second->redundancyPriority = |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 287 | std::make_unique<RedundancyPriority>( |
| 288 | bus, path, *(activations.find(id)->second), priority, |
| 289 | false); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 290 | } |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 291 | } |
| 292 | } |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 293 | |
Adriana Kobylak | 716cd78 | 2020-06-08 13:27:43 -0500 | [diff] [blame] | 294 | // If there are no bmc versions mounted under MEDIA_DIR, then read the |
| 295 | // /etc/os-release and create rofs-<versionId> under MEDIA_DIR, then call |
| 296 | // again processBMCImage() to create the D-Bus interface for it. |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 297 | if (activations.size() == 0) |
| 298 | { |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 299 | auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE); |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 300 | auto id = phosphor::software::manager::Version::getId(version); |
| 301 | auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/"; |
| 302 | try |
| 303 | { |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 304 | if (!fs::is_directory(versionFileDir)) |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 305 | { |
| 306 | fs::create_directories(versionFileDir); |
| 307 | } |
| 308 | auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE; |
| 309 | fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath); |
| 310 | ItemUpdater::processBMCImage(); |
| 311 | } |
| 312 | catch (const std::exception& e) |
| 313 | { |
| 314 | log<level::ERR>(e.what()); |
| 315 | } |
| 316 | } |
Eddie James | eaa1ee0 | 2018-03-01 10:09:10 -0600 | [diff] [blame] | 317 | |
| 318 | mirrorUbootToAlt(); |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 319 | return; |
| 320 | } |
| 321 | |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 322 | void ItemUpdater::erase(std::string entryId) |
| 323 | { |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 324 | // Find entry in versions map |
| 325 | auto it = versions.find(entryId); |
| 326 | if (it != versions.end()) |
| 327 | { |
Lei YU | 0f88b5a | 2018-08-21 15:28:53 +0800 | [diff] [blame] | 328 | if (it->second->isFunctional() && ACTIVE_BMC_MAX_ALLOWED > 1) |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 329 | { |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 330 | log<level::ERR>("Error: Version is currently running on the BMC. " |
| 331 | "Unable to remove.", |
| 332 | entry("VERSIONID=%s", entryId.c_str())); |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 333 | return; |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 334 | } |
Adriana Kobylak | d1a55ad | 2020-06-11 14:01:28 -0500 | [diff] [blame] | 335 | } |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 336 | |
Adriana Kobylak | d1a55ad | 2020-06-11 14:01:28 -0500 | [diff] [blame] | 337 | // First call resetUbootEnvVars() so that the BMC points to a valid image to |
| 338 | // boot from. If resetUbootEnvVars() is called after the image is actually |
| 339 | // deleted from the BMC flash, there'd be a time window where the BMC would |
| 340 | // be pointing to a non-existent image to boot from. |
| 341 | // Need to remove the entries from the activations map before that call so |
| 342 | // that resetUbootEnvVars() doesn't use the version to be deleted. |
| 343 | auto iteratorActivations = activations.find(entryId); |
| 344 | if (iteratorActivations == activations.end()) |
| 345 | { |
| 346 | log<level::ERR>("Error: Failed to find version in item updater " |
| 347 | "activations map. Unable to remove.", |
| 348 | entry("VERSIONID=%s", entryId.c_str())); |
| 349 | } |
| 350 | else |
| 351 | { |
| 352 | removeAssociations(iteratorActivations->second->path); |
| 353 | this->activations.erase(entryId); |
| 354 | } |
| 355 | ItemUpdater::resetUbootEnvVars(); |
| 356 | |
| 357 | if (it != versions.end()) |
| 358 | { |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 359 | // Delete ReadOnly partitions if it's not active |
| 360 | removeReadOnlyPartition(entryId); |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 361 | removePersistDataDirectory(entryId); |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 362 | |
| 363 | // Removing entry in versions map |
| 364 | this->versions.erase(entryId); |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 365 | } |
| 366 | else |
| 367 | { |
| 368 | // Delete ReadOnly partitions even if we can't find the version |
| 369 | removeReadOnlyPartition(entryId); |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 370 | removePersistDataDirectory(entryId); |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 371 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 372 | log<level::ERR>("Error: Failed to find version in item updater " |
| 373 | "versions map. Unable to remove.", |
| 374 | entry("VERSIONID=%s", entryId.c_str())); |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 375 | } |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 376 | |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 377 | helper.clearEntry(entryId); |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 378 | |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 379 | return; |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 380 | } |
| 381 | |
Michael Tritz | bc1bf3a | 2017-09-18 16:38:23 -0500 | [diff] [blame] | 382 | void ItemUpdater::deleteAll() |
| 383 | { |
Adriana Kobylak | 83cd21f | 2018-02-28 15:48:48 -0600 | [diff] [blame] | 384 | std::vector<std::string> deletableVersions; |
| 385 | |
Michael Tritz | bc1bf3a | 2017-09-18 16:38:23 -0500 | [diff] [blame] | 386 | for (const auto& versionIt : versions) |
| 387 | { |
| 388 | if (!versionIt.second->isFunctional()) |
| 389 | { |
Adriana Kobylak | 83cd21f | 2018-02-28 15:48:48 -0600 | [diff] [blame] | 390 | deletableVersions.push_back(versionIt.first); |
Michael Tritz | bc1bf3a | 2017-09-18 16:38:23 -0500 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Adriana Kobylak | 83cd21f | 2018-02-28 15:48:48 -0600 | [diff] [blame] | 394 | for (const auto& deletableIt : deletableVersions) |
| 395 | { |
| 396 | ItemUpdater::erase(deletableIt); |
| 397 | } |
| 398 | |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 399 | helper.cleanup(); |
Michael Tritz | bc1bf3a | 2017-09-18 16:38:23 -0500 | [diff] [blame] | 400 | } |
| 401 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 402 | ItemUpdater::ActivationStatus |
| 403 | ItemUpdater::validateSquashFSImage(const std::string& filePath) |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 404 | { |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 405 | bool invalid = false; |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 406 | |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 407 | for (auto& bmcImage : bmcImages) |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 408 | { |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 409 | fs::path file(filePath); |
| 410 | file /= bmcImage; |
| 411 | std::ifstream efile(file.c_str()); |
| 412 | if (efile.good() != 1) |
| 413 | { |
| 414 | log<level::ERR>("Failed to find the BMC image.", |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 415 | entry("IMAGE=%s", bmcImage.c_str())); |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 416 | invalid = true; |
| 417 | } |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 418 | } |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 419 | |
| 420 | if (invalid) |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 421 | { |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 422 | return ItemUpdater::ActivationStatus::invalid; |
| 423 | } |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 424 | |
| 425 | return ItemUpdater::ActivationStatus::ready; |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 426 | } |
| 427 | |
Adriana Kobylak | bbcb7be | 2018-07-17 15:47:34 -0500 | [diff] [blame] | 428 | void ItemUpdater::savePriority(const std::string& versionId, uint8_t value) |
| 429 | { |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 430 | storePriority(versionId, value); |
Adriana Kobylak | bbcb7be | 2018-07-17 15:47:34 -0500 | [diff] [blame] | 431 | helper.setEntry(versionId, value); |
| 432 | } |
| 433 | |
Saqib Khan | b9da663 | 2017-09-13 09:48:37 -0500 | [diff] [blame] | 434 | void ItemUpdater::freePriority(uint8_t value, const std::string& versionId) |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 435 | { |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 436 | std::map<std::string, uint8_t> priorityMap; |
| 437 | |
| 438 | // Insert the requested version and priority, it may not exist yet. |
| 439 | priorityMap.insert(std::make_pair(versionId, value)); |
| 440 | |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 441 | for (const auto& intf : activations) |
| 442 | { |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 443 | if (intf.second->redundancyPriority) |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 444 | { |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 445 | priorityMap.insert(std::make_pair( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 446 | intf.first, intf.second->redundancyPriority.get()->priority())); |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 447 | } |
| 448 | } |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 449 | |
| 450 | // Lambda function to compare 2 priority values, use <= to allow duplicates |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 451 | typedef std::function<bool(std::pair<std::string, uint8_t>, |
| 452 | std::pair<std::string, uint8_t>)> |
| 453 | cmpPriority; |
| 454 | cmpPriority cmpPriorityFunc = |
| 455 | [](std::pair<std::string, uint8_t> priority1, |
| 456 | std::pair<std::string, uint8_t> priority2) { |
| 457 | return priority1.second <= priority2.second; |
| 458 | }; |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 459 | |
| 460 | // Sort versions by ascending priority |
| 461 | std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 462 | priorityMap.begin(), priorityMap.end(), cmpPriorityFunc); |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 463 | |
| 464 | auto freePriorityValue = value; |
| 465 | for (auto& element : prioritySet) |
| 466 | { |
| 467 | if (element.first == versionId) |
| 468 | { |
| 469 | continue; |
| 470 | } |
| 471 | if (element.second == freePriorityValue) |
| 472 | { |
| 473 | ++freePriorityValue; |
| 474 | auto it = activations.find(element.first); |
| 475 | it->second->redundancyPriority.get()->sdbusPriority( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 476 | freePriorityValue); |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
| 480 | auto lowestVersion = prioritySet.begin()->first; |
| 481 | if (value == prioritySet.begin()->second) |
| 482 | { |
| 483 | lowestVersion = versionId; |
| 484 | } |
| 485 | updateUbootEnvVars(lowestVersion); |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 486 | } |
| 487 | |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 488 | void ItemUpdater::reset() |
| 489 | { |
Gunnar Mills | 60f5ccf | 2020-05-28 12:26:39 -0500 | [diff] [blame] | 490 | constexpr auto setFactoryResetWait = std::chrono::seconds(3); |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 491 | helper.factoryReset(); |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 492 | |
Gunnar Mills | 60f5ccf | 2020-05-28 12:26:39 -0500 | [diff] [blame] | 493 | // Need to wait for env variables to complete, otherwise an immediate reboot |
| 494 | // will not factory reset. |
| 495 | std::this_thread::sleep_for(setFactoryResetWait); |
| 496 | |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 497 | log<level::INFO>("BMC factory reset will take effect upon reboot."); |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 498 | } |
| 499 | |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 500 | void ItemUpdater::removeReadOnlyPartition(std::string versionId) |
| 501 | { |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 502 | helper.removeVersion(versionId); |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 503 | } |
| 504 | |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 505 | bool ItemUpdater::fieldModeEnabled(bool value) |
| 506 | { |
| 507 | // enabling field mode is intended to be one way: false -> true |
| 508 | if (value && !control::FieldMode::fieldModeEnabled()) |
| 509 | { |
| 510 | control::FieldMode::fieldModeEnabled(value); |
| 511 | |
Adriana Kobylak | 22848ec | 2019-10-28 10:08:39 -0500 | [diff] [blame] | 512 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 513 | SYSTEMD_INTERFACE, "StartUnit"); |
| 514 | method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service", |
| 515 | "replace"); |
| 516 | bus.call_noreply(method); |
| 517 | |
| 518 | method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 519 | SYSTEMD_INTERFACE, "StopUnit"); |
| 520 | method.append("usr-local.mount", "replace"); |
| 521 | bus.call_noreply(method); |
| 522 | |
| 523 | std::vector<std::string> usrLocal = {"usr-local.mount"}; |
| 524 | |
| 525 | method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 526 | SYSTEMD_INTERFACE, "MaskUnitFiles"); |
| 527 | method.append(usrLocal, false, true); |
| 528 | bus.call_noreply(method); |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 529 | } |
Adriana Kobylak | d5b8f75 | 2019-05-01 11:52:35 -0500 | [diff] [blame] | 530 | else if (!value && control::FieldMode::fieldModeEnabled()) |
| 531 | { |
| 532 | elog<NotAllowed>(xyz::openbmc_project::Common::NotAllowed::REASON( |
| 533 | "FieldMode is not allowed to be cleared")); |
| 534 | } |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 535 | |
| 536 | return control::FieldMode::fieldModeEnabled(); |
| 537 | } |
| 538 | |
| 539 | void ItemUpdater::restoreFieldModeStatus() |
| 540 | { |
Michael Tritz | ff0b421 | 2017-10-24 17:38:09 -0500 | [diff] [blame] | 541 | std::ifstream input("/dev/mtd/u-boot-env"); |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 542 | std::string envVar; |
| 543 | std::getline(input, envVar); |
| 544 | |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 545 | if (envVar.find("fieldmode=true") != std::string::npos) |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 546 | { |
| 547 | ItemUpdater::fieldModeEnabled(true); |
| 548 | } |
| 549 | } |
| 550 | |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 551 | void ItemUpdater::setBMCInventoryPath() |
| 552 | { |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 553 | auto depth = 0; |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 554 | auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 555 | MAPPER_INTERFACE, "GetSubTreePaths"); |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 556 | |
Adriana Kobylak | 1254c62 | 2017-12-07 12:24:56 -0600 | [diff] [blame] | 557 | mapperCall.append(INVENTORY_PATH); |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 558 | mapperCall.append(depth); |
Adriana Kobylak | 1254c62 | 2017-12-07 12:24:56 -0600 | [diff] [blame] | 559 | std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE}; |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 560 | mapperCall.append(filter); |
| 561 | |
Ed Tanous | 87c7817 | 2018-08-10 12:51:53 -0700 | [diff] [blame] | 562 | try |
| 563 | { |
| 564 | auto response = bus.call(mapperCall); |
| 565 | |
| 566 | using ObjectPaths = std::vector<std::string>; |
| 567 | ObjectPaths result; |
| 568 | response.read(result); |
| 569 | |
| 570 | if (!result.empty()) |
| 571 | { |
| 572 | bmcInventoryPath = result.front(); |
| 573 | } |
| 574 | } |
| 575 | catch (const sdbusplus::exception::SdBusError& e) |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 576 | { |
| 577 | log<level::ERR>("Error in mapper GetSubTreePath"); |
| 578 | return; |
| 579 | } |
| 580 | |
Adriana Kobylak | 1254c62 | 2017-12-07 12:24:56 -0600 | [diff] [blame] | 581 | return; |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 582 | } |
| 583 | |
Gunnar Mills | f10b232 | 2017-09-21 15:31:55 -0500 | [diff] [blame] | 584 | void ItemUpdater::createActiveAssociation(const std::string& path) |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 585 | { |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 586 | assocs.emplace_back( |
| 587 | std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path)); |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 588 | associations(assocs); |
| 589 | } |
| 590 | |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 591 | void ItemUpdater::createFunctionalAssociation(const std::string& path) |
| 592 | { |
| 593 | assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION, |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 594 | FUNCTIONAL_REV_ASSOCIATION, path)); |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 595 | associations(assocs); |
| 596 | } |
| 597 | |
AppaRao Puli | bbebec7 | 2020-01-28 23:57:41 +0530 | [diff] [blame] | 598 | void ItemUpdater::createUpdateableAssociation(const std::string& path) |
| 599 | { |
| 600 | assocs.emplace_back(std::make_tuple(UPDATEABLE_FWD_ASSOCIATION, |
| 601 | UPDATEABLE_REV_ASSOCIATION, path)); |
| 602 | associations(assocs); |
| 603 | } |
| 604 | |
Adriana Kobylak | 991af7e | 2018-12-10 13:08:38 -0600 | [diff] [blame] | 605 | void ItemUpdater::removeAssociations(const std::string& path) |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 606 | { |
| 607 | for (auto iter = assocs.begin(); iter != assocs.end();) |
| 608 | { |
Adriana Kobylak | 991af7e | 2018-12-10 13:08:38 -0600 | [diff] [blame] | 609 | if ((std::get<2>(*iter)).compare(path) == 0) |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 610 | { |
| 611 | iter = assocs.erase(iter); |
| 612 | associations(assocs); |
| 613 | } |
| 614 | else |
| 615 | { |
| 616 | ++iter; |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
Saqib Khan | b9da663 | 2017-09-13 09:48:37 -0500 | [diff] [blame] | 621 | bool ItemUpdater::isLowestPriority(uint8_t value) |
| 622 | { |
| 623 | for (const auto& intf : activations) |
| 624 | { |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 625 | if (intf.second->redundancyPriority) |
Saqib Khan | b9da663 | 2017-09-13 09:48:37 -0500 | [diff] [blame] | 626 | { |
| 627 | if (intf.second->redundancyPriority.get()->priority() < value) |
| 628 | { |
| 629 | return false; |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | return true; |
| 634 | } |
| 635 | |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 636 | void ItemUpdater::updateUbootEnvVars(const std::string& versionId) |
| 637 | { |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 638 | helper.updateUbootVersionId(versionId); |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 639 | } |
| 640 | |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 641 | void ItemUpdater::resetUbootEnvVars() |
| 642 | { |
| 643 | decltype(activations.begin()->second->redundancyPriority.get()->priority()) |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 644 | lowestPriority = std::numeric_limits<uint8_t>::max(); |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 645 | decltype(activations.begin()->second->versionId) lowestPriorityVersion; |
| 646 | for (const auto& intf : activations) |
| 647 | { |
| 648 | if (!intf.second->redundancyPriority.get()) |
| 649 | { |
| 650 | // Skip this version if the redundancyPriority is not initialized. |
| 651 | continue; |
| 652 | } |
| 653 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 654 | if (intf.second->redundancyPriority.get()->priority() <= lowestPriority) |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 655 | { |
| 656 | lowestPriority = intf.second->redundancyPriority.get()->priority(); |
| 657 | lowestPriorityVersion = intf.second->versionId; |
| 658 | } |
| 659 | } |
| 660 | |
Saqib Khan | f0382c3 | 2017-10-24 13:36:22 -0500 | [diff] [blame] | 661 | // Update the U-boot environment variable to point to the lowest priority |
Adriana Kobylak | b77551c | 2017-10-27 12:46:23 -0500 | [diff] [blame] | 662 | updateUbootEnvVars(lowestPriorityVersion); |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 663 | } |
| 664 | |
Adriana Kobylak | a696359 | 2018-09-07 14:13:29 -0500 | [diff] [blame] | 665 | void ItemUpdater::freeSpace(Activation& caller) |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 666 | { |
| 667 | // Versions with the highest priority in front |
| 668 | std::priority_queue<std::pair<int, std::string>, |
| 669 | std::vector<std::pair<int, std::string>>, |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 670 | std::less<std::pair<int, std::string>>> |
| 671 | versionsPQ; |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 672 | |
| 673 | std::size_t count = 0; |
| 674 | for (const auto& iter : activations) |
| 675 | { |
| 676 | if ((iter.second.get()->activation() == |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 677 | server::Activation::Activations::Active) || |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 678 | (iter.second.get()->activation() == |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 679 | server::Activation::Activations::Failed)) |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 680 | { |
| 681 | count++; |
| 682 | // Don't put the functional version on the queue since we can't |
| 683 | // remove the "running" BMC version. |
Lei YU | 0f88b5a | 2018-08-21 15:28:53 +0800 | [diff] [blame] | 684 | // If ACTIVE_BMC_MAX_ALLOWED <= 1, there is only one active BMC, |
| 685 | // so remove functional version as well. |
Adriana Kobylak | a696359 | 2018-09-07 14:13:29 -0500 | [diff] [blame] | 686 | // Don't delete the the Activation object that called this function. |
| 687 | if ((versions.find(iter.second->versionId) |
| 688 | ->second->isFunctional() && |
| 689 | ACTIVE_BMC_MAX_ALLOWED > 1) || |
| 690 | (iter.second->versionId == caller.versionId)) |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 691 | { |
| 692 | continue; |
| 693 | } |
Adriana Kobylak | a696359 | 2018-09-07 14:13:29 -0500 | [diff] [blame] | 694 | |
| 695 | // Failed activations don't have priority, assign them a large value |
| 696 | // for sorting purposes. |
| 697 | auto priority = 999; |
| 698 | if (iter.second.get()->activation() == |
| 699 | server::Activation::Activations::Active) |
| 700 | { |
| 701 | priority = iter.second->redundancyPriority.get()->priority(); |
| 702 | } |
| 703 | |
| 704 | versionsPQ.push(std::make_pair(priority, iter.second->versionId)); |
Adriana Kobylak | 204e1e7 | 2018-01-24 16:00:05 -0600 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
| 708 | // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1, |
| 709 | // remove the highest priority one(s). |
| 710 | while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty())) |
| 711 | { |
| 712 | erase(versionsPQ.top().second); |
| 713 | versionsPQ.pop(); |
| 714 | count--; |
| 715 | } |
| 716 | } |
| 717 | |
Eddie James | eaa1ee0 | 2018-03-01 10:09:10 -0600 | [diff] [blame] | 718 | void ItemUpdater::mirrorUbootToAlt() |
| 719 | { |
Lei YU | 56aaf45 | 2018-06-21 16:09:44 +0800 | [diff] [blame] | 720 | helper.mirrorAlt(); |
Eddie James | eaa1ee0 | 2018-03-01 10:09:10 -0600 | [diff] [blame] | 721 | } |
| 722 | |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 723 | } // namespace updater |
| 724 | } // namespace software |
| 725 | } // namespace phosphor |