target.c 109 KB
Newer Older
zwelch's avatar
zwelch committed
1001
		if((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1002
1003
			return retval;
	}
oharboe's avatar
oharboe committed
1004

1005
	area->free = 1;
oharboe's avatar
oharboe committed
1006

1007
1008
1009
	/* mark user pointer invalid */
	*area->user = NULL;
	area->user = NULL;
oharboe's avatar
oharboe committed
1010

1011
1012
1013
	return ERROR_OK;
}

oharboe's avatar
   
oharboe committed
1014
1015
1016
1017
1018
int target_free_working_area(struct target_s *target, working_area_t *area)
{
	return target_free_working_area_restore(target, area, 1);
}

1019
1020
1021
1022
/* 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)
1023
1024
1025
1026
1027
1028
{
	working_area_t *c = target->working_areas;

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

1031
1032
		if (c->backup)
			free(c->backup);
oharboe's avatar
oharboe committed
1033

1034
		free(c);
oharboe's avatar
oharboe committed
1035

1036
1037
		c = next;
	}
oharboe's avatar
oharboe committed
1038

1039
1040
1041
	target->working_areas = NULL;
}

1042
void target_free_all_working_areas(struct target_s *target)
oharboe's avatar
   
oharboe committed
1043
{
1044
	target_free_all_working_areas_restore(target, 1);
oharboe's avatar
   
oharboe committed
1045
1046
}

1047
1048
int target_register_commands(struct command_context_s *cmd_ctx)
{
1049

oharboe's avatar
oharboe committed
1050
	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)");
1051

1052
1053
1054



1055
	register_jim(cmd_ctx, "target", jim_target, "configure target" );
1056

1057
1058
1059
1060
1061
1062
1063
1064
	return ERROR_OK;
}

int target_arch_state(struct target_s *target)
{
	int retval;
	if (target==NULL)
	{
1065
		LOG_USER("No target has been configured");
1066
1067
		return ERROR_OK;
	}
oharboe's avatar
oharboe committed
1068

1069
	LOG_USER("target state: %s",
1070
		 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name);
oharboe's avatar
oharboe committed
1071

1072
1073
	if (target->state!=TARGET_HALTED)
		return ERROR_OK;
oharboe's avatar
oharboe committed
1074

1075
1076
1077
1078
	retval=target->type->arch_state(target);
	return retval;
}

oharboe's avatar
oharboe committed
1079
1080
/* Single aligned words are guaranteed to use 16 or 32 bit access
 * mode respectively, otherwise data is handled as quickly as
1081
1082
1083
1084
1085
 * possible
 */
int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
{
	int retval;
1086
1087
	LOG_DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);

1088
	if (!target_was_examined(target))
1089
1090
1091
1092
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1093

1094
1095
1096
1097
	if (size == 0) {
		return ERROR_OK;
	}

1098
	if ((address + size - 1) < address)
1099
1100
1101
1102
1103
	{
		/* GDB can request this when e.g. PC is 0xfffffffc*/
		LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1104

1105
1106
	if (((address % 2) == 0) && (size == 2))
	{
zwelch's avatar
zwelch committed
1107
		return target_write_memory(target, address, 2, 1, buffer);
1108
	}
oharboe's avatar
oharboe committed
1109

1110
1111
1112
	/* handle unaligned head bytes */
	if (address % 4)
	{
1113
		u32 unaligned = 4 - (address % 4);
oharboe's avatar
oharboe committed
1114

1115
1116
1117
		if (unaligned > size)
			unaligned = size;

zwelch's avatar
zwelch committed
1118
		if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1119
			return retval;
oharboe's avatar
oharboe committed
1120

1121
1122
1123
1124
		buffer += unaligned;
		address += unaligned;
		size -= unaligned;
	}
oharboe's avatar
oharboe committed
1125

1126
1127
1128
1129
	/* handle aligned words */
	if (size >= 4)
	{
		int aligned = size - (size % 4);
oharboe's avatar
oharboe committed
1130

1131
1132
1133
1134
1135
1136
1137
1138
		/* 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
1139
			if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1140
1141
				return retval;
		}
oharboe's avatar
oharboe committed
1142

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

1148
1149
1150
	/* handle tail writes of less than 4 bytes */
	if (size > 0)
	{
zwelch's avatar
zwelch committed
1151
		if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1152
1153
			return retval;
	}
oharboe's avatar
oharboe committed
1154

1155
1156
1157
	return ERROR_OK;
}

oharboe's avatar
oharboe committed
1158
1159
/* Single aligned words are guaranteed to use 16 or 32 bit access
 * mode respectively, otherwise data is handled as quickly as
1160
1161
1162
1163
1164
 * possible
 */
int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
{
	int retval;
1165
1166
	LOG_DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);

1167
	if (!target_was_examined(target))
1168
1169
1170
1171
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1172

1173
1174
1175
1176
	if (size == 0) {
		return ERROR_OK;
	}

1177
	if ((address + size - 1) < address)
1178
1179
1180
1181
1182
	{
		/* GDB can request this when e.g. PC is 0xfffffffc*/
		LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1183

1184
1185
	if (((address % 2) == 0) && (size == 2))
	{
zwelch's avatar
zwelch committed
1186
		return target_read_memory(target, address, 2, 1, buffer);
1187
	}
oharboe's avatar
oharboe committed
1188

1189
1190
1191
	/* handle unaligned head bytes */
	if (address % 4)
	{
1192
		u32 unaligned = 4 - (address % 4);
oharboe's avatar
oharboe committed
1193

1194
1195
1196
		if (unaligned > size)
			unaligned = size;

zwelch's avatar
zwelch committed
1197
		if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1198
			return retval;
oharboe's avatar
oharboe committed
1199

1200
1201
1202
1203
		buffer += unaligned;
		address += unaligned;
		size -= unaligned;
	}
oharboe's avatar
oharboe committed
1204

1205
1206
1207
1208
	/* handle aligned words */
	if (size >= 4)
	{
		int aligned = size - (size % 4);
oharboe's avatar
oharboe committed
1209

zwelch's avatar
zwelch committed
1210
		if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1211
			return retval;
oharboe's avatar
oharboe committed
1212

1213
1214
1215
1216
		buffer += aligned;
		address += aligned;
		size -= aligned;
	}
oharboe's avatar
oharboe committed
1217

1218
1219
1220
	/* handle tail writes of less than 4 bytes */
	if (size > 0)
	{
zwelch's avatar
zwelch committed
1221
		if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1222
1223
			return retval;
	}
oharboe's avatar
oharboe committed
1224

1225
1226
1227
1228
1229
1230
1231
	return ERROR_OK;
}

int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32* crc)
{
	u8 *buffer;
	int retval;
1232
	u32 i;
1233
	u32 checksum = 0;
1234
	if (!target_was_examined(target))
1235
1236
1237
1238
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1239

1240
	if ((retval = target->type->checksum_memory(target, address,
1241
		size, &checksum)) != ERROR_OK)
1242
1243
1244
1245
	{
		buffer = malloc(size);
		if (buffer == NULL)
		{
1246
			LOG_ERROR("error allocating buffer for section (%d bytes)", size);
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
			return ERROR_INVALID_ARGUMENTS;
		}
		retval = target_read_buffer(target, address, size, buffer);
		if (retval != ERROR_OK)
		{
			free(buffer);
			return retval;
		}

		/* convert to target endianess */
		for (i = 0; i < (size/sizeof(u32)); i++)
		{
			u32 target_data;
			target_data = target_buffer_get_u32(target, &buffer[i*sizeof(u32)]);
			target_buffer_set_u32(target, &buffer[i*sizeof(u32)], target_data);
		}

		retval = image_calculate_checksum( buffer, size, &checksum );
		free(buffer);
	}
oharboe's avatar
oharboe committed
1267

1268
	*crc = checksum;
oharboe's avatar
oharboe committed
1269

1270
1271
	return retval;
}
1272
1273
1274
1275

int target_blank_check_memory(struct target_s *target, u32 address, u32 size, u32* blank)
{
	int retval;
1276
	if (!target_was_examined(target))
1277
1278
1279
1280
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1281

1282
1283
	if (target->type->blank_check_memory == 0)
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
oharboe's avatar
oharboe committed
1284

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

1287
1288
	return retval;
}
1289
1290
1291
1292

int target_read_u32(struct target_s *target, u32 address, u32 *value)
{
	u8 value_buf[4];
1293
	if (!target_was_examined(target))
1294
1295
1296
1297
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
1298

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

1301
1302
1303
	if (retval == ERROR_OK)
	{
		*value = target_buffer_get_u32(target, value_buf);
1304
		LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, *value);
1305
1306
1307
1308
	}
	else
	{
		*value = 0x0;
1309
		LOG_DEBUG("address: 0x%8.8x failed", address);
1310
	}
oharboe's avatar
oharboe committed
1311

1312
1313
1314
1315
1316
1317
	return retval;
}

int target_read_u16(struct target_s *target, u32 address, u16 *value)
{
	u8 value_buf[2];
1318
	if (!target_was_examined(target))
1319
1320
1321
1322
1323
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}

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

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

1337
1338
1339
1340
1341
	return retval;
}

int target_read_u8(struct target_s *target, u32 address, u8 *value)
{
zwelch's avatar
zwelch committed
1342
	int retval = target_read_memory(target, address, 1, 1, value);
1343
	if (!target_was_examined(target))
1344
1345
1346
1347
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
1348
1349
1350

	if (retval == ERROR_OK)
	{
1351
		LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, *value);
1352
1353
1354
1355
	}
	else
	{
		*value = 0x0;
1356
		LOG_DEBUG("address: 0x%8.8x failed", address);
1357
	}
oharboe's avatar
oharboe committed
1358

1359
1360
1361
1362
1363
1364
1365
	return retval;
}

int target_write_u32(struct target_s *target, u32 address, u32 value)
{
	int retval;
	u8 value_buf[4];
1366
	if (!target_was_examined(target))
1367
1368
1369
1370
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
1371

1372
	LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1373

oharboe's avatar
oharboe committed
1374
	target_buffer_set_u32(target, value_buf, value);
zwelch's avatar
zwelch committed
1375
	if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1376
	{
1377
		LOG_DEBUG("failed: %i", retval);
1378
	}
oharboe's avatar
oharboe committed
1379

1380
1381
1382
1383
1384
1385
1386
	return retval;
}

int target_write_u16(struct target_s *target, u32 address, u16 value)
{
	int retval;
	u8 value_buf[2];
1387
	if (!target_was_examined(target))
1388
1389
1390
1391
1392
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}

1393
	LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1394

oharboe's avatar
oharboe committed
1395
	target_buffer_set_u16(target, value_buf, value);
zwelch's avatar
zwelch committed
1396
	if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1397
	{
1398
		LOG_DEBUG("failed: %i", retval);
1399
	}
oharboe's avatar
oharboe committed
1400

1401
1402
1403
1404
1405
1406
	return retval;
}

int target_write_u8(struct target_s *target, u32 address, u8 value)
{
	int retval;
1407
	if (!target_was_examined(target))
1408
1409
1410
1411
1412
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}

1413
	LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value);
1414

zwelch's avatar
zwelch committed
1415
	if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1416
	{
1417
		LOG_DEBUG("failed: %i", retval);
1418
	}
oharboe's avatar
oharboe committed
1419

1420
1421
1422
1423
1424
	return retval;
}

int target_register_user_commands(struct command_context_s *cmd_ctx)
{
1425
	int retval = ERROR_OK;
1426
1427
1428
1429


	/* script procedures */
	register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "profiling samples the CPU PC");
1430
1431
	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>");
1432
1433
1434
1435
1436
1437
1438
1439
1440

	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
1441
	register_command(cmd_ctx,  NULL, "reg", handle_reg_command, COMMAND_EXEC, "display or set a register");
1442
1443
1444
1445
1446
	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]");
1447
	register_command(cmd_ctx,  NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run|halt|init] - default is run");
1448
1449
1450
1451
1452
	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
1453

oharboe's avatar
oharboe committed
1454
1455
1456
	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
1457
1458

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

1463
	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]");
1464
1465
	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
1466
	register_command(cmd_ctx,  NULL, "test_image", handle_test_image_command, COMMAND_EXEC, "test_image <file> [offset] [type]");
oharboe's avatar
oharboe committed
1467

1468
1469
1470
1471
	if((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
		return retval;
	if((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
		return retval;
oharboe's avatar
oharboe committed
1472

1473
	return retval;
1474
1475
}

1476
static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1477
{
1478
	target_t *target = all_targets;
oharboe's avatar
oharboe committed
1479

1480
1481
	if (argc == 1)
	{
1482
1483
		target = get_target(args[0]);
		if (target == NULL) {
1484
1485
1486
			command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0] );
			goto DumpTargets;
		}
oharboe's avatar
oharboe committed
1487
1488
1489
1490
1491
1492
		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;
		}
1493

1494
		cmd_ctx->current_target = target->target_number;
1495
1496
		return ERROR_OK;
	}
1497
DumpTargets:
oharboe's avatar
oharboe committed
1498

1499
	target = all_targets;
oharboe's avatar
oharboe committed
1500
1501
	command_print(cmd_ctx, "    TargetName         Type       Endian TapName            State       ");
	command_print(cmd_ctx, "--  ------------------ ---------- ------ ------------------ ------------");
1502
1503
	while (target)
	{
oharboe's avatar
oharboe committed
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
		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",
1518
					  target->target_number,
oharboe's avatar
oharboe committed
1519
					  marker,
1520
					  target->cmd_name,
zwelch's avatar
zwelch committed
1521
					  target_get_name(target),
oharboe's avatar
oharboe committed
1522
1523
					  Jim_Nvp_value2name_simple(nvp_target_endian,
								target->endianness)->name,
1524
					  target->tap->dotted_name,
oharboe's avatar
oharboe committed
1525
					  state);
1526
1527
		target = target->next;
	}
oharboe's avatar
oharboe committed
1528

1529
1530
1531
	return ERROR_OK;
}

1532
/* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1533
1534
1535
1536

static int powerDropout;
static int srstAsserted;

1537
1538
1539
1540
1541
static int runPowerRestore;
static int runPowerDropout;
static int runSrstAsserted;
static int runSrstDeasserted;

1542
static int sense_handler(void)
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
{
	static int prevSrstAsserted = 0;
	static int prevPowerdropout = 0;

	int retval;
	if ((retval=jtag_power_dropout(&powerDropout))!=ERROR_OK)
		return retval;

	int powerRestored;
	powerRestored = prevPowerdropout && !powerDropout;
	if (powerRestored)
	{
1555
		runPowerRestore = 1;
1556
1557
1558
1559
1560
1561
1562
	}

	long long current = timeval_ms();
	static long long lastPower = 0;
	int waitMore = lastPower + 2000 > current;
	if (powerDropout && !waitMore)
	{
1563
		runPowerDropout = 1;
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
		lastPower = current;
	}

	if ((retval=jtag_srst_asserted(&srstAsserted))!=ERROR_OK)
		return retval;

	int srstDeasserted;
	srstDeasserted = prevSrstAsserted && !srstAsserted;

	static long long lastSrst = 0;
	waitMore = lastSrst + 2000 > current;
	if (srstDeasserted && !waitMore)
	{
1577
		runSrstDeasserted = 1;
1578
1579
1580
1581
1582
		lastSrst = current;
	}

	if (!prevSrstAsserted && srstAsserted)
	{
1583
		runSrstAsserted = 1;
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
	}

	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;
}

1600
1601
1602
/* process target state changes */
int handle_target(void *priv)
{
1603
	int retval = ERROR_OK;
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652

	/* 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)
		{
			Jim_Eval( interp, "srst_asserted");
			did_something = 1;
		}
		if (runSrstDeasserted)
		{
			Jim_Eval( interp, "srst_deasserted");
			did_something = 1;
		}
		if (runPowerDropout)
		{
			Jim_Eval( interp, "power_dropout");
			did_something = 1;
		}
		if (runPowerRestore)
		{
			Jim_Eval( interp, "power_restore");
			did_something = 1;
		}

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

		/* clear action flags */

		runSrstAsserted=0;
		runSrstDeasserted=0;
		runPowerRestore=0;
		runPowerDropout=0;

		recursive = 0;
	}

1653
	target_t *target = all_targets;
oharboe's avatar
oharboe committed
1654

1655
1656
	while (target)
	{
1657
1658
1659

		/* only poll target if we've got power and srst isn't asserted */
		if (target_continous_poll&&!powerDropout&&!srstAsserted)
1660
		{
1661
			/* polling may fail silently until the target has been examined */
1662
1663
			if((retval = target_poll(target)) != ERROR_OK)
				return retval;
1664
		}
oharboe's avatar
oharboe committed
1665

1666
1667
		target = target->next;
	}
oharboe's avatar
oharboe committed
1668

1669
	return retval;
1670
1671
}

1672
static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1673
1674
1675
1676
1677
{
	target_t *target;
	reg_t *reg = NULL;
	int count = 0;
	char *value;
oharboe's avatar
oharboe committed
1678

1679
	LOG_DEBUG("-");
oharboe's avatar
oharboe committed
1680

1681
	target = get_current_target(cmd_ctx);
oharboe's avatar
oharboe committed
1682

1683
1684
1685
1686
	/* list all available registers for the current target */
	if (argc == 0)
	{
		reg_cache_t *cache = target->reg_cache;
oharboe's avatar
oharboe committed
1687

1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
		count = 0;
		while(cache)
		{
			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);
				command_print(cmd_ctx, "(%i) %s (/%i): 0x%s (dirty: %i, valid: %i)", count++, cache->reg_list[i].name, cache->reg_list[i].size, value, cache->reg_list[i].dirty, cache->reg_list[i].valid);
				free(value);
			}
			cache = cache->next;
		}
oharboe's avatar
oharboe committed
1700

1701
1702
		return ERROR_OK;
	}
oharboe's avatar
oharboe committed
1703

1704
1705
1706
	/* access a single register by its ordinal number */
	if ((args[0][0] >= '0') && (args[0][0] <= '9'))
	{
1707
1708
1709
1710
		unsigned num;
		int retval = parse_uint(args[0], &num);
		if (ERROR_OK != retval)
			return ERROR_COMMAND_SYNTAX_ERROR;
oharboe's avatar
oharboe committed
1711

1712
		reg_cache_t *cache = target->reg_cache;
1713
1714
1715
1716
1717
1718
		count = 0;
		while(cache)
		{
			int i;
			for (i = 0; i < cache->num_regs; i++)
			{
1719
				if (count++ == (int)num)
1720
1721
1722
1723
1724
1725
1726
1727
1728
				{
					reg = &cache->reg_list[i];
					break;
				}
			}
			if (reg)
				break;
			cache = cache->next;
		}
oharboe's avatar
oharboe committed
1729

1730
1731
1732
1733
1734
1735
1736
1737
		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
1738

1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
		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
1751

1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
		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);
		command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
		free(value);
		return ERROR_OK;
	}
oharboe's avatar
oharboe committed
1762

1763
1764
1765
1766
1767
1768
1769
1770
	/* set register value */
	if (argc == 2)
	{
		u8 *buf = malloc(CEIL(reg->size, 8));
		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
1771

1772
1773
1774
		value = buf_to_str(reg->value, reg->size, 16);
		command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
		free(value);
oharboe's avatar
oharboe committed
1775

1776
		free(buf);
oharboe's avatar
oharboe committed
1777

1778
1779
		return ERROR_OK;
	}
oharboe's avatar
oharboe committed
1780

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

1783
1784
1785
	return ERROR_OK;
}

1786
static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1787
{
1788
	int retval = ERROR_OK;
1789
1790
1791
1792
	target_t *target = get_current_target(cmd_ctx);

	if (argc == 0)
	{
1793
1794
1795
		command_print(cmd_ctx, "background polling: %s",
				target_continous_poll ?  "on" : "off");
		if ((retval = target_poll(target)) != ERROR_OK)
1796
			return retval;
1797
		if ((retval = target_arch_state(target)) != ERROR_OK)
1798
1799
			return retval;

1800
	}
1801
	else if (argc==1)
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
	{
		if (strcmp(args[0], "on") == 0)
		{
			target_continous_poll = 1;
		}
		else if (strcmp(args[0], "off") == 0)
		{
			target_continous_poll = 0;
		}
		else
		{
			command_print(cmd_ctx, "arg is \"on\" or \"off\"");
		}
1815
1816
1817
	} else
	{
		return ERROR_COMMAND_SYNTAX_ERROR;
1818
	}
oharboe's avatar
oharboe committed
1819

1820
	return retval;
1821
1822
}

1823
static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1824
{
1825
1826
	if (argc > 1)
		return ERROR_COMMAND_SYNTAX_ERROR;
oharboe's avatar
oharboe committed
1827

1828
1829
	unsigned ms = 5000;
	if (1 == argc)
1830
	{
1831
1832
		int retval = parse_uint(args[0], &ms);
		if (ERROR_OK != retval)
1833
1834
		{
			command_print(cmd_ctx, "usage: %s [seconds]", cmd);
1835
			return ERROR_COMMAND_SYNTAX_ERROR;
1836
		}
1837
1838
		// convert seconds (given) to milliseconds (needed)
		ms *= 1000;
1839
1840
	}

1841
	target_t *target = get_current_target(cmd_ctx);
oharboe's avatar
oharboe committed
1842
	return target_wait_state(target, TARGET_HALTED, ms);
1843
1844
}

1845
1846
1847
1848
1849
1850
/* 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
 */
1851
int target_wait_state(target_t *target, enum target_state state, int ms)
1852
1853
{
	int retval;
1854
	long long then=0, cur;
1855
	int once=1;
oharboe's avatar
oharboe committed
1856

1857
1858
	for (;;)
	{
1859
		if ((retval=target_poll(target))!=ERROR_OK)
1860
1861
1862
1863
1864
			return retval;
		if (target->state == state)
		{
			break;
		}
1865
		cur = timeval_ms();
1866
1867
1868
		if (once)
		{
			once=0;
1869
			then = timeval_ms();
1870
			LOG_DEBUG("waiting for target %s...",
1871
				Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1872
		}
oharboe's avatar
oharboe committed
1873

1874
1875
1876
1877
1878
1879
		if (cur-then>500)
		{
			keep_alive();
		}

		if ((cur-then)>ms)
1880
		{
1881
			LOG_ERROR("timed out while waiting for target %s",
1882
				Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
oharboe's avatar
oharboe committed
1883
			return ERROR_FAIL;
1884
1885
		}
	}
oharboe's avatar
oharboe committed
1886

1887
1888
1889
	return ERROR_OK;
}

1890
static int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1891
{
1892
	LOG_DEBUG("-");
1893

1894
1895
1896
	target_t *target = get_current_target(cmd_ctx);
	int retval = target_halt(target);
	if (ERROR_OK != retval)
1897
		return retval;
oharboe's avatar
oharboe committed
1898

1899
1900
	if (argc == 1)
	{
1901
1902
1903
1904
1905
		unsigned wait;
		retval = parse_uint(args[0], &wait);
		if (ERROR_OK != retval)
			return ERROR_COMMAND_SYNTAX_ERROR;
		if (!wait)
1906
1907
1908
			return ERROR_OK;
	}

1909
1910
1911
	return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
}

1912
static int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1913
1914
{
	target_t *target = get_current_target(cmd_ctx);
oharboe's avatar
oharboe committed
1915

1916
	LOG_USER("requesting target halt and executing a soft reset");
oharboe's avatar
oharboe committed
1917

oharboe's avatar
   
oharboe committed
1918
	target->type->soft_reset_halt(target);
oharboe's avatar
oharboe committed
1919

1920
1921
1922
	return ERROR_OK;
}

1923
static int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1924
{
1925
1926
	if (argc > 1)
		return ERROR_COMMAND_SYNTAX_ERROR;
oharboe's avatar
oharboe committed
1927

1928
1929
	enum target_reset_mode reset_mode = RESET_RUN;
	if (argc == 1)
1930
	{
1931
		const Jim_Nvp *n;
1932
1933
		n = Jim_Nvp_name2value_simple( nvp_reset_modes, args[0] );
		if( (n->name == NULL) || (n->value == RESET_UNKNOWN) ){
oharboe's avatar
oharboe committed
1934
			return ERROR_COMMAND_SYNTAX_ERROR;
1935
		}
1936
		reset_mode = n->value;
1937
	}
oharboe's avatar
oharboe committed
1938

1939
	/* reset *all* targets */
1940
	return target_process_reset(cmd_ctx, reset_mode);
1941
1942
}

1943

1944
static int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1945
{
zwelch's avatar
zwelch committed
1946
1947
	if (argc > 1)
		return ERROR_COMMAND_SYNTAX_ERROR;
oharboe's avatar
oharboe committed
1948

zwelch's avatar
zwelch committed
1949
1950
	target_t *target = get_current_target(cmd_ctx);
	target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
oharboe's avatar
oharboe committed
1951

zwelch's avatar
zwelch committed
1952
1953
1954
1955
1956
	/* with no args, resume from current pc, addr = 0,
	 * with one arguments, addr = args[0],
	 * handle breakpoints, not debugging */
	u32 addr = 0;
	if (argc == 1)
1957
1958
1959
1960
1961
	{
		int retval = parse_u32(args[0], &addr);
		if (ERROR_OK != retval)
			return retval;
	}
oharboe's avatar
oharboe committed
1962

zwelch's avatar
zwelch committed
1963
	return target_resume(target, 0, addr, 1, 0);
1964
1965
}

1966
static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1967
{
zwelch's avatar
zwelch committed
1968
1969
	if (argc > 1)
		return ERROR_COMMAND_SYNTAX_ERROR;
oharboe's avatar
oharboe committed
1970

1971
	LOG_DEBUG("-");
oharboe's avatar
oharboe committed
1972

zwelch's avatar
zwelch committed
1973
1974
1975
1976
	/* with no args, step from current pc, addr = 0,
	 * with one argument addr = args[0],
	 * handle breakpoints, debugging */
	u32 addr = 0;