target_flash: change return types to bool

This commit is contained in:
Rafael Silva 2022-08-24 11:49:09 +01:00 committed by Rachel Mant
parent aec7460426
commit 1a1f4b76ff
7 changed files with 77 additions and 91 deletions

View File

@ -630,7 +630,7 @@ static void handle_v_packet(char *packet, const size_t plen)
return;
}
if (target_flash_erase(cur_target, addr, len) == 0)
if (target_flash_erase(cur_target, addr, len))
gdb_putpacketz("OK");
else {
target_flash_complete(cur_target);
@ -641,7 +641,7 @@ static void handle_v_packet(char *packet, const size_t plen)
/* Write Flash Memory */
const uint32_t count = plen - bin;
DEBUG_GDB("Flash Write %08" PRIX32 " %08" PRIX32 "\n", addr, count);
if (cur_target && target_flash_write(cur_target, addr, (void*)packet + bin, count) == 0)
if (cur_target && target_flash_write(cur_target, addr, (void*)packet + bin, count))
gdb_putpacketz("OK");
else {
target_flash_complete(cur_target);
@ -650,7 +650,7 @@ static void handle_v_packet(char *packet, const size_t plen)
} else if (!strcmp(packet, "vFlashDone")) {
/* Commit flash operations. */
if (target_flash_complete(cur_target) == 0)
if (target_flash_complete(cur_target))
gdb_putpacketz("OK");
else
gdb_putpacketz("EFF");

View File

@ -59,9 +59,9 @@ bool target_mem_map(target *t, char *buf, size_t len);
int target_mem_read(target *t, void *dest, target_addr_t src, size_t len);
int target_mem_write(target *t, target_addr_t dest, const void *src, size_t len);
/* Flash memory access functions */
int target_flash_erase(target *t, target_addr_t addr, size_t len);
int target_flash_write(target *t, target_addr_t dest, const void *src, size_t len);
int target_flash_complete(target *t);
bool target_flash_erase(target *t, target_addr_t addr, size_t len);
bool target_flash_write(target *t, target_addr_t dest, const void *src, size_t len);
bool target_flash_complete(target *t);
/* Register access functions */
size_t target_regs_size(target *t);

View File

@ -549,36 +549,24 @@ found_targets:
if (opt->opt_mode == BMP_MODE_RESET) {
target_reset(t);
} else if (opt->opt_mode == BMP_MODE_FLASH_ERASE) {
DEBUG_INFO("Erase %zu bytes at 0x%08" PRIx32 "\n", opt->opt_flash_size,
opt->opt_flash_start);
unsigned int erased = target_flash_erase(t, opt->opt_flash_start,
opt->opt_flash_size);
if (erased) {
DEBUG_INFO("Erase %zu bytes at 0x%08" PRIx32 "\n", opt->opt_flash_size, opt->opt_flash_start);
if (!target_flash_erase(t, opt->opt_flash_start, opt->opt_flash_size)) {
DEBUG_WARN("Erasure failed!\n");
res = -1;
goto free_map;
}
target_reset(t);
} else if ((opt->opt_mode == BMP_MODE_FLASH_WRITE) ||
(opt->opt_mode == BMP_MODE_FLASH_WRITE_VERIFY)) {
DEBUG_INFO("Erase %zu bytes at 0x%08" PRIx32 "\n", map.size,
opt->opt_flash_start);
} else if ((opt->opt_mode == BMP_MODE_FLASH_WRITE) || (opt->opt_mode == BMP_MODE_FLASH_WRITE_VERIFY)) {
DEBUG_INFO("Erase %zu bytes at 0x%08" PRIx32 "\n", map.size, opt->opt_flash_start);
uint32_t start_time = platform_time_ms();
unsigned int erased = target_flash_erase(t, opt->opt_flash_start,
map.size);
if (erased) {
if (!target_flash_erase(t, opt->opt_flash_start, map.size)) {
DEBUG_WARN("Erasure failed!\n");
res = -1;
goto free_map;
} else {
DEBUG_INFO("Flashing %zu bytes at 0x%08" PRIx32 "\n",
map.size, opt->opt_flash_start);
unsigned int flashed = target_flash_write(t, opt->opt_flash_start,
map.data, map.size);
DEBUG_INFO("Flashing %zu bytes at 0x%08" PRIx32 "\n", map.size, opt->opt_flash_start);
/* Buffered write cares for padding*/
if (!flashed)
flashed = target_flash_complete(t);
if (flashed) {
if (!target_flash_write(t, opt->opt_flash_start, map.data, map.size) || !target_flash_complete(t)) {
DEBUG_WARN("Flashing failed!\n");
res = -1;
goto free_map;

View File

@ -113,7 +113,7 @@ static bool kinetis_cmd_unsafe(target *t, int argc, char **argv)
static int kinetis_flash_cmd_erase(target_flash_s *f, target_addr_t addr, size_t len);
static int kinetis_flash_cmd_write(target_flash_s *f, target_addr_t dest, const void *src, size_t len);
static int kinetis_flash_done(target_flash_s *f);
static bool kinetis_flash_done(target_flash_s *f);
struct kinetis_flash {
target_flash_s f;
@ -479,15 +479,15 @@ static int kinetis_flash_cmd_write(target_flash_s *f, target_addr_t dest, const
return 0;
}
static int kinetis_flash_done(target_flash_s *const f)
static bool kinetis_flash_done(target_flash_s *const f)
{
struct kinetis_flash *const kf = (struct kinetis_flash *)f;
if (f->t->unsafe_enabled)
return 0;
return true;
if (target_mem_read8(f->t, FLASH_SECURITY_BYTE_ADDRESS) == FLASH_SECURITY_BYTE_UNSECURED)
return 0;
return true;
/* Load the security byte based on the alignment (determine 8 byte phrases
* vs 4 byte phrases).
@ -505,7 +505,7 @@ static int kinetis_flash_done(target_flash_s *const f)
kinetis_fccob_cmd(f->t, FTFx_CMD_PROGRAM_LONGWORD, FLASH_SECURITY_BYTE_ADDRESS, &val, 1);
}
return 0;
return true;
}
/*** Kinetis recovery mode using the MDM-AP ***/

View File

@ -118,7 +118,7 @@ static const uint8_t cmdLen[] = {
static bool ke04_command(target *t, uint8_t cmd, uint32_t addr, const uint8_t data[8]);
static int ke04_flash_erase(target_flash_s *f, target_addr_t addr, size_t len);
static int ke04_flash_write(target_flash_s *f, target_addr_t dest, const void *src, size_t len);
static int ke04_flash_done(target_flash_s *f);
static bool ke04_flash_done(target_flash_s *f);
static bool ke04_mass_erase(target *t);
/* Target specific commands */
@ -359,22 +359,20 @@ static int ke04_flash_write(target_flash_s *f, target_addr_t dest, const void *s
return 0;
}
static int ke04_flash_done(target_flash_s *f)
static bool ke04_flash_done(target_flash_s *f)
{
target *t = f->t;
if (t->unsafe_enabled)
return 0;
return true;
if (target_mem_read8(f->t, FLASH_SECURITY_BYTE_ADDRESS) ==
FLASH_SECURITY_BYTE_UNSECURED)
return 0;
if (target_mem_read8(f->t, FLASH_SECURITY_BYTE_ADDRESS) == FLASH_SECURITY_BYTE_UNSECURED)
return true;
/* Load the security byte from its field */
/* Note: Cumulative programming is not allowed according to the RM */
uint32_t vals[2] = {target_mem_read32(f->t, FLASH_SECURITY_WORD_ADDRESS), 0};
vals[0] = (vals[0] & 0xff00ffff) | (FLASH_SECURITY_BYTE_UNSECURED << 16);
ke04_command(f->t, CMD_PROGRAM_FLASH_32,
FLASH_SECURITY_WORD_ADDRESS, (uint8_t *)&vals);
ke04_command(f->t, CMD_PROGRAM_FLASH_32, FLASH_SECURITY_WORD_ADDRESS, (uint8_t *)&vals);
return 0;
return true;
}

View File

@ -38,8 +38,8 @@
#include "general.h"
#include "target_internal.h"
static int flash_buffered_write(target_flash_s *f, target_addr_t dest, const void *src, size_t len);
static int flash_buffered_flush(target_flash_s *f);
static bool flash_buffered_write(target_flash_s *f, target_addr_t dest, const void *src, size_t len);
static bool flash_buffered_flush(target_flash_s *f);
target_flash_s *target_flash_for_addr(target *t, uint32_t addr)
{
@ -49,12 +49,12 @@ target_flash_s *target_flash_for_addr(target *t, uint32_t addr)
return NULL;
}
static int target_enter_flash_mode(target *t)
static bool target_enter_flash_mode(target *t)
{
if (t->flash_mode)
return 0;
return true;
int ret = 0;
bool ret = true;
if (t->enter_flash_mode)
ret = t->enter_flash_mode(t);
else
@ -62,18 +62,18 @@ static int target_enter_flash_mode(target *t)
/* This saves us if we're interrupted in IRQ context */
target_reset(t);
if (ret == 0)
if (ret == true)
t->flash_mode = true;
return ret;
}
static int target_exit_flash_mode(target *t)
static bool target_exit_flash_mode(target *t)
{
if (!t->flash_mode)
return 0;
return true;
int ret = 0;
bool ret = true;
if (t->exit_flash_mode)
ret = t->exit_flash_mode(t);
else
@ -85,27 +85,27 @@ static int target_exit_flash_mode(target *t)
return ret;
}
static int flash_prepare(target_flash_s *f)
static bool flash_prepare(target_flash_s *f)
{
if (f->ready)
return 0;
return true;
int ret = 0;
bool ret = true;
if (f->prepare)
ret = f->prepare(f);
if (ret == 0)
if (ret == true)
f->ready = true;
return ret;
}
static int flash_done(target_flash_s *f)
static bool flash_done(target_flash_s *f)
{
if (!f->ready)
return 0;
return true;
int ret = 0;
bool ret = true;
if (f->done)
ret = f->done(f);
@ -119,65 +119,65 @@ static int flash_done(target_flash_s *f)
return ret;
}
int target_flash_erase(target *t, target_addr_t addr, size_t len)
bool target_flash_erase(target *t, target_addr_t addr, size_t len)
{
if (target_enter_flash_mode(t) != 0)
return 1;
if (!target_enter_flash_mode(t))
return false;
int ret = 0;
bool ret = true; /* catch false returns with &= */
while (len) {
target_flash_s *f = target_flash_for_addr(t, addr);
if (!f) {
DEBUG_WARN("Requested address is outside the valid range 0x%06" PRIx32 "\n", addr);
return 1;
return false;
}
/* terminate flash operations if we're not in the same target flash */
for (target_flash_s *target_f = t->flash; target_f; target_f = target_f->next)
if (target_f != f)
ret |= flash_done(target_f);
ret &= flash_done(target_f);
const target_addr_t local_start_addr = addr & ~(f->blocksize - 1U);
const target_addr_t local_end_addr = local_start_addr + f->blocksize;
if (flash_prepare(f) != 0)
return -1;
if (!flash_prepare(f))
return false;
ret |= f->erase(f, local_start_addr, f->blocksize);
ret &= f->erase(f, local_start_addr, f->blocksize) == 0;
len -= MIN(local_end_addr - addr, len);
addr = local_end_addr;
/* Issue flash done on last operation */
if (len == 0)
ret |= flash_done(f);
ret &= flash_done(f);
}
return ret;
}
int target_flash_write(target *t, target_addr_t dest, const void *src, size_t len)
bool target_flash_write(target *t, target_addr_t dest, const void *src, size_t len)
{
if (target_enter_flash_mode(t) != 0)
return 1;
if (!target_enter_flash_mode(t))
return false;
int ret = 0;
bool ret = true; /* catch false returns with &= */
while (len) {
target_flash_s *f = target_flash_for_addr(t, dest);
if (!f)
return 1;
return false;
/* terminate flash operations if we're not in the same target flash */
for (target_flash_s *target_f = t->flash; target_f; target_f = target_f->next) {
if (target_f != f) {
ret |= flash_buffered_flush(target_f);
ret |= flash_done(target_f);
ret &= flash_buffered_flush(target_f);
ret &= flash_done(target_f);
}
}
const target_addr_t local_end_addr = MIN(dest + len, f->start + f->length);
const target_addr_t local_length = local_end_addr - dest;
ret |= flash_buffered_write(f, dest, src, local_length);
ret &= flash_buffered_write(f, dest, src, local_length);
dest = local_end_addr;
src += local_length;
@ -185,49 +185,49 @@ int target_flash_write(target *t, target_addr_t dest, const void *src, size_t le
/* Flush operations if we reached the end of Flash */
if (dest == f->start + f->length) {
ret |= flash_buffered_flush(f);
ret |= flash_done(f);
ret &= flash_buffered_flush(f);
ret &= flash_done(f);
}
}
return ret;
}
int target_flash_complete(target *t)
bool target_flash_complete(target *t)
{
if (!t->flash_mode)
return -1;
return false;
int ret = 0;
bool ret = true; /* catch false returns with &= */
for (target_flash_s *f = t->flash; f; f = f->next) {
ret |= flash_buffered_flush(f);
ret |= flash_done(f);
ret &= flash_buffered_flush(f);
ret &= flash_done(f);
}
target_exit_flash_mode(t);
return ret;
}
static int flash_buffered_write(target_flash_s *f, target_addr_t dest, const void *src, size_t len)
static bool flash_buffered_write(target_flash_s *f, target_addr_t dest, const void *src, size_t len)
{
if (f->buf == NULL) {
/* Allocate buffer */
f->buf = malloc(f->writebufsize);
if (!f->buf) { /* malloc failed: heap exhaustion */
DEBUG_WARN("malloc: failed in %s\n", __func__);
return -1;
return false;
}
f->buf_addr_base = UINT32_MAX;
f->buf_addr_low = UINT32_MAX;
f->buf_addr_high = 0;
}
int ret = 0;
bool ret = true; /* catch false returns with &= */
while (len) {
const target_addr_t base_addr = dest & ~(f->writebufsize - 1U);
/* check for base address change */
if (base_addr != f->buf_addr_base) {
ret |= flash_buffered_flush(f);
ret &= flash_buffered_flush(f);
/* Setup buffer */
f->buf_addr_base = base_addr;
@ -251,15 +251,15 @@ static int flash_buffered_write(target_flash_s *f, target_addr_t dest, const voi
return ret;
}
static int flash_buffered_flush(target_flash_s *f)
static bool flash_buffered_flush(target_flash_s *f)
{
int ret = 0;
bool ret = true; /* catch false returns with &= */
if (f->buf && f->buf_addr_base != UINT32_MAX && f->buf_addr_low != UINT32_MAX &&
f->buf_addr_low < f->buf_addr_high) {
/* Write buffer to flash */
if (flash_prepare(f) != 0)
return -1;
if (!flash_prepare(f))
return false;
target_addr_t aligned_addr = f->buf_addr_low & ~(f->writesize - 1U);
@ -267,7 +267,7 @@ static int flash_buffered_flush(target_flash_s *f)
uint32_t len = f->buf_addr_high - aligned_addr;
while (len) {
ret = f->write(f, aligned_addr, src, f->writesize);
ret &= f->write(f, aligned_addr, src, f->writesize) == 0;
aligned_addr += f->writesize;
src += f->writesize;

View File

@ -34,10 +34,10 @@ struct target_ram {
typedef struct target_flash target_flash_s;
typedef int (*flash_prepare_func)(target_flash_s *f);
typedef bool (*flash_prepare_func)(target_flash_s *f);
typedef int (*flash_erase_func)(target_flash_s *f, target_addr_t addr, size_t len);
typedef int (*flash_write_func)(target_flash_s *f, target_addr_t dest, const void *src, size_t len);
typedef int (*flash_done_func)(target_flash_s *f);
typedef bool (*flash_done_func)(target_flash_s *f);
struct target_flash {
target *t; /* Target this flash is attached to */