Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 1 | #include <string> |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame^] | 2 | #include <experimental/filesystem> |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 3 | #include <fstream> |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 4 | #include <phosphor-logging/log.hpp> |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 5 | #include <xyz/openbmc_project/Software/Version/server.hpp> |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 6 | #include "config.h" |
| 7 | #include "item_updater.hpp" |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 8 | #include "activation.hpp" |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 9 | |
| 10 | namespace openpower |
| 11 | { |
| 12 | namespace software |
| 13 | { |
Adriana Kobylak | befe5ce | 2017-04-05 15:57:44 -0500 | [diff] [blame] | 14 | namespace updater |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 15 | { |
| 16 | |
Saqib Khan | a8ade7e | 2017-04-12 10:27: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; |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame^] | 19 | namespace fs = std::experimental::filesystem; |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 20 | |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 21 | using namespace phosphor::logging; |
| 22 | |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 23 | constexpr auto squashFSImage = "pnor.xz.squashfs"; |
| 24 | |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 25 | void ItemUpdater::createActivation(sdbusplus::message::message& m) |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 26 | { |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 27 | sdbusplus::message::object_path objPath; |
| 28 | std::map<std::string, |
| 29 | std::map<std::string, |
| 30 | sdbusplus::message::variant<std::string>>> interfaces; |
| 31 | m.read(objPath, interfaces); |
| 32 | std::string path(std::move(objPath)); |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 33 | |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame^] | 34 | std::string filePath; |
| 35 | |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 36 | for (const auto& intf : interfaces) |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 37 | { |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 38 | if (intf.first == VERSION_IFACE) |
| 39 | { |
| 40 | for (const auto& property : intf.second) |
| 41 | { |
| 42 | if (property.first == "Purpose") |
| 43 | { |
| 44 | // Only process the Host and System images |
| 45 | std::string value = sdbusplus::message::variant_ns::get< |
| 46 | std::string>(property.second); |
| 47 | if ((value != convertForMessage( |
| 48 | server::Version::VersionPurpose::Host)) && |
| 49 | (value != convertForMessage( |
| 50 | server::Version::VersionPurpose::System))) |
| 51 | { |
| 52 | return; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -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 | } |
| 68 | } |
| 69 | if (filePath.empty()) |
| 70 | { |
| 71 | return; |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 74 | // Version id is the last item in the path |
| 75 | auto pos = path.rfind("/"); |
| 76 | if (pos == std::string::npos) |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 77 | { |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 78 | log<level::ERR>("No version id found in object path", |
| 79 | entry("OBJPATH=%s", path)); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | auto versionId = path.substr(pos + 1); |
| 84 | |
| 85 | if (activations.find(versionId) == activations.end()) |
| 86 | { |
| 87 | // Determine the Activation state by processing the given image dir. |
| 88 | auto activationState = server::Activation::Activations::Invalid; |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame^] | 89 | if (ItemUpdater::validateSquashFSImage(filePath) == 0) |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 90 | { |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 91 | activationState = server::Activation::Activations::Ready; |
| 92 | |
| 93 | // Load the squashfs image to pnor so that it is available to be |
| 94 | // activated when requested. |
| 95 | // This is done since the image on the BMC can be removed. |
| 96 | constexpr auto squashfsMountService = |
| 97 | "obmc-flash-bios-squashfsmount@"; |
| 98 | auto squashfsMountServiceFile = |
| 99 | std::string(squashfsMountService) + |
| 100 | versionId + |
| 101 | ".service"; |
| 102 | auto method = bus.new_method_call( |
| 103 | SYSTEMD_BUSNAME, |
| 104 | SYSTEMD_PATH, |
| 105 | SYSTEMD_INTERFACE, |
| 106 | "StartUnit"); |
| 107 | method.append(squashfsMountServiceFile, "replace"); |
| 108 | bus.call_noreply(method); |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame^] | 111 | fs::path manifestPath(filePath); |
| 112 | manifestPath /= MANIFEST_FILE; |
| 113 | auto extendedVersion = ItemUpdater::getExtendedVersion(manifestPath); |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 114 | activations.insert(std::make_pair( |
| 115 | versionId, |
| 116 | std::make_unique<Activation>( |
| 117 | bus, |
| 118 | path, |
| 119 | versionId, |
| 120 | extendedVersion, |
| 121 | activationState))); |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 122 | } |
Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame] | 123 | return; |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 126 | std::string ItemUpdater::getExtendedVersion(const std::string& manifestFilePath) |
| 127 | { |
| 128 | constexpr auto extendedVersionKey = "extended_version="; |
| 129 | constexpr auto extendedVersionKeySize = strlen(extendedVersionKey); |
| 130 | |
| 131 | if (manifestFilePath.empty()) |
| 132 | { |
| 133 | log<level::ERR>("Error MANIFESTFilePath is empty"); |
| 134 | throw std::runtime_error("MANIFESTFilePath is empty"); |
| 135 | } |
| 136 | |
| 137 | std::string extendedVersion{}; |
| 138 | std::ifstream efile; |
| 139 | std::string line; |
| 140 | efile.exceptions(std::ifstream::failbit |
| 141 | | std::ifstream::badbit |
| 142 | | std::ifstream::eofbit); |
| 143 | |
| 144 | try |
| 145 | { |
| 146 | efile.open(manifestFilePath); |
| 147 | while (getline(efile, line)) |
| 148 | { |
| 149 | if (line.compare(0, extendedVersionKeySize, |
| 150 | extendedVersionKey) == 0) |
| 151 | { |
| 152 | extendedVersion = line.substr(extendedVersionKeySize); |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | efile.close(); |
| 157 | } |
| 158 | catch (const std::exception& e) |
| 159 | { |
| 160 | log<level::ERR>("Error in reading Host MANIFEST file"); |
| 161 | } |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 162 | return extendedVersion; |
| 163 | } |
| 164 | |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame^] | 165 | int ItemUpdater::validateSquashFSImage(const std::string& filePath) |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 166 | { |
Adriana Kobylak | 5ba6b10 | 2017-05-19 09:41:27 -0500 | [diff] [blame^] | 167 | fs::path file(filePath); |
| 168 | file /= squashFSImage; |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 169 | std::ifstream efile(file.c_str()); |
| 170 | |
| 171 | if (efile.good() == 1) |
| 172 | { |
| 173 | return 0; |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | log<level::ERR>("Failed to find the SquashFS image."); |
| 178 | return -1; |
| 179 | } |
| 180 | } |
| 181 | |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 182 | void ItemUpdater::reset() |
| 183 | { |
| 184 | for(const auto& it : activations) |
| 185 | { |
| 186 | auto serviceFile = "obmc-flash-bios-ubiumount-rw@" + it.first + |
| 187 | ".service"; |
| 188 | |
| 189 | // Remove the read-write partitions. |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 190 | auto method = bus.new_method_call( |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 191 | SYSTEMD_BUSNAME, |
| 192 | SYSTEMD_PATH, |
| 193 | SYSTEMD_INTERFACE, |
| 194 | "StartUnit"); |
| 195 | method.append(serviceFile, "replace"); |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 196 | bus.call_noreply(method); |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // Remove the preserved partition. |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 200 | auto method = bus.new_method_call( |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 201 | SYSTEMD_BUSNAME, |
| 202 | SYSTEMD_PATH, |
| 203 | SYSTEMD_INTERFACE, |
| 204 | "StartUnit"); |
| 205 | method.append("obmc-flash-bios-ubiumount-prsv.service", "replace"); |
Adriana Kobylak | d6a549e | 2017-05-10 16:23:01 -0500 | [diff] [blame] | 206 | bus.call_noreply(method); |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 207 | |
| 208 | return; |
| 209 | } |
| 210 | |
Adriana Kobylak | befe5ce | 2017-04-05 15:57:44 -0500 | [diff] [blame] | 211 | } // namespace updater |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 212 | } // namespace software |
| 213 | } // namespace openpower |