xref: /openbmc/smbios-mdr/include/mdrv2.hpp (revision 1d73dccc)
1 /*
2 // Copyright (c) 2018 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 
17 #pragma once
18 #include "cpu.hpp"
19 #include "dimm.hpp"
20 #include "pcieslot.hpp"
21 #include "smbios_mdrv2.hpp"
22 #include "system.hpp"
23 
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 
28 #include <boost/asio/io_context.hpp>
29 #include <boost/asio/steady_timer.hpp>
30 #include <boost/container/flat_map.hpp>
31 #include <phosphor-logging/elog-errors.hpp>
32 #include <phosphor-logging/lg2.hpp>
33 #include <phosphor-logging/log.hpp>
34 #include <sdbusplus/asio/object_server.hpp>
35 #include <sdbusplus/server.hpp>
36 #include <sdbusplus/timer.hpp>
37 #include <xyz/openbmc_project/Smbios/MDR_V2/server.hpp>
38 
39 #include <filesystem>
40 #include <memory>
41 
42 namespace phosphor
43 {
44 namespace smbios
45 {
46 
47 using RecordVariant =
48     std::variant<std::string, uint64_t, uint32_t, uint16_t, uint8_t>;
49 
50 static constexpr const char* defaultObjectPath =
51     "/xyz/openbmc_project/Smbios/MDR_V2";
52 static constexpr const char* smbiosInterfaceName =
53     "xyz.openbmc_project.Smbios.GetRecordType";
54 static constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
55 static constexpr const char* mapperPath = "/xyz/openbmc_project/object_mapper";
56 static constexpr const char* mapperInterface =
57     "xyz.openbmc_project.ObjectMapper";
58 static constexpr const char* defaultInventoryPath =
59     "/xyz/openbmc_project/inventory/system";
60 static constexpr const char* systemInterface =
61     "xyz.openbmc_project.Inventory.Item.System";
62 static constexpr const char* boardInterface =
63     "xyz.openbmc_project.Inventory.Item.Board";
64 constexpr const int limitEntryLen = 0xff;
65 
66 // Avoid putting multiple interfaces with same name on same object
placeGetRecordType(const std::string & objectPath)67 static std::string placeGetRecordType(const std::string& objectPath)
68 {
69     if (objectPath != defaultObjectPath)
70     {
71         // Place GetRecordType interface on object itself, not parent
72         return objectPath;
73     }
74 
75     std::filesystem::path path(objectPath);
76 
77     // As there is only one default, safe to place it on the common parent
78     return path.parent_path().string();
79 }
80 
81 class MDRV2 :
82     sdbusplus::server::object_t<
83         sdbusplus::server::xyz::openbmc_project::smbios::MDRV2>
84 {
85   public:
86     MDRV2() = delete;
87     MDRV2(const MDRV2&) = delete;
88     MDRV2& operator=(const MDRV2&) = delete;
89     MDRV2(MDRV2&&) = delete;
90     MDRV2& operator=(MDRV2&&) = delete;
91 
~MDRV2()92     virtual ~MDRV2()
93     {
94         if (smbiosInterface)
95         {
96             if (objServer)
97             {
98                 // Must manually undo add_interface()
99                 objServer->remove_interface(smbiosInterface);
100             }
101         }
102     }
103 
MDRV2(std::shared_ptr<boost::asio::io_context> io,std::shared_ptr<sdbusplus::asio::connection> conn,std::shared_ptr<sdbusplus::asio::object_server> obj,std::string filePath,std::string objectPath,std::string inventoryPath)104     MDRV2(std::shared_ptr<boost::asio::io_context> io,
105           std::shared_ptr<sdbusplus::asio::connection> conn,
106           std::shared_ptr<sdbusplus::asio::object_server> obj,
107           std::string filePath, std::string objectPath,
108           std::string inventoryPath) :
109         sdbusplus::server::object_t<
110             sdbusplus::server::xyz::openbmc_project::smbios::MDRV2>(
111             *conn, objectPath.c_str()),
112         timer(*io), bus(conn), objServer(std::move(obj)),
113         smbiosInterface(objServer->add_interface(placeGetRecordType(objectPath),
114                                                  smbiosInterfaceName)),
115         smbiosFilePath(std::move(filePath)),
116         smbiosObjectPath(std::move(objectPath)),
117         smbiosInventoryPath(std::move(inventoryPath))
118     {
119         lg2::info("SMBIOS data file path: {F}", "F", smbiosFilePath);
120         lg2::info("SMBIOS control object: {O}", "O", smbiosObjectPath);
121         lg2::info("SMBIOS inventory path: {I}", "I", smbiosInventoryPath);
122 
123         smbiosDir.agentVersion = smbiosAgentVersion;
124         smbiosDir.dirVersion = smbiosDirVersion;
125         smbiosDir.dirEntries = 1;
126         directoryEntries(smbiosDir.dirEntries);
127         smbiosDir.status = 1;
128         smbiosDir.remoteDirVersion = 0;
129 
130         std::copy(smbiosTableId.begin(), smbiosTableId.end(),
131                   smbiosDir.dir[smbiosDirIndex].common.id.dataInfo);
132 
133         smbiosDir.dir[smbiosDirIndex].dataStorage = smbiosTableStorage;
134 
135         agentSynchronizeData();
136 
137         smbiosInterface->register_method("GetRecordType", [this](size_t type) {
138             return getRecordType(type);
139         });
140         smbiosInterface->initialize();
141     }
142 
143     std::vector<uint8_t> getDirectoryInformation(uint8_t dirIndex) override;
144 
145     std::vector<uint8_t> getDataInformation(uint8_t idIndex) override;
146 
147     bool sendDirectoryInformation(
148         uint8_t dirVersion, uint8_t dirIndex, uint8_t returnedEntries,
149         uint8_t remainingEntries, std::vector<uint8_t> dirEntry) override;
150 
151     std::vector<uint8_t> getDataOffer() override;
152 
153     bool sendDataInformation(uint8_t idIndex, uint8_t flag, uint32_t dataLen,
154                              uint32_t dataVer, uint32_t timeStamp) override;
155 
156     int findIdIndex(std::vector<uint8_t> dataInfo) override;
157 
158     bool agentSynchronizeData() override;
159 
160     std::vector<uint32_t>
161         synchronizeDirectoryCommonData(uint8_t idIndex, uint32_t size) override;
162 
163     uint8_t directoryEntries(uint8_t value) override;
164 
165     std::vector<boost::container::flat_map<std::string, RecordVariant>>
166         getRecordType(size_t type);
167 
168   private:
169     boost::asio::steady_timer timer;
170 
171     std::shared_ptr<sdbusplus::asio::connection> bus;
172     std::shared_ptr<sdbusplus::asio::object_server> objServer;
173 
174     Mdr2DirStruct smbiosDir;
175 
176     bool readDataFromFlash(MDRSMBIOSHeader* mdrHdr, uint8_t* data);
177     bool checkSMBIOSVersion(uint8_t* dataIn);
178 
179     const std::array<uint8_t, 16> smbiosTableId{
180         40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 0x42};
181     uint8_t smbiosTableStorage[smbiosTableStorageSize] = {};
182 
183     bool smbiosIsUpdating(uint8_t index);
184     bool smbiosIsAvailForUpdate(uint8_t index);
185     inline uint8_t smbiosValidFlag(uint8_t index);
186     void systemInfoUpdate(void);
187 
188     std::optional<size_t> getTotalCpuSlot(void);
189     std::optional<size_t> getTotalDimmSlot(void);
190     std::optional<size_t> getTotalPcieSlot(void);
191     std::vector<std::unique_ptr<Cpu>> cpus;
192     std::vector<std::unique_ptr<Dimm>> dimms;
193     std::vector<std::unique_ptr<Pcie>> pcies;
194     std::unique_ptr<System> system;
195     std::shared_ptr<sdbusplus::asio::dbus_interface> smbiosInterface;
196 
197     std::string smbiosFilePath;
198     std::string smbiosObjectPath;
199     std::string smbiosInventoryPath;
200     std::unique_ptr<sdbusplus::bus::match_t> motherboardConfigMatch;
201 };
202 
203 } // namespace smbios
204 } // namespace phosphor
205