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