Willy Tu | a2056e9 | 2021-10-10 13:36:16 -0700 | [diff] [blame] | 1 | // 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 Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 14 | |
| 15 | #include "cpld.hpp" |
| 16 | |
Patrick Venture | 0e9aae5 | 2020-08-13 13:07:09 -0700 | [diff] [blame] | 17 | #include "commands.hpp" |
Patrick Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 18 | #include "errors.hpp" |
| 19 | #include "handler.hpp" |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 20 | |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 21 | #include <cstring> |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 22 | #include <ipmid/api-types.hpp> |
Willy Tu | b4e3704 | 2021-10-12 17:12:30 -0700 | [diff] [blame] | 23 | #include <span> |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 24 | #include <vector> |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 25 | |
| 26 | namespace google |
| 27 | { |
| 28 | namespace ipmi |
| 29 | { |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 30 | |
| 31 | struct CpldRequest |
| 32 | { |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 33 | uint8_t id; |
| 34 | } __attribute__((packed)); |
| 35 | |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 36 | // |
| 37 | // Handle reading the cpld version from the tmpfs. |
| 38 | // |
Willy Tu | b4e3704 | 2021-10-12 17:12:30 -0700 | [diff] [blame] | 39 | Resp cpldVersion(std::span<const uint8_t> data, const HandlerInterface* handler) |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 40 | { |
Patrick Venture | acd5423 | 2018-10-16 16:58:59 -0700 | [diff] [blame] | 41 | struct CpldRequest request; |
| 42 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 43 | if (data.size() < sizeof(request)) |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 44 | { |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 45 | std::fprintf(stderr, "Invalid command length: %u\n", |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 46 | static_cast<uint32_t>(data.size())); |
| 47 | return ::ipmi::responseReqDataLenInvalid(); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 50 | // data[0] is the CPLD id. "/run/cpld{id}.version" is what we read. |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 51 | // Verified that this cast actually returns the value 255 and not something |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 52 | // negative in the case where data[0] is 0xff. However, it looks weird |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 53 | // 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 Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 56 | std::memcpy(&request, data.data(), sizeof(request)); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 57 | |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 58 | try |
| 59 | { |
Patrick Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 60 | auto values = |
| 61 | handler->getCpldVersion(static_cast<unsigned int>(request.id)); |
| 62 | |
| 63 | // Truncate if the version is too high (documented). |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 64 | 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 Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 68 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 69 | return ::ipmi::responseSuccess( |
| 70 | SysOEMCommands::SysCpldVersion, |
| 71 | std::vector<std::uint8_t>{major, minor, point, subpoint}); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 72 | } |
Patrick Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 73 | catch (const IpmiException& e) |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 74 | { |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 75 | return ::ipmi::response(e.getIpmiError()); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 76 | } |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | } // namespace ipmi |
| 80 | } // namespace google |