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