target.c 64.9 KB
Newer Older
oharboe's avatar
oharboe committed
2001
2002
	 if ((argc < 2) || (argc > 3))
		return ERROR_COMMAND_SYNTAX_ERROR;
2003
2004
2005

	address = strtoul(args[0], NULL, 0);
	value = strtoul(args[1], NULL, 0);
oharboe's avatar
oharboe committed
2006
2007
	if (argc == 3)
		count = strtoul(args[2], NULL, 0);
2008
	
2009
2010
2011
	switch (cmd[2])
	{
		case 'w':
oharboe's avatar
oharboe committed
2012
			wordsize = 4;
2013
2014
2015
			target_buffer_set_u32(target, value_buf, value);
			break;
		case 'h':
oharboe's avatar
oharboe committed
2016
			wordsize = 2;
2017
2018
2019
			target_buffer_set_u16(target, value_buf, value);
			break;
		case 'b':
oharboe's avatar
oharboe committed
2020
			wordsize = 1;
2021
2022
2023
			value_buf[0] = value;
			break;
		default:
oharboe's avatar
oharboe committed
2024
			return ERROR_COMMAND_SYNTAX_ERROR;
2025
	}
oharboe's avatar
oharboe committed
2026
	for (i=0; i<count; i++)
2027
	{
oharboe's avatar
oharboe committed
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
		int retval;
		switch (wordsize)
		{
			case 4:
				retval = target->type->write_memory(target, address + i*wordsize, 4, 1, value_buf);
				break;
			case 2:
				retval = target->type->write_memory(target, address + i*wordsize, 2, 1, value_buf);
				break;
			case 1:
				retval = target->type->write_memory(target, address + i*wordsize, 1, 1, value_buf);
			break;
			default:
			return ERROR_OK;
		}
		if (retval!=ERROR_OK)
		{
			return retval;
		}
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
	}

	return ERROR_OK;

}

int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	u8 *buffer;
	u32 buf_cnt;
	u32 image_size;
	int i;
	int retval;

	image_t image;	
	
	duration_t duration;
	char *duration_text;
	
	target_t *target = get_current_target(cmd_ctx);

	if (argc < 1)
	{
		command_print(cmd_ctx, "usage: load_image <filename> [address] [type]");
		return ERROR_OK;
	}
	
	/* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
	if (argc >= 2)
	{
		image.base_address_set = 1;
		image.base_address = strtoul(args[1], NULL, 0);
	}
	else
	{
		image.base_address_set = 0;
	}
	
	image.start_address_set = 0;

	duration_start_measure(&duration);
	
	if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
	{
		return ERROR_OK;
	}
	
	image_size = 0x0;
2095
	retval = ERROR_OK;
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
	for (i = 0; i < image.num_sections; i++)
	{
		buffer = malloc(image.sections[i].size);
		if (buffer == NULL)
		{
			command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
			break;
		}
		
		if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
		{
			free(buffer);
2108
2109
2110
2111
2112
2113
			break;
		}
		if ((retval = target_write_buffer(target, image.sections[i].base_address, buf_cnt, buffer)) != ERROR_OK)
		{
			free(buffer);
			break;
2114
2115
2116
2117
2118
2119
2120
2121
		}
		image_size += buf_cnt;
		command_print(cmd_ctx, "%u byte written at address 0x%8.8x", buf_cnt, image.sections[i].base_address);
		
		free(buffer);
	}

	duration_stop_measure(&duration, &duration_text);
2122
2123
2124
2125
	if (retval==ERROR_OK)
	{
		command_print(cmd_ctx, "downloaded %u byte in %s", image_size, duration_text);
	}
2126
2127
2128
2129
	free(duration_text);
	
	image_close(&image);

2130
	return retval;
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140

}

int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	fileio_t fileio;
	
	u32 address;
	u32 size;
	u8 buffer[560];
2141
	int retval=ERROR_OK;
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
	
	duration_t duration;
	char *duration_text;
	
	target_t *target = get_current_target(cmd_ctx);

	if (argc != 3)
	{
		command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
		return ERROR_OK;
	}

	address = strtoul(args[1], NULL, 0);
	size = strtoul(args[2], NULL, 0);

	if ((address & 3) || (size & 3))
	{
		command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
		return ERROR_OK;
	}
	
	if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
	{
		return ERROR_OK;
	}
	
	duration_start_measure(&duration);
	
	while (size > 0)
	{
		u32 size_written;
		u32 this_run_size = (size > 560) ? 560 : size;
		
		retval = target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
		if (retval != ERROR_OK)
		{
			break;
		}
		
2181
2182
2183
2184
2185
		retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
		if (retval != ERROR_OK)
		{
			break;
		}
2186
2187
2188
2189
2190
2191
2192
2193
		
		size -= this_run_size;
		address += this_run_size;
	}

	fileio_close(&fileio);

	duration_stop_measure(&duration, &duration_text);
2194
2195
2196
2197
	if (retval==ERROR_OK)
	{
		command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text);
	}
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
	free(duration_text);
	
	return ERROR_OK;
}

int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	u8 *buffer;
	u32 buf_cnt;
	u32 image_size;
	int i;
	int retval;
	u32 checksum = 0;
	u32 mem_checksum = 0;

	image_t image;	
	
	duration_t duration;
	char *duration_text;
	
	target_t *target = get_current_target(cmd_ctx);
	
	if (argc < 1)
	{
2222
		return ERROR_COMMAND_SYNTAX_ERROR;
2223
2224
2225
2226
	}
	
	if (!target)
	{
2227
		LOG_ERROR("no target selected");
2228
		return ERROR_FAIL;
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
	}
	
	duration_start_measure(&duration);
	
	if (argc >= 2)
	{
		image.base_address_set = 1;
		image.base_address = strtoul(args[1], NULL, 0);
	}
	else
	{
		image.base_address_set = 0;
		image.base_address = 0x0;
	}

	image.start_address_set = 0;

2246
	if ((retval=image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2247
	{
2248
		return retval;
2249
2250
2251
	}
	
	image_size = 0x0;
2252
	retval=ERROR_OK;
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
	for (i = 0; i < image.num_sections; i++)
	{
		buffer = malloc(image.sections[i].size);
		if (buffer == NULL)
		{
			command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
			break;
		}
		if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
		{
			free(buffer);
2264
			break;
2265
2266
2267
2268
2269
2270
2271
2272
2273
		}
		
		/* calculate checksum of image */
		image_calculate_checksum( buffer, buf_cnt, &checksum );
		
		retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
		if( retval != ERROR_OK )
		{
			free(buffer);
2274
			break;
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
		}
		
		if( checksum != mem_checksum )
		{
			/* failed crc checksum, fall back to a binary compare */
			u8 *data;
			
			command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
			
			data = (u8*)malloc(buf_cnt);
			
			/* Can we use 32bit word accesses? */
			int size = 1;
			int count = buf_cnt;
			if ((count % 4) == 0)
			{
				size *= 4;
				count /= 4;
			}
			retval = target->type->read_memory(target, image.sections[i].base_address, size, count, data);
			if (retval == ERROR_OK)
			{
				int t;
				for (t = 0; t < buf_cnt; t++)
				{
					if (data[t] != buffer[t])
					{
						command_print(cmd_ctx, "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n", t + image.sections[i].base_address, data[t], buffer[t]);
						free(data);
						free(buffer);
2305
2306
						retval=ERROR_FAIL;
						goto done;
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
					}
				}
			}
			
			free(data);
		}
		
		free(buffer);
		image_size += buf_cnt;
	}
2317
done:	
2318
	duration_stop_measure(&duration, &duration_text);
2319
2320
2321
2322
	if (retval==ERROR_OK)
	{
		command_print(cmd_ctx, "verified %u bytes in %s", image_size, duration_text);
	}
2323
2324
2325
2326
	free(duration_text);
	
	image_close(&image);
	
2327
	return retval;
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
}

int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	int retval;
	target_t *target = get_current_target(cmd_ctx);

	if (argc == 0)
	{
		breakpoint_t *breakpoint = target->breakpoints;

		while (breakpoint)
		{
			if (breakpoint->type == BKPT_SOFT)
			{
				char* buf = buf_to_str(breakpoint->orig_instr, breakpoint->length, 16);
				command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint->address, breakpoint->length, breakpoint->set, buf);
				free(buf);
			}
			else
			{
				command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
			}
			breakpoint = breakpoint->next;
		}
	}
	else if (argc >= 2)
	{
		int hw = BKPT_SOFT;
		u32 length = 0;

		length = strtoul(args[1], NULL, 0);
		
		if (argc >= 3)
			if (strcmp(args[2], "hw") == 0)
				hw = BKPT_HARD;

		if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
		{
2367
			LOG_ERROR("Failure setting breakpoints");
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
		}
		else
		{
			command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0));
		}
	}
	else
	{
		command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
	}

	return ERROR_OK;
}

int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	target_t *target = get_current_target(cmd_ctx);

	if (argc > 0)
		breakpoint_remove(target, strtoul(args[0], NULL, 0));

	return ERROR_OK;
}

int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	target_t *target = get_current_target(cmd_ctx);
	int retval;

	if (argc == 0)
	{
		watchpoint_t *watchpoint = target->watchpoints;

		while (watchpoint)
		{
ntfreak's avatar
ntfreak committed
2403
			command_print(cmd_ctx, "address: 0x%8.8x, len: 0x%8.8x, r/w/a: %i, value: 0x%8.8x, mask: 0x%8.8x", watchpoint->address, watchpoint->length, watchpoint->rw, watchpoint->value, watchpoint->mask);
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
			watchpoint = watchpoint->next;
		}
	} 
	else if (argc >= 2)
	{
		enum watchpoint_rw type = WPT_ACCESS;
		u32 data_value = 0x0;
		u32 data_mask = 0xffffffff;
		
		if (argc >= 3)
		{
			switch(args[2][0])
			{
				case 'r':
					type = WPT_READ;
					break;
				case 'w':
					type = WPT_WRITE;
					break;
				case 'a':
					type = WPT_ACCESS;
					break;
				default:
					command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
					return ERROR_OK;
			}
		}
		if (argc >= 4)
		{
			data_value = strtoul(args[3], NULL, 0);
		}
		if (argc >= 5)
		{
			data_mask = strtoul(args[4], NULL, 0);
		}
		
		if ((retval = watchpoint_add(target, strtoul(args[0], NULL, 0),
				strtoul(args[1], NULL, 0), type, data_value, data_mask)) != ERROR_OK)
		{
2443
			LOG_ERROR("Failure setting breakpoints");
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
		}
	}
	else
	{
		command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
	}
		
	return ERROR_OK;
}

int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	target_t *target = get_current_target(cmd_ctx);

	if (argc > 0)
		watchpoint_remove(target, strtoul(args[0], NULL, 0));
	
	return ERROR_OK;
}

int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc)
{
	int retval;
	target_t *target = get_current_target(cmd_ctx);
	u32 va;
	u32 pa;

	if (argc != 1)
	{
		return ERROR_COMMAND_SYNTAX_ERROR;
	}
	va = strtoul(args[0], NULL, 0);

	retval = target->type->virt2phys(target, va, &pa);
	if (retval == ERROR_OK)
	{
		command_print(cmd_ctx, "Physical address 0x%08x", pa);
	}
	else
	{
		/* lower levels will have logged a detailed error which is 
		 * forwarded to telnet/GDB session.  
		 */
	}
	return retval;
}
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
static void writeLong(FILE *f, int l)
{
	int i;
	for (i=0; i<4; i++)
	{
		char c=(l>>(i*8))&0xff;
		fwrite(&c, 1, 1, f); 
	}
	
}
static void writeString(FILE *f, char *s)
{
	fwrite(s, 1, strlen(s), f); 
}



// Dump a gmon.out histogram file.
static void writeGmon(u32 *samples, int sampleNum, char *filename)
{
	int i;
	FILE *f=fopen(filename, "w");
	if (f==NULL)
		return;
	fwrite("gmon", 1, 4, f);
	writeLong(f, 0x00000001); // Version
	writeLong(f, 0); // padding
	writeLong(f, 0); // padding
	writeLong(f, 0); // padding
				
	fwrite("", 1, 1, f);  // GMON_TAG_TIME_HIST 

	// figure out bucket size
	u32 min=samples[0];
	u32 max=samples[0];
	for (i=0; i<sampleNum; i++)
	{
		if (min>samples[i])
		{
			min=samples[i];
		}
		if (max<samples[i])
		{
			max=samples[i];
		}
	}

	int addressSpace=(max-min+1);
	
	static int const maxBuckets=256*1024; // maximum buckets.
	int length=addressSpace;
	if (length > maxBuckets)
	{
		length=maxBuckets; 
	}
	int *buckets=malloc(sizeof(int)*length);
	if (buckets==NULL)
	{
		fclose(f);
		return;
	}
	memset(buckets, 0, sizeof(int)*length);
	for (i=0; i<sampleNum;i++)
	{
		u32 address=samples[i];
		long long a=address-min;
		long long b=length-1;
		long long c=addressSpace-1;
		int index=(a*b)/c; // danger!!!! int32 overflows 
		buckets[index]++;
	}
	
	//			   append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr))
	writeLong(f, min); 					// low_pc
	writeLong(f, max);		// high_pc
	writeLong(f, length);		// # of samples
	writeLong(f, 64000000); 			// 64MHz
	writeString(f, "seconds");
	for (i=0; i<(15-strlen("seconds")); i++)
	{
		fwrite("", 1, 1, f);  // padding
	}
	writeString(f, "s");
		
//			   append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size)
	
	char *data=malloc(2*length);
	if (data!=NULL)
	{
		for (i=0; i<length;i++)
		{
			int val;
			val=buckets[i];
			if (val>65535)
			{
				val=65535;
			}
			data[i*2]=val&0xff;
			data[i*2+1]=(val>>8)&0xff;
		}
		free(buckets);
		fwrite(data, 1, length*2, f);
		free(data);
	} else
	{
		free(buckets);
	}

	fclose(f);
}

/* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	target_t *target = get_current_target(cmd_ctx);
	struct timeval timeout, now;
	
	gettimeofday(&timeout, NULL);
	if (argc!=2)
	{
		return ERROR_COMMAND_SYNTAX_ERROR;
	}
	char *end;
	timeval_add_time(&timeout, strtoul(args[0], &end, 0), 0);
	if (*end) 
	{
		return ERROR_OK;
	}
	
	command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");

	static const int maxSample=10000;
	u32 *samples=malloc(sizeof(u32)*maxSample);
	if (samples==NULL)
		return ERROR_OK;
	
	int numSamples=0;
	int retval=ERROR_OK;
	// hopefully it is safe to cache! We want to stop/restart as quickly as possible.
	reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
	
	for (;;)
	{
2633
		target_poll(target);
2634
2635
2636
2637
		if (target->state == TARGET_HALTED)
		{
			u32 t=*((u32 *)reg->value);
			samples[numSamples++]=t;
2638
2639
			retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
			target_poll(target);
2640
2641
2642
2643
			usleep(10*1000); // sleep 10ms, i.e. <100 samples/second.
		} else if (target->state == TARGET_RUNNING)
		{
			// We want to quickly sample the PC.
2644
			target_halt(target);
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
		} else
		{
			command_print(cmd_ctx, "Target not halted or running");
			retval=ERROR_OK;
			break;
		}
		if (retval!=ERROR_OK)
		{
			break;
		}
		
		gettimeofday(&now, NULL);
		if ((numSamples>=maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
		{
			command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
2660
			target_poll(target);
2661
2662
			if (target->state == TARGET_HALTED)
			{
2663
				target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2664
			}
2665
			target_poll(target);
2666
2667
2668
2669
2670
2671
2672
2673
2674
			writeGmon(samples, numSamples, args[1]);
			command_print(cmd_ctx, "Wrote %s", args[1]);
			break;
		}
	}
	free(samples);
	
	return ERROR_OK;
}
For faster browsing, not all history is shown. View entire blame