Statistics
| Branch: | Tag: | Revision:

amiro-blt / Target / Source / xcp.c @ 69661903

History | View | Annotate | Download (51.396 KB)

1
/************************************************************************************//**
2
* \file         Source\xcp.c
3
* \brief        XCP 1.0 protocol core source file.
4
* \ingroup      Core
5
* \internal
6
*----------------------------------------------------------------------------------------
7
*                          C O P Y R I G H T
8
*----------------------------------------------------------------------------------------
9
*   Copyright (c) 2011  by Feaser    http://www.feaser.com    All rights reserved
10
*
11
*----------------------------------------------------------------------------------------
12
*                            L I C E N S E
13
*----------------------------------------------------------------------------------------
14
* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
15
* modify it under the terms of the GNU General Public License as published by the Free
16
* Software Foundation, either version 3 of the License, or (at your option) any later
17
* version.
18
*
19
* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
20
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21
* PURPOSE. See the GNU General Public License for more details.
22
*
23
* You should have received a copy of the GNU General Public License along with OpenBLT.
24
* If not, see <http://www.gnu.org/licenses/>.
25
*
26
* A special exception to the GPL is included to allow you to distribute a combined work 
27
* that includes OpenBLT without being obliged to provide the source code for any 
28
* proprietary components. The exception text is included at the bottom of the license
29
* file <license.html>.
30
* 
31
* \endinternal
32
****************************************************************************************/
33

    
34
/****************************************************************************************
35
* Include files
36
****************************************************************************************/
37
#include "boot.h"                                /* bootloader generic header          */
38

    
39

    
40
/****************************************************************************************
41
* Defines
42
****************************************************************************************/
43
/** \brief XCP protocol layer version number (16-bit). */
44
#define XCP_VERSION_PROTOCOL_LAYER  (0x0100)
45

    
46
/** \brief XCP transport layer version number (16-bit). */
47
#define XCP_VERSION_TRANSPORT_LAYER (0x0100)
48

    
49
/* XCP packet identifiers */
50
/** \brief Command response packet identifier. */
51
#define XCP_PID_RES                 (0xff)
52
/** \brief Error packet identifier. */            
53
#define XCP_PID_ERR                 (0xfe)
54

    
55
/* XCP error codes */
56
/** \brief Cmd processor synchronization error code. */
57
#define XCP_ERR_CMD_SYNCH           (0x00)
58
/** \brief Command was not executed error code. */
59
#define XCP_ERR_CMD_BUSY            (0x10)
60
/** \brief Unknown or unsupported command error code. */
61
#define XCP_ERR_CMD_UNKNOWN         (0x20)
62
/** \brief Parameter out of range error code. */
63
#define XCP_ERR_OUT_OF_RANGE        (0x22)
64
/** \brief Protected error code. Seed/key required. */
65
#define XCP_ERR_ACCESS_LOCKED       (0x25)
66
/** \brief Cal page not valid error code. */
67
#define XCP_ERR_PAGE_NOT_VALID      (0x26)
68
/** \brief Sequence error code. */
69
#define XCP_ERR_SEQUENCE            (0x29)
70
/** \brief Generic error code. */
71
#define XCP_ERR_GENERIC             (0x31)
72

    
73
/* XCP command codes */
74
/** \brief CONNECT command code. */
75
#define XCP_CMD_CONNECT             (0xff)
76
/** \brief DISCONNECT command code. */
77
#define XCP_CMD_DISCONNECT          (0xfe)
78
/** \brief GET_STATUS command code. */
79
#define XCP_CMD_GET_STATUS          (0xfd)
80
/** \brief SYNCH command code. */
81
#define XCP_CMD_SYNCH               (0xfc)
82
/** \brief GET_ID command code. */
83
#define XCP_CMD_GET_ID              (0xfa)
84
/** \brief GET_SEED command code. */
85
#define XCP_CMD_GET_SEED            (0xf8)
86
/** \brief UNLOCK command code. */
87
#define XCP_CMD_UNLOCK              (0xf7)
88
/** \brief SET_MTA command code. */
89
#define XCP_CMD_SET_MTA             (0xf6)
90
/** \brief UPLOAD command code. */
91
#define XCP_CMD_UPLOAD              (0xf5)
92
/** \brief SHORT_UPLOAD command code. */
93
#define XCP_CMD_SHORT_UPLOAD        (0xf4)
94
/** \brief BUILD_CHECKSUM command code. */
95
#define XCP_CMD_BUILD_CHECKSUM      (0xf3)
96
/** \brief DOWNLOAD command code. */
97
#define XCP_CMD_DOWNLOAD            (0xf0)
98
/** \brief DOWNLOAD_MAX command code. */
99
#define XCP_CMD_DOWLOAD_MAX         (0xee)
100
/** \brief SET_CALPAGE command code. */
101
#define XCP_CMD_SET_CAL_PAGE        (0xeb)
102
/** \brief GET_CALPAGE command code. */
103
#define XCP_CMD_GET_CAL_PAGE        (0xea)
104
/** \brief PROGRAM_START command code. */
105
#define XCP_CMD_PROGRAM_START       (0xd2)
106
/** \brief PROGRAM_CLEAR command code. */
107
#define XCP_CMD_PROGRAM_CLEAR       (0xd1)
108
/** \brief PROGRAM command code. */
109
#define XCP_CMD_PROGRAM             (0xd0)
110
/** \brief PROGRAM_RESET command code. */
111
#define XCP_CMD_PROGRAM_RESET       (0xcf)
112
/** \brief PROGRAM_PREPARE command code. */
113
#define XCP_CMD_PROGRAM_PREPARE     (0xcc)
114
/** \brief PROGRAM_MAX command code. */
115
#define XCP_CMD_PROGRAM_MAX         (0xc9)
116

    
117

    
118
/****************************************************************************************
119
* Type definitions
120
****************************************************************************************/
121
/** \brief Struture type for grouping XCP internal module information. */
122
typedef struct
123
{
124
  blt_int8u  connected;                             /**< connection established                      */
125
#if (BOOT_GATE_ENABLE > 0)
126
  blt_int8u  other_connection;                      /**< connection to other device established      */
127
#endif
128
#if (BOOTLOADER_OF_MAIN_DEVICE > 0)
129
  blt_bool   wasMainConnection;                     /**< connection to serialboot (0x00) established */
130
#endif
131
  blt_int8u  oldData[BOOT_COM_RX_MAX_DATA];         /**< old packet data buffer                      */
132
  blt_int8u  protection;                            /**< protection state                            */
133
  blt_int8u  s_n_k_resource;                        /**< for seed/key sequence                       */
134
  blt_int8u  ctoData[BOOT_COM_RX_MAX_DATA];         /**< cto packet data buffer                      */
135
  blt_int8u  ctoPending;                            /**< cto transmission pending flag               */
136
  blt_int16s ctoLen;                                /**< cto current packet length                   */
137
  blt_int32u mta;                                   /**< memory transfer address                     */
138
} tXcpInfo;
139

    
140

    
141
/****************************************************************************************
142
* Function prototypes
143
****************************************************************************************/
144
/* transport layer specific functions */
145
#if (BOOT_GATE_ENABLE > 0)
146
static void      XcpTransmitPacket(blt_int8u *data, blt_int16s len, blt_bool fromGate);
147
#else
148
static void      XcpTransmitPacket(blt_int8u *data, blt_int16s len);
149
#endif /* BOOT_GATE_ENABLE > 0 */
150

    
151
/* application specific functions */
152
static blt_int8u XcpComputeChecksum(blt_int32u address, blt_int32u length, 
153
                                    blt_int32u *checksum);
154

    
155
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
156
static blt_int8u XcpGetSeed(blt_int8u resource, blt_int8u *seed);
157
static blt_int8u XcpVerifyKey(blt_int8u resource, blt_int8u *key, blt_int8u len);
158
#endif
159

    
160
/* general utility functions */
161
static void XcpProtectResources(void);
162
static void XcpSetCtoError(blt_int8u error);
163

    
164
/* XCP command processors */
165
static void XcpCmdConnect(blt_int8u *data, blt_int16s dataLength);
166
static void XcpCmdDisconnect(blt_int8u *data);
167
static void XcpCmdGetStatus(blt_int8u *data);
168
static void XcpCmdSynch(blt_int8u *data);
169
static void XcpCmdGetId(blt_int8u *data);
170
static void XcpCmdSetMta(blt_int8u *data);
171
static void XcpCmdUpload(blt_int8u *data);
172
static void XcpCmdShortUpload(blt_int8u *data);
173
static void XcpCmdBuildCheckSum(blt_int8u *data);
174
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
175
static void XcpCmdGetSeed(blt_int8u *data);
176
static void XcpCmdUnlock(blt_int8u *data);
177
#endif
178
#if (XCP_RES_CALIBRATION_EN == 1)
179
static void XcpCmdDownload(blt_int8u *data);
180
static void XcpCmdDownloadMax(blt_int8u *data);
181
#endif
182
#if (XCP_RES_PAGING_EN == 1)
183
static void XcpCmdSetCalPage(blt_int8u *data);
184
static void XcpCmdGetCalPage(blt_int8u *data);
185
#endif
186
#if (XCP_RES_PROGRAMMING_EN == 1)
187
static void XcpCmdProgramMax(blt_int8u *data);
188
static void XcpCmdProgram(blt_int8u *data);
189
static void XcpCmdProgramStart(blt_int8u *data);
190
static void XcpCmdProgramClear(blt_int8u *data);
191
static void XcpCmdProgramReset(blt_int8u *data);
192
static void XcpCmdProgramPrepare(blt_int8u *data);
193
#endif
194
static void portedTransmission(blt_int8u *data);
195

    
196

    
197
/****************************************************************************************
198
* Hook functions
199
****************************************************************************************/
200
#if (XCP_RES_PAGING_EN == 1)
201
extern blt_int8u XcpCalSetPageHook(blt_int8u segment, blt_int8u page);
202
extern blt_int8u XcpCalGetPageHook(blt_int8u segment);
203
#endif
204

    
205
#if (XCP_CONNECT_MODE_HOOK_EN == 1)
206
extern blt_bool XcpConnectModeHook(blt_int8u mode);
207
#endif
208

    
209
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
210
extern blt_int8u XcpGetSeedHook(blt_int8u resource, blt_int8u *seed);
211
extern blt_int8u XcpVerifyKeyHook(blt_int8u resource, blt_int8u *key, blt_int8u len);
212
#endif
213

    
214

    
215

    
216
/****************************************************************************************
217
* External functions
218
****************************************************************************************/
219
#if (BOOT_COM_ENABLE == 0)
220
/* in case no internally supported communication interface is used, a custom 
221
 * communication module can be added. In order to use the XCP protocol in the custom
222
 * communication module, this hook function needs to be implemented. In the XCP protocol
223
 * is not needed, then simply remove the xcp.c source from the project.
224
 */
225
extern void XcpTransmitPacketHook(blt_int8u *data, blt_int16u len);
226
#endif
227

    
228

    
229
/****************************************************************************************
230
* Local constants
231
****************************************************************************************/
232
/** \brief String buffer with station id. */
233
static const blt_int8s xcpStationId[] = XCP_STATION_ID_STRING;
234

    
235

    
236
/****************************************************************************************
237
* Local data definitions
238
****************************************************************************************/
239
/** \brief Local variable for storing XCP internal module info. */
240
static tXcpInfo xcpInfo;
241

    
242

    
243
/************************************************************************************//**
244
** \brief     Initializes the XCP driver. Should be called once upon system startup.
245
** \return    none
246
**
247
****************************************************************************************/
248
void XcpInit(void)
249
{
250
  /* reset xcp module info */
251
  xcpInfo.connected = 0;
252
#if (BOOT_GATE_ENABLE > 0)
253
  xcpInfo.other_connection = 0;
254
#endif
255
#if (BOOTLOADER_OF_MAIN_DEVICE > 0)
256
  xcpInfo.wasMainConnection = BLT_FALSE;
257
#endif
258
  xcpInfo.mta = 0;
259
  xcpInfo.ctoPending = 0;
260
  xcpInfo.ctoLen = 0;
261
  xcpInfo.s_n_k_resource = 0;
262
  xcpInfo.protection = 0;
263
} /*** end of XcpInit ***/
264

    
265

    
266
/************************************************************************************//**
267
** \brief     Obtains information about the XCP connection state.
268
** \return    BLT_TRUE is an XCP connection is established, BLT_FALSE otherwise.
269
**
270
****************************************************************************************/
271
blt_bool XcpIsConnected(void)
272
{
273
#if (BOOT_GATE_ENABLE > 0)
274
  if (xcpInfo.connected == 0 && xcpInfo.other_connection == 0)
275
#else
276
  if (xcpInfo.connected == 0)
277
#endif
278
  {
279
    return BLT_FALSE;
280
  }
281
  return BLT_TRUE;
282
} /*** end of XcpIsConnected ***/
283

    
284

    
285
#if (BOOTLOADER_OF_MAIN_DEVICE > 0)
286
/************************************************************************************//**
287
** \brief     Obtains information about the XCP main connection state.
288
** \return    BLT_TRUE is an XCP connection to main was established, BLT_FALSE otherwise.
289
**
290
****************************************************************************************/
291
blt_bool XcpWasConnectedToMain(void)
292
{
293
  return xcpInfo.wasMainConnection;
294
} /*** end of XcpWasConnectedToMain ***/
295
#endif
296

    
297

    
298
/************************************************************************************//**
299
** \brief     Informs the core that a pending packet transmission was completed by 
300
**            the transport layer.
301
** \return    none
302
**
303
****************************************************************************************/
304
void XcpPacketTransmitted(void)
305
{
306
  /* reset packet transmission pending flag */
307
  xcpInfo.ctoPending = 0;
308
} /*** end of XcpPacketTransmitted ***/
309

    
310

    
311
/************************************************************************************//**
312
** \brief     Informs the core that a new packet was received by the transport layer.
313
** \param     data       Pointer to byte buffer with packet data.
314
** \param     dataLength Number of data bytes that need to be transmitted.
315
** \return    none
316
**
317
****************************************************************************************/
318
#if (BOOT_GATE_ENABLE > 0)
319
void XcpPacketReceived(blt_int8u *data, blt_int16s dataLength, blt_bool fromGate)
320
#else
321
void XcpPacketReceived(blt_int8u *data, blt_int16s dataLength)
322
#endif /* BOOT_GATE_ENABLE > 0 */
323
{
324
  /* was this a connect command? */
325
  if (data[0] == XCP_CMD_CONNECT)
326
  {
327
    /* process the connect command */
328
    XcpCmdConnect(data, dataLength);
329
  }
330
  /* only continue if connected */
331
  else if (xcpInfo.connected == 1)
332
  {
333
    switch (data[0])
334
    {
335
      case XCP_CMD_UPLOAD:
336
        XcpCmdUpload(data);
337
        break;
338
      case XCP_CMD_SHORT_UPLOAD:
339
        XcpCmdShortUpload(data);
340
        break;
341
      case XCP_CMD_SET_MTA:
342
        XcpCmdSetMta(data);
343
        break;
344
      case XCP_CMD_BUILD_CHECKSUM:
345
        XcpCmdBuildCheckSum(data);
346
        break;
347
      case XCP_CMD_GET_ID:
348
        XcpCmdGetId(data);
349
        break;
350
      case XCP_CMD_SYNCH:
351
        XcpCmdSynch(data);
352
        break;
353
      case XCP_CMD_GET_STATUS:
354
        XcpCmdGetStatus(data);
355
        break;
356
      case XCP_CMD_DISCONNECT:
357
        XcpCmdDisconnect(data);
358
        break;
359
#if (XCP_RES_CALIBRATION_EN == 1)
360
      case XCP_CMD_DOWNLOAD:
361
        XcpCmdDownload(data);
362
        break;
363
      case XCP_CMD_DOWLOAD_MAX:
364
        XcpCmdDownloadMax(data);
365
        break;
366
#endif
367
#if (XCP_RES_PROGRAMMING_EN == 1)
368
      case XCP_CMD_PROGRAM_MAX:
369
        XcpCmdProgramMax(data);
370
        break;
371
      case XCP_CMD_PROGRAM:
372
        XcpCmdProgram(data);
373
        break;
374
      case XCP_CMD_PROGRAM_START:
375
        XcpCmdProgramStart(data);
376
        break;
377
      case XCP_CMD_PROGRAM_CLEAR:
378
        XcpCmdProgramClear(data);
379
        break;
380
      case XCP_CMD_PROGRAM_RESET:
381
        XcpCmdProgramReset(data);
382
        break;
383
      case XCP_CMD_PROGRAM_PREPARE:
384
        XcpCmdProgramPrepare(data);
385
        break;
386
#endif
387
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
388
      case XCP_CMD_GET_SEED:
389
        XcpCmdGetSeed(data);
390
        break;
391
      case XCP_CMD_UNLOCK:
392
        XcpCmdUnlock(data);
393
        break;
394
#endif
395
#if (XCP_RES_PAGING_EN == 1)
396
      case XCP_CMD_SET_CAL_PAGE:
397
        XcpCmdSetCalPage(data);
398
        break;
399
      case XCP_CMD_GET_CAL_PAGE:
400
        XcpCmdGetCalPage(data);
401
        break;
402
#endif
403
      default:
404
        XcpSetCtoError(XCP_ERR_CMD_UNKNOWN);
405
        break;
406
    }
407
  }
408
#if (BOOT_GATE_ENABLE > 0)
409
  /* check if there is still an other connection */
410
  else if (xcpInfo.other_connection > 0) {
411
    /* Forwarding message */
412
    XcpPacketReceivedForwarding(data, dataLength);
413
    return;
414
  }
415
#endif /* (BOOT_GATE_ENABLE > 0) */
416
  else
417
  {
418
    /* return to make sure response packet is not send because we are not connected */
419
    return;
420
  }
421

    
422
  /* make sure the previous command was completed */
423
  if (xcpInfo.ctoPending == 1)
424
  {
425
    /* command overrun occurred */
426
    XcpSetCtoError(XCP_ERR_CMD_BUSY);
427
  }
428

    
429
  /* send the response if it contains something */
430
  if (xcpInfo.ctoLen > 0)
431
  {
432
    /* set cto packet transmission pending flag */
433
    xcpInfo.ctoPending = 1;
434

    
435
    /* transmit the cto response packet */
436
#if (BOOT_GATE_ENABLE > 0)
437
    XcpTransmitPacket(xcpInfo.ctoData, xcpInfo.ctoLen, fromGate);
438
#else
439
    XcpTransmitPacket(xcpInfo.ctoData, xcpInfo.ctoLen);
440
#endif /* BOOT_GATE_ENABLE > 0 */
441
  }
442

    
443
} /*** end of XcpPacketReceived ***/
444

    
445

    
446

    
447
#if (BOOT_GATE_ENABLE > 0)
448
/************************************************************************************//**
449
** \brief     Forwards the packet to other device.
450
** \param     data       Pointer to the byte buffer with packet data.
451
** \param     dataLength Number of data bytes that need to be transmitted.
452
** \return    none
453
**
454
****************************************************************************************/
455
void XcpPacketReceivedForwarding(blt_int8u *data, blt_int16s dataLength) {
456
  /* save id of connected device */
457
  blt_int8u connectionTo = xcpInfo.other_connection;
458

    
459
  /* proof commands */
460
  switch (data[0]) {
461
    /* if there is a disconnection command, everything else in xcp info
462
     * has to be set on disconnection
463
     */
464
    case XCP_CMD_DISCONNECT:
465
      xcpInfo.other_connection = 0;
466
      xcpInfo.connected = 0;
467
      break;
468
    /* if there is a reset command, it has to be handled like a disconnection
469
     * (above) because other devices shouldn't get the ability to reset
470
     * themselves alone without any general reset command to all devices
471
     */
472
    case XCP_CMD_PROGRAM_RESET:
473
      xcpInfo.other_connection = 0;
474
      xcpInfo.connected = 0;
475
      data[0] = XCP_CMD_DISCONNECT;
476
      break;
477
    default:
478
      break;
479
  }
480

    
481
  xcpInfo.ctoPending = 1;
482
#if (BOOT_DEBUGGING_UART2_ENABLE > 0)
483
  /* send debugging information */
484
  BuildData(&debugDataTCan, data, (blt_int8u)dataLength);
485
#endif
486
  /* send packet direct with CAN */
487
  GateTransmitPacketDirect(data, (blt_int8u)dataLength, connectionTo);
488
} /*** end of XcpPacketReceivedForwarding ***/
489

    
490

    
491
/************************************************************************************//**
492
** \brief     Transmits the packet from gateway to com.
493
** \param     data       Pointer to the byte buffer with packet data.
494
** \param     dataLength Number of data bytes that need to be transmitted.
495
** \return    none
496
**
497
****************************************************************************************/
498
void XcpGatewayPacketReceived(blt_int8u *data, blt_int16s dataLength) {
499
  xcpInfo.ctoPending = 1;
500
#if (BOOT_DEBUGGING_UART2_ENABLE > 0)
501
  /* send debugging information */
502
  BuildData(&debugDataRCan, data, (blt_int8u)dataLength);
503
#endif
504
  /* send packet direct with UART */
505
  ComTransmitPacketDirect(data, (blt_int8u)dataLength);
506
} /*** end of XcpGatewayPacketReceived ***/
507

    
508
#endif /* (BOOT_GATE_ENABLE > 0) */
509

    
510

    
511

    
512
/************************************************************************************//**
513
** \brief     Transmits the packet using the xcp transport layer.
514
** \param     data Pointer to the byte buffer with packet data.
515
** \param     len  Number of data bytes that need to be transmitted.
516
** \return    none
517
**
518
****************************************************************************************/
519

    
520
#if (BOOT_GATE_ENABLE > 0)
521
static void XcpTransmitPacket(blt_int8u *data, blt_int16s len, blt_bool fromGate)
522
#else
523
static void XcpTransmitPacket(blt_int8u *data, blt_int16s len)
524
#endif /* BOOT_GATE_ENABLE > 0 */
525
{
526
  /* submit packet to the communication interface for transmission */
527
#if (BOOT_GATE_ENABLE > 0)
528
  if (fromGate == BLT_TRUE) {
529
    GateTransmitPacket(data, len);
530
    return;
531
  }
532
#endif /* BOOT_GATE_ENABLE > 0 */
533
#if (BOOT_COM_ENABLE == 0)
534
  XcpTransmitPacketHook(data, len);
535
#else
536
  ComTransmitPacket(data, len);
537
#endif
538
  
539
} /*** end of XcpTransmitPacket ***/
540

    
541

    
542
/************************************************************************************//**
543
** \brief     Called by the BUILD_CHECKSUM command to perform a checksum calculation
544
**            over the specified memory region.
545
** \param     address   The start address of the memory region.
546
** \param     length    Length of the memory region in bytes.
547
** \param     checksum  Pointer to where the calculated checksum is to be stored.
548
** \return    Checksum type that was used during the checksum calculation.
549
**
550
****************************************************************************************/
551
static blt_int8u XcpComputeChecksum(blt_int32u address, blt_int32u length,
552
                                    blt_int32u *checksum)
553
{
554
  blt_int8u cs = 0;
555

    
556
  /* this example computes the checksum using the add byte to byte algorithm */
557
  while (length-- > 0)
558
  {
559
    cs += *((blt_int8u*)(blt_addr)address);
560
    address++;
561
  }
562

    
563
  *checksum = cs;
564

    
565
  return XCP_CS_ADD11;
566
} /*** end of XcpComputeChecksum ***/
567

    
568

    
569
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
570
/************************************************************************************//**
571
** \brief     Provides a seed to the XCP master that will be used for the key 
572
**            generation when the master attempts to unlock the specified resource. 
573
**            Called by the GET_SEED command.
574
** \param     resource  Resource that the seed if requested for (XCP_RES_XXX).
575
** \param     seed      Pointer to byte buffer wher the seed will be stored.
576
** \return    Length of the seed in bytes.
577
**
578
****************************************************************************************/
579
static blt_int8u XcpGetSeed(blt_int8u resource, blt_int8u *seed)
580
{
581
  /* pass request on to the application through a hook function */
582
  return XcpGetSeedHook(resource, seed);
583
} /*** end of XcpGetSeed ***/
584

    
585

    
586
/************************************************************************************//**
587
** \brief     Called by the UNLOCK command and checks if the key to unlock the 
588
**            specified resource was correct. If so, then the resource protection 
589
**            will be removed.
590
** \param     resource  resource to unlock (XCP_RES_XXX).
591
** \param     key       pointer to the byte buffer holding the key.
592
** \param     len       length of the key in bytes.
593
** \return    1 if the key was correct, 0 otherwise.
594
**
595
****************************************************************************************/
596
static blt_int8u XcpVerifyKey(blt_int8u resource, blt_int8u *key, blt_int8u len)
597
{
598
  /* pass request on to the application through a hook function */
599
  return XcpVerifyKeyHook(resource, key, len);
600
} /*** end of XcpVerifyKey ***/
601
#endif /* XCP_SEED_KEY_PROTECTION_EN == 1 */
602

    
603

    
604
/************************************************************************************//**
605
** \brief     Utility function to protects all the available resources.
606
** \return    none
607
**
608
****************************************************************************************/
609
static void XcpProtectResources(void)
610
{
611
  xcpInfo.protection = 0;
612

    
613
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
614
  #if (XCP_RES_CALIBRATION_EN == 1)
615
  xcpInfo.protection |= XCP_RES_CALPAG;
616
  #endif
617

    
618
  #if (XCP_RES_PAGING_EN == 1)
619
  xcpInfo.protection |= XCP_RES_CALPAG;
620
  #endif
621

    
622
  #if (XCP_RES_PROGRAMMING_EN == 1)
623
  xcpInfo.protection |= XCP_RES_PGM;
624
  #endif
625

    
626
  #if (XCP_RES_DATA_ACQUISITION_EN == 1)
627
  xcpInfo.protection |= XCP_RES_DAQ;
628
  #endif
629

    
630
  #if (XCP_RES_DATA_STIMULATION_EN == 1)
631
  xcpInfo.protection |= XCP_RES_STIM;
632
  #endif
633
#endif /* XCP_SEED_KEY_PROTECTION_EN == 1 */
634
} /*** end of XcpProtectResources ***/
635

    
636

    
637
/************************************************************************************//**
638
** \brief     Prepares the cto packet data for the specified error.
639
** \param     error XCP error code (XCP_ERR_XXX).
640
** \return    none
641
**
642
****************************************************************************************/
643
static void XcpSetCtoError(blt_int8u error)
644
{
645
  /* prepare the error packet */
646
  xcpInfo.ctoData[0] = XCP_PID_ERR;
647
  xcpInfo.ctoData[1] = error;
648
  xcpInfo.ctoLen = 2;
649
} /*** end of XcpSetCtoError ***/
650

    
651

    
652
/************************************************************************************//**
653
** \brief     XCP command processor function which handles the CONNECT command as
654
**            defined by the protocol.
655
** \param     data Pointer to a byte buffer with the packet data.
656
** \return    none
657
**
658
****************************************************************************************/
659
static void XcpCmdConnect(blt_int8u *data, blt_int16s dataLength)
660
{
661
  /* suppress compiler warning for unused parameter */
662
  data = data;
663

    
664
  #if (BOOT_FILE_SYS_ENABLE > 0)
665
  /* reject the connection if the file module is not idle. this means that a firmware
666
   * update from the locally attached storage is in progress
667
   */
668
  if (FileIsIdle() == BLT_FALSE)
669
  {
670
    /* command not processed because we are busy */
671
    XcpSetCtoError(XCP_ERR_CMD_BUSY);
672
    return;
673
  }
674
  #endif  
675
  
676
//  #if (XCP_CONNECT_MODE_HOOK_EN == 1)
677
//  /* pass on the mode to a application specific hook function. This function can determine
678
//   * is the mode is supported or not. A return value of BLT_FALSE causes the CONNECT command
679
//   * to be ignored. Note that this mode could potentially be used to specify a node ID in a
680
//   * multi XCP slave system.
681
//   */
682
//  if (XcpConnectModeHook(data[1]) == BLT_FALSE)
683
//  {
684
//    /* set the response length to 0 to suppress it */
685
//    xcpInfo.ctoLen = 0;
686
//    return;
687
//  }
688
//  #endif
689

    
690
  /* enable resource protection */
691
  XcpProtectResources();
692

    
693
  /* indicate that the connection is established */
694
#if (BOOT_GATE_ENABLE > 0)
695
  if (data[1] == 0x00 || data[1] == BOOT_COM_DEVICE_ID) {
696
#endif
697
    xcpInfo.connected = 1;
698
#if (BOOT_GATE_ENABLE > 0)
699
    xcpInfo.other_connection = 0;
700
#endif
701
#if (BOOTLOADER_OF_MAIN_DEVICE > 0)
702
    if (data[1] == 0x00) {
703
      xcpInfo.wasMainConnection = BLT_TRUE;
704
    }
705
#endif
706

    
707
    /* set packet id to command response packet */
708
    xcpInfo.ctoData[0] = XCP_PID_RES;
709

    
710
    /* report available resources */
711
    xcpInfo.ctoData[1] = 0;
712
#if (XCP_RES_CALIBRATION_EN == 1)
713
    xcpInfo.ctoData[1] |= XCP_RES_CALPAG;
714
#endif
715

    
716
#if (XCP_RES_PAGING_EN == 1)
717
    xcpInfo.ctoData[1] |= XCP_RES_CALPAG;
718
#endif
719

    
720
#if (XCP_RES_PROGRAMMING_EN == 1)
721
    xcpInfo.ctoData[1] |= XCP_RES_PGM;
722
#endif
723

    
724
#if (XCP_RES_DATA_ACQUISITION_EN == 1)
725
    xcpInfo.ctoData[1] |= XCP_RES_DAQ;
726
#endif
727

    
728
#if (XCP_RES_DATA_STIMULATION_EN == 1)
729
    xcpInfo.ctoData[1] |= XCP_RES_STIM;
730
#endif
731

    
732
    /* report communication mode info. only byte granularity is supported */
733
    xcpInfo.ctoData[2] = 0;
734
    /* configure for motorola or intel byte ordering */
735
    xcpInfo.ctoData[2] |= XCP_MOTOROLA_FORMAT;
736

    
737
    /* report max cto data length */
738
    xcpInfo.ctoData[3] = (blt_int8u)XCP_CTO_PACKET_LEN;
739

    
740
    /* report max dto data length */
741
#if (XCP_MOTOROLA_FORMAT == 0)
742
    xcpInfo.ctoData[4] = (blt_int8u)XCP_DTO_PACKET_LEN;
743
    xcpInfo.ctoData[5] = (blt_int8u)(XCP_DTO_PACKET_LEN >> 8);
744
#else
745
    xcpInfo.ctoData[4] = (blt_int8u)(XCP_DTO_PACKET_LEN >> 8);
746
    xcpInfo.ctoData[5] = (blt_int8u)XCP_DTO_PACKET_LEN;
747
#endif
748

    
749
    /* report msb of protocol layer version number */
750
    xcpInfo.ctoData[6] = XCP_VERSION_PROTOCOL_LAYER >> 8;
751

    
752
    /* report msb of transport layer version number */
753
    xcpInfo.ctoData[7] = XCP_VERSION_TRANSPORT_LAYER >> 8;
754

    
755
    /* set packet length */
756
    xcpInfo.ctoLen = 8;
757

    
758
#if (BOOT_GATE_ENABLE > 0)
759
  } else {
760
    xcpInfo.other_connection = data[1];
761
    xcpInfo.connected = 0;
762
    XcpPacketReceivedForwarding(data, dataLength);
763
    xcpInfo.ctoLen = 0;
764
    // TODO better cancel definition!
765
//    if (xcpInfo.ctoLen == 0) {
766
//      xcpInfo.other_connection = 0;
767
//    }
768
  }
769
#endif
770

    
771
  
772
} /*** end of XcpCmdConnect ***/
773

    
774

    
775
/************************************************************************************//**
776
** \brief     XCP command processor function which handles the DISCONNECT command as
777
**            defined by the protocol.
778
** \param     data Pointer to a byte buffer with the packet data.
779
** \return    none
780
**
781
****************************************************************************************/
782
static void XcpCmdDisconnect(blt_int8u *data)
783
{
784
  /* suppress compiler warning for unused parameter */
785
  data = data;
786

    
787
  /* indicate that the xcp connection is disconnected */
788
  xcpInfo.connected = 0;
789
#if (BOOT_GATE_ENABLE > 0)
790
  xcpInfo.other_connection = 0;
791
#endif
792

    
793
  /* enable resource protection */
794
  XcpProtectResources();
795

    
796
  /* set packet id to command response packet */
797
  xcpInfo.ctoData[0] = XCP_PID_RES;
798

    
799
  /* set packet length */
800
  xcpInfo.ctoLen = 1;
801

    
802
} /*** end of XcpCmdDisconnect ***/
803

    
804

    
805
/************************************************************************************//**
806
** \brief     XCP command processor function which handles the GET_STATUS command as
807
**            defined by the protocol.
808
** \param     data Pointer to a byte buffer with the packet data.
809
** \return    none
810
**
811
****************************************************************************************/
812
static void XcpCmdGetStatus(blt_int8u *data)
813
{
814
  /* suppress compiler warning for unused parameter */
815
  data = data;
816

    
817
  /* set packet id to command response packet */
818
  xcpInfo.ctoData[0] = XCP_PID_RES;
819

    
820
  /* report session status */
821
  xcpInfo.ctoData[1] = 0;
822

    
823
  /* report current resource protection status */
824
  xcpInfo.ctoData[2] = xcpInfo.protection;
825

    
826
  /* reset reserved and session configuration id values */
827
  xcpInfo.ctoData[3] = 0;
828
  xcpInfo.ctoData[4] = 0;
829
  xcpInfo.ctoData[5] = 0;
830

    
831
  /* set packet length */
832
  xcpInfo.ctoLen = 6;
833
} /*** end of XcpCmdGetStatus ***/
834

    
835

    
836
/************************************************************************************//**
837
** \brief     XCP command processor function which handles the SYNCH command as
838
**            defined by the protocol.
839
** \param     data Pointer to a byte buffer with the packet data.
840
** \return    none
841
**
842
****************************************************************************************/
843
static void XcpCmdSynch(blt_int8u *data)
844
{
845
  /* suppress compiler warning for unused parameter */
846
  data = data;
847

    
848
  /* synch requires a negative response */
849
  XcpSetCtoError(XCP_ERR_CMD_SYNCH);
850
} /*** end of XcpCmdSynch ***/
851

    
852

    
853
/************************************************************************************//**
854
** \brief     XCP command processor function which handles the GET_ID command as
855
**            defined by the protocol.
856
** \param     data Pointer to a byte buffer with the packet data.
857
** \return    none
858
**
859
****************************************************************************************/
860
static void XcpCmdGetId(blt_int8u *data)
861
{
862
  /* suppress compiler warning for unused parameter */
863
  data = data;
864

    
865
  /* set packet id to command response packet */
866
  xcpInfo.ctoData[0] = XCP_PID_RES;
867

    
868
  /* point mta to start of station id string */
869
  xcpInfo.mta = (blt_int32u)&xcpStationId[0];
870

    
871
  /* set station id mode to 0 */
872
  xcpInfo.ctoData[1] = 0;
873

    
874
  /* reset reserved values */
875
  xcpInfo.ctoData[2] = 0;
876
  xcpInfo.ctoData[3] = 0;
877

    
878
  /* store station id length (excl. null termination) for response packet */
879
  *(blt_int32u*)&xcpInfo.ctoData[4] = (sizeof(xcpStationId)/sizeof(xcpStationId[0])) - 1;
880

    
881
  /* set packet length */
882
  xcpInfo.ctoLen = 8;
883
} /*** end of XcpCmdGetId ***/
884

    
885

    
886
/************************************************************************************//**
887
** \brief     XCP command processor function which handles the SET_MTA command as
888
**            defined by the protocol.
889
** \param     data Pointer to a byte buffer with the packet data.
890
** \return    none
891
**
892
****************************************************************************************/
893
static void XcpCmdSetMta(blt_int8u *data)
894
{
895
  /* set packet id to command response packet */
896
  xcpInfo.ctoData[0] = XCP_PID_RES;
897

    
898
  /* update mta. current implementation ignores address extension */
899
  xcpInfo.mta = *(blt_int32u*)&data[4];
900

    
901
  /* set packet length */
902
  xcpInfo.ctoLen = 1;
903
} /*** end of XcpCmdSetMta ***/
904

    
905

    
906
/************************************************************************************//**
907
** \brief     XCP command processor function which handles the UPLOAD command as
908
**            defined by the protocol.
909
** \param     data Pointer to a byte buffer with the packet data.
910
** \return    none
911
**
912
****************************************************************************************/
913
static void XcpCmdUpload(blt_int8u *data)
914
{
915
  /* validate length of upload request */
916
  if (data[1] > (XCP_CTO_PACKET_LEN-1))
917
  {
918
    /* requested data length is too long */
919
    XcpSetCtoError(XCP_ERR_OUT_OF_RANGE);
920
    return;
921
  }
922

    
923
  /* copy the data from memory to the data packet */
924
  CpuMemCopy(((blt_addr)(blt_int32u)&xcpInfo.ctoData[1]),(blt_addr)xcpInfo.mta, data[1]);
925

    
926
  /* set packet id to command response packet */
927
  xcpInfo.ctoData[0] = XCP_PID_RES;
928

    
929
  /* post increment the mta */
930
  xcpInfo.mta += data[1];
931

    
932
  /* set packet length */
933
  xcpInfo.ctoLen = data[1]+1;
934
} /*** end of XcpCmdUpload ***/
935

    
936

    
937
/************************************************************************************//**
938
** \brief     XCP command processor function which handles the SHORT_UPLOAD command as
939
**            defined by the protocol.
940
** \param     data Pointer to a byte buffer with the packet data.
941
** \return    none
942
**
943
****************************************************************************************/
944
static void XcpCmdShortUpload(blt_int8u *data)
945
{
946
  /* validate length of upload request */
947
  if (data[1] > (XCP_CTO_PACKET_LEN-1))
948
  {
949
    /* requested data length is too long */
950
    XcpSetCtoError(XCP_ERR_OUT_OF_RANGE);
951
    return;
952
  }
953

    
954
  /* update mta. current implementation ignores address extension */
955
  xcpInfo.mta = *(blt_int32u*)&data[4];
956

    
957
  /* copy the data from memory to the data packet */
958
  CpuMemCopy((blt_addr)((blt_int32u)&xcpInfo.ctoData[1]),(blt_addr)xcpInfo.mta, data[1]);
959
  /* set packet id to command response packet */
960
  xcpInfo.ctoData[0] = XCP_PID_RES;
961

    
962
  /* post increment the mta */
963
  xcpInfo.mta += data[1];
964

    
965
  /* set packet length */
966
  xcpInfo.ctoLen = data[1]+1;
967
} /*** end of XcpCmdShortUpload ***/
968

    
969

    
970
#if (XCP_RES_CALIBRATION_EN == 1)
971
/************************************************************************************//**
972
** \brief     XCP command processor function which handles the DOWNLOAD command as
973
**            defined by the protocol.
974
** \param     data Pointer to a byte buffer with the packet data.
975
** \return    none
976
**
977
****************************************************************************************/
978
static void XcpCmdDownload(blt_int8u *data)
979
{
980
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
981
  /* check if CAL_PAG resource is unlocked */
982
  if ((xcpInfo.protection & XCP_RES_CALPAG) != 0)
983
  {
984
    /* resource is locked. use seed/key sequence to unlock */
985
    XcpSetCtoError(XCP_ERR_ACCESS_LOCKED);
986
    return;
987
  }
988
#endif
989

    
990
  /* validate length of download request */
991
  if (data[1] > (XCP_CTO_PACKET_LEN-2))
992
  {
993
    /* requested data length is too long */
994
    XcpSetCtoError(XCP_ERR_OUT_OF_RANGE);
995
    return;
996
  }
997

    
998
  /* copy the data from the data packet to memory */
999
  CpuMemCopy((blt_addr)xcpInfo.mta, (blt_addr)((blt_int32u)&data[2]), data[1]);
1000
  /* set packet id to command response packet */
1001
  xcpInfo.ctoData[0] = XCP_PID_RES;
1002

    
1003
  /* post increment the mta */
1004
  xcpInfo.mta += data[1];
1005

    
1006
  /* set packet length */
1007
  xcpInfo.ctoLen = 1;
1008
} /*** end of XcpCmdDownload ***/
1009

    
1010

    
1011
/************************************************************************************//**
1012
** \brief     XCP command processor function which handles the DOWNLOAD_MAX command as
1013
**            defined by the protocol.
1014
** \param     data Pointer to a byte buffer with the packet data.
1015
** \return    none
1016
**
1017
****************************************************************************************/
1018
static void XcpCmdDownloadMax(blt_int8u *data)
1019
{
1020
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
1021
  /* check if CAL_PAG resource is unlocked */
1022
  if ((xcpInfo.protection & XCP_RES_CALPAG) != 0)
1023
  {
1024
    /* resource is locked. use seed/key sequence to unlock */
1025
    XcpSetCtoError(XCP_ERR_ACCESS_LOCKED);
1026
    return;
1027
  }
1028
#endif
1029

    
1030
  /* copy the data from the data packet to memory */
1031
  CpuMemCopy((blt_addr)xcpInfo.mta, (blt_addr)((blt_int32u)&data[1]), \
1032
              XCP_CTO_PACKET_LEN-1);
1033

    
1034
  /* set packet id to command response packet */
1035
  xcpInfo.ctoData[0] = XCP_PID_RES;
1036

    
1037
  /* post increment the mta */
1038
  xcpInfo.mta += XCP_CTO_PACKET_LEN-1;
1039

    
1040
  /* set packet length */
1041
  xcpInfo.ctoLen = 1;
1042
} /*** end of XcpCmdDownloadMax ***/
1043
#endif /* XCP_RES_CALIBRATION_EN == 1 */
1044

    
1045

    
1046
/************************************************************************************//**
1047
** \brief     XCP command processor function which handles the BUILD_CHECKSUM command as
1048
**            defined by the protocol.
1049
** \param     data Pointer to a byte buffer with the packet data.
1050
** \return    none
1051
**
1052
****************************************************************************************/
1053
static void XcpCmdBuildCheckSum(blt_int8u *data)
1054
{
1055
  /* set packet id to command response packet */
1056
  xcpInfo.ctoData[0] = XCP_PID_RES;
1057

    
1058
  /* obtain checksum and checksum type */
1059
  xcpInfo.ctoData[1] = XcpComputeChecksum(xcpInfo.mta, *(blt_int32u*)&data[4],
1060
                                          (blt_int32u*)&xcpInfo.ctoData[4]);
1061

    
1062
  /* initialize reserved parameters */
1063
  xcpInfo.ctoData[2] = 0;
1064
  xcpInfo.ctoData[3] = 0;
1065

    
1066
  /* set packet length */
1067
  xcpInfo.ctoLen = 8;
1068
} /*** end of XcpCmdBuildCheckSum ***/
1069

    
1070

    
1071
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
1072
/************************************************************************************//**
1073
** \brief     XCP command processor function which handles the GET_SEED command as
1074
**            defined by the protocol.
1075
** \param     data Pointer to a byte buffer with the packet data.
1076
** \return    none
1077
**
1078
****************************************************************************************/
1079
static void XcpCmdGetSeed(blt_int8u *data)
1080
{
1081
  blt_int8u resourceOK;
1082

    
1083
  /* init resource check variable as if an illegal resource is requested */
1084
  resourceOK = 0;
1085

    
1086
  /* check if calibration/paging resource is requested for seed/key and make
1087
   * sure this is the only requested resource
1088
   */
1089
  if (((data[2] & XCP_RES_CALPAG) > 0) && ((data[2] & ~XCP_RES_CALPAG) == 0))
1090
  {
1091
    resourceOK = 1;
1092
  }
1093

    
1094
  /* check if programming resource is requested for seed/key and make
1095
   * sure this is the only requested resource
1096
   */
1097
  if (((data[2] & XCP_RES_PGM) > 0) && ((data[2] & ~XCP_RES_PGM) == 0))
1098
  {
1099
    resourceOK = 1;
1100
  }
1101

    
1102
  /* check if data acquisition resource is requested for seed/key and make
1103
   * sure this is the only requested resource
1104
   */
1105
  if (((data[2] & XCP_RES_DAQ) > 0) && ((data[2] & ~XCP_RES_DAQ) == 0))
1106
  {
1107
    resourceOK = 1;
1108
  }
1109

    
1110
  /* check if data stimulation resource is requested for seed/key and make
1111
   * sure this is the only requested resource
1112
   */
1113
  if (((data[2] & XCP_RES_STIM) > 0) && ((data[2] & ~XCP_RES_STIM) == 0))
1114
  {
1115
    resourceOK = 1;
1116
  }
1117

    
1118
  /* now process the resource validation */
1119
  if (resourceOK == 0)
1120
  {
1121
    XcpSetCtoError(XCP_ERR_OUT_OF_RANGE);
1122
    return;
1123
  }
1124

    
1125
  /* store resource for which the seed/key sequence is started */
1126
  xcpInfo.s_n_k_resource = data[2];
1127

    
1128
  /* set packet id to command response packet */
1129
  xcpInfo.ctoData[0] = XCP_PID_RES;
1130

    
1131
  /* request the seed from the application */
1132
  xcpInfo.ctoData[1] = XcpGetSeed(xcpInfo.s_n_k_resource, &xcpInfo.ctoData[2]);
1133

    
1134
  /* seed cannot be longer than XCP_CTO_PACKET_LEN-2 */
1135
  if (xcpInfo.ctoData[1] > (XCP_CTO_PACKET_LEN-2))
1136
  {
1137
    /* seed length length is too long */
1138
    XcpSetCtoError(XCP_ERR_OUT_OF_RANGE);
1139
    return;
1140
  }
1141

    
1142
  /* set packet length */
1143
  xcpInfo.ctoLen = xcpInfo.ctoData[1] + 2;
1144
} /*** end of XcpCmdGetSeed ***/
1145

    
1146

    
1147
/************************************************************************************//**
1148
** \brief     XCP command processor function which handles the UNLOCK command as
1149
**            defined by the protocol.
1150
** \param     data Pointer to a byte buffer with the packet data.
1151
** \return    none
1152
**
1153
****************************************************************************************/
1154
static void XcpCmdUnlock(blt_int8u *data)
1155
{
1156
  /* key cannot be longer than XCP_CTO_PACKET_LEN-2 */
1157
  if (data[1] > (XCP_CTO_PACKET_LEN-2))
1158
  {
1159
    /* key is too long incorrect */
1160
    XcpSetCtoError(XCP_ERR_SEQUENCE);
1161
    return;
1162
  }
1163

    
1164
  /* verify the key */
1165
  if (XcpVerifyKey(xcpInfo.s_n_k_resource, &data[2], data[1]) == 0)
1166
  {
1167
    /* invalid key so inform the master and do a disconnect */
1168
    XcpSetCtoError(XCP_ERR_ACCESS_LOCKED);
1169

    
1170
    /* indicate that the xcp connection is disconnected */
1171
    xcpInfo.connected = 0;
1172

    
1173
    /* enable resource protection */
1174
    XcpProtectResources();
1175

    
1176
    return;
1177
  }
1178

    
1179
  /* key correct so unlock the resource */
1180
  xcpInfo.protection &= ~xcpInfo.s_n_k_resource;
1181

    
1182
  /* reset seed/key resource variable for possible next unlock */
1183
  xcpInfo.s_n_k_resource = 0;
1184

    
1185
  /* set packet id to command response packet */
1186
  xcpInfo.ctoData[0] = XCP_PID_RES;
1187

    
1188
  /* report the current resource protection */
1189
  xcpInfo.ctoData[1] = xcpInfo.protection;
1190

    
1191
  /* set packet length */
1192
  xcpInfo.ctoLen = 2;
1193
} /*** end of XcpCmdUnlock ***/
1194
#endif /* XCP_SEED_KEY_PROTECTION_EN == 1 */
1195

    
1196

    
1197
#if (XCP_RES_PAGING_EN == 1)
1198
/************************************************************************************//**
1199
** \brief     XCP command processor function which handles the SET_CAL_PAGE command as
1200
**            defined by the protocol.
1201
** \param     data Pointer to a byte buffer with the packet data.
1202
** \return    none
1203
**
1204
****************************************************************************************/
1205
static void XcpCmdSetCalPage(blt_int8u *data)
1206
{
1207
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
1208
  /* check if CAL_PAG resource is unlocked */
1209
  if ((xcpInfo.protection & XCP_RES_CALPAG) == XCP_RES_CALPAG)
1210
  {
1211
    /* resource is locked. use seed/key sequence to unlock */
1212
    XcpSetCtoError(XCP_ERR_ACCESS_LOCKED);
1213
    return;
1214
  }
1215
#endif
1216

    
1217
  /* select the page. note that the mode parameter is ignored */
1218
  if (XcpCalSetPageHook(data[2], data[3]) == 0)
1219
  {
1220
    /* calibration page could not be selected */
1221
    XcpSetCtoError(XCP_ERR_PAGE_NOT_VALID);
1222
    return;
1223
  }
1224

    
1225
  /* set packet id to command response packet */
1226
  xcpInfo.ctoData[0] = XCP_PID_RES;
1227

    
1228
  /* set packet length */
1229
  xcpInfo.ctoLen = 1;
1230
} /*** end of XcpCmdSetCalPage ***/
1231

    
1232

    
1233
/************************************************************************************//**
1234
** \brief     XCP command processor function which handles the GET_CAL_PAGE command as
1235
**            defined by the protocol.
1236
** \param     data Pointer to a byte buffer with the packet data.
1237
** \return    none
1238
**
1239
****************************************************************************************/
1240
static void XcpCmdGetCalPage(blt_int8u *data)
1241
{
1242
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
1243
  /* check if CAL_PAG resource is unlocked */
1244
  if ((xcpInfo.protection & XCP_RES_CALPAG) == XCP_RES_CALPAG)
1245
  {
1246
    /* resource is locked. use seed/key sequence to unlock */
1247
    XcpSetCtoError(XCP_ERR_ACCESS_LOCKED);
1248
    return;
1249
  }
1250
#endif
1251

    
1252
  /* set packet id to command response packet */
1253
  xcpInfo.ctoData[0] = XCP_PID_RES;
1254

    
1255
  /* initialize reserved parameters */
1256
  xcpInfo.ctoData[1] = 0;
1257
  xcpInfo.ctoData[2] = 0;
1258

    
1259
  /* store the calibration page */
1260
  xcpInfo.ctoData[3] = XcpCalGetPageHook(data[2]);
1261

    
1262
  /* set packet length */
1263
  xcpInfo.ctoLen = 4;
1264
} /*** end of XcpCmdGetCalPage ***/
1265
#endif /* XCP_RES_PAGING_EN == 1 */
1266

    
1267

    
1268
#if (XCP_RES_PROGRAMMING_EN == 1)
1269
/************************************************************************************//**
1270
** \brief     XCP command processor function which handles the PROGRAM_START command as
1271
**            defined by the protocol.
1272
** \param     data Pointer to a byte buffer with the packet data.
1273
** \return    none
1274
**
1275
****************************************************************************************/
1276
static void XcpCmdProgramStart(blt_int8u *data)
1277
{
1278
  /* suppress compiler warning for unused parameter */
1279
  data = data;
1280

    
1281
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
1282
  /* check if PGM resource is unlocked */
1283
  if ((xcpInfo.protection & XCP_RES_PGM) == XCP_RES_PGM)
1284
  {
1285
    /* resource is locked. use seed/key sequence to unlock */
1286
    XcpSetCtoError(XCP_ERR_ACCESS_LOCKED);
1287
    return;
1288
  }
1289
#endif
1290

    
1291
  /* set packet id to command response packet */
1292
  xcpInfo.ctoData[0] = XCP_PID_RES;
1293

    
1294
  /* initialize reserved parameter */
1295
  xcpInfo.ctoData[1] = 0;
1296

    
1297
  /* no special communication mode supported during programming */
1298
  xcpInfo.ctoData[2] = 0;
1299

    
1300
  /* cto packet length stays the same during programming */
1301
  xcpInfo.ctoData[3] = (blt_int8u)XCP_CTO_PACKET_LEN;
1302

    
1303
  /* no block size, st-min time, or queue size supported */
1304
  xcpInfo.ctoData[4] = 0;
1305
  xcpInfo.ctoData[5] = 0;
1306
  xcpInfo.ctoData[6] = 0;
1307

    
1308
  /* set packet length */
1309
  xcpInfo.ctoLen = 7;
1310
} /*** end of XcpCmdProgramStart ***/
1311

    
1312

    
1313
/************************************************************************************//**
1314
** \brief     XCP command processor function which handles the PROGRAM_MAX command as
1315
**            defined by the protocol.
1316
** \param     data Pointer to a byte buffer with the packet data.
1317
** \return    none
1318
**
1319
****************************************************************************************/
1320
static void XcpCmdProgramMax(blt_int8u *data)
1321
{
1322
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
1323
  /* check if PGM resource is unlocked */
1324
  if ((xcpInfo.protection & XCP_RES_PGM) == XCP_RES_PGM)
1325
  {
1326
    /* resource is locked. use seed/key sequence to unlock */
1327
    XcpSetCtoError(XCP_ERR_ACCESS_LOCKED);
1328
    return;
1329
  }
1330
#endif
1331

    
1332
  /* program the data */
1333
  if (NvmWrite((blt_addr)xcpInfo.mta, XCP_CTO_PACKET_LEN-1, &data[1]) == 0)
1334
  {
1335
    /* error occurred during programming */
1336
    XcpSetCtoError(XCP_ERR_GENERIC);
1337
    return;
1338
  }
1339

    
1340
  /* set packet id to command response packet */
1341
  xcpInfo.ctoData[0] = XCP_PID_RES;
1342

    
1343
  /* post increment the mta */
1344
  xcpInfo.mta += XCP_CTO_PACKET_LEN-1;
1345

    
1346
  /* set packet length */
1347
  xcpInfo.ctoLen = 1;
1348
} /*** end of XcpCmdProgramMax ***/
1349

    
1350

    
1351
/************************************************************************************//**
1352
** \brief     XCP command processor function which handles the PROGRAM command as
1353
**            defined by the protocol.
1354
** \param     data Pointer to a byte buffer with the packet data.
1355
** \return    none
1356
**
1357
****************************************************************************************/
1358
static void XcpCmdProgram(blt_int8u *data)
1359
{
1360
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
1361
  /* check if PGM resource is unlocked */
1362
  if ((xcpInfo.protection & XCP_RES_PGM) == XCP_RES_PGM)
1363
  {
1364
    /* resource is locked. use seed/key sequence to unlock */
1365
    XcpSetCtoError(XCP_ERR_ACCESS_LOCKED);
1366
    return;
1367
  }
1368
#endif
1369

    
1370
  /* validate length of download request */
1371
  if (data[1] > (XCP_CTO_PACKET_LEN-2))
1372
  {
1373
    /* requested data length is too long */
1374
    XcpSetCtoError(XCP_ERR_OUT_OF_RANGE);
1375
    return;
1376
  }
1377

    
1378
  /* set packet id to command response packet */
1379
  xcpInfo.ctoData[0] = XCP_PID_RES;
1380

    
1381
  /* set packet length */
1382
  xcpInfo.ctoLen = 1;
1383

    
1384
  /* end of programming sequence (datasize is 0)? */
1385
  if (data[1] == 0)
1386
  {
1387
    /* call erase/programming cleanup routine */
1388
    if (NvmDone() == BLT_FALSE)
1389
    {
1390
      /* error occurred while finishing up programming */
1391
      XcpSetCtoError(XCP_ERR_GENERIC);
1392
    }
1393
    return;
1394
  }
1395
  /* program the data */
1396
  if (NvmWrite((blt_addr)xcpInfo.mta, data[1], &data[2]) == 0)
1397
  {
1398
    /* error occurred during programming */
1399
    XcpSetCtoError(XCP_ERR_GENERIC);
1400
    return;
1401
  }
1402

    
1403
  /* post increment the mta */
1404
  xcpInfo.mta += data[1];
1405
} /*** end of XcpCmdProgram ***/
1406

    
1407

    
1408
/************************************************************************************//**
1409
** \brief     XCP command processor function which handles the PROGRAM_CLEAR command as
1410
**            defined by the protocol.
1411
** \param     data Pointer to a byte buffer with the packet data.
1412
** \return    none
1413
**
1414
****************************************************************************************/
1415
static void XcpCmdProgramClear(blt_int8u *data)
1416
{
1417
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
1418
  /* check if PGM resource is unlocked */
1419
  if ((xcpInfo.protection & XCP_RES_PGM) == XCP_RES_PGM)
1420
  {
1421
    /* resource is locked. use seed/key sequence to unlock */
1422
    XcpSetCtoError(XCP_ERR_ACCESS_LOCKED);
1423
    return;
1424
  }
1425
#endif
1426

    
1427
  /* erase the memory */
1428
  if (NvmErase((blt_addr)xcpInfo.mta, *(blt_int32u*)&data[4]) == 0)
1429
  {
1430
    /* error occurred during erasure */
1431
    XcpSetCtoError(XCP_ERR_GENERIC);
1432
    return;
1433
  }
1434

    
1435
  /* set packet id to command response packet */
1436
  xcpInfo.ctoData[0] = XCP_PID_RES;
1437

    
1438
  /* set packet length */
1439
  xcpInfo.ctoLen = 1;
1440
} /*** end of XcpCmdProgramClear ***/
1441

    
1442

    
1443
/************************************************************************************//**
1444
** \brief     XCP command processor function which handles the PROGRAM_RESET command as
1445
**            defined by the protocol.
1446
** \param     data Pointer to a byte buffer with the packet data.
1447
** \return    none
1448
**
1449
****************************************************************************************/
1450
static void XcpCmdProgramReset(blt_int8u *data)
1451
{
1452
  /* suppress compiler warning for unused parameter */
1453
  data = data;
1454

    
1455
  xcpInfo.connected = 0;
1456
#if (BOOT_GATE_ENABLE > 0)
1457
  xcpInfo.other_connection = 0;
1458
#endif
1459

    
1460
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
1461
  /* check if PGM resource is unlocked */
1462
  if ((xcpInfo.protection & XCP_RES_PGM) == XCP_RES_PGM)
1463
  {
1464
    /* resource is locked. use seed/key sequence to unlock */
1465
    XcpSetCtoError(XCP_ERR_ACCESS_LOCKED);
1466
    return;
1467
  }
1468
#endif
1469

    
1470
  /* reset the ecu after programming is done. so basically, just start the newly programmed
1471
   * firmware. it is okay if the code does not return here. if CpuReset() is used here, then
1472
   * the bootloader is first activated again, including the backdoor timer which is not
1473
   * desired.
1474
   */
1475
  CpuStartUserProgram();
1476

    
1477
  /* set packet id to command response packet */
1478
  xcpInfo.ctoData[0] = XCP_PID_RES;
1479

    
1480
  /* set packet length */
1481
  xcpInfo.ctoLen = 1;
1482
} /*** end of XcpCmdProgramReset ***/
1483

    
1484

    
1485
/************************************************************************************//**
1486
** \brief     XCP command processor function which handles the PROGRAM_PREPARE command as
1487
**            defined by the protocol.
1488
** \param     data Pointer to a byte buffer with the packet data.
1489
** \return    none
1490
**
1491
****************************************************************************************/
1492
static void XcpCmdProgramPrepare(blt_int8u *data)
1493
{
1494
#if (XCP_SEED_KEY_PROTECTION_EN == 1)
1495
  /* check if PGM resource is unlocked */
1496
  if ((xcpInfo.protection & XCP_RES_PGM) == XCP_RES_PGM)
1497
  {
1498
    /* resource is locked. use seed/key sequence to unlock */
1499
    XcpSetCtoError(XCP_ERR_ACCESS_LOCKED);
1500
    return;
1501
  }
1502
#endif
1503

    
1504
  /* programming with kernel currently not needed and therefore not supported */
1505
  XcpSetCtoError(XCP_ERR_GENERIC);
1506
  return;
1507
} /*** end of XcpCmdProgramPrepare ***/
1508
#endif /* XCP_RES_PROGRAMMING_EN == 1 */
1509

    
1510

    
1511

    
1512
/******************************** end of xcp.c *****************************************/