/******************************************************************************
*                                                                             *
*              Copyright (C),  Philips Semiconductors SLH                     *
*                                                                             *
*  All rights are reserved. Reproduction in whole or in part is prohibited    *
*           without the written consent of the copyright owner.               *
*                                                                             *
*  Philips reserves the right to make changes without notice at any time.     *
* Philips makes no warranty, expressed, implied or statutory, including but   *
* not limited to any implied warranty of merchantibility or fitness for any   *
*   particular purpose, or that the use will not infringe any third party     *
* patent, copyright or trademark. Philips must not be liable for any loss or  *
*                      damage arising from its use.                           *
*                                                                             *
*******************************************************************************
*                                                                             *
*       Project: 336355/105 - Control SW Library for HTRC110/HTRC120/HITAG2   *
*                                                                             *
*       File: CO_PERS.C                                                       *
*       Author: F. Boeh                                                       *
*               S. de Zeeuw                                                   *
*                                                                             *
*******************************************************************************
*      DATE      *          CHANGES DONE                         *    BY      *
*******************************************************************************
*                *                                               *            *
*  Apr 27,1998   * Version 2.0 released                          * DE ZEEUW   *
*                *                                               *            *
*******************************************************************************/

#include "HTRC120.H"
#include "portab.h"


#pragma regparms


unsigned char Crypt_Personalize( unsigned char idata * addr_newkey,
                                 unsigned char idata * addr_oldkey,
                                 unsigned char idata * addr_newpass,
                                 unsigned char idata * addr_oldpass )
/********************************************************************************
*                                                                               *
* Description:                                                                  *
* This procedure executes the personalization of the co-processor as described  *
* in the data sheet. The value of CntVal depends on the above definition of     *
* Count_Value. The HITAG1 personalization is not perfomed, all corresponding    *
* values are assumed to have the initial values and are not changed.            *
*                                                                               *
* Parameters:                                                                   *
*   addr_newkey:  memory address where the new HITAG2-key has to be stored      *
*   addr_oldkey:  memory address where the old HITAG2-key has to be stored      *
*   addr_newpass: memory address where the new HITAG2-password has to be stored *
*                    (first Byte shall be a dummy Byte)                         *
*   addr_oldpass: memory address where the old HITAG2-password has to be stored *
*                    (first Byte shall be a dummy Byte)                         *
* Return:                                                                       *
*   FALSE         in case that no error occured                                 *
*   TRUE          in case of an error                                           *
*                                                                               *
********************************************************************************/

{
   unsigned char temp, count;

   init_copro();             /* reset co-processor */
    
   temp = CMD_PERSONALIZE;   /* set command personalization */
   if (send_copro ( (unsigned char idata *) &temp, 8 )) return TRUE;  
                             /* send command */

   temp = COUNT_VALUE;       /* set count value */
   if (send_copro ( (unsigned char idata *) &temp, 8 )) return TRUE;
                             /* send count value to co-processor */


   /* program old/new key A / B; old/new logdata 0A/0B / 1A/1B". */
   /* It is assumed that the HTRC120 is initialised with its     */
   /* factory values ( FF FF FF FF ). These values are           */
   /* reprogrammed here which "skips" the HITAG1 personalization */
   /* part.                                                      */
   temp = 0xFF;
   count=2*6*4;
   do
   {
      if (send_copro( (unsigned char idata *) &temp, 8 )) return TRUE;
   }
   while( --count );
   
   /* old/new Key LOW are stored in addr[4] and addr[5]; 2 bytes 0xFF have to  */
   /* be transmitted first:                                                    */

   temp = 0xFF;
   if (send_copro ( (unsigned char idata *) &temp, 8 )) return TRUE;
   if (send_copro ( (unsigned char idata *) &temp, 8 )) return TRUE;
   if (send_copro( addr_oldkey+4, 16 )) /* send old HITAG2 key LOW (16 bit)    */
      return TRUE;
   if (send_copro ( (unsigned char idata *) &temp, 8 )) return TRUE;
   if (send_copro ( (unsigned char idata *) &temp, 8 )) return TRUE;
   if (send_copro( addr_newkey+4, 16 )) /* program new HITAG2 key LOW (16 bit) */
      return TRUE;


   if (send_copro( addr_oldkey, 32 ))   /* send old HITAG2 key HIGH (32 bit)   */
      return TRUE;
   if (send_copro( addr_newkey, 32 ))   /* program new HITAG2 key HIGH (32 bit)*/
      return TRUE;


   temp = 0xFF;
   if (send_copro ( (unsigned char idata *) &temp, 8 )) return TRUE;
   if (send_copro( addr_oldpass, 24 ))  /* send old HITAG2 password            */
      return TRUE;

   temp = 0xFF;
   if (send_copro ( (unsigned char idata *) &temp, 8 )) return TRUE;
   if (send_copro( addr_newpass, 24 ))  /* program new HITAG2 password         */
      return TRUE;

   return wait_copro();
}

