OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [gdb/] [rdi-share/] [ardi.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.