Skip to content

Commit 41a5005

Browse files
committed
Fix GD32 flash read bounds and debug flow
Adjusts low-level flash handling and related debug behavior: fixes the FMC end-address boundary check, ensures debug exit is called on read validation failures, and adds read-path debug tracing in FlashCode/FMC. It also streamlines ConfigStore debug logging around timer/write paths and consolidates GD32 firmware image offset definitions in firmware.h while cleaning up preprocessor conditionals.
1 parent 2904b36 commit 41a5005

5 files changed

Lines changed: 16 additions & 31 deletions

File tree

lib-configstore/device/gd32/rom/storedevice.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,9 @@ bool StoreDevice::Erase(uint32_t offset, uint32_t length, storedevice::Result& r
8080
}
8181

8282
bool StoreDevice::Write(uint32_t offset, std::span<const uint8_t> buffer, storedevice::Result& result) {
83-
CONFIGSTORE_DEBUG_ENTRY();
84-
8583
flashcode::Result flashrom_result;
8684
const auto kState = FlashCode::Write(offset, buffer, flashrom_result);
8785

8886
result = static_cast<storedevice::Result>(flashrom_result);
89-
90-
CONFIGSTORE_DEBUG_EXIT();
9187
return kState;
9288
}

lib-configstore/include/configstore.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -518,16 +518,12 @@ class ConfigStore : StoreDevice {
518518
}
519519

520520
static void Timer([[maybe_unused]] TimerHandle_t timer_handle) {
521-
CONFIGSTORE_DEBUG_ENTRY();
522-
523521
if (!Instance().Commit()) {
524522
Instance().TimerStop();
525523

526524
CONFIGSTORE_DEBUG_EXIT();
527525
return;
528526
}
529-
530-
CONFIGSTORE_DEBUG_EXIT();
531527
}
532528

533529
void TimerStart() {
@@ -561,8 +557,6 @@ class ConfigStore : StoreDevice {
561557
}
562558

563559
bool Flash() {
564-
CONFIGSTORE_DEBUG_PUTS(kStateNames[static_cast<unsigned int>(s_state)]);
565-
566560
if (__builtin_expect((s_state == State::kIdle), 1)) {
567561
return false;
568562
}
@@ -596,6 +590,7 @@ class ConfigStore : StoreDevice {
596590
storedevice::Result result;
597591
if (StoreDevice::Write(s_start_address, std::span{s_store}.first<sizeof(ConfigurationStore)>(), result)) {
598592
s_state = State::kIdle;
593+
CONFIGSTORE_DEBUG_PUTS(kStateNames[static_cast<unsigned int>(s_state)]);
599594
return false;
600595
}
601596
assert(result == storedevice::Result::kOk);

lib-flashcode/src/gd32/flashcode.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,13 @@ uint32_t FlashCode::GetSectorSize() const {
6262
}
6363

6464
bool FlashCode::Read(uint32_t offset, std::span<uint8_t> buffer, flashcode::Result& result) {
65+
FLASHCODE_DEBUG_ENTRY();
66+
FLASHCODE_DEBUG_PRINTF("offset=%u", static_cast<unsigned>(offset));
67+
6568
const auto kStatus = gd32::fmc::Read(offset, buffer); // Blocking
6669
result = kStatus ? flashcode::Result::kOk : flashcode::Result::kError;
70+
71+
FLASHCODE_DEBUG_EXIT();
6772
return true;
6873
}
6974

lib-flashcodeinstall/include/firmware.h

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ namespace firmware {
3232
#if defined(__linux__) || defined (__APPLE__)
3333
inline constexpr char kFileName[] = "dummy.bin";
3434
#else
35-
# if defined (H3)
35+
# ifdef H3
3636
# if defined(ORANGE_PI)
3737
inline constexpr char kFileName[] = "orangepi_zero.uImage.gz";
3838
# else
3939
inline constexpr char kFileName[] = "orangepi_one.uImage.gz";
4040
# endif
41-
# elif defined (GD32)
42-
# if defined (GD32F10X)
41+
# elifdef GD32
42+
# ifdef GD32F10X
4343
inline constexpr char kFileName[] = "gd32f107.bin";
4444
# elif defined (GD32F20X)
4545
inline constexpr char kFileName[] = "gd32f207.bin";
@@ -55,7 +55,7 @@ inline constexpr char kFileName[] = "gd32h7xx.bin";
5555

5656
inline constexpr uint32_t kFileNameLength = sizeof(kFileName) - 1U;
5757

58-
#if defined (H3)
58+
#ifdef H3
5959
// nuc-i5:~/uboot-spi/u-boot$ grep CONFIG_BOOTCOMMAND include/configs/sunxi-common.h
6060
// #define CONFIG_BOOTCOMMAND "sf probe; sf read 48000000 180000 22000; bootm 48000000"
6161
# define FIRMWARE_MAX_SIZE 0x22000 // 136K
@@ -66,50 +66,36 @@ inline constexpr uint32_t kFileNameLength = sizeof(kFileName) - 1U;
6666
#elif defined (GD32)
6767
# define IH_LOAD 0x08008000
6868
# define IH_EP 0x08008000
69+
# define OFFSET_UIMAGE 0x00008000 // 32K
6970
# if defined (BOARD_GD32F107RC)
70-
# define OFFSET_UIMAGE 0x007000 // 28K
7171
# define FIRMWARE_MAX_SIZE (76 * 1024) // 76K
7272
# elif defined (BOARD_GD32F207RG)
73-
# define OFFSET_UIMAGE 0x008000 // 32K
7473
# define FIRMWARE_MAX_SIZE (234 * 1024) // 234K
7574
# elif defined (BOARD_GD32F207VC_2)
76-
# define OFFSET_UIMAGE 0x008000 // 32K
7775
# define FIRMWARE_MAX_SIZE (106 * 1024) // 106K
7876
# elif defined (BOARD_GD32F207VC_4)
79-
# define OFFSET_UIMAGE 0x008000 // 32K
8077
# define FIRMWARE_MAX_SIZE (106 * 1024) // 106K
8178
# elif defined (BOARD_GD32F207C_EVAL)
82-
# define OFFSET_UIMAGE 0x008000 // 32K
8379
# define FIRMWARE_MAX_SIZE (106 * 1024) // 106K
8480
# elif defined (BOARD_GD32F407RE)
85-
# define OFFSET_UIMAGE 0x008000 // 32K
8681
# define FIRMWARE_MAX_SIZE (116 * 1024) // 116K
8782
# elif defined (BOARD_BW_OPIDMX4)
88-
# define OFFSET_UIMAGE 0x008000 // 32K
8983
# define FIRMWARE_MAX_SIZE (116 * 1024) // 116K
9084
# elif defined (BOARD_DMX3)
91-
# define OFFSET_UIMAGE 0x008000 // 32K
9285
# define FIRMWARE_MAX_SIZE (116 * 1024) // 116K
9386
# elif defined (BOARD_DMX4)
94-
# define OFFSET_UIMAGE 0x008000 // 32K
9587
# define FIRMWARE_MAX_SIZE (116 * 1024) // 116K
9688
# elif defined (BOARD_GD32F450VE)
97-
# define OFFSET_UIMAGE 0x008000 // 32K
9889
# define FIRMWARE_MAX_SIZE (180 * 1024) // 180K
9990
# elif defined (BOARD_GD32F450VI)
100-
# define OFFSET_UIMAGE 0x008000 // 32K
10191
# define FIRMWARE_MAX_SIZE (234 * 1024) // 234K
10292
# elif defined (BOARD_16X4U_PIXEL)
103-
# define OFFSET_UIMAGE 0x008000 // 32K
10493
# define FIRMWARE_MAX_SIZE (234 * 1024) // 234K
10594
# elif defined (BOARD_GD32F470VG)
106-
# define OFFSET_UIMAGE 0x008000 // 32K
10795
# define FIRMWARE_MAX_SIZE (234 * 1024) // 234K
10896
# elif defined (BOARD_GD32F470Z_EVAL)
109-
# define OFFSET_UIMAGE 0x008000 // 32K
11097
# define FIRMWARE_MAX_SIZE (175 * 1024) // 175K
11198
# elif defined (BOARD_GD32H759I_EVAL)
112-
# define OFFSET_UIMAGE 0x008000 // 32K
11399
# define FIRMWARE_MAX_SIZE (300 * 1024) // 300K
114100
# else
115101
# error Board is not supported

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)