1f2d3b53dSSantosh Puranik #pragma once
2f2d3b53dSSantosh Puranik 
3f2d3b53dSSantosh Puranik #include "types.hpp"
4f2d3b53dSSantosh Puranik 
5f2d3b53dSSantosh Puranik #include <stdint.h>
6f2d3b53dSSantosh Puranik 
7*523af2e0SSunny Srivastava #include <sdbusplus/asio/connection.hpp>
8f2d3b53dSSantosh Puranik #include <string>
9f2d3b53dSSantosh Puranik 
10f2d3b53dSSantosh Puranik namespace openpower
11f2d3b53dSSantosh Puranik {
12f2d3b53dSSantosh Puranik namespace vpd
13f2d3b53dSSantosh Puranik {
14f2d3b53dSSantosh Puranik namespace manager
15f2d3b53dSSantosh Puranik {
16f2d3b53dSSantosh Puranik 
17f2d3b53dSSantosh Puranik class Manager;
18f2d3b53dSSantosh Puranik /**
19f2d3b53dSSantosh Puranik  * @brief A class that handles changes to BIOS attributes backed by VPD.
20f2d3b53dSSantosh Puranik  *
21f2d3b53dSSantosh Puranik  * This class has APIs that handle updates to BIOS attributes that need to
22f2d3b53dSSantosh Puranik  * be backed up to VPD. It mainly does the following:
23f2d3b53dSSantosh Puranik  * 1) Checks if the VPD keywords that BIOS attributes are backed to are
24f2d3b53dSSantosh Puranik  * uninitialized. If so, it initializes them.
25f2d3b53dSSantosh Puranik  * 2) Listens for changes to BIOS attributes and synchronizes them to the
26f2d3b53dSSantosh Puranik  * appropriate VPD keyword.
27f2d3b53dSSantosh Puranik  *
28f2d3b53dSSantosh Puranik  * Since on a factory reset like scenario, the BIOS attributes are initialized
29f2d3b53dSSantosh Puranik  * by PLDM, this code waits until PLDM has grabbed a bus name before attempting
30f2d3b53dSSantosh Puranik  * any syncs.
31f2d3b53dSSantosh Puranik  */
32f2d3b53dSSantosh Puranik class BiosHandler
33f2d3b53dSSantosh Puranik {
34f2d3b53dSSantosh Puranik   public:
35f2d3b53dSSantosh Puranik     // Some default and deleted constructors and assignments.
36f2d3b53dSSantosh Puranik     BiosHandler() = delete;
37f2d3b53dSSantosh Puranik     BiosHandler(const BiosHandler&) = delete;
38f2d3b53dSSantosh Puranik     BiosHandler& operator=(const BiosHandler&) = delete;
39f2d3b53dSSantosh Puranik     BiosHandler(Manager&&) = delete;
40f2d3b53dSSantosh Puranik     BiosHandler& operator=(BiosHandler&&) = delete;
41f2d3b53dSSantosh Puranik     ~BiosHandler() = default;
42f2d3b53dSSantosh Puranik 
43*523af2e0SSunny Srivastava     BiosHandler(std::shared_ptr<sdbusplus::asio::connection>& conn,
44*523af2e0SSunny Srivastava                 Manager& manager) :
45*523af2e0SSunny Srivastava         conn(conn),
46*523af2e0SSunny Srivastava         manager(manager)
47f2d3b53dSSantosh Puranik     {
48f2d3b53dSSantosh Puranik         checkAndListenPLDMService();
49f2d3b53dSSantosh Puranik     }
50f2d3b53dSSantosh Puranik 
51f2d3b53dSSantosh Puranik   private:
52f2d3b53dSSantosh Puranik     /**
53f2d3b53dSSantosh Puranik      * @brief Check if PLDM service is running and run BIOS sync
54f2d3b53dSSantosh Puranik      *
55f2d3b53dSSantosh Puranik      * This API checks if the PLDM service is running and if yes it will start
56f2d3b53dSSantosh Puranik      * an immediate sync of BIOS attributes. If the service is not running, it
57f2d3b53dSSantosh Puranik      * registers a listener to be notified when the service starts so that a
58f2d3b53dSSantosh Puranik      * restore can be performed.
59f2d3b53dSSantosh Puranik      */
60f2d3b53dSSantosh Puranik     void checkAndListenPLDMService();
61f2d3b53dSSantosh Puranik 
62f2d3b53dSSantosh Puranik     /**
63f2d3b53dSSantosh Puranik      * @brief Register listener for changes to BIOS Attributes.
64f2d3b53dSSantosh Puranik      *
65f2d3b53dSSantosh Puranik      * The VPD manager needs to listen to changes to certain BIOS attributes
66f2d3b53dSSantosh Puranik      * that are backed by VPD. When the attributes we are interested in
67f2d3b53dSSantosh Puranik      * change, the VPD manager will make sure that we write them back to the
68f2d3b53dSSantosh Puranik      * VPD keywords that back them up.
69f2d3b53dSSantosh Puranik      */
70f2d3b53dSSantosh Puranik     void listenBiosAttribs();
71f2d3b53dSSantosh Puranik 
72f2d3b53dSSantosh Puranik     /**
73f2d3b53dSSantosh Puranik      * @brief Callback for BIOS Attribute changes
74f2d3b53dSSantosh Puranik      *
75f2d3b53dSSantosh Puranik      * Checks if the BIOS attribute(s) changed are those backed up by VPD. If
76f2d3b53dSSantosh Puranik      * yes, it will update the VPD with the new attribute value.
77f2d3b53dSSantosh Puranik      * @param[in] msg - The callback message.
78f2d3b53dSSantosh Puranik      */
792eb0176cSPatrick Williams     void biosAttribsCallback(sdbusplus::message_t& msg);
80f2d3b53dSSantosh Puranik 
81f2d3b53dSSantosh Puranik     /**
82f2d3b53dSSantosh Puranik      * @brief Persistently saves the Memory mirror mode
83f2d3b53dSSantosh Puranik      *
84f2d3b53dSSantosh Puranik      * Memory mirror mode setting is saved to the UTIL/D0 keyword in the
85f2d3b53dSSantosh Puranik      * motherboard VPD. If the mirror mode in BIOS is "Disabled", set D0 to 1,
86f2d3b53dSSantosh Puranik      * if "Enabled" set D0 to 2
87f2d3b53dSSantosh Puranik      *
88f2d3b53dSSantosh Puranik      * @param[in] mirrorMode - The mirror mode BIOS attribute.
89f2d3b53dSSantosh Puranik      */
90f2d3b53dSSantosh Puranik     void saveAMMToVPD(const std::string& mirrorMode);
91f2d3b53dSSantosh Puranik 
92f2d3b53dSSantosh Puranik     /**
93f2d3b53dSSantosh Puranik      * @brief Persistently saves the Field Core Override setting
94f2d3b53dSSantosh Puranik      *
95f2d3b53dSSantosh Puranik      * Saves the field core override value (FCO) into the VSYS/RG keyword in
96f2d3b53dSSantosh Puranik      * the motherboard VPD.
97f2d3b53dSSantosh Puranik      *
98f2d3b53dSSantosh Puranik      * @param[in] fcoVal - The FCO value as an integer.
99f2d3b53dSSantosh Puranik      */
100f2d3b53dSSantosh Puranik     void saveFCOToVPD(int64_t fcoVal);
101f2d3b53dSSantosh Puranik 
102f2d3b53dSSantosh Puranik     /**
103b2c2ccc2SSantosh Puranik      * @brief Persistently saves the Keep and Clear setting
104b2c2ccc2SSantosh Puranik      *
105b2c2ccc2SSantosh Puranik      * Keep and clear setting is saved to the UTIL/D1 keyword's 0th bit in the
106b2c2ccc2SSantosh Puranik      * motherboard VPD. If the keep and clear in BIOS is "Disabled", set D1:0 to
107b2c2ccc2SSantosh Puranik      * 0, if "Enabled" set D1:0 to 1
108b2c2ccc2SSantosh Puranik      *
109b2c2ccc2SSantosh Puranik      * @param[in] keepAndClear - The keep and clear BIOS attribute.
110b2c2ccc2SSantosh Puranik      */
111b2c2ccc2SSantosh Puranik     void saveKeepAndClearToVPD(const std::string& keepAndClear);
112b2c2ccc2SSantosh Puranik 
113b2c2ccc2SSantosh Puranik     /**
114b2c2ccc2SSantosh Puranik      * @brief Persistently saves the Create default LPAR setting
115b2c2ccc2SSantosh Puranik      *
116b2c2ccc2SSantosh Puranik      * Create default LPAR setting is saved to the UTIL/D1 keyword's 1st bit in
117b2c2ccc2SSantosh Puranik      * the motherboard VPD. If the create default LPAR in BIOS is "Disabled",
118b2c2ccc2SSantosh Puranik      * set D1:1 to 0, if "Enabled" set D1:1 to 1
119b2c2ccc2SSantosh Puranik      *
120b2c2ccc2SSantosh Puranik      * @param[in] createDefaultLpar - The mirror mode BIOS attribute.
121b2c2ccc2SSantosh Puranik      */
122b2c2ccc2SSantosh Puranik     void saveCreateDefaultLparToVPD(const std::string& createDefaultLpar);
123b2c2ccc2SSantosh Puranik 
124b2c2ccc2SSantosh Puranik     /**
125a97b63ebSSantosh Puranik      * @brief Persistently saves the Clear NVRAM setting
126a97b63ebSSantosh Puranik      *
127a97b63ebSSantosh Puranik      * Create default LPAR setting is saved to the UTIL/D1 keyword's 2nd bit in
128a97b63ebSSantosh Puranik      * the motherboard VPD. If the clear NVRAM in BIOS is "Disabled",
129a97b63ebSSantosh Puranik      * set D1:2 to 0, if "Enabled" set D1:2 to 1
130a97b63ebSSantosh Puranik      *
131a97b63ebSSantosh Puranik      * @param[in] createDefaultLpar - The mirror mode BIOS attribute.
132a97b63ebSSantosh Puranik      */
133a97b63ebSSantosh Puranik     void saveClearNVRAMToVPD(const std::string& clearNVRAM);
134a97b63ebSSantosh Puranik 
135a97b63ebSSantosh Puranik     /**
136f2d3b53dSSantosh Puranik      * @brief Writes Memory mirror mode to BIOS
137f2d3b53dSSantosh Puranik      *
138b2c2ccc2SSantosh Puranik      * Writes to the hb_memory_mirror_mode BIOS attribute, if the value is
139b2c2ccc2SSantosh Puranik      * not already the same as we are trying to write.
140f2d3b53dSSantosh Puranik      *
141f2d3b53dSSantosh Puranik      * @param[in] ammVal - The mirror mode as read from VPD.
142a97b63ebSSantosh Puranik      * @param[in] ammInBIOS - The mirror mode in the BIOS table.
143f2d3b53dSSantosh Puranik      */
144f7f8da6fSSantosh Puranik     void saveAMMToBIOS(const std::string& ammVal, const std::string& ammInBIOS);
145f2d3b53dSSantosh Puranik 
146f2d3b53dSSantosh Puranik     /**
147f2d3b53dSSantosh Puranik      * @brief Writes Field Core Override to BIOS
148f2d3b53dSSantosh Puranik      *
149f7f8da6fSSantosh Puranik      * Writes to the hb_field_core_override BIOS attribute, if the value is not
150f7f8da6fSSantosh Puranik      * already the same as we are trying to write.
151f2d3b53dSSantosh Puranik      *
152f2d3b53dSSantosh Puranik      * @param[in] fcoVal - The FCO value as read from VPD.
153f7f8da6fSSantosh Puranik      * @param[in] fcoInBIOS - The FCO value already in the BIOS table.
154f2d3b53dSSantosh Puranik      */
155f7f8da6fSSantosh Puranik     void saveFCOToBIOS(const std::string& fcoVal, int64_t fcoInBIOS);
156f2d3b53dSSantosh Puranik 
157f2d3b53dSSantosh Puranik     /**
158b2c2ccc2SSantosh Puranik      * @brief Writes Keep and clear setting to BIOS
159b2c2ccc2SSantosh Puranik      *
160b2c2ccc2SSantosh Puranik      * Writes to the pvm_keep_and_clear BIOS attribute, if the value is
161b2c2ccc2SSantosh Puranik      * not already the same as we are trying to write.
162b2c2ccc2SSantosh Puranik      *
163a97b63ebSSantosh Puranik      * @param[in] keepAndClear - The keep and clear as read from VPD.
164a97b63ebSSantosh Puranik      * @param[in] keepAndClearInBIOS - The keep and clear in the BIOS table.
165b2c2ccc2SSantosh Puranik      */
166b2c2ccc2SSantosh Puranik     void saveKeepAndClearToBIOS(const std::string& keepAndClear,
167b2c2ccc2SSantosh Puranik                                 const std::string& keepAndClearInBIOS);
168b2c2ccc2SSantosh Puranik 
169b2c2ccc2SSantosh Puranik     /**
170b2c2ccc2SSantosh Puranik      * @brief Writes Create default LPAR setting to BIOS
171b2c2ccc2SSantosh Puranik      *
172b2c2ccc2SSantosh Puranik      * Writes to the pvm_create_default_lpar BIOS attribute, if the value is
173b2c2ccc2SSantosh Puranik      * not already the same as we are trying to write.
174b2c2ccc2SSantosh Puranik      *
175a97b63ebSSantosh Puranik      * @param[in] createDefaultLpar - The create default LPAR as read from VPD.
176a97b63ebSSantosh Puranik      * @param[in] createDefaultLparInBIOS - The create default LPAR in the BIOS
177a97b63ebSSantosh Puranik      * table.
178b2c2ccc2SSantosh Puranik      */
179b2c2ccc2SSantosh Puranik     void
180b2c2ccc2SSantosh Puranik         saveCreateDefaultLparToBIOS(const std::string& createDefaultLpar,
181b2c2ccc2SSantosh Puranik                                     const std::string& createDefaultLparInBIOS);
182b2c2ccc2SSantosh Puranik 
183b2c2ccc2SSantosh Puranik     /**
184a97b63ebSSantosh Puranik      * @brief Writes Clear NVRAM setting to BIOS
185a97b63ebSSantosh Puranik      *
186a97b63ebSSantosh Puranik      * Writes to the pvm_clear_nvram BIOS attribute, if the value is
187a97b63ebSSantosh Puranik      * not already the same as we are trying to write.
188a97b63ebSSantosh Puranik      *
189a97b63ebSSantosh Puranik      * @param[in] clearNVRAM - The clear NVRAM as read from VPD.
190a97b63ebSSantosh Puranik      * @param[in] clearNVRAMInBIOS - The clear NVRAM in the BIOS table.
191a97b63ebSSantosh Puranik      */
192a97b63ebSSantosh Puranik     void saveClearNVRAMToBIOS(const std::string& clearNVRAM,
193a97b63ebSSantosh Puranik                               const std::string& clearNVRAMInBIOS);
194a97b63ebSSantosh Puranik 
195a97b63ebSSantosh Puranik     /**
196f2d3b53dSSantosh Puranik      * @brief Reads the hb_memory_mirror_mode attribute
197f2d3b53dSSantosh Puranik      *
198b2c2ccc2SSantosh Puranik      * @return std::string - The AMM BIOS attribute. Empty string on failure.
199f2d3b53dSSantosh Puranik      */
200f2d3b53dSSantosh Puranik     std::string readBIOSAMM();
201f2d3b53dSSantosh Puranik 
202f2d3b53dSSantosh Puranik     /**
203f2d3b53dSSantosh Puranik      * @brief Reads the hb_field_core_override attribute
204f2d3b53dSSantosh Puranik      *
205b2c2ccc2SSantosh Puranik      * @return int64_t - The FCO BIOS attribute.  -1 on failure.
206f2d3b53dSSantosh Puranik      */
207f2d3b53dSSantosh Puranik     int64_t readBIOSFCO();
208f2d3b53dSSantosh Puranik 
209f2d3b53dSSantosh Puranik     /**
210b2c2ccc2SSantosh Puranik      * @brief Reads the pvm_keep_and_clear attribute
211b2c2ccc2SSantosh Puranik      *
212b2c2ccc2SSantosh Puranik      * @return std::string - The Keep and clear BIOS attribute. Empty string on
213b2c2ccc2SSantosh Puranik      * failure.
214b2c2ccc2SSantosh Puranik      */
215b2c2ccc2SSantosh Puranik     std::string readBIOSKeepAndClear();
216b2c2ccc2SSantosh Puranik 
217b2c2ccc2SSantosh Puranik     /**
218b2c2ccc2SSantosh Puranik      * @brief Reads the pvm_create_default_lpar attribute
219b2c2ccc2SSantosh Puranik      *
220b2c2ccc2SSantosh Puranik      * @return std::string - The Create default LPAR BIOS attribute. Empty
221b2c2ccc2SSantosh Puranik      * string on failure.
222b2c2ccc2SSantosh Puranik      */
223b2c2ccc2SSantosh Puranik     std::string readBIOSCreateDefaultLpar();
224b2c2ccc2SSantosh Puranik 
225b2c2ccc2SSantosh Puranik     /**
226a97b63ebSSantosh Puranik      * @brief Reads the pvm_clear_nvram attribute
227a97b63ebSSantosh Puranik      *
228a97b63ebSSantosh Puranik      * @return std::string - The Clear NVRAM BIOS attribute. Empty
229a97b63ebSSantosh Puranik      * string on failure.
230a97b63ebSSantosh Puranik      */
231a97b63ebSSantosh Puranik     std::string readBIOSClearNVRAM();
232a97b63ebSSantosh Puranik 
233a97b63ebSSantosh Puranik     /**
234f2d3b53dSSantosh Puranik      * @brief Restore BIOS attributes
235f2d3b53dSSantosh Puranik      *
236f2d3b53dSSantosh Puranik      * This function checks if we are coming out of a factory reset. If yes,
237a97b63ebSSantosh Puranik      * it checks the VPD cache for valid backed up copy of the applicable
238f2d3b53dSSantosh Puranik      * BIOS attributes. If valid values are found in the VPD, it will apply
239f2d3b53dSSantosh Puranik      * those to the BIOS attributes.
240f2d3b53dSSantosh Puranik      */
241f2d3b53dSSantosh Puranik     void restoreBIOSAttribs();
242f2d3b53dSSantosh Puranik 
243f2d3b53dSSantosh Puranik     /**
244*523af2e0SSunny Srivastava      * @brief Reference to the connection.
245f2d3b53dSSantosh Puranik      */
246*523af2e0SSunny Srivastava     std::shared_ptr<sdbusplus::asio::connection>& conn;
247f2d3b53dSSantosh Puranik 
248f2d3b53dSSantosh Puranik     /**
249f2d3b53dSSantosh Puranik      * @brief Reference to the manager.
250f2d3b53dSSantosh Puranik      */
251f2d3b53dSSantosh Puranik     Manager& manager;
252f2d3b53dSSantosh Puranik }; // class BiosHandler
253f2d3b53dSSantosh Puranik } // namespace manager
254f2d3b53dSSantosh Puranik } // namespace vpd
255f2d3b53dSSantosh Puranik } // namespace openpower