blob: 483cd7a2d72ded77215e3df115a7e0678cc962aa [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>
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;
Saqib Khan5d532672017-08-09 10:44:50 -050019namespace fs = std::experimental::filesystem;
20
Adriana Kobylak687e75e2019-11-07 11:23:25 -060021const std::string priorityName = "priority";
22
23void storePriority(const std::string& versionId, uint8_t priority)
Saqib Khan5d532672017-08-09 10:44:50 -050024{
Adriana Kobylak687e75e2019-11-07 11:23:25 -060025 auto path = fs::path(PERSIST_DIR) / versionId;
26 if (!fs::is_directory(path))
Saqib Khan5d532672017-08-09 10:44:50 -050027 {
Adriana Kobylak687e75e2019-11-07 11:23:25 -060028 if (fs::exists(path))
29 {
30 // Delete if it's a non-directory file
31 log<level::WARNING>("Removing non-directory file",
32 entry("PATH=%s", path.c_str()));
33 fs::remove_all(path);
34 }
35 fs::create_directories(path);
Saqib Khan5d532672017-08-09 10:44:50 -050036 }
Adriana Kobylak687e75e2019-11-07 11:23:25 -060037 path = path / priorityName;
Saqib Khan5d532672017-08-09 10:44:50 -050038
39 std::ofstream os(path.c_str());
40 cereal::JSONOutputArchive oarchive(os);
Adriana Kobylak687e75e2019-11-07 11:23:25 -060041 oarchive(cereal::make_nvp(priorityName, priority));
Saqib Khan5d532672017-08-09 10:44:50 -050042}
43
Adriana Kobylak687e75e2019-11-07 11:23:25 -060044bool restorePriority(const std::string& versionId, uint8_t& priority)
Saqib Khan5d532672017-08-09 10:44:50 -050045{
Adriana Kobylak687e75e2019-11-07 11:23:25 -060046 auto path = fs::path(PERSIST_DIR) / versionId / priorityName;
Saqib Khan5d532672017-08-09 10:44:50 -050047 if (fs::exists(path))
48 {
49 std::ifstream is(path.c_str(), std::ios::in);
Saqib Khan1eef62d2017-08-10 15:29:34 -050050 try
51 {
52 cereal::JSONInputArchive iarchive(is);
Adriana Kobylak687e75e2019-11-07 11:23:25 -060053 iarchive(cereal::make_nvp(priorityName, priority));
Saqib Khan1eef62d2017-08-10 15:29:34 -050054 return true;
55 }
Adriana Kobylak9eb631d2019-11-05 14:10:42 -060056 catch (cereal::Exception& e)
Saqib Khan1eef62d2017-08-10 15:29:34 -050057 {
Adriana Kobylak687e75e2019-11-07 11:23:25 -060058 fs::remove_all(path);
Saqib Khan1eef62d2017-08-10 15:29:34 -050059 }
Saqib Khan5d532672017-08-09 10:44:50 -050060 }
Saqib Khan1eef62d2017-08-10 15:29:34 -050061
62 // Find the mtd device "u-boot-env" to retrieve the environment variables
63 std::ifstream mtdDevices("/proc/mtd");
64 std::string device, devicePath;
65
66 try
67 {
Gunnar Millsffe04922017-10-06 13:20:43 -050068 while (std::getline(mtdDevices, device))
69 {
Saqib Khan1eef62d2017-08-10 15:29:34 -050070 if (device.find("u-boot-env") != std::string::npos)
71 {
72 devicePath = "/dev/" + device.substr(0, device.find(':'));
73 break;
74 }
75 }
76
77 if (!devicePath.empty())
78 {
79 std::ifstream input(devicePath.c_str());
80 std::string envVars;
81 std::getline(input, envVars);
82
Michael Tritz6273efd2017-12-14 15:21:18 -060083 std::string versionVar = versionId + "=";
84 auto varPosition = envVars.find(versionVar);
85
86 if (varPosition != std::string::npos)
Saqib Khan1eef62d2017-08-10 15:29:34 -050087 {
88 // Grab the environment variable for this versionId. These
89 // variables follow the format "versionId=priority\0"
Michael Tritz6273efd2017-12-14 15:21:18 -060090 auto var = envVars.substr(varPosition);
91 priority = std::stoi(var.substr(versionVar.length()));
Saqib Khan1eef62d2017-08-10 15:29:34 -050092 return true;
93 }
94 }
95 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -060096 catch (const std::exception& e)
97 {
98 }
Saqib Khan1eef62d2017-08-10 15:29:34 -050099
100 return false;
Saqib Khan5d532672017-08-09 10:44:50 -0500101}
102
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600103void removePersistDataDirectory(const std::string& versionId)
Saqib Khan5d532672017-08-09 10:44:50 -0500104{
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600105 auto path = fs::path(PERSIST_DIR) / versionId;
Saqib Khan5d532672017-08-09 10:44:50 -0500106 if (fs::exists(path))
107 {
Adriana Kobylak687e75e2019-11-07 11:23:25 -0600108 fs::remove_all(path);
Saqib Khan5d532672017-08-09 10:44:50 -0500109 }
110}
111
Gunnar Millsfa34e022018-09-04 10:05:45 -0500112} // namespace updater
Saqib Khan5d532672017-08-09 10:44:50 -0500113} // namespace software
Gunnar Millsfa34e022018-09-04 10:05:45 -0500114} // namespace phosphor