1099543e4SBrad Bishop // SPDX-License-Identifier: Apache-2.0
2099543e4SBrad Bishop 
3099543e4SBrad Bishop /**@file functions.cpp*/
4099543e4SBrad Bishop 
556f538caSAdriana Kobylak #include "config.h"
656f538caSAdriana Kobylak 
7099543e4SBrad Bishop #include "functions.hpp"
8099543e4SBrad Bishop 
9ae0998f1SAdriana Kobylak #include <nlohmann/json.hpp>
1056f538caSAdriana Kobylak #include <phosphor-logging/log.hpp>
11099543e4SBrad Bishop #include <sdbusplus/bus.hpp>
12099543e4SBrad Bishop #include <sdbusplus/bus/match.hpp>
13c79fa915SAdriana Kobylak #include <sdbusplus/exception.hpp>
14099543e4SBrad Bishop #include <sdbusplus/message.hpp>
15099543e4SBrad Bishop #include <sdeventplus/event.hpp>
16*2b78eb0eSAdriana Kobylak #include <xyz/openbmc_project/Common/error.hpp>
17099543e4SBrad Bishop 
18099543e4SBrad Bishop #include <filesystem>
19ae0998f1SAdriana Kobylak #include <fstream>
20099543e4SBrad Bishop #include <functional>
21099543e4SBrad Bishop #include <iostream>
22099543e4SBrad Bishop #include <map>
23099543e4SBrad Bishop #include <memory>
24099543e4SBrad Bishop #include <string>
25099543e4SBrad Bishop #include <variant>
26099543e4SBrad Bishop #include <vector>
27099543e4SBrad Bishop 
28099543e4SBrad Bishop namespace functions
29099543e4SBrad Bishop {
30099543e4SBrad Bishop namespace process_hostfirmware
31099543e4SBrad Bishop {
32099543e4SBrad Bishop 
3356f538caSAdriana Kobylak using namespace phosphor::logging;
34ebf67bf7SAdriana Kobylak using InterfacesPropertiesMap =
35ebf67bf7SAdriana Kobylak     std::map<std::string,
36ebf67bf7SAdriana Kobylak              std::map<std::string, std::variant<std::vector<std::string>>>>;
37ebf67bf7SAdriana Kobylak using ManagedObjectType =
38ebf67bf7SAdriana Kobylak     std::map<sdbusplus::message::object_path, InterfacesPropertiesMap>;
3956f538caSAdriana Kobylak 
40099543e4SBrad Bishop /**
41ebf67bf7SAdriana Kobylak  * @brief Returns the managed objects for a given service
42ebf67bf7SAdriana Kobylak  */
43ebf67bf7SAdriana Kobylak ManagedObjectType getManagedObjects(sdbusplus::bus::bus& bus,
44ebf67bf7SAdriana Kobylak                                     const std::string& service)
45ebf67bf7SAdriana Kobylak {
46ebf67bf7SAdriana Kobylak     auto method = bus.new_method_call(service.c_str(), "/",
47ebf67bf7SAdriana Kobylak                                       "org.freedesktop.DBus.ObjectManager",
48ebf67bf7SAdriana Kobylak                                       "GetManagedObjects");
49ebf67bf7SAdriana Kobylak 
50ebf67bf7SAdriana Kobylak     ManagedObjectType objects;
51ebf67bf7SAdriana Kobylak 
52ebf67bf7SAdriana Kobylak     try
53ebf67bf7SAdriana Kobylak     {
54ebf67bf7SAdriana Kobylak         auto reply = bus.call(method);
55ebf67bf7SAdriana Kobylak         reply.read(objects);
56ebf67bf7SAdriana Kobylak     }
577b5685d1SPatrick Williams     catch (const sdbusplus::exception::exception& e)
58ebf67bf7SAdriana Kobylak     {
59ebf67bf7SAdriana Kobylak         return ManagedObjectType{};
60ebf67bf7SAdriana Kobylak     }
61ebf67bf7SAdriana Kobylak     return objects;
62ebf67bf7SAdriana Kobylak }
63ebf67bf7SAdriana Kobylak 
64ebf67bf7SAdriana Kobylak /**
65099543e4SBrad Bishop  * @brief Issue callbacks safely
66099543e4SBrad Bishop  *
67099543e4SBrad Bishop  * std::function can be empty, so this wrapper method checks for that prior to
68099543e4SBrad Bishop  * calling it to avoid std::bad_function_call
69099543e4SBrad Bishop  *
70099543e4SBrad Bishop  * @tparam Sig the types of the std::function arguments
71099543e4SBrad Bishop  * @tparam Args the deduced argument types
72099543e4SBrad Bishop  * @param[in] callback the callback being wrapped
73099543e4SBrad Bishop  * @param[in] args the callback arguments
74099543e4SBrad Bishop  */
75099543e4SBrad Bishop template <typename... Sig, typename... Args>
76099543e4SBrad Bishop void makeCallback(const std::function<void(Sig...)>& callback, Args&&... args)
77099543e4SBrad Bishop {
78099543e4SBrad Bishop     if (callback)
79099543e4SBrad Bishop     {
80099543e4SBrad Bishop         callback(std::forward<Args>(args)...);
81099543e4SBrad Bishop     }
82099543e4SBrad Bishop }
83099543e4SBrad Bishop 
84099543e4SBrad Bishop /**
85099543e4SBrad Bishop  * @brief Get file extensions for IBMCompatibleSystem
86099543e4SBrad Bishop  *
87099543e4SBrad Bishop  * IBM host firmware can be deployed as blobs (files) in a filesystem.  Host
88099543e4SBrad Bishop  * firmware blobs for different values of
89099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem are packaged with
90099543e4SBrad Bishop  * different filename extensions.  getExtensionsForIbmCompatibleSystem
91099543e4SBrad Bishop  * maintains the mapping from a given value of
92099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem to an array of
93099543e4SBrad Bishop  * filename extensions.
94099543e4SBrad Bishop  *
95099543e4SBrad Bishop  * If a mapping is found getExtensionsForIbmCompatibleSystem returns true and
96099543e4SBrad Bishop  * the extensions parameter is reset with the map entry.  If no mapping is
97099543e4SBrad Bishop  * found getExtensionsForIbmCompatibleSystem returns false and extensions is
98099543e4SBrad Bishop  * unmodified.
99099543e4SBrad Bishop  *
100099543e4SBrad Bishop  * @param[in] extensionMap a map of
101099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem to host firmware blob
102099543e4SBrad Bishop  * file extensions.
103099543e4SBrad Bishop  * @param[in] ibmCompatibleSystem The names property of an instance of
104099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem
105099543e4SBrad Bishop  * @param[out] extentions the host firmware blob file extensions
106099543e4SBrad Bishop  * @return true if an entry was found, otherwise false
107099543e4SBrad Bishop  */
108099543e4SBrad Bishop bool getExtensionsForIbmCompatibleSystem(
109099543e4SBrad Bishop     const std::map<std::string, std::vector<std::string>>& extensionMap,
110099543e4SBrad Bishop     const std::vector<std::string>& ibmCompatibleSystem,
111099543e4SBrad Bishop     std::vector<std::string>& extensions)
112099543e4SBrad Bishop {
113099543e4SBrad Bishop     for (const auto& system : ibmCompatibleSystem)
114099543e4SBrad Bishop     {
115099543e4SBrad Bishop         auto extensionMapIterator = extensionMap.find(system);
116099543e4SBrad Bishop         if (extensionMapIterator != extensionMap.end())
117099543e4SBrad Bishop         {
118099543e4SBrad Bishop             extensions = extensionMapIterator->second;
119099543e4SBrad Bishop             return true;
120099543e4SBrad Bishop         }
121099543e4SBrad Bishop     }
122099543e4SBrad Bishop 
123099543e4SBrad Bishop     return false;
124099543e4SBrad Bishop }
125099543e4SBrad Bishop 
126099543e4SBrad Bishop /**
127099543e4SBrad Bishop  * @brief Write host firmware well-known name
128099543e4SBrad Bishop  *
129099543e4SBrad Bishop  * A wrapper around std::filesystem::create_symlink that avoids EEXIST by
130099543e4SBrad Bishop  * deleting any pre-existing file.
131099543e4SBrad Bishop  *
132099543e4SBrad Bishop  * @param[in] linkTarget The link target argument to
133099543e4SBrad Bishop  * std::filesystem::create_symlink
134099543e4SBrad Bishop  * @param[in] linkPath The link path argument to std::filesystem::create_symlink
135099543e4SBrad Bishop  * @param[in] errorCallback A callback made in the event of filesystem errors.
136099543e4SBrad Bishop  */
137099543e4SBrad Bishop void writeLink(const std::filesystem::path& linkTarget,
138099543e4SBrad Bishop                const std::filesystem::path& linkPath,
139099543e4SBrad Bishop                const ErrorCallbackType& errorCallback)
140099543e4SBrad Bishop {
141099543e4SBrad Bishop     std::error_code ec;
142099543e4SBrad Bishop 
143099543e4SBrad Bishop     // remove files with the same name as the symlink to be created,
144099543e4SBrad Bishop     // otherwise symlink will fail with EEXIST.
145099543e4SBrad Bishop     if (!std::filesystem::remove(linkPath, ec))
146099543e4SBrad Bishop     {
147099543e4SBrad Bishop         if (ec)
148099543e4SBrad Bishop         {
149099543e4SBrad Bishop             makeCallback(errorCallback, linkPath, ec);
150099543e4SBrad Bishop             return;
151099543e4SBrad Bishop         }
152099543e4SBrad Bishop     }
153099543e4SBrad Bishop 
154099543e4SBrad Bishop     std::filesystem::create_symlink(linkTarget, linkPath, ec);
155099543e4SBrad Bishop     if (ec)
156099543e4SBrad Bishop     {
157099543e4SBrad Bishop         makeCallback(errorCallback, linkPath, ec);
158099543e4SBrad Bishop         return;
159099543e4SBrad Bishop     }
160099543e4SBrad Bishop }
161099543e4SBrad Bishop 
162099543e4SBrad Bishop /**
163099543e4SBrad Bishop  * @brief Find host firmware blob files that need well-known names
164099543e4SBrad Bishop  *
165099543e4SBrad Bishop  * The IBM host firmware runtime looks for data and/or additional code while
166099543e4SBrad Bishop  * bootstraping in files with well-known names.  findLinks uses the provided
167099543e4SBrad Bishop  * extensions argument to find host firmware blob files that require a
168099543e4SBrad Bishop  * well-known name.  When a blob is found, issue the provided callback
169099543e4SBrad Bishop  * (typically a function that will write a symlink).
170099543e4SBrad Bishop  *
171099543e4SBrad Bishop  * @param[in] hostFirmwareDirectory The directory in which findLinks should
172099543e4SBrad Bishop  * look for host firmware blob files that need well-known names.
173099543e4SBrad Bishop  * @param[in] extentions The extensions of the firmware blob files denote a
174099543e4SBrad Bishop  * host firmware blob file requires a well-known name.
175099543e4SBrad Bishop  * @param[in] errorCallback A callback made in the event of filesystem errors.
176099543e4SBrad Bishop  * @param[in] linkCallback A callback made when host firmware blob files
177099543e4SBrad Bishop  * needing a well known name are found.
178099543e4SBrad Bishop  */
179099543e4SBrad Bishop void findLinks(const std::filesystem::path& hostFirmwareDirectory,
180099543e4SBrad Bishop                const std::vector<std::string>& extensions,
181099543e4SBrad Bishop                const ErrorCallbackType& errorCallback,
182099543e4SBrad Bishop                const LinkCallbackType& linkCallback)
183099543e4SBrad Bishop {
184099543e4SBrad Bishop     std::error_code ec;
185099543e4SBrad Bishop     std::filesystem::directory_iterator directoryIterator(hostFirmwareDirectory,
186099543e4SBrad Bishop                                                           ec);
187099543e4SBrad Bishop     if (ec)
188099543e4SBrad Bishop     {
189099543e4SBrad Bishop         makeCallback(errorCallback, hostFirmwareDirectory, ec);
190099543e4SBrad Bishop         return;
191099543e4SBrad Bishop     }
192099543e4SBrad Bishop 
1934e82bc84SAdriana Kobylak     // Create a symlink for pnor.toc
1944e82bc84SAdriana Kobylak     static const auto tocLid = "81e00994.lid";
1954e82bc84SAdriana Kobylak     auto tocLidPath = hostFirmwareDirectory / tocLid;
1964e82bc84SAdriana Kobylak     if (std::filesystem::exists(tocLidPath))
1974e82bc84SAdriana Kobylak     {
1984e82bc84SAdriana Kobylak         static const auto tocName = "pnor.toc";
1994e82bc84SAdriana Kobylak         auto tocLinkPath = hostFirmwareDirectory / tocName;
2004e82bc84SAdriana Kobylak         makeCallback(linkCallback, tocLid, tocLinkPath, errorCallback);
2014e82bc84SAdriana Kobylak     }
2024e82bc84SAdriana Kobylak 
203099543e4SBrad Bishop     for (; directoryIterator != std::filesystem::end(directoryIterator);
204099543e4SBrad Bishop          directoryIterator.increment(ec))
205099543e4SBrad Bishop     {
206099543e4SBrad Bishop         const auto& file = directoryIterator->path();
207099543e4SBrad Bishop         if (ec)
208099543e4SBrad Bishop         {
209099543e4SBrad Bishop             makeCallback(errorCallback, file, ec);
210099543e4SBrad Bishop             // quit here if the increment call failed otherwise the loop may
211099543e4SBrad Bishop             // never finish
212099543e4SBrad Bishop             break;
213099543e4SBrad Bishop         }
214099543e4SBrad Bishop 
215099543e4SBrad Bishop         if (std::find(extensions.begin(), extensions.end(), file.extension()) ==
216099543e4SBrad Bishop             extensions.end())
217099543e4SBrad Bishop         {
218099543e4SBrad Bishop             // this file doesn't have an extension or doesn't match any of the
219099543e4SBrad Bishop             // provided extensions.
220099543e4SBrad Bishop             continue;
221099543e4SBrad Bishop         }
222099543e4SBrad Bishop 
223099543e4SBrad Bishop         auto linkPath(file.parent_path().append(
224099543e4SBrad Bishop             static_cast<const std::string&>(file.stem())));
225099543e4SBrad Bishop 
226099543e4SBrad Bishop         makeCallback(linkCallback, file.filename(), linkPath, errorCallback);
227099543e4SBrad Bishop     }
228099543e4SBrad Bishop }
229099543e4SBrad Bishop 
230099543e4SBrad Bishop /**
231ae0998f1SAdriana Kobylak  * @brief Parse the elements json file and construct a string with the data to
232ae0998f1SAdriana Kobylak  *        be used to update the bios attribute table.
233ae0998f1SAdriana Kobylak  *
234ae0998f1SAdriana Kobylak  * @param[in] elementsJsonFilePath - The path to the host firmware json file.
235ae0998f1SAdriana Kobylak  * @param[in] extensions - The extensions of the firmware blob files.
23653a27395SAdriana Kobylak  */
237ae0998f1SAdriana Kobylak std::string getBiosAttrStr(const std::filesystem::path& elementsJsonFilePath,
238ae0998f1SAdriana Kobylak                            const std::vector<std::string>& extensions)
23956f538caSAdriana Kobylak {
24056f538caSAdriana Kobylak     std::string biosAttrStr{};
24156f538caSAdriana Kobylak 
242ae0998f1SAdriana Kobylak     std::ifstream jsonFile(elementsJsonFilePath.c_str());
243ae0998f1SAdriana Kobylak     if (!jsonFile)
244ae0998f1SAdriana Kobylak     {
245ae0998f1SAdriana Kobylak         return {};
246ae0998f1SAdriana Kobylak     }
247ae0998f1SAdriana Kobylak 
248ae0998f1SAdriana Kobylak     std::map<std::string, std::string> attr;
249ae0998f1SAdriana Kobylak     auto data = nlohmann::json::parse(jsonFile, nullptr, false);
250ae0998f1SAdriana Kobylak     if (data.is_discarded())
251ae0998f1SAdriana Kobylak     {
252ae0998f1SAdriana Kobylak         log<level::ERR>("Error parsing JSON file",
253ae0998f1SAdriana Kobylak                         entry("FILE=%s", elementsJsonFilePath.c_str()));
254ae0998f1SAdriana Kobylak         return {};
255ae0998f1SAdriana Kobylak     }
256ae0998f1SAdriana Kobylak 
257ae0998f1SAdriana Kobylak     // .get requires a non-const iterator
258ae0998f1SAdriana Kobylak     for (auto& iter : data["lids"])
259ae0998f1SAdriana Kobylak     {
260ae0998f1SAdriana Kobylak         std::string name{};
261ae0998f1SAdriana Kobylak         std::string lid{};
262ae0998f1SAdriana Kobylak 
263ae0998f1SAdriana Kobylak         try
264ae0998f1SAdriana Kobylak         {
265ae0998f1SAdriana Kobylak             name = iter["element_name"].get<std::string>();
266ae0998f1SAdriana Kobylak             lid = iter["short_lid_name"].get<std::string>();
267ae0998f1SAdriana Kobylak         }
26897a709b0SPatrick Williams         catch (const std::exception& e)
269ae0998f1SAdriana Kobylak         {
270ae0998f1SAdriana Kobylak             // Possibly the element or lid name field was not found
271ae0998f1SAdriana Kobylak             log<level::ERR>("Error reading JSON field",
272ae0998f1SAdriana Kobylak                             entry("FILE=%s", elementsJsonFilePath.c_str()),
273ae0998f1SAdriana Kobylak                             entry("ERROR=%s", e.what()));
274ae0998f1SAdriana Kobylak             continue;
275ae0998f1SAdriana Kobylak         }
276ae0998f1SAdriana Kobylak 
277ae0998f1SAdriana Kobylak         // The elements with the ipl extension have higher priority. Therefore
2789cbc06b1SAdriana Kobylak         // Use operator[] to overwrite value if an entry for it already exists,
2799cbc06b1SAdriana Kobylak         // and create a second entry with key name element_RT to specify it as
2809cbc06b1SAdriana Kobylak         // a runtime element.
2819cbc06b1SAdriana Kobylak         // Ex: if the JSON contains an entry A.P10 with lid name X, it'll create
2829cbc06b1SAdriana Kobylak         // and try A=X. If the JSON also contained an entry A.P10.iplTime with
2839cbc06b1SAdriana Kobylak         // lid name Y, the A entry would be overwritten to be A=Y and a second
2849cbc06b1SAdriana Kobylak         // entry A_RT=X would be created.
285ae0998f1SAdriana Kobylak         constexpr auto iplExtension = ".iplTime";
2869cbc06b1SAdriana Kobylak         constexpr auto runtimeSuffix = "_RT";
287ae0998f1SAdriana Kobylak         std::filesystem::path path(name);
288ae0998f1SAdriana Kobylak         if (path.extension() == iplExtension)
289ae0998f1SAdriana Kobylak         {
290ae0998f1SAdriana Kobylak             // Some elements have an additional extension, ex: .P10.iplTime
291ae0998f1SAdriana Kobylak             // Strip off the ipl extension with stem(), then check if there is
292ae0998f1SAdriana Kobylak             // an additional extension with extension().
293ae0998f1SAdriana Kobylak             if (!path.stem().extension().empty())
294ae0998f1SAdriana Kobylak             {
295ae0998f1SAdriana Kobylak                 // Check if the extension matches the extensions for this system
296ae0998f1SAdriana Kobylak                 if (std::find(extensions.begin(), extensions.end(),
297ae0998f1SAdriana Kobylak                               path.stem().extension()) == extensions.end())
298ae0998f1SAdriana Kobylak                 {
299ae0998f1SAdriana Kobylak                     continue;
300ae0998f1SAdriana Kobylak                 }
301ae0998f1SAdriana Kobylak             }
302ae0998f1SAdriana Kobylak             // Get the element name without extensions by calling stem() twice
303ae0998f1SAdriana Kobylak             // since stem() returns the base name if no periods are found.
304ae0998f1SAdriana Kobylak             // Therefore both "element.P10" and "element.P10.iplTime" would
305ae0998f1SAdriana Kobylak             // become "element".
3069cbc06b1SAdriana Kobylak             auto keyName = path.stem().stem();
3079cbc06b1SAdriana Kobylak             auto attrIt = attr.find(keyName);
3089cbc06b1SAdriana Kobylak             if (attrIt != attr.end())
3099cbc06b1SAdriana Kobylak             {
3109cbc06b1SAdriana Kobylak                 // Copy the existing entry to a runtime entry
3119cbc06b1SAdriana Kobylak                 auto runtimeKeyName = keyName.string() + runtimeSuffix;
3129cbc06b1SAdriana Kobylak                 attr.insert({runtimeKeyName, attrIt->second});
3139cbc06b1SAdriana Kobylak             }
3149cbc06b1SAdriana Kobylak             // Overwrite the exsiting element with the ipl entry
3159cbc06b1SAdriana Kobylak             attr[keyName] = lid;
316ae0998f1SAdriana Kobylak             continue;
317ae0998f1SAdriana Kobylak         }
318ae0998f1SAdriana Kobylak 
319ae0998f1SAdriana Kobylak         // Process all other extensions. The extension should match the list of
320ae0998f1SAdriana Kobylak         // supported extensions for this system. Use .insert() to only add
321ae0998f1SAdriana Kobylak         // entries that do not exist, so to not overwrite the values that may
322ae0998f1SAdriana Kobylak         // had been added that had the ipl extension.
323ae0998f1SAdriana Kobylak         if (std::find(extensions.begin(), extensions.end(), path.extension()) !=
324ae0998f1SAdriana Kobylak             extensions.end())
325ae0998f1SAdriana Kobylak         {
3269cbc06b1SAdriana Kobylak             auto keyName = path.stem();
3279cbc06b1SAdriana Kobylak             auto attrIt = attr.find(keyName);
3289cbc06b1SAdriana Kobylak             if (attrIt != attr.end())
3299cbc06b1SAdriana Kobylak             {
3309cbc06b1SAdriana Kobylak                 // The existing entry is an ipl entry, therefore create this
3319cbc06b1SAdriana Kobylak                 // entry as a runtime one.
3329cbc06b1SAdriana Kobylak                 auto runtimeKeyName = keyName.string() + runtimeSuffix;
3339cbc06b1SAdriana Kobylak                 attr.insert({runtimeKeyName, lid});
3349cbc06b1SAdriana Kobylak             }
3359cbc06b1SAdriana Kobylak             else
3369cbc06b1SAdriana Kobylak             {
337ae0998f1SAdriana Kobylak                 attr.insert({path.stem(), lid});
338ae0998f1SAdriana Kobylak             }
339ae0998f1SAdriana Kobylak         }
3409cbc06b1SAdriana Kobylak     }
341ae0998f1SAdriana Kobylak     for (const auto& a : attr)
342ae0998f1SAdriana Kobylak     {
343ae0998f1SAdriana Kobylak         // Build the bios attribute string with format:
344ae0998f1SAdriana Kobylak         // "element1=lid1,element2=lid2,elementN=lidN,"
345ae0998f1SAdriana Kobylak         biosAttrStr += a.first + "=" + a.second + ",";
3465dc5d6ccSAdriana Kobylak 
3475dc5d6ccSAdriana Kobylak         // Create symlinks from the hostfw elements to their corresponding
3485dc5d6ccSAdriana Kobylak         // lid files if they don't exist
3495dc5d6ccSAdriana Kobylak         auto elementFilePath =
3505dc5d6ccSAdriana Kobylak             std::filesystem::path("/media/hostfw/running") / a.first;
3515dc5d6ccSAdriana Kobylak         if (!std::filesystem::exists(elementFilePath))
3525dc5d6ccSAdriana Kobylak         {
3535dc5d6ccSAdriana Kobylak             std::error_code ec;
3545dc5d6ccSAdriana Kobylak             auto lidName = a.second + ".lid";
3555dc5d6ccSAdriana Kobylak             std::filesystem::create_symlink(lidName, elementFilePath, ec);
3565dc5d6ccSAdriana Kobylak             if (ec)
3575dc5d6ccSAdriana Kobylak             {
3585dc5d6ccSAdriana Kobylak                 log<level::ERR>("Error creating symlink",
3595dc5d6ccSAdriana Kobylak                                 entry("TARGET=%s", lidName.c_str()),
3605dc5d6ccSAdriana Kobylak                                 entry("LINK=%s", elementFilePath.c_str()));
3615dc5d6ccSAdriana Kobylak             }
3625dc5d6ccSAdriana Kobylak         }
363ae0998f1SAdriana Kobylak     }
364ae0998f1SAdriana Kobylak 
365a38f6e65SGeorge Liu     // Delete the last comma of the bios attribute string
366a38f6e65SGeorge Liu     if (biosAttrStr.back() == ',')
367a38f6e65SGeorge Liu     {
368a38f6e65SGeorge Liu         return biosAttrStr.substr(0, biosAttrStr.length() - 1);
369a38f6e65SGeorge Liu     }
370a38f6e65SGeorge Liu 
371ae0998f1SAdriana Kobylak     return biosAttrStr;
372ae0998f1SAdriana Kobylak }
373ae0998f1SAdriana Kobylak 
374ae0998f1SAdriana Kobylak /**
375ae0998f1SAdriana Kobylak  * @brief Set the bios attribute table with details of the host firmware data
376ae0998f1SAdriana Kobylak  * for this system.
377ae0998f1SAdriana Kobylak  *
378ae0998f1SAdriana Kobylak  * @param[in] elementsJsonFilePath - The path to the host firmware json file.
379ae0998f1SAdriana Kobylak  * @param[in] extentions - The extensions of the firmware blob files.
380ae0998f1SAdriana Kobylak  */
381ae0998f1SAdriana Kobylak void setBiosAttr(const std::filesystem::path& elementsJsonFilePath,
382ae0998f1SAdriana Kobylak                  const std::vector<std::string>& extensions)
383ae0998f1SAdriana Kobylak {
384ae0998f1SAdriana Kobylak     auto biosAttrStr = getBiosAttrStr(elementsJsonFilePath, extensions);
385ae0998f1SAdriana Kobylak 
38656f538caSAdriana Kobylak     constexpr auto biosConfigPath = "/xyz/openbmc_project/bios_config/manager";
38756f538caSAdriana Kobylak     constexpr auto biosConfigIntf = "xyz.openbmc_project.BIOSConfig.Manager";
38856f538caSAdriana Kobylak     constexpr auto dbusAttrName = "hb_lid_ids";
38956f538caSAdriana Kobylak     constexpr auto dbusAttrType =
39056f538caSAdriana Kobylak         "xyz.openbmc_project.BIOSConfig.Manager.AttributeType.String";
39156f538caSAdriana Kobylak 
39256f538caSAdriana Kobylak     using PendingAttributesType = std::vector<std::pair<
39356f538caSAdriana Kobylak         std::string, std::tuple<std::string, std::variant<std::string>>>>;
39456f538caSAdriana Kobylak     PendingAttributesType pendingAttributes;
39556f538caSAdriana Kobylak     pendingAttributes.emplace_back(std::make_pair(
39656f538caSAdriana Kobylak         dbusAttrName, std::make_tuple(dbusAttrType, biosAttrStr)));
39756f538caSAdriana Kobylak 
39856f538caSAdriana Kobylak     auto bus = sdbusplus::bus::new_default();
39956f538caSAdriana Kobylak     auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
40056f538caSAdriana Kobylak                                       MAPPER_INTERFACE, "GetObject");
40156f538caSAdriana Kobylak     method.append(biosConfigPath, std::vector<std::string>({biosConfigIntf}));
40256f538caSAdriana Kobylak     std::vector<std::pair<std::string, std::vector<std::string>>> response;
40356f538caSAdriana Kobylak     try
40456f538caSAdriana Kobylak     {
40556f538caSAdriana Kobylak         auto reply = bus.call(method);
40656f538caSAdriana Kobylak         reply.read(response);
40756f538caSAdriana Kobylak         if (response.empty())
40856f538caSAdriana Kobylak         {
409*2b78eb0eSAdriana Kobylak             log<level::INFO>("Error reading mapper response",
41056f538caSAdriana Kobylak                              entry("PATH=%s", biosConfigPath),
41156f538caSAdriana Kobylak                              entry("INTERFACE=%s", biosConfigIntf));
412*2b78eb0eSAdriana Kobylak             throw sdbusplus::xyz::openbmc_project::Common::Error::
413*2b78eb0eSAdriana Kobylak                 InternalFailure();
41456f538caSAdriana Kobylak         }
41556f538caSAdriana Kobylak         auto method = bus.new_method_call((response.begin()->first).c_str(),
41656f538caSAdriana Kobylak                                           biosConfigPath,
41756f538caSAdriana Kobylak                                           SYSTEMD_PROPERTY_INTERFACE, "Set");
41856f538caSAdriana Kobylak         method.append(biosConfigIntf, "PendingAttributes",
41956f538caSAdriana Kobylak                       std::variant<PendingAttributesType>(pendingAttributes));
42056f538caSAdriana Kobylak         bus.call(method);
42156f538caSAdriana Kobylak     }
4227b5685d1SPatrick Williams     catch (const sdbusplus::exception::exception& e)
42356f538caSAdriana Kobylak     {
424*2b78eb0eSAdriana Kobylak         log<level::INFO>("Error setting the bios attribute",
42556f538caSAdriana Kobylak                          entry("ERROR=%s", e.what()),
42656f538caSAdriana Kobylak                          entry("ATTRIBUTE=%s", dbusAttrName));
427*2b78eb0eSAdriana Kobylak         throw;
42856f538caSAdriana Kobylak     }
42956f538caSAdriana Kobylak }
43053a27395SAdriana Kobylak 
43153a27395SAdriana Kobylak /**
432099543e4SBrad Bishop  * @brief Make callbacks on
433099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem instances.
434099543e4SBrad Bishop  *
435099543e4SBrad Bishop  * Look for an instance of
436099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem in the provided
437099543e4SBrad Bishop  * argument and if found, issue the provided callback.
438099543e4SBrad Bishop  *
439099543e4SBrad Bishop  * @param[in] interfacesAndProperties the interfaces in which to look for an
440099543e4SBrad Bishop  * instance of xyz.openbmc_project.Configuration.IBMCompatibleSystem
441099543e4SBrad Bishop  * @param[in] callback the user callback to make if
442099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem is found in
443099543e4SBrad Bishop  * interfacesAndProperties
444099543e4SBrad Bishop  * @return true if interfacesAndProperties contained an instance of
445099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem, false otherwise
446099543e4SBrad Bishop  */
447099543e4SBrad Bishop bool maybeCall(const std::map<std::string,
448099543e4SBrad Bishop                               std::map<std::string,
449099543e4SBrad Bishop                                        std::variant<std::vector<std::string>>>>&
450099543e4SBrad Bishop                    interfacesAndProperties,
451099543e4SBrad Bishop                const MaybeCallCallbackType& callback)
452099543e4SBrad Bishop {
453099543e4SBrad Bishop     using namespace std::string_literals;
454099543e4SBrad Bishop 
455099543e4SBrad Bishop     static const auto interfaceName =
456099543e4SBrad Bishop         "xyz.openbmc_project.Configuration.IBMCompatibleSystem"s;
457099543e4SBrad Bishop     auto interfaceIterator = interfacesAndProperties.find(interfaceName);
458099543e4SBrad Bishop     if (interfaceIterator == interfacesAndProperties.cend())
459099543e4SBrad Bishop     {
460099543e4SBrad Bishop         // IBMCompatibleSystem interface not found, so instruct the caller to
461099543e4SBrad Bishop         // keep waiting or try again later.
462099543e4SBrad Bishop         return false;
463099543e4SBrad Bishop     }
464099543e4SBrad Bishop     auto propertyIterator = interfaceIterator->second.find("Names"s);
465099543e4SBrad Bishop     if (propertyIterator == interfaceIterator->second.cend())
466099543e4SBrad Bishop     {
467099543e4SBrad Bishop         // The interface exists but the property doesn't.  This is a bug in the
468099543e4SBrad Bishop         // IBMCompatibleSystem implementation.  The caller should not try
469099543e4SBrad Bishop         // again.
470099543e4SBrad Bishop         std::cerr << "Names property not implemented on " << interfaceName
471099543e4SBrad Bishop                   << "\n";
472099543e4SBrad Bishop         return true;
473099543e4SBrad Bishop     }
474099543e4SBrad Bishop 
475099543e4SBrad Bishop     const auto& ibmCompatibleSystem =
476099543e4SBrad Bishop         std::get<std::vector<std::string>>(propertyIterator->second);
477099543e4SBrad Bishop     if (callback)
478099543e4SBrad Bishop     {
479*2b78eb0eSAdriana Kobylak         try
480*2b78eb0eSAdriana Kobylak         {
481099543e4SBrad Bishop             callback(ibmCompatibleSystem);
482099543e4SBrad Bishop         }
483*2b78eb0eSAdriana Kobylak         catch (const sdbusplus::exception::exception& e)
484*2b78eb0eSAdriana Kobylak         {
485*2b78eb0eSAdriana Kobylak             return false;
486*2b78eb0eSAdriana Kobylak         }
487*2b78eb0eSAdriana Kobylak     }
488099543e4SBrad Bishop 
489099543e4SBrad Bishop     // IBMCompatibleSystem found and callback issued.
490099543e4SBrad Bishop     return true;
491099543e4SBrad Bishop }
492099543e4SBrad Bishop 
493099543e4SBrad Bishop /**
494099543e4SBrad Bishop  * @brief Make callbacks on
495099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem instances.
496099543e4SBrad Bishop  *
497099543e4SBrad Bishop  * Look for an instance of
498099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem in the provided
499099543e4SBrad Bishop  * argument and if found, issue the provided callback.
500099543e4SBrad Bishop  *
501099543e4SBrad Bishop  * @param[in] message the DBus message in which to look for an instance of
502099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem
503099543e4SBrad Bishop  * @param[in] callback the user callback to make if
504099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem is found in
505099543e4SBrad Bishop  * message
506099543e4SBrad Bishop  * @return true if message contained an instance of
507099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem, false otherwise
508099543e4SBrad Bishop  */
509099543e4SBrad Bishop bool maybeCallMessage(sdbusplus::message::message& message,
510099543e4SBrad Bishop                       const MaybeCallCallbackType& callback)
511099543e4SBrad Bishop {
512099543e4SBrad Bishop     std::map<std::string,
513099543e4SBrad Bishop              std::map<std::string, std::variant<std::vector<std::string>>>>
514099543e4SBrad Bishop         interfacesAndProperties;
515099543e4SBrad Bishop     sdbusplus::message::object_path _;
516099543e4SBrad Bishop     message.read(_, interfacesAndProperties);
517099543e4SBrad Bishop     return maybeCall(interfacesAndProperties, callback);
518099543e4SBrad Bishop }
519099543e4SBrad Bishop 
520099543e4SBrad Bishop /**
521099543e4SBrad Bishop  * @brief Determine system support for host firmware well-known names.
522099543e4SBrad Bishop  *
523099543e4SBrad Bishop  * Using the provided extensionMap and
524099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem, determine if
525099543e4SBrad Bishop  * well-known names for host firmare blob files are necessary and if so, create
526099543e4SBrad Bishop  * them.
527099543e4SBrad Bishop  *
528099543e4SBrad Bishop  * @param[in] extensionMap a map of
529099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem to host firmware blob
530099543e4SBrad Bishop  * file extensions.
531099543e4SBrad Bishop  * @param[in] hostFirmwareDirectory The directory in which findLinks should
532099543e4SBrad Bishop  * look for host firmware blob files that need well-known names.
533099543e4SBrad Bishop  * @param[in] ibmCompatibleSystem The names property of an instance of
534099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem
535099543e4SBrad Bishop  * @param[in] errorCallback A callback made in the event of filesystem errors.
536099543e4SBrad Bishop  */
537099543e4SBrad Bishop void maybeMakeLinks(
538099543e4SBrad Bishop     const std::map<std::string, std::vector<std::string>>& extensionMap,
539099543e4SBrad Bishop     const std::filesystem::path& hostFirmwareDirectory,
540099543e4SBrad Bishop     const std::vector<std::string>& ibmCompatibleSystem,
541099543e4SBrad Bishop     const ErrorCallbackType& errorCallback)
542099543e4SBrad Bishop {
543099543e4SBrad Bishop     std::vector<std::string> extensions;
544099543e4SBrad Bishop     if (getExtensionsForIbmCompatibleSystem(extensionMap, ibmCompatibleSystem,
545099543e4SBrad Bishop                                             extensions))
546099543e4SBrad Bishop     {
547099543e4SBrad Bishop         findLinks(hostFirmwareDirectory, extensions, errorCallback, writeLink);
548099543e4SBrad Bishop     }
549099543e4SBrad Bishop }
550099543e4SBrad Bishop 
551099543e4SBrad Bishop /**
55253a27395SAdriana Kobylak  * @brief Determine system support for updating the bios attribute table.
55353a27395SAdriana Kobylak  *
55453a27395SAdriana Kobylak  * Using the provided extensionMap and
55553a27395SAdriana Kobylak  * xyz.openbmc_project.Configuration.IBMCompatibleSystem, determine if the bios
55653a27395SAdriana Kobylak  * attribute table needs to be updated.
55753a27395SAdriana Kobylak  *
55853a27395SAdriana Kobylak  * @param[in] extensionMap a map of
55953a27395SAdriana Kobylak  * xyz.openbmc_project.Configuration.IBMCompatibleSystem to host firmware blob
56053a27395SAdriana Kobylak  * file extensions.
561ae0998f1SAdriana Kobylak  * @param[in] elementsJsonFilePath The file path to the json file
56253a27395SAdriana Kobylak  * @param[in] ibmCompatibleSystem The names property of an instance of
56353a27395SAdriana Kobylak  * xyz.openbmc_project.Configuration.IBMCompatibleSystem
56453a27395SAdriana Kobylak  */
56553a27395SAdriana Kobylak void maybeSetBiosAttr(
56653a27395SAdriana Kobylak     const std::map<std::string, std::vector<std::string>>& extensionMap,
567ae0998f1SAdriana Kobylak     const std::filesystem::path& elementsJsonFilePath,
56853a27395SAdriana Kobylak     const std::vector<std::string>& ibmCompatibleSystem)
56953a27395SAdriana Kobylak {
57053a27395SAdriana Kobylak     std::vector<std::string> extensions;
57153a27395SAdriana Kobylak     if (getExtensionsForIbmCompatibleSystem(extensionMap, ibmCompatibleSystem,
57253a27395SAdriana Kobylak                                             extensions))
57353a27395SAdriana Kobylak     {
574*2b78eb0eSAdriana Kobylak         try
575*2b78eb0eSAdriana Kobylak         {
576ae0998f1SAdriana Kobylak             setBiosAttr(elementsJsonFilePath, extensions);
57753a27395SAdriana Kobylak         }
578*2b78eb0eSAdriana Kobylak         catch (const sdbusplus::exception::exception& e)
579*2b78eb0eSAdriana Kobylak         {
580*2b78eb0eSAdriana Kobylak             throw;
581*2b78eb0eSAdriana Kobylak         }
582*2b78eb0eSAdriana Kobylak     }
58353a27395SAdriana Kobylak }
58453a27395SAdriana Kobylak 
58553a27395SAdriana Kobylak /**
586099543e4SBrad Bishop  * @brief process host firmware
587099543e4SBrad Bishop  *
588099543e4SBrad Bishop  * Allocate a callback context and register for DBus.ObjectManager Interfaces
589099543e4SBrad Bishop  * added signals from entity manager.
590099543e4SBrad Bishop  *
591099543e4SBrad Bishop  * Check the current entity manager object tree for a
592099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem instance (entity
593099543e4SBrad Bishop  * manager will be dbus activated if it is not running).  If one is found,
594099543e4SBrad Bishop  * determine if symlinks need to be created and create them.  Instruct the
595099543e4SBrad Bishop  * program event loop to exit.
596099543e4SBrad Bishop  *
597099543e4SBrad Bishop  * If no instance of xyz.openbmc_project.Configuration.IBMCompatibleSystem is
598099543e4SBrad Bishop  * found return the callback context to main, where the program will sleep
599099543e4SBrad Bishop  * until the callback is invoked one or more times and instructs the program
600099543e4SBrad Bishop  * event loop to exit when
601099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem is added.
602099543e4SBrad Bishop  *
603099543e4SBrad Bishop  * @param[in] bus a DBus client connection
604099543e4SBrad Bishop  * @param[in] extensionMap a map of
605099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem to host firmware blob
606099543e4SBrad Bishop  * file extensions.
607099543e4SBrad Bishop  * @param[in] hostFirmwareDirectory The directory in which processHostFirmware
608099543e4SBrad Bishop  * should look for blob files.
609099543e4SBrad Bishop  * @param[in] errorCallback A callback made in the event of filesystem errors.
610099543e4SBrad Bishop  * @param[in] loop a program event loop
611099543e4SBrad Bishop  * @return nullptr if an instance of
612099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem is found, otherwise a
613099543e4SBrad Bishop  * pointer to an sdbusplus match object.
614099543e4SBrad Bishop  */
615099543e4SBrad Bishop std::shared_ptr<void> processHostFirmware(
616099543e4SBrad Bishop     sdbusplus::bus::bus& bus,
617099543e4SBrad Bishop     std::map<std::string, std::vector<std::string>> extensionMap,
618099543e4SBrad Bishop     std::filesystem::path hostFirmwareDirectory,
619099543e4SBrad Bishop     ErrorCallbackType errorCallback, sdeventplus::Event& loop)
620099543e4SBrad Bishop {
621099543e4SBrad Bishop     // ownership of extensionMap, hostFirmwareDirectory and errorCallback can't
622099543e4SBrad Bishop     // be transfered to the match callback because they are needed in the non
623099543e4SBrad Bishop     // async part of this function below, so they need to be moved to the heap.
624099543e4SBrad Bishop     auto pExtensionMap =
625099543e4SBrad Bishop         std::make_shared<decltype(extensionMap)>(std::move(extensionMap));
626099543e4SBrad Bishop     auto pHostFirmwareDirectory =
627099543e4SBrad Bishop         std::make_shared<decltype(hostFirmwareDirectory)>(
628099543e4SBrad Bishop             std::move(hostFirmwareDirectory));
629099543e4SBrad Bishop     auto pErrorCallback =
630099543e4SBrad Bishop         std::make_shared<decltype(errorCallback)>(std::move(errorCallback));
631099543e4SBrad Bishop 
632099543e4SBrad Bishop     // register for a callback in case the IBMCompatibleSystem interface has
633099543e4SBrad Bishop     // not yet been published by entity manager.
634099543e4SBrad Bishop     auto interfacesAddedMatch = std::make_shared<sdbusplus::bus::match::match>(
635099543e4SBrad Bishop         bus,
636099543e4SBrad Bishop         sdbusplus::bus::match::rules::interfacesAdded() +
637099543e4SBrad Bishop             sdbusplus::bus::match::rules::sender(
638099543e4SBrad Bishop                 "xyz.openbmc_project.EntityManager"),
639099543e4SBrad Bishop         [pExtensionMap, pHostFirmwareDirectory, pErrorCallback,
640099543e4SBrad Bishop          &loop](auto& message) {
641099543e4SBrad Bishop             // bind the extension map, host firmware directory, and error
642099543e4SBrad Bishop             // callback to the maybeMakeLinks function.
643099543e4SBrad Bishop             auto maybeMakeLinksWithArgsBound =
644099543e4SBrad Bishop                 std::bind(maybeMakeLinks, std::cref(*pExtensionMap),
645099543e4SBrad Bishop                           std::cref(*pHostFirmwareDirectory),
646099543e4SBrad Bishop                           std::placeholders::_1, std::cref(*pErrorCallback));
647099543e4SBrad Bishop 
648099543e4SBrad Bishop             // if the InterfacesAdded message contains an an instance of
649099543e4SBrad Bishop             // xyz.openbmc_project.Configuration.IBMCompatibleSystem, check to
650099543e4SBrad Bishop             // see if links are necessary on this system and if so, create
651099543e4SBrad Bishop             // them.
652099543e4SBrad Bishop             if (maybeCallMessage(message, maybeMakeLinksWithArgsBound))
653099543e4SBrad Bishop             {
654099543e4SBrad Bishop                 // The IBMCompatibleSystem interface was found and the links
655099543e4SBrad Bishop                 // were created if applicable.  Instruct the event loop /
656099543e4SBrad Bishop                 // subcommand to exit.
657099543e4SBrad Bishop                 loop.exit(0);
658099543e4SBrad Bishop             }
659099543e4SBrad Bishop         });
660099543e4SBrad Bishop 
661099543e4SBrad Bishop     // now that we'll get a callback in the event of an InterfacesAdded signal
662099543e4SBrad Bishop     // (potentially containing
663099543e4SBrad Bishop     // xyz.openbmc_project.Configuration.IBMCompatibleSystem), activate entity
664099543e4SBrad Bishop     // manager if it isn't running and enumerate its objects
665099543e4SBrad Bishop     auto getManagedObjects = bus.new_method_call(
666099543e4SBrad Bishop         "xyz.openbmc_project.EntityManager", "/",
667099543e4SBrad Bishop         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
668099543e4SBrad Bishop     std::map<std::string,
669099543e4SBrad Bishop              std::map<std::string, std::variant<std::vector<std::string>>>>
670099543e4SBrad Bishop         interfacesAndProperties;
671099543e4SBrad Bishop     std::map<sdbusplus::message::object_path, decltype(interfacesAndProperties)>
672099543e4SBrad Bishop         objects;
673c79fa915SAdriana Kobylak     try
674c79fa915SAdriana Kobylak     {
675c79fa915SAdriana Kobylak         auto reply = bus.call(getManagedObjects);
676099543e4SBrad Bishop         reply.read(objects);
677c79fa915SAdriana Kobylak     }
6787b5685d1SPatrick Williams     catch (const sdbusplus::exception::exception& e)
679c79fa915SAdriana Kobylak     {
680c79fa915SAdriana Kobylak         // Error querying the EntityManager interface. Return the match to have
681c79fa915SAdriana Kobylak         // the callback run if/when the interface appears in D-Bus.
682c79fa915SAdriana Kobylak         return interfacesAddedMatch;
683c79fa915SAdriana Kobylak     }
684099543e4SBrad Bishop 
685099543e4SBrad Bishop     // bind the extension map, host firmware directory, and error callback to
686099543e4SBrad Bishop     // the maybeMakeLinks function.
687099543e4SBrad Bishop     auto maybeMakeLinksWithArgsBound =
688099543e4SBrad Bishop         std::bind(maybeMakeLinks, std::cref(*pExtensionMap),
689099543e4SBrad Bishop                   std::cref(*pHostFirmwareDirectory), std::placeholders::_1,
690099543e4SBrad Bishop                   std::cref(*pErrorCallback));
691099543e4SBrad Bishop 
692099543e4SBrad Bishop     for (const auto& pair : objects)
693099543e4SBrad Bishop     {
694099543e4SBrad Bishop         std::tie(std::ignore, interfacesAndProperties) = pair;
695099543e4SBrad Bishop         // if interfacesAndProperties contains an an instance of
696099543e4SBrad Bishop         // xyz.openbmc_project.Configuration.IBMCompatibleSystem, check to see
697099543e4SBrad Bishop         // if links are necessary on this system and if so, create them
698099543e4SBrad Bishop         if (maybeCall(interfacesAndProperties, maybeMakeLinksWithArgsBound))
699099543e4SBrad Bishop         {
700099543e4SBrad Bishop             // The IBMCompatibleSystem interface is already on the bus and the
701099543e4SBrad Bishop             // links were created if applicable.  Instruct the event loop to
702099543e4SBrad Bishop             // exit.
703099543e4SBrad Bishop             loop.exit(0);
704099543e4SBrad Bishop             // The match object isn't needed anymore, so destroy it on return.
705099543e4SBrad Bishop             return nullptr;
706099543e4SBrad Bishop         }
707099543e4SBrad Bishop     }
708099543e4SBrad Bishop 
709099543e4SBrad Bishop     // The IBMCompatibleSystem interface has not yet been published.  Move
710099543e4SBrad Bishop     // ownership of the match callback to the caller.
711099543e4SBrad Bishop     return interfacesAddedMatch;
712099543e4SBrad Bishop }
71353a27395SAdriana Kobylak 
71453a27395SAdriana Kobylak /**
71553a27395SAdriana Kobylak  * @brief Update the Bios Attribute Table
71653a27395SAdriana Kobylak  *
71753a27395SAdriana Kobylak  * If an instance of xyz.openbmc_project.Configuration.IBMCompatibleSystem is
71853a27395SAdriana Kobylak  * found, update the Bios Attribute Table with the appropriate host firmware
71953a27395SAdriana Kobylak  * data.
72053a27395SAdriana Kobylak  *
72153a27395SAdriana Kobylak  * @param[in] bus - D-Bus client connection.
72253a27395SAdriana Kobylak  * @param[in] extensionMap - Map of IBMCompatibleSystem names and host firmware
72353a27395SAdriana Kobylak  *                           file extensions.
724ae0998f1SAdriana Kobylak  * @param[in] elementsJsonFilePath - The Path to the json file
72553a27395SAdriana Kobylak  * @param[in] loop - Program event loop.
72653a27395SAdriana Kobylak  * @return nullptr
72753a27395SAdriana Kobylak  */
728ebf67bf7SAdriana Kobylak std::vector<std::shared_ptr<void>> updateBiosAttrTable(
72953a27395SAdriana Kobylak     sdbusplus::bus::bus& bus,
73053a27395SAdriana Kobylak     std::map<std::string, std::vector<std::string>> extensionMap,
731ae0998f1SAdriana Kobylak     std::filesystem::path elementsJsonFilePath, sdeventplus::Event& loop)
73253a27395SAdriana Kobylak {
733d0379ea5SAdriana Kobylak     constexpr auto pldmPath = "/xyz/openbmc_project/pldm";
734ebf67bf7SAdriana Kobylak     constexpr auto entityManagerServiceName =
735ebf67bf7SAdriana Kobylak         "xyz.openbmc_project.EntityManager";
736d0379ea5SAdriana Kobylak 
73753a27395SAdriana Kobylak     auto pExtensionMap =
73853a27395SAdriana Kobylak         std::make_shared<decltype(extensionMap)>(std::move(extensionMap));
739ae0998f1SAdriana Kobylak     auto pElementsJsonFilePath =
740ae0998f1SAdriana Kobylak         std::make_shared<decltype(elementsJsonFilePath)>(
741ae0998f1SAdriana Kobylak             std::move(elementsJsonFilePath));
74253a27395SAdriana Kobylak 
743d0379ea5SAdriana Kobylak     auto maybeSetAttrWithArgsBound =
744d0379ea5SAdriana Kobylak         std::bind(maybeSetBiosAttr, std::cref(*pExtensionMap),
745d0379ea5SAdriana Kobylak                   std::cref(*pElementsJsonFilePath), std::placeholders::_1);
746d0379ea5SAdriana Kobylak 
747ebf67bf7SAdriana Kobylak     std::vector<std::shared_ptr<void>> matches;
748*2b78eb0eSAdriana Kobylak 
749*2b78eb0eSAdriana Kobylak     // Entity Manager is needed to get the list of supported extensions. Add a
750*2b78eb0eSAdriana Kobylak     // match to monitor interfaces added in case it's not running yet.
751ebf67bf7SAdriana Kobylak     matches.emplace_back(std::make_shared<sdbusplus::bus::match::match>(
752d0379ea5SAdriana Kobylak         bus,
753d0379ea5SAdriana Kobylak         sdbusplus::bus::match::rules::interfacesAdded() +
754d0379ea5SAdriana Kobylak             sdbusplus::bus::match::rules::sender(
755d0379ea5SAdriana Kobylak                 "xyz.openbmc_project.EntityManager"),
756d0379ea5SAdriana Kobylak         [pldmPath, pExtensionMap, pElementsJsonFilePath,
757d0379ea5SAdriana Kobylak          maybeSetAttrWithArgsBound, &loop](auto& message) {
758d0379ea5SAdriana Kobylak             if (maybeCallMessage(message, maybeSetAttrWithArgsBound))
759d0379ea5SAdriana Kobylak             {
760fd4a6088SAdriana Kobylak                 loop.exit(0);
761d0379ea5SAdriana Kobylak             }
762ebf67bf7SAdriana Kobylak         }));
763*2b78eb0eSAdriana Kobylak 
764*2b78eb0eSAdriana Kobylak     // The BIOS attribute table can only be updated if PLDM is running because
765*2b78eb0eSAdriana Kobylak     // PLDM is the one that exposes this property. Add a match to monitor when
766*2b78eb0eSAdriana Kobylak     // the PLDM service starts.
767ebf67bf7SAdriana Kobylak     matches.emplace_back(std::make_shared<sdbusplus::bus::match::match>(
768ebf67bf7SAdriana Kobylak         bus,
769ebf67bf7SAdriana Kobylak         sdbusplus::bus::match::rules::nameOwnerChanged() +
770ebf67bf7SAdriana Kobylak             sdbusplus::bus::match::rules::arg0namespace(
771ebf67bf7SAdriana Kobylak                 "xyz.openbmc_project.PLDM"),
772ebf67bf7SAdriana Kobylak         [pExtensionMap, pElementsJsonFilePath, maybeSetAttrWithArgsBound,
773ebf67bf7SAdriana Kobylak          &loop](auto& message) {
774ebf67bf7SAdriana Kobylak             std::string name;
775ebf67bf7SAdriana Kobylak             std::string oldOwner;
776ebf67bf7SAdriana Kobylak             std::string newOwner;
777ebf67bf7SAdriana Kobylak             message.read(name, oldOwner, newOwner);
778ebf67bf7SAdriana Kobylak 
779ebf67bf7SAdriana Kobylak             if (newOwner.empty())
780ebf67bf7SAdriana Kobylak             {
781ebf67bf7SAdriana Kobylak                 return;
782ebf67bf7SAdriana Kobylak             }
783ebf67bf7SAdriana Kobylak 
784ebf67bf7SAdriana Kobylak             auto bus = sdbusplus::bus::new_default();
785ebf67bf7SAdriana Kobylak             InterfacesPropertiesMap interfacesAndProperties;
786ebf67bf7SAdriana Kobylak             auto objects = getManagedObjects(bus, entityManagerServiceName);
787ebf67bf7SAdriana Kobylak             for (const auto& pair : objects)
788ebf67bf7SAdriana Kobylak             {
789ebf67bf7SAdriana Kobylak                 std::tie(std::ignore, interfacesAndProperties) = pair;
790ebf67bf7SAdriana Kobylak                 if (maybeCall(interfacesAndProperties,
791ebf67bf7SAdriana Kobylak                               maybeSetAttrWithArgsBound))
792ebf67bf7SAdriana Kobylak                 {
793ebf67bf7SAdriana Kobylak                     loop.exit(0);
794ebf67bf7SAdriana Kobylak                 }
795ebf67bf7SAdriana Kobylak             }
796ebf67bf7SAdriana Kobylak         }));
797d0379ea5SAdriana Kobylak 
798ebf67bf7SAdriana Kobylak     InterfacesPropertiesMap interfacesAndProperties;
799ebf67bf7SAdriana Kobylak     auto objects = getManagedObjects(bus, entityManagerServiceName);
80053a27395SAdriana Kobylak     for (const auto& pair : objects)
80153a27395SAdriana Kobylak     {
80253a27395SAdriana Kobylak         std::tie(std::ignore, interfacesAndProperties) = pair;
80353a27395SAdriana Kobylak         if (maybeCall(interfacesAndProperties, maybeSetAttrWithArgsBound))
80453a27395SAdriana Kobylak         {
805d0379ea5SAdriana Kobylak             loop.exit(0);
806ebf67bf7SAdriana Kobylak             return {};
80753a27395SAdriana Kobylak         }
80853a27395SAdriana Kobylak     }
80953a27395SAdriana Kobylak 
810ebf67bf7SAdriana Kobylak     return matches;
81153a27395SAdriana Kobylak }
81253a27395SAdriana Kobylak 
813099543e4SBrad Bishop } // namespace process_hostfirmware
814099543e4SBrad Bishop } // namespace functions
815