Debian Codegraph @

src/main.cc

raw symbols SCIP
1
2 /*
3 * avarice - The "avarice" program.
4 * Copyright (C) 2001 Scott Finneran
5 * Copyright (C) 2002, 2003, 2004 Intel Corporation
6 * Copyright (C) 2005,2006,2007 Joerg Wunsch
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License Version 2
10 * as published by the Free Software Foundation.
11 *
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
21 *
22 * This file contains the main() & support for avrjtagd.
23 *
24 * $Id: main.cc 383 2020-08-31 22:32:42Z joerg_wunsch $
25 */
26
27
28 #include <ctype.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <termios.h>
36 #include <arpa/inet.h>
37 #include <netdb.h>
38 #include <netinet/in.h>
39 #include <unistd.h>
40 #include <netinet/tcp.h>
41 #include <fcntl.h>
42
43 #include "avarice.h"
44 #include "remote.h"
45 #include "jtag.h"
46 #include "jtag1.h"
47 #include "jtag2.h"
48 #include "jtag3.h"
49 #include "gnu_getopt.h"
50
51 bool ignoreInterrupts;
52
53 static int makeSocket(struct sockaddr_in *name)
54 {
55 int sock;
56 int tmp;
57 struct protoent *protoent;
58
59 sock = socket(PF_INET, SOCK_STREAM, 0);
60 if (sock < 0)
61 throw jtag_exception();
62
63 // Allow rapid reuse of this port.
64 tmp = 1;
65 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&tmp, sizeof(tmp)) < 0)
66 throw jtag_exception();
67
68 // Enable TCP keep alive process.
69 tmp = 1;
70 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&tmp, sizeof(tmp)) < 0)
71 throw jtag_exception();
72
73 if (bind(sock, (struct sockaddr *)name, sizeof(*name)) < 0)
74 {
75 if (errno == EADDRINUSE)
76 fprintf(stderr, "bind() failed: another server is still running on this port\n");
77 throw jtag_exception("bind() failed");
78 }
79
80 protoent = getprotobyname("tcp");
81 if (protoent == NULL)
82 {
83 fprintf(stderr, "tcp protocol unknown (oops?)");
84 throw jtag_exception();
85 }
86
87 tmp = 1;
88 if (setsockopt(sock, protoent->p_proto, TCP_NODELAY,
89 (char *)&tmp, sizeof(tmp)) < 0)
90 throw jtag_exception();
91
92 return sock;
93 }
94
95
96 static void initSocketAddress(struct sockaddr_in *name,
97 const char *hostname, unsigned short int port)
98 {
99 struct hostent *hostInfo;
100
101 memset(name, 0, sizeof(*name));
102 name->sin_family = AF_INET;
103 name->sin_port = htons(port);
104 // Try numeric interpretation (1.2.3.4) first, then
105 // hostname resolution if that failed.
106 if (inet_aton(hostname, &name->sin_addr) == 0)
107 {
108 hostInfo = gethostbyname(hostname);
109 if (hostInfo == NULL)
110 {
111 fprintf(stderr, "Unknown host %s", hostname);
112 throw jtag_exception();
113 }
114 name->sin_addr = *(struct in_addr *)hostInfo->h_addr;
115 }
116 }
117
118 static unsigned long parseJtagBitrate(const char *val)
119 {
120 char *endptr, c;
121 unsigned long v;
122
123 if (*val == '\0')
124 {
125 fprintf(stderr, "invalid number in JTAG bit rate");
126 throw jtag_exception();
127 }
128 v = strtoul(val, &endptr, 10);
129 if (*endptr == '\0')
130 return v;
131 while (isspace((unsigned char)(c = *endptr)))
132 endptr++;
133 switch (c)
134 {
135 case 'k':
136 case 'K':
137 v *= 1000UL;
138 return v;
139 case 'm':
140 case 'M':
141 v *= 1000000UL;
142 return v;
143 }
144 if (strcmp(endptr, "Hz") == 0)
145 return v;
146
147 fprintf(stderr, "invalid number in JTAG bit rate");
148 throw jtag_exception();
149 }
150
151 static void usage(const char *progname)
152 {
153 fprintf(stderr,
154 "Usage: %s [OPTION]... [[HOST_NAME]:PORT]\n\n", progname);
155 fprintf(stderr, "Options:\n");
156 fprintf(stderr,
157 " -h, --help Print this message.\n");
158 fprintf(stderr,
159 " -1, --mkI Connect to JTAG ICE mkI (default)\n");
160 fprintf(stderr,
161 " -2, --mkII Connect to JTAG ICE mkII\n");
162 fprintf(stderr,
163 " -3, --jtag3 Connect to JTAGICE3 (Firmware 2.x)\n");
164 fprintf(stderr,
165 " -4, --edbg Atmel-ICE, or JTAGICE3 (firmware 3.x), or EDBG Integrated Debugger\n");
166 fprintf(stderr,
167 " -B, --jtag-bitrate <rate> Set the bitrate that the JTAG box communicates\n"
168 " with the avr target device. This must be less\n"
169 " than 1/4 of the frequency of the target. Valid\n"
170 " values are 1000/500/250/125 kHz (mkI),\n"
171 " or 22 through 6400 kHz (mkII).\n"
172 " (default: 250 kHz)\n");
173 fprintf(stderr,
174 " -C, --capture Capture running program.\n"
175 " Note: debugging must have been enabled prior\n"
176 " to starting the program. (e.g., by running\n"
177 " avarice earlier)\n");
178 fprintf(stderr,
179 " -c, --daisy-chain <ub,ua,bb,ba> Daisy chain settings:\n"
180 " <units before, units after,\n"
181 " bits before, bits after>\n");
182 fprintf(stderr,
183 " -D, --detach Detach once synced with JTAG ICE\n");
184 fprintf(stderr,
185 " -d, --debug Enable printing of debug information.\n");
186 fprintf(stderr,
187 " -e, --erase Erase target.\n");
188 fprintf(stderr,
189 " -E, --event <eventlist> List of events that do not interrupt.\n"
190 " JTAG ICE mkII and AVR Dragon only.\n"
191 " Default is \"none,run,target_power_on,target_sleep,target_wakeup\"\n");
192 #if ENABLE_TARGET_PROGRAMMING
193 fprintf(stderr,
194 " -f, --file <filename> Specify a file for use with the --program and\n"
195 " --verify options. If --file is passed and\n"
196 " neither --program or --verify are given then\n"
197 " --program is implied.\n");
198 #endif // ENABLE_TARGET_PROGRAMMING
199 fprintf(stderr,
200 " -g, --dragon Connect to an AVR Dragon rather than a JTAG ICE.\n"
201 " This implies --mkII, but might be required in\n"
202 " addition to --debugwire when debugWire is to\n"
203 " be used.\n");
204 fprintf(stderr,
205 " -I, --ignore-intr Automatically step over interrupts.\n"
206 " Note: EXPERIMENTAL. Can not currently handle\n"
207 " devices fused for compatibility.\n");
208 fprintf(stderr,
209 " -j, --jtag <devname> Port attached to JTAG box (default: /dev/avrjtag).\n");
210 fprintf(stderr,
211 " -k, --known-devices Print a list of known devices.\n");
212 fprintf(stderr,
213 " -L, --write-lockbits <ll> Write lock bits.\n");
214 fprintf(stderr,
215 " -l, --read-lockbits Read lock bits.\n");
216 fprintf(stderr,
217 " -P, --part <name> Target device name (e.g."
218 " atmega16)\n\n");
219 #if ENABLE_TARGET_PROGRAMMING
220 fprintf(stderr,
221 " -p, --program Program the target.\n"
222 " Binary filename must be specified with --file\n"
223 " option.\n");
224 #endif // ENABLE_TARGET_PROGRAMMING
225 fprintf(stderr,
226 " -r, --read-fuses Read fuses bytes.\n");
227 fprintf(stderr,
228 " -R, --reset-srst External reset through nSRST signal.\n");
229 fprintf(stderr,
230 " -V, --version Print version information.\n");
231 #if ENABLE_TARGET_PROGRAMMING
232 fprintf(stderr,
233 " -v, --verify Verify program in device against file specified\n"
234 " with --file option.\n");
235 #endif // ENABLE_TARGET_PROGRAMMING
236 fprintf(stderr,
237 " -w, --debugwire For the JTAG ICE mkII, connect to the target\n"
238 " using debugWire protocol rather than JTAG.\n");
239 fprintf(stderr,
240 " -W, --write-fuses <eehhll> Write fuses bytes.\n");
241 fprintf(stderr,
242 " -x, --xmega AVR part is an ATxmega device, using JTAG.\n");
243 fprintf(stderr,
244 " -X, --pdi AVR part is an ATxmega device, using PDI.\n");
245 fprintf(stderr,
246 "HOST_NAME defaults to 0.0.0.0 (listen on any interface).\n"
247 "\":PORT\" is required to put avarice into gdb server mode.\n\n");
248 fprintf(stderr,
249 "Example usage:\n");
250 fprintf(stderr,
251 "\t%s --jtag /dev/ttyS0 :4242\n",
252 progname);
253 fprintf(stderr,
254 "\t%s --dragon :4242\n",
255 progname);
256 fprintf(stderr, "\n");
257 exit(0);
258 }
259
260 static int comparenames(const void *a, const void *b)
261 {
262 const jtag_device_def_type *ja = (const jtag_device_def_type *)a;
263 const jtag_device_def_type *jb = (const jtag_device_def_type *)b;
264
265 return strcmp(ja->name, jb->name);
266 }
267
268 static void knownParts()
269 {
270 fprintf(stderr, "List of known AVR devices:\n\n");
271
272 jtag_device_def_type* dev = deviceDefinitions;
273 // Count the device descriptor records.
274 size_t n = 0;
275 while (dev->name != NULL)
276 n++, dev++;
277 // For historical reasons, device definitions are not
278 // sorted. Sort them here.
279 qsort(deviceDefinitions, n, sizeof(jtag_device_def_type),
280 comparenames);
281 fprintf(stderr,
282 "%-15s %10s %8s %8s\n", "Device Name", "Device ID",
283 "Flash", "EEPROM");
284 fprintf(stderr,
285 "%-15s %10s %8s %8s\n", "---------------", "---------",
286 "-------", "-------");
287 dev = deviceDefinitions;
288 while (n-- != 0)
289 {
290 unsigned eesize = dev->eeprom_page_size * dev->eeprom_page_count;
291
292 if (eesize != 0 && eesize < 1024)
293 fprintf(stderr, "%-15s 0x%04X %4d KiB %4.1f KiB\n",
294 dev->name,
295 dev->device_id,
296 dev->flash_page_size * dev->flash_page_count / 1024,
297 eesize / 1024.0);
298 else
299 fprintf(stderr, "%-15s 0x%04X %4d KiB %4d KiB\n",
300 dev->name,
301 dev->device_id,
302 dev->flash_page_size * dev->flash_page_count / 1024,
303 eesize / 1024);
304 dev++;
305 }
306
307 exit(1);
308 }
309
310 static struct option long_opts[] = {
311 /* name, has_arg, flag, val */
312 { "mkI", 0, 0, '1' },
313 { "mkII", 0, 0, '2' },
314 { "jtag3", 0, 0, '3' },
315 { "edbg", 0, 0, '4' },
316 { "jtag-bitrate", 1, 0, 'B' },
317 { "capture", 0, 0, 'C' },
318 { "daisy-chain", 1, 0, 'c' },
319 { "detach", 0, 0, 'D' },
320 { "debug", 0, 0, 'd' },
321 { "erase", 0, 0, 'e' },
322 { "event", 1, 0, 'E' },
323 { "file", 1, 0, 'f' },
324 { "dragon", 0, 0, 'g' },
325 { "help", 0, 0, 'h' },
326 { "ignore-intr", 0, 0, 'I' },
327 { "jtag", 1, 0, 'j' },
328 { "known-devices", 0, 0, 'k' },
329 { "write-lockbits", 1, 0, 'L' },
330 { "read-lockbits", 0, 0, 'l' },
331 { "part", 1, 0, 'P' },
332 { "program", 0, 0, 'p' },
333 { "reset-srst", 0, 0, 'R' },
334 { "read-fuses", 0, 0, 'r' },
335 { "version", 0, 0, 'V' },
336 { "verify", 0, 0, 'v' },
337 { "debugwire", 0, 0, 'w' },
338 { "write-fuses", 1, 0, 'W' },
339 { "xmega", 0, 0, 'x' },
340 { "pdi", 0, 0, 'X' },
341 { 0, 0, 0, 0 }
342 };
343
344 jtag *theJtagICE;
345
346 int main(int argc, char **argv)
347 {
348 int sock;
349 struct sockaddr_in clientname;
350 struct sockaddr_in name;
351 char *inFileName = 0;
352 const char *jtagDeviceName = NULL;
353 char *device_name = 0;
354 const char *eventlist = "none,run,target_power_on,target_sleep,target_wakeup";
355 unsigned long jtagBitrate = 0;
356 const char *hostName = "0.0.0.0"; /* INADDR_ANY */
357 int hostPortNumber = 0;
358 bool erase = false;
359 bool program = false;
360 bool readFuses = false;
361 bool writeFuses = false;
362 char *fuses = NULL;
363 bool readLockBits = false;
364 bool writeLockBits = false;
365 bool gdbServerMode = false;
366 char *lockBits = NULL;
367 bool detach = false;
368 bool capture = false;
369 bool verify = false;
370 bool apply_nsrst = false;
371 bool is_xmega = false;
372 char *progname = argv[0];
373 enum {
374 MKI, MKII, DRAGON, JTAG3, EDBG
375 } devicetype = MKI; // default to mkI devicetype
376 enum debugproto proto = PROTO_JTAG;
377 int option_index;
378 unsigned int units_before = 0;
379 unsigned int units_after = 0;
380 unsigned int bits_before = 0;
381 unsigned int bits_after = 0;
382
383 statusOut("AVaRICE version %s, %s %s\n\n",
384 PACKAGE_VERSION, __DATE__, __TIME__);
385
386 device_name = 0;
387
388 opterr = 0; /* disable default error message */
389
390 while (1)
391 {
392 int c = getopt_long (argc, argv, "1234B:Cc:DdeE:f:ghIj:kL:lP:pRrVvwW:xX",
393 long_opts, &option_index);
394 if (c == -1)
395 break; /* no more options */
396
397 switch (c) {
398 case 'h':
399 case '?':
400 usage(progname);
401 case 'k':
402 knownParts();
403 case '1':
404 devicetype = MKI;
405 break;
406 case '2':
407 devicetype = MKII;
408 break;
409 case '3':
410 devicetype = JTAG3;
411 break;
412 case '4':
413 devicetype = EDBG;
414 break;
415 case 'B':
416 jtagBitrate = parseJtagBitrate(optarg);
417 break;
418 case 'C':
419 capture = true;
420 break;
421 case 'c':
422 if (sscanf(optarg,"%u,%u,%u,%u",
423 &units_before, &units_after,
424 &bits_before, &bits_after) != 4)
425 usage(progname);
426 if (units_before > bits_before || units_after > bits_after ||
427 bits_before > 32 || bits_after > 32) {
428 fprintf(stderr,
429 "%s: daisy-chain parameters out of range"
430 " (max. 32 bits before/after)\n",
431 progname);
432 exit(1);
433 }
434 break;
435 case 'D':
436 detach = true;
437 break;
438 case 'd':
439 debugMode = true;
440 break;
441 case 'e':
442 erase = true;
443 break;
444 case 'E':
445 eventlist = optarg;
446 break;
447 case 'f':
448 inFileName = optarg;
449 break;
450 case 'g':
451 devicetype = DRAGON;
452 break;
453 case 'I':
454 ignoreInterrupts = true;
455 break;
456 case 'j':
457 jtagDeviceName = optarg;
458 break;
459 case 'L':
460 lockBits = optarg;
461 writeLockBits = true;
462 break;
463 case 'l':
464 readLockBits = true;
465 break;
466 case 'P':
467 device_name = optarg;
468 break;
469 case 'p':
470 program = true;
471 break;
472 case 'R':
473 apply_nsrst = true;
474 break;
475 case 'r':
476 readFuses = true;
477 break;
478 case 'V':
479 exit(0);
480 case 'v':
481 verify = true;
482 break;
483 case 'w':
484 proto = PROTO_DW;
485 break;
486 case 'W':
487 fuses = optarg;
488 writeFuses = true;
489 break;
490 case 'x':
491 is_xmega = true;
492 break;
493 case 'X':
494 is_xmega = true;
495 proto = PROTO_PDI;
496 break;
497 default:
498 fprintf (stderr, "getop() did something screwey");
499 exit (1);
500 }
501 }
502
503 if ((optind+1) == argc) {
504 /* Looks like user has given [[host]:port], so parse out the host and
505 port number then enable gdb server mode. */
506
507 int i;
508 char *arg = argv[optind];
509 int len = strlen (arg);
510 char *host = new char[len+1];
511 memset (host, '\0', len+1);
512
513 for (i=0; i<len; i++) {
514 if ((arg[i] == '\0') || (arg[i] == ':'))
515 break;
516
517 host[i] = arg[i];
518 }
519
520 if (strlen (host)) {
521 hostName = host;
522 }
523
524 if (arg[i] == ':') {
525 i++;
526 }
527
528 if (i >= len) {
529 /* No port was given. */
530 fprintf (stderr, "avarice: %s is not a valid host:port value.\n",
531 arg);
532 exit (1);
533 }
534
535 char *endptr;
536 hostPortNumber = (int)strtol(arg+i, &endptr, 0);
537 if (endptr == arg+i) {
538 /* Invalid convertion. */
539 fprintf (stderr, "avarice: failed to convert port number: %s\n",
540 arg+i);
541 exit (1);
542 }
543
544 /* Make sure the the port value is not a priviledged port and is not
545 greater than max port value. */
546
547 if ((hostPortNumber < 1024) || (hostPortNumber > 0xffff)) {
548 fprintf (stderr, "avarice: invalid port number: %d (must be >= %d"
549 " and <= %d)\n", hostPortNumber, 1024, 0xffff);
550 exit (1);
551 }
552
553 gdbServerMode = true;
554 }
555 else if (optind != argc) {
556 usage (progname);
557 }
558
559 if (jtagBitrate == 0 && (proto == PROTO_JTAG))
560 {
561 fprintf (stdout,
562 "Defaulting JTAG bitrate to 250 kHz.\n\n");
563
564 jtagBitrate = 250000;
565 }
566
567 // Use a default device name to connect to if not specified on the
568 // command-line. If the JTAG_DEV environment variable is set, use
569 // the name given there. As the AVR Dragon can only be talked to
570 // through USB, default it to USB, but use a generic name else.
571 if (jtagDeviceName == NULL)
572 {
573 char *cp = getenv ("JTAG_DEV");
574
575 if (cp != NULL)
576 jtagDeviceName = cp;
577 else if (devicetype == DRAGON || devicetype == JTAG3 || devicetype == EDBG)
578 jtagDeviceName = "usb";
579 else
580 jtagDeviceName = "/dev/avrjtag";
581 }
582
583 if (debugMode)
584 setvbuf(stderr, NULL, _IOLBF, 0);
585
586 int rv = 0; // return value from main()
587
588 try {
589 // And say hello to the JTAG box
590 switch (devicetype) {
591 case MKI:
592 theJtagICE = new jtag1(jtagDeviceName, device_name, apply_nsrst);
593 break;
594
595 case MKII:
596 case DRAGON:
597 theJtagICE = new jtag2(jtagDeviceName, device_name, proto,
598 devicetype == DRAGON, apply_nsrst, is_xmega);
599 break;
600
601 case JTAG3:
602 theJtagICE = new jtag3(jtagDeviceName, device_name, proto,
603 apply_nsrst, is_xmega);
604 break;
605
606 case EDBG:
607 theJtagICE = new jtag3(jtagDeviceName, device_name, proto,
608 apply_nsrst, is_xmega, true);
609 break;
610 }
611
612 // Set Daisy-chain variables
613 theJtagICE->dchain.units_before = (unsigned char) units_before;
614 theJtagICE->dchain.units_after = (unsigned char) units_after;
615 theJtagICE->dchain.bits_before = (unsigned char) bits_before;
616 theJtagICE->dchain.bits_after = (unsigned char) bits_after;
617
618 // Tell which events to ignore.
619 theJtagICE->parseEvents(eventlist);
620
621 // Init JTAG box.
622 theJtagICE->initJtagBox();
623
624 if (erase)
625 {
626 if (proto == PROTO_DW)
627 {
628 statusOut("WARNING: Chip erase not possible in debugWire mode; ignored\n");
629 }
630 else
631 {
632 theJtagICE->enableProgramming();
633 statusOut("Erasing program memory.\n");
634 theJtagICE->eraseProgramMemory();
635 statusOut("Erase complete.\n");
636 theJtagICE->disableProgramming();
637 }
638 }
639
640 if (readFuses)
641 {
642 theJtagICE->jtagReadFuses();
643 }
644
645 if (readLockBits)
646 {
647 theJtagICE->jtagReadLockBits();
648 }
649
650 if (writeFuses)
651 theJtagICE->jtagWriteFuses(fuses);
652
653 // Init JTAG debugger for initial use.
654 // - If we're attaching to a running target, we cannot do this.
655 // - If we're running as a standalone programmer, we don't want
656 // this.
657 if( gdbServerMode && ( ! capture ) )
658 theJtagICE->initJtagOnChipDebugging(jtagBitrate);
659
660 if (inFileName != (char *)0)
661 {
662 #if ENABLE_TARGET_PROGRAMMING
663 if ((program == false) && (verify == false)) {
664 /* If --file is given and neither --program or --verify, then we
665 program, but not verify so as to be backward compatible with
666 the old meaning of --file from before the addition of --program
667 and --verify. */
668
669 program = true;
670 }
671
672 if ((erase == false) && (program == true)) {
673 statusOut("WARNING: The default behaviour has changed.\n"
674 "Programming no longer erases by default. If you want to"
675 " erase and program\nin a single step, use the --erase "
676 "in addition to --program. The reason for\nthis change "
677 "is to allow programming multiple sections (e.g. "
678 "application and\nbootloader) in multiple passes.\n\n");
679 }
680
681 theJtagICE->downloadToTarget(inFileName, program, verify);
682 theJtagICE->resetProgram(false);
683 #else // !ENABLE_TARGET_PROGRAMMING
684 statusOut("\n\n"
685 "AVaRICE has not been configured for target programming\n"
686 "through the --program option. Target programming in\n"
687 "AVaRICE is a deprecated feature; use AVRDUDE instead.\n");
688 rv = 1;
689 #endif // ENABLE_TARGET_PROGRAMMING
690 }
691 else
692 {
693 if( (program) || (verify))
694 {
695 #if ENABLE_TARGET_PROGRAMMING
696 fprintf(stderr,
697 "\nERROR: Filename not specified."
698 " Use the --file option.\n");
699 throw jtag_exception();
700 #else // !ENABLE_TARGET_PROGRAMMING
701 statusOut("\n\n"
702 "AVaRICE has not been configured for target programming\n"
703 "through the --program option. Target programming in\n"
704 "AVaRICE is a deprecated feature; use AVRDUDE instead.\n");
705 rv = 1;
706 #endif // ENABLE_TARGET_PROGRAMMING
707 }
708 }
709
710 // Write fuses after all programming parts have completed.
711 if (writeLockBits)
712 theJtagICE->jtagWriteLockBits(lockBits);
713
714 // Quit & resume mote for operations that don't interact with gdb.
715 if (!gdbServerMode)
716 theJtagICE->resumeProgram();
717 else
718 {
719 initSocketAddress(&name, hostName, hostPortNumber);
720 sock = makeSocket(&name);
721 statusOut("Waiting for connection on port %hu.\n", hostPortNumber);
722 if (listen(sock, 1) < 0)
723 throw jtag_exception();
724
725 if (detach)
726 {
727 int child = fork();
728
729 if (child < 0)
730 {
731 fprintf(stderr, "Failed to fork");
732 throw jtag_exception();
733 }
734 if (child != 0)
735 _exit(0);
736 else
737 if (setsid() < 0)
738 {
739 fprintf(stderr, "setsid failed - weird bug");
740 throw jtag_exception();
741 }
742 }
743
744 // Connection request on original socket.
745 socklen_t size = (socklen_t)sizeof(clientname);
746 int gfd = accept(sock, (struct sockaddr *)&clientname, &size);
747 if (gfd < 0)
748 throw jtag_exception();
749 statusOut("Connection opened by host %s, port %hu.\n",
750 inet_ntoa(clientname.sin_addr), ntohs(clientname.sin_port));
751
752 setGdbFile(gfd);
753
754 // Now do the actual processing of GDB messages
755 // We stay here until exiting because of error of EOF on the
756 // gdb connection
757 for (;;)
758 talkToGdb();
759 }
760 }
761 catch (const char *msg)
762 {
763 fprintf(stderr, "%s\n", msg);
764 return 1;
765 }
766 catch (jtag_exception&)
767 {
768 // ignored; guarantee theJtagICE object will be deleted
769 // correctly, as this says "good-bye" to the JTAG ICE mkII
770 rv = 1;
771 }
772
773 delete theJtagICE;
774
775 return rv;
776 }