xref: /openbmc/google-ipmi-sys/bm_instance.cpp (revision 8c0094e4eb82029af1d111d2736111cbdda59b63)
1559cb011SBrandon Kim // Copyright 2024 Google LLC
2559cb011SBrandon Kim //
3559cb011SBrandon Kim // Licensed under the Apache License, Version 2.0 (the "License");
4559cb011SBrandon Kim // you may not use this file except in compliance with the License.
5559cb011SBrandon Kim // You may obtain a copy of the License at
6559cb011SBrandon Kim //
7559cb011SBrandon Kim //      http://www.apache.org/licenses/LICENSE-2.0
8559cb011SBrandon Kim //
9559cb011SBrandon Kim // Unless required by applicable law or agreed to in writing, software
10559cb011SBrandon Kim // distributed under the License is distributed on an "AS IS" BASIS,
11559cb011SBrandon Kim // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12559cb011SBrandon Kim // See the License for the specific language governing permissions and
13559cb011SBrandon Kim // limitations under the License.
14559cb011SBrandon Kim 
15559cb011SBrandon Kim #include "bm_instance.hpp"
16559cb011SBrandon Kim 
17559cb011SBrandon Kim #include "commands.hpp"
18559cb011SBrandon Kim #include "errors.hpp"
19559cb011SBrandon Kim #include "handler.hpp"
20559cb011SBrandon Kim 
21559cb011SBrandon Kim #include <ipmid/api-types.hpp>
22559cb011SBrandon Kim #include <stdplus/print.hpp>
23559cb011SBrandon Kim 
24559cb011SBrandon Kim #include <span>
25559cb011SBrandon Kim #include <vector>
26559cb011SBrandon Kim 
27559cb011SBrandon Kim namespace google
28559cb011SBrandon Kim {
29559cb011SBrandon Kim namespace ipmi
30559cb011SBrandon Kim {
31559cb011SBrandon Kim 
32559cb011SBrandon Kim namespace
33559cb011SBrandon Kim {
34559cb011SBrandon Kim #ifndef MAX_IPMI_BUFFER
35559cb011SBrandon Kim #define MAX_IPMI_BUFFER 64
36559cb011SBrandon Kim #endif
37559cb011SBrandon Kim } // namespace
38559cb011SBrandon Kim 
39559cb011SBrandon Kim struct BMInstancePropertyRequest
40559cb011SBrandon Kim {
41559cb011SBrandon Kim     std::uint8_t bmInstancePropertyType;
42559cb011SBrandon Kim } __attribute__((packed));
43559cb011SBrandon Kim 
getBMInstanceProperty(std::span<const uint8_t> data,HandlerInterface * handler)44559cb011SBrandon Kim Resp getBMInstanceProperty(std::span<const uint8_t> data,
45559cb011SBrandon Kim                            HandlerInterface* handler)
46559cb011SBrandon Kim {
47559cb011SBrandon Kim     if (data.size() < sizeof(struct BMInstancePropertyRequest))
48559cb011SBrandon Kim     {
49559cb011SBrandon Kim         stdplus::print(stderr, "Invalid command length: {}\n",
50559cb011SBrandon Kim                        static_cast<uint32_t>(data.size()));
51559cb011SBrandon Kim         return ::ipmi::responseReqDataLenInvalid();
52559cb011SBrandon Kim     }
53559cb011SBrandon Kim 
54559cb011SBrandon Kim     std::string bmInstanceProperty =
55559cb011SBrandon Kim         handler->getBMInstanceProperty(/*type=*/data[0]);
56559cb011SBrandon Kim 
57*8c0094e4SPatrick Williams     const size_t length =
58*8c0094e4SPatrick Williams         sizeof(struct BMInstancePropertyReply) + bmInstanceProperty.size();
59559cb011SBrandon Kim 
60559cb011SBrandon Kim     if (length > MAX_IPMI_BUFFER)
61559cb011SBrandon Kim     {
62559cb011SBrandon Kim         stdplus::print(stderr, "Response would overflow response buffer\n");
63559cb011SBrandon Kim         return ::ipmi::responseInvalidCommand();
64559cb011SBrandon Kim     }
65559cb011SBrandon Kim 
66559cb011SBrandon Kim     std::vector<std::uint8_t> reply;
67559cb011SBrandon Kim     reply.reserve(length);
68559cb011SBrandon Kim     reply.emplace_back(bmInstanceProperty.size());
69559cb011SBrandon Kim     reply.insert(reply.end(), bmInstanceProperty.begin(),
70559cb011SBrandon Kim                  bmInstanceProperty.end());
71559cb011SBrandon Kim 
72559cb011SBrandon Kim     return ::ipmi::responseSuccess(SysOEMCommands::SysGetBMInstanceProperty,
73559cb011SBrandon Kim                                    reply);
74559cb011SBrandon Kim }
75559cb011SBrandon Kim } // namespace ipmi
76559cb011SBrandon Kim } // namespace google
77