target.c 117 KB
Newer Older
1001
1002
1003
1004
1005
1006
1007
1008
	{
		if ((c->free) && (c->size == size))
		{
			new_wa = c;
			break;
		}
		c = c->next;
	}
oharboe's avatar
oharboe committed
1009

1010
1011
1012
1013
	/* if not, allocate a new one */
	if (!new_wa)
	{
		working_area_t **p = &target->working_areas;
1014
1015
		uint32_t first_free = target->working_area;
		uint32_t free_size = target->working_area_size;
oharboe's avatar
oharboe committed
1016

1017
		LOG_DEBUG("allocating new working area");
oharboe's avatar
oharboe committed
1018

1019
1020
1021
1022
1023
1024
1025
1026
		c = target->working_areas;
		while (c)
		{
			first_free += c->size;
			free_size -= c->size;
			p = &c->next;
			c = c->next;
		}
oharboe's avatar
oharboe committed
1027

1028
1029
		if (free_size < size)
		{
1030
			LOG_WARNING("not enough working area available(requested %u, free %u)",
duane's avatar
duane committed
1031
				    (unsigned)(size), (unsigned)(free_size));
1032
1033
			return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
		}
oharboe's avatar
oharboe committed
1034

1035
1036
1037
1038
		new_wa = malloc(sizeof(working_area_t));
		new_wa->next = NULL;
		new_wa->size = size;
		new_wa->address = first_free;
oharboe's avatar
oharboe committed
1039

1040
1041
		if (target->backup_working_area)
		{
1042
			int retval;
1043
			new_wa->backup = malloc(new_wa->size);
zwelch's avatar
zwelch committed
1044
			if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1045
1046
1047
1048
1049
			{
				free(new_wa->backup);
				free(new_wa);
				return retval;
			}
1050
1051
1052
1053
1054
		}
		else
		{
			new_wa->backup = NULL;
		}
oharboe's avatar
oharboe committed
1055

1056
1057
1058
		/* put new entry in list */
		*p = new_wa;
	}
oharboe's avatar
oharboe committed
1059

1060
1061
1062
	/* mark as used, and return the new (reused) area */
	new_wa->free = 0;
	*area = new_wa;
oharboe's avatar
oharboe committed
1063

1064
1065
	/* user pointer */
	new_wa->user = area;
oharboe's avatar
oharboe committed
1066

1067
1068
1069
	return ERROR_OK;
}

oharboe's avatar
   
oharboe committed
1070
int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
1071
1072
1073
{
	if (area->free)
		return ERROR_OK;
oharboe's avatar
oharboe committed
1074

zwelch's avatar
zwelch committed
1075
	if (restore && target->backup_working_area)
1076
1077
	{
		int retval;
zwelch's avatar
zwelch committed
1078
		if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1079
1080
			return retval;
	}
oharboe's avatar
oharboe committed
1081

1082
	area->free = 1;
oharboe's avatar
oharboe committed
1083

1084
1085
1086
	/* mark user pointer invalid */
	*area->user = NULL;
	area->user = NULL;
oharboe's avatar
oharboe committed
1087

1088
1089
1090
	return ERROR_OK;
}

oharboe's avatar
   
oharboe committed
1091
1092
1093
1094
1095
int target_free_working_area(struct target_s *target, working_area_t *area)
{
	return target_free_working_area_restore(target, area, 1);
}

1096
1097
1098
1099
/* 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)
1100
1101
1102
1103
1104
1105
{
	working_area_t *c = target->working_areas;

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

1108
1109
		if (c->backup)
			free(c->backup);
oharboe's avatar
oharboe committed
1110

1111
		free(c);
oharboe's avatar
oharboe committed
1112

1113
1114
		c = next;
	}
oharboe's avatar
oharboe committed
1115

1116
1117
1118
	target->working_areas = NULL;
}

1119
void target_free_all_working_areas(struct target_s *target)
oharboe's avatar
   
oharboe committed
1120
{
1121
	target_free_all_working_areas_restore(target, 1);
oharboe's avatar
   
oharboe committed
1122
1123
}

1124
1125
int target_register_commands(struct command_context_s *cmd_ctx)
{
1126

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

1129
1130
1131



1132
	register_jim(cmd_ctx, "target", jim_target, "configure target");
1133

1134
1135
1136
1137
1138
1139
	return ERROR_OK;
}

int target_arch_state(struct target_s *target)
{
	int retval;
zwelch's avatar
zwelch committed
1140
	if (target == NULL)
1141
	{
1142
		LOG_USER("No target has been configured");
1143
1144
		return ERROR_OK;
	}
oharboe's avatar
oharboe committed
1145

1146
	LOG_USER("target state: %s", target_state_name( target ));
oharboe's avatar
oharboe committed
1147

zwelch's avatar
zwelch committed
1148
	if (target->state != TARGET_HALTED)
1149
		return ERROR_OK;
oharboe's avatar
oharboe committed
1150

zwelch's avatar
zwelch committed
1151
	retval = target->type->arch_state(target);
1152
1153
1154
	return retval;
}

oharboe's avatar
oharboe committed
1155
1156
/* Single aligned words are guaranteed to use 16 or 32 bit access
 * mode respectively, otherwise data is handled as quickly as
1157
1158
 * possible
 */
1159
int target_write_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1160
1161
{
	int retval;
1162
	LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
duane's avatar
duane committed
1163
		  (int)size, (unsigned)address);
1164

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

1171
1172
1173
1174
	if (size == 0) {
		return ERROR_OK;
	}

1175
	if ((address + size - 1) < address)
1176
1177
	{
		/* GDB can request this when e.g. PC is 0xfffffffc*/
1178
1179
		LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
				  (unsigned)address,
duane's avatar
duane committed
1180
				  (unsigned)size);
1181
1182
		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_write_memory(target, address, 2, 1, buffer);
1187
	}
oharboe's avatar
oharboe committed
1188

1189
1190
1191
	/* handle unaligned head bytes */
	if (address % 4)
	{
1192
		uint32_t 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_write_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

1210
1211
1212
1213
1214
1215
1216
1217
		/* 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
1218
			if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1219
1220
				return retval;
		}
oharboe's avatar
oharboe committed
1221

1222
1223
1224
1225
		buffer += aligned;
		address += aligned;
		size -= aligned;
	}
oharboe's avatar
oharboe committed
1226

1227
1228
1229
	/* handle tail writes of less than 4 bytes */
	if (size > 0)
	{
zwelch's avatar
zwelch committed
1230
		if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1231
1232
			return retval;
	}
oharboe's avatar
oharboe committed
1233

1234
1235
1236
	return ERROR_OK;
}

oharboe's avatar
oharboe committed
1237
1238
/* Single aligned words are guaranteed to use 16 or 32 bit access
 * mode respectively, otherwise data is handled as quickly as
1239
1240
 * possible
 */
1241
int target_read_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1242
1243
{
	int retval;
1244
	LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
duane's avatar
duane committed
1245
			  (int)size, (unsigned)address);
1246

1247
	if (!target_was_examined(target))
1248
1249
1250
1251
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1252

1253
1254
1255
1256
	if (size == 0) {
		return ERROR_OK;
	}

1257
	if ((address + size - 1) < address)
1258
1259
	{
		/* GDB can request this when e.g. PC is 0xfffffffc*/
1260
1261
		LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
				  address,
duane's avatar
duane committed
1262
				  size);
1263
1264
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1265

1266
1267
	if (((address % 2) == 0) && (size == 2))
	{
zwelch's avatar
zwelch committed
1268
		return target_read_memory(target, address, 2, 1, buffer);
1269
	}
oharboe's avatar
oharboe committed
1270

1271
1272
1273
	/* handle unaligned head bytes */
	if (address % 4)
	{
1274
		uint32_t unaligned = 4 - (address % 4);
oharboe's avatar
oharboe committed
1275

1276
1277
1278
		if (unaligned > size)
			unaligned = size;

zwelch's avatar
zwelch committed
1279
		if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1280
			return retval;
oharboe's avatar
oharboe committed
1281

1282
1283
1284
1285
		buffer += unaligned;
		address += unaligned;
		size -= unaligned;
	}
oharboe's avatar
oharboe committed
1286

1287
1288
1289
1290
	/* handle aligned words */
	if (size >= 4)
	{
		int aligned = size - (size % 4);
oharboe's avatar
oharboe committed
1291

zwelch's avatar
zwelch committed
1292
		if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1293
			return retval;
oharboe's avatar
oharboe committed
1294

1295
1296
1297
1298
		buffer += aligned;
		address += aligned;
		size -= aligned;
	}
1299

1300
1301
1302
1303
1304
1305
1306
	/*prevent byte access when possible (avoid AHB access limitations in some cases)*/
	if(size	>=2)
	{
		int aligned = size - (size%2);
		retval = target_read_memory(target, address, 2, aligned / 2, buffer);
		if (retval != ERROR_OK)
			return retval;
oharboe's avatar
oharboe committed
1307

1308
1309
1310
1311
		buffer += aligned;
		address += aligned;
		size -= aligned;
	}
1312
1313
1314
	/* handle tail writes of less than 4 bytes */
	if (size > 0)
	{
zwelch's avatar
zwelch committed
1315
		if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1316
1317
			return retval;
	}
oharboe's avatar
oharboe committed
1318

1319
1320
1321
	return ERROR_OK;
}

1322
int target_checksum_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* crc)
1323
{
1324
	uint8_t *buffer;
1325
	int retval;
1326
1327
	uint32_t i;
	uint32_t checksum = 0;
1328
	if (!target_was_examined(target))
1329
1330
1331
1332
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1333

1334
	if ((retval = target->type->checksum_memory(target, address,
1335
		size, &checksum)) != ERROR_OK)
1336
1337
1338
1339
	{
		buffer = malloc(size);
		if (buffer == NULL)
		{
duane's avatar
duane committed
1340
			LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
			return ERROR_INVALID_ARGUMENTS;
		}
		retval = target_read_buffer(target, address, size, buffer);
		if (retval != ERROR_OK)
		{
			free(buffer);
			return retval;
		}

		/* convert to target endianess */
1351
		for (i = 0; i < (size/sizeof(uint32_t)); i++)
1352
		{
1353
1354
1355
			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);
1356
1357
		}

1358
		retval = image_calculate_checksum(buffer, size, &checksum);
1359
1360
		free(buffer);
	}
oharboe's avatar
oharboe committed
1361

1362
	*crc = checksum;
oharboe's avatar
oharboe committed
1363

1364
1365
	return retval;
}
1366

1367
int target_blank_check_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* blank)
1368
1369
{
	int retval;
1370
	if (!target_was_examined(target))
1371
1372
1373
1374
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1375

1376
1377
	if (target->type->blank_check_memory == 0)
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
oharboe's avatar
oharboe committed
1378

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

1381
1382
	return retval;
}
1383

1384
int target_read_u32(struct target_s *target, uint32_t address, uint32_t *value)
1385
{
1386
	uint8_t value_buf[4];
1387
	if (!target_was_examined(target))
1388
1389
1390
1391
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
1392

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

1395
1396
1397
	if (retval == ERROR_OK)
	{
		*value = target_buffer_get_u32(target, value_buf);
1398
1399
		LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
				  address,
duane's avatar
duane committed
1400
				  *value);
1401
1402
1403
1404
	}
	else
	{
		*value = 0x0;
1405
		LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
duane's avatar
duane committed
1406
				  address);
1407
	}
oharboe's avatar
oharboe committed
1408

1409
1410
1411
	return retval;
}

1412
int target_read_u16(struct target_s *target, uint32_t address, uint16_t *value)
1413
{
1414
	uint8_t value_buf[2];
1415
	if (!target_was_examined(target))
1416
1417
1418
1419
1420
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}

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

1423
1424
1425
	if (retval == ERROR_OK)
	{
		*value = target_buffer_get_u16(target, value_buf);
1426
1427
		LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
				  address,
duane's avatar
duane committed
1428
				  *value);
1429
1430
1431
1432
	}
	else
	{
		*value = 0x0;
1433
		LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
duane's avatar
duane committed
1434
				  address);
1435
	}
oharboe's avatar
oharboe committed
1436

1437
1438
1439
	return retval;
}

1440
int target_read_u8(struct target_s *target, uint32_t address, uint8_t *value)
1441
{
zwelch's avatar
zwelch committed
1442
	int retval = target_read_memory(target, address, 1, 1, value);
1443
	if (!target_was_examined(target))
1444
1445
1446
1447
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
1448
1449
1450

	if (retval == ERROR_OK)
	{
1451
1452
		LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
				  address,
duane's avatar
duane committed
1453
				  *value);
1454
1455
1456
1457
	}
	else
	{
		*value = 0x0;
1458
		LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
duane's avatar
duane committed
1459
				  address);
1460
	}
oharboe's avatar
oharboe committed
1461

1462
1463
1464
	return retval;
}

1465
int target_write_u32(struct target_s *target, uint32_t address, uint32_t value)
1466
1467
{
	int retval;
1468
	uint8_t value_buf[4];
1469
	if (!target_was_examined(target))
1470
1471
1472
1473
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
1474

1475
1476
	LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
			  address,
duane's avatar
duane committed
1477
			  value);
1478

oharboe's avatar
oharboe committed
1479
	target_buffer_set_u32(target, value_buf, value);
zwelch's avatar
zwelch committed
1480
	if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1481
	{
1482
		LOG_DEBUG("failed: %i", retval);
1483
	}
oharboe's avatar
oharboe committed
1484

1485
1486
1487
	return retval;
}

1488
int target_write_u16(struct target_s *target, uint32_t address, uint16_t value)
1489
1490
{
	int retval;
1491
	uint8_t value_buf[2];
1492
	if (!target_was_examined(target))
1493
1494
1495
1496
1497
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}

1498
1499
	LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
			  address,
duane's avatar
duane committed
1500
			  value);
1501

oharboe's avatar
oharboe committed
1502
	target_buffer_set_u16(target, value_buf, value);
zwelch's avatar
zwelch committed
1503
	if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1504
	{
1505
		LOG_DEBUG("failed: %i", retval);
1506
	}
oharboe's avatar
oharboe committed
1507

1508
1509
1510
	return retval;
}

1511
int target_write_u8(struct target_s *target, uint32_t address, uint8_t value)
1512
1513
{
	int retval;
1514
	if (!target_was_examined(target))
1515
1516
1517
1518
1519
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}

1520
	LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
duane's avatar
duane committed
1521
			  address, value);
1522

zwelch's avatar
zwelch committed
1523
	if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1524
	{
1525
		LOG_DEBUG("failed: %i", retval);
1526
	}
oharboe's avatar
oharboe committed
1527

1528
1529
1530
1531
1532
	return retval;
}

int target_register_user_commands(struct command_context_s *cmd_ctx)
{
1533
	int retval = ERROR_OK;
1534
1535
1536
1537


	/* script procedures */
	register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "profiling samples the CPU PC");
zwelch's avatar
zwelch committed
1538
1539
	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>");
1540
1541
1542
1543
1544
1545
1546
1547
1548

	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
1549
	register_command(cmd_ctx,  NULL, "reg", handle_reg_command, COMMAND_EXEC, "display or set a register");
1550
1551
1552
1553
1554
	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
1555
	register_command(cmd_ctx,  NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run | halt | init] - default is run");
1556
1557
	register_command(cmd_ctx,  NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");

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

1562
1563
1564
	register_command(cmd_ctx,  NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word [phys]  <addr> <value> [count]");
	register_command(cmd_ctx,  NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word [phys]  <addr> <value> [count]");
	register_command(cmd_ctx,  NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte [phys] <addr> <value> [count]");
oharboe's avatar
oharboe committed
1565

1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
	register_command(cmd_ctx,  NULL, "bp",
			handle_bp_command, COMMAND_EXEC,
			"list or set breakpoint [<address> <length> [hw]]");
	register_command(cmd_ctx,  NULL, "rbp",
			handle_rbp_command, COMMAND_EXEC,
			"remove breakpoint <address>");
	register_command(cmd_ctx,  NULL, "wp",
			handle_wp_command, COMMAND_EXEC,
			"list or set watchpoint "
				"[<address> <length> <r/w/a> [value] [mask]]");
	register_command(cmd_ctx,  NULL, "rwp",
			handle_rwp_command, COMMAND_EXEC,
			"remove watchpoint <address>");
oharboe's avatar
oharboe committed
1579

1580
	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]");
1581
1582
	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
1583
	register_command(cmd_ctx,  NULL, "test_image", handle_test_image_command, COMMAND_EXEC, "test_image <file> [offset] [type]");
oharboe's avatar
oharboe committed
1584

zwelch's avatar
zwelch committed
1585
	if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
1586
		return retval;
zwelch's avatar
zwelch committed
1587
	if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
1588
		return retval;
oharboe's avatar
oharboe committed
1589

1590
	return retval;
1591
1592
}

1593
static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1594
{
1595
	target_t *target = all_targets;
oharboe's avatar
oharboe committed
1596

1597
1598
	if (argc == 1)
	{
1599
1600
		target = get_target(args[0]);
		if (target == NULL) {
1601
			command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0]);
1602
1603
			goto DumpTargets;
		}
oharboe's avatar
oharboe committed
1604
1605
1606
1607
1608
1609
		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;
		}
1610

1611
		cmd_ctx->current_target = target->target_number;
1612
1613
		return ERROR_OK;
	}
1614
DumpTargets:
oharboe's avatar
oharboe committed
1615

1616
	target = all_targets;
oharboe's avatar
oharboe committed
1617
1618
	command_print(cmd_ctx, "    TargetName         Type       Endian TapName            State       ");
	command_print(cmd_ctx, "--  ------------------ ---------- ------ ------------------ ------------");
1619
1620
	while (target)
	{
oharboe's avatar
oharboe committed
1621
1622
1623
1624
		const char *state;
		char marker = ' ';

		if (target->tap->enabled)
1625
			state = target_state_name( target );
oharboe's avatar
oharboe committed
1626
1627
1628
1629
1630
1631
1632
1633
		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",
1634
					  target->target_number,
oharboe's avatar
oharboe committed
1635
					  marker,
1636
					  target->cmd_name,
zwelch's avatar
zwelch committed
1637
					  target_get_name(target),
oharboe's avatar
oharboe committed
1638
1639
					  Jim_Nvp_value2name_simple(nvp_target_endian,
								target->endianness)->name,
1640
					  target->tap->dotted_name,
oharboe's avatar
oharboe committed
1641
					  state);
1642
1643
		target = target->next;
	}
oharboe's avatar
oharboe committed
1644

1645
1646
1647
	return ERROR_OK;
}

1648
/* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1649
1650
1651
1652

static int powerDropout;
static int srstAsserted;

1653
1654
1655
1656
1657
static int runPowerRestore;
static int runPowerDropout;
static int runSrstAsserted;
static int runSrstDeasserted;

1658
static int sense_handler(void)
1659
1660
1661
1662
1663
{
	static int prevSrstAsserted = 0;
	static int prevPowerdropout = 0;

	int retval;
zwelch's avatar
zwelch committed
1664
	if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1665
1666
1667
1668
1669
1670
		return retval;

	int powerRestored;
	powerRestored = prevPowerdropout && !powerDropout;
	if (powerRestored)
	{
1671
		runPowerRestore = 1;
1672
1673
1674
1675
1676
1677
1678
	}

	long long current = timeval_ms();
	static long long lastPower = 0;
	int waitMore = lastPower + 2000 > current;
	if (powerDropout && !waitMore)
	{
1679
		runPowerDropout = 1;
1680
1681
1682
		lastPower = current;
	}

zwelch's avatar
zwelch committed
1683
	if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1684
1685
1686
1687
1688
1689
1690
1691
1692
		return retval;

	int srstDeasserted;
	srstDeasserted = prevSrstAsserted && !srstAsserted;

	static long long lastSrst = 0;
	waitMore = lastSrst + 2000 > current;
	if (srstDeasserted && !waitMore)
	{
1693
		runSrstDeasserted = 1;
1694
1695
1696
1697
1698
		lastSrst = current;
	}

	if (!prevSrstAsserted && srstAsserted)
	{
1699
		runSrstAsserted = 1;
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
	}

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

1716
1717
1718
1719
1720
1721
1722
1723
1724
static void target_call_event_callbacks_all(enum target_event e) {
	target_t *target;
	target = all_targets;
	while (target) {
		target_call_event_callbacks(target, e);
		target = target->next;
	}
}

1725
1726
1727
/* process target state changes */
int handle_target(void *priv)
{
1728
	int retval = ERROR_OK;
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742

	/* 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)
		{
1743
			target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1744
			Jim_Eval(interp, "srst_asserted");
1745
1746
1747
1748
			did_something = 1;
		}
		if (runSrstDeasserted)
		{
1749
			Jim_Eval(interp, "srst_deasserted");
1750
1751
1752
1753
			did_something = 1;
		}
		if (runPowerDropout)
		{
1754
			target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1755
			Jim_Eval(interp, "power_dropout");
1756
1757
1758
1759
			did_something = 1;
		}
		if (runPowerRestore)
		{
1760
			Jim_Eval(interp, "power_restore");
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
			did_something = 1;
		}

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

		/* clear action flags */

zwelch's avatar
zwelch committed
1772
1773
1774
1775
		runSrstAsserted = 0;
		runSrstDeasserted = 0;
		runPowerRestore = 0;
		runPowerDropout = 0;
1776
1777
1778
1779

		recursive = 0;
	}

zwelch's avatar
zwelch committed
1780
1781
1782
1783
	/* Poll targets for state changes unless that's globally disabled.
	 * Skip targets that are currently disabled.
	 */
	for (target_t *target = all_targets;
1784
			is_jtag_poll_safe() && target;
zwelch's avatar
zwelch committed
1785
			target = target->next)
1786
	{
zwelch's avatar
zwelch committed
1787
1788
		if (!target->tap->enabled)
			continue;
1789
1790

		/* only poll target if we've got power and srst isn't asserted */
zwelch's avatar
zwelch committed
1791
		if (!powerDropout && !srstAsserted)
1792
		{
1793
			/* polling may fail silently until the target has been examined */
zwelch's avatar
zwelch committed
1794
			if ((retval = target_poll(target)) != ERROR_OK)
1795
1796
			{
				target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1797
				return retval;
1798
			}
1799
1800
		}
	}
oharboe's avatar
oharboe committed
1801

1802
	return retval;
1803
1804
}

1805
static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1806
1807
1808
1809
1810
{
	target_t *target;
	reg_t *reg = NULL;
	int count = 0;
	char *value;
oharboe's avatar
oharboe committed
1811

1812
	LOG_DEBUG("-");
oharboe's avatar
oharboe committed
1813

1814
	target = get_current_target(cmd_ctx);
oharboe's avatar
oharboe committed
1815

1816
1817
1818
1819
	/* list all available registers for the current target */
	if (argc == 0)
	{
		reg_cache_t *cache = target->reg_cache;
oharboe's avatar
oharboe committed
1820

1821
		count = 0;
zwelch's avatar
zwelch committed
1822
		while (cache)
1823
1824
		{
			int i;
1825

1826
1827
			command_print(cmd_ctx, "===== %s", cache->name);

1828
1829
1830
			for (i = 0, reg = cache->reg_list;
					i < cache->num_regs;
					i++, reg++, count++)
1831
			{
1832
1833
1834
1835
1836
				/* only print cached values if they are valid */
				if (reg->valid) {
					value = buf_to_str(reg->value,
							reg->size, 16);
					command_print(cmd_ctx,
1837
							"(%i) %s (/%" PRIu32 "): 0x%s%s",
1838
1839
1840
1841
1842
1843
1844
							count, reg->name,
							reg->size, value,
							reg->dirty
								? " (dirty)"
								: "");
					free(value);
				} else {
1845
					command_print(cmd_ctx, "(%i) %s (/%" PRIu32 ")",
1846
1847
1848
							  count, reg->name,
							  reg->size) ;
				}
1849
1850
1851
			}
			cache = cache->next;
		}
oharboe's avatar
oharboe committed
1852

1853
1854
		return ERROR_OK;
	}
oharboe's avatar
oharboe committed
1855

1856
1857
1858
	/* access a single register by its ordinal number */
	if ((args[0][0] >= '0') && (args[0][0] <= '9'))
	{
1859
1860
1861
1862
		unsigned num;
		int retval = parse_uint(args[0], &num);
		if (ERROR_OK != retval)
			return ERROR_COMMAND_SYNTAX_ERROR;
oharboe's avatar
oharboe committed
1863

1864
		reg_cache_t *cache = target->reg_cache;
1865
		count = 0;
zwelch's avatar
zwelch committed
1866
		while (cache)
1867
1868
1869
1870
		{
			int i;
			for (i = 0; i < cache->num_regs; i++)
			{
1871
				if (count++ == (int)num)
1872
1873
1874
1875
1876
1877
1878
1879
1880
				{
					reg = &cache->reg_list[i];
					break;
				}
			}
			if (reg)
				break;
			cache = cache->next;
		}
oharboe's avatar
oharboe committed
1881

1882
1883
1884
1885
1886
1887
1888
1889
		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
1890

1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
		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
1903

1904
1905
1906
1907
1908
1909
		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
1910
		command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1911
1912
1913
		free(value);
		return ERROR_OK;
	}
oharboe's avatar
oharboe committed
1914

1915
1916
1917
	/* set register value */
	if (argc == 2)
	{
1918
		uint8_t *buf = malloc(CEIL(reg->size, 8));