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 | |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 22 | int ItemUpdater::createActivation(sd_bus_message* msg, |
| 23 | void* userData, |
| 24 | sd_bus_error* retErr) |
| 25 | { |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 26 | auto* updater = static_cast<ItemUpdater*>(userData); |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 27 | auto mapper = updater->busItem.new_method_call( |
| 28 | MAPPER_BUSNAME, |
| 29 | MAPPER_PATH, |
| 30 | MAPPER_INTERFACE, |
| 31 | "GetSubTreePaths"); |
| 32 | mapper.append(SOFTWARE_OBJPATH, |
| 33 | 1, // Depth |
| 34 | std::vector<std::string>({VERSION_IFACE})); |
| 35 | |
| 36 | auto mapperResponseMsg = updater->busItem.call(mapper); |
| 37 | if (mapperResponseMsg.is_method_error()) |
| 38 | { |
| 39 | log<level::ERR>("Error in mapper call", |
| 40 | entry("PATH=%s", SOFTWARE_OBJPATH), |
| 41 | entry("INTERFACE=%s", VERSION_IFACE)); |
| 42 | return -1; |
| 43 | } |
| 44 | |
| 45 | std::vector<std::string> mapperResponse; |
| 46 | mapperResponseMsg.read(mapperResponse); |
| 47 | if (mapperResponse.empty()) |
| 48 | { |
| 49 | log<level::ERR>("Error reading mapper response", |
| 50 | entry("PATH=%s", SOFTWARE_OBJPATH), |
| 51 | entry("INTERFACE=%s", VERSION_IFACE)); |
| 52 | return -1; |
| 53 | } |
| 54 | |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 55 | auto extendedVersion = ItemUpdater::getExtendedVersion(MANIFEST_FILE); |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 56 | for (const auto& resp : mapperResponse) |
| 57 | { |
| 58 | // Version id is the last item in the path |
| 59 | auto pos = resp.rfind("/"); |
| 60 | if (pos == std::string::npos) |
| 61 | { |
| 62 | log<level::ERR>("No version id found in object path", |
| 63 | entry("OBJPATH=%s", resp)); |
| 64 | return -1; |
| 65 | } |
| 66 | |
Adriana Kobylak | 268616b | 2017-04-05 15:23:30 -0500 | [diff] [blame] | 67 | auto versionId = resp.substr(pos + 1); |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame^] | 68 | |
| 69 | // Determine the Activation state by processing the given image dir. |
| 70 | auto activationState = server::Activation::Activations::Invalid; |
| 71 | if (ItemUpdater::validateSquashFSImage(versionId) == 0) |
| 72 | { |
| 73 | activationState = server::Activation::Activations::Ready; |
| 74 | } |
| 75 | |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 76 | if (updater->activations.find(versionId) == updater->activations.end()) |
| 77 | { |
| 78 | updater->activations.insert(std::make_pair( |
| 79 | versionId, |
| 80 | std::make_unique<Activation>( |
| 81 | updater->busItem, |
Adriana Kobylak | bc37a4c | 2017-04-10 09:45:36 -0500 | [diff] [blame] | 82 | resp, |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 83 | versionId, |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame^] | 84 | extendedVersion, |
| 85 | activationState))); |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 86 | } |
| 87 | } |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 91 | std::string ItemUpdater::getExtendedVersion(const std::string& manifestFilePath) |
| 92 | { |
| 93 | constexpr auto extendedVersionKey = "extended_version="; |
| 94 | constexpr auto extendedVersionKeySize = strlen(extendedVersionKey); |
| 95 | |
| 96 | if (manifestFilePath.empty()) |
| 97 | { |
| 98 | log<level::ERR>("Error MANIFESTFilePath is empty"); |
| 99 | throw std::runtime_error("MANIFESTFilePath is empty"); |
| 100 | } |
| 101 | |
| 102 | std::string extendedVersion{}; |
| 103 | std::ifstream efile; |
| 104 | std::string line; |
| 105 | efile.exceptions(std::ifstream::failbit |
| 106 | | std::ifstream::badbit |
| 107 | | std::ifstream::eofbit); |
| 108 | |
| 109 | try |
| 110 | { |
| 111 | efile.open(manifestFilePath); |
| 112 | while (getline(efile, line)) |
| 113 | { |
| 114 | if (line.compare(0, extendedVersionKeySize, |
| 115 | extendedVersionKey) == 0) |
| 116 | { |
| 117 | extendedVersion = line.substr(extendedVersionKeySize); |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | efile.close(); |
| 122 | } |
| 123 | catch (const std::exception& e) |
| 124 | { |
| 125 | log<level::ERR>("Error in reading Host MANIFEST file"); |
| 126 | } |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 127 | return extendedVersion; |
| 128 | } |
| 129 | |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame^] | 130 | int ItemUpdater::validateSquashFSImage(const std::string& versionId) |
| 131 | { |
| 132 | auto file = IMAGE_DIR + versionId + "/" + |
| 133 | std::string(squashFSImage); |
| 134 | std::ifstream efile(file.c_str()); |
| 135 | |
| 136 | if (efile.good() == 1) |
| 137 | { |
| 138 | return 0; |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | log<level::ERR>("Failed to find the SquashFS image."); |
| 143 | return -1; |
| 144 | } |
| 145 | } |
| 146 | |
Adriana Kobylak | befe5ce | 2017-04-05 15:57:44 -0500 | [diff] [blame] | 147 | } // namespace updater |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 148 | } // namespace software |
| 149 | } // namespace openpower |
| 150 | |