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