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 | |
Armando Montanez | 28ecccb | 2020-05-04 15:35:54 -0700 | [diff] [blame] | 15 | #define PW_LOG_MODULE_NAME "KVS" |
| 16 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 17 | #include "pw_kvs/flash_memory.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <cinttypes> |
| 21 | #include <cstring> |
| 22 | |
David Rogers | ca59296 | 2020-07-01 09:21:54 -0700 | [diff] [blame] | 23 | #include "pw_kvs_private/config.h" |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 24 | #include "pw_kvs_private/macros.h" |
| 25 | #include "pw_log/log.h" |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 26 | #include "pw_status/status_with_size.h" |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 27 | |
| 28 | namespace pw::kvs { |
| 29 | |
| 30 | using std::byte; |
| 31 | |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 32 | StatusWithSize FlashPartition::Output::DoWrite(std::span<const byte> data) { |
Wyatt Hepler | 50f7077 | 2020-02-13 11:25:10 -0800 | [diff] [blame] | 33 | TRY_WITH_SIZE(flash_.Write(address_, data)); |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 34 | address_ += data.size(); |
| 35 | return StatusWithSize(data.size()); |
| 36 | } |
| 37 | |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 38 | StatusWithSize FlashPartition::Input::DoRead(std::span<byte> data) { |
Wyatt Hepler | ee6fd76 | 2020-03-09 08:32:19 -0700 | [diff] [blame] | 39 | StatusWithSize result = flash_.Read(address_, data); |
| 40 | address_ += result.size(); |
| 41 | return result; |
| 42 | } |
| 43 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 44 | Status FlashPartition::Erase(Address address, size_t num_sectors) { |
| 45 | if (permission_ == PartitionPermission::kReadOnly) { |
| 46 | return Status::PERMISSION_DENIED; |
| 47 | } |
| 48 | |
| 49 | TRY(CheckBounds(address, num_sectors * sector_size_bytes())); |
| 50 | return flash_.Erase(PartitionToFlashAddress(address), num_sectors); |
| 51 | } |
| 52 | |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 53 | StatusWithSize FlashPartition::Read(Address address, std::span<byte> output) { |
Wyatt Hepler | 50f7077 | 2020-02-13 11:25:10 -0800 | [diff] [blame] | 54 | TRY_WITH_SIZE(CheckBounds(address, output.size())); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 55 | return flash_.Read(PartitionToFlashAddress(address), output); |
| 56 | } |
| 57 | |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 58 | StatusWithSize FlashPartition::Write(Address address, |
| 59 | std::span<const byte> data) { |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 60 | if (permission_ == PartitionPermission::kReadOnly) { |
Wyatt Hepler | f707880 | 2020-02-25 13:50:05 -0800 | [diff] [blame] | 61 | return StatusWithSize::PERMISSION_DENIED; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 62 | } |
Wyatt Hepler | 50f7077 | 2020-02-13 11:25:10 -0800 | [diff] [blame] | 63 | TRY_WITH_SIZE(CheckBounds(address, data.size())); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 64 | return flash_.Write(PartitionToFlashAddress(address), data); |
| 65 | } |
| 66 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 67 | Status FlashPartition::IsRegionErased(Address source_flash_address, |
| 68 | size_t length, |
| 69 | bool* is_erased) { |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 70 | // Relying on Read() to check address and len arguments. |
| 71 | if (is_erased == nullptr) { |
| 72 | return Status::INVALID_ARGUMENT; |
| 73 | } |
David Rogers | ca59296 | 2020-07-01 09:21:54 -0700 | [diff] [blame] | 74 | |
| 75 | // TODO(pwbug/214): Currently using a single flash alignment to do both the |
| 76 | // read and write. The allowable flash read length may be less than what write |
| 77 | // needs (possibly by a bunch), resulting in buffer and erased_pattern_buffer |
| 78 | // being bigger than they need to be. |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 79 | const size_t alignment = alignment_bytes(); |
David Rogers | ca59296 | 2020-07-01 09:21:54 -0700 | [diff] [blame] | 80 | if (alignment > kMaxFlashAlignment || kMaxFlashAlignment % alignment || |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 81 | length % alignment) { |
| 82 | return Status::INVALID_ARGUMENT; |
| 83 | } |
| 84 | |
David Rogers | ca59296 | 2020-07-01 09:21:54 -0700 | [diff] [blame] | 85 | byte buffer[kMaxFlashAlignment]; |
David Rogers | a5661ef | 2020-07-09 15:15:12 -0700 | [diff] [blame] | 86 | const byte erased_byte = flash_.erased_memory_content(); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 87 | size_t offset = 0; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 88 | *is_erased = false; |
| 89 | while (length > 0u) { |
| 90 | // Check earlier that length is aligned, no need to round up |
| 91 | size_t read_size = std::min(sizeof(buffer), length); |
| 92 | TRY(Read(source_flash_address + offset, read_size, buffer).status()); |
David Rogers | a5661ef | 2020-07-09 15:15:12 -0700 | [diff] [blame] | 93 | |
| 94 | for (byte b : std::span(buffer, read_size)) { |
| 95 | if (b != erased_byte) { |
| 96 | // Detected memory chunk is not entirely erased |
| 97 | return Status::OK; |
| 98 | } |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 99 | } |
David Rogers | a5661ef | 2020-07-09 15:15:12 -0700 | [diff] [blame] | 100 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 101 | offset += read_size; |
| 102 | length -= read_size; |
| 103 | } |
| 104 | *is_erased = true; |
| 105 | return Status::OK; |
| 106 | } |
| 107 | |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 108 | bool FlashPartition::AppearsErased(std::span<const byte> data) const { |
David Rogers | 6a6dae6 | 2020-07-10 01:13:38 -0700 | [diff] [blame^] | 109 | byte erased_content = flash_.erased_memory_content(); |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 110 | for (byte b : data) { |
David Rogers | 6a6dae6 | 2020-07-10 01:13:38 -0700 | [diff] [blame^] | 111 | if (b != erased_content) { |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 118 | Status FlashPartition::CheckBounds(Address address, size_t length) const { |
| 119 | if (address + length > size_bytes()) { |
David Rogers | 9fc78b8 | 2020-06-12 13:56:12 -0700 | [diff] [blame] | 120 | PW_LOG_ERROR( |
| 121 | "Attempted out-of-bound flash memory access (address: %u length: %u)", |
| 122 | unsigned(address), |
| 123 | unsigned(length)); |
Wyatt Hepler | 6c24c06 | 2020-02-05 15:30:49 -0800 | [diff] [blame] | 124 | return Status::OUT_OF_RANGE; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 125 | } |
| 126 | return Status::OK; |
| 127 | } |
| 128 | |
| 129 | } // namespace pw::kvs |