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