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> |
Eddie James | 13fc66a | 2017-08-31 15:36:44 -0500 | [diff] [blame] | 4 | #include <phosphor-logging/elog-errors.hpp> |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
Eddie James | 13fc66a | 2017-08-31 15:36:44 -0500 | [diff] [blame] | 6 | #include "xyz/openbmc_project/Common/error.hpp" |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 7 | #include <xyz/openbmc_project/Software/Version/server.hpp> |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 8 | #include "version.hpp" |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 9 | #include "config.h" |
| 10 | #include "item_updater.hpp" |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 11 | #include "activation.hpp" |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 12 | #include "serialize.hpp" |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 13 | |
| 14 | namespace openpower |
| 15 | { |
| 16 | namespace software |
| 17 | { |
Adriana Kobylak | befe5ce | 2017-04-05 15:57:44 -0500 | [diff] [blame] | 18 | namespace updater |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 19 | { |
| 20 | |
Saqib Khan | a8ade7e | 2017-04-12 10:27: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; |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 23 | namespace fs = std::experimental::filesystem; |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 24 | |
Eddie James | 13fc66a | 2017-08-31 15:36:44 -0500 | [diff] [blame] | 25 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 26 | using namespace phosphor::logging; |
| 27 | |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 28 | constexpr auto squashFSImage = "pnor.xz.squashfs"; |
Eddie James | 13fc66a | 2017-08-31 15:36:44 -0500 | [diff] [blame] | 29 | constexpr auto CHASSIS_STATE_PATH = "/xyz/openbmc_project/state/chassis0"; |
| 30 | constexpr auto CHASSIS_STATE_OBJ = "xyz.openbmc_project.State.Chassis"; |
| 31 | constexpr auto CHASSIS_STATE_OFF = |
| 32 | "xyz.openbmc_project.State.Chassis.PowerState.Off"; |
| 33 | constexpr auto SYSTEMD_PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 34 | |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 35 | void ItemUpdater::createActivation(sdbusplus::message::message& m) |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 36 | { |
Patrick Williams | e429094 | 2017-06-16 05:43:08 -0500 | [diff] [blame] | 37 | using SVersion = server::Version; |
| 38 | using VersionPurpose = SVersion::VersionPurpose; |
| 39 | namespace msg = sdbusplus::message; |
| 40 | namespace variant_ns = msg::variant_ns; |
| 41 | |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 42 | sdbusplus::message::object_path objPath; |
| 43 | std::map<std::string, |
Patrick Williams | e429094 | 2017-06-16 05:43:08 -0500 | [diff] [blame] | 44 | std::map<std::string, msg::variant<std::string>>> interfaces; |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 45 | m.read(objPath, interfaces); |
Patrick Williams | e429094 | 2017-06-16 05:43:08 -0500 | [diff] [blame] | 46 | |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 47 | std::string path(std::move(objPath)); |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 48 | std::string filePath; |
Patrick Williams | e429094 | 2017-06-16 05:43:08 -0500 | [diff] [blame] | 49 | auto purpose = VersionPurpose::Unknown; |
Saqib Khan | ce14870 | 2017-06-11 12:01:58 -0500 | [diff] [blame] | 50 | std::string version; |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 51 | |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 52 | for (const auto& intf : interfaces) |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 53 | { |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 54 | if (intf.first == VERSION_IFACE) |
| 55 | { |
| 56 | for (const auto& property : intf.second) |
| 57 | { |
| 58 | if (property.first == "Purpose") |
| 59 | { |
| 60 | // Only process the Host and System images |
Patrick Williams | e429094 | 2017-06-16 05:43:08 -0500 | [diff] [blame] | 61 | auto value = SVersion::convertVersionPurposeFromString( |
| 62 | variant_ns::get<std::string>(property.second)); |
| 63 | |
| 64 | if (value == VersionPurpose::Host || |
| 65 | value == VersionPurpose::System) |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 66 | { |
Saqib Khan | ce14870 | 2017-06-11 12:01:58 -0500 | [diff] [blame] | 67 | purpose = value; |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 68 | } |
| 69 | } |
Saqib Khan | ce14870 | 2017-06-11 12:01:58 -0500 | [diff] [blame] | 70 | else if (property.first == "Version") |
| 71 | { |
Patrick Williams | e429094 | 2017-06-16 05:43:08 -0500 | [diff] [blame] | 72 | version = variant_ns::get<std::string>(property.second); |
Saqib Khan | ce14870 | 2017-06-11 12:01:58 -0500 | [diff] [blame] | 73 | } |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 74 | } |
| 75 | } |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 76 | else if (intf.first == FILEPATH_IFACE) |
| 77 | { |
| 78 | for (const auto& property : intf.second) |
| 79 | { |
| 80 | if (property.first == "Path") |
| 81 | { |
Patrick Williams | e429094 | 2017-06-16 05:43:08 -0500 | [diff] [blame] | 82 | filePath = variant_ns::get<std::string>(property.second); |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
Patrick Williams | e429094 | 2017-06-16 05:43:08 -0500 | [diff] [blame] | 87 | if ((filePath.empty()) || (purpose == VersionPurpose::Unknown)) |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 88 | { |
| 89 | return; |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 90 | } |
| 91 | |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 92 | // Version id is the last item in the path |
| 93 | auto pos = path.rfind("/"); |
| 94 | if (pos == std::string::npos) |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 95 | { |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 96 | log<level::ERR>("No version id found in object path", |
| 97 | entry("OBJPATH=%s", path)); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | auto versionId = path.substr(pos + 1); |
| 102 | |
| 103 | if (activations.find(versionId) == activations.end()) |
| 104 | { |
| 105 | // Determine the Activation state by processing the given image dir. |
| 106 | auto activationState = server::Activation::Activations::Invalid; |
Gunnar Mills | 3588acc | 2017-09-07 13:13:22 -0500 | [diff] [blame] | 107 | AssociationList associations = {}; |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 108 | if (ItemUpdater::validateSquashFSImage(filePath) == 0) |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 109 | { |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 110 | activationState = server::Activation::Activations::Ready; |
Gunnar Mills | 3588acc | 2017-09-07 13:13:22 -0500 | [diff] [blame] | 111 | // Create an association to the host inventory item |
| 112 | associations.emplace_back(std::make_tuple( |
| 113 | ACTIVATION_FWD_ASSOCIATION, |
| 114 | ACTIVATION_REV_ASSOCIATION, |
| 115 | HOST_INVENTORY_PATH)); |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 118 | fs::path manifestPath(filePath); |
| 119 | manifestPath /= MANIFEST_FILE; |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 120 | std::string extendedVersion = (Version::getValue(manifestPath.string(), |
| 121 | std::map<std::string, std::string> |
| 122 | {{"extended_version", ""}})).begin()->second; |
Gunnar Mills | 3edb84b | 2017-08-18 15:13:15 -0500 | [diff] [blame] | 123 | |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 124 | activations.insert(std::make_pair( |
| 125 | versionId, |
| 126 | std::make_unique<Activation>( |
| 127 | bus, |
| 128 | path, |
Saqib Khan | 81bac88 | 2017-06-08 12:17:01 -0500 | [diff] [blame] | 129 | *this, |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 130 | versionId, |
| 131 | extendedVersion, |
Gunnar Mills | 3edb84b | 2017-08-18 15:13:15 -0500 | [diff] [blame] | 132 | activationState, |
| 133 | associations))); |
Saqib Khan | ce14870 | 2017-06-11 12:01:58 -0500 | [diff] [blame] | 134 | versions.insert(std::make_pair( |
| 135 | versionId, |
| 136 | std::make_unique<Version>( |
| 137 | bus, |
| 138 | path, |
| 139 | version, |
| 140 | purpose, |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 141 | filePath, |
| 142 | *this))); |
Saqib Khan | 00044f4 | 2017-07-10 17:24:43 -0500 | [diff] [blame] | 143 | } |
Saqib Khan | 3fb2d17 | 2017-08-07 12:14:03 -0500 | [diff] [blame] | 144 | else |
| 145 | { |
| 146 | log<level::INFO>("Software Object with the same version already exists", |
Gunnar Mills | 3fa7028 | 2017-08-18 15:30:42 -0500 | [diff] [blame] | 147 | entry("VERSION_ID=%s", versionId)); |
Saqib Khan | 3fb2d17 | 2017-08-07 12:14:03 -0500 | [diff] [blame] | 148 | } |
Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame] | 149 | return; |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 150 | } |
| 151 | |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 152 | void ItemUpdater::processPNORImage() |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 153 | { |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame] | 154 | // Read pnor.toc from folders under /media/ |
| 155 | // to get Active Software Versions. |
Gunnar Mills | 3fa7028 | 2017-08-18 15:30:42 -0500 | [diff] [blame] | 156 | for (const auto& iter : fs::directory_iterator(MEDIA_DIR)) |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame] | 157 | { |
| 158 | auto activationState = server::Activation::Activations::Active; |
| 159 | |
| 160 | static const auto PNOR_RO_PREFIX_LEN = strlen(PNOR_RO_PREFIX); |
Saqib Khan | 2be9ba9 | 2017-09-26 22:44:10 -0500 | [diff] [blame^] | 161 | static const auto PNOR_RW_PREFIX_LEN = strlen(PNOR_RW_PREFIX); |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame] | 162 | |
| 163 | // Check if the PNOR_RO_PREFIX is the prefix of the iter.path |
| 164 | if (0 == iter.path().native().compare(0, PNOR_RO_PREFIX_LEN, |
| 165 | PNOR_RO_PREFIX)) |
| 166 | { |
| 167 | auto pnorTOC = iter.path() / PNOR_TOC_FILE; |
| 168 | if (!fs::is_regular_file(pnorTOC)) |
| 169 | { |
| 170 | log<level::ERR>("Failed to read pnorTOC\n", |
| 171 | entry("FileName=%s", pnorTOC.string())); |
| 172 | activationState = server::Activation::Activations::Invalid; |
| 173 | } |
| 174 | auto keyValues = |
| 175 | Version::getValue(pnorTOC, |
| 176 | {{ "version", "" }, |
| 177 | { "extended_version", "" } }); |
| 178 | auto& version = keyValues.at("version"); |
| 179 | if (version.empty()) |
| 180 | { |
| 181 | log<level::ERR>("Failed to read version from pnorTOC", |
| 182 | entry("FILENAME=%s", pnorTOC.string())); |
| 183 | activationState = server::Activation::Activations::Invalid; |
| 184 | } |
| 185 | |
| 186 | auto& extendedVersion = keyValues.at("extended_version"); |
| 187 | if (extendedVersion.empty()) |
| 188 | { |
| 189 | log<level::ERR>("Failed to read extendedVersion from pnorTOC", |
| 190 | entry("FILENAME=%s", pnorTOC.string())); |
| 191 | activationState = server::Activation::Activations::Invalid; |
| 192 | } |
| 193 | |
| 194 | // The versionId is extracted from the path |
| 195 | // for example /media/pnor-ro-2a1022fe |
| 196 | auto id = iter.path().native().substr(PNOR_RO_PREFIX_LEN); |
| 197 | auto purpose = server::Version::VersionPurpose::Host; |
| 198 | auto path = fs::path(SOFTWARE_OBJPATH) / id; |
Gunnar Mills | 3588acc | 2017-09-07 13:13:22 -0500 | [diff] [blame] | 199 | AssociationList associations = {}; |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame] | 200 | |
Gunnar Mills | 3588acc | 2017-09-07 13:13:22 -0500 | [diff] [blame] | 201 | if (activationState == server::Activation::Activations::Active) |
| 202 | { |
| 203 | // Create an association to the host inventory item |
| 204 | associations.emplace_back(std::make_tuple( |
| 205 | ACTIVATION_FWD_ASSOCIATION, |
| 206 | ACTIVATION_REV_ASSOCIATION, |
| 207 | HOST_INVENTORY_PATH)); |
Gunnar Mills | 3edb84b | 2017-08-18 15:13:15 -0500 | [diff] [blame] | 208 | |
Gunnar Mills | 3588acc | 2017-09-07 13:13:22 -0500 | [diff] [blame] | 209 | // Create an active association since this image is active |
| 210 | createActiveAssociation(path); |
| 211 | } |
Gunnar Mills | 3edb84b | 2017-08-18 15:13:15 -0500 | [diff] [blame] | 212 | |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame] | 213 | // Create Activation instance for this version. |
| 214 | activations.insert(std::make_pair( |
| 215 | id, |
| 216 | std::make_unique<Activation>( |
| 217 | bus, |
| 218 | path, |
| 219 | *this, |
| 220 | id, |
| 221 | extendedVersion, |
Gunnar Mills | 3edb84b | 2017-08-18 15:13:15 -0500 | [diff] [blame] | 222 | activationState, |
| 223 | associations))); |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame] | 224 | |
| 225 | // If Active, create RedundancyPriority instance for this version. |
| 226 | if (activationState == server::Activation::Activations::Active) |
| 227 | { |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 228 | uint8_t priority = std::numeric_limits<uint8_t>::max(); |
| 229 | if (!restoreFromFile(id, priority)) |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame] | 230 | { |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 231 | log<level::ERR>("Unable to restore priority from file.", |
Gunnar Mills | 3fa7028 | 2017-08-18 15:30:42 -0500 | [diff] [blame] | 232 | entry("VERSIONID=%s", id)); |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame] | 233 | } |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 234 | activations.find(id)->second->redundancyPriority = |
| 235 | std::make_unique<RedundancyPriority>( |
| 236 | bus, |
| 237 | path, |
| 238 | *(activations.find(id)->second), |
| 239 | priority); |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // Create Version instance for this version. |
| 243 | versions.insert(std::make_pair( |
| 244 | id, |
| 245 | std::make_unique<Version>( |
| 246 | bus, |
| 247 | path, |
| 248 | version, |
| 249 | purpose, |
| 250 | "", |
| 251 | *this))); |
| 252 | } |
Saqib Khan | 2be9ba9 | 2017-09-26 22:44:10 -0500 | [diff] [blame^] | 253 | else if (0 == iter.path().native().compare(0, PNOR_RW_PREFIX_LEN, |
| 254 | PNOR_RW_PREFIX)) |
| 255 | { |
| 256 | auto id = iter.path().native().substr(PNOR_RW_PREFIX_LEN); |
| 257 | auto roDir = PNOR_RO_PREFIX + id; |
| 258 | if (!fs::is_directory(roDir)) |
| 259 | { |
| 260 | log<level::ERR>("No corresponding read-only volume found.", |
| 261 | entry("DIRNAME=%s", roDir)); |
| 262 | ItemUpdater::erase(id); |
| 263 | } |
| 264 | } |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame] | 265 | } |
Gunnar Mills | 2badd7a | 2017-09-20 12:51:28 -0500 | [diff] [blame] | 266 | |
| 267 | // Look at the RO symlink to determine if there is a functional image |
| 268 | auto id = determineId(PNOR_RO_ACTIVE_PATH); |
| 269 | if (!id.empty()) |
| 270 | { |
| 271 | updateFunctionalAssociation(std::string{SOFTWARE_OBJPATH} + '/' + id); |
| 272 | } |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 273 | return; |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 274 | } |
| 275 | |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame] | 276 | int ItemUpdater::validateSquashFSImage(const std::string& filePath) |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 277 | { |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame] | 278 | auto file = fs::path(filePath) / squashFSImage; |
| 279 | if (fs::is_regular_file(file)) |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 280 | { |
| 281 | return 0; |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | log<level::ERR>("Failed to find the SquashFS image."); |
| 286 | return -1; |
| 287 | } |
| 288 | } |
| 289 | |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 290 | void ItemUpdater::removeReadOnlyPartition(std::string versionId) |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 291 | { |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 292 | auto serviceFile = "obmc-flash-bios-ubiumount-ro@" + versionId + |
| 293 | ".service"; |
| 294 | |
| 295 | // Remove the read-only partitions. |
| 296 | auto method = bus.new_method_call( |
| 297 | SYSTEMD_BUSNAME, |
| 298 | SYSTEMD_PATH, |
| 299 | SYSTEMD_INTERFACE, |
| 300 | "StartUnit"); |
| 301 | method.append(serviceFile, "replace"); |
| 302 | bus.call_noreply(method); |
| 303 | } |
| 304 | |
| 305 | void ItemUpdater::removeReadWritePartition(std::string versionId) |
| 306 | { |
| 307 | auto serviceFile = "obmc-flash-bios-ubiumount-rw@" + versionId + |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 308 | ".service"; |
| 309 | |
| 310 | // Remove the read-write partitions. |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 311 | auto method = bus.new_method_call( |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 312 | SYSTEMD_BUSNAME, |
| 313 | SYSTEMD_PATH, |
| 314 | SYSTEMD_INTERFACE, |
| 315 | "StartUnit"); |
| 316 | method.append(serviceFile, "replace"); |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 317 | bus.call_noreply(method); |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 318 | } |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 319 | |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 320 | void ItemUpdater::removePreservedPartition() |
| 321 | { |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 322 | // Remove the preserved partition. |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 323 | auto method = bus.new_method_call( |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 324 | SYSTEMD_BUSNAME, |
| 325 | SYSTEMD_PATH, |
| 326 | SYSTEMD_INTERFACE, |
| 327 | "StartUnit"); |
| 328 | method.append("obmc-flash-bios-ubiumount-prsv.service", "replace"); |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 329 | bus.call_noreply(method); |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 330 | |
| 331 | return; |
| 332 | } |
| 333 | |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 334 | void ItemUpdater::reset() |
| 335 | { |
Gunnar Mills | 3fa7028 | 2017-08-18 15:30:42 -0500 | [diff] [blame] | 336 | for (const auto& it : activations) |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 337 | { |
| 338 | removeReadWritePartition(it.first); |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 339 | removeFile(it.first); |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 340 | } |
| 341 | removePreservedPartition(); |
| 342 | return; |
| 343 | } |
| 344 | |
Eddie James | 13fc66a | 2017-08-31 15:36:44 -0500 | [diff] [blame] | 345 | bool ItemUpdater::isVersionFunctional(std::string versionId) |
| 346 | { |
| 347 | if (!fs::exists(PNOR_RO_ACTIVE_PATH)) |
| 348 | { |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | fs::path activeRO = fs::read_symlink(PNOR_RO_ACTIVE_PATH); |
| 353 | |
| 354 | if (!fs::is_directory(activeRO)) |
| 355 | { |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | if (activeRO.string().find(versionId) == std::string::npos) |
| 360 | { |
| 361 | return false; |
| 362 | } |
| 363 | |
| 364 | // active PNOR is the version we're checking |
| 365 | return true; |
| 366 | } |
| 367 | |
| 368 | bool ItemUpdater::isChassisOn() |
| 369 | { |
| 370 | auto mapperCall = bus.new_method_call( |
| 371 | MAPPER_BUSNAME, |
| 372 | MAPPER_PATH, |
| 373 | MAPPER_INTERFACE, |
| 374 | "GetObject"); |
| 375 | |
| 376 | mapperCall.append(CHASSIS_STATE_PATH, |
| 377 | std::vector<std::string>({CHASSIS_STATE_OBJ})); |
| 378 | auto mapperResponseMsg = bus.call(mapperCall); |
| 379 | if (mapperResponseMsg.is_method_error()) |
| 380 | { |
| 381 | log<level::ERR>("Error in Mapper call"); |
| 382 | elog<InternalFailure>(); |
| 383 | } |
| 384 | using MapperResponseType = std::map<std::string, std::vector<std::string>>; |
| 385 | MapperResponseType mapperResponse; |
| 386 | mapperResponseMsg.read(mapperResponse); |
| 387 | if (mapperResponse.empty()) |
| 388 | { |
| 389 | log<level::ERR>("Invalid Response from mapper"); |
| 390 | elog<InternalFailure>(); |
| 391 | } |
| 392 | |
| 393 | auto method = bus.new_method_call((mapperResponse.begin()->first).c_str(), |
| 394 | CHASSIS_STATE_PATH, |
| 395 | SYSTEMD_PROPERTY_INTERFACE, |
| 396 | "Get"); |
| 397 | method.append(CHASSIS_STATE_OBJ, "CurrentPowerState"); |
| 398 | auto response = bus.call(method); |
| 399 | if (response.is_method_error()) |
| 400 | { |
| 401 | log<level::ERR>("Error in fetching current Chassis State", |
| 402 | entry("MapperResponse=%s", |
| 403 | (mapperResponse.begin()->first).c_str())); |
| 404 | elog<InternalFailure>(); |
| 405 | } |
| 406 | sdbusplus::message::variant<std::string> currentChassisState; |
| 407 | response.read(currentChassisState); |
| 408 | auto strParam = |
| 409 | sdbusplus::message::variant_ns::get<std::string>(currentChassisState); |
| 410 | return (strParam != CHASSIS_STATE_OFF); |
| 411 | } |
| 412 | |
Saqib Khan | b8e7f31 | 2017-08-12 10:24:10 -0500 | [diff] [blame] | 413 | void ItemUpdater::freePriority(uint8_t value, const std::string& versionId) |
Saqib Khan | 81bac88 | 2017-06-08 12:17:01 -0500 | [diff] [blame] | 414 | { |
| 415 | //TODO openbmc/openbmc#1896 Improve the performance of this function |
| 416 | for (const auto& intf : activations) |
| 417 | { |
Gunnar Mills | 3fa7028 | 2017-08-18 15:30:42 -0500 | [diff] [blame] | 418 | if (intf.second->redundancyPriority) |
Saqib Khan | 81bac88 | 2017-06-08 12:17:01 -0500 | [diff] [blame] | 419 | { |
Saqib Khan | b8e7f31 | 2017-08-12 10:24:10 -0500 | [diff] [blame] | 420 | if (intf.second->redundancyPriority.get()->priority() == value && |
| 421 | intf.second->versionId != versionId) |
Saqib Khan | 81bac88 | 2017-06-08 12:17:01 -0500 | [diff] [blame] | 422 | { |
Gunnar Mills | 3fa7028 | 2017-08-18 15:30:42 -0500 | [diff] [blame] | 423 | intf.second->redundancyPriority.get()->priority(value + 1); |
Saqib Khan | 81bac88 | 2017-06-08 12:17:01 -0500 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
Saqib Khan | 2af5c49 | 2017-07-17 16:15:13 -0500 | [diff] [blame] | 429 | bool ItemUpdater::isLowestPriority(uint8_t value) |
| 430 | { |
| 431 | for (const auto& intf : activations) |
| 432 | { |
Gunnar Mills | 3fa7028 | 2017-08-18 15:30:42 -0500 | [diff] [blame] | 433 | if (intf.second->redundancyPriority) |
Saqib Khan | 2af5c49 | 2017-07-17 16:15:13 -0500 | [diff] [blame] | 434 | { |
| 435 | if (intf.second->redundancyPriority.get()->priority() < value) |
| 436 | { |
| 437 | return false; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | return true; |
| 442 | } |
| 443 | |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 444 | void ItemUpdater::erase(std::string entryId) |
| 445 | { |
Eddie James | 13fc66a | 2017-08-31 15:36:44 -0500 | [diff] [blame] | 446 | if (isVersionFunctional(entryId) && isChassisOn()) { |
| 447 | log<level::ERR>(("Error: Version " + entryId + \ |
| 448 | " is currently active and running on the host." \ |
| 449 | " Unable to remove.").c_str()); |
| 450 | return; |
| 451 | } |
Saqib Khan | ef8cd9f | 2017-08-16 14:20:30 -0500 | [diff] [blame] | 452 | // Remove priority persistence file |
| 453 | removeFile(entryId); |
| 454 | |
Saqib Khan | 1e0aa5c | 2017-08-31 11:04:17 -0500 | [diff] [blame] | 455 | // Removing read-only and read-write partitions |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 456 | removeReadWritePartition(entryId); |
| 457 | removeReadOnlyPartition(entryId); |
| 458 | |
| 459 | // Removing entry in versions map |
| 460 | auto it = versions.find(entryId); |
| 461 | if (it == versions.end()) |
| 462 | { |
| 463 | log<level::ERR>(("Error: Failed to find version " + entryId + \ |
Gunnar Mills | 3fa7028 | 2017-08-18 15:30:42 -0500 | [diff] [blame] | 464 | " in item updater versions map." \ |
| 465 | " Unable to remove.").c_str()); |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 466 | return; |
| 467 | } |
| 468 | versions.erase(entryId); |
| 469 | |
| 470 | // Removing entry in activations map |
| 471 | auto ita = activations.find(entryId); |
| 472 | if (ita == activations.end()) |
| 473 | { |
| 474 | log<level::ERR>(("Error: Failed to find version " + entryId + \ |
Gunnar Mills | 3fa7028 | 2017-08-18 15:30:42 -0500 | [diff] [blame] | 475 | " in item updater activations map." \ |
| 476 | " Unable to remove.").c_str()); |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 477 | return; |
| 478 | } |
| 479 | activations.erase(entryId); |
| 480 | } |
| 481 | |
Michael Tritz | 234a07e | 2017-09-21 00:53:06 -0500 | [diff] [blame] | 482 | void ItemUpdater::deleteAll() |
| 483 | { |
| 484 | std::vector<std::string> deletableActivations; |
| 485 | |
| 486 | for (const auto& activationIt : activations) |
| 487 | { |
| 488 | if (!isVersionFunctional(activationIt.first)) |
| 489 | { |
| 490 | deletableActivations.push_back(activationIt.first); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | for (const auto& deletableIt : deletableActivations) |
| 495 | { |
| 496 | ItemUpdater::erase(deletableIt); |
| 497 | } |
| 498 | |
| 499 | // Remove any remaining pnor-ro- or pnor-rw- volumes that do not match |
| 500 | // the current version. |
| 501 | auto method = bus.new_method_call( |
| 502 | SYSTEMD_BUSNAME, |
| 503 | SYSTEMD_PATH, |
| 504 | SYSTEMD_INTERFACE, |
| 505 | "StartUnit"); |
| 506 | method.append("obmc-flash-bios-cleanup.service", "replace"); |
| 507 | bus.call_noreply(method); |
| 508 | } |
| 509 | |
Saqib Khan | 2cbfa03 | 2017-08-17 14:52:37 -0500 | [diff] [blame] | 510 | // TODO: openbmc/openbmc#1402 Monitor flash usage |
| 511 | void ItemUpdater::freeSpace() |
| 512 | { |
| 513 | std::size_t count = 0; |
| 514 | decltype(activations.begin()->second->redundancyPriority.get()->priority()) |
| 515 | highestPriority = 0; |
| 516 | decltype(activations.begin()->second->versionId) highestPriorityVersion; |
| 517 | for (const auto& iter : activations) |
| 518 | { |
| 519 | if (iter.second.get()->activation() == server::Activation::Activations::Active) |
| 520 | { |
| 521 | count++; |
| 522 | if (iter.second->redundancyPriority.get()->priority() > highestPriority) |
| 523 | { |
| 524 | highestPriority = iter.second->redundancyPriority.get()->priority(); |
| 525 | highestPriorityVersion = iter.second->versionId; |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | // Remove the pnor version with highest priority since the PNOR |
| 530 | // can't hold more than 2 versions. |
| 531 | if (count >= ACTIVE_PNOR_MAX_ALLOWED) |
| 532 | { |
| 533 | erase(highestPriorityVersion); |
| 534 | } |
| 535 | } |
| 536 | |
Gunnar Mills | 61010b2 | 2017-09-20 15:25:26 -0500 | [diff] [blame] | 537 | void ItemUpdater::createActiveAssociation(const std::string& path) |
Gunnar Mills | 9741cd1 | 2017-08-28 15:09:00 -0500 | [diff] [blame] | 538 | { |
| 539 | assocs.emplace_back(std::make_tuple(ACTIVE_FWD_ASSOCIATION, |
| 540 | ACTIVE_REV_ASSOCIATION, |
| 541 | path)); |
| 542 | associations(assocs); |
| 543 | } |
| 544 | |
Gunnar Mills | 833e4f3 | 2017-09-14 12:30:27 -0500 | [diff] [blame] | 545 | void ItemUpdater::updateFunctionalAssociation(const std::string& path) |
| 546 | { |
| 547 | // remove all functional associations |
| 548 | for (auto iter = assocs.begin(); iter != assocs.end();) |
| 549 | { |
| 550 | if ((std::get<0>(*iter)).compare(FUNCTIONAL_FWD_ASSOCIATION) == 0) |
| 551 | { |
| 552 | iter = assocs.erase(iter); |
| 553 | } |
| 554 | else |
| 555 | { |
| 556 | ++iter; |
| 557 | } |
| 558 | } |
| 559 | assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION, |
| 560 | FUNCTIONAL_REV_ASSOCIATION, |
| 561 | path)); |
| 562 | associations(assocs); |
| 563 | } |
| 564 | |
Gunnar Mills | 61010b2 | 2017-09-20 15:25:26 -0500 | [diff] [blame] | 565 | void ItemUpdater::removeActiveAssociation(const std::string& path) |
Gunnar Mills | 9741cd1 | 2017-08-28 15:09:00 -0500 | [diff] [blame] | 566 | { |
| 567 | for (auto iter = assocs.begin(); iter != assocs.end();) |
| 568 | { |
Gunnar Mills | 833e4f3 | 2017-09-14 12:30:27 -0500 | [diff] [blame] | 569 | if ((std::get<0>(*iter)).compare(ACTIVE_FWD_ASSOCIATION) == 0 && |
| 570 | (std::get<2>(*iter)).compare(path) == 0) |
Gunnar Mills | 9741cd1 | 2017-08-28 15:09:00 -0500 | [diff] [blame] | 571 | { |
| 572 | iter = assocs.erase(iter); |
| 573 | associations(assocs); |
| 574 | } |
| 575 | else |
| 576 | { |
| 577 | ++iter; |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
Gunnar Mills | 2badd7a | 2017-09-20 12:51:28 -0500 | [diff] [blame] | 582 | std::string ItemUpdater::determineId(const std::string& symlinkPath) |
| 583 | { |
| 584 | if (!fs::exists(symlinkPath)) |
| 585 | { |
| 586 | return {}; |
| 587 | } |
| 588 | |
| 589 | auto target = fs::canonical(symlinkPath).string(); |
| 590 | |
| 591 | // check to make sure the target really exists |
| 592 | if (!fs::is_regular_file(target + "/" + PNOR_TOC_FILE)) |
| 593 | { |
| 594 | return {}; |
| 595 | } |
| 596 | // Get the image <id> from the symlink target |
| 597 | // for example /media/ro-2a1022fe |
| 598 | static const auto PNOR_RO_PREFIX_LEN = strlen(PNOR_RO_PREFIX); |
| 599 | return target.substr(PNOR_RO_PREFIX_LEN); |
| 600 | } |
| 601 | |
Adriana Kobylak | befe5ce | 2017-04-05 15:57:44 -0500 | [diff] [blame] | 602 | } // namespace updater |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 603 | } // namespace software |
| 604 | } // namespace openpower |