target.c 124 KB
Newer Older
1001

1002
1003
1004
1005
1006
1007
1008
1009
	if (cb->periodic)
		return target_timer_callback_periodic_restart(cb, now);

	return target_unregister_timer_callback(cb->callback, cb->priv);
}

static int target_call_timer_callbacks_check_time(int checktime)
{
oharboe's avatar
   
oharboe committed
1010
1011
	keep_alive();

1012
	struct timeval now;
1013
	gettimeofday(&now, NULL);
oharboe's avatar
oharboe committed
1014

1015
	struct target_timer_callback *callback = target_timer_callbacks;
1016
1017
	while (callback)
	{
1018
		// cleaning up may unregister and free this callback
1019
		struct target_timer_callback *next_callback = callback->next;
1020
1021
1022
1023
1024
1025

		bool call_it = callback->callback &&
			((!checktime && callback->periodic) ||
			  now.tv_sec > callback->when.tv_sec ||
			 (now.tv_sec == callback->when.tv_sec &&
			  now.tv_usec >= callback->when.tv_usec));
oharboe's avatar
oharboe committed
1026

1027
		if (call_it)
1028
		{
1029
1030
1031
			int retval = target_call_timer_callback(callback, &now);
			if (retval != ERROR_OK)
				return retval;
1032
		}
oharboe's avatar
oharboe committed
1033

1034
1035
		callback = next_callback;
	}
oharboe's avatar
oharboe committed
1036

1037
1038
1039
	return ERROR_OK;
}

oharboe's avatar
oharboe committed
1040
int target_call_timer_callbacks(void)
1041
1042
1043
1044
1045
{
	return target_call_timer_callbacks_check_time(1);
}

/* invoke periodic callbacks immediately */
oharboe's avatar
oharboe committed
1046
int target_call_timer_callbacks_now(void)
1047
{
oharboe's avatar
   
oharboe committed
1048
	return target_call_timer_callbacks_check_time(0);
1049
1050
}

Zachary T Welch's avatar
Zachary T Welch committed
1051
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1052
{
1053
1054
	struct working_area *c = target->working_areas;
	struct working_area *new_wa = NULL;
oharboe's avatar
oharboe committed
1055

1056
1057
1058
1059
1060
	/* Reevaluate working area address based on MMU state*/
	if (target->working_areas == NULL)
	{
		int retval;
		int enabled;
1061

1062
1063
1064
1065
1066
		retval = target->type->mmu(target, &enabled);
		if (retval != ERROR_OK)
		{
			return retval;
		}
1067

1068
1069
1070
1071
1072
		if (!enabled) {
			if (target->working_area_phys_spec) {
				LOG_DEBUG("MMU disabled, using physical "
					"address for working memory 0x%08x",
					(unsigned)target->working_area_phys);
1073
				target->working_area = target->working_area_phys;
1074
1075
1076
			} else {
				LOG_ERROR("No working memory available. "
					"Specify -work-area-phys to target.");
1077
1078
				return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
			}
1079
1080
1081
1082
1083
		} else {
			if (target->working_area_virt_spec) {
				LOG_DEBUG("MMU enabled, using virtual "
					"address for working memory 0x%08x",
					(unsigned)target->working_area_virt);
1084
				target->working_area = target->working_area_virt;
1085
1086
1087
			} else {
				LOG_ERROR("No working memory available. "
					"Specify -work-area-virt to target.");
1088
1089
				return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
			}
1090
1091
		}
	}
oharboe's avatar
oharboe committed
1092

1093
1094
1095
	/* only allocate multiples of 4 byte */
	if (size % 4)
	{
duane's avatar
duane committed
1096
1097
		LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
		size = (size + 3) & (~3);
1098
	}
oharboe's avatar
oharboe committed
1099

1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
	/* see if there's already a matching working area */
	while (c)
	{
		if ((c->free) && (c->size == size))
		{
			new_wa = c;
			break;
		}
		c = c->next;
	}
oharboe's avatar
oharboe committed
1110

1111
1112
1113
	/* if not, allocate a new one */
	if (!new_wa)
	{
1114
		struct working_area **p = &target->working_areas;
1115
1116
		uint32_t first_free = target->working_area;
		uint32_t free_size = target->working_area_size;
oharboe's avatar
oharboe committed
1117

1118
1119
1120
1121
1122
1123
1124
1125
		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
1126

1127
1128
		if (free_size < size)
		{
1129
			LOG_WARNING("not enough working area available(requested %u, free %u)",
duane's avatar
duane committed
1130
				    (unsigned)(size), (unsigned)(free_size));
1131
1132
			return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
		}
oharboe's avatar
oharboe committed
1133

1134
1135
		LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);

1136
		new_wa = malloc(sizeof(struct working_area));
1137
1138
1139
		new_wa->next = NULL;
		new_wa->size = size;
		new_wa->address = first_free;
oharboe's avatar
oharboe committed
1140

1141
1142
		if (target->backup_working_area)
		{
1143
			int retval;
1144
			new_wa->backup = malloc(new_wa->size);
zwelch's avatar
zwelch committed
1145
			if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1146
1147
1148
1149
1150
			{
				free(new_wa->backup);
				free(new_wa);
				return retval;
			}
1151
1152
1153
1154
1155
		}
		else
		{
			new_wa->backup = NULL;
		}
oharboe's avatar
oharboe committed
1156

1157
1158
1159
		/* put new entry in list */
		*p = new_wa;
	}
oharboe's avatar
oharboe committed
1160

1161
1162
1163
	/* mark as used, and return the new (reused) area */
	new_wa->free = 0;
	*area = new_wa;
oharboe's avatar
oharboe committed
1164

1165
1166
	/* user pointer */
	new_wa->user = area;
oharboe's avatar
oharboe committed
1167

1168
1169
1170
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
1171
int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1172
1173
1174
{
	if (area->free)
		return ERROR_OK;
oharboe's avatar
oharboe committed
1175

zwelch's avatar
zwelch committed
1176
	if (restore && target->backup_working_area)
1177
1178
	{
		int retval;
zwelch's avatar
zwelch committed
1179
		if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1180
1181
			return retval;
	}
oharboe's avatar
oharboe committed
1182

1183
	area->free = 1;
oharboe's avatar
oharboe committed
1184

1185
1186
1187
	/* mark user pointer invalid */
	*area->user = NULL;
	area->user = NULL;
oharboe's avatar
oharboe committed
1188

1189
1190
1191
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
1192
int target_free_working_area(struct target *target, struct working_area *area)
oharboe's avatar
   
oharboe committed
1193
1194
1195
1196
{
	return target_free_working_area_restore(target, area, 1);
}

1197
1198
1199
/* free resources and restore memory, if restoring memory fails,
 * free up resources anyway
 */
Zachary T Welch's avatar
Zachary T Welch committed
1200
void target_free_all_working_areas_restore(struct target *target, int restore)
1201
{
1202
	struct working_area *c = target->working_areas;
1203
1204
1205

	while (c)
	{
1206
		struct working_area *next = c->next;
oharboe's avatar
   
oharboe committed
1207
		target_free_working_area_restore(target, c, restore);
oharboe's avatar
oharboe committed
1208

1209
1210
		if (c->backup)
			free(c->backup);
oharboe's avatar
oharboe committed
1211

1212
		free(c);
oharboe's avatar
oharboe committed
1213

1214
1215
		c = next;
	}
oharboe's avatar
oharboe committed
1216

1217
1218
1219
	target->working_areas = NULL;
}

Zachary T Welch's avatar
Zachary T Welch committed
1220
void target_free_all_working_areas(struct target *target)
oharboe's avatar
   
oharboe committed
1221
{
1222
	target_free_all_working_areas_restore(target, 1);
oharboe's avatar
   
oharboe committed
1223
1224
}

Zachary T Welch's avatar
Zachary T Welch committed
1225
int target_arch_state(struct target *target)
1226
1227
{
	int retval;
zwelch's avatar
zwelch committed
1228
	if (target == NULL)
1229
	{
1230
		LOG_USER("No target has been configured");
1231
1232
		return ERROR_OK;
	}
oharboe's avatar
oharboe committed
1233

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

zwelch's avatar
zwelch committed
1236
	if (target->state != TARGET_HALTED)
1237
		return ERROR_OK;
oharboe's avatar
oharboe committed
1238

zwelch's avatar
zwelch committed
1239
	retval = target->type->arch_state(target);
1240
1241
1242
	return retval;
}

oharboe's avatar
oharboe committed
1243
1244
/* Single aligned words are guaranteed to use 16 or 32 bit access
 * mode respectively, otherwise data is handled as quickly as
1245
1246
 * possible
 */
Zachary T Welch's avatar
Zachary T Welch committed
1247
int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1248
1249
{
	int retval;
1250
	LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
duane's avatar
duane committed
1251
		  (int)size, (unsigned)address);
1252

1253
	if (!target_was_examined(target))
1254
1255
1256
1257
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1258

1259
1260
1261
1262
	if (size == 0) {
		return ERROR_OK;
	}

1263
	if ((address + size - 1) < address)
1264
1265
	{
		/* GDB can request this when e.g. PC is 0xfffffffc*/
1266
1267
		LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
				  (unsigned)address,
duane's avatar
duane committed
1268
				  (unsigned)size);
1269
1270
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1271

1272
1273
	if (((address % 2) == 0) && (size == 2))
	{
zwelch's avatar
zwelch committed
1274
		return target_write_memory(target, address, 2, 1, buffer);
1275
	}
oharboe's avatar
oharboe committed
1276

1277
1278
1279
	/* handle unaligned head bytes */
	if (address % 4)
	{
1280
		uint32_t unaligned = 4 - (address % 4);
oharboe's avatar
oharboe committed
1281

1282
1283
1284
		if (unaligned > size)
			unaligned = size;

zwelch's avatar
zwelch committed
1285
		if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1286
			return retval;
oharboe's avatar
oharboe committed
1287

1288
1289
1290
1291
		buffer += unaligned;
		address += unaligned;
		size -= unaligned;
	}
oharboe's avatar
oharboe committed
1292

1293
1294
1295
1296
	/* handle aligned words */
	if (size >= 4)
	{
		int aligned = size - (size % 4);
oharboe's avatar
oharboe committed
1297

1298
1299
1300
1301
1302
1303
1304
1305
		/* 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
1306
			if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1307
1308
				return retval;
		}
oharboe's avatar
oharboe committed
1309

1310
1311
1312
1313
		buffer += aligned;
		address += aligned;
		size -= aligned;
	}
oharboe's avatar
oharboe committed
1314

1315
1316
1317
	/* handle tail writes of less than 4 bytes */
	if (size > 0)
	{
zwelch's avatar
zwelch committed
1318
		if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1319
1320
			return retval;
	}
oharboe's avatar
oharboe committed
1321

1322
1323
1324
	return ERROR_OK;
}

oharboe's avatar
oharboe committed
1325
1326
/* Single aligned words are guaranteed to use 16 or 32 bit access
 * mode respectively, otherwise data is handled as quickly as
1327
1328
 * possible
 */
Zachary T Welch's avatar
Zachary T Welch committed
1329
int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1330
1331
{
	int retval;
1332
	LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
duane's avatar
duane committed
1333
			  (int)size, (unsigned)address);
1334

1335
	if (!target_was_examined(target))
1336
1337
1338
1339
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1340

1341
1342
1343
1344
	if (size == 0) {
		return ERROR_OK;
	}

1345
	if ((address + size - 1) < address)
1346
1347
	{
		/* GDB can request this when e.g. PC is 0xfffffffc*/
1348
1349
		LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
				  address,
duane's avatar
duane committed
1350
				  size);
1351
1352
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1353

1354
1355
	if (((address % 2) == 0) && (size == 2))
	{
zwelch's avatar
zwelch committed
1356
		return target_read_memory(target, address, 2, 1, buffer);
1357
	}
oharboe's avatar
oharboe committed
1358

1359
1360
1361
	/* handle unaligned head bytes */
	if (address % 4)
	{
1362
		uint32_t unaligned = 4 - (address % 4);
oharboe's avatar
oharboe committed
1363

1364
1365
1366
		if (unaligned > size)
			unaligned = size;

zwelch's avatar
zwelch committed
1367
		if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1368
			return retval;
oharboe's avatar
oharboe committed
1369

1370
1371
1372
1373
		buffer += unaligned;
		address += unaligned;
		size -= unaligned;
	}
oharboe's avatar
oharboe committed
1374

1375
1376
1377
1378
	/* handle aligned words */
	if (size >= 4)
	{
		int aligned = size - (size % 4);
oharboe's avatar
oharboe committed
1379

zwelch's avatar
zwelch committed
1380
		if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1381
			return retval;
oharboe's avatar
oharboe committed
1382

1383
1384
1385
1386
		buffer += aligned;
		address += aligned;
		size -= aligned;
	}
1387

1388
1389
1390
1391
1392
1393
1394
	/*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
1395

1396
1397
1398
1399
		buffer += aligned;
		address += aligned;
		size -= aligned;
	}
1400
1401
1402
	/* handle tail writes of less than 4 bytes */
	if (size > 0)
	{
zwelch's avatar
zwelch committed
1403
		if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1404
1405
			return retval;
	}
oharboe's avatar
oharboe committed
1406

1407
1408
1409
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
1410
int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1411
{
1412
	uint8_t *buffer;
1413
	int retval;
1414
1415
	uint32_t i;
	uint32_t checksum = 0;
1416
	if (!target_was_examined(target))
1417
1418
1419
1420
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1421

1422
	if ((retval = target->type->checksum_memory(target, address,
1423
		size, &checksum)) != ERROR_OK)
1424
1425
1426
1427
	{
		buffer = malloc(size);
		if (buffer == NULL)
		{
duane's avatar
duane committed
1428
			LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
			return ERROR_INVALID_ARGUMENTS;
		}
		retval = target_read_buffer(target, address, size, buffer);
		if (retval != ERROR_OK)
		{
			free(buffer);
			return retval;
		}

		/* convert to target endianess */
1439
		for (i = 0; i < (size/sizeof(uint32_t)); i++)
1440
		{
1441
1442
1443
			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);
1444
1445
		}

1446
		retval = image_calculate_checksum(buffer, size, &checksum);
1447
1448
		free(buffer);
	}
oharboe's avatar
oharboe committed
1449

1450
	*crc = checksum;
oharboe's avatar
oharboe committed
1451

1452
1453
	return retval;
}
1454

Zachary T Welch's avatar
Zachary T Welch committed
1455
int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1456
1457
{
	int retval;
1458
	if (!target_was_examined(target))
1459
1460
1461
1462
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
1463

1464
1465
	if (target->type->blank_check_memory == 0)
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
oharboe's avatar
oharboe committed
1466

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

1469
1470
	return retval;
}
1471

Zachary T Welch's avatar
Zachary T Welch committed
1472
int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1473
{
1474
	uint8_t value_buf[4];
1475
	if (!target_was_examined(target))
1476
1477
1478
1479
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
1480

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

1483
1484
1485
	if (retval == ERROR_OK)
	{
		*value = target_buffer_get_u32(target, value_buf);
1486
1487
		LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
				  address,
duane's avatar
duane committed
1488
				  *value);
1489
1490
1491
1492
	}
	else
	{
		*value = 0x0;
1493
		LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
duane's avatar
duane committed
1494
				  address);
1495
	}
oharboe's avatar
oharboe committed
1496

1497
1498
1499
	return retval;
}

Zachary T Welch's avatar
Zachary T Welch committed
1500
int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1501
{
1502
	uint8_t value_buf[2];
1503
	if (!target_was_examined(target))
1504
1505
1506
1507
1508
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}

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

1511
1512
1513
	if (retval == ERROR_OK)
	{
		*value = target_buffer_get_u16(target, value_buf);
1514
1515
		LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
				  address,
duane's avatar
duane committed
1516
				  *value);
1517
1518
1519
1520
	}
	else
	{
		*value = 0x0;
1521
		LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
duane's avatar
duane committed
1522
				  address);
1523
	}
oharboe's avatar
oharboe committed
1524

1525
1526
1527
	return retval;
}

Zachary T Welch's avatar
Zachary T Welch committed
1528
int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1529
{
zwelch's avatar
zwelch committed
1530
	int retval = target_read_memory(target, address, 1, 1, value);
1531
	if (!target_was_examined(target))
1532
1533
1534
1535
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
1536
1537
1538

	if (retval == ERROR_OK)
	{
1539
1540
		LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
				  address,
duane's avatar
duane committed
1541
				  *value);
1542
1543
1544
1545
	}
	else
	{
		*value = 0x0;
1546
		LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
duane's avatar
duane committed
1547
				  address);
1548
	}
oharboe's avatar
oharboe committed
1549

1550
1551
1552
	return retval;
}

Zachary T Welch's avatar
Zachary T Welch committed
1553
int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1554
1555
{
	int retval;
1556
	uint8_t value_buf[4];
1557
	if (!target_was_examined(target))
1558
1559
1560
1561
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
1562

1563
1564
	LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
			  address,
duane's avatar
duane committed
1565
			  value);
1566

oharboe's avatar
oharboe committed
1567
	target_buffer_set_u32(target, value_buf, value);
zwelch's avatar
zwelch committed
1568
	if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1569
	{
1570
		LOG_DEBUG("failed: %i", retval);
1571
	}
oharboe's avatar
oharboe committed
1572

1573
1574
1575
	return retval;
}

Zachary T Welch's avatar
Zachary T Welch committed
1576
int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1577
1578
{
	int retval;
1579
	uint8_t value_buf[2];
1580
	if (!target_was_examined(target))
1581
1582
1583
1584
1585
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}

1586
1587
	LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
			  address,
duane's avatar
duane committed
1588
			  value);
1589

oharboe's avatar
oharboe committed
1590
	target_buffer_set_u16(target, value_buf, value);
zwelch's avatar
zwelch committed
1591
	if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1592
	{
1593
		LOG_DEBUG("failed: %i", retval);
1594
	}
oharboe's avatar
oharboe committed
1595

1596
1597
1598
	return retval;
}

Zachary T Welch's avatar
Zachary T Welch committed
1599
int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1600
1601
{
	int retval;
1602
	if (!target_was_examined(target))
1603
1604
1605
1606
1607
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}

1608
	LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
duane's avatar
duane committed
1609
			  address, value);
1610

zwelch's avatar
zwelch committed
1611
	if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1612
	{
1613
		LOG_DEBUG("failed: %i", retval);
1614
	}
oharboe's avatar
oharboe committed
1615

1616
1617
1618
	return retval;
}

1619
COMMAND_HANDLER(handle_targets_command)
1620
{
Zachary T Welch's avatar
Zachary T Welch committed
1621
	struct target *target = all_targets;
oharboe's avatar
oharboe committed
1622

1623
	if (CMD_ARGC == 1)
1624
	{
1625
		target = get_target(CMD_ARGV[0]);
1626
		if (target == NULL) {
1627
			command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1628
1629
			goto DumpTargets;
		}
oharboe's avatar
oharboe committed
1630
		if (!target->tap->enabled) {
1631
			command_print(CMD_CTX,"Target: TAP %s is disabled, "
oharboe's avatar
oharboe committed
1632
1633
1634
1635
					"can't be the current target\n",
					target->tap->dotted_name);
			return ERROR_FAIL;
		}
1636

1637
		CMD_CTX->current_target = target->target_number;
1638
1639
		return ERROR_OK;
	}
1640
DumpTargets:
oharboe's avatar
oharboe committed
1641

1642
	target = all_targets;
1643
1644
	command_print(CMD_CTX, "    TargetName         Type       Endian TapName            State       ");
	command_print(CMD_CTX, "--  ------------------ ---------- ------ ------------------ ------------");
1645
1646
	while (target)
	{
oharboe's avatar
oharboe committed
1647
1648
1649
1650
		const char *state;
		char marker = ' ';

		if (target->tap->enabled)
1651
			state = target_state_name( target );
oharboe's avatar
oharboe committed
1652
1653
1654
		else
			state = "tap-disabled";

1655
		if (CMD_CTX->current_target == target->target_number)
oharboe's avatar
oharboe committed
1656
1657
1658
			marker = '*';

		/* keep columns lined up to match the headers above */
1659
		command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1660
					  target->target_number,
oharboe's avatar
oharboe committed
1661
					  marker,
1662
					  target_name(target),
1663
					  target_type_name(target),
oharboe's avatar
oharboe committed
1664
1665
					  Jim_Nvp_value2name_simple(nvp_target_endian,
								target->endianness)->name,
1666
					  target->tap->dotted_name,
oharboe's avatar
oharboe committed
1667
					  state);
1668
1669
		target = target->next;
	}
oharboe's avatar
oharboe committed
1670

1671
1672
1673
	return ERROR_OK;
}

1674
/* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1675
1676
1677
1678

static int powerDropout;
static int srstAsserted;

1679
1680
1681
1682
1683
static int runPowerRestore;
static int runPowerDropout;
static int runSrstAsserted;
static int runSrstDeasserted;

1684
static int sense_handler(void)
1685
1686
1687
1688
1689
{
	static int prevSrstAsserted = 0;
	static int prevPowerdropout = 0;

	int retval;
zwelch's avatar
zwelch committed
1690
	if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1691
1692
1693
1694
1695
1696
		return retval;

	int powerRestored;
	powerRestored = prevPowerdropout && !powerDropout;
	if (powerRestored)
	{
1697
		runPowerRestore = 1;
1698
1699
1700
1701
1702
1703
1704
	}

	long long current = timeval_ms();
	static long long lastPower = 0;
	int waitMore = lastPower + 2000 > current;
	if (powerDropout && !waitMore)
	{
1705
		runPowerDropout = 1;
1706
1707
1708
		lastPower = current;
	}

zwelch's avatar
zwelch committed
1709
	if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1710
1711
1712
1713
1714
1715
1716
1717
1718
		return retval;

	int srstDeasserted;
	srstDeasserted = prevSrstAsserted && !srstAsserted;

	static long long lastSrst = 0;
	waitMore = lastSrst + 2000 > current;
	if (srstDeasserted && !waitMore)
	{
1719
		runSrstDeasserted = 1;
1720
1721
1722
1723
1724
		lastSrst = current;
	}

	if (!prevSrstAsserted && srstAsserted)
	{
1725
		runSrstAsserted = 1;
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
	}

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

1742
/* process target state changes */
1743
static int handle_target(void *priv)
1744
{
1745
	Jim_Interp *interp = (Jim_Interp *)priv;
1746
	int retval = ERROR_OK;
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760

	/* 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)
		{
1761
			LOG_INFO("srst asserted detected, running srst_asserted proc.");
1762
			Jim_Eval(interp, "srst_asserted");
1763
1764
1765
1766
			did_something = 1;
		}
		if (runSrstDeasserted)
		{
1767
			Jim_Eval(interp, "srst_deasserted");
1768
1769
1770
1771
			did_something = 1;
		}
		if (runPowerDropout)
		{
1772
			LOG_INFO("Power dropout detected, running power_dropout proc.");
1773
			Jim_Eval(interp, "power_dropout");
1774
1775
1776
1777
			did_something = 1;
		}
		if (runPowerRestore)
		{
1778
			Jim_Eval(interp, "power_restore");
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
			did_something = 1;
		}

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

		/* clear action flags */

zwelch's avatar
zwelch committed
1790
1791
1792
1793
		runSrstAsserted = 0;
		runSrstDeasserted = 0;
		runPowerRestore = 0;
		runPowerDropout = 0;
1794
1795
1796
1797

		recursive = 0;
	}

zwelch's avatar
zwelch committed
1798
1799
1800
	/* Poll targets for state changes unless that's globally disabled.
	 * Skip targets that are currently disabled.
	 */
Zachary T Welch's avatar
Zachary T Welch committed
1801
	for (struct target *target = all_targets;
1802
			is_jtag_poll_safe() && target;
zwelch's avatar
zwelch committed
1803
			target = target->next)
1804
	{
zwelch's avatar
zwelch committed
1805
1806
		if (!target->tap->enabled)
			continue;
1807
1808

		/* only poll target if we've got power and srst isn't asserted */
zwelch's avatar
zwelch committed
1809
		if (!powerDropout && !srstAsserted)
1810
		{
1811
			/* polling may fail silently until the target has been examined */
zwelch's avatar
zwelch committed
1812
			if ((retval = target_poll(target)) != ERROR_OK)
1813
			{
1814
1815
1816
1817
1818
1819
1820
1821
				/* FIX!!!!! If we add a LOG_INFO() here to output a line in GDB
				 * *why* we are aborting GDB, then we'll spam telnet when the
				 * poll is failing persistently.
				 *
				 * If we could implement an event that detected the
				 * target going from non-pollable to pollable, we could issue
				 * an error only upon the transition.
				 */
1822
				target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1823
				return retval;
1824
			}
1825
1826
		}
	}
oharboe's avatar
oharboe committed
1827

1828
	return retval;
1829
1830
}

1831
COMMAND_HANDLER(handle_reg_command)
1832
{
Zachary T Welch's avatar
Zachary T Welch committed
1833
	struct target *target;
Zachary T Welch's avatar
Zachary T Welch committed
1834
	struct reg *reg = NULL;
1835
	unsigned count = 0;
1836
	char *value;
oharboe's avatar
oharboe committed
1837

1838
	LOG_DEBUG("-");
oharboe's avatar
oharboe committed
1839

1840
	target = get_current_target(CMD_CTX);
oharboe's avatar
oharboe committed
1841

1842
	/* list all available registers for the current target */
1843
	if (CMD_ARGC == 0)
1844
	{
1845
		struct reg_cache *cache = target->reg_cache;
oharboe's avatar
oharboe committed
1846

1847
		count = 0;
zwelch's avatar
zwelch committed
1848
		while (cache)
1849
		{
1850
			unsigned i;
1851

1852
			command_print(CMD_CTX, "===== %s", cache->name);
1853

1854
1855
1856
			for (i = 0, reg = cache->reg_list;
					i < cache->num_regs;
					i++, reg++, count++)
1857
			{
1858
1859
1860
1861
				/* only print cached values if they are valid */
				if (reg->valid) {
					value = buf_to_str(reg->value,
							reg->size, 16);
1862
					command_print(CMD_CTX,
1863
							"(%i) %s (/%" PRIu32 "): 0x%s%s",
1864
1865
1866
1867
1868
1869
1870
							count, reg->name,
							reg->size, value,
							reg->dirty
								? " (dirty)"</