blob: 0664a705ad400fc6ef9bbfe538700c3719612fec [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
13namespace phosphor
14{
15namespace software
16{
17namespace updater
18{
19
Michael Tritz37a59042017-07-12 13:44:53 -050020using ItemUpdaterInherit = sdbusplus::server::object::object<
Adriana Kobylak2285fe02018-02-27 15:36:59 -060021 sdbusplus::xyz::openbmc_project::Common::server::FactoryReset,
22 sdbusplus::xyz::openbmc_project::Control::server::FieldMode,
John Wang85c356f2019-09-11 16:20:13 +080023 sdbusplus::xyz::openbmc_project::Association::server::Definitions,
Adriana Kobylak2285fe02018-02-27 15:36:59 -060024 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
Michael Tritz37a59042017-07-12 13:44:53 -050025
Patrick Williamse75d10f2017-05-30 16:56:32 -050026namespace MatchRules = sdbusplus::bus::match::rules;
Gunnar Mills48442912017-10-06 13:34:07 -050027using VersionClass = phosphor::software::manager::Version;
Gunnar Millsded875d2017-08-28 16:44:52 -050028using AssociationList =
Adriana Kobylak2285fe02018-02-27 15:36:59 -060029 std::vector<std::tuple<std::string, std::string, std::string>>;
Gunnar Millsded875d2017-08-28 16:44:52 -050030
Gunnar Millsec1b41c2017-05-02 12:20:36 -050031/** @class ItemUpdater
32 * @brief Manages the activation of the BMC version items.
33 */
Michael Tritz37a59042017-07-12 13:44:53 -050034class ItemUpdater : public ItemUpdaterInherit
Gunnar Millsec1b41c2017-05-02 12:20:36 -050035{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060036 public:
37 /*
38 * @brief Types of Activation status for image validation.
39 */
40 enum class ActivationStatus
41 {
42 ready,
43 invalid,
44 active
45 };
Saqib Khan35e83f32017-05-22 11:37:32 -050046
Adriana Kobylak2285fe02018-02-27 15:36:59 -060047 /** @brief Constructs ItemUpdater
48 *
49 * @param[in] bus - The D-Bus bus object
50 */
51 ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) :
Lei YU56aaf452018-06-21 16:09:44 +080052 ItemUpdaterInherit(bus, path.c_str(), false), bus(bus), helper(bus),
Adriana Kobylak2285fe02018-02-27 15:36:59 -060053 versionMatch(bus,
54 MatchRules::interfacesAdded() +
55 MatchRules::path("/xyz/openbmc_project/software"),
56 std::bind(std::mem_fn(&ItemUpdater::createActivation),
57 this, std::placeholders::_1))
58 {
59 setBMCInventoryPath();
60 processBMCImage();
61 restoreFieldModeStatus();
62 emit_object_added();
63 };
Gunnar Millsec1b41c2017-05-02 12:20:36 -050064
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -050065 /** @brief Save priority value to persistent storage (flash and optionally
66 * a U-Boot environment variable)
67 *
68 * @param[in] versionId - The Id of the version
69 * @param[in] value - The priority value
70 * @return None
71 */
72 void savePriority(const std::string& versionId, uint8_t value);
73
Adriana Kobylak2285fe02018-02-27 15:36:59 -060074 /** @brief Sets the given priority free by incrementing
75 * any existing priority with the same value by 1
76 *
77 * @param[in] value - The priority that needs to be set free.
78 * @param[in] versionId - The Id of the version for which we
79 * are trying to free up the priority.
80 * @return None
81 */
82 void freePriority(uint8_t value, const std::string& versionId);
Saqib Khan4c1aec02017-07-06 11:46:13 -050083
Adriana Kobylak2285fe02018-02-27 15:36:59 -060084 /**
85 * @brief Create and populate the active BMC Version.
86 */
87 void processBMCImage();
Saqib Khanba239882017-05-26 08:41:54 -050088
Adriana Kobylak2285fe02018-02-27 15:36:59 -060089 /**
90 * @brief Erase specified entry D-Bus object
91 * if Action property is not set to Active
92 *
93 * @param[in] entryId - unique identifier of the entry
94 */
95 void erase(std::string entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -050096
Adriana Kobylak2285fe02018-02-27 15:36:59 -060097 /**
98 * @brief Deletes all versions except for the current one
99 */
100 void deleteAll();
Gunnar Millsded875d2017-08-28 16:44:52 -0500101
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600102 /** @brief Creates an active association to the
103 * newly active software image
104 *
105 * @param[in] path - The path to create the association to.
106 */
107 void createActiveAssociation(const std::string& path);
Gunnar Millsded875d2017-08-28 16:44:52 -0500108
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600109 /** @brief Removes the associations from the provided software image path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600110 *
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600111 * @param[in] path - The path to remove the associations from.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600112 */
Adriana Kobylak991af7e2018-12-10 13:08:38 -0600113 void removeAssociations(const std::string& path);
Gunnar Millsded875d2017-08-28 16:44:52 -0500114
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600115 /** @brief Determine if the given priority is the lowest
116 *
117 * @param[in] value - The priority that needs to be checked.
118 *
119 * @return boolean corresponding to whether the given
120 * priority is lowest.
121 */
122 bool isLowestPriority(uint8_t value);
Saqib Khanb9da6632017-09-13 09:48:37 -0500123
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600124 /**
125 * @brief Updates the U-Boot variables to point to the requested
126 * versionId, so that the systems boots from this version on
127 * the next reboot.
128 *
129 * @param[in] versionId - The version to point the system to boot from.
130 */
131 void updateUbootEnvVars(const std::string& versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500132
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600133 /**
134 * @brief Updates the uboot variables to point to BMC version with lowest
135 * priority, so that the system boots from this version on the
136 * next boot.
137 */
138 void resetUbootEnvVars();
Saqib Khan49446ae2017-10-02 10:54:20 -0500139
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600140 /** @brief Brings the total number of active BMC versions to
141 * ACTIVE_BMC_MAX_ALLOWED -1. This function is intended to be
142 * run before activating a new BMC version. If this function
143 * needs to delete any BMC version(s) it will delete the
144 * version(s) with the highest priority, skipping the
145 * functional BMC version.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500146 *
147 * @param[in] caller - The Activation object that called this function.
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600148 */
Adriana Kobylaka6963592018-09-07 14:13:29 -0500149 void freeSpace(Activation& caller);
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600150
Adriana Kobylakec4eec32019-11-13 14:28:35 -0600151 /** @brief Persistent map of Version D-Bus objects and their
152 * version id */
153 std::map<std::string, std::unique_ptr<VersionClass>> versions;
154
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600155 private:
156 /** @brief Callback function for Software.Version match.
157 * @details Creates an Activation D-Bus object.
158 *
159 * @param[in] msg - Data associated with subscribed signal
160 */
161 void createActivation(sdbusplus::message::message& msg);
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500162
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600163 /**
164 * @brief Validates the presence of SquashFS image in the image dir.
165 *
166 * @param[in] filePath - The path to the image dir.
167 * @param[out] result - ActivationStatus Enum.
168 * ready if validation was successful.
169 * invalid if validation fail.
170 * active if image is the current version.
171 *
172 */
173 ActivationStatus validateSquashFSImage(const std::string& filePath);
Saqib Khan35e83f32017-05-22 11:37:32 -0500174
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600175 /** @brief BMC factory reset - marks the read-write partition for
176 * recreation upon reboot. */
177 void reset() override;
Michael Tritz37a59042017-07-12 13:44:53 -0500178
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600179 /**
180 * @brief Enables field mode, if value=true.
181 *
182 * @param[in] value - If true, enables field mode.
183 * @param[out] result - Returns the current state of field mode.
184 *
185 */
186 bool fieldModeEnabled(bool value) override;
Michael Tritz0129d922017-08-10 19:33:46 -0500187
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600188 /** @brief Sets the BMC inventory item path under
189 * /xyz/openbmc_project/inventory/system/chassis/. */
190 void setBMCInventoryPath();
Gunnar Millsb60add12017-08-24 16:41:42 -0500191
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600192 /** @brief The path to the BMC inventory item. */
193 std::string bmcInventoryPath;
Gunnar Millsb60add12017-08-24 16:41:42 -0500194
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600195 /** @brief Restores field mode status on reboot. */
196 void restoreFieldModeStatus();
Michael Tritz0129d922017-08-10 19:33:46 -0500197
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600198 /** @brief Creates a functional association to the
199 * "running" BMC software image
200 *
201 * @param[in] path - The path to create the association to.
202 */
203 void createFunctionalAssociation(const std::string& path);
Gunnar Mills88e8a322017-09-13 11:09:28 -0500204
AppaRao Pulibbebec72020-01-28 23:57:41 +0530205 /** @brief Creates a updateable association to the
206 * "running" BMC software image
207 *
208 * @param[in] path - The path to create the association.
209 */
210 void createUpdateableAssociation(const std::string& path);
211
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600212 /** @brief Persistent sdbusplus D-Bus bus connection. */
213 sdbusplus::bus::bus& bus;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500214
Lei YU56aaf452018-06-21 16:09:44 +0800215 /** @brief The helper of image updater. */
216 Helper helper;
217
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600218 /** @brief Persistent map of Activation D-Bus objects and their
219 * version id */
220 std::map<std::string, std::unique_ptr<Activation>> activations;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500221
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600222 /** @brief sdbusplus signal match for Software.Version */
223 sdbusplus::bus::match_t versionMatch;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500224
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600225 /** @brief This entry's associations */
226 AssociationList assocs = {};
Gunnar Millsded875d2017-08-28 16:44:52 -0500227
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600228 /** @brief Clears read only partition for
229 * given Activation D-Bus object.
230 *
231 * @param[in] versionId - The version id.
232 */
233 void removeReadOnlyPartition(std::string versionId);
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600234
235 /** @brief Copies U-Boot from the currently booted BMC chip to the
236 * alternate chip.
237 */
238 void mirrorUbootToAlt();
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500239};
240
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500241} // namespace updater
242} // namespace software
243} // namespace phosphor