blob: 50127a281e09e822b9a3ec66073bf830b79c5f93 [file] [log] [blame]
Michael Tritz60bc20f2017-07-29 23:32:21 -05001#include "config.h"
Gunnar Millsf6ed5892018-09-07 17:08:02 -05002
Michael Tritz60bc20f2017-07-29 23:32:21 -05003#include "serialize.hpp"
Gunnar Millsf6ed5892018-09-07 17:08:02 -05004
5#include <cereal/archives/json.hpp>
Brad Bishop8facccf2020-11-04 09:44:58 -05006#include <sdbusplus/server.hpp>
7
Brad Bishop9f44c992020-11-06 14:48:46 -05008#include <filesystem>
Gunnar Millsf6ed5892018-09-07 17:08:02 -05009#include <fstream>
Michael Tritz60bc20f2017-07-29 23:32:21 -050010
11namespace openpower
12{
13namespace software
14{
15namespace updater
16{
17
Lei YU1db9adf2019-03-05 16:02:31 +080018void storeToFile(const std::string& versionId, uint8_t priority)
Michael Tritz60bc20f2017-07-29 23:32:21 -050019{
Michael Tritz40ecdb62017-11-20 16:56:41 -060020 auto bus = sdbusplus::bus::new_default();
21
Brad Bishop9f44c992020-11-06 14:48:46 -050022 if (!std::filesystem::is_directory(PERSIST_DIR))
Michael Tritz60bc20f2017-07-29 23:32:21 -050023 {
Brad Bishop9f44c992020-11-06 14:48:46 -050024 std::filesystem::create_directories(PERSIST_DIR);
Michael Tritz60bc20f2017-07-29 23:32:21 -050025 }
Michael Tritz60bc20f2017-07-29 23:32:21 -050026
Michael Tritz4fb952c2017-08-13 14:55:04 -050027 // store one copy in /var/lib/obmc/openpower-pnor-code-mgmt/[versionId]
28 auto varPath = PERSIST_DIR + versionId;
29 std::ofstream varOutput(varPath.c_str());
30 cereal::JSONOutputArchive varArchive(varOutput);
31 varArchive(cereal::make_nvp("priority", priority));
32
Brad Bishop9f44c992020-11-06 14:48:46 -050033 if (std::filesystem::is_directory(PNOR_RW_PREFIX + versionId))
Michael Tritz4fb952c2017-08-13 14:55:04 -050034 {
35 // store another copy in /media/pnor-rw-[versionId]/[versionId]
36 auto rwPath = PNOR_RW_PREFIX + versionId + "/" + versionId;
37 std::ofstream rwOutput(rwPath.c_str());
38 cereal::JSONOutputArchive rwArchive(rwOutput);
39 rwArchive(cereal::make_nvp("priority", priority));
40 }
Michael Tritz40ecdb62017-11-20 16:56:41 -060041
42 // lastly, store the priority as an environment variable pnor-[versionId]
43 std::string serviceFile = "obmc-flash-bmc-setenv@pnor\\x2d" + versionId +
Adriana Kobylak70dcb632018-02-27 15:46:52 -060044 "\\x3d" + std::to_string(priority) + ".service";
45 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
46 SYSTEMD_INTERFACE, "StartUnit");
Michael Tritz40ecdb62017-11-20 16:56:41 -060047 method.append(serviceFile, "replace");
48 bus.call_noreply(method);
Michael Tritz60bc20f2017-07-29 23:32:21 -050049}
50
Lei YU1db9adf2019-03-05 16:02:31 +080051bool restoreFromFile(const std::string& versionId, uint8_t& priority)
Michael Tritz60bc20f2017-07-29 23:32:21 -050052{
Michael Tritz4fb952c2017-08-13 14:55:04 -050053 auto varPath = PERSIST_DIR + versionId;
Brad Bishop9f44c992020-11-06 14:48:46 -050054 if (std::filesystem::exists(varPath))
Michael Tritz60bc20f2017-07-29 23:32:21 -050055 {
Michael Tritz4fb952c2017-08-13 14:55:04 -050056 std::ifstream varInput(varPath.c_str(), std::ios::in);
Michael Tritz36417922017-08-04 14:00:29 -050057 try
58 {
Michael Tritz4fb952c2017-08-13 14:55:04 -050059 cereal::JSONInputArchive varArchive(varInput);
60 varArchive(cereal::make_nvp("priority", priority));
Michael Tritz36417922017-08-04 14:00:29 -050061 return true;
62 }
Patrick Williams97a709b2021-10-06 13:05:40 -050063 catch (const cereal::RapidJSONException& e)
Michael Tritz36417922017-08-04 14:00:29 -050064 {
Brad Bishop9f44c992020-11-06 14:48:46 -050065 std::filesystem::remove(varPath);
Michael Tritz4fb952c2017-08-13 14:55:04 -050066 }
67 }
68
69 auto rwPath = PNOR_RW_PREFIX + versionId + "/" + versionId;
Brad Bishop9f44c992020-11-06 14:48:46 -050070 if (std::filesystem::exists(rwPath))
Michael Tritz4fb952c2017-08-13 14:55:04 -050071 {
72 std::ifstream rwInput(rwPath.c_str(), std::ios::in);
73 try
74 {
75 cereal::JSONInputArchive rwArchive(rwInput);
76 rwArchive(cereal::make_nvp("priority", priority));
77 return true;
78 }
Patrick Williams97a709b2021-10-06 13:05:40 -050079 catch (const cereal::RapidJSONException& e)
Michael Tritz4fb952c2017-08-13 14:55:04 -050080 {
Brad Bishop9f44c992020-11-06 14:48:46 -050081 std::filesystem::remove(rwPath);
Michael Tritz36417922017-08-04 14:00:29 -050082 }
Michael Tritz60bc20f2017-07-29 23:32:21 -050083 }
Michael Tritz40ecdb62017-11-20 16:56:41 -060084
85 try
86 {
87 std::string devicePath = "/dev/mtd/u-boot-env";
88
Brad Bishop9f44c992020-11-06 14:48:46 -050089 if (std::filesystem::exists(devicePath) && !devicePath.empty())
Michael Tritz40ecdb62017-11-20 16:56:41 -060090 {
91 std::ifstream input(devicePath.c_str());
92 std::string envVars;
93 std::getline(input, envVars);
94
95 std::string versionVar = "pnor-" + versionId + "=";
96 auto varPosition = envVars.find(versionVar);
97
98 if (varPosition != std::string::npos)
99 {
100 // Grab the environment variable for this versionId. These
101 // variables follow the format "pnor-[versionId]=[priority]\0"
102 auto var = envVars.substr(varPosition);
103 priority = std::stoi(var.substr(versionVar.length()));
104 return true;
105 }
106 }
107 }
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600108 catch (const std::exception& e)
Brad Bishop8facccf2020-11-04 09:44:58 -0500109 {}
Michael Tritz40ecdb62017-11-20 16:56:41 -0600110
Michael Tritz36417922017-08-04 14:00:29 -0500111 return false;
Michael Tritz60bc20f2017-07-29 23:32:21 -0500112}
113
Lei YU1db9adf2019-03-05 16:02:31 +0800114void removeFile(const std::string& versionId)
Michael Tritz60bc20f2017-07-29 23:32:21 -0500115{
Michael Tritz40ecdb62017-11-20 16:56:41 -0600116 auto bus = sdbusplus::bus::new_default();
117
118 // Clear the environment variable pnor-[versionId].
Patrick Williams7fb6c342023-05-10 07:50:18 -0500119 std::string serviceFile = "obmc-flash-bmc-setenv@pnor\\x2d" + versionId +
120 ".service";
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600121 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
122 SYSTEMD_INTERFACE, "StartUnit");
Michael Tritz40ecdb62017-11-20 16:56:41 -0600123 method.append(serviceFile, "replace");
124 bus.call_noreply(method);
125
126 // Delete the file /var/lib/obmc/openpower-pnor-code-mgmt/[versionId].
127 // Note that removeFile() is called in the case of a version being deleted,
128 // so the file /media/pnor-rw-[versionId]/[versionId] will also be deleted
129 // along with its surrounding directory.
Michael Tritz60bc20f2017-07-29 23:32:21 -0500130 std::string path = PERSIST_DIR + versionId;
Brad Bishop9f44c992020-11-06 14:48:46 -0500131 if (std::filesystem::exists(path))
Michael Tritz60bc20f2017-07-29 23:32:21 -0500132 {
Brad Bishop9f44c992020-11-06 14:48:46 -0500133 std::filesystem::remove(path);
Michael Tritz60bc20f2017-07-29 23:32:21 -0500134 }
135}
136
137} // namespace updater
138} // namespace software
139} // namespace openpower