Lei YU | 322f3f4 | 2019-02-21 16:10:41 +0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "item_updater_static.hpp" |
| 4 | |
Lei YU | b53425d | 2019-02-22 11:38:40 +0800 | [diff] [blame] | 5 | #include "activation_static.hpp" |
Lei YU | e499446 | 2019-03-14 14:41:53 +0800 | [diff] [blame] | 6 | #include "utils.hpp" |
Lei YU | dec8cf9 | 2019-02-21 17:47:05 +0800 | [diff] [blame] | 7 | #include "version.hpp" |
| 8 | |
Lei YU | a7b4ade | 2019-02-25 17:49:29 +0800 | [diff] [blame] | 9 | #include <array> |
Lei YU | dec8cf9 | 2019-02-21 17:47:05 +0800 | [diff] [blame] | 10 | #include <cstring> |
| 11 | #include <filesystem> |
| 12 | #include <fstream> |
| 13 | #include <phosphor-logging/elog-errors.hpp> |
| 14 | #include <phosphor-logging/log.hpp> |
Lei YU | a2e6716 | 2019-02-22 17:35:24 +0800 | [diff] [blame] | 15 | #include <sstream> |
Lei YU | dec8cf9 | 2019-02-21 17:47:05 +0800 | [diff] [blame] | 16 | #include <string> |
Lei YU | 5efca58 | 2019-02-25 17:05:29 +0800 | [diff] [blame] | 17 | #include <tuple> |
| 18 | #include <xyz/openbmc_project/Common/error.hpp> |
Lei YU | dec8cf9 | 2019-02-21 17:47:05 +0800 | [diff] [blame] | 19 | |
| 20 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 21 | using namespace phosphor::logging; |
Lei YU | 5efca58 | 2019-02-25 17:05:29 +0800 | [diff] [blame] | 22 | using sdbusplus::exception::SdBusError; |
Lei YU | dec8cf9 | 2019-02-21 17:47:05 +0800 | [diff] [blame] | 23 | |
| 24 | // When you see server:: you know we're referencing our base class |
| 25 | namespace server = sdbusplus::xyz::openbmc_project::Software::server; |
| 26 | namespace fs = std::filesystem; |
| 27 | |
| 28 | namespace utils |
| 29 | { |
| 30 | |
| 31 | template <typename... Ts> |
| 32 | std::string concat_string(Ts const&... ts) |
| 33 | { |
| 34 | std::stringstream s; |
| 35 | ((s << ts << " "), ...) << std::endl; |
| 36 | return s.str(); |
| 37 | } |
| 38 | |
| 39 | // Helper function to run pflash command |
Lei YU | 5efca58 | 2019-02-25 17:05:29 +0800 | [diff] [blame] | 40 | // Returns return code and the stdout |
Lei YU | dec8cf9 | 2019-02-21 17:47:05 +0800 | [diff] [blame] | 41 | template <typename... Ts> |
Lei YU | 5efca58 | 2019-02-25 17:05:29 +0800 | [diff] [blame] | 42 | std::pair<int, std::string> pflash(Ts const&... ts) |
Lei YU | dec8cf9 | 2019-02-21 17:47:05 +0800 | [diff] [blame] | 43 | { |
| 44 | std::array<char, 512> buffer; |
| 45 | std::string cmd = concat_string("pflash", ts...); |
| 46 | std::stringstream result; |
Lei YU | 5efca58 | 2019-02-25 17:05:29 +0800 | [diff] [blame] | 47 | int rc; |
| 48 | FILE* pipe = popen(cmd.c_str(), "r"); |
Lei YU | dec8cf9 | 2019-02-21 17:47:05 +0800 | [diff] [blame] | 49 | if (!pipe) |
| 50 | { |
| 51 | throw std::runtime_error("popen() failed!"); |
| 52 | } |
Lei YU | 5efca58 | 2019-02-25 17:05:29 +0800 | [diff] [blame] | 53 | while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) |
Lei YU | dec8cf9 | 2019-02-21 17:47:05 +0800 | [diff] [blame] | 54 | { |
| 55 | result << buffer.data(); |
| 56 | } |
Lei YU | 5efca58 | 2019-02-25 17:05:29 +0800 | [diff] [blame] | 57 | rc = pclose(pipe); |
| 58 | return {rc, result.str()}; |
Lei YU | dec8cf9 | 2019-02-21 17:47:05 +0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | std::string getPNORVersion() |
| 62 | { |
| 63 | // A signed version partition will have an extra 4K header starting with |
| 64 | // the magic number 17082011 in big endian: |
| 65 | // https://github.com/open-power/skiboot/blob/master/libstb/container.h#L47 |
| 66 | |
| 67 | constexpr uint8_t MAGIC[] = {0x17, 0x08, 0x20, 0x11}; |
| 68 | constexpr auto MAGIC_SIZE = sizeof(MAGIC); |
| 69 | static_assert(MAGIC_SIZE == 4); |
| 70 | |
| 71 | auto tmp = fs::temp_directory_path(); |
| 72 | std::string tmpDir(tmp / "versionXXXXXX"); |
| 73 | if (!mkdtemp(tmpDir.data())) |
| 74 | { |
| 75 | log<level::ERR>("Failed to create temp dir"); |
| 76 | return {}; |
| 77 | } |
| 78 | |
| 79 | fs::path versionFile = tmpDir; |
| 80 | versionFile /= "version"; |
| 81 | |
Lei YU | 5efca58 | 2019-02-25 17:05:29 +0800 | [diff] [blame] | 82 | auto [rc, r] = |
| 83 | pflash("-P VERSION -r", versionFile.string(), "2>&1 > /dev/null"); |
| 84 | if (rc != 0) |
| 85 | { |
| 86 | log<level::ERR>("Failed to read VERSION", entry("RETURNCODE=%d", rc)); |
| 87 | return {}; |
| 88 | } |
| 89 | |
Lei YU | dec8cf9 | 2019-02-21 17:47:05 +0800 | [diff] [blame] | 90 | std::ifstream f(versionFile.c_str(), std::ios::in | std::ios::binary); |
| 91 | uint8_t magic[MAGIC_SIZE]; |
| 92 | std::string version; |
| 93 | |
| 94 | f.read(reinterpret_cast<char*>(magic), MAGIC_SIZE); |
| 95 | f.seekg(0, std::ios::beg); |
| 96 | if (std::memcmp(magic, MAGIC, MAGIC_SIZE) == 0) |
| 97 | { |
| 98 | // Skip the first 4K header |
| 99 | f.ignore(4096); |
| 100 | } |
| 101 | |
| 102 | getline(f, version, '\0'); |
| 103 | f.close(); |
| 104 | |
| 105 | // Clear the temp dir |
| 106 | std::error_code ec; |
| 107 | fs::remove_all(tmpDir, ec); |
| 108 | if (ec) |
| 109 | { |
| 110 | log<level::ERR>("Failed to remove temp dir", |
| 111 | entry("DIR=%s", tmpDir.c_str()), |
| 112 | entry("ERR=%s", ec.message().c_str())); |
| 113 | } |
| 114 | |
| 115 | return version; |
| 116 | } |
| 117 | |
Lei YU | 6cecc9b | 2019-02-26 17:27:23 +0800 | [diff] [blame] | 118 | void pnorClear(const std::string& part, bool shouldEcc = true) |
Lei YU | a7b4ade | 2019-02-25 17:49:29 +0800 | [diff] [blame] | 119 | { |
| 120 | int rc; |
| 121 | std::tie(rc, std::ignore) = |
Lei YU | 6cecc9b | 2019-02-26 17:27:23 +0800 | [diff] [blame] | 122 | utils::pflash("-P", part, shouldEcc ? "-c" : "-e", "-f >/dev/null"); |
Lei YU | a7b4ade | 2019-02-25 17:49:29 +0800 | [diff] [blame] | 123 | if (rc != 0) |
| 124 | { |
| 125 | log<level::ERR>("Failed to clear partition", |
| 126 | entry("PART=%s", part.c_str()), |
| 127 | entry("RETURNCODE=%d", rc)); |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | log<level::INFO>("Clear partition successfully", |
| 132 | entry("PART=%s", part.c_str())); |
| 133 | } |
| 134 | } |
| 135 | |
Lei YU | 6cecc9b | 2019-02-26 17:27:23 +0800 | [diff] [blame] | 136 | // The pair contains the partition name and if it should use ECC clear |
| 137 | using PartClear = std::pair<std::string, bool>; |
| 138 | |
| 139 | std::vector<PartClear> getPartsToClear(const std::string& info) |
| 140 | { |
| 141 | std::vector<PartClear> ret; |
| 142 | std::istringstream iss(info); |
| 143 | std::string line; |
| 144 | |
| 145 | while (std::getline(iss, line)) |
| 146 | { |
| 147 | // Each line looks like |
| 148 | // ID=06 MVPD 0x0012d000..0x001bd000 (actual=0x00090000) [E--P--F-C-] |
| 149 | // Flag 'F' means REPROVISION |
Alexander Filippov | fa9a6be | 2019-12-18 12:06:47 +0300 | [diff] [blame] | 150 | // Flag 'E' means ECC required |
Lei YU | 0783076 | 2019-03-01 17:17:28 +0800 | [diff] [blame] | 151 | auto pos = line.find('['); |
| 152 | if (pos == std::string::npos) |
| 153 | { |
| 154 | continue; |
| 155 | } |
| 156 | auto flags = line.substr(pos); |
Lei YU | 6cecc9b | 2019-02-26 17:27:23 +0800 | [diff] [blame] | 157 | if (flags.find('F') != std::string::npos) |
| 158 | { |
| 159 | // This is a partition to be cleared |
Lei YU | 0783076 | 2019-03-01 17:17:28 +0800 | [diff] [blame] | 160 | pos = line.find_first_of(' '); // After "ID=xx" |
| 161 | if (pos == std::string::npos) |
| 162 | { |
| 163 | continue; |
| 164 | } |
| 165 | line = line.substr(pos); // Skiping "ID=xx" |
| 166 | |
| 167 | pos = line.find_first_not_of(' '); // After spaces |
| 168 | if (pos == std::string::npos) |
| 169 | { |
| 170 | continue; |
| 171 | } |
| 172 | line = line.substr(pos); // Skipping spaces |
| 173 | |
| 174 | pos = line.find_first_of(' '); // The end of part name |
| 175 | if (pos == std::string::npos) |
| 176 | { |
| 177 | continue; |
| 178 | } |
| 179 | line = line.substr(0, pos); // The part name |
| 180 | |
Alexander Filippov | fa9a6be | 2019-12-18 12:06:47 +0300 | [diff] [blame] | 181 | bool ecc = flags.find('E') != std::string::npos; |
Lei YU | 6cecc9b | 2019-02-26 17:27:23 +0800 | [diff] [blame] | 182 | ret.emplace_back(line, ecc); |
| 183 | } |
| 184 | } |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | // Get partitions that should be cleared |
| 189 | std::vector<PartClear> getPartsToClear() |
| 190 | { |
| 191 | const auto& [rc, pflashInfo] = pflash("-i | grep ^ID | grep 'F'"); |
| 192 | return getPartsToClear(pflashInfo); |
| 193 | } |
| 194 | |
Lei YU | dec8cf9 | 2019-02-21 17:47:05 +0800 | [diff] [blame] | 195 | } // namespace utils |
| 196 | |
Lei YU | 322f3f4 | 2019-02-21 16:10:41 +0800 | [diff] [blame] | 197 | namespace openpower |
| 198 | { |
| 199 | namespace software |
| 200 | { |
| 201 | namespace updater |
| 202 | { |
Lei YU | 5efca58 | 2019-02-25 17:05:29 +0800 | [diff] [blame] | 203 | |
Lei YU | 322f3f4 | 2019-02-21 16:10:41 +0800 | [diff] [blame] | 204 | std::unique_ptr<Activation> ItemUpdaterStatic::createActivationObject( |
| 205 | const std::string& path, const std::string& versionId, |
| 206 | const std::string& extVersion, |
| 207 | sdbusplus::xyz::openbmc_project::Software::server::Activation::Activations |
| 208 | activationStatus, |
| 209 | AssociationList& assocs) |
| 210 | { |
Lei YU | b53425d | 2019-02-22 11:38:40 +0800 | [diff] [blame] | 211 | return std::make_unique<ActivationStatic>( |
| 212 | bus, path, *this, versionId, extVersion, activationStatus, assocs); |
Lei YU | 322f3f4 | 2019-02-21 16:10:41 +0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | std::unique_ptr<Version> ItemUpdaterStatic::createVersionObject( |
| 216 | const std::string& objPath, const std::string& versionId, |
| 217 | const std::string& versionString, |
| 218 | sdbusplus::xyz::openbmc_project::Software::server::Version::VersionPurpose |
| 219 | versionPurpose, |
| 220 | const std::string& filePath) |
| 221 | { |
Lei YU | b53425d | 2019-02-22 11:38:40 +0800 | [diff] [blame] | 222 | auto version = std::make_unique<Version>( |
| 223 | bus, objPath, *this, versionId, versionString, versionPurpose, filePath, |
| 224 | std::bind(&ItemUpdaterStatic::erase, this, std::placeholders::_1)); |
| 225 | version->deleteObject = std::make_unique<Delete>(bus, objPath, *version); |
| 226 | return version; |
Lei YU | 322f3f4 | 2019-02-21 16:10:41 +0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | bool ItemUpdaterStatic::validateImage(const std::string& path) |
| 230 | { |
Lei YU | b53425d | 2019-02-22 11:38:40 +0800 | [diff] [blame] | 231 | // There is no need to validate static layout pnor |
Lei YU | 322f3f4 | 2019-02-21 16:10:41 +0800 | [diff] [blame] | 232 | return true; |
| 233 | } |
| 234 | |
| 235 | void ItemUpdaterStatic::processPNORImage() |
| 236 | { |
Lei YU | dec8cf9 | 2019-02-21 17:47:05 +0800 | [diff] [blame] | 237 | auto fullVersion = utils::getPNORVersion(); |
| 238 | |
| 239 | const auto& [version, extendedVersion] = Version::getVersions(fullVersion); |
| 240 | auto id = Version::getId(version); |
| 241 | |
Lei YU | 91add6d | 2019-03-01 14:23:40 +0800 | [diff] [blame] | 242 | if (id.empty()) |
| 243 | { |
| 244 | // Possibly a corrupted PNOR |
| 245 | return; |
| 246 | } |
| 247 | |
Lei YU | dec8cf9 | 2019-02-21 17:47:05 +0800 | [diff] [blame] | 248 | auto activationState = server::Activation::Activations::Active; |
| 249 | if (version.empty()) |
| 250 | { |
| 251 | log<level::ERR>("Failed to read version", |
| 252 | entry("VERSION=%s", fullVersion.c_str())); |
| 253 | activationState = server::Activation::Activations::Invalid; |
| 254 | } |
| 255 | |
| 256 | if (extendedVersion.empty()) |
| 257 | { |
| 258 | log<level::ERR>("Failed to read extendedVersion", |
| 259 | entry("VERSION=%s", fullVersion.c_str())); |
| 260 | activationState = server::Activation::Activations::Invalid; |
| 261 | } |
| 262 | |
| 263 | auto purpose = server::Version::VersionPurpose::Host; |
| 264 | auto path = fs::path(SOFTWARE_OBJPATH) / id; |
| 265 | AssociationList associations = {}; |
| 266 | |
| 267 | if (activationState == server::Activation::Activations::Active) |
| 268 | { |
| 269 | // Create an association to the host inventory item |
| 270 | associations.emplace_back(std::make_tuple(ACTIVATION_FWD_ASSOCIATION, |
| 271 | ACTIVATION_REV_ASSOCIATION, |
| 272 | HOST_INVENTORY_PATH)); |
| 273 | |
| 274 | // Create an active association since this image is active |
| 275 | createActiveAssociation(path); |
| 276 | } |
| 277 | |
Lei YU | b53425d | 2019-02-22 11:38:40 +0800 | [diff] [blame] | 278 | // Create Activation instance for this version. |
| 279 | activations.insert(std::make_pair( |
| 280 | id, std::make_unique<ActivationStatic>(bus, path, *this, id, |
| 281 | extendedVersion, activationState, |
| 282 | associations))); |
| 283 | |
| 284 | // If Active, create RedundancyPriority instance for this version. |
| 285 | if (activationState == server::Activation::Activations::Active) |
| 286 | { |
| 287 | // For now only one PNOR is supported with static layout |
| 288 | activations.find(id)->second->redundancyPriority = |
| 289 | std::make_unique<RedundancyPriority>( |
| 290 | bus, path, *(activations.find(id)->second), 0); |
| 291 | } |
| 292 | |
Lei YU | dec8cf9 | 2019-02-21 17:47:05 +0800 | [diff] [blame] | 293 | // Create Version instance for this version. |
| 294 | auto versionPtr = std::make_unique<Version>( |
| 295 | bus, path, *this, id, version, purpose, "", |
| 296 | std::bind(&ItemUpdaterStatic::erase, this, std::placeholders::_1)); |
| 297 | versionPtr->deleteObject = std::make_unique<Delete>(bus, path, *versionPtr); |
| 298 | versions.insert(std::make_pair(id, std::move(versionPtr))); |
| 299 | |
| 300 | if (!id.empty()) |
| 301 | { |
| 302 | updateFunctionalAssociation(id); |
| 303 | } |
Lei YU | 322f3f4 | 2019-02-21 16:10:41 +0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void ItemUpdaterStatic::reset() |
| 307 | { |
Lei YU | 6cecc9b | 2019-02-26 17:27:23 +0800 | [diff] [blame] | 308 | auto partitions = utils::getPartsToClear(); |
Lei YU | a7b4ade | 2019-02-25 17:49:29 +0800 | [diff] [blame] | 309 | |
Lei YU | e499446 | 2019-03-14 14:41:53 +0800 | [diff] [blame] | 310 | utils::hiomapdSuspend(bus); |
Lei YU | a7b4ade | 2019-02-25 17:49:29 +0800 | [diff] [blame] | 311 | |
Lei YU | a7b4ade | 2019-02-25 17:49:29 +0800 | [diff] [blame] | 312 | for (auto p : partitions) |
| 313 | { |
| 314 | utils::pnorClear(p.first, p.second); |
| 315 | } |
| 316 | |
Lei YU | e499446 | 2019-03-14 14:41:53 +0800 | [diff] [blame] | 317 | utils::hiomapdResume(bus); |
Lei YU | 322f3f4 | 2019-02-21 16:10:41 +0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | bool ItemUpdaterStatic::isVersionFunctional(const std::string& versionId) |
| 321 | { |
Lei YU | a2e6716 | 2019-02-22 17:35:24 +0800 | [diff] [blame] | 322 | return versionId == functionalVersionId; |
Lei YU | 322f3f4 | 2019-02-21 16:10:41 +0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void ItemUpdaterStatic::freePriority(uint8_t value, |
| 326 | const std::string& versionId) |
| 327 | { |
| 328 | } |
| 329 | |
| 330 | void ItemUpdaterStatic::deleteAll() |
| 331 | { |
Lei YU | a2e6716 | 2019-02-22 17:35:24 +0800 | [diff] [blame] | 332 | // Static layout has only one active and function pnor |
| 333 | // There is no implementation for this interface |
Lei YU | 322f3f4 | 2019-02-21 16:10:41 +0800 | [diff] [blame] | 334 | } |
| 335 | |
Lei YU | 6da3dae | 2019-02-28 14:26:37 +0800 | [diff] [blame] | 336 | bool ItemUpdaterStatic::freeSpace() |
Lei YU | 322f3f4 | 2019-02-21 16:10:41 +0800 | [diff] [blame] | 337 | { |
Lei YU | a2e6716 | 2019-02-22 17:35:24 +0800 | [diff] [blame] | 338 | // For now assume static layout only has 1 active PNOR, |
| 339 | // so erase the active PNOR |
| 340 | for (const auto& iter : activations) |
| 341 | { |
| 342 | if (iter.second.get()->activation() == |
| 343 | server::Activation::Activations::Active) |
| 344 | { |
Lei YU | 6da3dae | 2019-02-28 14:26:37 +0800 | [diff] [blame] | 345 | return erase(iter.second->versionId); |
Lei YU | a2e6716 | 2019-02-22 17:35:24 +0800 | [diff] [blame] | 346 | } |
| 347 | } |
Lei YU | 91add6d | 2019-03-01 14:23:40 +0800 | [diff] [blame] | 348 | // No active PNOR means PNOR is empty or corrupted |
| 349 | return true; |
Lei YU | a2e6716 | 2019-02-22 17:35:24 +0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void ItemUpdaterStatic::updateFunctionalAssociation( |
| 353 | const std::string& versionId) |
| 354 | { |
| 355 | functionalVersionId = versionId; |
| 356 | ItemUpdater::updateFunctionalAssociation(versionId); |
Lei YU | 322f3f4 | 2019-02-21 16:10:41 +0800 | [diff] [blame] | 357 | } |
| 358 | |
Lei YU | 716de5b | 2019-03-01 16:03:53 +0800 | [diff] [blame] | 359 | void GardResetStatic::reset() |
Lei YU | 322f3f4 | 2019-02-21 16:10:41 +0800 | [diff] [blame] | 360 | { |
Lei YU | 5efca58 | 2019-02-25 17:05:29 +0800 | [diff] [blame] | 361 | // Clear gard partition |
Lei YU | e499446 | 2019-03-14 14:41:53 +0800 | [diff] [blame] | 362 | utils::hiomapdSuspend(bus); |
Lei YU | 5efca58 | 2019-02-25 17:05:29 +0800 | [diff] [blame] | 363 | |
Lei YU | a7b4ade | 2019-02-25 17:49:29 +0800 | [diff] [blame] | 364 | utils::pnorClear("GUARD"); |
Lei YU | 5efca58 | 2019-02-25 17:05:29 +0800 | [diff] [blame] | 365 | |
Lei YU | e499446 | 2019-03-14 14:41:53 +0800 | [diff] [blame] | 366 | utils::hiomapdResume(bus); |
Lei YU | 322f3f4 | 2019-02-21 16:10:41 +0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | } // namespace updater |
| 370 | } // namespace software |
| 371 | } // namespace openpower |