Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 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 "process.hpp" |
| 18 | |
| 19 | #include "ipmi.hpp" |
| 20 | |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 21 | #include <ipmiblob/crc.hpp> |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 22 | #include <ipmid/api-types.hpp> |
Patrick Williams | 5250957 | 2023-05-10 07:51:18 -0500 | [diff] [blame] | 23 | |
| 24 | #include <cstring> |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 25 | #include <span> |
Patrick Venture | b15b305 | 2018-10-04 10:31:53 -0700 | [diff] [blame] | 26 | #include <unordered_map> |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 27 | #include <utility> |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
| 30 | namespace blobs |
| 31 | { |
| 32 | |
| 33 | /* Used by all commands with data. */ |
| 34 | struct BmcRx |
| 35 | { |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 36 | uint16_t crc; |
| 37 | uint8_t data; /* one byte minimum of data. */ |
| 38 | } __attribute__((packed)); |
| 39 | |
Patrick Venture | b15b305 | 2018-10-04 10:31:53 -0700 | [diff] [blame] | 40 | static const std::unordered_map<BlobOEMCommands, IpmiBlobHandler> handlers = { |
| 41 | {BlobOEMCommands::bmcBlobGetCount, getBlobCount}, |
| 42 | {BlobOEMCommands::bmcBlobEnumerate, enumerateBlob}, |
| 43 | {BlobOEMCommands::bmcBlobOpen, openBlob}, |
| 44 | {BlobOEMCommands::bmcBlobRead, readBlob}, |
| 45 | {BlobOEMCommands::bmcBlobWrite, writeBlob}, |
| 46 | {BlobOEMCommands::bmcBlobCommit, commitBlob}, |
| 47 | {BlobOEMCommands::bmcBlobClose, closeBlob}, |
| 48 | {BlobOEMCommands::bmcBlobDelete, deleteBlob}, |
| 49 | {BlobOEMCommands::bmcBlobStat, statBlob}, |
| 50 | {BlobOEMCommands::bmcBlobSessionStat, sessionStatBlob}, |
Patrick Venture | 5c4b17b | 2018-10-04 10:32:22 -0700 | [diff] [blame] | 51 | {BlobOEMCommands::bmcBlobWriteMeta, writeMeta}, |
Patrick Venture | b15b305 | 2018-10-04 10:31:53 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 54 | IpmiBlobHandler validateBlobCommand(uint8_t cmd, std::span<const uint8_t> data) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 55 | { |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 56 | size_t requestLength = data.size(); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 57 | /* We know dataLen is at least 1 already */ |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 58 | auto command = static_cast<BlobOEMCommands>(cmd); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 59 | |
| 60 | /* Validate it's at least well-formed. */ |
| 61 | if (!validateRequestLength(command, requestLength)) |
| 62 | { |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 63 | return [](ManagerInterface*, std::span<const uint8_t>) { |
| 64 | return ipmi::responseReqDataLenInvalid(); |
| 65 | }; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* If there is a payload. */ |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 69 | if (requestLength > sizeof(cmd)) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 70 | { |
| 71 | /* Verify the request includes: command, crc16, data */ |
| 72 | if (requestLength < sizeof(struct BmcRx)) |
| 73 | { |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 74 | return [](ManagerInterface*, std::span<const uint8_t>) { |
| 75 | return ipmi::responseReqDataLenInvalid(); |
| 76 | }; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* We don't include the command byte at offset 0 as part of the crc |
| 80 | * payload area or the crc bytes at the beginning. |
| 81 | */ |
| 82 | size_t requestBodyLen = requestLength - 3; |
| 83 | |
| 84 | /* We start after the command byte. */ |
| 85 | std::vector<uint8_t> bytes(requestBodyLen); |
| 86 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 87 | /* It likely has a well-formed payload. |
| 88 | * Get the first two bytes of the request for crc. |
| 89 | */ |
| 90 | uint16_t crc; |
| 91 | if (data.size() < sizeof(crc)) |
| 92 | { |
| 93 | return [](ManagerInterface*, std::span<const uint8_t>) { |
| 94 | return ipmi::responseReqDataLenInvalid(); |
| 95 | }; |
| 96 | } |
| 97 | std::memcpy(&crc, data.data(), sizeof(crc)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 98 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 99 | /* Set the in-place CRC to zero. |
| 100 | * Remove the first two bytes for crc and get the reset of the request. |
| 101 | */ |
| 102 | data = data.subspan(sizeof(crc)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 103 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 104 | /* Crc expected but didn't match. */ |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 105 | if (crc != ipmiblob::generateCrc( |
| 106 | std::vector<uint8_t>(data.begin(), data.end()))) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 107 | { |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 108 | return [](ManagerInterface*, std::span<const uint8_t>) { |
| 109 | return ipmi::responseUnspecifiedError(); |
| 110 | }; |
| 111 | }; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Patrick Venture | b15b305 | 2018-10-04 10:31:53 -0700 | [diff] [blame] | 114 | /* Grab the corresponding handler for the command. */ |
| 115 | auto found = handlers.find(command); |
| 116 | if (found == handlers.end()) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 117 | { |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 118 | return [](ManagerInterface*, std::span<const uint8_t>) { |
| 119 | return ipmi::responseInvalidFieldRequest(); |
| 120 | }; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Patrick Venture | b15b305 | 2018-10-04 10:31:53 -0700 | [diff] [blame] | 123 | return found->second; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 126 | Resp processBlobCommand(IpmiBlobHandler cmd, ManagerInterface* mgr, |
Willy Tu | 83f9992 | 2022-06-22 14:59:07 -0700 | [diff] [blame] | 127 | std::span<const uint8_t> data, size_t maxSize) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 128 | { |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 129 | Resp result = cmd(mgr, data); |
| 130 | if (std::get<0>(result) != ipmi::ccSuccess) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 131 | { |
| 132 | return result; |
| 133 | } |
| 134 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 135 | std::vector<uint8_t>& response = std::get<0>( |
| 136 | // std::variant<std::vector<uint8_t>> |
| 137 | *std::get<1>(result)); |
| 138 | size_t replyLength = response.size(); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 139 | |
| 140 | /* The command, whatever it was, returned success. */ |
| 141 | if (replyLength == 0) |
| 142 | { |
| 143 | return result; |
| 144 | } |
| 145 | |
Patrick Venture | d1c3e86 | 2019-01-10 13:12:20 -0800 | [diff] [blame] | 146 | /* Read can return 0 bytes, and just a CRC, otherwise you need a CRC and 1 |
| 147 | * byte, therefore the limit is 2 bytes. |
| 148 | */ |
| 149 | if (replyLength < (sizeof(uint16_t))) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 150 | { |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 151 | return ipmi::responseUnspecifiedError(); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Willy Tu | 83f9992 | 2022-06-22 14:59:07 -0700 | [diff] [blame] | 154 | /* Make sure the reply size fits the ipmi buffer */ |
| 155 | if (replyLength > maxSize) |
| 156 | { |
| 157 | return ipmi::responseResponseError(); |
| 158 | } |
| 159 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 160 | /* The command, whatever it was, replied, so let's set the CRC. */ |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 161 | std::span<const uint8_t> responseView = response; |
| 162 | responseView = responseView.subspan(sizeof(uint16_t)); |
| 163 | std::vector<std::uint8_t> crcBuffer(responseView.begin(), |
| 164 | responseView.end()); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 165 | /* Copy the CRC into place. */ |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 166 | uint16_t crcValue = ipmiblob::generateCrc(crcBuffer); |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 167 | if (response.size() < sizeof(crcValue)) |
| 168 | { |
| 169 | return ipmi::responseReqDataLenInvalid(); |
| 170 | } |
| 171 | std::memcpy(response.data(), &crcValue, sizeof(crcValue)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 172 | |
| 173 | return result; |
| 174 | } |
Patrick Venture | 03fd5b8 | 2020-05-07 14:51:42 -0700 | [diff] [blame] | 175 | |
Willy Tu | 83f9992 | 2022-06-22 14:59:07 -0700 | [diff] [blame] | 176 | Resp handleBlobCommand(uint8_t cmd, std::vector<uint8_t> data, size_t maxSize) |
Patrick Venture | 03fd5b8 | 2020-05-07 14:51:42 -0700 | [diff] [blame] | 177 | { |
Patrick Venture | 03fd5b8 | 2020-05-07 14:51:42 -0700 | [diff] [blame] | 178 | /* on failure rc is set to the corresponding IPMI error. */ |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 179 | return processBlobCommand(validateBlobCommand(cmd, data), getBlobManager(), |
Willy Tu | 83f9992 | 2022-06-22 14:59:07 -0700 | [diff] [blame] | 180 | data, maxSize); |
Patrick Venture | 03fd5b8 | 2020-05-07 14:51:42 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 183 | } // namespace blobs |