target.c 113 KB
Newer Older
oharboe's avatar
oharboe committed
1001

1002
1003
1004
	/* mark as used, and return the new (reused) area */
	new_wa->free = 0;
	*area = new_wa;
oharboe's avatar
oharboe committed
1005

1006
1007
	/* user pointer */
	new_wa->user = area;
oharboe's avatar
oharboe committed
1008

1009
1010
1011
	return ERROR_OK;
}

oharboe's avatar
   
oharboe committed
1012
int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
1013
1014
1015
{
	if (area->free)
		return ERROR_OK;
oharboe's avatar
oharboe committed
1016

zwelch's avatar
zwelch committed
1017
	if (restore && target->backup_working_area)
1018
1019
	{
		int retval;
zwelch's avatar
zwelch committed
1020
		if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1021
1022
			return retval;
	}
oharboe's avatar
oharboe committed
1023

1024
	area->free = 1;
oharboe's avatar
oharboe committed
1025

1026
1027
1028
	/* mark user pointer invalid */
	*area->user = NULL;
	area->user = NULL;
oharboe's avatar
oharboe committed
1029

1030
1031
1032
	return ERROR_OK;
}

oharboe's avatar
   
oharboe committed
1033
1034
1035
1036
1037
int target_free_working_area(struct target_s *target, working_area_t *area)
{
	return target_free_working_area_restore(target, area, 1);
}

1038
1039
1040
1041
/* free resources and restore memory, if restoring memory fails,
 * free up resources anyway
 */
void target_free_all_working_areas_restore(struct target_s *target, int restore)
1042
1043
1044
1045
1046
1047
{
	working_area_t *c = target->working_areas;

	while (c)
	{
		working_area_t *next = c->next;
oharboe's avatar
   
oharboe committed
1048
		target_free_working_area_restore(target, c, restore);
oharboe's avatar
oharboe committed
1049

1050
1051
		if (c->backup)
			free(c->backup);
oharboe's avatar
oharboe committed
1052

1053
		free(c);
oharboe's avatar
oharboe committed
1054

1055
1056
		c = next;
	}
oharboe's avatar
oharboe committed
1057

1058
1059
1060
	target->working_areas = NULL;
}

1061
void target_free_all_working_areas(struct target_s *target)
oharboe's avatar
   
oharboe committed
1062
{
1063
	target_free_all_working_areas_restore(target, 1);
oharboe's avatar
   
oharboe committed
1064
1065
}

1066
1067
int target_register_commands(struct command_context_s *cmd_ctx)
{
1068

oharboe's avatar
oharboe committed
1069
	register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, "change the current command line target (one parameter) or lists targets (with no parameter)");
1070

1071
1072
1073



1074
	register_jim(cmd_ctx, "target", jim_target, "configure target");
1075

1076
1077
1078
1079
1080
1081
	return ERROR_OK;
}

int target_arch_state(struct target_s *target)
{
	int retval;
zwelch's avatar
zwelch committed
1082
	if (target == NULL)
1083
	{
1084
		LOG_USER("No target has been configured");
1085
1086
		return ERROR_OK;
	}
oharboe's avatar
oharboe committed
1087

1088
	LOG_USER("target state: %s",
1089
		 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name);
oharboe's avatar
oharboe committed
1090

zwelch's avatar
zwelch committed
1091
	if (target->state != TARGET_HALTED)
1092
		return ERROR_OK;
oharboe's avatar
oharboe committed
1093

zwelch's avatar
zwelch committed
1094
	retval = target->type->arch_state(target);
1095
1096
1097
	return retval;
}

oharboe's avatar
oharboe committed
1098
1099
/* Single aligned words are guaranteed to use 16 or 32 bit access
 * mode respectively, otherwise data is handled as quickly as
1100
1101
 * possible
 */
1102
int target_write_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1103
1104
{
	int retval;
duane's avatar
duane committed
1105
1106
	LOG_DEBUG("writing buffer of %i byte at 0x%8.8x", 
		  (int)size, (unsigned)address);
1107

1108
	if (!target_was_examined(target))
1109
1110
1111
1112
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1113

1114
1115
1116
1117
	if (size == 0) {
		return ERROR_OK;
	}

1118
	if ((address + size - 1) < address)
1119
1120
	{
		/* GDB can request this when e.g. PC is 0xfffffffc*/
zwelch's avatar
zwelch committed
1121
		LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)", 
duane's avatar
duane committed
1122
1123
				  (unsigned)address, 
				  (unsigned)size);
1124
1125
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1126

1127
1128
	if (((address % 2) == 0) && (size == 2))
	{
zwelch's avatar
zwelch committed
1129
		return target_write_memory(target, address, 2, 1, buffer);
1130
	}
oharboe's avatar
oharboe committed
1131

1132
1133
1134
	/* handle unaligned head bytes */
	if (address % 4)
	{
1135
		uint32_t unaligned = 4 - (address % 4);
oharboe's avatar
oharboe committed
1136

1137
1138
1139
		if (unaligned > size)
			unaligned = size;

zwelch's avatar
zwelch committed
1140
		if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1141
			return retval;
oharboe's avatar
oharboe committed
1142

1143
1144
1145
1146
		buffer += unaligned;
		address += unaligned;
		size -= unaligned;
	}
oharboe's avatar
oharboe committed
1147

1148
1149
1150
1151
	/* handle aligned words */
	if (size >= 4)
	{
		int aligned = size - (size % 4);
oharboe's avatar
oharboe committed
1152

1153
1154
1155
1156
1157
1158
1159
1160
		/* use bulk writes above a certain limit. This may have to be changed */
		if (aligned > 128)
		{
			if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
				return retval;
		}
		else
		{
zwelch's avatar
zwelch committed
1161
			if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1162
1163
				return retval;
		}
oharboe's avatar
oharboe committed
1164

1165
1166
1167
1168
		buffer += aligned;
		address += aligned;
		size -= aligned;
	}
oharboe's avatar
oharboe committed
1169

1170
1171
1172
	/* handle tail writes of less than 4 bytes */
	if (size > 0)
	{
zwelch's avatar
zwelch committed
1173
		if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1174
1175
			return retval;
	}
oharboe's avatar
oharboe committed
1176

1177
1178
1179
	return ERROR_OK;
}

oharboe's avatar
oharboe committed
1180
1181
/* Single aligned words are guaranteed to use 16 or 32 bit access
 * mode respectively, otherwise data is handled as quickly as
1182
1183
 * possible
 */
1184
int target_read_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1185
1186
{
	int retval;
duane's avatar
duane committed
1187
1188
	LOG_DEBUG("reading buffer of %i byte at 0x%8.8x", 
			  (int)size, (unsigned)address);
1189

1190
	if (!target_was_examined(target))
1191
1192
1193
1194
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1195

1196
1197
1198
1199
	if (size == 0) {
		return ERROR_OK;
	}

1200
	if ((address + size - 1) < address)
1201
1202
	{
		/* GDB can request this when e.g. PC is 0xfffffffc*/
zwelch's avatar
zwelch committed
1203
		LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")", 
duane's avatar
duane committed
1204
1205
				  address, 
				  size);
1206
1207
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1208

1209
1210
	if (((address % 2) == 0) && (size == 2))
	{
zwelch's avatar
zwelch committed
1211
		return target_read_memory(target, address, 2, 1, buffer);
1212
	}
oharboe's avatar
oharboe committed
1213

1214
1215
1216
	/* handle unaligned head bytes */
	if (address % 4)
	{
1217
		uint32_t unaligned = 4 - (address % 4);
oharboe's avatar
oharboe committed
1218

1219
1220
1221
		if (unaligned > size)
			unaligned = size;

zwelch's avatar
zwelch committed
1222
		if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1223
			return retval;
oharboe's avatar
oharboe committed
1224

1225
1226
1227
1228
		buffer += unaligned;
		address += unaligned;
		size -= unaligned;
	}
oharboe's avatar
oharboe committed
1229

1230
1231
1232
1233
	/* handle aligned words */
	if (size >= 4)
	{
		int aligned = size - (size % 4);
oharboe's avatar
oharboe committed
1234

zwelch's avatar
zwelch committed
1235
		if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1236
			return retval;
oharboe's avatar
oharboe committed
1237

1238
1239
1240
1241
		buffer += aligned;
		address += aligned;
		size -= aligned;
	}
oharboe's avatar
oharboe committed
1242

1243
1244
1245
	/* handle tail writes of less than 4 bytes */
	if (size > 0)
	{
zwelch's avatar
zwelch committed
1246
		if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1247
1248
			return retval;
	}
oharboe's avatar
oharboe committed
1249

1250
1251
1252
	return ERROR_OK;
}

1253
int target_checksum_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* crc)
1254
{
1255
	uint8_t *buffer;
1256
	int retval;
1257
1258
	uint32_t i;
	uint32_t checksum = 0;
1259
	if (!target_was_examined(target))
1260
1261
1262
1263
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1264

1265
	if ((retval = target->type->checksum_memory(target, address,
1266
		size, &checksum)) != ERROR_OK)
1267
1268
1269
1270
	{
		buffer = malloc(size);
		if (buffer == NULL)
		{
duane's avatar
duane committed
1271
			LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
			return ERROR_INVALID_ARGUMENTS;
		}
		retval = target_read_buffer(target, address, size, buffer);
		if (retval != ERROR_OK)
		{
			free(buffer);
			return retval;
		}

		/* convert to target endianess */
1282
		for (i = 0; i < (size/sizeof(uint32_t)); i++)
1283
		{
1284
1285
1286
			uint32_t target_data;
			target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
			target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1287
1288
		}

1289
		retval = image_calculate_checksum(buffer, size, &checksum);
1290
1291
		free(buffer);
	}
oharboe's avatar
oharboe committed
1292

1293
	*crc = checksum;
oharboe's avatar
oharboe committed
1294

1295
1296
	return retval;
}
1297

1298
int target_blank_check_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* blank)
1299
1300
{
	int retval;
1301
	if (!target_was_examined(target))
1302
1303
1304
1305
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1306

1307
1308
	if (target->type->blank_check_memory == 0)
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
oharboe's avatar
oharboe committed
1309

1310
	retval = target->type->blank_check_memory(target, address, size, blank);
oharboe's avatar
oharboe committed
1311

1312
1313
	return retval;
}
1314

1315
int target_read_u32(struct target_s *target, uint32_t address, uint32_t *value)
1316
{
1317
	uint8_t value_buf[4];
1318
	if (!target_was_examined(target))
1319
1320
1321
1322
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
1323

zwelch's avatar
zwelch committed
1324
	int retval = target_read_memory(target, address, 4, 1, value_buf);
oharboe's avatar
oharboe committed
1325

1326
1327
1328
	if (retval == ERROR_OK)
	{
		*value = target_buffer_get_u32(target, value_buf);
duane's avatar
duane committed
1329
1330
1331
		LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "", 
				  address, 
				  *value);
1332
1333
1334
1335
	}
	else
	{
		*value = 0x0;
duane's avatar
duane committed
1336
1337
		LOG_DEBUG("address: 0x%8.8" PRIx32 " failed", 
				  address);
1338
	}
oharboe's avatar
oharboe committed
1339

1340
1341
1342
	return retval;
}

1343
int target_read_u16(struct target_s *target, uint32_t address, uint16_t *value)
1344
{
1345
	uint8_t value_buf[2];
1346
	if (!target_was_examined(target))
1347
1348
1349
1350
1351
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}

zwelch's avatar
zwelch committed
1352
	int retval = target_read_memory(target, address, 2, 1, value_buf);
oharboe's avatar
oharboe committed
1353

1354
1355
1356
	if (retval == ERROR_OK)
	{
		*value = target_buffer_get_u16(target, value_buf);
duane's avatar
duane committed
1357
1358
1359
		LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x", 
				  address, 
				  *value);
1360
1361
1362
1363
	}
	else
	{
		*value = 0x0;
duane's avatar
duane committed
1364
1365
		LOG_DEBUG("address: 0x%8.8" PRIx32 " failed", 
				  address);
1366
	}
oharboe's avatar
oharboe committed
1367

1368
1369
1370
	return retval;
}

1371
int target_read_u8(struct target_s *target, uint32_t address, uint8_t *value)
1372
{
zwelch's avatar
zwelch committed
1373
	int retval = target_read_memory(target, address, 1, 1, value);
1374
	if (!target_was_examined(target))
1375
1376
1377
1378
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
1379
1380
1381

	if (retval == ERROR_OK)
	{
duane's avatar
duane committed
1382
1383
1384
		LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x", 
				  address, 
				  *value);
1385
1386
1387
1388
	}
	else
	{
		*value = 0x0;
duane's avatar
duane committed
1389
1390
		LOG_DEBUG("address: 0x%8.8" PRIx32 " failed", 
				  address);
1391
	}
oharboe's avatar
oharboe committed
1392

1393
1394
1395
	return retval;
}

1396
int target_write_u32(struct target_s *target, uint32_t address, uint32_t value)
1397
1398
{
	int retval;
1399
	uint8_t value_buf[4];
1400
	if (!target_was_examined(target))
1401
1402
1403
1404
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
1405

duane's avatar
duane committed
1406
1407
1408
	LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "", 
			  address, 
			  value);
1409

oharboe's avatar
oharboe committed
1410
	target_buffer_set_u32(target, value_buf, value);
zwelch's avatar
zwelch committed
1411
	if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1412
	{
1413
		LOG_DEBUG("failed: %i", retval);
1414
	}
oharboe's avatar
oharboe committed
1415

1416
1417
1418
	return retval;
}

1419
int target_write_u16(struct target_s *target, uint32_t address, uint16_t value)
1420
1421
{
	int retval;
1422
	uint8_t value_buf[2];
1423
	if (!target_was_examined(target))
1424
1425
1426
1427
1428
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}

duane's avatar
duane committed
1429
1430
1431
	LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x", 
			  address, 
			  value);
1432

oharboe's avatar
oharboe committed
1433
	target_buffer_set_u16(target, value_buf, value);
zwelch's avatar
zwelch committed
1434
	if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1435
	{
1436
		LOG_DEBUG("failed: %i", retval);
1437
	}
oharboe's avatar
oharboe committed
1438

1439
1440
1441
	return retval;
}

1442
int target_write_u8(struct target_s *target, uint32_t address, uint8_t value)
1443
1444
{
	int retval;
1445
	if (!target_was_examined(target))
1446
1447
1448
1449
1450
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}

duane's avatar
duane committed
1451
1452
	LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x", 
			  address, value);
1453

zwelch's avatar
zwelch committed
1454
	if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1455
	{
1456
		LOG_DEBUG("failed: %i", retval);
1457
	}
oharboe's avatar
oharboe committed
1458

1459
1460
1461
1462
1463
	return retval;
}

int target_register_user_commands(struct command_context_s *cmd_ctx)
{
1464
	int retval = ERROR_OK;
1465
1466
1467
1468


	/* script procedures */
	register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "profiling samples the CPU PC");
zwelch's avatar
zwelch committed
1469
1470
	register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array, "read memory and return as a TCL array for script processing <ARRAYNAME> <WIDTH = 32/16/8> <ADDRESS> <COUNT>");
	register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem, "convert a TCL array to memory locations and write the values  <ARRAYNAME> <WIDTH = 32/16/8> <ADDRESS> <COUNT>");
1471
1472
1473
1474
1475
1476
1477
1478
1479

	register_command(cmd_ctx, NULL, "fast_load_image", handle_fast_load_image_command, COMMAND_ANY,
			"same args as load_image, image stored in memory - mainly for profiling purposes");

	register_command(cmd_ctx, NULL, "fast_load", handle_fast_load_command, COMMAND_ANY,
			"loads active fast load image to current target - mainly for profiling purposes");


	register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "translate a virtual address into a physical address");
oharboe's avatar
oharboe committed
1480
	register_command(cmd_ctx,  NULL, "reg", handle_reg_command, COMMAND_EXEC, "display or set a register");
1481
1482
1483
1484
1485
	register_command(cmd_ctx,  NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
	register_command(cmd_ctx,  NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
	register_command(cmd_ctx,  NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
	register_command(cmd_ctx,  NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
	register_command(cmd_ctx,  NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
zwelch's avatar
zwelch committed
1486
	register_command(cmd_ctx,  NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run | halt | init] - default is run");
1487
1488
1489
1490
1491
	register_command(cmd_ctx,  NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");

	register_command(cmd_ctx,  NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
	register_command(cmd_ctx,  NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
	register_command(cmd_ctx,  NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
oharboe's avatar
oharboe committed
1492

oharboe's avatar
oharboe committed
1493
1494
1495
	register_command(cmd_ctx,  NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value> [count]");
	register_command(cmd_ctx,  NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value> [count]");
	register_command(cmd_ctx,  NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value> [count]");
oharboe's avatar
oharboe committed
1496
1497

	register_command(cmd_ctx,  NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");
1498
	register_command(cmd_ctx,  NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
oharboe's avatar
oharboe committed
1499
	register_command(cmd_ctx,  NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
1500
	register_command(cmd_ctx,  NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
oharboe's avatar
oharboe committed
1501

1502
	register_command(cmd_ctx,  NULL, "load_image", handle_load_image_command, COMMAND_EXEC, "load_image <file> <address> ['bin'|'ihex'|'elf'|'s19'] [min_address] [max_length]");
1503
1504
	register_command(cmd_ctx,  NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
	register_command(cmd_ctx,  NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
oharboe's avatar
oharboe committed
1505
	register_command(cmd_ctx,  NULL, "test_image", handle_test_image_command, COMMAND_EXEC, "test_image <file> [offset] [type]");
oharboe's avatar
oharboe committed
1506

zwelch's avatar
zwelch committed
1507
	if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
1508
		return retval;
zwelch's avatar
zwelch committed
1509
	if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
1510
		return retval;
oharboe's avatar
oharboe committed
1511

1512
	return retval;
1513
1514
}

1515
static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1516
{
1517
	target_t *target = all_targets;
oharboe's avatar
oharboe committed
1518

1519
1520
	if (argc == 1)
	{
1521
1522
		target = get_target(args[0]);
		if (target == NULL) {
1523
			command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0]);
1524
1525
			goto DumpTargets;
		}
oharboe's avatar
oharboe committed
1526
1527
1528
1529
1530
1531
		if (!target->tap->enabled) {
			command_print(cmd_ctx,"Target: TAP %s is disabled, "
					"can't be the current target\n",
					target->tap->dotted_name);
			return ERROR_FAIL;
		}
1532

1533
		cmd_ctx->current_target = target->target_number;
1534
1535
		return ERROR_OK;
	}
1536
DumpTargets:
oharboe's avatar
oharboe committed
1537

1538
	target = all_targets;
oharboe's avatar
oharboe committed
1539
1540
	command_print(cmd_ctx, "    TargetName         Type       Endian TapName            State       ");
	command_print(cmd_ctx, "--  ------------------ ---------- ------ ------------------ ------------");
1541
1542
	while (target)
	{
oharboe's avatar
oharboe committed
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
		const char *state;
		char marker = ' ';

		if (target->tap->enabled)
			state = Jim_Nvp_value2name_simple(nvp_target_state,
					target->state)->name;
		else
			state = "tap-disabled";

		if (cmd_ctx->current_target == target->target_number)
			marker = '*';

		/* keep columns lined up to match the headers above */
		command_print(cmd_ctx, "%2d%c %-18s %-10s %-6s %-18s %s",
1557
					  target->target_number,
oharboe's avatar
oharboe committed
1558
					  marker,
1559
					  target->cmd_name,
zwelch's avatar
zwelch committed
1560
					  target_get_name(target),
oharboe's avatar
oharboe committed
1561
1562
					  Jim_Nvp_value2name_simple(nvp_target_endian,
								target->endianness)->name,
1563
					  target->tap->dotted_name,
oharboe's avatar
oharboe committed
1564
					  state);
1565
1566
		target = target->next;
	}
oharboe's avatar
oharboe committed
1567

1568
1569
1570
	return ERROR_OK;
}

1571
/* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1572
1573
1574
1575

static int powerDropout;
static int srstAsserted;

1576
1577
1578
1579
1580
static int runPowerRestore;
static int runPowerDropout;
static int runSrstAsserted;
static int runSrstDeasserted;

1581
static int sense_handler(void)
1582
1583
1584
1585
1586
{
	static int prevSrstAsserted = 0;
	static int prevPowerdropout = 0;

	int retval;
zwelch's avatar
zwelch committed
1587
	if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1588
1589
1590
1591
1592
1593
		return retval;

	int powerRestored;
	powerRestored = prevPowerdropout && !powerDropout;
	if (powerRestored)
	{
1594
		runPowerRestore = 1;
1595
1596
1597
1598
1599
1600
1601
	}

	long long current = timeval_ms();
	static long long lastPower = 0;
	int waitMore = lastPower + 2000 > current;
	if (powerDropout && !waitMore)
	{
1602
		runPowerDropout = 1;
1603
1604
1605
		lastPower = current;
	}

zwelch's avatar
zwelch committed
1606
	if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1607
1608
1609
1610
1611
1612
1613
1614
1615
		return retval;

	int srstDeasserted;
	srstDeasserted = prevSrstAsserted && !srstAsserted;

	static long long lastSrst = 0;
	waitMore = lastSrst + 2000 > current;
	if (srstDeasserted && !waitMore)
	{
1616
		runSrstDeasserted = 1;
1617
1618
1619
1620
1621
		lastSrst = current;
	}

	if (!prevSrstAsserted && srstAsserted)
	{
1622
		runSrstAsserted = 1;
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
	}

	prevSrstAsserted = srstAsserted;
	prevPowerdropout = powerDropout;

	if (srstDeasserted || powerRestored)
	{
		/* Other than logging the event we can't do anything here.
		 * Issuing a reset is a particularly bad idea as we might
		 * be inside a reset already.
		 */
	}

	return ERROR_OK;
}

1639
1640
1641
/* process target state changes */
int handle_target(void *priv)
{
1642
	int retval = ERROR_OK;
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656

	/* we do not want to recurse here... */
	static int recursive = 0;
	if (! recursive)
	{
		recursive = 1;
		sense_handler();
		/* danger! running these procedures can trigger srst assertions and power dropouts.
		 * We need to avoid an infinite loop/recursion here and we do that by
		 * clearing the flags after running these events.
		 */
		int did_something = 0;
		if (runSrstAsserted)
		{
1657
			Jim_Eval(interp, "srst_asserted");
1658
1659
1660
1661
			did_something = 1;
		}
		if (runSrstDeasserted)
		{
1662
			Jim_Eval(interp, "srst_deasserted");
1663
1664
1665
1666
			did_something = 1;
		}
		if (runPowerDropout)
		{
1667
			Jim_Eval(interp, "power_dropout");
1668
1669
1670
1671
			did_something = 1;
		}
		if (runPowerRestore)
		{
1672
			Jim_Eval(interp, "power_restore");
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
			did_something = 1;
		}

		if (did_something)
		{
			/* clear detect flags */
			sense_handler();
		}

		/* clear action flags */

zwelch's avatar
zwelch committed
1684
1685
1686
1687
		runSrstAsserted = 0;
		runSrstDeasserted = 0;
		runPowerRestore = 0;
		runPowerDropout = 0;
1688
1689
1690
1691

		recursive = 0;
	}

zwelch's avatar
zwelch committed
1692
1693
1694
1695
1696
1697
	/* Poll targets for state changes unless that's globally disabled.
	 * Skip targets that are currently disabled.
	 */
	for (target_t *target = all_targets;
			target_continuous_poll && target;
			target = target->next)
1698
	{
zwelch's avatar
zwelch committed
1699
1700
		if (!target->tap->enabled)
			continue;
1701
1702

		/* only poll target if we've got power and srst isn't asserted */
zwelch's avatar
zwelch committed
1703
		if (!powerDropout && !srstAsserted)
1704
		{
1705
			/* polling may fail silently until the target has been examined */
zwelch's avatar
zwelch committed
1706
			if ((retval = target_poll(target)) != ERROR_OK)
1707
				return retval;
1708
1709
		}
	}
oharboe's avatar
oharboe committed
1710

1711
	return retval;
1712
1713
}

1714
static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1715
1716
1717
1718
1719
{
	target_t *target;
	reg_t *reg = NULL;
	int count = 0;
	char *value;
oharboe's avatar
oharboe committed
1720

1721
	LOG_DEBUG("-");
oharboe's avatar
oharboe committed
1722

1723
	target = get_current_target(cmd_ctx);
oharboe's avatar
oharboe committed
1724

1725
1726
1727
1728
	/* list all available registers for the current target */
	if (argc == 0)
	{
		reg_cache_t *cache = target->reg_cache;
oharboe's avatar
oharboe committed
1729

1730
		count = 0;
zwelch's avatar
zwelch committed
1731
		while (cache)
1732
1733
1734
1735
1736
		{
			int i;
			for (i = 0; i < cache->num_regs; i++)
			{
				value = buf_to_str(cache->reg_list[i].value, cache->reg_list[i].size, 16);
duane's avatar
duane committed
1737
1738
1739
1740
1741
1742
1743
				command_print(cmd_ctx, "(%i) %s (/%i): 0x%s (dirty: %i, valid: %i)", 
							  count++, 
							  cache->reg_list[i].name, 
							  (int)(cache->reg_list[i].size),
							  value, 
							  cache->reg_list[i].dirty, 
							  cache->reg_list[i].valid);
1744
1745
1746
1747
				free(value);
			}
			cache = cache->next;
		}
oharboe's avatar
oharboe committed
1748

1749
1750
		return ERROR_OK;
	}
oharboe's avatar
oharboe committed
1751

1752
1753
1754
	/* access a single register by its ordinal number */
	if ((args[0][0] >= '0') && (args[0][0] <= '9'))
	{
1755
1756
1757
1758
		unsigned num;
		int retval = parse_uint(args[0], &num);
		if (ERROR_OK != retval)
			return ERROR_COMMAND_SYNTAX_ERROR;
oharboe's avatar
oharboe committed
1759

1760
		reg_cache_t *cache = target->reg_cache;
1761
		count = 0;
zwelch's avatar
zwelch committed
1762
		while (cache)
1763
1764
1765
1766
		{
			int i;
			for (i = 0; i < cache->num_regs; i++)
			{
1767
				if (count++ == (int)num)
1768
1769
1770
1771
1772
1773
1774
1775
1776
				{
					reg = &cache->reg_list[i];
					break;
				}
			}
			if (reg)
				break;
			cache = cache->next;
		}
oharboe's avatar
oharboe committed
1777

1778
1779
1780
1781
1782
1783
1784
1785
		if (!reg)
		{
			command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
			return ERROR_OK;
		}
	} else /* access a single register by its name */
	{
		reg = register_get_by_name(target->reg_cache, args[0], 1);
oharboe's avatar
oharboe committed
1786

1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
		if (!reg)
		{
			command_print(cmd_ctx, "register %s not found in current target", args[0]);
			return ERROR_OK;
		}
	}

	/* display a register */
	if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
	{
		if ((argc == 2) && (strcmp(args[1], "force") == 0))
			reg->valid = 0;
oharboe's avatar
oharboe committed
1799

1800
1801
1802
1803
1804
1805
		if (reg->valid == 0)
		{
			reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
			arch_type->get(reg);
		}
		value = buf_to_str(reg->value, reg->size, 16);
duane's avatar
duane committed
1806
		command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1807
1808
1809
		free(value);
		return ERROR_OK;
	}
oharboe's avatar
oharboe committed
1810

1811
1812
1813
	/* set register value */
	if (argc == 2)
	{
1814
		uint8_t *buf = malloc(CEIL(reg->size, 8));
1815
1816
1817
1818
		str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);

		reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
		arch_type->set(reg, buf);
oharboe's avatar
oharboe committed
1819

1820
		value = buf_to_str(reg->value, reg->size, 16);
duane's avatar
duane committed
1821
		command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1822
		free(value);
oharboe's avatar
oharboe committed
1823

1824
		free(buf);
oharboe's avatar
oharboe committed
1825

1826
1827
		return ERROR_OK;
	}
oharboe's avatar
oharboe committed
1828

1829
	command_print(cmd_ctx, "usage: reg <#|name> [value]");
oharboe's avatar
oharboe committed
1830

1831
1832
1833
	return ERROR_OK;
}

1834
static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1835
{
1836
	int retval = ERROR_OK;
1837
1838
1839
1840
	target_t *target = get_current_target(cmd_ctx);

	if (argc == 0)
	{
1841
		command_print(cmd_ctx, "background polling: %s",
zwelch's avatar
zwelch committed
1842
1843
1844
1845
1846
1847
				target_continuous_poll ?  "on" : "off");
		command_print(cmd_ctx, "TAP: %s (%s)",
				target->tap->dotted_name,
				target->tap->enabled ? "enabled" : "disabled");
		if (!target->tap->enabled)
			return ERROR_OK;
1848
		if ((retval = target_poll(target)) != ERROR_OK)
1849
			return retval;
1850
		if ((retval = target_arch_state(target)) != ERROR_OK)
1851
1852
			return retval;

1853
	}
zwelch's avatar
zwelch committed
1854
	else if (argc == 1)
1855
1856
1857
	{
		if (strcmp(args[0], "on") == 0)
		{
zwelch's avatar
zwelch committed
1858
			target_continuous_poll = 1;
1859
1860
1861
		}
		else if (strcmp(args[0], "off") == 0)
		{
zwelch's avatar
zwelch committed
1862
			target_continuous_poll = 0;
1863
1864
1865
1866
1867
		}
		else
		{
			command_print(cmd_ctx, "arg is \"on\" or \"off\"");
		}
1868
1869
1870
	} else
	{
		return ERROR_COMMAND_SYNTAX_ERROR;
1871
	}
oharboe's avatar
oharboe committed
1872

1873
	return retval;
1874
1875
}

1876
static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1877
{
1878
1879
	if (argc > 1)
		return ERROR_COMMAND_SYNTAX_ERROR;
oharboe's avatar
oharboe committed
1880

1881
1882
	unsigned ms = 5000;
	if (1 == argc)
1883
	{
1884
1885
		int retval = parse_uint(args[0], &ms);
		if (ERROR_OK != retval)
1886
1887
		{
			command_print(cmd_ctx, "usage: %s [seconds]", cmd);
1888
			return ERROR_COMMAND_SYNTAX_ERROR;
1889
		}
1890
1891
		// convert seconds (given) to milliseconds (needed)
		ms *= 1000;
1892
1893
	}

1894
	target_t *target = get_current_target(cmd_ctx);
oharboe's avatar
oharboe committed
1895
	return target_wait_state(target, TARGET_HALTED, ms);
1896
1897
}

1898
1899
1900
1901
1902
1903
/* wait for target state to change. The trick here is to have a low
 * latency for short waits and not to suck up all the CPU time
 * on longer waits.
 *
 * After 500ms, keep_alive() is invoked
 */
1904
int target_wait_state(target_t *target, enum target_state state, int ms)
1905
1906
{
	int retval;
zwelch's avatar
zwelch committed
1907
1908
	long long then = 0, cur;
	int once = 1;
oharboe's avatar