Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 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 | |
David Rogers | eb05d88 | 2021-10-12 15:24:48 -0700 | [diff] [blame] | 15 | #define PW_LOG_MODULE_NAME "PW_FLASH" |
Wyatt Hepler | ae222dc | 2020-10-14 10:46:27 -0700 | [diff] [blame] | 16 | #define PW_LOG_LEVEL PW_KVS_LOG_LEVEL |
Armando Montanez | 28ecccb | 2020-05-04 15:35:54 -0700 | [diff] [blame] | 17 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 18 | #include "pw_kvs/flash_memory.h" |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <cinttypes> |
| 22 | #include <cstring> |
| 23 | |
Wyatt Hepler | f298de4 | 2021-03-19 15:06:36 -0700 | [diff] [blame] | 24 | #include "pw_assert/check.h" |
David Rogers | ca59296 | 2020-07-01 09:21:54 -0700 | [diff] [blame] | 25 | #include "pw_kvs_private/config.h" |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 26 | #include "pw_log/log.h" |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 27 | #include "pw_status/status_with_size.h" |
David Rogers | b6b14b8 | 2020-09-11 16:27:27 -0700 | [diff] [blame] | 28 | #include "pw_status/try.h" |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 29 | |
| 30 | namespace pw::kvs { |
| 31 | |
| 32 | using std::byte; |
| 33 | |
Wyatt Hepler | 79525f5 | 2022-03-02 11:16:28 -0800 | [diff] [blame] | 34 | #if PW_CXX_STANDARD_IS_SUPPORTED(17) |
| 35 | |
David Rogers | b9acbb0 | 2022-02-11 14:19:55 -0800 | [diff] [blame] | 36 | Status FlashPartition::Writer::DoWrite(ConstByteSpan data) { |
| 37 | if (partition_.size_bytes() <= position_) { |
| 38 | return Status::OutOfRange(); |
| 39 | } |
| 40 | if (data.size_bytes() > (partition_.size_bytes() - position_)) { |
| 41 | return Status::ResourceExhausted(); |
| 42 | } |
| 43 | if (data.size_bytes() == 0) { |
| 44 | return OkStatus(); |
| 45 | } |
| 46 | |
| 47 | const StatusWithSize sws = partition_.Write(position_, data); |
| 48 | if (sws.ok()) { |
| 49 | position_ += data.size_bytes(); |
| 50 | } |
| 51 | return sws.status(); |
| 52 | } |
| 53 | |
| 54 | StatusWithSize FlashPartition::Reader::DoRead(ByteSpan data) { |
| 55 | if (position_ >= partition_.size_bytes()) { |
| 56 | return StatusWithSize::OutOfRange(); |
| 57 | } |
| 58 | |
| 59 | size_t bytes_to_read = |
| 60 | std::min(data.size_bytes(), partition_.size_bytes() - position_); |
| 61 | |
| 62 | const StatusWithSize sws = |
| 63 | partition_.Read(position_, data.first(bytes_to_read)); |
| 64 | if (sws.ok()) { |
| 65 | position_ += bytes_to_read; |
| 66 | } |
| 67 | return sws; |
| 68 | } |
| 69 | |
Wyatt Hepler | 79525f5 | 2022-03-02 11:16:28 -0800 | [diff] [blame] | 70 | #endif // PW_CXX_STANDARD_IS_SUPPORTED(17) |
| 71 | |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 72 | StatusWithSize FlashPartition::Output::DoWrite(std::span<const byte> data) { |
David Rogers | c4dc864 | 2020-09-14 10:52:36 -0700 | [diff] [blame] | 73 | PW_TRY_WITH_SIZE(flash_.Write(address_, data)); |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 74 | address_ += data.size(); |
| 75 | return StatusWithSize(data.size()); |
| 76 | } |
| 77 | |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 78 | StatusWithSize FlashPartition::Input::DoRead(std::span<byte> data) { |
Wyatt Hepler | ee6fd76 | 2020-03-09 08:32:19 -0700 | [diff] [blame] | 79 | StatusWithSize result = flash_.Read(address_, data); |
| 80 | address_ += result.size(); |
| 81 | return result; |
| 82 | } |
| 83 | |
Paul Mathieu | 9a9ed13 | 2020-10-12 19:15:34 -0700 | [diff] [blame] | 84 | FlashPartition::FlashPartition( |
| 85 | FlashMemory* flash, |
| 86 | uint32_t start_sector_index, |
| 87 | uint32_t sector_count, |
| 88 | uint32_t alignment_bytes, // Defaults to flash alignment |
| 89 | PartitionPermission permission) |
| 90 | |
| 91 | : flash_(*flash), |
| 92 | start_sector_index_(start_sector_index), |
| 93 | sector_count_(sector_count), |
| 94 | alignment_bytes_( |
| 95 | alignment_bytes == 0 |
| 96 | ? flash_.alignment_bytes() |
| 97 | : std::max(alignment_bytes, uint32_t(flash_.alignment_bytes()))), |
| 98 | permission_(permission) { |
| 99 | uint32_t misalignment = (alignment_bytes_ % flash_.alignment_bytes()); |
| 100 | PW_DCHECK_UINT_EQ(misalignment, |
| 101 | 0, |
| 102 | "Flash partition alignmentmust be a multiple of the flash " |
| 103 | "memory alignment"); |
| 104 | } |
| 105 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 106 | Status FlashPartition::Erase(Address address, size_t num_sectors) { |
| 107 | if (permission_ == PartitionPermission::kReadOnly) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 108 | return Status::PermissionDenied(); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 109 | } |
| 110 | |
David Rogers | c4dc864 | 2020-09-14 10:52:36 -0700 | [diff] [blame] | 111 | PW_TRY(CheckBounds(address, num_sectors * sector_size_bytes())); |
David Rogers | 907570b | 2020-08-06 12:44:27 -0700 | [diff] [blame] | 112 | const size_t address_sector_offset = address % sector_size_bytes(); |
| 113 | PW_CHECK_UINT_EQ(address_sector_offset, 0u); |
| 114 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 115 | return flash_.Erase(PartitionToFlashAddress(address), num_sectors); |
| 116 | } |
| 117 | |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 118 | StatusWithSize FlashPartition::Read(Address address, std::span<byte> output) { |
David Rogers | c4dc864 | 2020-09-14 10:52:36 -0700 | [diff] [blame] | 119 | PW_TRY_WITH_SIZE(CheckBounds(address, output.size())); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 120 | return flash_.Read(PartitionToFlashAddress(address), output); |
| 121 | } |
| 122 | |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 123 | StatusWithSize FlashPartition::Write(Address address, |
| 124 | std::span<const byte> data) { |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 125 | if (permission_ == PartitionPermission::kReadOnly) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 126 | return StatusWithSize::PermissionDenied(); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 127 | } |
David Rogers | c4dc864 | 2020-09-14 10:52:36 -0700 | [diff] [blame] | 128 | PW_TRY_WITH_SIZE(CheckBounds(address, data.size())); |
David Rogers | 907570b | 2020-08-06 12:44:27 -0700 | [diff] [blame] | 129 | const size_t address_alignment_offset = address % alignment_bytes(); |
| 130 | PW_CHECK_UINT_EQ(address_alignment_offset, 0u); |
| 131 | const size_t size_alignment_offset = data.size() % alignment_bytes(); |
| 132 | PW_CHECK_UINT_EQ(size_alignment_offset, 0u); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 133 | return flash_.Write(PartitionToFlashAddress(address), data); |
| 134 | } |
| 135 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 136 | Status FlashPartition::IsRegionErased(Address source_flash_address, |
| 137 | size_t length, |
| 138 | bool* is_erased) { |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 139 | // Relying on Read() to check address and len arguments. |
| 140 | if (is_erased == nullptr) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 141 | return Status::InvalidArgument(); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 142 | } |
David Rogers | ca59296 | 2020-07-01 09:21:54 -0700 | [diff] [blame] | 143 | |
| 144 | // TODO(pwbug/214): Currently using a single flash alignment to do both the |
| 145 | // read and write. The allowable flash read length may be less than what write |
David Rogers | 3f12bff | 2021-04-13 21:59:26 -0700 | [diff] [blame] | 146 | // needs (possibly by a bunch), resulting in read_buffer and |
| 147 | // erased_pattern_buffer being bigger than they need to be. |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 148 | const size_t alignment = alignment_bytes(); |
David Rogers | ca59296 | 2020-07-01 09:21:54 -0700 | [diff] [blame] | 149 | if (alignment > kMaxFlashAlignment || kMaxFlashAlignment % alignment || |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 150 | length % alignment) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 151 | return Status::InvalidArgument(); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 152 | } |
| 153 | |
David Rogers | 3f12bff | 2021-04-13 21:59:26 -0700 | [diff] [blame] | 154 | byte read_buffer[kMaxFlashAlignment]; |
David Rogers | a5661ef | 2020-07-09 15:15:12 -0700 | [diff] [blame] | 155 | const byte erased_byte = flash_.erased_memory_content(); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 156 | size_t offset = 0; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 157 | *is_erased = false; |
| 158 | while (length > 0u) { |
| 159 | // Check earlier that length is aligned, no need to round up |
David Rogers | 3f12bff | 2021-04-13 21:59:26 -0700 | [diff] [blame] | 160 | size_t read_size = std::min(sizeof(read_buffer), length); |
| 161 | PW_TRY( |
| 162 | Read(source_flash_address + offset, read_size, read_buffer).status()); |
David Rogers | a5661ef | 2020-07-09 15:15:12 -0700 | [diff] [blame] | 163 | |
David Rogers | 3f12bff | 2021-04-13 21:59:26 -0700 | [diff] [blame] | 164 | for (byte b : std::span(read_buffer, read_size)) { |
David Rogers | a5661ef | 2020-07-09 15:15:12 -0700 | [diff] [blame] | 165 | if (b != erased_byte) { |
| 166 | // Detected memory chunk is not entirely erased |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 167 | return OkStatus(); |
David Rogers | a5661ef | 2020-07-09 15:15:12 -0700 | [diff] [blame] | 168 | } |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 169 | } |
David Rogers | a5661ef | 2020-07-09 15:15:12 -0700 | [diff] [blame] | 170 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 171 | offset += read_size; |
| 172 | length -= read_size; |
| 173 | } |
| 174 | *is_erased = true; |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 175 | return OkStatus(); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 178 | bool FlashPartition::AppearsErased(std::span<const byte> data) const { |
David Rogers | 6a6dae6 | 2020-07-10 01:13:38 -0700 | [diff] [blame] | 179 | byte erased_content = flash_.erased_memory_content(); |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 180 | for (byte b : data) { |
David Rogers | 6a6dae6 | 2020-07-10 01:13:38 -0700 | [diff] [blame] | 181 | if (b != erased_content) { |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 182 | return false; |
| 183 | } |
| 184 | } |
| 185 | return true; |
| 186 | } |
| 187 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 188 | Status FlashPartition::CheckBounds(Address address, size_t length) const { |
| 189 | if (address + length > size_bytes()) { |
David Rogers | 9fc78b8 | 2020-06-12 13:56:12 -0700 | [diff] [blame] | 190 | PW_LOG_ERROR( |
David Rogers | eb05d88 | 2021-10-12 15:24:48 -0700 | [diff] [blame] | 191 | "FlashPartition - Attempted access (address: %u length: %u), exceeds " |
| 192 | "partition size %u bytes", |
David Rogers | 9fc78b8 | 2020-06-12 13:56:12 -0700 | [diff] [blame] | 193 | unsigned(address), |
David Rogers | eb05d88 | 2021-10-12 15:24:48 -0700 | [diff] [blame] | 194 | unsigned(length), |
| 195 | unsigned(size_bytes())); |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 196 | return Status::OutOfRange(); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 197 | } |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 198 | return OkStatus(); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | } // namespace pw::kvs |