target.c 114 KB
Newer Older
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
	}

	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)
{
	command_context_t *context;
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
	target_t *target;

	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
3031
	return 	target_mem2array(interp, target, argc-1, argv + 1);
3032
3033
3034
3035
}

static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
{
3036
	long l;
3037
	uint32_t width;
3038
	int len;
3039
3040
3041
	uint32_t addr;
	uint32_t count;
	uint32_t v;
3042
	const char *varname;
3043
	uint8_t buffer[4096];
3044
	int  n, e, retval;
3045
	uint32_t i;
3046
3047
3048

	/* argv[1] = name of array to receive the data
	 * argv[2] = desired width
oharboe's avatar
oharboe committed
3049
	 * argv[3] = memory address
3050
3051
	 * argv[4] = count of times to read
	 */
3052
	if (argc != 4) {
3053
3054
3055
		Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
		return JIM_ERR;
	}
3056
	varname = Jim_GetString(argv[0], &len);
3057
3058
	/* given "foo" get space for worse case "foo(%d)" .. add 20 */

3059
	e = Jim_GetLong(interp, argv[1], &l);
3060
3061
3062
3063
	width = l;
	if (e != JIM_OK) {
		return e;
	}
oharboe's avatar
oharboe committed
3064

3065
	e = Jim_GetLong(interp, argv[2], &l);
3066
3067
3068
3069
	addr = l;
	if (e != JIM_OK) {
		return e;
	}
3070
	e = Jim_GetLong(interp, argv[3], &l);
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
	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));
			Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
			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
3105
3106
	}

3107
3108
3109
3110
3111
3112
3113
	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));
duane's avatar
duane committed
3114
3115
3116
		sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads", 
				addr, 
				width);
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
		Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
		return JIM_ERR;
	}

	/* Transfer loop */

	/* index counter */
	n = 0;
	/* assume ok */
	e = JIM_OK;
	while (len) {
		/* Slurp... in buffer size chunks */
oharboe's avatar
oharboe committed
3129

3130
3131
3132
3133
		count = len; /* in objects.. */
		if (count > (sizeof(buffer)/width)) {
			count = (sizeof(buffer)/width);
		}
oharboe's avatar
oharboe committed
3134

zwelch's avatar
zwelch committed
3135
		retval = target_read_memory( target, addr, width, count, buffer );
3136
3137
		if (retval != ERROR_OK) {
			/* BOO !*/
duane's avatar
duane committed
3138
3139
3140
3141
			LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed", 
					  (unsigned int)addr, 
					  (int)width, 
					  (int)count);
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
			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
3165

3166
3167
3168
3169
3170
	Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));

	return JIM_OK;
}

3171
static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
{
	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)
{
	command_context_t *context;
3205
	target_t *target;
3206

3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
	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;
	}
3217

zwelch's avatar
zwelch committed
3218
	return target_array2mem( interp,target, argc-1, argv + 1 );
3219
}
3220

3221
3222
static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
{
3223
	long l;
3224
	uint32_t width;
3225
	int len;
3226
3227
3228
	uint32_t addr;
	uint32_t count;
	uint32_t v;
3229
	const char *varname;
3230
	uint8_t buffer[4096];
3231
	int  n, e, retval;
3232
	uint32_t i;
3233
3234
3235

	/* argv[1] = name of array to get the data
	 * argv[2] = desired width
oharboe's avatar
oharboe committed
3236
	 * argv[3] = memory address
3237
3238
	 * argv[4] = count to write
	 */
3239
	if (argc != 4) {
3240
3241
3242
		Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
		return JIM_ERR;
	}
3243
	varname = Jim_GetString(argv[0], &len);
3244
3245
	/* given "foo" get space for worse case "foo(%d)" .. add 20 */

3246
	e = Jim_GetLong(interp, argv[1], &l);
3247
3248
3249
3250
	width = l;
	if (e != JIM_OK) {
		return e;
	}
oharboe's avatar
oharboe committed
3251

3252
	e = Jim_GetLong(interp, argv[2], &l);
3253
3254
3255
3256
	addr = l;
	if (e != JIM_OK) {
		return e;
	}
3257
	e = Jim_GetLong(interp, argv[3], &l);
3258
3259
3260
3261
3262
3263
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
	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));
			Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
			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
3292
3293
	}

3294
3295
3296
3297
3298
3299
3300
	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));
duane's avatar
duane committed
3301
3302
3303
		sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads", 
				(unsigned int)addr, 
				(int)width);
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
		Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
		return JIM_ERR;
	}

	/* Transfer loop */

	/* index counter */
	n = 0;
	/* assume ok */
	e = JIM_OK;
	while (len) {
		/* Slurp... in buffer size chunks */
oharboe's avatar
oharboe committed
3316

3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
		count = len; /* in objects.. */
		if (count > (sizeof(buffer)/width)) {
			count = (sizeof(buffer)/width);
		}

		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
3339
		retval = target_write_memory(target, addr, width, count, buffer);
3340
3341
		if (retval != ERROR_OK) {
			/* BOO !*/
duane's avatar
duane committed
3342
3343
3344
3345
			LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed", 
					  (unsigned int)addr, 
					  (int)width, 
					  (int)count);
3346
			Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3347
			Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3348
3349
3350
3351
			e = JIM_ERR;
			len = 0;
		}
	}
oharboe's avatar
oharboe committed
3352

3353
3354
3355
3356
	Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));

	return JIM_OK;
}
3357

3358
void target_all_handle_event( enum target_event e )
3359
3360
3361
{
	target_t *target;

3362
	LOG_DEBUG( "**all*targets: event: %d, %s",
duane's avatar
duane committed
3363
3364
			   (int)e,
			   Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
3365
3366
3367
3368
3369
3370
3371
3372

	target = all_targets;
	while (target){
		target_handle_event( target, e );
		target = target->next;
	}
}

3373
void target_handle_event( target_t *target, enum target_event e )
3374
3375
3376
3377
3378
3379
3380
{
	target_event_action_t *teap;
	int done;

	teap = target->event_action;

	done = 0;
zwelch's avatar
zwelch committed
3381
	while ( teap ){
zwelch's avatar
zwelch committed
3382
		if ( teap->event == e ){
3383
3384
3385
3386
			done = 1;
			LOG_DEBUG( "target: (%d) %s (%s) event: %d (%s) action: %s\n",
					   target->target_number,
					   target->cmd_name,
zwelch's avatar
zwelch committed
3387
					   target_get_name(target),
3388
3389
3390
					   e,
					   Jim_Nvp_value2name_simple( nvp_target_event, e )->name,
					   Jim_GetString( teap->body, NULL ) );
zwelch's avatar
zwelch committed
3391
			if (Jim_EvalObj( interp, teap->body ) != JIM_OK)
3392
3393
3394
			{
				Jim_PrintErrorMessage(interp);
			}
3395
3396
3397
		}
		teap = teap->next;
	}
zwelch's avatar
zwelch committed
3398
	if ( !done ){
3399
		LOG_DEBUG( "event: %d %s - no action",
3400
3401
3402
3403
3404
3405
3406
				   e,
				   Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
	}
}

enum target_cfg_param {
	TCFG_TYPE,
3407
	TCFG_EVENT,
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
	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 },
3427

3428
3429
	{ .name = NULL, .value = -1 }
};
3430

3431
static int target_configure( Jim_GetOptInfo *goi, target_t *target )
3432
3433
3434
3435
3436
3437
3438
3439
{
	Jim_Nvp *n;
	Jim_Obj *o;
	jim_wide w;
	char *cp;
	int e;

	/* parse config or cget options ... */
zwelch's avatar
zwelch committed
3440
	while ( goi->argc > 0 ){
3441
		Jim_SetEmptyResult( goi->interp );
3442
		/* Jim_GetOpt_Debug( goi ); */
3443

zwelch's avatar
zwelch committed
3444
		if ( target->type->target_jim_configure ){
3445
3446
3447
			/* target defines a configure function */
			/* target gets first dibs on parameters */
			e = (*(target->type->target_jim_configure))( target, goi );
zwelch's avatar
zwelch committed
3448
			if ( e == JIM_OK ){
3449
3450
3451
				/* more? */
				continue;
			}
zwelch's avatar
zwelch committed
3452
			if ( e == JIM_ERR ){
3453
3454
3455
3456
3457
3458
				/* An error */
				return e;
			}
			/* otherwise we 'continue' below */
		}
		e = Jim_GetOpt_Nvp( goi, nvp_config_opts, &n );
zwelch's avatar
zwelch committed
3459
		if ( e != JIM_OK ){
3460
3461
3462
			Jim_GetOpt_NvpUnknown( goi, nvp_config_opts, 0 );
			return e;
		}
zwelch's avatar
zwelch committed
3463
		switch ( n->value ){
3464
3465
		case TCFG_TYPE:
			/* not setable */
zwelch's avatar
zwelch committed
3466
			if ( goi->isconfigure ){
3467
3468
3469
3470
				Jim_SetResult_sprintf( goi->interp, "not setable: %s", n->name );
				return JIM_ERR;
			} else {
			no_params:
zwelch's avatar
zwelch committed
3471
				if ( goi->argc != 0 ){
3472
3473
3474
3475
					Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "NO PARAMS");
					return JIM_ERR;
				}
			}
zwelch's avatar
zwelch committed
3476
			Jim_SetResultString( goi->interp, target_get_name(target), -1 );
3477
3478
3479
			/* loop for more */
			break;
		case TCFG_EVENT:
zwelch's avatar
zwelch committed
3480
			if ( goi->argc == 0 ){
3481
3482
3483
3484
3485
				Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
				return JIM_ERR;
			}

			e = Jim_GetOpt_Nvp( goi, nvp_target_event, &n );
zwelch's avatar
zwelch committed
3486
			if ( e != JIM_OK ){
3487
3488
3489
3490
				Jim_GetOpt_NvpUnknown( goi, nvp_target_event, 1 );
				return e;
			}

zwelch's avatar
zwelch committed
3491
3492
			if ( goi->isconfigure ){
				if ( goi->argc != 1 ){
3493
3494
					Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
					return JIM_ERR;
3495
				}
3496
			} else {
zwelch's avatar
zwelch committed
3497
				if ( goi->argc != 0 ){
3498
3499
3500
3501
3502
					Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
					return JIM_ERR;
				}
			}

3503
			{
3504
				target_event_action_t *teap;
3505

3506
3507
				teap = target->event_action;
				/* replace existing? */
zwelch's avatar
zwelch committed
3508
				while ( teap ){
zwelch's avatar
zwelch committed
3509
					if ( teap->event == (enum target_event)n->value ){
3510
3511
3512
3513
						break;
					}
					teap = teap->next;
				}
3514

zwelch's avatar
zwelch committed
3515
3516
				if ( goi->isconfigure ){
					if ( teap == NULL ){
3517
3518
3519
3520
3521
						/* create new */
						teap = calloc( 1, sizeof(*teap) );
					}
					teap->event = n->value;
					Jim_GetOpt_Obj( goi, &o );
zwelch's avatar
zwelch committed
3522
					if ( teap->body ){
3523
3524
3525
3526
						Jim_DecrRefCount( interp, teap->body );
					}
					teap->body  = Jim_DuplicateObj( goi->interp, o );
					/*
3527
					 * FIXME:
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
					 *     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.
					 */
					Jim_IncrRefCount( teap->body );

					/* add to head of event list */
					teap->next = target->event_action;
					target->event_action = teap;
					Jim_SetEmptyResult(goi->interp);
				} else {
					/* get */
zwelch's avatar
zwelch committed
3544
					if ( teap == NULL ){
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
						Jim_SetEmptyResult( goi->interp );
					} else {
						Jim_SetResult( goi->interp, Jim_DuplicateObj( goi->interp, teap->body ) );
					}
				}
			}
			/* loop for more */
			break;

		case TCFG_WORK_AREA_VIRT:
zwelch's avatar
zwelch committed
3555
			if ( goi->isconfigure ){
3556
3557
				target_free_all_working_areas(target);
				e = Jim_GetOpt_Wide( goi, &w );
zwelch's avatar
zwelch committed
3558
				if ( e != JIM_OK ){
3559
3560
3561
3562
					return e;
				}
				target->working_area_virt = w;
			} else {
zwelch's avatar
zwelch committed
3563
				if ( goi->argc != 0 ){
3564
3565
					goto no_params;
				}
3566
			}
3567
3568
3569
			Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_virt ) );
			/* loop for more */
			break;
3570

3571
		case TCFG_WORK_AREA_PHYS:
zwelch's avatar
zwelch committed
3572
			if ( goi->isconfigure ){
3573
3574
				target_free_all_working_areas(target);
				e = Jim_GetOpt_Wide( goi, &w );
zwelch's avatar
zwelch committed
3575
				if ( e != JIM_OK ){
3576
3577
3578
3579
					return e;
				}
				target->working_area_phys = w;
			} else {
zwelch's avatar
zwelch committed
3580
				if ( goi->argc != 0 ){
3581
3582
					goto no_params;
				}
3583
			}
3584
3585
3586
3587
3588
			Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_phys ) );
			/* loop for more */
			break;

		case TCFG_WORK_AREA_SIZE:
zwelch's avatar
zwelch committed
3589
			if ( goi->isconfigure ){
3590
3591
				target_free_all_working_areas(target);
				e = Jim_GetOpt_Wide( goi, &w );
zwelch's avatar
zwelch committed
3592
				if ( e != JIM_OK ){
3593
3594
3595
3596
					return e;
				}
				target->working_area_size = w;
			} else {
zwelch's avatar
zwelch committed
3597
				if ( goi->argc != 0 ){
3598
3599
					goto no_params;
				}
3600
			}
3601
3602
3603
3604
3605
			Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
			/* loop for more */
			break;

		case TCFG_WORK_AREA_BACKUP:
zwelch's avatar
zwelch committed
3606
			if ( goi->isconfigure ){
3607
3608
				target_free_all_working_areas(target);
				e = Jim_GetOpt_Wide( goi, &w );
zwelch's avatar
zwelch committed
3609
				if ( e != JIM_OK ){
3610
3611
3612
3613
3614
					return e;
				}
				/* make this exactly 1 or 0 */
				target->backup_working_area = (!!w);
			} else {
zwelch's avatar
zwelch committed
3615
				if ( goi->argc != 0 ){
3616
3617
					goto no_params;
				}
3618
			}
3619
			Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3620
3621
3622
3623
			/* loop for more e*/
			break;

		case TCFG_ENDIAN:
zwelch's avatar
zwelch committed
3624
			if ( goi->isconfigure ){
3625
				e = Jim_GetOpt_Nvp( goi, nvp_target_endian, &n );
zwelch's avatar
zwelch committed
3626
				if ( e != JIM_OK ){
3627
3628
3629
3630
3631
					Jim_GetOpt_NvpUnknown( goi, nvp_target_endian, 1 );
					return e;
				}
				target->endianness = n->value;
			} else {
zwelch's avatar
zwelch committed
3632
				if ( goi->argc != 0 ){
3633
3634
3635
3636
					goto no_params;
				}
			}
			n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
zwelch's avatar
zwelch committed
3637
			if ( n->name == NULL ){
3638
3639
				target->endianness = TARGET_LITTLE_ENDIAN;
				n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
3640
			}
3641
3642
3643
3644
3645
			Jim_SetResultString( goi->interp, n->name, -1 );
			/* loop for more */
			break;

		case TCFG_VARIANT:
zwelch's avatar
zwelch committed
3646
3647
			if ( goi->isconfigure ){
				if ( goi->argc < 1 ){
3648
3649
3650
3651
3652
					Jim_SetResult_sprintf( goi->interp,
										   "%s ?STRING?",
										   n->name );
					return JIM_ERR;
				}
zwelch's avatar
zwelch committed
3653
				if ( target->variant ){
3654
3655
3656
3657
3658
					free((void *)(target->variant));
				}
				e = Jim_GetOpt_String( goi, &cp, NULL );
				target->variant = strdup(cp);
			} else {
zwelch's avatar
zwelch committed
3659
				if ( goi->argc != 0 ){
3660
3661
					goto no_params;
				}
3662
			}
3663
3664
3665
3666
			Jim_SetResultString( goi->interp, target->variant,-1 );
			/* loop for more */
			break;
		case TCFG_CHAIN_POSITION:
zwelch's avatar
zwelch committed
3667
			if ( goi->isconfigure ){
3668
3669
				Jim_Obj *o;
				jtag_tap_t *tap;
3670
				target_free_all_working_areas(target);
3671
				e = Jim_GetOpt_Obj( goi, &o );
zwelch's avatar
zwelch committed
3672
				if ( e != JIM_OK ){
3673
3674
					return e;
				}
3675
				tap = jtag_tap_by_jim_obj( goi->interp, o );
zwelch's avatar
zwelch committed
3676
				if ( tap == NULL ){
3677
					return JIM_ERR;
3678
				}
3679
				/* make this exactly 1 or 0 */
3680
				target->tap = tap;
3681
			} else {
zwelch's avatar
zwelch committed
3682
				if ( goi->argc != 0 ){
3683
3684
					goto no_params;
				}
3685
			}
3686
			Jim_SetResultString( interp, target->tap->dotted_name, -1 );
3687
3688
3689
			/* loop for more e*/
			break;
		}
zwelch's avatar
zwelch committed
3690
	} /* while ( goi->argc ) */
3691
3692


3693
		/* done - we return */
3694
3695
3696
3697
	return JIM_OK;
}

/** this is the 'tcl' handler for the target specific command */
3698
static int tcl_target_func( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
3699
3700
3701
3702
{
	Jim_GetOptInfo goi;
	jim_wide a,b,c;
	int x,y,z;
3703
	uint8_t  target_buf[32];
3704
3705
3706
3707
3708
3709
3710
3711
	Jim_Nvp *n;
	target_t *target;
	struct command_context_s *cmd_ctx;
	int e;

	enum {
		TS_CMD_CONFIGURE,
		TS_CMD_CGET,
3712

3713
3714
3715
3716
		TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
		TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
		TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
		TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3717
		TS_CMD_EXAMINE,
3718
3719
3720
3721
3722
3723
		TS_CMD_POLL,
		TS_CMD_RESET,
		TS_CMD_HALT,
		TS_CMD_WAITSTATE,
		TS_CMD_EVENTLIST,
		TS_CMD_CURSTATE,
3724
		TS_CMD_INVOKE_EVENT,
3725
	};
3726

3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
	static const Jim_Nvp target_options[] = {
		{ .name = "configure", .value = TS_CMD_CONFIGURE },
		{ .name = "cget", .value = TS_CMD_CGET },
		{ .name = "mww", .value = TS_CMD_MWW },
		{ .name = "mwh", .value = TS_CMD_MWH },
		{ .name = "mwb", .value = TS_CMD_MWB },
		{ .name = "mdw", .value = TS_CMD_MDW },
		{ .name = "mdh", .value = TS_CMD_MDH },
		{ .name = "mdb", .value = TS_CMD_MDB },
		{ .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
		{ .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
		{ .name = "eventlist", .value = TS_CMD_EVENTLIST },
		{ .name = "curstate",  .value = TS_CMD_CURSTATE },

		{ .name = "arp_examine", .value = TS_CMD_EXAMINE },
		{ .name = "arp_poll", .value = TS_CMD_POLL },
		{ .name = "arp_reset", .value = TS_CMD_RESET },
		{ .name = "arp_halt", .value = TS_CMD_HALT },
		{ .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3746
		{ .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3747
3748
3749
3750
3751

		{ .name = NULL, .value = -1 },
	};

	/* go past the "command" */
zwelch's avatar
zwelch committed
3752
	Jim_GetOpt_Setup( &goi, interp, argc-1, argv + 1 );
3753
3754
3755
3756
3757
3758

	target = Jim_CmdPrivData( goi.interp );
	cmd_ctx = Jim_GetAssocData(goi.interp, "context");

	/* commands here are in an NVP table */
	e = Jim_GetOpt_Nvp( &goi, target_options, &n );
zwelch's avatar
zwelch committed
3759
	if ( e != JIM_OK ){
3760
3761
3762
		Jim_GetOpt_NvpUnknown( &goi, target_options, 0 );
		return e;
	}
3763
	/* Assume blank result */
3764
3765
	Jim_SetEmptyResult( goi.interp );

zwelch's avatar
zwelch committed
3766
	switch ( n->value ){
3767
	case TS_CMD_CONFIGURE:
zwelch's avatar
zwelch committed
3768
		if ( goi.argc < 2 ){
3769
3770
3771
3772
3773
3774
3775
			Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
			return JIM_ERR;
		}
		goi.isconfigure = 1;
		return target_configure( &goi, target );
	case TS_CMD_CGET:
		// some things take params
zwelch's avatar
zwelch committed
3776
		if ( goi.argc < 1 ){
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
			Jim_WrongNumArgs( goi.interp, 0, goi.argv, "missing: ?-option?");
			return JIM_ERR;
		}
		goi.isconfigure = 0;
		return target_configure( &goi, target );
		break;
	case TS_CMD_MWW:
	case TS_CMD_MWH:
	case TS_CMD_MWB:
		/* argv[0] = cmd
		 * argv[1] = address
		 * argv[2] = data
		 * argv[3] = optional count.
		 */
3791

zwelch's avatar
zwelch committed
3792
		if ( (goi.argc == 2) || (goi.argc == 3) ){
3793
3794
3795
3796
3797
3798
			/* all is well */
		} else {
		mwx_error:
			Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR DATA [COUNT]", n->name );
			return JIM_ERR;
		}
3799

3800
		e = Jim_GetOpt_Wide( &goi, &a );
zwelch's avatar
zwelch committed
3801
		if ( e != JIM_OK ){
3802
3803
3804
3805
			goto mwx_error;
		}

		e = Jim_GetOpt_Wide( &goi, &b );
zwelch's avatar
zwelch committed
3806
		if ( e != JIM_OK ){
3807
3808
			goto mwx_error;
		}
zwelch's avatar
zwelch committed
3809
		if (goi.argc == 3) {
3810
			e = Jim_GetOpt_Wide( &goi, &c );
zwelch's avatar
zwelch committed
3811
			if ( e != JIM_OK ){
3812
3813
3814
3815
3816
				goto mwx_error;
			}
		} else {
			c = 1;
		}
3817

zwelch's avatar
zwelch committed
3818
		switch ( n->value ){
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
		case TS_CMD_MWW:
			target_buffer_set_u32( target, target_buf, b );
			b = 4;
			break;
		case TS_CMD_MWH:
			target_buffer_set_u16( target, target_buf, b );
			b = 2;
			break;
		case TS_CMD_MWB:
			target_buffer_set_u8( target, target_buf, b );
			b = 1;
			break;
		}
zwelch's avatar
zwelch committed
3832
		for ( x = 0 ; x < c ; x++ ){
zwelch's avatar
zwelch committed
3833
			e = target_write_memory( target, a, b, 1, target_buf );
zwelch's avatar
zwelch committed
3834
			if ( e != ERROR_OK ){
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
				Jim_SetResult_sprintf( interp, "Error writing @ 0x%08x: %d\n", (int)(a), e );
				return JIM_ERR;
			}
			/* b = width */
			a = a + b;
		}
		return JIM_OK;
		break;

		/* display */
	case TS_CMD_MDW:
	case TS_CMD_MDH:
	case TS_CMD_MDB:
		/* argv[0] = command
		 * argv[1] = address
		 * argv[2] = optional count
		 */
zwelch's avatar
zwelch committed
3852
		if ( (goi.argc == 2) || (goi.argc == 3) ){
3853
3854
3855
3856
			Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR [COUNT]", n->name );
			return JIM_ERR;
		}
		e = Jim_GetOpt_Wide( &goi, &a );
zwelch's avatar
zwelch committed
3857
		if ( e != JIM_OK ){
3858
3859
			return JIM_ERR;
		}
zwelch's avatar
zwelch committed
3860
		if ( goi.argc ){
3861
			e = Jim_GetOpt_Wide( &goi, &c );
zwelch's avatar
zwelch committed
3862
			if ( e != JIM_OK ){
3863
3864
3865
3866
3867
3868
				return JIM_ERR;
			}
		} else {
			c = 1;
		}
		b = 1; /* shut up gcc */
zwelch's avatar
zwelch committed
3869
		switch ( n->value ){
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
		case TS_CMD_MDW:
			b =  4;
			break;
		case TS_CMD_MDH:
			b = 2;
			break;
		case TS_CMD_MDB:
			b = 1;
			break;
		}

		/* convert to "bytes" */
		c = c * b;
		/* count is now in 'BYTES' */
zwelch's avatar
zwelch committed
3884
		while ( c > 0 ){
3885
			y = c;
zwelch's avatar
zwelch committed
3886
			if ( y > 16 ){
3887
3888
				y = 16;
			}
zwelch's avatar
zwelch committed
3889
			e = target_read_memory( target, a, b, y / b, target_buf );
zwelch's avatar
zwelch committed
3890
			if ( e != ERROR_OK ){
3891
3892
3893
				Jim_SetResult_sprintf( interp, "error reading target @ 0x%08lx", (int)(a) );
				return JIM_ERR;
			}
3894

3895
			Jim_fprintf( interp, interp->cookie_stdout, "0x%08x ", (int)(a) );
zwelch's avatar
zwelch committed
3896
			switch ( b ){
3897
			case 4:
zwelch's avatar
zwelch committed
3898
				for ( x = 0 ; (x < 16) && (x < y) ; x += 4 ){
3899
3900
3901
					z = target_buffer_get_u32( target, &(target_buf[ x * 4 ]) );
					Jim_fprintf( interp, interp->cookie_stdout, "%08x ", (int)(z) );
				}
zwelch's avatar
zwelch committed
3902
				for ( ; (x < 16) ; x += 4 ){
3903
3904
3905
3906
					Jim_fprintf( interp, interp->cookie_stdout, "         " );
				}
				break;
			case 2:
zwelch's avatar
zwelch committed
3907
				for ( x = 0 ; (x < 16) && (x < y) ; x += 2 ){
3908
3909
3910
					z = target_buffer_get_u16( target, &(target_buf[ x * 2 ]) );
					Jim_fprintf( interp, interp->cookie_stdout, "%04x ", (int)(z) );
				}
zwelch's avatar
zwelch committed
3911
				for ( ; (x < 16) ; x += 2 ){
3912
3913
3914
3915
3916
					Jim_fprintf( interp, interp->cookie_stdout, "     " );
				}
				break;
			case 1:
			default:
zwelch's avatar
zwelch committed
3917
				for ( x = 0 ; (x < 16) && (x < y) ; x += 1 ){
3918
3919
3920
					z = target_buffer_get_u8( target, &(target_buf[ x * 4 ]) );
					Jim_fprintf( interp, interp->cookie_stdout, "%02x ", (int)(z) );
				}
zwelch's avatar
zwelch committed
3921
				for ( ; (x < 16) ; x += 1 ){
3922
3923
3924
3925
3926
					Jim_fprintf( interp, interp->cookie_stdout, "   " );
				}
				break;
			}
			/* ascii-ify the bytes */
zwelch's avatar
zwelch committed
3927
			for ( x = 0 ; x < y ; x++ ){
zwelch's avatar
zwelch committed
3928
				if ( (target_buf[x] >= 0x20) &&
3929
3930
3931
3932
3933
3934
3935
3936
					(target_buf[x] <= 0x7e) ){
					/* good */
				} else {
					/* smack it */
					target_buf[x] = '.';
				}
			}
			/* space pad  */
zwelch's avatar
zwelch committed
3937
			while ( x < 16 ){
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
				target_buf[x] = ' ';
				x++;
			}
			/* terminate */
			target_buf[16] = 0;
			/* print - with a newline */
			Jim_fprintf( interp, interp->cookie_stdout, "%s\n", target_buf );
			/* NEXT... */
			c -= 16;
			a += 16;
		}
		return JIM_OK;
	case TS_CMD_MEM2ARRAY:
		return target_mem2array( goi.interp, target, goi.argc, goi.argv );
		break;
	case TS_CMD_ARRAY2MEM:
		return target_array2mem( goi.interp, target, goi.argc, goi.argv );
		break;
	case TS_CMD_EXAMINE:
zwelch's avatar
zwelch committed
3957
		if ( goi.argc ){
3958
			Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3959
3960
			return JIM_ERR;
		}
zwelch's avatar
zwelch committed
3961
3962
		if (!target->tap->enabled)
			goto err_tap_disabled;
3963
		e = target->type->examine( target );
zwelch's avatar
zwelch committed
3964
		if ( e != ERROR_OK ){
3965
3966
3967
3968
3969
			Jim_SetResult_sprintf( interp, "examine-fails: %d", e );
			return JIM_ERR;
		}
		return JIM_OK;
	case TS_CMD_POLL:
zwelch's avatar
zwelch committed
3970
		if ( goi.argc ){
3971
			Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3972
3973
			return JIM_ERR;
		}
zwelch's avatar
zwelch committed
3974
3975
		if (!target->tap->enabled)
			goto err_tap_disabled;
zwelch's avatar
zwelch committed
3976
		if ( !(target_was_examined(target)) ){
3977
3978
3979
3980
			e = ERROR_TARGET_NOT_EXAMINED;
		} else {
			e = target->type->poll( target );
		}
zwelch's avatar
zwelch committed
3981
		if ( e != ERROR_OK ){
3982
3983
3984
3985
3986
3987
3988
			Jim_SetResult_sprintf( interp, "poll-fails: %d", e );
			return JIM_ERR;
		} else {
			return JIM_OK;
		}
		break;
	case TS_CMD_RESET:
zwelch's avatar
zwelch committed
3989
		if ( goi.argc != 2 ){
zwelch's avatar
zwelch committed
3990
			Jim_WrongNumArgs( interp, 2, argv, "t | f|assert | deassert BOOL");
3991
3992
3993
			return JIM_ERR;
		}
		e = Jim_GetOpt_Nvp( &goi, nvp_assert, &n );
zwelch's avatar
zwelch committed
3994
		if ( e != JIM_OK ){
3995
3996
3997
			Jim_GetOpt_NvpUnknown( &goi, nvp_assert, 1 );
			return e;
		}
3998
		/* the halt or not param */
3999
		e = Jim_GetOpt_Wide( &goi, &a);
zwelch's avatar
zwelch committed
4000
		if ( e != JIM_OK ){
For faster browsing, not all history is shown. View entire blame