ecosboard.c 52.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/***************************************************************************
 *   Copyright (C) 2007-2008 by yvind Harboe                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "log.h"
#include "types.h"
#include "jtag.h"
#include "configuration.h"
#include "xsvf.h"
#include "target.h"
#include "flash.h"
#include "nand.h"
#include "pld.h"

#include "command.h"
#include "server.h"
#include "telnet_server.h"
#include "gdb_server.h"

#include <time_support.h>
#include <sys/time.h>
#include <sys/types.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <cyg/io/flash.h>
#include <pkgconf/fs_jffs2.h>	// Address of JFFS2
#include <network.h>

#include <fcntl.h>
#include <sys/stat.h>
#include <cyg/fileio/fileio.h>
#include <dirent.h>
#include <cyg/athttpd/http.h>
#include <cyg/athttpd/socket.h>
#include <cyg/athttpd/handler.h>
#include <cyg/athttpd/cgi.h>
#include <cyg/athttpd/forms.h>
oharboe's avatar
oharboe committed
62
#include <cyg/discover/discover.h>
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <cyg/hal/hal_diag.h>
#include <cyg/kernel/kapi.h>
#include <cyg/io/serialio.h>
#include <cyg/io/io.h>
#include <netinet/tcp.h>
#include "rom.h"
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <ifaddrs.h>
#include <string.h>

oharboe's avatar
oharboe committed
84

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <unistd.h>
#include <stdio.h>
#define MAX_IFS 64
#if defined(CYGPKG_NET_FREEBSD_STACK)
#include <tftp_support.h>
/* posix compatibility broken*/
struct tftpd_fileops fileops =
{
	(int (*)(const char *, int))open,
	close,
	(int (*)(int, const void *, int))write,
	( int (*)(int, void *, int))read
};

#endif

oharboe's avatar
oharboe committed
101
#define ZYLIN_VERSION "1.48"
102
103
104
105
#define ZYLIN_DATE __DATE__
#define ZYLIN_TIME __TIME__
/* hmmm....  we can't pick up the right # during build if we've checked this out
 * in Eclipse... arrggghh...*/
106
#define ZYLIN_OPENOCD "$Revision$"
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#define ZYLIN_OPENOCD_VERSION "Zylin JTAG ZY1000 " ZYLIN_VERSION " " ZYLIN_DATE " " ZYLIN_TIME
#define ZYLIN_CONFIG_DIR "/config/settings"

void diag_write(char *buf, int len)
{
	int j;
	for (j = 0; j < len; j++)
	{
		diag_printf("%c", buf[j]);
	}
}

static bool serialLog = true;
static bool writeLog = true;

122
char hwaddr[512];
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137


/* Give TELNET a way to find out what version this is */
int handle_zy1000_version_command(struct command_context_s *cmd_ctx, char *cmd,
		char **args, int argc)
{
	if (argc > 1)
	{
		return ERROR_COMMAND_SYNTAX_ERROR;
	}
	if (argc == 0)
	{
		command_print(cmd_ctx, ZYLIN_OPENOCD_VERSION);
	} else if (strcmp("openocd", args[0])==0)
	{
138
139
140
		int revision;
		revision=atol(ZYLIN_OPENOCD+strlen("XRevision: "));
		command_print(cmd_ctx, "%d", revision);
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	} else if (strcmp("zy1000", args[0])==0)
	{
		command_print(cmd_ctx, "%s", ZYLIN_VERSION);
	} else if (strcmp("date", args[0])==0)
	{
		command_print(cmd_ctx, "%s", ZYLIN_DATE);
	} else
	{
		return ERROR_COMMAND_SYNTAX_ERROR;
	}

	return ERROR_OK;
}

oharboe's avatar
wip    
oharboe committed
155
156
157



158
159
160
161
162
163
164
165
extern flash_driver_t *flash_drivers[];
extern target_type_t *target_types[];

#ifdef CYGPKG_PROFILE_GPROF
#include <cyg/profile/profile.h>

extern char _stext, _etext; // Defined by the linker

oharboe's avatar
oharboe committed
166
167
168
static char *start_of_code=&_stext;
static char *end_of_code=&_etext;

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
void start_profile(void)
{
	// This starts up the system-wide profiling, gathering
	// profile information on all of the code, with a 16 byte
	// "bucket" size, at a rate of 100us/profile hit.
	// Note: a bucket size of 16 will give pretty good function
	//       resolution.  Much smaller and the buffer becomes
	//       much too large for very little gain.
	// Note: a timer period of 100us is also a reasonable
	//       compromise.  Any smaller and the overhead of
	//       handling the timter (profile) interrupt could
	//       swamp the system.  A fast processor might get
	//       by with a smaller value, but a slow one could
	//       even be swamped by this value.  If the value is
	//       too large, the usefulness of the profile is reduced.

	// no more interrupts than 1/10ms.
	//profile_on((void *)0, (void *)0x40000, 16, 10000); // SRAM
oharboe's avatar
oharboe committed
187
188
//	profile_on(0, &_etext, 16, 10000); // SRAM & DRAM
	profile_on(start_of_code, end_of_code, 16, 10000); // Nios DRAM
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
}
#endif

// launch GDB server if a config file exists
bool zylinjtag_parse_config_file(struct command_context_s *cmd_ctx, const char *config_file_name)
{
	bool foundFile = false;
	FILE *config_file = NULL;
	command_print(cmd_ctx, "executing config file %s", config_file_name);
	config_file = fopen(config_file_name, "r");
	if (config_file)
	{
		fclose(config_file);
		int retval;
		retval = command_run_linef(cmd_ctx, "script %s", config_file_name);
		if (retval == ERROR_OK)
		{
			foundFile = true;
		}
		else
		{
			command_print(cmd_ctx, "Failed executing %s %d", config_file_name, retval);
		}
	}
	else
	{
		command_print(cmd_ctx, "No %s found", config_file_name);
	}

	return foundFile;
}

extern int eth0_up;
static FILE *log;

static char reboot_stack[2048];


static void
zylinjtag_reboot(cyg_addrword_t data)
{
	serialLog = true;
	diag_printf("Rebooting in 100 ticks..\n");
	cyg_thread_delay(100);
	diag_printf("Unmounting /config..\n");
	umount("/config");
	diag_printf("Rebooting..\n");
	HAL_PLATFORM_RESET();
}
static cyg_thread zylinjtag_thread_object;
static cyg_handle_t zylinjtag_thread_handle;

void reboot(void)
{
    cyg_thread_create(1,
                      zylinjtag_reboot,
                      (cyg_addrword_t)0,
                      "reboot Thread",
                      (void *)reboot_stack,
                      sizeof(reboot_stack),
                      &zylinjtag_thread_handle,
                      &zylinjtag_thread_object);
	cyg_thread_resume(zylinjtag_thread_handle);
}

int configuration_output_handler(struct command_context_s *context, const char* line)
{
	diag_printf("%s", line);

	return ERROR_OK;
}

int zy1000_configuration_output_handler_log(struct command_context_s *context, const char* line)
{
	LOG_USER_N("%s", line);

	return ERROR_OK;
}

int handle_rm_command(struct command_context_s *cmd_ctx, char *cmd,
		char **args, int argc)
{
	if (argc != 1)
	{
		command_print(cmd_ctx, "rm <filename>");
		return ERROR_INVALID_ARGUMENTS;
	}

	if (unlink(args[0]) != 0)
	{
		command_print(cmd_ctx, "failed: %d", errno);
	}

	return ERROR_OK;
}

int loadFile(const char *fileName, void **data, int *len);

int handle_cat_command(struct command_context_s *cmd_ctx, char *cmd,
		char **args, int argc)
{
	if (argc != 1)
	{
		command_print(cmd_ctx, "cat <filename>");
		return ERROR_INVALID_ARGUMENTS;
	}

	// NOTE!!! we only have line printing capability so we print the entire file as a single line.
	void *data;
	int len;

	int retval = loadFile(args[0], &data, &len);
	if (retval == ERROR_OK)
	{
		command_print(cmd_ctx, "%s", data);
		free(data);
	}
	else
	{
		command_print(cmd_ctx, "%s not found %d", args[0], retval);
	}

	return ERROR_OK;
}
int handle_trunc_command(struct command_context_s *cmd_ctx, char *cmd,
		char **args, int argc)
{
	if (argc != 1)
	{
		command_print(cmd_ctx, "trunc <filename>");
		return ERROR_INVALID_ARGUMENTS;
	}

	FILE *config_file = NULL;
	config_file = fopen(args[0], "w");
	if (config_file != NULL)
		fclose(config_file);

	return ERROR_OK;
}


int handle_meminfo_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	static int prev = 0;
	struct mallinfo info;

	if (argc != 0)
	{
		command_print(cmd_ctx, "meminfo");
		return ERROR_INVALID_ARGUMENTS;
	}

	info = mallinfo();

	if (prev > 0)
	{
		command_print(cmd_ctx, "Diff:            %d", prev - info.fordblks);
	}
	prev = info.fordblks;

	command_print(cmd_ctx, "Available ram:   %d", info.fordblks );

	return ERROR_OK;
}

static bool savePower;

static void setPower(bool power)
{
	savePower = power;
	if (power)
	{
oharboe's avatar
wip    
oharboe committed
362
		HAL_WRITE_UINT32(ZY1000_JTAG_BASE+0x14, 0x8);
363
364
	} else
	{
oharboe's avatar
wip    
oharboe committed
365
		HAL_WRITE_UINT32(ZY1000_JTAG_BASE+0x10, 0x8);
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
	}
}

int handle_power_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	if (argc > 1)
	{
		return ERROR_INVALID_ARGUMENTS;
	}

	if (argc == 1)
	{
		if (strcmp(args[0], "on") == 0)
		{
			setPower(1);
		}
		else if (strcmp(args[0], "off") == 0)
		{
			setPower(0);
		} else
		{
			command_print(cmd_ctx, "arg is \"on\" or \"off\"");
			return ERROR_INVALID_ARGUMENTS;
		}
	}

	command_print(cmd_ctx, "Target power %s", savePower ? "on" : "off");

	return ERROR_OK;
}

int handle_append_command(struct command_context_s *cmd_ctx, char *cmd,
		char **args, int argc)
{
	if (argc < 1)
	{
		command_print(cmd_ctx,
				"append <filename> [<string1>, [<string2>, ...]]");
		return ERROR_INVALID_ARGUMENTS;
	}

	FILE *config_file = NULL;
	config_file = fopen(args[0], "a");
	if (config_file != NULL)
	{
		int i;
		fseek(config_file, 0, SEEK_END);

		for (i = 1; i < argc; i++)
		{
			fwrite(args[i], strlen(args[i]), 1, config_file);
			if (i != argc - 1)
			{
				fwrite(" ", 1, 1, config_file);
			}
		}
		fwrite("\n", 1, 1, config_file);
		fclose(config_file);
	}

	return ERROR_OK;
}

extern int telnet_socket;

int readMore(int fd, void *data, int length)
{
	/* used in select() */
	fd_set read_fds;

	/* monitor sockets for acitvity */
	int fd_max = 1;
	FD_ZERO(&read_fds);
	/* listen for new connections */
	FD_SET(fd, &read_fds);

	// Maximum 5 seconds.
	struct timeval tv;
	tv.tv_sec = 5;
	tv.tv_usec = 0;

	int retval = select(fd_max + 1, &read_fds, NULL, NULL, &tv);
	if (retval == 0)
	{
		diag_printf("Timed out waiting for binary payload\n");
		return -1;
	}
	if (retval != 1)
		return -1;

	return read_socket(fd, data, length);
}

int readAll(int fd, void *data, int length)
{
	int pos = 0;
	for (;;)
	{
		int actual = readMore(fd, ((char *) data) + pos, length - pos);
		//		diag_printf("Read %d bytes(pos=%d, length=%d)\n", actual, pos, length);
		if (actual <= 0)
			return -1;
		pos += actual;
		if (pos == length)
			break;
	}
	return length;
}

int handle_peek_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	cyg_uint32 value;
	if (argc != 1)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
	HAL_READ_UINT32(strtoul(args[0], NULL, 0), value);
	command_print(cmd_ctx, "0x%x : 0x%x", strtoul(args[0], NULL, 0), value);
	return ERROR_OK;
}

int handle_poke_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	if (argc != 2)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
	HAL_WRITE_UINT32(strtoul(args[0], NULL, 0), strtoul(args[1], NULL, 0));
	return ERROR_OK;
}

int handle_cp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	if (argc != 2)
	{
		return ERROR_INVALID_ARGUMENTS;
	}

	// NOTE!!! we only have line printing capability so we print the entire file as a single line.
	void *data;
	int len;

	int retval = loadFile(args[0], &data, &len);
	if (retval != ERROR_OK)
		return retval;

	FILE *f = fopen(args[1], "wb");
	if (f == NULL)
		retval = ERROR_INVALID_ARGUMENTS;

	int pos = 0;
	for (;;)
	{
		int chunk = len - pos;
		static const int maxChunk = 512 * 1024; // ~1/sec
		if (chunk > maxChunk)
		{
			chunk = maxChunk;
		}

		if ((retval==ERROR_OK)&&(fwrite(((char *)data)+pos, 1, chunk, f)!=chunk))
			retval = ERROR_INVALID_ARGUMENTS;

		if (retval != ERROR_OK)
		{
			break;
		}

		command_print(cmd_ctx, "%d", len - pos);

		pos += chunk;

		if (pos == len)
			break;
	}

	if (retval == ERROR_OK)
	{
		command_print(cmd_ctx, "Copied %s to %s", args[0], args[1]);
	} else
	{
		command_print(cmd_ctx, "Failed: %d", retval);
	}

	if (data != NULL)
		free(data);
	if (f != NULL)
		fclose(f);

	if (retval != ERROR_OK)
		unlink(args[1]);

	return retval;
}

#ifdef CYGPKG_PROFILE_GPROF
extern void start_profile();

int eCosBoard_handle_eCosBoard_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	command_print(cmd_ctx, "Profiling started");
	start_profile();
	return ERROR_OK;
}

#endif

externC void phi_init_all_network_interfaces();

command_context_t *cmd_ctx;

static bool webRunning = false;

void keep_webserver()
{
	// Target initialisation is only attempted at startup, so we sleep forever and
	// let the http server bail us out(i.e. get config files set up).
	diag_printf("OpenOCD has invoked exit().\n"
		"Use web server to correct any configuration settings and reboot.\n");
	if (!webRunning)
		reboot();

	// exit() will terminate the current thread and we we'll then sleep eternally or
	// we'll have a reboot scheduled.
}

extern void printDccChar(char c);

static char logBuffer[128 * 1024];
static const int logSize = sizeof(logBuffer);
int writePtr = 0;
int logCount = 0;

void _zylinjtag_diag_write_char(char c, void **param)
{
	if (writeLog)
	{
		logBuffer[writePtr] = c;
		writePtr = (writePtr + 1) % logSize;
		logCount++;
	}
	if (serialLog)
	{
		if (c == '\n')
		{
			HAL_DIAG_WRITE_CHAR('\r');
		}
		HAL_DIAG_WRITE_CHAR(c);
	}

oharboe's avatar
oharboe committed
616
#ifdef CYGPKG_HAL_ZYLIN_PHI
617
	printDccChar(c);
oharboe's avatar
oharboe committed
618
#endif
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
}

#define SHOW_RESULT(a, b) diag_printf(#a " failed %d\n", (int)b)

#define IOSIZE 512
static void copyfile(char *name2, char *name1)
{

	int err;
	char buf[IOSIZE];
	int fd1, fd2;
	ssize_t done, wrote;

	fd1 = open(name1, O_WRONLY | O_CREAT);
	if (fd1 < 0)
		SHOW_RESULT( open, fd1 );

	fd2 = open(name2, O_RDONLY);
	if (fd2 < 0)
		SHOW_RESULT( open, fd2 );

	for (;;)
	{
		done = read(fd2, buf, IOSIZE );
		if (done < 0)
		{
			SHOW_RESULT( read, done );
			break;
		}

        if( done == 0 ) break;

		wrote = write(fd1, buf, done);
        if( wrote != done ) SHOW_RESULT( write, wrote );

        if( wrote != done ) break;
	}

	err = close(fd1);
    if( err < 0 ) SHOW_RESULT( close, err );

	err = close(fd2);
    if( err < 0 ) SHOW_RESULT( close, err );

}
664
static void copydir(char *name, char *destdir)
665
666
667
668
{
	int err;
	DIR *dirp;

669
670
671
672
673
674
675
676
	dirp = opendir(destdir);
	if (dirp==NULL)
	{
		mkdir(destdir, 0777);
	} else
	{
		err = closedir(dirp);
	}
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714

	dirp = opendir(name);
    if( dirp == NULL ) SHOW_RESULT( opendir, -1 );

	for (;;)
	{
		struct dirent *entry = readdir(dirp);

		if (entry == NULL)
			break;

		if (strcmp(entry->d_name, ".") == 0)
			continue;
		if (strcmp(entry->d_name, "..") == 0)
			continue;

		bool isDir = false;
		struct stat buf;
		char fullPath[PATH_MAX];
		strncpy(fullPath, name, PATH_MAX);
		strcat(fullPath, "/");
		strncat(fullPath, entry->d_name, PATH_MAX - strlen(fullPath));

		if (stat(fullPath, &buf) == -1)
		{
			diag_printf("unable to read status from %s", fullPath);
			break;
		}
		isDir = S_ISDIR(buf.st_mode) != 0;

		if (isDir)
			continue;

		//        diag_printf("<INFO>: entry %14s",entry->d_name);
		char fullname[PATH_MAX];
		char fullname2[PATH_MAX];

		strcpy(fullname, name);
715
		strcat(fullname, "/");
716
717
		strcat(fullname, entry->d_name);

718
719
		strcpy(fullname2, destdir);
		strcat(fullname2, "/");
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
		strcat(fullname2, entry->d_name);
		//        diag_printf("from %s to %s\n", fullname, fullname2);
		copyfile(fullname, fullname2);

		//       diag_printf("\n");
	}

	err = closedir(dirp);
    if( err < 0 ) SHOW_RESULT( stat, err );
}

#if 0
MTAB_ENTRY( romfs_mte1,
		"/rom",
		"romfs",
		"",
		(CYG_ADDRWORD) &filedata[0] );
#endif

void openocd_sleep_prelude()
{
	cyg_mutex_unlock(&httpstate.jim_lock);
}

void openocd_sleep_postlude()
{
	cyg_mutex_lock(&httpstate.jim_lock);
}

oharboe's avatar
wip    
oharboe committed
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810

void format(void)
{
	diag_printf("Formatting JFFS2...\n");

	cyg_io_handle_t handle;

	Cyg_ErrNo err;
	err = cyg_io_lookup(CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1, &handle);
	if (err != ENOERR)
	{
		diag_printf("Flash Error cyg_io_lookup: %d\n", err);
		reboot();
	}


	cyg_uint32 len;
	cyg_io_flash_getconfig_devsize_t ds;
	len = sizeof (ds);
	err = cyg_io_get_config(handle,
				CYG_IO_GET_CONFIG_FLASH_DEVSIZE, &ds, &len);
	if (err != ENOERR)
	{
		diag_printf("Flash error cyg_io_get_config %d\n", err);
		reboot();
	}

	cyg_io_flash_getconfig_erase_t e;
	void *err_addr;
	len = sizeof (e);

	e.offset = 0;
	e.len = ds.dev_size;
	e.err_address = &err_addr;

	diag_printf("Formatting 0x%08x bytes\n", ds.dev_size);
	err = cyg_io_get_config(handle, CYG_IO_GET_CONFIG_FLASH_ERASE,
				&e, &len);
	if (err != ENOERR)
	{
		diag_printf("Flash erase error %d offset 0x%p\n", err, err_addr);
		reboot();
	}

	diag_printf("Flash formatted successfully\n");

	reboot();
}



static int
zylinjtag_Jim_Command_format_jffs2(Jim_Interp *interp,
                                   int argc,
		Jim_Obj * const *argv)
{
	if (argc != 1)
	{
		return JIM_ERR;
	}

	format();
oharboe's avatar
oharboe committed
811
	for(;;);
oharboe's avatar
wip    
oharboe committed
812
813
814
}


815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
static int
zylinjtag_Jim_Command_rm(Jim_Interp *interp,
                                   int argc,
		Jim_Obj * const *argv)
{
	int del;
	if (argc != 2)
	{
		Jim_WrongNumArgs(interp, 1, argv, "rm ?dirorfile?");
		return JIM_ERR;
	}

	del = 0;
	if (unlink(Jim_GetString(argv[1], NULL)) == 0)
		del = 1;
	if (rmdir(Jim_GetString(argv[1], NULL)) == 0)
		del = 1;

	return del ? JIM_OK : JIM_ERR;
}

836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
static int zylinjtag_Jim_Command_threads(Jim_Interp *interp, int argc,
		Jim_Obj * const *argv)
{
	cyg_handle_t thread = 0;
	cyg_uint16 id = 0;
	Jim_Obj *threads = Jim_NewListObj(interp, NULL, 0);

	/* Loop over the threads, and generate a table row for
	 * each.
	 */
	while (cyg_thread_get_next(&thread, &id))
	{
		Jim_Obj *threadObj = Jim_NewListObj(interp, NULL, 0);

		cyg_thread_info info;
		char *state_string;

		cyg_thread_get_info(thread, id, &info);

		if (info.name == NULL)
			info.name = "<no name>";

		Jim_ListAppendElement(interp, threadObj, Jim_NewStringObj(interp,
				info.name, strlen(info.name)));

		/* Translate the state into a string.
		 */
		if (info.state == 0)
			state_string = "RUN";
		else if (info.state & 0x04)
			state_string = "SUSP";
		else
			switch (info.state & 0x1b)
			{
			case 0x01:
				state_string = "SLEEP";
				break;
			case 0x02:
				state_string = "CNTSLEEP";
				break;
			case 0x08:
				state_string = "CREATE";
				break;
			case 0x10:
				state_string = "EXIT";
				break;
			default:
				state_string = "????";
				break;
			}

		Jim_ListAppendElement(interp, threadObj, Jim_NewStringObj(interp,
				state_string, strlen(state_string)));

		Jim_ListAppendElement	(interp, threadObj, Jim_NewIntObj(interp, id));
		Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp, info.set_pri));
		Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp, info.cur_pri));

		Jim_ListAppendElement(interp, threads, threadObj);
	}
	Jim_SetResult( interp, threads);

	return JIM_OK;
}


902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
static int
zylinjtag_Jim_Command_ls(Jim_Interp *interp,
                                   int argc,
		Jim_Obj * const *argv)
{
	if (argc != 2)
	{
		Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
		return JIM_ERR;
	}

	char *name = (char*) Jim_GetString(argv[1], NULL);

	DIR *dirp = NULL;
	dirp = opendir(name);
	if (dirp == NULL)
	{
		return JIM_ERR;
	}
	Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0);

	for (;;)
	{
		struct dirent *entry = NULL;
		entry = readdir(dirp);
		if (entry == NULL)
			break;

		if ((strcmp(".", entry->d_name)==0)||(strcmp("..", entry->d_name)==0))
			continue;

        Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, entry->d_name, strlen(entry->d_name)));
	}
	closedir(dirp);

	Jim_SetResult(interp, objPtr);

	return JIM_OK;
}


static int
zylinjtag_Jim_Command_getmem(Jim_Interp *interp,
                                   int argc,
		Jim_Obj * const *argv)
{
	if (argc != 3)
	{
		Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
		return JIM_ERR;
	}

	long address;
	long length;
	if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
		return JIM_ERR;
	if (Jim_GetLong(interp, argv[2], &length) != JIM_OK)
		return JIM_ERR;

	if (length < 0 && length > (4096 * 1024))
	{
		Jim_WrongNumArgs(interp, 1, argv, "getmem ?dir?");
		return JIM_ERR;
	}

	void *mem = malloc(length);
	if (mem == NULL)
		return JIM_ERR;

	target_t *target = get_current_target(cmd_ctx);

	int retval;
	int size = 1;
	int count = length;
	if ((address % 4 == 0) && (count % 4 == 0))
	{
		size = 4;
		count /= 4;
	}

	if ((retval  = target->type->read_memory(target, address, size, count, mem)) != ERROR_OK)
	{
		free(mem);
		return JIM_ERR;
	}

	Jim_Obj *objPtr = Jim_NewStringObj(interp, mem, length);
	Jim_SetResult(interp, objPtr);

	free(mem);

	return JIM_OK;
}

static int
zylinjtag_Jim_Command_peek(Jim_Interp *interp,
                                   int argc,
		Jim_Obj * const *argv)
{
For faster browsing, not all history is shown. View entire blame