Skip to content
This repository was archived by the owner on Mar 7, 2026. It is now read-only.

Commit 3c5737e

Browse files
committed
stm32g0: Make use of the target structure's unsafe_enabled member in place of a custom private structure for space and complexity savings
1 parent 3dd0dcd commit 3c5737e

1 file changed

Lines changed: 10 additions & 27 deletions

File tree

src/target/stm32g0.c

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,6 @@ static stm32g0_option_register_s stm32g0_options_def[OPT_REG_COUNT] = {
230230
[OPT_REG_SECR] = {STM32G0_FPEC_SECURITY_OPT, 0x00000000},
231231
};
232232

233-
typedef struct stm32g0_priv {
234-
bool irreversible_enabled;
235-
} stm32g0_priv_s;
236-
237233
static bool stm32g0_attach(target_s *target);
238234
static void stm32g0_detach(target_s *target);
239235
static bool stm32g0_flash_erase(target_flash_s *flash, target_addr_t addr, size_t len);
@@ -279,22 +275,6 @@ static void stm32g0_add_flash(target_s *const target, const uint32_t addr, const
279275

280276
static bool stm32g0_configure_dbgmcu(target_s *const target)
281277
{
282-
/* If we're in the probe phase */
283-
if (target->target_storage == NULL) {
284-
/* Allocate target-specific storage */
285-
stm32g0_priv_s *const priv_storage = calloc(1, sizeof(*priv_storage));
286-
if (!priv_storage) { /* calloc failed: heap exhaustion */
287-
DEBUG_ERROR("calloc: failed in %s\n", __func__);
288-
return false;
289-
}
290-
target->target_storage = priv_storage;
291-
/* Mark irriversible operations disabled */
292-
priv_storage->irreversible_enabled = false;
293-
294-
target->attach = stm32g0_attach;
295-
target->detach = stm32g0_detach;
296-
}
297-
298278
/* Enable the clock for the DBGMCU if it's not already */
299279
target_mem32_write32(
300280
target, STM32G0_RCC_APBENR1, target_mem32_read32(target, STM32G0_RCC_APBENR1) | STM32G0_RCC_APBENR1_DBGEN);
@@ -388,6 +368,12 @@ bool stm32g0_probe(target_s *const target)
388368
if (!stm32g0_configure_dbgmcu(target))
389369
return false;
390370

371+
/* Mark irriversible (unsafe) operations disabled */
372+
target->unsafe_enabled = false;
373+
374+
target->attach = stm32g0_attach;
375+
target->detach = stm32g0_detach;
376+
391377
target_add_ram32(target, STM32G0_SRAM_BASE, ram_size);
392378
/* Even dual Flash bank devices have a contiguous Flash memory space */
393379
stm32g0_add_flash(target, STM32G0_FLASH_BASE, flash_size, STM32G0_FLASH_PAGE_SIZE);
@@ -518,9 +504,8 @@ static bool stm32g0_flash_write(
518504
target_flash_s *const flash, const target_addr_t dest, const void *const src, const size_t len)
519505
{
520506
target_s *const target = flash->t;
521-
stm32g0_priv_s *priv = (stm32g0_priv_s *)target->target_storage;
522507

523-
if (flash->start == STM32G0_OTP_BASE && !priv->irreversible_enabled) {
508+
if (flash->start == STM32G0_OTP_BASE && !target->unsafe_enabled) {
524509
tc_printf(target, "Irreversible operations disabled\n");
525510
stm32g0_flash_op_finish(target);
526511
return false;
@@ -691,9 +676,8 @@ static bool stm32g0_parse_cmdline_registers(
691676
/* Validates option bytes settings. Only allow level 2 device protection if explicitly allowed. */
692677
static bool stm32g0_validate_options(target_s *const target, const stm32g0_option_register_s *options_req)
693678
{
694-
stm32g0_priv_s *priv = (stm32g0_priv_s *)target->target_storage;
695679
const bool valid =
696-
(options_req[OPT_REG_OPTR].val & STM32G0_FPEC_OPTION_RDP_MASK) != 0xccU || priv->irreversible_enabled;
680+
(options_req[OPT_REG_OPTR].val & STM32G0_FPEC_OPTION_RDP_MASK) != 0xccU || target->unsafe_enabled;
697681
if (!valid)
698682
tc_printf(target, "Irreversible operations disabled\n");
699683
return valid;
@@ -740,9 +724,8 @@ static bool stm32g0_cmd_option(target_s *const target, const int argc, const cha
740724
/* Enables the irreversible operation that is level 2 device protection. */
741725
static bool stm32g0_cmd_irreversible(target_s *const target, const int argc, const char **const argv)
742726
{
743-
stm32g0_priv_s *priv = (stm32g0_priv_s *)target->target_storage;
744-
const bool ret = argc != 2 || parse_enable_or_disable(argv[1], &priv->irreversible_enabled);
745-
tc_printf(target, "Irreversible operations: %s\n", priv->irreversible_enabled ? "enabled" : "disabled");
727+
const bool ret = argc != 2 || parse_enable_or_disable(argv[1], &target->unsafe_enabled);
728+
tc_printf(target, "Irreversible operations: %s\n", target->unsafe_enabled ? "enabled" : "disabled");
746729
return ret;
747730
}
748731

0 commit comments

Comments
 (0)