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>
16099543e4SBrad Bishop 
17099543e4SBrad Bishop #include <filesystem>
18ae0998f1SAdriana Kobylak #include <fstream>
19099543e4SBrad Bishop #include <functional>
20099543e4SBrad Bishop #include <iostream>
21099543e4SBrad Bishop #include <map>
22099543e4SBrad Bishop #include <memory>
23099543e4SBrad Bishop #include <string>
24099543e4SBrad Bishop #include <variant>
25099543e4SBrad Bishop #include <vector>
26099543e4SBrad Bishop 
27099543e4SBrad Bishop namespace functions
28099543e4SBrad Bishop {
29099543e4SBrad Bishop namespace process_hostfirmware
30099543e4SBrad Bishop {
31099543e4SBrad Bishop 
3256f538caSAdriana Kobylak using namespace phosphor::logging;
33ebf67bf7SAdriana Kobylak using InterfacesPropertiesMap =
34ebf67bf7SAdriana Kobylak     std::map<std::string,
35ebf67bf7SAdriana Kobylak              std::map<std::string, std::variant<std::vector<std::string>>>>;
36ebf67bf7SAdriana Kobylak using ManagedObjectType =
37ebf67bf7SAdriana Kobylak     std::map<sdbusplus::message::object_path, InterfacesPropertiesMap>;
3856f538caSAdriana Kobylak 
39099543e4SBrad Bishop /**
40fd4a6088SAdriana Kobylak  * @brief GetObject function to find the service given an object path.
41fd4a6088SAdriana Kobylak  *        It is used to determine if a service is running, so there is no need
42fd4a6088SAdriana Kobylak  *        to specify interfaces as a parameter to constrain the search.
43fd4a6088SAdriana Kobylak  */
44fd4a6088SAdriana Kobylak std::string getObject(sdbusplus::bus::bus& bus, const std::string& path)
45fd4a6088SAdriana Kobylak {
46fd4a6088SAdriana Kobylak     auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
47fd4a6088SAdriana Kobylak                                       MAPPER_BUSNAME, "GetObject");
48fd4a6088SAdriana Kobylak     method.append(path);
49fd4a6088SAdriana Kobylak     std::vector<std::string> interfaces;
50fd4a6088SAdriana Kobylak     method.append(interfaces);
51fd4a6088SAdriana Kobylak 
52fd4a6088SAdriana Kobylak     std::vector<std::pair<std::string, std::vector<std::string>>> response;
53fd4a6088SAdriana Kobylak 
54fd4a6088SAdriana Kobylak     try
55fd4a6088SAdriana Kobylak     {
56fd4a6088SAdriana Kobylak         auto reply = bus.call(method);
57fd4a6088SAdriana Kobylak         reply.read(response);
58fd4a6088SAdriana Kobylak         if (response.empty())
59fd4a6088SAdriana Kobylak         {
60fd4a6088SAdriana Kobylak             return std::string{};
61fd4a6088SAdriana Kobylak         }
62fd4a6088SAdriana Kobylak     }
637b5685d1SPatrick Williams     catch (const sdbusplus::exception::exception& e)
64fd4a6088SAdriana Kobylak     {
65fd4a6088SAdriana Kobylak         return std::string{};
66fd4a6088SAdriana Kobylak     }
67fd4a6088SAdriana Kobylak     return response[0].first;
68fd4a6088SAdriana Kobylak }
69fd4a6088SAdriana Kobylak 
70fd4a6088SAdriana Kobylak /**
71ebf67bf7SAdriana Kobylak  * @brief Returns the managed objects for a given service
72ebf67bf7SAdriana Kobylak  */
73ebf67bf7SAdriana Kobylak ManagedObjectType getManagedObjects(sdbusplus::bus::bus& bus,
74ebf67bf7SAdriana Kobylak                                     const std::string& service)
75ebf67bf7SAdriana Kobylak {
76ebf67bf7SAdriana Kobylak     auto method = bus.new_method_call(service.c_str(), "/",
77ebf67bf7SAdriana Kobylak                                       "org.freedesktop.DBus.ObjectManager",
78ebf67bf7SAdriana Kobylak                                       "GetManagedObjects");
79ebf67bf7SAdriana Kobylak 
80ebf67bf7SAdriana Kobylak     ManagedObjectType objects;
81ebf67bf7SAdriana Kobylak 
82ebf67bf7SAdriana Kobylak     try
83ebf67bf7SAdriana Kobylak     {
84ebf67bf7SAdriana Kobylak         auto reply = bus.call(method);
85ebf67bf7SAdriana Kobylak         reply.read(objects);
86ebf67bf7SAdriana Kobylak     }
877b5685d1SPatrick Williams     catch (const sdbusplus::exception::exception& e)
88ebf67bf7SAdriana Kobylak     {
89ebf67bf7SAdriana Kobylak         return ManagedObjectType{};
90ebf67bf7SAdriana Kobylak     }
91ebf67bf7SAdriana Kobylak     return objects;
92ebf67bf7SAdriana Kobylak }
93ebf67bf7SAdriana Kobylak 
94ebf67bf7SAdriana Kobylak /**
95099543e4SBrad Bishop  * @brief Issue callbacks safely
96099543e4SBrad Bishop  *
97099543e4SBrad Bishop  * std::function can be empty, so this wrapper method checks for that prior to
98099543e4SBrad Bishop  * calling it to avoid std::bad_function_call
99099543e4SBrad Bishop  *
100099543e4SBrad Bishop  * @tparam Sig the types of the std::function arguments
101099543e4SBrad Bishop  * @tparam Args the deduced argument types
102099543e4SBrad Bishop  * @param[in] callback the callback being wrapped
103099543e4SBrad Bishop  * @param[in] args the callback arguments
104099543e4SBrad Bishop  */
105099543e4SBrad Bishop template <typename... Sig, typename... Args>
106099543e4SBrad Bishop void makeCallback(const std::function<void(Sig...)>& callback, Args&&... args)
107099543e4SBrad Bishop {
108099543e4SBrad Bishop     if (callback)
109099543e4SBrad Bishop     {
110099543e4SBrad Bishop         callback(std::forward<Args>(args)...);
111099543e4SBrad Bishop     }
112099543e4SBrad Bishop }
113099543e4SBrad Bishop 
114099543e4SBrad Bishop /**
115099543e4SBrad Bishop  * @brief Get file extensions for IBMCompatibleSystem
116099543e4SBrad Bishop  *
117099543e4SBrad Bishop  * IBM host firmware can be deployed as blobs (files) in a filesystem.  Host
118099543e4SBrad Bishop  * firmware blobs for different values of
119099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem are packaged with
120099543e4SBrad Bishop  * different filename extensions.  getExtensionsForIbmCompatibleSystem
121099543e4SBrad Bishop  * maintains the mapping from a given value of
122099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem to an array of
123099543e4SBrad Bishop  * filename extensions.
124099543e4SBrad Bishop  *
125099543e4SBrad Bishop  * If a mapping is found getExtensionsForIbmCompatibleSystem returns true and
126099543e4SBrad Bishop  * the extensions parameter is reset with the map entry.  If no mapping is
127099543e4SBrad Bishop  * found getExtensionsForIbmCompatibleSystem returns false and extensions is
128099543e4SBrad Bishop  * unmodified.
129099543e4SBrad Bishop  *
130099543e4SBrad Bishop  * @param[in] extensionMap a map of
131099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem to host firmware blob
132099543e4SBrad Bishop  * file extensions.
133099543e4SBrad Bishop  * @param[in] ibmCompatibleSystem The names property of an instance of
134099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem
135099543e4SBrad Bishop  * @param[out] extentions the host firmware blob file extensions
136099543e4SBrad Bishop  * @return true if an entry was found, otherwise false
137099543e4SBrad Bishop  */
138099543e4SBrad Bishop bool getExtensionsForIbmCompatibleSystem(
139099543e4SBrad Bishop     const std::map<std::string, std::vector<std::string>>& extensionMap,
140099543e4SBrad Bishop     const std::vector<std::string>& ibmCompatibleSystem,
141099543e4SBrad Bishop     std::vector<std::string>& extensions)
142099543e4SBrad Bishop {
143099543e4SBrad Bishop     for (const auto& system : ibmCompatibleSystem)
144099543e4SBrad Bishop     {
145099543e4SBrad Bishop         auto extensionMapIterator = extensionMap.find(system);
146099543e4SBrad Bishop         if (extensionMapIterator != extensionMap.end())
147099543e4SBrad Bishop         {
148099543e4SBrad Bishop             extensions = extensionMapIterator->second;
149099543e4SBrad Bishop             return true;
150099543e4SBrad Bishop         }
151099543e4SBrad Bishop     }
152099543e4SBrad Bishop 
153099543e4SBrad Bishop     return false;
154099543e4SBrad Bishop }
155099543e4SBrad Bishop 
156099543e4SBrad Bishop /**
157099543e4SBrad Bishop  * @brief Write host firmware well-known name
158099543e4SBrad Bishop  *
159099543e4SBrad Bishop  * A wrapper around std::filesystem::create_symlink that avoids EEXIST by
160099543e4SBrad Bishop  * deleting any pre-existing file.
161099543e4SBrad Bishop  *
162099543e4SBrad Bishop  * @param[in] linkTarget The link target argument to
163099543e4SBrad Bishop  * std::filesystem::create_symlink
164099543e4SBrad Bishop  * @param[in] linkPath The link path argument to std::filesystem::create_symlink
165099543e4SBrad Bishop  * @param[in] errorCallback A callback made in the event of filesystem errors.
166099543e4SBrad Bishop  */
167099543e4SBrad Bishop void writeLink(const std::filesystem::path& linkTarget,
168099543e4SBrad Bishop                const std::filesystem::path& linkPath,
169099543e4SBrad Bishop                const ErrorCallbackType& errorCallback)
170099543e4SBrad Bishop {
171099543e4SBrad Bishop     std::error_code ec;
172099543e4SBrad Bishop 
173099543e4SBrad Bishop     // remove files with the same name as the symlink to be created,
174099543e4SBrad Bishop     // otherwise symlink will fail with EEXIST.
175099543e4SBrad Bishop     if (!std::filesystem::remove(linkPath, ec))
176099543e4SBrad Bishop     {
177099543e4SBrad Bishop         if (ec)
178099543e4SBrad Bishop         {
179099543e4SBrad Bishop             makeCallback(errorCallback, linkPath, ec);
180099543e4SBrad Bishop             return;
181099543e4SBrad Bishop         }
182099543e4SBrad Bishop     }
183099543e4SBrad Bishop 
184099543e4SBrad Bishop     std::filesystem::create_symlink(linkTarget, linkPath, ec);
185099543e4SBrad Bishop     if (ec)
186099543e4SBrad Bishop     {
187099543e4SBrad Bishop         makeCallback(errorCallback, linkPath, ec);
188099543e4SBrad Bishop         return;
189099543e4SBrad Bishop     }
190099543e4SBrad Bishop }
191099543e4SBrad Bishop 
192099543e4SBrad Bishop /**
193099543e4SBrad Bishop  * @brief Find host firmware blob files that need well-known names
194099543e4SBrad Bishop  *
195099543e4SBrad Bishop  * The IBM host firmware runtime looks for data and/or additional code while
196099543e4SBrad Bishop  * bootstraping in files with well-known names.  findLinks uses the provided
197099543e4SBrad Bishop  * extensions argument to find host firmware blob files that require a
198099543e4SBrad Bishop  * well-known name.  When a blob is found, issue the provided callback
199099543e4SBrad Bishop  * (typically a function that will write a symlink).
200099543e4SBrad Bishop  *
201099543e4SBrad Bishop  * @param[in] hostFirmwareDirectory The directory in which findLinks should
202099543e4SBrad Bishop  * look for host firmware blob files that need well-known names.
203099543e4SBrad Bishop  * @param[in] extentions The extensions of the firmware blob files denote a
204099543e4SBrad Bishop  * host firmware blob file requires a well-known name.
205099543e4SBrad Bishop  * @param[in] errorCallback A callback made in the event of filesystem errors.
206099543e4SBrad Bishop  * @param[in] linkCallback A callback made when host firmware blob files
207099543e4SBrad Bishop  * needing a well known name are found.
208099543e4SBrad Bishop  */
209099543e4SBrad Bishop void findLinks(const std::filesystem::path& hostFirmwareDirectory,
210099543e4SBrad Bishop                const std::vector<std::string>& extensions,
211099543e4SBrad Bishop                const ErrorCallbackType& errorCallback,
212099543e4SBrad Bishop                const LinkCallbackType& linkCallback)
213099543e4SBrad Bishop {
214099543e4SBrad Bishop     std::error_code ec;
215099543e4SBrad Bishop     std::filesystem::directory_iterator directoryIterator(hostFirmwareDirectory,
216099543e4SBrad Bishop                                                           ec);
217099543e4SBrad Bishop     if (ec)
218099543e4SBrad Bishop     {
219099543e4SBrad Bishop         makeCallback(errorCallback, hostFirmwareDirectory, ec);
220099543e4SBrad Bishop         return;
221099543e4SBrad Bishop     }
222099543e4SBrad Bishop 
2234e82bc84SAdriana Kobylak     // Create a symlink for pnor.toc
2244e82bc84SAdriana Kobylak     static const auto tocLid = "81e00994.lid";
2254e82bc84SAdriana Kobylak     auto tocLidPath = hostFirmwareDirectory / tocLid;
2264e82bc84SAdriana Kobylak     if (std::filesystem::exists(tocLidPath))
2274e82bc84SAdriana Kobylak     {
2284e82bc84SAdriana Kobylak         static const auto tocName = "pnor.toc";
2294e82bc84SAdriana Kobylak         auto tocLinkPath = hostFirmwareDirectory / tocName;
2304e82bc84SAdriana Kobylak         makeCallback(linkCallback, tocLid, tocLinkPath, errorCallback);
2314e82bc84SAdriana Kobylak     }
2324e82bc84SAdriana Kobylak 
233099543e4SBrad Bishop     for (; directoryIterator != std::filesystem::end(directoryIterator);
234099543e4SBrad Bishop          directoryIterator.increment(ec))
235099543e4SBrad Bishop     {
236099543e4SBrad Bishop         const auto& file = directoryIterator->path();
237099543e4SBrad Bishop         if (ec)
238099543e4SBrad Bishop         {
239099543e4SBrad Bishop             makeCallback(errorCallback, file, ec);
240099543e4SBrad Bishop             // quit here if the increment call failed otherwise the loop may
241099543e4SBrad Bishop             // never finish
242099543e4SBrad Bishop             break;
243099543e4SBrad Bishop         }
244099543e4SBrad Bishop 
245099543e4SBrad Bishop         if (std::find(extensions.begin(), extensions.end(), file.extension()) ==
246099543e4SBrad Bishop             extensions.end())
247099543e4SBrad Bishop         {
248099543e4SBrad Bishop             // this file doesn't have an extension or doesn't match any of the
249099543e4SBrad Bishop             // provided extensions.
250099543e4SBrad Bishop             continue;
251099543e4SBrad Bishop         }
252099543e4SBrad Bishop 
253099543e4SBrad Bishop         auto linkPath(file.parent_path().append(
254099543e4SBrad Bishop             static_cast<const std::string&>(file.stem())));
255099543e4SBrad Bishop 
256099543e4SBrad Bishop         makeCallback(linkCallback, file.filename(), linkPath, errorCallback);
257099543e4SBrad Bishop     }
258099543e4SBrad Bishop }
259099543e4SBrad Bishop 
260099543e4SBrad Bishop /**
261ae0998f1SAdriana Kobylak  * @brief Parse the elements json file and construct a string with the data to
262ae0998f1SAdriana Kobylak  *        be used to update the bios attribute table.
263ae0998f1SAdriana Kobylak  *
264ae0998f1SAdriana Kobylak  * @param[in] elementsJsonFilePath - The path to the host firmware json file.
265ae0998f1SAdriana Kobylak  * @param[in] extensions - The extensions of the firmware blob files.
26653a27395SAdriana Kobylak  */
267ae0998f1SAdriana Kobylak std::string getBiosAttrStr(const std::filesystem::path& elementsJsonFilePath,
268ae0998f1SAdriana Kobylak                            const std::vector<std::string>& extensions)
26956f538caSAdriana Kobylak {
27056f538caSAdriana Kobylak     std::string biosAttrStr{};
27156f538caSAdriana Kobylak 
272ae0998f1SAdriana Kobylak     std::ifstream jsonFile(elementsJsonFilePath.c_str());
273ae0998f1SAdriana Kobylak     if (!jsonFile)
274ae0998f1SAdriana Kobylak     {
275ae0998f1SAdriana Kobylak         return {};
276ae0998f1SAdriana Kobylak     }
277ae0998f1SAdriana Kobylak 
278ae0998f1SAdriana Kobylak     std::map<std::string, std::string> attr;
279ae0998f1SAdriana Kobylak     auto data = nlohmann::json::parse(jsonFile, nullptr, false);
280ae0998f1SAdriana Kobylak     if (data.is_discarded())
281ae0998f1SAdriana Kobylak     {
282ae0998f1SAdriana Kobylak         log<level::ERR>("Error parsing JSON file",
283ae0998f1SAdriana Kobylak                         entry("FILE=%s", elementsJsonFilePath.c_str()));
284ae0998f1SAdriana Kobylak         return {};
285ae0998f1SAdriana Kobylak     }
286ae0998f1SAdriana Kobylak 
287ae0998f1SAdriana Kobylak     // .get requires a non-const iterator
288ae0998f1SAdriana Kobylak     for (auto& iter : data["lids"])
289ae0998f1SAdriana Kobylak     {
290ae0998f1SAdriana Kobylak         std::string name{};
291ae0998f1SAdriana Kobylak         std::string lid{};
292ae0998f1SAdriana Kobylak 
293ae0998f1SAdriana Kobylak         try
294ae0998f1SAdriana Kobylak         {
295ae0998f1SAdriana Kobylak             name = iter["element_name"].get<std::string>();
296ae0998f1SAdriana Kobylak             lid = iter["short_lid_name"].get<std::string>();
297ae0998f1SAdriana Kobylak         }
298ae0998f1SAdriana Kobylak         catch (std::exception& e)
299ae0998f1SAdriana Kobylak         {
300ae0998f1SAdriana Kobylak             // Possibly the element or lid name field was not found
301ae0998f1SAdriana Kobylak             log<level::ERR>("Error reading JSON field",
302ae0998f1SAdriana Kobylak                             entry("FILE=%s", elementsJsonFilePath.c_str()),
303ae0998f1SAdriana Kobylak                             entry("ERROR=%s", e.what()));
304ae0998f1SAdriana Kobylak             continue;
305ae0998f1SAdriana Kobylak         }
306ae0998f1SAdriana Kobylak 
307ae0998f1SAdriana Kobylak         // The elements with the ipl extension have higher priority. Therefore
308ae0998f1SAdriana Kobylak         // Use operator[] to overwrite value if an entry for it already exists.
309ae0998f1SAdriana Kobylak         // Ex: if the JSON contains an entry A.P10 followed by A.P10.iplTime,
310ae0998f1SAdriana Kobylak         // the lid value for the latter one will be overwrite the value of the
311ae0998f1SAdriana Kobylak         // first one.
312ae0998f1SAdriana Kobylak         constexpr auto iplExtension = ".iplTime";
313ae0998f1SAdriana Kobylak         std::filesystem::path path(name);
314ae0998f1SAdriana Kobylak         if (path.extension() == iplExtension)
315ae0998f1SAdriana Kobylak         {
316ae0998f1SAdriana Kobylak             // Some elements have an additional extension, ex: .P10.iplTime
317ae0998f1SAdriana Kobylak             // Strip off the ipl extension with stem(), then check if there is
318ae0998f1SAdriana Kobylak             // an additional extension with extension().
319ae0998f1SAdriana Kobylak             if (!path.stem().extension().empty())
320ae0998f1SAdriana Kobylak             {
321ae0998f1SAdriana Kobylak                 // Check if the extension matches the extensions for this system
322ae0998f1SAdriana Kobylak                 if (std::find(extensions.begin(), extensions.end(),
323ae0998f1SAdriana Kobylak                               path.stem().extension()) == extensions.end())
324ae0998f1SAdriana Kobylak                 {
325ae0998f1SAdriana Kobylak                     continue;
326ae0998f1SAdriana Kobylak                 }
327ae0998f1SAdriana Kobylak             }
328ae0998f1SAdriana Kobylak             // Get the element name without extensions by calling stem() twice
329ae0998f1SAdriana Kobylak             // since stem() returns the base name if no periods are found.
330ae0998f1SAdriana Kobylak             // Therefore both "element.P10" and "element.P10.iplTime" would
331ae0998f1SAdriana Kobylak             // become "element".
332ae0998f1SAdriana Kobylak             attr[path.stem().stem()] = lid;
333ae0998f1SAdriana Kobylak             continue;
334ae0998f1SAdriana Kobylak         }
335ae0998f1SAdriana Kobylak 
336ae0998f1SAdriana Kobylak         // Process all other extensions. The extension should match the list of
337ae0998f1SAdriana Kobylak         // supported extensions for this system. Use .insert() to only add
338ae0998f1SAdriana Kobylak         // entries that do not exist, so to not overwrite the values that may
339ae0998f1SAdriana Kobylak         // had been added that had the ipl extension.
340ae0998f1SAdriana Kobylak         if (std::find(extensions.begin(), extensions.end(), path.extension()) !=
341ae0998f1SAdriana Kobylak             extensions.end())
342ae0998f1SAdriana Kobylak         {
343ae0998f1SAdriana Kobylak             attr.insert({path.stem(), lid});
344ae0998f1SAdriana Kobylak         }
345ae0998f1SAdriana Kobylak     }
346ae0998f1SAdriana Kobylak     for (const auto& a : attr)
347ae0998f1SAdriana Kobylak     {
348ae0998f1SAdriana Kobylak         // Build the bios attribute string with format:
349ae0998f1SAdriana Kobylak         // "element1=lid1,element2=lid2,elementN=lidN,"
350ae0998f1SAdriana Kobylak         biosAttrStr += a.first + "=" + a.second + ",";
3515dc5d6ccSAdriana Kobylak 
3525dc5d6ccSAdriana Kobylak         // Create symlinks from the hostfw elements to their corresponding
3535dc5d6ccSAdriana Kobylak         // lid files if they don't exist
3545dc5d6ccSAdriana Kobylak         auto elementFilePath =
3555dc5d6ccSAdriana Kobylak             std::filesystem::path("/media/hostfw/running") / a.first;
3565dc5d6ccSAdriana Kobylak         if (!std::filesystem::exists(elementFilePath))
3575dc5d6ccSAdriana Kobylak         {
3585dc5d6ccSAdriana Kobylak             std::error_code ec;
3595dc5d6ccSAdriana Kobylak             auto lidName = a.second + ".lid";
3605dc5d6ccSAdriana Kobylak             std::filesystem::create_symlink(lidName, elementFilePath, ec);
3615dc5d6ccSAdriana Kobylak             if (ec)
3625dc5d6ccSAdriana Kobylak             {
3635dc5d6ccSAdriana Kobylak                 log<level::ERR>("Error creating symlink",
3645dc5d6ccSAdriana Kobylak                                 entry("TARGET=%s", lidName.c_str()),
3655dc5d6ccSAdriana Kobylak                                 entry("LINK=%s", elementFilePath.c_str()));
3665dc5d6ccSAdriana Kobylak             }
3675dc5d6ccSAdriana Kobylak         }
368ae0998f1SAdriana Kobylak     }
369ae0998f1SAdriana Kobylak 
370*a38f6e65SGeorge Liu     // Delete the last comma of the bios attribute string
371*a38f6e65SGeorge Liu     if (biosAttrStr.back() == ',')
372*a38f6e65SGeorge Liu     {
373*a38f6e65SGeorge Liu         return biosAttrStr.substr(0, biosAttrStr.length() - 1);
374*a38f6e65SGeorge Liu     }
375*a38f6e65SGeorge Liu 
376ae0998f1SAdriana Kobylak     return biosAttrStr;
377ae0998f1SAdriana Kobylak }
378ae0998f1SAdriana Kobylak 
379ae0998f1SAdriana Kobylak /**
380ae0998f1SAdriana Kobylak  * @brief Set the bios attribute table with details of the host firmware data
381ae0998f1SAdriana Kobylak  * for this system.
382ae0998f1SAdriana Kobylak  *
383ae0998f1SAdriana Kobylak  * @param[in] elementsJsonFilePath - The path to the host firmware json file.
384ae0998f1SAdriana Kobylak  * @param[in] extentions - The extensions of the firmware blob files.
385ae0998f1SAdriana Kobylak  */
386ae0998f1SAdriana Kobylak void setBiosAttr(const std::filesystem::path& elementsJsonFilePath,
387ae0998f1SAdriana Kobylak                  const std::vector<std::string>& extensions)
388ae0998f1SAdriana Kobylak {
389ae0998f1SAdriana Kobylak     auto biosAttrStr = getBiosAttrStr(elementsJsonFilePath, extensions);
390ae0998f1SAdriana Kobylak 
39156f538caSAdriana Kobylak     constexpr auto biosConfigPath = "/xyz/openbmc_project/bios_config/manager";
39256f538caSAdriana Kobylak     constexpr auto biosConfigIntf = "xyz.openbmc_project.BIOSConfig.Manager";
39356f538caSAdriana Kobylak     constexpr auto dbusAttrName = "hb_lid_ids";
39456f538caSAdriana Kobylak     constexpr auto dbusAttrType =
39556f538caSAdriana Kobylak         "xyz.openbmc_project.BIOSConfig.Manager.AttributeType.String";
39656f538caSAdriana Kobylak 
39756f538caSAdriana Kobylak     using PendingAttributesType = std::vector<std::pair<
39856f538caSAdriana Kobylak         std::string, std::tuple<std::string, std::variant<std::string>>>>;
39956f538caSAdriana Kobylak     PendingAttributesType pendingAttributes;
40056f538caSAdriana Kobylak     pendingAttributes.emplace_back(std::make_pair(
40156f538caSAdriana Kobylak         dbusAttrName, std::make_tuple(dbusAttrType, biosAttrStr)));
40256f538caSAdriana Kobylak 
40356f538caSAdriana Kobylak     auto bus = sdbusplus::bus::new_default();
40456f538caSAdriana Kobylak     auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
40556f538caSAdriana Kobylak                                       MAPPER_INTERFACE, "GetObject");
40656f538caSAdriana Kobylak     method.append(biosConfigPath, std::vector<std::string>({biosConfigIntf}));
40756f538caSAdriana Kobylak     std::vector<std::pair<std::string, std::vector<std::string>>> response;
40856f538caSAdriana Kobylak     try
40956f538caSAdriana Kobylak     {
41056f538caSAdriana Kobylak         auto reply = bus.call(method);
41156f538caSAdriana Kobylak         reply.read(response);
41256f538caSAdriana Kobylak         if (response.empty())
41356f538caSAdriana Kobylak         {
41456f538caSAdriana Kobylak             log<level::ERR>("Error reading mapper response",
41556f538caSAdriana Kobylak                             entry("PATH=%s", biosConfigPath),
41656f538caSAdriana Kobylak                             entry("INTERFACE=%s", biosConfigIntf));
41756f538caSAdriana Kobylak             return;
41856f538caSAdriana Kobylak         }
41956f538caSAdriana Kobylak         auto method = bus.new_method_call((response.begin()->first).c_str(),
42056f538caSAdriana Kobylak                                           biosConfigPath,
42156f538caSAdriana Kobylak                                           SYSTEMD_PROPERTY_INTERFACE, "Set");
42256f538caSAdriana Kobylak         method.append(biosConfigIntf, "PendingAttributes",
42356f538caSAdriana Kobylak                       std::variant<PendingAttributesType>(pendingAttributes));
42456f538caSAdriana Kobylak         bus.call(method);
42556f538caSAdriana Kobylak     }
4267b5685d1SPatrick Williams     catch (const sdbusplus::exception::exception& e)
42756f538caSAdriana Kobylak     {
42856f538caSAdriana Kobylak         log<level::ERR>("Error setting the bios attribute",
42956f538caSAdriana Kobylak                         entry("ERROR=%s", e.what()),
43056f538caSAdriana Kobylak                         entry("ATTRIBUTE=%s", dbusAttrName));
43156f538caSAdriana Kobylak         return;
43256f538caSAdriana Kobylak     }
43356f538caSAdriana Kobylak }
43453a27395SAdriana Kobylak 
43553a27395SAdriana Kobylak /**
436099543e4SBrad Bishop  * @brief Make callbacks on
437099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem instances.
438099543e4SBrad Bishop  *
439099543e4SBrad Bishop  * Look for an instance of
440099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem in the provided
441099543e4SBrad Bishop  * argument and if found, issue the provided callback.
442099543e4SBrad Bishop  *
443099543e4SBrad Bishop  * @param[in] interfacesAndProperties the interfaces in which to look for an
444099543e4SBrad Bishop  * instance of xyz.openbmc_project.Configuration.IBMCompatibleSystem
445099543e4SBrad Bishop  * @param[in] callback the user callback to make if
446099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem is found in
447099543e4SBrad Bishop  * interfacesAndProperties
448099543e4SBrad Bishop  * @return true if interfacesAndProperties contained an instance of
449099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem, false otherwise
450099543e4SBrad Bishop  */
451099543e4SBrad Bishop bool maybeCall(const std::map<std::string,
452099543e4SBrad Bishop                               std::map<std::string,
453099543e4SBrad Bishop                                        std::variant<std::vector<std::string>>>>&
454099543e4SBrad Bishop                    interfacesAndProperties,
455099543e4SBrad Bishop                const MaybeCallCallbackType& callback)
456099543e4SBrad Bishop {
457099543e4SBrad Bishop     using namespace std::string_literals;
458099543e4SBrad Bishop 
459099543e4SBrad Bishop     static const auto interfaceName =
460099543e4SBrad Bishop         "xyz.openbmc_project.Configuration.IBMCompatibleSystem"s;
461099543e4SBrad Bishop     auto interfaceIterator = interfacesAndProperties.find(interfaceName);
462099543e4SBrad Bishop     if (interfaceIterator == interfacesAndProperties.cend())
463099543e4SBrad Bishop     {
464099543e4SBrad Bishop         // IBMCompatibleSystem interface not found, so instruct the caller to
465099543e4SBrad Bishop         // keep waiting or try again later.
466099543e4SBrad Bishop         return false;
467099543e4SBrad Bishop     }
468099543e4SBrad Bishop     auto propertyIterator = interfaceIterator->second.find("Names"s);
469099543e4SBrad Bishop     if (propertyIterator == interfaceIterator->second.cend())
470099543e4SBrad Bishop     {
471099543e4SBrad Bishop         // The interface exists but the property doesn't.  This is a bug in the
472099543e4SBrad Bishop         // IBMCompatibleSystem implementation.  The caller should not try
473099543e4SBrad Bishop         // again.
474099543e4SBrad Bishop         std::cerr << "Names property not implemented on " << interfaceName
475099543e4SBrad Bishop                   << "\n";
476099543e4SBrad Bishop         return true;
477099543e4SBrad Bishop     }
478099543e4SBrad Bishop 
479099543e4SBrad Bishop     const auto& ibmCompatibleSystem =
480099543e4SBrad Bishop         std::get<std::vector<std::string>>(propertyIterator->second);
481099543e4SBrad Bishop     if (callback)
482099543e4SBrad Bishop     {
483099543e4SBrad Bishop         callback(ibmCompatibleSystem);
484099543e4SBrad Bishop     }
485099543e4SBrad Bishop 
486099543e4SBrad Bishop     // IBMCompatibleSystem found and callback issued.
487099543e4SBrad Bishop     return true;
488099543e4SBrad Bishop }
489099543e4SBrad Bishop 
490099543e4SBrad Bishop /**
491099543e4SBrad Bishop  * @brief Make callbacks on
492099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem instances.
493099543e4SBrad Bishop  *
494099543e4SBrad Bishop  * Look for an instance of
495099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem in the provided
496099543e4SBrad Bishop  * argument and if found, issue the provided callback.
497099543e4SBrad Bishop  *
498099543e4SBrad Bishop  * @param[in] message the DBus message in which to look for an instance of
499099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem
500099543e4SBrad Bishop  * @param[in] callback the user callback to make if
501099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem is found in
502099543e4SBrad Bishop  * message
503099543e4SBrad Bishop  * @return true if message contained an instance of
504099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem, false otherwise
505099543e4SBrad Bishop  */
506099543e4SBrad Bishop bool maybeCallMessage(sdbusplus::message::message& message,
507099543e4SBrad Bishop                       const MaybeCallCallbackType& callback)
508099543e4SBrad Bishop {
509099543e4SBrad Bishop     std::map<std::string,
510099543e4SBrad Bishop              std::map<std::string, std::variant<std::vector<std::string>>>>
511099543e4SBrad Bishop         interfacesAndProperties;
512099543e4SBrad Bishop     sdbusplus::message::object_path _;
513099543e4SBrad Bishop     message.read(_, interfacesAndProperties);
514099543e4SBrad Bishop     return maybeCall(interfacesAndProperties, callback);
515099543e4SBrad Bishop }
516099543e4SBrad Bishop 
517099543e4SBrad Bishop /**
518099543e4SBrad Bishop  * @brief Determine system support for host firmware well-known names.
519099543e4SBrad Bishop  *
520099543e4SBrad Bishop  * Using the provided extensionMap and
521099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem, determine if
522099543e4SBrad Bishop  * well-known names for host firmare blob files are necessary and if so, create
523099543e4SBrad Bishop  * them.
524099543e4SBrad Bishop  *
525099543e4SBrad Bishop  * @param[in] extensionMap a map of
526099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem to host firmware blob
527099543e4SBrad Bishop  * file extensions.
528099543e4SBrad Bishop  * @param[in] hostFirmwareDirectory The directory in which findLinks should
529099543e4SBrad Bishop  * look for host firmware blob files that need well-known names.
530099543e4SBrad Bishop  * @param[in] ibmCompatibleSystem The names property of an instance of
531099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem
532099543e4SBrad Bishop  * @param[in] errorCallback A callback made in the event of filesystem errors.
533099543e4SBrad Bishop  */
534099543e4SBrad Bishop void maybeMakeLinks(
535099543e4SBrad Bishop     const std::map<std::string, std::vector<std::string>>& extensionMap,
536099543e4SBrad Bishop     const std::filesystem::path& hostFirmwareDirectory,
537099543e4SBrad Bishop     const std::vector<std::string>& ibmCompatibleSystem,
538099543e4SBrad Bishop     const ErrorCallbackType& errorCallback)
539099543e4SBrad Bishop {
540099543e4SBrad Bishop     std::vector<std::string> extensions;
541099543e4SBrad Bishop     if (getExtensionsForIbmCompatibleSystem(extensionMap, ibmCompatibleSystem,
542099543e4SBrad Bishop                                             extensions))
543099543e4SBrad Bishop     {
544099543e4SBrad Bishop         findLinks(hostFirmwareDirectory, extensions, errorCallback, writeLink);
545099543e4SBrad Bishop     }
546099543e4SBrad Bishop }
547099543e4SBrad Bishop 
548099543e4SBrad Bishop /**
54953a27395SAdriana Kobylak  * @brief Determine system support for updating the bios attribute table.
55053a27395SAdriana Kobylak  *
55153a27395SAdriana Kobylak  * Using the provided extensionMap and
55253a27395SAdriana Kobylak  * xyz.openbmc_project.Configuration.IBMCompatibleSystem, determine if the bios
55353a27395SAdriana Kobylak  * attribute table needs to be updated.
55453a27395SAdriana Kobylak  *
55553a27395SAdriana Kobylak  * @param[in] extensionMap a map of
55653a27395SAdriana Kobylak  * xyz.openbmc_project.Configuration.IBMCompatibleSystem to host firmware blob
55753a27395SAdriana Kobylak  * file extensions.
558ae0998f1SAdriana Kobylak  * @param[in] elementsJsonFilePath The file path to the json file
55953a27395SAdriana Kobylak  * @param[in] ibmCompatibleSystem The names property of an instance of
56053a27395SAdriana Kobylak  * xyz.openbmc_project.Configuration.IBMCompatibleSystem
56153a27395SAdriana Kobylak  */
56253a27395SAdriana Kobylak void maybeSetBiosAttr(
56353a27395SAdriana Kobylak     const std::map<std::string, std::vector<std::string>>& extensionMap,
564ae0998f1SAdriana Kobylak     const std::filesystem::path& elementsJsonFilePath,
56553a27395SAdriana Kobylak     const std::vector<std::string>& ibmCompatibleSystem)
56653a27395SAdriana Kobylak {
56753a27395SAdriana Kobylak     std::vector<std::string> extensions;
56853a27395SAdriana Kobylak     if (getExtensionsForIbmCompatibleSystem(extensionMap, ibmCompatibleSystem,
56953a27395SAdriana Kobylak                                             extensions))
57053a27395SAdriana Kobylak     {
571ae0998f1SAdriana Kobylak         setBiosAttr(elementsJsonFilePath, extensions);
57253a27395SAdriana Kobylak     }
57353a27395SAdriana Kobylak }
57453a27395SAdriana Kobylak 
57553a27395SAdriana Kobylak /**
576099543e4SBrad Bishop  * @brief process host firmware
577099543e4SBrad Bishop  *
578099543e4SBrad Bishop  * Allocate a callback context and register for DBus.ObjectManager Interfaces
579099543e4SBrad Bishop  * added signals from entity manager.
580099543e4SBrad Bishop  *
581099543e4SBrad Bishop  * Check the current entity manager object tree for a
582099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem instance (entity
583099543e4SBrad Bishop  * manager will be dbus activated if it is not running).  If one is found,
584099543e4SBrad Bishop  * determine if symlinks need to be created and create them.  Instruct the
585099543e4SBrad Bishop  * program event loop to exit.
586099543e4SBrad Bishop  *
587099543e4SBrad Bishop  * If no instance of xyz.openbmc_project.Configuration.IBMCompatibleSystem is
588099543e4SBrad Bishop  * found return the callback context to main, where the program will sleep
589099543e4SBrad Bishop  * until the callback is invoked one or more times and instructs the program
590099543e4SBrad Bishop  * event loop to exit when
591099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem is added.
592099543e4SBrad Bishop  *
593099543e4SBrad Bishop  * @param[in] bus a DBus client connection
594099543e4SBrad Bishop  * @param[in] extensionMap a map of
595099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem to host firmware blob
596099543e4SBrad Bishop  * file extensions.
597099543e4SBrad Bishop  * @param[in] hostFirmwareDirectory The directory in which processHostFirmware
598099543e4SBrad Bishop  * should look for blob files.
599099543e4SBrad Bishop  * @param[in] errorCallback A callback made in the event of filesystem errors.
600099543e4SBrad Bishop  * @param[in] loop a program event loop
601099543e4SBrad Bishop  * @return nullptr if an instance of
602099543e4SBrad Bishop  * xyz.openbmc_project.Configuration.IBMCompatibleSystem is found, otherwise a
603099543e4SBrad Bishop  * pointer to an sdbusplus match object.
604099543e4SBrad Bishop  */
605099543e4SBrad Bishop std::shared_ptr<void> processHostFirmware(
606099543e4SBrad Bishop     sdbusplus::bus::bus& bus,
607099543e4SBrad Bishop     std::map<std::string, std::vector<std::string>> extensionMap,
608099543e4SBrad Bishop     std::filesystem::path hostFirmwareDirectory,
609099543e4SBrad Bishop     ErrorCallbackType errorCallback, sdeventplus::Event& loop)
610099543e4SBrad Bishop {
611099543e4SBrad Bishop     // ownership of extensionMap, hostFirmwareDirectory and errorCallback can't
612099543e4SBrad Bishop     // be transfered to the match callback because they are needed in the non
613099543e4SBrad Bishop     // async part of this function below, so they need to be moved to the heap.
614099543e4SBrad Bishop     auto pExtensionMap =
615099543e4SBrad Bishop         std::make_shared<decltype(extensionMap)>(std::move(extensionMap));
616099543e4SBrad Bishop     auto pHostFirmwareDirectory =
617099543e4SBrad Bishop         std::make_shared<decltype(hostFirmwareDirectory)>(
618099543e4SBrad Bishop             std::move(hostFirmwareDirectory));
619099543e4SBrad Bishop     auto pErrorCallback =
620099543e4SBrad Bishop         std::make_shared<decltype(errorCallback)>(std::move(errorCallback));
621099543e4SBrad Bishop 
622099543e4SBrad Bishop     // register for a callback in case the IBMCompatibleSystem interface has
623099543e4SBrad Bishop     // not yet been published by entity manager.
624099543e4SBrad Bishop     auto interfacesAddedMatch = std::make_shared<sdbusplus::bus::match::match>(
625099543e4SBrad Bishop         bus,
626099543e4SBrad Bishop         sdbusplus::bus::match::rules::interfacesAdded() +
627099543e4SBrad Bishop             sdbusplus::bus::match::rules::sender(
628099543e4SBrad Bishop                 "xyz.openbmc_project.EntityManager"),
629099543e4SBrad Bishop         [pExtensionMap, pHostFirmwareDirectory, pErrorCallback,
630099543e4SBrad Bishop          &loop](auto& message) {
631099543e4SBrad Bishop             // bind the extension map, host firmware directory, and error
632099543e4SBrad Bishop             // callback to the maybeMakeLinks function.
633099543e4SBrad Bishop             auto maybeMakeLinksWithArgsBound =
634099543e4SBrad Bishop                 std::bind(maybeMakeLinks, std::cref(*pExtensionMap),
635099543e4SBrad Bishop                           std::cref(*pHostFirmwareDirectory),
636099543e4SBrad Bishop                           std::placeholders::_1, std::cref(*pErrorCallback));
637099543e4SBrad Bishop 
638099543e4SBrad Bishop             // if the InterfacesAdded message contains an an instance of
639099543e4SBrad Bishop             // xyz.openbmc_project.Configuration.IBMCompatibleSystem, check to
640099543e4SBrad Bishop             // see if links are necessary on this system and if so, create
641099543e4SBrad Bishop             // them.
642099543e4SBrad Bishop             if (maybeCallMessage(message, maybeMakeLinksWithArgsBound))
643099543e4SBrad Bishop             {
644099543e4SBrad Bishop                 // The IBMCompatibleSystem interface was found and the links
645099543e4SBrad Bishop                 // were created if applicable.  Instruct the event loop /
646099543e4SBrad Bishop                 // subcommand to exit.
647099543e4SBrad Bishop                 loop.exit(0);
648099543e4SBrad Bishop             }
649099543e4SBrad Bishop         });
650099543e4SBrad Bishop 
651099543e4SBrad Bishop     // now that we'll get a callback in the event of an InterfacesAdded signal
652099543e4SBrad Bishop     // (potentially containing
653099543e4SBrad Bishop     // xyz.openbmc_project.Configuration.IBMCompatibleSystem), activate entity
654099543e4SBrad Bishop     // manager if it isn't running and enumerate its objects
655099543e4SBrad Bishop     auto getManagedObjects = bus.new_method_call(
656099543e4SBrad Bishop         "xyz.openbmc_project.EntityManager", "/",
657099543e4SBrad Bishop         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
658099543e4SBrad Bishop     std::map<std::string,
659099543e4SBrad Bishop              std::map<std::string, std::variant<std::vector<std::string>>>>
660099543e4SBrad Bishop         interfacesAndProperties;
661099543e4SBrad Bishop     std::map<sdbusplus::message::object_path, decltype(interfacesAndProperties)>
662099543e4SBrad Bishop         objects;
663c79fa915SAdriana Kobylak     try
664c79fa915SAdriana Kobylak     {
665c79fa915SAdriana Kobylak         auto reply = bus.call(getManagedObjects);
666099543e4SBrad Bishop         reply.read(objects);
667c79fa915SAdriana Kobylak     }
6687b5685d1SPatrick Williams     catch (const sdbusplus::exception::exception& e)
669c79fa915SAdriana Kobylak     {
670c79fa915SAdriana Kobylak         // Error querying the EntityManager interface. Return the match to have
671c79fa915SAdriana Kobylak         // the callback run if/when the interface appears in D-Bus.
672c79fa915SAdriana Kobylak         return interfacesAddedMatch;
673c79fa915SAdriana Kobylak     }
674099543e4SBrad Bishop 
675099543e4SBrad Bishop     // bind the extension map, host firmware directory, and error callback to
676099543e4SBrad Bishop     // the maybeMakeLinks function.
677099543e4SBrad Bishop     auto maybeMakeLinksWithArgsBound =
678099543e4SBrad Bishop         std::bind(maybeMakeLinks, std::cref(*pExtensionMap),
679099543e4SBrad Bishop                   std::cref(*pHostFirmwareDirectory), std::placeholders::_1,
680099543e4SBrad Bishop                   std::cref(*pErrorCallback));
681099543e4SBrad Bishop 
682099543e4SBrad Bishop     for (const auto& pair : objects)
683099543e4SBrad Bishop     {
684099543e4SBrad Bishop         std::tie(std::ignore, interfacesAndProperties) = pair;
685099543e4SBrad Bishop         // if interfacesAndProperties contains an an instance of
686099543e4SBrad Bishop         // xyz.openbmc_project.Configuration.IBMCompatibleSystem, check to see
687099543e4SBrad Bishop         // if links are necessary on this system and if so, create them
688099543e4SBrad Bishop         if (maybeCall(interfacesAndProperties, maybeMakeLinksWithArgsBound))
689099543e4SBrad Bishop         {
690099543e4SBrad Bishop             // The IBMCompatibleSystem interface is already on the bus and the
691099543e4SBrad Bishop             // links were created if applicable.  Instruct the event loop to
692099543e4SBrad Bishop             // exit.
693099543e4SBrad Bishop             loop.exit(0);
694099543e4SBrad Bishop             // The match object isn't needed anymore, so destroy it on return.
695099543e4SBrad Bishop             return nullptr;
696099543e4SBrad Bishop         }
697099543e4SBrad Bishop     }
698099543e4SBrad Bishop 
699099543e4SBrad Bishop     // The IBMCompatibleSystem interface has not yet been published.  Move
700099543e4SBrad Bishop     // ownership of the match callback to the caller.
701099543e4SBrad Bishop     return interfacesAddedMatch;
702099543e4SBrad Bishop }
70353a27395SAdriana Kobylak 
70453a27395SAdriana Kobylak /**
70553a27395SAdriana Kobylak  * @brief Update the Bios Attribute Table
70653a27395SAdriana Kobylak  *
70753a27395SAdriana Kobylak  * If an instance of xyz.openbmc_project.Configuration.IBMCompatibleSystem is
70853a27395SAdriana Kobylak  * found, update the Bios Attribute Table with the appropriate host firmware
70953a27395SAdriana Kobylak  * data.
71053a27395SAdriana Kobylak  *
71153a27395SAdriana Kobylak  * @param[in] bus - D-Bus client connection.
71253a27395SAdriana Kobylak  * @param[in] extensionMap - Map of IBMCompatibleSystem names and host firmware
71353a27395SAdriana Kobylak  *                           file extensions.
714ae0998f1SAdriana Kobylak  * @param[in] elementsJsonFilePath - The Path to the json file
71553a27395SAdriana Kobylak  * @param[in] loop - Program event loop.
71653a27395SAdriana Kobylak  * @return nullptr
71753a27395SAdriana Kobylak  */
718ebf67bf7SAdriana Kobylak std::vector<std::shared_ptr<void>> updateBiosAttrTable(
71953a27395SAdriana Kobylak     sdbusplus::bus::bus& bus,
72053a27395SAdriana Kobylak     std::map<std::string, std::vector<std::string>> extensionMap,
721ae0998f1SAdriana Kobylak     std::filesystem::path elementsJsonFilePath, sdeventplus::Event& loop)
72253a27395SAdriana Kobylak {
723d0379ea5SAdriana Kobylak     constexpr auto pldmPath = "/xyz/openbmc_project/pldm";
724ebf67bf7SAdriana Kobylak     constexpr auto entityManagerServiceName =
725ebf67bf7SAdriana Kobylak         "xyz.openbmc_project.EntityManager";
726d0379ea5SAdriana Kobylak 
72753a27395SAdriana Kobylak     auto pExtensionMap =
72853a27395SAdriana Kobylak         std::make_shared<decltype(extensionMap)>(std::move(extensionMap));
729ae0998f1SAdriana Kobylak     auto pElementsJsonFilePath =
730ae0998f1SAdriana Kobylak         std::make_shared<decltype(elementsJsonFilePath)>(
731ae0998f1SAdriana Kobylak             std::move(elementsJsonFilePath));
73253a27395SAdriana Kobylak 
733d0379ea5SAdriana Kobylak     // Entity Manager is needed to get the list of supported extensions. Add a
734d0379ea5SAdriana Kobylak     // match to monitor interfaces added in case it's not running yet.
735d0379ea5SAdriana Kobylak     auto maybeSetAttrWithArgsBound =
736d0379ea5SAdriana Kobylak         std::bind(maybeSetBiosAttr, std::cref(*pExtensionMap),
737d0379ea5SAdriana Kobylak                   std::cref(*pElementsJsonFilePath), std::placeholders::_1);
738d0379ea5SAdriana Kobylak 
739ebf67bf7SAdriana Kobylak     std::vector<std::shared_ptr<void>> matches;
740ebf67bf7SAdriana Kobylak     matches.emplace_back(std::make_shared<sdbusplus::bus::match::match>(
741d0379ea5SAdriana Kobylak         bus,
742d0379ea5SAdriana Kobylak         sdbusplus::bus::match::rules::interfacesAdded() +
743d0379ea5SAdriana Kobylak             sdbusplus::bus::match::rules::sender(
744d0379ea5SAdriana Kobylak                 "xyz.openbmc_project.EntityManager"),
745d0379ea5SAdriana Kobylak         [pldmPath, pExtensionMap, pElementsJsonFilePath,
746d0379ea5SAdriana Kobylak          maybeSetAttrWithArgsBound, &loop](auto& message) {
747d0379ea5SAdriana Kobylak             auto bus = sdbusplus::bus::new_default();
748fd4a6088SAdriana Kobylak             auto pldmObject = getObject(bus, pldmPath);
749fd4a6088SAdriana Kobylak             if (pldmObject.empty())
750fd4a6088SAdriana Kobylak             {
751d0379ea5SAdriana Kobylak                 return;
752d0379ea5SAdriana Kobylak             }
753d0379ea5SAdriana Kobylak             if (maybeCallMessage(message, maybeSetAttrWithArgsBound))
754d0379ea5SAdriana Kobylak             {
755fd4a6088SAdriana Kobylak                 loop.exit(0);
756d0379ea5SAdriana Kobylak             }
757ebf67bf7SAdriana Kobylak         }));
758ebf67bf7SAdriana Kobylak     matches.emplace_back(std::make_shared<sdbusplus::bus::match::match>(
759ebf67bf7SAdriana Kobylak         bus,
760ebf67bf7SAdriana Kobylak         sdbusplus::bus::match::rules::nameOwnerChanged() +
761ebf67bf7SAdriana Kobylak             sdbusplus::bus::match::rules::arg0namespace(
762ebf67bf7SAdriana Kobylak                 "xyz.openbmc_project.PLDM"),
763ebf67bf7SAdriana Kobylak         [pExtensionMap, pElementsJsonFilePath, maybeSetAttrWithArgsBound,
764ebf67bf7SAdriana Kobylak          &loop](auto& message) {
765ebf67bf7SAdriana Kobylak             std::string name;
766ebf67bf7SAdriana Kobylak             std::string oldOwner;
767ebf67bf7SAdriana Kobylak             std::string newOwner;
768ebf67bf7SAdriana Kobylak             message.read(name, oldOwner, newOwner);
769ebf67bf7SAdriana Kobylak 
770ebf67bf7SAdriana Kobylak             if (newOwner.empty())
771ebf67bf7SAdriana Kobylak             {
772ebf67bf7SAdriana Kobylak                 return;
773ebf67bf7SAdriana Kobylak             }
774ebf67bf7SAdriana Kobylak 
775ebf67bf7SAdriana Kobylak             auto bus = sdbusplus::bus::new_default();
776ebf67bf7SAdriana Kobylak             InterfacesPropertiesMap interfacesAndProperties;
777ebf67bf7SAdriana Kobylak             auto objects = getManagedObjects(bus, entityManagerServiceName);
778ebf67bf7SAdriana Kobylak             for (const auto& pair : objects)
779ebf67bf7SAdriana Kobylak             {
780ebf67bf7SAdriana Kobylak                 std::tie(std::ignore, interfacesAndProperties) = pair;
781ebf67bf7SAdriana Kobylak                 if (maybeCall(interfacesAndProperties,
782ebf67bf7SAdriana Kobylak                               maybeSetAttrWithArgsBound))
783ebf67bf7SAdriana Kobylak                 {
784ebf67bf7SAdriana Kobylak                     loop.exit(0);
785ebf67bf7SAdriana Kobylak                 }
786ebf67bf7SAdriana Kobylak             }
787ebf67bf7SAdriana Kobylak         }));
788d0379ea5SAdriana Kobylak 
789d0379ea5SAdriana Kobylak     // The BIOS attribute table can only be updated if PLDM is running because
790d0379ea5SAdriana Kobylak     // PLDM is the one that exposes this property. Return if it's not running.
791d0379ea5SAdriana Kobylak     auto pldmObject = getObject(bus, pldmPath);
792d0379ea5SAdriana Kobylak     if (pldmObject.empty())
793d0379ea5SAdriana Kobylak     {
794ebf67bf7SAdriana Kobylak         return matches;
795fd4a6088SAdriana Kobylak     }
796fd4a6088SAdriana Kobylak 
797ebf67bf7SAdriana Kobylak     InterfacesPropertiesMap interfacesAndProperties;
798ebf67bf7SAdriana Kobylak     auto objects = getManagedObjects(bus, entityManagerServiceName);
79953a27395SAdriana Kobylak     for (const auto& pair : objects)
80053a27395SAdriana Kobylak     {
80153a27395SAdriana Kobylak         std::tie(std::ignore, interfacesAndProperties) = pair;
80253a27395SAdriana Kobylak         if (maybeCall(interfacesAndProperties, maybeSetAttrWithArgsBound))
80353a27395SAdriana Kobylak         {
804d0379ea5SAdriana Kobylak             loop.exit(0);
805ebf67bf7SAdriana Kobylak             return {};
80653a27395SAdriana Kobylak         }
80753a27395SAdriana Kobylak     }
80853a27395SAdriana Kobylak 
809ebf67bf7SAdriana Kobylak     return matches;
81053a27395SAdriana Kobylak }
81153a27395SAdriana Kobylak 
812099543e4SBrad Bishop } // namespace process_hostfirmware
813099543e4SBrad Bishop } // namespace functions
814