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