blob: 4c1dae1f0293b7ac5351a2bfeb41a6191d3b61a7 [file] [log] [blame]
Willy Tua2056e92021-10-10 13:36:16 -07001// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Patrick Venture4d49ae62018-09-17 11:35:32 -070014
15#include "cpld.hpp"
16
Patrick Venture0e9aae52020-08-13 13:07:09 -070017#include "commands.hpp"
Patrick Venturebb90d4f2019-03-15 13:42:06 -070018#include "errors.hpp"
19#include "handler.hpp"
Patrick Venture4d49ae62018-09-17 11:35:32 -070020
Willy Tuff3cd8e2021-09-14 22:49:55 -070021#include <ipmid/api-types.hpp>
Michael Shen8d618532023-10-25 09:14:07 +000022#include <stdplus/print.hpp>
Patrick Williams444b5ea2023-05-19 13:56:42 -050023
24#include <cstring>
Willy Tub4e37042021-10-12 17:12:30 -070025#include <span>
Willy Tuff3cd8e2021-09-14 22:49:55 -070026#include <vector>
Patrick Venture4d49ae62018-09-17 11:35:32 -070027
28namespace google
29{
30namespace ipmi
31{
Patrick Venture4d49ae62018-09-17 11:35:32 -070032
33struct CpldRequest
34{
Patrick Venture4d49ae62018-09-17 11:35:32 -070035 uint8_t id;
36} __attribute__((packed));
37
Patrick Venture4d49ae62018-09-17 11:35:32 -070038//
39// Handle reading the cpld version from the tmpfs.
40//
Willy Tub4e37042021-10-12 17:12:30 -070041Resp cpldVersion(std::span<const uint8_t> data, const HandlerInterface* handler)
Patrick Venture4d49ae62018-09-17 11:35:32 -070042{
Patrick Ventureacd54232018-10-16 16:58:59 -070043 struct CpldRequest request;
44
Willy Tuff3cd8e2021-09-14 22:49:55 -070045 if (data.size() < sizeof(request))
Patrick Venture4d49ae62018-09-17 11:35:32 -070046 {
Michael Shen8d618532023-10-25 09:14:07 +000047 stdplus::print(stderr, "Invalid command length: {}\n", data.size());
Willy Tuff3cd8e2021-09-14 22:49:55 -070048 return ::ipmi::responseReqDataLenInvalid();
Patrick Venture4d49ae62018-09-17 11:35:32 -070049 }
50
Willy Tuff3cd8e2021-09-14 22:49:55 -070051 // data[0] is the CPLD id. "/run/cpld{id}.version" is what we read.
Patrick Venture4d49ae62018-09-17 11:35:32 -070052 // Verified that this cast actually returns the value 255 and not something
Willy Tuff3cd8e2021-09-14 22:49:55 -070053 // negative in the case where data[0] is 0xff. However, it looks weird
Patrick Venture4d49ae62018-09-17 11:35:32 -070054 // since I would expect int(uint8(0xff)) to be -1. So, just cast it
55 // unsigned. we're casting to an int width to avoid it thinking it's a
56 // letter, because it does that.
Willy Tuff3cd8e2021-09-14 22:49:55 -070057 std::memcpy(&request, data.data(), sizeof(request));
Patrick Venture4d49ae62018-09-17 11:35:32 -070058
Patrick Venture4d49ae62018-09-17 11:35:32 -070059 try
60 {
Patrick Venturebb90d4f2019-03-15 13:42:06 -070061 auto values =
62 handler->getCpldVersion(static_cast<unsigned int>(request.id));
63
64 // Truncate if the version is too high (documented).
Willy Tuff3cd8e2021-09-14 22:49:55 -070065 auto major = std::get<0>(values);
66 auto minor = std::get<1>(values);
67 auto point = std::get<2>(values);
68 auto subpoint = std::get<3>(values);
Patrick Venturebb90d4f2019-03-15 13:42:06 -070069
Willy Tuff3cd8e2021-09-14 22:49:55 -070070 return ::ipmi::responseSuccess(
71 SysOEMCommands::SysCpldVersion,
72 std::vector<std::uint8_t>{major, minor, point, subpoint});
Patrick Venture4d49ae62018-09-17 11:35:32 -070073 }
Patrick Venturebb90d4f2019-03-15 13:42:06 -070074 catch (const IpmiException& e)
Patrick Venture4d49ae62018-09-17 11:35:32 -070075 {
Willy Tuff3cd8e2021-09-14 22:49:55 -070076 return ::ipmi::response(e.getIpmiError());
Patrick Venture4d49ae62018-09-17 11:35:32 -070077 }
Patrick Venture4d49ae62018-09-17 11:35:32 -070078}
79
80} // namespace ipmi
81} // namespace google