Skip to content

Commit f55ec6d

Browse files
committed
target: rewrite command 'write_memory' as COMMAND_HANDLER
While there: - drop the command name from the error messages; - check the returned value from Jim_GetWide() to detect incorrect numeric values. Change-Id: I399402ac11b6d459f1771e59e44210aef3e2a637 Signed-off-by: Antonio Borneo <[email protected]> Reviewed-on: https://review.openocd.org/c/openocd/+/8582 Tested-by: jenkins Reviewed-by: Evgeniy Naydanov <[email protected]>
1 parent 864e134 commit f55ec6d

File tree

1 file changed

+38
-52
lines changed

1 file changed

+38
-52
lines changed

src/target/target.c

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4526,50 +4526,36 @@ COMMAND_HANDLER(handle_target_read_memory)
45264526
return ERROR_OK;
45274527
}
45284528

4529-
static int target_jim_write_memory(Jim_Interp *interp, int argc,
4530-
Jim_Obj * const *argv)
4529+
COMMAND_HANDLER(handle_target_write_memory)
45314530
{
45324531
/*
4533-
* argv[1] = memory address
4534-
* argv[2] = desired element width in bits
4535-
* argv[3] = list of data to write
4536-
* argv[4] = optional "phys"
4532+
* CMD_ARGV[0] = memory address
4533+
* CMD_ARGV[1] = desired element width in bits
4534+
* CMD_ARGV[2] = list of data to write
4535+
* CMD_ARGV[3] = optional "phys"
45374536
*/
45384537

4539-
if (argc < 4 || argc > 5) {
4540-
Jim_WrongNumArgs(interp, 1, argv, "address width data ['phys']");
4541-
return JIM_ERR;
4542-
}
4538+
if (CMD_ARGC < 3 || CMD_ARGC > 4)
4539+
return ERROR_COMMAND_SYNTAX_ERROR;
45434540

45444541
/* Arg 1: Memory address. */
4545-
int e;
4546-
jim_wide wide_addr;
4547-
e = Jim_GetWide(interp, argv[1], &wide_addr);
4548-
4549-
if (e != JIM_OK)
4550-
return e;
4551-
4552-
target_addr_t addr = (target_addr_t)wide_addr;
4542+
target_addr_t addr;
4543+
COMMAND_PARSE_NUMBER(u64, CMD_ARGV[0], addr);
45534544

45544545
/* Arg 2: Bit width of one element. */
4555-
long l;
4556-
e = Jim_GetLong(interp, argv[2], &l);
4557-
4558-
if (e != JIM_OK)
4559-
return e;
4546+
unsigned int width_bits;
4547+
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], width_bits);
45604548

4561-
const unsigned int width_bits = l;
4562-
size_t count = Jim_ListLength(interp, argv[3]);
4549+
/* Arg 3: Elements to write. */
4550+
size_t count = Jim_ListLength(CMD_CTX->interp, CMD_JIMTCL_ARGV[2]);
45634551

45644552
/* Arg 4: Optional 'phys'. */
45654553
bool is_phys = false;
45664554

4567-
if (argc > 4) {
4568-
const char *phys = Jim_GetString(argv[4], NULL);
4569-
4570-
if (strcmp(phys, "phys")) {
4571-
Jim_SetResultFormatted(interp, "invalid argument '%s', must be 'phys'", phys);
4572-
return JIM_ERR;
4555+
if (CMD_ARGC == 4) {
4556+
if (strcmp(CMD_ARGV[3], "phys")) {
4557+
command_print(CMD, "invalid argument '%s', must be 'phys'", CMD_ARGV[3]);
4558+
return ERROR_COMMAND_ARGUMENT_INVALID;
45734559
}
45744560

45754561
is_phys = true;
@@ -4582,36 +4568,32 @@ static int target_jim_write_memory(Jim_Interp *interp, int argc,
45824568
case 64:
45834569
break;
45844570
default:
4585-
Jim_SetResultString(interp, "invalid width, must be 8, 16, 32 or 64", -1);
4586-
return JIM_ERR;
4571+
command_print(CMD, "invalid width, must be 8, 16, 32 or 64");
4572+
return ERROR_COMMAND_ARGUMENT_INVALID;
45874573
}
45884574

45894575
if (count > 65536) {
4590-
Jim_SetResultString(interp,
4591-
"write_memory: too large memory write request, exceeds 64K elements", -1);
4592-
return JIM_ERR;
4576+
command_print(CMD, "too large memory write request, exceeds 64K elements");
4577+
return ERROR_COMMAND_ARGUMENT_INVALID;
45934578
}
45944579

45954580
const unsigned int width = width_bits / 8;
45964581
/* -1 is needed to handle cases when (addr + count * width) results in zero
45974582
* due to overflow.
45984583
*/
45994584
if ((addr + count * width - 1) < addr) {
4600-
Jim_SetResultFormatted(interp,
4601-
"write_memory: memory region wraps over address zero");
4602-
return JIM_ERR;
4585+
command_print(CMD, "memory region wraps over address zero");
4586+
return ERROR_COMMAND_ARGUMENT_INVALID;
46034587
}
46044588

4605-
struct command_context *cmd_ctx = current_command_context(interp);
4606-
assert(cmd_ctx);
4607-
struct target *target = get_current_target(cmd_ctx);
4589+
struct target *target = get_current_target(CMD_CTX);
46084590

46094591
const size_t buffersize = 4096;
46104592
uint8_t *buffer = malloc(buffersize);
46114593

46124594
if (!buffer) {
46134595
LOG_ERROR("Failed to allocate memory");
4614-
return JIM_ERR;
4596+
return ERROR_FAIL;
46154597
}
46164598

46174599
size_t j = 0;
@@ -4621,9 +4603,13 @@ static int target_jim_write_memory(Jim_Interp *interp, int argc,
46214603
const size_t chunk_len = MIN(count, max_chunk_len);
46224604

46234605
for (size_t i = 0; i < chunk_len; i++, j++) {
4624-
Jim_Obj *tmp = Jim_ListGetIndex(interp, argv[3], j);
4606+
Jim_Obj *tmp = Jim_ListGetIndex(CMD_CTX->interp, CMD_JIMTCL_ARGV[2], j);
46254607
jim_wide element_wide;
4626-
Jim_GetWide(interp, tmp, &element_wide);
4608+
int jimretval = Jim_GetWide(CMD_CTX->interp, tmp, &element_wide);
4609+
if (jimretval != JIM_OK) {
4610+
command_print(CMD, "invalid value \"%s\"", Jim_GetString(tmp, NULL));
4611+
return ERROR_COMMAND_ARGUMENT_INVALID;
4612+
}
46274613

46284614
const uint64_t v = element_wide;
46294615

@@ -4653,19 +4639,19 @@ static int target_jim_write_memory(Jim_Interp *interp, int argc,
46534639
retval = target_write_memory(target, addr, width, chunk_len, buffer);
46544640

46554641
if (retval != ERROR_OK) {
4656-
LOG_ERROR("write_memory: write at " TARGET_ADDR_FMT " with width=%u and count=%zu failed",
4642+
LOG_DEBUG("write at " TARGET_ADDR_FMT " with width=%u and count=%zu failed",
46574643
addr, width_bits, chunk_len);
4658-
Jim_SetResultString(interp, "write_memory: failed to write memory", -1);
4659-
e = JIM_ERR;
4660-
break;
4644+
command_print(CMD, "failed to write memory");
4645+
free(buffer);
4646+
return retval;
46614647
}
46624648

46634649
addr += chunk_len * width;
46644650
}
46654651

46664652
free(buffer);
46674653

4668-
return e;
4654+
return ERROR_OK;
46694655
}
46704656

46714657
/* FIX? should we propagate errors here rather than printing them
@@ -5612,7 +5598,7 @@ static const struct command_registration target_instance_command_handlers[] = {
56125598
{
56135599
.name = "write_memory",
56145600
.mode = COMMAND_EXEC,
5615-
.jim_handler = target_jim_write_memory,
5601+
.handler = handle_target_write_memory,
56165602
.help = "Write Tcl list of 8/16/32/64 bit numbers to target memory",
56175603
.usage = "address width data ['phys']",
56185604
},
@@ -6747,7 +6733,7 @@ static const struct command_registration target_exec_command_handlers[] = {
67476733
{
67486734
.name = "write_memory",
67496735
.mode = COMMAND_EXEC,
6750-
.jim_handler = target_jim_write_memory,
6736+
.handler = handle_target_write_memory,
67516737
.help = "Write Tcl list of 8/16/32/64 bit numbers to target memory",
67526738
.usage = "address width data ['phys']",
67536739
},

0 commit comments

Comments
 (0)