Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 1 | #include <string> |
| 2 | #include <fstream> |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 3 | #include <phosphor-logging/log.hpp> |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 4 | #include "config.h" |
| 5 | #include "item_updater.hpp" |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 6 | #include "activation.hpp" |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 7 | |
| 8 | namespace openpower |
| 9 | { |
| 10 | namespace software |
| 11 | { |
Adriana Kobylak | befe5ce | 2017-04-05 15:57:44 -0500 | [diff] [blame] | 12 | namespace updater |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 13 | { |
| 14 | |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 15 | // When you see server:: you know we're referencing our base class |
| 16 | namespace server = sdbusplus::xyz::openbmc_project::Software::server; |
| 17 | |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 18 | using namespace phosphor::logging; |
| 19 | |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 20 | constexpr auto squashFSImage = "pnor.xz.squashfs"; |
| 21 | |
Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame^] | 22 | void ItemUpdater::createActivation(sdbusplus::message::message&) |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 23 | { |
Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame^] | 24 | auto mapper = busItem.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 25 | MAPPER_INTERFACE, "GetSubTreePaths"); |
| 26 | mapper.append(SOFTWARE_OBJPATH, /* Depth */ 1, |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 27 | std::vector<std::string>({VERSION_IFACE})); |
| 28 | |
Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame^] | 29 | auto mapperResponseMsg = busItem.call(mapper); |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 30 | if (mapperResponseMsg.is_method_error()) |
| 31 | { |
| 32 | log<level::ERR>("Error in mapper call", |
| 33 | entry("PATH=%s", SOFTWARE_OBJPATH), |
| 34 | entry("INTERFACE=%s", VERSION_IFACE)); |
Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame^] | 35 | return; |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | std::vector<std::string> mapperResponse; |
| 39 | mapperResponseMsg.read(mapperResponse); |
| 40 | if (mapperResponse.empty()) |
| 41 | { |
| 42 | log<level::ERR>("Error reading mapper response", |
| 43 | entry("PATH=%s", SOFTWARE_OBJPATH), |
| 44 | entry("INTERFACE=%s", VERSION_IFACE)); |
Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame^] | 45 | return; |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 48 | auto extendedVersion = ItemUpdater::getExtendedVersion(MANIFEST_FILE); |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 49 | for (const auto& resp : mapperResponse) |
| 50 | { |
| 51 | // Version id is the last item in the path |
| 52 | auto pos = resp.rfind("/"); |
| 53 | if (pos == std::string::npos) |
| 54 | { |
| 55 | log<level::ERR>("No version id found in object path", |
| 56 | entry("OBJPATH=%s", resp)); |
Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame^] | 57 | return; |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 58 | } |
| 59 | |
Adriana Kobylak | 268616b | 2017-04-05 15:23:30 -0500 | [diff] [blame] | 60 | auto versionId = resp.substr(pos + 1); |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 61 | |
Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame^] | 62 | if (activations.find(versionId) == activations.end()) |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 63 | { |
Adriana Kobylak | 2fdb931 | 2017-05-14 19:08:26 -0500 | [diff] [blame] | 64 | // Determine the Activation state by processing the given image dir. |
| 65 | auto activationState = server::Activation::Activations::Invalid; |
| 66 | if (ItemUpdater::validateSquashFSImage(versionId) == 0) |
| 67 | { |
| 68 | activationState = server::Activation::Activations::Ready; |
| 69 | |
| 70 | // Load the squashfs image to pnor so that it is available to be |
| 71 | // activated when requested. |
| 72 | // This is done since the image on the BMC can be removed. |
| 73 | constexpr auto squashfsMountService = |
Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame^] | 74 | "obmc-flash-bios-squashfsmount@"; |
Adriana Kobylak | 2fdb931 | 2017-05-14 19:08:26 -0500 | [diff] [blame] | 75 | auto squashfsMountServiceFile = |
Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame^] | 76 | std::string(squashfsMountService) + |
| 77 | versionId + ".service"; |
| 78 | auto method = busItem.new_method_call(SYSTEMD_BUSNAME, |
| 79 | SYSTEMD_PATH, |
| 80 | SYSTEMD_INTERFACE, |
| 81 | "StartUnit"); |
Adriana Kobylak | 2fdb931 | 2017-05-14 19:08:26 -0500 | [diff] [blame] | 82 | method.append(squashfsMountServiceFile, "replace"); |
Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame^] | 83 | busItem.call_noreply(method); |
Adriana Kobylak | 2fdb931 | 2017-05-14 19:08:26 -0500 | [diff] [blame] | 84 | } |
| 85 | |
Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame^] | 86 | activations.insert(std::make_pair( |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 87 | versionId, |
| 88 | std::make_unique<Activation>( |
Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame^] | 89 | busItem, |
Adriana Kobylak | bc37a4c | 2017-04-10 09:45:36 -0500 | [diff] [blame] | 90 | resp, |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 91 | versionId, |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 92 | extendedVersion, |
| 93 | activationState))); |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 94 | } |
| 95 | } |
Patrick Williams | 3accb32 | 2017-05-30 16:29:52 -0500 | [diff] [blame^] | 96 | return; |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 99 | std::string ItemUpdater::getExtendedVersion(const std::string& manifestFilePath) |
| 100 | { |
| 101 | constexpr auto extendedVersionKey = "extended_version="; |
| 102 | constexpr auto extendedVersionKeySize = strlen(extendedVersionKey); |
| 103 | |
| 104 | if (manifestFilePath.empty()) |
| 105 | { |
| 106 | log<level::ERR>("Error MANIFESTFilePath is empty"); |
| 107 | throw std::runtime_error("MANIFESTFilePath is empty"); |
| 108 | } |
| 109 | |
| 110 | std::string extendedVersion{}; |
| 111 | std::ifstream efile; |
| 112 | std::string line; |
| 113 | efile.exceptions(std::ifstream::failbit |
| 114 | | std::ifstream::badbit |
| 115 | | std::ifstream::eofbit); |
| 116 | |
| 117 | try |
| 118 | { |
| 119 | efile.open(manifestFilePath); |
| 120 | while (getline(efile, line)) |
| 121 | { |
| 122 | if (line.compare(0, extendedVersionKeySize, |
| 123 | extendedVersionKey) == 0) |
| 124 | { |
| 125 | extendedVersion = line.substr(extendedVersionKeySize); |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | efile.close(); |
| 130 | } |
| 131 | catch (const std::exception& e) |
| 132 | { |
| 133 | log<level::ERR>("Error in reading Host MANIFEST file"); |
| 134 | } |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 135 | return extendedVersion; |
| 136 | } |
| 137 | |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 138 | int ItemUpdater::validateSquashFSImage(const std::string& versionId) |
| 139 | { |
| 140 | auto file = IMAGE_DIR + versionId + "/" + |
| 141 | std::string(squashFSImage); |
| 142 | std::ifstream efile(file.c_str()); |
| 143 | |
| 144 | if (efile.good() == 1) |
| 145 | { |
| 146 | return 0; |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | log<level::ERR>("Failed to find the SquashFS image."); |
| 151 | return -1; |
| 152 | } |
| 153 | } |
| 154 | |
Michael Tritz | dd961b6 | 2017-05-17 14:07:03 -0500 | [diff] [blame] | 155 | void ItemUpdater::reset() |
| 156 | { |
| 157 | for(const auto& it : activations) |
| 158 | { |
| 159 | auto serviceFile = "obmc-flash-bios-ubiumount-rw@" + it.first + |
| 160 | ".service"; |
| 161 | |
| 162 | // Remove the read-write partitions. |
| 163 | auto method = busItem.new_method_call( |
| 164 | SYSTEMD_BUSNAME, |
| 165 | SYSTEMD_PATH, |
| 166 | SYSTEMD_INTERFACE, |
| 167 | "StartUnit"); |
| 168 | method.append(serviceFile, "replace"); |
| 169 | busItem.call_noreply(method); |
| 170 | } |
| 171 | |
| 172 | // Remove the preserved partition. |
| 173 | auto method = busItem.new_method_call( |
| 174 | SYSTEMD_BUSNAME, |
| 175 | SYSTEMD_PATH, |
| 176 | SYSTEMD_INTERFACE, |
| 177 | "StartUnit"); |
| 178 | method.append("obmc-flash-bios-ubiumount-prsv.service", "replace"); |
| 179 | busItem.call_noreply(method); |
| 180 | |
| 181 | return; |
| 182 | } |
| 183 | |
Adriana Kobylak | befe5ce | 2017-04-05 15:57:44 -0500 | [diff] [blame] | 184 | } // namespace updater |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 185 | } // namespace software |
| 186 | } // namespace openpower |