blob: 4e431f4f32e7cfdc88a0eee3eecdcadad2ac3f68 [file] [log] [blame]
Cheng C Yang3e3269a2019-12-02 15:11:30 +08001/*
Zhikui Ren18a5ab92020-09-01 21:35:20 -07002// Copyright (c) 2018 Intel Corporation
Cheng C Yang3e3269a2019-12-02 15:11:30 +08003//
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
Cheng C Yang43c6a1d2019-12-19 00:48:34 +080018#include "cpu.hpp"
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +080019#include "dimm.hpp"
Jie Yang08e4a6d2021-08-23 13:07:41 -070020#include "pcieslot.hpp"
Zhikui Ren18a5ab92020-09-01 21:35:20 -070021#include "smbios_mdrv2.hpp"
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +080022#include "system.hpp"
Cheng C Yang3e3269a2019-12-02 15:11:30 +080023
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27
Zhikui Ren18a5ab92020-09-01 21:35:20 -070028#include <boost/asio/io_context.hpp>
29#include <boost/asio/steady_timer.hpp>
30#include <boost/container/flat_map.hpp>
Cheng C Yang3e3269a2019-12-02 15:11:30 +080031#include <phosphor-logging/elog-errors.hpp>
Alex Schendel5f2d6272021-09-22 10:35:40 -070032#include <phosphor-logging/lg2.hpp>
Cheng C Yang3e3269a2019-12-02 15:11:30 +080033#include <phosphor-logging/log.hpp>
Zhikui Ren18a5ab92020-09-01 21:35:20 -070034#include <sdbusplus/asio/object_server.hpp>
Cheng C Yang3e3269a2019-12-02 15:11:30 +080035#include <sdbusplus/server.hpp>
36#include <sdbusplus/timer.hpp>
Cheng C Yangeecaf822019-12-19 00:34:23 +080037#include <xyz/openbmc_project/Smbios/MDR_V2/server.hpp>
Cheng C Yang3e3269a2019-12-02 15:11:30 +080038
Josh Lehan027277a2023-09-11 05:28:19 -070039#include <filesystem>
40#include <memory>
Cheng C Yang3e3269a2019-12-02 15:11:30 +080041
Cheng C Yang3e3269a2019-12-02 15:11:30 +080042namespace phosphor
43{
44namespace smbios
45{
46
Josh Lehan027277a2023-09-11 05:28:19 -070047using RecordVariant =
48 std::variant<std::string, uint64_t, uint32_t, uint16_t, uint8_t>;
49
50static constexpr const char* defaultObjectPath =
51 "/xyz/openbmc_project/Smbios/MDR_V2";
Zhikui Ren18a5ab92020-09-01 21:35:20 -070052static constexpr const char* smbiosInterfaceName =
53 "xyz.openbmc_project.Smbios.GetRecordType";
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +000054static constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
55static constexpr const char* mapperPath = "/xyz/openbmc_project/object_mapper";
56static constexpr const char* mapperInterface =
57 "xyz.openbmc_project.ObjectMapper";
Josh Lehan027277a2023-09-11 05:28:19 -070058static constexpr const char* defaultInventoryPath =
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +000059 "/xyz/openbmc_project/inventory/system";
60static constexpr const char* systemInterface =
61 "xyz.openbmc_project.Inventory.Item.System";
Zhikui Ren18a5ab92020-09-01 21:35:20 -070062constexpr const int limitEntryLen = 0xff;
63
Josh Lehan027277a2023-09-11 05:28:19 -070064// Avoid putting multiple interfaces with same name on same object
65static std::string placeGetRecordType(const std::string& objectPath)
66{
67 if (objectPath != defaultObjectPath)
68 {
69 // Place GetRecordType interface on object itself, not parent
70 return objectPath;
71 }
72
73 std::filesystem::path path(objectPath);
74
75 // As there is only one default, safe to place it on the common parent
76 return path.parent_path().string();
77}
78
Jason M. Billsa3f5b382023-04-26 08:17:28 -070079class MDRV2 :
Patrick Williams77b9c472022-07-22 19:26:57 -050080 sdbusplus::server::object_t<
Jason M. Bills33ae81f2023-04-26 09:06:08 -070081 sdbusplus::server::xyz::openbmc_project::smbios::MDRV2>
Cheng C Yang3e3269a2019-12-02 15:11:30 +080082{
83 public:
Jason M. Billsa3f5b382023-04-26 08:17:28 -070084 MDRV2() = delete;
85 MDRV2(const MDRV2&) = delete;
86 MDRV2& operator=(const MDRV2&) = delete;
87 MDRV2(MDRV2&&) = delete;
88 MDRV2& operator=(MDRV2&&) = delete;
Cheng C Yang3e3269a2019-12-02 15:11:30 +080089
Josh Lehan027277a2023-09-11 05:28:19 -070090 virtual ~MDRV2()
Cheng C Yang3e3269a2019-12-02 15:11:30 +080091 {
Josh Lehan027277a2023-09-11 05:28:19 -070092 if (smbiosInterface)
93 {
94 if (objServer)
95 {
96 // Must manually undo add_interface()
97 objServer->remove_interface(smbiosInterface);
98 }
99 }
100 }
101
102 MDRV2(std::shared_ptr<boost::asio::io_context> io,
103 std::shared_ptr<sdbusplus::asio::connection> conn,
104 std::shared_ptr<sdbusplus::asio::object_server> obj,
105 std::string filePath, std::string objectPath,
106 std::string inventoryPath) :
107 sdbusplus::server::object_t<
108 sdbusplus::server::xyz::openbmc_project::smbios::MDRV2>(
109 *conn, objectPath.c_str()),
110 timer(*io), bus(conn), objServer(std::move(obj)),
111 smbiosInterface(objServer->add_interface(placeGetRecordType(objectPath),
112 smbiosInterfaceName)),
113 smbiosFilePath(std::move(filePath)),
114 smbiosObjectPath(std::move(objectPath)),
115 smbiosInventoryPath(std::move(inventoryPath))
116 {
117 lg2::info("SMBIOS data file path: {F}", "F", smbiosFilePath);
118 lg2::info("SMBIOS control object: {O}", "O", smbiosObjectPath);
119 lg2::info("SMBIOS inventory path: {I}", "I", smbiosInventoryPath);
120
Cheng C Yang3e3269a2019-12-02 15:11:30 +0800121 smbiosDir.agentVersion = smbiosAgentVersion;
Josh Lehanf079e832023-09-19 15:52:52 -0700122 smbiosDir.dirVersion = smbiosDirVersion;
Cheng C Yang3e3269a2019-12-02 15:11:30 +0800123 smbiosDir.dirEntries = 1;
124 directoryEntries(smbiosDir.dirEntries);
125 smbiosDir.status = 1;
126 smbiosDir.remoteDirVersion = 0;
127
128 std::copy(smbiosTableId.begin(), smbiosTableId.end(),
129 smbiosDir.dir[smbiosDirIndex].common.id.dataInfo);
130
131 smbiosDir.dir[smbiosDirIndex].dataStorage = smbiosTableStorage;
132
133 agentSynchronizeData();
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700134
135 smbiosInterface->register_method("GetRecordType", [this](size_t type) {
136 return getRecordType(type);
137 });
138 smbiosInterface->initialize();
Cheng C Yang3e3269a2019-12-02 15:11:30 +0800139 }
140
Cheng C Yangeecaf822019-12-19 00:34:23 +0800141 std::vector<uint8_t> getDirectoryInformation(uint8_t dirIndex) override;
142
143 std::vector<uint8_t> getDataInformation(uint8_t idIndex) override;
144
145 bool sendDirectoryInformation(uint8_t dirVersion, uint8_t dirIndex,
146 uint8_t returnedEntries,
147 uint8_t remainingEntries,
148 std::vector<uint8_t> dirEntry) override;
149
150 std::vector<uint8_t> getDataOffer() override;
151
152 bool sendDataInformation(uint8_t idIndex, uint8_t flag, uint32_t dataLen,
153 uint32_t dataVer, uint32_t timeStamp) override;
154
155 int findIdIndex(std::vector<uint8_t> dataInfo) override;
156
157 bool agentSynchronizeData() override;
158
159 std::vector<uint32_t>
160 synchronizeDirectoryCommonData(uint8_t idIndex, uint32_t size) override;
161
162 uint8_t directoryEntries(uint8_t value) override;
163
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700164 std::vector<boost::container::flat_map<std::string, RecordVariant>>
165 getRecordType(size_t type);
166
Cheng C Yang3e3269a2019-12-02 15:11:30 +0800167 private:
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700168 boost::asio::steady_timer timer;
Cheng C Yang3e3269a2019-12-02 15:11:30 +0800169
Josh Lehan027277a2023-09-11 05:28:19 -0700170 std::shared_ptr<sdbusplus::asio::connection> bus;
171 std::shared_ptr<sdbusplus::asio::object_server> objServer;
Cheng C Yang3e3269a2019-12-02 15:11:30 +0800172
173 Mdr2DirStruct smbiosDir;
174
Cheng C Yangec634252019-12-19 00:42:36 +0800175 bool readDataFromFlash(MDRSMBIOSHeader* mdrHdr, uint8_t* data);
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530176 bool checkSMBIOSVersion(uint8_t* dataIn);
Cheng C Yangec634252019-12-19 00:42:36 +0800177
Cheng C Yang3e3269a2019-12-02 15:11:30 +0800178 const std::array<uint8_t, 16> smbiosTableId{
179 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 0x42};
180 uint8_t smbiosTableStorage[smbiosTableStorageSize];
181
182 bool smbiosIsUpdating(uint8_t index);
183 bool smbiosIsAvailForUpdate(uint8_t index);
184 inline uint8_t smbiosValidFlag(uint8_t index);
185 void systemInfoUpdate(void);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800186
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700187 std::optional<size_t> getTotalCpuSlot(void);
188 std::optional<size_t> getTotalDimmSlot(void);
189 std::optional<size_t> getTotalPcieSlot(void);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800190 std::vector<std::unique_ptr<Cpu>> cpus;
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800191 std::vector<std::unique_ptr<Dimm>> dimms;
Jie Yang08e4a6d2021-08-23 13:07:41 -0700192 std::vector<std::unique_ptr<Pcie>> pcies;
Cheng C Yangb4651b92019-12-19 00:59:01 +0800193 std::unique_ptr<System> system;
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700194 std::shared_ptr<sdbusplus::asio::dbus_interface> smbiosInterface;
Josh Lehan027277a2023-09-11 05:28:19 -0700195
196 std::string smbiosFilePath;
197 std::string smbiosObjectPath;
198 std::string smbiosInventoryPath;
199 std::unique_ptr<sdbusplus::bus::match_t> motherboardConfigMatch;
Cheng C Yang3e3269a2019-12-02 15:11:30 +0800200};
201
202} // namespace smbios
203} // namespace phosphor