blob: 313c568d24309711a32a0cfdb547fa9666471d71 [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>
Andrew Geissler9155b712020-05-16 13:04:44 -05009#include <string>
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>
Gunnar Millsec1b41c2017-05-02 12:20:36 -050013
14namespace phosphor
15{
16namespace software
17{
18namespace updater
19{
20
Michael Tritz37a59042017-07-12 13:44:53 -050021using ItemUpdaterInherit = sdbusplus::server::object::object<
Adriana Kobylak2285fe02018-02-27 15:36:59 -060022 sdbusplus::xyz::openbmc_project::Common::server::FactoryReset,
23 sdbusplus::xyz::openbmc_project::Control::server::FieldMode,
John Wang85c356f2019-09-11 16:20:13 +080024 sdbusplus::xyz::openbmc_project::Association::server::Definitions,
Adriana Kobylak2285fe02018-02-27 15:36:59 -060025 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
Michael Tritz37a59042017-07-12 13:44:53 -050026
Patrick Williamse75d10f2017-05-30 16:56:32 -050027namespace MatchRules = sdbusplus::bus::match::rules;
Gunnar Mills48442912017-10-06 13:34:07 -050028using VersionClass = phosphor::software::manager::Version;
Gunnar Millsded875d2017-08-28 16:44:52 -050029using AssociationList =
Adriana Kobylak2285fe02018-02-27 15:36:59 -060030 std::vector<std::tuple<std::string, std::string, std::string>>;
Gunnar Millsded875d2017-08-28 16:44:52 -050031
Gunnar Millsec1b41c2017-05-02 12:20:36 -050032/** @class ItemUpdater
33 * @brief Manages the activation of the BMC version items.
34 */
Michael Tritz37a59042017-07-12 13:44:53 -050035class ItemUpdater : public ItemUpdaterInherit
Gunnar Millsec1b41c2017-05-02 12:20:36 -050036{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060037 public:
38 /*
39 * @brief Types of Activation status for image validation.
40 */
41 enum class ActivationStatus
42 {
43 ready,
44 invalid,
45 active
46 };
Saqib Khan35e83f32017-05-22 11:37:32 -050047
Adriana Kobylak2285fe02018-02-27 15:36:59 -060048 /** @brief Constructs ItemUpdater
49 *
50 * @param[in] bus - The D-Bus bus object
51 */
52 ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) :
Lei YU56aaf452018-06-21 16:09:44 +080053 ItemUpdaterInherit(bus, path.c_str(), false), bus(bus), helper(bus),
Adriana Kobylak2285fe02018-02-27 15:36:59 -060054 versionMatch(bus,
55 MatchRules::interfacesAdded() +
56 MatchRules::path("/xyz/openbmc_project/software"),
57 std::bind(std::mem_fn(&ItemUpdater::createActivation),
58 this, std::placeholders::_1))
59 {
60 setBMCInventoryPath();
61 processBMCImage();
62 restoreFieldModeStatus();
63 emit_object_added();
64 };
Gunnar Millsec1b41c2017-05-02 12:20:36 -050065
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -050066 /** @brief Save priority value to persistent storage (flash and optionally
67 * a U-Boot environment variable)
68 *
69 * @param[in] versionId - The Id of the version
70 * @param[in] value - The priority value
71 * @return None
72 */
73 void savePriority(const std::string& versionId, uint8_t value);
74
Adriana Kobylak2285fe02018-02-27 15:36:59 -060075 /** @brief Sets the given priority free by incrementing
76 * any existing priority with the same value by 1
77 *
78 * @param[in] value - The priority that needs to be set free.
79 * @param[in] versionId - The Id of the version for which we
80 * are trying to free up the priority.
81 * @return None
82 */
83 void freePriority(uint8_t value, const std::string& versionId);
Saqib Khan4c1aec02017-07-06 11:46:13 -050084
Adriana Kobylak2285fe02018-02-27 15:36:59 -060085 /**
86 * @brief Create and populate the active BMC Version.
87 */
88 void processBMCImage();
Saqib Khanba239882017-05-26 08:41:54 -050089
Adriana Kobylak2285fe02018-02-27 15:36:59 -060090 /**
91 * @brief Erase specified entry D-Bus object
92 * if Action property is not set to Active
93 *
94 * @param[in] entryId - unique identifier of the entry
95 */
96 void erase(std::string entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -050097
Adriana Kobylak2285fe02018-02-27 15:36:59 -060098 /**
99 * @brief Deletes all versions except for the current one
100 */
101 void deleteAll();
Gunnar Millsded875d2017-08-28 16:44:52 -0500102
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600103 /** @brief Creates an active association to the
104 * newly active software image
105 *
106 * @param[in] path - The path to create the association to.
107 */
108 void createActiveAssociation(const std::string& path);
Gunnar Millsded875d2017-08-28 16:44:52 -0500109
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600110 /** @brief Removes the associations from the provided software image path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600111 *
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600112 * @param[in] path - The path to remove the associations from.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600113 */
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600114 void removeAssociations(const std::string& path);
Gunnar Millsded875d2017-08-28 16:44:52 -0500115
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600116 /** @brief Determine if the given priority is the lowest
117 *
118 * @param[in] value - The priority that needs to be checked.
119 *
120 * @return boolean corresponding to whether the given
121 * priority is lowest.
122 */
123 bool isLowestPriority(uint8_t value);
Saqib Khanb9da6632017-09-13 09:48:37 -0500124
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600125 /**
126 * @brief Updates the U-Boot variables to point to the requested
127 * versionId, so that the systems boots from this version on
128 * the next reboot.
129 *
130 * @param[in] versionId - The version to point the system to boot from.
131 */
132 void updateUbootEnvVars(const std::string& versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500133
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600134 /**
135 * @brief Updates the uboot variables to point to BMC version with lowest
136 * priority, so that the system boots from this version on the
137 * next boot.
138 */
139 void resetUbootEnvVars();
Saqib Khan49446ae2017-10-02 10:54:20 -0500140
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600141 /** @brief Brings the total number of active BMC versions to
142 * ACTIVE_BMC_MAX_ALLOWED -1. This function is intended to be
143 * run before activating a new BMC version. If this function
144 * needs to delete any BMC version(s) it will delete the
145 * version(s) with the highest priority, skipping the
146 * functional BMC version.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500147 *
148 * @param[in] caller - The Activation object that called this function.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600149 */
Adriana Kobylaka6963592018-09-07 14:13:29 -0500150 void freeSpace(Activation& caller);
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600151
AppaRao Puli1bb6dcb2020-03-27 20:51:57 +0530152 /** @brief Creates a updateable association to the
153 * "running" BMC software image
154 *
155 * @param[in] path - The path to create the association.
156 */
157 void createUpdateableAssociation(const std::string& path);
158
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600159 /** @brief Persistent map of Version D-Bus objects and their
160 * version id */
161 std::map<std::string, std::unique_ptr<VersionClass>> versions;
162
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600163 private:
164 /** @brief Callback function for Software.Version match.
165 * @details Creates an Activation D-Bus object.
166 *
167 * @param[in] msg - Data associated with subscribed signal
168 */
169 void createActivation(sdbusplus::message::message& msg);
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500170
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600171 /**
172 * @brief Validates the presence of SquashFS image in the image dir.
173 *
174 * @param[in] filePath - The path to the image dir.
175 * @param[out] result - ActivationStatus Enum.
176 * ready if validation was successful.
177 * invalid if validation fail.
178 * active if image is the current version.
179 *
180 */
181 ActivationStatus validateSquashFSImage(const std::string& filePath);
Saqib Khan35e83f32017-05-22 11:37:32 -0500182
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600183 /** @brief BMC factory reset - marks the read-write partition for
184 * recreation upon reboot. */
185 void reset() override;
Michael Tritz37a59042017-07-12 13:44:53 -0500186
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600187 /**
188 * @brief Enables field mode, if value=true.
189 *
190 * @param[in] value - If true, enables field mode.
191 * @param[out] result - Returns the current state of field mode.
192 *
193 */
194 bool fieldModeEnabled(bool value) override;
Michael Tritz0129d922017-08-10 19:33:46 -0500195
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600196 /** @brief Sets the BMC inventory item path under
197 * /xyz/openbmc_project/inventory/system/chassis/. */
198 void setBMCInventoryPath();
Gunnar Millsb60add12017-08-24 16:41:42 -0500199
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600200 /** @brief The path to the BMC inventory item. */
201 std::string bmcInventoryPath;
Gunnar Millsb60add12017-08-24 16:41:42 -0500202
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600203 /** @brief Restores field mode status on reboot. */
204 void restoreFieldModeStatus();
Michael Tritz0129d922017-08-10 19:33:46 -0500205
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600206 /** @brief Creates a functional association to the
207 * "running" BMC software image
208 *
209 * @param[in] path - The path to create the association to.
210 */
211 void createFunctionalAssociation(const std::string& path);
Gunnar Mills88e8a322017-09-13 11:09:28 -0500212
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600213 /** @brief Persistent sdbusplus D-Bus bus connection. */
214 sdbusplus::bus::bus& bus;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500215
Lei YU56aaf452018-06-21 16:09:44 +0800216 /** @brief The helper of image updater. */
217 Helper helper;
218
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600219 /** @brief Persistent map of Activation D-Bus objects and their
220 * version id */
221 std::map<std::string, std::unique_ptr<Activation>> activations;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500222
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600223 /** @brief sdbusplus signal match for Software.Version */
224 sdbusplus::bus::match_t versionMatch;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500225
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600226 /** @brief This entry's associations */
227 AssociationList assocs = {};
Gunnar Millsded875d2017-08-28 16:44:52 -0500228
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600229 /** @brief Clears read only partition for
230 * given Activation D-Bus object.
231 *
232 * @param[in] versionId - The version id.
233 */
234 void removeReadOnlyPartition(std::string versionId);
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600235
236 /** @brief Copies U-Boot from the currently booted BMC chip to the
237 * alternate chip.
238 */
239 void mirrorUbootToAlt();
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500240};
241
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500242} // namespace updater
243} // namespace software
244} // namespace phosphor