blob: f52ec16d03126d18541ef909ee9bc128bcfae7f4 [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{
Gunnar Millsffe04922017-10-06 13:20:43 -050019 if (!fs::is_directory(PERSIST_DIR))
Saqib Khan5d532672017-08-09 10:44:50 -050020 {
Michael Tritz49860e12017-09-18 14:33:54 -050021 fs::create_directories(PERSIST_DIR);
Saqib Khan5d532672017-08-09 10:44:50 -050022 }
23 std::string path = PERSIST_DIR + versionId;
24
25 std::ofstream os(path.c_str());
26 cereal::JSONOutputArchive oarchive(os);
27 oarchive(cereal::make_nvp("priority", priority));
28}
29
Saqib Khan1eef62d2017-08-10 15:29:34 -050030bool restoreFromFile(std::string versionId, uint8_t& priority)
Saqib Khan5d532672017-08-09 10:44:50 -050031{
32 std::string path = PERSIST_DIR + versionId;
33 if (fs::exists(path))
34 {
35 std::ifstream is(path.c_str(), std::ios::in);
Saqib Khan1eef62d2017-08-10 15:29:34 -050036 try
37 {
38 cereal::JSONInputArchive iarchive(is);
39 iarchive(cereal::make_nvp("priority", priority));
40 return true;
41 }
Gunnar Millsffe04922017-10-06 13:20:43 -050042 catch (cereal::RapidJSONException& e)
Saqib Khan1eef62d2017-08-10 15:29:34 -050043 {
44 fs::remove(path);
45 }
Saqib Khan5d532672017-08-09 10:44:50 -050046 }
Saqib Khan1eef62d2017-08-10 15:29:34 -050047
48 // Find the mtd device "u-boot-env" to retrieve the environment variables
49 std::ifstream mtdDevices("/proc/mtd");
50 std::string device, devicePath;
51
52 try
53 {
Gunnar Millsffe04922017-10-06 13:20:43 -050054 while (std::getline(mtdDevices, device))
55 {
Saqib Khan1eef62d2017-08-10 15:29:34 -050056 if (device.find("u-boot-env") != std::string::npos)
57 {
58 devicePath = "/dev/" + device.substr(0, device.find(':'));
59 break;
60 }
61 }
62
63 if (!devicePath.empty())
64 {
65 std::ifstream input(devicePath.c_str());
66 std::string envVars;
67 std::getline(input, envVars);
68
Michael Tritz6273efd2017-12-14 15:21:18 -060069 std::string versionVar = versionId + "=";
70 auto varPosition = envVars.find(versionVar);
71
72 if (varPosition != std::string::npos)
Saqib Khan1eef62d2017-08-10 15:29:34 -050073 {
74 // Grab the environment variable for this versionId. These
75 // variables follow the format "versionId=priority\0"
Michael Tritz6273efd2017-12-14 15:21:18 -060076 auto var = envVars.substr(varPosition);
77 priority = std::stoi(var.substr(versionVar.length()));
Saqib Khan1eef62d2017-08-10 15:29:34 -050078 return true;
79 }
80 }
81 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -060082 catch (const std::exception& e)
83 {
84 }
Saqib Khan1eef62d2017-08-10 15:29:34 -050085
86 return false;
Saqib Khan5d532672017-08-09 10:44:50 -050087}
88
89void removeFile(std::string versionId)
90{
91 std::string path = PERSIST_DIR + versionId;
92 if (fs::exists(path))
93 {
94 fs::remove(path);
95 }
96}
97
Gunnar Millsfa34e022018-09-04 10:05:45 -050098} // namespace updater
Saqib Khan5d532672017-08-09 10:44:50 -050099} // namespace software
Gunnar Millsfa34e022018-09-04 10:05:45 -0500100} // namespace phosphor