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