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