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