blob: 52c5ac5183211e18d838c5d62e252c7a444f5794 [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>
Patrick Williamsc9bb6422021-08-27 06:18:35 -05006#include <phosphor-logging/lg2.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
Patrick Williamsc9bb6422021-08-27 06:18:35 -050019PHOSPHOR_LOG2_USING;
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
Patrick Williamsc9bb6422021-08-27 06:18:35 -050033 warning("Removing non-directory file: {PATH}", "PATH", path);
Adriana Kobylak687e75e2019-11-07 11:23:25 -060034 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
Patrick Williamsc9bb6422021-08-27 06:18:35 -050053 warning("Removing non-directory file: {PATH}", "PATH", path);
Adriana Kobylakec4eec32019-11-13 14:28:35 -060054 fs::remove_all(path);
55 }
56 fs::create_directories(path);
57 }
58 path = path / purposeName;
59
60 std::ofstream os(path.c_str());
61 cereal::JSONOutputArchive oarchive(os);
62 oarchive(cereal::make_nvp(purposeName, purpose));
63}
64
Adriana Kobylak687e75e2019-11-07 11:23:25 -060065bool restorePriority(const std::string& versionId, uint8_t& priority)
Saqib Khan5d532672017-08-09 10:44:50 -050066{
Adriana Kobylak687e75e2019-11-07 11:23:25 -060067 auto path = fs::path(PERSIST_DIR) / versionId / priorityName;
Saqib Khan5d532672017-08-09 10:44:50 -050068 if (fs::exists(path))
69 {
70 std::ifstream is(path.c_str(), std::ios::in);
Saqib Khan1eef62d2017-08-10 15:29:34 -050071 try
72 {
73 cereal::JSONInputArchive iarchive(is);
Adriana Kobylak687e75e2019-11-07 11:23:25 -060074 iarchive(cereal::make_nvp(priorityName, priority));
Saqib Khan1eef62d2017-08-10 15:29:34 -050075 return true;
76 }
Patrick Williams58e18972021-10-06 12:25:58 -050077 catch (const cereal::Exception& e)
Saqib Khan1eef62d2017-08-10 15:29:34 -050078 {
Adriana Kobylak687e75e2019-11-07 11:23:25 -060079 fs::remove_all(path);
Saqib Khan1eef62d2017-08-10 15:29:34 -050080 }
Saqib Khan5d532672017-08-09 10:44:50 -050081 }
Saqib Khan1eef62d2017-08-10 15:29:34 -050082
83 // Find the mtd device "u-boot-env" to retrieve the environment variables
84 std::ifstream mtdDevices("/proc/mtd");
85 std::string device, devicePath;
86
87 try
88 {
Gunnar Millsffe04922017-10-06 13:20:43 -050089 while (std::getline(mtdDevices, device))
90 {
Saqib Khan1eef62d2017-08-10 15:29:34 -050091 if (device.find("u-boot-env") != std::string::npos)
92 {
93 devicePath = "/dev/" + device.substr(0, device.find(':'));
94 break;
95 }
96 }
97
98 if (!devicePath.empty())
99 {
100 std::ifstream input(devicePath.c_str());
101 std::string envVars;
102 std::getline(input, envVars);
103
Michael Tritz6273efd2017-12-14 15:21:18 -0600104 std::string versionVar = versionId + "=";
105 auto varPosition = envVars.find(versionVar);
106
107 if (varPosition != std::string::npos)
Saqib Khan1eef62d2017-08-10 15:29:34 -0500108 {
109 // Grab the environment variable for this versionId. These
110 // variables follow the format "versionId=priority\0"
Michael Tritz6273efd2017-12-14 15:21:18 -0600111 auto var = envVars.substr(varPosition);
112 priority = std::stoi(var.substr(versionVar.length()));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500113 return true;
114 }
115 }
116 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600117 catch (const std::exception& e)
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500118 {
119 error("Error during processing: {ERROR}", "ERROR", e);
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 }
Patrick Williams58e18972021-10-06 12:25:58 -0500137 catch (const cereal::Exception& e)
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600138 {
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