blob: bad4d706c2f9b537e32359168e312b14b4a829b8 [file] [log] [blame]
Patrick Ventureef3aead2018-09-12 08:53:29 -07001/*
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 Venturede8a16e2019-03-07 12:48:32 -080022#include <ipmiblob/crc.hpp>
Patrick Ventureb15b3052018-10-04 10:31:53 -070023#include <unordered_map>
Patrick Ventureef3aead2018-09-12 08:53:29 -070024#include <vector>
25
26namespace blobs
27{
28
29/* Used by all commands with data. */
30struct BmcRx
31{
32 uint8_t cmd;
33 uint16_t crc;
34 uint8_t data; /* one byte minimum of data. */
35} __attribute__((packed));
36
Patrick Ventureb15b3052018-10-04 10:31:53 -070037static 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 Venture5c4b17b2018-10-04 10:32:22 -070048 {BlobOEMCommands::bmcBlobWriteMeta, writeMeta},
Patrick Ventureb15b3052018-10-04 10:31:53 -070049};
50
Patrick Venturede8a16e2019-03-07 12:48:32 -080051IpmiBlobHandler validateBlobCommand(const uint8_t* reqBuf, uint8_t* replyCmdBuf,
52 size_t* dataLen, ipmi_ret_t* code)
Patrick Ventureef3aead2018-09-12 08:53:29 -070053{
Patrick Ventureef3aead2018-09-12 08:53:29 -070054 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 Venture41258802018-11-12 10:46:30 -080061 *code = IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Ventureef3aead2018-09-12 08:53:29 -070062 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 Venture41258802018-11-12 10:46:30 -080071 *code = IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Ventureef3aead2018-09-12 08:53:29 -070072 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
Patrick Ventureef3aead2018-09-12 08:53:29 -070091 /* Crc expected but didn't match. */
Patrick Venturede8a16e2019-03-07 12:48:32 -080092 if (crcValue != ipmiblob::generateCrc(bytes))
Patrick Ventureef3aead2018-09-12 08:53:29 -070093 {
Patrick Venture41258802018-11-12 10:46:30 -080094 *code = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Ventureef3aead2018-09-12 08:53:29 -070095 return nullptr;
96 }
97 }
98
Patrick Ventureb15b3052018-10-04 10:31:53 -070099 /* Grab the corresponding handler for the command. */
100 auto found = handlers.find(command);
101 if (found == handlers.end())
Patrick Ventureef3aead2018-09-12 08:53:29 -0700102 {
Patrick Venture41258802018-11-12 10:46:30 -0800103 *code = IPMI_CC_INVALID_FIELD_REQUEST;
Patrick Ventureb15b3052018-10-04 10:31:53 -0700104 return nullptr;
Patrick Ventureef3aead2018-09-12 08:53:29 -0700105 }
106
Patrick Ventureb15b3052018-10-04 10:31:53 -0700107 return found->second;
Patrick Ventureef3aead2018-09-12 08:53:29 -0700108}
109
110ipmi_ret_t processBlobCommand(IpmiBlobHandler cmd, ManagerInterface* mgr,
Patrick Venturede8a16e2019-03-07 12:48:32 -0800111 const uint8_t* reqBuf, uint8_t* replyCmdBuf,
112 size_t* dataLen)
Patrick Ventureef3aead2018-09-12 08:53:29 -0700113{
114 ipmi_ret_t result = cmd(mgr, reqBuf, replyCmdBuf, dataLen);
115 if (result != IPMI_CC_OK)
116 {
117 return result;
118 }
119
120 size_t replyLength = (*dataLen);
121
122 /* The command, whatever it was, returned success. */
123 if (replyLength == 0)
124 {
125 return result;
126 }
127
Patrick Ventured1c3e862019-01-10 13:12:20 -0800128 /* Read can return 0 bytes, and just a CRC, otherwise you need a CRC and 1
129 * byte, therefore the limit is 2 bytes.
130 */
131 if (replyLength < (sizeof(uint16_t)))
Patrick Ventureef3aead2018-09-12 08:53:29 -0700132 {
Patrick Venture41258802018-11-12 10:46:30 -0800133 return IPMI_CC_UNSPECIFIED_ERROR;
Patrick Ventureef3aead2018-09-12 08:53:29 -0700134 }
135
136 /* The command, whatever it was, replied, so let's set the CRC. */
Patrick Venturede8a16e2019-03-07 12:48:32 -0800137 std::vector<std::uint8_t> crcBuffer(replyCmdBuf + sizeof(uint16_t),
138 replyCmdBuf + replyLength);
Patrick Ventureef3aead2018-09-12 08:53:29 -0700139 /* Copy the CRC into place. */
Patrick Venturede8a16e2019-03-07 12:48:32 -0800140 uint16_t crcValue = ipmiblob::generateCrc(crcBuffer);
Patrick Ventureef3aead2018-09-12 08:53:29 -0700141 std::memcpy(replyCmdBuf, &crcValue, sizeof(crcValue));
142
143 return result;
144}
Patrick Venture03fd5b82020-05-07 14:51:42 -0700145
146ipmi_ret_t handleBlobCommand(ipmi_cmd_t cmd, const uint8_t* reqBuf,
147 uint8_t* replyCmdBuf, size_t* dataLen)
148{
149 /* It's holding at least a sub-command. The OEN is trimmed from the bytes
150 * before this is called.
151 */
152 if ((*dataLen) < 1)
153 {
154 return IPMI_CC_REQ_DATA_LEN_INVALID;
155 }
156
157 /* on failure rc is set to the corresponding IPMI error. */
158 ipmi_ret_t rc = IPMI_CC_OK;
159 IpmiBlobHandler command =
160 validateBlobCommand(reqBuf, replyCmdBuf, dataLen, &rc);
161 if (command == nullptr)
162 {
163 (*dataLen) = 0;
164 return rc;
165 }
166
167 return processBlobCommand(command, getBlobManager(), reqBuf, replyCmdBuf,
168 dataLen);
169}
170
Patrick Ventureef3aead2018-09-12 08:53:29 -0700171} // namespace blobs