Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 1 | #include <string> |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 2 | #include <experimental/filesystem> |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 3 | #include <fstream> |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 4 | #include <phosphor-logging/log.hpp> |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 5 | #include <xyz/openbmc_project/Software/Version/server.hpp> |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 6 | #include "version.hpp" |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 7 | #include "config.h" |
| 8 | #include "item_updater.hpp" |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 9 | #include "activation.hpp" |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 10 | |
| 11 | namespace openpower |
| 12 | { |
| 13 | namespace software |
| 14 | { |
Adriana Kobylak | befe5ce | 2017-04-05 15:57:44 -0500 | [diff] [blame] | 15 | namespace updater |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 16 | { |
| 17 | |
Saqib Khan | a8ade7e | 2017-04-12 10:27: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; |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 20 | namespace fs = std::experimental::filesystem; |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 21 | |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 22 | using namespace phosphor::logging; |
| 23 | |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 24 | constexpr auto squashFSImage = "pnor.xz.squashfs"; |
| 25 | |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 26 | void ItemUpdater::createActivation(sdbusplus::message::message& m) |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 27 | { |
Patrick Williams | e429094 | 2017-06-16 05:43:08 -0500 | [diff] [blame] | 28 | using SVersion = server::Version; |
| 29 | using VersionPurpose = SVersion::VersionPurpose; |
| 30 | namespace msg = sdbusplus::message; |
| 31 | namespace variant_ns = msg::variant_ns; |
| 32 | |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 33 | sdbusplus::message::object_path objPath; |
| 34 | std::map<std::string, |
Patrick Williams | e429094 | 2017-06-16 05:43:08 -0500 | [diff] [blame] | 35 | std::map<std::string, msg::variant<std::string>>> interfaces; |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 36 | m.read(objPath, interfaces); |
Patrick Williams | e429094 | 2017-06-16 05:43:08 -0500 | [diff] [blame] | 37 | |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 38 | std::string path(std::move(objPath)); |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 39 | std::string filePath; |
Patrick Williams | e429094 | 2017-06-16 05:43:08 -0500 | [diff] [blame] | 40 | auto purpose = VersionPurpose::Unknown; |
Saqib Khan | ce14870 | 2017-06-11 12:01:58 -0500 | [diff] [blame] | 41 | std::string version; |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 42 | |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 43 | for (const auto& intf : interfaces) |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 44 | { |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 45 | if (intf.first == VERSION_IFACE) |
| 46 | { |
| 47 | for (const auto& property : intf.second) |
| 48 | { |
| 49 | if (property.first == "Purpose") |
| 50 | { |
| 51 | // Only process the Host and System images |
Patrick Williams | e429094 | 2017-06-16 05:43:08 -0500 | [diff] [blame] | 52 | auto value = SVersion::convertVersionPurposeFromString( |
| 53 | variant_ns::get<std::string>(property.second)); |
| 54 | |
| 55 | if (value == VersionPurpose::Host || |
| 56 | value == VersionPurpose::System) |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 57 | { |
Saqib Khan | ce14870 | 2017-06-11 12:01:58 -0500 | [diff] [blame] | 58 | purpose = value; |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 59 | } |
| 60 | } |
Saqib Khan | ce14870 | 2017-06-11 12:01:58 -0500 | [diff] [blame] | 61 | else if (property.first == "Version") |
| 62 | { |
Patrick Williams | e429094 | 2017-06-16 05:43:08 -0500 | [diff] [blame] | 63 | version = variant_ns::get<std::string>(property.second); |
Saqib Khan | ce14870 | 2017-06-11 12:01:58 -0500 | [diff] [blame] | 64 | } |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 65 | } |
| 66 | } |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 67 | else if (intf.first == FILEPATH_IFACE) |
| 68 | { |
| 69 | for (const auto& property : intf.second) |
| 70 | { |
| 71 | if (property.first == "Path") |
| 72 | { |
Patrick Williams | e429094 | 2017-06-16 05:43:08 -0500 | [diff] [blame] | 73 | filePath = variant_ns::get<std::string>(property.second); |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
Patrick Williams | e429094 | 2017-06-16 05:43:08 -0500 | [diff] [blame] | 78 | if ((filePath.empty()) || (purpose == VersionPurpose::Unknown)) |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 79 | { |
| 80 | return; |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 83 | // Version id is the last item in the path |
| 84 | auto pos = path.rfind("/"); |
| 85 | if (pos == std::string::npos) |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 86 | { |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 87 | log<level::ERR>("No version id found in object path", |
| 88 | entry("OBJPATH=%s", path)); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | auto versionId = path.substr(pos + 1); |
| 93 | |
| 94 | if (activations.find(versionId) == activations.end()) |
| 95 | { |
| 96 | // Determine the Activation state by processing the given image dir. |
| 97 | auto activationState = server::Activation::Activations::Invalid; |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 98 | if (ItemUpdater::validateSquashFSImage(filePath) == 0) |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 99 | { |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 100 | activationState = server::Activation::Activations::Ready; |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 103 | fs::path manifestPath(filePath); |
| 104 | manifestPath /= MANIFEST_FILE; |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 105 | std::string extendedVersion = (Version::getValue(manifestPath.string(), |
| 106 | std::map<std::string, std::string> |
| 107 | {{"extended_version", ""}})).begin()->second; |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 108 | activations.insert(std::make_pair( |
| 109 | versionId, |
| 110 | std::make_unique<Activation>( |
| 111 | bus, |
| 112 | path, |
Saqib Khan | 81bac88 | 2017-06-08 12:17:01 -0500 | [diff] [blame] | 113 | *this, |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 114 | versionId, |
| 115 | extendedVersion, |
| 116 | activationState))); |
Saqib Khan | ce14870 | 2017-06-11 12:01:58 -0500 | [diff] [blame] | 117 | versions.insert(std::make_pair( |
| 118 | versionId, |
| 119 | std::make_unique<Version>( |
| 120 | bus, |
| 121 | path, |
| 122 | version, |
| 123 | purpose, |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 124 | filePath, |
| 125 | *this))); |
Saqib Khan | 00044f4 | 2017-07-10 17:24:43 -0500 | [diff] [blame] | 126 | } |
Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame] | 127 | return; |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 130 | void ItemUpdater::processPNORImage() |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 131 | { |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame^] | 132 | // Get the current PNOR version |
| 133 | auto pnorTOC = fs::path(PNOR_RO_ACTIVE_PATH) / PNOR_TOC_FILE; |
| 134 | if (!fs::is_regular_file(pnorTOC)) |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 135 | { |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 136 | log<level::INFO>("Error PNOR current version is empty"); |
| 137 | return; |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 138 | } |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame^] | 139 | std::string currentVersion = |
| 140 | Version::getValue(pnorTOC, {{ "version", "" }}).begin()->second; |
| 141 | |
| 142 | // Read pnor.toc from folders under /media/ |
| 143 | // to get Active Software Versions. |
| 144 | for(const auto& iter : fs::directory_iterator(MEDIA_DIR)) |
| 145 | { |
| 146 | auto activationState = server::Activation::Activations::Active; |
| 147 | |
| 148 | static const auto PNOR_RO_PREFIX_LEN = strlen(PNOR_RO_PREFIX); |
| 149 | |
| 150 | // Check if the PNOR_RO_PREFIX is the prefix of the iter.path |
| 151 | if (0 == iter.path().native().compare(0, PNOR_RO_PREFIX_LEN, |
| 152 | PNOR_RO_PREFIX)) |
| 153 | { |
| 154 | auto pnorTOC = iter.path() / PNOR_TOC_FILE; |
| 155 | if (!fs::is_regular_file(pnorTOC)) |
| 156 | { |
| 157 | log<level::ERR>("Failed to read pnorTOC\n", |
| 158 | entry("FileName=%s", pnorTOC.string())); |
| 159 | activationState = server::Activation::Activations::Invalid; |
| 160 | } |
| 161 | auto keyValues = |
| 162 | Version::getValue(pnorTOC, |
| 163 | {{ "version", "" }, |
| 164 | { "extended_version", "" } }); |
| 165 | auto& version = keyValues.at("version"); |
| 166 | if (version.empty()) |
| 167 | { |
| 168 | log<level::ERR>("Failed to read version from pnorTOC", |
| 169 | entry("FILENAME=%s", pnorTOC.string())); |
| 170 | activationState = server::Activation::Activations::Invalid; |
| 171 | } |
| 172 | |
| 173 | auto& extendedVersion = keyValues.at("extended_version"); |
| 174 | if (extendedVersion.empty()) |
| 175 | { |
| 176 | log<level::ERR>("Failed to read extendedVersion from pnorTOC", |
| 177 | entry("FILENAME=%s", pnorTOC.string())); |
| 178 | activationState = server::Activation::Activations::Invalid; |
| 179 | } |
| 180 | |
| 181 | // The versionId is extracted from the path |
| 182 | // for example /media/pnor-ro-2a1022fe |
| 183 | auto id = iter.path().native().substr(PNOR_RO_PREFIX_LEN); |
| 184 | auto purpose = server::Version::VersionPurpose::Host; |
| 185 | auto path = fs::path(SOFTWARE_OBJPATH) / id; |
| 186 | |
| 187 | // Create Activation instance for this version. |
| 188 | activations.insert(std::make_pair( |
| 189 | id, |
| 190 | std::make_unique<Activation>( |
| 191 | bus, |
| 192 | path, |
| 193 | *this, |
| 194 | id, |
| 195 | extendedVersion, |
| 196 | activationState))); |
| 197 | |
| 198 | // If Active, create RedundancyPriority instance for this version. |
| 199 | if (activationState == server::Activation::Activations::Active) |
| 200 | { |
| 201 | // Current PNOR needs the lowest Priority, so setting to 0. |
| 202 | // TODO openbmc/openbmc#2040 Need to store Priority in the |
| 203 | // RW partition to be able to restore the priorities that |
| 204 | // were set before the BMC reboot. |
| 205 | auto priority = 1; |
| 206 | if (currentVersion == version) |
| 207 | { |
| 208 | priority = 0; |
| 209 | } |
| 210 | activations.find(id)->second->redundancyPriority = |
| 211 | std::make_unique<RedundancyPriority>( |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 212 | bus, |
| 213 | path, |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame^] | 214 | *(activations.find(id)->second), |
| 215 | priority); |
| 216 | } |
| 217 | |
| 218 | // Create Version instance for this version. |
| 219 | versions.insert(std::make_pair( |
| 220 | id, |
| 221 | std::make_unique<Version>( |
| 222 | bus, |
| 223 | path, |
| 224 | version, |
| 225 | purpose, |
| 226 | "", |
| 227 | *this))); |
| 228 | } |
| 229 | } |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 230 | return; |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 231 | } |
| 232 | |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 233 | int ItemUpdater::validateSquashFSImage(const std::string& filePath) |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 234 | { |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame^] | 235 | auto file = fs::path(filePath) / squashFSImage; |
| 236 | if (fs::is_regular_file(file)) |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 237 | { |
| 238 | return 0; |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | log<level::ERR>("Failed to find the SquashFS image."); |
| 243 | return -1; |
| 244 | } |
| 245 | } |
| 246 | |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 247 | void ItemUpdater::removeReadOnlyPartition(std::string versionId) |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 248 | { |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 249 | auto serviceFile = "obmc-flash-bios-ubiumount-ro@" + versionId + |
| 250 | ".service"; |
| 251 | |
| 252 | // Remove the read-only partitions. |
| 253 | auto method = bus.new_method_call( |
| 254 | SYSTEMD_BUSNAME, |
| 255 | SYSTEMD_PATH, |
| 256 | SYSTEMD_INTERFACE, |
| 257 | "StartUnit"); |
| 258 | method.append(serviceFile, "replace"); |
| 259 | bus.call_noreply(method); |
| 260 | } |
| 261 | |
| 262 | void ItemUpdater::removeReadWritePartition(std::string versionId) |
| 263 | { |
| 264 | auto serviceFile = "obmc-flash-bios-ubiumount-rw@" + versionId + |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 265 | ".service"; |
| 266 | |
| 267 | // Remove the read-write partitions. |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 268 | auto method = bus.new_method_call( |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 269 | SYSTEMD_BUSNAME, |
| 270 | SYSTEMD_PATH, |
| 271 | SYSTEMD_INTERFACE, |
| 272 | "StartUnit"); |
| 273 | method.append(serviceFile, "replace"); |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 274 | bus.call_noreply(method); |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 275 | } |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 276 | |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 277 | void ItemUpdater::removePreservedPartition() |
| 278 | { |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 279 | // Remove the preserved partition. |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 280 | auto method = bus.new_method_call( |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 281 | SYSTEMD_BUSNAME, |
| 282 | SYSTEMD_PATH, |
| 283 | SYSTEMD_INTERFACE, |
| 284 | "StartUnit"); |
| 285 | method.append("obmc-flash-bios-ubiumount-prsv.service", "replace"); |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 286 | bus.call_noreply(method); |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 287 | |
| 288 | return; |
| 289 | } |
| 290 | |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 291 | void ItemUpdater::reset() |
| 292 | { |
| 293 | for(const auto& it : activations) |
| 294 | { |
| 295 | removeReadWritePartition(it.first); |
| 296 | } |
| 297 | removePreservedPartition(); |
| 298 | return; |
| 299 | } |
| 300 | |
Saqib Khan | 81bac88 | 2017-06-08 12:17:01 -0500 | [diff] [blame] | 301 | void ItemUpdater::freePriority(uint8_t value) |
| 302 | { |
| 303 | //TODO openbmc/openbmc#1896 Improve the performance of this function |
| 304 | for (const auto& intf : activations) |
| 305 | { |
| 306 | if(intf.second->redundancyPriority) |
| 307 | { |
| 308 | if (intf.second->redundancyPriority.get()->priority() == value) |
| 309 | { |
| 310 | intf.second->redundancyPriority.get()->priority(value+1); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
Saqib Khan | 2af5c49 | 2017-07-17 16:15:13 -0500 | [diff] [blame] | 316 | bool ItemUpdater::isLowestPriority(uint8_t value) |
| 317 | { |
| 318 | for (const auto& intf : activations) |
| 319 | { |
| 320 | if(intf.second->redundancyPriority) |
| 321 | { |
| 322 | if (intf.second->redundancyPriority.get()->priority() < value) |
| 323 | { |
| 324 | return false; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | return true; |
| 329 | } |
| 330 | |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 331 | void ItemUpdater::erase(std::string entryId) |
| 332 | { |
| 333 | // Removing partitions |
| 334 | removeReadWritePartition(entryId); |
| 335 | removeReadOnlyPartition(entryId); |
| 336 | |
| 337 | // Removing entry in versions map |
| 338 | auto it = versions.find(entryId); |
| 339 | if (it == versions.end()) |
| 340 | { |
| 341 | log<level::ERR>(("Error: Failed to find version " + entryId + \ |
| 342 | " in item updater versions map." \ |
| 343 | " Unable to remove.").c_str()); |
| 344 | return; |
| 345 | } |
| 346 | versions.erase(entryId); |
| 347 | |
| 348 | // Removing entry in activations map |
| 349 | auto ita = activations.find(entryId); |
| 350 | if (ita == activations.end()) |
| 351 | { |
| 352 | log<level::ERR>(("Error: Failed to find version " + entryId + \ |
| 353 | " in item updater activations map." \ |
| 354 | " Unable to remove.").c_str()); |
| 355 | return; |
| 356 | } |
| 357 | activations.erase(entryId); |
| 358 | } |
| 359 | |
Adriana Kobylak | befe5ce | 2017-04-05 15:57:44 -0500 | [diff] [blame] | 360 | } // namespace updater |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 361 | } // namespace software |
| 362 | } // namespace openpower |