blob: a8385773ea827b8772c397900ebbb9402e9ee41f [file] [log] [blame]
Cheng C Yangb4651b92019-12-19 00:59:01 +08001/*
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#include "system.hpp"
18
19#include "mdrv2.hpp"
20
Kuiying Wang26de0d72020-11-18 19:31:46 +080021#include <fstream>
Cheng C Yangb4651b92019-12-19 00:59:01 +080022#include <iomanip>
23#include <iostream>
24#include <sstream>
Jayaprakash Mutyala7393e482022-09-19 18:18:59 +000025
26static constexpr const char* biosActiveObjPath =
27 "/xyz/openbmc_project/software/bios_active";
28static constexpr const char* biosVersionIntf =
29 "xyz.openbmc_project.Software.Version";
30static constexpr const char* biosVersionProp = "Version";
31
Cheng C Yangb4651b92019-12-19 00:59:01 +080032namespace phosphor
33{
34namespace smbios
35{
36
Jonathan Domanf2d8bb42023-07-26 10:13:34 -070037std::string System::uuid(std::string /* value */)
Cheng C Yangb4651b92019-12-19 00:59:01 +080038{
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +080039 uint8_t* dataIn = storage;
Cheng C Yangb4651b92019-12-19 00:59:01 +080040 dataIn = getSMBIOSTypePtr(dataIn, systemType);
41 if (dataIn != nullptr)
42 {
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +080043 auto systemInfo = reinterpret_cast<struct SystemInfo*>(dataIn);
Cheng C Yangb4651b92019-12-19 00:59:01 +080044 std::stringstream stream;
45 stream << std::setfill('0') << std::hex;
Jason M. Billse7770992021-05-14 13:24:33 -070046 stream << std::setw(8) << systemInfo->uuid.timeLow;
Cheng C Yangb4651b92019-12-19 00:59:01 +080047 stream << "-";
Jason M. Billse7770992021-05-14 13:24:33 -070048 stream << std::setw(4) << systemInfo->uuid.timeMid;
Cheng C Yangb4651b92019-12-19 00:59:01 +080049 stream << "-";
Jason M. Billse7770992021-05-14 13:24:33 -070050 stream << std::setw(4) << systemInfo->uuid.timeHiAndVer;
Cheng C Yangb4651b92019-12-19 00:59:01 +080051 stream << "-";
Jason M. Billse7770992021-05-14 13:24:33 -070052 stream << std::setw(2) << static_cast<int>(systemInfo->uuid.clockSeqHi);
Cheng C Yangb4651b92019-12-19 00:59:01 +080053 stream << std::setw(2)
Jason M. Billse7770992021-05-14 13:24:33 -070054 << static_cast<int>(systemInfo->uuid.clockSeqLow);
Cheng C Yangb4651b92019-12-19 00:59:01 +080055 stream << "-";
Jason M. Billse7770992021-05-14 13:24:33 -070056 static_assert(sizeof(systemInfo->uuid.node) == 6);
57 stream << std::setw(2) << static_cast<int>(systemInfo->uuid.node[0]);
58 stream << std::setw(2) << static_cast<int>(systemInfo->uuid.node[1]);
59 stream << std::setw(2) << static_cast<int>(systemInfo->uuid.node[2]);
60 stream << std::setw(2) << static_cast<int>(systemInfo->uuid.node[3]);
61 stream << std::setw(2) << static_cast<int>(systemInfo->uuid.node[4]);
62 stream << std::setw(2) << static_cast<int>(systemInfo->uuid.node[5]);
Cheng C Yangb4651b92019-12-19 00:59:01 +080063
Jason M. Bills33ae81f2023-04-26 09:06:08 -070064 return sdbusplus::server::xyz::openbmc_project::common::UUID::uuid(
Cheng C Yangb4651b92019-12-19 00:59:01 +080065 stream.str());
66 }
67
Jason M. Bills33ae81f2023-04-26 09:06:08 -070068 return sdbusplus::server::xyz::openbmc_project::common::UUID::uuid(
Cheng C Yangb4651b92019-12-19 00:59:01 +080069 "00000000-0000-0000-0000-000000000000");
70}
71
Patrick Williams966fcb12022-11-26 09:41:58 -060072static std::string getService(sdbusplus::bus_t& bus,
Jayaprakash Mutyala7393e482022-09-19 18:18:59 +000073 const std::string& objectPath,
74 const std::string& interface)
75{
Patrick Williamsc39d3df2023-05-10 07:51:14 -050076 auto method = bus.new_method_call("xyz.openbmc_project.ObjectMapper",
77 "/xyz/openbmc_project/object_mapper",
78 "xyz.openbmc_project.ObjectMapper",
79 "GetObject");
Jayaprakash Mutyala7393e482022-09-19 18:18:59 +000080
81 method.append(objectPath);
82 method.append(std::vector<std::string>({interface}));
83
84 std::vector<std::pair<std::string, std::vector<std::string>>> response;
85
86 try
87 {
88 auto reply = bus.call(method);
89 reply.read(response);
90 }
91 catch (const sdbusplus::exception_t& e)
92 {
93 lg2::error("Error in mapper method call - {ERROR}, SERVICE - "
94 "{SERVICE}, PATH - {PATH}",
95 "ERROR", e.what(), "SERVICE", objectPath.c_str(), "PATH",
96 interface.c_str());
97
98 return std::string{};
99 }
100
101 return response[0].first;
102}
103
Patrick Williams966fcb12022-11-26 09:41:58 -0600104static void setProperty(sdbusplus::bus_t& bus, const std::string& objectPath,
Jayaprakash Mutyala7393e482022-09-19 18:18:59 +0000105 const std::string& interface,
106 const std::string& propertyName,
107 const std::string& value)
108{
109 auto service = getService(bus, objectPath, interface);
110 if (service.empty())
111 {
112 return;
113 }
114
115 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
116 "org.freedesktop.DBus.Properties", "Set");
117 method.append(interface.c_str(), propertyName.c_str(),
118 std::variant<std::string>{value});
119
120 bus.call_noreply(method);
121}
122
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700123std::string System::version(std::string /* value */)
Cheng C Yangb4651b92019-12-19 00:59:01 +0800124{
125 std::string result = "No BIOS Version";
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800126 uint8_t* dataIn = storage;
Cheng C Yangb4651b92019-12-19 00:59:01 +0800127 dataIn = getSMBIOSTypePtr(dataIn, biosType);
128 if (dataIn != nullptr)
129 {
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800130 auto biosInfo = reinterpret_cast<struct BIOSInfo*>(dataIn);
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500131 std::string tempS = positionToString(biosInfo->biosVersion,
132 biosInfo->length, dataIn);
Kuiying Wang26de0d72020-11-18 19:31:46 +0800133 if (std::find_if(tempS.begin(), tempS.end(),
134 [](char ch) { return !isprint(ch); }) != tempS.end())
135 {
Josh Lehan027277a2023-09-11 05:28:19 -0700136 std::ofstream smbiosFile(smbiosFilePath, std::ios_base::trunc);
Kuiying Wang26de0d72020-11-18 19:31:46 +0800137 if (!smbiosFile.good())
138 {
139 phosphor::logging::log<phosphor::logging::level::ERR>(
140 "Open MDRV2 table file failure");
141 return result;
142 }
143 smbiosFile.clear();
144 smbiosFile.close();
145 phosphor::logging::log<phosphor::logging::level::ERR>(
146 "Find non-print char, delete the broken MDRV2 table file!");
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700147 return sdbusplus::server::xyz::openbmc_project::inventory::
148 decorator::Revision::version(result);
Kuiying Wang26de0d72020-11-18 19:31:46 +0800149 }
Cheng C Yangb4651b92019-12-19 00:59:01 +0800150 result = tempS;
Jayaprakash Mutyala7393e482022-09-19 18:18:59 +0000151
Josh Lehan027277a2023-09-11 05:28:19 -0700152 setProperty(*bus, biosActiveObjPath, biosVersionIntf, biosVersionProp,
Jayaprakash Mutyala7393e482022-09-19 18:18:59 +0000153 result);
Cheng C Yangb4651b92019-12-19 00:59:01 +0800154 }
Alex Schendel5f2d6272021-09-22 10:35:40 -0700155 lg2::info("VERSION INFO - BIOS - {VER}", "VER", result);
Jayaprakash Mutyala7393e482022-09-19 18:18:59 +0000156
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700157 return sdbusplus::server::xyz::openbmc_project::inventory::decorator::
Cheng C Yangb4651b92019-12-19 00:59:01 +0800158 Revision::version(result);
159}
160
161} // namespace smbios
162} // namespace phosphor