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