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