Commit | Line | Data |
---|---|---|
51d6a954 RS |
1 | /* Remote debugging interface for boot monitors, for GDB. |
2 | Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc. | |
3 | Contributed by Cygnus Support. Written by Rob Savoye for Cygnus. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | /* This file was derived from various remote-* modules. It is a collection | |
22 | of generic support functions so GDB can talk directly to a ROM based | |
23 | monitor. This saves use from having to hack an exception based handler | |
24 | into existance, and makes for quick porting. | |
25 | ||
26 | This module talks to a debug monitor called 'MONITOR', which | |
27 | We communicate with MONITOR via either a direct serial line, or a TCP | |
28 | (or possibly TELNET) stream to a terminal multiplexor, | |
29 | which in turn talks to the target board. | |
30 | */ | |
31 | ||
32 | #include "defs.h" | |
33 | #include "gdbcore.h" | |
34 | #include "target.h" | |
35 | #include "wait.h" | |
36 | #include <varargs.h> | |
37 | #include <signal.h> | |
38 | #include <string.h> | |
39 | #include <sys/types.h> | |
40 | #include "command.h" | |
41 | #include "serial.h" | |
42 | #include "monitor.h" | |
43 | #include "remote-utils.h" | |
44 | ||
cf51c601 RS |
45 | #if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY) |
46 | #define HAVE_SGTTY | |
47 | #endif | |
48 | ||
49 | #ifdef HAVE_TERMIOS | |
50 | #include <termio.h> | |
51 | #include <termios.h> | |
51d6a954 RS |
52 | # define TERMINAL struct termios |
53 | #else | |
cf51c601 | 54 | #include <fcntl.h> |
51d6a954 RS |
55 | # define TERMINAL struct sgttyb |
56 | #endif | |
cf51c601 RS |
57 | #include "terminal.h" |
58 | #ifndef CSTOPB | |
59 | #define CSTOPB 0x00000040 | |
60 | #endif | |
61 | ||
1b552670 RS |
62 | static const char hexchars[]="0123456789abcdef"; |
63 | static char *hex2mem(); | |
64 | ||
cf51c601 RS |
65 | #define SWAP_TARGET_AND_HOST(buffer,len) \ |
66 | do \ | |
67 | { \ | |
68 | if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER) \ | |
69 | { \ | |
70 | char tmp; \ | |
71 | char *p = (char *)(buffer); \ | |
72 | char *q = ((char *)(buffer)) + len - 1; \ | |
73 | for (; p < q; p++, q--) \ | |
74 | { \ | |
75 | tmp = *q; \ | |
76 | *q = *p; \ | |
77 | *p = tmp; \ | |
78 | } \ | |
79 | } \ | |
80 | } \ | |
81 | while (0) | |
82 | ||
1b552670 RS |
83 | static void make_xmodem_packet(); |
84 | static void print_xmodem_packet(); | |
85 | static void make_gdb_packet(); | |
86 | static unsigned long ascii2hexword(); | |
87 | static char *hexword2ascii(); | |
88 | static int tohex(); | |
89 | static int to_hex(); | |
90 | static int from_hex(); | |
06b8f5e4 | 91 | |
51d6a954 RS |
92 | struct monitor_ops *current_monitor; |
93 | extern struct cmd_list_element *setlist; | |
94 | extern struct cmd_list_element *unsetlist; | |
95 | struct cmd_list_element *showlist; | |
21ed3dcd RS |
96 | extern char *version; |
97 | extern char *host_name; | |
98 | extern char *target_name; | |
51d6a954 RS |
99 | |
100 | static int hashmark; /* flag set by "set hash" */ | |
101 | ||
51d6a954 RS |
102 | #define LOG_FILE "monitor.log" |
103 | #if defined (LOG_FILE) | |
104 | FILE *log_file; | |
105 | #endif | |
106 | ||
1b552670 RS |
107 | static int timeout = 30; |
108 | /* Having this larger than 400 causes us to be incompatible with m68k-stub.c | |
109 | and i386-stub.c. Normally, no one would notice because it only matters | |
110 | for writing large chunks of memory (e.g. in downloads). Also, this needs | |
111 | to be more than 400 if required to hold the registers (see below, where | |
112 | we round it up based on REGISTER_BYTES). */ | |
113 | #define PBUFSIZ 400 | |
51d6a954 | 114 | |
06b8f5e4 RS |
115 | /* |
116 | * Descriptor for I/O to remote machine. Initialize it to NULL so that | |
117 | * monitor_open knows that we don't have a file open when the program starts. | |
118 | */ | |
cf51c601 | 119 | serial_t monitor_desc = NULL; |
51d6a954 RS |
120 | |
121 | /* sets the download protocol, choices are srec, generic, boot */ | |
122 | char *loadtype; | |
123 | static char *loadtype_str; | |
b3b8d9bf | 124 | static char *loadproto_str; |
51d6a954 | 125 | static void set_loadtype_command(); |
b3b8d9bf | 126 | static void set_loadproto_command(); |
21ed3dcd RS |
127 | static void monitor_load_srec(); |
128 | static int monitor_write_srec(); | |
51d6a954 | 129 | |
06b8f5e4 RS |
130 | /* |
131 | * these definitions are for xmodem protocol | |
132 | */ | |
133 | #define SOH 0x01 | |
134 | #define ACK 0x06 | |
135 | #define NAK 0x15 | |
136 | #define EOT 0x04 | |
137 | #define CANCEL 0x18 | |
138 | #define GETACK getacknak(ACK) | |
139 | #define GETNAK getacknak(NAK) | |
140 | #define XMODEM_DATASIZE 128 /* the data size is ALWAYS 128 */ | |
141 | #define XMODEM_PACKETSIZE 131 /* the packet size is ALWAYS 132 (zero based) */ | |
142 | #define XMODEM 1 | |
143 | ||
51d6a954 RS |
144 | /* |
145 | * set_loadtype_command -- set the type for downloading. Check to make | |
146 | * sure you have a support protocol for this target. | |
147 | */ | |
148 | static void | |
149 | set_loadtype_command (ignore, from_tty, c) | |
150 | char *ignore; | |
151 | int from_tty; | |
152 | struct cmd_list_element *c; | |
153 | { | |
21ed3dcd | 154 | char *tmp; |
51d6a954 | 155 | char *type; |
3f9ef4ff RS |
156 | |
157 | if (current_monitor == 0x0) | |
158 | return; | |
159 | ||
160 | if (STREQ (LOADTYPES, "")) { | |
51d6a954 RS |
161 | error ("No loadtype set"); |
162 | return; | |
163 | } | |
164 | ||
21ed3dcd RS |
165 | tmp = savestring (LOADTYPES, strlen(LOADTYPES)); |
166 | type = strtok(tmp, ","); | |
51d6a954 RS |
167 | if (STREQ (type, (*(char **) c->var))) { |
168 | loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var)); | |
169 | return; | |
170 | } | |
171 | ||
21ed3dcd | 172 | while ((type = strtok (NULL, ",")) != (char *)NULL) { |
51d6a954 RS |
173 | if (STREQ (type, (*(char **) c->var))) |
174 | loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var)); | |
21ed3dcd RS |
175 | return; |
176 | } | |
177 | free (tmp); | |
178 | error ("Loadtype \"%s\" does not exist.", (*(char **) c->var)); | |
51d6a954 | 179 | } |
b3b8d9bf RS |
180 | /* |
181 | * set_loadproto_command -- set the protocol for downloading. Check to make | |
182 | * sure you have a supported protocol for this target. | |
183 | */ | |
184 | static void | |
185 | set_loadproto_command (ignore, from_tty, c) | |
186 | char *ignore; | |
187 | int from_tty; | |
188 | struct cmd_list_element *c; | |
189 | { | |
190 | char *tmp; | |
191 | char *type; | |
3f9ef4ff RS |
192 | |
193 | if (current_monitor == 0x0) | |
194 | return; | |
195 | ||
b3b8d9bf RS |
196 | if (STREQ (LOADPROTOS, "")) { |
197 | error ("No load protocols set"); | |
198 | return; | |
199 | } | |
200 | ||
201 | tmp = savestring (LOADPROTOS, strlen(LOADPROTOS)); | |
202 | type = strtok(tmp, ","); | |
203 | if (STREQ (type, (*(char **) c->var))) { | |
204 | loadproto_str = savestring (*(char **) c->var, strlen (*(char **) c->var)); | |
205 | return; | |
206 | } | |
207 | ||
208 | while ((type = strtok (NULL, ",")) != (char *)NULL) { | |
209 | if (STREQ (type, (*(char **) c->var))) | |
210 | loadproto_str = savestring (*(char **) c->var, strlen (*(char **) c->var)); | |
211 | return; | |
212 | } | |
213 | free (tmp); | |
214 | error ("Load protocol \"%s\" does not exist.", (*(char **) c->var)); | |
215 | } | |
51d6a954 RS |
216 | |
217 | /* | |
218 | * printf_monitor -- send data to monitor. Works just like printf. | |
219 | */ | |
220 | static void | |
221 | printf_monitor(va_alist) | |
222 | va_dcl | |
223 | { | |
224 | va_list args; | |
225 | char *pattern; | |
1b552670 | 226 | char buf[PBUFSIZ]; |
51d6a954 RS |
227 | int i; |
228 | ||
229 | va_start(args); | |
230 | ||
231 | pattern = va_arg(args, char *); | |
232 | ||
233 | vsprintf(buf, pattern, args); | |
234 | ||
e6fa5bd6 | 235 | debuglogs (1, "printf_monitor(), Sending: \"%s\".", buf); |
51d6a954 | 236 | |
1b552670 RS |
237 | if (strlen(buf) > PBUFSIZ) |
238 | error ("printf_monitor(): string too long"); | |
51d6a954 RS |
239 | if (SERIAL_WRITE(monitor_desc, buf, strlen(buf))) |
240 | fprintf(stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno)); | |
241 | } | |
06b8f5e4 RS |
242 | /* |
243 | * write_monitor -- send raw data to monitor. | |
244 | */ | |
245 | static void | |
246 | write_monitor(data, len) | |
247 | char data[]; | |
248 | int len; | |
249 | { | |
250 | if (SERIAL_WRITE(monitor_desc, data, len)) | |
251 | fprintf(stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno)); | |
252 | ||
253 | *(data + len+1) = '\0'; | |
254 | debuglogs (1, "write_monitor(), Sending: \"%s\".", data); | |
255 | ||
256 | } | |
51d6a954 | 257 | |
e6fa5bd6 RS |
258 | /* |
259 | * debuglogs -- deal with debugging info to multiple sources. This takes | |
260 | * two real args, the first one is the level to be compared against | |
261 | * the sr_get_debug() value, the second arg is a printf buffer and args | |
262 | * to be formatted and printed. A CR is added after each string is printed. | |
263 | */ | |
cf51c601 | 264 | void |
e6fa5bd6 RS |
265 | debuglogs(va_alist) |
266 | va_dcl | |
267 | { | |
268 | va_list args; | |
269 | char *pattern, *p; | |
1b552670 RS |
270 | unsigned char buf[PBUFSIZ]; |
271 | char newbuf[PBUFSIZ]; | |
e6fa5bd6 RS |
272 | int level, i; |
273 | ||
274 | va_start(args); | |
275 | ||
276 | level = va_arg(args, int); /* get the debug level */ | |
277 | if ((level <0) || (level > 100)) { | |
06b8f5e4 | 278 | error ("Bad argument passed to debuglogs(), needs debug level"); |
e6fa5bd6 RS |
279 | return; |
280 | } | |
281 | ||
282 | pattern = va_arg(args, char *); /* get the printf style pattern */ | |
283 | ||
284 | vsprintf(buf, pattern, args); /* format the string */ | |
285 | ||
286 | /* convert some characters so it'll look right in the log */ | |
287 | p = newbuf; | |
1b552670 RS |
288 | for (i = 0 ; buf[i] != '\0'; i++) { |
289 | if (i > PBUFSIZ) | |
290 | error ("Debug message too long"); | |
e6fa5bd6 RS |
291 | switch (buf[i]) { |
292 | case '\n': /* newlines */ | |
293 | *p++ = '\\'; | |
294 | *p++ = 'n'; | |
295 | continue; | |
296 | case '\r': /* carriage returns */ | |
297 | *p++ = '\\'; | |
298 | *p++ = 'r'; | |
299 | continue; | |
300 | case '\033': /* escape */ | |
301 | *p++ = '\\'; | |
302 | *p++ = 'e'; | |
303 | continue; | |
304 | case '\t': /* tab */ | |
305 | *p++ = '\\'; | |
306 | *p++ = 't'; | |
307 | continue; | |
308 | case '\b': /* backspace */ | |
309 | *p++ = '\\'; | |
310 | *p++ = 'b'; | |
311 | continue; | |
312 | default: /* no change */ | |
313 | *p++ = buf[i]; | |
314 | } | |
315 | ||
316 | if (buf[i] < 26) { /* modify control characters */ | |
317 | *p++ = '^'; | |
318 | *p++ = buf[i] + 'A'; | |
319 | continue; | |
320 | } | |
1b552670 RS |
321 | if (buf[i] >= 128) { /* modify control characters */ |
322 | *p++ = '!'; | |
323 | *p++ = buf[i] + 'A'; | |
324 | continue; | |
325 | } | |
326 | } | |
e6fa5bd6 RS |
327 | *p = '\0'; /* terminate the string */ |
328 | ||
329 | if (sr_get_debug() > level) | |
330 | puts (newbuf); | |
331 | ||
332 | #ifdef LOG_FILE /* write to the monitor log */ | |
333 | if (log_file != 0x0) { | |
334 | fputs (newbuf, log_file); | |
335 | fputc ('\n', log_file); | |
336 | fflush (log_file); | |
337 | } | |
338 | #endif | |
339 | } | |
340 | ||
7804e5bc RS |
341 | /* readchar -- read a character from the remote system, doing all the fancy |
342 | * timeout stuff. | |
343 | */ | |
51d6a954 RS |
344 | static int |
345 | readchar(timeout) | |
346 | int timeout; | |
347 | { | |
348 | int c; | |
349 | ||
350 | c = SERIAL_READCHAR(monitor_desc, timeout); | |
351 | ||
1b552670 | 352 | if (sr_get_debug() > 5) { |
51d6a954 | 353 | putchar(c & 0x7f); |
1b552670 RS |
354 | debuglogs (5, "readchar: timeout = %d\n", timeout); |
355 | } | |
51d6a954 RS |
356 | |
357 | #ifdef LOG_FILE | |
358 | if (isascii (c)) | |
359 | putc(c & 0x7f, log_file); | |
360 | #endif | |
361 | ||
362 | if (c >= 0) | |
363 | return c & 0x7f; | |
364 | ||
7804e5bc RS |
365 | if (c == SERIAL_TIMEOUT) { |
366 | if (timeout == 0) | |
367 | return c; /* Polls shouldn't generate timeout errors */ | |
368 | error("Timeout reading from remote system."); | |
7a1330f7 | 369 | #ifdef LOG_FILE |
06b8f5e4 | 370 | fputs ("ERROR: Timeout reading from remote system", log_file); |
7a1330f7 | 371 | #endif |
7804e5bc | 372 | } |
51d6a954 RS |
373 | perror_with_name("remote-monitor"); |
374 | } | |
375 | ||
7804e5bc RS |
376 | /* |
377 | * expect -- scan input from the remote system, until STRING is found. | |
378 | * If DISCARD is non-zero, then discard non-matching input, else print | |
379 | * it out. Let the user break out immediately. | |
380 | */ | |
51d6a954 RS |
381 | static void |
382 | expect (string, discard) | |
383 | char *string; | |
384 | int discard; | |
385 | { | |
386 | char *p = string; | |
387 | int c; | |
388 | ||
389 | ||
e6fa5bd6 | 390 | debuglogs (1, "Expecting \"%s\".", string); |
51d6a954 RS |
391 | |
392 | immediate_quit = 1; | |
393 | while (1) { | |
394 | c = readchar(timeout); | |
395 | if (!isascii (c)) | |
396 | continue; | |
397 | if (c == *p++) { | |
398 | if (*p == '\0') { | |
399 | immediate_quit = 0; | |
7a1330f7 | 400 | debuglogs (4, "Matched"); |
51d6a954 RS |
401 | return; |
402 | } | |
403 | } else { | |
404 | if (!discard) { | |
405 | fwrite(string, 1, (p - 1) - string, stdout); | |
406 | putchar((char)c); | |
407 | fflush(stdout); | |
408 | } | |
409 | p = string; | |
410 | } | |
411 | } | |
412 | } | |
413 | ||
414 | /* Keep discarding input until we see the MONITOR prompt. | |
415 | ||
416 | The convention for dealing with the prompt is that you | |
417 | o give your command | |
418 | o *then* wait for the prompt. | |
419 | ||
420 | Thus the last thing that a procedure does with the serial line | |
421 | will be an expect_prompt(). Exception: monitor_resume does not | |
422 | wait for the prompt, because the terminal is being handed over | |
423 | to the inferior. However, the next thing which happens after that | |
424 | is a monitor_wait which does wait for the prompt. | |
425 | Note that this includes abnormal exit, e.g. error(). This is | |
426 | necessary to prevent getting into states from which we can't | |
427 | recover. */ | |
428 | static void | |
429 | expect_prompt(discard) | |
430 | int discard; | |
431 | { | |
51d6a954 RS |
432 | expect (PROMPT, discard); |
433 | } | |
434 | ||
435 | /* | |
436 | * junk -- ignore junk characters. Returns a 1 if junk, 0 otherwise | |
437 | */ | |
438 | static int | |
439 | junk(ch) | |
440 | char ch; | |
441 | { | |
442 | switch (ch) { | |
06b8f5e4 | 443 | case '\0': |
51d6a954 RS |
444 | case ' ': |
445 | case '-': | |
446 | case '\t': | |
447 | case '\r': | |
448 | case '\n': | |
449 | if (sr_get_debug() > 5) | |
e6fa5bd6 | 450 | debuglogs (5, "Ignoring \'%c\'.", ch); |
51d6a954 RS |
451 | return 1; |
452 | default: | |
453 | if (sr_get_debug() > 5) | |
e6fa5bd6 | 454 | debuglogs (5, "Accepting \'%c\'.", ch); |
51d6a954 RS |
455 | return 0; |
456 | } | |
457 | } | |
458 | ||
459 | /* | |
460 | * get_hex_digit -- Get a hex digit from the remote system & return its value. | |
461 | * If ignore is nonzero, ignore spaces, newline & tabs. | |
462 | */ | |
463 | static int | |
464 | get_hex_digit(ignore) | |
465 | int ignore; | |
466 | { | |
467 | static int ch; | |
468 | while (1) { | |
469 | ch = readchar(timeout); | |
470 | if (junk(ch)) | |
471 | continue; | |
cf51c601 | 472 | if (sr_get_debug() > 4) { |
e6fa5bd6 | 473 | debuglogs (4, "get_hex_digit() got a 0x%x(%c)", ch, ch); |
cf51c601 RS |
474 | } else { |
475 | #ifdef LOG_FILE /* write to the monitor log */ | |
476 | if (log_file != 0x0) { | |
477 | fputs ("get_hex_digit() got a 0x", log_file); | |
478 | fputc (ch, log_file); | |
479 | fputc ('\n', log_file); | |
480 | fflush (log_file); | |
481 | } | |
482 | #endif | |
483 | } | |
51d6a954 RS |
484 | |
485 | if (ch >= '0' && ch <= '9') | |
486 | return ch - '0'; | |
487 | else if (ch >= 'A' && ch <= 'F') | |
488 | return ch - 'A' + 10; | |
489 | else if (ch >= 'a' && ch <= 'f') | |
490 | return ch - 'a' + 10; | |
491 | else if (ch == ' ' && ignore) | |
492 | ; | |
493 | else { | |
494 | expect_prompt(1); | |
cf51c601 | 495 | debuglogs (4, "Invalid hex digit from remote system. (0x%x)", ch); |
06b8f5e4 | 496 | error("Invalid hex digit from remote system. (0x%x)", ch); |
51d6a954 RS |
497 | } |
498 | } | |
499 | } | |
500 | ||
501 | /* get_hex_byte -- Get a byte from monitor and put it in *BYT. | |
502 | * Accept any number leading spaces. | |
503 | */ | |
504 | static void | |
505 | get_hex_byte (byt) | |
506 | char *byt; | |
507 | { | |
508 | int val; | |
509 | ||
510 | val = get_hex_digit (1) << 4; | |
cf51c601 | 511 | debuglogs (4, "get_hex_byte() -- Read first nibble 0x%x", val); |
51d6a954 RS |
512 | |
513 | val |= get_hex_digit (0); | |
cf51c601 | 514 | debuglogs (4, "get_hex_byte() -- Read second nibble 0x%x", val); |
51d6a954 RS |
515 | *byt = val; |
516 | ||
cf51c601 | 517 | debuglogs (4, "get_hex_byte() -- Read a 0x%x", val); |
51d6a954 RS |
518 | } |
519 | ||
520 | /* | |
521 | * get_hex_word -- Get N 32-bit words from remote, each preceded by a space, | |
522 | * and put them in registers starting at REGNO. | |
523 | */ | |
524 | static int | |
525 | get_hex_word () | |
526 | { | |
cf51c601 | 527 | long val, newval; |
51d6a954 RS |
528 | int i; |
529 | ||
530 | val = 0; | |
cf51c601 RS |
531 | |
532 | #if 0 | |
533 | if (HOST_BYTE_ORDER == BIG_ENDIAN) { | |
534 | #endif | |
535 | for (i = 0; i < 8; i++) | |
536 | val = (val << 4) + get_hex_digit (i == 0); | |
537 | #if 0 | |
538 | } else { | |
539 | for (i = 7; i >= 0; i--) | |
540 | val = (val << 4) + get_hex_digit (i == 0); | |
541 | } | |
542 | #endif | |
543 | ||
544 | debuglogs (4, "get_hex_word() got a 0x%x for a %s host.", val, (HOST_BYTE_ORDER == BIG_ENDIAN) ? "big endian" : "little endian"); | |
51d6a954 RS |
545 | |
546 | return val; | |
547 | } | |
548 | ||
549 | /* This is called not only when we first attach, but also when the | |
550 | user types "run" after having attached. */ | |
551 | void | |
552 | monitor_create_inferior (execfile, args, env) | |
553 | char *execfile; | |
554 | char *args; | |
555 | char **env; | |
556 | { | |
557 | int entry_pt; | |
558 | ||
559 | if (args && *args) | |
560 | error("Can't pass arguments to remote MONITOR process"); | |
561 | ||
562 | if (execfile == 0 || exec_bfd == 0) | |
563 | error("No exec file specified"); | |
564 | ||
565 | entry_pt = (int) bfd_get_start_address (exec_bfd); | |
566 | ||
51d6a954 RS |
567 | /* The "process" (board) is already stopped awaiting our commands, and |
568 | the program is already downloaded. We just set its PC and go. */ | |
569 | ||
570 | clear_proceed_status (); | |
571 | ||
572 | /* Tell wait_for_inferior that we've started a new process. */ | |
573 | init_wait_for_inferior (); | |
574 | ||
575 | /* Set up the "saved terminal modes" of the inferior | |
576 | based on what modes we are starting it with. */ | |
577 | target_terminal_init (); | |
578 | ||
579 | /* Install inferior's terminal modes. */ | |
580 | target_terminal_inferior (); | |
581 | ||
582 | /* insert_step_breakpoint (); FIXME, do we need this? */ | |
583 | ||
584 | /* Let 'er rip... */ | |
585 | proceed ((CORE_ADDR)entry_pt, TARGET_SIGNAL_DEFAULT, 0); | |
586 | } | |
587 | ||
7804e5bc RS |
588 | /* |
589 | * monitor_open -- open a connection to a remote debugger. | |
590 | * NAME is the filename used for communication. | |
591 | */ | |
51d6a954 RS |
592 | static int baudrate = 9600; |
593 | static char dev_name[100]; | |
594 | ||
595 | void | |
596 | monitor_open(args, name, from_tty) | |
597 | char *args; | |
598 | char *name; | |
599 | int from_tty; | |
600 | { | |
cf51c601 | 601 | TERMINAL *temptempio; |
51d6a954 RS |
602 | |
603 | if (args == NULL) | |
604 | error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\ | |
605 | `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name); | |
606 | ||
607 | /* if (is_open) */ | |
608 | monitor_close(0); | |
609 | ||
610 | strcpy(dev_name, args); | |
611 | monitor_desc = SERIAL_OPEN(dev_name); | |
612 | ||
613 | if (monitor_desc == NULL) | |
614 | perror_with_name(dev_name); | |
615 | ||
7a1330f7 RS |
616 | if (baud_rate != -1) { |
617 | if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate)) { | |
618 | SERIAL_CLOSE (monitor_desc); | |
619 | perror_with_name (name); | |
51d6a954 | 620 | } |
7a1330f7 RS |
621 | } |
622 | ||
51d6a954 RS |
623 | SERIAL_RAW(monitor_desc); |
624 | ||
1b552670 | 625 | #if !defined(__GO32__) && !defined(GDB_TARGET_IS_PA_ELF) |
cf51c601 RS |
626 | /* some systems only work with 2 stop bits */ |
627 | if (STOPBITS == 2) { | |
628 | temptempio = (TERMINAL *)SERIAL_GET_TTY_STATE(monitor_desc); | |
629 | #ifdef HAVE_SGTTY | |
630 | temptempio->sg_cflag |= baud_rate | CSTOPB; | |
631 | #else | |
632 | temptempio->c_cflag |= baud_rate | CSTOPB; | |
1b552670 | 633 | /*** temptempio->c_lflag |= ~0x00000008; turn off echo ***/ |
cf51c601 RS |
634 | #endif |
635 | SERIAL_SET_TTY_STATE(monitor_desc, temptempio); | |
636 | debuglogs (4, "Set serial port to 2 stop bits"); | |
637 | } | |
638 | #endif /* __GO32__ */ | |
639 | ||
51d6a954 RS |
640 | #if defined (LOG_FILE) |
641 | log_file = fopen (LOG_FILE, "w"); | |
642 | if (log_file == NULL) | |
643 | perror_with_name (LOG_FILE); | |
21ed3dcd RS |
644 | fprintf_filtered (log_file, "GDB %s (%s", version, host_name); |
645 | fprintf_filtered (log_file, " --target %s)\n", target_name); | |
646 | fprintf_filtered (log_file, "Remote target %s connected to %s\n\n", TARGET_NAME, dev_name); | |
51d6a954 RS |
647 | #endif |
648 | ||
1b552670 RS |
649 | /* see if the target is alive. For a ROM monitor, we can just try to force the |
650 | prompt to print a few times. FOr the GDB remote protocol, the application | |
651 | being debugged is sitting at a breakpoint and waiting for GDB to initialize | |
652 | the connection. We force it to give us an empty packet to see if it's alive. | |
653 | */ | |
654 | if (GDBPROTO) { | |
655 | debuglogs (3, "Trying to ACK the target's debug stub"); | |
656 | printf_monitor (INIT_CMD); /* ask for the last signal */ | |
657 | expect ("$S05#b8",0); /* look for a response */ | |
658 | printf_monitor ("+"); /* ask for the last signal */ | |
659 | expect_prompt(1); /* See if we get a prompt */ | |
660 | } else { | |
661 | /* wake up the monitor and see if it's alive */ | |
662 | printf_monitor(INIT_CMD); | |
663 | expect_prompt(1); /* See if we get a prompt */ | |
51d6a954 | 664 | |
1b552670 RS |
665 | /* try again to be sure */ |
666 | printf_monitor(INIT_CMD); | |
667 | expect_prompt(1); /* See if we get a prompt */ | |
668 | } | |
51d6a954 RS |
669 | |
670 | if (from_tty) | |
671 | printf("Remote target %s connected to %s\n", TARGET_NAME, dev_name); | |
51d6a954 RS |
672 | } |
673 | ||
674 | /* | |
7804e5bc RS |
675 | * monitor_close -- Close out all files and local state before this |
676 | * target loses control. | |
51d6a954 RS |
677 | */ |
678 | ||
679 | void | |
680 | monitor_close (quitting) | |
681 | int quitting; | |
682 | { | |
683 | SERIAL_CLOSE(monitor_desc); | |
684 | monitor_desc = NULL; | |
685 | ||
e6fa5bd6 | 686 | debuglogs (1, "monitor_close (quitting=%d)", quitting); |
7804e5bc | 687 | |
51d6a954 RS |
688 | #if defined (LOG_FILE) |
689 | if (log_file) { | |
690 | if (ferror(log_file)) | |
691 | fprintf(stderr, "Error writing log file.\n"); | |
692 | if (fclose(log_file) != 0) | |
693 | fprintf(stderr, "Error closing log file.\n"); | |
694 | } | |
695 | #endif | |
696 | } | |
697 | ||
7804e5bc RS |
698 | /* |
699 | * monitor_detach -- terminate the open connection to the remote | |
700 | * debugger. Use this when you want to detach and do something | |
701 | * else with your gdb. | |
702 | */ | |
51d6a954 RS |
703 | void |
704 | monitor_detach (from_tty) | |
705 | int from_tty; | |
706 | { | |
e6fa5bd6 | 707 | |
7a1330f7 | 708 | debuglogs (1, "monitor_detach ()"); |
7804e5bc | 709 | |
51d6a954 RS |
710 | pop_target(); /* calls monitor_close to do the real work */ |
711 | if (from_tty) | |
712 | printf ("Ending remote %s debugging\n", target_shortname); | |
713 | } | |
7804e5bc RS |
714 | |
715 | /* | |
716 | * monitor_attach -- attach GDB to the target. | |
717 | */ | |
718 | void | |
719 | monitor_attach (args, from_tty) | |
720 | char *args; | |
721 | int from_tty; | |
722 | { | |
723 | if (from_tty) | |
724 | printf ("Starting remote %s debugging\n", target_shortname); | |
51d6a954 | 725 | |
7a1330f7 | 726 | debuglogs (1, "monitor_attach (args=%s)", args); |
7804e5bc | 727 | |
7804e5bc RS |
728 | printf_monitor (GO_CMD); |
729 | /* swallow the echo. */ | |
730 | expect (GO_CMD, 1); | |
731 | } | |
732 | ||
51d6a954 | 733 | /* |
7804e5bc | 734 | * monitor_resume -- Tell the remote machine to resume. |
51d6a954 RS |
735 | */ |
736 | void | |
737 | monitor_resume (pid, step, sig) | |
738 | int pid, step; | |
739 | enum target_signal sig; | |
740 | { | |
7a1330f7 | 741 | debuglogs (1, "monitor_resume (step=%d, sig=%d)", step, sig); |
7804e5bc RS |
742 | |
743 | if (step) { | |
744 | printf_monitor (STEP_CMD); | |
7804e5bc RS |
745 | } else { |
746 | printf_monitor (CONT_CMD); | |
7804e5bc | 747 | } |
51d6a954 RS |
748 | } |
749 | ||
750 | /* | |
7a1330f7 | 751 | * monitor_wait -- Wait until the remote machine stops, then return, |
51d6a954 RS |
752 | * storing status in status just as `wait' would. |
753 | */ | |
51d6a954 RS |
754 | int |
755 | monitor_wait (pid, status) | |
756 | int pid; | |
757 | struct target_waitstatus *status; | |
758 | { | |
759 | int old_timeout = timeout; | |
51d6a954 | 760 | |
7a1330f7 | 761 | debuglogs(1, "monitor_wait (), printing extraneous text."); |
e6fa5bd6 | 762 | |
51d6a954 RS |
763 | status->kind = TARGET_WAITKIND_EXITED; |
764 | status->value.integer = 0; | |
765 | ||
766 | timeout = 0; /* Don't time out -- user program is running. */ | |
767 | ||
768 | expect_prompt(0); /* Wait for prompt, outputting extraneous text */ | |
7a1330f7 | 769 | debuglogs (4, "monitor_wait(), got the prompt."); |
51d6a954 RS |
770 | |
771 | status->kind = TARGET_WAITKIND_STOPPED; | |
772 | status->value.sig = TARGET_SIGNAL_TRAP; | |
773 | ||
cf51c601 RS |
774 | |
775 | ||
51d6a954 RS |
776 | timeout = old_timeout; |
777 | ||
778 | return 0; | |
779 | } | |
780 | ||
781 | /* Return the name of register number regno in the form input and output by | |
782 | monitor. Currently, register_names just happens to contain exactly what | |
783 | monitor wants. Lets take advantage of that just as long as possible! */ | |
784 | ||
785 | static char * | |
786 | get_reg_name (regno) | |
787 | int regno; | |
788 | { | |
789 | static char buf[50]; | |
790 | const char *p; | |
791 | char *b; | |
b3b8d9bf | 792 | |
51d6a954 RS |
793 | b = buf; |
794 | ||
795 | if (regno < 0) | |
796 | return (""); | |
797 | ||
798 | for (p = REGNAMES(regno); *p; p++) | |
799 | *b++ = tolower(*p); | |
800 | ||
801 | *b = '\000'; | |
802 | ||
e6fa5bd6 | 803 | debuglogs (5, "Got name \"%s\" from regno #%d.", buf, regno); |
f1ca4cbc | 804 | |
51d6a954 RS |
805 | return buf; |
806 | } | |
807 | ||
e6fa5bd6 RS |
808 | /* |
809 | * monitor_fetch_registers -- read the remote registers into the | |
810 | * block regs. | |
811 | */ | |
51d6a954 | 812 | void |
1b552670 RS |
813 | monitor_fetch_registers (ignored) |
814 | int ignored; | |
51d6a954 | 815 | { |
1b552670 RS |
816 | int regno, i; |
817 | char *p; | |
818 | unsigned char packet[PBUFSIZ]; | |
819 | char regs[REGISTER_BYTES]; | |
820 | ||
821 | debuglogs (1, "monitor_fetch_registers (ignored=%d)\n", ignored); | |
822 | ||
823 | memset (packet, 0, PBUFSIZ); | |
824 | if (GDBPROTO) { | |
825 | /* Unimplemented registers read as all bits zero. */ | |
826 | memset (regs, 0, REGISTER_BYTES); | |
827 | make_gdb_packet (packet, "g"); | |
828 | if (monitor_send_packet (packet) == 0) | |
829 | error ("Couldn't transmit packet\n"); | |
830 | if (monitor_get_packet (packet) == 0) | |
831 | error ("Couldn't receive packet\n"); | |
832 | /* FIXME: read bytes from packet */ | |
833 | debuglogs (4, "monitor_fetch_registers: Got a \"%s\" back\n", packet); | |
834 | for (regno = 0; regno <= PC_REGNUM+4; regno++) { | |
835 | /* supply register stores in target byte order, so swap here */ | |
836 | /* FIXME: convert from ASCII hex to raw bytes */ | |
837 | i = ascii2hexword (packet + (regno * 8)); | |
838 | debuglogs (5, "Adding register %d = %x\n", regno, i); | |
839 | SWAP_TARGET_AND_HOST (&i, 4); | |
840 | supply_register (regno, (char *)&i); | |
841 | } | |
842 | } else { | |
843 | for (regno = 0; regno <= PC_REGNUM; regno++) | |
844 | monitor_fetch_register(regno); | |
845 | } | |
51d6a954 RS |
846 | } |
847 | ||
848 | /* | |
f1ca4cbc RS |
849 | * monitor_fetch_register -- fetch register REGNO, or all registers if REGNO |
850 | * is -1. Returns errno value. | |
51d6a954 RS |
851 | */ |
852 | void | |
853 | monitor_fetch_register (regno) | |
854 | int regno; | |
855 | { | |
cf51c601 | 856 | int newval, val, j; |
51d6a954 | 857 | |
7a1330f7 | 858 | debuglogs (1, "monitor_fetch_register (reg=%s)", get_reg_name (regno)); |
51d6a954 RS |
859 | |
860 | if (regno < 0) { | |
861 | monitor_fetch_registers (); | |
862 | } else { | |
863 | char *name = get_reg_name (regno); | |
864 | if (STREQ(name, "")) | |
865 | return; | |
866 | printf_monitor (ROMCMD(GET_REG), name); /* send the command */ | |
867 | expect (name, 1); /* then strip the leading garbage */ | |
868 | if (*ROMDELIM(GET_REG) != 0) { /* if there's a delimiter */ | |
869 | expect (ROMDELIM(GET_REG), 1); | |
870 | } | |
871 | ||
872 | val = get_hex_word(); /* get the value, ignore junk */ | |
cf51c601 RS |
873 | |
874 | ||
875 | /* supply register stores in target byte order, so swap here */ | |
876 | SWAP_TARGET_AND_HOST (&val, 4); | |
51d6a954 RS |
877 | supply_register (regno, (char *) &val); |
878 | ||
879 | if (*ROMDELIM(GET_REG) != 0) { | |
f1ca4cbc | 880 | /*** expect (ROMRES(GET_REG)); ***/ |
51d6a954 RS |
881 | printf_monitor (CMD_END); |
882 | } | |
883 | expect_prompt (1); | |
884 | } | |
885 | return; | |
886 | } | |
887 | ||
1b552670 RS |
888 | /* |
889 | * monitor_store_registers - store the remote registers. | |
890 | */ | |
51d6a954 | 891 | void |
1b552670 RS |
892 | monitor_store_registers (ignored) |
893 | int ignored; | |
51d6a954 RS |
894 | { |
895 | int regno; | |
1b552670 RS |
896 | unsigned long i; |
897 | char packet[PBUFSIZ]; | |
898 | char buf[PBUFSIZ]; | |
899 | char num[9]; | |
900 | ||
e6fa5bd6 RS |
901 | debuglogs (1, "monitor_store_registers()"); |
902 | ||
1b552670 RS |
903 | if (GDBPROTO) { |
904 | memset (packet, 0, PBUFSIZ); | |
905 | memset (buf, 0, PBUFSIZ); | |
906 | buf[0] = 'G'; | |
907 | ||
908 | /* Unimplemented registers read as all bits zero. */ | |
909 | /* FIXME: read bytes from packet */ | |
910 | for (regno = 0; regno < 41; regno++) { /* FIXME */ | |
911 | /* supply register stores in target byte order, so swap here */ | |
912 | /* FIXME: convert from ASCII hex to raw bytes */ | |
913 | i = (unsigned long)read_register (regno); | |
914 | #if 0 | |
915 | SWAP_TARGET_AND_HOST (&i, 4); | |
916 | #endif | |
917 | hexword2ascii (num, i); | |
918 | strcpy (buf+(regno * 8)+1, num); | |
919 | } | |
920 | *(buf + (regno * 8) + 2) = 0; | |
921 | make_gdb_packet (packet, buf); | |
922 | if (monitor_send_packet (packet) == 0) | |
923 | error ("Couldn't transmit packet\n"); | |
924 | if (monitor_get_packet (packet) == 0) | |
925 | error ("Couldn't receive packet\n"); | |
926 | } else { | |
927 | for (regno = 0; regno <= PC_REGNUM; regno++) | |
928 | monitor_store_register(regno); | |
929 | } | |
51d6a954 RS |
930 | |
931 | registers_changed (); | |
932 | } | |
933 | ||
f1ca4cbc RS |
934 | /* |
935 | * monitor_store_register -- store register REGNO, or all if REGNO == 0. | |
936 | * return errno value. | |
937 | */ | |
51d6a954 RS |
938 | void |
939 | monitor_store_register (regno) | |
940 | int regno; | |
941 | { | |
942 | char *name; | |
f1ca4cbc RS |
943 | int i; |
944 | ||
945 | i = read_register(regno); | |
51d6a954 | 946 | |
e6fa5bd6 | 947 | debuglogs (1, "monitor_store_register (regno=%d)", regno); |
51d6a954 RS |
948 | |
949 | if (regno < 0) | |
950 | monitor_store_registers (); | |
951 | else { | |
e6fa5bd6 | 952 | debuglogs (3, "Setting register %s to 0x%x", get_reg_name (regno), read_register (regno)); |
51d6a954 RS |
953 | |
954 | name = get_reg_name (regno); | |
955 | if (STREQ(name, "")) | |
956 | return; | |
f1ca4cbc RS |
957 | printf_monitor (ROMCMD(SET_REG), name, read_register(regno)); |
958 | expect (name, 1); /* strip the leading garbage */ | |
51d6a954 RS |
959 | if (*ROMDELIM(SET_REG) != 0) { /* if there's a delimiter */ |
960 | expect (ROMDELIM(SET_REG), 1); | |
f1ca4cbc RS |
961 | get_hex_word(1); |
962 | printf_monitor ("%d%s\n", i, CMD_END); | |
51d6a954 RS |
963 | } |
964 | expect_prompt (1); | |
965 | } | |
966 | return; | |
967 | ||
968 | #if 0 | |
969 | printf_monitor (SET_REG, get_reg_name (regno), | |
970 | read_register (regno)); | |
971 | expect_prompt (1); | |
972 | } | |
973 | #endif | |
974 | } | |
975 | ||
976 | /* Get ready to modify the registers array. On machines which store | |
977 | individual registers, this doesn't need to do anything. On machines | |
978 | which store all the registers in one fell swoop, this makes sure | |
979 | that registers contains all the registers from the program being | |
980 | debugged. */ | |
981 | ||
982 | void | |
983 | monitor_prepare_to_store () | |
984 | { | |
985 | /* Do nothing, since we can store individual regs */ | |
986 | } | |
987 | ||
988 | void | |
989 | monitor_files_info () | |
990 | { | |
991 | printf ("\tAttached to %s at %d baud.\n", | |
992 | dev_name, baudrate); | |
993 | } | |
994 | ||
f1ca4cbc RS |
995 | /* |
996 | * monitor_write_inferior_memory -- Copy LEN bytes of data from debugger | |
997 | * memory at MYADDR to inferior's memory at MEMADDR. Returns length moved. | |
998 | */ | |
51d6a954 RS |
999 | int |
1000 | monitor_write_inferior_memory (memaddr, myaddr, len) | |
1001 | CORE_ADDR memaddr; | |
1002 | unsigned char *myaddr; | |
1003 | int len; | |
1004 | { | |
1b552670 RS |
1005 | unsigned long i; |
1006 | int j; | |
1007 | char packet[PBUFSIZ]; | |
1008 | char buf[PBUFSIZ]; | |
1009 | char num[9]; | |
1010 | char *p; | |
1011 | ||
e6fa5bd6 | 1012 | debuglogs (1, "monitor_write_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len); |
1b552670 RS |
1013 | memset (buf, '\0', PBUFSIZ); /* this also sets the string terminator */ |
1014 | p = buf; | |
1015 | ||
1016 | if (GDBPROTO) { | |
1017 | *p++ = 'M'; /* The command to write memory */ | |
1018 | hexword2ascii (num, memaddr); /* convert the address */ | |
1019 | strcpy (p, num); /* copy the address */ | |
1020 | p += 8; | |
1021 | *p++ = ','; /* add comma delimeter */ | |
1022 | hexword2ascii (num, len); /* Get the length as a 4 digit number */ | |
1023 | *p++ = num[4]; | |
1024 | *p++ = num[5]; | |
1025 | *p++ = num[6]; | |
1026 | *p++ = num[7]; | |
1027 | *p++ = ':'; /* add the colon delimeter */ | |
1028 | for (j = 0; j < len; j++) { /* copy the data in after converting it */ | |
1029 | #if 0 | |
1030 | hexword2ascii (num, myaddr[j]); | |
1031 | #endif | |
1032 | *p++ = tohex ((myaddr[j] >> 4) & 0xf); | |
1033 | *p++ = tohex (myaddr[j] & 0xf); | |
1034 | #if 0 | |
1035 | strcpy ((buf+14)+(j * 2), num+6); | |
1036 | #endif | |
51d6a954 | 1037 | } |
1b552670 RS |
1038 | |
1039 | make_gdb_packet (packet, buf); | |
1040 | if (monitor_send_packet (packet) == 0) | |
1041 | error ("Couldn't transmit packet\n"); | |
1042 | if (monitor_get_packet (packet) == 0) | |
1043 | error ("Couldn't receive packet\n"); | |
1044 | } else { | |
1045 | for (i = 0; i < len; i++) { | |
1046 | printf_monitor (ROMCMD(SET_MEM), memaddr + i, myaddr[i] ); | |
1047 | if (*ROMDELIM(SET_MEM) != 0) { /* if there's a delimiter */ | |
1048 | expect (ROMDELIM(SET_MEM), 1); | |
1049 | expect (CMD_DELIM); | |
1050 | printf_monitor ("%x", myaddr[i]); | |
1051 | } | |
1052 | /*** printf_monitor ("%x", myaddr[i]); ***/ | |
1053 | if (sr_get_debug() > 1) | |
1054 | printf ("\nSet 0x%x to 0x%x\n", memaddr + i, myaddr[i]); | |
1055 | if (*ROMDELIM(SET_MEM) != 0) { | |
1056 | expect (CMD_DELIM); | |
1057 | printf_monitor (CMD_END); | |
1058 | } | |
1059 | expect_prompt (1); | |
f1ca4cbc | 1060 | } |
f1ca4cbc | 1061 | } |
51d6a954 RS |
1062 | return len; |
1063 | } | |
1064 | ||
1065 | /* | |
1066 | * monitor_read_inferior_memory -- read LEN bytes from inferior memory | |
1067 | * at MEMADDR. Put the result at debugger address MYADDR. Returns | |
1068 | * length moved. | |
1069 | */ | |
1070 | int | |
1071 | monitor_read_inferior_memory(memaddr, myaddr, len) | |
1072 | CORE_ADDR memaddr; | |
1073 | char *myaddr; | |
1074 | int len; | |
1075 | { | |
1076 | int i, j; | |
1077 | char buf[20]; | |
1b552670 | 1078 | char packet[PBUFSIZ]; |
51d6a954 RS |
1079 | |
1080 | /* Number of bytes read so far. */ | |
1081 | int count; | |
1082 | ||
1083 | /* Starting address of this pass. */ | |
1084 | unsigned long startaddr; | |
1085 | ||
cf51c601 RS |
1086 | /* Starting address of this pass. */ |
1087 | unsigned long endaddr; | |
1088 | ||
51d6a954 RS |
1089 | /* Number of bytes to read in this pass. */ |
1090 | int len_this_pass; | |
1091 | ||
e6fa5bd6 | 1092 | debuglogs (1, "monitor_read_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len); |
51d6a954 RS |
1093 | |
1094 | /* Note that this code works correctly if startaddr is just less | |
1095 | than UINT_MAX (well, really CORE_ADDR_MAX if there was such a | |
1096 | thing). That is, something like | |
1097 | monitor_read_bytes (CORE_ADDR_MAX - 4, foo, 4) | |
1098 | works--it never adds len To memaddr and gets 0. */ | |
1099 | /* However, something like | |
1100 | monitor_read_bytes (CORE_ADDR_MAX - 3, foo, 4) | |
1101 | doesn't need to work. Detect it and give up if there's an attempt | |
1102 | to do that. */ | |
1103 | if (((memaddr - 1) + len) < memaddr) { | |
1104 | errno = EIO; | |
1105 | return 0; | |
1106 | } | |
1107 | ||
1108 | startaddr = memaddr; | |
1109 | count = 0; | |
1110 | while (count < len) { | |
1111 | len_this_pass = 16; | |
1112 | if ((startaddr % 16) != 0) | |
1113 | len_this_pass -= startaddr % 16; | |
1114 | if (len_this_pass > (len - count)) | |
1115 | len_this_pass = (len - count); | |
cf51c601 RS |
1116 | |
1117 | debuglogs (3, "Display %d bytes at %x for Big Endian host", len_this_pass, startaddr); | |
51d6a954 | 1118 | |
1b552670 RS |
1119 | if (GDBPROTO) { |
1120 | for (i = 0; i < len_this_pass; i++) { | |
1121 | sprintf (buf, "m%08x,%04x", startaddr, len_this_pass); | |
1122 | make_gdb_packet (packet, buf); | |
1123 | if (monitor_send_packet (packet) == 0) | |
1124 | error ("Couldn't transmit packet\n"); | |
1125 | if (monitor_get_packet (packet) == 0) | |
1126 | error ("Couldn't receive packet\n"); | |
1127 | debuglogs (4, "monitor_read_inferior: Got a \"%s\" back\n", packet); | |
1128 | for (j = 0; j < len_this_pass ; j++) { /* extract the byte values */ | |
1129 | myaddr[count++] = from_hex (*(packet+(j*2))) * 16 + from_hex (*(packet+(j*2)+1)); | |
1130 | debuglogs (5, "myaddr set to %x\n", myaddr[count-1]); | |
1131 | } | |
1132 | startaddr += 1; | |
51d6a954 | 1133 | } |
1b552670 RS |
1134 | } else { |
1135 | for (i = 0; i < len_this_pass; i++) { | |
1136 | printf_monitor (ROMCMD(GET_MEM), startaddr, startaddr); | |
1137 | sprintf (buf, ROMCMD(GET_MEM), startaddr, startaddr); | |
1138 | if (*ROMDELIM(GET_MEM) != 0) { /* if there's a delimiter */ | |
1139 | expect (ROMDELIM(GET_MEM), 1); | |
1140 | } else { | |
1141 | sprintf (buf, ROMCMD(GET_MEM), startaddr, startaddr); | |
1142 | expect (buf,1); /* get the command echo */ | |
1143 | get_hex_word(1); /* strip away the address */ | |
1144 | } | |
1145 | get_hex_byte (&myaddr[count++]); /* get the value at this address */ | |
1146 | ||
1147 | if (*ROMDELIM(GET_MEM) != 0) { | |
1148 | printf_monitor (CMD_END); | |
1149 | } | |
1150 | expect_prompt (1); | |
1151 | startaddr += 1; | |
51d6a954 | 1152 | } |
51d6a954 | 1153 | } |
cf51c601 | 1154 | } |
51d6a954 RS |
1155 | return len; |
1156 | } | |
1157 | ||
1158 | /* FIXME-someday! merge these two. */ | |
1159 | int | |
1160 | monitor_xfer_inferior_memory (memaddr, myaddr, len, write, target) | |
1161 | CORE_ADDR memaddr; | |
1162 | char *myaddr; | |
1163 | int len; | |
1164 | int write; | |
1165 | struct target_ops *target; /* ignored */ | |
1166 | { | |
1167 | if (write) | |
1168 | return monitor_write_inferior_memory (memaddr, myaddr, len); | |
1169 | else | |
1170 | return monitor_read_inferior_memory (memaddr, myaddr, len); | |
1171 | } | |
1172 | ||
1173 | void | |
1174 | monitor_kill (args, from_tty) | |
1175 | char *args; | |
1176 | int from_tty; | |
1177 | { | |
1178 | return; /* ignore attempts to kill target system */ | |
1179 | } | |
1180 | ||
1181 | /* Clean up when a program exits. | |
1182 | The program actually lives on in the remote processor's RAM, and may be | |
1183 | run again without a download. Don't leave it full of breakpoint | |
1184 | instructions. */ | |
1185 | ||
1186 | void | |
1187 | monitor_mourn_inferior () | |
1188 | { | |
1189 | remove_breakpoints (); | |
1190 | generic_mourn_inferior (); /* Do all the proper things now */ | |
1191 | } | |
1192 | ||
1193 | #define MAX_MONITOR_BREAKPOINTS 16 | |
1194 | ||
1195 | extern int memory_breakpoint_size; | |
1196 | static CORE_ADDR breakaddr[MAX_MONITOR_BREAKPOINTS] = {0}; | |
1197 | ||
1198 | /* | |
1199 | * monitor_insert_breakpoint -- add a breakpoint | |
1200 | */ | |
1201 | int | |
1202 | monitor_insert_breakpoint (addr, shadow) | |
1203 | CORE_ADDR addr; | |
1204 | char *shadow; | |
1205 | { | |
1206 | int i; | |
1207 | ||
e6fa5bd6 | 1208 | debuglogs (1, "monitor_insert_breakpoint() addr = 0x%x", addr); |
f1ca4cbc RS |
1209 | |
1210 | for (i = 0; i <= MAX_MONITOR_BREAKPOINTS; i++) { | |
1211 | if (breakaddr[i] == 0) { | |
1212 | breakaddr[i] = addr; | |
1213 | if (sr_get_debug() > 4) | |
1214 | printf ("Breakpoint at %x\n", addr); | |
1215 | monitor_read_inferior_memory(addr, shadow, memory_breakpoint_size); | |
1216 | printf_monitor(SET_BREAK_CMD, addr); | |
1217 | expect_prompt(1); | |
1218 | return 0; | |
1219 | } | |
1220 | } | |
1221 | ||
51d6a954 RS |
1222 | fprintf(stderr, "Too many breakpoints (> 16) for monitor\n"); |
1223 | return 1; | |
1224 | } | |
1225 | ||
1226 | /* | |
1227 | * _remove_breakpoint -- Tell the monitor to remove a breakpoint | |
1228 | */ | |
1229 | int | |
1230 | monitor_remove_breakpoint (addr, shadow) | |
1231 | CORE_ADDR addr; | |
1232 | char *shadow; | |
1233 | { | |
1234 | int i; | |
1235 | ||
e6fa5bd6 | 1236 | debuglogs (1, "monitor_remove_breakpoint() addr = 0x%x", addr); |
7804e5bc RS |
1237 | |
1238 | for (i = 0; i < MAX_MONITOR_BREAKPOINTS; i++) { | |
1239 | if (breakaddr[i] == addr) { | |
1240 | breakaddr[i] = 0; | |
1241 | /* some monitors remove breakpoints based on the address */ | |
1242 | if (CLR_BREAK_ADDR) | |
1243 | printf_monitor(CLR_BREAK_CMD, addr); | |
1244 | else | |
1245 | printf_monitor(CLR_BREAK_CMD, i); | |
1246 | expect_prompt(1); | |
1247 | return 0; | |
1248 | } | |
1249 | } | |
51d6a954 RS |
1250 | fprintf(stderr, "Can't find breakpoint associated with 0x%x\n", addr); |
1251 | return 1; | |
1252 | } | |
1253 | ||
21ed3dcd RS |
1254 | /* monitor_load -- load a file. This file determines which of the |
1255 | * supported formats to use. The current types are: | |
1256 | * FIXME: not all types supported yet. | |
06b8f5e4 RS |
1257 | * default - reads any file using bfd and writes it to memory. This |
1258 | * is really slow. | |
21ed3dcd RS |
1259 | * srec - reads binary file using bfd and writes it as an |
1260 | * ascii srecord. | |
1261 | * xmodem-bin - reads a binary file using bfd, and downloads it | |
1262 | * using xmodem protocol. | |
1263 | * xmodem-srec - reads a binary file using bfd, and after converting | |
1264 | * it downloads it as an srecord using xmodem protocol. | |
1265 | * ascii-srec - reads a ascii srecord file and downloads it | |
1266 | * without a change. | |
1267 | * ascii-xmodem - reads a ascii file and downloads using xmodem | |
1268 | * protocol. | |
1269 | */ | |
1270 | void | |
1271 | monitor_load (file, fromtty) | |
1272 | char *file; | |
1273 | int fromtty; | |
1274 | { | |
1275 | FILE *download; | |
1276 | int i, bytes_read; | |
1277 | ||
1278 | debuglogs (1, "Loading %s to monitor", file); | |
51d6a954 | 1279 | |
21ed3dcd RS |
1280 | if (STREQ (loadtype_str, "default")) { /* default, load a binary */ |
1281 | gr_load_image (file, fromtty); /* by writing it into memory */ | |
62b32254 RS |
1282 | return; |
1283 | } | |
1284 | ||
1285 | /* load an srecord by converting */ | |
1286 | if ((STREQ (loadtype_str, "srec")) && STREQ (loadproto_str, "xmodem")) { | |
1287 | monitor_load_srec(file, XMODEM); | |
1288 | return; | |
21ed3dcd RS |
1289 | } |
1290 | ||
1291 | if (STREQ (loadtype_str, "srec")) { /* load an srecord by converting */ | |
06b8f5e4 | 1292 | monitor_load_srec(file, 0); /* if from a binary */ |
62b32254 | 1293 | return; |
21ed3dcd RS |
1294 | } |
1295 | ||
3f9ef4ff RS |
1296 | if (STREQ (loadtype_str, "none")) { /* load an srecord by converting */ |
1297 | error ("Unimplemented"); | |
62b32254 | 1298 | return; |
3f9ef4ff RS |
1299 | } |
1300 | ||
1301 | if (STREQ (loadproto_str, "none")) { /* load an srecord file */ | |
21ed3dcd | 1302 | monitor_load_ascii_srec(file, fromtty); /* if from a binary */ |
62b32254 | 1303 | return; |
21ed3dcd RS |
1304 | } |
1305 | ||
3f9ef4ff | 1306 | if (STREQ (loadproto_str, "xmodem")) { /* load an srecord using the */ |
06b8f5e4 | 1307 | monitor_load_srec(file, XMODEM); |
62b32254 | 1308 | return; |
21ed3dcd RS |
1309 | } |
1310 | } | |
1311 | ||
1312 | /* | |
1313 | * monitor_load_ascii_srec -- download an ASCII srecord file. | |
1314 | */ | |
51d6a954 | 1315 | #define DOWNLOAD_LINE_SIZE 100 |
21ed3dcd RS |
1316 | int |
1317 | monitor_load_ascii_srec (file, fromtty) | |
1318 | char *file; | |
1319 | int fromtty; | |
51d6a954 RS |
1320 | { |
1321 | FILE *download; | |
1322 | char buf[DOWNLOAD_LINE_SIZE]; | |
1323 | int i, bytes_read; | |
1324 | ||
21ed3dcd | 1325 | debuglogs (1, "Loading an ASCII srecord file, %s.", file); |
51d6a954 | 1326 | |
21ed3dcd RS |
1327 | download = fopen (file, "r"); |
1328 | if (download == NULL) { | |
1329 | error ("%s Does not exist", file); | |
51d6a954 RS |
1330 | return; |
1331 | } | |
1332 | ||
1333 | printf_monitor (LOAD_CMD); | |
06b8f5e4 | 1334 | sleep(1); |
21ed3dcd RS |
1335 | while (!feof (download)) { |
1336 | bytes_read = fread (buf, sizeof (char), DOWNLOAD_LINE_SIZE, download); | |
1337 | if (hashmark) { | |
1338 | putchar ('.'); | |
1339 | fflush (stdout); | |
51d6a954 | 1340 | } |
21ed3dcd RS |
1341 | if (SERIAL_WRITE(monitor_desc, buf, bytes_read)) { |
1342 | fprintf(stderr, "SERIAL_WRITE failed: (while downloading) %s\n", safe_strerror(errno)); | |
1343 | break; | |
1344 | } | |
1345 | i = 0; | |
1346 | while (i++ <=200) {} ; /* Ugly HACK, probably needs flow control */ | |
1347 | if (bytes_read < DOWNLOAD_LINE_SIZE) { | |
1348 | if (!feof (download)) | |
1349 | error ("Only read %d bytes\n", bytes_read); | |
1350 | break; | |
51d6a954 | 1351 | } |
21ed3dcd RS |
1352 | } |
1353 | ||
1354 | if (hashmark) { | |
1355 | putchar ('\n'); | |
1356 | } | |
51d6a954 RS |
1357 | if (!feof (download)) |
1358 | error ("Never got EOF while downloading"); | |
21ed3dcd | 1359 | expect_prompt(1); |
51d6a954 RS |
1360 | fclose (download); |
1361 | } | |
1362 | ||
7804e5bc RS |
1363 | /* |
1364 | * monitor_command -- put a command string, in args, out to MONITOR. | |
1365 | * Output from MONITOR is placed on the users terminal until the | |
1366 | * prompt is seen. FIXME: We read the charcters ourseleves here | |
1367 | * cause of a nasty echo. | |
1368 | */ | |
51d6a954 RS |
1369 | void |
1370 | monitor_command (args, fromtty) | |
1371 | char *args; | |
1372 | int fromtty; | |
1373 | { | |
7804e5bc RS |
1374 | |
1375 | char *p; | |
1376 | char c, cp; | |
1377 | p = PROMPT; | |
1378 | ||
e6fa5bd6 RS |
1379 | debuglogs (1, "monitor_command (args=%s)", args); |
1380 | ||
51d6a954 RS |
1381 | if (monitor_desc == NULL) |
1382 | error("monitor target not open."); | |
7804e5bc | 1383 | |
51d6a954 RS |
1384 | if (!args) |
1385 | error("Missing command."); | |
1386 | ||
7804e5bc RS |
1387 | printf_monitor ("%s\n", args); |
1388 | ||
51d6a954 RS |
1389 | expect_prompt(0); |
1390 | } | |
1391 | ||
21ed3dcd | 1392 | /* |
06b8f5e4 RS |
1393 | * monitor_load_srec -- download a binary file by converting it to srecords. This |
1394 | * will also use xmodem to download the resulting file. | |
1395 | * | |
1396 | * A download goes like this when using xmodem: | |
1397 | * Receiver: Sender | |
1398 | * NAK ----------> | |
1399 | * <-------- (packet) [SOH|1|1|data|SUM] | |
1400 | * ACK ----------> | |
1401 | * <-------- (packet) [SOH|2|2|data|SUM] | |
1402 | * ACK ----------> | |
1403 | * <-------- EOT | |
1404 | * ACK ----------> | |
1405 | * | |
1406 | * ACK = 0x06 | |
1407 | * NAK = 0x15 | |
1408 | * EOT = 0x04 | |
1409 | * | |
21ed3dcd RS |
1410 | */ |
1411 | static void | |
06b8f5e4 | 1412 | monitor_load_srec (args, protocol) |
21ed3dcd | 1413 | char *args; |
06b8f5e4 | 1414 | int protocol; |
21ed3dcd RS |
1415 | { |
1416 | bfd *abfd; | |
1417 | asection *s; | |
1418 | char buffer[1024]; | |
06b8f5e4 RS |
1419 | char srec[1024]; |
1420 | char packet[XMODEM_PACKETSIZE]; | |
1421 | int i; | |
1422 | int retries; | |
1423 | int type = 0; /* default to a type 0, header record */ | |
1424 | int srec_frame = 57; /* FIXME: this must be 57 There is 12 bytes | |
1425 | of header, and 2 bytes of checksum at the end. | |
1426 | The problem is an xmodem packet holds exactly | |
1427 | 128 bytes. */ | |
21ed3dcd RS |
1428 | |
1429 | abfd = bfd_openr (args, 0); | |
1430 | if (!abfd) { | |
1431 | printf_filtered ("Unable to open file %s\n", args); | |
1432 | return; | |
1433 | } | |
1434 | ||
1435 | if (bfd_check_format (abfd, bfd_object) == 0) { | |
1436 | printf_filtered ("File is not an object file\n"); | |
1437 | return; | |
1438 | } | |
1439 | ||
06b8f5e4 RS |
1440 | printf_monitor (LOAD_CMD); /* tell the monitor to load */ |
1441 | if (protocol == XMODEM) { /* get the NAK from the target */ | |
1442 | if (GETNAK) { | |
1443 | debuglogs (3, "Got the NAK to start loading"); | |
1444 | } else { | |
1445 | printf_monitor ("%c", EOT); | |
1446 | debuglogs (3, "Never got the NAK to start loading"); | |
1447 | error ("Never got the NAK to start loading"); | |
1448 | } | |
1449 | } | |
1450 | ||
21ed3dcd RS |
1451 | s = abfd->sections; |
1452 | while (s != (asection *) NULL) { | |
21ed3dcd | 1453 | if (s->flags & SEC_LOAD) { |
21ed3dcd | 1454 | char *buffer = xmalloc (srec_frame); |
06b8f5e4 | 1455 | printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma, s->vma + s->_raw_size); |
21ed3dcd RS |
1456 | fflush (stdout); |
1457 | for (i = 0; i < s->_raw_size; i += srec_frame) { | |
1458 | if (srec_frame > s->_raw_size - i) | |
1459 | srec_frame = s->_raw_size - i; | |
1460 | ||
1461 | bfd_get_section_contents (abfd, s, buffer, i, srec_frame); | |
06b8f5e4 RS |
1462 | monitor_make_srec (srec, type, s->vma + i, buffer, srec_frame); |
1463 | if (protocol == XMODEM) { /* send a packet using xmodem */ | |
1464 | make_xmodem_packet (packet, srec, XMODEM_DATASIZE); | |
1465 | write_monitor (packet, XMODEM_PACKETSIZE+1); | |
1466 | retries = 0; | |
1467 | while (retries++ <= 3) { | |
1468 | if (GETNAK) { /* Resend packet */ | |
1469 | debuglogs (3, "Got a NAK, resending packet"); | |
1470 | sleep(1); | |
1471 | write_monitor (packet, XMODEM_PACKETSIZE+1); /* send it again */ | |
1472 | if (GETACK) /* ACKnowledged, get next data chunk */ | |
1473 | break; | |
62b32254 RS |
1474 | } else { /* assume we got an ACK */ |
1475 | if (hashmark) | |
1476 | printf_filtered ("#"); | |
1477 | debuglogs (3, "Got an ACK, sending next packet"); | |
1478 | break; | |
06b8f5e4 RS |
1479 | } |
1480 | } | |
62b32254 | 1481 | if (retries >= 4) { /* too many tries, must be hosed */ |
06b8f5e4 RS |
1482 | printf_monitor ("%c", EOT); |
1483 | error ("Never got a ACK after sending an xmodem packet"); | |
1484 | } | |
1485 | } else { /* no protocols at all */ | |
1486 | printf_monitor ("%s\n", srec); | |
1487 | } | |
1488 | if (hashmark) | |
1489 | printf_filtered ("#"); | |
1490 | type = 3; /* switch to a 4 byte address record */ | |
21ed3dcd RS |
1491 | fflush (stdout); |
1492 | } | |
21ed3dcd | 1493 | free (buffer); |
06b8f5e4 RS |
1494 | } else { |
1495 | debuglogs (3, "%s doesn't need to be loaded", s->name); | |
21ed3dcd RS |
1496 | } |
1497 | s = s->next; | |
1498 | } | |
cf51c601 | 1499 | printf_filtered ("\n"); |
06b8f5e4 RS |
1500 | |
1501 | /* | |
1502 | write a type 7 terminator record. no data for a type 7, | |
1503 | and there is no data, so len is 0. | |
1504 | */ | |
62b32254 RS |
1505 | if (protocol == XMODEM) { /* send a packet using xmodem */ |
1506 | monitor_make_srec (srec, 7, abfd->start_address, "", 0); | |
1507 | make_xmodem_packet (packet, srec, XMODEM_DATASIZE); | |
1508 | write_monitor (packet, XMODEM_PACKETSIZE+1); | |
1509 | } else { | |
1510 | monitor_make_srec (srec, 7, abfd->start_address, "", 0); | |
1511 | printf_monitor ("%s\n", srec); | |
1512 | } | |
06b8f5e4 RS |
1513 | if (protocol == XMODEM) { |
1514 | printf_monitor ("%c", EOT); | |
1515 | if (!GETACK) | |
1516 | error ("Never got ACK after sending EOT"); | |
1517 | } | |
1518 | ||
1519 | if (hashmark) | |
1520 | putchar ('\n'); | |
1521 | ||
21ed3dcd RS |
1522 | expect_prompt (); |
1523 | } | |
1524 | ||
06b8f5e4 RS |
1525 | /* |
1526 | * getacknak -- get an ACK or a NAK from the target. | |
1527 | * returns 1 (true) or 0 (false) This is | |
1528 | * for xmodem. ANy string starting with "***" | |
1529 | * is an error message from the target. | |
1530 | * Here's a few from the WinBond w89k "Cougar" PA board. | |
1531 | * *** Too many errors found. | |
1532 | * *** Bad command | |
1533 | * *** Command syntax error | |
1534 | */ | |
1535 | int | |
1536 | getacknak (byte) | |
1537 | int byte; | |
1538 | { | |
1539 | char character; | |
1540 | int i; | |
1541 | ||
1542 | i = 0; | |
1543 | while (i++ < 60) { | |
1544 | character = (char)readchar (0); | |
b3b8d9bf | 1545 | if ((character == 0xfffffffe) || (character == 0x7f)) { /* empty uart */ |
06b8f5e4 RS |
1546 | if (sr_get_debug() > 3) |
1547 | putchar ('.'); | |
1548 | fflush (stdout); | |
1549 | sleep (1); | |
1550 | continue; | |
1551 | } | |
1552 | if (character == CANCEL) { /* target aborted load */ | |
1553 | expect_prompt (0); | |
1554 | error ("Got a CANCEL from the target."); | |
1555 | } | |
1556 | if (character == '*') { /* look for missed error message */ | |
1557 | expect_prompt (0); | |
1558 | error ("Got an error message from the target"); | |
1559 | } | |
1560 | debuglogs (3, "Got a %s (0x%x or \'%c\'), expecting a %s.\n", | |
1561 | (character == ACK) ? "ACK" : (character == NAK) ? "NAK" : "BOGUS", | |
1562 | character, character, (byte == ACK) ? "ACK" : "NAK"); | |
1563 | if (character == byte) /* got what we wanted */ | |
1564 | return 1; | |
1565 | if (character == ((byte == ACK) ? NAK : ACK)) { /* got the opposite */ | |
1566 | debuglogs (3, "Got the opposite, wanted 0x%x, got a 0x%x", byte, character); | |
1567 | return 0; | |
1568 | } | |
1569 | sleep (1); | |
1570 | } | |
1571 | return 0; | |
1572 | } | |
21ed3dcd | 1573 | |
06b8f5e4 RS |
1574 | /* |
1575 | * monitor_make_srec -- make an srecord. This writes each line, one at a | |
1576 | * time, each with it's own header and trailer line. | |
1577 | * An srecord looks like this: | |
1578 | * | |
1579 | * byte count-+ address | |
1580 | * start ---+ | | data +- checksum | |
1581 | * | | | | | |
1582 | * S01000006F6B692D746573742E73726563E4 | |
1583 | * S315000448600000000000000000FC00005900000000E9 | |
1584 | * S31A0004000023C1400037DE00F023604000377B009020825000348D | |
1585 | * S30B0004485A0000000000004E | |
1586 | * S70500040000F6 | |
1587 | * | |
1588 | * S<type><length><address><data><checksum> | |
1589 | * | |
1590 | * Where | |
1591 | * - length | |
1592 | * is the number of bytes following upto the checksum. Note that | |
1593 | * this is not the number of chars following, since it takes two | |
1594 | * chars to represent a byte. | |
1595 | * - type | |
1596 | * is one of: | |
1597 | * 0) header record | |
1598 | * 1) two byte address data record | |
1599 | * 2) three byte address data record | |
1600 | * 3) four byte address data record | |
1601 | * 7) four byte address termination record | |
1602 | * 8) three byte address termination record | |
1603 | * 9) two byte address termination record | |
1604 | * | |
1605 | * - address | |
1606 | * is the start address of the data following, or in the case of | |
1607 | * a termination record, the start address of the image | |
1608 | * - data | |
1609 | * is the data. | |
1610 | * - checksum | |
1611 | * is the sum of all the raw byte data in the record, from the length | |
1612 | * upwards, modulo 256 and subtracted from 255. | |
1613 | */ | |
1614 | int | |
1615 | monitor_make_srec (buffer, type, memaddr, myaddr, len) | |
1616 | char *buffer; | |
1617 | int type; | |
21ed3dcd RS |
1618 | CORE_ADDR memaddr; |
1619 | unsigned char *myaddr; | |
1620 | int len; | |
1621 | { | |
21ed3dcd | 1622 | int checksum; |
06b8f5e4 RS |
1623 | int i; |
1624 | char *buf; | |
21ed3dcd | 1625 | |
06b8f5e4 RS |
1626 | buf = buffer; |
1627 | debuglogs (4, "monitor_make_srec (buffer=0x%x, type=%d, memaddr=0x%x, len=%d", | |
1628 | buffer, type, memaddr, len); | |
1629 | checksum = 0; | |
1630 | ||
1631 | /* | |
1632 | create the header for the srec. 4 is the number of bytes in the address, | |
1633 | and 1 is the number of bytes in the count. | |
1634 | */ | |
1635 | if (type == 0) /* FIXME: type 0 is optional */ | |
1636 | type = 3; /* so use data as it works */ | |
1637 | sprintf (buf, "S%d%02X%08X", type, len + 4 + 1, memaddr); | |
1638 | buf += 12; | |
1639 | ||
1640 | checksum += (len + 4 + 1 /* calculate the checksum */ | |
1641 | + (memaddr & 0xff) | |
1642 | + ((memaddr >> 8) & 0xff) | |
1643 | + ((memaddr >> 16) & 0xff) | |
1644 | + ((memaddr >> 24) & 0xff)); | |
1645 | ||
1646 | for (i = 0; i < len; i++) { /* build the srecord */ | |
1647 | sprintf (buf, "%02X", myaddr[i]); | |
1648 | checksum += myaddr[i]; | |
1649 | buf += 2; | |
1650 | } | |
21ed3dcd | 1651 | |
06b8f5e4 RS |
1652 | sprintf(buf, "%02X", ~checksum & 0xff); /* add the checksum */ |
1653 | debuglogs (3, "srec is \"%s\"", buffer); | |
1654 | ||
1655 | return(0); | |
1656 | } | |
21ed3dcd | 1657 | |
06b8f5e4 RS |
1658 | /* |
1659 | * make_xmodem_packet -- this takes a 128 bytes of data and makes a packet | |
1660 | * out of it. | |
1661 | * | |
1662 | * Each packet looks like this: | |
1663 | * +-----+-------+-------+------+-----+ | |
1664 | * | SOH | Seq1. | Seq2. | data | SUM | | |
1665 | * +-----+-------+-------+------+-----+ | |
1666 | * SOH = 0x01 | |
1667 | * Seq1 = The sequence number. | |
1668 | * Seq2 = The complement of the sequence number. | |
1669 | * Data = A 128 bytes of data. | |
1670 | * SUM = Add the contents of the 128 bytes and use the low-order | |
1671 | * 8 bits of the result. | |
1672 | */ | |
1b552670 | 1673 | static void |
06b8f5e4 RS |
1674 | make_xmodem_packet (packet, data, len) |
1675 | unsigned char packet[]; | |
1676 | unsigned char *data; | |
1677 | int len; | |
1678 | { | |
1679 | static int sequence = 1; | |
1680 | int i, sum; | |
1681 | unsigned char *buf; | |
1682 | ||
1683 | buf = data; | |
1684 | /* build the packet header */ | |
1685 | packet[0] = SOH; | |
1686 | packet[1] = sequence; | |
1687 | packet[2] = 255 - sequence; | |
1688 | sequence++; | |
21ed3dcd | 1689 | #if 0 |
06b8f5e4 | 1690 | packet[2] = ~sequence++; /* the complement is the sequence checksum */ |
21ed3dcd | 1691 | #endif |
06b8f5e4 RS |
1692 | |
1693 | sum = 0; /* calculate the data checksum */ | |
1694 | for (i = 3; i <= len + 2; i++) { | |
1695 | packet[i] = *buf; | |
1696 | sum += *buf; | |
1697 | buf++; | |
1698 | } | |
1699 | ||
1700 | for (i = len+1 ; i <= XMODEM_DATASIZE ; i++) { /* add padding for the rest of the packet */ | |
1701 | packet[i] = '0'; | |
1702 | } | |
1703 | ||
1704 | packet[XMODEM_PACKETSIZE] = sum & 0xff; /* add the checksum */ | |
1705 | ||
62b32254 | 1706 | if (sr_get_debug() > 4) { |
06b8f5e4 RS |
1707 | debuglogs (4, "The xmodem checksum is %d (0x%x)\n", sum & 0xff, sum & 0xff); |
1708 | print_xmodem_packet (packet); | |
62b32254 | 1709 | } |
06b8f5e4 RS |
1710 | } |
1711 | ||
1712 | /* | |
1713 | * print_xmodem_packet -- print the packet as a debug check | |
1714 | */ | |
1b552670 | 1715 | static void |
06b8f5e4 RS |
1716 | print_xmodem_packet(packet) |
1717 | char packet[]; | |
1718 | { | |
1719 | int i; | |
1720 | static int lastseq; | |
1721 | int sum; | |
1722 | ||
1723 | /* take apart the packet header the packet header */ | |
1724 | if (packet[0] == SOH) { | |
1725 | ("SOH"); | |
1726 | } else { | |
1727 | error ("xmodem: SOH is wrong"); | |
1728 | } | |
1729 | ||
1730 | /* check the sequence */ | |
1731 | if (packet[1] != 0) { | |
1732 | lastseq = packet[1]; | |
1733 | if (packet[2] != ~lastseq) | |
1734 | error ("xmodem: Sequence checksum is wrong"); | |
1735 | else | |
1736 | printf_filtered (" %d %d", lastseq, ~lastseq); | |
1737 | } | |
1738 | ||
1739 | /* check the data checksum */ | |
1740 | sum = 0; | |
1741 | for (i = 3; i <= XMODEM_DATASIZE; i++) { | |
1742 | sum += packet[i]; | |
1743 | } | |
1744 | ||
1745 | /* ignore the data */ | |
21ed3dcd | 1746 | #if 0 |
06b8f5e4 | 1747 | printf (" [128 bytes of data] %d\n", sum & 0xff); |
21ed3dcd | 1748 | #endif |
06b8f5e4 | 1749 | printf_filtered (" [%s] %d\n", packet, sum & 0xff); |
21ed3dcd | 1750 | |
06b8f5e4 RS |
1751 | if ((packet[XMODEM_PACKETSIZE] & 0xff) != (sum & 0xff)) { |
1752 | debuglogs (4, "xmodem: data checksum wrong, got a %d", packet[XMODEM_PACKETSIZE] & 0xff); | |
21ed3dcd | 1753 | } |
06b8f5e4 | 1754 | putchar ('\n'); |
21ed3dcd RS |
1755 | } |
1756 | ||
1b552670 RS |
1757 | /* |
1758 | * make_gdb_packet -- make a GDB packet. The data is always ASCII. | |
1759 | * A debug packet whose contents are <data> | |
1760 | * is encapsulated for transmission in the form: | |
1761 | * | |
1762 | * $ <data> # CSUM1 CSUM2 | |
1763 | * | |
1764 | * <data> must be ASCII alphanumeric and cannot include characters | |
1765 | * '$' or '#'. If <data> starts with two characters followed by | |
1766 | * ':', then the existing stubs interpret this as a sequence number. | |
1767 | * | |
1768 | * CSUM1 and CSUM2 are ascii hex representation of an 8-bit | |
1769 | * checksum of <data>, the most significant nibble is sent first. | |
1770 | * the hex digits 0-9,a-f are used. | |
1771 | * | |
1772 | */ | |
1773 | static void | |
1774 | make_gdb_packet (buf, data) | |
1775 | char *buf, *data; | |
1776 | { | |
1777 | int i; | |
1778 | unsigned char csum = 0; | |
1779 | int cnt; | |
1780 | char *p; | |
1781 | ||
1782 | debuglogs (3, "make_gdb_packet(%s)\n", data); | |
1783 | cnt = strlen (data); | |
1784 | if (cnt > PBUFSIZ) | |
1785 | error ("make_gdb_packet(): to much data\n"); | |
1786 | ||
1787 | /* start with the packet header */ | |
1788 | p = buf; | |
1789 | *p++ = '$'; | |
1790 | ||
1791 | /* calculate the checksum */ | |
1792 | for (i = 0; i < cnt; i++) { | |
1793 | csum += data[i]; | |
1794 | *p++ = data[i]; | |
1795 | } | |
1796 | ||
1797 | /* terminate the data with a '#' */ | |
1798 | *p++ = '#'; | |
1799 | ||
1800 | /* add the checksum as two ascii digits */ | |
1801 | *p++ = tohex ((csum >> 4) & 0xf); | |
1802 | *p++ = tohex (csum & 0xf); | |
1803 | *p = 0x0; /* Null terminator on string */ | |
1804 | } | |
1805 | ||
1806 | /* | |
1807 | * monitor_send_packet -- send a GDB packet to the target with error handling. We | |
1808 | * get a '+' (ACK) back if the packet is received and the checksum | |
1809 | * matches. Otherwise a '-' (NAK) is returned. It returns a 1 for a | |
1810 | * successful transmition, or a 0 for a failure. | |
1811 | */ | |
1812 | int | |
1813 | monitor_send_packet (packet) | |
1814 | char *packet; | |
1815 | { | |
1816 | int c, retries, i; | |
1817 | char junk[PBUFSIZ]; | |
1818 | ||
1819 | retries = 0; | |
1820 | ||
1821 | #if 0 | |
1822 | /* scan the packet to make sure it only contains valid characters. | |
1823 | this may sound silly, but sometimes a garbled packet will hang | |
1824 | the target board. We scan the whole thing, then print the error | |
1825 | message. | |
1826 | */ | |
1827 | for (i = 0; i < strlen(packet); i++) { | |
1828 | debuglogs (5, "monitor_send_packet(): Scanning \'%c\'\n", packet[i]); | |
1829 | /* legit hex numbers or command */ | |
1830 | if ((isxdigit(packet[i])) || (isalpha(packet[i]))) | |
1831 | continue; | |
1832 | switch (packet[i]) { | |
1833 | case '+': /* ACK */ | |
1834 | case '-': /* NAK */ | |
1835 | case '#': /* end of packet */ | |
1836 | case '$': /* start of packet */ | |
1837 | continue; | |
1838 | default: /* bogus character */ | |
1839 | retries++; | |
1840 | debuglogs (4, "monitor_send_packet(): Found a non-ascii digit \'%c\' in the packet.\n", packet[i]); | |
1841 | } | |
1842 | } | |
1843 | #endif | |
1844 | ||
1845 | if (retries > 0) | |
1846 | error ("Can't send packet, found %d non-ascii characters", retries); | |
1847 | ||
1848 | /* ok, try to send the packet */ | |
1849 | retries = 0; | |
1850 | while (retries <= 10) { | |
1851 | printf_monitor ("%s", packet); | |
1852 | ||
1853 | /* read until either a timeout occurs (-2) or '+' is read */ | |
1854 | while (retries <= 10) { | |
1855 | c = readchar (timeout); | |
1856 | debuglogs (3, "Reading a GDB protocol packet... Got a '%c'\n", c); | |
1857 | switch (c) { | |
1858 | case '+': | |
1859 | debuglogs (3, "Got Ack\n"); | |
1860 | return 1; | |
1861 | case SERIAL_TIMEOUT: | |
1862 | debuglogs (3, "Timed out reading serial port\n"); | |
1863 | break; /* Retransmit buffer */ | |
1864 | case '-': | |
1865 | debuglogs (3, "Got NAK\n"); | |
1866 | break; /* FIXME: (maybe) was a continue */ | |
1867 | case '$': | |
1868 | /* it's probably an old response, or the echo of our command. | |
1869 | * just gobble up the packet and ignore it. | |
1870 | */ | |
1871 | debuglogs (3, "Got a junk packet\n"); | |
1872 | do { | |
1873 | c = readchar (timeout); | |
1874 | junk[i++] = c; | |
1875 | } while (c != '#'); | |
1876 | c = readchar (timeout); | |
1877 | junk[i++] = c; | |
1878 | c = readchar (timeout); | |
1879 | junk[i++] = c; | |
1880 | junk[i++] = '\0'; | |
1881 | debuglogs (3, "Reading a junk packet, got a \"%s\"\n", junk); | |
1882 | continue; /* Now, go look for next packet */ | |
1883 | default: | |
1884 | continue; | |
1885 | } | |
1886 | retries++; | |
1887 | debuglogs (3, "Retransmitting packet \"%s\"\n", packet); | |
1888 | break; /* Here to retransmit */ | |
1889 | } | |
1890 | } /* outer while */ | |
1891 | return 0; | |
1892 | } | |
1893 | ||
1894 | /* | |
1895 | * monitor_get_packet -- get a GDB packet from the target. Basically we read till we | |
1896 | * see a '#', then check the checksum. It returns a 1 if it's gotten a | |
1897 | * packet, or a 0 it the packet wasn't transmitted correctly. | |
1898 | */ | |
1899 | int | |
1900 | monitor_get_packet (packet) | |
1901 | char *packet; | |
1902 | { | |
1903 | int c; | |
1904 | int retries; | |
1905 | unsigned char csum; | |
1906 | unsigned char pktcsum; | |
1907 | char *bp; | |
1908 | ||
1909 | csum = 0; | |
1910 | bp = packet; | |
1911 | ||
1912 | memset (packet, 1, PBUFSIZ); | |
1913 | retries = 0; | |
1914 | while (retries <= 10) { | |
1915 | do { | |
1916 | c = readchar (timeout); | |
1917 | if (c == SERIAL_TIMEOUT) { | |
1918 | debuglogs (3, "monitor_get_packet: got time out from serial port.\n"); | |
1919 | } | |
1920 | debuglogs (3, "Waiting for a '$', got a %c\n", c); | |
1921 | } while (c != '$'); | |
1922 | ||
1923 | retries = 0; | |
1924 | while (retries <= 10) { | |
1925 | c = readchar (timeout); | |
1926 | debuglogs (3, "monitor_get_packet: got a '%c'\n", c); | |
1927 | switch (c) { | |
1928 | case SERIAL_TIMEOUT: | |
1929 | debuglogs (3, "Timeout in mid-packet, retrying\n"); | |
1930 | return 0; | |
1931 | case '$': | |
1932 | debuglogs (3, "Saw new packet start in middle of old one\n"); | |
1933 | return 0; /* Start a new packet, count retries */ | |
1934 | case '#': | |
1935 | *bp = '\0'; | |
1936 | pktcsum = from_hex (readchar (timeout)) << 4; | |
1937 | pktcsum |= from_hex (readchar (timeout)); | |
1938 | if (csum == pktcsum) { | |
1939 | debuglogs (3, "\nGDB packet checksum correct, packet data is \"%s\",\n", packet); | |
1940 | printf_monitor ("+"); | |
1941 | expect_prompt (1); | |
1942 | return 1; | |
1943 | } | |
1944 | debuglogs (3, "Bad checksum, sentsum=0x%x, csum=0x%x\n", pktcsum, csum); | |
1945 | return 0; | |
1946 | case '*': /* Run length encoding */ | |
1947 | debuglogs (5, "Run length encoding in packet\n"); | |
1948 | csum += c; | |
1949 | c = readchar (timeout); | |
1950 | csum += c; | |
1951 | c = c - ' ' + 3; /* Compute repeat count */ | |
1952 | ||
1953 | if (c > 0 && c < 255 && bp + c - 1 < packet + PBUFSIZ - 1) { | |
1954 | memset (bp, *(bp - 1), c); | |
1955 | bp += c; | |
1956 | continue; | |
1957 | } | |
1958 | *bp = '\0'; | |
1959 | printf_filtered ("Repeat count %d too large for buffer.\n", c); | |
1960 | return 0; | |
1961 | ||
1962 | default: | |
1963 | if ((!isxdigit(c)) && (!ispunct(c))) | |
1964 | debuglogs (4, "Got a non-ascii digit \'%c\'.\\n", c); | |
1965 | if (bp < packet + PBUFSIZ - 1) { | |
1966 | *bp++ = c; | |
1967 | csum += c; | |
1968 | continue; | |
1969 | } | |
1970 | ||
1971 | *bp = '\0'; | |
1972 | puts_filtered ("Remote packet too long.\n"); | |
1973 | return 0; | |
1974 | } | |
1975 | } | |
1976 | } | |
1977 | } | |
1978 | ||
1979 | /* | |
1980 | * ascii2hexword -- convert an ascii number represented by 8 digits to a hex value. | |
1981 | */ | |
1982 | static unsigned long | |
1983 | ascii2hexword (mem) | |
1984 | unsigned char *mem; | |
1985 | { | |
1986 | unsigned long val; | |
1987 | int i; | |
1988 | char buf[9]; | |
1989 | ||
1990 | val = 0; | |
1991 | for (i = 0; i < 8; i++) { | |
1992 | val <<= 4; | |
1993 | if (mem[i] >= 'A' && mem[i] <= 'F') | |
1994 | val = val + mem[i] - 'A' + 10; | |
1995 | if (mem[i] >= 'a' && mem[i] <= 'f') | |
1996 | val = val + mem[i] - 'a' + 10; | |
1997 | if (mem[i] >= '0' && mem[i] <= '9') | |
1998 | val = val + mem[i] - '0'; | |
1999 | buf[i] = mem[i]; | |
2000 | } | |
2001 | buf[8] = '\0'; | |
2002 | debuglogs (4, "ascii2hexword() got a 0x%x from %s(%x).\n", val, buf, mem); | |
2003 | return val; | |
2004 | } | |
2005 | ||
2006 | /* | |
2007 | * ascii2hexword -- convert a hex value to an ascii number represented by 8 | |
2008 | * digits. | |
2009 | */ | |
2010 | static char* | |
2011 | hexword2ascii (mem, num) | |
2012 | unsigned char *mem; | |
2013 | unsigned long num; | |
2014 | { | |
2015 | int i; | |
2016 | unsigned char ch; | |
2017 | ||
2018 | debuglogs (4, "hexword2ascii() converting %x ", num); | |
2019 | for (i = 7; i >= 0; i--) { | |
2020 | mem[i] = tohex ((num >> 4) & 0xf); | |
2021 | mem[i] = tohex (num & 0xf); | |
2022 | num = num >> 4; | |
2023 | } | |
2024 | mem[8] = '\0'; | |
2025 | debuglogs (4, "\tto a %s", mem); | |
2026 | } | |
2027 | ||
2028 | /* Convert hex digit A to a number. */ | |
2029 | static int | |
2030 | from_hex (a) | |
2031 | int a; | |
2032 | { | |
2033 | if (a == 0) | |
2034 | return 0; | |
2035 | ||
2036 | debuglogs (4, "from_hex got a 0x%x(%c)\n",a,a); | |
2037 | if (a >= '0' && a <= '9') | |
2038 | return a - '0'; | |
2039 | if (a >= 'a' && a <= 'f') | |
2040 | return a - 'a' + 10; | |
2041 | if (a >= 'A' && a <= 'F') | |
2042 | return a - 'A' + 10; | |
2043 | else { | |
2044 | error ("Reply contains invalid hex digit 0x%x", a); | |
2045 | } | |
2046 | } | |
2047 | ||
2048 | /* Convert number NIB to a hex digit. */ | |
2049 | static int | |
2050 | tohex (nib) | |
2051 | int nib; | |
2052 | { | |
2053 | if (nib < 10) | |
2054 | return '0'+nib; | |
2055 | else | |
2056 | return 'a'+nib-10; | |
2057 | } | |
2058 | ||
51d6a954 RS |
2059 | /* |
2060 | * _initialize_remote_monitors -- setup a few addtitional commands that | |
2061 | * are usually only used by monitors. | |
2062 | */ | |
2063 | void | |
2064 | _initialize_remote_monitors () | |
2065 | { | |
2066 | struct cmd_list_element *c; | |
2067 | ||
2068 | /* this sets the type of download protocol */ | |
b3b8d9bf RS |
2069 | c = add_set_cmd ("remoteloadprotocol", no_class, var_string, (char *)&loadproto_str, |
2070 | "Set the type of the remote load protocol.\n", &setlist); | |
2071 | c->function.sfunc = set_loadproto_command; | |
2072 | add_show_from_set (c, &showlist); | |
2073 | loadproto_str = savestring ("none", 5); | |
2074 | ||
2075 | /* this sets the conversion type when loading */ | |
2076 | c = add_set_cmd ("remoteloadtype", no_class, var_string, (char *)&loadtype_str, | |
51d6a954 RS |
2077 | "Set the type of the remote load protocol.\n", &setlist); |
2078 | c->function.sfunc = set_loadtype_command; | |
2079 | add_show_from_set (c, &showlist); | |
b3b8d9bf | 2080 | loadtype_str = savestring ("srec", 5); |
51d6a954 RS |
2081 | |
2082 | add_show_from_set (add_set_cmd ("hash", no_class, var_boolean, | |
2083 | (char *)&hashmark, | |
2084 | "Set display of activity while downloading a file.\n\ | |
2085 | When enabled, a period \'.\' is displayed.", | |
2086 | &setlist), | |
2087 | &showlist); | |
2088 | ||
2089 | /* generic monitor command */ | |
2090 | add_com ("monitor", class_obscure, monitor_command, | |
2091 | "Send a command to the debug monitor."); | |
2092 | } |