blob: e28129f6b123af29bbe4ed3a41ebc61d06411692 [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>
George Liu44b9fef2023-02-07 14:31:32 +080011#include <system_error>
Adriana Kobylak58aa7502020-06-08 11:12:11 -050012
Saqib Khan5d532672017-08-09 10:44:50 -050013namespace phosphor
14{
15namespace software
16{
17namespace updater
18{
19
Patrick Williamsc9bb6422021-08-27 06:18:35 -050020PHOSPHOR_LOG2_USING;
Adriana Kobylakc98d9122020-05-05 10:36:01 -050021namespace fs = std::filesystem;
Saqib Khan5d532672017-08-09 10:44:50 -050022
Adriana Kobylak687e75e2019-11-07 11:23:25 -060023const std::string priorityName = "priority";
Adriana Kobylakec4eec32019-11-13 14:28:35 -060024const std::string purposeName = "purpose";
Adriana Kobylak687e75e2019-11-07 11:23:25 -060025
Adriana Kobylak780220f2022-01-18 20:01:53 +000026void storePriority(const std::string& flashId, uint8_t priority)
Saqib Khan5d532672017-08-09 10:44:50 -050027{
George Liu44b9fef2023-02-07 14:31:32 +080028 std::error_code ec;
Adriana Kobylak780220f2022-01-18 20:01:53 +000029 auto path = fs::path(PERSIST_DIR) / flashId;
George Liu44b9fef2023-02-07 14:31:32 +080030 if (!fs::is_directory(path, ec))
Saqib Khan5d532672017-08-09 10:44:50 -050031 {
George Liu44b9fef2023-02-07 14:31:32 +080032 if (fs::exists(path, ec))
Adriana Kobylak687e75e2019-11-07 11:23:25 -060033 {
34 // Delete if it's a non-directory file
Patrick Williamsc9bb6422021-08-27 06:18:35 -050035 warning("Removing non-directory file: {PATH}", "PATH", path);
George Liu44b9fef2023-02-07 14:31:32 +080036 fs::remove_all(path, ec);
Adriana Kobylak687e75e2019-11-07 11:23:25 -060037 }
George Liu44b9fef2023-02-07 14:31:32 +080038 fs::create_directories(path, ec);
Saqib Khan5d532672017-08-09 10:44:50 -050039 }
Adriana Kobylak687e75e2019-11-07 11:23:25 -060040 path = path / priorityName;
Saqib Khan5d532672017-08-09 10:44:50 -050041
42 std::ofstream os(path.c_str());
43 cereal::JSONOutputArchive oarchive(os);
Adriana Kobylak687e75e2019-11-07 11:23:25 -060044 oarchive(cereal::make_nvp(priorityName, priority));
Saqib Khan5d532672017-08-09 10:44:50 -050045}
46
Adriana Kobylak780220f2022-01-18 20:01:53 +000047void storePurpose(const std::string& flashId, VersionPurpose purpose)
Adriana Kobylakec4eec32019-11-13 14:28:35 -060048{
George Liu44b9fef2023-02-07 14:31:32 +080049 std::error_code ec;
Adriana Kobylak780220f2022-01-18 20:01:53 +000050 auto path = fs::path(PERSIST_DIR) / flashId;
George Liu44b9fef2023-02-07 14:31:32 +080051 if (!fs::is_directory(path, ec))
Adriana Kobylakec4eec32019-11-13 14:28:35 -060052 {
George Liu44b9fef2023-02-07 14:31:32 +080053 if (fs::exists(path, ec))
Adriana Kobylakec4eec32019-11-13 14:28:35 -060054 {
55 // Delete if it's a non-directory file
Patrick Williamsc9bb6422021-08-27 06:18:35 -050056 warning("Removing non-directory file: {PATH}", "PATH", path);
George Liu44b9fef2023-02-07 14:31:32 +080057 fs::remove_all(path, ec);
Adriana Kobylakec4eec32019-11-13 14:28:35 -060058 }
George Liu44b9fef2023-02-07 14:31:32 +080059 fs::create_directories(path, ec);
Adriana Kobylakec4eec32019-11-13 14:28:35 -060060 }
61 path = path / purposeName;
62
63 std::ofstream os(path.c_str());
64 cereal::JSONOutputArchive oarchive(os);
65 oarchive(cereal::make_nvp(purposeName, purpose));
66}
67
Adriana Kobylak780220f2022-01-18 20:01:53 +000068bool restorePriority(const std::string& flashId, uint8_t& priority)
Saqib Khan5d532672017-08-09 10:44:50 -050069{
George Liu44b9fef2023-02-07 14:31:32 +080070 std::error_code ec;
Adriana Kobylak780220f2022-01-18 20:01:53 +000071 auto path = fs::path(PERSIST_DIR) / flashId / priorityName;
George Liu44b9fef2023-02-07 14:31:32 +080072 if (fs::exists(path, ec))
Saqib Khan5d532672017-08-09 10:44:50 -050073 {
74 std::ifstream is(path.c_str(), std::ios::in);
Saqib Khan1eef62d2017-08-10 15:29:34 -050075 try
76 {
77 cereal::JSONInputArchive iarchive(is);
Adriana Kobylak687e75e2019-11-07 11:23:25 -060078 iarchive(cereal::make_nvp(priorityName, priority));
Saqib Khan1eef62d2017-08-10 15:29:34 -050079 return true;
80 }
Patrick Williams58e18972021-10-06 12:25:58 -050081 catch (const cereal::Exception& e)
Saqib Khan1eef62d2017-08-10 15:29:34 -050082 {
George Liu44b9fef2023-02-07 14:31:32 +080083 fs::remove_all(path, ec);
Saqib Khan1eef62d2017-08-10 15:29:34 -050084 }
Saqib Khan5d532672017-08-09 10:44:50 -050085 }
Saqib Khan1eef62d2017-08-10 15:29:34 -050086
87 // Find the mtd device "u-boot-env" to retrieve the environment variables
88 std::ifstream mtdDevices("/proc/mtd");
Pavithra Barithaya7b7fb302024-06-26 01:37:41 -050089 std::string device;
90 std::string devicePath;
Saqib Khan1eef62d2017-08-10 15:29:34 -050091
92 try
93 {
Gunnar Millsffe04922017-10-06 13:20:43 -050094 while (std::getline(mtdDevices, device))
95 {
Saqib Khan1eef62d2017-08-10 15:29:34 -050096 if (device.find("u-boot-env") != std::string::npos)
97 {
98 devicePath = "/dev/" + device.substr(0, device.find(':'));
99 break;
100 }
101 }
102
103 if (!devicePath.empty())
104 {
105 std::ifstream input(devicePath.c_str());
106 std::string envVars;
107 std::getline(input, envVars);
108
Adriana Kobylak780220f2022-01-18 20:01:53 +0000109 std::string versionVar = flashId + "=";
Michael Tritz6273efd2017-12-14 15:21:18 -0600110 auto varPosition = envVars.find(versionVar);
111
112 if (varPosition != std::string::npos)
Saqib Khan1eef62d2017-08-10 15:29:34 -0500113 {
Adriana Kobylak780220f2022-01-18 20:01:53 +0000114 // Grab the environment variable for this flashId. These
115 // variables follow the format "flashId=priority\0"
Michael Tritz6273efd2017-12-14 15:21:18 -0600116 auto var = envVars.substr(varPosition);
117 priority = std::stoi(var.substr(versionVar.length()));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500118 return true;
119 }
120 }
121 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600122 catch (const std::exception& e)
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500123 {
124 error("Error during processing: {ERROR}", "ERROR", e);
125 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500126
127 return false;
Saqib Khan5d532672017-08-09 10:44:50 -0500128}
129
Adriana Kobylak780220f2022-01-18 20:01:53 +0000130bool restorePurpose(const std::string& flashId, VersionPurpose& purpose)
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600131{
George Liu44b9fef2023-02-07 14:31:32 +0800132 std::error_code ec;
Adriana Kobylak780220f2022-01-18 20:01:53 +0000133 auto path = fs::path(PERSIST_DIR) / flashId / purposeName;
George Liu44b9fef2023-02-07 14:31:32 +0800134 if (fs::exists(path, ec))
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600135 {
136 std::ifstream is(path.c_str(), std::ios::in);
137 try
138 {
139 cereal::JSONInputArchive iarchive(is);
140 iarchive(cereal::make_nvp(purposeName, purpose));
141 return true;
142 }
Patrick Williams58e18972021-10-06 12:25:58 -0500143 catch (const cereal::Exception& e)
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600144 {
George Liu44b9fef2023-02-07 14:31:32 +0800145 fs::remove_all(path, ec);
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600146 }
147 }
148
149 return false;
150}
151
Adriana Kobylak780220f2022-01-18 20:01:53 +0000152void removePersistDataDirectory(const std::string& flashId)
Saqib Khan5d532672017-08-09 10:44:50 -0500153{
George Liu44b9fef2023-02-07 14:31:32 +0800154 std::error_code ec;
Adriana Kobylak780220f2022-01-18 20:01:53 +0000155 auto path = fs::path(PERSIST_DIR) / flashId;
George Liu44b9fef2023-02-07 14:31:32 +0800156 if (fs::exists(path, ec))
Saqib Khan5d532672017-08-09 10:44:50 -0500157 {
George Liu44b9fef2023-02-07 14:31:32 +0800158 fs::remove_all(path, ec);
Saqib Khan5d532672017-08-09 10:44:50 -0500159 }
160}
161
Gunnar Millsfa34e022018-09-04 10:05:45 -0500162} // namespace updater
Saqib Khan5d532672017-08-09 10:44:50 -0500163} // namespace software
Gunnar Millsfa34e022018-09-04 10:05:45 -0500164} // namespace phosphor