blob: e1a8d3f6f18abc2231624affd3ec2942f093f451 [file] [log] [blame]
Gunnar Millsec1b41c2017-05-02 12:20:36 -05001#pragma once
2
Gunnar Millsec1b41c2017-05-02 12:20:36 -05003#include "activation.hpp"
Lei YU56aaf452018-06-21 16:09:44 +08004#include "item_updater_helper.hpp"
Adriana Kobylak30352a62024-04-09 09:25:36 -05005#include "msl_verify.hpp"
Jagpal Singh Gill6d131aa2024-04-07 23:56:48 -07006#include "update_manager.hpp"
Saqib Khan705f1bf2017-06-09 23:58:38 -05007#include "version.hpp"
Gunnar Millsb0ce9962018-09-07 13:39:10 -05008#include "xyz/openbmc_project/Collection/DeleteAll/server.hpp"
9
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -070010#include <sdbusplus/async.hpp>
Gunnar Millsb0ce9962018-09-07 13:39:10 -050011#include <sdbusplus/server.hpp>
John Wang85c356f2019-09-11 16:20:13 +080012#include <xyz/openbmc_project/Association/Definitions/server.hpp>
Michael Tritz37a59042017-07-12 13:44:53 -050013#include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
Michael Tritz0129d922017-08-10 19:33:46 -050014#include <xyz/openbmc_project/Control/FieldMode/server.hpp>
Adriana Kobylak30352a62024-04-09 09:25:36 -050015#include <xyz/openbmc_project/Software/MinimumVersion/server.hpp>
Gunnar Millsec1b41c2017-05-02 12:20:36 -050016
Adriana Kobylak58aa7502020-06-08 11:12:11 -050017#include <string>
Bright Cheng8e9ccfe2019-11-18 16:18:44 +080018#include <vector>
Adriana Kobylak58aa7502020-06-08 11:12:11 -050019
Gunnar Millsec1b41c2017-05-02 12:20:36 -050020namespace phosphor
21{
22namespace software
23{
24namespace updater
25{
26
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -070027using ActivationIntf =
28 sdbusplus::xyz::openbmc_project::Software::server::Activation;
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -050029using ItemUpdaterInherit = sdbusplus::server::object_t<
Patrick Williams1e9a5f12023-08-23 16:53:06 -050030 sdbusplus::server::xyz::openbmc_project::common::FactoryReset,
31 sdbusplus::server::xyz::openbmc_project::control::FieldMode,
32 sdbusplus::server::xyz::openbmc_project::association::Definitions,
33 sdbusplus::server::xyz::openbmc_project::collection::DeleteAll>;
Adriana Kobylak30352a62024-04-09 09:25:36 -050034using MinimumVersionInherit = sdbusplus::server::object_t<
35 sdbusplus::server::xyz::openbmc_project::software::MinimumVersion>;
Michael Tritz37a59042017-07-12 13:44:53 -050036
Patrick Williamse75d10f2017-05-30 16:56:32 -050037namespace MatchRules = sdbusplus::bus::match::rules;
Gunnar Mills48442912017-10-06 13:34:07 -050038using VersionClass = phosphor::software::manager::Version;
Gunnar Millsded875d2017-08-28 16:44:52 -050039using AssociationList =
Adriana Kobylak2285fe02018-02-27 15:36:59 -060040 std::vector<std::tuple<std::string, std::string, std::string>>;
Jagpal Singh Gill6d131aa2024-04-07 23:56:48 -070041using UpdateManager = phosphor::software::update::Manager;
Gunnar Millsded875d2017-08-28 16:44:52 -050042
Adriana Kobylak30352a62024-04-09 09:25:36 -050043/** @class MinimumVersion
44 * @brief OpenBMC MinimumVersion implementation.
45 * @details A concrete implementation for
46 * xyz.openbmc_project.Software.MinimumVersion DBus API.
47 */
48class MinimumVersion : public MinimumVersionInherit
49{
50 public:
51 /** @brief Constructs MinimumVersion
52 *
53 * @param[in] bus - The D-Bus bus object
54 * @param[in] path - The D-bus object path
55 */
56 MinimumVersion(sdbusplus::bus_t& bus, const std::string& path) :
57 MinimumVersionInherit(bus, path.c_str(), action::emit_interface_added)
58 {}
59};
60
Gunnar Millsec1b41c2017-05-02 12:20:36 -050061/** @class ItemUpdater
62 * @brief Manages the activation of the BMC version items.
63 */
Michael Tritz37a59042017-07-12 13:44:53 -050064class ItemUpdater : public ItemUpdaterInherit
Gunnar Millsec1b41c2017-05-02 12:20:36 -050065{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060066 public:
67 /*
68 * @brief Types of Activation status for image validation.
69 */
70 enum class ActivationStatus
71 {
72 ready,
73 invalid,
74 active
75 };
Saqib Khan35e83f32017-05-22 11:37:32 -050076
Adriana Kobylak2285fe02018-02-27 15:36:59 -060077 /** @brief Constructs ItemUpdater
78 *
79 * @param[in] bus - The D-Bus bus object
80 */
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -070081 ItemUpdater(sdbusplus::async::context& ctx, const std::string& path,
82 bool useUpdateDBusInterface = true) :
83 ItemUpdaterInherit(ctx.get_bus(), path.c_str(),
Patrick Williams35aa9a82022-04-05 15:55:30 -050084 ItemUpdaterInherit::action::defer_emit),
Jagpal Singh Gill6d131aa2024-04-07 23:56:48 -070085 useUpdateDBusInterface(useUpdateDBusInterface), ctx(ctx),
86 bus(ctx.get_bus()), helper(bus)
Adriana Kobylak2285fe02018-02-27 15:36:59 -060087 {
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -070088 if (!useUpdateDBusInterface)
89 {
90 versionMatch = std::make_unique<sdbusplus::bus::match_t>(
91 bus,
92 MatchRules::interfacesAdded() +
93 MatchRules::path("/xyz/openbmc_project/software"),
94 std::bind(std::mem_fn(&ItemUpdater::createActivation), this,
95 std::placeholders::_1));
96 }
Lei YU531fbc22021-12-10 20:03:18 +080097 getRunningSlot();
Adriana Kobylak2285fe02018-02-27 15:36:59 -060098 setBMCInventoryPath();
99 processBMCImage();
100 restoreFieldModeStatus();
Lei YU6e9fb1d2021-02-19 18:01:40 +0800101#ifdef HOST_BIOS_UPGRADE
102 createBIOSObject();
103#endif
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600104 emit_object_added();
105 };
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500106
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500107 /** @brief Save priority value to persistent storage (flash and optionally
108 * a U-Boot environment variable)
109 *
110 * @param[in] versionId - The Id of the version
111 * @param[in] value - The priority value
112 * @return None
113 */
114 void savePriority(const std::string& versionId, uint8_t value);
115
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600116 /** @brief Sets the given priority free by incrementing
117 * any existing priority with the same value by 1
118 *
119 * @param[in] value - The priority that needs to be set free.
120 * @param[in] versionId - The Id of the version for which we
121 * are trying to free up the priority.
122 * @return None
123 */
124 void freePriority(uint8_t value, const std::string& versionId);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500125
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600126 /**
127 * @brief Create and populate the active BMC Version.
128 */
129 void processBMCImage();
Saqib Khanba239882017-05-26 08:41:54 -0500130
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600131 /**
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -0700132 * @brief Verifies the image at filepath and creates the version and
133 * activation object. In case activation object already exists for the
134 * specified id, update the activation status based on image verification.
135 * @param[in] id - The unique identifier for the update.
136 * @param[in] path - The object path for the relevant objects.
137 * @param[in] version - The version of the image.
138 * @param[in] purpose - The purpose of the image.
139 * @param[in] extendedVersion The extended version of the image.
140 * @param[in] filePath - The file path where the image is located.
141 * @param[in] compatibleNames - The compatible name for the image.
142 * @param[out] Activations - Whether the image is ready to activate or not.
143 */
144 ActivationIntf::Activations verifyAndCreateObjects(
145 std::string& id, std::string& path, std::string& version,
146 VersionClass::VersionPurpose purpose, std::string& extendedVersion,
147 std ::string& filePath, std::vector<std::string>& compatibleNames);
148
149 /**
150 * @brief Creates the activation object
151 * @param[in] id - The unique identifier for the update.
152 * @param[in] path - The object path for the activation object.
153 * @param[in] applyTime - The apply time for the image
154 */
155 void createActivationWithApplyTime(
156 std::string& id, std::string& path,
157 ApplyTimeIntf::RequestedApplyTimes applyTime);
158
159 /**
160 * @brief Request the activation for the specified update.
161 * @param[in] id - The unique identifier for the update.
162 * @param[out] bool - status for the action.
163 */
164 bool requestActivation(std::string& id);
165
166 /**
167 * @brief Change the activation status for the specified update.
168 * @param[in] id - The unique identifier for the update.
169 * @param[in] status - The activation status to set.
170 * @param[out] bool - status for the action.
171 */
172 bool updateActivationStatus(std::string& id,
173 ActivationIntf::Activations status);
174
175 /**
Jagpal Singh Gill6d131aa2024-04-07 23:56:48 -0700176 * @brief Create the Update object
177 * @param[in] id - The unique identifier for the update.
178 * @param[in] path - The object path for the update object.
179 */
180 void createUpdateObject(const std::string& id, const std::string& path);
181
182 /**
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600183 * @brief Erase specified entry D-Bus object
184 * if Action property is not set to Active
185 *
186 * @param[in] entryId - unique identifier of the entry
187 */
188 void erase(std::string entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500189
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600190 /**
191 * @brief Deletes all versions except for the current one
192 */
Pavithra Barithaya36cc1c82024-06-22 00:41:28 -0500193 void deleteAll() override;
Gunnar Millsded875d2017-08-28 16:44:52 -0500194
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600195 /** @brief Creates an active association to the
196 * newly active software image
197 *
198 * @param[in] path - The path to create the association to.
199 */
200 void createActiveAssociation(const std::string& path);
Gunnar Millsded875d2017-08-28 16:44:52 -0500201
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600202 /** @brief Removes the associations from the provided software image path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600203 *
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600204 * @param[in] path - The path to remove the associations from.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600205 */
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600206 void removeAssociations(const std::string& path);
Gunnar Millsded875d2017-08-28 16:44:52 -0500207
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600208 /** @brief Determine if the given priority is the lowest
209 *
210 * @param[in] value - The priority that needs to be checked.
211 *
212 * @return boolean corresponding to whether the given
213 * priority is lowest.
214 */
215 bool isLowestPriority(uint8_t value);
Saqib Khanb9da6632017-09-13 09:48:37 -0500216
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600217 /**
218 * @brief Updates the U-Boot variables to point to the requested
219 * versionId, so that the systems boots from this version on
220 * the next reboot.
221 *
222 * @param[in] versionId - The version to point the system to boot from.
223 */
224 void updateUbootEnvVars(const std::string& versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500225
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600226 /**
227 * @brief Updates the uboot variables to point to BMC version with lowest
228 * priority, so that the system boots from this version on the
229 * next boot.
230 */
231 void resetUbootEnvVars();
Saqib Khan49446ae2017-10-02 10:54:20 -0500232
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600233 /** @brief Brings the total number of active BMC versions to
234 * ACTIVE_BMC_MAX_ALLOWED -1. This function is intended to be
235 * run before activating a new BMC version. If this function
236 * needs to delete any BMC version(s) it will delete the
237 * version(s) with the highest priority, skipping the
238 * functional BMC version.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500239 *
240 * @param[in] caller - The Activation object that called this function.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600241 */
Lei YU0cd6d842021-12-27 11:56:02 +0800242 void freeSpace(const Activation& caller);
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600243
AppaRao Puli1bb6dcb2020-03-27 20:51:57 +0530244 /** @brief Creates a updateable association to the
245 * "running" BMC software image
246 *
247 * @param[in] path - The path to create the association.
248 */
249 void createUpdateableAssociation(const std::string& path);
250
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600251 /** @brief Persistent map of Version D-Bus objects and their
252 * version id */
253 std::map<std::string, std::unique_ptr<VersionClass>> versions;
254
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800255 /** @brief Vector of needed BMC images in the tarball*/
256 std::vector<std::string> imageUpdateList;
257
Manojkiran Eda19dd56b2024-06-17 14:29:52 +0530258 /** @brief The slot of running BMC image */
Lei YU531fbc22021-12-10 20:03:18 +0800259 uint32_t runningImageSlot = 0;
260
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -0700261 /** @brief Flag to indicate if the update interface is used or not */
262 bool useUpdateDBusInterface;
263
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600264 private:
265 /** @brief Callback function for Software.Version match.
266 * @details Creates an Activation D-Bus object.
267 *
268 * @param[in] msg - Data associated with subscribed signal
269 */
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -0500270 void createActivation(sdbusplus::message_t& msg);
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500271
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600272 /**
273 * @brief Validates the presence of SquashFS image in the image dir.
274 *
275 * @param[in] filePath - The path to the image dir.
276 * @param[out] result - ActivationStatus Enum.
277 * ready if validation was successful.
278 * invalid if validation fail.
279 * active if image is the current version.
280 *
281 */
282 ActivationStatus validateSquashFSImage(const std::string& filePath);
Saqib Khan35e83f32017-05-22 11:37:32 -0500283
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600284 /** @brief BMC factory reset - marks the read-write partition for
285 * recreation upon reboot. */
286 void reset() override;
Michael Tritz37a59042017-07-12 13:44:53 -0500287
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600288 /**
289 * @brief Enables field mode, if value=true.
290 *
291 * @param[in] value - If true, enables field mode.
292 * @param[out] result - Returns the current state of field mode.
293 *
294 */
295 bool fieldModeEnabled(bool value) override;
Michael Tritz0129d922017-08-10 19:33:46 -0500296
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600297 /** @brief Sets the BMC inventory item path under
298 * /xyz/openbmc_project/inventory/system/chassis/. */
299 void setBMCInventoryPath();
Gunnar Millsb60add12017-08-24 16:41:42 -0500300
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600301 /** @brief The path to the BMC inventory item. */
302 std::string bmcInventoryPath;
Gunnar Millsb60add12017-08-24 16:41:42 -0500303
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600304 /** @brief Restores field mode status on reboot. */
305 void restoreFieldModeStatus();
Michael Tritz0129d922017-08-10 19:33:46 -0500306
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600307 /** @brief Creates a functional association to the
308 * "running" BMC software image
309 *
310 * @param[in] path - The path to create the association to.
311 */
312 void createFunctionalAssociation(const std::string& path);
Gunnar Mills88e8a322017-09-13 11:09:28 -0500313
Jagpal Singh Gill6d131aa2024-04-07 23:56:48 -0700314 /** @brief D-Bus context */
315 sdbusplus::async::context& ctx;
316
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600317 /** @brief Persistent sdbusplus D-Bus bus connection. */
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -0500318 sdbusplus::bus_t& bus;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500319
Lei YU56aaf452018-06-21 16:09:44 +0800320 /** @brief The helper of image updater. */
321 Helper helper;
322
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600323 /** @brief Persistent map of Activation D-Bus objects and their
324 * version id */
325 std::map<std::string, std::unique_ptr<Activation>> activations;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500326
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600327 /** @brief sdbusplus signal match for Software.Version */
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -0700328 std::unique_ptr<sdbusplus::bus::match_t> versionMatch;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500329
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600330 /** @brief This entry's associations */
Patrick Williamsfc33ba82024-08-16 15:19:54 -0400331 AssociationList assocs;
Gunnar Millsded875d2017-08-28 16:44:52 -0500332
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600333 /** @brief Clears read only partition for
334 * given Activation D-Bus object.
335 *
336 * @param[in] versionId - The version id.
337 */
Pavithra Barithaya6d178522024-06-24 04:17:29 -0500338 void removeReadOnlyPartition(const std::string& versionId);
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600339
340 /** @brief Copies U-Boot from the currently booted BMC chip to the
341 * alternate chip.
342 */
343 void mirrorUbootToAlt();
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800344
345 /** @brief Check the required image files
346 *
347 * @param[in] filePath - BMC tarball file path
348 * @param[in] imageList - Image filenames included in the BMC tarball
349 * @param[out] result - Boolean
350 * true if all image files are found in BMC tarball
351 * false if one of image files is missing
352 */
Pavithra Barithayac5f6e7e2024-06-24 09:50:21 -0500353 static bool checkImage(const std::string& filePath,
354 const std::vector<std::string>& imageList);
Lei YU6e9fb1d2021-02-19 18:01:40 +0800355
Adriana Kobylak30352a62024-04-09 09:25:36 -0500356 /** @brief Persistent MinimumVersion D-Bus object */
357 std::unique_ptr<MinimumVersion> minimumVersionObject;
358
Jagpal Singh Gill6d131aa2024-04-07 23:56:48 -0700359 /** @brief Persistent map of Update D-Bus objects and their SwIds */
360 std::map<std::string, std::unique_ptr<UpdateManager>> updateManagers;
361
Lei YU6e9fb1d2021-02-19 18:01:40 +0800362#ifdef HOST_BIOS_UPGRADE
363 /** @brief Create the BIOS object without knowing the version.
364 *
365 * The object is created only to provide the DBus access so that an
366 * external service could set the correct BIOS version.
367 * On BIOS code update, the version is updated accordingly.
368 */
369 void createBIOSObject();
370
371 /** @brief Persistent Activation D-Bus object for BIOS */
372 std::unique_ptr<Activation> biosActivation;
373
Lei YU16aa28a2021-05-07 10:17:30 +0800374 public:
Lei YU6e9fb1d2021-02-19 18:01:40 +0800375 /** @brief Persistent Version D-Bus object for BIOS */
376 std::unique_ptr<VersionClass> biosVersion;
377#endif
Lei YU531fbc22021-12-10 20:03:18 +0800378
379 /** @brief Get the slot number of running image */
380 void getRunningSlot();
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500381};
382
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500383} // namespace updater
384} // namespace software
385} // namespace phosphor