2004-06-24 Andrew Cagney <cagney@gnu.org>
[deliverable/binutils-gdb.git] / gdb / rdi-share / ardi.c
1 /*
2 * Copyright (C) 1995 Advanced RISC Machines Limited. All rights reserved.
3 *
4 * This software may be freely used, copied, modified, and distributed
5 * provided that the above copyright notice is preserved in all copies of the
6 * software.
7 */
8
9 /*
10 * ARDI.c
11 * Angel Remote Debug Interface
12 *
13 *
14 * $Revision$
15 * $Date$
16 *
17 * This file is based on /plg/pisd/rdi.c, but instead of using RDP it uses
18 * ADP messages.
19 */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #define uint HIDE_HPs_uint
26 #include <signal.h>
27 #undef uint
28
29
30 #include "angel_endian.h"
31 #include "ardi.h"
32 #include "buffers.h"
33 #include "channels.h"
34 #include "hostchan.h"
35 #include "host.h"
36 #include "angel_bytesex.h"
37 #include "dbg_cp.h"
38 #include "adp.h"
39 #include "hsys.h"
40 #include "logging.h"
41 #include "msgbuild.h"
42 #include "rxtx.h"
43 #include "devsw.h"
44 #include "params.h"
45
46 #ifdef COMPILING_ON_WINDOWS
47 # define IGNORE(x) (x = x) /* must go after #includes to work on Windows */
48 #endif
49 #define NOT(x) (!(x))
50
51 #define ADP_INITIAL_TIMEOUT_PERIOD 5
52
53 static volatile int executing;
54 static int rdi_log = 0 ; /* debugging ? */
55
56 /* we need a starting point for our first buffers, this is a safe one */
57 int Armsd_BufferSize = ADP_BUFFER_MIN_SIZE;
58 int Armsd_LongBufSize = ADP_BUFFER_MIN_SIZE;
59
60 #ifdef WIN32
61 extern int interrupted;
62 extern int swiprocessing;
63 #endif
64
65 static char dummycline = 0;
66 char *ardi_commandline = &dummycline ; /* exported in ardi.h */
67
68 extern unsigned int heartbeat_enabled;
69
70 static unsigned char *cpwords[16];
71
72 typedef struct stoppedProcListElement {
73 struct stoppedProcListElement *next;
74 angel_RDI_TargetStoppedProc *fn;
75 void *arg;
76 } stoppedProcListElement;
77
78 static stoppedProcListElement *stopped_proc_list=NULL;
79
80 const struct Dbg_HostosInterface *angel_hostif;
81 static hsys_state *hstate;
82
83 static void angel_DebugPrint(const char *format, ...)
84 { va_list ap;
85 va_start(ap, format);
86 angel_hostif->dbgprint(angel_hostif->dbgarg, format, ap);
87 va_end(ap);
88 }
89
90 #ifdef RDI_VERBOSE
91 #define TracePrint(s) \
92 if (rdi_log & 2) angel_DebugPrint("\n"); \
93 if (rdi_log & 1) angel_DebugPrint s
94 #else
95 #define TracePrint(s)
96 #endif
97
98 typedef struct receive_dbgmsg_state {
99 volatile int received;
100 Packet *packet;
101 } receive_dbgmsg_state;
102
103 static receive_dbgmsg_state dbgmsg_state;
104
105 static void receive_debug_packet(Packet *packet, void *stateptr)
106 {
107 receive_dbgmsg_state *state = stateptr;
108
109 state->packet = packet;
110 state->received = 1;
111 }
112
113 static int register_debug_message_handler(void)
114 {
115 int err;
116 dbgmsg_state.received = 0;
117
118 err = Adp_ChannelRegisterRead(CI_HADP, receive_debug_packet, &dbgmsg_state);
119 #ifdef DEBUG
120 if (err!=adp_ok) angel_DebugPrint("register_debug_message_handler failed %i\n", err);
121 #endif
122 return err;
123 }
124
125
126 static int wait_for_debug_message(int *rcode, int *debugID,
127 int *OSinfo1, int *OSinfo2,
128 int *status, Packet **packet)
129 {
130 unsigned int reason;
131
132 #ifdef DEBUG
133 angel_DebugPrint("wait_for_debug_message waiting for %X\n", *rcode);
134 #endif
135
136 for ( ; dbgmsg_state.received == 0 ; )
137 Adp_AsynchronousProcessing(async_block_on_read);
138
139 #ifdef DEBUG
140 angel_DebugPrint("wait_for_debug_message got packet\n");
141 #endif
142
143 *packet = dbgmsg_state.packet;
144
145 Adp_ChannelRegisterRead(CI_HADP, NULL, NULL);
146
147 /*
148 * TODO:
149 * If ADP_Unrecognised return error.
150 * If ADP_Acknowledge - handle appropriately.
151 * If expected message read arguments and return RDIError_NoError.
152 * Note: if RDIError occurs then the data values returned are junk
153 */
154
155 unpack_message(BUFFERDATA((*packet)->pk_buffer), "%w%w%w%w%w", &reason, debugID,
156 OSinfo1, OSinfo2, status);
157 if ((reason&0xffffff) == ADP_HADPUnrecognised)
158 return RDIError_UnimplementedMessage;
159 if (reason != (unsigned ) *rcode) {
160 if((reason&0xffffff) == ADP_HADPUnrecognised)
161 return RDIError_UnimplementedMessage;
162 else {
163 angel_DebugPrint("ARDI ERROR: Expected reasoncode %x got reasoncode %x.\n",
164 *rcode, reason);
165 return RDIError_Error;
166 }
167 }
168 else
169 return RDIError_NoError;
170 return RDIError_Error; /* stop a pesky ANSI compiler warning */
171 }
172
173
174 /*
175 * Handler and registration for logging messages from target
176 */
177 static void TargetLogCallback( Packet *packet, void *state )
178 {
179 p_Buffer reply = BUFFERDATA(packet->pk_buffer);
180 unsigned int len = packet->pk_length;
181 IGNORE(state);
182 angel_hostif->write(angel_hostif->hostosarg,
183 (char *)reply, len - CHAN_HEADER_SIZE);
184 DevSW_FreePacket(packet);
185
186 packet = DevSW_AllocatePacket(4); /* better not ask for 0 */
187 /* the reply is the ACK - any contents are ignored */
188 if (packet != NULL)
189 Adp_ChannelWrite( CI_TLOG, packet );
190 }
191
192 static void TargetLogInit( void )
193 {
194 AdpErrs err = Adp_ChannelRegisterRead( CI_TLOG, TargetLogCallback, NULL );
195
196 #ifdef DEBUG
197 if (err != adp_ok)
198 angel_DebugPrint("CI_TLOG RegisterRead failed %d\n", err);
199 #else
200 IGNORE(err);
201 #endif
202 }
203
204 /*----------------------------------------------------------------------*/
205 /*----angel_RDI_open-----------------------------------------------------*/
206 /*----------------------------------------------------------------------*/
207
208 typedef struct NegotiateState {
209 bool negotiate_resp;
210 bool negotiate_ack;
211 bool link_check_resp;
212 ParameterConfig *accepted_config;
213 } NegotiateState;
214
215 static void receive_negotiate(Packet *packet, void *stateptr)
216 {
217 unsigned reason, debugID, OSinfo1, OSinfo2, status;
218 NegotiateState *n_state = (NegotiateState *)stateptr;
219 p_Buffer reply = BUFFERDATA(packet->pk_buffer);
220
221 unpack_message( reply, "%w%w%w%w",
222 &reason, &debugID, &OSinfo1, &OSinfo2 );
223 reply += ADP_DEFAULT_HEADER_SIZE;
224
225 #ifdef DEBUG
226 angel_DebugPrint( "receive_negotiate: reason %x\n", reason );
227 #endif
228
229 switch ( reason )
230 {
231 case ADP_ParamNegotiate | TtoH:
232 {
233 n_state->negotiate_resp = TRUE;
234
235 status = GET32LE( reply );
236 reply += sizeof(word);
237 #ifdef DEBUG
238 angel_DebugPrint( "ParamNegotiate status %u\n", status );
239 #endif
240 if ( status == RDIError_NoError )
241 {
242 if ( Angel_ReadParamConfigMessage(
243 reply, n_state->accepted_config ) )
244 n_state->negotiate_ack = TRUE;
245 }
246 break;
247 }
248
249 case ADP_LinkCheck | TtoH:
250 {
251 #ifdef DEBUG
252 angel_DebugPrint( "PONG!\n" );
253 #endif
254 n_state->link_check_resp = TRUE;
255 break;
256 }
257
258 default:
259 {
260 #ifdef DEBUG
261 angel_DebugPrint( "Unexpected!\n" );
262 #endif
263 break;
264 }
265 }
266 DevSW_FreePacket( packet );
267 }
268
269 # include <sys/types.h>
270 #ifdef __unix
271 # include <sys/time.h>
272 #else
273 # include <time.h>
274 #endif
275
276 /*
277 * convert a config into a single-valued options list
278 */
279 static ParameterOptions *config_to_options( const ParameterConfig *config )
280 {
281 unsigned int num_params;
282 size_t size;
283 ParameterOptions *base_p;
284
285 num_params = config->num_parameters;
286 size =
287 sizeof(ParameterOptions)
288 + num_params*(sizeof(ParameterList) + sizeof(unsigned int));
289 base_p = malloc( size );
290
291 if ( base_p != NULL )
292 {
293 unsigned int u;
294 ParameterList *list_p =
295 (ParameterList *)((char *)base_p + sizeof(ParameterOptions));
296 unsigned int *option_p =
297 (unsigned int *)(list_p + num_params);
298
299 base_p->num_param_lists = num_params;
300 base_p->param_list = list_p;
301
302 for ( u = 0; u < num_params; ++u )
303 {
304 option_p[u] = config->param[u].value;
305 list_p[u].type = config->param[u].type;
306 list_p[u].num_options = 1;
307 list_p[u].option = &option_p[u];
308 }
309 }
310
311 return base_p;
312 }
313
314 static AdpErrs negotiate_params( const ParameterOptions *user_options )
315 {
316 Packet *packet;
317 unsigned int count;
318 static Parameter params[AP_NUM_PARAMS];
319 static ParameterConfig accepted_config = { AP_NUM_PARAMS, params };
320
321 time_t t;
322
323 static volatile NegotiateState n_state;
324 n_state.negotiate_resp = FALSE;
325 n_state.negotiate_ack = FALSE;
326 n_state.link_check_resp = FALSE;
327 n_state.accepted_config = &accepted_config;
328
329 #ifdef DEBUG
330 angel_DebugPrint( "negotiate_params\n" );
331 #endif
332
333 Adp_ChannelRegisterRead( CI_HBOOT, receive_negotiate, (void *)&n_state );
334
335 packet = (Packet *)DevSW_AllocatePacket(Armsd_BufferSize);
336 count = msgbuild( BUFFERDATA(packet->pk_buffer), "%w%w%w%w",
337 ADP_ParamNegotiate | HtoT, 0,
338 ADP_HandleUnknown, ADP_HandleUnknown );
339 count += Angel_BuildParamOptionsMessage(
340 BUFFERDATA(packet->pk_buffer) + count, user_options );
341 packet->pk_length = count;
342 Adp_ChannelWriteAsync( CI_HBOOT, packet );
343
344 #ifdef DEBUG
345 angel_DebugPrint( "sent negotiate packet\n" );
346 #endif
347
348 t=time(NULL);
349
350 do {
351 Adp_AsynchronousProcessing(async_block_on_nothing);
352
353 if ((time(NULL)-t) > ADP_INITIAL_TIMEOUT_PERIOD) {
354 return adp_timeout_on_open;
355 }
356 } while ( ! n_state.negotiate_resp );
357
358 if ( n_state.negotiate_ack )
359 {
360 /* select accepted config */
361 Adp_Ioctl( DC_SET_PARAMS, (void *)n_state.accepted_config );
362
363 /*
364 * 960430 KWelton
365 *
366 * There is a race in the renegotiation protocol: the
367 * target has to have had time to load new config before
368 * we send the link check packet - insert a deliberate
369 * pause (100ms) to give the target some time
370 */
371 Adp_delay(100000);
372
373 /* do link check */
374 msgsend( CI_HBOOT, "%w%w%w%w", ADP_LinkCheck | HtoT, 0,
375 ADP_HandleUnknown, ADP_HandleUnknown );
376 #ifdef DEBUG
377 angel_DebugPrint("sent link check\n");
378 #endif
379
380 do {
381 Adp_AsynchronousProcessing(async_block_on_read);
382 } while ( ! n_state.link_check_resp );
383 Adp_initSeq();
384 }
385 return adp_ok;
386 }
387
388 static int late_booted = FALSE;
389 static bool ardi_handler_installed = FALSE;
390
391 #ifdef __unix
392 static struct sigaction old_action;
393 #else
394 static void (*old_handler)();
395 #endif
396
397 static bool boot_interrupted = FALSE;
398 static volatile bool interrupt_request = FALSE;
399 static volatile bool stop_request = FALSE;
400
401 static void ardi_sigint_handler(int sig) {
402 #ifdef DEBUG
403 if (sig != SIGINT)
404 angel_DebugPrint("Expecting SIGINT got %d.\n", sig);
405 #else
406 IGNORE(sig);
407 #endif
408 boot_interrupted = TRUE;
409 interrupt_request = TRUE;
410 #ifndef __unix
411 signal(SIGINT, ardi_sigint_handler);
412 #endif
413 }
414
415 static void install_ardi_handler( void ) {
416 if (!ardi_handler_installed) {
417 /* install a new Ctrl-C handler so we can abandon waiting */
418 #ifdef __unix
419 struct sigaction new_action;
420 sigemptyset(&new_action.sa_mask);
421 new_action.sa_handler = ardi_sigint_handler;
422 new_action.sa_flags = 0;
423 sigaction(SIGINT, &new_action, &old_action);
424 #else
425 old_handler = signal(SIGINT, ardi_sigint_handler);
426 #endif
427 ardi_handler_installed = TRUE;
428 }
429 }
430
431 static int angel_RDI_errmess(char *buf, int blen, int errnum);
432
433 static void receive_reset_acknowledge(Packet *packet, void *stateptr) {
434 unsigned reason, debugID, OSinfo1, OSinfo2, status;
435 IGNORE(stateptr);
436
437 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w", &reason, &debugID,
438 &OSinfo1, &OSinfo2, &status);
439 if (reason==(ADP_Reset | TtoH) && status==AB_NORMAL_ACK) {
440 #ifdef DEBUG
441 angel_DebugPrint("DEBUG: Successfully received normal reset acknowledgement\n");
442 late_booted = FALSE;
443 #endif
444 } else if (reason==(ADP_Reset | TtoH) && status==AB_LATE_ACK) {
445 char late_msg[AdpMessLen_LateStartup];
446 int late_len;
447 #ifdef DEBUG
448 angel_DebugPrint("DEBUG: Successfully received LATE reset acknowledgement\n");
449 #endif
450 late_booted = TRUE;
451 install_ardi_handler();
452 late_len = angel_RDI_errmess(late_msg,
453 AdpMessLen_LateStartup, adp_late_startup);
454 angel_hostif->write(angel_hostif->hostosarg, late_msg, late_len);
455 } else {
456 #ifdef DEBUG
457 angel_DebugPrint("DEBUG: Bad reset ack: reason=%8X, status=%8X\n", reason, status);
458 #endif
459 }
460 DevSW_FreePacket(packet);
461 }
462
463 static int booted_not_received;
464 static unsigned int angel_version;
465 static unsigned int adp_version;
466 static unsigned int arch_info;
467 static unsigned int cpu_info;
468 static unsigned int hw_status;
469
470 static void receive_booted(Packet *packet, void *stateptr) {
471 unsigned reason, debugID, OSinfo1, OSinfo2, banner_length, bufsiz, longsiz;
472 unsigned i, count;
473
474 IGNORE(stateptr);
475
476 count = unpack_message(BUFFERDATA(packet->pk_buffer),
477 "%w%w%w%w%w%w%w%w%w%w%w%w",
478 &reason, &debugID, &OSinfo1, &OSinfo2, &bufsiz, &longsiz,
479 &angel_version, &adp_version,
480 &arch_info, &cpu_info, &hw_status, &banner_length);
481 if (reason==(ADP_Booted | TtoH)) {
482 #ifdef MONITOR_DOWNLOAD_PACKETS
483 angel_DebugPrint("DEBUG: Successfully received Booted\n");
484 angel_DebugPrint(" cpu_info=%8X, hw_status=%8X, bufsiz=%d, longsiz=%d\n",
485 cpu_info, hw_status, bufsiz, longsiz);
486 #endif
487 /* Get the banner from the booted message */
488 for (i=0; i<banner_length; i++)
489 angel_hostif->writec(angel_hostif->hostosarg,
490 (BUFFERDATA(packet->pk_buffer)+count)[i]);
491
492 booted_not_received=0;
493 #ifndef NO_HEARTBEAT
494 heartbeat_enabled = TRUE;
495 #endif
496 Armsd_BufferSize = bufsiz + CHAN_HEADER_SIZE;
497 Armsd_LongBufSize = longsiz + CHAN_HEADER_SIZE;
498 } else {
499 #ifdef DEBUG
500 angel_DebugPrint("DEBUG: Bad Booted msg: reason=%8X\n", reason);
501 #endif
502 }
503 DevSW_FreePacket(packet);
504 }
505
506
507 /* forward declaration */
508 static int angel_negotiate_defaults( void );
509
510 /* Open communications. */
511 int angel_RDI_open(
512 unsigned type, Dbg_ConfigBlock const *config,
513 Dbg_HostosInterface const *hostif, struct Dbg_MCState *dbg_state)
514 {
515 Packet *packet;
516 int status, reasoncode, debugID, OSinfo1, OSinfo2, err;
517 ParameterOptions *user_options = NULL;
518
519 time_t t;
520
521 IGNORE( dbg_state );
522
523 if ((type & 1) == 0) {
524 /* cold start */
525 if (hostif != NULL) {
526 angel_hostif = hostif;
527 err = HostSysInit(hostif, &ardi_commandline, &hstate);
528 if (err != RDIError_NoError) {
529 #ifdef DEBUG
530 angel_DebugPrint("DEBUG: HostSysInit error %i\n",err);
531 #endif
532 return err;
533 }
534 }
535 TargetLogInit();
536 }
537
538 #ifdef DEBUG
539 angel_DebugPrint("DEBUG: Buffer allocated in angel_RDI_open(type=%i).\n",type);
540 #endif
541
542 if ((type & 1) == 0) {
543 /* cold start */
544 unsigned endian;
545 Adp_Ioctl( DC_GET_USER_PARAMS, (void *)&user_options );
546 if ( user_options != NULL ) {
547 err = negotiate_params( user_options );
548 if (err != adp_ok) return err;
549 }
550 else {
551 ParameterConfig *default_config = NULL;
552 Adp_Ioctl( DC_GET_DEFAULT_PARAMS, (void *)&default_config );
553 if ( default_config != NULL ) {
554 ParameterOptions *default_options = config_to_options(default_config);
555 err = negotiate_params( default_options );
556 if (err != adp_ok) return err;
557 }
558 }
559
560 /* Register handlers before sending any messages */
561 booted_not_received=1;
562 Adp_ChannelRegisterRead(CI_HBOOT, receive_reset_acknowledge, NULL);
563 Adp_ChannelRegisterRead(CI_TBOOT, receive_booted, NULL);
564 endian = 0;
565 if (config!=NULL) {
566 if (config->bytesex & RDISex_Little) endian |= ADP_BootHostFeature_LittleEnd;
567 if (config->bytesex & RDISex_Big) endian |= ADP_BootHostFeature_BigEnd;
568 }
569 msgsend(CI_HBOOT,"%w%w%w%w%w", ADP_Reset | HtoT, 0,
570 ADP_HandleUnknown, ADP_HandleUnknown, endian);
571 #ifdef DEBUG
572 angel_DebugPrint("DEBUG: Transmitted Reset message in angel_RDI_open.\n");
573 #endif
574
575 /* We will now either get an acknowledgement for the Reset message
576 * or if the target was started after the host, we will get a
577 * rebooted message first.
578 */
579
580 #ifdef DEBUG
581 angel_DebugPrint("DEBUG: waiting for a booted message\n");
582 #endif
583
584 {
585 boot_interrupted = FALSE;
586
587 if (late_booted)
588 install_ardi_handler();
589
590 t=time(NULL);
591
592 do {
593 Adp_AsynchronousProcessing(async_block_on_nothing);
594 if ((time(NULL)-t) > ADP_INITIAL_TIMEOUT_PERIOD && !late_booted) {
595 return adp_timeout_on_open;
596 }
597 } while (booted_not_received && !boot_interrupted);
598
599 if (ardi_handler_installed)
600 {
601 /* uninstall our Ctrl-C handler */
602 #ifdef __unix
603 sigaction(SIGINT, &old_action, NULL);
604 #else
605 signal(SIGINT, old_handler);
606 #endif
607 }
608
609 if (boot_interrupted) {
610 angel_negotiate_defaults();
611 return adp_abandon_boot_wait;
612 }
613 }
614
615 booted_not_received=1;
616 Adp_ChannelRegisterRead(CI_HBOOT, NULL, NULL);
617
618 /* Leave the booted handler installed */
619 msgsend(CI_TBOOT, "%w%w%w%w%w", ADP_Booted | HtoT, 0,
620 ADP_HandleUnknown, ADP_HandleUnknown, 0);
621 Adp_initSeq();
622 #ifdef DEBUG
623 angel_DebugPrint("DEBUG: Transmitted ADP_Booted acknowledgement.\n");
624 angel_DebugPrint("DEBUG: Boot sequence completed, leaving angel_RDI_open.\n");
625 #endif
626
627 return (hw_status & ADP_CPU_BigEndian )? RDIError_BigEndian :
628 RDIError_LittleEndian;
629 }
630 else {
631 /* warm start */
632 register_debug_message_handler();
633
634 msgsend(CI_HADP, "%w%w%w%w",
635 ADP_InitialiseApplication | HtoT, 0,
636 ADP_HandleUnknown, ADP_HandleUnknown);
637 #ifdef DEBUG
638 angel_DebugPrint("DEBUG: Transmitted Initialise Application\n");
639 #endif
640 reasoncode=ADP_InitialiseApplication | TtoH;
641 err = wait_for_debug_message(&reasoncode, &debugID, &OSinfo1, &OSinfo2,
642 &status, &packet);
643 if (err != RDIError_NoError) return err;
644 return status;
645 }
646 return -1;
647 }
648
649
650 /*----------------------------------------------------------------------*/
651 /*----angel_RDI_close----------------------------------------------------*/
652 /*----------------------------------------------------------------------*/
653
654 static int angel_negotiate_defaults( void ) {
655 int err = adp_ok;
656 ParameterConfig *default_config = NULL;
657 Adp_Ioctl( DC_GET_DEFAULT_PARAMS, (void *)&default_config );
658 if ( default_config != NULL ) {
659 ParameterOptions *default_options = config_to_options(default_config);
660 err = negotiate_params( default_options );
661 free( default_options );
662 }
663 return err;
664 }
665
666 int angel_RDI_close(void) {
667 /*Angel host exit */
668 int err;
669 int status,debugID, OSinfo1,OSinfo2;
670 int reason;
671 Packet *packet = NULL;;
672 #ifdef DEBUG
673 angel_DebugPrint("DEBUG: Entered angel_RDI_Close.\n");
674 #endif
675
676 register_debug_message_handler();
677
678 heartbeat_enabled = FALSE;
679
680 err = msgsend(CI_HADP,"%w%w%w%w",ADP_End | HtoT,0,
681 ADP_HandleUnknown, ADP_HandleUnknown);
682 if (err != RDIError_NoError) return err;
683 reason = ADP_End | TtoH;
684 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
685 &status, &packet);
686 DevSW_FreePacket(packet);
687 if (err != RDIError_NoError) return err;
688 if (status == RDIError_NoError) {
689 err = angel_negotiate_defaults();
690 if (err != adp_ok) return err;
691 Adp_Ioctl( DC_RESET, NULL ); /* just to be safe */
692 return HostSysExit(hstate);
693 }
694 else
695 return status;
696 }
697
698
699 /*----------------------------------------------------------------------*/
700 /*----angel_RDI_read-----------------------------------------------------*/
701 /*----------------------------------------------------------------------*/
702
703 /* Read memory contents from target to host: use ADP_Read */
704 int angel_RDI_read(ARMword source, void *dest, unsigned *nbytes)
705 {
706 Packet *packet=NULL;
707 int len; /* Integer to hold message length. */
708 unsigned int nbtogo = *nbytes, nbinpacket, nbdone=0;
709 int rnbytes = 0, status, reason, debugID, OSinfo1, OSinfo2, err;
710 unsigned int maxlen = Armsd_BufferSize-CHAN_HEADER_SIZE-ADP_ReadHeaderSize;
711
712 /* Print debug trace information, this is just copied straight from rdi.c
713 and I can see no reason why it should have to be changed. */
714 TracePrint(("angel_RDI_read: source=%.8lx dest=%p nbytes=%.8x\n",
715 (unsigned long)source, dest, *nbytes));
716 if (*nbytes == 0) return RDIError_NoError; /* Read nothing - easy! */
717 /* check the buffer size */
718 while (nbtogo >0) {
719 register_debug_message_handler();
720
721 nbinpacket = (nbtogo <= maxlen) ? nbtogo : maxlen;
722 len = msgsend(CI_HADP, "%w%w%w%w%w%w", ADP_Read | HtoT, 0,
723 ADP_HandleUnknown, ADP_HandleUnknown, source+nbdone,
724 nbinpacket);
725 reason=ADP_Read | TtoH;
726 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
727 &status, &packet);
728 TracePrint(("angel_RDI_read: nbinpacket =%d status=%08x err = %d\n",
729 nbinpacket,status,err));
730 if (err != RDIError_NoError) return err; /* Was there an error? */
731 if (status == RDIError_NoError){
732 rnbytes += PREAD(LE,(unsigned int *)(BUFFERDATA(packet->pk_buffer)+20));
733 TracePrint(("angel_RDI_read: rnbytes = %d\n",rnbytes));
734 memcpy(((unsigned char *)dest)+nbdone, BUFFERDATA(packet->pk_buffer)+24,
735 nbinpacket);
736 }
737 nbdone += nbinpacket;
738 nbtogo -= nbinpacket;
739 }
740 *nbytes -= rnbytes;
741 return status;
742 }
743
744
745 /*----------------------------------------------------------------------*/
746 /*----angel_RDI_write----------------------------------------------------*/
747 /*----------------------------------------------------------------------*/
748
749 /* Transfer memory block from host to target. Use ADP_Write>. */
750 int angel_RDI_write(const void *source, ARMword dest, unsigned *nbytes)
751 {
752 Packet *packet;/* Message buffers. */
753 unsigned int len, nbtogo = *nbytes, nboffset = 0, nbinpacket;
754 int status, reason, debugID, OSinfo1, OSinfo2, err;
755 unsigned int maxlen = Armsd_LongBufSize-CHAN_HEADER_SIZE-ADP_WriteHeaderSize;
756
757 TracePrint(("angel_RDI_write: source=%p dest=%.8lx nbytes=%.8x\n",
758 source, (unsigned long)dest, *nbytes));
759
760 if (*nbytes == 0) return RDIError_NoError;
761
762 *nbytes = 0;
763 while (nbtogo > 0) {
764 packet = (Packet *) DevSW_AllocatePacket(Armsd_LongBufSize);
765 nbinpacket = (nbtogo <= maxlen) ? nbtogo : maxlen;
766 len = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w",
767 ADP_Write | HtoT, 0, ADP_HandleUnknown,
768 ADP_HandleUnknown, dest+nboffset, nbinpacket);
769 /* Copy the data into the packet. */
770
771 memcpy(BUFFERDATA(packet->pk_buffer)+len,
772 ((const unsigned char *) source)+nboffset, nbinpacket);
773 nboffset += nbinpacket;
774 packet->pk_length = nbinpacket+len;
775
776 #ifdef MONITOR_DOWNLOAD_PACKETS
777 angel_DebugPrint("angel_RDI_write packet size=%i, bytes done=%i\n",
778 nbinpacket, nboffset);
779 #endif
780
781 register_debug_message_handler();
782 Adp_ChannelWrite(CI_HADP, packet);
783 reason=ADP_Write | TtoH;
784 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
785 &status, &packet);
786 nbtogo -= nbinpacket;
787 if (err != RDIError_NoError) return err;
788 if (status == RDIError_NoError)
789 *nbytes += nbinpacket;
790
791 DevSW_FreePacket(packet);
792 }
793 return status;
794 }
795
796
797 /*----------------------------------------------------------------------*/
798 /*----angel_RDI_CPUread--------------------------------------------------*/
799 /*----------------------------------------------------------------------*/
800
801 /* Reads the values of registers in the CPU, uses ADP_CPUwrite. */
802 int angel_RDI_CPUread(unsigned mode, unsigned long mask, ARMword *buffer)
803 {
804 unsigned int i, j;
805 Packet *packet = NULL;
806 int err, status, reason, debugID, OSinfo1, OSinfo2;
807 #ifdef DEBUG
808 angel_DebugPrint("DEBUG: Entered angel_RDI_CPUread.\n");
809 #endif
810 for (i=0, j=0 ; i < RDINumCPURegs ; i++)
811 if (mask & (1L << i)) j++; /* Count the number of registers. */
812
813 register_debug_message_handler();
814 msgsend(CI_HADP, "%w%w%w%w%c%w", ADP_CPUread | HtoT, 0,
815 ADP_HandleUnknown, ADP_HandleUnknown, mode, mask);
816 reason = ADP_CPUread | TtoH;
817 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
818 &status, &packet);
819 if (err != RDIError_NoError) {
820 DevSW_FreePacket(packet);
821 return err;
822 }
823 if(status == RDIError_NoError) {
824 for (i=0; i<j; i++)
825 buffer[i] = GET32LE(BUFFERDATA(packet->pk_buffer)+20+(i*4));
826 TracePrint(("angel_RDI_CPUread: mode=%.8x mask=%.8lx", mode, mask));
827 DevSW_FreePacket(packet);
828 #ifdef RDI_VERBOSE
829 if (rdi_log & 1) {
830 unsigned k;
831 for (k = 0, j = 0 ; j <= 20 ; j++)
832 if (mask & (1L << j)) {
833 angel_DebugPrint("%c%.8lx",k%4==0?'\n':' ',
834 (unsigned long)buffer[k]);
835 k++ ;
836 }
837 angel_DebugPrint("\n") ;
838 }
839 #endif
840
841 }
842 return status;
843 }
844
845 /*----------------------------------------------------------------------*/
846 /*----angel_RDI_CPUwrite-------------------------------------------------*/
847 /*----------------------------------------------------------------------*/
848
849 /* Write CPU registers: use ADP_CPUwrite. */
850 int angel_RDI_CPUwrite(unsigned mode, unsigned long mask,
851 ARMword const *buffer){
852
853 unsigned i, j, c;
854 Packet *packet;
855 int status, reason, debugID, OSinfo1, OSinfo2, err, len;
856
857 TracePrint(("angel_RDI_CPUwrite: mode=%.8x mask=%.8lx", mode, mask));
858 #ifdef RDI_VERBOSE
859 if (rdi_log & 1) {
860 for (j = 0, i = 0 ; i <= 20 ; i++)
861 if (mask & (1L << i)) {
862 angel_DebugPrint("%c%.8lx",j%4==0?'\n':' ',
863 (unsigned long)buffer[j]);
864 j++ ;
865 }
866 angel_DebugPrint("\n") ;
867 }
868 #endif
869 packet = (Packet *)DevSW_AllocatePacket(Armsd_BufferSize);
870 for (i=0, j=0; i < RDINumCPURegs ; i++)
871 if (mask & (1L << i)) j++; /* count the number of registers */
872
873 len = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%b%w",
874 ADP_CPUwrite | HtoT, 0,
875 ADP_HandleUnknown, ADP_HandleUnknown, mode, mask);
876 for(c=0; c<j; c++)
877 PUT32LE(BUFFERDATA(packet->pk_buffer)+len+(c*4), buffer[c]);
878 packet->pk_length = len+(j*4);
879 register_debug_message_handler();
880
881 Adp_ChannelWrite(CI_HADP, packet);
882 reason = ADP_CPUwrite | TtoH;
883 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
884 &status, &packet);
885 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w", &reason, &debugID,
886 &OSinfo1, &OSinfo2, &status);
887 DevSW_FreePacket(packet);
888 if (err != RDIError_NoError)
889 return err; /* Was there an error? */
890 else
891 return status;
892 }
893
894
895 /*----------------------------------------------------------------------*/
896 /*----angel_RDI_CPread---------------------------------------------------*/
897 /*----------------------------------------------------------------------*/
898
899 /* Read coprocessor's internal state. See dbg_cp.h for help.
900 * Use ADP_CPRead.
901 * It would appear that the correct behaviour at this point is to leave
902 * the unpacking to a the caller and to simply copy the stream of data
903 * words into the buffer
904 */
905
906 int angel_RDI_CPread(unsigned CPnum, unsigned long mask, ARMword *buffer){
907 Packet *packet = NULL;
908 int i, j, status, reasoncode, OSinfo1, OSinfo2, err, debugID;
909 unsigned char *rmap = cpwords[CPnum];
910 int n;
911 #ifdef DEBUG
912 angel_DebugPrint("DEBUG: Entered angel_RDI_CPread.\n");
913 #endif
914 if (rmap == NULL) return RDIError_UnknownCoPro;
915
916 register_debug_message_handler();
917 n = rmap[-1];
918 msgsend(CI_HADP, "%w%w%w%w%b%w", ADP_CPread | HtoT, 0,
919 ADP_HandleUnknown, ADP_HandleUnknown, CPnum, mask);
920 reasoncode=ADP_CPread | TtoH;
921 err = wait_for_debug_message(&reasoncode, &debugID, &OSinfo1, &OSinfo2,
922 &status, &packet);
923 if (err != RDIError_NoError) {
924 DevSW_FreePacket(packet);
925 return err; /* Was there an error? */
926 }
927 for (j=i=0; i < n ; i++) /* count the number of registers */
928 if (mask & (1L << i)) {
929 j++;
930 }
931 for (i=0; i<j; i++)
932 buffer[i] = PREAD32(LE, BUFFERDATA(packet->pk_buffer) + 20 + (i*4));
933 DevSW_FreePacket(packet);
934 TracePrint(("angel_RDI_CPread: CPnum=%.8x mask=%.8lx\n", CPnum, mask));
935 #ifdef RDI_VERBOSE
936 if (rdi_log & 1) {
937 for (i = 0, j = 0; j < n ; j++) {
938 if (mask & (1L << j)) {
939 int nw = rmap[j];
940 angel_DebugPrint("%2d ", j);
941 while (--nw > 0)
942 angel_DebugPrint("%.8lx ", (unsigned long)buffer[i++]);
943 angel_DebugPrint("%.8lx\n", (unsigned long)buffer[i++]);
944 }
945 }
946 }
947 #endif
948 return status;
949 }
950
951
952 /*----------------------------------------------------------------------*/
953 /*----angel_RDI_CPwrite--------------------------------------------------*/
954 /*----------------------------------------------------------------------*/
955
956 /* Write coprocessor's internal state. See dbg_cp.h for help. Use
957 * ADP_CPwrite.
958 */
959
960 int angel_RDI_CPwrite(unsigned CPnum, unsigned long mask,
961 ARMword const *buffer)
962 {
963 Packet *packet = NULL;
964 int i, j, len, status, reason, OSinfo1, OSinfo2, err, debugID;
965 unsigned char *rmap = cpwords[CPnum];
966 int n;
967
968 if (rmap == NULL) return RDIError_UnknownCoPro;
969 n = rmap[-1];
970
971 TracePrint(("angel_RDI_CPwrite: CPnum=%d mask=%.8lx\n", CPnum, mask));
972
973 #ifdef RDI_VERBOSE
974 if (rdi_log & 1) {
975 for (i = 0, j = 0; j < n ; j++)
976 if (mask & (1L << j)) {
977 int nw = rmap[j];
978 angel_DebugPrint("%2d ", j);
979 while (--nw > 0)
980 angel_DebugPrint("%.8lx ", (unsigned long)buffer[i++]);
981 angel_DebugPrint("%.8lx\n", (unsigned long)buffer[i++]);
982 }
983 }
984 #endif
985
986 for (j=i=0; i < n ; i++) /* Count the number of registers. */
987 if (mask & (1L << i)) j++;
988 packet = DevSW_AllocatePacket(Armsd_BufferSize);
989 len = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%c%w",
990 ADP_CPwrite | HtoT, 0,
991 ADP_HandleUnknown, ADP_HandleUnknown, CPnum, mask);
992 for(i=0; i<j; i++)
993 len+=msgbuild(BUFFERDATA(packet->pk_buffer) + len, "%w", buffer[i]);
994 packet->pk_length = len;
995 register_debug_message_handler();
996 Adp_ChannelWrite(CI_HADP, packet); /* Transmit message. */
997 reason=ADP_CPwrite | TtoH;
998 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
999 &status, &packet);
1000 DevSW_FreePacket(packet);
1001 if (err != RDIError_NoError)
1002 return err;
1003 else
1004 return status;
1005 }
1006
1007
1008 /*----------------------------------------------------------------------*/
1009 /*----angel_RDI_pointinq-------------------------------------------------*/
1010 /*----------------------------------------------------------------------*/
1011
1012 /* Do test calls to ADP_SetBreak/ADP_SetWatch to see if resources exist to
1013 carry out request. */
1014 int angel_RDI_pointinq(ARMword *address, unsigned type, unsigned datatype,
1015 ARMword *bound)
1016 {
1017 Packet *packet = NULL;
1018 int len, status, reason, OSinfo1, OSinfo2, err=RDIError_NoError;
1019 /* stop a compiler warning */
1020 int debugID, pointhandle;
1021 TracePrint(
1022 ("angel_RDI_pointinq: address=%.8lx type=%d datatype=%d bound=%.8lx ",
1023 (unsigned long)*address, type, datatype, (unsigned long)*bound));
1024 /* for a buffer. */
1025 packet = DevSW_AllocatePacket(Armsd_BufferSize);
1026 len = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%b",
1027 ((datatype == 0) ? ADP_SetBreak : ADP_SetWatch) | HtoT, 0,
1028 ADP_HandleUnknown, ADP_HandleUnknown, address, type);
1029 if (datatype == 0)
1030 len += msgbuild(BUFFERDATA(packet->pk_buffer) + 21, "%w", bound);
1031 else
1032 len += msgbuild(BUFFERDATA(packet->pk_buffer) + 21, "%b%w", datatype, bound);
1033
1034 register_debug_message_handler();
1035 packet->pk_length = len;
1036 Adp_ChannelWrite(CI_HADP, packet);
1037 reason = ((datatype == 0) ? ADP_SetBreak : ADP_SetWatch | TtoH);
1038 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
1039 &status, &packet);
1040 if (err != RDIError_NoError) {
1041 DevSW_FreePacket(packet);
1042 return err; /* Was there an error? */
1043 }
1044 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w%w",
1045 &reason, &debugID, &OSinfo1, &OSinfo2, &status,
1046 &pointhandle, &address, &bound);
1047 DevSW_FreePacket(packet);
1048 return err;
1049 }
1050
1051
1052 /*----------------------------------------------------------------------*/
1053 /*----angel_RDI_setbreak-------------------------------------------------*/
1054 /*----------------------------------------------------------------------*/
1055
1056 /* Set a breakpoint: Use ADP_SetBreak */
1057 int angel_RDI_setbreak(ARMword address, unsigned type, ARMword bound,
1058 PointHandle *handle)
1059 {
1060 int status, reason, OSinfo1, OSinfo2, err, debugID;
1061 int tmpval, tmpaddr, tmpbnd;
1062 Packet *packet;
1063 TracePrint(("angel_RDI_setbreak address=%.8lx type=%d bound=%.8lx \n",
1064 (unsigned long)address, type, (unsigned long)bound));
1065
1066 register_debug_message_handler();
1067 msgsend(CI_HADP, "%w%w%w%w%w%b%w",
1068 ADP_SetBreak| HtoT, 0, ADP_HandleUnknown,
1069 ADP_HandleUnknown, address, type, bound);
1070 reason = ADP_SetBreak |TtoH;
1071 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
1072 &status, &packet);
1073 if (err != RDIError_NoError) {
1074 DevSW_FreePacket(packet);
1075 return err; /* Was there an error? */
1076 }
1077 /* Work around varargs problem... -sts */
1078 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w%w",
1079 &reason, &debugID, &OSinfo1, &OSinfo2, &status,
1080 &tmpval, &tmpaddr, &tmpbnd);
1081 *handle = tmpval;
1082 address = tmpaddr;
1083 bound = tmpbnd;
1084 DevSW_FreePacket(packet);
1085 if (status != RDIError_NoError) return status;
1086 TracePrint(("returns handle %.8lx\n", (unsigned long)*handle));
1087 return RDIError_NoError;
1088 }
1089
1090
1091 /*----------------------------------------------------------------------*/
1092 /*----angel_RDI_clearbreak-----------------------------------------------*/
1093 /*----------------------------------------------------------------------*/
1094
1095 /* Clear a breakpoint: Use ADP_ClearBreak. */
1096 int angel_RDI_clearbreak(PointHandle handle)
1097 {
1098 Packet *packet = NULL;
1099 int status, reason, OSinfo1, OSinfo2, err, debugID;
1100
1101 TracePrint(("angel_RDI_clearbreak: handle=%.8lx\n", (unsigned long)handle));
1102
1103 register_debug_message_handler();
1104 msgsend(CI_HADP, "%w%w%w%w%w",
1105 ADP_ClearBreak| HtoT, 0, ADP_HandleUnknown,
1106 ADP_HandleUnknown, handle);
1107 reason = ADP_ClearBreak|TtoH;
1108 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
1109 &status, &packet);
1110 if (err != RDIError_NoError) {
1111 DevSW_FreePacket(packet);
1112 angel_DebugPrint("***RECEIVE DEBUG MESSAGE RETURNED ERR = %d.\n", err);
1113 return err; /* Was there an error? */
1114 }
1115 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w", &reason,
1116 &debugID, &OSinfo1, &OSinfo2, &status);
1117 DevSW_FreePacket(packet);
1118 #ifdef DEBUG
1119 angel_DebugPrint("DEBUG: Clear Break completed OK.\n");
1120 #endif
1121 return RDIError_NoError;
1122 }
1123
1124
1125 /*----------------------------------------------------------------------*/
1126 /*----angel_RDI_setwatch-------------------------------------------------*/
1127 /*----------------------------------------------------------------------*/
1128
1129 /* Set a watchpoint: use ADP_SetWatch. */
1130 int angel_RDI_setwatch(ARMword address, unsigned type, unsigned datatype,
1131 ARMword bound, PointHandle *handle)
1132 {
1133 Packet *packet = NULL;
1134 int status, reason, OSinfo1, OSinfo2, err, debugID;
1135
1136 TracePrint(("angel_RDI_setwatch: address=%.8lx type=%d bound=%.8lx ",
1137 (unsigned long)address, type, (unsigned long)bound));
1138
1139 register_debug_message_handler();
1140 msgsend(CI_HADP, "%w%w%w%w%w%b%b%w",
1141 ADP_SetWatch| HtoT, 0, ADP_HandleUnknown,
1142 ADP_HandleUnknown, address, type, datatype, bound);
1143
1144 reason = ADP_SetWatch | TtoH;
1145 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
1146 &status, &packet);
1147 if (err != RDIError_NoError) {
1148 DevSW_FreePacket(packet);
1149 return err; /* Was there an error? */
1150 }
1151 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w%w",
1152 &reason, &debugID, &OSinfo1, &OSinfo2, &status,
1153 handle, &address, &bound);
1154 DevSW_FreePacket(packet);
1155 TracePrint(("returns handle %.8lx\n", (unsigned long)*handle));
1156 return RDIError_NoError;
1157 }
1158
1159 /*----------------------------------------------------------------------*/
1160 /*----angel_RDI_clearwatch-----------------------------------------------*/
1161 /*----------------------------------------------------------------------*/
1162
1163 /* Clear a watchpoint: use ADP_ClearWatch. */
1164 int angel_RDI_clearwatch(PointHandle handle) {
1165
1166 int status, reason, OSinfo1, OSinfo2, err, debugID;
1167 Packet *packet = NULL;
1168
1169 TracePrint(("angel_RDI_clearwatch: handle=%.8lx\n", (unsigned long)handle));
1170
1171 register_debug_message_handler();
1172 msgsend(CI_HADP, "%w%w%w%w%w",
1173 ADP_ClearWatch| HtoT, 0, ADP_HandleUnknown,
1174 ADP_HandleUnknown, handle);
1175 reason = ADP_ClearWatch|TtoH;
1176 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
1177 &status, &packet);
1178 if (err != RDIError_NoError) {
1179 DevSW_FreePacket(packet);
1180 return err; /* Was there an error? */
1181 }
1182 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w", &reason, &debugID,
1183 &OSinfo1, &OSinfo2, &status);
1184 DevSW_FreePacket(packet);
1185 return RDIError_NoError;
1186 }
1187
1188 typedef struct {
1189 unsigned stopped_reason;
1190 int stopped_status;
1191 int data;
1192 } adp_stopped_struct;
1193
1194
1195 int angel_RDI_OnTargetStopping(angel_RDI_TargetStoppedProc *fn,
1196 void *arg)
1197 {
1198 stoppedProcListElement **lptr = &stopped_proc_list;
1199
1200 /* Find the address of the NULL ptr at the end of the list */
1201 for (; *lptr!=NULL ; lptr = &((*lptr)->next))
1202 ; /* Do nothing */
1203
1204 *lptr = (stoppedProcListElement *) malloc(sizeof(stoppedProcListElement));
1205 if (*lptr == NULL) return RDIError_OutOfStore;
1206 (*lptr)->fn = fn;
1207 (*lptr)->arg = arg;
1208
1209 return RDIError_NoError;
1210 }
1211
1212 static int CallStoppedProcs(unsigned reason)
1213 {
1214 stoppedProcListElement *p = stopped_proc_list;
1215 int err=RDIError_NoError;
1216
1217 for (; p!=NULL ; p=p->next) {
1218 int local_err = p->fn(reason, p->arg);
1219 if (local_err != RDIError_NoError) err=local_err;
1220 }
1221
1222 return err;
1223 }
1224
1225 /*----------------------------------------------------------------------*/
1226 /*----angel_RDI_execute--------------------------------------------------*/
1227 /*----------------------------------------------------------------------*/
1228
1229 static int HandleStoppedMessage(Packet *packet, void *stateptr) {
1230 unsigned int err, reason, debugID, OSinfo1, OSinfo2, count;
1231 adp_stopped_struct *stopped_info;
1232 stopped_info = (adp_stopped_struct *) stateptr;
1233 IGNORE(stateptr);
1234 count = unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w",
1235 &reason, &debugID,
1236 &OSinfo1, &OSinfo2,
1237 &stopped_info->stopped_reason, &stopped_info->data);
1238 DevSW_FreePacket(packet);
1239
1240 if (reason != (ADP_Stopped | TtoH)) {
1241 #ifdef DEBUG
1242 angel_DebugPrint("Expecting stopped message, got %x", reason);
1243 #endif
1244 return RDIError_Error;
1245 }
1246 else {
1247 executing = FALSE;
1248 #ifdef DEBUG
1249 angel_DebugPrint("Received stopped message.\n");
1250 #endif
1251 }
1252
1253 err = msgsend(CI_TADP, "%w%w%w%w%w", (ADP_Stopped | HtoT), 0,
1254 ADP_HandleUnknown, ADP_HandleUnknown, RDIError_NoError);
1255 #ifdef DEBUG
1256 angel_DebugPrint("Transmiting stopped acknowledge.\n");
1257 #endif
1258 if (err != RDIError_NoError) angel_DebugPrint("Transmit failed.\n");
1259 #ifdef DEBUG
1260 angel_DebugPrint("DEBUG: Stopped reason : %x\n", stopped_info->stopped_reason);
1261 #endif
1262 switch (stopped_info->stopped_reason) {
1263 case ADP_Stopped_BranchThroughZero:
1264 stopped_info->stopped_status = RDIError_BranchThrough0;
1265 break;
1266 case ADP_Stopped_UndefinedInstr:
1267 stopped_info->stopped_status = RDIError_UndefinedInstruction;
1268 break;
1269 case ADP_Stopped_SoftwareInterrupt:
1270 stopped_info->stopped_status = RDIError_SoftwareInterrupt;
1271 break;
1272 case ADP_Stopped_PrefetchAbort:
1273 stopped_info->stopped_status = RDIError_PrefetchAbort;
1274 break;
1275 case ADP_Stopped_DataAbort:
1276 stopped_info->stopped_status = RDIError_DataAbort;
1277 break;
1278 case ADP_Stopped_AddressException:
1279 stopped_info->stopped_status = RDIError_AddressException;
1280 break;
1281 case ADP_Stopped_IRQ:
1282 stopped_info->stopped_status = RDIError_IRQ;
1283 break;
1284 case ADP_Stopped_BreakPoint:
1285 stopped_info->stopped_status = RDIError_BreakpointReached;
1286 break;
1287 case ADP_Stopped_WatchPoint:
1288 stopped_info->stopped_status = RDIError_WatchpointAccessed;
1289 break;
1290 case ADP_Stopped_StepComplete:
1291 stopped_info->stopped_status = RDIError_ProgramFinishedInStep;
1292 break;
1293 case ADP_Stopped_RunTimeErrorUnknown:
1294 case ADP_Stopped_StackOverflow:
1295 case ADP_Stopped_DivisionByZero:
1296 stopped_info->stopped_status = RDIError_Error;
1297 break;
1298 case ADP_Stopped_FIQ:
1299 stopped_info->stopped_status = RDIError_FIQ;
1300 break;
1301 case ADP_Stopped_UserInterruption:
1302 case ADP_Stopped_OSSpecific:
1303 stopped_info->stopped_status = RDIError_UserInterrupt;
1304 break;
1305 case ADP_Stopped_ApplicationExit:
1306 stopped_info->stopped_status = RDIError_NoError;
1307 break;
1308 default:
1309 stopped_info->stopped_status = RDIError_Error;
1310 break;
1311 }
1312 return RDIError_NoError;
1313 }
1314
1315
1316 static void interrupt_target( void )
1317 {
1318 Packet *packet = NULL;
1319 int err;
1320 int reason, debugID, OSinfo1, OSinfo2, status;
1321
1322 #ifdef DEBUG
1323 angel_DebugPrint("DEBUG: interrupt_target.\n");
1324 #endif
1325
1326 register_debug_message_handler();
1327 msgsend(CI_HADP, "%w%w%w%w", ADP_InterruptRequest | HtoT, 0,
1328 ADP_HandleUnknown, ADP_HandleUnknown);
1329
1330 reason = ADP_InterruptRequest |TtoH;
1331 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
1332 &status, &packet);
1333 DevSW_FreePacket(packet);
1334 #ifdef DEBUG
1335 angel_DebugPrint("DEBUG: got interrupt ack ok err = %d, status=%i\n",
1336 err, status);
1337 #endif
1338
1339 return;
1340 }
1341
1342 #ifdef TEST_DC_APPL
1343 extern void test_dc_appl_handler( const DeviceDescr *device,
1344 Packet *packet );
1345 #endif
1346
1347 void angel_RDI_stop_request(void)
1348 {
1349 stop_request = 1;
1350 }
1351
1352 /* Core functionality for execute and step */
1353 static int angel_RDI_ExecuteOrStep(PointHandle *handle, word type,
1354 unsigned ninstr)
1355 {
1356 int err;
1357 adp_stopped_struct stopped_info;
1358 void* stateptr = (void *)&stopped_info;
1359 ChannelCallback HandleStoppedMessageFPtr=(ChannelCallback) HandleStoppedMessage;
1360 int status, reasoncode, debugID, OSinfo1, OSinfo2;
1361 Packet *packet = NULL;
1362
1363 TracePrint(("angel_RDI_ExecuteOrStep\n"));
1364
1365 err = Adp_ChannelRegisterRead(CI_TADP,
1366 HandleStoppedMessageFPtr, stateptr);
1367 if (err != RDIError_NoError) {
1368 #ifdef DEBUG
1369 angel_DebugPrint("TADP Register failed.\n");
1370 #endif
1371 return err;
1372 }
1373 /* Set executing TRUE here, as it must be set up before the target has
1374 * had any chance at all to execute, or it may send its stopped message
1375 * before we get round to setting executing = TRUE !!!
1376 */
1377 executing = TRUE;
1378
1379 register_debug_message_handler();
1380
1381 #ifdef TEST_DC_APPL
1382 Adp_Install_DC_Appl_Handler( test_dc_appl_handler );
1383 #endif
1384
1385 #ifdef DEBUG
1386 angel_DebugPrint("Transmiting %s message.\n",
1387 type == ADP_Execute ? "execute": "step");
1388 #endif
1389
1390 register_debug_message_handler();
1391 /* Extra ninstr parameter for execute message will simply be ignored */
1392 err = msgsend(CI_HADP,"%w%w%w%w%w", type | HtoT, 0,
1393 ADP_HandleUnknown, ADP_HandleUnknown, ninstr);
1394 #if DEBUG
1395 if (err != RDIError_NoError) angel_DebugPrint("Transmit failed.\n");
1396 #endif
1397
1398 reasoncode = type | TtoH;
1399 err = wait_for_debug_message( &reasoncode, &debugID, &OSinfo1, &OSinfo2,
1400 &status, &packet );
1401 if (err != RDIError_NoError)
1402 return err;
1403 else if (status != RDIError_NoError)
1404 return status;
1405
1406 #ifdef DEBUG
1407 angel_DebugPrint("Waiting for program to finish...\n");
1408 #endif
1409
1410 interrupt_request = FALSE;
1411 stop_request = FALSE;
1412
1413 signal(SIGINT, ardi_sigint_handler);
1414 while( executing )
1415 {
1416 if (ui_loop_hook)
1417 ui_loop_hook(0);
1418
1419 if (interrupt_request || stop_request)
1420 {
1421 interrupt_target();
1422 interrupt_request = FALSE;
1423 stop_request = FALSE;
1424 }
1425 Adp_AsynchronousProcessing( async_block_on_nothing );
1426 }
1427 signal(SIGINT, SIG_IGN);
1428
1429
1430 #ifdef TEST_DC_APPL
1431 Adp_Install_DC_Appl_Handler( NULL );
1432 #endif
1433
1434 (void)Adp_ChannelRegisterRead(CI_TADP, NULL, NULL);
1435
1436 *handle = (PointHandle)stopped_info.data;
1437
1438 CallStoppedProcs(stopped_info.stopped_reason);
1439
1440 return stopped_info.stopped_status;
1441 }
1442
1443 /* Request that the target starts executing from the stored CPU state: use
1444 ADP_Execute. */
1445 int angel_RDI_execute(PointHandle *handle)
1446 {
1447 return angel_RDI_ExecuteOrStep(handle, ADP_Execute, 0);
1448 }
1449
1450 #ifdef __WATCOMC__
1451 typedef void handlertype(int);
1452
1453 static int interrupted=0;
1454
1455 static void myhandler(int sig) {
1456 IGNORE(sig);
1457 interrupted=1;
1458 signal(SIGINT, myhandler);
1459 }
1460 #endif
1461
1462 /*----------------------------------------------------------------------*/
1463 /*----angel_RDI_step-----------------------------------------------------*/
1464 /*----------------------------------------------------------------------*/
1465
1466 /* Step 'ninstr' through the code: use ADP_Step. */
1467 int angel_RDI_step(unsigned ninstr, PointHandle *handle)
1468 {
1469 int err = angel_RDI_ExecuteOrStep(handle, ADP_Step, ninstr);
1470 if (err == RDIError_ProgramFinishedInStep)
1471 return RDIError_NoError;
1472 else
1473 return err;
1474 }
1475
1476
1477 static void SetCPWords(int cpnum, struct Dbg_CoProDesc const *cpd) {
1478 int i, rmax = 0;
1479 for (i = 0; i < cpd->entries; i++)
1480 if (cpd->regdesc[i].rmax > rmax)
1481 rmax = cpd->regdesc[i].rmax;
1482
1483 { unsigned char *rmap = (unsigned char *)malloc(rmax + 2);
1484 *rmap++ = rmax + 1;
1485 for (i = 0; i < cpd->entries; i++) {
1486 int r;
1487 for (r = cpd->regdesc[i].rmin; r <= cpd->regdesc[i].rmax; r++)
1488 rmap[r] = (cpd->regdesc[i].nbytes+3) / 4;
1489 }
1490 /* if (cpwords[cpnum] != NULL) free(cpwords[cpnum]); */
1491 cpwords[cpnum] = rmap;
1492 }
1493 }
1494
1495 /*----------------------------------------------------------------------*/
1496 /*----angel_RDI_info-----------------------------------------------------*/
1497 /*----------------------------------------------------------------------*/
1498
1499 /* Use ADP_Info, ADP_Ctrl and ADP_Profile calls to implement these,
1500 see adp.h for more details. */
1501
1502 static int angel_cc_exists( void )
1503 {
1504 Packet *packet = NULL;
1505 int err;
1506 int reason, debugID, OSinfo1, OSinfo2, subreason, status;
1507
1508 #ifdef DEBUG
1509 angel_DebugPrint("DEBUG: ADP_ICEB_CC_Exists.\n");
1510 #endif
1511
1512 if ( angel_RDI_info( RDIInfo_Icebreaker, NULL, NULL ) == RDIError_NoError ) {
1513 register_debug_message_handler();
1514 msgsend(CI_HADP, "%w%w%w%w%w", ADP_ICEbreakerHADP | HtoT, 0,
1515 ADP_HandleUnknown, ADP_HandleUnknown,
1516 ADP_ICEB_CC_Exists );
1517 reason = ADP_ICEbreakerHADP |TtoH;
1518 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
1519 &status, &packet);
1520 if (err != RDIError_NoError) {
1521 DevSW_FreePacket(packet);
1522 return err;
1523 }
1524 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason,
1525 &debugID, &OSinfo1, &OSinfo2, &subreason, &status);
1526 if (subreason != ADP_ICEB_CC_Exists) {
1527 DevSW_FreePacket(packet);
1528 return RDIError_Error;
1529 }
1530 else
1531 return status;
1532 }
1533 else
1534 return RDIError_UnimplementedMessage;
1535 }
1536
1537 typedef struct {
1538 RDICCProc_ToHost *tohost; void *tohostarg;
1539 RDICCProc_FromHost *fromhost; void *fromhostarg;
1540 bool registered;
1541 } CCState;
1542 static CCState ccstate = { NULL, NULL, NULL, NULL, FALSE };
1543
1544 static void HandleDCCMessage( Packet *packet, void *stateptr )
1545 {
1546 unsigned int reason, debugID, OSinfo1, OSinfo2;
1547 int count;
1548 CCState *ccstate_p = (CCState *)stateptr;
1549
1550 count = unpack_message( BUFFERDATA(packet->pk_buffer), "%w%w%w%w",
1551 &reason, &debugID, &OSinfo1, &OSinfo2 );
1552 switch ( reason )
1553 {
1554 case ADP_TDCC_ToHost | TtoH:
1555 {
1556 /* only handles a single word of data, for now */
1557
1558 unsigned int nbytes, data;
1559
1560 unpack_message( BUFFERDATA(packet->pk_buffer)+count, "%w%w",
1561 &nbytes, &data );
1562 #ifdef DEBUG
1563 angel_DebugPrint( "DEBUG: received CC_ToHost message: nbytes %d data %08x.\n",
1564 nbytes, data );
1565 #endif
1566 ccstate_p->tohost( ccstate_p->tohostarg, data );
1567 msgsend(CI_TTDCC, "%w%w%w%w%w",
1568 ADP_TDCC_ToHost | HtoT, debugID, OSinfo1, OSinfo2,
1569 RDIError_NoError );
1570 break;
1571 }
1572
1573 case ADP_TDCC_FromHost | TtoH:
1574 {
1575 /* only handles a single word of data, for now */
1576
1577 int valid;
1578 ARMword data;
1579
1580 ccstate_p->fromhost( ccstate_p->fromhostarg, &data, &valid );
1581 #ifdef DEBUG
1582 angel_DebugPrint( "DEBUG: received CC_FromHost message, returning: %08x %s.\n",
1583 data, valid ? "VALID" : "INvalid" );
1584 #endif
1585 msgsend(CI_TTDCC, "%w%w%w%w%w%w%w",
1586 ADP_TDCC_FromHost | HtoT, debugID, OSinfo1, OSinfo2,
1587 RDIError_NoError, valid ? 1 : 0, data );
1588 break;
1589 }
1590
1591 default:
1592 #ifdef DEBUG
1593 angel_DebugPrint( "Unexpected TDCC message %08x received\n", reason );
1594 #endif
1595 break;
1596 }
1597 DevSW_FreePacket(packet);
1598 return;
1599 }
1600
1601 static void angel_check_DCC_handler( CCState *ccstate_p )
1602 {
1603 int err;
1604
1605 if ( ccstate_p->tohost != NULL || ccstate_p->fromhost != NULL )
1606 {
1607 /* doing DCC, so need a handler */
1608 if ( ! ccstate_p->registered )
1609 {
1610 #ifdef DEBUG
1611 angel_DebugPrint( "Registering handler for TTDCC channel.\n" );
1612 #endif
1613 err = Adp_ChannelRegisterRead( CI_TTDCC, HandleDCCMessage,
1614 ccstate_p );
1615 if ( err == adp_ok )
1616 ccstate_p->registered = TRUE;
1617 #ifdef DEBUG
1618 else
1619 angel_DebugPrint( "angel_check_DCC_handler: register failed!\n" );
1620 #endif
1621 }
1622 }
1623 else
1624 {
1625 /* not doing DCC, so don't need a handler */
1626 if ( ccstate_p->registered )
1627 {
1628 #ifdef DEBUG
1629 angel_DebugPrint( "Unregistering handler for TTDCC channel.\n" );
1630 #endif
1631 err = Adp_ChannelRegisterRead( CI_TTDCC, NULL, NULL );
1632 if ( err == adp_ok )
1633 ccstate_p->registered = FALSE;
1634 #ifdef DEBUG
1635 else
1636 angel_DebugPrint( "angel_check_DCC_handler: unregister failed!\n" );
1637 #endif
1638 }
1639 }
1640 }
1641
1642
1643 static int CheckSubMessageReply(int reason, int subreason) {
1644 Packet *packet = NULL;
1645 int status, debugID, OSinfo1, OSinfo2;
1646 int err = RDIError_NoError;
1647 reason |= TtoH;
1648 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
1649 &status, &packet);
1650 if (err != RDIError_NoError) {
1651 status = err;
1652 } else {
1653 int sr;
1654 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason, &debugID,
1655 &OSinfo1, &OSinfo2, &sr, &status);
1656 if (subreason != sr) status = RDIError_Error;
1657 }
1658 DevSW_FreePacket(packet);
1659 return status;
1660 }
1661
1662 static int SendSubMessageAndCheckReply(int reason, int subreason) {
1663 register_debug_message_handler();
1664 msgsend(CI_HADP, "%w%w%w%w%w", reason | HtoT, 0,
1665 ADP_HandleUnknown, ADP_HandleUnknown,
1666 subreason);
1667 return CheckSubMessageReply(reason, subreason);
1668 }
1669
1670 static int SendSubMessageWordAndCheckReply(int reason, int subreason, ARMword word) {
1671 register_debug_message_handler();
1672 msgsend(CI_HADP, "%w%w%w%w%w%w", reason | HtoT, 0,
1673 ADP_HandleUnknown, ADP_HandleUnknown,
1674 subreason, word);
1675 return CheckSubMessageReply(reason, subreason);
1676 }
1677
1678 static int SendSubMessageGetWordAndCheckReply(int reason, int subreason, ARMword *resp) {
1679 Packet *packet = NULL;
1680 int status, debugID, OSinfo1, OSinfo2;
1681 int err = RDIError_NoError;
1682
1683 register_debug_message_handler();
1684 msgsend(CI_HADP, "%w%w%w%w%w", reason | HtoT, 0,
1685 ADP_HandleUnknown, ADP_HandleUnknown,
1686 subreason);
1687 reason |= TtoH;
1688 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
1689 &status, &packet);
1690 if (err != RDIError_NoError) {
1691 status = err;
1692 } else {
1693 int sr;
1694 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w", &reason, &debugID,
1695 &OSinfo1, &OSinfo2, &sr, &status, resp);
1696 if (subreason != sr) status = RDIError_Error;
1697 }
1698 DevSW_FreePacket(packet);
1699 return status;
1700 }
1701
1702 static int const hostsex = 1;
1703
1704 int angel_RDI_info(unsigned type, ARMword *arg1, ARMword *arg2) {
1705 Packet *packet = NULL;
1706 int len, status, c, reason, subreason, debugID, OSinfo1, OSinfo2;
1707 int err=RDIError_NoError, cpnum=0;
1708 struct Dbg_CoProDesc *cpd;
1709 int count, i;
1710 unsigned char *bp;
1711
1712 #ifdef DEBUG
1713 angel_DebugPrint("DEBUG: Entered angel_RDI_info.\n");
1714 #endif
1715 switch (type) {
1716 case RDIInfo_Target:
1717 #ifdef DEBUG
1718 angel_DebugPrint("DEBUG: RDIInfo_Target.\n");
1719 #endif
1720
1721 register_debug_message_handler();
1722 msgsend(CI_HADP, "%w%w%w%w%w", ADP_Info | HtoT, 0,
1723 ADP_HandleUnknown, ADP_HandleUnknown, ADP_Info_Target);
1724 reason = ADP_Info |TtoH;
1725 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
1726 &status, &packet);
1727 if (err != RDIError_NoError) {
1728 DevSW_FreePacket(packet);
1729 return err;
1730 }
1731 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w%w", &reason,
1732 &debugID, &OSinfo1, &OSinfo2, &subreason, &status,
1733 arg1, arg2);
1734 DevSW_FreePacket(packet);
1735
1736 if (subreason != ADP_Info_Target)
1737 return RDIError_Error;
1738 else
1739 return status;
1740
1741 case RDISignal_Stop:
1742 #ifdef DEBUG
1743 angel_DebugPrint("DEBUG: RDISignal_Stop.\n");
1744 if (interrupt_request)
1745 angel_DebugPrint(" STILL WAITING to send previous interrupt request\n");
1746 #endif
1747 interrupt_request = TRUE;
1748 return RDIError_NoError;
1749
1750 case RDIInfo_Points:
1751 #ifdef DEBUG
1752 angel_DebugPrint("DEBUG: RDIInfo_Points.\n");
1753 #endif
1754 return SendSubMessageGetWordAndCheckReply(ADP_Info, ADP_Info_Points, arg1);
1755
1756 case RDIInfo_Step:
1757 #ifdef DEBUG
1758 angel_DebugPrint("DEBUG: RDIInfo_Step.\n");
1759 #endif
1760 return SendSubMessageGetWordAndCheckReply(ADP_Info, ADP_Info_Step, arg1);
1761
1762 case RDISet_Cmdline:
1763 #ifdef DEBUG
1764 angel_DebugPrint("DEBUG: RDISet_Cmdline.\n");
1765 #endif
1766 if (ardi_commandline != &dummycline)
1767 free(ardi_commandline);
1768 ardi_commandline = (char *)malloc(strlen((char*)arg1) + 1) ;
1769 (void)strcpy(ardi_commandline, (char *)arg1) ;
1770 return RDIError_NoError;
1771
1772 case RDIInfo_SetLog:
1773 #ifdef DEBUG
1774 angel_DebugPrint("DEBUG: RDIInfo_SetLog.\n");
1775 #endif
1776 rdi_log = (int) *arg1;
1777 return RDIError_NoError;
1778
1779 case RDIInfo_Log:
1780 #ifdef DEBUG
1781 angel_DebugPrint("DEBUG: RDIInfo_Log.\n");
1782 #endif
1783 *arg1 = rdi_log;
1784 return RDIError_NoError;
1785
1786
1787 case RDIInfo_MMU:
1788 #ifdef DEBUG
1789 angel_DebugPrint("DEBUG: RDIInfo_MMU.\n");
1790 #endif
1791 return SendSubMessageGetWordAndCheckReply(ADP_Info, ADP_Info_MMU, arg1);
1792
1793 case RDIInfo_SemiHosting:
1794 #ifdef DEBUG
1795 angel_DebugPrint("DEBUG: RDIInfo_SemiHosting.\n");
1796 #endif
1797 return SendSubMessageAndCheckReply(ADP_Info, ADP_Info_SemiHosting);
1798
1799 case RDIInfo_CoPro:
1800 #ifdef DEBUG
1801 angel_DebugPrint("DEBUG: RDIInfo_CoPro.\n");
1802 #endif
1803 return SendSubMessageAndCheckReply(ADP_Info, ADP_Info_CoPro);
1804
1805 case RDICycles:
1806 #ifdef DEBUG
1807 angel_DebugPrint("DEBUG: RDICycles.\n");
1808 #endif
1809 register_debug_message_handler();
1810 msgsend(CI_HADP, "%w%w%w%w%w", ADP_Info | HtoT, 0,
1811 ADP_HandleUnknown, ADP_HandleUnknown, ADP_Info_Cycles);
1812 reason = ADP_Info |TtoH;
1813 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
1814 &status, &packet);
1815 if (err != RDIError_NoError) {
1816 DevSW_FreePacket(packet);
1817 return err;
1818 }
1819 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason, &debugID,
1820 &OSinfo1, &OSinfo2, &subreason, &status);
1821 DevSW_FreePacket(packet);
1822 if (subreason != ADP_Info_Cycles)
1823 return RDIError_Error;
1824 if (status != RDIError_NoError) return status;
1825 for (c=0; c<12; c++)
1826 arg1[c]=GET32LE(BUFFERDATA(packet->pk_buffer)+24+(c*4));
1827 return status;
1828
1829 case RDIInfo_DescribeCoPro:
1830 #ifdef DEBUG
1831 angel_DebugPrint("DEBUG: RDIInfo_DescribeCoPro.\n");
1832 #endif
1833 cpnum = *(int *)arg1;
1834 cpd = (struct Dbg_CoProDesc *)arg2;
1835 packet = DevSW_AllocatePacket(Armsd_BufferSize);
1836 if (angel_RDI_info(ADP_Info_CoPro, NULL, NULL) != RDIError_NoError)
1837 return RDIError_Error;
1838 len = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w", ADP_Info | HtoT, 0,
1839 ADP_HandleUnknown, ADP_HandleUnknown,
1840 ADP_Info_DescribeCoPro);
1841 len +=msgbuild(BUFFERDATA(packet->pk_buffer)+20, "%b%b%b%b%b", cpnum,
1842 cpd->regdesc[cpnum].rmin, cpd->regdesc[cpnum].rmax,
1843 cpd->regdesc[cpnum].nbytes, cpd->regdesc[cpnum].access);
1844 if ((cpd->regdesc[cpnum].access&0x3) == 0x3){
1845 len += msgbuild(BUFFERDATA(packet->pk_buffer)+25, "%b%b%b%b%b",
1846 cpd->regdesc[cpnum].accessinst.cprt.read_b0,
1847 cpd->regdesc[cpnum].accessinst.cprt.read_b1,
1848 cpd->regdesc[cpnum].accessinst.cprt.write_b0,
1849 cpd->regdesc[cpnum].accessinst.cprt.write_b1, 0xff);
1850 }
1851 else {
1852 len += msgbuild(BUFFERDATA(packet->pk_buffer)+25, "%b%b%b%b%b%",
1853 cpd->regdesc[cpnum].accessinst.cpdt.rdbits,
1854 cpd->regdesc[cpnum].accessinst.cpdt.nbit,0,0, 0xff);
1855 }
1856 register_debug_message_handler();
1857 packet->pk_length = len;
1858 Adp_ChannelWrite(CI_HADP, packet); /* Transmit message. */
1859 reason = ADP_Info |TtoH;
1860 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
1861 &status, &packet);
1862 if (err != RDIError_NoError) {
1863 DevSW_FreePacket(packet);
1864 return err;
1865 }
1866 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason, &debugID,
1867 &OSinfo1, &OSinfo2, &subreason, &status);
1868 DevSW_FreePacket(packet);
1869 if (subreason != ADP_Info_DescribeCoPro)
1870 return RDIError_Error;
1871 else
1872 return status;
1873
1874 case RDIInfo_RequestCoProDesc:
1875 #ifdef DEBUG
1876 angel_DebugPrint("DEBUG: RDIInfo_RequestCoProDesc.\n");
1877 #endif
1878 cpnum = *(int *)arg1;
1879 cpd = (struct Dbg_CoProDesc *)arg2;
1880 packet = DevSW_AllocatePacket(Armsd_BufferSize);
1881 len = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w", ADP_Info | HtoT, 0,
1882 ADP_HandleUnknown, ADP_HandleUnknown,
1883 ADP_Info_RequestCoProDesc);
1884 len += msgbuild(BUFFERDATA(packet->pk_buffer)+20, "%b", *(int *)arg1);
1885 packet->pk_length = len;
1886 register_debug_message_handler();
1887 Adp_ChannelWrite(CI_HADP, packet); /* Transmit message. */
1888 reason = ADP_Info |TtoH;
1889 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
1890 &status, &packet);
1891 if (err != RDIError_NoError) {
1892 DevSW_FreePacket(packet);
1893 return err;
1894 }
1895 count = unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason,
1896 &debugID, &OSinfo1, &OSinfo2, &subreason, &status);
1897 if (subreason != ADP_Info_RequestCoProDesc) {
1898 DevSW_FreePacket(packet);
1899 return RDIError_Error;
1900 } else if ( status != RDIError_NoError ) {
1901 DevSW_FreePacket(packet);
1902 return status;
1903 } else {
1904 bp = BUFFERDATA(packet->pk_buffer)+count;
1905 for ( i = 0; *bp != 0xFF && i < cpd->entries; ++i ) {
1906 cpd->regdesc[i].rmin = *bp++;
1907 cpd->regdesc[i].rmax = *bp++;
1908 cpd->regdesc[i].nbytes = *bp++;
1909 cpd->regdesc[i].access = *bp++;
1910 }
1911 cpd->entries = i;
1912 if ( *bp != 0xFF )
1913 status = RDIError_BufferFull;
1914 else
1915 SetCPWords( cpnum, cpd );
1916 DevSW_FreePacket(packet);
1917 return status;
1918 }
1919
1920 case RDIInfo_GetLoadSize:
1921 #ifdef DEBUG
1922 angel_DebugPrint("DEBUG: ADP_Info_AngelBufferSize.\n");
1923 #endif
1924 register_debug_message_handler();
1925 msgsend(CI_HADP, "%w%w%w%w%w", ADP_Info | HtoT, 0,
1926 ADP_HandleUnknown, ADP_HandleUnknown,
1927 ADP_Info_AngelBufferSize);
1928 reason = ADP_Info |TtoH;
1929 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
1930 &status, &packet);
1931 if (err != RDIError_NoError) {
1932 DevSW_FreePacket(packet);
1933 return err;
1934 }
1935 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason,
1936 &debugID, &OSinfo1, &OSinfo2, &subreason, &status);
1937 if (subreason != ADP_Info_AngelBufferSize) {
1938 DevSW_FreePacket(packet);
1939 return RDIError_Error;
1940 }
1941 else {
1942 word defaultsize, longsize;
1943 unpack_message(BUFFERDATA(packet->pk_buffer)+24, "%w%w",
1944 &defaultsize, &longsize);
1945 *arg1 = longsize - ADP_WriteHeaderSize; /* space for ADP header */
1946 #ifdef MONITOR_DOWNLOAD_PACKETS
1947 angel_DebugPrint("DEBUG: ADP_Info_AngelBufferSize: got (%d, %d), returning %d.\n",
1948 defaultsize, longsize, *arg1);
1949 #endif
1950 DevSW_FreePacket(packet);
1951 return status;
1952 }
1953
1954 case RDIVector_Catch:
1955 #ifdef DEBUG
1956 angel_DebugPrint("DEBUG: ADP_Ctrl_VectorCatch %lx.\n", *arg1);
1957 #endif
1958 return SendSubMessageWordAndCheckReply(ADP_Control, ADP_Ctrl_VectorCatch, *arg1);
1959
1960 case RDISemiHosting_SetState:
1961 #ifdef DEBUG
1962 angel_DebugPrint("DEBUG: ADP_Ctrl_SemiHosting_SetState %lx.\n", *arg1);
1963 #endif
1964 return SendSubMessageWordAndCheckReply(ADP_Control, ADP_Ctrl_SemiHosting_SetState, *arg1);
1965
1966 case RDISemiHosting_GetState:
1967 #ifdef DEBUG
1968 angel_DebugPrint("DEBUG: ADP_Ctrl_SemiHosting_GetState.\n");
1969 #endif
1970 return SendSubMessageGetWordAndCheckReply(ADP_Control, ADP_Ctrl_SemiHosting_GetState, arg1);
1971
1972 case RDISemiHosting_SetVector:
1973 #ifdef DEBUG
1974 angel_DebugPrint("DEBUG: ADP_Ctrl_SemiHosting_SetVector %lx.\n", *arg1);
1975 #endif
1976 return SendSubMessageWordAndCheckReply(ADP_Control, ADP_Ctrl_SemiHosting_SetVector, *arg1);
1977
1978 case RDISemiHosting_GetVector:
1979 #ifdef DEBUG
1980 angel_DebugPrint("DEBUG: ADP_Ctrl_SemiHosting_GetVector.\n");
1981 #endif
1982 return SendSubMessageGetWordAndCheckReply(ADP_Control, ADP_Ctrl_SemiHosting_GetVector, arg1);
1983
1984 case RDISemiHosting_SetARMSWI:
1985 #ifdef DEBUG
1986 angel_DebugPrint("DEBUG: ADP_Ctrl_SemiHosting_SetARMSWI.\n");
1987 #endif
1988 return SendSubMessageWordAndCheckReply(ADP_Control, ADP_Ctrl_SemiHosting_SetARMSWI, *arg1);
1989
1990 case RDISemiHosting_GetARMSWI:
1991 #ifdef DEBUG
1992 angel_DebugPrint("DEBUG: ADP_Ctrl_SemiHosting_GetARMSWI.\n");
1993 #endif
1994 return SendSubMessageGetWordAndCheckReply(ADP_Control, ADP_Ctrl_SemiHosting_GetARMSWI, arg1);
1995
1996 case RDISemiHosting_SetThumbSWI:
1997 #ifdef DEBUG
1998 angel_DebugPrint("DEBUG: ADP_Ctrl_SemiHosting_SetThumbSWI.\n");
1999 #endif
2000 return SendSubMessageWordAndCheckReply(ADP_Control, ADP_Ctrl_SemiHosting_SetThumbSWI, *arg1);
2001
2002 case RDISemiHosting_GetThumbSWI:
2003 #ifdef DEBUG
2004 angel_DebugPrint("DEBUG: ADP_Ctrl_SemiHosting_GetThumbSWI.\n");
2005 #endif
2006 return SendSubMessageGetWordAndCheckReply(ADP_Control, ADP_Ctrl_SemiHosting_GetThumbSWI, arg1);
2007
2008 case RDIInfo_SetTopMem:
2009 #ifdef DEBUG
2010 angel_DebugPrint("DEBUG: ADP_Ctrl_SetTopMem.\n");
2011 #endif
2012 return SendSubMessageWordAndCheckReply(ADP_Control, ADP_Ctrl_SetTopMem, *arg1);
2013
2014 case RDIPointStatus_Watch:
2015 #ifdef DEBUG
2016 angel_DebugPrint("DEBUG: ADP_Ctrl_PointStatus_Watch.\n");
2017 #endif
2018 register_debug_message_handler();
2019 msgsend(CI_HADP, "%w%w%w%w%w%w", ADP_Control | HtoT, 0,
2020 ADP_HandleUnknown, ADP_HandleUnknown,
2021 ADP_Ctrl_PointStatus_Watch, *arg1 );
2022 reason = ADP_Control |TtoH;
2023 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
2024 &status, &packet);
2025 if (err != RDIError_NoError) {
2026 DevSW_FreePacket(packet);
2027 return err;
2028 }
2029 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w%w", &reason,
2030 &debugID, &OSinfo1, &OSinfo2, &subreason, &status,
2031 arg1, arg2);
2032 if (subreason != ADP_Ctrl_PointStatus_Watch) {
2033 DevSW_FreePacket(packet);
2034 return RDIError_Error;
2035 }
2036 else
2037 return status;
2038
2039 case RDIPointStatus_Break:
2040 #ifdef DEBUG
2041 angel_DebugPrint("DEBUG: ADP_Ctrl_PointStatus_Break.\n");
2042 #endif
2043 register_debug_message_handler();
2044 msgsend(CI_HADP, "%w%w%w%w%w%w", ADP_Control | HtoT, 0,
2045 ADP_HandleUnknown, ADP_HandleUnknown,
2046 ADP_Ctrl_PointStatus_Break, *arg1 );
2047 reason = ADP_Control |TtoH;
2048 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
2049 &status, &packet);
2050 if (err != RDIError_NoError) {
2051 DevSW_FreePacket(packet);
2052 return err;
2053 }
2054 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w%w", &reason,
2055 &debugID, &OSinfo1, &OSinfo2, &subreason, &status,
2056 arg1, arg2);
2057 if (subreason != ADP_Ctrl_PointStatus_Break) {
2058 DevSW_FreePacket(packet);
2059 return RDIError_Error;
2060 }
2061 else
2062 return status;
2063
2064 case RDIInfo_DownLoad:
2065 #ifdef DEBUG
2066 angel_DebugPrint("DEBUG: ADP_Ctrl_Download_Supported.\n");
2067 #endif
2068 return SendSubMessageAndCheckReply(ADP_Control, ADP_Ctrl_Download_Supported);
2069
2070 case RDIConfig_Count:
2071 #ifdef DEBUG
2072 angel_DebugPrint("DEBUG: ADP_ICEM_ConfigCount.\n");
2073 #endif
2074 return SendSubMessageGetWordAndCheckReply(ADP_ICEman, ADP_ICEM_ConfigCount, arg1);
2075
2076 case RDIConfig_Nth:
2077 #ifdef DEBUG
2078 angel_DebugPrint("DEBUG: ADP_ICEM_ConfigNth.\n");
2079 #endif
2080 register_debug_message_handler();
2081 msgsend(CI_HADP, "%w%w%w%w%w%w", ADP_ICEman | HtoT, 0,
2082 ADP_HandleUnknown, ADP_HandleUnknown,
2083 ADP_ICEM_ConfigNth, *arg1 );
2084 reason = ADP_ICEman |TtoH;
2085 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
2086 &status, &packet);
2087 if (err != RDIError_NoError) {
2088 DevSW_FreePacket(packet);
2089 return err;
2090 } else {
2091 RDI_ConfigDesc *cd = (RDI_ConfigDesc *)arg2;
2092 unsigned char n;
2093 len = unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w%b",
2094 &reason, &debugID,
2095 &OSinfo1, &OSinfo2, &subreason, &status,
2096 &cd->version, &n);
2097 if (subreason != ADP_ICEM_ConfigNth) {
2098 DevSW_FreePacket(packet);
2099 return RDIError_Error;
2100 }
2101 else {
2102 memcpy( cd->name, BUFFERDATA(packet->pk_buffer)+len, n+1 );
2103 cd->name[n] = 0;
2104 return status;
2105 }
2106 }
2107
2108 case RDIInfo_Icebreaker:
2109 #ifdef DEBUG
2110 angel_DebugPrint("DEBUG: ADP_ICEB_Exists.\n");
2111 #endif
2112 return SendSubMessageAndCheckReply(ADP_ICEbreakerHADP, ADP_ICEB_Exists);
2113
2114 case RDIIcebreaker_GetLocks:
2115 #ifdef DEBUG
2116 angel_DebugPrint("DEBUG: ADP_ICEB_GetLocks.\n");
2117 #endif
2118 return SendSubMessageGetWordAndCheckReply(ADP_ICEbreakerHADP, ADP_ICEB_GetLocks, arg1);
2119
2120 case RDIIcebreaker_SetLocks:
2121 #ifdef DEBUG
2122 angel_DebugPrint("DEBUG: ADP_ICEB_SetLocks.\n");
2123 #endif
2124 return SendSubMessageWordAndCheckReply(ADP_ICEbreakerHADP, ADP_ICEB_SetLocks, *arg1);
2125
2126 case RDICommsChannel_ToHost:
2127 #ifdef DEBUG
2128 angel_DebugPrint("DEBUG: ADP_ICEB_CC_Connect_ToHost.\n");
2129 #endif
2130 if ( angel_cc_exists() == RDIError_NoError ) {
2131
2132 /*
2133 * The following three lines of code have to be removed in order to get
2134 * the Windows Angel Channel Viewer working with the Thumb comms channel.
2135 * At the moment it allows the ARMSD command line to register a CCIN/CCOUT
2136 * callback which stops the ACV working!
2137 */
2138 #ifdef __unix
2139 ccstate.tohost = (RDICCProc_ToHost *)arg1;
2140 ccstate.tohostarg = arg2;
2141 angel_check_DCC_handler( &ccstate );
2142 #endif
2143 #ifdef _WIN32
2144
2145 #endif
2146
2147 register_debug_message_handler();
2148 msgsend(CI_HADP, "%w%w%w%w%w%b", ADP_ICEbreakerHADP | HtoT, 0,
2149 ADP_HandleUnknown, ADP_HandleUnknown,
2150 ADP_ICEB_CC_Connect_ToHost, (arg1 != NULL) );
2151 return CheckSubMessageReply(ADP_ICEbreakerHADP, ADP_ICEB_CC_Connect_ToHost);
2152 } else {
2153 return RDIError_UnimplementedMessage;
2154 }
2155
2156 case RDICommsChannel_FromHost:
2157 #ifdef DEBUG
2158 angel_DebugPrint("DEBUG: ADP_ICEB_CC_Connect_FromHost.\n");
2159 #endif
2160 if ( angel_cc_exists() == RDIError_NoError ) {
2161
2162 ccstate.fromhost = (RDICCProc_FromHost *)arg1;
2163 ccstate.fromhostarg = arg2;
2164 angel_check_DCC_handler( &ccstate );
2165
2166 register_debug_message_handler();
2167 msgsend(CI_HADP, "%w%w%w%w%w%b", ADP_ICEbreakerHADP | HtoT, 0,
2168 ADP_HandleUnknown, ADP_HandleUnknown,
2169 ADP_ICEB_CC_Connect_FromHost, (arg1 != NULL) );
2170 return CheckSubMessageReply(ADP_ICEbreakerHADP, ADP_ICEB_CC_Connect_FromHost);
2171 } else {
2172 return RDIError_UnimplementedMessage;
2173 }
2174
2175 case RDIProfile_Stop:
2176 return SendSubMessageAndCheckReply(ADP_Profile, ADP_Profile_Stop);
2177
2178 case RDIProfile_ClearCounts:
2179 return SendSubMessageAndCheckReply(ADP_Profile, ADP_Profile_ClearCounts);
2180
2181 case RDIProfile_Start:
2182 #ifdef DEBUG
2183 angel_DebugPrint("DEBUG: ADP_Profile_Start %ld.\n", (long)*arg1);
2184 #endif
2185 return SendSubMessageWordAndCheckReply(ADP_Profile, ADP_Profile_Start, *arg1);
2186
2187 case RDIProfile_WriteMap:
2188 { RDI_ProfileMap *map = (RDI_ProfileMap *)arg1;
2189 int32 maplen = map->len,
2190 offset,
2191 size;
2192 int32 chunk = (Armsd_LongBufSize-CHAN_HEADER_SIZE-ADP_ProfileWriteHeaderSize) / sizeof(ARMword);
2193 /* Maximum number of words sendable in one message */
2194 int oldrev = bytesex_reversing();
2195 int host_little = *(uint8 const *)&hostsex;
2196 #ifdef DEBUG
2197 angel_DebugPrint("DEBUG: ADP_Profile_WriteMap %ld.\n", maplen);
2198 #endif
2199 status = RDIError_NoError;
2200 if (!host_little) {
2201 bytesex_reverse(1);
2202 for (offset = 0; offset < maplen; offset++)
2203 map->map[offset] = bytesex_hostval(map->map[offset]);
2204 }
2205 for (offset = 0; offset < maplen; offset += size) {
2206 unsigned hdrlen;
2207 size = maplen - offset;
2208 packet = (Packet *)DevSW_AllocatePacket(Armsd_LongBufSize);
2209 if (size > chunk) size = chunk;
2210 hdrlen = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w%w",
2211 ADP_Profile | HtoT, 0, ADP_HandleUnknown,
2212 ADP_HandleUnknown, ADP_Profile_WriteMap,
2213 maplen, size, offset);
2214
2215 /* Copy the data into the packet. */
2216 memcpy(BUFFERDATA(packet->pk_buffer)+hdrlen,
2217 &map->map[offset], (size_t)size * sizeof(ARMword));
2218 packet->pk_length = size * sizeof(ARMword) + hdrlen;
2219 register_debug_message_handler();
2220 Adp_ChannelWrite(CI_HADP, packet);
2221 reason = ADP_Profile | TtoH;
2222 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
2223 &status, &packet);
2224 if (err == RDIError_NoError) {
2225 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason,
2226 &debugID, &OSinfo1, &OSinfo2, &subreason, &status);
2227 if (subreason != ADP_Profile_WriteMap) {
2228 err = RDIError_Error;
2229 }
2230 DevSW_FreePacket(packet);
2231 }
2232 if (err != RDIError_NoError) { status = err; break; }
2233 }
2234 if (!host_little) {
2235 for (offset = 0; offset < maplen; offset++)
2236 map->map[offset] = bytesex_hostval(map->map[offset]);
2237 bytesex_reverse(oldrev);
2238 }
2239 return status;
2240 }
2241
2242 case RDIProfile_ReadMap:
2243 { int32 maplen = *(int32 *)arg1,
2244 offset = 0,
2245 size;
2246 int32 chunk = (Armsd_BufferSize-CHAN_HEADER_SIZE-ADP_ProfileReadHeaderSize) / sizeof(ARMword);
2247 #ifdef DEBUG
2248 angel_DebugPrint("DEBUG: ADP_Profile_ReadMap %ld.\n", maplen);
2249 #endif
2250 status = RDIError_NoError;
2251 for (offset = 0; offset < maplen; offset += size) {
2252 size = maplen - offset;
2253 if (size > chunk) size = chunk;
2254 register_debug_message_handler();
2255 msgsend(CI_HADP, "%w%w%w%w%w%w%w", ADP_Profile | HtoT, 0,
2256 ADP_HandleUnknown, ADP_HandleUnknown,
2257 ADP_Profile_ReadMap, offset, size);
2258 reason = ADP_Profile | TtoH;
2259 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
2260 &status, &packet);
2261 if (err != RDIError_NoError) return err;
2262 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason,
2263 &debugID, &OSinfo1, &OSinfo2, &subreason, &status);
2264 memcpy(&arg2[offset], BUFFERDATA(packet->pk_buffer)+ADP_ProfileReadHeaderSize,
2265 size * sizeof(ARMword));
2266 DevSW_FreePacket(packet);
2267 if (status != RDIError_NoError) break;
2268 }
2269 { int oldrev = bytesex_reversing();
2270 int host_little = *(uint8 const *)&hostsex;
2271 if (!host_little) {
2272 bytesex_reverse(1);
2273 for (offset = 0; offset < maplen; offset++)
2274 arg2[offset] = bytesex_hostval(arg2[offset]);
2275 }
2276 bytesex_reverse(oldrev);
2277 }
2278 return status;
2279 }
2280
2281 case RDIInfo_CanTargetExecute:
2282 #ifdef DEBUG
2283 printf("DEBUG: RDIInfo_CanTargetExecute.\n");
2284 #endif
2285 return SendSubMessageAndCheckReply(ADP_Info, ADP_Info_CanTargetExecute);
2286
2287 case RDIInfo_AgentEndianess:
2288 return SendSubMessageAndCheckReply(ADP_Info, ADP_Info_AgentEndianess);
2289
2290 default:
2291 #ifdef DEBUG
2292 angel_DebugPrint("DEBUG: Fell through ADP_Info, default case taken.\n");
2293 angel_DebugPrint("DEBUG: type = 0x%x.\n", type);
2294 #endif
2295 if (type & RDIInfo_CapabilityRequest) {
2296 switch (type & ~RDIInfo_CapabilityRequest) {
2297 case RDISemiHosting_SetARMSWI:
2298 return SendSubMessageAndCheckReply(ADP_Info, ADP_Info_ChangeableSHSWI);
2299 default:
2300 #ifdef DEBUG
2301 angel_DebugPrint(
2302 "DEBUG: ADP_Info - Capability Request(%d) - reporting unimplemented \n",
2303 type & ~RDIInfo_CapabilityRequest);
2304 #endif
2305 break;
2306 }
2307 }
2308 return RDIError_UnimplementedMessage;
2309 }
2310 }
2311
2312
2313 /*----------------------------------------------------------------------*/
2314 /*----angel_RDI_AddConfig------------------------------------------------*/
2315 /*----------------------------------------------------------------------*/
2316
2317 /* Add a configuration: use ADP_ICEM_AddConfig. */
2318 int angel_RDI_AddConfig(unsigned long nbytes) {
2319 Packet *packet = NULL;
2320 int status, reason, subreason, debugID, OSinfo1, OSinfo2, err;
2321
2322 #ifdef DEBUG
2323 angel_DebugPrint("DEBUG: Entered angel_RDI_AddConfig.\n");
2324 #endif
2325 register_debug_message_handler();
2326 msgsend(CI_HADP, "%w%w%w%w%w%w", ADP_ICEman | HtoT,
2327 0, ADP_HandleUnknown, ADP_HandleUnknown,
2328 ADP_ICEM_AddConfig, nbytes);
2329 reason=ADP_ICEman | TtoH;
2330 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
2331 &status, &packet);
2332 if (err != RDIError_NoError) {
2333 DevSW_FreePacket(packet);
2334 return -1;
2335 }
2336 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason, &debugID,
2337 &OSinfo1, &OSinfo2, &subreason, &status);
2338 DevSW_FreePacket(packet);
2339 if ( subreason != ADP_ICEM_AddConfig )
2340 return RDIError_Error;
2341 else
2342 return status;
2343 }
2344
2345
2346 /*----------------------------------------------------------------------*/
2347 /*----angel_RDI_LoadConfigData-------------------------------------------*/
2348 /*----------------------------------------------------------------------*/
2349
2350 /* Load configuration data: use ADP_Ctrl_Download_Data. */
2351 int angel_RDI_LoadConfigData(unsigned long nbytes, char const *data) {
2352 Packet *packet = NULL;
2353 int len, status, reason, subreason, debugID, OSinfo1, OSinfo2, err;
2354
2355 #ifdef DEBUG
2356 angel_DebugPrint("DEBUG: Entered angel_RDI_LoadConfigData (%d bytes)\n", nbytes);
2357 #endif
2358 #if 0
2359 if (err = angel_RDI_AddConfig(nbytes) != RDIError_NoError)
2360 return err;
2361 #endif
2362 packet = DevSW_AllocatePacket(Armsd_LongBufSize);
2363 len = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w",
2364 ADP_Control | HtoT, 0,
2365 ADP_HandleUnknown, ADP_HandleUnknown,
2366 ADP_Ctrl_Download_Data, nbytes);
2367 memcpy(BUFFERDATA(packet->pk_buffer)+len, data, nbytes);
2368 len += nbytes;
2369 packet->pk_length = len;
2370 #ifdef DEBUG
2371 angel_DebugPrint("DEBUG: packet len %d.\n", len);
2372 #endif
2373 register_debug_message_handler();
2374 Adp_ChannelWrite(CI_HADP, packet);
2375 reason=ADP_Control | TtoH;
2376 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
2377 &status, &packet);
2378 if (err != RDIError_NoError) {
2379 DevSW_FreePacket(packet);
2380 return -1;
2381 }
2382 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason, &debugID,
2383 &OSinfo1, &OSinfo2, &subreason, &status);
2384 DevSW_FreePacket(packet);
2385 if ( subreason != ADP_Ctrl_Download_Data )
2386 return RDIError_Error;
2387 else
2388 return status;
2389 }
2390
2391
2392 /*----------------------------------------------------------------------*/
2393 /*----angel_RDI_SelectConfig---------------------------------------------*/
2394 /*----------------------------------------------------------------------*/
2395
2396 /* Select a configuration: use ADP_ICEM_SelecConfig.*/
2397 int angel_RDI_SelectConfig(RDI_ConfigAspect aspect, char const *name,
2398 RDI_ConfigMatchType matchtype, unsigned versionreq,
2399 unsigned *versionp)
2400 {
2401 Packet *packet = NULL;
2402 int len, status, reason, subreason, debugID, OSinfo1, OSinfo2, err;
2403
2404 #ifdef DEBUG
2405 angel_DebugPrint("DEBUG: Entered angel_RDI_SelectConfig.\n");
2406 #endif
2407 packet = DevSW_AllocatePacket(Armsd_BufferSize);
2408 len = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%b%b%b%w",
2409 ADP_ICEman | HtoT, 0,
2410 ADP_HandleUnknown, ADP_HandleUnknown,
2411 ADP_ICEM_SelectConfig, aspect, strlen(name),
2412 matchtype, versionreq);
2413 /* copy the name into the buffer */
2414 memcpy(BUFFERDATA(packet->pk_buffer)+len, name, strlen(name));
2415 len += strlen(name);
2416 packet->pk_length = len;
2417 register_debug_message_handler();
2418 Adp_ChannelWrite(CI_HADP, packet);
2419 reason=ADP_ICEman | TtoH;
2420 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
2421 &status, &packet);
2422 if (err != RDIError_NoError) {
2423 DevSW_FreePacket(packet);
2424 return err;
2425 }
2426 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w",
2427 &reason, &debugID, &OSinfo1, &OSinfo2,
2428 &subreason, &status, versionp);
2429 DevSW_FreePacket(packet);
2430 if ( subreason != ADP_ICEM_SelectConfig )
2431 return RDIError_Error;
2432 else
2433 return status;
2434 }
2435
2436
2437 /*----------------------------------------------------------------------*/
2438 /*----angel_RDI_LoadAgent------------------------------------------------*/
2439 /*----------------------------------------------------------------------*/
2440
2441 /* Load a new debug agent: use ADP_Ctrl_Download_Agent. */
2442 int angel_RDI_LoadAgent(ARMword dest, unsigned long size,
2443 getbufferproc *getb, void *getbarg)
2444 {
2445 Packet *packet = NULL;
2446 int status, reason, subreason, debugID, OSinfo1, OSinfo2, err;
2447 time_t t;
2448
2449 #if defined(DEBUG) || defined(DEBUG_LOADAGENT)
2450 angel_DebugPrint("DEBUG: Entered angel_RDI_LoadAgent.\n");
2451 #endif
2452 register_debug_message_handler();
2453 msgsend(CI_HADP, "%w%w%w%w%w%w%w", ADP_Control | HtoT,
2454 0, ADP_HandleUnknown, ADP_HandleUnknown,
2455 ADP_Ctrl_Download_Agent, dest, size);
2456 reason=ADP_Control | TtoH;
2457 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
2458 &status, &packet);
2459 if (err != RDIError_NoError) {
2460 DevSW_FreePacket(packet);
2461 return -1;
2462 }
2463 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason, &debugID,
2464 &OSinfo1, &OSinfo2, &subreason, &status);
2465 DevSW_FreePacket(packet);
2466 if ( subreason != ADP_Ctrl_Download_Agent )
2467 return RDIError_Error;
2468 if ( status != RDIError_NoError )
2469 return status;
2470
2471 #if defined(DEBUG) || defined(DEBUG_LOADAGENT)
2472 angel_DebugPrint("DEBUG: starting agent data download.\n");
2473 #endif
2474 { unsigned long pos = 0, segsize;
2475 for (; pos < size; pos += segsize) {
2476 char *b = getb(getbarg, &segsize);
2477 if (b == NULL) return RDIError_NoError;
2478 err = angel_RDI_LoadConfigData( segsize, b );
2479 if (err != RDIError_NoError) return err;
2480 }
2481 }
2482 #if defined(DEBUG) || defined(DEBUG_LOADAGENT)
2483 angel_DebugPrint("DEBUG: finished downloading new agent.\n");
2484 #endif
2485
2486 /* renegotiate back down */
2487 err = angel_negotiate_defaults();
2488 if (err != adp_ok)
2489 return err;
2490
2491 /* Output a message to tell the user what is going on. This is vital
2492 * when switching from ADP EICE to ADP over JTAG, as then the user
2493 * has to reset the target board !
2494 */
2495 { char msg[256];
2496 int len=angel_RDI_errmess(msg, 256, adp_new_agent_starting);
2497 angel_hostif->write(angel_hostif->hostosarg, msg, len);
2498 }
2499
2500 /* get new image started */
2501 #if defined(DEBUG) || defined(DEBUG_LOADAGENT)
2502 angel_DebugPrint("DEBUG: sending start message for new agent.\n");
2503 #endif
2504
2505 register_debug_message_handler();
2506 msgsend(CI_HADP, "%w%w%w%w%w%w", ADP_Control | HtoT,
2507 0, ADP_HandleUnknown, ADP_HandleUnknown,
2508 ADP_Ctrl_Start_Agent, dest);
2509 reason=ADP_Control | TtoH;
2510 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
2511 &status, &packet);
2512 if (err != RDIError_NoError) {
2513 DevSW_FreePacket(packet);
2514 return -1;
2515 }
2516 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason,
2517 &debugID, &OSinfo1, &OSinfo2, &subreason, &status);
2518 DevSW_FreePacket(packet);
2519 if ( subreason != ADP_Ctrl_Start_Agent )
2520 return RDIError_Error;
2521 if ( status != RDIError_NoError )
2522 return status;
2523
2524 /* wait for image to start up */
2525 heartbeat_enabled = FALSE;
2526 t=time(NULL);
2527 do {
2528 Adp_AsynchronousProcessing(async_block_on_nothing);
2529 if ((time(NULL)-t) > 2) {
2530 #ifdef DEBUG
2531 angel_DebugPrint("DEBUG: no booted message from new image yet.\n");
2532 #endif
2533 break;
2534 }
2535 } while (booted_not_received);
2536 booted_not_received=1;
2537
2538 /* Give device driver a chance to do any necessary resyncing with new agent.
2539 * Only used by etherdrv.c at the moment.
2540 */
2541 (void)Adp_Ioctl( DC_RESYNC, NULL );
2542
2543 #if defined(DEBUG) || defined(DEBUG_LOADAGENT)
2544 angel_DebugPrint("DEBUG: reopening to new agent.\n");
2545 #endif
2546 err = angel_RDI_open(0, NULL, NULL, NULL);
2547 switch ( err )
2548 {
2549 case RDIError_NoError:
2550 {
2551 #if defined(DEBUG) || defined(DEBUG_LOADAGENT)
2552 angel_DebugPrint( "LoadAgent: Open returned RDIError_NoError\n" );
2553 #endif
2554 break;
2555 }
2556
2557 case RDIError_LittleEndian:
2558 {
2559 #if defined(DEBUG) || defined(DEBUG_LOADAGENT)
2560 angel_DebugPrint( "LoadAgent: Open returned RDIError_LittleEndian (OK)\n" );
2561 #endif
2562 err = RDIError_NoError;
2563 break;
2564 }
2565
2566 case RDIError_BigEndian:
2567 {
2568 #if defined(DEBUG) || defined(DEBUG_LOADAGENT)
2569 angel_DebugPrint( "LoadAgent: Open returned RDIError_BigEndian (OK)\n" );
2570 #endif
2571 err = RDIError_NoError;
2572 break;
2573 }
2574
2575 default:
2576 {
2577 #if defined(DEBUG) || defined(DEBUG_LOADAGENT)
2578 angel_DebugPrint( "LoadAgent: Open returned %d - unexpected!\n", err );
2579 #endif
2580 break;
2581 }
2582 }
2583 #ifndef NO_HEARTBEAT
2584 heartbeat_enabled = TRUE;
2585 #endif
2586 return err;
2587 }
2588
2589 static int angel_RDI_errmess(char *buf, int blen, int errnum) {
2590 char *s=NULL;
2591 int n;
2592
2593 switch (errnum) {
2594 case adp_malloc_failure:
2595 s=AdpMess_MallocFailed; break;
2596 case adp_illegal_args:
2597 s=AdpMess_IllegalArgs; break;
2598 case adp_device_not_found:
2599 s=AdpMess_DeviceNotFound; break;
2600 case adp_device_open_failed:
2601 s=AdpMess_DeviceOpenFailed; break;
2602 case adp_device_already_open:
2603 s=AdpMess_DeviceAlreadyOpen; break;
2604 case adp_device_not_open:
2605 s=AdpMess_DeviceNotOpen; break;
2606 case adp_bad_channel_id:
2607 s=AdpMess_BadChannelId; break;
2608 case adp_callback_already_registered:
2609 s=AdpMess_CBAlreadyRegd; break;
2610 case adp_write_busy:
2611 s=AdpMess_WriteBusy; break;
2612 case adp_bad_packet:
2613 s=AdpMess_BadPacket; break;
2614 case adp_seq_high:
2615 s=AdpMess_SeqHigh; break;
2616 case adp_seq_low:
2617 s=AdpMess_SeqLow; break;
2618 case adp_timeout_on_open:
2619 s=AdpMess_TimeoutOnOpen; break;
2620 case adp_failed:
2621 s=AdpMess_Failed; break;
2622 case adp_abandon_boot_wait:
2623 s=AdpMess_AbandonBootWait; break;
2624 case adp_late_startup:
2625 s=AdpMess_LateStartup; break;
2626 case adp_new_agent_starting:
2627 s=AdpMess_NewAgentStarting; break;
2628 default: return 0;
2629 }
2630 n=strlen(s);
2631 if (n>blen-1) n=blen-1;
2632 memcpy(buf, s, n);
2633 buf[n++]=0;
2634 return n;
2635 }
2636
2637 extern const struct RDIProcVec angel_rdi;
2638 const struct RDIProcVec angel_rdi = {
2639 "ADP",
2640 angel_RDI_open,
2641 angel_RDI_close,
2642 angel_RDI_read,
2643 angel_RDI_write,
2644 angel_RDI_CPUread,
2645 angel_RDI_CPUwrite,
2646 angel_RDI_CPread,
2647 angel_RDI_CPwrite,
2648 angel_RDI_setbreak,
2649 angel_RDI_clearbreak,
2650 angel_RDI_setwatch,
2651 angel_RDI_clearwatch,
2652 angel_RDI_execute,
2653 angel_RDI_step,
2654 angel_RDI_info,
2655 angel_RDI_pointinq,
2656
2657 angel_RDI_AddConfig,
2658 angel_RDI_LoadConfigData,
2659 angel_RDI_SelectConfig,
2660
2661 0, /*angel_RDI_drivernames,*/
2662 0, /* cpunames */
2663
2664 angel_RDI_errmess,
2665
2666 angel_RDI_LoadAgent
2667 };
2668
2669 /* EOF ardi.c */
2670
2671 /* Not strictly necessary, but allows linking this code into armsd. */
2672
2673 struct foo {
2674 char *name;
2675 int (*action)();
2676 char *syntax;
2677 char **helpmessage;
2678 int doafterend;
2679 int dobeforestart;
2680 int doinmidline;
2681 } hostappl_CmdTable[1] = {{"", NULL}};
2682
2683 void
2684 hostappl_Init()
2685 {
2686 }
2687
2688 int
2689 hostappl_Backstop()
2690 {
2691 return -30;
2692 }
This page took 0.09817 seconds and 4 git commands to generate.