|
|
@ -37,6 +37,7 @@ |
|
|
|
#include "general.h" |
|
|
|
#include "target.h" |
|
|
|
#include "target_internal.h" |
|
|
|
#include "adiv5.h" |
|
|
|
|
|
|
|
#define SIM_SDID 0x40048024 |
|
|
|
#define SIM_FCFG1 0x4004804C |
|
|
@ -73,18 +74,16 @@ |
|
|
|
/* 8 byte phrases need to be written to the k64 flash */ |
|
|
|
#define K64_WRITE_LEN 8 |
|
|
|
|
|
|
|
static bool kinetis_cmd_unsafe(target *t, int argc, char *argv[]); |
|
|
|
static bool kinetis_cmd_unsafe(target *t, int argc, char **argv); |
|
|
|
|
|
|
|
const struct command_s kinetis_cmd_list[] = { |
|
|
|
{"unsafe", (cmd_handler)kinetis_cmd_unsafe, "Allow programming security byte (enable|disable)"}, |
|
|
|
{NULL, NULL, NULL} |
|
|
|
}; |
|
|
|
{NULL, NULL, NULL}}; |
|
|
|
|
|
|
|
static bool kinetis_cmd_unsafe(target *t, int argc, char *argv[]) |
|
|
|
static bool kinetis_cmd_unsafe(target *t, int argc, char **argv) |
|
|
|
{ |
|
|
|
if (argc == 1) { |
|
|
|
tc_printf(t, "Allow programming security byte: %s\n", |
|
|
|
t->unsafe_enabled ? "enabled" : "disabled"); |
|
|
|
tc_printf(t, "Allow programming security byte: %s\n", t->unsafe_enabled ? "enabled" : "disabled"); |
|
|
|
} else { |
|
|
|
parse_enable_or_disable(argv[1], &t->unsafe_enabled); |
|
|
|
} |
|
|
@ -92,8 +91,7 @@ static bool kinetis_cmd_unsafe(target *t, int argc, char *argv[]) |
|
|
|
} |
|
|
|
|
|
|
|
static int kl_gen_flash_erase(struct target_flash *f, target_addr addr, size_t len); |
|
|
|
static int kl_gen_flash_write(struct target_flash *f, |
|
|
|
target_addr dest, const void *src, size_t len); |
|
|
|
static int kl_gen_flash_write(struct target_flash *f, target_addr dest, const void *src, size_t len); |
|
|
|
static int kl_gen_flash_done(struct target_flash *f); |
|
|
|
|
|
|
|
struct kinetis_flash { |
|
|
@ -101,8 +99,8 @@ struct kinetis_flash { |
|
|
|
uint8_t write_len; |
|
|
|
}; |
|
|
|
|
|
|
|
static void kl_gen_add_flash(target *t, uint32_t addr, size_t length, |
|
|
|
size_t erasesize, size_t write_len) |
|
|
|
static void kl_gen_add_flash( |
|
|
|
target *const t, const uint32_t addr, const size_t length, const size_t erasesize, const size_t write_len) |
|
|
|
{ |
|
|
|
struct kinetis_flash *kf = calloc(1, sizeof(*kf)); |
|
|
|
struct target_flash *f; |
|
|
@ -135,7 +133,7 @@ static void kl_s32k14_setup( |
|
|
|
kl_gen_add_flash(t, 0x10000000, flexmem_size, 0x1000, K64_WRITE_LEN); /* FlexNVM, 4 KB Sectors */ |
|
|
|
} |
|
|
|
|
|
|
|
bool kinetis_probe(target *t) |
|
|
|
bool kinetis_probe(target *const t) |
|
|
|
{ |
|
|
|
uint32_t sdid = target_mem_read32(t, SIM_SDID); |
|
|
|
uint32_t fcfg1 = target_mem_read32(t, SIM_FCFG1); |
|
|
@ -375,8 +373,7 @@ bool kinetis_probe(target *t) |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
static bool |
|
|
|
kl_gen_command(target *t, uint8_t cmd, uint32_t addr, const uint32_t *data, int n_items) |
|
|
|
static bool kl_gen_command(target *t, uint8_t cmd, uint32_t addr, const uint32_t *data, int n_items) |
|
|
|
{ |
|
|
|
uint8_t fstat; |
|
|
|
|
|
|
@ -412,7 +409,7 @@ kl_gen_command(target *t, uint8_t cmd, uint32_t addr, const uint32_t *data, int |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
static int kl_gen_flash_erase(struct target_flash *f, target_addr addr, size_t len) |
|
|
|
static int kl_gen_flash_erase(struct target_flash *const f, target_addr addr, size_t len) |
|
|
|
{ |
|
|
|
while (len) { |
|
|
|
if (kl_gen_command(f->t, FTFA_CMD_ERASE_SECTOR, addr, NULL, 0)) { |
|
|
@ -432,17 +429,13 @@ static int kl_gen_flash_erase(struct target_flash *f, target_addr addr, size_t l |
|
|
|
#define FLASH_SECURITY_BYTE_ADDRESS 0x40C |
|
|
|
#define FLASH_SECURITY_BYTE_UNSECURED 0xFE |
|
|
|
|
|
|
|
static int kl_gen_flash_write(struct target_flash *f, |
|
|
|
target_addr dest, const void *src, size_t len) |
|
|
|
static int kl_gen_flash_write(struct target_flash *f, target_addr dest, const void *src, size_t len) |
|
|
|
{ |
|
|
|
struct kinetis_flash *kf = (struct kinetis_flash *)f; |
|
|
|
struct kinetis_flash *const kf = (struct kinetis_flash *)f; |
|
|
|
|
|
|
|
/* Ensure we don't write something horrible over the security byte */ |
|
|
|
if (!f->t->unsafe_enabled && |
|
|
|
dest <= FLASH_SECURITY_BYTE_ADDRESS && |
|
|
|
dest + len > FLASH_SECURITY_BYTE_ADDRESS) { |
|
|
|
((uint8_t*)src)[FLASH_SECURITY_BYTE_ADDRESS - dest] = |
|
|
|
FLASH_SECURITY_BYTE_UNSECURED; |
|
|
|
if (!f->t->unsafe_enabled && dest <= FLASH_SECURITY_BYTE_ADDRESS && dest + len > FLASH_SECURITY_BYTE_ADDRESS) { |
|
|
|
((uint8_t *)src)[FLASH_SECURITY_BYTE_ADDRESS - dest] = FLASH_SECURITY_BYTE_UNSECURED; |
|
|
|
} |
|
|
|
|
|
|
|
/* Determine write command based on the alignment. */ |
|
|
@ -466,9 +459,9 @@ static int kl_gen_flash_write(struct target_flash *f, |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
static int kl_gen_flash_done(struct target_flash *f) |
|
|
|
static int kl_gen_flash_done(struct target_flash *const f) |
|
|
|
{ |
|
|
|
struct kinetis_flash *kf = (struct kinetis_flash *)f; |
|
|
|
struct kinetis_flash *const kf = (struct kinetis_flash *)f; |
|
|
|
|
|
|
|
if (f->t->unsafe_enabled) |
|
|
|
return 0; |
|
|
@ -484,14 +477,12 @@ static int kl_gen_flash_done(struct target_flash *f) |
|
|
|
vals[0] = target_mem_read32(f->t, FLASH_SECURITY_BYTE_ADDRESS - 4); |
|
|
|
vals[1] = target_mem_read32(f->t, FLASH_SECURITY_BYTE_ADDRESS); |
|
|
|
vals[1] = (vals[1] & 0xffffff00) | FLASH_SECURITY_BYTE_UNSECURED; |
|
|
|
kl_gen_command(f->t, FTFE_CMD_PROGRAM_PHRASE, |
|
|
|
FLASH_SECURITY_BYTE_ADDRESS - 4, vals, 2); |
|
|
|
kl_gen_command(f->t, FTFE_CMD_PROGRAM_PHRASE, FLASH_SECURITY_BYTE_ADDRESS - 4, vals, 2); |
|
|
|
} else { |
|
|
|
uint32_t vals[1]; |
|
|
|
vals[0] = target_mem_read32(f->t, FLASH_SECURITY_BYTE_ADDRESS); |
|
|
|
vals[0] = (vals[0] & 0xffffff00) | FLASH_SECURITY_BYTE_UNSECURED; |
|
|
|
kl_gen_command(f->t, FTFA_CMD_PROGRAM_LONGWORD, |
|
|
|
FLASH_SECURITY_BYTE_ADDRESS, vals, 1); |
|
|
|
kl_gen_command(f->t, FTFA_CMD_PROGRAM_LONGWORD, FLASH_SECURITY_BYTE_ADDRESS, vals, 1); |
|
|
|
} |
|
|
|
|
|
|
|
return 0; |
|
|
@ -504,7 +495,6 @@ static int kl_gen_flash_done(struct target_flash *f) |
|
|
|
* a backdoor AP is provided which may allow a mass erase to recover the |
|
|
|
* device. This provides a fake target to allow a monitor command interface |
|
|
|
*/ |
|
|
|
#include "adiv5.h" |
|
|
|
|
|
|
|
#define KINETIS_MDM_IDR_K22F 0x1c0000 |
|
|
|
#define KINETIS_MDM_IDR_KZ03 0x1c0020 |
|
|
@ -514,13 +504,12 @@ static bool kinetis_mdm_cmd_ke04_mode(target *t, int argc, const char **argv); |
|
|
|
|
|
|
|
const struct command_s kinetis_mdm_cmd_list[] = { |
|
|
|
{"erase_mass", (cmd_handler)kinetis_mdm_cmd_erase_mass, "Erase entire flash memory"}, |
|
|
|
{"ke04_mode", (cmd_handler)kinetis_mdm_cmd_ke04_mode, "Allow erase for KE04"}, |
|
|
|
{NULL, NULL, NULL} |
|
|
|
}; |
|
|
|
{"ke04_mode", (cmd_handler)kinetis_mdm_cmd_ke04_mode, "Allow erase for KE04"}, {NULL, NULL, NULL}}; |
|
|
|
|
|
|
|
enum target_halt_reason mdm_halt_poll(target *t, target_addr *watch) |
|
|
|
enum target_halt_reason mdm_halt_poll(target *t, const target_addr *const watch) |
|
|
|
{ |
|
|
|
(void)t; (void)watch; |
|
|
|
(void)t; |
|
|
|
(void)watch; |
|
|
|
return TARGET_HALT_REQUEST; |
|
|
|
} |
|
|
|
|
|
|
@ -569,6 +558,7 @@ static bool kinetis_mdm_cmd_ke04_mode(target *t, int argc, const char **argv) |
|
|
|
tc_printf(t, "Mass erase for KE04 now allowed\n"); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
static bool kinetis_mdm_cmd_erase_mass(target *t, int argc, const char **argv) |
|
|
|
{ |
|
|
|
(void)argc; |
|
|
@ -579,9 +569,8 @@ static bool kinetis_mdm_cmd_erase_mass(target *t, int argc, const char **argv) |
|
|
|
if (t->ke04_mode) |
|
|
|
adiv5_ap_write(ap, MDM_CONTROL, MDM_CONTROL_SYS_RESET); |
|
|
|
|
|
|
|
uint32_t status, control; |
|
|
|
status = adiv5_ap_read(ap, MDM_STATUS); |
|
|
|
control = adiv5_ap_read(ap, MDM_CONTROL); |
|
|
|
uint32_t status = adiv5_ap_read(ap, MDM_STATUS); |
|
|
|
uint32_t control = adiv5_ap_read(ap, MDM_CONTROL); |
|
|
|
tc_printf(t, "Requesting mass erase (status = 0x%" PRIx32 ")\n", status); |
|
|
|
|
|
|
|
/* This flag does not exist on KE04 */ |
|
|
|