target.c 124 KB
Newer Older
3001
	command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3002

zwelch's avatar
zwelch committed
3003
3004
	static const int maxSample = 10000;
	uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
zwelch's avatar
zwelch committed
3005
	if (samples == NULL)
3006
		return ERROR_OK;
oharboe's avatar
oharboe committed
3007

zwelch's avatar
zwelch committed
3008
	int numSamples = 0;
3009
	/* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
Zachary T Welch's avatar
Zachary T Welch committed
3010
	struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
oharboe's avatar
oharboe committed
3011

3012
3013
	for (;;)
	{
3014
		int retval;
3015
		target_poll(target);
3016
3017
		if (target->state == TARGET_HALTED)
		{
3018
			uint32_t t=*((uint32_t *)reg->value);
3019
			samples[numSamples++]=t;
3020
3021
			retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
			target_poll(target);
3022
			alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3023
3024
		} else if (target->state == TARGET_RUNNING)
		{
3025
			/* We want to quickly sample the PC. */
zwelch's avatar
zwelch committed
3026
			if ((retval = target_halt(target)) != ERROR_OK)
3027
3028
3029
3030
			{
				free(samples);
				return retval;
			}
3031
3032
		} else
		{
3033
			command_print(CMD_CTX, "Target not halted or running");
zwelch's avatar
zwelch committed
3034
			retval = ERROR_OK;
3035
3036
			break;
		}
zwelch's avatar
zwelch committed
3037
		if (retval != ERROR_OK)
3038
3039
3040
		{
			break;
		}
oharboe's avatar
oharboe committed
3041

3042
		gettimeofday(&now, NULL);
zwelch's avatar
zwelch committed
3043
		if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3044
		{
3045
			command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
zwelch's avatar
zwelch committed
3046
			if ((retval = target_poll(target)) != ERROR_OK)
3047
3048
3049
3050
			{
				free(samples);
				return retval;
			}
3051
3052
			if (target->state == TARGET_HALTED)
			{
3053
				target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3054
			}
zwelch's avatar
zwelch committed
3055
			if ((retval = target_poll(target)) != ERROR_OK)
3056
3057
3058
3059
			{
				free(samples);
				return retval;
			}
3060
			writeGmon(samples, numSamples, CMD_ARGV[1]);
3061
			command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3062
3063
3064
3065
			break;
		}
	}
	free(samples);
oharboe's avatar
oharboe committed
3066

3067
3068
	return ERROR_OK;
}
3069

3070
static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3071
3072
3073
3074
3075
3076
3077
3078
{
	char *namebuf;
	Jim_Obj *nameObjPtr, *valObjPtr;
	int result;

	namebuf = alloc_printf("%s(%d)", varname, idx);
	if (!namebuf)
		return JIM_ERR;
oharboe's avatar
oharboe committed
3079

3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
	nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
	valObjPtr = Jim_NewIntObj(interp, val);
	if (!nameObjPtr || !valObjPtr)
	{
		free(namebuf);
		return JIM_ERR;
	}

	Jim_IncrRefCount(nameObjPtr);
	Jim_IncrRefCount(valObjPtr);
	result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
	Jim_DecrRefCount(interp, nameObjPtr);
	Jim_DecrRefCount(interp, valObjPtr);
	free(namebuf);
	/* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
	return result;
}

static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
3100
	struct command_context *context;
Zachary T Welch's avatar
Zachary T Welch committed
3101
	struct target *target;
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115

	context = Jim_GetAssocData(interp, "context");
	if (context == NULL)
	{
		LOG_ERROR("mem2array: no command context");
		return JIM_ERR;
	}
	target = get_current_target(context);
	if (target == NULL)
	{
		LOG_ERROR("mem2array: no current target");
		return JIM_ERR;
	}

zwelch's avatar
zwelch committed
3116
	return 	target_mem2array(interp, target, argc-1, argv + 1);
3117
3118
}

Zachary T Welch's avatar
Zachary T Welch committed
3119
static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3120
{
3121
	long l;
3122
	uint32_t width;
3123
	int len;
3124
3125
3126
	uint32_t addr;
	uint32_t count;
	uint32_t v;
3127
	const char *varname;
3128
	int  n, e, retval;
3129
	uint32_t i;
3130
3131
3132

	/* argv[1] = name of array to receive the data
	 * argv[2] = desired width
oharboe's avatar
oharboe committed
3133
	 * argv[3] = memory address
3134
3135
	 * argv[4] = count of times to read
	 */
3136
	if (argc != 4) {
3137
3138
3139
		Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
		return JIM_ERR;
	}
3140
	varname = Jim_GetString(argv[0], &len);
3141
3142
	/* given "foo" get space for worse case "foo(%d)" .. add 20 */

3143
	e = Jim_GetLong(interp, argv[1], &l);
3144
3145
3146
3147
	width = l;
	if (e != JIM_OK) {
		return e;
	}
oharboe's avatar
oharboe committed
3148

3149
	e = Jim_GetLong(interp, argv[2], &l);
3150
3151
3152
3153
	addr = l;
	if (e != JIM_OK) {
		return e;
	}
3154
	e = Jim_GetLong(interp, argv[3], &l);
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
	len = l;
	if (e != JIM_OK) {
		return e;
	}
	switch (width) {
		case 8:
			width = 1;
			break;
		case 16:
			width = 2;
			break;
		case 32:
			width = 4;
			break;
		default:
			Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3171
			Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
			return JIM_ERR;
	}
	if (len == 0) {
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
		Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
		return JIM_ERR;
	}
	if ((addr + (len * width)) < addr) {
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
		Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
		return JIM_ERR;
	}
	/* absurd transfer size? */
	if (len > 65536) {
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
		Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
		return JIM_ERR;
oharboe's avatar
oharboe committed
3189
3190
	}

3191
3192
3193
3194
3195
3196
3197
	if ((width == 1) ||
		((width == 2) && ((addr & 1) == 0)) ||
		((width == 4) && ((addr & 3) == 0))) {
		/* all is well */
	} else {
		char buf[100];
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3198
3199
		sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
				addr,
duane's avatar
duane committed
3200
				width);
3201
3202
3203
3204
3205
3206
3207
3208
		Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
		return JIM_ERR;
	}

	/* Transfer loop */

	/* index counter */
	n = 0;
Øyvind Harboe's avatar
Øyvind Harboe committed
3209
3210
3211
3212
3213
3214

	size_t buffersize = 4096;
	uint8_t *buffer = malloc(buffersize);
	if (buffer == NULL)
		return JIM_ERR;

3215
3216
3217
3218
	/* assume ok */
	e = JIM_OK;
	while (len) {
		/* Slurp... in buffer size chunks */
oharboe's avatar
oharboe committed
3219

3220
		count = len; /* in objects.. */
Øyvind Harboe's avatar
Øyvind Harboe committed
3221
3222
		if (count > (buffersize/width)) {
			count = (buffersize/width);
3223
		}
oharboe's avatar
oharboe committed
3224

3225
		retval = target_read_memory(target, addr, width, count, buffer);
3226
3227
		if (retval != ERROR_OK) {
			/* BOO !*/
3228
3229
3230
			LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
					  (unsigned int)addr,
					  (int)width,
duane's avatar
duane committed
3231
					  (int)count);
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
			Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
			Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
			e = JIM_ERR;
			len = 0;
		} else {
			v = 0; /* shut up gcc */
			for (i = 0 ;i < count ;i++, n++) {
				switch (width) {
					case 4:
						v = target_buffer_get_u32(target, &buffer[i*width]);
						break;
					case 2:
						v = target_buffer_get_u16(target, &buffer[i*width]);
						break;
					case 1:
						v = buffer[i] & 0x0ff;
						break;
				}
				new_int_array_element(interp, varname, n, v);
			}
			len -= count;
		}
	}
oharboe's avatar
oharboe committed
3255

Øyvind Harboe's avatar
Øyvind Harboe committed
3256
3257
	free(buffer);

3258
3259
3260
3261
3262
	Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));

	return JIM_OK;
}

3263
static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
{
	char *namebuf;
	Jim_Obj *nameObjPtr, *valObjPtr;
	int result;
	long l;

	namebuf = alloc_printf("%s(%d)", varname, idx);
	if (!namebuf)
		return JIM_ERR;

	nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
	if (!nameObjPtr)
	{
		free(namebuf);
		return JIM_ERR;
	}

	Jim_IncrRefCount(nameObjPtr);
	valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
	Jim_DecrRefCount(interp, nameObjPtr);
	free(namebuf);
	if (valObjPtr == NULL)
		return JIM_ERR;

	result = Jim_GetLong(interp, valObjPtr, &l);
	/* printf("%s(%d) => 0%08x\n", varname, idx, val); */
	*val = l;
	return result;
}

static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
3296
	struct command_context *context;
Zachary T Welch's avatar
Zachary T Welch committed
3297
	struct target *target;
3298

3299
	context = Jim_GetAssocData(interp, "context");
zwelch's avatar
zwelch committed
3300
	if (context == NULL) {
3301
3302
3303
3304
		LOG_ERROR("array2mem: no command context");
		return JIM_ERR;
	}
	target = get_current_target(context);
zwelch's avatar
zwelch committed
3305
	if (target == NULL) {
3306
3307
3308
		LOG_ERROR("array2mem: no current target");
		return JIM_ERR;
	}
3309

3310
	return target_array2mem(interp,target, argc-1, argv + 1);
3311
}
3312
3313
3314

static int target_array2mem(Jim_Interp *interp, struct target *target,
		int argc, Jim_Obj *const *argv)
3315
{
3316
	long l;
3317
	uint32_t width;
3318
	int len;
3319
3320
3321
	uint32_t addr;
	uint32_t count;
	uint32_t v;
3322
	const char *varname;
3323
	int  n, e, retval;
3324
	uint32_t i;
3325
3326
3327

	/* argv[1] = name of array to get the data
	 * argv[2] = desired width
oharboe's avatar
oharboe committed
3328
	 * argv[3] = memory address
3329
3330
	 * argv[4] = count to write
	 */
3331
	if (argc != 4) {
3332
		Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3333
3334
		return JIM_ERR;
	}
3335
	varname = Jim_GetString(argv[0], &len);
3336
3337
	/* given "foo" get space for worse case "foo(%d)" .. add 20 */

3338
	e = Jim_GetLong(interp, argv[1], &l);
3339
3340
3341
3342
	width = l;
	if (e != JIM_OK) {
		return e;
	}
oharboe's avatar
oharboe committed
3343

3344
	e = Jim_GetLong(interp, argv[2], &l);
3345
3346
3347
3348
	addr = l;
	if (e != JIM_OK) {
		return e;
	}
3349
	e = Jim_GetLong(interp, argv[3], &l);
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
	len = l;
	if (e != JIM_OK) {
		return e;
	}
	switch (width) {
		case 8:
			width = 1;
			break;
		case 16:
			width = 2;
			break;
		case 32:
			width = 4;
			break;
		default:
			Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3366
			Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
			return JIM_ERR;
	}
	if (len == 0) {
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
		Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
		return JIM_ERR;
	}
	if ((addr + (len * width)) < addr) {
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
		Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
		return JIM_ERR;
	}
	/* absurd transfer size? */
	if (len > 65536) {
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
		Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
		return JIM_ERR;
oharboe's avatar
oharboe committed
3384
3385
	}

3386
3387
3388
3389
3390
3391
3392
	if ((width == 1) ||
		((width == 2) && ((addr & 1) == 0)) ||
		((width == 4) && ((addr & 3) == 0))) {
		/* all is well */
	} else {
		char buf[100];
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3393
3394
		sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
				(unsigned int)addr,
duane's avatar
duane committed
3395
				(int)width);
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
		Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
		return JIM_ERR;
	}

	/* Transfer loop */

	/* index counter */
	n = 0;
	/* assume ok */
	e = JIM_OK;
Øyvind Harboe's avatar
Øyvind Harboe committed
3406
3407
3408
3409
3410
3411

	size_t buffersize = 4096;
	uint8_t *buffer = malloc(buffersize);
	if (buffer == NULL)
		return JIM_ERR;

3412
3413
	while (len) {
		/* Slurp... in buffer size chunks */
oharboe's avatar
oharboe committed
3414

3415
		count = len; /* in objects.. */
Øyvind Harboe's avatar
Øyvind Harboe committed
3416
3417
		if (count > (buffersize/width)) {
			count = (buffersize/width);
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
		}

		v = 0; /* shut up gcc */
		for (i = 0 ;i < count ;i++, n++) {
			get_int_array_element(interp, varname, n, &v);
			switch (width) {
			case 4:
				target_buffer_set_u32(target, &buffer[i*width], v);
				break;
			case 2:
				target_buffer_set_u16(target, &buffer[i*width], v);
				break;
			case 1:
				buffer[i] = v & 0x0ff;
				break;
			}
		}
		len -= count;

zwelch's avatar
zwelch committed
3437
		retval = target_write_memory(target, addr, width, count, buffer);
3438
3439
		if (retval != ERROR_OK) {
			/* BOO !*/
3440
3441
3442
			LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
					  (unsigned int)addr,
					  (int)width,
duane's avatar
duane committed
3443
					  (int)count);
3444
			Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3445
			Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3446
3447
3448
3449
			e = JIM_ERR;
			len = 0;
		}
	}
oharboe's avatar
oharboe committed
3450

Øyvind Harboe's avatar
Øyvind Harboe committed
3451
3452
	free(buffer);

3453
3454
3455
3456
	Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));

	return JIM_OK;
}
3457

3458
void target_all_handle_event(enum target_event e)
3459
{
Zachary T Welch's avatar
Zachary T Welch committed
3460
	struct target *target;
3461

3462
	LOG_DEBUG("**all*targets: event: %d, %s",
duane's avatar
duane committed
3463
			   (int)e,
3464
			   Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3465
3466

	target = all_targets;
zwelch's avatar
zwelch committed
3467
	while (target) {
3468
		target_handle_event(target, e);
3469
3470
3471
3472
		target = target->next;
	}
}

3473
3474
3475
3476

/* FIX? should we propagate errors here rather than printing them
 * and continuing?
 */
Zachary T Welch's avatar
Zachary T Welch committed
3477
void target_handle_event(struct target *target, enum target_event e)
3478
{
3479
	struct target_event_action *teap;
3480

dbrownell's avatar
dbrownell committed
3481
	for (teap = target->event_action; teap != NULL; teap = teap->next) {
zwelch's avatar
zwelch committed
3482
		if (teap->event == e) {
3483
			LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3484
					   target->target_number,
3485
					   target_name(target),
3486
					   target_type_name(target),
3487
					   e,
3488
3489
					   Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
					   Jim_GetString(teap->body, NULL));
3490
			if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3491
			{
3492
				Jim_PrintErrorMessage(teap->interp);
3493
			}
3494
3495
3496
3497
		}
	}
}

3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
/**
 * Returns true only if the target has a handler for the specified event.
 */
bool target_has_event_action(struct target *target, enum target_event event)
{
	struct target_event_action *teap;

	for (teap = target->event_action; teap != NULL; teap = teap->next) {
		if (teap->event == event)
			return true;
	}
	return false;
}

3512
3513
enum target_cfg_param {
	TCFG_TYPE,
3514
	TCFG_EVENT,
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
	TCFG_WORK_AREA_VIRT,
	TCFG_WORK_AREA_PHYS,
	TCFG_WORK_AREA_SIZE,
	TCFG_WORK_AREA_BACKUP,
	TCFG_ENDIAN,
	TCFG_VARIANT,
	TCFG_CHAIN_POSITION,
};

static Jim_Nvp nvp_config_opts[] = {
	{ .name = "-type",             .value = TCFG_TYPE },
	{ .name = "-event",            .value = TCFG_EVENT },
	{ .name = "-work-area-virt",   .value = TCFG_WORK_AREA_VIRT },
	{ .name = "-work-area-phys",   .value = TCFG_WORK_AREA_PHYS },
	{ .name = "-work-area-size",   .value = TCFG_WORK_AREA_SIZE },
	{ .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
	{ .name = "-endian" ,          .value = TCFG_ENDIAN },
	{ .name = "-variant",          .value = TCFG_VARIANT },
	{ .name = "-chain-position",   .value = TCFG_CHAIN_POSITION },
3534

3535
3536
	{ .name = NULL, .value = -1 }
};
3537

Zachary T Welch's avatar
Zachary T Welch committed
3538
static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3539
3540
3541
3542
3543
3544
3545
3546
{
	Jim_Nvp *n;
	Jim_Obj *o;
	jim_wide w;
	char *cp;
	int e;

	/* parse config or cget options ... */
zwelch's avatar
zwelch committed
3547
	while (goi->argc > 0) {
3548
3549
		Jim_SetEmptyResult(goi->interp);
		/* Jim_GetOpt_Debug(goi); */
3550

zwelch's avatar
zwelch committed
3551
		if (target->type->target_jim_configure) {
3552
3553
			/* target defines a configure function */
			/* target gets first dibs on parameters */
3554
			e = (*(target->type->target_jim_configure))(target, goi);
zwelch's avatar
zwelch committed
3555
			if (e == JIM_OK) {
3556
3557
3558
				/* more? */
				continue;
			}
zwelch's avatar
zwelch committed
3559
			if (e == JIM_ERR) {
3560
3561
3562
3563
3564
				/* An error */
				return e;
			}
			/* otherwise we 'continue' below */
		}
3565
		e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
zwelch's avatar
zwelch committed
3566
		if (e != JIM_OK) {
3567
			Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3568
3569
			return e;
		}
zwelch's avatar
zwelch committed
3570
		switch (n->value) {
3571
3572
		case TCFG_TYPE:
			/* not setable */
zwelch's avatar
zwelch committed
3573
			if (goi->isconfigure) {
3574
3575
				Jim_SetResult_sprintf(goi->interp,
						"not settable: %s", n->name);
3576
3577
3578
				return JIM_ERR;
			} else {
			no_params:
zwelch's avatar
zwelch committed
3579
				if (goi->argc != 0) {
3580
3581
3582
					Jim_WrongNumArgs(goi->interp,
							goi->argc, goi->argv,
							"NO PARAMS");
3583
3584
3585
					return JIM_ERR;
				}
			}
3586
3587
			Jim_SetResultString(goi->interp,
					target_type_name(target), -1);
3588
3589
3590
			/* loop for more */
			break;
		case TCFG_EVENT:
zwelch's avatar
zwelch committed
3591
			if (goi->argc == 0) {
3592
				Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3593
3594
3595
				return JIM_ERR;
			}

3596
			e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
zwelch's avatar
zwelch committed
3597
			if (e != JIM_OK) {
3598
				Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3599
3600
3601
				return e;
			}

zwelch's avatar
zwelch committed
3602
3603
			if (goi->isconfigure) {
				if (goi->argc != 1) {
3604
					Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3605
					return JIM_ERR;
3606
				}
3607
			} else {
zwelch's avatar
zwelch committed
3608
				if (goi->argc != 0) {
3609
3610
3611
3612
3613
					Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
					return JIM_ERR;
				}
			}

3614
			{
3615
				struct target_event_action *teap;
3616

3617
3618
				teap = target->event_action;
				/* replace existing? */
zwelch's avatar
zwelch committed
3619
3620
				while (teap) {
					if (teap->event == (enum target_event)n->value) {
3621
3622
3623
3624
						break;
					}
					teap = teap->next;
				}
3625

zwelch's avatar
zwelch committed
3626
				if (goi->isconfigure) {
3627
					bool replace = true;
zwelch's avatar
zwelch committed
3628
					if (teap == NULL) {
3629
						/* create new */
3630
						teap = calloc(1, sizeof(*teap));
3631
						replace = false;
3632
3633
					}
					teap->event = n->value;
3634
					teap->interp = goi->interp;
3635
					Jim_GetOpt_Obj(goi, &o);
zwelch's avatar
zwelch committed
3636
					if (teap->body) {
3637
						Jim_DecrRefCount(teap->interp, teap->body);
3638
					}
3639
					teap->body  = Jim_DuplicateObj(goi->interp, o);
3640
					/*
3641
					 * FIXME:
3642
3643
3644
3645
3646
3647
3648
3649
					 *     Tcl/TK - "tk events" have a nice feature.
					 *     See the "BIND" command.
					 *    We should support that here.
					 *     You can specify %X and %Y in the event code.
					 *     The idea is: %T - target name.
					 *     The idea is: %N - target number
					 *     The idea is: %E - event name.
					 */
3650
					Jim_IncrRefCount(teap->body);
3651

3652
3653
3654
3655
3656
3657
					if (!replace)
					{
						/* add to head of event list */
						teap->next = target->event_action;
						target->event_action = teap;
					}
3658
3659
3660
					Jim_SetEmptyResult(goi->interp);
				} else {
					/* get */
zwelch's avatar
zwelch committed
3661
					if (teap == NULL) {
3662
						Jim_SetEmptyResult(goi->interp);
3663
					} else {
3664
						Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3665
3666
3667
3668
3669
3670
3671
					}
				}
			}
			/* loop for more */
			break;

		case TCFG_WORK_AREA_VIRT:
zwelch's avatar
zwelch committed
3672
			if (goi->isconfigure) {
3673
				target_free_all_working_areas(target);
3674
				e = Jim_GetOpt_Wide(goi, &w);
zwelch's avatar
zwelch committed
3675
				if (e != JIM_OK) {
3676
3677
3678
					return e;
				}
				target->working_area_virt = w;
3679
				target->working_area_virt_spec = true;
3680
			} else {
zwelch's avatar
zwelch committed
3681
				if (goi->argc != 0) {
3682
3683
					goto no_params;
				}
3684
			}
3685
			Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3686
3687
			/* loop for more */
			break;
3688

3689
		case TCFG_WORK_AREA_PHYS:
zwelch's avatar
zwelch committed
3690
			if (goi->isconfigure) {
3691
				target_free_all_working_areas(target);
3692
				e = Jim_GetOpt_Wide(goi, &w);
zwelch's avatar
zwelch committed
3693
				if (e != JIM_OK) {
3694
3695
3696
					return e;
				}
				target->working_area_phys = w;
3697
				target->working_area_phys_spec = true;
3698
			} else {
zwelch's avatar
zwelch committed
3699
				if (goi->argc != 0) {
3700
3701
					goto no_params;
				}
3702
			}
3703
			Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3704
3705
3706
3707
			/* loop for more */
			break;

		case TCFG_WORK_AREA_SIZE:
zwelch's avatar
zwelch committed
3708
			if (goi->isconfigure) {
3709
				target_free_all_working_areas(target);
3710
				e = Jim_GetOpt_Wide(goi, &w);
zwelch's avatar
zwelch committed
3711
				if (e != JIM_OK) {
3712
3713
3714
3715
					return e;
				}
				target->working_area_size = w;
			} else {
zwelch's avatar
zwelch committed
3716
				if (goi->argc != 0) {
3717
3718
					goto no_params;
				}
3719
			}
3720
			Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3721
3722
3723
3724
			/* loop for more */
			break;

		case TCFG_WORK_AREA_BACKUP:
zwelch's avatar
zwelch committed
3725
			if (goi->isconfigure) {
3726
				target_free_all_working_areas(target);
3727
				e = Jim_GetOpt_Wide(goi, &w);
zwelch's avatar
zwelch committed
3728
				if (e != JIM_OK) {
3729
3730
3731
3732
3733
					return e;
				}
				/* make this exactly 1 or 0 */
				target->backup_working_area = (!!w);
			} else {
zwelch's avatar
zwelch committed
3734
				if (goi->argc != 0) {
3735
3736
					goto no_params;
				}
3737
			}
3738
			Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3739
3740
3741
3742
			/* loop for more e*/
			break;

		case TCFG_ENDIAN:
zwelch's avatar
zwelch committed
3743
			if (goi->isconfigure) {
3744
				e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
zwelch's avatar
zwelch committed
3745
				if (e != JIM_OK) {
3746
					Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3747
3748
3749
3750
					return e;
				}
				target->endianness = n->value;
			} else {
zwelch's avatar
zwelch committed
3751
				if (goi->argc != 0) {
3752
3753
3754
					goto no_params;
				}
			}
3755
			n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
zwelch's avatar
zwelch committed
3756
			if (n->name == NULL) {
3757
				target->endianness = TARGET_LITTLE_ENDIAN;
3758
				n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3759
			}
3760
			Jim_SetResultString(goi->interp, n->name, -1);
3761
3762
3763
3764
			/* loop for more */
			break;

		case TCFG_VARIANT:
zwelch's avatar
zwelch committed
3765
3766
			if (goi->isconfigure) {
				if (goi->argc < 1) {
3767
					Jim_SetResult_sprintf(goi->interp,
3768
										   "%s ?STRING?",
3769
										   n->name);
3770
3771
					return JIM_ERR;
				}
zwelch's avatar
zwelch committed
3772
				if (target->variant) {
3773
3774
					free((void *)(target->variant));
				}
3775
				e = Jim_GetOpt_String(goi, &cp, NULL);
3776
3777
				target->variant = strdup(cp);
			} else {
zwelch's avatar
zwelch committed
3778
				if (goi->argc != 0) {
3779
3780
					goto no_params;
				}
3781
			}
3782
			Jim_SetResultString(goi->interp, target->variant,-1);
3783
3784
3785
			/* loop for more */
			break;
		case TCFG_CHAIN_POSITION:
zwelch's avatar
zwelch committed
3786
			if (goi->isconfigure) {
3787
				Jim_Obj *o;
3788
				struct jtag_tap *tap;
3789
				target_free_all_working_areas(target);
3790
				e = Jim_GetOpt_Obj(goi, &o);
zwelch's avatar
zwelch committed
3791
				if (e != JIM_OK) {
3792
3793
					return e;
				}
3794
				tap = jtag_tap_by_jim_obj(goi->interp, o);
zwelch's avatar
zwelch committed
3795
				if (tap == NULL) {
3796
					return JIM_ERR;
3797
				}
3798
				/* make this exactly 1 or 0 */
3799
				target->tap = tap;
3800
			} else {
zwelch's avatar
zwelch committed
3801
				if (goi->argc != 0) {
3802
3803
					goto no_params;
				}
3804
			}
3805
			Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
3806
3807
3808
			/* loop for more e*/
			break;
		}
3809
	} /* while (goi->argc) */
3810
3811


3812
		/* done - we return */
3813
3814
3815
	return JIM_OK;
}

3816
3817
static int
jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3818
3819
{
	Jim_GetOptInfo goi;
3820

3821
	Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3822
	goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
	int need_args = 1 + goi.isconfigure;
	if (goi.argc < need_args)
	{
		Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
			goi.isconfigure
				? "missing: -option VALUE ..."
				: "missing: -option ...");
		return JIM_ERR;
	}
	struct target *target = Jim_CmdPrivData(goi.interp);
	return target_configure(&goi, target);
}
3835

3836
3837
3838
static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	const char *cmd_name = Jim_GetString(argv[0], NULL);
3839

3840
3841
	Jim_GetOptInfo goi;
	Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3842

3843
3844
3845
3846
3847
3848
	if (goi.argc != 2 && goi.argc != 3)
	{
		Jim_SetResult_sprintf(goi.interp,
				"usage: %s <address> <data> [<count>]", cmd_name);
		return JIM_ERR;
	}
3849

3850
3851
3852
3853
	jim_wide a;
	int e = Jim_GetOpt_Wide(&goi, &a);
	if (e != JIM_OK)
		return e;
3854

3855
3856
3857
	jim_wide b;
	e = Jim_GetOpt_Wide(&goi, &b);
	if (e != JIM_OK)
3858
		return e;
3859
3860
3861
3862
3863
3864
3865

	jim_wide c = 1;
	if (goi.argc == 3)
	{
		e = Jim_GetOpt_Wide(&goi, &c);
		if (e != JIM_OK)
			return e;
3866
3867
	}

3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
	struct target *target = Jim_CmdPrivData(goi.interp);
	uint8_t  target_buf[32];
	if (strcasecmp(cmd_name, "mww") == 0) {
		target_buffer_set_u32(target, target_buf, b);
		b = 4;
	}
	else if (strcasecmp(cmd_name, "mwh") == 0) {
		target_buffer_set_u16(target, target_buf, b);
		b = 2;
	}
	else if (strcasecmp(cmd_name, "mwb") == 0) {
		target_buffer_set_u8(target, target_buf, b);
		b = 1;
	} else {
		LOG_ERROR("command '%s' unknown: ", cmd_name);
		return JIM_ERR;
	}
3885

3886
3887
3888
3889
3890
3891
3892
	for (jim_wide x = 0; x < c; x++)
	{
		e = target_write_memory(target, a, b, 1, target_buf);
		if (e != ERROR_OK)
		{
			Jim_SetResult_sprintf(interp,
					"Error writing @ 0x%08x: %d\n", (int)(a), e);
3893
3894
			return JIM_ERR;
		}
3895
3896
3897
3898
3899
		/* b = width */
		a = a + b;
	}
	return JIM_OK;
}
3900

3901
3902
3903
static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	const char *cmd_name = Jim_GetString(argv[0], NULL);
3904

3905
3906
	Jim_GetOptInfo goi;
	Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3907

3908
3909
3910
3911
3912
3913
	if ((goi.argc == 2) || (goi.argc == 3))
	{
		Jim_SetResult_sprintf(goi.interp,
				"usage: %s <address> [<count>]", cmd_name);
		return JIM_ERR;
	}
3914

3915
3916
3917
3918
3919
3920
3921
3922
	jim_wide a;
	int e = Jim_GetOpt_Wide(&goi, &a);
	if (e != JIM_OK) {
		return JIM_ERR;
	}
	jim_wide c;
	if (goi.argc) {
		e = Jim_GetOpt_Wide(&goi, &c);
zwelch's avatar
zwelch committed
3923
		if (e != JIM_OK) {
3924
3925
			return JIM_ERR;
		}
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
	} else {
		c = 1;
	}
	jim_wide b = 1; /* shut up gcc */
	if (strcasecmp(cmd_name, "mdw") == 0)
		b = 4;
	else if (strcasecmp(cmd_name, "mdh") == 0)
		b = 2;
	else if (strcasecmp(cmd_name, "mdb") == 0)
		b = 1;
	else {
		LOG_ERROR("command '%s' unknown: ", cmd_name);
		return JIM_ERR;
	}

	/* convert count to "bytes" */
	c = c * b;

	struct target *target = Jim_CmdPrivData(goi.interp);
	uint8_t  target_buf[32];
	jim_wide x, y, z;
	while (c > 0) {
		y = c;
		if (y > 16) {
			y = 16;
3951
		}
3952
3953
3954
3955
		e = target_read_memory(target, a, b, y / b, target_buf);
		if (e != ERROR_OK) {
			Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
			return JIM_ERR;
3956
3957
		}

3958
3959
3960
3961
3962
3963
3964
		Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
		switch (b) {
		case 4:
			for (x = 0; x < 16 && x < y; x += 4)
			{
				z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
				Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
3965
			}
3966
3967
			for (; (x < 16) ; x += 4) {
				Jim_fprintf(interp, interp->cookie_stdout, "         ");
3968
			}
3969
3970
3971
3972
3973
3974
			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));
3975
			}
3976
3977
			for (; (x < 16) ; x += 2) {
				Jim_fprintf(interp, interp->cookie_stdout, "     ");
3978
			}
3979
3980
3981
3982
3983
3984
			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));
3985
			}