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