blob: e14bdeef0e5fa0b5ff6567dcc5701315104f6333 [file] [log] [blame]
Saqib Khan7254f0e2017-04-10 21:45:37 -05001#include <string>
Adriana Kobylak5ba6b102017-05-19 09:41:27 -05002#include <experimental/filesystem>
Saqib Khan7254f0e2017-04-10 21:45:37 -05003#include <fstream>
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -05004#include <phosphor-logging/log.hpp>
Adriana Kobylakd6a549e2017-05-10 16:23:01 -05005#include <xyz/openbmc_project/Software/Version/server.hpp>
Saqib Khan167601b2017-06-18 23:33:46 -05006#include "version.hpp"
Adriana Kobylak2d8fa222017-03-15 12:34:32 -05007#include "config.h"
8#include "item_updater.hpp"
Saqib Khana8ade7e2017-04-12 10:27:56 -05009#include "activation.hpp"
Michael Tritz60bc20f2017-07-29 23:32:21 -050010#include "serialize.hpp"
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050011
12namespace openpower
13{
14namespace software
15{
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -050016namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050017{
18
Saqib Khana8ade7e2017-04-12 10:27:56 -050019// When you see server:: you know we're referencing our base class
20namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050021namespace fs = std::experimental::filesystem;
Saqib Khana8ade7e2017-04-12 10:27:56 -050022
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050023using namespace phosphor::logging;
24
Saqib Khana8ade7e2017-04-12 10:27:56 -050025constexpr auto squashFSImage = "pnor.xz.squashfs";
26
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050027void ItemUpdater::createActivation(sdbusplus::message::message& m)
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050028{
Patrick Williamse4290942017-06-16 05:43:08 -050029 using SVersion = server::Version;
30 using VersionPurpose = SVersion::VersionPurpose;
31 namespace msg = sdbusplus::message;
32 namespace variant_ns = msg::variant_ns;
33
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050034 sdbusplus::message::object_path objPath;
35 std::map<std::string,
Patrick Williamse4290942017-06-16 05:43:08 -050036 std::map<std::string, msg::variant<std::string>>> interfaces;
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050037 m.read(objPath, interfaces);
Patrick Williamse4290942017-06-16 05:43:08 -050038
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050039 std::string path(std::move(objPath));
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050040 std::string filePath;
Patrick Williamse4290942017-06-16 05:43:08 -050041 auto purpose = VersionPurpose::Unknown;
Saqib Khance148702017-06-11 12:01:58 -050042 std::string version;
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050043
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050044 for (const auto& intf : interfaces)
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050045 {
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050046 if (intf.first == VERSION_IFACE)
47 {
48 for (const auto& property : intf.second)
49 {
50 if (property.first == "Purpose")
51 {
52 // Only process the Host and System images
Patrick Williamse4290942017-06-16 05:43:08 -050053 auto value = SVersion::convertVersionPurposeFromString(
54 variant_ns::get<std::string>(property.second));
55
56 if (value == VersionPurpose::Host ||
57 value == VersionPurpose::System)
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050058 {
Saqib Khance148702017-06-11 12:01:58 -050059 purpose = value;
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050060 }
61 }
Saqib Khance148702017-06-11 12:01:58 -050062 else if (property.first == "Version")
63 {
Patrick Williamse4290942017-06-16 05:43:08 -050064 version = variant_ns::get<std::string>(property.second);
Saqib Khance148702017-06-11 12:01:58 -050065 }
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050066 }
67 }
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050068 else if (intf.first == FILEPATH_IFACE)
69 {
70 for (const auto& property : intf.second)
71 {
72 if (property.first == "Path")
73 {
Patrick Williamse4290942017-06-16 05:43:08 -050074 filePath = variant_ns::get<std::string>(property.second);
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050075 }
76 }
77 }
78 }
Patrick Williamse4290942017-06-16 05:43:08 -050079 if ((filePath.empty()) || (purpose == VersionPurpose::Unknown))
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050080 {
81 return;
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050082 }
83
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050084 // Version id is the last item in the path
85 auto pos = path.rfind("/");
86 if (pos == std::string::npos)
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050087 {
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050088 log<level::ERR>("No version id found in object path",
89 entry("OBJPATH=%s", path));
90 return;
91 }
92
93 auto versionId = path.substr(pos + 1);
94
95 if (activations.find(versionId) == activations.end())
96 {
97 // Determine the Activation state by processing the given image dir.
98 auto activationState = server::Activation::Activations::Invalid;
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050099 if (ItemUpdater::validateSquashFSImage(filePath) == 0)
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -0500100 {
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500101 activationState = server::Activation::Activations::Ready;
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -0500102 }
103
Adriana Kobylak5ba6b102017-05-19 09:41:27 -0500104 fs::path manifestPath(filePath);
105 manifestPath /= MANIFEST_FILE;
Saqib Khan167601b2017-06-18 23:33:46 -0500106 std::string extendedVersion = (Version::getValue(manifestPath.string(),
107 std::map<std::string, std::string>
108 {{"extended_version", ""}})).begin()->second;
Gunnar Mills3edb84b2017-08-18 15:13:15 -0500109
110 // Create an association to the host inventory item
111 AssociationList associations{(std::make_tuple(
112 ACTIVATION_FWD_ASSOCIATION,
113 ACTIVATION_REV_ASSOCIATION,
114 HOST_INVENTORY_PATH))};
115
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500116 activations.insert(std::make_pair(
117 versionId,
118 std::make_unique<Activation>(
119 bus,
120 path,
Saqib Khan81bac882017-06-08 12:17:01 -0500121 *this,
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500122 versionId,
123 extendedVersion,
Gunnar Mills3edb84b2017-08-18 15:13:15 -0500124 activationState,
125 associations)));
Saqib Khance148702017-06-11 12:01:58 -0500126 versions.insert(std::make_pair(
127 versionId,
128 std::make_unique<Version>(
129 bus,
130 path,
131 version,
132 purpose,
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500133 filePath,
134 *this)));
Saqib Khan00044f42017-07-10 17:24:43 -0500135 }
Saqib Khan3fb2d172017-08-07 12:14:03 -0500136 else
137 {
138 log<level::INFO>("Software Object with the same version already exists",
139 entry("VERSION_ID=%s", versionId));
140 }
Patrick Williams3accb322017-05-30 16:29:52 -0500141 return;
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500142}
143
Saqib Khan167601b2017-06-18 23:33:46 -0500144void ItemUpdater::processPNORImage()
Saqib Khan7254f0e2017-04-10 21:45:37 -0500145{
Saqib Khan4c5d7442017-07-18 00:43:52 -0500146 // Read pnor.toc from folders under /media/
147 // to get Active Software Versions.
148 for(const auto& iter : fs::directory_iterator(MEDIA_DIR))
149 {
150 auto activationState = server::Activation::Activations::Active;
151
152 static const auto PNOR_RO_PREFIX_LEN = strlen(PNOR_RO_PREFIX);
153
154 // Check if the PNOR_RO_PREFIX is the prefix of the iter.path
155 if (0 == iter.path().native().compare(0, PNOR_RO_PREFIX_LEN,
156 PNOR_RO_PREFIX))
157 {
158 auto pnorTOC = iter.path() / PNOR_TOC_FILE;
159 if (!fs::is_regular_file(pnorTOC))
160 {
161 log<level::ERR>("Failed to read pnorTOC\n",
162 entry("FileName=%s", pnorTOC.string()));
163 activationState = server::Activation::Activations::Invalid;
164 }
165 auto keyValues =
166 Version::getValue(pnorTOC,
167 {{ "version", "" },
168 { "extended_version", "" } });
169 auto& version = keyValues.at("version");
170 if (version.empty())
171 {
172 log<level::ERR>("Failed to read version from pnorTOC",
173 entry("FILENAME=%s", pnorTOC.string()));
174 activationState = server::Activation::Activations::Invalid;
175 }
176
177 auto& extendedVersion = keyValues.at("extended_version");
178 if (extendedVersion.empty())
179 {
180 log<level::ERR>("Failed to read extendedVersion from pnorTOC",
181 entry("FILENAME=%s", pnorTOC.string()));
182 activationState = server::Activation::Activations::Invalid;
183 }
184
185 // The versionId is extracted from the path
186 // for example /media/pnor-ro-2a1022fe
187 auto id = iter.path().native().substr(PNOR_RO_PREFIX_LEN);
188 auto purpose = server::Version::VersionPurpose::Host;
189 auto path = fs::path(SOFTWARE_OBJPATH) / id;
190
Gunnar Mills3edb84b2017-08-18 15:13:15 -0500191
192 // Create an association to the host inventory item
193 AssociationList associations{(std::make_tuple(
194 ACTIVATION_FWD_ASSOCIATION,
195 ACTIVATION_REV_ASSOCIATION,
196 HOST_INVENTORY_PATH))};
197
Saqib Khan4c5d7442017-07-18 00:43:52 -0500198 // Create Activation instance for this version.
199 activations.insert(std::make_pair(
200 id,
201 std::make_unique<Activation>(
202 bus,
203 path,
204 *this,
205 id,
206 extendedVersion,
Gunnar Mills3edb84b2017-08-18 15:13:15 -0500207 activationState,
208 associations)));
Saqib Khan4c5d7442017-07-18 00:43:52 -0500209
210 // If Active, create RedundancyPriority instance for this version.
211 if (activationState == server::Activation::Activations::Active)
212 {
Michael Tritz36417922017-08-04 14:00:29 -0500213 uint8_t priority = std::numeric_limits<uint8_t>::max();
214 if (!restoreFromFile(id, priority))
Saqib Khan4c5d7442017-07-18 00:43:52 -0500215 {
Michael Tritz36417922017-08-04 14:00:29 -0500216 log<level::ERR>("Unable to restore priority from file.",
217 entry("VERSIONID=%s", id));
Saqib Khan4c5d7442017-07-18 00:43:52 -0500218 }
Michael Tritz36417922017-08-04 14:00:29 -0500219 activations.find(id)->second->redundancyPriority =
220 std::make_unique<RedundancyPriority>(
221 bus,
222 path,
223 *(activations.find(id)->second),
224 priority);
Saqib Khan4c5d7442017-07-18 00:43:52 -0500225 }
226
227 // Create Version instance for this version.
228 versions.insert(std::make_pair(
229 id,
230 std::make_unique<Version>(
231 bus,
232 path,
233 version,
234 purpose,
235 "",
236 *this)));
237 }
238 }
Saqib Khan167601b2017-06-18 23:33:46 -0500239 return;
Saqib Khan7254f0e2017-04-10 21:45:37 -0500240}
241
Adriana Kobylak5ba6b102017-05-19 09:41:27 -0500242int ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khana8ade7e2017-04-12 10:27:56 -0500243{
Saqib Khan4c5d7442017-07-18 00:43:52 -0500244 auto file = fs::path(filePath) / squashFSImage;
245 if (fs::is_regular_file(file))
Saqib Khana8ade7e2017-04-12 10:27:56 -0500246 {
247 return 0;
248 }
249 else
250 {
251 log<level::ERR>("Failed to find the SquashFS image.");
252 return -1;
253 }
254}
255
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500256void ItemUpdater::removeReadOnlyPartition(std::string versionId)
Michael Tritzdd961b62017-05-17 14:07:03 -0500257{
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500258 auto serviceFile = "obmc-flash-bios-ubiumount-ro@" + versionId +
259 ".service";
260
261 // Remove the read-only partitions.
262 auto method = bus.new_method_call(
263 SYSTEMD_BUSNAME,
264 SYSTEMD_PATH,
265 SYSTEMD_INTERFACE,
266 "StartUnit");
267 method.append(serviceFile, "replace");
268 bus.call_noreply(method);
269}
270
271void ItemUpdater::removeReadWritePartition(std::string versionId)
272{
273 auto serviceFile = "obmc-flash-bios-ubiumount-rw@" + versionId +
Michael Tritzdd961b62017-05-17 14:07:03 -0500274 ".service";
275
276 // Remove the read-write partitions.
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500277 auto method = bus.new_method_call(
Michael Tritzdd961b62017-05-17 14:07:03 -0500278 SYSTEMD_BUSNAME,
279 SYSTEMD_PATH,
280 SYSTEMD_INTERFACE,
281 "StartUnit");
282 method.append(serviceFile, "replace");
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500283 bus.call_noreply(method);
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500284}
Michael Tritzdd961b62017-05-17 14:07:03 -0500285
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500286void ItemUpdater::removePreservedPartition()
287{
Michael Tritzdd961b62017-05-17 14:07:03 -0500288 // Remove the preserved partition.
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500289 auto method = bus.new_method_call(
Michael Tritzdd961b62017-05-17 14:07:03 -0500290 SYSTEMD_BUSNAME,
291 SYSTEMD_PATH,
292 SYSTEMD_INTERFACE,
293 "StartUnit");
294 method.append("obmc-flash-bios-ubiumount-prsv.service", "replace");
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500295 bus.call_noreply(method);
Michael Tritzdd961b62017-05-17 14:07:03 -0500296
297 return;
298}
299
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500300void ItemUpdater::reset()
301{
302 for(const auto& it : activations)
303 {
304 removeReadWritePartition(it.first);
Michael Tritz60bc20f2017-07-29 23:32:21 -0500305 removeFile(it.first);
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500306 }
307 removePreservedPartition();
308 return;
309}
310
Saqib Khanb8e7f312017-08-12 10:24:10 -0500311void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan81bac882017-06-08 12:17:01 -0500312{
313 //TODO openbmc/openbmc#1896 Improve the performance of this function
314 for (const auto& intf : activations)
315 {
316 if(intf.second->redundancyPriority)
317 {
Saqib Khanb8e7f312017-08-12 10:24:10 -0500318 if (intf.second->redundancyPriority.get()->priority() == value &&
319 intf.second->versionId != versionId)
Saqib Khan81bac882017-06-08 12:17:01 -0500320 {
321 intf.second->redundancyPriority.get()->priority(value+1);
322 }
323 }
324 }
325}
326
Saqib Khan2af5c492017-07-17 16:15:13 -0500327bool ItemUpdater::isLowestPriority(uint8_t value)
328{
329 for (const auto& intf : activations)
330 {
331 if(intf.second->redundancyPriority)
332 {
333 if (intf.second->redundancyPriority.get()->priority() < value)
334 {
335 return false;
336 }
337 }
338 }
339 return true;
340}
341
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500342void ItemUpdater::erase(std::string entryId)
343{
Saqib Khanef8cd9f2017-08-16 14:20:30 -0500344 // Remove priority persistence file
345 removeFile(entryId);
346
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500347 // Removing partitions
348 removeReadWritePartition(entryId);
349 removeReadOnlyPartition(entryId);
350
351 // Removing entry in versions map
352 auto it = versions.find(entryId);
353 if (it == versions.end())
354 {
355 log<level::ERR>(("Error: Failed to find version " + entryId + \
356 " in item updater versions map." \
357 " Unable to remove.").c_str());
358 return;
359 }
360 versions.erase(entryId);
361
362 // Removing entry in activations map
363 auto ita = activations.find(entryId);
364 if (ita == activations.end())
365 {
366 log<level::ERR>(("Error: Failed to find version " + entryId + \
367 " in item updater activations map." \
368 " Unable to remove.").c_str());
369 return;
370 }
371 activations.erase(entryId);
372}
373
Saqib Khan2cbfa032017-08-17 14:52:37 -0500374// TODO: openbmc/openbmc#1402 Monitor flash usage
375void ItemUpdater::freeSpace()
376{
377 std::size_t count = 0;
378 decltype(activations.begin()->second->redundancyPriority.get()->priority())
379 highestPriority = 0;
380 decltype(activations.begin()->second->versionId) highestPriorityVersion;
381 for (const auto& iter : activations)
382 {
383 if (iter.second.get()->activation() == server::Activation::Activations::Active)
384 {
385 count++;
386 if (iter.second->redundancyPriority.get()->priority() > highestPriority)
387 {
388 highestPriority = iter.second->redundancyPriority.get()->priority();
389 highestPriorityVersion = iter.second->versionId;
390 }
391 }
392 }
393 // Remove the pnor version with highest priority since the PNOR
394 // can't hold more than 2 versions.
395 if (count >= ACTIVE_PNOR_MAX_ALLOWED)
396 {
397 erase(highestPriorityVersion);
398 }
399}
400
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -0500401} // namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500402} // namespace software
403} // namespace openpower