1 // Copyright 2020 The Pigweed Authors
2 //
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
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
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
13 // the License.
14
15 #define PW_LOG_MODULE_NAME "KVS"
16 #define PW_LOG_LEVEL PW_KVS_LOG_LEVEL
17
18 #include "pw_kvs/fake_flash_memory.h"
19
20 #include "pw_kvs_private/config.h"
21 #include "pw_log/log.h"
22
23 namespace pw::kvs {
24
Check(std::span<FlashError> errors,FlashMemory::Address address,size_t size)25 Status FlashError::Check(std::span<FlashError> errors,
26 FlashMemory::Address address,
27 size_t size) {
28 for (auto& error : errors) {
29 if (Status status = error.Check(address, size); !status.ok()) {
30 return status;
31 }
32 }
33
34 return OkStatus();
35 }
36
Check(FlashMemory::Address start_address,size_t size)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_)) {
41 return OkStatus();
42 }
43
44 if (delay_ > 0u) {
45 delay_ -= 1;
46 return OkStatus();
47 }
48
49 if (remaining_ == 0u) {
50 return OkStatus();
51 }
52
53 if (remaining_ != kAlways) {
54 remaining_ -= 1;
55 }
56
57 return status_;
58 }
59
Erase(Address address,size_t num_sectors)60 Status FakeFlashMemory::Erase(Address address, size_t num_sectors) {
61 if (address % sector_size_bytes() != 0) {
62 PW_LOG_ERROR(
63 "Attempted to erase sector at non-sector aligned boundary; address %x",
64 unsigned(address));
65 return Status::InvalidArgument();
66 }
67 const size_t sector_id = address / sector_size_bytes();
68 if (address / sector_size_bytes() + num_sectors > sector_count()) {
69 PW_LOG_ERROR(
70 "Tried to erase a sector at an address past flash end; "
71 "address: %x, sector implied: %u",
72 unsigned(address),
73 unsigned(sector_id));
74 return Status::OutOfRange();
75 }
76
77 std::memset(
78 &buffer_[address], int(kErasedValue), sector_size_bytes() * num_sectors);
79 return OkStatus();
80 }
81
Read(Address address,std::span<std::byte> output)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();
86 }
87
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());
92 }
93
Write(Address address,std::span<const std::byte> data)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",
99 unsigned(address),
100 unsigned(data.size()),
101 unsigned(alignment_bytes()));
102 return StatusWithSize::InvalidArgument();
103 }
104
105 if (data.size() > sector_size_bytes() - (address % sector_size_bytes())) {
106 PW_LOG_ERROR("Write crosses sector boundary; address %x, size %u B",
107 unsigned(address),
108 unsigned(data.size()));
109 return StatusWithSize::InvalidArgument();
110 }
111
112 if (address + data.size() > sector_count() * sector_size_bytes()) {
113 PW_LOG_ERROR(
114 "Write beyond end of memory; address %x, size %u B, max address %x",
115 unsigned(address),
116 unsigned(data.size()),
117 unsigned(sector_count() * sector_size_bytes()));
118 return StatusWithSize::OutOfRange();
119 }
120
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",
125 unsigned(address));
126 return StatusWithSize::Unknown();
127 }
128 }
129
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());
134 }
135
FlashAddressToMcuAddress(Address address) const136 std::byte* FakeFlashMemory::FlashAddressToMcuAddress(Address address) const {
137 if (address > sector_count() * sector_size_bytes()) {
138 PW_LOG_ERROR(
139 "FlashAddressToMcuAddress beyond end of memory; address %x, max "
140 "address %x",
141 unsigned(address),
142 unsigned(sector_count() * sector_size_bytes()));
143 return nullptr;
144 }
145 return buffer_.data() + address;
146 }
147
148 } // namespace pw::kvs
149