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