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