blob: 97686276ab683ccf1287811a35559c9a2b4e1de9 [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
9#include <sdbusplus/server.hpp>
John Wang85c356f2019-09-11 16:20:13 +080010#include <xyz/openbmc_project/Association/Definitions/server.hpp>
Michael Tritz37a59042017-07-12 13:44:53 -050011#include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
Michael Tritz0129d922017-08-10 19:33:46 -050012#include <xyz/openbmc_project/Control/FieldMode/server.hpp>
Adriana Kobylak30352a62024-04-09 09:25:36 -050013#include <xyz/openbmc_project/Software/MinimumVersion/server.hpp>
Gunnar Millsec1b41c2017-05-02 12:20:36 -050014
Adriana Kobylak58aa7502020-06-08 11:12:11 -050015#include <string>
Bright Cheng8e9ccfe2019-11-18 16:18:44 +080016#include <vector>
Adriana Kobylak58aa7502020-06-08 11:12:11 -050017
Gunnar Millsec1b41c2017-05-02 12:20:36 -050018namespace phosphor
19{
20namespace software
21{
22namespace updater
23{
24
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -050025using ItemUpdaterInherit = sdbusplus::server::object_t<
Patrick Williams1e9a5f12023-08-23 16:53:06 -050026 sdbusplus::server::xyz::openbmc_project::common::FactoryReset,
27 sdbusplus::server::xyz::openbmc_project::control::FieldMode,
28 sdbusplus::server::xyz::openbmc_project::association::Definitions,
29 sdbusplus::server::xyz::openbmc_project::collection::DeleteAll>;
Adriana Kobylak30352a62024-04-09 09:25:36 -050030using MinimumVersionInherit = sdbusplus::server::object_t<
31 sdbusplus::server::xyz::openbmc_project::software::MinimumVersion>;
Michael Tritz37a59042017-07-12 13:44:53 -050032
Patrick Williamse75d10f2017-05-30 16:56:32 -050033namespace MatchRules = sdbusplus::bus::match::rules;
Gunnar Mills48442912017-10-06 13:34:07 -050034using VersionClass = phosphor::software::manager::Version;
Gunnar Millsded875d2017-08-28 16:44:52 -050035using AssociationList =
Adriana Kobylak2285fe02018-02-27 15:36:59 -060036 std::vector<std::tuple<std::string, std::string, std::string>>;
Gunnar Millsded875d2017-08-28 16:44:52 -050037
Adriana Kobylak30352a62024-04-09 09:25:36 -050038/** @class MinimumVersion
39 * @brief OpenBMC MinimumVersion implementation.
40 * @details A concrete implementation for
41 * xyz.openbmc_project.Software.MinimumVersion DBus API.
42 */
43class MinimumVersion : public MinimumVersionInherit
44{
45 public:
46 /** @brief Constructs MinimumVersion
47 *
48 * @param[in] bus - The D-Bus bus object
49 * @param[in] path - The D-bus object path
50 */
51 MinimumVersion(sdbusplus::bus_t& bus, const std::string& path) :
52 MinimumVersionInherit(bus, path.c_str(), action::emit_interface_added)
53 {}
54};
55
Gunnar Millsec1b41c2017-05-02 12:20:36 -050056/** @class ItemUpdater
57 * @brief Manages the activation of the BMC version items.
58 */
Michael Tritz37a59042017-07-12 13:44:53 -050059class ItemUpdater : public ItemUpdaterInherit
Gunnar Millsec1b41c2017-05-02 12:20:36 -050060{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060061 public:
62 /*
63 * @brief Types of Activation status for image validation.
64 */
65 enum class ActivationStatus
66 {
67 ready,
68 invalid,
69 active
70 };
Saqib Khan35e83f32017-05-22 11:37:32 -050071
Adriana Kobylak2285fe02018-02-27 15:36:59 -060072 /** @brief Constructs ItemUpdater
73 *
74 * @param[in] bus - The D-Bus bus object
75 */
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -050076 ItemUpdater(sdbusplus::bus_t& bus, const std::string& path) :
Patrick Williams35aa9a82022-04-05 15:55:30 -050077 ItemUpdaterInherit(bus, path.c_str(),
78 ItemUpdaterInherit::action::defer_emit),
79 bus(bus), helper(bus),
Adriana Kobylak2285fe02018-02-27 15:36:59 -060080 versionMatch(bus,
81 MatchRules::interfacesAdded() +
82 MatchRules::path("/xyz/openbmc_project/software"),
83 std::bind(std::mem_fn(&ItemUpdater::createActivation),
84 this, std::placeholders::_1))
85 {
Lei YU531fbc22021-12-10 20:03:18 +080086 getRunningSlot();
Adriana Kobylak2285fe02018-02-27 15:36:59 -060087 setBMCInventoryPath();
88 processBMCImage();
89 restoreFieldModeStatus();
Lei YU6e9fb1d2021-02-19 18:01:40 +080090#ifdef HOST_BIOS_UPGRADE
91 createBIOSObject();
92#endif
Adriana Kobylak2285fe02018-02-27 15:36:59 -060093 emit_object_added();
94 };
Gunnar Millsec1b41c2017-05-02 12:20:36 -050095
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -050096 /** @brief Save priority value to persistent storage (flash and optionally
97 * a U-Boot environment variable)
98 *
99 * @param[in] versionId - The Id of the version
100 * @param[in] value - The priority value
101 * @return None
102 */
103 void savePriority(const std::string& versionId, uint8_t value);
104
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600105 /** @brief Sets the given priority free by incrementing
106 * any existing priority with the same value by 1
107 *
108 * @param[in] value - The priority that needs to be set free.
109 * @param[in] versionId - The Id of the version for which we
110 * are trying to free up the priority.
111 * @return None
112 */
113 void freePriority(uint8_t value, const std::string& versionId);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500114
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600115 /**
116 * @brief Create and populate the active BMC Version.
117 */
118 void processBMCImage();
Saqib Khanba239882017-05-26 08:41:54 -0500119
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600120 /**
121 * @brief Erase specified entry D-Bus object
122 * if Action property is not set to Active
123 *
124 * @param[in] entryId - unique identifier of the entry
125 */
126 void erase(std::string entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500127
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600128 /**
129 * @brief Deletes all versions except for the current one
130 */
Pavithra Barithaya36cc1c82024-06-22 00:41:28 -0500131 void deleteAll() override;
Gunnar Millsded875d2017-08-28 16:44:52 -0500132
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600133 /** @brief Creates an active association to the
134 * newly active software image
135 *
136 * @param[in] path - The path to create the association to.
137 */
138 void createActiveAssociation(const std::string& path);
Gunnar Millsded875d2017-08-28 16:44:52 -0500139
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600140 /** @brief Removes the associations from the provided software image path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600141 *
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600142 * @param[in] path - The path to remove the associations from.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600143 */
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600144 void removeAssociations(const std::string& path);
Gunnar Millsded875d2017-08-28 16:44:52 -0500145
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600146 /** @brief Determine if the given priority is the lowest
147 *
148 * @param[in] value - The priority that needs to be checked.
149 *
150 * @return boolean corresponding to whether the given
151 * priority is lowest.
152 */
153 bool isLowestPriority(uint8_t value);
Saqib Khanb9da6632017-09-13 09:48:37 -0500154
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600155 /**
156 * @brief Updates the U-Boot variables to point to the requested
157 * versionId, so that the systems boots from this version on
158 * the next reboot.
159 *
160 * @param[in] versionId - The version to point the system to boot from.
161 */
162 void updateUbootEnvVars(const std::string& versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500163
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600164 /**
165 * @brief Updates the uboot variables to point to BMC version with lowest
166 * priority, so that the system boots from this version on the
167 * next boot.
168 */
169 void resetUbootEnvVars();
Saqib Khan49446ae2017-10-02 10:54:20 -0500170
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600171 /** @brief Brings the total number of active BMC versions to
172 * ACTIVE_BMC_MAX_ALLOWED -1. This function is intended to be
173 * run before activating a new BMC version. If this function
174 * needs to delete any BMC version(s) it will delete the
175 * version(s) with the highest priority, skipping the
176 * functional BMC version.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500177 *
178 * @param[in] caller - The Activation object that called this function.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600179 */
Lei YU0cd6d842021-12-27 11:56:02 +0800180 void freeSpace(const Activation& caller);
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600181
AppaRao Puli1bb6dcb2020-03-27 20:51:57 +0530182 /** @brief Creates a updateable association to the
183 * "running" BMC software image
184 *
185 * @param[in] path - The path to create the association.
186 */
187 void createUpdateableAssociation(const std::string& path);
188
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600189 /** @brief Persistent map of Version D-Bus objects and their
190 * version id */
191 std::map<std::string, std::unique_ptr<VersionClass>> versions;
192
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800193 /** @brief Vector of needed BMC images in the tarball*/
194 std::vector<std::string> imageUpdateList;
195
Manojkiran Eda19dd56b2024-06-17 14:29:52 +0530196 /** @brief The slot of running BMC image */
Lei YU531fbc22021-12-10 20:03:18 +0800197 uint32_t runningImageSlot = 0;
198
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600199 private:
200 /** @brief Callback function for Software.Version match.
201 * @details Creates an Activation D-Bus object.
202 *
203 * @param[in] msg - Data associated with subscribed signal
204 */
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -0500205 void createActivation(sdbusplus::message_t& msg);
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500206
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600207 /**
208 * @brief Validates the presence of SquashFS image in the image dir.
209 *
210 * @param[in] filePath - The path to the image dir.
211 * @param[out] result - ActivationStatus Enum.
212 * ready if validation was successful.
213 * invalid if validation fail.
214 * active if image is the current version.
215 *
216 */
217 ActivationStatus validateSquashFSImage(const std::string& filePath);
Saqib Khan35e83f32017-05-22 11:37:32 -0500218
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600219 /** @brief BMC factory reset - marks the read-write partition for
220 * recreation upon reboot. */
221 void reset() override;
Michael Tritz37a59042017-07-12 13:44:53 -0500222
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600223 /**
224 * @brief Enables field mode, if value=true.
225 *
226 * @param[in] value - If true, enables field mode.
227 * @param[out] result - Returns the current state of field mode.
228 *
229 */
230 bool fieldModeEnabled(bool value) override;
Michael Tritz0129d922017-08-10 19:33:46 -0500231
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600232 /** @brief Sets the BMC inventory item path under
233 * /xyz/openbmc_project/inventory/system/chassis/. */
234 void setBMCInventoryPath();
Gunnar Millsb60add12017-08-24 16:41:42 -0500235
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600236 /** @brief The path to the BMC inventory item. */
237 std::string bmcInventoryPath;
Gunnar Millsb60add12017-08-24 16:41:42 -0500238
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600239 /** @brief Restores field mode status on reboot. */
240 void restoreFieldModeStatus();
Michael Tritz0129d922017-08-10 19:33:46 -0500241
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600242 /** @brief Creates a functional association to the
243 * "running" BMC software image
244 *
245 * @param[in] path - The path to create the association to.
246 */
247 void createFunctionalAssociation(const std::string& path);
Gunnar Mills88e8a322017-09-13 11:09:28 -0500248
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600249 /** @brief Persistent sdbusplus D-Bus bus connection. */
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -0500250 sdbusplus::bus_t& bus;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500251
Lei YU56aaf452018-06-21 16:09:44 +0800252 /** @brief The helper of image updater. */
253 Helper helper;
254
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600255 /** @brief Persistent map of Activation D-Bus objects and their
256 * version id */
257 std::map<std::string, std::unique_ptr<Activation>> activations;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500258
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600259 /** @brief sdbusplus signal match for Software.Version */
260 sdbusplus::bus::match_t versionMatch;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500261
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600262 /** @brief This entry's associations */
Patrick Williamsfc33ba82024-08-16 15:19:54 -0400263 AssociationList assocs;
Gunnar Millsded875d2017-08-28 16:44:52 -0500264
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600265 /** @brief Clears read only partition for
266 * given Activation D-Bus object.
267 *
268 * @param[in] versionId - The version id.
269 */
Pavithra Barithaya6d178522024-06-24 04:17:29 -0500270 void removeReadOnlyPartition(const std::string& versionId);
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600271
272 /** @brief Copies U-Boot from the currently booted BMC chip to the
273 * alternate chip.
274 */
275 void mirrorUbootToAlt();
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800276
277 /** @brief Check the required image files
278 *
279 * @param[in] filePath - BMC tarball file path
280 * @param[in] imageList - Image filenames included in the BMC tarball
281 * @param[out] result - Boolean
282 * true if all image files are found in BMC tarball
283 * false if one of image files is missing
284 */
Pavithra Barithayac5f6e7e2024-06-24 09:50:21 -0500285 static bool checkImage(const std::string& filePath,
286 const std::vector<std::string>& imageList);
Lei YU6e9fb1d2021-02-19 18:01:40 +0800287
Adriana Kobylak30352a62024-04-09 09:25:36 -0500288 /** @brief Persistent MinimumVersion D-Bus object */
289 std::unique_ptr<MinimumVersion> minimumVersionObject;
290
Lei YU6e9fb1d2021-02-19 18:01:40 +0800291#ifdef HOST_BIOS_UPGRADE
292 /** @brief Create the BIOS object without knowing the version.
293 *
294 * The object is created only to provide the DBus access so that an
295 * external service could set the correct BIOS version.
296 * On BIOS code update, the version is updated accordingly.
297 */
298 void createBIOSObject();
299
300 /** @brief Persistent Activation D-Bus object for BIOS */
301 std::unique_ptr<Activation> biosActivation;
302
Lei YU16aa28a2021-05-07 10:17:30 +0800303 public:
Lei YU6e9fb1d2021-02-19 18:01:40 +0800304 /** @brief Persistent Version D-Bus object for BIOS */
305 std::unique_ptr<VersionClass> biosVersion;
306#endif
Lei YU531fbc22021-12-10 20:03:18 +0800307
308 /** @brief Get the slot number of running image */
309 void getRunningSlot();
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500310};
311
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500312} // namespace updater
313} // namespace software
314} // namespace phosphor