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
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,span<std::byte> output)82 StatusWithSize FakeFlashMemory::Read(Address address, span<std::byte> output) {
83 if (address + output.size() >= sector_count() * size_bytes()) {
84 return StatusWithSize::OutOfRange();
85 }
86
87 // Check for injected read errors
88 Status status = FlashError::Check(read_errors_, address, output.size());
89 std::memcpy(output.data(), &buffer_[address], output.size());
90 return StatusWithSize(status, output.size());
91 }
92
Write(Address address,span<const std::byte> data)93 StatusWithSize FakeFlashMemory::Write(Address address,
94 span<const std::byte> data) {
95 if (address % alignment_bytes() != 0 ||
96 data.size() % alignment_bytes() != 0) {
97 PW_LOG_ERROR("Unaligned write; address %x, size %u B, alignment %u",
98 unsigned(address),
99 unsigned(data.size()),
100 unsigned(alignment_bytes()));
101 return StatusWithSize::InvalidArgument();
102 }
103
104 if (address + data.size() > sector_count() * sector_size_bytes()) {
105 PW_LOG_ERROR(
106 "Write beyond end of memory; address %x, size %u B, max address %x",
107 unsigned(address),
108 unsigned(data.size()),
109 unsigned(sector_count() * sector_size_bytes()));
110 return StatusWithSize::OutOfRange();
111 }
112
113 // Check in erased state
114 for (unsigned i = 0; i < data.size(); i++) {
115 if (buffer_[address + i] != kErasedValue) {
116 PW_LOG_ERROR("Writing to previously written address: %x",
117 unsigned(address));
118 return StatusWithSize::Unknown();
119 }
120 }
121
122 // Check for any injected write errors
123 Status status = FlashError::Check(write_errors_, address, data.size());
124 std::memcpy(&buffer_[address], data.data(), data.size());
125 return StatusWithSize(status, data.size());
126 }
127
FlashAddressToMcuAddress(Address address) const128 std::byte* FakeFlashMemory::FlashAddressToMcuAddress(Address address) const {
129 if (address > sector_count() * sector_size_bytes()) {
130 PW_LOG_ERROR(
131 "FlashAddressToMcuAddress beyond end of memory; address %x, max "
132 "address %x",
133 unsigned(address),
134 unsigned(sector_count() * sector_size_bytes()));
135 return nullptr;
136 }
137 return buffer_.data() + address;
138 }
139
140 } // namespace pw::kvs
141