ecosboard.c 36.5 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
/***************************************************************************
 *   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

24
25
#include "log.h"
#include "types.h"
26
#include "jtag.h"
27
#include "configuration.h"
28
#include "xsvf.h"
29
#include "svf.h"
30
31
32
33
34
#include "target.h"
#include "flash.h"
#include "nand.h"
#include "pld.h"

35
#include "command.h"
36
37
38
39
#include "server.h"
#include "telnet_server.h"
#include "gdb_server.h"

40
41
42
43
44
45
46
47
48
#include <time_support.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

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

52
#include <fcntl.h>
53
#include <sys/stat.h>
54
55
#include <cyg/fileio/fileio.h>
#include <dirent.h>
56
57
58
59
60
#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
61
#include <cyg/discover/discover.h>
62
#include <cyg/hal/hal_diag.h>
63
#include <cyg/kernel/kapi.h>
64
65
#include <cyg/io/serialio.h>
#include <cyg/io/io.h>
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#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>
80
#include <ifaddrs.h>
81
82
83
84
85
#include <string.h>

#include <unistd.h>
#include <stdio.h>

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

#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


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;

114
char hwaddr[512];
115
116
117
118
119
120
121
122
123
124


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
125
126
127
static char *start_of_code=&_stext;
static char *end_of_code=&_etext;

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
146
	//	profile_on(0, &_etext, 16, 10000); // SRAM & DRAM
oharboe's avatar
oharboe committed
147
	profile_on(start_of_code, end_of_code, 16, 10000); // Nios DRAM
148
149
150
151
152
153
154
}
#endif

static FILE *log;

static char reboot_stack[2048];

155
static void zylinjtag_reboot(cyg_addrword_t data)
156
157
158
159
160
161
162
163
164
165
166
167
168
169
{
	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)
{
170
171
172
	cyg_thread_create(1, zylinjtag_reboot, (cyg_addrword_t) 0, "reboot Thread",
			(void *) reboot_stack, sizeof(reboot_stack),
			&zylinjtag_thread_handle, &zylinjtag_thread_object);
173
174
175
	cyg_thread_resume(zylinjtag_thread_handle);
}

176
177
int configuration_output_handler(struct command_context_s *context,
		const char* line)
178
179
180
181
182
183
{
	diag_printf("%s", line);

	return ERROR_OK;
}

184
185
int zy1000_configuration_output_handler_log(struct command_context_s *context,
		const char* line)
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
{
	LOG_USER_N("%s", line);

	return ERROR_OK;
}

#ifdef CYGPKG_PROFILE_GPROF

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

203
externC void phi_init_all_network_interfaces(void);
204
205
206
207
208

command_context_t *cmd_ctx;

static bool webRunning = false;

209
void keep_webserver(void)
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
{
	// 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
246
#ifdef CYGPKG_HAL_ZYLIN_PHI
247
	printDccChar(c);
oharboe's avatar
oharboe committed
248
#endif
249
250
}

251
void copyfile(char *name2, char *name1);
252

253
void copydir(char *name, char *destdir);
254
255
256
257
258
259
260
261
262

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

263
void openocd_sleep_prelude(void)
264
265
266
267
{
	cyg_mutex_unlock(&httpstate.jim_lock);
}

268
void openocd_sleep_postlude(void)
269
270
271
272
{
	cyg_mutex_lock(&httpstate.jim_lock);
}

oharboe's avatar
wip    
oharboe committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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;
289
290
	len = sizeof(ds);
	err = cyg_io_get_config(handle, CYG_IO_GET_CONFIG_FLASH_DEVSIZE, &ds, &len);
oharboe's avatar
wip    
oharboe committed
291
292
293
294
295
296
297
	if (err != ENOERR)
	{
		diag_printf("Flash error cyg_io_get_config %d\n", err);
		reboot();
	}

	cyg_io_flash_getconfig_erase_t e;
298
	len = sizeof(e);
oharboe's avatar
wip    
oharboe committed
299
300
301
302

	e.offset = 0;
	e.len = ds.dev_size;

oharboe's avatar
oharboe committed
303
	diag_printf("Formatting 0x%08x bytes\n", (int)ds.dev_size);
304
	err = cyg_io_get_config(handle, CYG_IO_GET_CONFIG_FLASH_ERASE, &e, &len);
oharboe's avatar
wip    
oharboe committed
305
306
	if (err != ENOERR)
	{
oharboe's avatar
oharboe committed
307
		diag_printf("Flash erase error %d offset 0x%08x\n", err, e.err_address);
oharboe's avatar
wip    
oharboe committed
308
309
310
311
312
313
314
315
		reboot();
	}

	diag_printf("Flash formatted successfully\n");

	reboot();
}

316
static int zylinjtag_Jim_Command_format_jffs2(Jim_Interp *interp, int argc,
oharboe's avatar
wip    
oharboe committed
317
318
319
320
321
322
323
324
		Jim_Obj * const *argv)
{
	if (argc != 1)
	{
		return JIM_ERR;
	}

	format();
325
326
	for (;;)
		;
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
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)));

383
384
385
386
387
		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));
388
389
390

		Jim_ListAppendElement(interp, threads, threadObj);
	}
391
	Jim_SetResult(interp, threads);
392
393
394
395

	return JIM_OK;
}

396
static int zylinjtag_Jim_Command_log(Jim_Interp *interp, int argc,
397
398
399
400
401
402
		Jim_Obj * const *argv)
{
	Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);

	if (logCount >= logSize)
	{
403
404
		Jim_AppendString(httpstate.jim_interp, tclOutput, logBuffer + logCount
				% logSize, logSize - logCount % logSize);
405
406
407
408
409
410
411
	}
	Jim_AppendString(httpstate.jim_interp, tclOutput, logBuffer, writePtr);

	Jim_SetResult(interp, tclOutput);
	return JIM_OK;
}

412
static int zylinjtag_Jim_Command_reboot(Jim_Interp *interp, int argc,
413
414
415
416
417
418
419
		Jim_Obj * const *argv)
{
	reboot();
	return JIM_OK;
}


420
static void zylinjtag_startNetwork(void)
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
{
	// Bring TCP/IP up immediately before we're ready to accept commands.
	//
	// That is as soon as a PING responds, we're accepting telnet sessions.
#if defined(CYGPKG_NET_FREEBSD_STACK)
	phi_init_all_network_interfaces();
#else
	lwip_init();
#endif
	if (!eth0_up)
	{
		diag_printf("Network not up and running\n");
		exit(-1);
	}
#if defined(CYGPKG_NET_FREEBSD_STACK)
	/*start TFTP*/
	tftpd_start(69, &fileops);
#endif

	cyg_httpd_init_tcl_interpreter();

	interp = httpstate.jim_interp;

444
445
446
447
448
449
450
451
	Jim_CreateCommand(httpstate.jim_interp, "log", zylinjtag_Jim_Command_log,
			NULL, NULL);
	Jim_CreateCommand(httpstate.jim_interp, "reboot",
			zylinjtag_Jim_Command_reboot, NULL, NULL);
	Jim_CreateCommand(httpstate.jim_interp, "threads",
			zylinjtag_Jim_Command_threads, NULL, NULL);
	Jim_CreateCommand(httpstate.jim_interp, "format_jffs2",
			zylinjtag_Jim_Command_format_jffs2, NULL, NULL);
452
453
454
455
456
457

	cyg_httpd_start();

	webRunning = true;

	diag_printf("Web server running\n");
oharboe's avatar
oharboe committed
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
	int s;
	struct ifreq ifr;
	s = socket(AF_INET, SOCK_DGRAM, 0);
	if (s >= 0)
	{
		strcpy(ifr.ifr_name, "eth0");
		int res;
		res = ioctl(s, SIOCGIFHWADDR, &ifr);
		close(s);

		if (res < 0)
		{
			diag_printf("Can't obtain MAC address\n");
			reboot();
		}
	}

	sprintf(hwaddr, "%02x:%02x:%02x:%02x:%02x:%02x",
			(int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[0],
			(int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[1],
			(int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[2],
			(int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[3],
			(int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[4],
			(int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[5]);

484
485
	discover_message
			= alloc_printf("ZY1000 Zylin JTAG debugger MAC %s", hwaddr);
486

oharboe's avatar
oharboe committed
487
	discover_launch();
488
489
}

490
491
static void print_exception_handler(cyg_addrword_t data, cyg_code_t exception,
		cyg_addrword_t info)
492
493
494
495
496
497
{
	writeLog = false;
	serialLog = true;
	char *infoStr = "unknown";
	switch (exception)
	{
oharboe's avatar
oharboe committed
498
#ifdef CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION
499
	case CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION:
500
501
	infoStr = "undefined instruction";
	break;
502
	case CYGNUM_HAL_VECTOR_SOFTWARE_INTERRUPT:
503
504
	infoStr = "software interrupt";
	break;
505
	case CYGNUM_HAL_VECTOR_ABORT_PREFETCH:
506
507
	infoStr = "abort prefetch";
	break;
508
	case CYGNUM_HAL_VECTOR_ABORT_DATA:
509
510
	infoStr = "abort data";
	break;
oharboe's avatar
oharboe committed
511
#endif
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
	default:
		break;
	}

	diag_printf("Exception: %08x(%s) %08x\n", exception, infoStr, info);

	diag_printf("Dumping log\n---\n");
	if (logCount >= logSize)
	{
		diag_write(logBuffer + logCount % logSize, logSize - logCount % logSize);
	}
	diag_write(logBuffer, writePtr);

	diag_printf("---\nLogdump complete.\n");
	diag_printf("Exception: %08x(%s) %08x\n", exception, infoStr, info);
	diag_printf("\n---\nRebooting\n");
	HAL_PLATFORM_RESET();

}

static void setHandler(cyg_code_t exception)
{
	cyg_exception_handler_t *old_handler;
	cyg_addrword_t old_data;

537
538
	cyg_exception_set_handler(exception, print_exception_handler, 0,
			&old_handler, &old_data);
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
}

static cyg_thread zylinjtag_uart_thread_object;
static cyg_handle_t zylinjtag_uart_thread_handle;
static char uart_stack[4096];

static char forwardBuffer[1024]; // NB! must be smaller than a TCP/IP packet!!!!!
static char backwardBuffer[1024];

void setNoDelay(int session, int flag)
{
#if 1
	// This decreases latency dramatically for e.g. GDB load which
	// does not have a sliding window protocol
	//
	// Can cause *lots* of TCP/IP packets to be sent and it would have
	// to be enabled/disabled on the fly to avoid the CPU being
	// overloaded...
	setsockopt(session, /* socket affected */
	IPPROTO_TCP, /* set option at TCP level */
	TCP_NODELAY, /* name of option */
	(char *) &flag, /* the cast is historical
	 cruft */
	sizeof(int)); /* length of option value */
#endif
}

struct
{
	int req;
	int actual;
	int req2;
	int actual2;
} tcpipSent[512 * 1024];
int cur;

575
static void zylinjtag_uart(cyg_addrword_t data)
576
577
578
579
580
581
582
583
584
585
{
	int so_reuseaddr_option = 1;

	int fd;
	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
		LOG_ERROR("error creating socket: %s", strerror(errno));
		exit(-1);
	}

586
587
	setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*) &so_reuseaddr_option,
			sizeof(int));
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629

	struct sockaddr_in sin;
	unsigned int address_size;
	address_size = sizeof(sin);
	memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = INADDR_ANY;
	sin.sin_port = htons(5555);

	if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1)
	{
		LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
		exit(-1);
	}

	if (listen(fd, 1) == -1)
	{
		LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
		exit(-1);
	}
	//	socket_nonblock(fd);


	for (;;)
	{
		int session = accept(fd, (struct sockaddr *) &sin, &address_size);
		if (session < 0)
		{
			continue;
		}

		setNoDelay(session, 1);
		int oldopts = fcntl(session, F_GETFL, 0);
		fcntl(session, F_SETFL, oldopts | O_NONBLOCK); //

		int serHandle = open("/dev/ser0", O_RDWR | O_NONBLOCK);
		if (serHandle < 0)
		{
			close(session);
			continue;
		}

oharboe's avatar
oharboe committed
630
#ifdef CYGPKG_PROFILE_GPROF
631
		start_profile();
oharboe's avatar
oharboe committed
632
#endif
oharboe's avatar
oharboe committed
633
634
635
		size_t actual = 0;
		size_t actual2 = 0;
		size_t pos, pos2;
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
664
665
666
667
		pos = 0;
		pos2 = 0;
		cur = 0;
		for (;;)
		{
			fd_set write_fds;
			fd_set read_fds;
			FD_ZERO(&write_fds);
			FD_ZERO(&read_fds);
			int fd_max = -1;
			FD_SET(session, &read_fds);
			fd_max = session;
			FD_SET(serHandle, &read_fds);
			if (serHandle > fd_max)
			{
				fd_max = serHandle;
			}
			/* Wait... */

			cyg_thread_delay(5); // 50ms fixed delay to wait for data to be sent/received
			if ((actual == 0) && (actual2 == 0))
			{
				int retval = select(fd_max + 1, &read_fds, NULL, NULL, NULL);
				if (retval <= 0)
				{
					break;
				}
			}

			if (actual2 <= 0)
			{
				memset(backwardBuffer, 's', sizeof(backwardBuffer));
oharboe's avatar
oharboe committed
668
669
				int t;
				t = read(serHandle, backwardBuffer,
670
						sizeof(backwardBuffer));
oharboe's avatar
oharboe committed
671
672
				actual2 = t;
				if (t < 0)
673
674
675
676
677
678
679
680
681
682
				{
					if (errno != EAGAIN)
					{
						goto closeSession;
					}
					actual2 = 0;
				}
				pos2 = 0;
			}

oharboe's avatar
oharboe committed
683
684
			size_t x = actual2;
			size_t y = 0;
685
686
687
688
689
690
691
692
693
694
			if (actual2 > 0)
			{
				int written = write(session, backwardBuffer + pos2, actual2);
				if (written <= 0)
					goto closeSession;
				actual2 -= written;
				pos2 += written;
				y = written;
			}

695
696
			if (FD_ISSET(session, &read_fds)
					&& (sizeof(forwardBuffer) > actual))
697
698
699
700
701
702
703
			{
				// NB! Here it is important that we empty the TCP/IP read buffer
				// to make transmission tick right
				memmove(forwardBuffer, forwardBuffer + pos, actual);
				pos = 0;
				int t;
				// this will block if there is no data at all
704
705
				t = read_socket(session, forwardBuffer + actual,
						sizeof(forwardBuffer) - actual);
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
				if (t <= 0)
				{
					goto closeSession;
				}
				actual += t;
			}

			int x2 = actual;
			int y2 = 0;
			if (actual > 0)
			{
				/* Do not put things into the serial buffer if it has something to send
				 * as that can cause a single byte to be sent at the time.
				 *
				 *
				 */
				int written = write(serHandle, forwardBuffer + pos, actual);
				if (written < 0)
				{
					if (errno != EAGAIN)
					{
						goto closeSession;
					}
					// The serial buffer is full
					written = 0;
731
732
				}
				else
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
				{
					actual -= written;
					pos += written;
				}
				y2 = written;
			}
			if (cur < 1024)
			{
				tcpipSent[cur].req = x;
				tcpipSent[cur].actual = y;
				tcpipSent[cur].req2 = x2;
				tcpipSent[cur].actual2 = y2;
				cur++;
			}

		}
749
		closeSession: close(session);
750
751
752
753
754
		close(serHandle);

		int i;
		for (i = 0; i < 1024; i++)
		{
755
756
			diag_printf("%d %d %d %d\n", tcpipSent[i].req, tcpipSent[i].actual,
					tcpipSent[i].req2, tcpipSent[i].actual2);
757
758
759
760
761
762
763
764
765

		}
	}
	close(fd);

}

void startUart(void)
{
766
767
768
	cyg_thread_create(1, zylinjtag_uart, (cyg_addrword_t) 0, "uart thread",
			(void *) uart_stack, sizeof(uart_stack),
			&zylinjtag_uart_thread_handle, &zylinjtag_uart_thread_object);
769
770
771
772
	cyg_thread_set_priority(zylinjtag_uart_thread_handle, 1); // low priority as it sits in a busy loop
	cyg_thread_resume(zylinjtag_uart_thread_handle);
}

773
774
int handle_uart_command(struct command_context_s *cmd_ctx, char *cmd,
		char **args, int argc)
775
{
776
777
778
779
780
	static int current_baud = 38400;
	if (argc == 0)
	{
		command_print(cmd_ctx, "%d", current_baud);
		return ERROR_OK;
781
782
	}
	else if (argc != 1)
783
784
785
786
	{
		return ERROR_INVALID_ARGUMENTS;
	}

787
	current_baud = atol(args[0]);
788

789
790
	int baud;
	switch (current_baud)
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
	{
	case 9600:
		baud = CYGNUM_SERIAL_BAUD_9600;
		break;
	case 19200:
		baud = CYGNUM_SERIAL_BAUD_19200;
		break;
	case 38400:
		baud = CYGNUM_SERIAL_BAUD_38400;
		break;
	case 57600:
		baud = CYGNUM_SERIAL_BAUD_57600;
		break;
	case 115200:
		baud = CYGNUM_SERIAL_BAUD_115200;
		break;
	case 230400:
		baud = CYGNUM_SERIAL_BAUD_230400;
		break;
	default:
		command_print(cmd_ctx, "unsupported baudrate");
		return ERROR_INVALID_ARGUMENTS;
	}

	cyg_serial_info_t buf;
	cyg_uint32 len = 1;
	//get existing serial configuration
	len = sizeof(cyg_serial_info_t);
	int err;
oharboe's avatar
wip    
oharboe committed
820
821
822
823
824
825
826
827
828
	cyg_io_handle_t serial_handle;

	err = cyg_io_lookup("/dev/ser0", &serial_handle);
	if (err != ENOERR)
	{
		LOG_ERROR("/dev/ser0 not found\n");
		return ERROR_FAIL;
	}

829
830
831
832
	err = cyg_io_get_config(serial_handle,
			CYG_IO_GET_CONFIG_SERIAL_OUTPUT_DRAIN, &buf, &len);
	err = cyg_io_get_config(serial_handle, CYG_IO_GET_CONFIG_SERIAL_INFO, &buf,
			&len);
833
834
835
836
837
838
839
	if (err != ENOERR)
	{
		command_print(cmd_ctx, "Failed to get serial port settings %d", err);
		return ERROR_OK;
	}
	buf.baud = baud;

840
841
	err = cyg_io_set_config(serial_handle, CYG_IO_SET_CONFIG_SERIAL_INFO, &buf,
			&len);
842
843
844
845
846
847
848
849
850
851
852
	if (err != ENOERR)
	{
		command_print(cmd_ctx, "Failed to set serial port settings %d", err);
		return ERROR_OK;
	}

	return ERROR_OK;
}

bool logAllToSerial = false;

853
854
855

int boolParam(char *var);

856

857
command_context_t *setup_command_handler(void);
858

oharboe's avatar
oharboe committed
859
static const char *zylin_config_dir="/config/settings";
860

861
862
int add_default_dirs(void)
{
863
	add_script_search_dir(zylin_config_dir);
864
865
866
867
868
	add_script_search_dir("/rom/lib/openocd");
	add_script_search_dir("/rom");
	return ERROR_OK;
}

869
870
int ioutil_init(struct command_context_s *cmd_ctx);

871
872
int main(int argc, char *argv[])
{
873
874
875
	/* ramblockdevice will be the same address every time. The deflate app uses a buffer 16mBytes out, so we
	 * need to allocate towards the end of the heap.  */

oharboe's avatar
oharboe committed
876
#ifdef CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION
877
878
879
	setHandler(CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION);
	setHandler(CYGNUM_HAL_VECTOR_ABORT_PREFETCH);
	setHandler(CYGNUM_HAL_VECTOR_ABORT_DATA);
oharboe's avatar
oharboe committed
880
#endif
881
882
883
884
885

	int err;

	atexit(keep_webserver);

886
887
888
889
	diag_init_putc(_zylinjtag_diag_write_char);
	// We want this in the log.
	diag_printf("Zylin ZY1000.\n");

890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
	err = mount("", "/ram", "ramfs");
	if (err < 0)
	{
		diag_printf("unable to mount ramfs\n");
	}
	chdir("/ram");

	char address[16];
	sprintf(address, "%p", &filedata[0]);
	err = mount(address, "/rom", "romfs");
	if (err < 0)
	{
		diag_printf("unable to mount /rom\n");
	}

	err = mount("", "/log", "logfs");
	if (err < 0)
	{
		diag_printf("unable to mount logfs\n");
	}

	err = mount("", "/tftp", "tftpfs");
	if (err < 0)
	{
		diag_printf("unable to mount logfs\n");
	}

	log = fopen("/log/log", "w");
	if (log == NULL)
	{
		diag_printf("Could not open log file /ram/log\n");
		exit(-1);
	}


925
	copydir("/rom", "/ram/cgi");
926
927
928
929

	err = mount("/dev/flash1", "/config", "jffs2");
	if (err < 0)
	{
oharboe's avatar
wip    
oharboe committed
930
931
		diag_printf("unable to mount jffs2, falling back to ram disk..\n");
		err = mount("", "/config", "ramfs");
932
		if (err < 0)
933
		{
oharboe's avatar
wip    
oharboe committed
934
			diag_printf("unable to mount /config as ramdisk.\n");
935
936
			reboot();
		}
937
938
	}
	else
oharboe's avatar
wip    
oharboe committed
939
940
941
942
943
944
945
	{
		/* are we using a ram disk instead of a flash disk? This is used
		 * for ZY1000 live demo...
		 *
		 * copy over flash disk to ram block device
		 */
		if (boolParam("ramdisk"))
946
		{
oharboe's avatar
wip    
oharboe committed
947
			diag_printf("Unmounting /config from flash and using ram instead\n");
948
			err = umount("/config");
oharboe's avatar
wip    
oharboe committed
949
950
951
952
953
			if (err < 0)
			{
				diag_printf("unable to unmount jffs\n");
				reboot();
			}
954

oharboe's avatar
wip    
oharboe committed
955
956
957
958
959
960
			err = mount("/dev/flash1", "/config2", "jffs2");
			if (err < 0)
			{
				diag_printf("unable to mount jffs\n");
				reboot();
			}
961

oharboe's avatar
oharboe committed
962
			err = mount("", "/config", "ramfs");
oharboe's avatar
wip    
oharboe committed
963
964
965
966
967
			if (err < 0)
			{
				diag_printf("unable to mount ram block device\n");
				reboot();
			}
968

969
			//		copydir("/config2", "/config");
oharboe's avatar
wip    
oharboe committed
970
971
972
973
974
			copyfile("/config2/ip", "/config/ip");
			copydir("/config2/settings", "/config/settings");

			umount("/config2");
		}
975
976
	}

977
	mkdir(zylin_config_dir, 0777);
zwelch's avatar
zwelch committed
978
	char *dirname = alloc_printf("%s/target", zylin_config_dir);
979
	mkdir(dirname, 0777);
980
	free(dirname);
zwelch's avatar
zwelch committed
981
	dirname = alloc_printf("%s/board", zylin_config_dir);
982
	mkdir(dirname, 0777);
983
	free(dirname);
zwelch's avatar
zwelch committed
984
	dirname = alloc_printf("%s/event", zylin_config_dir);
985
986
	mkdir(dirname, 0777);
	free(dirname);
987
988
989
990
991
992
993
994
995
996
997
998
999
1000

	logAllToSerial = boolParam("logserial");

	// We need the network & web server in case there is something wrong with
	// the config files that invoke exit()
	zylinjtag_startNetwork();

	/* we're going to access the jim interpreter from here on... */
	openocd_sleep_postlude();
	startUart();

	add_default_dirs();

	/* initialize commandline interface */
For faster browsing, not all history is shown. View entire blame