Commit | Line | Data |
---|---|---|
51d6a954 | 1 | /* Remote debugging interface for boot monitors, for GDB. |
431b7d5f | 2 | Copyright 1990, 1991, 1992, 1993, 1995 Free Software Foundation, Inc. |
51d6a954 RS |
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, | |
431b7d5f | 29 | which in turn talks to the target board. */ |
51d6a954 RS |
30 | |
31 | #include "defs.h" | |
32 | #include "gdbcore.h" | |
33 | #include "target.h" | |
34 | #include "wait.h" | |
35 | #include <varargs.h> | |
36 | #include <signal.h> | |
37 | #include <string.h> | |
38 | #include <sys/types.h> | |
39 | #include "command.h" | |
40 | #include "serial.h" | |
41 | #include "monitor.h" | |
1265e2d8 SG |
42 | #include "gdbcmd.h" |
43 | #include "inferior.h" | |
51d6a954 | 44 | |
8f078234 SG |
45 | static void monitor_command PARAMS ((char *args, int fromtty)); |
46 | static void monitor_load_srec PARAMS ((char *args, int protocol)); | |
47 | static int getacknak PARAMS ((int byte)); | |
48 | ||
49 | static void make_xmodem_packet PARAMS ((unsigned char *packet, | |
50 | unsigned char *data, | |
51 | int len)); | |
52 | static void print_xmodem_packet PARAMS ((char *packet)); | |
431b7d5f SS |
53 | |
54 | static void monitor_load_ascii_srec PARAMS ((char *file, int fromtty)); | |
55 | ||
56 | static int monitor_make_srec PARAMS ((char *buffer, int type, | |
57 | CORE_ADDR memaddr, | |
58 | unsigned char *myaddr, int len)); | |
59 | ||
1265e2d8 SG |
60 | static void monitor_fetch_register PARAMS ((int regno)); |
61 | static void monitor_store_register PARAMS ((int regno)); | |
62 | ||
8f078234 | 63 | static int from_hex PARAMS ((int a)); |
1265e2d8 | 64 | static unsigned long get_hex_word PARAMS ((void)); |
06b8f5e4 | 65 | |
8f078234 | 66 | static struct monitor_ops *current_monitor; |
431b7d5f | 67 | |
1265e2d8 SG |
68 | static char *loadtype_str = "srec"; |
69 | static char *loadproto_str = "none"; | |
51d6a954 RS |
70 | |
71 | static int hashmark; /* flag set by "set hash" */ | |
72 | ||
1b552670 | 73 | static int timeout = 30; |
431b7d5f | 74 | |
1265e2d8 SG |
75 | static int expect PARAMS ((char *string, char *buf, int buflen)); |
76 | static int expect_prompt PARAMS ((char *buf, int buflen)); | |
77 | ||
1b552670 RS |
78 | /* Having this larger than 400 causes us to be incompatible with m68k-stub.c |
79 | and i386-stub.c. Normally, no one would notice because it only matters | |
80 | for writing large chunks of memory (e.g. in downloads). Also, this needs | |
81 | to be more than 400 if required to hold the registers (see below, where | |
82 | we round it up based on REGISTER_BYTES). */ | |
431b7d5f | 83 | |
1b552670 | 84 | #define PBUFSIZ 400 |
51d6a954 | 85 | |
431b7d5f SS |
86 | /* Descriptor for I/O to remote machine. Initialize it to NULL so |
87 | that monitor_open knows that we don't have a file open when the | |
88 | program starts. */ | |
89 | ||
1265e2d8 | 90 | static serial_t monitor_desc = NULL; |
431b7d5f | 91 | |
431b7d5f SS |
92 | /* These definitions are for xmodem protocol. */ |
93 | ||
06b8f5e4 RS |
94 | #define SOH 0x01 |
95 | #define ACK 0x06 | |
96 | #define NAK 0x15 | |
97 | #define EOT 0x04 | |
98 | #define CANCEL 0x18 | |
99 | #define GETACK getacknak(ACK) | |
100 | #define GETNAK getacknak(NAK) | |
101 | #define XMODEM_DATASIZE 128 /* the data size is ALWAYS 128 */ | |
102 | #define XMODEM_PACKETSIZE 131 /* the packet size is ALWAYS 132 (zero based) */ | |
103 | #define XMODEM 1 | |
104 | ||
1265e2d8 SG |
105 | static unsigned char output_buf[0x200]; |
106 | static int obp; | |
107 | ||
51d6a954 | 108 | static void |
1265e2d8 SG |
109 | debug_save_output (buf, len) |
110 | unsigned char *buf; | |
111 | int len; | |
51d6a954 | 112 | { |
1265e2d8 SG |
113 | #if 0 |
114 | for (; len > 0; len--) | |
115 | output_buf[obp++ & 0x1ff] = *buf++; | |
116 | #else | |
117 | fputs_unfiltered (buf, gdb_stdout); | |
118 | #endif | |
119 | } | |
3f9ef4ff | 120 | |
1265e2d8 SG |
121 | static unsigned char input_buf[0x200]; |
122 | static int ibp; | |
3f9ef4ff | 123 | |
b3b8d9bf | 124 | static void |
1265e2d8 SG |
125 | debug_save_input_char (c) |
126 | int c; | |
b3b8d9bf | 127 | { |
1265e2d8 SG |
128 | #if 0 |
129 | input_buf[ibp++ & 0x1ff] = c; | |
130 | #else | |
131 | fputc_unfiltered (c, gdb_stdout); | |
132 | #endif | |
b3b8d9bf | 133 | } |
51d6a954 | 134 | |
431b7d5f SS |
135 | /* printf_monitor -- send data to monitor. Works just like printf. */ |
136 | ||
51d6a954 | 137 | static void |
431b7d5f | 138 | printf_monitor (va_alist) |
51d6a954 RS |
139 | va_dcl |
140 | { | |
141 | va_list args; | |
142 | char *pattern; | |
1b552670 | 143 | char buf[PBUFSIZ]; |
51d6a954 | 144 | |
431b7d5f | 145 | va_start (args); |
51d6a954 | 146 | |
431b7d5f | 147 | pattern = va_arg (args, char *); |
51d6a954 | 148 | |
431b7d5f | 149 | vsprintf (buf, pattern, args); |
51d6a954 | 150 | |
1265e2d8 SG |
151 | if (remote_debug > 0) |
152 | debug_save_output (buf, strlen (buf)); | |
51d6a954 | 153 | |
431b7d5f | 154 | if (strlen (buf) > PBUFSIZ) |
1b552670 | 155 | error ("printf_monitor(): string too long"); |
431b7d5f | 156 | if (SERIAL_WRITE(monitor_desc, buf, strlen (buf))) |
1265e2d8 | 157 | fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno)); |
51d6a954 | 158 | } |
431b7d5f SS |
159 | |
160 | /* Send raw data to monitor. */ | |
161 | ||
06b8f5e4 | 162 | static void |
431b7d5f SS |
163 | write_monitor (data, len) |
164 | char *data; | |
06b8f5e4 RS |
165 | int len; |
166 | { | |
431b7d5f | 167 | if (SERIAL_WRITE (monitor_desc, data, len)) |
1265e2d8 | 168 | fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno)); |
06b8f5e4 | 169 | |
431b7d5f | 170 | *(data + len + 1) = '\0'; |
e6fa5bd6 RS |
171 | } |
172 | ||
431b7d5f SS |
173 | /* Read a character from the remote system, doing all the fancy |
174 | timeout stuff. */ | |
175 | ||
51d6a954 | 176 | static int |
431b7d5f | 177 | readchar (timeout) |
51d6a954 RS |
178 | int timeout; |
179 | { | |
180 | int c; | |
181 | ||
431b7d5f | 182 | c = SERIAL_READCHAR (monitor_desc, timeout); |
51d6a954 | 183 | |
1265e2d8 SG |
184 | if (remote_debug > 0) |
185 | debug_save_input_char (c & 0x7f); | |
51d6a954 RS |
186 | |
187 | if (c >= 0) | |
188 | return c & 0x7f; | |
189 | ||
431b7d5f SS |
190 | if (c == SERIAL_TIMEOUT) |
191 | { | |
192 | if (timeout == 0) | |
193 | return c; /* Polls shouldn't generate timeout errors */ | |
194 | error ("Timeout reading from remote system."); | |
431b7d5f SS |
195 | } |
196 | perror_with_name ("remote-monitor"); | |
51d6a954 RS |
197 | } |
198 | ||
1265e2d8 SG |
199 | /* Scan input from the remote system, until STRING is found. If BUF is non- |
200 | zero, then collect input until either STRING has been collected or BUFLEN | |
201 | chars have been collected. If input overflows BUF because STRING can't be | |
202 | found, return -1, else return number of chars in BUF (including STRING). */ | |
431b7d5f | 203 | |
1265e2d8 SG |
204 | static int |
205 | expect (string, buf, buflen) | |
51d6a954 | 206 | char *string; |
1265e2d8 SG |
207 | char *buf; |
208 | int buflen; | |
51d6a954 RS |
209 | { |
210 | char *p = string; | |
1265e2d8 | 211 | int obuflen = buflen; |
51d6a954 RS |
212 | int c; |
213 | ||
51d6a954 | 214 | immediate_quit = 1; |
431b7d5f SS |
215 | while (1) |
216 | { | |
1265e2d8 | 217 | if (buf) |
431b7d5f | 218 | { |
1265e2d8 | 219 | if (buflen <= 0) |
431b7d5f SS |
220 | { |
221 | immediate_quit = 0; | |
1265e2d8 | 222 | return -1; |
431b7d5f | 223 | } |
1265e2d8 SG |
224 | |
225 | c = readchar (timeout); | |
226 | *buf++ = c; | |
227 | buflen--; | |
431b7d5f SS |
228 | } |
229 | else | |
1265e2d8 SG |
230 | c = readchar (timeout); |
231 | ||
232 | if (c == *p++) | |
431b7d5f | 233 | { |
1265e2d8 | 234 | if (*p == '\0') |
431b7d5f | 235 | { |
1265e2d8 SG |
236 | immediate_quit = 0; |
237 | ||
238 | return obuflen - buflen; | |
431b7d5f | 239 | } |
1265e2d8 SG |
240 | } |
241 | else | |
242 | { | |
431b7d5f | 243 | p = string; |
1265e2d8 SG |
244 | if (c == *p) |
245 | p++; | |
431b7d5f | 246 | } |
51d6a954 | 247 | } |
51d6a954 RS |
248 | } |
249 | ||
250 | /* Keep discarding input until we see the MONITOR prompt. | |
251 | ||
252 | The convention for dealing with the prompt is that you | |
253 | o give your command | |
254 | o *then* wait for the prompt. | |
255 | ||
256 | Thus the last thing that a procedure does with the serial line | |
257 | will be an expect_prompt(). Exception: monitor_resume does not | |
258 | wait for the prompt, because the terminal is being handed over | |
259 | to the inferior. However, the next thing which happens after that | |
260 | is a monitor_wait which does wait for the prompt. | |
261 | Note that this includes abnormal exit, e.g. error(). This is | |
262 | necessary to prevent getting into states from which we can't | |
263 | recover. */ | |
431b7d5f | 264 | |
1265e2d8 SG |
265 | static int |
266 | expect_prompt (buf, buflen) | |
267 | char *buf; | |
268 | int buflen; | |
51d6a954 | 269 | { |
1265e2d8 | 270 | return expect (PROMPT, buf, buflen); |
51d6a954 RS |
271 | } |
272 | ||
431b7d5f SS |
273 | /* Get N 32-bit words from remote, each preceded by a space, and put |
274 | them in registers starting at REGNO. */ | |
275 | ||
1265e2d8 | 276 | static unsigned long |
51d6a954 RS |
277 | get_hex_word () |
278 | { | |
1265e2d8 | 279 | unsigned long val; |
51d6a954 | 280 | int i; |
1265e2d8 | 281 | int ch; |
51d6a954 | 282 | |
1265e2d8 SG |
283 | do |
284 | ch = readchar (timeout); | |
285 | while (isspace(ch)); | |
cf51c601 | 286 | |
1265e2d8 SG |
287 | val = from_hex (ch); |
288 | ||
289 | for (i = 7; i >= 1; i--) | |
431b7d5f | 290 | { |
1265e2d8 SG |
291 | ch = readchar (timeout); |
292 | if (!isxdigit (ch)) | |
293 | break; | |
294 | val = (val << 4) | from_hex (ch); | |
431b7d5f | 295 | } |
51d6a954 RS |
296 | |
297 | return val; | |
298 | } | |
299 | ||
431b7d5f SS |
300 | /* Open a connection to a remote debugger. NAME is the filename used |
301 | for communication. */ | |
302 | ||
1265e2d8 | 303 | static char *dev_name; |
8f078234 | 304 | static struct target_ops *targ_ops; |
51d6a954 RS |
305 | |
306 | void | |
1265e2d8 | 307 | monitor_open (args, mon_ops, from_tty) |
51d6a954 | 308 | char *args; |
1265e2d8 | 309 | struct monitor_ops *mon_ops; |
51d6a954 RS |
310 | int from_tty; |
311 | { | |
1265e2d8 SG |
312 | char *name; |
313 | int i; | |
314 | ||
315 | targ_ops = mon_ops->target; | |
316 | name = targ_ops->to_shortname; | |
51d6a954 | 317 | |
1265e2d8 | 318 | if (!args) |
51d6a954 RS |
319 | error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\ |
320 | `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name); | |
321 | ||
1265e2d8 SG |
322 | target_preopen (from_tty); |
323 | ||
324 | unpush_target (targ_ops); | |
325 | ||
326 | if (dev_name) | |
327 | free (dev_name); | |
328 | dev_name = strsave (args); | |
51d6a954 | 329 | |
431b7d5f | 330 | monitor_desc = SERIAL_OPEN (dev_name); |
51d6a954 | 331 | |
1265e2d8 | 332 | if (!monitor_desc) |
431b7d5f SS |
333 | perror_with_name (dev_name); |
334 | ||
335 | if (baud_rate != -1) | |
336 | { | |
337 | if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate)) | |
338 | { | |
339 | SERIAL_CLOSE (monitor_desc); | |
1265e2d8 | 340 | perror_with_name (dev_name); |
431b7d5f | 341 | } |
51d6a954 | 342 | } |
7a1330f7 | 343 | |
431b7d5f | 344 | SERIAL_RAW (monitor_desc); |
51d6a954 | 345 | |
1265e2d8 SG |
346 | SERIAL_FLUSH_INPUT (monitor_desc); |
347 | ||
cf51c601 | 348 | /* some systems only work with 2 stop bits */ |
1265e2d8 SG |
349 | |
350 | SERIAL_SETSTOPBITS (monitor_desc, mon_ops->stopbits); | |
351 | ||
352 | current_monitor = mon_ops; | |
51d6a954 | 353 | |
431b7d5f | 354 | /* see if the target is alive. For a ROM monitor, we can just try to |
1265e2d8 | 355 | force the prompt to print a few times. */ |
431b7d5f | 356 | |
1265e2d8 SG |
357 | /* wake up the monitor and see if it's alive */ |
358 | printf_monitor (mon_ops->init); | |
359 | expect_prompt (NULL, 0); /* See if we get a prompt */ | |
431b7d5f | 360 | |
1265e2d8 SG |
361 | /* try again to be sure */ |
362 | printf_monitor (mon_ops->init); | |
363 | expect_prompt (NULL, 0); /* See if we get a prompt */ | |
364 | ||
365 | /* Setup the suffixes for the `set remoteloadtype' command */ | |
366 | ||
367 | add_show_from_set (add_set_enum_cmd ("remoteloadtype", no_class, | |
368 | mon_ops->loadtypes, | |
369 | (char *)&loadtype_str, | |
370 | "Set the remote load type.", | |
371 | &setlist), | |
372 | &showlist); | |
373 | ||
374 | /* Setup the suffixes for the `set remoteloadprotocol' command */ | |
375 | ||
376 | add_show_from_set (add_set_enum_cmd ("remoteloadprotocol", no_class, | |
377 | mon_ops->loadprotos, | |
378 | (char *)&loadproto_str, | |
379 | "Set the remote load protocol.", | |
380 | &setlist), | |
381 | &showlist); | |
51d6a954 RS |
382 | |
383 | if (from_tty) | |
1265e2d8 SG |
384 | printf_unfiltered ("Remote target %s connected to %s\n", name, dev_name); |
385 | ||
386 | push_target (targ_ops); | |
387 | ||
388 | inferior_pid = 42000; /* Make run command think we are busy... */ | |
389 | ||
390 | printf_monitor ("\r"); | |
391 | ||
392 | start_remote (); | |
51d6a954 RS |
393 | } |
394 | ||
431b7d5f SS |
395 | /* Close out all files and local state before this target loses |
396 | control. */ | |
51d6a954 RS |
397 | |
398 | void | |
399 | monitor_close (quitting) | |
400 | int quitting; | |
401 | { | |
1265e2d8 SG |
402 | if (monitor_desc) |
403 | SERIAL_CLOSE (monitor_desc); | |
51d6a954 | 404 | monitor_desc = NULL; |
51d6a954 RS |
405 | } |
406 | ||
431b7d5f SS |
407 | /* Terminate the open connection to the remote debugger. Use this |
408 | when you want to detach and do something else with your gdb. */ | |
409 | ||
51d6a954 | 410 | void |
1265e2d8 SG |
411 | monitor_detach (args, from_tty) |
412 | char *args; | |
51d6a954 RS |
413 | int from_tty; |
414 | { | |
431b7d5f | 415 | pop_target (); /* calls monitor_close to do the real work */ |
51d6a954 | 416 | if (from_tty) |
1265e2d8 | 417 | printf_unfiltered ("Ending remote %s debugging\n", target_shortname); |
51d6a954 | 418 | } |
7804e5bc | 419 | |
431b7d5f SS |
420 | /* Tell the remote machine to resume. */ |
421 | ||
51d6a954 RS |
422 | void |
423 | monitor_resume (pid, step, sig) | |
424 | int pid, step; | |
425 | enum target_signal sig; | |
426 | { | |
431b7d5f | 427 | if (step) |
7804e5bc | 428 | printf_monitor (STEP_CMD); |
431b7d5f | 429 | else |
7804e5bc | 430 | printf_monitor (CONT_CMD); |
51d6a954 RS |
431 | } |
432 | ||
431b7d5f SS |
433 | /* Wait until the remote machine stops, then return, storing status in |
434 | status just as `wait' would. */ | |
435 | ||
51d6a954 RS |
436 | int |
437 | monitor_wait (pid, status) | |
438 | int pid; | |
439 | struct target_waitstatus *status; | |
440 | { | |
441 | int old_timeout = timeout; | |
51d6a954 RS |
442 | |
443 | status->kind = TARGET_WAITKIND_EXITED; | |
444 | status->value.integer = 0; | |
445 | ||
1265e2d8 | 446 | timeout = -1; /* Don't time out -- user program is running. */ |
51d6a954 | 447 | |
1265e2d8 | 448 | expect_prompt (NULL, 0); /* Wait for prompt, outputting extraneous text */ |
51d6a954 RS |
449 | |
450 | status->kind = TARGET_WAITKIND_STOPPED; | |
451 | status->value.sig = TARGET_SIGNAL_TRAP; | |
452 | ||
453 | timeout = old_timeout; | |
454 | ||
1265e2d8 | 455 | return inferior_pid; |
51d6a954 RS |
456 | } |
457 | ||
1265e2d8 SG |
458 | /* Fetch register REGNO, or all registers if REGNO is -1. Returns |
459 | errno value. */ | |
51d6a954 | 460 | |
1265e2d8 SG |
461 | static void |
462 | monitor_fetch_register (regno) | |
463 | int regno; | |
464 | { | |
465 | unsigned LONGEST val; | |
466 | unsigned char regbuf[MAX_REGISTER_RAW_SIZE]; | |
467 | char buf[200]; | |
468 | char *p, *p1; | |
469 | char *name; | |
470 | int resp_len; | |
51d6a954 | 471 | |
8f078234 | 472 | name = REGNAMES (regno); |
f1ca4cbc | 473 | |
1265e2d8 SG |
474 | if (!name) |
475 | return; | |
51d6a954 | 476 | |
1265e2d8 | 477 | /* send the register examine command */ |
431b7d5f | 478 | |
1265e2d8 | 479 | printf_monitor (current_monitor->getreg.cmd, name); |
1b552670 | 480 | |
1265e2d8 SG |
481 | /* If TERM is present, we wait for that to show up. Also, (if TERM is |
482 | present), we will send TERM_CMD if that is present. In any case, we collect | |
483 | all of the output into buf, and then wait for the normal prompt. */ | |
1b552670 | 484 | |
1265e2d8 | 485 | if (current_monitor->getreg.term) |
431b7d5f | 486 | { |
1265e2d8 SG |
487 | resp_len = expect (current_monitor->getreg.term, buf, sizeof buf); /* get response */ |
488 | ||
489 | if (resp_len <= 0) | |
8f078234 SG |
490 | error ("monitor_fetch_register (%d): excessive response from monitor: %.*s.", |
491 | regno, resp_len, buf); | |
1265e2d8 SG |
492 | |
493 | if (current_monitor->getreg.term_cmd) | |
431b7d5f | 494 | { |
1265e2d8 SG |
495 | SERIAL_WRITE (monitor_desc, current_monitor->getreg.term_cmd, |
496 | strlen (current_monitor->getreg.term_cmd)); | |
497 | expect_prompt (NULL, 0); | |
431b7d5f SS |
498 | } |
499 | } | |
500 | else | |
1265e2d8 SG |
501 | resp_len = expect_prompt (buf, sizeof buf); /* get response */ |
502 | ||
503 | ||
504 | /* If RESP_DELIM is specified, we search for that as a leading delimiter for | |
505 | the register value. Otherwise, we just start searching from the start of | |
506 | the buf. */ | |
507 | ||
508 | if (current_monitor->getreg.resp_delim) | |
431b7d5f | 509 | { |
1265e2d8 SG |
510 | p = strstr (buf, current_monitor->getreg.resp_delim); |
511 | if (!p) | |
8f078234 SG |
512 | error ("monitor_fetch_register (%d): bad response from monitor: %.*s.", |
513 | regno, resp_len, buf); | |
1265e2d8 | 514 | p += strlen (current_monitor->getreg.resp_delim); |
1b552670 | 515 | } |
1265e2d8 SG |
516 | else |
517 | p = buf; | |
51d6a954 | 518 | |
1265e2d8 | 519 | val = strtoul (p, &p1, 16); |
431b7d5f | 520 | |
1265e2d8 SG |
521 | if (val == 0 && p == p1) |
522 | error ("monitor_fetch_register (%d): bad value from monitor: %.*s.", | |
8f078234 | 523 | regno, resp_len, buf); |
51d6a954 | 524 | |
1265e2d8 | 525 | /* supply register stores in target byte order, so swap here */ |
51d6a954 | 526 | |
1265e2d8 SG |
527 | store_unsigned_integer (regbuf, REGISTER_RAW_SIZE (regno), val); |
528 | ||
529 | supply_register (regno, regbuf); | |
51d6a954 RS |
530 | } |
531 | ||
1265e2d8 | 532 | /* Read the remote registers into the block regs. */ |
431b7d5f | 533 | |
51d6a954 | 534 | void |
1265e2d8 SG |
535 | monitor_fetch_registers (regno) |
536 | int regno; | |
51d6a954 | 537 | { |
1265e2d8 | 538 | if (regno >= 0) |
431b7d5f | 539 | { |
1265e2d8 SG |
540 | monitor_fetch_register (regno); |
541 | return; | |
1b552670 | 542 | } |
51d6a954 | 543 | |
1265e2d8 SG |
544 | for (regno = 0; regno < NUM_REGS; regno++) |
545 | monitor_fetch_register (regno); | |
51d6a954 RS |
546 | } |
547 | ||
431b7d5f SS |
548 | /* Store register REGNO, or all if REGNO == 0. Return errno value. */ |
549 | ||
1265e2d8 | 550 | static void |
51d6a954 RS |
551 | monitor_store_register (regno) |
552 | int regno; | |
553 | { | |
554 | char *name; | |
1265e2d8 | 555 | unsigned LONGEST val; |
f1ca4cbc | 556 | |
8f078234 | 557 | name = REGNAMES (regno); |
1265e2d8 SG |
558 | if (!name) |
559 | return; | |
51d6a954 | 560 | |
1265e2d8 | 561 | val = read_register (regno); |
51d6a954 | 562 | |
1265e2d8 SG |
563 | /* send the register deposit command */ |
564 | ||
565 | printf_monitor (current_monitor->setreg.cmd, name, val); | |
566 | ||
567 | /* It's possible that there are actually some monitors out there that will | |
568 | prompt you when you set a register. In that case, you may need to add some | |
569 | code here to deal with TERM and TERM_CMD (see monitor_fetch_register to get | |
570 | an idea of what's needed...) */ | |
571 | ||
572 | expect_prompt (NULL, 0); | |
573 | } | |
574 | ||
575 | /* Store the remote registers. */ | |
576 | ||
577 | void | |
578 | monitor_store_registers (regno) | |
579 | int regno; | |
580 | { | |
581 | if (regno >= 0) | |
431b7d5f | 582 | { |
1265e2d8 SG |
583 | monitor_store_register (regno); |
584 | return; | |
51d6a954 | 585 | } |
431b7d5f | 586 | |
1265e2d8 SG |
587 | for (regno = 0; regno < NUM_REGS; regno++) |
588 | monitor_store_register (regno); | |
51d6a954 RS |
589 | } |
590 | ||
591 | /* Get ready to modify the registers array. On machines which store | |
592 | individual registers, this doesn't need to do anything. On machines | |
593 | which store all the registers in one fell swoop, this makes sure | |
594 | that registers contains all the registers from the program being | |
595 | debugged. */ | |
596 | ||
597 | void | |
598 | monitor_prepare_to_store () | |
599 | { | |
600 | /* Do nothing, since we can store individual regs */ | |
601 | } | |
602 | ||
603 | void | |
604 | monitor_files_info () | |
605 | { | |
1265e2d8 | 606 | printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name, baud_rate); |
51d6a954 RS |
607 | } |
608 | ||
1265e2d8 SG |
609 | static int |
610 | monitor_write_memory (memaddr, myaddr, len) | |
51d6a954 RS |
611 | CORE_ADDR memaddr; |
612 | unsigned char *myaddr; | |
613 | int len; | |
614 | { | |
1265e2d8 | 615 | /* send the memory deposit command */ |
1b552670 | 616 | |
1265e2d8 | 617 | printf_monitor (current_monitor->setmem.cmd, memaddr, *myaddr); |
1b552670 | 618 | |
1265e2d8 SG |
619 | /* It's possible that there are actually some monitors out there that will |
620 | prompt you when you deposit to memory. In that case, you may need to add | |
621 | some code here to deal with TERM and TERM_CMD (see monitor_read_memory to | |
622 | get an idea of what's needed...) */ | |
623 | ||
624 | expect_prompt (NULL, 0); | |
625 | ||
626 | return 1; | |
51d6a954 RS |
627 | } |
628 | ||
1265e2d8 SG |
629 | /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's memory |
630 | at MEMADDR. Returns length moved. Currently, we only do one byte at a | |
631 | time. */ | |
431b7d5f | 632 | |
1265e2d8 SG |
633 | static int |
634 | monitor_read_memory (memaddr, myaddr, len) | |
51d6a954 RS |
635 | CORE_ADDR memaddr; |
636 | char *myaddr; | |
637 | int len; | |
638 | { | |
1265e2d8 SG |
639 | unsigned LONGEST val; |
640 | unsigned char regbuf[MAX_REGISTER_RAW_SIZE]; | |
641 | char buf[200]; | |
642 | char *p, *p1; | |
643 | char *name; | |
644 | int resp_len; | |
645 | ||
646 | /* send the memory examine command */ | |
647 | ||
648 | printf_monitor (current_monitor->getmem.cmd, memaddr); | |
649 | ||
650 | /* If TERM is present, we wait for that to show up. Also, (if TERM is | |
651 | present), we will send TERM_CMD if that is present. In any case, we collect | |
652 | all of the output into buf, and then wait for the normal prompt. */ | |
653 | ||
654 | if (current_monitor->getmem.term) | |
431b7d5f | 655 | { |
1265e2d8 SG |
656 | resp_len = expect (current_monitor->getmem.term, buf, sizeof buf); /* get response */ |
657 | ||
658 | if (resp_len <= 0) | |
8f078234 SG |
659 | error ("monitor_read_memory (0x%x): excessive response from monitor: %.*s.", |
660 | memaddr, resp_len, buf); | |
1265e2d8 SG |
661 | |
662 | if (current_monitor->getmem.term_cmd) | |
431b7d5f | 663 | { |
1265e2d8 SG |
664 | SERIAL_WRITE (monitor_desc, current_monitor->getmem.term_cmd, |
665 | strlen (current_monitor->getmem.term_cmd)); | |
666 | expect_prompt (NULL, 0); | |
1b552670 | 667 | } |
51d6a954 | 668 | } |
1265e2d8 SG |
669 | else |
670 | resp_len = expect_prompt (buf, sizeof buf); /* get response */ | |
671 | ||
672 | /* If RESP_DELIM is specified, we search for that as a leading delimiter for | |
673 | the register value. Otherwise, we just start searching from the start of | |
674 | the buf. */ | |
675 | ||
676 | if (current_monitor->getmem.resp_delim) | |
677 | { | |
678 | p = strstr (buf, current_monitor->getmem.resp_delim); | |
679 | if (!p) | |
8f078234 SG |
680 | error ("monitor_read_memory (0x%x): bad response from monitor: %.*s.", |
681 | memaddr, resp_len, buf); | |
1265e2d8 SG |
682 | p += strlen (current_monitor->getmem.resp_delim); |
683 | } | |
684 | else | |
685 | p = buf; | |
686 | ||
687 | val = strtoul (p, &p1, 16); | |
688 | ||
689 | if (val == 0 && p == p1) | |
8f078234 | 690 | error ("monitor_read_memory (0x%x): bad value from monitor: %.*s.", memaddr, |
1265e2d8 SG |
691 | resp_len, buf); |
692 | ||
693 | *myaddr = val; | |
694 | ||
695 | return 1; /* Got 1 byte */ | |
51d6a954 RS |
696 | } |
697 | ||
698 | /* FIXME-someday! merge these two. */ | |
431b7d5f | 699 | |
51d6a954 | 700 | int |
1265e2d8 | 701 | monitor_xfer_memory (memaddr, myaddr, len, write, target) |
51d6a954 RS |
702 | CORE_ADDR memaddr; |
703 | char *myaddr; | |
704 | int len; | |
705 | int write; | |
706 | struct target_ops *target; /* ignored */ | |
707 | { | |
708 | if (write) | |
1265e2d8 | 709 | return monitor_write_memory (memaddr, myaddr, len); |
51d6a954 | 710 | else |
1265e2d8 | 711 | return monitor_read_memory (memaddr, myaddr, len); |
51d6a954 RS |
712 | } |
713 | ||
714 | void | |
715 | monitor_kill (args, from_tty) | |
716 | char *args; | |
717 | int from_tty; | |
718 | { | |
719 | return; /* ignore attempts to kill target system */ | |
720 | } | |
721 | ||
722 | /* Clean up when a program exits. | |
723 | The program actually lives on in the remote processor's RAM, and may be | |
724 | run again without a download. Don't leave it full of breakpoint | |
725 | instructions. */ | |
726 | ||
727 | void | |
728 | monitor_mourn_inferior () | |
729 | { | |
8f078234 | 730 | unpush_target (targ_ops); |
51d6a954 RS |
731 | generic_mourn_inferior (); /* Do all the proper things now */ |
732 | } | |
733 | ||
1265e2d8 | 734 | #define NUM_MONITOR_BREAKPOINTS 8 |
51d6a954 | 735 | |
1265e2d8 | 736 | static CORE_ADDR breakaddr[NUM_MONITOR_BREAKPOINTS] = {0}; |
51d6a954 | 737 | |
431b7d5f SS |
738 | /* Tell the monitor to add a breakpoint. */ |
739 | ||
51d6a954 RS |
740 | int |
741 | monitor_insert_breakpoint (addr, shadow) | |
742 | CORE_ADDR addr; | |
743 | char *shadow; | |
744 | { | |
745 | int i; | |
1265e2d8 | 746 | static unsigned char break_insn[] = BREAKPOINT; |
51d6a954 | 747 | |
1265e2d8 | 748 | for (i = 0; i < NUM_MONITOR_BREAKPOINTS; i++) |
431b7d5f SS |
749 | { |
750 | if (breakaddr[i] == 0) | |
751 | { | |
752 | breakaddr[i] = addr; | |
1265e2d8 | 753 | monitor_read_memory (addr, shadow, sizeof (break_insn)); |
431b7d5f | 754 | printf_monitor (SET_BREAK_CMD, addr); |
1265e2d8 | 755 | expect_prompt (NULL, 0); |
431b7d5f SS |
756 | return 0; |
757 | } | |
f1ca4cbc | 758 | } |
f1ca4cbc | 759 | |
1265e2d8 | 760 | error ("Too many breakpoints (> %d) for monitor.", NUM_MONITOR_BREAKPOINTS); |
51d6a954 RS |
761 | } |
762 | ||
431b7d5f SS |
763 | /* Tell the monitor to remove a breakpoint. */ |
764 | ||
51d6a954 RS |
765 | int |
766 | monitor_remove_breakpoint (addr, shadow) | |
767 | CORE_ADDR addr; | |
768 | char *shadow; | |
769 | { | |
770 | int i; | |
771 | ||
1265e2d8 | 772 | for (i = 0; i < NUM_MONITOR_BREAKPOINTS; i++) |
431b7d5f SS |
773 | { |
774 | if (breakaddr[i] == addr) | |
775 | { | |
776 | breakaddr[i] = 0; | |
777 | /* some monitors remove breakpoints based on the address */ | |
778 | if (CLR_BREAK_ADDR) | |
779 | printf_monitor(CLR_BREAK_CMD, addr); | |
780 | else | |
781 | printf_monitor(CLR_BREAK_CMD, i); | |
1265e2d8 | 782 | expect_prompt (NULL, 0); |
431b7d5f SS |
783 | return 0; |
784 | } | |
7804e5bc | 785 | } |
1265e2d8 | 786 | fprintf_unfiltered (stderr, "Can't find breakpoint associated with 0x%x\n", addr); |
51d6a954 RS |
787 | return 1; |
788 | } | |
789 | ||
21ed3dcd RS |
790 | /* monitor_load -- load a file. This file determines which of the |
791 | * supported formats to use. The current types are: | |
792 | * FIXME: not all types supported yet. | |
06b8f5e4 RS |
793 | * default - reads any file using bfd and writes it to memory. This |
794 | * is really slow. | |
21ed3dcd RS |
795 | * srec - reads binary file using bfd and writes it as an |
796 | * ascii srecord. | |
797 | * xmodem-bin - reads a binary file using bfd, and downloads it | |
798 | * using xmodem protocol. | |
799 | * xmodem-srec - reads a binary file using bfd, and after converting | |
800 | * it downloads it as an srecord using xmodem protocol. | |
801 | * ascii-srec - reads a ascii srecord file and downloads it | |
802 | * without a change. | |
803 | * ascii-xmodem - reads a ascii file and downloads using xmodem | |
804 | * protocol. | |
805 | */ | |
431b7d5f | 806 | |
21ed3dcd RS |
807 | void |
808 | monitor_load (file, fromtty) | |
809 | char *file; | |
810 | int fromtty; | |
811 | { | |
431b7d5f SS |
812 | /* default, load a binary */ |
813 | if (STREQ (loadtype_str, "default")) | |
814 | { | |
1265e2d8 | 815 | error ("default load type not supported."); |
431b7d5f | 816 | } |
62b32254 RS |
817 | |
818 | /* load an srecord by converting */ | |
431b7d5f SS |
819 | if ((STREQ (loadtype_str, "srec")) && STREQ (loadproto_str, "xmodem")) |
820 | { | |
821 | monitor_load_srec (file, XMODEM); | |
822 | return; | |
823 | } | |
21ed3dcd | 824 | |
431b7d5f SS |
825 | /* load an srecord by converting */ |
826 | if (STREQ (loadtype_str, "srec")) | |
827 | { | |
828 | monitor_load_srec (file, 0); /* if from a binary */ | |
829 | return; | |
830 | } | |
21ed3dcd | 831 | |
431b7d5f SS |
832 | /* load an srecord by converting */ |
833 | if (STREQ (loadtype_str, "none")) | |
834 | { | |
835 | error ("Unimplemented"); | |
836 | return; | |
837 | } | |
3f9ef4ff | 838 | |
431b7d5f SS |
839 | /* load an srecord file */ |
840 | if (STREQ (loadproto_str, "none")) | |
841 | { | |
842 | monitor_load_ascii_srec (file, fromtty); /* if from a binary */ | |
843 | return; | |
844 | } | |
21ed3dcd | 845 | |
431b7d5f SS |
846 | if (STREQ (loadproto_str, "xmodem")) |
847 | { | |
848 | monitor_load_srec (file, XMODEM); | |
849 | return; | |
850 | } | |
21ed3dcd RS |
851 | } |
852 | ||
431b7d5f SS |
853 | /* Download an ASCII srecord file. */ |
854 | ||
51d6a954 | 855 | #define DOWNLOAD_LINE_SIZE 100 |
431b7d5f SS |
856 | |
857 | static void | |
21ed3dcd RS |
858 | monitor_load_ascii_srec (file, fromtty) |
859 | char *file; | |
860 | int fromtty; | |
51d6a954 RS |
861 | { |
862 | FILE *download; | |
863 | char buf[DOWNLOAD_LINE_SIZE]; | |
864 | int i, bytes_read; | |
865 | ||
21ed3dcd | 866 | download = fopen (file, "r"); |
431b7d5f SS |
867 | if (download == NULL) |
868 | { | |
869 | error ("%s does not exist", file); | |
870 | return; | |
871 | } | |
51d6a954 RS |
872 | |
873 | printf_monitor (LOAD_CMD); | |
431b7d5f SS |
874 | sleep (1); |
875 | while (!feof (download)) | |
876 | { | |
877 | bytes_read = fread (buf, sizeof (char), DOWNLOAD_LINE_SIZE, download); | |
878 | if (hashmark) | |
879 | { | |
1265e2d8 SG |
880 | putchar_unfiltered ('.'); |
881 | gdb_flush (gdb_stdout); | |
431b7d5f SS |
882 | } |
883 | if (SERIAL_WRITE (monitor_desc, buf, bytes_read)) | |
884 | { | |
1265e2d8 | 885 | fprintf_unfiltered (stderr, "SERIAL_WRITE failed: (while downloading) %s\n", |
431b7d5f SS |
886 | safe_strerror (errno)); |
887 | break; | |
888 | } | |
889 | i = 0; | |
890 | while (i++ <=200) {} ; /* Ugly HACK, probably needs flow control */ | |
891 | if (bytes_read < DOWNLOAD_LINE_SIZE) | |
892 | { | |
893 | if (!feof (download)) | |
894 | error ("Only read %d bytes\n", bytes_read); | |
895 | break; | |
896 | } | |
51d6a954 | 897 | } |
21ed3dcd | 898 | |
431b7d5f | 899 | if (hashmark) |
1265e2d8 | 900 | putchar_unfiltered ('\n'); |
431b7d5f | 901 | |
51d6a954 RS |
902 | if (!feof (download)) |
903 | error ("Never got EOF while downloading"); | |
1265e2d8 | 904 | expect_prompt (NULL, 0); |
51d6a954 RS |
905 | fclose (download); |
906 | } | |
907 | ||
431b7d5f SS |
908 | /* Put a command string, in args, out to MONITOR. Output from MONITOR |
909 | is placed on the users terminal until the prompt is seen. FIXME: We | |
910 | read the characters ourseleves here cause of a nasty echo. */ | |
911 | ||
8f078234 | 912 | static void |
51d6a954 | 913 | monitor_command (args, fromtty) |
431b7d5f SS |
914 | char *args; |
915 | int fromtty; | |
51d6a954 | 916 | { |
7804e5bc | 917 | char *p; |
431b7d5f | 918 | |
7804e5bc RS |
919 | p = PROMPT; |
920 | ||
51d6a954 | 921 | if (monitor_desc == NULL) |
431b7d5f | 922 | error ("monitor target not open."); |
7804e5bc | 923 | |
431b7d5f SS |
924 | /* Send the command. Note that if no args were supplied, then we're |
925 | just sending the monitor a newline, which is sometimes useful. */ | |
926 | ||
927 | printf_monitor ("%s\n", (args ? args : "")); | |
7804e5bc | 928 | |
1265e2d8 | 929 | expect_prompt (NULL, 0); |
51d6a954 RS |
930 | } |
931 | ||
431b7d5f SS |
932 | /* Download a binary file by converting it to srecords. This |
933 | will also use xmodem to download the resulting file. | |
934 | ||
935 | A download goes like this when using xmodem: | |
936 | Receiver: Sender | |
937 | NAK ----------> | |
938 | <-------- (packet) [SOH|1|1|data|SUM] | |
939 | ACK ----------> | |
940 | <-------- (packet) [SOH|2|2|data|SUM] | |
941 | ACK ----------> | |
942 | <-------- EOT | |
943 | ACK ----------> | |
944 | ||
945 | ACK = 0x06 | |
946 | NAK = 0x15 | |
947 | EOT = 0x04 | |
948 | */ | |
949 | ||
21ed3dcd | 950 | static void |
06b8f5e4 | 951 | monitor_load_srec (args, protocol) |
21ed3dcd | 952 | char *args; |
06b8f5e4 | 953 | int protocol; |
21ed3dcd RS |
954 | { |
955 | bfd *abfd; | |
956 | asection *s; | |
431b7d5f | 957 | char *buffer, srec[1024]; |
06b8f5e4 RS |
958 | char packet[XMODEM_PACKETSIZE]; |
959 | int i; | |
960 | int retries; | |
431b7d5f SS |
961 | int type = 0; /* default to a type 0, header record */ |
962 | int srec_frame = 57; /* FIXME: this must be 57 There is 12 bytes | |
963 | of header, and 2 bytes of checksum at the end. | |
964 | The problem is an xmodem packet holds exactly | |
965 | 128 bytes. */ | |
21ed3dcd RS |
966 | |
967 | abfd = bfd_openr (args, 0); | |
431b7d5f SS |
968 | if (!abfd) |
969 | { | |
970 | printf_filtered ("Unable to open file %s\n", args); | |
971 | return; | |
972 | } | |
21ed3dcd | 973 | |
431b7d5f SS |
974 | if (bfd_check_format (abfd, bfd_object) == 0) |
975 | { | |
976 | printf_filtered ("File is not an object file\n"); | |
977 | return; | |
978 | } | |
21ed3dcd | 979 | |
06b8f5e4 | 980 | printf_monitor (LOAD_CMD); /* tell the monitor to load */ |
82fc3432 | 981 | sleep (3); |
431b7d5f SS |
982 | /* get the NAK from the target */ |
983 | if (protocol == XMODEM) | |
984 | { | |
1265e2d8 | 985 | if (!GETNAK) |
431b7d5f SS |
986 | { |
987 | printf_monitor ("%c", EOT); | |
431b7d5f SS |
988 | error ("Never got the NAK to start loading"); |
989 | } | |
06b8f5e4 | 990 | } |
06b8f5e4 | 991 | |
21ed3dcd | 992 | s = abfd->sections; |
431b7d5f SS |
993 | while (s != (asection *) NULL) |
994 | { | |
995 | if (s->flags & SEC_LOAD) | |
996 | { | |
997 | buffer = xmalloc (srec_frame); | |
998 | ||
999 | printf_filtered ("%s\t: 0x%4x .. 0x%4x ", | |
1000 | s->name, s->vma, s->vma + s->_raw_size); | |
1265e2d8 | 1001 | gdb_flush (gdb_stdout); |
431b7d5f SS |
1002 | for (i = 0; i < s->_raw_size; i += srec_frame) |
1003 | { | |
1004 | if (srec_frame > s->_raw_size - i) | |
1005 | srec_frame = s->_raw_size - i; | |
21ed3dcd | 1006 | |
431b7d5f SS |
1007 | bfd_get_section_contents (abfd, s, buffer, i, srec_frame); |
1008 | monitor_make_srec (srec, type, s->vma + i, buffer, srec_frame); | |
1009 | /* send a packet using xmodem */ | |
1010 | if (protocol == XMODEM) | |
1011 | { | |
1012 | make_xmodem_packet (packet, srec, XMODEM_DATASIZE); | |
1013 | write_monitor (packet, XMODEM_PACKETSIZE+1); | |
1014 | retries = 0; | |
1015 | while (retries++ <= 3) | |
1016 | { | |
1017 | /* Resend packet */ | |
1018 | if (GETNAK) | |
1019 | { | |
431b7d5f SS |
1020 | sleep (1); |
1021 | /* send it again */ | |
1022 | write_monitor (packet, XMODEM_PACKETSIZE+1); | |
1023 | if (GETACK) /* ACKnowledged, get next data chunk */ | |
1024 | break; | |
1025 | } | |
1026 | else | |
1027 | { /* assume we got an ACK */ | |
1028 | if (hashmark) | |
1029 | { | |
1265e2d8 | 1030 | putchar_unfiltered ('#'); |
431b7d5f SS |
1031 | gdb_flush (gdb_stdout); |
1032 | } | |
431b7d5f SS |
1033 | break; |
1034 | } | |
1035 | } | |
1036 | if (retries >= 4) | |
1037 | { /* too many tries, must be hosed */ | |
1038 | printf_monitor ("%c", EOT); | |
1039 | error ("Never got a ACK after sending an xmodem packet"); | |
1040 | } | |
1041 | } | |
1042 | else | |
1043 | { /* no protocols at all */ | |
1044 | printf_monitor ("%s\n", srec); | |
1045 | } | |
1046 | if (hashmark) | |
1047 | { | |
1265e2d8 | 1048 | putchar_unfiltered ('#'); |
431b7d5f SS |
1049 | gdb_flush (gdb_stdout); |
1050 | } | |
1051 | type = 3; /* switch to a 4 byte address record */ | |
1265e2d8 | 1052 | gdb_flush (gdb_stdout); |
06b8f5e4 | 1053 | } |
431b7d5f | 1054 | free (buffer); |
06b8f5e4 | 1055 | } |
431b7d5f | 1056 | s = s->next; |
21ed3dcd | 1057 | } |
1265e2d8 | 1058 | putchar_unfiltered ('\n'); |
06b8f5e4 | 1059 | |
431b7d5f SS |
1060 | /* Write a type 7 terminator record. no data for a type 7, and there |
1061 | is no data, so len is 0. */ | |
1062 | ||
1063 | if (protocol == XMODEM) | |
1064 | { | |
1065 | /* send a packet using xmodem */ | |
1066 | monitor_make_srec (srec, 7, abfd->start_address, "", 0); | |
1067 | make_xmodem_packet (packet, srec, XMODEM_DATASIZE); | |
1068 | write_monitor (packet, XMODEM_PACKETSIZE+1); | |
1069 | } | |
1070 | else | |
1071 | { | |
1072 | monitor_make_srec (srec, 7, abfd->start_address, "", 0); | |
1073 | printf_monitor ("%s\n", srec); | |
1074 | } | |
1075 | if (protocol == XMODEM) | |
1076 | { | |
1077 | printf_monitor ("%c", EOT); | |
1078 | if (!GETACK) | |
1079 | error ("Never got ACK after sending EOT"); | |
1080 | } | |
06b8f5e4 RS |
1081 | |
1082 | if (hashmark) | |
1265e2d8 | 1083 | putchar_unfiltered ('\n'); |
06b8f5e4 | 1084 | |
1265e2d8 | 1085 | expect_prompt (NULL, 0); |
f17aed8b SG |
1086 | |
1087 | /* Finally, make the PC point at the start address */ | |
1088 | ||
1089 | write_register (PC_REGNUM, bfd_get_start_address (abfd)); | |
21ed3dcd RS |
1090 | } |
1091 | ||
431b7d5f SS |
1092 | /* Get an ACK or a NAK from the target. returns 1 (true) or 0 (false) |
1093 | This is for xmodem. ANy string starting with "***" is an error | |
1094 | message from the target. Here's a few from the WinBond w89k | |
1095 | "Cougar" PA board: | |
1096 | *** Too many errors found. | |
1097 | *** Bad command | |
1098 | *** Command syntax error | |
1099 | */ | |
1100 | ||
8f078234 | 1101 | static int |
06b8f5e4 RS |
1102 | getacknak (byte) |
1103 | int byte; | |
1104 | { | |
1105 | char character; | |
1106 | int i; | |
1107 | ||
1108 | i = 0; | |
431b7d5f SS |
1109 | while (i++ < 60) |
1110 | { | |
1111 | character = (char) readchar (0); | |
1112 | if ((character == 0xfffffffe) || (character == 0x7f)) | |
1113 | { /* empty uart */ | |
431b7d5f SS |
1114 | sleep (1); |
1115 | continue; | |
1116 | } | |
1117 | if (character == CANCEL) | |
1118 | { /* target aborted load */ | |
1265e2d8 | 1119 | expect_prompt (NULL, 0); |
431b7d5f SS |
1120 | error ("Got a CANCEL from the target."); |
1121 | } | |
1122 | if (character == '*') | |
1123 | { /* look for missed error message */ | |
1265e2d8 | 1124 | expect_prompt (NULL, 0); |
431b7d5f SS |
1125 | error ("Got an error message from the target"); |
1126 | } | |
431b7d5f SS |
1127 | if (character == byte) /* got what we wanted */ |
1128 | return 1; | |
1129 | if (character == ((byte == ACK) ? NAK : ACK)) | |
1265e2d8 | 1130 | return 0; |
431b7d5f | 1131 | sleep (1); |
06b8f5e4 | 1132 | } |
431b7d5f | 1133 | |
06b8f5e4 RS |
1134 | return 0; |
1135 | } | |
21ed3dcd | 1136 | |
06b8f5e4 RS |
1137 | /* |
1138 | * monitor_make_srec -- make an srecord. This writes each line, one at a | |
1139 | * time, each with it's own header and trailer line. | |
1140 | * An srecord looks like this: | |
1141 | * | |
1142 | * byte count-+ address | |
1143 | * start ---+ | | data +- checksum | |
1144 | * | | | | | |
1145 | * S01000006F6B692D746573742E73726563E4 | |
1146 | * S315000448600000000000000000FC00005900000000E9 | |
1147 | * S31A0004000023C1400037DE00F023604000377B009020825000348D | |
1148 | * S30B0004485A0000000000004E | |
1149 | * S70500040000F6 | |
1150 | * | |
1151 | * S<type><length><address><data><checksum> | |
1152 | * | |
1153 | * Where | |
1154 | * - length | |
1155 | * is the number of bytes following upto the checksum. Note that | |
1156 | * this is not the number of chars following, since it takes two | |
1157 | * chars to represent a byte. | |
1158 | * - type | |
1159 | * is one of: | |
1160 | * 0) header record | |
1161 | * 1) two byte address data record | |
1162 | * 2) three byte address data record | |
1163 | * 3) four byte address data record | |
1164 | * 7) four byte address termination record | |
1165 | * 8) three byte address termination record | |
1166 | * 9) two byte address termination record | |
1167 | * | |
1168 | * - address | |
1169 | * is the start address of the data following, or in the case of | |
1170 | * a termination record, the start address of the image | |
1171 | * - data | |
1172 | * is the data. | |
1173 | * - checksum | |
1174 | * is the sum of all the raw byte data in the record, from the length | |
1175 | * upwards, modulo 256 and subtracted from 255. | |
1176 | */ | |
431b7d5f SS |
1177 | |
1178 | static int | |
06b8f5e4 RS |
1179 | monitor_make_srec (buffer, type, memaddr, myaddr, len) |
1180 | char *buffer; | |
1181 | int type; | |
21ed3dcd RS |
1182 | CORE_ADDR memaddr; |
1183 | unsigned char *myaddr; | |
1184 | int len; | |
1185 | { | |
21ed3dcd | 1186 | int checksum; |
06b8f5e4 RS |
1187 | int i; |
1188 | char *buf; | |
21ed3dcd | 1189 | |
06b8f5e4 | 1190 | buf = buffer; |
1265e2d8 | 1191 | |
06b8f5e4 RS |
1192 | checksum = 0; |
1193 | ||
431b7d5f SS |
1194 | /* Create the header for the srec. 4 is the number of bytes in the address, |
1195 | and 1 is the number of bytes in the count. */ | |
1196 | ||
06b8f5e4 RS |
1197 | if (type == 0) /* FIXME: type 0 is optional */ |
1198 | type = 3; /* so use data as it works */ | |
1199 | sprintf (buf, "S%d%02X%08X", type, len + 4 + 1, memaddr); | |
1200 | buf += 12; | |
1201 | ||
1202 | checksum += (len + 4 + 1 /* calculate the checksum */ | |
1203 | + (memaddr & 0xff) | |
1204 | + ((memaddr >> 8) & 0xff) | |
1205 | + ((memaddr >> 16) & 0xff) | |
1206 | + ((memaddr >> 24) & 0xff)); | |
1207 | ||
431b7d5f SS |
1208 | /* build the srecord */ |
1209 | for (i = 0; i < len; i++) | |
1210 | { | |
1211 | sprintf (buf, "%02X", myaddr[i]); | |
1212 | checksum += myaddr[i]; | |
1213 | buf += 2; | |
1214 | } | |
21ed3dcd | 1215 | |
06b8f5e4 | 1216 | sprintf(buf, "%02X", ~checksum & 0xff); /* add the checksum */ |
06b8f5e4 | 1217 | |
431b7d5f | 1218 | return 0; |
06b8f5e4 | 1219 | } |
21ed3dcd | 1220 | |
431b7d5f | 1221 | /* Take 128 bytes of data and make a packet out of it. |
06b8f5e4 RS |
1222 | * |
1223 | * Each packet looks like this: | |
1224 | * +-----+-------+-------+------+-----+ | |
1225 | * | SOH | Seq1. | Seq2. | data | SUM | | |
1226 | * +-----+-------+-------+------+-----+ | |
1227 | * SOH = 0x01 | |
1228 | * Seq1 = The sequence number. | |
1229 | * Seq2 = The complement of the sequence number. | |
1230 | * Data = A 128 bytes of data. | |
1231 | * SUM = Add the contents of the 128 bytes and use the low-order | |
1232 | * 8 bits of the result. | |
1233 | */ | |
431b7d5f | 1234 | |
1b552670 | 1235 | static void |
06b8f5e4 | 1236 | make_xmodem_packet (packet, data, len) |
431b7d5f | 1237 | unsigned char *packet; |
06b8f5e4 RS |
1238 | unsigned char *data; |
1239 | int len; | |
1240 | { | |
1241 | static int sequence = 1; | |
1242 | int i, sum; | |
1243 | unsigned char *buf; | |
1244 | ||
1245 | buf = data; | |
1246 | /* build the packet header */ | |
1247 | packet[0] = SOH; | |
1248 | packet[1] = sequence; | |
1249 | packet[2] = 255 - sequence; | |
1250 | sequence++; | |
21ed3dcd | 1251 | #if 0 |
431b7d5f | 1252 | packet[2] = ~sequence++; /* the complement is the sequence checksum */ |
21ed3dcd | 1253 | #endif |
06b8f5e4 | 1254 | |
431b7d5f | 1255 | sum = 0; /* calculate the data checksum */ |
06b8f5e4 RS |
1256 | for (i = 3; i <= len + 2; i++) { |
1257 | packet[i] = *buf; | |
1258 | sum += *buf; | |
1259 | buf++; | |
1260 | } | |
1261 | ||
431b7d5f SS |
1262 | /* add padding for the rest of the packet */ |
1263 | for (i = len+1 ; i <= XMODEM_DATASIZE ; i++) | |
06b8f5e4 | 1264 | packet[i] = '0'; |
06b8f5e4 RS |
1265 | |
1266 | packet[XMODEM_PACKETSIZE] = sum & 0xff; /* add the checksum */ | |
06b8f5e4 RS |
1267 | } |
1268 | ||
431b7d5f SS |
1269 | /* Print the packet as a debug check. */ |
1270 | ||
1b552670 | 1271 | static void |
431b7d5f SS |
1272 | print_xmodem_packet (packet) |
1273 | char *packet; | |
06b8f5e4 RS |
1274 | { |
1275 | int i; | |
1276 | static int lastseq; | |
1277 | int sum; | |
1278 | ||
1279 | /* take apart the packet header the packet header */ | |
431b7d5f | 1280 | if (packet[0] == SOH) |
1265e2d8 | 1281 | printf_unfiltered ("SOH"); |
431b7d5f | 1282 | else |
06b8f5e4 | 1283 | error ("xmodem: SOH is wrong"); |
06b8f5e4 RS |
1284 | |
1285 | /* check the sequence */ | |
431b7d5f SS |
1286 | if (packet[1] != 0) |
1287 | { | |
1288 | lastseq = packet[1]; | |
1289 | if (packet[2] != ~lastseq) | |
1290 | error ("xmodem: Sequence checksum is wrong"); | |
1291 | else | |
1292 | printf_filtered (" %d %d", lastseq, ~lastseq); | |
1293 | } | |
06b8f5e4 RS |
1294 | |
1295 | /* check the data checksum */ | |
1296 | sum = 0; | |
431b7d5f | 1297 | for (i = 3; i <= XMODEM_DATASIZE; i++) |
06b8f5e4 | 1298 | sum += packet[i]; |
06b8f5e4 RS |
1299 | |
1300 | /* ignore the data */ | |
21ed3dcd | 1301 | #if 0 |
1265e2d8 | 1302 | printf_unfiltered (" [128 bytes of data] %d\n", sum & 0xff); |
21ed3dcd | 1303 | #endif |
06b8f5e4 | 1304 | printf_filtered (" [%s] %d\n", packet, sum & 0xff); |
21ed3dcd | 1305 | |
431b7d5f | 1306 | if ((packet[XMODEM_PACKETSIZE] & 0xff) != (sum & 0xff)) |
1265e2d8 SG |
1307 | printf_unfiltered ("xmodem: data checksum wrong, got a %d", |
1308 | packet[XMODEM_PACKETSIZE] & 0xff); | |
1b552670 | 1309 | |
1265e2d8 | 1310 | putchar_unfiltered ('\n'); |
1b552670 RS |
1311 | } |
1312 | ||
1313 | /* Convert hex digit A to a number. */ | |
431b7d5f | 1314 | |
1b552670 RS |
1315 | static int |
1316 | from_hex (a) | |
1317 | int a; | |
1318 | { | |
1b552670 RS |
1319 | if (a >= '0' && a <= '9') |
1320 | return a - '0'; | |
1321 | if (a >= 'a' && a <= 'f') | |
1322 | return a - 'a' + 10; | |
1323 | if (a >= 'A' && a <= 'F') | |
1324 | return a - 'A' + 10; | |
1b552670 | 1325 | |
1265e2d8 | 1326 | error ("Reply contains invalid hex digit 0x%x", a); |
1b552670 RS |
1327 | } |
1328 | ||
431b7d5f SS |
1329 | /* Define additional commands that are usually only used by monitors. */ |
1330 | ||
51d6a954 RS |
1331 | void |
1332 | _initialize_remote_monitors () | |
1333 | { | |
51d6a954 RS |
1334 | add_show_from_set (add_set_cmd ("hash", no_class, var_boolean, |
1335 | (char *)&hashmark, | |
1336 | "Set display of activity while downloading a file.\n\ | |
1265e2d8 | 1337 | When enabled, a hashmark \'#\' is displayed.", |
51d6a954 RS |
1338 | &setlist), |
1339 | &showlist); | |
1340 | ||
51d6a954 RS |
1341 | add_com ("monitor", class_obscure, monitor_command, |
1342 | "Send a command to the debug monitor."); | |
1343 | } |