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