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 "PW_FLASH"
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(span<FlashError> errors,FlashMemory::Address address,size_t size)25 Status FlashError::Check(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
60 Vector<FlashError, 0> FakeFlashMemory::no_errors_;
61
Erase(Address address,size_t num_sectors)62 Status FakeFlashMemory::Erase(Address address, size_t num_sectors) {
63 if (address % sector_size_bytes() != 0) {
64 PW_LOG_ERROR(
65 "Attempted to erase sector at non-sector aligned boundary; address %x",
66 unsigned(address));
67 return Status::InvalidArgument();
68 }
69 const size_t sector_id = address / sector_size_bytes();
70 if (address / sector_size_bytes() + num_sectors > sector_count()) {
71 PW_LOG_ERROR(
72 "Tried to erase a sector at an address past flash end; "
73 "address: %x, sector implied: %u",
74 unsigned(address),
75 unsigned(sector_id));
76 return Status::OutOfRange();
77 }
78
79 std::memset(
80 &buffer_[address], int(kErasedValue), sector_size_bytes() * num_sectors);
81 return OkStatus();
82 }
83
Read(Address address,span<std::byte> output)84 StatusWithSize FakeFlashMemory::Read(Address address, span<std::byte> output) {
85 if (address + output.size() >= sector_count() * size_bytes()) {
86 return StatusWithSize::OutOfRange();
87 }
88
89 // Check for injected read errors
90 Status status = FlashError::Check(read_errors_, address, output.size());
91 std::memcpy(output.data(), &buffer_[address], output.size());
92 return StatusWithSize(status, output.size());
93 }
94
Write(Address address,span<const std::byte> data)95 StatusWithSize FakeFlashMemory::Write(Address address,
96 span<const std::byte> data) {
97 if (address % alignment_bytes() != 0 ||
98 data.size() % alignment_bytes() != 0) {
99 PW_LOG_ERROR("Unaligned write; address %x, size %u B, alignment %u",
100 unsigned(address),
101 unsigned(data.size()),
102 unsigned(alignment_bytes()));
103 return StatusWithSize::InvalidArgument();
104 }
105
106 if (address + data.size() > sector_count() * sector_size_bytes()) {
107 PW_LOG_ERROR(
108 "Write beyond end of memory; address %x, size %u B, max address %x",
109 unsigned(address),
110 unsigned(data.size()),
111 unsigned(sector_count() * sector_size_bytes()));
112 return StatusWithSize::OutOfRange();
113 }
114
115 // Check in erased state
116 for (unsigned i = 0; i < data.size(); i++) {
117 if (buffer_[address + i] != kErasedValue) {
118 PW_LOG_ERROR("Writing to previously written address: %x",
119 unsigned(address));
120 return StatusWithSize::Unknown();
121 }
122 }
123
124 // Check for any injected write errors
125 Status status = FlashError::Check(write_errors_, address, data.size());
126 std::memcpy(&buffer_[address], data.data(), data.size());
127 return StatusWithSize(status, data.size());
128 }
129
FlashAddressToMcuAddress(Address address) const130 std::byte* FakeFlashMemory::FlashAddressToMcuAddress(Address address) const {
131 if (address > sector_count() * sector_size_bytes()) {
132 PW_LOG_ERROR(
133 "FlashAddressToMcuAddress beyond end of memory; address %x, max "
134 "address %x",
135 unsigned(address),
136 unsigned(sector_count() * sector_size_bytes()));
137 return nullptr;
138 }
139 return buffer_.data() + address;
140 }
141
142 } // namespace pw::kvs
143