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 | |
| 15 | #include "pw_kvs/flash_memory.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include <cinttypes> |
| 19 | #include <cstring> |
| 20 | |
| 21 | #include "pw_kvs_private/macros.h" |
| 22 | #include "pw_log/log.h" |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 23 | #include "pw_status/status_with_size.h" |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 24 | |
| 25 | namespace pw::kvs { |
| 26 | |
| 27 | using std::byte; |
| 28 | |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 29 | StatusWithSize FlashPartition::Output::Write(span<const byte> data) { |
Wyatt Hepler | 50f7077 | 2020-02-13 11:25:10 -0800 | [diff] [blame] | 30 | TRY_WITH_SIZE(flash_.Write(address_, data)); |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 31 | address_ += data.size(); |
| 32 | return StatusWithSize(data.size()); |
| 33 | } |
| 34 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 35 | Status FlashPartition::Erase(Address address, size_t num_sectors) { |
| 36 | if (permission_ == PartitionPermission::kReadOnly) { |
| 37 | return Status::PERMISSION_DENIED; |
| 38 | } |
| 39 | |
| 40 | TRY(CheckBounds(address, num_sectors * sector_size_bytes())); |
| 41 | return flash_.Erase(PartitionToFlashAddress(address), num_sectors); |
| 42 | } |
| 43 | |
| 44 | StatusWithSize FlashPartition::Read(Address address, span<byte> output) { |
Wyatt Hepler | 50f7077 | 2020-02-13 11:25:10 -0800 | [diff] [blame] | 45 | TRY_WITH_SIZE(CheckBounds(address, output.size())); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 46 | return flash_.Read(PartitionToFlashAddress(address), output); |
| 47 | } |
| 48 | |
| 49 | StatusWithSize FlashPartition::Write(Address address, span<const byte> data) { |
| 50 | if (permission_ == PartitionPermission::kReadOnly) { |
Wyatt Hepler | f707880 | 2020-02-25 13:50:05 -0800 | [diff] [blame] | 51 | return StatusWithSize::PERMISSION_DENIED; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 52 | } |
Wyatt Hepler | 50f7077 | 2020-02-13 11:25:10 -0800 | [diff] [blame] | 53 | TRY_WITH_SIZE(CheckBounds(address, data.size())); |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 54 | return flash_.Write(PartitionToFlashAddress(address), data); |
| 55 | } |
| 56 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 57 | Status FlashPartition::IsRegionErased(Address source_flash_address, |
| 58 | size_t length, |
| 59 | bool* is_erased) { |
| 60 | // Max alignment is artificial to keep the stack usage low for this |
| 61 | // function. Using 16 because it's the alignment of encrypted flash. |
| 62 | constexpr size_t kMaxAlignment = 16; |
| 63 | |
| 64 | // Relying on Read() to check address and len arguments. |
| 65 | if (is_erased == nullptr) { |
| 66 | return Status::INVALID_ARGUMENT; |
| 67 | } |
| 68 | const size_t alignment = alignment_bytes(); |
| 69 | if (alignment > kMaxAlignment || kMaxAlignment % alignment || |
| 70 | length % alignment) { |
| 71 | return Status::INVALID_ARGUMENT; |
| 72 | } |
| 73 | |
| 74 | byte buffer[kMaxAlignment]; |
| 75 | byte erased_pattern_buffer[kMaxAlignment]; |
| 76 | |
| 77 | size_t offset = 0; |
| 78 | std::memset(erased_pattern_buffer, |
| 79 | int(flash_.erased_memory_content()), |
| 80 | sizeof(erased_pattern_buffer)); |
| 81 | *is_erased = false; |
| 82 | while (length > 0u) { |
| 83 | // Check earlier that length is aligned, no need to round up |
| 84 | size_t read_size = std::min(sizeof(buffer), length); |
| 85 | TRY(Read(source_flash_address + offset, read_size, buffer).status()); |
| 86 | if (std::memcmp(buffer, erased_pattern_buffer, read_size)) { |
| 87 | // Detected memory chunk is not entirely erased |
| 88 | return Status::OK; |
| 89 | } |
| 90 | offset += read_size; |
| 91 | length -= read_size; |
| 92 | } |
| 93 | *is_erased = true; |
| 94 | return Status::OK; |
| 95 | } |
| 96 | |
Wyatt Hepler | e541e07 | 2020-02-14 09:10:53 -0800 | [diff] [blame] | 97 | bool FlashPartition::AppearsErased(span<const byte> data) const { |
| 98 | for (byte b : data) { |
| 99 | if (b != flash_.erased_memory_content()) { |
| 100 | return false; |
| 101 | } |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 106 | Status FlashPartition::CheckBounds(Address address, size_t length) const { |
| 107 | if (address + length > size_bytes()) { |
| 108 | PW_LOG_ERROR("Attempted out-of-bound flash memory access (address: %" PRIu32 |
| 109 | " length: %zu)", |
| 110 | address, |
| 111 | length); |
Wyatt Hepler | 6c24c06 | 2020-02-05 15:30:49 -0800 | [diff] [blame] | 112 | return Status::OUT_OF_RANGE; |
Wyatt Hepler | 4da1fcb | 2020-01-30 17:32:18 -0800 | [diff] [blame] | 113 | } |
| 114 | return Status::OK; |
| 115 | } |
| 116 | |
| 117 | } // namespace pw::kvs |