blob: aba923be1d343e260723d81008f2de6d369f2e25 [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
Armando Montanez28ecccb2020-05-04 15:35:54 -070015#define PW_LOG_MODULE_NAME "KVS"
16
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080017#include "pw_kvs/flash_memory.h"
18
19#include <algorithm>
20#include <cinttypes>
21#include <cstring>
22
David Rogersca592962020-07-01 09:21:54 -070023#include "pw_kvs_private/config.h"
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080024#include "pw_kvs_private/macros.h"
25#include "pw_log/log.h"
Wyatt Hepler1927c282020-02-11 16:45:02 -080026#include "pw_status/status_with_size.h"
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080027
28namespace pw::kvs {
29
30using std::byte;
31
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070032StatusWithSize FlashPartition::Output::DoWrite(std::span<const byte> data) {
Wyatt Hepler50f70772020-02-13 11:25:10 -080033 TRY_WITH_SIZE(flash_.Write(address_, data));
Wyatt Hepler1927c282020-02-11 16:45:02 -080034 address_ += data.size();
35 return StatusWithSize(data.size());
36}
37
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070038StatusWithSize FlashPartition::Input::DoRead(std::span<byte> data) {
Wyatt Hepleree6fd762020-03-09 08:32:19 -070039 StatusWithSize result = flash_.Read(address_, data);
40 address_ += result.size();
41 return result;
42}
43
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080044Status 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 Heplere2cbadf2020-06-22 11:21:45 -070053StatusWithSize FlashPartition::Read(Address address, std::span<byte> output) {
Wyatt Hepler50f70772020-02-13 11:25:10 -080054 TRY_WITH_SIZE(CheckBounds(address, output.size()));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080055 return flash_.Read(PartitionToFlashAddress(address), output);
56}
57
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070058StatusWithSize FlashPartition::Write(Address address,
59 std::span<const byte> data) {
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080060 if (permission_ == PartitionPermission::kReadOnly) {
Wyatt Heplerf7078802020-02-25 13:50:05 -080061 return StatusWithSize::PERMISSION_DENIED;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080062 }
Wyatt Hepler50f70772020-02-13 11:25:10 -080063 TRY_WITH_SIZE(CheckBounds(address, data.size()));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080064 return flash_.Write(PartitionToFlashAddress(address), data);
65}
66
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080067Status FlashPartition::IsRegionErased(Address source_flash_address,
68 size_t length,
69 bool* is_erased) {
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080070 // Relying on Read() to check address and len arguments.
71 if (is_erased == nullptr) {
72 return Status::INVALID_ARGUMENT;
73 }
David Rogersca592962020-07-01 09:21:54 -070074
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 Hepler4da1fcb2020-01-30 17:32:18 -080079 const size_t alignment = alignment_bytes();
David Rogersca592962020-07-01 09:21:54 -070080 if (alignment > kMaxFlashAlignment || kMaxFlashAlignment % alignment ||
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080081 length % alignment) {
82 return Status::INVALID_ARGUMENT;
83 }
84
David Rogersca592962020-07-01 09:21:54 -070085 byte buffer[kMaxFlashAlignment];
David Rogersa5661ef2020-07-09 15:15:12 -070086 const byte erased_byte = flash_.erased_memory_content();
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080087 size_t offset = 0;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080088 *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 Rogersa5661ef2020-07-09 15:15:12 -070093
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 Hepler4da1fcb2020-01-30 17:32:18 -080099 }
David Rogersa5661ef2020-07-09 15:15:12 -0700100
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800101 offset += read_size;
102 length -= read_size;
103 }
104 *is_erased = true;
105 return Status::OK;
106}
107
Wyatt Heplere2cbadf2020-06-22 11:21:45 -0700108bool FlashPartition::AppearsErased(std::span<const byte> data) const {
David Rogers6a6dae62020-07-10 01:13:38 -0700109 byte erased_content = flash_.erased_memory_content();
Wyatt Heplere541e072020-02-14 09:10:53 -0800110 for (byte b : data) {
David Rogers6a6dae62020-07-10 01:13:38 -0700111 if (b != erased_content) {
Wyatt Heplere541e072020-02-14 09:10:53 -0800112 return false;
113 }
114 }
115 return true;
116}
117
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800118Status FlashPartition::CheckBounds(Address address, size_t length) const {
119 if (address + length > size_bytes()) {
David Rogers9fc78b82020-06-12 13:56:12 -0700120 PW_LOG_ERROR(
121 "Attempted out-of-bound flash memory access (address: %u length: %u)",
122 unsigned(address),
123 unsigned(length));
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800124 return Status::OUT_OF_RANGE;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800125 }
126 return Status::OK;
127}
128
129} // namespace pw::kvs