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