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
|
/* communicate.c -- ARMulator RDP comms code: ARM6 Instruction Emulator.
Copyright (C) 1994 Advanced RISC Machines Ltd.
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
/**************************************************************************/
/* Functions to read and write characters or groups of characters */
/* down sockets or pipes. Those that return a value return -1 on failure */
/* and 0 on success. */
/**************************************************************************/
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "armdefs.h"
/* The socket to the debugger */
int debugsock;
/* The maximum number of file descriptors */
extern int nfds;
/* The socket handle */
extern int sockethandle;
/* Read and Write routines down a pipe or socket */
/****************************************************************/
/* Read an individual character. */
/* All other read functions rely on this one. */
/* It waits 15 seconds until there is a character available: if */
/* no character is available, then it timeouts and returns -1. */
/****************************************************************/
int
MYread_char (int sock, unsigned char *c)
{
int i;
fd_set readfds;
struct timeval timeout = { 15, 0 };
struct sockaddr_in isa;
retry:
FD_ZERO (&readfds);
FD_SET (sock, &readfds);
i = select (nfds, &readfds, (fd_set *) 0, (fd_set *) 0, &timeout);
if (i < 0)
{
perror ("select");
exit (1);
}
if (!i)
{
fprintf (stderr, "read: Timeout\n");
return -1;
}
if ((i = read (sock, c, 1)) < 1)
{
if (!i && sock == debugsock)
{
fprintf (stderr, "Connection with debugger severed.\n");
/* This shouldn't be necessary for a detached armulator, but
the armulator cannot be cold started a second time, so
this is probably preferable to locking up. */
return -1;
fprintf (stderr, "Waiting for connection from debugger...");
debugsock = accept (sockethandle, &isa, &i);
if (debugsock == -1)
{ /* Now we are in serious trouble... */
perror ("accept");
return -1;
}
fprintf (stderr, " done.\nConnection Established.\n");
sock = debugsock;
goto retry;
}
perror ("read");
return -1;
}
#ifdef DEBUG
if (sock == debugsock)
fprintf (stderr, "<%02x ", *c);
#endif
return 0;
}
/****************************************************************/
/* Read an individual character. */
/* It waits until there is a character available. Returns -1 if */
/* an error occurs. */
/****************************************************************/
int
MYread_charwait (int sock, unsigned char *c)
{
int i;
fd_set readfds;
struct sockaddr_in isa;
retry:
FD_ZERO (&readfds);
FD_SET (sock, &readfds);
i = select (nfds, &readfds,
(fd_set *) 0, (fd_set *) 0, (struct timeval *) 0);
if (i < 0)
{
perror ("select");
exit (-1);
}
if ((i = read (sock, c, 1)) < 1)
{
if (!i && sock == debugsock)
{
fprintf (stderr, "Connection with debugger severed.\n");
return -1;
fprintf (stderr, "Waiting for connection from debugger...");
debugsock = accept (sockethandle, &isa, &i);
if (debugsock == -1)
{ /* Now we are in serious trouble... */
perror ("accept");
return -1;
}
fprintf (stderr, " done.\nConnection Established.\n");
sock = debugsock;
goto retry;
}
perror ("read");
return -1;
}
#ifdef DEBUG
if (sock == debugsock)
fprintf (stderr, "<%02x ", *c);
#endif
return 0;
}
void
MYwrite_char (int sock, unsigned char c)
{
if (write (sock, &c, 1) < 1)
perror ("write");
#ifdef DEBUG
if (sock == debugsock)
fprintf (stderr, ">%02x ", c);
#endif
}
int
MYread_word (int sock, ARMword * here)
{
unsigned char a, b, c, d;
if (MYread_char (sock, &a) < 0)
return -1;
if (MYread_char (sock, &b) < 0)
return -1;
if (MYread_char (sock, &c) < 0)
return -1;
if (MYread_char (sock, &d) < 0)
return -1;
*here = a | b << 8 | c << 16 | d << 24;
return 0;
}
void
MYwrite_word (int sock, ARMword i)
{
MYwrite_char (sock, i & 0xff);
MYwrite_char (sock, (i & 0xff00) >> 8);
MYwrite_char (sock, (i & 0xff0000) >> 16);
MYwrite_char (sock, (i & 0xff000000) >> 24);
}
void
MYwrite_string (int sock, char *s)
{
int i;
for (i = 0; MYwrite_char (sock, s[i]), s[i]; i++);
}
int
MYread_FPword (int sock, char *putinhere)
{
int i;
for (i = 0; i < 16; i++)
if (MYread_char (sock, &putinhere[i]) < 0)
return -1;
return 0;
}
void
MYwrite_FPword (int sock, char *fromhere)
{
int i;
for (i = 0; i < 16; i++)
MYwrite_char (sock, fromhere[i]);
}
/* Takes n bytes from source and those n bytes */
/* down to dest */
int
passon (int source, int dest, int n)
{
char *p;
int i;
p = (char *) malloc (n);
if (!p)
{
perror ("Out of memory\n");
exit (1);
}
if (n)
{
for (i = 0; i < n; i++)
if (MYread_char (source, &p[i]) < 0)
return -1;
#ifdef DEBUG
if (dest == debugsock)
for (i = 0; i < n; i++)
fprintf (stderr, ")%02x ", (unsigned char) p[i]);
#endif
write (dest, p, n);
}
free (p);
return 0;
}
|