blob: ff9e8328585645f9752d09324919bee8a3773004 [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");
89 std::string device, devicePath;
90
91 try
92 {
Gunnar Millsffe04922017-10-06 13:20:43 -050093 while (std::getline(mtdDevices, device))
94 {
Saqib Khan1eef62d2017-08-10 15:29:34 -050095 if (device.find("u-boot-env") != std::string::npos)
96 {
97 devicePath = "/dev/" + device.substr(0, device.find(':'));
98 break;
99 }
100 }
101
102 if (!devicePath.empty())
103 {
104 std::ifstream input(devicePath.c_str());
105 std::string envVars;
106 std::getline(input, envVars);
107
Adriana Kobylak780220f2022-01-18 20:01:53 +0000108 std::string versionVar = flashId + "=";
Michael Tritz6273efd2017-12-14 15:21:18 -0600109 auto varPosition = envVars.find(versionVar);
110
111 if (varPosition != std::string::npos)
Saqib Khan1eef62d2017-08-10 15:29:34 -0500112 {
Adriana Kobylak780220f2022-01-18 20:01:53 +0000113 // Grab the environment variable for this flashId. These
114 // variables follow the format "flashId=priority\0"
Michael Tritz6273efd2017-12-14 15:21:18 -0600115 auto var = envVars.substr(varPosition);
116 priority = std::stoi(var.substr(versionVar.length()));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500117 return true;
118 }
119 }
120 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600121 catch (const std::exception& e)
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500122 {
123 error("Error during processing: {ERROR}", "ERROR", e);
124 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500125
126 return false;
Saqib Khan5d532672017-08-09 10:44:50 -0500127}
128
Adriana Kobylak780220f2022-01-18 20:01:53 +0000129bool restorePurpose(const std::string& flashId, VersionPurpose& purpose)
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600130{
George Liu44b9fef2023-02-07 14:31:32 +0800131 std::error_code ec;
Adriana Kobylak780220f2022-01-18 20:01:53 +0000132 auto path = fs::path(PERSIST_DIR) / flashId / purposeName;
George Liu44b9fef2023-02-07 14:31:32 +0800133 if (fs::exists(path, ec))
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600134 {
135 std::ifstream is(path.c_str(), std::ios::in);
136 try
137 {
138 cereal::JSONInputArchive iarchive(is);
139 iarchive(cereal::make_nvp(purposeName, purpose));
140 return true;
141 }
Patrick Williams58e18972021-10-06 12:25:58 -0500142 catch (const cereal::Exception& e)
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600143 {
George Liu44b9fef2023-02-07 14:31:32 +0800144 fs::remove_all(path, ec);
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600145 }
146 }
147
148 return false;
149}
150
Adriana Kobylak780220f2022-01-18 20:01:53 +0000151void removePersistDataDirectory(const std::string& flashId)
Saqib Khan5d532672017-08-09 10:44:50 -0500152{
George Liu44b9fef2023-02-07 14:31:32 +0800153 std::error_code ec;
Adriana Kobylak780220f2022-01-18 20:01:53 +0000154 auto path = fs::path(PERSIST_DIR) / flashId;
George Liu44b9fef2023-02-07 14:31:32 +0800155 if (fs::exists(path, ec))
Saqib Khan5d532672017-08-09 10:44:50 -0500156 {
George Liu44b9fef2023-02-07 14:31:32 +0800157 fs::remove_all(path, ec);
Saqib Khan5d532672017-08-09 10:44:50 -0500158 }
159}
160
Gunnar Millsfa34e022018-09-04 10:05:45 -0500161} // namespace updater
Saqib Khan5d532672017-08-09 10:44:50 -0500162} // namespace software
Gunnar Millsfa34e022018-09-04 10:05:45 -0500163} // namespace phosphor