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