blob: 2acc3733898233aabb77d07d5f0b6fac2155298e [file] [log] [blame]
Saqib Khan5d532672017-08-09 10:44:50 -05001#include "config.h"
Gunnar Millsb0ce9962018-09-07 13:39:10 -05002
Saqib Khan5d532672017-08-09 10:44:50 -05003#include "serialize.hpp"
Gunnar Millsb0ce9962018-09-07 13:39:10 -05004
5#include <cereal/archives/json.hpp>
6#include <experimental/filesystem>
7#include <fstream>
Saqib Khan1eef62d2017-08-10 15:29:34 -05008#include <sdbusplus/server.hpp>
Saqib Khan5d532672017-08-09 10:44:50 -05009
10namespace phosphor
11{
12namespace software
13{
14namespace updater
15{
16
17namespace fs = std::experimental::filesystem;
18
19void storeToFile(std::string versionId, uint8_t priority)
20{
Gunnar Millsffe04922017-10-06 13:20:43 -050021 if (!fs::is_directory(PERSIST_DIR))
Saqib Khan5d532672017-08-09 10:44:50 -050022 {
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));
30}
31
Saqib Khan1eef62d2017-08-10 15:29:34 -050032bool restoreFromFile(std::string versionId, uint8_t& priority)
Saqib Khan5d532672017-08-09 10:44:50 -050033{
34 std::string path = PERSIST_DIR + versionId;
35 if (fs::exists(path))
36 {
37 std::ifstream is(path.c_str(), std::ios::in);
Saqib Khan1eef62d2017-08-10 15:29:34 -050038 try
39 {
40 cereal::JSONInputArchive iarchive(is);
41 iarchive(cereal::make_nvp("priority", priority));
42 return true;
43 }
Gunnar Millsffe04922017-10-06 13:20:43 -050044 catch (cereal::RapidJSONException& e)
Saqib Khan1eef62d2017-08-10 15:29:34 -050045 {
46 fs::remove(path);
47 }
Saqib Khan5d532672017-08-09 10:44:50 -050048 }
Saqib Khan1eef62d2017-08-10 15:29:34 -050049
50 // Find the mtd device "u-boot-env" to retrieve the environment variables
51 std::ifstream mtdDevices("/proc/mtd");
52 std::string device, devicePath;
53
54 try
55 {
Gunnar Millsffe04922017-10-06 13:20:43 -050056 while (std::getline(mtdDevices, device))
57 {
Saqib Khan1eef62d2017-08-10 15:29:34 -050058 if (device.find("u-boot-env") != std::string::npos)
59 {
60 devicePath = "/dev/" + device.substr(0, device.find(':'));
61 break;
62 }
63 }
64
65 if (!devicePath.empty())
66 {
67 std::ifstream input(devicePath.c_str());
68 std::string envVars;
69 std::getline(input, envVars);
70
Michael Tritz6273efd2017-12-14 15:21:18 -060071 std::string versionVar = versionId + "=";
72 auto varPosition = envVars.find(versionVar);
73
74 if (varPosition != std::string::npos)
Saqib Khan1eef62d2017-08-10 15:29:34 -050075 {
76 // Grab the environment variable for this versionId. These
77 // variables follow the format "versionId=priority\0"
Michael Tritz6273efd2017-12-14 15:21:18 -060078 auto var = envVars.substr(varPosition);
79 priority = std::stoi(var.substr(versionVar.length()));
Saqib Khan1eef62d2017-08-10 15:29:34 -050080 return true;
81 }
82 }
83 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -060084 catch (const std::exception& e)
85 {
86 }
Saqib Khan1eef62d2017-08-10 15:29:34 -050087
88 return false;
Saqib Khan5d532672017-08-09 10:44:50 -050089}
90
91void removeFile(std::string versionId)
92{
93 std::string path = PERSIST_DIR + versionId;
94 if (fs::exists(path))
95 {
96 fs::remove(path);
97 }
98}
99
Gunnar Millsfa34e022018-09-04 10:05:45 -0500100} // namespace updater
Saqib Khan5d532672017-08-09 10:44:50 -0500101} // namespace software
Gunnar Millsfa34e022018-09-04 10:05:45 -0500102} // namespace phosphor