blob: 4c47c8bdbe40f73fc5b82623e1aaeb07068294cd [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"
Saqib Khan705f1bf2017-06-09 23:58:38 -05005#include "version.hpp"
Gunnar Millsb0ce9962018-09-07 13:39:10 -05006#include "xyz/openbmc_project/Collection/DeleteAll/server.hpp"
7
8#include <sdbusplus/server.hpp>
John Wang85c356f2019-09-11 16:20:13 +08009#include <xyz/openbmc_project/Association/Definitions/server.hpp>
Michael Tritz37a59042017-07-12 13:44:53 -050010#include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
Michael Tritz0129d922017-08-10 19:33:46 -050011#include <xyz/openbmc_project/Control/FieldMode/server.hpp>
Gunnar Millsec1b41c2017-05-02 12:20:36 -050012
Adriana Kobylak58aa7502020-06-08 11:12:11 -050013#include <string>
Bright Cheng8e9ccfe2019-11-18 16:18:44 +080014#include <vector>
Adriana Kobylak58aa7502020-06-08 11:12:11 -050015
Gunnar Millsec1b41c2017-05-02 12:20:36 -050016namespace phosphor
17{
18namespace software
19{
20namespace updater
21{
22
Michael Tritz37a59042017-07-12 13:44:53 -050023using ItemUpdaterInherit = sdbusplus::server::object::object<
Adriana Kobylak2285fe02018-02-27 15:36:59 -060024 sdbusplus::xyz::openbmc_project::Common::server::FactoryReset,
25 sdbusplus::xyz::openbmc_project::Control::server::FieldMode,
John Wang85c356f2019-09-11 16:20:13 +080026 sdbusplus::xyz::openbmc_project::Association::server::Definitions,
Adriana Kobylak2285fe02018-02-27 15:36:59 -060027 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
Michael Tritz37a59042017-07-12 13:44:53 -050028
Patrick Williamse75d10f2017-05-30 16:56:32 -050029namespace MatchRules = sdbusplus::bus::match::rules;
Gunnar Mills48442912017-10-06 13:34:07 -050030using VersionClass = phosphor::software::manager::Version;
Gunnar Millsded875d2017-08-28 16:44:52 -050031using AssociationList =
Adriana Kobylak2285fe02018-02-27 15:36:59 -060032 std::vector<std::tuple<std::string, std::string, std::string>>;
Gunnar Millsded875d2017-08-28 16:44:52 -050033
Gunnar Millsec1b41c2017-05-02 12:20:36 -050034/** @class ItemUpdater
35 * @brief Manages the activation of the BMC version items.
36 */
Michael Tritz37a59042017-07-12 13:44:53 -050037class ItemUpdater : public ItemUpdaterInherit
Gunnar Millsec1b41c2017-05-02 12:20:36 -050038{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060039 public:
40 /*
41 * @brief Types of Activation status for image validation.
42 */
43 enum class ActivationStatus
44 {
45 ready,
46 invalid,
47 active
48 };
Saqib Khan35e83f32017-05-22 11:37:32 -050049
Adriana Kobylak2285fe02018-02-27 15:36:59 -060050 /** @brief Constructs ItemUpdater
51 *
52 * @param[in] bus - The D-Bus bus object
53 */
54 ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) :
Lei YU56aaf452018-06-21 16:09:44 +080055 ItemUpdaterInherit(bus, path.c_str(), false), bus(bus), helper(bus),
Adriana Kobylak2285fe02018-02-27 15:36:59 -060056 versionMatch(bus,
57 MatchRules::interfacesAdded() +
58 MatchRules::path("/xyz/openbmc_project/software"),
59 std::bind(std::mem_fn(&ItemUpdater::createActivation),
60 this, std::placeholders::_1))
61 {
Lei YU531fbc22021-12-10 20:03:18 +080062 getRunningSlot();
Adriana Kobylak2285fe02018-02-27 15:36:59 -060063 setBMCInventoryPath();
64 processBMCImage();
65 restoreFieldModeStatus();
Lei YU6e9fb1d2021-02-19 18:01:40 +080066#ifdef HOST_BIOS_UPGRADE
67 createBIOSObject();
68#endif
Adriana Kobylak2285fe02018-02-27 15:36:59 -060069 emit_object_added();
70 };
Gunnar Millsec1b41c2017-05-02 12:20:36 -050071
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -050072 /** @brief Save priority value to persistent storage (flash and optionally
73 * a U-Boot environment variable)
74 *
75 * @param[in] versionId - The Id of the version
76 * @param[in] value - The priority value
77 * @return None
78 */
79 void savePriority(const std::string& versionId, uint8_t value);
80
Adriana Kobylak2285fe02018-02-27 15:36:59 -060081 /** @brief Sets the given priority free by incrementing
82 * any existing priority with the same value by 1
83 *
84 * @param[in] value - The priority that needs to be set free.
85 * @param[in] versionId - The Id of the version for which we
86 * are trying to free up the priority.
87 * @return None
88 */
89 void freePriority(uint8_t value, const std::string& versionId);
Saqib Khan4c1aec02017-07-06 11:46:13 -050090
Adriana Kobylak2285fe02018-02-27 15:36:59 -060091 /**
92 * @brief Create and populate the active BMC Version.
93 */
94 void processBMCImage();
Saqib Khanba239882017-05-26 08:41:54 -050095
Adriana Kobylak2285fe02018-02-27 15:36:59 -060096 /**
97 * @brief Erase specified entry D-Bus object
98 * if Action property is not set to Active
99 *
100 * @param[in] entryId - unique identifier of the entry
101 */
102 void erase(std::string entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500103
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600104 /**
105 * @brief Deletes all versions except for the current one
106 */
107 void deleteAll();
Gunnar Millsded875d2017-08-28 16:44:52 -0500108
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600109 /** @brief Creates an active association to the
110 * newly active software image
111 *
112 * @param[in] path - The path to create the association to.
113 */
114 void createActiveAssociation(const std::string& path);
Gunnar Millsded875d2017-08-28 16:44:52 -0500115
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600116 /** @brief Removes the associations from the provided software image path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600117 *
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600118 * @param[in] path - The path to remove the associations from.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600119 */
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600120 void removeAssociations(const std::string& path);
Gunnar Millsded875d2017-08-28 16:44:52 -0500121
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600122 /** @brief Determine if the given priority is the lowest
123 *
124 * @param[in] value - The priority that needs to be checked.
125 *
126 * @return boolean corresponding to whether the given
127 * priority is lowest.
128 */
129 bool isLowestPriority(uint8_t value);
Saqib Khanb9da6632017-09-13 09:48:37 -0500130
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600131 /**
132 * @brief Updates the U-Boot variables to point to the requested
133 * versionId, so that the systems boots from this version on
134 * the next reboot.
135 *
136 * @param[in] versionId - The version to point the system to boot from.
137 */
138 void updateUbootEnvVars(const std::string& versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500139
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600140 /**
141 * @brief Updates the uboot variables to point to BMC version with lowest
142 * priority, so that the system boots from this version on the
143 * next boot.
144 */
145 void resetUbootEnvVars();
Saqib Khan49446ae2017-10-02 10:54:20 -0500146
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600147 /** @brief Brings the total number of active BMC versions to
148 * ACTIVE_BMC_MAX_ALLOWED -1. This function is intended to be
149 * run before activating a new BMC version. If this function
150 * needs to delete any BMC version(s) it will delete the
151 * version(s) with the highest priority, skipping the
152 * functional BMC version.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500153 *
154 * @param[in] caller - The Activation object that called this function.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600155 */
Lei YU0cd6d842021-12-27 11:56:02 +0800156 void freeSpace(const Activation& caller);
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600157
AppaRao Puli1bb6dcb2020-03-27 20:51:57 +0530158 /** @brief Creates a updateable association to the
159 * "running" BMC software image
160 *
161 * @param[in] path - The path to create the association.
162 */
163 void createUpdateableAssociation(const std::string& path);
164
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600165 /** @brief Persistent map of Version D-Bus objects and their
166 * version id */
167 std::map<std::string, std::unique_ptr<VersionClass>> versions;
168
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800169 /** @brief Vector of needed BMC images in the tarball*/
170 std::vector<std::string> imageUpdateList;
171
Lei YU531fbc22021-12-10 20:03:18 +0800172 /** @breif The slot of running BMC image */
173 uint32_t runningImageSlot = 0;
174
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600175 private:
176 /** @brief Callback function for Software.Version match.
177 * @details Creates an Activation D-Bus object.
178 *
179 * @param[in] msg - Data associated with subscribed signal
180 */
181 void createActivation(sdbusplus::message::message& msg);
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500182
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600183 /**
184 * @brief Validates the presence of SquashFS image in the image dir.
185 *
186 * @param[in] filePath - The path to the image dir.
187 * @param[out] result - ActivationStatus Enum.
188 * ready if validation was successful.
189 * invalid if validation fail.
190 * active if image is the current version.
191 *
192 */
193 ActivationStatus validateSquashFSImage(const std::string& filePath);
Saqib Khan35e83f32017-05-22 11:37:32 -0500194
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600195 /** @brief BMC factory reset - marks the read-write partition for
196 * recreation upon reboot. */
197 void reset() override;
Michael Tritz37a59042017-07-12 13:44:53 -0500198
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600199 /**
200 * @brief Enables field mode, if value=true.
201 *
202 * @param[in] value - If true, enables field mode.
203 * @param[out] result - Returns the current state of field mode.
204 *
205 */
206 bool fieldModeEnabled(bool value) override;
Michael Tritz0129d922017-08-10 19:33:46 -0500207
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600208 /** @brief Sets the BMC inventory item path under
209 * /xyz/openbmc_project/inventory/system/chassis/. */
210 void setBMCInventoryPath();
Gunnar Millsb60add12017-08-24 16:41:42 -0500211
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600212 /** @brief The path to the BMC inventory item. */
213 std::string bmcInventoryPath;
Gunnar Millsb60add12017-08-24 16:41:42 -0500214
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600215 /** @brief Restores field mode status on reboot. */
216 void restoreFieldModeStatus();
Michael Tritz0129d922017-08-10 19:33:46 -0500217
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600218 /** @brief Creates a functional association to the
219 * "running" BMC software image
220 *
221 * @param[in] path - The path to create the association to.
222 */
223 void createFunctionalAssociation(const std::string& path);
Gunnar Mills88e8a322017-09-13 11:09:28 -0500224
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600225 /** @brief Persistent sdbusplus D-Bus bus connection. */
226 sdbusplus::bus::bus& bus;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500227
Lei YU56aaf452018-06-21 16:09:44 +0800228 /** @brief The helper of image updater. */
229 Helper helper;
230
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600231 /** @brief Persistent map of Activation D-Bus objects and their
232 * version id */
233 std::map<std::string, std::unique_ptr<Activation>> activations;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500234
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600235 /** @brief sdbusplus signal match for Software.Version */
236 sdbusplus::bus::match_t versionMatch;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500237
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600238 /** @brief This entry's associations */
239 AssociationList assocs = {};
Gunnar Millsded875d2017-08-28 16:44:52 -0500240
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600241 /** @brief Clears read only partition for
242 * given Activation D-Bus object.
243 *
244 * @param[in] versionId - The version id.
245 */
246 void removeReadOnlyPartition(std::string versionId);
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600247
248 /** @brief Copies U-Boot from the currently booted BMC chip to the
249 * alternate chip.
250 */
251 void mirrorUbootToAlt();
Bright Cheng8e9ccfe2019-11-18 16:18:44 +0800252
253 /** @brief Check the required image files
254 *
255 * @param[in] filePath - BMC tarball file path
256 * @param[in] imageList - Image filenames included in the BMC tarball
257 * @param[out] result - Boolean
258 * true if all image files are found in BMC tarball
259 * false if one of image files is missing
260 */
261 bool checkImage(const std::string& filePath,
262 const std::vector<std::string>& imageList);
Lei YU6e9fb1d2021-02-19 18:01:40 +0800263
264#ifdef HOST_BIOS_UPGRADE
265 /** @brief Create the BIOS object without knowing the version.
266 *
267 * The object is created only to provide the DBus access so that an
268 * external service could set the correct BIOS version.
269 * On BIOS code update, the version is updated accordingly.
270 */
271 void createBIOSObject();
272
273 /** @brief Persistent Activation D-Bus object for BIOS */
274 std::unique_ptr<Activation> biosActivation;
275
Lei YU16aa28a2021-05-07 10:17:30 +0800276 public:
Lei YU6e9fb1d2021-02-19 18:01:40 +0800277 /** @brief Persistent Version D-Bus object for BIOS */
278 std::unique_ptr<VersionClass> biosVersion;
279#endif
Lei YU531fbc22021-12-10 20:03:18 +0800280
281 /** @brief Get the slot number of running image */
282 void getRunningSlot();
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500283};
284
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500285} // namespace updater
286} // namespace software
287} // namespace phosphor