#include "config.h" #include "item_updater_static.hpp" #include "activation_static.hpp" #include "version.hpp" #include #include #include #include #include #include #include #include #include using namespace sdbusplus::xyz::openbmc_project::Common::Error; using namespace phosphor::logging; using sdbusplus::exception::SdBusError; // When you see server:: you know we're referencing our base class namespace server = sdbusplus::xyz::openbmc_project::Software::server; namespace fs = std::filesystem; namespace utils { template std::string concat_string(Ts const&... ts) { std::stringstream s; ((s << ts << " "), ...) << std::endl; return s.str(); } // Helper function to run pflash command // Returns return code and the stdout template std::pair pflash(Ts const&... ts) { std::array buffer; std::string cmd = concat_string("pflash", ts...); std::stringstream result; int rc; FILE* pipe = popen(cmd.c_str(), "r"); if (!pipe) { throw std::runtime_error("popen() failed!"); } while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) { result << buffer.data(); } rc = pclose(pipe); return {rc, result.str()}; } std::string getPNORVersion() { // A signed version partition will have an extra 4K header starting with // the magic number 17082011 in big endian: // https://github.com/open-power/skiboot/blob/master/libstb/container.h#L47 constexpr uint8_t MAGIC[] = {0x17, 0x08, 0x20, 0x11}; constexpr auto MAGIC_SIZE = sizeof(MAGIC); static_assert(MAGIC_SIZE == 4); auto tmp = fs::temp_directory_path(); std::string tmpDir(tmp / "versionXXXXXX"); if (!mkdtemp(tmpDir.data())) { log("Failed to create temp dir"); return {}; } fs::path versionFile = tmpDir; versionFile /= "version"; auto [rc, r] = pflash("-P VERSION -r", versionFile.string(), "2>&1 > /dev/null"); if (rc != 0) { log("Failed to read VERSION", entry("RETURNCODE=%d", rc)); return {}; } std::ifstream f(versionFile.c_str(), std::ios::in | std::ios::binary); uint8_t magic[MAGIC_SIZE]; std::string version; f.read(reinterpret_cast(magic), MAGIC_SIZE); f.seekg(0, std::ios::beg); if (std::memcmp(magic, MAGIC, MAGIC_SIZE) == 0) { // Skip the first 4K header f.ignore(4096); } getline(f, version, '\0'); f.close(); // Clear the temp dir std::error_code ec; fs::remove_all(tmpDir, ec); if (ec) { log("Failed to remove temp dir", entry("DIR=%s", tmpDir.c_str()), entry("ERR=%s", ec.message().c_str())); } return version; } } // namespace utils namespace openpower { namespace software { namespace updater { // TODO: Change paths once openbmc/openbmc#1663 is completed. constexpr auto MBOXD_INTERFACE = "org.openbmc.mboxd"; constexpr auto MBOXD_PATH = "/org/openbmc/mboxd"; std::unique_ptr ItemUpdaterStatic::createActivationObject( const std::string& path, const std::string& versionId, const std::string& extVersion, sdbusplus::xyz::openbmc_project::Software::server::Activation::Activations activationStatus, AssociationList& assocs) { return std::make_unique( bus, path, *this, versionId, extVersion, activationStatus, assocs); } std::unique_ptr ItemUpdaterStatic::createVersionObject( const std::string& objPath, const std::string& versionId, const std::string& versionString, sdbusplus::xyz::openbmc_project::Software::server::Version::VersionPurpose versionPurpose, const std::string& filePath) { auto version = std::make_unique( bus, objPath, *this, versionId, versionString, versionPurpose, filePath, std::bind(&ItemUpdaterStatic::erase, this, std::placeholders::_1)); version->deleteObject = std::make_unique(bus, objPath, *version); return version; } bool ItemUpdaterStatic::validateImage(const std::string& path) { // There is no need to validate static layout pnor return true; } void ItemUpdaterStatic::processPNORImage() { auto fullVersion = utils::getPNORVersion(); const auto& [version, extendedVersion] = Version::getVersions(fullVersion); auto id = Version::getId(version); auto activationState = server::Activation::Activations::Active; if (version.empty()) { log("Failed to read version", entry("VERSION=%s", fullVersion.c_str())); activationState = server::Activation::Activations::Invalid; } if (extendedVersion.empty()) { log("Failed to read extendedVersion", entry("VERSION=%s", fullVersion.c_str())); activationState = server::Activation::Activations::Invalid; } auto purpose = server::Version::VersionPurpose::Host; auto path = fs::path(SOFTWARE_OBJPATH) / id; AssociationList associations = {}; if (activationState == server::Activation::Activations::Active) { // Create an association to the host inventory item associations.emplace_back(std::make_tuple(ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION, HOST_INVENTORY_PATH)); // Create an active association since this image is active createActiveAssociation(path); } // Create Activation instance for this version. activations.insert(std::make_pair( id, std::make_unique(bus, path, *this, id, extendedVersion, activationState, associations))); // If Active, create RedundancyPriority instance for this version. if (activationState == server::Activation::Activations::Active) { // For now only one PNOR is supported with static layout activations.find(id)->second->redundancyPriority = std::make_unique( bus, path, *(activations.find(id)->second), 0); } // Create Version instance for this version. auto versionPtr = std::make_unique( bus, path, *this, id, version, purpose, "", std::bind(&ItemUpdaterStatic::erase, this, std::placeholders::_1)); versionPtr->deleteObject = std::make_unique(bus, path, *versionPtr); versions.insert(std::make_pair(id, std::move(versionPtr))); if (!id.empty()) { updateFunctionalAssociation(id); } } void ItemUpdaterStatic::reset() { } bool ItemUpdaterStatic::isVersionFunctional(const std::string& versionId) { return versionId == functionalVersionId; } void ItemUpdaterStatic::freePriority(uint8_t value, const std::string& versionId) { } void ItemUpdaterStatic::deleteAll() { // Static layout has only one active and function pnor // There is no implementation for this interface } void ItemUpdaterStatic::freeSpace() { // For now assume static layout only has 1 active PNOR, // so erase the active PNOR for (const auto& iter : activations) { if (iter.second.get()->activation() == server::Activation::Activations::Active) { erase(iter.second->versionId); break; } } } void ItemUpdaterStatic::updateFunctionalAssociation( const std::string& versionId) { functionalVersionId = versionId; ItemUpdater::updateFunctionalAssociation(versionId); } void GardReset::reset() { // Clear gard partition std::vector mboxdArgs; int rc; auto dbusCall = bus.new_method_call(MBOXD_INTERFACE, MBOXD_PATH, MBOXD_INTERFACE, "cmd"); // Suspend mboxd - no args required. dbusCall.append(static_cast(3), mboxdArgs); try { bus.call_noreply(dbusCall); } catch (const SdBusError& e) { log("Error in mboxd suspend call", entry("ERROR=%s", e.what())); elog(); } // Clear guard partition std::tie(rc, std::ignore) = utils::pflash("-P GUARD -c -f >/dev/null"); if (rc != 0) { log("Failed to clear GUARD", entry("RETURNCODE=%d", rc)); } else { log("Clear GUARD successfully"); } dbusCall = bus.new_method_call(MBOXD_INTERFACE, MBOXD_PATH, MBOXD_INTERFACE, "cmd"); // Resume mboxd with arg 1, indicating that the flash is modified. mboxdArgs.push_back(1); dbusCall.append(static_cast(4), mboxdArgs); try { bus.call_noreply(dbusCall); } catch (const SdBusError& e) { log("Error in mboxd resume call", entry("ERROR=%s", e.what())); elog(); } } } // namespace updater } // namespace software } // namespace openpower