summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/binutils/gdb/remote-nrom.c
blob: d67791b67bb57184a95094fafbaee9fc5b15fedd (plain)
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
/* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#include "defs.h"
#include "gdbcmd.h"
#include "serial.h"
#include "target.h"

/* Default ports used to talk with the NetROM.  */

#define DEFAULT_NETROM_LOAD_PORT    1236
#define DEFAULT_NETROM_CONTROL_PORT 1237

static void nrom_close PARAMS ((int quitting));

/* New commands.  */

static void nrom_passthru PARAMS ((char *, int));

/* We talk to the NetROM over these sockets.  */

static serial_t load_desc = NULL;
static serial_t ctrl_desc = NULL;

static int load_port = DEFAULT_NETROM_LOAD_PORT;
static int control_port = DEFAULT_NETROM_CONTROL_PORT;

static char nrom_hostname[100];

/* Forward data declaration. */

extern struct target_ops nrom_ops;

/* Scan input from the remote system, until STRING is found.  Print chars that
   don't match.  */

static int
expect (string)
     char *string;
{
  char *p = string;
  int c;

  immediate_quit = 1;

  while (1)
    {
      c = SERIAL_READCHAR (ctrl_desc, 5);

      if (c == *p++)
	{
	  if (*p == '\0')
	    {
	      immediate_quit = 0;

	      return 0;
	    }
	}
      else
	{
	  fputc_unfiltered (c, gdb_stdout);
	  p = string;
	  if (c == *p)
	    p++;
	}
    }
}

static void
nrom_kill ()
{
  nrom_close (0);
}

static serial_t
open_socket (name, port)
     char *name;
     int port;
{
  char sockname[100];
  serial_t desc;

  sprintf (sockname, "%s:%d", name, port);
  desc = SERIAL_OPEN (sockname);
  if (!desc)
    perror_with_name (sockname);

  return desc;
}

static void
load_cleanup ()
{
  SERIAL_CLOSE (load_desc);
  load_desc = NULL;
}

/* Download a file specified in ARGS to the netROM.  */

static void
nrom_load (args, fromtty)
     char *args;
     int fromtty;
{
  int fd, rd_amt, fsize;
  bfd *pbfd;
  asection *section;
  char *downloadstring = "download 0\n";
  struct cleanup *old_chain;

  /* Tell the netrom to get ready to download. */
  if (SERIAL_WRITE (ctrl_desc, downloadstring, strlen (downloadstring)))
    error ("nrom_load: control_send() of `%s' failed", downloadstring);

  expect ("Waiting for a connection...\n");

  load_desc = open_socket (nrom_hostname, load_port);

  old_chain = make_cleanup (load_cleanup, 0);

  pbfd = bfd_openr (args, 0);

  if (pbfd)
    {
      make_cleanup (bfd_close, 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);

		      SERIAL_WRITE (load_desc, 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);

  do_cleanups (old_chain);
}

/* Open a connection to the remote NetROM devices.  */

static void
nrom_open (name, from_tty)
     char *name;
     int from_tty;
{
  int errn;

  if (!name || strchr (name, '/') || strchr (name, ':'))
    error (
"To open a NetROM connection, you must specify the hostname\n\
or IP address of the NetROM device you wish to use.");

  strcpy (nrom_hostname, name);

  target_preopen (from_tty);

  unpush_target (&nrom_ops);

  ctrl_desc = open_socket (nrom_hostname, control_port);

  push_target (&nrom_ops);

  if (from_tty)
    printf_filtered ("Connected to NetROM device \"%s\"\n", nrom_hostname);
}

/* Close out all files and local state before this target loses control. */

static void
nrom_close (quitting)
     int quitting;
{
  if (load_desc)
    SERIAL_CLOSE (load_desc);
  if (ctrl_desc)
    SERIAL_CLOSE (ctrl_desc);
}

/* 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 (SERIAL_WRITE (ctrl_desc, buf, strlen (buf)))
    error ("nrom_reset: control_send() of `%s'failed", args);
}

static void
nrom_mourn() 
{ 
  unpush_target (&nrom_ops);
  generic_mourn_inferior ();
}

/* 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",  /* to_doc */
  nrom_open,			/* to_open */
  nrom_close,			/* to_close */
  NULL,				/* to_attach */
  NULL,				/* to_detach */
  NULL,				/* to_resume */
  NULL,				/* to_wait */
  NULL,				/* to_fetch_registers */
  NULL,				/* to_store_registers */
  NULL,				/* to_prepare_to_store */
  NULL,				/* to_xfer_memory */
  NULL,				/* to_files_info */
  NULL,				/* to_insert_breakpoint */
  NULL,				/* to_remove_breakpoint */
  NULL,				/* to_terminal_init */
  NULL,				/* to_terminal_inferior */
  NULL,				/* to_terminal_ours_for_output */
  NULL,				/* to_terminal_ours */
  NULL,				/* to_terminal_info */
  nrom_kill,			/* to_kill */
  nrom_load,			/* to_load */
  NULL,				/* to_lookup_symbol */
  NULL,				/* to_create_inferior */
  nrom_mourn,			/* to_mourn_inferior */
  NULL,				/* to_can_run */
  0,				/* to_notice_signals */
  0,				/* to_thread_alive */
  0,				/* to_stop */
  download_stratum,		/* to_stratum */
  NULL,				/* to_next */
  1,				/* to_has_all_memory */
  1,				/* to_has_memory */
  1,				/* to_has_stack */
  1,				/* to_has_registers */
  0,				/* to_has_execution */
  NULL,				/* sections */
  NULL,				/* sections_end */
  OPS_MAGIC			/* to_magic */
};

void
_initialize_remote_nrom ()
{
  add_target (&nrom_ops);

  add_show_from_set (
    add_set_cmd ("nrom_load_port", no_class, var_zinteger, (char *)&load_port,
		 "Set the port to use for NetROM downloads\n", &setlist),
		     &showlist);

  add_show_from_set (
    add_set_cmd ("nrom_control_port", no_class, var_zinteger, (char *)&control_port,
		 "Set the port to use for NetROM debugger services\n", &setlist),
		     &showlist);

  add_cmd ("nrom", no_class, nrom_passthru,
	   "Pass arguments as command to NetROM",
	   &cmdlist);
}