blob: fa3938e089f1bd1b69c3fa69dcc3d4996fdc2e47 [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"
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050010
11namespace openpower
12{
13namespace software
14{
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -050015namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050016{
17
Saqib Khana8ade7e2017-04-12 10:27:56 -050018// When you see server:: you know we're referencing our base class
19namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050020namespace fs = std::experimental::filesystem;
Saqib Khana8ade7e2017-04-12 10:27:56 -050021
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050022using namespace phosphor::logging;
23
Saqib Khana8ade7e2017-04-12 10:27:56 -050024constexpr auto squashFSImage = "pnor.xz.squashfs";
25
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050026void ItemUpdater::createActivation(sdbusplus::message::message& m)
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050027{
Patrick Williamse4290942017-06-16 05:43:08 -050028 using SVersion = server::Version;
29 using VersionPurpose = SVersion::VersionPurpose;
30 namespace msg = sdbusplus::message;
31 namespace variant_ns = msg::variant_ns;
32
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050033 sdbusplus::message::object_path objPath;
34 std::map<std::string,
Patrick Williamse4290942017-06-16 05:43:08 -050035 std::map<std::string, msg::variant<std::string>>> interfaces;
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050036 m.read(objPath, interfaces);
Patrick Williamse4290942017-06-16 05:43:08 -050037
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050038 std::string path(std::move(objPath));
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050039 std::string filePath;
Patrick Williamse4290942017-06-16 05:43:08 -050040 auto purpose = VersionPurpose::Unknown;
Saqib Khance148702017-06-11 12:01:58 -050041 std::string version;
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050042
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050043 for (const auto& intf : interfaces)
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050044 {
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050045 if (intf.first == VERSION_IFACE)
46 {
47 for (const auto& property : intf.second)
48 {
49 if (property.first == "Purpose")
50 {
51 // Only process the Host and System images
Patrick Williamse4290942017-06-16 05:43:08 -050052 auto value = SVersion::convertVersionPurposeFromString(
53 variant_ns::get<std::string>(property.second));
54
55 if (value == VersionPurpose::Host ||
56 value == VersionPurpose::System)
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050057 {
Saqib Khance148702017-06-11 12:01:58 -050058 purpose = value;
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050059 }
60 }
Saqib Khance148702017-06-11 12:01:58 -050061 else if (property.first == "Version")
62 {
Patrick Williamse4290942017-06-16 05:43:08 -050063 version = variant_ns::get<std::string>(property.second);
Saqib Khance148702017-06-11 12:01:58 -050064 }
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050065 }
66 }
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050067 else if (intf.first == FILEPATH_IFACE)
68 {
69 for (const auto& property : intf.second)
70 {
71 if (property.first == "Path")
72 {
Patrick Williamse4290942017-06-16 05:43:08 -050073 filePath = variant_ns::get<std::string>(property.second);
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050074 }
75 }
76 }
77 }
Patrick Williamse4290942017-06-16 05:43:08 -050078 if ((filePath.empty()) || (purpose == VersionPurpose::Unknown))
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050079 {
80 return;
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050081 }
82
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050083 // Version id is the last item in the path
84 auto pos = path.rfind("/");
85 if (pos == std::string::npos)
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050086 {
Adriana Kobylakd6a549e2017-05-10 16:23:01 -050087 log<level::ERR>("No version id found in object path",
88 entry("OBJPATH=%s", path));
89 return;
90 }
91
92 auto versionId = path.substr(pos + 1);
93
94 if (activations.find(versionId) == activations.end())
95 {
96 // Determine the Activation state by processing the given image dir.
97 auto activationState = server::Activation::Activations::Invalid;
Adriana Kobylak5ba6b102017-05-19 09:41:27 -050098 if (ItemUpdater::validateSquashFSImage(filePath) == 0)
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050099 {
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500100 activationState = server::Activation::Activations::Ready;
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -0500101 }
102
Adriana Kobylak5ba6b102017-05-19 09:41:27 -0500103 fs::path manifestPath(filePath);
104 manifestPath /= MANIFEST_FILE;
Saqib Khan167601b2017-06-18 23:33:46 -0500105 std::string extendedVersion = (Version::getValue(manifestPath.string(),
106 std::map<std::string, std::string>
107 {{"extended_version", ""}})).begin()->second;
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500108 activations.insert(std::make_pair(
109 versionId,
110 std::make_unique<Activation>(
111 bus,
112 path,
Saqib Khan81bac882017-06-08 12:17:01 -0500113 *this,
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500114 versionId,
115 extendedVersion,
116 activationState)));
Saqib Khance148702017-06-11 12:01:58 -0500117 versions.insert(std::make_pair(
118 versionId,
119 std::make_unique<Version>(
120 bus,
121 path,
122 version,
123 purpose,
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500124 filePath,
125 *this)));
Saqib Khan00044f42017-07-10 17:24:43 -0500126 }
Patrick Williams3accb322017-05-30 16:29:52 -0500127 return;
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500128}
129
Saqib Khan167601b2017-06-18 23:33:46 -0500130void ItemUpdater::processPNORImage()
Saqib Khan7254f0e2017-04-10 21:45:37 -0500131{
Saqib Khan4c5d7442017-07-18 00:43:52 -0500132 // Get the current PNOR version
133 auto pnorTOC = fs::path(PNOR_RO_ACTIVE_PATH) / PNOR_TOC_FILE;
134 if (!fs::is_regular_file(pnorTOC))
Saqib Khan7254f0e2017-04-10 21:45:37 -0500135 {
Saqib Khan167601b2017-06-18 23:33:46 -0500136 log<level::INFO>("Error PNOR current version is empty");
137 return;
Saqib Khan7254f0e2017-04-10 21:45:37 -0500138 }
Saqib Khan4c5d7442017-07-18 00:43:52 -0500139 std::string currentVersion =
140 Version::getValue(pnorTOC, {{ "version", "" }}).begin()->second;
141
142 // Read pnor.toc from folders under /media/
143 // to get Active Software Versions.
144 for(const auto& iter : fs::directory_iterator(MEDIA_DIR))
145 {
146 auto activationState = server::Activation::Activations::Active;
147
148 static const auto PNOR_RO_PREFIX_LEN = strlen(PNOR_RO_PREFIX);
149
150 // Check if the PNOR_RO_PREFIX is the prefix of the iter.path
151 if (0 == iter.path().native().compare(0, PNOR_RO_PREFIX_LEN,
152 PNOR_RO_PREFIX))
153 {
154 auto pnorTOC = iter.path() / PNOR_TOC_FILE;
155 if (!fs::is_regular_file(pnorTOC))
156 {
157 log<level::ERR>("Failed to read pnorTOC\n",
158 entry("FileName=%s", pnorTOC.string()));
159 activationState = server::Activation::Activations::Invalid;
160 }
161 auto keyValues =
162 Version::getValue(pnorTOC,
163 {{ "version", "" },
164 { "extended_version", "" } });
165 auto& version = keyValues.at("version");
166 if (version.empty())
167 {
168 log<level::ERR>("Failed to read version from pnorTOC",
169 entry("FILENAME=%s", pnorTOC.string()));
170 activationState = server::Activation::Activations::Invalid;
171 }
172
173 auto& extendedVersion = keyValues.at("extended_version");
174 if (extendedVersion.empty())
175 {
176 log<level::ERR>("Failed to read extendedVersion from pnorTOC",
177 entry("FILENAME=%s", pnorTOC.string()));
178 activationState = server::Activation::Activations::Invalid;
179 }
180
181 // The versionId is extracted from the path
182 // for example /media/pnor-ro-2a1022fe
183 auto id = iter.path().native().substr(PNOR_RO_PREFIX_LEN);
184 auto purpose = server::Version::VersionPurpose::Host;
185 auto path = fs::path(SOFTWARE_OBJPATH) / id;
186
187 // Create Activation instance for this version.
188 activations.insert(std::make_pair(
189 id,
190 std::make_unique<Activation>(
191 bus,
192 path,
193 *this,
194 id,
195 extendedVersion,
196 activationState)));
197
198 // If Active, create RedundancyPriority instance for this version.
199 if (activationState == server::Activation::Activations::Active)
200 {
201 // Current PNOR needs the lowest Priority, so setting to 0.
202 // TODO openbmc/openbmc#2040 Need to store Priority in the
203 // RW partition to be able to restore the priorities that
204 // were set before the BMC reboot.
205 auto priority = 1;
206 if (currentVersion == version)
207 {
208 priority = 0;
209 }
210 activations.find(id)->second->redundancyPriority =
211 std::make_unique<RedundancyPriority>(
Saqib Khan167601b2017-06-18 23:33:46 -0500212 bus,
213 path,
Saqib Khan4c5d7442017-07-18 00:43:52 -0500214 *(activations.find(id)->second),
215 priority);
216 }
217
218 // Create Version instance for this version.
219 versions.insert(std::make_pair(
220 id,
221 std::make_unique<Version>(
222 bus,
223 path,
224 version,
225 purpose,
226 "",
227 *this)));
228 }
229 }
Saqib Khan167601b2017-06-18 23:33:46 -0500230 return;
Saqib Khan7254f0e2017-04-10 21:45:37 -0500231}
232
Adriana Kobylak5ba6b102017-05-19 09:41:27 -0500233int ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khana8ade7e2017-04-12 10:27:56 -0500234{
Saqib Khan4c5d7442017-07-18 00:43:52 -0500235 auto file = fs::path(filePath) / squashFSImage;
236 if (fs::is_regular_file(file))
Saqib Khana8ade7e2017-04-12 10:27:56 -0500237 {
238 return 0;
239 }
240 else
241 {
242 log<level::ERR>("Failed to find the SquashFS image.");
243 return -1;
244 }
245}
246
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500247void ItemUpdater::removeReadOnlyPartition(std::string versionId)
Michael Tritzdd961b62017-05-17 14:07:03 -0500248{
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500249 auto serviceFile = "obmc-flash-bios-ubiumount-ro@" + versionId +
250 ".service";
251
252 // Remove the read-only partitions.
253 auto method = bus.new_method_call(
254 SYSTEMD_BUSNAME,
255 SYSTEMD_PATH,
256 SYSTEMD_INTERFACE,
257 "StartUnit");
258 method.append(serviceFile, "replace");
259 bus.call_noreply(method);
260}
261
262void ItemUpdater::removeReadWritePartition(std::string versionId)
263{
264 auto serviceFile = "obmc-flash-bios-ubiumount-rw@" + versionId +
Michael Tritzdd961b62017-05-17 14:07:03 -0500265 ".service";
266
267 // Remove the read-write partitions.
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500268 auto method = bus.new_method_call(
Michael Tritzdd961b62017-05-17 14:07:03 -0500269 SYSTEMD_BUSNAME,
270 SYSTEMD_PATH,
271 SYSTEMD_INTERFACE,
272 "StartUnit");
273 method.append(serviceFile, "replace");
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500274 bus.call_noreply(method);
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500275}
Michael Tritzdd961b62017-05-17 14:07:03 -0500276
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500277void ItemUpdater::removePreservedPartition()
278{
Michael Tritzdd961b62017-05-17 14:07:03 -0500279 // Remove the preserved partition.
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500280 auto method = bus.new_method_call(
Michael Tritzdd961b62017-05-17 14:07:03 -0500281 SYSTEMD_BUSNAME,
282 SYSTEMD_PATH,
283 SYSTEMD_INTERFACE,
284 "StartUnit");
285 method.append("obmc-flash-bios-ubiumount-prsv.service", "replace");
Adriana Kobylakd6a549e2017-05-10 16:23:01 -0500286 bus.call_noreply(method);
Michael Tritzdd961b62017-05-17 14:07:03 -0500287
288 return;
289}
290
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500291void ItemUpdater::reset()
292{
293 for(const auto& it : activations)
294 {
295 removeReadWritePartition(it.first);
296 }
297 removePreservedPartition();
298 return;
299}
300
Saqib Khan81bac882017-06-08 12:17:01 -0500301void ItemUpdater::freePriority(uint8_t value)
302{
303 //TODO openbmc/openbmc#1896 Improve the performance of this function
304 for (const auto& intf : activations)
305 {
306 if(intf.second->redundancyPriority)
307 {
308 if (intf.second->redundancyPriority.get()->priority() == value)
309 {
310 intf.second->redundancyPriority.get()->priority(value+1);
311 }
312 }
313 }
314}
315
Saqib Khan2af5c492017-07-17 16:15:13 -0500316bool ItemUpdater::isLowestPriority(uint8_t value)
317{
318 for (const auto& intf : activations)
319 {
320 if(intf.second->redundancyPriority)
321 {
322 if (intf.second->redundancyPriority.get()->priority() < value)
323 {
324 return false;
325 }
326 }
327 }
328 return true;
329}
330
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -0500331void ItemUpdater::erase(std::string entryId)
332{
333 // Removing partitions
334 removeReadWritePartition(entryId);
335 removeReadOnlyPartition(entryId);
336
337 // Removing entry in versions map
338 auto it = versions.find(entryId);
339 if (it == versions.end())
340 {
341 log<level::ERR>(("Error: Failed to find version " + entryId + \
342 " in item updater versions map." \
343 " Unable to remove.").c_str());
344 return;
345 }
346 versions.erase(entryId);
347
348 // Removing entry in activations map
349 auto ita = activations.find(entryId);
350 if (ita == activations.end())
351 {
352 log<level::ERR>(("Error: Failed to find version " + entryId + \
353 " in item updater activations map." \
354 " Unable to remove.").c_str());
355 return;
356 }
357 activations.erase(entryId);
358}
359
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -0500360} // namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500361} // namespace software
362} // namespace openpower