blob: 1f3c0f1273e958f599b5091980564f84e7db303e [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"
Saqib Khan705f1bf2017-06-09 23:58:38 -05006#include "version.hpp"
Gunnar Millsb0ce9962018-09-07 13:39:10 -05007#include "xyz/openbmc_project/Collection/DeleteAll/server.hpp"
8
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -07009#include <sdbusplus/async.hpp>
Gunnar Millsb0ce9962018-09-07 13:39:10 -050010#include <sdbusplus/server.hpp>
John Wang85c356f2019-09-11 16:20:13 +080011#include <xyz/openbmc_project/Association/Definitions/server.hpp>
Michael Tritz37a59042017-07-12 13:44:53 -050012#include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
Michael Tritz0129d922017-08-10 19:33:46 -050013#include <xyz/openbmc_project/Control/FieldMode/server.hpp>
Adriana Kobylak30352a62024-04-09 09:25:36 -050014#include <xyz/openbmc_project/Software/MinimumVersion/server.hpp>
Gunnar Millsec1b41c2017-05-02 12:20:36 -050015
Adriana Kobylak58aa7502020-06-08 11:12:11 -050016#include <string>
Bright Cheng8e9ccfe2019-11-18 16:18:44 +080017#include <vector>
Adriana Kobylak58aa7502020-06-08 11:12:11 -050018
Gunnar Millsec1b41c2017-05-02 12:20:36 -050019namespace phosphor
20{
21namespace software
22{
23namespace updater
24{
25
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -070026using ActivationIntf =
27 sdbusplus::xyz::openbmc_project::Software::server::Activation;
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -050028using ItemUpdaterInherit = sdbusplus::server::object_t<
Patrick Williams1e9a5f12023-08-23 16:53:06 -050029 sdbusplus::server::xyz::openbmc_project::common::FactoryReset,
30 sdbusplus::server::xyz::openbmc_project::control::FieldMode,
31 sdbusplus::server::xyz::openbmc_project::association::Definitions,
32 sdbusplus::server::xyz::openbmc_project::collection::DeleteAll>;
Adriana Kobylak30352a62024-04-09 09:25:36 -050033using MinimumVersionInherit = sdbusplus::server::object_t<
34 sdbusplus::server::xyz::openbmc_project::software::MinimumVersion>;
Michael Tritz37a59042017-07-12 13:44:53 -050035
Patrick Williamse75d10f2017-05-30 16:56:32 -050036namespace MatchRules = sdbusplus::bus::match::rules;
Gunnar Mills48442912017-10-06 13:34:07 -050037using VersionClass = phosphor::software::manager::Version;
Gunnar Millsded875d2017-08-28 16:44:52 -050038using AssociationList =
Adriana Kobylak2285fe02018-02-27 15:36:59 -060039 std::vector<std::tuple<std::string, std::string, std::string>>;
Gunnar Millsded875d2017-08-28 16:44:52 -050040
Adriana Kobylak30352a62024-04-09 09:25:36 -050041/** @class MinimumVersion
42 * @brief OpenBMC MinimumVersion implementation.
43 * @details A concrete implementation for
44 * xyz.openbmc_project.Software.MinimumVersion DBus API.
45 */
46class MinimumVersion : public MinimumVersionInherit
47{
48 public:
49 /** @brief Constructs MinimumVersion
50 *
51 * @param[in] bus - The D-Bus bus object
52 * @param[in] path - The D-bus object path
53 */
54 MinimumVersion(sdbusplus::bus_t& bus, const std::string& path) :
55 MinimumVersionInherit(bus, path.c_str(), action::emit_interface_added)
56 {}
57};
58
Gunnar Millsec1b41c2017-05-02 12:20:36 -050059/** @class ItemUpdater
60 * @brief Manages the activation of the BMC version items.
61 */
Michael Tritz37a59042017-07-12 13:44:53 -050062class ItemUpdater : public ItemUpdaterInherit
Gunnar Millsec1b41c2017-05-02 12:20:36 -050063{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060064 public:
65 /*
66 * @brief Types of Activation status for image validation.
67 */
68 enum class ActivationStatus
69 {
70 ready,
71 invalid,
72 active
73 };
Saqib Khan35e83f32017-05-22 11:37:32 -050074
Adriana Kobylak2285fe02018-02-27 15:36:59 -060075 /** @brief Constructs ItemUpdater
76 *
77 * @param[in] bus - The D-Bus bus object
78 */
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -070079 ItemUpdater(sdbusplus::async::context& ctx, const std::string& path,
80 bool useUpdateDBusInterface = true) :
81 ItemUpdaterInherit(ctx.get_bus(), path.c_str(),
Patrick Williams35aa9a82022-04-05 15:55:30 -050082 ItemUpdaterInherit::action::defer_emit),
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -070083 useUpdateDBusInterface(useUpdateDBusInterface), bus(ctx.get_bus()),
84 helper(bus)
Adriana Kobylak2285fe02018-02-27 15:36:59 -060085 {
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -070086 if (!useUpdateDBusInterface)
87 {
88 versionMatch = std::make_unique<sdbusplus::bus::match_t>(
89 bus,
90 MatchRules::interfacesAdded() +
91 MatchRules::path("/xyz/openbmc_project/software"),
92 std::bind(std::mem_fn(&ItemUpdater::createActivation), this,
93 std::placeholders::_1));
94 }
Lei YU531fbc22021-12-10 20:03:18 +080095 getRunningSlot();
Adriana Kobylak2285fe02018-02-27 15:36:59 -060096 setBMCInventoryPath();
97 processBMCImage();
98 restoreFieldModeStatus();
Lei YU6e9fb1d2021-02-19 18:01:40 +080099#ifdef HOST_BIOS_UPGRADE
100 createBIOSObject();
101#endif
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600102 emit_object_added();
103 };
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500104
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500105 /** @brief Save priority value to persistent storage (flash and optionally
106 * a U-Boot environment variable)
107 *
108 * @param[in] versionId - The Id of the version
109 * @param[in] value - The priority value
110 * @return None
111 */
112 void savePriority(const std::string& versionId, uint8_t value);
113
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600114 /** @brief Sets the given priority free by incrementing
115 * any existing priority with the same value by 1
116 *
117 * @param[in] value - The priority that needs to be set free.
118 * @param[in] versionId - The Id of the version for which we
119 * are trying to free up the priority.
120 * @return None
121 */
122 void freePriority(uint8_t value, const std::string& versionId);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500123
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600124 /**
125 * @brief Create and populate the active BMC Version.
126 */
127 void processBMCImage();
Saqib Khanba239882017-05-26 08:41:54 -0500128
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600129 /**
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -0700130 * @brief Verifies the image at filepath and creates the version and
131 * activation object. In case activation object already exists for the
132 * specified id, update the activation status based on image verification.
133 * @param[in] id - The unique identifier for the update.
134 * @param[in] path - The object path for the relevant objects.
135 * @param[in] version - The version of the image.
136 * @param[in] purpose - The purpose of the image.
137 * @param[in] extendedVersion The extended version of the image.
138 * @param[in] filePath - The file path where the image is located.
139 * @param[in] compatibleNames - The compatible name for the image.
140 * @param[out] Activations - Whether the image is ready to activate or not.
141 */
142 ActivationIntf::Activations verifyAndCreateObjects(
143 std::string& id, std::string& path, std::string& version,
144 VersionClass::VersionPurpose purpose, std::string& extendedVersion,
145 std ::string& filePath, std::vector<std::string>& compatibleNames);
146
147 /**
148 * @brief Creates the activation object
149 * @param[in] id - The unique identifier for the update.
150 * @param[in] path - The object path for the activation object.
151 * @param[in] applyTime - The apply time for the image
152 */
153 void createActivationWithApplyTime(
154 std::string& id, std::string& path,
155 ApplyTimeIntf::RequestedApplyTimes applyTime);
156
157 /**
158 * @brief Request the activation for the specified update.
159 * @param[in] id - The unique identifier for the update.
160 * @param[out] bool - status for the action.
161 */
162 bool requestActivation(std::string& id);
163
164 /**
165 * @brief Change the activation status for the specified update.
166 * @param[in] id - The unique identifier for the update.
167 * @param[in] status - The activation status to set.
168 * @param[out] bool - status for the action.
169 */
170 bool updateActivationStatus(std::string& id,
171 ActivationIntf::Activations status);
172
173 /**
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600174 * @brief Erase specified entry D-Bus object
175 * if Action property is not set to Active
176 *
177 * @param[in] entryId - unique identifier of the entry
178 */
179 void erase(std::string entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500180
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600181 /**
182 * @brief Deletes all versions except for the current one
183 */
Pavithra Barithaya36cc1c82024-06-22 00:41:28 -0500184 void deleteAll() override;
Gunnar Millsded875d2017-08-28 16:44:52 -0500185
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600186 /** @brief Creates an active association to the
187 * newly active software image
188 *
189 * @param[in] path - The path to create the association to.
190 */
191 void createActiveAssociation(const std::string& path);
Gunnar Millsded875d2017-08-28 16:44:52 -0500192
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600193 /** @brief Removes the associations from the provided software image path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600194 *
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600195 * @param[in] path - The path to remove the associations from.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600196 */
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600197 void removeAssociations(const std::string& path);
Gunnar Millsded875d2017-08-28 16:44:52 -0500198
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600199 /** @brief Determine if the given priority is the lowest
200 *
201 * @param[in] value - The priority that needs to be checked.
202 *
203 * @return boolean corresponding to whether the given
204 * priority is lowest.
205 */
206 bool isLowestPriority(uint8_t value);
Saqib Khanb9da6632017-09-13 09:48:37 -0500207
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600208 /**
209 * @brief Updates the U-Boot variables to point to the requested
210 * versionId, so that the systems boots from this version on
211 * the next reboot.
212 *
213 * @param[in] versionId - The version to point the system to boot from.
214 */
215 void updateUbootEnvVars(const std::string& versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500216
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600217 /**
218 * @brief Updates the uboot variables to point to BMC version with lowest
219 * priority, so that the system boots from this version on the
220 * next boot.
221 */
222 void resetUbootEnvVars();
Saqib Khan49446ae2017-10-02 10:54:20 -0500223
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600224 /** @brief Brings the total number of active BMC versions to
225 * ACTIVE_BMC_MAX_ALLOWED -1. This function is intended to be
226 * run before activating a new BMC version. If this function
227 * needs to delete any BMC version(s) it will delete the
228 * version(s) with the highest priority, skipping the
229 * functional BMC version.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500230 *
231 * @param[in] caller - The Activation object that called this function.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600232 */
Lei YU0cd6d842021-12-27 11:56:02 +0800233 void freeSpace(const Activation& caller);
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600234
AppaRao Puli1bb6dcb2020-03-27 20:51:57 +0530235 /** @brief Creates a updateable association to the
236 * "running" BMC software image
237 *
238 * @param[in] path - The path to create the association.
239 */
240 void createUpdateableAssociation(const std::string& path);
241
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600242 /** @brief Persistent map of Version D-Bus objects and their
243 * version id */
244 std::map<std::string, std::unique_ptr<VersionClass>> versions;
245
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800246 /** @brief Vector of needed BMC images in the tarball*/
247 std::vector<std::string> imageUpdateList;
248
Manojkiran Eda19dd56b2024-06-17 14:29:52 +0530249 /** @brief The slot of running BMC image */
Lei YU531fbc22021-12-10 20:03:18 +0800250 uint32_t runningImageSlot = 0;
251
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -0700252 /** @brief Flag to indicate if the update interface is used or not */
253 bool useUpdateDBusInterface;
254
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600255 private:
256 /** @brief Callback function for Software.Version match.
257 * @details Creates an Activation D-Bus object.
258 *
259 * @param[in] msg - Data associated with subscribed signal
260 */
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -0500261 void createActivation(sdbusplus::message_t& msg);
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500262
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600263 /**
264 * @brief Validates the presence of SquashFS image in the image dir.
265 *
266 * @param[in] filePath - The path to the image dir.
267 * @param[out] result - ActivationStatus Enum.
268 * ready if validation was successful.
269 * invalid if validation fail.
270 * active if image is the current version.
271 *
272 */
273 ActivationStatus validateSquashFSImage(const std::string& filePath);
Saqib Khan35e83f32017-05-22 11:37:32 -0500274
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600275 /** @brief BMC factory reset - marks the read-write partition for
276 * recreation upon reboot. */
277 void reset() override;
Michael Tritz37a59042017-07-12 13:44:53 -0500278
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600279 /**
280 * @brief Enables field mode, if value=true.
281 *
282 * @param[in] value - If true, enables field mode.
283 * @param[out] result - Returns the current state of field mode.
284 *
285 */
286 bool fieldModeEnabled(bool value) override;
Michael Tritz0129d922017-08-10 19:33:46 -0500287
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600288 /** @brief Sets the BMC inventory item path under
289 * /xyz/openbmc_project/inventory/system/chassis/. */
290 void setBMCInventoryPath();
Gunnar Millsb60add12017-08-24 16:41:42 -0500291
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600292 /** @brief The path to the BMC inventory item. */
293 std::string bmcInventoryPath;
Gunnar Millsb60add12017-08-24 16:41:42 -0500294
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600295 /** @brief Restores field mode status on reboot. */
296 void restoreFieldModeStatus();
Michael Tritz0129d922017-08-10 19:33:46 -0500297
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600298 /** @brief Creates a functional association to the
299 * "running" BMC software image
300 *
301 * @param[in] path - The path to create the association to.
302 */
303 void createFunctionalAssociation(const std::string& path);
Gunnar Mills88e8a322017-09-13 11:09:28 -0500304
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600305 /** @brief Persistent sdbusplus D-Bus bus connection. */
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -0500306 sdbusplus::bus_t& bus;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500307
Lei YU56aaf452018-06-21 16:09:44 +0800308 /** @brief The helper of image updater. */
309 Helper helper;
310
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600311 /** @brief Persistent map of Activation D-Bus objects and their
312 * version id */
313 std::map<std::string, std::unique_ptr<Activation>> activations;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500314
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600315 /** @brief sdbusplus signal match for Software.Version */
Jagpal Singh Gillbb024eb2024-04-07 23:34:00 -0700316 std::unique_ptr<sdbusplus::bus::match_t> versionMatch;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500317
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600318 /** @brief This entry's associations */
Patrick Williamsfc33ba82024-08-16 15:19:54 -0400319 AssociationList assocs;
Gunnar Millsded875d2017-08-28 16:44:52 -0500320
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600321 /** @brief Clears read only partition for
322 * given Activation D-Bus object.
323 *
324 * @param[in] versionId - The version id.
325 */
Pavithra Barithaya6d178522024-06-24 04:17:29 -0500326 void removeReadOnlyPartition(const std::string& versionId);
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600327
328 /** @brief Copies U-Boot from the currently booted BMC chip to the
329 * alternate chip.
330 */
331 void mirrorUbootToAlt();
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800332
333 /** @brief Check the required image files
334 *
335 * @param[in] filePath - BMC tarball file path
336 * @param[in] imageList - Image filenames included in the BMC tarball
337 * @param[out] result - Boolean
338 * true if all image files are found in BMC tarball
339 * false if one of image files is missing
340 */
Pavithra Barithayac5f6e7e2024-06-24 09:50:21 -0500341 static bool checkImage(const std::string& filePath,
342 const std::vector<std::string>& imageList);
Lei YU6e9fb1d2021-02-19 18:01:40 +0800343
Adriana Kobylak30352a62024-04-09 09:25:36 -0500344 /** @brief Persistent MinimumVersion D-Bus object */
345 std::unique_ptr<MinimumVersion> minimumVersionObject;
346
Lei YU6e9fb1d2021-02-19 18:01:40 +0800347#ifdef HOST_BIOS_UPGRADE
348 /** @brief Create the BIOS object without knowing the version.
349 *
350 * The object is created only to provide the DBus access so that an
351 * external service could set the correct BIOS version.
352 * On BIOS code update, the version is updated accordingly.
353 */
354 void createBIOSObject();
355
356 /** @brief Persistent Activation D-Bus object for BIOS */
357 std::unique_ptr<Activation> biosActivation;
358
Lei YU16aa28a2021-05-07 10:17:30 +0800359 public:
Lei YU6e9fb1d2021-02-19 18:01:40 +0800360 /** @brief Persistent Version D-Bus object for BIOS */
361 std::unique_ptr<VersionClass> biosVersion;
362#endif
Lei YU531fbc22021-12-10 20:03:18 +0800363
364 /** @brief Get the slot number of running image */
365 void getRunningSlot();
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500366};
367
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500368} // namespace updater
369} // namespace software
370} // namespace phosphor