Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 1 | #include <fstream> |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 2 | #include <string> |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 3 | #include <phosphor-logging/log.hpp> |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 4 | #include "config.h" |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 5 | #include "item_updater.hpp" |
| 6 | #include "xyz/openbmc_project/Software/Version/server.hpp" |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 7 | #include <experimental/filesystem> |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame^] | 8 | #include "version.hpp" |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 9 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace software |
| 13 | { |
| 14 | namespace updater |
| 15 | { |
| 16 | |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 17 | // When you see server:: you know we're referencing our base class |
| 18 | namespace server = sdbusplus::xyz::openbmc_project::Software::server; |
| 19 | |
| 20 | using namespace phosphor::logging; |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 21 | namespace fs = std::experimental::filesystem; |
| 22 | |
| 23 | constexpr auto bmcImage = "image-rofs"; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 24 | |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 25 | void ItemUpdater::createActivation(sdbusplus::message::message& msg) |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 26 | { |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 27 | sdbusplus::message::object_path objPath; |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame^] | 28 | auto purpose = server::Version::VersionPurpose::Unknown; |
| 29 | std::string version; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 30 | std::map<std::string, |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 31 | std::map<std::string, |
| 32 | sdbusplus::message::variant<std::string>>> interfaces; |
| 33 | msg.read(objPath, interfaces); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 34 | std::string path(std::move(objPath)); |
| 35 | |
| 36 | for (const auto& intf : interfaces) |
| 37 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame^] | 38 | if (intf.first == VERSION_IFACE) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 39 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame^] | 40 | for (const auto& property : intf.second) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 41 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame^] | 42 | if (property.first == "Purpose") |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 43 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame^] | 44 | std::string str = sdbusplus::message::variant_ns:: |
| 45 | get<std::string>(property.second); |
| 46 | purpose = server::Version:: |
| 47 | convertVersionPurposeFromString(str); |
| 48 | } |
| 49 | else if (property.first == "Version") |
| 50 | { |
| 51 | version = sdbusplus::message::variant_ns:: |
| 52 | get<std::string>(property.second); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame^] | 57 | if (version.empty() || |
| 58 | (purpose != server::Version::VersionPurpose::BMC && |
| 59 | purpose != server::Version::VersionPurpose::System)) |
| 60 | { |
| 61 | return; |
| 62 | } |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 63 | |
| 64 | // Version id is the last item in the path |
| 65 | auto pos = path.rfind("/"); |
| 66 | if (pos == std::string::npos) |
| 67 | { |
| 68 | log<level::ERR>("No version id found in object path", |
| 69 | entry("OBJPATH=%s", path)); |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 70 | return; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | auto versionId = path.substr(pos + 1); |
| 74 | |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 75 | if (activations.find(versionId) == activations.end()) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 76 | { |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 77 | // Determine the Activation state by processing the given image dir. |
| 78 | auto activationState = server::Activation::Activations::Invalid; |
| 79 | ItemUpdater::ActivationStatus result = ItemUpdater:: |
| 80 | validateSquashFSImage(versionId); |
| 81 | if (result == ItemUpdater::ActivationStatus::ready) |
| 82 | { |
| 83 | activationState = server::Activation::Activations::Ready; |
| 84 | } |
| 85 | else if (result == ItemUpdater::ActivationStatus::active) |
| 86 | { |
| 87 | activationState = server::Activation::Activations::Active; |
| 88 | } |
| 89 | activations.insert(std::make_pair( |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame^] | 90 | versionId, |
| 91 | std::make_unique<Activation>( |
| 92 | bus, |
| 93 | path, |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 94 | versionId, |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame^] | 95 | activationState))); |
| 96 | versions.insert(std::make_pair( |
| 97 | versionId, |
| 98 | std::make_unique<phosphor::software:: |
| 99 | manager::Version>( |
| 100 | bus, |
| 101 | path, |
| 102 | version, |
| 103 | purpose, |
| 104 | ""))); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 105 | } |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 106 | return; |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 109 | ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage( |
| 110 | const std::string& versionId) |
| 111 | { |
| 112 | |
| 113 | // TODO openbmc/openbmc#1715 Check the Common.FilePath to |
| 114 | // determine the active image. |
| 115 | fs::path imageDirPath(IMG_UPLOAD_DIR); |
| 116 | imageDirPath /= versionId; |
| 117 | if (!fs::is_directory(imageDirPath)) |
| 118 | { |
| 119 | return ItemUpdater::ActivationStatus::active; |
| 120 | } |
| 121 | |
| 122 | fs::path file(imageDirPath); |
| 123 | file /= bmcImage; |
| 124 | std::ifstream efile(file.c_str()); |
| 125 | |
| 126 | if (efile.good() == 1) |
| 127 | { |
| 128 | return ItemUpdater::ActivationStatus::ready; |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | log<level::ERR>("Failed to find the SquashFS image."); |
| 133 | return ItemUpdater::ActivationStatus::invalid; |
| 134 | } |
| 135 | } |
| 136 | |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 137 | } // namespace updater |
| 138 | } // namespace software |
| 139 | } // namespace phosphor |