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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
362
363
364
365
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
616
617
618
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
664
665
666
667
668
669
670
671
672
673
674
675
676
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
715
716
717
718
719
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
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
|
/* Remote debugging with the XLNT Designs, Inc (XDI) NetROM.
Copyright 1990, 1991, 1992, 1995 Free Software Foundation, Inc.
Contributed by:
Roger Moyers
XLNT Designs, Inc.
15050 Avenue of Science, Suite 106
San Diego, CA 92128
(619)487-9320
roger@xlnt.com
Adapted from work done at Cygnus Support in remote-nindy.c,
later merged in by Stan Shebs at Cygnus.
This file is part of GDB.
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "defs.h"
#include "gdbcmd.h"
#include <string.h>
#include "inferior.h"
#include "wait.h"
#include "value.h"
#include <ctype.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <stropts.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include "terminal.h"
#include "target.h"
#include "gdbcore.h"
/* Packet header found on every packet sent to/from GDB. */
typedef struct gpkthdr {
unsigned char csum; /* Check sum */
unsigned char seq_num; /* Sequence number */
unsigned short len; /* Number of bytes in packet */
unsigned char cmd; /* GDB command */
unsigned char pad[3];
unsigned long addr; /* Address if needed */
unsigned long datalen; /* # of bytes to read/write */
} GPKTHDR;
#define GDB_START_DELIMITER '['
#define GDB_END_DELIMITER ']'
/* GDB requests. */
#define GDB_QUERY_CMD 0x01
#define GDB_STATUS 0x02
#define GDB_READ_REGS 0x03
#define GDB_WRITE_REGS 0x04
#define GDB_READ_MEM 0x05
#define GDB_WRITE_MEM 0x06
#define GDB_CONTINUE 0x07
#define GDB_STEP 0x08
/* Responses. */
#define GDB_ACK 0x11
#define GDB_NACK 0x12
#define GDB_READ_REG_RESP 0x13
#define GDB_READ_MEM_RESP 0x14
#define GDB_WRITE_REG_RESP 0x15
#define GDB_WRITE_MEM_RESP 0x16
#define GDB_BP_TRAP "05"
#define GDB_BUSERR_TRAP "10"
#define GDB_ILLOP_TRAP "40"
#define GDB_ACK_VALUE "OK"
/* Default ports used to talk with the NetROM. */
#define DEFAULT_NETROM_TARGET_PORT 1235
#define DEFAULT_NETROM_LOAD_PORT 1236
#define DEFAULT_NETROM_CONTROL_PORT 1237
#define UC(b) (((long)b)&0xff)
#define NROM_BUF_SIZE 2048
#define MAX_SEND_ATTEMPTS 10
#define READ_BUF_SIZE 2048
/* Definitions for filetype. */
#define BINARY_FTYPE 0
#define MOTO_SREC 1
#define INTEL_HEX 2
#if 0 /* for debugging, if anyone cares */
static char *GCMDTYPE[] = {
"ZERO",
"GDB_QUERY",
"GDB_STATUS",
"GDB_READ_REGS",
"GDB_WRITE_REGS",
"GDB_READ_MEM",
"GDB_WRITE_MEM",
"GDB_CONTINUE",
"GDB_STEP",
"CMD9",
"CMDA",
"CMDB",
"CMDC",
"CMDD",
"CMDE",
"CMDF",
"RESP0",
"GDB_ACK",
"GDB_NACK",
"GDB_READ_REG_RESP",
"GDB_READ_MEM_RESP",
"GDB_WRITE_REG_RESP",
"GDB_WRITE_MEM_RESP"
};
#endif
static void nrom_attach PARAMS ((char *, int));
static int nrom_can_run PARAMS ((void));
static void nrom_close PARAMS ((int));
static void nrom_create_inferior PARAMS ((char *, char *, char **));
static void nrom_detach PARAMS ((char *, int));
static void nrom_fetch_registers PARAMS ((int));
static void nrom_files_info PARAMS ((struct target_ops *));
static void nrom_kill PARAMS ((void));
static void nrom_load PARAMS ((char *, int));
static void nrom_mourn PARAMS ((void));
static void nrom_open PARAMS ((char *,int));
static void nrom_resume PARAMS ((int, int, enum target_signal));
static void nrom_prepare_to_store PARAMS ((void));
static void nrom_store_registers PARAMS ((int));
static int nrom_wait PARAMS ((int pid, struct target_waitstatus *status));
static int nrom_xfer_inferior_memory PARAMS ((CORE_ADDR, char *, int, int,
struct target_ops *));
static int nrom_write_inferior_memory PARAMS ((CORE_ADDR, char *, int));
static int nrom_read_inferior_memory PARAMS ((CORE_ADDR, char *, int));
/* New commands. */
static void nrom_set_target_port PARAMS ((char *, int));
static void nrom_set_control_port PARAMS ((char *, int));
static void nrom_set_load_port PARAMS ((char *, int));
static void nrom_set_ipaddr PARAMS ((char *, int));
static void nrom_set_filetype PARAMS ((char *, int));
static void nrom_show_status PARAMS ((char *, int));
static void nrom_passthru PARAMS ((char *, int));
/* Packet functions. */
static void build_pkt PARAMS ((int, unsigned char *, long,
unsigned char *, unsigned long, unsigned long,
int));
static int compute_csum PARAMS ((unsigned char *, int));
#if 0
static void dump_pkt PARAMS ((GPKTHDR *, unsigned char *));
#endif
static int get_seq_number PARAMS ((void));
static char *hex2mem PARAMS ((char *, char *, int));
static char *mem2hex PARAMS ((char *, char *, int));
static void nrom_send PARAMS ((int, char *, int, long, long, char *));
static void send_query_cmd PARAMS ((void));
static int send_pkt PARAMS ((int, char *, int, long, int));
static int read_pkt PARAMS ((char *));
static void send_query_cmd PARAMS ((void));
static int tohex PARAMS ((int));
static int parse_pkt PARAMS ((unsigned char *, GPKTHDR *, char *));
static int writen PARAMS ((int, char *, int));
/* Private globals. */
/* We talk to the NetROM over these sockets. */
static int nrom_load_sock = -1;
static int nrom_targ_sock = -1;
static int nrom_ctrl_sock = -1;
/* For binding to the socket we ned a sockaddr_in structure. */
static struct sockaddr_in nrom_sin;
/* The IP Address of the NetROM is needed so we know who to talk to. */
static unsigned long nrom_ipaddr = 0;
static int load_port = DEFAULT_NETROM_LOAD_PORT;
static int target_port = DEFAULT_NETROM_TARGET_PORT;
static int control_port = DEFAULT_NETROM_CONTROL_PORT;
static int nrom_filetype = BINARY_FTYPE;
static unsigned char host_seq_num = 0;
static char hexchars[] = "0123456789abcdef";
static char freadbuf[READ_BUF_SIZE];
static char readbuf[NROM_BUF_SIZE];
static int bufdata = 0;
static int bufindex = 0;
static char workbuf[NROM_BUF_SIZE];
static char sendbuf[NROM_BUF_SIZE];
/* Forward data declaration. */
extern struct target_ops nrom_ops;
/* This routine builds a packet to send to gdb running on the host. */
static void
build_pkt (cmd, data, datalen, pkt, addr, len, seq)
int cmd;
unsigned char *data;
long datalen;
unsigned char *pkt;
unsigned long addr;
unsigned long len;
int seq;
{
GPKTHDR phdr;
char *dptr;
char *pktptr;
unsigned char csum;
int plen;
phdr.csum = 0;
phdr.len = sizeof(GPKTHDR) + datalen;
phdr.seq_num = seq;
phdr.cmd = cmd;
phdr.pad[0] = phdr.pad[1] = phdr.pad[2] = 0;
phdr.addr = addr;
phdr.datalen = len;
/* Convert packet header to ASCII. */
dptr = mem2hex ((char *) &phdr, pkt, sizeof(GPKTHDR));
/* Calculate pkt length now that it is converted. */
plen = (int) ((unsigned long)dptr - (unsigned long)pkt) + datalen;
/* Put data in packet. */
if (datalen > 0)
memcpy (dptr, data, datalen);
/* Compute checksum. For computing checksum we skip first two bytes
since this is where the checksum will go. */
pktptr = pkt + 2;
csum = compute_csum (pktptr, plen - 2);
*pkt++ = hexchars[csum >> 4];
*pkt++ = hexchars[csum % 16];
dptr += datalen;
*dptr = '\0';
}
static int
compute_csum (data, len)
unsigned char *data;
int len;
{
unsigned char csum;
csum = 0;
while (len > 0)
{
csum += *data++;
--len;
}
return csum;
}
#if 0 /* for debugging, if anyone cares */
static void
dump_pkt (hdr, data)
GPKTHDR *hdr;
unsigned char *data;
{
int i;
printf_filtered ("PACKET: csum = %x,seq = %d,len = %d\n",hdr->csum,hdr->seq_num,
hdr->len);
printf_filtered ("cmd = %s,addr = %x, datalen = %d\n", GCMDTYPE[hdr->cmd],
hdr->addr,hdr->datalen);
if (hdr->datalen)
{
for (i = 0; i < hdr->datalen * 2; i++)
printf_filtered ("%x",data[i]);
printf_filtered ("\n");
}
}
#endif
static int
get_seq_number()
{
return host_seq_num++;
}
int
hex (ch)
int ch;
{
if ((ch >= 'a') && (ch <= 'f'))
return (ch - 'a' + 10);
if((ch >= 'A') && (ch <= 'F'))
return ((ch - 'A') + 10);
if ((ch >= '0') && (ch <= '9'))
return (ch - '0');
return 0;
}
/* Convert the hex array pointed to by buf into binary to be placed in mem
return a pointer to the character AFTER the last byte written. */
static char*
hex2mem(buf, mem, count)
char* buf;
char* mem;
int count;
{
int i;
unsigned char ch;
for (i = 0; i < count; i++)
{
ch = hex(*buf++) << 4;
ch = ch + hex(*buf++);
*mem++ = ch;
}
return mem;
}
/* Convert the memory pointed to by mem into hex, placing result in buf
return a pointer to the last char put in buf (null) */
static char *
mem2hex (mem, buf, count)
char* mem;
char* buf;
int count;
{
int i;
unsigned char ch;
for (i = 0; i < count; i++)
{
ch = *mem++;
*buf++ = hexchars[ch >> 4];
*buf++ = hexchars[ch % 16];
}
*buf = 0;
return buf;
}
static int
nrom_control_send (s, nbytes)
char *s;
int nbytes;
{
long len;
char buf[10];
/* clear leading characters */
/* FIXME: The ioctl uses here seem bogus to me. -sts */
len = 1;
while (len > 0)
{
if (ioctl (nrom_ctrl_sock, FIONREAD, &len) < 0)
{
perror ("nrom_control_send ioctl");
return (-1);
}
if (len > 0)
{
if (read (nrom_ctrl_sock, buf, 1) < 0)
{
perror ("nrom_control_send read");
return (-1);
}
}
}
if (remote_debug)
printf_filtered ("nrom_control_send: sending '%s' (%d bytes) to NetROM\n",
s, nbytes);
if (writen (nrom_ctrl_sock, s, nbytes) < 0)
{
perror ("nrom_control_send");
return (-1);
}
/* clear trailing characters */
len = 1;
while (len > 0)
{
if (ioctl (nrom_ctrl_sock, FIONREAD, &len) < 0)
{
perror ("nrom_control_send ioctl");
return (-1);
}
if (len > 0)
{
if (read (nrom_ctrl_sock, buf, 1) < 0)
{
perror ("nrom_control_send read");
return (-1);
}
}
}
return 0;
}
static void
nrom_kill ()
{
}
/* Download a file specified in ARGS to the netROM. */
static void
nrom_load (args, fromtty)
char *args;
int fromtty;
{
int fd, rd_amt, fsize;
struct sockaddr_in sin;
bfd *pbfd;
asection *section;
char *downloadstring = "download 0\n";
/* Tell the netrom to get ready to download. */
if (nrom_control_send (downloadstring, strlen (downloadstring)) < 0)
error ("nrom_load: control_send() of `%s' failed", downloadstring);
/* Wait for the download daemon to start up. */
sleep (1);
nrom_load_sock = socket (AF_INET, SOCK_STREAM, 0);
if (nrom_load_sock == -1)
error ("Could not create download socket, error %d", errno);
memset (&sin, 0, sizeof(struct sockaddr_in));
sin.sin_family = AF_INET;
sin.sin_port = htons (load_port);
sin.sin_addr.s_addr = htonl (nrom_ipaddr);
if (connect (nrom_load_sock, &sin, sizeof(sin)) == -1)
error ("Connect failed, error %d", errno);
pbfd = bfd_openr (args, 0);
if (pbfd)
{
if (!bfd_check_format (pbfd, bfd_object))
error ("\"%s\": not in executable format: %s",
args, bfd_errmsg (bfd_get_error ()));
for (section = pbfd->sections; section; section = section->next)
{
if (bfd_get_section_flags (pbfd, section) & SEC_ALLOC)
{
bfd_vma section_address;
unsigned long section_size;
const char *section_name;
section_name = bfd_get_section_name (pbfd, section);
section_address = bfd_get_section_vma (pbfd, section);
section_size = bfd_section_size (pbfd, section);
if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
{
file_ptr fptr;
printf_filtered ("[Loading section %s at %x (%d bytes)]\n",
section_name, section_address,
section_size);
fptr = 0;
while (section_size > 0)
{
char buffer[1024];
int count;
count = min (section_size, 1024);
bfd_get_section_contents (pbfd, section, buffer, fptr,
count);
writen (nrom_load_sock, buffer, count);
section_address += count;
fptr += count;
section_size -= count;
}
}
else /* BSS and such */
{
printf_filtered ("[section %s: not loading]\n",
section_name);
}
}
}
}
else
error ("\"%s\": Could not open", args);
close (nrom_load_sock);
}
/* This is called not only when we first attach, but also when the
user types "run" after having attached. */
static void
nrom_create_inferior (execfile, args, env)
char *execfile;
char *args;
char **env;
{
}
/* Open a connection to the remote NetROM devices. */
static void
nrom_open (name, from_tty)
char *name;
int from_tty;
{
int errn;
if (name)
nrom_set_ipaddr (name, from_tty);
else if (nrom_ipaddr == 0)
error (
"To open a NetROM connection, you must specify the hostname\n\
or IP address of the NetROM device you wish to use.");
push_target (&nrom_ops);
/* Create the socket used for talking with the target. */
nrom_targ_sock = socket (AF_INET, SOCK_STREAM, 0);
/* Bind the socket. */
nrom_sin.sin_family = AF_INET;
nrom_sin.sin_port = htons (target_port);
nrom_sin.sin_addr.S_un.S_addr = htonl (nrom_ipaddr);
/* Connect to the remote host. */
if (connect (nrom_targ_sock, &nrom_sin, sizeof(nrom_sin)) == -1)
error ("Connect failed, error %d", errno);
/* Create the socket used for talking with the debugger services. */
nrom_ctrl_sock = socket (AF_INET, SOCK_STREAM, 0);
/* Bind the socket. */
nrom_sin.sin_family = AF_INET;
nrom_sin.sin_port = htons (control_port);
nrom_sin.sin_addr.S_un.S_addr = htonl (nrom_ipaddr);
/* Connect to the remote host. */
if (connect (nrom_ctrl_sock, &nrom_sin, sizeof(nrom_sin)) == -1)
{
errn = errno;
close (nrom_targ_sock);
error ("Connect control_socket failed, error %d", errn);
}
if (from_tty)
{
unsigned char *i;
printf_filtered ("Connected to NetROM device \"%s\"", name);
i = (unsigned char *) &nrom_ipaddr;
printf_filtered (" (%d.%d.%d.%d)\n",
UC(i[0]), UC(i[1]), UC(i[2]), UC(i[3]));
}
}
static int
nrom_can_run ()
{
return 1;
}
/* Close out all files and local state before this target loses control. */
static void
nrom_close (quitting)
int quitting;
{
}
/* Attach to the target that is already loaded and possibly running */
static void
nrom_attach (args, from_tty)
char *args;
int from_tty;
{
int nwaiting;
char buf[10];
if (from_tty)
printf_filtered ("Attaching to NetROM\n");
/* clear any pending data on the socket */
printf_filtered ("Waiting for pending data to arrive... ");
fflush(stdout);
sleep(1);
printf_filtered ("that's long enough!\n");
while (1)
{
if (ioctl(nrom_targ_sock, FIONREAD, &nwaiting) != 0)
{
/* couldn't determine data left */
perror("nrom_attach: ioctl FIONREAD");
break;
}
else if (nwaiting > 0)
{
/* flush incoming data */
while (nwaiting != 0)
{
if (read (nrom_targ_sock, buf, 1) < 0)
{
perror("nrom_attach: read");
exit(1);
}
if (remote_debug > 2)
putc(buf[0], stdout);
nwaiting--;
}
}
else
{
/* no more data */
break;
}
}
printf_filtered ("Pending data removed\n");
/* We will get a task spawn event immediately. */
send_query_cmd ();
target_has_execution = 1;
/*
start_remote();
*/
}
/* Terminate the open connection to the remote debugger. Use this
when you want to detach and do something else with your gdb. */
static void
nrom_detach (args, from_tty)
char *args;
int from_tty;
{
pop_target ();
if (from_tty)
printf_filtered ("Ending remote debugging\n");
}
/* Tell the remote machine to resume. */
static void
nrom_prepare_to_store()
{
}
static void
nrom_resume (pid, step, siggnal)
int pid, step;
enum target_signal siggnal;
{
if (step)
send_pkt (GDB_STEP, NULL, 0, 0, 0);
else
send_pkt (GDB_CONTINUE, NULL, 0, 0, 0);
}
/* Wait until the remote machine stops, then return,
storing status in STATUS just as `wait' would. */
static int
nrom_wait (pid, status)
int pid;
struct target_waitstatus *status;
{
static char pkt[NROM_BUF_SIZE], inbuf[NROM_BUF_SIZE];
GPKTHDR phdr;
status->kind = TARGET_WAITKIND_EXITED;
status->value.integer = 0;
while (1)
{
if (read_pkt (pkt) == -1)
continue;
if (parse_pkt (pkt, &phdr, inbuf) < 0)
{
if (remote_debug)
printf_filtered ("Bad packet in nrom_wait\n");
send_query_cmd ();
continue;
}
/* Got good packet. Verify command is right. */
if (phdr.cmd != GDB_STATUS)
{
/* Wrong response. Resend command. */
send_query_cmd ();
continue;
}
/* Packet is fine. Exit loop. */
return inferior_pid;
}
}
/* Read the remote registers. */
static void
nrom_fetch_registers (regno)
int regno;
{
char buf[NROM_BUF_SIZE];
char regs[REGISTER_BYTES];
int i;
#ifdef DEBUG
printf_filtered ("reg no is %d\n",regno);
#endif
nrom_send (GDB_READ_REGS, NULL, 0, -1, 0, buf);
memcpy (regs, buf, REGISTER_BYTES);
for (i = 0; i < NUM_REGS; i++)
supply_register (i, ®s[REGISTER_BYTE(i)]);
#ifdef NO_SINGLE_REG
nrom_send (GDB_READ_REGS, NULL, 0, regno, 0, buf);
if (regno != -1)
supply_register(regno,buf);
else
{
memcpy (regs, buf, REGISTER_BYTES);
for (i = 0; i < NUM_REGS; i++)
supply_register (i, ®s[REGISTER_BYTE(i)]);
}
#endif
}
static void
nrom_send (cmd, data, datalen, addr, len, resp)
int cmd;
char *data;
int datalen;
long addr;
long len;
char *resp;
{
GPKTHDR phdr;
char inbuf[NROM_BUF_SIZE];
while (1)
{
while (send_pkt (cmd, data, datalen, addr, len) < 0)
;
if (read_pkt (inbuf) != -1)
{
if (parse_pkt (inbuf, &phdr, resp) < 0)
continue;
if (phdr.cmd != GDB_NACK)
return;
}
}
}
static void
nrom_set_filetype (args, fromtty)
char *args;
int fromtty;
{
if (args[0] == 'b')
nrom_filetype = BINARY_FTYPE;
else if (args[0] == 'm')
nrom_filetype = MOTO_SREC;
else if (args[0] == 'i')
nrom_filetype = INTEL_HEX;
else
printf_filtered ("Unknown file type\n");
}
static void
nrom_set_ipaddr (args, fromtty)
char *args;
int fromtty;
{
struct hostent *h;
char buf[10];
int i,j,val;
unsigned long newip = 0;
/* First do a check to see if they typed in a hostname. */
if (!(*args))
error ("Please enter a hostname or IP address");
h = gethostbyname (args);
if (h)
{
/* It is a hostname. We just need the ipaddress. */
memcpy (&nrom_ipaddr, h->h_addr, h->h_length);
}
else
{
/* Better be in decimal dot notation,ie. xx.xx.xx.xx */
if (isdigit (args[0]) && strchr (args, '.'))
{
j = 4;
while (j)
{
memset (buf, 0, 10);
i = 0;
while((*args) && (*args != '.'))
{
buf[i] = *args++;
i++;
}
if (i)
{
val = (int) strtol (buf, NULL, 10);
if (val > 255)
error ("Invalid IP address");
j--;
newip |= val << (8 * j);
args++;
}
}
}
else
{
error ("Invalid host name/address");
}
nrom_ipaddr = newip;
}
}
static void
nrom_set_load_port (args, fromtty)
char *args;
int fromtty;
{
load_port = (int) strtol (args, NULL, 10);
}
static void
nrom_set_target_port (args, from_tty)
char *args;
int from_tty;
{
target_port = (int) strtol (args, NULL, 10);
}
static void
nrom_set_control_port (args, fromtty)
char *args;
int fromtty;
{
control_port = (int) strtol (args, NULL, 10);
}
static void
nrom_show_status (args,from_tty)
char *args;
int from_tty;
{
unsigned char *i;
i = (unsigned char *)&nrom_ipaddr;
printf_filtered ("NetROM target port is %d\n", target_port);
printf_filtered ("NetROM download port is %d\n", load_port);
printf_filtered ("NetROM debug control port is %d\n", control_port);
printf_filtered ("NetROM IP Address is %d.%d.%d.%d\n",
UC(i[0]), UC(i[1]), UC(i[2]), UC(i[3]));
if (nrom_filetype == BINARY_FTYPE)
printf_filtered ("download filetype is binary\n");
else if (nrom_filetype == MOTO_SREC)
printf_filtered ("download filetype is moto-srec\n");
else if (nrom_filetype == INTEL_HEX)
printf_filtered ("download filetype is intel-hex\n");
}
/* Pass arguments directly to the NetROM. */
static void
nrom_passthru (args, fromtty)
char *args;
int fromtty;
{
char buf[1024];
sprintf(buf, "%s\n", args);
if (nrom_control_send (buf, strlen (buf)) < 0)
error ("nrom_reset: control_send() of `%s'failed", args);
}
static void
nrom_store_registers (regno)
int regno;
{
char buf[NROM_BUF_SIZE];
int i;
char *p;
p = buf;
for (i = 0; i < REGISTER_BYTES; i++)
{
*p++ = tohex ((registers[i] >> 4) & 0xf);
*p++ = tohex (registers[i] & 0xf);
}
*p = '\0';
nrom_send (GDB_WRITE_REGS, buf, REGISTER_BYTES * 2, 0, REGISTER_BYTES * 2,
buf);
}
static int
nrom_xfer_inferior_memory (memaddr, myaddr, len, write, target)
CORE_ADDR memaddr;
char *myaddr;
int len;
int write;
struct target_ops *target;
{
if (write)
return nrom_write_inferior_memory (memaddr, myaddr, len);
else
return nrom_read_inferior_memory (memaddr, myaddr, len);
}
/* Copy LEN bytes of data from debugger memory at MYADDR
to inferior's memory at MEMADDR. Returns errno value. */
static int
nrom_write_inferior_memory (memaddr, myaddr, len)
CORE_ADDR memaddr;
char *myaddr;
int len;
{
char buf[NROM_BUF_SIZE],obuf[NROM_BUF_SIZE];
int i;
char *p;
p = obuf;
for (i = 0; i < len; i++)
{
*p++ = tohex ((myaddr[i] >> 4) & 0xf);
*p++ = tohex (myaddr[i] & 0xf);
}
*p = '\0';
nrom_send (GDB_WRITE_MEM, obuf, len * 2, memaddr, len, buf);
return len;
}
/* Read LEN bytes from inferior memory at MEMADDR. Put the result
at debugger address MYADDR. Returns errno value. */
static int
nrom_read_inferior_memory (memaddr, myaddr, len)
CORE_ADDR memaddr;
char *myaddr;
int len;
{
char buf[NROM_BUF_SIZE];
nrom_send (GDB_READ_MEM, NULL, 0, memaddr, len, buf);
memcpy (myaddr, buf, len);
return len;
}
static void
nrom_files_info (ignore)
struct target_ops *ignore;
{
}
static void
nrom_mourn()
{
unpush_target (&nrom_ops);
generic_mourn_inferior ();
}
/* Convert a packet into its parts and verify check sum. */
static int
parse_pkt (pkt, hdr, data)
unsigned char *pkt;
GPKTHDR *hdr;
char *data;
{
unsigned char xcsum;
unsigned char *dptr;
/* Build packet header from received data. */
hex2mem (pkt, (char *) hdr, sizeof(GPKTHDR));
if (remote_debug > 1)
{
printf_filtered ("Packet received: csum = %x,seq number = %x,len = %d\n",
hdr->csum,hdr->seq_num,hdr->len);
printf_filtered ("cmd = %x,addr = %x,datalen = %d\n",
hdr->cmd,hdr->addr,hdr->datalen);
}
/* Skip first two bytes of packet when computing checksum. */
dptr = (sizeof(GPKTHDR) * 2) + pkt;
pkt += 2;
if (remote_debug > 1)
{
printf_filtered ("Computing checksum over pkt %s\n",pkt);
printf_filtered ("Of length %d\n",strlen(pkt));
}
xcsum = compute_csum (pkt, strlen (pkt));
if (xcsum != hdr->csum)
{
if (remote_debug)
printf_filtered ("Checksum failure: computed %x, received %x\n",xcsum,
hdr->csum);
return (-1);
}
/* Copy data portion to callers data buffer converting from ASCII
to data as we go. */
hex2mem (dptr, data, hdr->datalen);
return 0;
}
static int
read_pkt (pkt)
char *pkt;
{
int n, tries;
struct sockaddr_in from;
int from_len = sizeof(from);
int gotstart;
int total_len;
char *p;
p = pkt;
total_len = 0;
gotstart = 0;
while (1)
{
/* Don't let us get wedged if the target is losing. */
QUIT;
if (bufdata == 0)
{
bufindex = 0;
n = NROM_BUF_SIZE;
/* Perform read on socket. This will wait. */
bufdata = recvfrom (nrom_targ_sock, readbuf, n, 0, &from, &from_len);
if (bufdata < 0)
{
printf_filtered ("Error on socket read of %d\n",errno);
return (-1);
}
if (remote_debug > 2)
{
readbuf[bufdata] = '\0';
printf_filtered ("Received %d bytes. Data is %s\n",
bufdata, readbuf);
}
}
/* skip stuff between packets */
while (gotstart == 0 && bufdata != 0
&& readbuf[bufindex] != GDB_START_DELIMITER)
{
bufdata--;
bufindex++;
}
/* look for a start if we haven't seen one */
if (gotstart == 0 && bufdata != 0
&& readbuf[bufindex] == GDB_START_DELIMITER)
{
gotstart = 1;
bufindex++;
bufdata--;
}
/* copy packet data */
if (gotstart != 0)
{
while (bufdata && readbuf[bufindex] != GDB_END_DELIMITER)
{
*p = readbuf[bufindex];
p++;
bufdata--;
bufindex++;
total_len++;
if (total_len > NROM_BUF_SIZE)
{
error ("read_pkt: packet length exceeds %d\n",
NROM_BUF_SIZE);
return (-1);
}
}
*p = '\0';
if (remote_debug > 2)
printf_filtered ("Packet is '%s'\n", pkt);
}
/* Make sure this is the end of the packet. */
if (gotstart != 0 && bufdata != 0
&& readbuf[bufindex] == GDB_END_DELIMITER)
{
gotstart = 0;
bufindex++;
bufdata--;
/* Ensure that the packet is terminated. */
*p = '\0';
if (remote_debug > 2)
printf_filtered ("Returning '%s'\n", pkt);
return 0;
}
}
}
static void
send_query_cmd ()
{
while (send_pkt (GDB_QUERY_CMD, NULL, 0, 0, 0) < 0)
;
}
static int
send_pkt (cmd, data, datalen, addr, len)
int cmd;
char *data;
int datalen;
long addr;
int len;
{
char c[2];
unsigned char seq;
struct sockaddr_in mysin;
int send_cnt;
while (1)
{
/* Get a sequence number for this packet. */
seq = get_seq_number ();
/* Build the packet. */
build_pkt (cmd, data, datalen, workbuf, addr, len, seq);
/* Put delimiters around the pkt. */
memset (sendbuf, 0, NROM_BUF_SIZE);
sendbuf[0] = GDB_START_DELIMITER;
strcat (sendbuf, workbuf);
c[0] = GDB_END_DELIMITER;
c[1] = '\0';
strcat (sendbuf, c);
/* Send the packet out on our socket. */
if (remote_debug > 1)
printf_filtered ("Sending pkt: %s\n", sendbuf);
mysin.sin_family = AF_INET;
mysin.sin_port = target_port;
mysin.sin_addr.S_un.S_addr = nrom_ipaddr;
send_cnt = 0;
while (send_cnt < MAX_SEND_ATTEMPTS)
{
if (sendto (nrom_targ_sock, sendbuf, strlen(sendbuf), 0, &mysin,
sizeof(struct sockaddr_in)) < 0)
{
printf_filtered ("sendto error of %d\n", errno);
send_cnt++;
}
else
break;
}
if (send_cnt >= MAX_SEND_ATTEMPTS)
{
printf_filtered ("Socket send failed after %d tries\n",
MAX_SEND_ATTEMPTS);
return (-1);
}
return 1;
}
}
/* Convert number NIB to a hex digit. */
static int
tohex (nib)
int nib;
{
if (nib < 10)
return '0' + nib;
else
return 'a' + nib - 10;
}
/* snatched from Stevens, pp279+ */
int
writen (sock, ptr, nbytes)
int sock;
char *ptr;
int nbytes;
{
int nleft, nwritten;
char *buf = ptr;
nleft = nbytes;
while (nleft > 0)
{
nwritten = write (sock, buf, nleft);
if (nwritten <= 0)
return nwritten;
nleft -= nwritten;
buf += nwritten;
}
return (nbytes - nleft);
}
/* Define the target vector. */
struct target_ops nrom_ops = {
"nrom", /* to_shortname */
"Remote XDI `NetROM' target", /* to_longname */
"Remote debug using a NetROM over Ethernet",
nrom_open, /* to_open */
nrom_close,
nrom_attach,
nrom_detach,
nrom_resume,
nrom_wait, /* to_wait */
nrom_fetch_registers, /* to_fetch_registers */
nrom_store_registers, /* to_store_registers */
nrom_prepare_to_store, /* to_prepare_to_store */
nrom_xfer_inferior_memory, /* to_xfer_memory */
nrom_files_info, /* to_files_info */
NULL, /* to_insert_breakpoint */
NULL, /* to_remove_breakpoint */
NULL,
NULL,
NULL,
NULL,
NULL,
nrom_kill,
nrom_load,
NULL,
nrom_create_inferior, /* to_create_inferior */
nrom_mourn,
nrom_can_run,
0, /* to_notice_signals */
0,
process_stratum, /* to_stratum */
NULL, /* to_next */
1,
1,
1,
1,
0, /* to_has_execution */
NULL, /* sections */
NULL, /* sections_end */
OPS_MAGIC /* to_magic */
};
void
_initialize_remote_nrom ()
{
add_target (&nrom_ops);
/* Add some commands for helpers. */
add_cmd ("nrom_ipaddr", no_class, nrom_set_ipaddr,
"Set the IP Address of the NetROM\n",
&setlist);
add_cmd ("target_port", no_class, nrom_set_target_port,
"Set the Port to use for NetROM target communication\n",
&setlist);
add_cmd ("load_port", no_class, nrom_set_load_port,
"Set the Port to use for NetROM downloads\n",
&setlist);
add_cmd ("control_port", no_class, nrom_set_control_port,
"Set the Port to use for NetROM debugger services\n",
&setlist);
add_cmd ("nrom_filetype", no_class, nrom_set_filetype,
"Set the filetype to use on NetROM downloads",
&setlist);
add_cmd ("nrom", no_class, nrom_show_status,
"Show the current NetROM status\n",
&showlist);
add_cmd ("nrom", no_class, nrom_passthru,
"Pass arguments as command to NetROM",
&cmdlist);
}
|