blob: d692c0f132e20df1f329569062656061a47f2ce8 [file] [log] [blame]
Saqib Khan5d532672017-08-09 10:44:50 -05001#include "config.h"
2#include <experimental/filesystem>
3#include <cereal/archives/json.hpp>
4#include <fstream>
5#include "serialize.hpp"
Saqib Khan1eef62d2017-08-10 15:29:34 -05006#include <sdbusplus/server.hpp>
Saqib Khan5d532672017-08-09 10:44:50 -05007
8namespace phosphor
9{
10namespace software
11{
12namespace updater
13{
14
15namespace fs = std::experimental::filesystem;
16
17void storeToFile(std::string versionId, uint8_t priority)
18{
Saqib Khan1eef62d2017-08-10 15:29:34 -050019 auto bus = sdbusplus::bus::new_default();
20
Saqib Khan5d532672017-08-09 10:44:50 -050021 if(!fs::is_directory(PERSIST_DIR))
22 {
Michael Tritz49860e12017-09-18 14:33:54 -050023 fs::create_directories(PERSIST_DIR);
Saqib Khan5d532672017-08-09 10:44:50 -050024 }
25 std::string path = PERSIST_DIR + versionId;
26
27 std::ofstream os(path.c_str());
28 cereal::JSONOutputArchive oarchive(os);
29 oarchive(cereal::make_nvp("priority", priority));
Saqib Khan1eef62d2017-08-10 15:29:34 -050030
31 std::string serviceFile = "obmc-flash-bmc-setenv@" + versionId + "\\x3d" +
32 std::to_string(priority) + ".service";
33 auto method = bus.new_method_call(
34 SYSTEMD_BUSNAME,
35 SYSTEMD_PATH,
36 SYSTEMD_INTERFACE,
37 "StartUnit");
38 method.append(serviceFile, "replace");
39 bus.call_noreply(method);
Saqib Khan5d532672017-08-09 10:44:50 -050040}
41
Saqib Khan1eef62d2017-08-10 15:29:34 -050042bool restoreFromFile(std::string versionId, uint8_t& priority)
Saqib Khan5d532672017-08-09 10:44:50 -050043{
44 std::string path = PERSIST_DIR + versionId;
45 if (fs::exists(path))
46 {
47 std::ifstream is(path.c_str(), std::ios::in);
Saqib Khan1eef62d2017-08-10 15:29:34 -050048 try
49 {
50 cereal::JSONInputArchive iarchive(is);
51 iarchive(cereal::make_nvp("priority", priority));
52 return true;
53 }
54 catch(cereal::RapidJSONException& e)
55 {
56 fs::remove(path);
57 }
Saqib Khan5d532672017-08-09 10:44:50 -050058 }
Saqib Khan1eef62d2017-08-10 15:29:34 -050059
60 // Find the mtd device "u-boot-env" to retrieve the environment variables
61 std::ifstream mtdDevices("/proc/mtd");
62 std::string device, devicePath;
63
64 try
65 {
66 while (std::getline(mtdDevices, device)) {
67 if (device.find("u-boot-env") != std::string::npos)
68 {
69 devicePath = "/dev/" + device.substr(0, device.find(':'));
70 break;
71 }
72 }
73
74 if (!devicePath.empty())
75 {
76 std::ifstream input(devicePath.c_str());
77 std::string envVars;
78 std::getline(input, envVars);
79
80 if (envVars.find(versionId) != std::string::npos)
81 {
82 // Grab the environment variable for this versionId. These
83 // variables follow the format "versionId=priority\0"
84 auto var = envVars.substr(envVars.find(versionId));
85 priority = std::stoi(var.substr(var.find('=') + 1));
86 return true;
87 }
88 }
89 }
90 catch (const std::exception& e){}
91
92 return false;
Saqib Khan5d532672017-08-09 10:44:50 -050093}
94
95void removeFile(std::string versionId)
96{
97 std::string path = PERSIST_DIR + versionId;
98 if (fs::exists(path))
99 {
100 fs::remove(path);
101 }
102}
103
104} // namespace phosphor
105} // namespace software
106} // namespace openpower