Skip to content

Commit 67fb951

Browse files
committed
Fix FMC read address bounds
The FMC flash end address was off by one, causing boundary checks to reject valid reads at the end of the flash region. This updates the end-of-range calculation to the actual flash limit and adds debug logging around the read validation path.
1 parent 6060eee commit 67fb951

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

lib-gd32/src/f/fmc/gd32_fmc.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ constexpr auto kBanK1FlashPage = 4 * k1KiB;
5151
constexpr uint32_t kStartAddress = FLASH_BASE;
5252
constexpr uint32_t kBank0StartAddress = kStartAddress;
5353
constexpr uint32_t kBank1StartAddress = 0x08080000;
54-
const uint32_t kEndAddress = (kStartAddress + (FMC_SIZE * k1KiB) - 1);
54+
const uint32_t kEndAddress = (kStartAddress + (FMC_SIZE * k1KiB));
5555

5656
enum class State { kIdle, kEraseBusy, kEraseProgram, kWriteBusy, kWriteProgram };
5757

@@ -124,9 +124,12 @@ bool Read(uint32_t offset, std::span<uint8_t> buffer) {
124124
GD32_FMC_DEBUG_ENTRY();
125125

126126
const auto kAddress = offset + FLASH_BASE;
127+
128+
GD32_FMC_DEBUG_PRINTF("kStartAddress=%p, kAddress=%p, kEndAddress=%p", reinterpret_cast<void*>(kStartAddress), reinterpret_cast<void*>(kAddress), reinterpret_cast<void*>(kEndAddress));
127129

128130
if (buffer.empty() || ((buffer.size() % sizeof(uint32_t)) != 0) || (kAddress < kStartAddress) || (kAddress >= kEndAddress) || (buffer.size() > (kEndAddress - kAddress))) {
129-
return false;
131+
GD32_FMC_DEBUG_EXIT();
132+
return false;
130133
}
131134

132135
assert((reinterpret_cast<uintptr_t>(buffer.data()) % alignof(uint32_t)) == 0);

0 commit comments

Comments
 (0)