blob: e5dc276ac5314ed2d711c4fa4f32415879100eb9 [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
Patrick Venturece07ee02018-09-19 18:09:32 -070021#include <cstring>
Willy Tuff3cd8e2021-09-14 22:49:55 -070022#include <ipmid/api-types.hpp>
Willy Tub4e37042021-10-12 17:12:30 -070023#include <span>
Willy Tuff3cd8e2021-09-14 22:49:55 -070024#include <vector>
Patrick Venture4d49ae62018-09-17 11:35:32 -070025
26namespace google
27{
28namespace ipmi
29{
Patrick Venture4d49ae62018-09-17 11:35:32 -070030
31struct CpldRequest
32{
Patrick Venture4d49ae62018-09-17 11:35:32 -070033 uint8_t id;
34} __attribute__((packed));
35
Patrick Venture4d49ae62018-09-17 11:35:32 -070036//
37// Handle reading the cpld version from the tmpfs.
38//
Willy Tub4e37042021-10-12 17:12:30 -070039Resp cpldVersion(std::span<const uint8_t> data, const HandlerInterface* handler)
Patrick Venture4d49ae62018-09-17 11:35:32 -070040{
Patrick Ventureacd54232018-10-16 16:58:59 -070041 struct CpldRequest request;
42
Willy Tuff3cd8e2021-09-14 22:49:55 -070043 if (data.size() < sizeof(request))
Patrick Venture4d49ae62018-09-17 11:35:32 -070044 {
Patrick Venturece07ee02018-09-19 18:09:32 -070045 std::fprintf(stderr, "Invalid command length: %u\n",
Willy Tuff3cd8e2021-09-14 22:49:55 -070046 static_cast<uint32_t>(data.size()));
47 return ::ipmi::responseReqDataLenInvalid();
Patrick Venture4d49ae62018-09-17 11:35:32 -070048 }
49
Willy Tuff3cd8e2021-09-14 22:49:55 -070050 // data[0] is the CPLD id. "/run/cpld{id}.version" is what we read.
Patrick Venture4d49ae62018-09-17 11:35:32 -070051 // Verified that this cast actually returns the value 255 and not something
Willy Tuff3cd8e2021-09-14 22:49:55 -070052 // negative in the case where data[0] is 0xff. However, it looks weird
Patrick Venture4d49ae62018-09-17 11:35:32 -070053 // since I would expect int(uint8(0xff)) to be -1. So, just cast it
54 // unsigned. we're casting to an int width to avoid it thinking it's a
55 // letter, because it does that.
Willy Tuff3cd8e2021-09-14 22:49:55 -070056 std::memcpy(&request, data.data(), sizeof(request));
Patrick Venture4d49ae62018-09-17 11:35:32 -070057
Patrick Venture4d49ae62018-09-17 11:35:32 -070058 try
59 {
Patrick Venturebb90d4f2019-03-15 13:42:06 -070060 auto values =
61 handler->getCpldVersion(static_cast<unsigned int>(request.id));
62
63 // Truncate if the version is too high (documented).
Willy Tuff3cd8e2021-09-14 22:49:55 -070064 auto major = std::get<0>(values);
65 auto minor = std::get<1>(values);
66 auto point = std::get<2>(values);
67 auto subpoint = std::get<3>(values);
Patrick Venturebb90d4f2019-03-15 13:42:06 -070068
Willy Tuff3cd8e2021-09-14 22:49:55 -070069 return ::ipmi::responseSuccess(
70 SysOEMCommands::SysCpldVersion,
71 std::vector<std::uint8_t>{major, minor, point, subpoint});
Patrick Venture4d49ae62018-09-17 11:35:32 -070072 }
Patrick Venturebb90d4f2019-03-15 13:42:06 -070073 catch (const IpmiException& e)
Patrick Venture4d49ae62018-09-17 11:35:32 -070074 {
Willy Tuff3cd8e2021-09-14 22:49:55 -070075 return ::ipmi::response(e.getIpmiError());
Patrick Venture4d49ae62018-09-17 11:35:32 -070076 }
Patrick Venture4d49ae62018-09-17 11:35:32 -070077}
78
79} // namespace ipmi
80} // namespace google