blob: f5aeb112a6b992c2cf92220d730163325a29592a [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 Kobylakc98d9122020-05-05 10:36:01 -05006#include <filesystem>
Gunnar Millsb0ce9962018-09-07 13:39:10 -05007#include <fstream>
Adriana Kobylak687e75e2019-11-07 11:23:25 -06008#include <phosphor-logging/log.hpp>
Saqib Khan1eef62d2017-08-10 15:29:34 -05009#include <sdbusplus/server.hpp>
Saqib Khan5d532672017-08-09 10:44:50 -050010
11namespace phosphor
12{
13namespace software
14{
15namespace updater
16{
17
Adriana Kobylak687e75e2019-11-07 11:23:25 -060018using namespace phosphor::logging;
Adriana Kobylakc98d9122020-05-05 10:36:01 -050019namespace fs = std::filesystem;
Saqib Khan5d532672017-08-09 10:44:50 -050020
Adriana Kobylak687e75e2019-11-07 11:23:25 -060021const std::string priorityName = "priority";
Adriana Kobylakec4eec32019-11-13 14:28:35 -060022const std::string purposeName = "purpose";
Adriana Kobylak687e75e2019-11-07 11:23:25 -060023
24void storePriority(const std::string& versionId, uint8_t priority)
Saqib Khan5d532672017-08-09 10:44:50 -050025{
Adriana Kobylak687e75e2019-11-07 11:23:25 -060026 auto path = fs::path(PERSIST_DIR) / versionId;
27 if (!fs::is_directory(path))
Saqib Khan5d532672017-08-09 10:44:50 -050028 {
Adriana Kobylak687e75e2019-11-07 11:23:25 -060029 if (fs::exists(path))
30 {
31 // Delete if it's a non-directory file
32 log<level::WARNING>("Removing non-directory file",
33 entry("PATH=%s", path.c_str()));
34 fs::remove_all(path);
35 }
36 fs::create_directories(path);
Saqib Khan5d532672017-08-09 10:44:50 -050037 }
Adriana Kobylak687e75e2019-11-07 11:23:25 -060038 path = path / priorityName;
Saqib Khan5d532672017-08-09 10:44:50 -050039
40 std::ofstream os(path.c_str());
41 cereal::JSONOutputArchive oarchive(os);
Adriana Kobylak687e75e2019-11-07 11:23:25 -060042 oarchive(cereal::make_nvp(priorityName, priority));
Saqib Khan5d532672017-08-09 10:44:50 -050043}
44
Adriana Kobylakec4eec32019-11-13 14:28:35 -060045void storePurpose(const std::string& versionId, VersionPurpose purpose)
46{
47 auto path = fs::path(PERSIST_DIR) / versionId;
48 if (!fs::is_directory(path))
49 {
50 if (fs::exists(path))
51 {
52 // Delete if it's a non-directory file
53 log<level::WARNING>("Removing non-directory file",
54 entry("PATH=%s", path.c_str()));
55 fs::remove_all(path);
56 }
57 fs::create_directories(path);
58 }
59 path = path / purposeName;
60
61 std::ofstream os(path.c_str());
62 cereal::JSONOutputArchive oarchive(os);
63 oarchive(cereal::make_nvp(purposeName, purpose));
64}
65
Adriana Kobylak687e75e2019-11-07 11:23:25 -060066bool restorePriority(const std::string& versionId, uint8_t& priority)
Saqib Khan5d532672017-08-09 10:44:50 -050067{
Adriana Kobylak687e75e2019-11-07 11:23:25 -060068 auto path = fs::path(PERSIST_DIR) / versionId / priorityName;
Saqib Khan5d532672017-08-09 10:44:50 -050069 if (fs::exists(path))
70 {
71 std::ifstream is(path.c_str(), std::ios::in);
Saqib Khan1eef62d2017-08-10 15:29:34 -050072 try
73 {
74 cereal::JSONInputArchive iarchive(is);
Adriana Kobylak687e75e2019-11-07 11:23:25 -060075 iarchive(cereal::make_nvp(priorityName, priority));
Saqib Khan1eef62d2017-08-10 15:29:34 -050076 return true;
77 }
Adriana Kobylak9eb631d2019-11-05 14:10:42 -060078 catch (cereal::Exception& e)
Saqib Khan1eef62d2017-08-10 15:29:34 -050079 {
Adriana Kobylak687e75e2019-11-07 11:23:25 -060080 fs::remove_all(path);
Saqib Khan1eef62d2017-08-10 15:29:34 -050081 }
Saqib Khan5d532672017-08-09 10:44:50 -050082 }
Saqib Khan1eef62d2017-08-10 15:29:34 -050083
84 // Find the mtd device "u-boot-env" to retrieve the environment variables
85 std::ifstream mtdDevices("/proc/mtd");
86 std::string device, devicePath;
87
88 try
89 {
Gunnar Millsffe04922017-10-06 13:20:43 -050090 while (std::getline(mtdDevices, device))
91 {
Saqib Khan1eef62d2017-08-10 15:29:34 -050092 if (device.find("u-boot-env") != std::string::npos)
93 {
94 devicePath = "/dev/" + device.substr(0, device.find(':'));
95 break;
96 }
97 }
98
99 if (!devicePath.empty())
100 {
101 std::ifstream input(devicePath.c_str());
102 std::string envVars;
103 std::getline(input, envVars);
104
Michael Tritz6273efd2017-12-14 15:21:18 -0600105 std::string versionVar = versionId + "=";
106 auto varPosition = envVars.find(versionVar);
107
108 if (varPosition != std::string::npos)
Saqib Khan1eef62d2017-08-10 15:29:34 -0500109 {
110 // Grab the environment variable for this versionId. These
111 // variables follow the format "versionId=priority\0"
Michael Tritz6273efd2017-12-14 15:21:18 -0600112 auto var = envVars.substr(varPosition);
113 priority = std::stoi(var.substr(versionVar.length()));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500114 return true;
115 }
116 }
117 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600118 catch (const std::exception& e)
119 {
120 }
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