target.c 113 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
	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));
3087
			Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
			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

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

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
	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));
3274
			Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
			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
			   (int)e,
3364
			   Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3365
3366
3367

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

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 ... */
3440
3441
3442
	while (goi->argc > 0){
		Jim_SetEmptyResult(goi->interp);
		/* Jim_GetOpt_Debug(goi); */
3443

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

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

3491
3492
			if (goi->isconfigure){
				if (goi->argc != 1){
3493
					Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3494
					return JIM_ERR;
3495
				}
3496
			} else {
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? */
3508
3509
				while (teap){
					if (teap->event == (enum target_event)n->value){
3510
3511
3512
3513
						break;
					}
					teap = teap->next;
				}
3514

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

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

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

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

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

		case TCFG_WORK_AREA_BACKUP:
3606
			if (goi->isconfigure){
3607
				target_free_all_working_areas(target);
3608
3609
				e = Jim_GetOpt_Wide(goi, &w);
				if (e != JIM_OK){
3610
3611
3612
3613
3614
					return e;
				}
				/* make this exactly 1 or 0 */
				target->backup_working_area = (!!w);
			} else {
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:
3624
3625
3626
3627
			if (goi->isconfigure){
				e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
				if (e != JIM_OK){
					Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3628
3629
3630
3631
					return e;
				}
				target->endianness = n->value;
			} else {
3632
				if (goi->argc != 0){
3633
3634
3635
					goto no_params;
				}
			}
3636
3637
			n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
			if (n->name == NULL){
3638
				target->endianness = TARGET_LITTLE_ENDIAN;
3639
				n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3640
			}
3641
			Jim_SetResultString(goi->interp, n->name, -1);
3642
3643
3644
3645
			/* loop for more */
			break;

		case TCFG_VARIANT:
3646
3647
			if (goi->isconfigure){
				if (goi->argc < 1){
3648
					Jim_SetResult_sprintf(goi->interp,
3649
										   "%s ?STRING?",
3650
										   n->name);
3651
3652
					return JIM_ERR;
				}
3653
				if (target->variant){
3654
3655
					free((void *)(target->variant));
				}
3656
				e = Jim_GetOpt_String(goi, &cp, NULL);
3657
3658
				target->variant = strdup(cp);
			} else {
3659
				if (goi->argc != 0){
3660
3661
					goto no_params;
				}
3662
			}
3663
			Jim_SetResultString(goi->interp, target->variant,-1);
3664
3665
3666
			/* loop for more */
			break;
		case TCFG_CHAIN_POSITION:
3667
			if (goi->isconfigure){
3668
3669
				Jim_Obj *o;
				jtag_tap_t *tap;
3670
				target_free_all_working_areas(target);
3671
3672
				e = Jim_GetOpt_Obj(goi, &o);
				if (e != JIM_OK){
3673
3674
					return e;
				}
3675
3676
				tap = jtag_tap_by_jim_obj(goi->interp, o);
				if (tap == NULL){
3677
					return JIM_ERR;
3678
				}
3679
				/* make this exactly 1 or 0 */
3680
				target->tap = tap;
3681
			} else {
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;
		}
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" */
3752
	Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3753

3754
	target = Jim_CmdPrivData(goi.interp);
3755
3756
3757
	cmd_ctx = Jim_GetAssocData(goi.interp, "context");

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

3766
	switch (n->value){
3767
	case TS_CMD_CONFIGURE:
3768
		if (goi.argc < 2){
3769
			Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3770
3771
3772
			return JIM_ERR;
		}
		goi.isconfigure = 1;
3773
		return target_configure(&goi, target);
3774
3775
	case TS_CMD_CGET:
		// some things take params
3776
		if (goi.argc < 1){
3777
			Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
3778
3779
3780
			return JIM_ERR;
		}
		goi.isconfigure = 0;
3781
		return target_configure(&goi, target);
3782
3783
3784
3785
3786
3787
3788
3789
3790
		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

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

3800
3801
		e = Jim_GetOpt_Wide(&goi, &a);
		if (e != JIM_OK){
3802
3803
3804
			goto mwx_error;
		}

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

3818
		switch (n->value){
3819
		case TS_CMD_MWW:
3820
			target_buffer_set_u32(target, target_buf, b);
3821
3822
3823
			b = 4;
			break;
		case TS_CMD_MWH:
3824
			target_buffer_set_u16(target, target_buf, b);
3825
3826
3827
			b = 2;
			break;
		case TS_CMD_MWB:
3828
			target_buffer_set_u8(target, target_buf, b);
3829
3830
3831
			b = 1;
			break;
		}
3832
3833
3834
3835
		for (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);
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
				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
		 */
3852
3853
		if ((goi.argc == 2) || (goi.argc == 3)){
			Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
3854
3855
			return JIM_ERR;
		}
3856
3857
		e = Jim_GetOpt_Wide(&goi, &a);
		if (e != JIM_OK){
3858
3859
			return JIM_ERR;
		}
3860
3861
3862
		if (goi.argc){
			e = Jim_GetOpt_Wide(&goi, &c);
			if (e != JIM_OK){
3863
3864
3865
3866
3867
3868
				return JIM_ERR;
			}
		} else {
			c = 1;
		}
		b = 1; /* shut up gcc */
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' */
3884
		while (c > 0){
3885
			y = c;
3886
			if (y > 16){
3887
3888
				y = 16;
			}
3889
3890
3891
			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));
3892
3893
				return JIM_ERR;
			}
3894

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