blob: 140d5cc5b1ecc32a8c31860af68ded215adf88a7 [file] [log] [blame]
Lei YU322f3f42019-02-21 16:10:41 +08001#include "config.h"
2
3#include "item_updater_static.hpp"
4
Lei YUb53425d2019-02-22 11:38:40 +08005#include "activation_static.hpp"
Lei YUdec8cf92019-02-21 17:47:05 +08006#include "version.hpp"
7
Lei YUa7b4ade2019-02-25 17:49:29 +08008#include <array>
Lei YUdec8cf92019-02-21 17:47:05 +08009#include <cstring>
10#include <filesystem>
11#include <fstream>
12#include <phosphor-logging/elog-errors.hpp>
13#include <phosphor-logging/log.hpp>
Lei YUa2e67162019-02-22 17:35:24 +080014#include <sstream>
Lei YUdec8cf92019-02-21 17:47:05 +080015#include <string>
Lei YU5efca582019-02-25 17:05:29 +080016#include <tuple>
17#include <xyz/openbmc_project/Common/error.hpp>
Lei YUdec8cf92019-02-21 17:47:05 +080018
19using namespace sdbusplus::xyz::openbmc_project::Common::Error;
20using namespace phosphor::logging;
Lei YU5efca582019-02-25 17:05:29 +080021using sdbusplus::exception::SdBusError;
Lei YUdec8cf92019-02-21 17:47:05 +080022
23// When you see server:: you know we're referencing our base class
24namespace server = sdbusplus::xyz::openbmc_project::Software::server;
25namespace fs = std::filesystem;
26
27namespace utils
28{
29
30template <typename... Ts>
31std::string concat_string(Ts const&... ts)
32{
33 std::stringstream s;
34 ((s << ts << " "), ...) << std::endl;
35 return s.str();
36}
37
38// Helper function to run pflash command
Lei YU5efca582019-02-25 17:05:29 +080039// Returns return code and the stdout
Lei YUdec8cf92019-02-21 17:47:05 +080040template <typename... Ts>
Lei YU5efca582019-02-25 17:05:29 +080041std::pair<int, std::string> pflash(Ts const&... ts)
Lei YUdec8cf92019-02-21 17:47:05 +080042{
43 std::array<char, 512> buffer;
44 std::string cmd = concat_string("pflash", ts...);
45 std::stringstream result;
Lei YU5efca582019-02-25 17:05:29 +080046 int rc;
47 FILE* pipe = popen(cmd.c_str(), "r");
Lei YUdec8cf92019-02-21 17:47:05 +080048 if (!pipe)
49 {
50 throw std::runtime_error("popen() failed!");
51 }
Lei YU5efca582019-02-25 17:05:29 +080052 while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
Lei YUdec8cf92019-02-21 17:47:05 +080053 {
54 result << buffer.data();
55 }
Lei YU5efca582019-02-25 17:05:29 +080056 rc = pclose(pipe);
57 return {rc, result.str()};
Lei YUdec8cf92019-02-21 17:47:05 +080058}
59
60std::string getPNORVersion()
61{
62 // A signed version partition will have an extra 4K header starting with
63 // the magic number 17082011 in big endian:
64 // https://github.com/open-power/skiboot/blob/master/libstb/container.h#L47
65
66 constexpr uint8_t MAGIC[] = {0x17, 0x08, 0x20, 0x11};
67 constexpr auto MAGIC_SIZE = sizeof(MAGIC);
68 static_assert(MAGIC_SIZE == 4);
69
70 auto tmp = fs::temp_directory_path();
71 std::string tmpDir(tmp / "versionXXXXXX");
72 if (!mkdtemp(tmpDir.data()))
73 {
74 log<level::ERR>("Failed to create temp dir");
75 return {};
76 }
77
78 fs::path versionFile = tmpDir;
79 versionFile /= "version";
80
Lei YU5efca582019-02-25 17:05:29 +080081 auto [rc, r] =
82 pflash("-P VERSION -r", versionFile.string(), "2>&1 > /dev/null");
83 if (rc != 0)
84 {
85 log<level::ERR>("Failed to read VERSION", entry("RETURNCODE=%d", rc));
86 return {};
87 }
88
Lei YUdec8cf92019-02-21 17:47:05 +080089 std::ifstream f(versionFile.c_str(), std::ios::in | std::ios::binary);
90 uint8_t magic[MAGIC_SIZE];
91 std::string version;
92
93 f.read(reinterpret_cast<char*>(magic), MAGIC_SIZE);
94 f.seekg(0, std::ios::beg);
95 if (std::memcmp(magic, MAGIC, MAGIC_SIZE) == 0)
96 {
97 // Skip the first 4K header
98 f.ignore(4096);
99 }
100
101 getline(f, version, '\0');
102 f.close();
103
104 // Clear the temp dir
105 std::error_code ec;
106 fs::remove_all(tmpDir, ec);
107 if (ec)
108 {
109 log<level::ERR>("Failed to remove temp dir",
110 entry("DIR=%s", tmpDir.c_str()),
111 entry("ERR=%s", ec.message().c_str()));
112 }
113
114 return version;
115}
116
Lei YU6cecc9b2019-02-26 17:27:23 +0800117void pnorClear(const std::string& part, bool shouldEcc = true)
Lei YUa7b4ade2019-02-25 17:49:29 +0800118{
119 int rc;
120 std::tie(rc, std::ignore) =
Lei YU6cecc9b2019-02-26 17:27:23 +0800121 utils::pflash("-P", part, shouldEcc ? "-c" : "-e", "-f >/dev/null");
Lei YUa7b4ade2019-02-25 17:49:29 +0800122 if (rc != 0)
123 {
124 log<level::ERR>("Failed to clear partition",
125 entry("PART=%s", part.c_str()),
126 entry("RETURNCODE=%d", rc));
127 }
128 else
129 {
130 log<level::INFO>("Clear partition successfully",
131 entry("PART=%s", part.c_str()));
132 }
133}
134
Lei YU6cecc9b2019-02-26 17:27:23 +0800135// The pair contains the partition name and if it should use ECC clear
136using PartClear = std::pair<std::string, bool>;
137
138std::vector<PartClear> getPartsToClear(const std::string& info)
139{
140 std::vector<PartClear> ret;
141 std::istringstream iss(info);
142 std::string line;
143
144 while (std::getline(iss, line))
145 {
146 // Each line looks like
147 // ID=06 MVPD 0x0012d000..0x001bd000 (actual=0x00090000) [E--P--F-C-]
148 // Flag 'F' means REPROVISION
149 // Flag 'C' means CLEARECC
150 auto flags = line.substr(line.find('['));
151 if (flags.find('F') != std::string::npos)
152 {
153 // This is a partition to be cleared
154 line = line.substr(line.find_first_of(' ')); // Skiping "ID=xx"
155 line = line.substr(line.find_first_not_of(' ')); // Skipping spaces
156 line = line.substr(0, line.find_first_of(' ')); // The part name
157 bool ecc = flags.find('C') != std::string::npos;
158 ret.emplace_back(line, ecc);
159 }
160 }
161 return ret;
162}
163
164// Get partitions that should be cleared
165std::vector<PartClear> getPartsToClear()
166{
167 const auto& [rc, pflashInfo] = pflash("-i | grep ^ID | grep 'F'");
168 return getPartsToClear(pflashInfo);
169}
170
Lei YUdec8cf92019-02-21 17:47:05 +0800171} // namespace utils
172
Lei YU322f3f42019-02-21 16:10:41 +0800173namespace openpower
174{
175namespace software
176{
177namespace updater
178{
Lei YU5efca582019-02-25 17:05:29 +0800179// TODO: Change paths once openbmc/openbmc#1663 is completed.
180constexpr auto MBOXD_INTERFACE = "org.openbmc.mboxd";
181constexpr auto MBOXD_PATH = "/org/openbmc/mboxd";
182
Lei YU322f3f42019-02-21 16:10:41 +0800183std::unique_ptr<Activation> ItemUpdaterStatic::createActivationObject(
184 const std::string& path, const std::string& versionId,
185 const std::string& extVersion,
186 sdbusplus::xyz::openbmc_project::Software::server::Activation::Activations
187 activationStatus,
188 AssociationList& assocs)
189{
Lei YUb53425d2019-02-22 11:38:40 +0800190 return std::make_unique<ActivationStatic>(
191 bus, path, *this, versionId, extVersion, activationStatus, assocs);
Lei YU322f3f42019-02-21 16:10:41 +0800192}
193
194std::unique_ptr<Version> ItemUpdaterStatic::createVersionObject(
195 const std::string& objPath, const std::string& versionId,
196 const std::string& versionString,
197 sdbusplus::xyz::openbmc_project::Software::server::Version::VersionPurpose
198 versionPurpose,
199 const std::string& filePath)
200{
Lei YUb53425d2019-02-22 11:38:40 +0800201 auto version = std::make_unique<Version>(
202 bus, objPath, *this, versionId, versionString, versionPurpose, filePath,
203 std::bind(&ItemUpdaterStatic::erase, this, std::placeholders::_1));
204 version->deleteObject = std::make_unique<Delete>(bus, objPath, *version);
205 return version;
Lei YU322f3f42019-02-21 16:10:41 +0800206}
207
208bool ItemUpdaterStatic::validateImage(const std::string& path)
209{
Lei YUb53425d2019-02-22 11:38:40 +0800210 // There is no need to validate static layout pnor
Lei YU322f3f42019-02-21 16:10:41 +0800211 return true;
212}
213
214void ItemUpdaterStatic::processPNORImage()
215{
Lei YUdec8cf92019-02-21 17:47:05 +0800216 auto fullVersion = utils::getPNORVersion();
217
218 const auto& [version, extendedVersion] = Version::getVersions(fullVersion);
219 auto id = Version::getId(version);
220
221 auto activationState = server::Activation::Activations::Active;
222 if (version.empty())
223 {
224 log<level::ERR>("Failed to read version",
225 entry("VERSION=%s", fullVersion.c_str()));
226 activationState = server::Activation::Activations::Invalid;
227 }
228
229 if (extendedVersion.empty())
230 {
231 log<level::ERR>("Failed to read extendedVersion",
232 entry("VERSION=%s", fullVersion.c_str()));
233 activationState = server::Activation::Activations::Invalid;
234 }
235
236 auto purpose = server::Version::VersionPurpose::Host;
237 auto path = fs::path(SOFTWARE_OBJPATH) / id;
238 AssociationList associations = {};
239
240 if (activationState == server::Activation::Activations::Active)
241 {
242 // Create an association to the host inventory item
243 associations.emplace_back(std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
244 ACTIVATION_REV_ASSOCIATION,
245 HOST_INVENTORY_PATH));
246
247 // Create an active association since this image is active
248 createActiveAssociation(path);
249 }
250
Lei YUb53425d2019-02-22 11:38:40 +0800251 // Create Activation instance for this version.
252 activations.insert(std::make_pair(
253 id, std::make_unique<ActivationStatic>(bus, path, *this, id,
254 extendedVersion, activationState,
255 associations)));
256
257 // If Active, create RedundancyPriority instance for this version.
258 if (activationState == server::Activation::Activations::Active)
259 {
260 // For now only one PNOR is supported with static layout
261 activations.find(id)->second->redundancyPriority =
262 std::make_unique<RedundancyPriority>(
263 bus, path, *(activations.find(id)->second), 0);
264 }
265
Lei YUdec8cf92019-02-21 17:47:05 +0800266 // Create Version instance for this version.
267 auto versionPtr = std::make_unique<Version>(
268 bus, path, *this, id, version, purpose, "",
269 std::bind(&ItemUpdaterStatic::erase, this, std::placeholders::_1));
270 versionPtr->deleteObject = std::make_unique<Delete>(bus, path, *versionPtr);
271 versions.insert(std::make_pair(id, std::move(versionPtr)));
272
273 if (!id.empty())
274 {
275 updateFunctionalAssociation(id);
276 }
Lei YU322f3f42019-02-21 16:10:41 +0800277}
278
279void ItemUpdaterStatic::reset()
280{
Lei YU6cecc9b2019-02-26 17:27:23 +0800281 auto partitions = utils::getPartsToClear();
Lei YUa7b4ade2019-02-25 17:49:29 +0800282 std::vector<uint8_t> mboxdArgs;
283
284 // Suspend mboxd - no args required.
285 auto dbusCall = bus.new_method_call(MBOXD_INTERFACE, MBOXD_PATH,
286 MBOXD_INTERFACE, "cmd");
287 dbusCall.append(static_cast<uint8_t>(3), mboxdArgs);
288
289 try
290 {
291 bus.call_noreply(dbusCall);
292 }
293 catch (const SdBusError& e)
294 {
295 log<level::ERR>("Error in mboxd suspend call",
296 entry("ERROR=%s", e.what()));
297 elog<InternalFailure>();
298 }
299 for (auto p : partitions)
300 {
301 utils::pnorClear(p.first, p.second);
302 }
303
304 // Resume mboxd with arg 1, indicating that the flash was modified.
305 dbusCall = bus.new_method_call(MBOXD_INTERFACE, MBOXD_PATH, MBOXD_INTERFACE,
306 "cmd");
307 mboxdArgs.push_back(1);
308 dbusCall.append(static_cast<uint8_t>(4), mboxdArgs);
309
310 try
311 {
312 bus.call_noreply(dbusCall);
313 }
314 catch (const SdBusError& e)
315 {
316 log<level::ERR>("Error in mboxd resume call",
317 entry("ERROR=%s", e.what()));
318 elog<InternalFailure>();
319 }
Lei YU322f3f42019-02-21 16:10:41 +0800320}
321
322bool ItemUpdaterStatic::isVersionFunctional(const std::string& versionId)
323{
Lei YUa2e67162019-02-22 17:35:24 +0800324 return versionId == functionalVersionId;
Lei YU322f3f42019-02-21 16:10:41 +0800325}
326
327void ItemUpdaterStatic::freePriority(uint8_t value,
328 const std::string& versionId)
329{
330}
331
332void ItemUpdaterStatic::deleteAll()
333{
Lei YUa2e67162019-02-22 17:35:24 +0800334 // Static layout has only one active and function pnor
335 // There is no implementation for this interface
Lei YU322f3f42019-02-21 16:10:41 +0800336}
337
Lei YU6da3dae2019-02-28 14:26:37 +0800338bool ItemUpdaterStatic::freeSpace()
Lei YU322f3f42019-02-21 16:10:41 +0800339{
Lei YUa2e67162019-02-22 17:35:24 +0800340 // For now assume static layout only has 1 active PNOR,
341 // so erase the active PNOR
342 for (const auto& iter : activations)
343 {
344 if (iter.second.get()->activation() ==
345 server::Activation::Activations::Active)
346 {
Lei YU6da3dae2019-02-28 14:26:37 +0800347 return erase(iter.second->versionId);
Lei YUa2e67162019-02-22 17:35:24 +0800348 }
349 }
Lei YU6da3dae2019-02-28 14:26:37 +0800350 return false;
Lei YUa2e67162019-02-22 17:35:24 +0800351}
352
353void ItemUpdaterStatic::updateFunctionalAssociation(
354 const std::string& versionId)
355{
356 functionalVersionId = versionId;
357 ItemUpdater::updateFunctionalAssociation(versionId);
Lei YU322f3f42019-02-21 16:10:41 +0800358}
359
360void GardReset::reset()
361{
Lei YU5efca582019-02-25 17:05:29 +0800362 // Clear gard partition
363 std::vector<uint8_t> mboxdArgs;
Lei YU5efca582019-02-25 17:05:29 +0800364
365 auto dbusCall = bus.new_method_call(MBOXD_INTERFACE, MBOXD_PATH,
366 MBOXD_INTERFACE, "cmd");
367 // Suspend mboxd - no args required.
368 dbusCall.append(static_cast<uint8_t>(3), mboxdArgs);
369
370 try
371 {
372 bus.call_noreply(dbusCall);
373 }
374 catch (const SdBusError& e)
375 {
376 log<level::ERR>("Error in mboxd suspend call",
377 entry("ERROR=%s", e.what()));
378 elog<InternalFailure>();
379 }
380
381 // Clear guard partition
Lei YUa7b4ade2019-02-25 17:49:29 +0800382 utils::pnorClear("GUARD");
Lei YU5efca582019-02-25 17:05:29 +0800383
384 dbusCall = bus.new_method_call(MBOXD_INTERFACE, MBOXD_PATH, MBOXD_INTERFACE,
385 "cmd");
386 // Resume mboxd with arg 1, indicating that the flash is modified.
387 mboxdArgs.push_back(1);
388 dbusCall.append(static_cast<uint8_t>(4), mboxdArgs);
389
390 try
391 {
392 bus.call_noreply(dbusCall);
393 }
394 catch (const SdBusError& e)
395 {
396 log<level::ERR>("Error in mboxd resume call",
397 entry("ERROR=%s", e.what()));
398 elog<InternalFailure>();
399 }
Lei YU322f3f42019-02-21 16:10:41 +0800400}
401
402} // namespace updater
403} // namespace software
404} // namespace openpower