target.c 123 KB
Newer Older
4001
4002
			for (; (x < 16) ; x += 4) {
				Jim_fprintf(interp, interp->cookie_stdout, "         ");
4003
			}
4004
4005
4006
4007
4008
4009
			break;
		case 2:
			for (x = 0; x < 16 && x < y; x += 2)
			{
				z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
				Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4010
			}
4011
4012
			for (; (x < 16) ; x += 2) {
				Jim_fprintf(interp, interp->cookie_stdout, "     ");
4013
			}
4014
4015
4016
4017
4018
4019
			break;
		case 1:
		default:
			for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
				z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
				Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4020
			}
4021
4022
			for (; (x < 16) ; x += 1) {
				Jim_fprintf(interp, interp->cookie_stdout, "   ");
4023
			}
4024
			break;
4025
		}
4026
4027
4028
4029
4030
4031
4032
4033
4034
		/* ascii-ify the bytes */
		for (x = 0 ; x < y ; x++) {
			if ((target_buf[x] >= 0x20) &&
				(target_buf[x] <= 0x7e)) {
				/* good */
			} else {
				/* smack it */
				target_buf[x] = '.';
			}
4035
		}
4036
4037
4038
4039
		/* space pad  */
		while (x < 16) {
			target_buf[x] = ' ';
			x++;
4040
		}
4041
4042
4043
4044
4045
4046
4047
		/* terminate */
		target_buf[16] = 0;
		/* print - with a newline */
		Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
		/* NEXT... */
		c -= 16;
		a += 16;
4048
	}
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
	return JIM_OK;
}

static int jim_target_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	struct target *target = Jim_CmdPrivData(interp);
	return target_mem2array(interp, target, argc - 1, argv + 1);
}

static int jim_target_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	struct target *target = Jim_CmdPrivData(interp);
	return target_array2mem(interp, target, argc - 1, argv + 1);
}
zwelch's avatar
zwelch committed
4063

4064
4065
static int jim_target_tap_disabled(Jim_Interp *interp)
{
zwelch's avatar
zwelch committed
4066
4067
	Jim_SetResult_sprintf(interp, "[TAP is disabled]");
	return JIM_ERR;
4068
4069
}

4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	if (argc != 1)
	{
		Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
		return JIM_ERR;
	}
	struct target *target = Jim_CmdPrivData(interp);
	if (!target->tap->enabled)
		return jim_target_tap_disabled(interp);

	int e = target->type->examine(target);
	if (e != ERROR_OK)
	{
		Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
		return JIM_ERR;
	}
	return JIM_OK;
}

static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	if (argc != 1)
	{
		Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
		return JIM_ERR;
	}
	struct target *target = Jim_CmdPrivData(interp);
	if (!target->tap->enabled)
		return jim_target_tap_disabled(interp);

	int e;
	if (!(target_was_examined(target))) {
		e = ERROR_TARGET_NOT_EXAMINED;
	} else {
		e = target->type->poll(target);
	}
	if (e != ERROR_OK)
	{
		Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
		return JIM_ERR;
	}
	return JIM_OK;
}

static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	Jim_GetOptInfo goi;
	Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);

	if (goi.argc != 2)
	{
		Jim_WrongNumArgs(interp, 0, argv,
				"([tT]|[fF]|assert|deassert) BOOL");
		return JIM_ERR;
	}

	Jim_Nvp *n;
	int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
	if (e != JIM_OK)
	{
		Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
		return e;
	}
	/* the halt or not param */
	jim_wide a;
	e = Jim_GetOpt_Wide(&goi, &a);
	if (e != JIM_OK)
		return e;

	struct target *target = Jim_CmdPrivData(goi.interp);
	if (!target->tap->enabled)
		return jim_target_tap_disabled(interp);
	if (!target->type->assert_reset || !target->type->deassert_reset)
	{
		Jim_SetResult_sprintf(interp,
				"No target-specific reset for %s",
				target_name(target));
		return JIM_ERR;
	}
	/* determine if we should halt or not. */
	target->reset_halt = !!a;
	/* When this happens - all workareas are invalid. */
	target_free_all_working_areas_restore(target, 0);

	/* do the assert */
	if (n->value == NVP_ASSERT) {
		e = target->type->assert_reset(target);
	} else {
		e = target->type->deassert_reset(target);
	}
	return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
}

static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	if (argc != 1) {
		Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
		return JIM_ERR;
	}
	struct target *target = Jim_CmdPrivData(interp);
	if (!target->tap->enabled)
		return jim_target_tap_disabled(interp);
	int e = target->type->halt(target);
	return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
}

static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	Jim_GetOptInfo goi;
	Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);

	/* params:  <name>  statename timeoutmsecs */
	if (goi.argc != 2)
	{
		const char *cmd_name = Jim_GetString(argv[0], NULL);
		Jim_SetResult_sprintf(goi.interp,
				"%s <state_name> <timeout_in_msec>", cmd_name);
		return JIM_ERR;
	}

	Jim_Nvp *n;
	int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
	if (e != JIM_OK) {
		Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
		return e;
	}
	jim_wide a;
	e = Jim_GetOpt_Wide(&goi, &a);
	if (e != JIM_OK) {
		return e;
	}
	struct target *target = Jim_CmdPrivData(interp);
	if (!target->tap->enabled)
		return jim_target_tap_disabled(interp);

	e = target_wait_state(target, n->value, a);
	if (e != ERROR_OK)
	{
		Jim_SetResult_sprintf(goi.interp,
				"target: %s wait %s fails (%d) %s",
				target_name(target), n->name,
				e, target_strerror_safe(e));
		return JIM_ERR;
	}
	return JIM_OK;
}
/* List for human, Events defined for this target.
 * scripts/programs should use 'name cget -event NAME'
 */
static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
	struct target *target = Jim_CmdPrivData(interp);
	struct target_event_action *teap = target->event_action;
	command_print(cmd_ctx, "Event actions for target (%d) %s\n",
				   target->target_number,
				   target_name(target));
	command_print(cmd_ctx, "%-25s | Body", "Event");
	command_print(cmd_ctx, "------------------------- | "
			"----------------------------------------");
	while (teap)
	{
		Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
		command_print(cmd_ctx, "%-25s | %s",
				opt->name, Jim_GetString(teap->body, NULL));
		teap = teap->next;
	}
	command_print(cmd_ctx, "***END***");
	return JIM_OK;
}
static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	if (argc != 1)
	{
		Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
		return JIM_ERR;
	}
	struct target *target = Jim_CmdPrivData(interp);
	Jim_SetResultString(interp, target_state_name(target), -1);
	return JIM_OK;
}
static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	Jim_GetOptInfo goi;
	Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
	if (goi.argc != 1)
	{
		const char *cmd_name = Jim_GetString(argv[0], NULL);
		Jim_SetResult_sprintf(goi.interp, "%s <eventname>", cmd_name);
		return JIM_ERR;
	}
	Jim_Nvp *n;
	int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
	if (e != JIM_OK)
	{
		Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
		return e;
	}
	struct target *target = Jim_CmdPrivData(interp);
	target_handle_event(target, n->value);
	return JIM_OK;
}

static const struct command_registration target_instance_command_handlers[] = {
	{
		.name = "configure",
		.mode = COMMAND_CONFIG,
		.jim_handler = &jim_target_configure,
		.usage = "[<target_options> ...]",
		.help  = "configure a new target for use",
	},
	{
		.name = "cget",
		.mode = COMMAND_ANY,
		.jim_handler = &jim_target_configure,
		.usage = "<target_type> [<target_options> ...]",
		.help  = "configure a new target for use",
	},
	{
		.name = "mww",
		.mode = COMMAND_EXEC,
		.jim_handler = &jim_target_mw,
		.usage = "<address> <data> [<count>]",
		.help = "Write 32-bit word(s) to target memory",
	},
	{
		.name = "mwh",
		.mode = COMMAND_EXEC,
		.jim_handler = &jim_target_mw,
		.usage = "<address> <data> [<count>]",
		.help = "Write 16-bit half-word(s) to target memory",
	},
	{
		.name = "mwb",
		.mode = COMMAND_EXEC,
		.jim_handler = &jim_target_mw,
		.usage = "<address> <data> [<count>]",
		.help = "Write byte(s) to target memory",
	},
	{
		.name = "mdw",
		.mode = COMMAND_EXEC,
		.jim_handler = &jim_target_md,
		.usage = "<address> [<count>]",
		.help = "Display target memory as 32-bit words",
	},
	{
		.name = "mdh",
		.mode = COMMAND_EXEC,
		.jim_handler = &jim_target_md,
		.usage = "<address> [<count>]",
		.help = "Display target memory as 16-bit half-words",
	},
	{
		.name = "mdb",
		.mode = COMMAND_EXEC,
		.jim_handler = &jim_target_md,
		.usage = "<address> [<count>]",
		.help = "Display target memory as 8-bit bytes",
	},
	{
		.name = "array2mem",
		.mode = COMMAND_EXEC,
		.jim_handler = &jim_target_array2mem,
	},
	{
		.name = "mem2array",
		.mode = COMMAND_EXEC,
		.jim_handler = &jim_target_mem2array,
	},
	{
		.name = "eventlist",
		.mode = COMMAND_EXEC,
		.jim_handler = &jim_target_event_list,
	},
	{
		.name = "curstate",
		.mode = COMMAND_EXEC,
		.jim_handler = &jim_target_current_state,
	},
	{
		.name = "arp_examine",
		.mode = COMMAND_EXEC,
		.jim_handler = &jim_target_examine,
	},
	{
		.name = "arp_poll",
		.mode = COMMAND_EXEC,
		.jim_handler = &jim_target_poll,
	},
	{
		.name = "arp_reset",
		.mode = COMMAND_EXEC,
		.jim_handler = &jim_target_reset,
	},
	{
		.name = "arp_halt",
		.mode = COMMAND_EXEC,
		.jim_handler = &jim_target_halt,
	},
	{
		.name = "arp_waitstate",
		.mode = COMMAND_EXEC,
		.jim_handler = &jim_target_wait_state,
	},
	{
		.name = "invoke-event",
		.mode = COMMAND_EXEC,
		.jim_handler = &jim_target_invoke_event,
	},
	COMMAND_REGISTRATION_DONE
};

4384
static int target_create(Jim_GetOptInfo *goi)
4385
{
4386
4387
4388
4389
4390
4391
	Jim_Obj *new_cmd;
	Jim_Cmd *cmd;
	const char *cp;
	char *cp2;
	int e;
	int x;
Zachary T Welch's avatar
Zachary T Welch committed
4392
	struct target *target;
4393
	struct command_context *cmd_ctx;
4394
4395

	cmd_ctx = Jim_GetAssocData(goi->interp, "context");
zwelch's avatar
zwelch committed
4396
	if (goi->argc < 3) {
4397
		Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4398
4399
4400
4401
		return JIM_ERR;
	}

	/* COMMAND */
4402
	Jim_GetOpt_Obj(goi, &new_cmd);
4403
	/* does this command exist? */
4404
	cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
zwelch's avatar
zwelch committed
4405
	if (cmd) {
4406
		cp = Jim_GetString(new_cmd, NULL);
4407
4408
4409
		Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
		return JIM_ERR;
	}
4410

4411
	/* TYPE */
4412
	e = Jim_GetOpt_String(goi, &cp2, NULL);
4413
4414
	cp = cp2;
	/* now does target type exist */
zwelch's avatar
zwelch committed
4415
4416
	for (x = 0 ; target_types[x] ; x++) {
		if (0 == strcmp(cp, target_types[x]->name)) {
4417
4418
4419
4420
			/* found */
			break;
		}
	}
zwelch's avatar
zwelch committed
4421
	if (target_types[x] == NULL) {
4422
		Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
zwelch's avatar
zwelch committed
4423
4424
		for (x = 0 ; target_types[x] ; x++) {
			if (target_types[x + 1]) {
4425
				Jim_AppendStrings(goi->interp,
4426
4427
4428
4429
								   Jim_GetResult(goi->interp),
								   target_types[x]->name,
								   ", ", NULL);
			} else {
4430
				Jim_AppendStrings(goi->interp,
4431
4432
								   Jim_GetResult(goi->interp),
								   " or ",
4433
								   target_types[x]->name,NULL);
4434
4435
4436
4437
4438
4439
			}
		}
		return JIM_ERR;
	}

	/* Create it */
Zachary T Welch's avatar
Zachary T Welch committed
4440
	target = calloc(1,sizeof(struct target));
4441
4442
4443
4444
	/* set target number */
	target->target_number = new_target_number();

	/* allocate memory for each unique target type */
4445
	target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4446

4447
	memcpy(target->type, target_types[x], sizeof(struct target_type));
4448

4449
4450
4451
4452
4453
4454
4455
	/* will be set by "-endian" */
	target->endianness = TARGET_ENDIAN_UNKNOWN;

	target->working_area        = 0x0;
	target->working_area_size   = 0x0;
	target->working_areas       = NULL;
	target->backup_working_area = 0;
4456

4457
4458
4459
4460
4461
4462
4463
	target->state               = TARGET_UNKNOWN;
	target->debug_reason        = DBG_REASON_UNDEFINED;
	target->reg_cache           = NULL;
	target->breakpoints         = NULL;
	target->watchpoints         = NULL;
	target->next                = NULL;
	target->arch_info           = NULL;
4464

4465
4466
	target->display             = 1;

4467
4468
	target->halt_issued			= false;

4469
	/* initialize trace information */
Zachary T Welch's avatar
Zachary T Welch committed
4470
	target->trace_info = malloc(sizeof(struct trace));
4471
4472
4473
4474
4475
4476
4477
	target->trace_info->num_trace_points         = 0;
	target->trace_info->trace_points_size        = 0;
	target->trace_info->trace_points             = NULL;
	target->trace_info->trace_history_size       = 0;
	target->trace_info->trace_history            = NULL;
	target->trace_info->trace_history_pos        = 0;
	target->trace_info->trace_history_overflowed = 0;
4478

4479
4480
4481
4482
4483
4484
4485
	target->dbgmsg          = NULL;
	target->dbg_msg_enabled = 0;

	target->endianness = TARGET_ENDIAN_UNKNOWN;

	/* Do the rest as "configure" options */
	goi->isconfigure = 1;
4486
	e = target_configure(goi, target);
4487
4488
4489

	if (target->tap == NULL)
	{
4490
		Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
zwelch's avatar
zwelch committed
4491
		e = JIM_ERR;
4492
4493
	}

zwelch's avatar
zwelch committed
4494
	if (e != JIM_OK) {
4495
4496
		free(target->type);
		free(target);
4497
4498
4499
		return e;
	}

zwelch's avatar
zwelch committed
4500
	if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4501
4502
4503
4504
		/* default endian to little if not specified */
		target->endianness = TARGET_LITTLE_ENDIAN;
	}

4505
4506
4507
	/* incase variant is not set */
	if (!target->variant)
		target->variant = strdup("");
4508

4509
4510
4511
	cp = Jim_GetString(new_cmd, NULL);
	target->cmd_name = strdup(cp);

4512
	/* create the target specific commands */
4513
4514
4515
4516
	if (target->type->commands) {
		e = register_commands(cmd_ctx, NULL, target->type->commands);
		if (ERROR_OK != e)
			LOG_ERROR("unable to register '%s' commands", cp);
4517
	}
zwelch's avatar
zwelch committed
4518
	if (target->type->target_create) {
4519
		(*(target->type->target_create))(target, goi->interp);
4520
4521
4522
4523
	}

	/* append to end of list */
	{
Zachary T Welch's avatar
Zachary T Welch committed
4524
		struct target **tpp;
4525
		tpp = &(all_targets);
zwelch's avatar
zwelch committed
4526
		while (*tpp) {
4527
			tpp = &((*tpp)->next);
4528
4529
4530
		}
		*tpp = target;
	}
4531
	
4532
	/* now - create the new target name command */
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
	const const struct command_registration target_subcommands[] = {
		{
			.chain = target_instance_command_handlers,
		},
		{
			.chain = target->type->commands,
		},
		COMMAND_REGISTRATION_DONE
	};
	const const struct command_registration target_commands[] = {
		{
			.name = cp,
			.mode = COMMAND_ANY,
			.help = "target command group",
			.chain = target_subcommands,
		},
		COMMAND_REGISTRATION_DONE
4550
	};
4551
4552
4553
4554
4555
4556
4557
4558
4559
	e = register_commands(cmd_ctx, NULL, target_commands);
	if (ERROR_OK != e)
		return JIM_ERR;

	struct command *c = command_find_in_context(cmd_ctx, cp);
	assert(c);
	command_set_handler_data(c, target);

	return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
4560
4561
}

4562
static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4563
{
4564
4565
4566
4567
4568
4569
4570
4571
4572
	if (argc != 1)
	{
		Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
		return JIM_ERR;
	}
	struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
	Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
	return JIM_OK;
}
4573

4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	if (argc != 1)
	{
		Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
		return JIM_ERR;
	}
	Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
	for (unsigned x = 0; NULL != target_types[x]; x++)
	{
		Jim_ListAppendElement(interp, Jim_GetResult(interp),
			Jim_NewStringObj(interp, target_types[x]->name, -1));
	}
	return JIM_OK;
}
4589

4590
4591
4592
4593
4594
static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	if (argc != 1)
	{
		Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4595
4596
		return JIM_ERR;
	}
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
	Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
	struct target *target = all_targets;
	while (target)
	{
		Jim_ListAppendElement(interp, Jim_GetResult(interp),
			Jim_NewStringObj(interp, target_name(target), -1));
		target = target->next;
	}
	return JIM_OK;
}
4607

4608
4609
4610
4611
4612
4613
4614
4615
4616
static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	Jim_GetOptInfo goi;
	Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
	if (goi.argc < 3)
	{
		Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
			"<name> <target_type> [<target_options> ...]");
		return JIM_ERR;
4617
	}
4618
4619
	return target_create(&goi);
}
4620

4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	Jim_GetOptInfo goi;
	Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);

	/* It's OK to remove this mechanism sometime after August 2010 or so */
	LOG_WARNING("don't use numbers as target identifiers; use names");
	if (goi.argc != 1)
	{
		Jim_SetResult_sprintf(goi.interp, "usage: target number <number>");
4631
		return JIM_ERR;
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
	}
	jim_wide w;
	int e = Jim_GetOpt_Wide(&goi, &w);
	if (e != JIM_OK)
		return JIM_ERR;

	struct target *target;
	for (target = all_targets; NULL != target; target = target->next)
	{
		if (target->target_number != w)
4642
			continue;
4643
4644

		Jim_SetResultString(goi.interp, target_name(target), -1);
4645
4646
		return JIM_OK;
	}
4647
4648
	Jim_SetResult_sprintf(goi.interp,
			"Target: number %d does not exist", (int)(w));
4649
	return JIM_ERR;
4650
}
4651

4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	if (argc != 1)
	{
		Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
		return JIM_ERR;
	}
	unsigned count = 0;
	struct target *target = all_targets;
	while (NULL != target)
	{
		target = target->next;
		count++;
	}
	Jim_SetResult(interp, Jim_NewIntObj(interp, count));
	return JIM_OK;
}

static const struct command_registration target_subcommand_handlers[] = {
	{
		.name = "create",
		.mode = COMMAND_ANY,
		.jim_handler = &jim_target_create,
		.usage = "<name> <type> ...",
		.help = "Returns the currently selected target",
	},
	{
		.name = "current",
		.mode = COMMAND_ANY,
		.jim_handler = &jim_target_current,
		.help = "Returns the currently selected target",
	},
	{
		.name = "types",
		.mode = COMMAND_ANY,
		.jim_handler = &jim_target_types,
		.help = "Returns the available target types as a list of strings",
	},
	{
		.name = "names",
		.mode = COMMAND_ANY,
		.jim_handler = &jim_target_names,
		.help = "Returns the names of all targets as a list of strings",
	},
	{
		.name = "number",
		.mode = COMMAND_ANY,
		.jim_handler = &jim_target_number,
		.usage = "<number>",
		.help = "Returns the name of target <n>",
	},
	{
		.name = "count",
		.mode = COMMAND_ANY,
		.jim_handler = &jim_target_count,
		.help = "Returns the number of targets as an integer",
	},
	COMMAND_REGISTRATION_DONE
};

4712
4713
4714

struct FastLoad
{
4715
	uint32_t address;
4716
	uint8_t *data;
4717
4718
4719
4720
4721
4722
4723
	int length;

};

static int fastload_num;
static struct FastLoad *fastload;

4724
static void free_fastload(void)
4725
{
zwelch's avatar
zwelch committed
4726
	if (fastload != NULL)
4727
4728
	{
		int i;
zwelch's avatar
zwelch committed
4729
		for (i = 0; i < fastload_num; i++)
4730
4731
4732
4733
4734
		{
			if (fastload[i].data)
				free(fastload[i].data);
		}
		free(fastload);
zwelch's avatar
zwelch committed
4735
		fastload = NULL;
4736
4737
4738
4739
4740
4741
	}
}




4742
COMMAND_HANDLER(handle_fast_load_image_command)
4743
{
4744
	uint8_t *buffer;
Zachary T Welch's avatar
Zachary T Welch committed
4745
	size_t buf_cnt;
4746
	uint32_t image_size;
zwelch's avatar
zwelch committed
4747
4748
	uint32_t min_address = 0;
	uint32_t max_address = 0xffffffff;
4749
4750
	int i;

Zachary T Welch's avatar
Zachary T Welch committed
4751
	struct image image;
4752

4753
	int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4754
4755
4756
			&image, &min_address, &max_address);
	if (ERROR_OK != retval)
		return retval;
4757

Zachary T Welch's avatar
Zachary T Welch committed
4758
4759
	struct duration bench;
	duration_start(&bench);
4760

4761
	if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4762
4763
4764
4765
4766
4767
	{
		return ERROR_OK;
	}

	image_size = 0x0;
	retval = ERROR_OK;
zwelch's avatar
zwelch committed
4768
4769
	fastload_num = image.num_sections;
	fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
zwelch's avatar
zwelch committed
4770
	if (fastload == NULL)
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
	{
		image_close(&image);
		return ERROR_FAIL;
	}
	memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
	for (i = 0; i < image.num_sections; i++)
	{
		buffer = malloc(image.sections[i].size);
		if (buffer == NULL)
		{
4781
			command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
duane's avatar
duane committed
4782
						  (int)(image.sections[i].size));
4783
4784
4785
4786
4787
4788
4789
4790
4791
			break;
		}

		if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
		{
			free(buffer);
			break;
		}

zwelch's avatar
zwelch committed
4792
4793
		uint32_t offset = 0;
		uint32_t length = buf_cnt;
4794
4795
4796
4797


		/* DANGER!!! beware of unsigned comparision here!!! */

zwelch's avatar
zwelch committed
4798
		if ((image.sections[i].base_address + buf_cnt >= min_address)&&
zwelch's avatar
zwelch committed
4799
				(image.sections[i].base_address < max_address))
4800
		{
zwelch's avatar
zwelch committed
4801
			if (image.sections[i].base_address < min_address)
4802
4803
			{
				/* clip addresses below */
zwelch's avatar
zwelch committed
4804
				offset += min_address-image.sections[i].base_address;
zwelch's avatar
zwelch committed
4805
				length -= offset;
4806
4807
			}

zwelch's avatar
zwelch committed
4808
			if (image.sections[i].base_address + buf_cnt > max_address)
4809
			{
zwelch's avatar
zwelch committed
4810
				length -= (image.sections[i].base_address + buf_cnt)-max_address;
4811
4812
			}

zwelch's avatar
zwelch committed
4813
			fastload[i].address = image.sections[i].base_address + offset;
zwelch's avatar
zwelch committed
4814
			fastload[i].data = malloc(length);
zwelch's avatar
zwelch committed
4815
			if (fastload[i].data == NULL)
4816
4817
4818
4819
			{
				free(buffer);
				break;
			}
zwelch's avatar
zwelch committed
4820
			memcpy(fastload[i].data, buffer + offset, length);
zwelch's avatar
zwelch committed
4821
			fastload[i].length = length;
4822
4823

			image_size += length;
4824
			command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4825
						  (unsigned int)length,
zwelch's avatar
zwelch committed
4826
						  ((unsigned int)(image.sections[i].base_address + offset)));
4827
4828
4829
4830
4831
		}

		free(buffer);
	}

Zachary T Welch's avatar
Zachary T Welch committed
4832
	if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4833
	{
4834
		command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
Zachary T Welch's avatar
Zachary T Welch committed
4835
4836
4837
				"in %fs (%0.3f kb/s)", image_size, 
				duration_elapsed(&bench), duration_kbps(&bench, image_size));

4838
		command_print(CMD_CTX,
Zachary T Welch's avatar
Zachary T Welch committed
4839
4840
				"WARNING: image has not been loaded to target!"
				"You can issue a 'fast_load' to finish loading.");
4841
4842
4843
4844
	}

	image_close(&image);

zwelch's avatar
zwelch committed
4845
	if (retval != ERROR_OK)
4846
4847
4848
4849
4850
4851
4852
	{
		free_fastload();
	}

	return retval;
}

4853
COMMAND_HANDLER(handle_fast_load_command)
4854
{
4855
	if (CMD_ARGC > 0)
4856
		return ERROR_COMMAND_SYNTAX_ERROR;
zwelch's avatar
zwelch committed
4857
	if (fastload == NULL)
4858
4859
4860
4861
4862
	{
		LOG_ERROR("No image in memory");
		return ERROR_FAIL;
	}
	int i;
zwelch's avatar
zwelch committed
4863
4864
4865
	int ms = timeval_ms();
	int size = 0;
	int retval = ERROR_OK;
zwelch's avatar
zwelch committed
4866
	for (i = 0; i < fastload_num;i++)
4867
	{
4868
4869
		struct target *target = get_current_target(CMD_CTX);
		command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4870
					  (unsigned int)(fastload[i].address),
duane's avatar
duane committed
4871
					  (unsigned int)(fastload[i].length));
zwelch's avatar
zwelch committed
4872
		if (retval == ERROR_OK)
4873
		{
oharboe's avatar
oharboe committed
4874
			retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4875
		}
zwelch's avatar
zwelch committed
4876
		size += fastload[i].length;
4877
	}
zwelch's avatar
zwelch committed
4878
	int after = timeval_ms();
4879
	command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
oharboe's avatar
oharboe committed
4880
	return retval;
4881
}
duane's avatar
duane committed
4882

4883
4884
static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
4885
	struct command_context *context;
Zachary T Welch's avatar
Zachary T Welch committed
4886
	struct target *target;
4887
	int retval;
duane's avatar
duane committed
4888

4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
	context = Jim_GetAssocData(interp, "context");
	if (context == NULL) {
		LOG_ERROR("array2mem: no command context");
		return JIM_ERR;
	}
	target = get_current_target(context);
	if (target == NULL) {
		LOG_ERROR("array2mem: no current target");
		return JIM_ERR;
	}

	if ((argc < 6) || (argc > 7))
	{
		return JIM_ERR;
	}

	int cpnum;
	uint32_t op1;
	uint32_t op2;
	uint32_t CRn;
	uint32_t CRm;
	uint32_t value;

	int e;
	long l;
	e = Jim_GetLong(interp, argv[1], &l);
	if (e != JIM_OK) {
		return e;
	}
	cpnum = l;

	e = Jim_GetLong(interp, argv[2], &l);
	if (e != JIM_OK) {
		return e;
	}
	op1 = l;

	e = Jim_GetLong(interp, argv[3], &l);
	if (e != JIM_OK) {
		return e;
	}
4930
	CRn = l;
4931
4932
4933
4934
4935

	e = Jim_GetLong(interp, argv[4], &l);
	if (e != JIM_OK) {
		return e;
	}
4936
	CRm = l;
4937
4938
4939
4940
4941

	e = Jim_GetLong(interp, argv[5], &l);
	if (e != JIM_OK) {
		return e;
	}
4942
	op2 = l;
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967

	value = 0;

	if (argc == 7)
	{
		e = Jim_GetLong(interp, argv[6], &l);
		if (e != JIM_OK) {
			return e;
		}
		value = l;

		retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
		if (retval != ERROR_OK)
			return JIM_ERR;
	} else
	{
		retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
		if (retval != ERROR_OK)
			return JIM_ERR;

		Jim_SetResult(interp, Jim_NewIntObj(interp, value));
	}

	return JIM_OK;
}
4968

4969
4970
4971
4972
4973
4974
4975
4976
4977
static const struct command_registration target_command_handlers[] = {
	{
		.name = "targets",
		.handler = &handle_targets_command,
		.mode = COMMAND_ANY,
		.help = "change current command line target (one parameter) "
			"or list targets (no parameters)",
		.usage = "[<new_current_target>]",
	},
4978
4979
4980
4981
	{
		.name = "target",
		.mode = COMMAND_CONFIG,
		.help = "configure target",
4982
4983

		.chain = target_subcommand_handlers,
4984
	},
4985
4986
4987
	COMMAND_REGISTRATION_DONE
};

4988
int target_register_commands(struct command_context *cmd_ctx)
4989
{
4990
	return register_commands(cmd_ctx, NULL, target_command_handlers);
4991
4992
}

4993
4994
4995
4996
4997
4998
4999
5000
static const struct command_registration target_exec_command_handlers[] = {
	{
		.name = "fast_load_image",
		.handler = &handle_fast_load_image_command,
		.mode = COMMAND_ANY,
		.help = "Load image into memory, mainly for profiling purposes",
		.usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
			"[min_address] [max_length]",
For faster browsing, not all history is shown. View entire blame