blob: a6956c2740842d41e3d69ca999ed46220dd6f867 [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>
Adriana Kobylak687e75e2019-11-07 11:23:25 -06006#include <phosphor-logging/log.hpp>
Saqib Khan1eef62d2017-08-10 15:29:34 -05007#include <sdbusplus/server.hpp>
Saqib Khan5d532672017-08-09 10:44:50 -05008
Adriana Kobylak58aa7502020-06-08 11:12:11 -05009#include <filesystem>
10#include <fstream>
11
Saqib Khan5d532672017-08-09 10:44:50 -050012namespace phosphor
13{
14namespace software
15{
16namespace updater
17{
18
Adriana Kobylak687e75e2019-11-07 11:23:25 -060019using namespace phosphor::logging;
Adriana Kobylakc98d9122020-05-05 10:36:01 -050020namespace fs = std::filesystem;
Saqib Khan5d532672017-08-09 10:44:50 -050021
Adriana Kobylak687e75e2019-11-07 11:23:25 -060022const std::string priorityName = "priority";
Adriana Kobylakec4eec32019-11-13 14:28:35 -060023const std::string purposeName = "purpose";
Adriana Kobylak687e75e2019-11-07 11:23:25 -060024
25void storePriority(const std::string& versionId, uint8_t priority)
Saqib Khan5d532672017-08-09 10:44:50 -050026{
Adriana Kobylak687e75e2019-11-07 11:23:25 -060027 auto path = fs::path(PERSIST_DIR) / versionId;
28 if (!fs::is_directory(path))
Saqib Khan5d532672017-08-09 10:44:50 -050029 {
Adriana Kobylak687e75e2019-11-07 11:23:25 -060030 if (fs::exists(path))
31 {
32 // Delete if it's a non-directory file
33 log<level::WARNING>("Removing non-directory file",
34 entry("PATH=%s", path.c_str()));
35 fs::remove_all(path);
36 }
37 fs::create_directories(path);
Saqib Khan5d532672017-08-09 10:44:50 -050038 }
Adriana Kobylak687e75e2019-11-07 11:23:25 -060039 path = path / priorityName;
Saqib Khan5d532672017-08-09 10:44:50 -050040
41 std::ofstream os(path.c_str());
42 cereal::JSONOutputArchive oarchive(os);
Adriana Kobylak687e75e2019-11-07 11:23:25 -060043 oarchive(cereal::make_nvp(priorityName, priority));
Saqib Khan5d532672017-08-09 10:44:50 -050044}
45
Adriana Kobylakec4eec32019-11-13 14:28:35 -060046void storePurpose(const std::string& versionId, VersionPurpose purpose)
47{
48 auto path = fs::path(PERSIST_DIR) / versionId;
49 if (!fs::is_directory(path))
50 {
51 if (fs::exists(path))
52 {
53 // Delete if it's a non-directory file
54 log<level::WARNING>("Removing non-directory file",
55 entry("PATH=%s", path.c_str()));
56 fs::remove_all(path);
57 }
58 fs::create_directories(path);
59 }
60 path = path / purposeName;
61
62 std::ofstream os(path.c_str());
63 cereal::JSONOutputArchive oarchive(os);
64 oarchive(cereal::make_nvp(purposeName, purpose));
65}
66
Adriana Kobylak687e75e2019-11-07 11:23:25 -060067bool restorePriority(const std::string& versionId, uint8_t& priority)
Saqib Khan5d532672017-08-09 10:44:50 -050068{
Adriana Kobylak687e75e2019-11-07 11:23:25 -060069 auto path = fs::path(PERSIST_DIR) / versionId / priorityName;
Saqib Khan5d532672017-08-09 10:44:50 -050070 if (fs::exists(path))
71 {
72 std::ifstream is(path.c_str(), std::ios::in);
Saqib Khan1eef62d2017-08-10 15:29:34 -050073 try
74 {
75 cereal::JSONInputArchive iarchive(is);
Adriana Kobylak687e75e2019-11-07 11:23:25 -060076 iarchive(cereal::make_nvp(priorityName, priority));
Saqib Khan1eef62d2017-08-10 15:29:34 -050077 return true;
78 }
Adriana Kobylak9eb631d2019-11-05 14:10:42 -060079 catch (cereal::Exception& e)
Saqib Khan1eef62d2017-08-10 15:29:34 -050080 {
Adriana Kobylak687e75e2019-11-07 11:23:25 -060081 fs::remove_all(path);
Saqib Khan1eef62d2017-08-10 15:29:34 -050082 }
Saqib Khan5d532672017-08-09 10:44:50 -050083 }
Saqib Khan1eef62d2017-08-10 15:29:34 -050084
85 // Find the mtd device "u-boot-env" to retrieve the environment variables
86 std::ifstream mtdDevices("/proc/mtd");
87 std::string device, devicePath;
88
89 try
90 {
Gunnar Millsffe04922017-10-06 13:20:43 -050091 while (std::getline(mtdDevices, device))
92 {
Saqib Khan1eef62d2017-08-10 15:29:34 -050093 if (device.find("u-boot-env") != std::string::npos)
94 {
95 devicePath = "/dev/" + device.substr(0, device.find(':'));
96 break;
97 }
98 }
99
100 if (!devicePath.empty())
101 {
102 std::ifstream input(devicePath.c_str());
103 std::string envVars;
104 std::getline(input, envVars);
105
Michael Tritz6273efd2017-12-14 15:21:18 -0600106 std::string versionVar = versionId + "=";
107 auto varPosition = envVars.find(versionVar);
108
109 if (varPosition != std::string::npos)
Saqib Khan1eef62d2017-08-10 15:29:34 -0500110 {
111 // Grab the environment variable for this versionId. These
112 // variables follow the format "versionId=priority\0"
Michael Tritz6273efd2017-12-14 15:21:18 -0600113 auto var = envVars.substr(varPosition);
114 priority = std::stoi(var.substr(versionVar.length()));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500115 return true;
116 }
117 }
118 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600119 catch (const std::exception& e)
Adriana Kobylak58aa7502020-06-08 11:12:11 -0500120 {}
Saqib Khan1eef62d2017-08-10 15:29:34 -0500121
122 return false;
Saqib Khan5d532672017-08-09 10:44:50 -0500123}
124
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600125bool restorePurpose(const std::string& versionId, VersionPurpose& purpose)
126{
127 auto path = fs::path(PERSIST_DIR) / versionId / purposeName;
128 if (fs::exists(path))
129 {
130 std::ifstream is(path.c_str(), std::ios::in);
131 try
132 {
133 cereal::JSONInputArchive iarchive(is);
134 iarchive(cereal::make_nvp(purposeName, purpose));
135 return true;
136 }
137 catch (cereal::Exception& e)
138 {
139 fs::remove_all(path);
140 }
141 }
142
143 return false;
144}
145
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600146void removePersistDataDirectory(const std::string& versionId)
Saqib Khan5d532672017-08-09 10:44:50 -0500147{
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600148 auto path = fs::path(PERSIST_DIR) / versionId;
Saqib Khan5d532672017-08-09 10:44:50 -0500149 if (fs::exists(path))
150 {
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600151 fs::remove_all(path);
Saqib Khan5d532672017-08-09 10:44:50 -0500152 }
153}
154
Gunnar Millsfa34e022018-09-04 10:05:45 -0500155} // namespace updater
Saqib Khan5d532672017-08-09 10:44:50 -0500156} // namespace software
Gunnar Millsfa34e022018-09-04 10:05:45 -0500157} // namespace phosphor