Keep COPYING.NEWLIB if keep-newlib.
[deliverable/binutils-gdb.git] / gdb / remote-nrom.c
CommitLineData
33bc979d
SS
1/* Remote debugging with the XLNT Designs, Inc (XDI) NetROM.
2 Copyright 1990, 1991, 1992, 1995 Free Software Foundation, Inc.
3 Contributed by:
4 Roger Moyers
5 XLNT Designs, Inc.
6 15050 Avenue of Science, Suite 106
7 San Diego, CA 92128
8 (619)487-9320
9 roger@xlnt.com
10 Adapted from work done at Cygnus Support in remote-nindy.c,
11 later merged in by Stan Shebs at Cygnus.
12
13This file is part of GDB.
14
15This program is free software; you can redistribute it and/or modify
16it under the terms of the GNU General Public License as published by
17the Free Software Foundation; either version 2 of the License, or
18(at your option) any later version.
19
20This program is distributed in the hope that it will be useful,
21but WITHOUT ANY WARRANTY; without even the implied warranty of
22MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23GNU General Public License for more details.
24
25You should have received a copy of the GNU General Public License
26along with this program; if not, write to the Free Software
27Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
28
29#include "defs.h"
30#include "gdbcmd.h"
4887063b 31#include "serial.h"
33bc979d 32#include "target.h"
33bc979d
SS
33
34/* Default ports used to talk with the NetROM. */
35
33bc979d
SS
36#define DEFAULT_NETROM_LOAD_PORT 1236
37#define DEFAULT_NETROM_CONTROL_PORT 1237
38
e431d135 39static void nrom_close PARAMS ((int quitting));
33bc979d
SS
40
41/* New commands. */
42
33bc979d
SS
43static void nrom_passthru PARAMS ((char *, int));
44
33bc979d
SS
45/* We talk to the NetROM over these sockets. */
46
4887063b 47static serial_t load_desc = NULL;
4887063b
SG
48static serial_t ctrl_desc = NULL;
49
33bc979d 50static int load_port = DEFAULT_NETROM_LOAD_PORT;
33bc979d
SS
51static int control_port = DEFAULT_NETROM_CONTROL_PORT;
52
4887063b
SG
53static char nrom_hostname[100];
54
33bc979d
SS
55/* Forward data declaration. */
56
57extern struct target_ops nrom_ops;
58
e431d135
SG
59/* Scan input from the remote system, until STRING is found. Print chars that
60 don't match. */
4887063b 61
33bc979d 62static int
4887063b
SG
63expect (string)
64 char *string;
33bc979d 65{
4887063b
SG
66 char *p = string;
67 int c;
33bc979d 68
4887063b 69 immediate_quit = 1;
33bc979d 70
4887063b 71 while (1)
33bc979d 72 {
4887063b 73 c = SERIAL_READCHAR (ctrl_desc, 5);
33bc979d 74
4887063b 75 if (c == *p++)
33bc979d 76 {
4887063b 77 if (*p == '\0')
33bc979d 78 {
4887063b
SG
79 immediate_quit = 0;
80
81 return 0;
33bc979d
SS
82 }
83 }
4887063b
SG
84 else
85 {
86 fputc_unfiltered (c, gdb_stdout);
87 p = string;
88 if (c == *p)
89 p++;
90 }
33bc979d 91 }
4887063b
SG
92}
93
33bc979d
SS
94static void
95nrom_kill ()
96{
4887063b
SG
97 nrom_close (0);
98}
99
100static serial_t
101open_socket (name, port)
102 char *name;
103 int port;
104{
105 char sockname[100];
106 serial_t desc;
107
108 sprintf (sockname, "%s:%d", name, port);
109 desc = SERIAL_OPEN (sockname);
110 if (!desc)
111 perror_with_name (sockname);
112
113 return desc;
33bc979d
SS
114}
115
e431d135
SG
116static void
117load_cleanup ()
118{
119 SERIAL_CLOSE (load_desc);
120 load_desc = NULL;
121}
122
33bc979d
SS
123/* Download a file specified in ARGS to the netROM. */
124
125static void
126nrom_load (args, fromtty)
127 char *args;
128 int fromtty;
129{
130 int fd, rd_amt, fsize;
33bc979d
SS
131 bfd *pbfd;
132 asection *section;
133 char *downloadstring = "download 0\n";
e431d135 134 struct cleanup *old_chain;
33bc979d
SS
135
136 /* Tell the netrom to get ready to download. */
e431d135 137 if (SERIAL_WRITE (ctrl_desc, downloadstring, strlen (downloadstring)))
33bc979d
SS
138 error ("nrom_load: control_send() of `%s' failed", downloadstring);
139
4887063b 140 expect ("Waiting for a connection...\n");
33bc979d 141
4887063b 142 load_desc = open_socket (nrom_hostname, load_port);
33bc979d 143
e431d135
SG
144 old_chain = make_cleanup (load_cleanup, 0);
145
33bc979d
SS
146 pbfd = bfd_openr (args, 0);
147
148 if (pbfd)
149 {
e431d135
SG
150 make_cleanup (bfd_close, pbfd);
151
33bc979d
SS
152 if (!bfd_check_format (pbfd, bfd_object))
153 error ("\"%s\": not in executable format: %s",
154 args, bfd_errmsg (bfd_get_error ()));
155
156 for (section = pbfd->sections; section; section = section->next)
157 {
158 if (bfd_get_section_flags (pbfd, section) & SEC_ALLOC)
159 {
160 bfd_vma section_address;
161 unsigned long section_size;
162 const char *section_name;
163
164 section_name = bfd_get_section_name (pbfd, section);
165 section_address = bfd_get_section_vma (pbfd, section);
166 section_size = bfd_section_size (pbfd, section);
167
168 if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
169 {
170 file_ptr fptr;
171
172 printf_filtered ("[Loading section %s at %x (%d bytes)]\n",
173 section_name, section_address,
174 section_size);
175
176 fptr = 0;
177
178 while (section_size > 0)
179 {
180 char buffer[1024];
181 int count;
182
183 count = min (section_size, 1024);
184
185 bfd_get_section_contents (pbfd, section, buffer, fptr,
186 count);
187
4887063b 188 SERIAL_WRITE (load_desc, buffer, count);
33bc979d
SS
189 section_address += count;
190 fptr += count;
191 section_size -= count;
192 }
193 }
194 else /* BSS and such */
195 {
196 printf_filtered ("[section %s: not loading]\n",
197 section_name);
198 }
199 }
200 }
201 }
202 else
203 error ("\"%s\": Could not open", args);
204
e431d135 205 do_cleanups (old_chain);
33bc979d
SS
206}
207
208/* Open a connection to the remote NetROM devices. */
209
210static void
211nrom_open (name, from_tty)
212 char *name;
213 int from_tty;
214{
215 int errn;
216
4887063b 217 if (!name || strchr (name, '/') || strchr (name, ':'))
33bc979d
SS
218 error (
219"To open a NetROM connection, you must specify the hostname\n\
220or IP address of the NetROM device you wish to use.");
221
4887063b 222 strcpy (nrom_hostname, name);
33bc979d 223
4887063b 224 target_preopen (from_tty);
33bc979d 225
4887063b 226 unpush_target (&nrom_ops);
33bc979d 227
4887063b 228 ctrl_desc = open_socket (nrom_hostname, control_port);
33bc979d 229
4887063b 230 push_target (&nrom_ops);
33bc979d
SS
231
232 if (from_tty)
4887063b 233 printf_filtered ("Connected to NetROM device \"%s\"\n", nrom_hostname);
33bc979d
SS
234}
235
33bc979d
SS
236/* Close out all files and local state before this target loses control. */
237
238static void
239nrom_close (quitting)
240 int quitting;
241{
4887063b
SG
242 if (load_desc)
243 SERIAL_CLOSE (load_desc);
4887063b
SG
244 if (ctrl_desc)
245 SERIAL_CLOSE (ctrl_desc);
33bc979d
SS
246}
247
248/* Pass arguments directly to the NetROM. */
249
250static void
251nrom_passthru (args, fromtty)
252 char *args;
253 int fromtty;
254{
255 char buf[1024];
256
e431d135
SG
257 sprintf (buf, "%s\n", args);
258 if (SERIAL_WRITE (ctrl_desc, buf, strlen (buf)))
33bc979d
SS
259 error ("nrom_reset: control_send() of `%s'failed", args);
260}
261
33bc979d
SS
262static void
263nrom_mourn()
264{
265 unpush_target (&nrom_ops);
266 generic_mourn_inferior ();
267}
268
33bc979d
SS
269/* Define the target vector. */
270
271struct target_ops nrom_ops = {
272 "nrom", /* to_shortname */
273 "Remote XDI `NetROM' target", /* to_longname */
274 "Remote debug using a NetROM over Ethernet",
275 nrom_open, /* to_open */
e431d135
SG
276 nrom_close, /* to_close */
277 NULL, /* to_attach */
278 NULL, /* to_detach */
279 NULL, /* to_resume */
280 NULL, /* to_wait */
281 NULL, /* to_fetch_registers */
282 NULL, /* to_store_registers */
283 NULL, /* to_prepare_to_store */
284 NULL, /* to_xfer_memory */
285 NULL, /* to_files_info */
33bc979d
SS
286 NULL, /* to_insert_breakpoint */
287 NULL, /* to_remove_breakpoint */
288 NULL,
289 NULL,
290 NULL,
291 NULL,
292 NULL,
293 nrom_kill,
294 nrom_load,
295 NULL,
e431d135 296 NULL, /* to_create_inferior */
33bc979d 297 nrom_mourn,
e431d135 298 NULL, /* to_can_run */
33bc979d
SS
299 0, /* to_notice_signals */
300 0,
4887063b 301 download_stratum, /* to_stratum */
33bc979d
SS
302 NULL, /* to_next */
303 1,
304 1,
305 1,
306 1,
307 0, /* to_has_execution */
308 NULL, /* sections */
309 NULL, /* sections_end */
310 OPS_MAGIC /* to_magic */
311};
312
313void
314_initialize_remote_nrom ()
315{
316 add_target (&nrom_ops);
317
e431d135
SG
318 add_show_from_set (
319 add_set_cmd ("nrom_load_port", no_class, var_zinteger, (char *)&load_port,
320 "Set the port to use for NetROM downloads\n", &setlist),
321 &showlist);
322
323 add_show_from_set (
324 add_set_cmd ("nrom_control_port", no_class, var_zinteger, (char *)&control_port,
325 "Set the port to use for NetROM debugger services\n", &setlist),
326 &showlist);
33bc979d
SS
327
328 add_cmd ("nrom", no_class, nrom_passthru,
329 "Pass arguments as command to NetROM",
330 &cmdlist);
331}
This page took 0.048628 seconds and 4 git commands to generate.