blob: 0d164d8983251d6711968e9bff654d6d58a43a2e [file] [log] [blame]
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -08001// 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 Hepler1927c282020-02-11 16:45:02 -080023#include "pw_status/status_with_size.h"
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080024
25namespace pw::kvs {
26
27using std::byte;
28
Wyatt Hepler1927c282020-02-11 16:45:02 -080029StatusWithSize FlashPartition::Output::Write(span<const byte> data) {
Wyatt Hepler50f70772020-02-13 11:25:10 -080030 TRY_WITH_SIZE(flash_.Write(address_, data));
Wyatt Hepler1927c282020-02-11 16:45:02 -080031 address_ += data.size();
32 return StatusWithSize(data.size());
33}
34
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080035Status 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
44StatusWithSize FlashPartition::Read(Address address, span<byte> output) {
Wyatt Hepler50f70772020-02-13 11:25:10 -080045 TRY_WITH_SIZE(CheckBounds(address, output.size()));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080046 return flash_.Read(PartitionToFlashAddress(address), output);
47}
48
49StatusWithSize FlashPartition::Write(Address address, span<const byte> data) {
50 if (permission_ == PartitionPermission::kReadOnly) {
Wyatt Heplerf7078802020-02-25 13:50:05 -080051 return StatusWithSize::PERMISSION_DENIED;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080052 }
Wyatt Hepler50f70772020-02-13 11:25:10 -080053 TRY_WITH_SIZE(CheckBounds(address, data.size()));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080054 return flash_.Write(PartitionToFlashAddress(address), data);
55}
56
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080057Status 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 Heplere541e072020-02-14 09:10:53 -080097bool 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 Hepler4da1fcb2020-01-30 17:32:18 -0800106Status 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 Hepler6c24c062020-02-05 15:30:49 -0800112 return Status::OUT_OF_RANGE;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800113 }
114 return Status::OK;
115}
116
117} // namespace pw::kvs