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