blob: bc382e845e5be9750e62b115ca973e94ba28cac1 [file] [log] [blame]
Saqib Khan35e83f32017-05-22 11:37:32 -05001#include <fstream>
Gunnar Millsec1b41c2017-05-02 12:20:36 -05002#include <string>
Gunnar Mills2ce7da22017-05-04 15:37:56 -05003#include <phosphor-logging/log.hpp>
Gunnar Millsec1b41c2017-05-02 12:20:36 -05004#include "config.h"
Gunnar Mills2ce7da22017-05-04 15:37:56 -05005#include "item_updater.hpp"
6#include "xyz/openbmc_project/Software/Version/server.hpp"
Saqib Khan35e83f32017-05-22 11:37:32 -05007#include <experimental/filesystem>
Gunnar Millsec1b41c2017-05-02 12:20:36 -05008
9namespace phosphor
10{
11namespace software
12{
13namespace updater
14{
15
Gunnar Mills2ce7da22017-05-04 15:37:56 -050016// When you see server:: you know we're referencing our base class
17namespace server = sdbusplus::xyz::openbmc_project::Software::server;
18
19using namespace phosphor::logging;
Saqib Khan35e83f32017-05-22 11:37:32 -050020namespace fs = std::experimental::filesystem;
21
22constexpr auto bmcImage = "image-rofs";
Gunnar Mills2ce7da22017-05-04 15:37:56 -050023
Patrick Williamse75d10f2017-05-30 16:56:32 -050024void ItemUpdater::createActivation(sdbusplus::message::message& msg)
Gunnar Millsec1b41c2017-05-02 12:20:36 -050025{
Gunnar Mills2ce7da22017-05-04 15:37:56 -050026 sdbusplus::message::object_path objPath;
27 std::map<std::string,
Patrick Williamse75d10f2017-05-30 16:56:32 -050028 std::map<std::string,
29 sdbusplus::message::variant<std::string>>> interfaces;
30 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050031 std::string path(std::move(objPath));
32
33 for (const auto& intf : interfaces)
34 {
35 if (intf.first.compare(VERSION_IFACE))
36 {
37 continue;
38 }
39
40 for (const auto& property : intf.second)
41 {
42 if (!property.first.compare("Purpose"))
43 {
44 // Only process the BMC images
Patrick Williamse75d10f2017-05-30 16:56:32 -050045 auto value = sdbusplus::message::variant_ns::get<std::string>(
46 property.second);
47 if (value !=
Patrick Williams9f357142017-05-30 16:58:30 -050048 convertForMessage(server::Version::VersionPurpose::BMC) &&
49 value !=
50 convertForMessage(server::Version::VersionPurpose::System))
Gunnar Mills2ce7da22017-05-04 15:37:56 -050051 {
Patrick Williamse75d10f2017-05-30 16:56:32 -050052 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050053 }
54 }
55 }
56 }
57
58 // Version id is the last item in the path
59 auto pos = path.rfind("/");
60 if (pos == std::string::npos)
61 {
62 log<level::ERR>("No version id found in object path",
63 entry("OBJPATH=%s", path));
Patrick Williamse75d10f2017-05-30 16:56:32 -050064 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050065 }
66
67 auto versionId = path.substr(pos + 1);
68
Patrick Williamse75d10f2017-05-30 16:56:32 -050069 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -050070 {
Saqib Khan35e83f32017-05-22 11:37:32 -050071 // Determine the Activation state by processing the given image dir.
72 auto activationState = server::Activation::Activations::Invalid;
73 ItemUpdater::ActivationStatus result = ItemUpdater::
74 validateSquashFSImage(versionId);
75 if (result == ItemUpdater::ActivationStatus::ready)
76 {
77 activationState = server::Activation::Activations::Ready;
78 }
79 else if (result == ItemUpdater::ActivationStatus::active)
80 {
81 activationState = server::Activation::Activations::Active;
82 }
83 activations.insert(std::make_pair(
84 versionId,
85 std::make_unique<Activation>(
86 bus,
87 path,
88 versionId,
89 activationState)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -050090 }
Patrick Williamse75d10f2017-05-30 16:56:32 -050091 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -050092}
93
Saqib Khan35e83f32017-05-22 11:37:32 -050094ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
95 const std::string& versionId)
96{
97
98 // TODO openbmc/openbmc#1715 Check the Common.FilePath to
99 // determine the active image.
100 fs::path imageDirPath(IMG_UPLOAD_DIR);
101 imageDirPath /= versionId;
102 if (!fs::is_directory(imageDirPath))
103 {
104 return ItemUpdater::ActivationStatus::active;
105 }
106
107 fs::path file(imageDirPath);
108 file /= bmcImage;
109 std::ifstream efile(file.c_str());
110
111 if (efile.good() == 1)
112 {
113 return ItemUpdater::ActivationStatus::ready;
114 }
115 else
116 {
117 log<level::ERR>("Failed to find the SquashFS image.");
118 return ItemUpdater::ActivationStatus::invalid;
119 }
120}
121
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500122} // namespace updater
123} // namespace software
124} // namespace phosphor