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. |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 156 | for (const auto& iter : fs::directory_iterator(MEDIA_DIR)) |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 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 | { |
Gunnar Mills | 2ad1b55 | 2017-10-19 15:58:52 -0500 | [diff] [blame] | 171 | log<level::ERR>("Failed to read osRelease", |
| 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 | { |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 260 | auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE); |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 261 | auto id = phosphor::software::manager::Version::getId(version); |
| 262 | auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/"; |
| 263 | try |
| 264 | { |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 265 | if (!fs::is_directory(versionFileDir)) |
Saqib Khan | dcbfa04 | 2017-09-18 13:08:39 -0500 | [diff] [blame] | 266 | { |
| 267 | fs::create_directories(versionFileDir); |
| 268 | } |
| 269 | auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE; |
| 270 | fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath); |
| 271 | ItemUpdater::processBMCImage(); |
| 272 | } |
| 273 | catch (const std::exception& e) |
| 274 | { |
| 275 | log<level::ERR>(e.what()); |
| 276 | } |
| 277 | } |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 281 | void ItemUpdater::erase(std::string entryId) |
| 282 | { |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 283 | // Find entry in versions map |
| 284 | auto it = versions.find(entryId); |
| 285 | if (it != versions.end()) |
| 286 | { |
| 287 | if (it->second->isFunctional()) |
| 288 | { |
| 289 | log<level::ERR>(("Error: Version " + entryId + \ |
| 290 | " is currently running on the BMC." \ |
| 291 | " Unable to remove.").c_str()); |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 292 | return; |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // Delete ReadOnly partitions if it's not active |
| 296 | removeReadOnlyPartition(entryId); |
| 297 | removeFile(entryId); |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | // Delete ReadOnly partitions even if we can't find the version |
| 302 | removeReadOnlyPartition(entryId); |
| 303 | removeFile(entryId); |
| 304 | |
| 305 | log<level::ERR>(("Error: Failed to find version " + entryId + \ |
| 306 | " in item updater versions map." \ |
| 307 | " Unable to remove.").c_str()); |
| 308 | return; |
| 309 | } |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 310 | |
| 311 | // Remove the priority environment variable. |
| 312 | auto serviceFile = "obmc-flash-bmc-setenv@" + entryId + ".service"; |
| 313 | auto method = bus.new_method_call( |
| 314 | SYSTEMD_BUSNAME, |
| 315 | SYSTEMD_PATH, |
| 316 | SYSTEMD_INTERFACE, |
| 317 | "StartUnit"); |
| 318 | method.append(serviceFile, "replace"); |
| 319 | bus.call_noreply(method); |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 320 | |
| 321 | // Removing entry in versions map |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 322 | this->versions.erase(entryId); |
| 323 | |
| 324 | // Removing entry in activations map |
| 325 | auto ita = activations.find(entryId); |
| 326 | if (ita == activations.end()) |
| 327 | { |
| 328 | log<level::ERR>(("Error: Failed to find version " + entryId + \ |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 329 | " in item updater activations map." \ |
| 330 | " Unable to remove.").c_str()); |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 331 | return; |
| 332 | } |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 333 | |
| 334 | this->activations.erase(entryId); |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 335 | ItemUpdater::resetUbootEnvVars(); |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 336 | } |
| 337 | |
Michael Tritz | bc1bf3a | 2017-09-18 16:38:23 -0500 | [diff] [blame] | 338 | void ItemUpdater::deleteAll() |
| 339 | { |
| 340 | std::vector<std::string> deletableVersions; |
| 341 | |
| 342 | for (const auto& versionIt : versions) |
| 343 | { |
| 344 | if (!versionIt.second->isFunctional()) |
| 345 | { |
| 346 | deletableVersions.push_back(versionIt.first); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | for (const auto& deletableIt : deletableVersions) |
| 351 | { |
| 352 | ItemUpdater::erase(deletableIt); |
| 353 | } |
| 354 | |
| 355 | // Remove any volumes that do not match current versions. |
| 356 | auto method = bus.new_method_call( |
| 357 | SYSTEMD_BUSNAME, |
| 358 | SYSTEMD_PATH, |
| 359 | SYSTEMD_INTERFACE, |
| 360 | "StartUnit"); |
| 361 | method.append("obmc-flash-bmc-cleanup.service", "replace"); |
| 362 | bus.call_noreply(method); |
| 363 | } |
| 364 | |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 365 | ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage( |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 366 | const std::string& filePath) |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 367 | { |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 368 | bool invalid = false; |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 369 | |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 370 | for (auto& bmcImage : bmcImages) |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 371 | { |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 372 | fs::path file(filePath); |
| 373 | file /= bmcImage; |
| 374 | std::ifstream efile(file.c_str()); |
| 375 | if (efile.good() != 1) |
| 376 | { |
| 377 | log<level::ERR>("Failed to find the BMC image.", |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 378 | entry("IMAGE=%s", bmcImage.c_str())); |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 379 | invalid = true; |
| 380 | } |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 381 | } |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 382 | |
| 383 | if (invalid) |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 384 | { |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 385 | return ItemUpdater::ActivationStatus::invalid; |
| 386 | } |
Michael Tritz | b1cfdf9 | 2017-08-14 14:33:30 -0500 | [diff] [blame] | 387 | |
| 388 | return ItemUpdater::ActivationStatus::ready; |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 389 | } |
| 390 | |
Saqib Khan | b9da663 | 2017-09-13 09:48:37 -0500 | [diff] [blame] | 391 | void ItemUpdater::freePriority(uint8_t value, const std::string& versionId) |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 392 | { |
| 393 | //TODO openbmc/openbmc#1896 Improve the performance of this function |
| 394 | for (const auto& intf : activations) |
| 395 | { |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 396 | if (intf.second->redundancyPriority) |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 397 | { |
Saqib Khan | b9da663 | 2017-09-13 09:48:37 -0500 | [diff] [blame] | 398 | if (intf.second->redundancyPriority.get()->priority() == value && |
| 399 | intf.second->versionId != versionId) |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 400 | { |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 401 | intf.second->redundancyPriority.get()->priority(value + 1); |
Saqib Khan | 4c1aec0 | 2017-07-06 11:46:13 -0500 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 407 | void ItemUpdater::reset() |
| 408 | { |
| 409 | // Mark the read-write partition for recreation upon reboot. |
| 410 | auto method = bus.new_method_call( |
| 411 | SYSTEMD_BUSNAME, |
| 412 | SYSTEMD_PATH, |
| 413 | SYSTEMD_INTERFACE, |
| 414 | "StartUnit"); |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 415 | method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace"); |
Michael Tritz | 37a5904 | 2017-07-12 13:44:53 -0500 | [diff] [blame] | 416 | bus.call_noreply(method); |
| 417 | |
| 418 | log<level::INFO>("BMC factory reset will take effect upon reboot."); |
| 419 | |
| 420 | return; |
| 421 | } |
| 422 | |
Leonel Gonzalez | 3526ef7 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 423 | void ItemUpdater::removeReadOnlyPartition(std::string versionId) |
| 424 | { |
| 425 | auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId + |
| 426 | ".service"; |
| 427 | |
| 428 | // Remove the read-only partitions. |
| 429 | auto method = bus.new_method_call( |
| 430 | SYSTEMD_BUSNAME, |
| 431 | SYSTEMD_PATH, |
| 432 | SYSTEMD_INTERFACE, |
| 433 | "StartUnit"); |
| 434 | method.append(serviceFile, "replace"); |
| 435 | bus.call_noreply(method); |
| 436 | } |
| 437 | |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 438 | bool ItemUpdater::fieldModeEnabled(bool value) |
| 439 | { |
| 440 | // enabling field mode is intended to be one way: false -> true |
| 441 | if (value && !control::FieldMode::fieldModeEnabled()) |
| 442 | { |
| 443 | control::FieldMode::fieldModeEnabled(value); |
| 444 | |
| 445 | auto method = bus.new_method_call( |
| 446 | SYSTEMD_BUSNAME, |
| 447 | SYSTEMD_PATH, |
| 448 | SYSTEMD_INTERFACE, |
| 449 | "StartUnit"); |
| 450 | method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service", |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 451 | "replace"); |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 452 | bus.call_noreply(method); |
| 453 | |
| 454 | method = bus.new_method_call( |
| 455 | SYSTEMD_BUSNAME, |
| 456 | SYSTEMD_PATH, |
| 457 | SYSTEMD_INTERFACE, |
| 458 | "StopUnit"); |
| 459 | method.append("usr-local.mount", "replace"); |
| 460 | bus.call_noreply(method); |
| 461 | |
| 462 | std::vector<std::string> usrLocal = {"usr-local.mount"}; |
| 463 | |
| 464 | method = bus.new_method_call( |
| 465 | SYSTEMD_BUSNAME, |
| 466 | SYSTEMD_PATH, |
| 467 | SYSTEMD_INTERFACE, |
| 468 | "MaskUnitFiles"); |
| 469 | method.append(usrLocal, false, true); |
| 470 | bus.call_noreply(method); |
| 471 | } |
| 472 | |
| 473 | return control::FieldMode::fieldModeEnabled(); |
| 474 | } |
| 475 | |
| 476 | void ItemUpdater::restoreFieldModeStatus() |
| 477 | { |
| 478 | std::ifstream input("/run/fw_env"); |
| 479 | std::string envVar; |
| 480 | std::getline(input, envVar); |
| 481 | |
Gunnar Mills | 9a78224 | 2017-08-22 16:23:15 -0500 | [diff] [blame] | 482 | if (envVar.find("fieldmode=true") != std::string::npos) |
Michael Tritz | 0129d92 | 2017-08-10 19:33:46 -0500 | [diff] [blame] | 483 | { |
| 484 | ItemUpdater::fieldModeEnabled(true); |
| 485 | } |
| 486 | } |
| 487 | |
Gunnar Mills | b60add1 | 2017-08-24 16:41:42 -0500 | [diff] [blame] | 488 | void ItemUpdater::setBMCInventoryPath() |
| 489 | { |
| 490 | //TODO: openbmc/openbmc#1786 - Get the BMC path by looking for objects |
| 491 | // that implement the BMC inventory interface |
| 492 | auto depth = 0; |
| 493 | auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, |
| 494 | MAPPER_PATH, |
| 495 | MAPPER_INTERFACE, |
| 496 | "GetSubTreePaths"); |
| 497 | |
| 498 | mapperCall.append(CHASSIS_INVENTORY_PATH); |
| 499 | mapperCall.append(depth); |
| 500 | |
| 501 | // TODO: openbmc/openbmc#2226 - Add Inventory Item filter when |
| 502 | // mapper is fixed. |
| 503 | std::vector<std::string> filter = {}; |
| 504 | mapperCall.append(filter); |
| 505 | |
| 506 | auto response = bus.call(mapperCall); |
| 507 | if (response.is_method_error()) |
| 508 | { |
| 509 | log<level::ERR>("Error in mapper GetSubTreePath"); |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | using ObjectPaths = std::vector<std::string>; |
| 514 | ObjectPaths result; |
| 515 | response.read(result); |
| 516 | |
| 517 | if (result.empty()) |
| 518 | { |
| 519 | log<level::ERR>("Invalid response from mapper"); |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | for (auto& iter : result) |
| 524 | { |
| 525 | const auto& path = iter; |
| 526 | if (path.substr(path.find_last_of('/') + 1).compare("bmc") == 0) |
| 527 | { |
| 528 | bmcInventoryPath = path; |
| 529 | return; |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
Gunnar Mills | f10b232 | 2017-09-21 15:31:55 -0500 | [diff] [blame] | 534 | void ItemUpdater::createActiveAssociation(const std::string& path) |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 535 | { |
| 536 | assocs.emplace_back(std::make_tuple(ACTIVE_FWD_ASSOCIATION, |
| 537 | ACTIVE_REV_ASSOCIATION, |
| 538 | path)); |
| 539 | associations(assocs); |
| 540 | } |
| 541 | |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 542 | void ItemUpdater::createFunctionalAssociation(const std::string& path) |
| 543 | { |
| 544 | assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION, |
| 545 | FUNCTIONAL_REV_ASSOCIATION, |
| 546 | path)); |
| 547 | associations(assocs); |
| 548 | } |
| 549 | |
Gunnar Mills | f10b232 | 2017-09-21 15:31:55 -0500 | [diff] [blame] | 550 | void ItemUpdater::removeActiveAssociation(const std::string& path) |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 551 | { |
| 552 | for (auto iter = assocs.begin(); iter != assocs.end();) |
| 553 | { |
Gunnar Mills | 88e8a32 | 2017-09-13 11:09:28 -0500 | [diff] [blame] | 554 | // Since there could be multiple associations to the same path, |
| 555 | // only remove ones that have an active forward association. |
| 556 | if ((std::get<0>(*iter)).compare(ACTIVE_FWD_ASSOCIATION) == 0 && |
| 557 | (std::get<2>(*iter)).compare(path) == 0) |
Gunnar Mills | ded875d | 2017-08-28 16:44:52 -0500 | [diff] [blame] | 558 | { |
| 559 | iter = assocs.erase(iter); |
| 560 | associations(assocs); |
| 561 | } |
| 562 | else |
| 563 | { |
| 564 | ++iter; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
Saqib Khan | b9da663 | 2017-09-13 09:48:37 -0500 | [diff] [blame] | 569 | bool ItemUpdater::isLowestPriority(uint8_t value) |
| 570 | { |
| 571 | for (const auto& intf : activations) |
| 572 | { |
Gunnar Mills | d16bcbd | 2017-10-08 16:50:42 -0500 | [diff] [blame] | 573 | if (intf.second->redundancyPriority) |
Saqib Khan | b9da663 | 2017-09-13 09:48:37 -0500 | [diff] [blame] | 574 | { |
| 575 | if (intf.second->redundancyPriority.get()->priority() < value) |
| 576 | { |
| 577 | return false; |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | return true; |
| 582 | } |
| 583 | |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 584 | void ItemUpdater::resetUbootEnvVars() |
| 585 | { |
| 586 | decltype(activations.begin()->second->redundancyPriority.get()->priority()) |
| 587 | lowestPriority = std::numeric_limits<uint8_t>::max(); |
| 588 | decltype(activations.begin()->second->versionId) lowestPriorityVersion; |
| 589 | for (const auto& intf : activations) |
| 590 | { |
| 591 | if (!intf.second->redundancyPriority.get()) |
| 592 | { |
| 593 | // Skip this version if the redundancyPriority is not initialized. |
| 594 | continue; |
| 595 | } |
| 596 | |
| 597 | if (intf.second->redundancyPriority.get()->priority() |
| 598 | <= lowestPriority) |
| 599 | { |
| 600 | lowestPriority = intf.second->redundancyPriority.get()->priority(); |
| 601 | lowestPriorityVersion = intf.second->versionId; |
| 602 | } |
| 603 | } |
| 604 | |
Saqib Khan | f0382c3 | 2017-10-24 13:36:22 -0500 | [diff] [blame] | 605 | // Update the U-boot environment variable to point to the lowest priority |
| 606 | auto it = activations.find(lowestPriorityVersion); |
| 607 | it->second->updateUbootEnvVars(); |
Saqib Khan | 49446ae | 2017-10-02 10:54:20 -0500 | [diff] [blame] | 608 | } |
| 609 | |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 610 | } // namespace updater |
| 611 | } // namespace software |
| 612 | } // namespace phosphor |