blob: ec7aee05ce51280dbd4f07c9b17fbc89de7e1838 [file] [log] [blame]
Gunnar Millsec1b41c2017-05-02 12:20:36 -05001#pragma once
2
3#include <sdbusplus/server.hpp>
4#include "activation.hpp"
Lei YU56aaf452018-06-21 16:09:44 +08005#include "item_updater_helper.hpp"
Saqib Khan705f1bf2017-06-09 23:58:38 -05006#include "version.hpp"
Michael Tritz37a59042017-07-12 13:44:53 -05007#include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
Michael Tritz0129d922017-08-10 19:33:46 -05008#include <xyz/openbmc_project/Control/FieldMode/server.hpp>
Gunnar Millsded875d2017-08-28 16:44:52 -05009#include "org/openbmc/Associations/server.hpp"
Michael Tritzbc1bf3a2017-09-18 16:38:23 -050010#include "xyz/openbmc_project/Collection/DeleteAll/server.hpp"
Gunnar Millsec1b41c2017-05-02 12:20:36 -050011
12namespace phosphor
13{
14namespace software
15{
16namespace updater
17{
18
Michael Tritz37a59042017-07-12 13:44:53 -050019using ItemUpdaterInherit = sdbusplus::server::object::object<
Adriana Kobylak2285fe02018-02-27 15:36:59 -060020 sdbusplus::xyz::openbmc_project::Common::server::FactoryReset,
21 sdbusplus::xyz::openbmc_project::Control::server::FieldMode,
22 sdbusplus::org::openbmc::server::Associations,
23 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
Michael Tritz37a59042017-07-12 13:44:53 -050024
Patrick Williamse75d10f2017-05-30 16:56:32 -050025namespace MatchRules = sdbusplus::bus::match::rules;
Gunnar Mills48442912017-10-06 13:34:07 -050026using VersionClass = phosphor::software::manager::Version;
Gunnar Millsded875d2017-08-28 16:44:52 -050027using AssociationList =
Adriana Kobylak2285fe02018-02-27 15:36:59 -060028 std::vector<std::tuple<std::string, std::string, std::string>>;
Gunnar Millsded875d2017-08-28 16:44:52 -050029
Gunnar Millsec1b41c2017-05-02 12:20:36 -050030/** @class ItemUpdater
31 * @brief Manages the activation of the BMC version items.
32 */
Michael Tritz37a59042017-07-12 13:44:53 -050033class ItemUpdater : public ItemUpdaterInherit
Gunnar Millsec1b41c2017-05-02 12:20:36 -050034{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060035 public:
36 /*
37 * @brief Types of Activation status for image validation.
38 */
39 enum class ActivationStatus
40 {
41 ready,
42 invalid,
43 active
44 };
Saqib Khan35e83f32017-05-22 11:37:32 -050045
Adriana Kobylak2285fe02018-02-27 15:36:59 -060046 /** @brief Constructs ItemUpdater
47 *
48 * @param[in] bus - The D-Bus bus object
49 */
50 ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) :
Lei YU56aaf452018-06-21 16:09:44 +080051 ItemUpdaterInherit(bus, path.c_str(), false), bus(bus), helper(bus),
Adriana Kobylak2285fe02018-02-27 15:36:59 -060052 versionMatch(bus,
53 MatchRules::interfacesAdded() +
54 MatchRules::path("/xyz/openbmc_project/software"),
55 std::bind(std::mem_fn(&ItemUpdater::createActivation),
56 this, std::placeholders::_1))
57 {
58 setBMCInventoryPath();
59 processBMCImage();
60 restoreFieldModeStatus();
61 emit_object_added();
62 };
Gunnar Millsec1b41c2017-05-02 12:20:36 -050063
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -050064 /** @brief Save priority value to persistent storage (flash and optionally
65 * a U-Boot environment variable)
66 *
67 * @param[in] versionId - The Id of the version
68 * @param[in] value - The priority value
69 * @return None
70 */
71 void savePriority(const std::string& versionId, uint8_t value);
72
Adriana Kobylak2285fe02018-02-27 15:36:59 -060073 /** @brief Sets the given priority free by incrementing
74 * any existing priority with the same value by 1
75 *
76 * @param[in] value - The priority that needs to be set free.
77 * @param[in] versionId - The Id of the version for which we
78 * are trying to free up the priority.
79 * @return None
80 */
81 void freePriority(uint8_t value, const std::string& versionId);
Saqib Khan4c1aec02017-07-06 11:46:13 -050082
Adriana Kobylak2285fe02018-02-27 15:36:59 -060083 /**
84 * @brief Create and populate the active BMC Version.
85 */
86 void processBMCImage();
Saqib Khanba239882017-05-26 08:41:54 -050087
Adriana Kobylak2285fe02018-02-27 15:36:59 -060088 /**
89 * @brief Erase specified entry D-Bus object
90 * if Action property is not set to Active
91 *
92 * @param[in] entryId - unique identifier of the entry
93 */
94 void erase(std::string entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -050095
Adriana Kobylak2285fe02018-02-27 15:36:59 -060096 /**
97 * @brief Deletes all versions except for the current one
98 */
99 void deleteAll();
Gunnar Millsded875d2017-08-28 16:44:52 -0500100
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600101 /** @brief Creates an active association to the
102 * newly active software image
103 *
104 * @param[in] path - The path to create the association to.
105 */
106 void createActiveAssociation(const std::string& path);
Gunnar Millsded875d2017-08-28 16:44:52 -0500107
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600108 /** @brief Removes an active association to the software image
109 *
110 * @param[in] path - The path to remove the association from.
111 */
112 void removeActiveAssociation(const std::string& path);
Gunnar Millsded875d2017-08-28 16:44:52 -0500113
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600114 /** @brief Determine if the given priority is the lowest
115 *
116 * @param[in] value - The priority that needs to be checked.
117 *
118 * @return boolean corresponding to whether the given
119 * priority is lowest.
120 */
121 bool isLowestPriority(uint8_t value);
Saqib Khanb9da6632017-09-13 09:48:37 -0500122
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600123 /**
124 * @brief Updates the U-Boot variables to point to the requested
125 * versionId, so that the systems boots from this version on
126 * the next reboot.
127 *
128 * @param[in] versionId - The version to point the system to boot from.
129 */
130 void updateUbootEnvVars(const std::string& versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500131
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600132 /**
133 * @brief Updates the uboot variables to point to BMC version with lowest
134 * priority, so that the system boots from this version on the
135 * next boot.
136 */
137 void resetUbootEnvVars();
Saqib Khan49446ae2017-10-02 10:54:20 -0500138
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600139 /** @brief Brings the total number of active BMC versions to
140 * ACTIVE_BMC_MAX_ALLOWED -1. This function is intended to be
141 * run before activating a new BMC version. If this function
142 * needs to delete any BMC version(s) it will delete the
143 * version(s) with the highest priority, skipping the
144 * functional BMC version.
145 */
146 void freeSpace();
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600147
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600148 private:
149 /** @brief Callback function for Software.Version match.
150 * @details Creates an Activation D-Bus object.
151 *
152 * @param[in] msg - Data associated with subscribed signal
153 */
154 void createActivation(sdbusplus::message::message& msg);
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500155
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600156 /**
157 * @brief Validates the presence of SquashFS image in the image dir.
158 *
159 * @param[in] filePath - The path to the image dir.
160 * @param[out] result - ActivationStatus Enum.
161 * ready if validation was successful.
162 * invalid if validation fail.
163 * active if image is the current version.
164 *
165 */
166 ActivationStatus validateSquashFSImage(const std::string& filePath);
Saqib Khan35e83f32017-05-22 11:37:32 -0500167
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600168 /** @brief BMC factory reset - marks the read-write partition for
169 * recreation upon reboot. */
170 void reset() override;
Michael Tritz37a59042017-07-12 13:44:53 -0500171
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600172 /**
173 * @brief Enables field mode, if value=true.
174 *
175 * @param[in] value - If true, enables field mode.
176 * @param[out] result - Returns the current state of field mode.
177 *
178 */
179 bool fieldModeEnabled(bool value) override;
Michael Tritz0129d922017-08-10 19:33:46 -0500180
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600181 /** @brief Sets the BMC inventory item path under
182 * /xyz/openbmc_project/inventory/system/chassis/. */
183 void setBMCInventoryPath();
Gunnar Millsb60add12017-08-24 16:41:42 -0500184
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600185 /** @brief The path to the BMC inventory item. */
186 std::string bmcInventoryPath;
Gunnar Millsb60add12017-08-24 16:41:42 -0500187
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600188 /** @brief Restores field mode status on reboot. */
189 void restoreFieldModeStatus();
Michael Tritz0129d922017-08-10 19:33:46 -0500190
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600191 /** @brief Creates a functional association to the
192 * "running" BMC software image
193 *
194 * @param[in] path - The path to create the association to.
195 */
196 void createFunctionalAssociation(const std::string& path);
Gunnar Mills88e8a322017-09-13 11:09:28 -0500197
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600198 /** @brief Persistent sdbusplus D-Bus bus connection. */
199 sdbusplus::bus::bus& bus;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500200
Lei YU56aaf452018-06-21 16:09:44 +0800201 /** @brief The helper of image updater. */
202 Helper helper;
203
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600204 /** @brief Persistent map of Activation D-Bus objects and their
205 * version id */
206 std::map<std::string, std::unique_ptr<Activation>> activations;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500207
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600208 /** @brief Persistent map of Version D-Bus objects and their
209 * version id */
210 std::map<std::string, std::unique_ptr<VersionClass>> versions;
Saqib Khan705f1bf2017-06-09 23:58:38 -0500211
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600212 /** @brief sdbusplus signal match for Software.Version */
213 sdbusplus::bus::match_t versionMatch;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500214
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600215 /** @brief This entry's associations */
216 AssociationList assocs = {};
Gunnar Millsded875d2017-08-28 16:44:52 -0500217
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600218 /** @brief Clears read only partition for
219 * given Activation D-Bus object.
220 *
221 * @param[in] versionId - The version id.
222 */
223 void removeReadOnlyPartition(std::string versionId);
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600224
225 /** @brief Copies U-Boot from the currently booted BMC chip to the
226 * alternate chip.
227 */
228 void mirrorUbootToAlt();
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500229};
230
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500231} // namespace updater
232} // namespace software
233} // namespace phosphor