blob: e047b8723a507a0f4d6795897ac24e4325fad4c7 [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
Gunnar Millsf6ed5892018-09-07 17:08:02 -05008#include <experimental/filesystem>
9#include <fstream>
Michael Tritz60bc20f2017-07-29 23:32:21 -050010
11namespace openpower
12{
13namespace software
14{
15namespace updater
16{
17
18namespace fs = std::experimental::filesystem;
19
Lei YU1db9adf2019-03-05 16:02:31 +080020void storeToFile(const std::string& versionId, uint8_t priority)
Michael Tritz60bc20f2017-07-29 23:32:21 -050021{
Michael Tritz40ecdb62017-11-20 16:56:41 -060022 auto bus = sdbusplus::bus::new_default();
23
Gunnar Millse7ff6452017-09-21 16:19:36 -050024 if (!fs::is_directory(PERSIST_DIR))
Michael Tritz60bc20f2017-07-29 23:32:21 -050025 {
Michael Tritz48d9a4e2017-09-18 14:30:46 -050026 fs::create_directories(PERSIST_DIR);
Michael Tritz60bc20f2017-07-29 23:32:21 -050027 }
Michael Tritz60bc20f2017-07-29 23:32:21 -050028
Michael Tritz4fb952c2017-08-13 14:55:04 -050029 // store one copy in /var/lib/obmc/openpower-pnor-code-mgmt/[versionId]
30 auto varPath = PERSIST_DIR + versionId;
31 std::ofstream varOutput(varPath.c_str());
32 cereal::JSONOutputArchive varArchive(varOutput);
33 varArchive(cereal::make_nvp("priority", priority));
34
Gunnar Millse7ff6452017-09-21 16:19:36 -050035 if (fs::is_directory(PNOR_RW_PREFIX + versionId))
Michael Tritz4fb952c2017-08-13 14:55:04 -050036 {
37 // store another copy in /media/pnor-rw-[versionId]/[versionId]
38 auto rwPath = PNOR_RW_PREFIX + versionId + "/" + versionId;
39 std::ofstream rwOutput(rwPath.c_str());
40 cereal::JSONOutputArchive rwArchive(rwOutput);
41 rwArchive(cereal::make_nvp("priority", priority));
42 }
Michael Tritz40ecdb62017-11-20 16:56:41 -060043
44 // lastly, store the priority as an environment variable pnor-[versionId]
45 std::string serviceFile = "obmc-flash-bmc-setenv@pnor\\x2d" + versionId +
Adriana Kobylak70dcb632018-02-27 15:46:52 -060046 "\\x3d" + std::to_string(priority) + ".service";
47 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
48 SYSTEMD_INTERFACE, "StartUnit");
Michael Tritz40ecdb62017-11-20 16:56:41 -060049 method.append(serviceFile, "replace");
50 bus.call_noreply(method);
Michael Tritz60bc20f2017-07-29 23:32:21 -050051}
52
Lei YU1db9adf2019-03-05 16:02:31 +080053bool restoreFromFile(const std::string& versionId, uint8_t& priority)
Michael Tritz60bc20f2017-07-29 23:32:21 -050054{
Michael Tritz4fb952c2017-08-13 14:55:04 -050055 auto varPath = PERSIST_DIR + versionId;
56 if (fs::exists(varPath))
Michael Tritz60bc20f2017-07-29 23:32:21 -050057 {
Michael Tritz4fb952c2017-08-13 14:55:04 -050058 std::ifstream varInput(varPath.c_str(), std::ios::in);
Michael Tritz36417922017-08-04 14:00:29 -050059 try
60 {
Michael Tritz4fb952c2017-08-13 14:55:04 -050061 cereal::JSONInputArchive varArchive(varInput);
62 varArchive(cereal::make_nvp("priority", priority));
Michael Tritz36417922017-08-04 14:00:29 -050063 return true;
64 }
Gunnar Millse7ff6452017-09-21 16:19:36 -050065 catch (cereal::RapidJSONException& e)
Michael Tritz36417922017-08-04 14:00:29 -050066 {
Michael Tritz4fb952c2017-08-13 14:55:04 -050067 fs::remove(varPath);
68 }
69 }
70
71 auto rwPath = PNOR_RW_PREFIX + versionId + "/" + versionId;
72 if (fs::exists(rwPath))
73 {
74 std::ifstream rwInput(rwPath.c_str(), std::ios::in);
75 try
76 {
77 cereal::JSONInputArchive rwArchive(rwInput);
78 rwArchive(cereal::make_nvp("priority", priority));
79 return true;
80 }
Gunnar Millse7ff6452017-09-21 16:19:36 -050081 catch (cereal::RapidJSONException& e)
Michael Tritz4fb952c2017-08-13 14:55:04 -050082 {
83 fs::remove(rwPath);
Michael Tritz36417922017-08-04 14:00:29 -050084 }
Michael Tritz60bc20f2017-07-29 23:32:21 -050085 }
Michael Tritz40ecdb62017-11-20 16:56:41 -060086
87 try
88 {
89 std::string devicePath = "/dev/mtd/u-boot-env";
90
91 if (fs::exists(devicePath) && !devicePath.empty())
92 {
93 std::ifstream input(devicePath.c_str());
94 std::string envVars;
95 std::getline(input, envVars);
96
97 std::string versionVar = "pnor-" + versionId + "=";
98 auto varPosition = envVars.find(versionVar);
99
100 if (varPosition != std::string::npos)
101 {
102 // Grab the environment variable for this versionId. These
103 // variables follow the format "pnor-[versionId]=[priority]\0"
104 auto var = envVars.substr(varPosition);
105 priority = std::stoi(var.substr(versionVar.length()));
106 return true;
107 }
108 }
109 }
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600110 catch (const std::exception& e)
Brad Bishop8facccf2020-11-04 09:44:58 -0500111 {}
Michael Tritz40ecdb62017-11-20 16:56:41 -0600112
Michael Tritz36417922017-08-04 14:00:29 -0500113 return false;
Michael Tritz60bc20f2017-07-29 23:32:21 -0500114}
115
Lei YU1db9adf2019-03-05 16:02:31 +0800116void removeFile(const std::string& versionId)
Michael Tritz60bc20f2017-07-29 23:32:21 -0500117{
Michael Tritz40ecdb62017-11-20 16:56:41 -0600118 auto bus = sdbusplus::bus::new_default();
119
120 // Clear the environment variable pnor-[versionId].
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600121 std::string serviceFile =
122 "obmc-flash-bmc-setenv@pnor\\x2d" + versionId + ".service";
123 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
124 SYSTEMD_INTERFACE, "StartUnit");
Michael Tritz40ecdb62017-11-20 16:56:41 -0600125 method.append(serviceFile, "replace");
126 bus.call_noreply(method);
127
128 // Delete the file /var/lib/obmc/openpower-pnor-code-mgmt/[versionId].
129 // Note that removeFile() is called in the case of a version being deleted,
130 // so the file /media/pnor-rw-[versionId]/[versionId] will also be deleted
131 // along with its surrounding directory.
Michael Tritz60bc20f2017-07-29 23:32:21 -0500132 std::string path = PERSIST_DIR + versionId;
133 if (fs::exists(path))
134 {
135 fs::remove(path);
136 }
137}
138
139} // namespace updater
140} // namespace software
141} // namespace openpower