Commit 938e01d0 authored by ntfreak's avatar ntfreak
Browse files

- moved flash erase_check target code to target.c

git-svn-id: svn://svn.berlios.de/openocd/trunk@694 b42882b7-edfa-0310-969c-e2dbd0fdcd60
parent 506ca886
......@@ -1090,11 +1090,11 @@ int flash_write(target_t *target, image_t *image, u32 *written, int erase)
return retval;
}
int default_flash_blank_check(struct flash_bank_s *bank)
int default_flash_mem_blank_check(struct flash_bank_s *bank)
{
target_t *target = bank->target;
u8 buffer[1024];
int buffer_size=sizeof(buffer);
int buffer_size = sizeof(buffer);
int i;
int nBytes;
......@@ -1103,227 +1103,73 @@ int default_flash_blank_check(struct flash_bank_s *bank)
return ERROR_TARGET_NOT_HALTED;
}
int retval;
int fast_check=0;
working_area_t *erase_check_algorithm;
#if 0
/* FIX! doesn't work yet... */
/*
char test(char *a, int len, char t)
for (i = 0; i < bank->num_sectors; i++)
{
int i=0;
for (i=0; i<len; i++)
int j;
bank->sectors[i].is_erased = 1;
for (j = 0; j < bank->sectors[i].size; j += buffer_size)
{
t&=a[i];
int chunk;
int retval;
chunk = buffer_size;
if (chunk > (j - bank->sectors[i].size))
{
chunk = (j - bank->sectors[i].size);
}
retval = target->type->read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
if (retval != ERROR_OK)
return retval;
for (nBytes = 0; nBytes < chunk; nBytes++)
{
if (buffer[nBytes] != 0xFF)
{
bank->sectors[i].is_erased = 0;
break;
}
}
}
}
$ arm-elf-gcc -c -mthumb -O3 test.c
$ arm-elf-objdump --disassemble test.o
test.o: file format elf32-littlearm
Disassembly of section .text:
00000000 <test>:
0: b510 push {r4, lr}
2: 0612 lsl r2, r2, #24
4: 1c04 mov r4, r0 (add r4, r0, #0)
6: 0e10 lsr r0, r2, #24
8: 2200 mov r2, #0
a: 2900 cmp r1, #0
c: dd04 ble 18 <test+0x18>
e: 5ca3 ldrb r3, [r4, r2]
10: 3201 add r2, #1
12: 4018 and r0, r3
14: 428a cmp r2, r1
16: dbfa blt e <test+0xe>
18: bd10 pop {r4, pc}
1a: 46c0 nop (mov r8, r8)
*/
u16 erase_check_code[] =
{
0x0612,// lsl r2, r2, #24
0x1c04,// mov r4, r0 (add r4, r0, #0)
0x0e10,// lsr r0, r2, #24
0x2200,// mov r2, #0
0x2900,// cmp r1, #0
0xdd04,// ble 18 <test+0x18>
0x5ca3,// ldrb r3, [r4, r2]
0x3201,// add r2, #1
0x4018,// and r0, r3
0x428a,// cmp r2, r1
0xdbfa,// blt e <test+0xe>
0x46c0,// nop (mov r8, r8)
};
return ERROR_OK;
}
int default_flash_blank_check(struct flash_bank_s *bank)
{
target_t *target = bank->target;
int i;
int retval;
int fast_check = 0;
int blank;
/* make sure we have a working area */
if (target_alloc_working_area(target, ((sizeof(erase_check_code)+3)/4)*4, &erase_check_algorithm) != ERROR_OK)
if (bank->target->state != TARGET_HALTED)
{
erase_check_algorithm = NULL;
return ERROR_TARGET_NOT_HALTED;
}
if (erase_check_algorithm)
{
u8 erase_check_code_buf[((sizeof(erase_check_code)+3)/4)*4];
LOG_DEBUG("Running fast flash erase check");
for (i = 0; i < sizeof(erase_check_code)/sizeof(*erase_check_code); i++)
target_buffer_set_u16(target, erase_check_code_buf + (i*2), erase_check_code[i]);
/* write algorithm code to working area */
if ((retval=target->type->write_memory(target, erase_check_algorithm->address, 2, sizeof(erase_check_code)/sizeof(*erase_check_code), erase_check_code_buf))==ERROR_OK)
{
for (i = 0; i < bank->num_sectors; i++)
{
u32 address = bank->base + bank->sectors[i].offset;
u32 size = bank->sectors[i].size;
reg_param_t reg_params[3];
armv7m_algorithm_t arm_info;
arm_info.common_magic = ARMV7M_COMMON_MAGIC;
arm_info.core_mode = ARMV7M_MODE_ANY;
arm_info.core_state = ARMV7M_STATE_THUMB;
init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
buf_set_u32(reg_params[0].value, 0, 32, address);
init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
buf_set_u32(reg_params[1].value, 0, 32, size);
init_reg_param(&reg_params[2], "r2", 32, PARAM_IN_OUT);
buf_set_u32(reg_params[2].value, 0, 32, 0xff);
if ((retval = target->type->run_algorithm(target, 0, NULL, 3, reg_params, erase_check_algorithm->address,
erase_check_algorithm->address + sizeof(erase_check_code) - 2, 10000, &arm_info)) != ERROR_OK)
break;
if (buf_get_u32(reg_params[2].value, 0, 32) == 0xff)
bank->sectors[i].is_erased = 1;
else
bank->sectors[i].is_erased = 0;
destroy_reg_param(&reg_params[0]);
destroy_reg_param(&reg_params[1]);
destroy_reg_param(&reg_params[2]);
}
if (i == bank->num_sectors)
{
fast_check = 1;
}
}
target_free_working_area(target, erase_check_algorithm);
}
#endif
if (!fast_check)
for (i = 0; i < bank->num_sectors; i++)
{
/* try ARM7 instead */
u32 erase_check_code[] =
{
0xe4d03001, /* ldrb r3, [r0], #1 */
0xe0022003, /* and r2, r2, r3 */
0xe2511001, /* subs r1, r1, #1 */
0x1afffffb, /* b -4 */
0xeafffffe /* b 0 */
};
/* make sure we have a working area */
if (target_alloc_working_area(target, 20, &erase_check_algorithm) == ERROR_OK)
u32 address = bank->base + bank->sectors[i].offset;
u32 size = bank->sectors[i].size;
if ((retval = target_blank_check_memory(target, address, size, &blank)) != ERROR_OK)
{
u8 erase_check_code_buf[5 * 4];
for (i = 0; i < 5; i++)
target_buffer_set_u32(target, erase_check_code_buf + (i*4), erase_check_code[i]);
/* write algorithm code to working area */
if ((retval=target->type->write_memory(target, erase_check_algorithm->address, 4, 5, erase_check_code_buf))==ERROR_OK)
{
for (i = 0; i < bank->num_sectors; i++)
{
u32 address = bank->base + bank->sectors[i].offset;
u32 size = bank->sectors[i].size;
reg_param_t reg_params[3];
armv4_5_algorithm_t armv4_5_info;
armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
armv4_5_info.core_state = ARMV4_5_STATE_ARM;
init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
buf_set_u32(reg_params[0].value, 0, 32, address);
init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
buf_set_u32(reg_params[1].value, 0, 32, size);
init_reg_param(&reg_params[2], "r2", 32, PARAM_IN_OUT);
buf_set_u32(reg_params[2].value, 0, 32, 0xff);
if ((retval = target->type->run_algorithm(target, 0, NULL, 3, reg_params,
erase_check_algorithm->address, erase_check_algorithm->address + 0x10, 10000, &armv4_5_info)) != ERROR_OK)
break;
if (buf_get_u32(reg_params[2].value, 0, 32) == 0xff)
bank->sectors[i].is_erased = 1;
else
bank->sectors[i].is_erased = 0;
destroy_reg_param(&reg_params[0]);
destroy_reg_param(&reg_params[1]);
destroy_reg_param(&reg_params[2]);
}
if (i == bank->num_sectors)
{
fast_check = 1;
}
}
target_free_working_area(target, erase_check_algorithm);
fast_check = 0;
break;
}
if (blank == 0xFF)
bank->sectors[i].is_erased = 1;
else
bank->sectors[i].is_erased = 0;
fast_check = 1;
}
if (!fast_check)
{
LOG_USER("Running slow fallback erase check - add working memory");
for (i = 0; i < bank->num_sectors; i++)
{
int j;
bank->sectors[i].is_erased = 1;
for (j=0; j<bank->sectors[i].size; j+=buffer_size)
{
int chunk;
int retval;
chunk=buffer_size;
if (chunk>(j-bank->sectors[i].size))
{
chunk=(j-bank->sectors[i].size);
}
retval=target->type->read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
if (retval!=ERROR_OK)
return retval;
for (nBytes = 0; nBytes < chunk; nBytes++)
{
if (buffer[nBytes] != 0xFF)
{
bank->sectors[i].is_erased = 0;
break;
}
}
}
}
return default_flash_mem_blank_check(bank);
}
return ERROR_OK;
......
......@@ -78,7 +78,8 @@ extern int flash_erase_address_range(target_t *target, u32 addr, u32 length);
extern int flash_write(target_t *target, image_t *image, u32 *written, int erase);
extern void flash_set_dirty(void);
extern int flash_get_bank_count();
int default_flash_blank_check(struct flash_bank_s *bank);
extern int default_flash_blank_check(struct flash_bank_s *bank);
extern int default_flash_mem_blank_check(struct flash_bank_s *bank);
extern flash_bank_t *get_flash_bank_by_num(int num);
extern flash_bank_t *get_flash_bank_by_num_noprobe(int num);
......
......@@ -70,7 +70,7 @@ flash_driver_t stellaris_flash =
.write = stellaris_write,
.probe = stellaris_probe,
.auto_probe = stellaris_auto_probe,
.erase_check = default_flash_blank_check,
.erase_check = default_flash_mem_blank_check,
.protect_check = stellaris_protect_check,
.info = stellaris_info
};
......
......@@ -61,7 +61,7 @@ flash_driver_t stm32x_flash =
.write = stm32x_write,
.probe = stm32x_probe,
.auto_probe = stm32x_auto_probe,
.erase_check = default_flash_blank_check,
.erase_check = default_flash_mem_blank_check,
.protect_check = stm32x_protect_check,
.info = stm32x_info
};
......
......@@ -70,6 +70,7 @@ target_type_t arm720t_target =
.write_memory = arm720t_write_memory,
.bulk_write_memory = arm7_9_bulk_write_memory,
.checksum_memory = arm7_9_checksum_memory,
.blank_check_memory = arm7_9_blank_check_memory,
.run_algorithm = armv4_5_run_algorithm,
......
......@@ -2305,6 +2305,69 @@ int arm7_9_checksum_memory(struct target_s *target, u32 address, u32 count, u32*
return ERROR_OK;
}
int arm7_9_blank_check_memory(struct target_s *target, u32 address, u32 count, u32* blank)
{
working_area_t *erase_check_algorithm;
reg_param_t reg_params[3];
armv4_5_algorithm_t armv4_5_info;
int retval;
int i;
u32 erase_check_code[] =
{
/* loop: */
0xe4d03001, /* ldrb r3, [r0], #1 */
0xe0022003, /* and r2, r2, r3 */
0xe2511001, /* subs r1, r1, #1 */
0x1afffffb, /* bne loop */
/* end: */
0xeafffffe /* b end */
};
/* make sure we have a working area */
if (target_alloc_working_area(target, sizeof(erase_check_code), &erase_check_algorithm) != ERROR_OK)
{
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}
/* convert flash writing code into a buffer in target endianness */
for (i = 0; i < (sizeof(erase_check_code)/sizeof(u32)); i++)
target_write_u32(target, erase_check_algorithm->address + i*sizeof(u32), erase_check_code[i]);
armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
armv4_5_info.core_state = ARMV4_5_STATE_ARM;
init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
buf_set_u32(reg_params[0].value, 0, 32, address);
init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
buf_set_u32(reg_params[1].value, 0, 32, count);
init_reg_param(&reg_params[2], "r2", 32, PARAM_IN_OUT);
buf_set_u32(reg_params[2].value, 0, 32, 0xff);
if ((retval = target->type->run_algorithm(target, 0, NULL, 3, reg_params,
erase_check_algorithm->address, erase_check_algorithm->address + (sizeof(erase_check_code) - 4), 10000, &armv4_5_info)) != ERROR_OK)
{
destroy_reg_param(&reg_params[0]);
destroy_reg_param(&reg_params[1]);
destroy_reg_param(&reg_params[2]);
target_free_working_area(target, erase_check_algorithm);
return 0;
}
*blank = buf_get_u32(reg_params[2].value, 0, 32);
destroy_reg_param(&reg_params[0]);
destroy_reg_param(&reg_params[1]);
destroy_reg_param(&reg_params[2]);
target_free_working_area(target, erase_check_algorithm);
return ERROR_OK;
}
int arm7_9_register_commands(struct command_context_s *cmd_ctx)
{
command_t *arm7_9_cmd;
......
......@@ -123,6 +123,7 @@ int arm7_9_read_memory(struct target_s *target, u32 address, u32 size, u32 count
int arm7_9_write_memory(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer);
int arm7_9_bulk_write_memory(target_t *target, u32 address, u32 count, u8 *buffer);
int arm7_9_checksum_memory(struct target_s *target, u32 address, u32 count, u32* checksum);
int arm7_9_blank_check_memory(struct target_s *target, u32 address, u32 count, u32* blank);
int arm7_9_run_algorithm(struct target_s *target, int num_mem_params, mem_param_t *mem_params, int num_reg_prams, reg_param_t *reg_param, u32 entry_point, void *arch_info);
......
......@@ -75,6 +75,7 @@ target_type_t arm7tdmi_target =
.write_memory = arm7_9_write_memory,
.bulk_write_memory = arm7_9_bulk_write_memory,
.checksum_memory = arm7_9_checksum_memory,
.blank_check_memory = arm7_9_blank_check_memory,
.run_algorithm = armv4_5_run_algorithm,
......
......@@ -79,6 +79,7 @@ target_type_t arm920t_target =
.write_memory = arm920t_write_memory,
.bulk_write_memory = arm7_9_bulk_write_memory,
.checksum_memory = arm7_9_checksum_memory,
.blank_check_memory = arm7_9_blank_check_memory,
.run_algorithm = armv4_5_run_algorithm,
......
......@@ -79,6 +79,7 @@ target_type_t arm926ejs_target =
.write_memory = arm926ejs_write_memory,
.bulk_write_memory = arm7_9_bulk_write_memory,
.checksum_memory = arm7_9_checksum_memory,
.blank_check_memory = arm7_9_blank_check_memory,
.run_algorithm = armv4_5_run_algorithm,
......
......@@ -70,6 +70,7 @@ target_type_t arm966e_target =
.write_memory = arm7_9_write_memory,
.bulk_write_memory = arm7_9_bulk_write_memory,
.checksum_memory = arm7_9_checksum_memory,
.blank_check_memory = arm7_9_blank_check_memory,
.run_algorithm = armv4_5_run_algorithm,
......
......@@ -73,6 +73,7 @@ target_type_t arm9tdmi_target =
.write_memory = arm7_9_write_memory,
.bulk_write_memory = arm7_9_bulk_write_memory,
.checksum_memory = arm7_9_checksum_memory,
.blank_check_memory = arm7_9_blank_check_memory,
.run_algorithm = armv4_5_run_algorithm,
......
......@@ -596,4 +596,64 @@ int armv7m_checksum_memory(struct target_s *target, u32 address, u32 count, u32*
return ERROR_OK;
}
int armv7m_blank_check_memory(struct target_s *target, u32 address, u32 count, u32* blank)
{
working_area_t *erase_check_algorithm;
reg_param_t reg_params[3];
armv7m_algorithm_t armv7m_info;
int retval;
int i;
u16 erase_check_code[] =
{
/* loop: */
0xF810, 0x3B01, /* ldrb r3, [r0], #1 */
0xEA02, 0x0203, /* and r2, r2, r3 */
0x3901, /* subs r1, r1, #1 */
0xD1F9, /* bne loop */
/* end: */
0xE7FE, /* b end */
};
/* make sure we have a working area */
if (target_alloc_working_area(target, sizeof(erase_check_code), &erase_check_algorithm) != ERROR_OK)
{
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}
/* convert flash writing code into a buffer in target endianness */
for (i = 0; i < (sizeof(erase_check_code)/sizeof(u16)); i++)
target_write_u16(target, erase_check_algorithm->address + i*sizeof(u16), erase_check_code[i]);
armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
armv7m_info.core_mode = ARMV7M_MODE_ANY;
init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
buf_set_u32(reg_params[0].value, 0, 32, address);
init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
buf_set_u32(reg_params[1].value, 0, 32, count);
init_reg_param(&reg_params[2], "r2", 32, PARAM_IN_OUT);
buf_set_u32(reg_params[2].value, 0, 32, 0xff);
if ((retval = target->type->run_algorithm(target, 0, NULL, 3, reg_params,
erase_check_algorithm->address, erase_check_algorithm->address + (sizeof(erase_check_code)-2), 10000, &armv7m_info)) != ERROR_OK)
{
destroy_reg_param(&reg_params[0]);
destroy_reg_param(&reg_params[1]);
destroy_reg_param(&reg_params[2]);
target_free_working_area(target, erase_check_algorithm);
return 0;
}
*blank = buf_get_u32(reg_params[2].value, 0, 32);
destroy_reg_param(&reg_params[0]);
destroy_reg_param(&reg_params[1]);
destroy_reg_param(&reg_params[2]);
target_free_working_area(target, erase_check_algorithm);
return ERROR_OK;
}
......@@ -129,6 +129,7 @@ extern int armv7m_invalidate_core_regs(target_t *target);
extern int armv7m_restore_context(target_t *target);
extern int armv7m_checksum_memory(struct target_s *target, u32 address, u32 count, u32* checksum);
extern int armv7m_blank_check_memory(struct target_s *target, u32 address, u32 count, u32* blank);
/* Thumb mode instructions
*/
......
......@@ -81,6 +81,7 @@ target_type_t cortexm3_target =
.write_memory = cortex_m3_write_memory,
.bulk_write_memory = cortex_m3_bulk_write_memory,
.checksum_memory = armv7m_checksum_memory,
.blank_check_memory = armv7m_blank_check_memory,
.run_algorithm = armv7m_run_algorithm,
......
......@@ -83,6 +83,7 @@ target_type_t feroceon_target =
.write_memory = arm926ejs_write_memory,
.bulk_write_memory = feroceon_bulk_write_memory,
.checksum_memory = arm7_9_checksum_memory,
.blank_check_memory = arm7_9_blank_check_memory,
.run_algorithm = armv4_5_run_algorithm,
......
......@@ -1136,6 +1136,23 @@ int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32*
return retval;
}
int target_blank_check_memory(struct target_s *target, u32 address, u32 size, u32* blank)
{
int retval;
if (!target->type->examined)
{
LOG_ERROR("Target not examined yet");
return ERROR_FAIL;
}
if (target->type->blank_check_memory == 0)
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
retval = target->type->blank_check_memory(target, address, size, blank);
return retval;
}
int target_read_u32(struct target_s *target, u32 address, u32 *value)
{
u8 value_buf[4];
......
......@@ -159,6 +159,7 @@ typedef struct target_type_s
int (*bulk_write_memory)(struct target_s *target, u32 address, u32 count, u8 *buffer);
int (*checksum_memory)(struct target_s *target, u32 address, u32 count, u32* checksum);
int (*blank_check_memory)(struct target_s *target, u32 address, u32 count, u32* blank);
/* target break-/watchpoint control
* rw: 0 = write, 1 = read, 2 = access
......@@ -283,6 +284,7 @@ extern target_t* get_target_by_num(int num);
extern int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer);
extern int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer);
extern int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32* crc);
extern int target_blank_check_memory(struct target_s *target, u32 address, u32 size, u32* blank);
/* DANGER!!!!!
*
......
......@@ -111,7 +111,8 @@ target_type_t xscale_target =
.write_memory = xscale_write_memory,
.bulk_write_memory = xscale_bulk_write_memory,
.checksum_memory = xscale_checksum_memory,
.blank_check_memory = NULL,
.run_algorithm = armv4_5_run_algorithm,
.add_breakpoint = xscale_add_breakpoint,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment