Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 1 | #include "binarystore.hpp" |
| 2 | |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 3 | #include "sys_file.hpp" |
| 4 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 5 | #include <pb_decode.h> |
| 6 | #include <pb_encode.h> |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 7 | #include <sys/types.h> |
Kun Yi | d2d8cb6 | 2019-01-10 15:06:12 -0800 | [diff] [blame] | 8 | #include <unistd.h> |
| 9 | |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 10 | #include <algorithm> |
| 11 | #include <blobs-ipmid/blobs.hpp> |
Kun Yi | 7005917 | 2019-02-22 16:31:42 -0800 | [diff] [blame] | 12 | #include <boost/endian/arithmetic.hpp> |
Kun Yi | d2d8cb6 | 2019-01-10 15:06:12 -0800 | [diff] [blame] | 13 | #include <cstdint> |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 14 | #include <ipmid/handler.hpp> |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 15 | #include <map> |
Kun Yi | d2d8cb6 | 2019-01-10 15:06:12 -0800 | [diff] [blame] | 16 | #include <memory> |
Willy Tu | 67391d5 | 2021-12-07 19:53:49 -0800 | [diff] [blame] | 17 | #include <optional> |
Kun Yi | 8bcf79d | 2019-01-16 15:17:57 -0800 | [diff] [blame] | 18 | #include <phosphor-logging/elog.hpp> |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 19 | #include <stdplus/str/cat.hpp> |
Kun Yi | d2d8cb6 | 2019-01-10 15:06:12 -0800 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 23 | #include "binaryblob.pb.n.h" |
Kun Yi | d2d8cb6 | 2019-01-10 15:06:12 -0800 | [diff] [blame] | 24 | |
| 25 | using std::size_t; |
| 26 | using std::uint16_t; |
| 27 | using std::uint32_t; |
| 28 | using std::uint64_t; |
| 29 | using std::uint8_t; |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 30 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 31 | namespace binstore |
| 32 | { |
| 33 | |
Kun Yi | 8bcf79d | 2019-01-16 15:17:57 -0800 | [diff] [blame] | 34 | using namespace phosphor::logging; |
| 35 | |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame] | 36 | std::unique_ptr<BinaryStoreInterface> BinaryStore::createFromConfig( |
| 37 | const std::string& baseBlobId, std::unique_ptr<SysFile> file, |
| 38 | std::optional<uint32_t> maxSize, std::optional<std::string> aliasBlobBaseId) |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 39 | { |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 40 | if (baseBlobId.empty() || !file) |
| 41 | { |
Kun Yi | 8bcf79d | 2019-01-16 15:17:57 -0800 | [diff] [blame] | 42 | log<level::ERR>("Unable to create binarystore from invalid config", |
| 43 | entry("BASE_ID=%s", baseBlobId.c_str())); |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 44 | return nullptr; |
| 45 | } |
| 46 | |
Willy Tu | 67391d5 | 2021-12-07 19:53:49 -0800 | [diff] [blame] | 47 | auto store = |
| 48 | std::make_unique<BinaryStore>(baseBlobId, std::move(file), maxSize); |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 49 | |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame] | 50 | if (!store->loadSerializedData(aliasBlobBaseId)) |
Kun Yi | 8ca234e | 2019-03-04 10:14:45 -0800 | [diff] [blame] | 51 | { |
| 52 | return nullptr; |
| 53 | } |
| 54 | |
Willy Tu | 351a5d0 | 2021-12-07 19:48:40 -0800 | [diff] [blame] | 55 | return store; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 58 | std::unique_ptr<BinaryStoreInterface> |
Willy Tu | 67391d5 | 2021-12-07 19:53:49 -0800 | [diff] [blame] | 59 | BinaryStore::createFromFile(std::unique_ptr<SysFile> file, bool readOnly, |
| 60 | std::optional<uint32_t> maxSize) |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 61 | { |
| 62 | if (!file) |
| 63 | { |
| 64 | log<level::ERR>("Unable to create binarystore from invalid file"); |
| 65 | return nullptr; |
| 66 | } |
| 67 | |
Willy Tu | 67391d5 | 2021-12-07 19:53:49 -0800 | [diff] [blame] | 68 | auto store = |
| 69 | std::make_unique<BinaryStore>(std::move(file), readOnly, maxSize); |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 70 | |
| 71 | if (!store->loadSerializedData()) |
| 72 | { |
| 73 | return nullptr; |
| 74 | } |
| 75 | |
Willy Tu | 351a5d0 | 2021-12-07 19:48:40 -0800 | [diff] [blame] | 76 | return store; |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 77 | } |
| 78 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 79 | template <typename S> |
| 80 | static constexpr auto pbDecodeStr = [](pb_istream_t* stream, |
| 81 | const pb_field_iter_t*, |
| 82 | void** arg) noexcept { |
| 83 | static_assert(sizeof(*std::declval<S>().data()) == sizeof(pb_byte_t)); |
| 84 | auto& s = *reinterpret_cast<S*>(*arg); |
| 85 | s.resize(stream->bytes_left); |
| 86 | return pb_read(stream, reinterpret_cast<pb_byte_t*>(s.data()), s.size()); |
| 87 | }; |
| 88 | |
| 89 | template <typename T> |
| 90 | static pb_callback_t pbStrDecoder(T& t) noexcept |
| 91 | { |
| 92 | return {{.decode = pbDecodeStr<T>}, &t}; |
| 93 | } |
| 94 | |
| 95 | template <typename S> |
| 96 | static constexpr auto pbEncodeStr = [](pb_ostream_t* stream, |
| 97 | const pb_field_iter_t* field, |
| 98 | void* const* arg) noexcept { |
| 99 | static_assert(sizeof(*std::declval<S>().data()) == sizeof(pb_byte_t)); |
| 100 | const auto& s = *reinterpret_cast<const S*>(*arg); |
| 101 | return pb_encode_tag_for_field(stream, field) && |
| 102 | pb_encode_string( |
| 103 | stream, reinterpret_cast<const pb_byte_t*>(s.data()), s.size()); |
| 104 | }; |
| 105 | |
| 106 | template <typename T> |
| 107 | static pb_callback_t pbStrEncoder(const T& t) noexcept |
| 108 | { |
| 109 | return {{.encode = pbEncodeStr<T>}, const_cast<T*>(&t)}; |
| 110 | } |
| 111 | |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame] | 112 | bool BinaryStore::loadSerializedData(std::optional<std::string> aliasBlobBaseId) |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 113 | { |
| 114 | /* Load blob from sysfile if we know it might not match what we have. |
| 115 | * Note it will overwrite existing unsaved data per design. */ |
Kun Yi | e535a73 | 2019-04-03 19:03:25 -0700 | [diff] [blame] | 116 | if (commitState_ == CommitState::Clean || |
| 117 | commitState_ == CommitState::Uninitialized) |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 118 | { |
| 119 | return true; |
| 120 | } |
| 121 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 122 | std::string protoBlobId; |
| 123 | static constexpr auto blobcb = [](pb_istream_t* stream, |
| 124 | const pb_field_iter_t*, |
| 125 | void** arg) noexcept { |
| 126 | std::string id; |
| 127 | std::vector<std::uint8_t> data; |
| 128 | binstore_binaryblobproto_BinaryBlob msg = { |
| 129 | .blob_id = pbStrDecoder(id), |
| 130 | .data = pbStrDecoder(data), |
| 131 | }; |
| 132 | if (!pb_decode(stream, binstore_binaryblobproto_BinaryBlob_fields, |
| 133 | &msg)) |
| 134 | { |
| 135 | return false; |
| 136 | } |
| 137 | reinterpret_cast<decltype(std::declval<BinaryStore>().blobs_)*>(*arg) |
| 138 | ->emplace(id, data); |
| 139 | return true; |
| 140 | }; |
| 141 | |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 142 | try |
| 143 | { |
| 144 | /* Parse length-prefixed format to protobuf */ |
| 145 | boost::endian::little_uint64_t size = 0; |
| 146 | file_->readToBuf(0, sizeof(size), reinterpret_cast<char*>(&size)); |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 147 | auto proto = file_->readAsStr(sizeof(size), size); |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 148 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 149 | auto ist = pb_istream_from_buffer( |
| 150 | reinterpret_cast<const pb_byte_t*>(proto.data()), proto.size()); |
| 151 | binstore_binaryblobproto_BinaryBlobBase msg = { |
| 152 | .blob_base_id = pbStrDecoder(protoBlobId), |
| 153 | .blobs = {{.decode = blobcb}, &blobs_}, |
| 154 | }; |
| 155 | blobs_.clear(); // Purge old contents before new append during decode |
| 156 | if (!pb_decode(&ist, binstore_binaryblobproto_BinaryBlobBase_fields, |
| 157 | &msg)) |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 158 | { |
| 159 | /* Fail to parse the data, which might mean no preexsiting blobs |
| 160 | * and is a valid case to handle. Simply init an empty binstore. */ |
Kun Yi | e535a73 | 2019-04-03 19:03:25 -0700 | [diff] [blame] | 161 | commitState_ = CommitState::Uninitialized; |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
Kun Yi | c83d2fa | 2019-04-03 18:40:19 -0700 | [diff] [blame] | 164 | catch (const std::system_error& e) |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 165 | { |
| 166 | /* Read causes unexpected system-level failure */ |
| 167 | log<level::ERR>("Reading from sysfile failed", |
| 168 | entry("ERROR=%s", e.what())); |
| 169 | return false; |
| 170 | } |
Kun Yi | c83d2fa | 2019-04-03 18:40:19 -0700 | [diff] [blame] | 171 | catch (const std::exception& e) |
| 172 | { |
Kun Yi | e535a73 | 2019-04-03 19:03:25 -0700 | [diff] [blame] | 173 | /* Non system error originates from junk value in 'size' */ |
| 174 | commitState_ = CommitState::Uninitialized; |
| 175 | } |
| 176 | |
| 177 | if (commitState_ == CommitState::Uninitialized) |
| 178 | { |
| 179 | log<level::WARNING>("Fail to parse. There might be no persisted blobs", |
| 180 | entry("BASE_ID=%s", baseBlobId_.c_str())); |
Kun Yi | c83d2fa | 2019-04-03 18:40:19 -0700 | [diff] [blame] | 181 | return true; |
| 182 | } |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 183 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 184 | if (baseBlobId_.empty() && !protoBlobId.empty()) |
| 185 | { |
| 186 | baseBlobId_ = std::move(protoBlobId); |
| 187 | } |
| 188 | else if (protoBlobId == aliasBlobBaseId) |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame] | 189 | { |
| 190 | log<level::WARNING>("Alias blob id, rename blob id...", |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 191 | entry("LOADED=%s", protoBlobId.c_str()), |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame] | 192 | entry("RENAMED=%s", baseBlobId_.c_str())); |
| 193 | std::string tmpBlobId = baseBlobId_; |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 194 | baseBlobId_ = *aliasBlobBaseId; |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame] | 195 | return setBaseBlobId(tmpBlobId); |
| 196 | } |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 197 | else if (protoBlobId != baseBlobId_ && !readOnly_) |
Kun Yi | 8ca234e | 2019-03-04 10:14:45 -0800 | [diff] [blame] | 198 | { |
| 199 | /* Uh oh, stale data loaded. Clean it and commit. */ |
| 200 | // TODO: it might be safer to add an option in config to error out |
| 201 | // instead of to overwrite. |
| 202 | log<level::ERR>("Stale blob data, resetting internals...", |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 203 | entry("LOADED=%s", protoBlobId.c_str()), |
Kun Yi | 8ca234e | 2019-03-04 10:14:45 -0800 | [diff] [blame] | 204 | entry("EXPECTED=%s", baseBlobId_.c_str())); |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 205 | blobs_.clear(); |
Kun Yi | 8ca234e | 2019-03-04 10:14:45 -0800 | [diff] [blame] | 206 | return this->commit(); |
| 207 | } |
| 208 | |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 209 | return true; |
| 210 | } |
| 211 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 212 | std::string BinaryStore::getBaseBlobId() const |
| 213 | { |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 214 | return baseBlobId_; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame] | 217 | bool BinaryStore::setBaseBlobId(const std::string& baseBlobId) |
| 218 | { |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 219 | for (auto it = blobs_.begin(); it != blobs_.end();) |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame] | 220 | { |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 221 | auto curr = it++; |
| 222 | if (curr->first.starts_with(baseBlobId_)) |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame] | 223 | { |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 224 | auto nh = blobs_.extract(curr); |
| 225 | nh.key() = stdplus::strCat( |
| 226 | baseBlobId, |
| 227 | std::string_view(curr->first).substr(baseBlobId_.size())); |
| 228 | blobs_.insert(std::move(nh)); |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame] | 229 | } |
| 230 | } |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 231 | baseBlobId_ = baseBlobId; |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame] | 232 | return this->commit(); |
| 233 | } |
| 234 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 235 | std::vector<std::string> BinaryStore::getBlobIds() const |
| 236 | { |
| 237 | std::vector<std::string> result; |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 238 | result.reserve(blobs_.size() + 1); |
Willy Tu | fdebaa3 | 2022-02-08 16:34:20 -0800 | [diff] [blame] | 239 | result.emplace_back(getBaseBlobId()); |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 240 | for (const auto& kv : blobs_) |
| 241 | { |
| 242 | result.emplace_back(kv.first); |
| 243 | } |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 244 | return result; |
| 245 | } |
| 246 | |
| 247 | bool BinaryStore::openOrCreateBlob(const std::string& blobId, uint16_t flags) |
| 248 | { |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 249 | if (!(flags & blobs::OpenFlags::read)) |
| 250 | { |
Kun Yi | 8bcf79d | 2019-01-16 15:17:57 -0800 | [diff] [blame] | 251 | log<level::ERR>("OpenFlags::read not specified when opening", |
| 252 | entry("BLOB_ID=%s", blobId.c_str())); |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 253 | return false; |
| 254 | } |
| 255 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 256 | if (!currentBlob_.empty()) |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 257 | { |
Kun Yi | 8bcf79d | 2019-01-16 15:17:57 -0800 | [diff] [blame] | 258 | log<level::ERR>("Already handling a different blob", |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 259 | entry("EXPECTED=%s", currentBlob_.c_str()), |
Kun Yi | 8bcf79d | 2019-01-16 15:17:57 -0800 | [diff] [blame] | 260 | entry("RECEIVED=%s", blobId.c_str())); |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 264 | if (readOnly_ && (flags & blobs::OpenFlags::write)) |
| 265 | { |
| 266 | log<level::ERR>("Can't open the blob for writing: read-only store", |
| 267 | entry("BLOB_ID=%s", blobId.c_str())); |
| 268 | return false; |
| 269 | } |
| 270 | |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 271 | writable_ = flags & blobs::OpenFlags::write; |
| 272 | |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 273 | /* If there are uncommitted data, discard them. */ |
| 274 | if (!this->loadSerializedData()) |
Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 275 | { |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 276 | return false; |
Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 279 | /* Iterate and find if there is an existing blob with this id. |
| 280 | * blobsPtr points to a BinaryBlob container with STL-like semantics*/ |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 281 | if (blobs_.find(blobId) != blobs_.end()) |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 282 | { |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 283 | currentBlob_ = blobId; |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 284 | return true; |
| 285 | } |
| 286 | |
| 287 | /* Otherwise, create the blob and append it */ |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 288 | if (readOnly_) |
| 289 | { |
| 290 | return false; |
| 291 | } |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 292 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 293 | blobs_.emplace(blobId, std::vector<std::uint8_t>{}); |
| 294 | currentBlob_ = blobId; |
| 295 | commitState_ = CommitState::Dirty; |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 296 | return true; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Willy Tu | 351a5d0 | 2021-12-07 19:48:40 -0800 | [diff] [blame] | 299 | bool BinaryStore::deleteBlob(const std::string&) |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 300 | { |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | std::vector<uint8_t> BinaryStore::read(uint32_t offset, uint32_t requestedSize) |
| 305 | { |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 306 | if (currentBlob_.empty()) |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame] | 307 | { |
Kun Yi | 8bcf79d | 2019-01-16 15:17:57 -0800 | [diff] [blame] | 308 | log<level::ERR>("No open blob to read"); |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 309 | return {}; |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame] | 310 | } |
| 311 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 312 | const auto& data = blobs_.find(currentBlob_)->second; |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame] | 313 | |
| 314 | /* If it is out of bound, return empty vector */ |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 315 | if (offset >= data.size()) |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame] | 316 | { |
Kun Yi | 8bcf79d | 2019-01-16 15:17:57 -0800 | [diff] [blame] | 317 | log<level::ERR>("Read offset is beyond data size", |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 318 | entry("MAX_SIZE=0x%x", data.size()), |
Kun Yi | 8bcf79d | 2019-01-16 15:17:57 -0800 | [diff] [blame] | 319 | entry("RECEIVED_OFFSET=0x%x", offset)); |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 320 | return {}; |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame] | 321 | } |
| 322 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 323 | auto s = data.begin() + offset; |
| 324 | return {s, s + std::min<size_t>(requestedSize, data.size() - offset)}; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 327 | std::vector<uint8_t> BinaryStore::readBlob(const std::string& blobId) const |
| 328 | { |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 329 | const auto blobIt = blobs_.find(blobId); |
| 330 | if (blobIt == blobs_.end()) |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 331 | { |
| 332 | throw ipmi::HandlerCompletion(ipmi::ccUnspecifiedError); |
| 333 | } |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 334 | return blobIt->second; |
| 335 | } |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 336 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 337 | static binstore_binaryblobproto_BinaryBlobBase makeEncoder( |
| 338 | const std::string& base, |
| 339 | const std::map<std::string, std::vector<std::uint8_t>>& blobs) noexcept |
| 340 | { |
| 341 | using Blobs = std::decay_t<decltype(blobs)>; |
| 342 | static constexpr auto blobcb = [](pb_ostream_t* stream, |
| 343 | const pb_field_iter_t* field, |
| 344 | void* const* arg) noexcept { |
| 345 | const auto& blobs = *reinterpret_cast<const Blobs*>(*arg); |
| 346 | for (const auto& [id, data] : blobs) |
| 347 | { |
| 348 | binstore_binaryblobproto_BinaryBlob msg = { |
| 349 | .blob_id = pbStrEncoder(id), |
| 350 | .data = pbStrEncoder(data), |
| 351 | }; |
| 352 | if (!pb_encode_tag_for_field(stream, field) || |
| 353 | !pb_encode_submessage( |
| 354 | stream, binstore_binaryblobproto_BinaryBlob_fields, &msg)) |
| 355 | { |
| 356 | return false; |
| 357 | } |
| 358 | } |
| 359 | return true; |
| 360 | }; |
| 361 | return { |
| 362 | .blob_base_id = pbStrEncoder(base), |
| 363 | .blobs = {{.encode = blobcb}, |
| 364 | const_cast<void*>(reinterpret_cast<const void*>(&blobs))}, |
| 365 | }; |
| 366 | } |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 367 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 368 | static std::size_t |
| 369 | payloadCalcSize(const binstore_binaryblobproto_BinaryBlobBase& msg) |
| 370 | { |
| 371 | pb_ostream_t nost = {}; |
| 372 | if (!pb_encode(&nost, binstore_binaryblobproto_BinaryBlobBase_fields, &msg)) |
| 373 | { |
| 374 | throw std::runtime_error( |
| 375 | std::format("Calculating msg size: {}", PB_GET_ERROR(&nost))); |
| 376 | } |
| 377 | // Proto is prepended with the size of the proto |
| 378 | return nost.bytes_written + sizeof(boost::endian::little_uint64_t); |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 381 | bool BinaryStore::write(uint32_t offset, const std::vector<uint8_t>& data) |
| 382 | { |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 383 | if (currentBlob_.empty()) |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame] | 384 | { |
Kun Yi | 8bcf79d | 2019-01-16 15:17:57 -0800 | [diff] [blame] | 385 | log<level::ERR>("No open blob to write"); |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame] | 386 | return false; |
| 387 | } |
| 388 | |
| 389 | if (!writable_) |
| 390 | { |
Kun Yi | 8bcf79d | 2019-01-16 15:17:57 -0800 | [diff] [blame] | 391 | log<level::ERR>("Open blob is not writable"); |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame] | 392 | return false; |
| 393 | } |
| 394 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 395 | auto& bdata = blobs_.find(currentBlob_)->second; |
| 396 | if (offset > bdata.size()) |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame] | 397 | { |
Kun Yi | 8bcf79d | 2019-01-16 15:17:57 -0800 | [diff] [blame] | 398 | log<level::ERR>("Write would leave a gap with undefined data. Return."); |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame] | 399 | return false; |
| 400 | } |
| 401 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 402 | std::size_t oldsize = bdata.size(), reqSize = offset + data.size(); |
| 403 | if (reqSize > bdata.size()) |
| 404 | { |
| 405 | bdata.resize(reqSize); |
| 406 | } |
Willy Tu | 67391d5 | 2021-12-07 19:53:49 -0800 | [diff] [blame] | 407 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 408 | if (payloadCalcSize(makeEncoder(baseBlobId_, blobs_)) > |
| 409 | maxSize.value_or( |
| 410 | std::numeric_limits<std::decay_t<decltype(*maxSize)>>::max())) |
Willy Tu | 67391d5 | 2021-12-07 19:53:49 -0800 | [diff] [blame] | 411 | { |
| 412 | log<level::ERR>("Write data would make the total size exceed the max " |
| 413 | "size allowed. Return."); |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 414 | bdata.resize(oldsize); |
Willy Tu | 67391d5 | 2021-12-07 19:53:49 -0800 | [diff] [blame] | 415 | return false; |
| 416 | } |
| 417 | |
Kun Yi | 1a25e0d | 2020-05-11 12:28:53 -0700 | [diff] [blame] | 418 | commitState_ = CommitState::Dirty; |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 419 | std::copy(data.begin(), data.end(), bdata.data() + offset); |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame] | 420 | return true; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | bool BinaryStore::commit() |
| 424 | { |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 425 | if (readOnly_) |
| 426 | { |
| 427 | log<level::ERR>("ReadOnly blob, not committing"); |
| 428 | return false; |
| 429 | } |
| 430 | |
Kun Yi | 7005917 | 2019-02-22 16:31:42 -0800 | [diff] [blame] | 431 | /* Store as little endian to be platform agnostic. Consistent with read. */ |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 432 | auto msg = makeEncoder(baseBlobId_, blobs_); |
| 433 | auto outSize = payloadCalcSize(msg); |
| 434 | if (outSize > |
| 435 | maxSize.value_or( |
| 436 | std::numeric_limits<std::decay_t<decltype(*maxSize)>>::max())) |
Willy Tu | 67391d5 | 2021-12-07 19:53:49 -0800 | [diff] [blame] | 437 | { |
Willy Tu | 9163385 | 2022-02-18 08:55:43 -0800 | [diff] [blame] | 438 | log<level::ERR>("Commit Data exceeded maximum allowed size"); |
Willy Tu | 67391d5 | 2021-12-07 19:53:49 -0800 | [diff] [blame] | 439 | return false; |
| 440 | } |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 441 | std::string buf(outSize, '\0'); |
| 442 | auto& size = *reinterpret_cast<boost::endian::little_uint64_t*>(buf.data()); |
| 443 | auto ost = pb_ostream_from_buffer(reinterpret_cast<pb_byte_t*>(buf.data()) + |
| 444 | sizeof(size), |
| 445 | buf.size() - sizeof(size)); |
| 446 | pb_encode(&ost, binstore_binaryblobproto_BinaryBlobBase_fields, &msg); |
| 447 | size = ost.bytes_written; |
Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 448 | try |
| 449 | { |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 450 | file_->writeStr(buf, 0); |
Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 451 | } |
| 452 | catch (const std::exception& e) |
| 453 | { |
Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 454 | commitState_ = CommitState::CommitError; |
Kun Yi | 8bcf79d | 2019-01-16 15:17:57 -0800 | [diff] [blame] | 455 | log<level::ERR>("Writing to sysfile failed", |
| 456 | entry("ERROR=%s", e.what())); |
Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 457 | return false; |
| 458 | }; |
| 459 | |
| 460 | commitState_ = CommitState::Clean; |
| 461 | return true; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | bool BinaryStore::close() |
| 465 | { |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 466 | currentBlob_.clear(); |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 467 | writable_ = false; |
Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 468 | commitState_ = CommitState::Dirty; |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 469 | return true; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 470 | } |
| 471 | |
Kun Yi | 1a25e0d | 2020-05-11 12:28:53 -0700 | [diff] [blame] | 472 | /* |
| 473 | * Sets |meta| with size and state of the blob. Returns |blobState| with |
| 474 | * standard definition from phosphor-ipmi-blobs header blob.hpp, plus OEM |
| 475 | * flag bits BinaryStore::CommitState. |
| 476 | |
| 477 | enum StateFlags |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 478 | { |
Kun Yi | 1a25e0d | 2020-05-11 12:28:53 -0700 | [diff] [blame] | 479 | open_read = (1 << 0), |
| 480 | open_write = (1 << 1), |
| 481 | committing = (1 << 2), |
| 482 | committed = (1 << 3), |
| 483 | commit_error = (1 << 4), |
| 484 | }; |
| 485 | |
| 486 | enum CommitState |
| 487 | { |
| 488 | Dirty = (1 << 8), // In-memory data might not match persisted data |
| 489 | Clean = (1 << 9), // In-memory data matches persisted data |
| 490 | Uninitialized = (1 << 10), // Cannot find persisted data |
| 491 | CommitError = (1 << 11) // Error happened during committing |
| 492 | }; |
| 493 | |
| 494 | */ |
| 495 | bool BinaryStore::stat(blobs::BlobMeta* meta) |
| 496 | { |
| 497 | uint16_t blobState = blobs::StateFlags::open_read; |
| 498 | if (writable_) |
| 499 | { |
| 500 | blobState |= blobs::StateFlags::open_write; |
| 501 | } |
| 502 | |
| 503 | if (commitState_ == CommitState::Clean) |
| 504 | { |
| 505 | blobState |= blobs::StateFlags::committed; |
| 506 | } |
| 507 | else if (commitState_ == CommitState::CommitError) |
| 508 | { |
| 509 | blobState |= blobs::StateFlags::commit_error; |
| 510 | } |
| 511 | blobState |= commitState_; |
| 512 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 513 | if (!currentBlob_.empty()) |
Kun Yi | 1a25e0d | 2020-05-11 12:28:53 -0700 | [diff] [blame] | 514 | { |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame] | 515 | meta->size = blobs_.find(currentBlob_)->second.size(); |
Kun Yi | 1a25e0d | 2020-05-11 12:28:53 -0700 | [diff] [blame] | 516 | } |
| 517 | else |
| 518 | { |
| 519 | meta->size = 0; |
| 520 | } |
| 521 | meta->blobState = blobState; |
| 522 | |
| 523 | return true; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | } // namespace binstore |