target.c 109 KB
Newer Older
4001
4002
4003
4004
								   Jim_GetResult(goi->interp),
								   target_types[x]->name,
								   ", ", NULL);
			} else {
4005
				Jim_AppendStrings( goi->interp,
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
								   Jim_GetResult(goi->interp),
								   " or ",
								   target_types[x]->name,NULL );
			}
		}
		return JIM_ERR;
	}

	/* Create it */
	target = calloc(1,sizeof(target_t));
	/* set target number */
	target->target_number = new_target_number();

	/* allocate memory for each unique target type */
	target->type = (target_type_t*)calloc(1,sizeof(target_type_t));

	memcpy( target->type, target_types[x], sizeof(target_type_t));
4023

4024
4025
4026
4027
4028
4029
4030
	/* 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;
4031

4032
4033
4034
4035
4036
4037
4038
	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;
4039

4040
4041
	target->display             = 1;

4042
4043
4044
4045
4046
4047
4048
4049
4050
	/* initialize trace information */
	target->trace_info = malloc(sizeof(trace_t));
	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;
4051

4052
4053
4054
4055
4056
4057
4058
4059
	target->dbgmsg          = NULL;
	target->dbg_msg_enabled = 0;

	target->endianness = TARGET_ENDIAN_UNKNOWN;

	/* Do the rest as "configure" options */
	goi->isconfigure = 1;
	e = target_configure( goi, target);
4060
4061
4062
4063
4064
4065
4066

	if (target->tap == NULL)
	{
		Jim_SetResultString( interp, "-chain-position required when creating target", -1);
		e=JIM_ERR;
	}

4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
	if( e != JIM_OK ){
		free( target->type );
		free( target );
		return e;
	}

	if( target->endianness == TARGET_ENDIAN_UNKNOWN ){
		/* default endian to little if not specified */
		target->endianness = TARGET_LITTLE_ENDIAN;
	}

4078
4079
4080
	/* incase variant is not set */
	if (!target->variant)
		target->variant = strdup("");
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
	/* create the target specific commands */
	if( target->type->register_commands ){
		(*(target->type->register_commands))( cmd_ctx );
	}
	if( target->type->target_create ){
		(*(target->type->target_create))( target, goi->interp );
	}

	/* append to end of list */
	{
		target_t **tpp;
		tpp = &(all_targets);
		while( *tpp ){
			tpp = &( (*tpp)->next );
		}
		*tpp = target;
	}

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

	/* now - create the new target name command */
	e = Jim_CreateCommand( goi->interp,
						   /* name */
						   cp,
						   tcl_target_func, /* C function */
						   target, /* private data */
						   NULL ); /* no del proc */

	return e;
}

4114
static int jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
{
	int x,r,e;
	jim_wide w;
	struct command_context_s *cmd_ctx;
	target_t *target;
	Jim_GetOptInfo goi;
	enum tcmd {
		/* TG = target generic */
		TG_CMD_CREATE,
		TG_CMD_TYPES,
		TG_CMD_NAMES,
		TG_CMD_CURRENT,
		TG_CMD_NUMBER,
		TG_CMD_COUNT,
	};
	const char *target_cmds[] = {
4131
		"create", "types", "names", "current", "number",
4132
		"count",
4133
		NULL /* terminate */
4134
4135
4136
	};

	LOG_DEBUG("Target command params:");
4137
	LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147

	cmd_ctx = Jim_GetAssocData( interp, "context" );

	Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );

	if( goi.argc == 0 ){
		Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
		return JIM_ERR;
	}

4148
	/* Jim_GetOpt_Debug( &goi ); */
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
	r = Jim_GetOpt_Enum( &goi, target_cmds, &x   );
	if( r != JIM_OK ){
		return r;
	}

	switch(x){
	default:
		Jim_Panic(goi.interp,"Why am I here?");
		return JIM_ERR;
	case TG_CMD_CURRENT:
		if( goi.argc != 0 ){
			Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters");
			return JIM_ERR;
		}
		Jim_SetResultString( goi.interp, get_current_target( cmd_ctx )->cmd_name, -1 );
		return JIM_OK;
	case TG_CMD_TYPES:
		if( goi.argc != 0 ){
			Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
			return JIM_ERR;
		}
		Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
		for( x = 0 ; target_types[x] ; x++ ){
			Jim_ListAppendElement( goi.interp,
								   Jim_GetResult(goi.interp),
								   Jim_NewStringObj( goi.interp, target_types[x]->name, -1 ) );
		}
		return JIM_OK;
	case TG_CMD_NAMES:
		if( goi.argc != 0 ){
			Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
			return JIM_ERR;
		}
		Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
		target = all_targets;
		while( target ){
			Jim_ListAppendElement( goi.interp,
								   Jim_GetResult(goi.interp),
								   Jim_NewStringObj( goi.interp, target->cmd_name, -1 ) );
			target = target->next;
		}
4190
		return JIM_OK;
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
	case TG_CMD_CREATE:
		if( goi.argc < 3 ){
			Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "?name  ... config options ...");
			return JIM_ERR;
		}
		return target_create( &goi );
		break;
	case TG_CMD_NUMBER:
		if( goi.argc != 1 ){
			Jim_SetResult_sprintf( goi.interp, "expected: target number ?NUMBER?");
			return JIM_ERR;
		}
		e = Jim_GetOpt_Wide( &goi, &w );
		if( e != JIM_OK ){
			return JIM_ERR;
		}
		{
			target_t *t;
			t = get_target_by_num(w);
			if( t == NULL ){
				Jim_SetResult_sprintf( goi.interp,"Target: number %d does not exist", (int)(w));
				return JIM_ERR;
			}
			Jim_SetResultString( goi.interp, t->cmd_name, -1 );
			return JIM_OK;
		}
	case TG_CMD_COUNT:
		if( goi.argc != 0 ){
			Jim_WrongNumArgs( goi.interp, 0, goi.argv, "<no parameters>");
			return JIM_ERR;
		}
4222
		Jim_SetResult( goi.interp,
4223
4224
4225
					   Jim_NewIntObj( goi.interp, max_target_number()));
		return JIM_OK;
	}
4226
4227

	return JIM_ERR;
4228
}
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241


struct FastLoad
{
	u32 address;
	u8 *data;
	int length;

};

static int fastload_num;
static struct FastLoad *fastload;

4242
static void free_fastload(void)
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
{
	if (fastload!=NULL)
	{
		int i;
		for (i=0; i<fastload_num; i++)
		{
			if (fastload[i].data)
				free(fastload[i].data);
		}
		free(fastload);
		fastload=NULL;
	}
}




4260
static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
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
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
{
	u8 *buffer;
	u32 buf_cnt;
	u32 image_size;
	u32 min_address=0;
	u32 max_address=0xffffffff;
	int i;
	int retval;

	image_t image;

	duration_t duration;
	char *duration_text;

	if ((argc < 1)||(argc > 5))
	{
		return ERROR_COMMAND_SYNTAX_ERROR;
	}

	/* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
	if (argc >= 2)
	{
		image.base_address_set = 1;
		image.base_address = strtoul(args[1], NULL, 0);
	}
	else
	{
		image.base_address_set = 0;
	}


	image.start_address_set = 0;

	if (argc>=4)
	{
		min_address=strtoul(args[3], NULL, 0);
	}
	if (argc>=5)
	{
		max_address=strtoul(args[4], NULL, 0)+min_address;
	}

	if (min_address>max_address)
	{
		return ERROR_COMMAND_SYNTAX_ERROR;
	}

	duration_start_measure(&duration);

	if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
	{
		return ERROR_OK;
	}

	image_size = 0x0;
	retval = ERROR_OK;
	fastload_num=image.num_sections;
	fastload=(struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
	if (fastload==NULL)
	{
		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)
		{
			command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
			break;
		}

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

		u32 offset=0;
		u32 length=buf_cnt;


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

		if ((image.sections[i].base_address+buf_cnt>=min_address)&&
				(image.sections[i].base_address<max_address))
		{
			if (image.sections[i].base_address<min_address)
			{
				/* clip addresses below */
				offset+=min_address-image.sections[i].base_address;
				length-=offset;
			}

			if (image.sections[i].base_address+buf_cnt>max_address)
			{
				length-=(image.sections[i].base_address+buf_cnt)-max_address;
			}

			fastload[i].address=image.sections[i].base_address+offset;
			fastload[i].data=malloc(length);
			if (fastload[i].data==NULL)
			{
				free(buffer);
				break;
			}
			memcpy(fastload[i].data, buffer+offset, length);
			fastload[i].length=length;

			image_size += length;
			command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
		}

		free(buffer);
	}

	duration_stop_measure(&duration, &duration_text);
	if (retval==ERROR_OK)
	{
		command_print(cmd_ctx, "Loaded %u bytes in %s", image_size, duration_text);
		command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
	}
	free(duration_text);

	image_close(&image);

	if (retval!=ERROR_OK)
	{
		free_fastload();
	}

	return retval;
}

4396
static int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
{
	if (argc>0)
		return ERROR_COMMAND_SYNTAX_ERROR;
	if (fastload==NULL)
	{
		LOG_ERROR("No image in memory");
		return ERROR_FAIL;
	}
	int i;
	int ms=timeval_ms();
	int size=0;
oharboe's avatar
oharboe committed
4408
	int retval=ERROR_OK;
4409
4410
4411
	for (i=0; i<fastload_num;i++)
	{
		target_t *target = get_current_target(cmd_ctx);
oharboe's avatar
oharboe committed
4412
4413
		command_print(cmd_ctx, "Write to 0x%08x, length 0x%08x", fastload[i].address, fastload[i].length);
		if (retval==ERROR_OK)
4414
		{
oharboe's avatar
oharboe committed
4415
			retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4416
4417
4418
4419
4420
		}
		size+=fastload[i].length;
	}
	int after=timeval_ms();
	command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
oharboe's avatar
oharboe committed
4421
	return retval;
4422
}
For faster browsing, not all history is shown. View entire blame