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)); |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame^] | 35 | std::string filePath; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 36 | |
| 37 | for (const auto& intf : interfaces) |
| 38 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 39 | if (intf.first == VERSION_IFACE) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 40 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 41 | for (const auto& property : intf.second) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 42 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 43 | if (property.first == "Purpose") |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 44 | { |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 45 | std::string str = sdbusplus::message::variant_ns:: |
| 46 | get<std::string>(property.second); |
| 47 | purpose = server::Version:: |
| 48 | convertVersionPurposeFromString(str); |
| 49 | } |
| 50 | else if (property.first == "Version") |
| 51 | { |
| 52 | version = sdbusplus::message::variant_ns:: |
| 53 | get<std::string>(property.second); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | } |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame^] | 57 | else if (intf.first == FILEPATH_IFACE) |
| 58 | { |
| 59 | for (const auto& property : intf.second) |
| 60 | { |
| 61 | if (property.first == "Path") |
| 62 | { |
| 63 | filePath = sdbusplus::message::variant_ns::get< |
| 64 | std::string>(property.second); |
| 65 | } |
| 66 | } |
| 67 | } |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 68 | } |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 69 | if (version.empty() || |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame^] | 70 | filePath.empty() || |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 71 | (purpose != server::Version::VersionPurpose::BMC && |
| 72 | purpose != server::Version::VersionPurpose::System)) |
| 73 | { |
| 74 | return; |
| 75 | } |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 76 | |
| 77 | // Version id is the last item in the path |
| 78 | auto pos = path.rfind("/"); |
| 79 | if (pos == std::string::npos) |
| 80 | { |
| 81 | log<level::ERR>("No version id found in object path", |
| 82 | entry("OBJPATH=%s", path)); |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 83 | return; |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | auto versionId = path.substr(pos + 1); |
| 87 | |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 88 | if (activations.find(versionId) == activations.end()) |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 89 | { |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 90 | // Determine the Activation state by processing the given image dir. |
| 91 | auto activationState = server::Activation::Activations::Invalid; |
| 92 | ItemUpdater::ActivationStatus result = ItemUpdater:: |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame^] | 93 | validateSquashFSImage(filePath); |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 94 | if (result == ItemUpdater::ActivationStatus::ready) |
| 95 | { |
| 96 | activationState = server::Activation::Activations::Ready; |
| 97 | } |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 98 | activations.insert(std::make_pair( |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 99 | versionId, |
| 100 | std::make_unique<Activation>( |
| 101 | bus, |
| 102 | path, |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 103 | versionId, |
Saqib Khan | 705f1bf | 2017-06-09 23:58:38 -0500 | [diff] [blame] | 104 | activationState))); |
| 105 | versions.insert(std::make_pair( |
| 106 | versionId, |
| 107 | std::make_unique<phosphor::software:: |
| 108 | manager::Version>( |
| 109 | bus, |
| 110 | path, |
| 111 | version, |
| 112 | purpose, |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame^] | 113 | filePath))); |
Gunnar Mills | 2ce7da2 | 2017-05-04 15:37:56 -0500 | [diff] [blame] | 114 | } |
Patrick Williams | e75d10f | 2017-05-30 16:56:32 -0500 | [diff] [blame] | 115 | return; |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 118 | void ItemUpdater::processBMCImage() |
| 119 | { |
| 120 | auto purpose = server::Version::VersionPurpose::BMC; |
| 121 | auto version = phosphor::software::manager::Version::getBMCVersion(); |
| 122 | auto id = phosphor::software::manager::Version::getId(version); |
| 123 | auto path = std::string{SOFTWARE_OBJPATH} + '/' + id; |
| 124 | activations.insert(std::make_pair( |
| 125 | id, |
| 126 | std::make_unique<Activation>( |
| 127 | bus, |
| 128 | path, |
| 129 | id, |
| 130 | server::Activation::Activations::Active))); |
| 131 | versions.insert(std::make_pair( |
| 132 | id, |
| 133 | std::make_unique<phosphor::software:: |
| 134 | manager::Version>( |
| 135 | bus, |
| 136 | path, |
| 137 | version, |
| 138 | purpose, |
| 139 | ""))); |
| 140 | return; |
| 141 | } |
| 142 | |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 143 | ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage( |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame^] | 144 | const std::string& filePath) |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 145 | { |
| 146 | |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame^] | 147 | fs::path file(filePath); |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 148 | file /= bmcImage; |
| 149 | std::ifstream efile(file.c_str()); |
| 150 | |
| 151 | if (efile.good() == 1) |
| 152 | { |
| 153 | return ItemUpdater::ActivationStatus::ready; |
| 154 | } |
| 155 | else |
| 156 | { |
Saqib Khan | 19177d3 | 2017-06-20 08:11:49 -0500 | [diff] [blame^] | 157 | log<level::ERR>("Failed to find the BMC image."); |
Saqib Khan | 35e83f3 | 2017-05-22 11:37:32 -0500 | [diff] [blame] | 158 | return ItemUpdater::ActivationStatus::invalid; |
| 159 | } |
| 160 | } |
| 161 | |
Gunnar Mills | ec1b41c | 2017-05-02 12:20:36 -0500 | [diff] [blame] | 162 | } // namespace updater |
| 163 | } // namespace software |
| 164 | } // namespace phosphor |