1 // Copyright 2020 The Pigweed Authors
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
15 #define PW_LOG_MODULE_NAME "KVS"
16 #define PW_LOG_LEVEL PW_KVS_LOG_LEVEL
18 #include "pw_kvs/fake_flash_memory.h"
20 #include "pw_kvs_private/config.h"
21 #include "pw_log/log.h"
25 Status FlashError::Check(std::span<FlashError> errors,
26 FlashMemory::Address address,
28 for (auto& error : errors) {
29 if (Status status = error.Check(address, size); !status.ok()) {
37 Status FlashError::Check(FlashMemory::Address start_address, size_t size) {
38 // Check if the event overlaps with this address range.
39 if (begin_ != kAnyAddress &&
40 (start_address >= end_ || (start_address + size) <= begin_)) {
49 if (remaining_ == 0u) {
53 if (remaining_ != kAlways) {
60 Status FakeFlashMemory::Erase(Address address, size_t num_sectors) {
61 if (address % sector_size_bytes() != 0) {
63 "Attempted to erase sector at non-sector aligned boundary; address %x",
65 return Status::InvalidArgument();
67 const size_t sector_id = address / sector_size_bytes();
68 if (address / sector_size_bytes() + num_sectors > sector_count()) {
70 "Tried to erase a sector at an address past flash end; "
71 "address: %x, sector implied: %u",
74 return Status::OutOfRange();
78 &buffer_[address], int(kErasedValue), sector_size_bytes() * num_sectors);
82 StatusWithSize FakeFlashMemory::Read(Address address,
83 std::span<std::byte> output) {
84 if (address + output.size() >= sector_count() * size_bytes()) {
85 return StatusWithSize::OutOfRange();
88 // Check for injected read errors
89 Status status = FlashError::Check(read_errors_, address, output.size());
90 std::memcpy(output.data(), &buffer_[address], output.size());
91 return StatusWithSize(status, output.size());
94 StatusWithSize FakeFlashMemory::Write(Address address,
95 std::span<const std::byte> data) {
96 if (address % alignment_bytes() != 0 ||
97 data.size() % alignment_bytes() != 0) {
98 PW_LOG_ERROR("Unaligned write; address %x, size %u B, alignment %u",
100 unsigned(data.size()),
101 unsigned(alignment_bytes()));
102 return StatusWithSize::InvalidArgument();
105 if (data.size() > sector_size_bytes() - (address % sector_size_bytes())) {
106 PW_LOG_ERROR("Write crosses sector boundary; address %x, size %u B",
108 unsigned(data.size()));
109 return StatusWithSize::InvalidArgument();
112 if (address + data.size() > sector_count() * sector_size_bytes()) {
114 "Write beyond end of memory; address %x, size %u B, max address %x",
116 unsigned(data.size()),
117 unsigned(sector_count() * sector_size_bytes()));
118 return StatusWithSize::OutOfRange();
121 // Check in erased state
122 for (unsigned i = 0; i < data.size(); i++) {
123 if (buffer_[address + i] != kErasedValue) {
124 PW_LOG_ERROR("Writing to previously written address: %x",
126 return StatusWithSize::Unknown();
130 // Check for any injected write errors
131 Status status = FlashError::Check(write_errors_, address, data.size());
132 std::memcpy(&buffer_[address], data.data(), data.size());
133 return StatusWithSize(status, data.size());
136 std::byte* FakeFlashMemory::FlashAddressToMcuAddress(Address address) const {
137 if (address > sector_count() * sector_size_bytes()) {
139 "FlashAddressToMcuAddress beyond end of memory; address %x, max "
142 unsigned(sector_count() * sector_size_bytes()));
145 return buffer_.data() + address;
148 } // namespace pw::kvs