Home | History | Annotate | Download | only in src
      1  /*
      2   * Copyright (C) 2015 NXP Semiconductors
      3   *
      4   * Licensed under the Apache License, Version 2.0 (the "License");
      5   * you may not use this file except in compliance with the License.
      6   * You may obtain a copy of the License at
      7   *
      8   *      http://www.apache.org/licenses/LICENSE-2.0
      9   *
     10   * Unless required by applicable law or agreed to in writing, software
     11   * distributed under the License is distributed on an "AS IS" BASIS,
     12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13   * See the License for the specific language governing permissions and
     14   * limitations under the License.
     15   */
     16 #include <cutils/log.h>
     17 #include <Ala.h>
     18 #include <AlaLib.h>
     19 #include <IChannel.h>
     20 #include <errno.h>
     21 #include <string.h>
     22 #include <stdlib.h>
     23 
     24 pAla_Dwnld_Context_t gpAla_Dwnld_Context=NULL;
     25 extern INT32 gTransceiveTimeout;
     26 #ifdef JCOP3_WR
     27 UINT8 Cmd_Buffer[64*1024];
     28 static INT32 cmd_count = 0;
     29 bool islastcmdLoad;
     30 bool SendBack_cmds = false;
     31 UINT8 *pBuffer;
     32 #endif
     33 BOOLEAN mIsInit;
     34 UINT8 Select_Rsp[1024];
     35 UINT8 Jsbl_RefKey[256];
     36 UINT8 Jsbl_keylen;
     37 #if(NXP_LDR_SVC_VER_2 == TRUE)
     38 UINT8 StoreData[22];
     39 #else
     40 UINT8 StoreData[34];
     41 #endif
     42 int Select_Rsp_Len;
     43 #if(NXP_LDR_SVC_VER_2 == TRUE)
     44 UINT8 lsVersionArr[2];
     45 UINT8 tag42Arr[17];
     46 UINT8 tag45Arr[9];
     47 UINT8 lsExecuteResp[4];
     48 UINT8 AID_ARRAY[22];
     49 INT32 resp_len = 0;
     50 FILE *fAID_MEM = NULL;
     51 FILE *fLS_STATUS = NULL;
     52 UINT8 lsGetStatusArr[2];
     53 tJBL_STATUS (*ls_GetStatus_seqhandler[])(Ala_ImageInfo_t *pContext, tJBL_STATUS status, Ala_TranscieveInfo_t *pInfo)=
     54 {
     55     ALA_OpenChannel,
     56     ALA_SelectAla,
     57     ALA_getAppletLsStatus,
     58     ALA_CloseChannel,
     59     NULL
     60 };
     61 #endif
     62 tJBL_STATUS (*Applet_load_seqhandler[])(Ala_ImageInfo_t *pContext, tJBL_STATUS status, Ala_TranscieveInfo_t *pInfo)=
     63 {
     64     ALA_OpenChannel,
     65     ALA_SelectAla,
     66     ALA_StoreData,
     67     ALA_loadapplet,
     68     NULL
     69 };
     70 
     71 tJBL_STATUS (*Jsblcer_id_seqhandler[])(Ala_ImageInfo_t *pContext, tJBL_STATUS status, Ala_TranscieveInfo_t *pInfo)=
     72 {
     73     ALA_OpenChannel,
     74     ALA_SelectAla,
     75     ALA_CloseChannel,
     76     NULL
     77 };
     78 
     79 
     80 /*******************************************************************************
     81 **
     82 ** Function:        initialize
     83 **
     84 ** Description:     Initialize all member variables.
     85 **                  native: Native data.
     86 **
     87 ** Returns:         True if ok.
     88 **
     89 *******************************************************************************/
     90 BOOLEAN initialize (IChannel_t* channel)
     91 {
     92     static const char fn [] = "Ala_initialize";
     93 
     94     ALOGD ("%s: enter", fn);
     95 
     96     gpAla_Dwnld_Context = (pAla_Dwnld_Context_t)malloc(sizeof(Ala_Dwnld_Context_t));
     97     if(gpAla_Dwnld_Context != NULL)
     98     {
     99         memset((void *)gpAla_Dwnld_Context, 0, (UINT32)sizeof(Ala_Dwnld_Context_t));
    100     }
    101     else
    102     {
    103         ALOGD("%s: Memory allocation failed", fn);
    104         return (FALSE);
    105     }
    106     gpAla_Dwnld_Context->mchannel = channel;
    107 
    108 #ifdef JCOP3_WR
    109     cmd_count = 0;
    110     SendBack_cmds = false;
    111     islastcmdLoad = false;
    112 #endif
    113 #if(NXP_LDR_SVC_VER_2 == TRUE)
    114     fAID_MEM = fopen(AID_MEM_PATH,"r");
    115 
    116     if(fAID_MEM == NULL)
    117     {
    118         ALOGD("%s: AID data file does not exists", fn);
    119         memcpy(&ArrayOfAIDs[2][1],&SelectAla[0],sizeof(SelectAla));
    120         ArrayOfAIDs[2][0] = sizeof(SelectAla);
    121     }
    122     else
    123     {
    124         /*Change is required aidLen = 0x00*/
    125         UINT8 aidLen = 0x00;
    126         INT32 wStatus = 0;
    127 
    128     while(!(feof(fAID_MEM)))
    129     {
    130         /*the length is not incremented*/
    131         wStatus = FSCANF_BYTE(fAID_MEM,"%2x",&ArrayOfAIDs[2][aidLen++]);
    132         if(wStatus == 0)
    133         {
    134             ALOGE ("%s: exit: Error during read AID data", fn);
    135             fclose(fAID_MEM);
    136             return FALSE;
    137         }
    138     }
    139     ArrayOfAIDs[2][0] = aidLen - 1;
    140     fclose(fAID_MEM);
    141     }
    142     lsExecuteResp[0] = TAG_LSES_RESP;
    143     lsExecuteResp[1] = TAG_LSES_RSPLEN;
    144     lsExecuteResp[2] = LS_ABORT_SW1;
    145     lsExecuteResp[3] = LS_ABORT_SW2;
    146 #endif
    147 #ifdef JCOP3_WR
    148     pBuffer = Cmd_Buffer;
    149 #endif
    150     mIsInit = TRUE;
    151     ALOGD ("%s: exit", fn);
    152     return (TRUE);
    153 }
    154 
    155 
    156 /*******************************************************************************
    157 **
    158 ** Function:        finalize
    159 **
    160 ** Description:     Release all resources.
    161 **
    162 ** Returns:         None
    163 **
    164 *******************************************************************************/
    165 void finalize ()
    166 {
    167     static const char fn [] = "Ala_finalize";
    168     ALOGD ("%s: enter", fn);
    169     mIsInit       = FALSE;
    170     if(gpAla_Dwnld_Context != NULL)
    171     {
    172         gpAla_Dwnld_Context->mchannel = NULL;
    173         free(gpAla_Dwnld_Context);
    174         gpAla_Dwnld_Context = NULL;
    175     }
    176     ALOGD ("%s: exit", fn);
    177 }
    178 
    179 /*******************************************************************************
    180 **
    181 ** Function:        Perform_ALA
    182 **
    183 ** Description:     Performs the ALA download sequence
    184 **
    185 ** Returns:         Success if ok.
    186 **
    187 *******************************************************************************/
    188 #if(NXP_LDR_SVC_VER_2 == TRUE)
    189 tJBL_STATUS Perform_ALA(const char *name,const char *dest, const UINT8 *pdata,
    190 UINT16 len, UINT8 *respSW)
    191 #else
    192 tJBL_STATUS Perform_ALA(const char *name, const UINT8 *pdata, UINT16 len)
    193 #endif
    194 {
    195     static const char fn [] = "Perform_ALA";
    196     static const char Ala_path[] = APPLET_PATH;
    197     tJBL_STATUS status = STATUS_FAILED;
    198     ALOGD ("%s: enter; sha-len=%d", fn, len);
    199 
    200     if(mIsInit == false)
    201     {
    202         ALOGD ("%s: ALA lib is not initialized", fn);
    203         status = STATUS_FAILED;
    204     }
    205     else if((pdata == NULL) ||
    206             (len == 0x00))
    207     {
    208         ALOGD ("%s: Invalid SHA-data", fn);
    209     }
    210     else
    211     {
    212         StoreData[0] = STORE_DATA_TAG;
    213         StoreData[1] = len;
    214         memcpy(&StoreData[2], pdata, len);
    215 #if(NXP_LDR_SVC_VER_2 == TRUE)
    216         status = ALA_update_seq_handler(Applet_load_seqhandler, name, dest);
    217         if((status != STATUS_OK)&&(lsExecuteResp[2] == 0x90)&&
    218         (lsExecuteResp[3] == 0x00))
    219         {
    220             lsExecuteResp[2] = LS_ABORT_SW1;
    221             lsExecuteResp[3] = LS_ABORT_SW2;
    222         }
    223         memcpy(&respSW[0],&lsExecuteResp[0],4);
    224         ALOGD ("%s: lsExecuteScript Response SW=%2x%2x",fn, lsExecuteResp[2],
    225         lsExecuteResp[3]);
    226 #else
    227         status = ALA_update_seq_handler(Applet_load_seqhandler, name);
    228 #endif
    229     }
    230 
    231     ALOGD("%s: exit; status=0x0%x", fn, status);
    232     return status;
    233 }
    234 #if(NXP_LDR_SVC_VER_2 == FALSE)
    235 /*******************************************************************************
    236 **
    237 ** Function:        GetJsbl_Certificate_ID
    238 **
    239 ** Description:     Performs the GetJsbl_Certificate_ID sequence
    240 **
    241 ** Returns:         Success if ok.
    242 **
    243 *******************************************************************************/
    244 tJBL_STATUS GetJsbl_Certificate_Refkey(UINT8 *pKey, INT32 *pKeylen)
    245 {
    246     static const char fn [] = "GetJsbl_Certificate_ID";
    247     tJBL_STATUS status = STATUS_FAILED;
    248     ALOGD ("%s: enter", fn);
    249 
    250     if(mIsInit == false)
    251     {
    252         ALOGD ("%s: ALA lib is not initialized", fn);
    253         status = STATUS_FAILED;
    254     }
    255     else
    256     {
    257         status = JsblCerId_seq_handler(Jsblcer_id_seqhandler);
    258         if(status == STATUS_SUCCESS)
    259         {
    260             if(Jsbl_keylen != 0x00)
    261             {
    262                 *pKeylen = (INT32)Jsbl_keylen;
    263                 memcpy(pKey, Jsbl_RefKey, Jsbl_keylen);
    264                 Jsbl_keylen = 0;
    265             }
    266         }
    267     }
    268 
    269     ALOGD("%s: exit; status=0x0%x", fn, status);
    270     return status;
    271 }
    272 
    273 /*******************************************************************************
    274 **
    275 ** Function:        JsblCerId_seq_handler
    276 **
    277 ** Description:     Performs get JSBL Certificate Identifier sequence
    278 **
    279 ** Returns:         Success if ok.
    280 **
    281 *******************************************************************************/
    282 tJBL_STATUS JsblCerId_seq_handler(tJBL_STATUS (*seq_handler[])(Ala_ImageInfo_t* pContext, tJBL_STATUS status, Ala_TranscieveInfo_t* pInfo))
    283 {
    284     static const char fn[] = "JsblCerId_seq_handler";
    285     UINT16 seq_counter = 0;
    286     Ala_ImageInfo_t update_info = (Ala_ImageInfo_t )gpAla_Dwnld_Context->Image_info;
    287     Ala_TranscieveInfo_t trans_info = (Ala_TranscieveInfo_t )gpAla_Dwnld_Context->Transcv_Info;
    288     tJBL_STATUS status = STATUS_FAILED;
    289     ALOGD("%s: enter", fn);
    290 
    291     while((seq_handler[seq_counter]) != NULL )
    292     {
    293         status = STATUS_FAILED;
    294         status = (*(seq_handler[seq_counter]))(&update_info, status, &trans_info );
    295         if(STATUS_SUCCESS != status)
    296         {
    297             ALOGE("%s: exiting; status=0x0%X", fn, status);
    298             break;
    299         }
    300         seq_counter++;
    301     }
    302 
    303     ALOGE("%s: exit; status=0x%x", fn, status);
    304     return status;
    305 }
    306 #else
    307 
    308 /*******************************************************************************
    309 **
    310 ** Function:        GetLs_Version
    311 **
    312 ** Description:     Performs the GetLs_Version sequence
    313 **
    314 ** Returns:         Success if ok.
    315 **
    316 *******************************************************************************/
    317 tJBL_STATUS GetLs_Version(UINT8 *pVersion)
    318 {
    319     static const char fn [] = "GetLs_Version";
    320     tJBL_STATUS status = STATUS_FAILED;
    321     ALOGD ("%s: enter", fn);
    322 
    323     if(mIsInit == false)
    324     {
    325         ALOGD ("%s: ALA lib is not initialized", fn);
    326         status = STATUS_FAILED;
    327     }
    328     else
    329     {
    330         status = GetVer_seq_handler(Jsblcer_id_seqhandler);
    331         if(status == STATUS_SUCCESS)
    332         {
    333             pVersion[0] = 2;
    334             pVersion[1] = 0;
    335             memcpy(&pVersion[2], lsVersionArr, sizeof(lsVersionArr));
    336             ALOGD("%s: GetLsVersion is =0x0%x%x", fn, lsVersionArr[0],lsVersionArr[1]);
    337         }
    338     }
    339     ALOGD("%s: exit; status=0x0%x", fn, status);
    340     return status;
    341 }
    342 /*******************************************************************************
    343 **
    344 ** Function:        Get_LsAppletStatus
    345 **
    346 ** Description:     Performs the Get_LsAppletStatus sequence
    347 **
    348 ** Returns:         Success if ok.
    349 **
    350 *******************************************************************************/
    351 tJBL_STATUS Get_LsAppletStatus(UINT8 *pVersion)
    352 {
    353     static const char fn [] = "GetLs_Version";
    354     tJBL_STATUS status = STATUS_FAILED;
    355     ALOGD ("%s: enter", fn);
    356 
    357     if(mIsInit == false)
    358     {
    359         ALOGD ("%s: ALA lib is not initialized", fn);
    360         status = STATUS_FAILED;
    361     }
    362     else
    363     {
    364         status = GetLsStatus_seq_handler(ls_GetStatus_seqhandler);
    365         if(status == STATUS_SUCCESS)
    366         {
    367             pVersion[0] = lsGetStatusArr[0];
    368             pVersion[1] = lsGetStatusArr[1];
    369             ALOGD("%s: GetLsAppletStatus is =0x0%x%x", fn, lsGetStatusArr[0],lsGetStatusArr[1]);
    370         }
    371     }
    372     ALOGD("%s: exit; status=0x0%x", fn, status);
    373     return status;
    374 }
    375 
    376 /*******************************************************************************
    377 **
    378 ** Function:        GetVer_seq_handler
    379 **
    380 ** Description:     Performs GetVer_seq_handler sequence
    381 **
    382 ** Returns:         Success if ok.
    383 **
    384 *******************************************************************************/
    385 tJBL_STATUS GetVer_seq_handler(tJBL_STATUS (*seq_handler[])(Ala_ImageInfo_t*
    386         pContext, tJBL_STATUS status, Ala_TranscieveInfo_t* pInfo))
    387 {
    388     static const char fn[] = "GetVer_seq_handler";
    389     UINT16 seq_counter = 0;
    390     Ala_ImageInfo_t update_info = (Ala_ImageInfo_t )gpAla_Dwnld_Context->Image_info;
    391     Ala_TranscieveInfo_t trans_info = (Ala_TranscieveInfo_t )gpAla_Dwnld_Context->Transcv_Info;
    392     tJBL_STATUS status = STATUS_FAILED;
    393     ALOGD("%s: enter", fn);
    394 
    395     while((seq_handler[seq_counter]) != NULL )
    396     {
    397         status = STATUS_FAILED;
    398         status = (*(seq_handler[seq_counter]))(&update_info, status, &trans_info );
    399         if(STATUS_SUCCESS != status)
    400         {
    401             ALOGE("%s: exiting; status=0x0%X", fn, status);
    402             break;
    403         }
    404         seq_counter++;
    405     }
    406 
    407     ALOGE("%s: exit; status=0x%x", fn, status);
    408     return status;
    409 }
    410 
    411 /*******************************************************************************
    412 **
    413 ** Function:        GetLsStatus_seq_handler
    414 **
    415 ** Description:     Performs GetVer_seq_handler sequence
    416 **
    417 ** Returns:         Success if ok.
    418 **
    419 *******************************************************************************/
    420 tJBL_STATUS GetLsStatus_seq_handler(tJBL_STATUS (*seq_handler[])(Ala_ImageInfo_t*
    421         pContext, tJBL_STATUS status, Ala_TranscieveInfo_t* pInfo))
    422 {
    423     static const char fn[] = "ls_GetStatus_seqhandler";
    424     UINT16 seq_counter = 0;
    425     Ala_ImageInfo_t update_info = (Ala_ImageInfo_t )gpAla_Dwnld_Context->Image_info;
    426     Ala_TranscieveInfo_t trans_info = (Ala_TranscieveInfo_t )gpAla_Dwnld_Context->Transcv_Info;
    427     tJBL_STATUS status = STATUS_FAILED;
    428     ALOGD("%s: enter", fn);
    429 
    430     while((seq_handler[seq_counter]) != NULL )
    431     {
    432         status = STATUS_FAILED;
    433         status = (*(seq_handler[seq_counter]))(&update_info, status, &trans_info );
    434         if(STATUS_SUCCESS != status)
    435         {
    436             ALOGE("%s: exiting; status=0x0%X", fn, status);
    437             break;
    438         }
    439         seq_counter++;
    440     }
    441 
    442     ALOGE("%s: exit; status=0x%x", fn, status);
    443     return status;
    444 }
    445 #endif
    446 /*******************************************************************************
    447 **
    448 ** Function:        ALA_update_seq_handler
    449 **
    450 ** Description:     Performs the ALA update sequence handler sequence
    451 **
    452 ** Returns:         Success if ok.
    453 **
    454 *******************************************************************************/
    455 #if(NXP_LDR_SVC_VER_2 == TRUE)
    456 tJBL_STATUS ALA_update_seq_handler(tJBL_STATUS (*seq_handler[])
    457         (Ala_ImageInfo_t* pContext, tJBL_STATUS status, Ala_TranscieveInfo_t*
    458                 pInfo), const char *name, const char *dest)
    459 #else
    460 tJBL_STATUS ALA_update_seq_handler(tJBL_STATUS (*seq_handler[])(Ala_ImageInfo_t* pContext, tJBL_STATUS status, Ala_TranscieveInfo_t* pInfo), const char *name)
    461 #endif
    462 {
    463     static const char fn[] = "ALA_update_seq_handler";
    464     static const char Ala_path[] = APPLET_PATH;
    465     UINT16 seq_counter = 0;
    466     Ala_ImageInfo_t update_info = (Ala_ImageInfo_t )gpAla_Dwnld_Context->
    467         Image_info;
    468     Ala_TranscieveInfo_t trans_info = (Ala_TranscieveInfo_t )gpAla_Dwnld_Context
    469     ->Transcv_Info;
    470     tJBL_STATUS status = STATUS_FAILED;
    471     ALOGD("%s: enter", fn);
    472 
    473 #if(NXP_LDR_SVC_VER_2 == TRUE)
    474     if(dest != NULL)
    475     {
    476         strcat(update_info.fls_RespPath, dest);
    477         ALOGD("Loader Service response data path/destination: %s", dest);
    478         update_info.bytes_wrote = 0xAA;
    479     }
    480     else
    481     {
    482         update_info.bytes_wrote = 0x55;
    483     }
    484     if((ALA_UpdateExeStatus(LS_DEFAULT_STATUS))!= TRUE)
    485     {
    486         return FALSE;
    487     }
    488 #endif
    489     //memcpy(update_info.fls_path, (char*)Ala_path, sizeof(Ala_path));
    490     strcat(update_info.fls_path, name);
    491     ALOGD("Selected applet to install is: %s", update_info.fls_path);
    492 
    493     while((seq_handler[seq_counter]) != NULL )
    494     {
    495         status = STATUS_FAILED;
    496         status = (*(seq_handler[seq_counter]))(&update_info, status,
    497                 &trans_info);
    498         if(STATUS_SUCCESS != status)
    499         {
    500             ALOGE("%s: exiting; status=0x0%X", fn, status);
    501             break;
    502         }
    503         seq_counter++;
    504     }
    505 
    506     ALA_CloseChannel(&update_info, STATUS_FAILED, &trans_info);
    507     ALOGE("%s: exit; status=0x%x", fn, status);
    508     return status;
    509 
    510 }
    511 /*******************************************************************************
    512 **
    513 ** Function:        ALA_OpenChannel
    514 **
    515 ** Description:     Creates the logical channel with ala
    516 **
    517 ** Returns:         Success if ok.
    518 **
    519 *******************************************************************************/
    520 tJBL_STATUS ALA_OpenChannel(Ala_ImageInfo_t *Os_info, tJBL_STATUS status,
    521         Ala_TranscieveInfo_t *pTranscv_Info)
    522 {
    523     static const char fn[] = "ALA_OpenChannel";
    524     bool stat = false;
    525     INT32 recvBufferActualSize = 0;
    526     IChannel_t *mchannel = gpAla_Dwnld_Context->mchannel;
    527     Os_info->channel_cnt = 0x00;
    528     ALOGD("%s: enter", fn);
    529     if(Os_info == NULL ||
    530        pTranscv_Info == NULL)
    531     {
    532         ALOGD("%s: Invalid parameter", fn);
    533     }
    534     else
    535     {
    536         pTranscv_Info->timeout = gTransceiveTimeout;
    537         pTranscv_Info->sSendlength = (INT32)sizeof(OpenChannel);
    538         pTranscv_Info->sRecvlength = 1024;//(INT32)sizeof(INT32);
    539         memcpy(pTranscv_Info->sSendData, OpenChannel, pTranscv_Info->sSendlength);
    540 
    541         ALOGD("%s: Calling Secure Element Transceive", fn);
    542         stat = mchannel->transceive (pTranscv_Info->sSendData,
    543                                 pTranscv_Info->sSendlength,
    544                                 pTranscv_Info->sRecvData,
    545                                 pTranscv_Info->sRecvlength,
    546                                 recvBufferActualSize,
    547                                 pTranscv_Info->timeout);
    548         if(stat != TRUE &&
    549            (recvBufferActualSize < 0x03))
    550         {
    551 #if(NXP_LDR_SVC_VER_2 == TRUE)
    552             if(recvBufferActualSize == 0x02)
    553             memcpy(&lsExecuteResp[2],
    554                     &pTranscv_Info->sRecvData[recvBufferActualSize-2],2);
    555 #endif
    556             status = STATUS_FAILED;
    557             ALOGE("%s: SE transceive failed status = 0x%X", fn, status);
    558         }
    559         else if(((pTranscv_Info->sRecvData[recvBufferActualSize-2] != 0x90) &&
    560                (pTranscv_Info->sRecvData[recvBufferActualSize-1] != 0x00)))
    561         {
    562 #if(NXP_LDR_SVC_VER_2 == TRUE)
    563             memcpy(&lsExecuteResp[2],
    564                     &pTranscv_Info->sRecvData[recvBufferActualSize-2],2);
    565 #endif
    566             status = STATUS_FAILED;
    567             ALOGE("%s: invalid response = 0x%X", fn, status);
    568         }
    569         else
    570         {
    571             UINT8 cnt = Os_info->channel_cnt;
    572             Os_info->Channel_Info[cnt].channel_id = pTranscv_Info->sRecvData[recvBufferActualSize-3];
    573             Os_info->Channel_Info[cnt].isOpend = true;
    574             Os_info->channel_cnt++;
    575             status = STATUS_OK;
    576         }
    577     }
    578     ALOGE("%s: exit; status=0x%x", fn, status);
    579     return status;
    580 }
    581 /*******************************************************************************
    582 **
    583 ** Function:        ALA_SelectAla
    584 **
    585 ** Description:     Creates the logical channel with ala
    586 **                  Channel_id will be used for any communication with Ala
    587 **
    588 ** Returns:         Success if ok.
    589 **
    590 *******************************************************************************/
    591 tJBL_STATUS ALA_SelectAla(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info)
    592 {
    593     static const char fn[] = "ALA_SelectAla";
    594     bool stat = false;
    595     INT32 recvBufferActualSize = 0;
    596     IChannel_t *mchannel = gpAla_Dwnld_Context->mchannel;
    597 #if(NXP_LDR_SVC_VER_2 == TRUE)
    598     UINT8 selectCnt = 3;
    599 #endif
    600     ALOGD("%s: enter", fn);
    601 
    602     if(Os_info == NULL ||
    603        pTranscv_Info == NULL)
    604     {
    605         ALOGD("%s: Invalid parameter", fn);
    606     }
    607     else
    608     {
    609         pTranscv_Info->sSendData[0] = Os_info->Channel_Info[0].channel_id;
    610         pTranscv_Info->timeout = gTransceiveTimeout;
    611         pTranscv_Info->sSendlength = (INT32)sizeof(SelectAla);
    612         pTranscv_Info->sRecvlength = 1024;//(INT32)sizeof(INT32);
    613 
    614 #if(NXP_LDR_SVC_VER_2 == TRUE)
    615     while((selectCnt--) > 0)
    616     {
    617         memcpy(&(pTranscv_Info->sSendData[1]), &ArrayOfAIDs[selectCnt][2],
    618                 ((ArrayOfAIDs[selectCnt][0])-1));
    619         pTranscv_Info->sSendlength = (INT32)ArrayOfAIDs[selectCnt][0];
    620         /*If NFC/SPI Deinitialize requested*/
    621 #else
    622         memcpy(&(pTranscv_Info->sSendData[1]), &SelectAla[1], sizeof(SelectAla)-1);
    623 #endif
    624         ALOGD("%s: Calling Secure Element Transceive with Loader service AID", fn);
    625 
    626         stat = mchannel->transceive (pTranscv_Info->sSendData,
    627                                 pTranscv_Info->sSendlength,
    628                                 pTranscv_Info->sRecvData,
    629                                 pTranscv_Info->sRecvlength,
    630                                 recvBufferActualSize,
    631                                 pTranscv_Info->timeout);
    632         if(stat != TRUE &&
    633            (recvBufferActualSize == 0x00))
    634         {
    635             status = STATUS_FAILED;
    636             ALOGE("%s: SE transceive failed status = 0x%X", fn, status);
    637 #if(NXP_LDR_SVC_VER_2 == TRUE)
    638             break;
    639 #endif
    640         }
    641         else if(((pTranscv_Info->sRecvData[recvBufferActualSize-2] == 0x90) &&
    642                (pTranscv_Info->sRecvData[recvBufferActualSize-1] == 0x00)))
    643         {
    644             status = Process_SelectRsp(pTranscv_Info->sRecvData, (recvBufferActualSize-2));
    645             if(status != STATUS_OK)
    646             {
    647                 ALOGE("%s: Select Ala Rsp doesnt have a valid key; status = 0x%X", fn, status);
    648             }
    649 #if(NXP_LDR_SVC_VER_2 == TRUE)
    650            /*If AID is found which is successfully selected break while loop*/
    651            if(status == STATUS_OK)
    652            {
    653                UINT8 totalLen = ArrayOfAIDs[selectCnt][0];
    654                UINT8 cnt  = 0;
    655                INT32 wStatus= 0;
    656                status = STATUS_FAILED;
    657 
    658                fAID_MEM = fopen(AID_MEM_PATH,"w+");
    659 
    660                if(fAID_MEM == NULL)
    661                {
    662                    ALOGE("Error opening AID data file for writing: %s",
    663                            strerror(errno));
    664                    return status;
    665                }
    666                while(cnt <= totalLen)
    667                {
    668                    wStatus = fprintf(fAID_MEM, "%02x",
    669                            ArrayOfAIDs[selectCnt][cnt++]);
    670                    if(wStatus != 2)
    671                    {
    672                       ALOGE("%s: Error writing AID data to AID_MEM file: %s",
    673                               fn, strerror(errno));
    674                       break;
    675                    }
    676                }
    677                if(wStatus == 2)
    678                    status = STATUS_OK;
    679                fclose(fAID_MEM);
    680                break;
    681            }
    682 #endif
    683         }
    684 #if(NXP_LDR_SVC_VER_2 == TRUE)
    685         else if(((pTranscv_Info->sRecvData[recvBufferActualSize-2] != 0x90)))
    686         {
    687             /*Copy the response SW in failure case*/
    688             memcpy(&lsExecuteResp[2], &(pTranscv_Info->
    689                     sRecvData[recvBufferActualSize-2]),2);
    690         }
    691 #endif
    692         else
    693         {
    694             status = STATUS_FAILED;
    695         }
    696 #if(NXP_LDR_SVC_VER_2 == TRUE)
    697     }
    698 #endif
    699     }
    700     ALOGE("%s: exit; status=0x%x", fn, status);
    701     return status;
    702 }
    703 
    704 /*******************************************************************************
    705 **
    706 ** Function:        ALA_StoreData
    707 **
    708 ** Description:     It is used to provide the ALA with an Unique
    709 **                  Identifier of the Application that has triggered the ALA script.
    710 **
    711 ** Returns:         Success if ok.
    712 **
    713 *******************************************************************************/
    714 tJBL_STATUS ALA_StoreData(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info)
    715 {
    716     static const char fn[] = "ALA_StoreData";
    717     bool stat = false;
    718     INT32 recvBufferActualSize = 0;
    719     INT32 xx=0, len = 0;
    720     IChannel_t *mchannel = gpAla_Dwnld_Context->mchannel;
    721     ALOGD("%s: enter", fn);
    722     if(Os_info == NULL ||
    723        pTranscv_Info == NULL)
    724     {
    725         ALOGD("%s: Invalid parameter", fn);
    726     }
    727     else
    728     {
    729         len = StoreData[1] + 2;  //+2 offset is for tag value and length byte
    730         pTranscv_Info->sSendData[xx++] = STORE_DATA_CLA | (Os_info->Channel_Info[0].channel_id);
    731         pTranscv_Info->sSendData[xx++] = STORE_DATA_INS;
    732         pTranscv_Info->sSendData[xx++] = 0x00; //P1
    733         pTranscv_Info->sSendData[xx++] = 0x00; //P2
    734         pTranscv_Info->sSendData[xx++] = len;
    735         memcpy(&(pTranscv_Info->sSendData[xx]), StoreData, len);
    736         pTranscv_Info->timeout = gTransceiveTimeout;
    737         pTranscv_Info->sSendlength = (INT32)(xx + sizeof(StoreData));
    738         pTranscv_Info->sRecvlength = 1024;
    739 
    740         ALOGD("%s: Calling Secure Element Transceive", fn);
    741         stat = mchannel->transceive (pTranscv_Info->sSendData,
    742                                 pTranscv_Info->sSendlength,
    743                                 pTranscv_Info->sRecvData,
    744                                 pTranscv_Info->sRecvlength,
    745                                 recvBufferActualSize,
    746                                 pTranscv_Info->timeout);
    747         if((stat != TRUE) &&
    748            (recvBufferActualSize == 0x00))
    749         {
    750             status = STATUS_FAILED;
    751             ALOGE("%s: SE transceive failed status = 0x%X", fn, status);
    752         }
    753         else if((pTranscv_Info->sRecvData[recvBufferActualSize-2] == 0x90) &&
    754                 (pTranscv_Info->sRecvData[recvBufferActualSize-1] == 0x00))
    755         {
    756             ALOGE("STORE CMD is successful");
    757             status = STATUS_SUCCESS;
    758         }
    759         else
    760         {
    761 #if(NXP_LDR_SVC_VER_2 == TRUE)
    762             /*Copy the response SW in failure case*/
    763             memcpy(&lsExecuteResp[2], &(pTranscv_Info->sRecvData
    764                     [recvBufferActualSize-2]),2);
    765 #endif
    766             status = STATUS_FAILED;
    767         }
    768     }
    769     ALOGE("%s: exit; status=0x%x", fn, status);
    770     return status;
    771 }
    772 
    773 /*******************************************************************************
    774 **
    775 ** Function:        ALA_loadapplet
    776 **
    777 ** Description:     Reads the script from the file and sent to Ala
    778 **
    779 ** Returns:         Success if ok.
    780 **
    781 *******************************************************************************/
    782 tJBL_STATUS ALA_loadapplet(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info)
    783 {
    784     static const char fn [] = "ALA_loadapplet";
    785     BOOLEAN stat = FALSE;
    786     int wResult, size =0;
    787     INT32 wIndex,wCount=0;
    788     INT32 wLen = 0;
    789     INT32 recvBufferActualSize = 0;
    790     UINT8 temp_buf[1024];
    791     UINT8 len_byte=0, offset =0;
    792     IChannel_t *mchannel = gpAla_Dwnld_Context->mchannel;
    793     Os_info->bytes_read = 0;
    794 #if(NXP_LDR_SVC_VER_2 == TRUE)
    795     BOOLEAN reachEOFCheck = FALSE;
    796     tJBL_STATUS tag40_found = STATUS_FAILED;
    797     if(Os_info->bytes_wrote == 0xAA)
    798     {
    799         Os_info->fResp = fopen(Os_info->fls_RespPath, "a+");
    800         if(Os_info->fResp == NULL)
    801         {
    802             ALOGE("Error opening response recording file <%s> for reading: %s",
    803             Os_info->fls_path, strerror(errno));
    804             return status;
    805         }
    806         ALOGD("%s: Response OUT FILE path is successfully created", fn);
    807     }
    808     else
    809     {
    810         ALOGD("%s: Response Out file is optional as per input", fn);
    811     }
    812 #endif
    813     ALOGD("%s: enter", fn);
    814     if(Os_info == NULL ||
    815        pTranscv_Info == NULL)
    816     {
    817         ALOGE("%s: invalid parameter", fn);
    818         return status;
    819     }
    820     Os_info->fp = fopen(Os_info->fls_path, "r");
    821 
    822     if (Os_info->fp == NULL) {
    823         ALOGE("Error opening OS image file <%s> for reading: %s",
    824                     Os_info->fls_path, strerror(errno));
    825         return status;
    826     }
    827     wResult = fseek(Os_info->fp, 0L, SEEK_END);
    828     if (wResult) {
    829         ALOGE("Error seeking end OS image file %s", strerror(errno));
    830         goto exit;
    831     }
    832     Os_info->fls_size = ftell(Os_info->fp);
    833     ALOGE("fls_size=%d", Os_info->fls_size);
    834     if (Os_info->fls_size < 0) {
    835         ALOGE("Error ftelling file %s", strerror(errno));
    836         goto exit;
    837     }
    838     wResult = fseek(Os_info->fp, 0L, SEEK_SET);
    839     if (wResult) {
    840         ALOGE("Error seeking start image file %s", strerror(errno));
    841         goto exit;
    842     }
    843 #if(NXP_LDR_SVC_VER_2 == TRUE)
    844     status = ALA_Check_KeyIdentifier(Os_info, status, pTranscv_Info,
    845         NULL, STATUS_FAILED, 0);
    846 #else
    847     status = ALA_Check_KeyIdentifier(Os_info, status, pTranscv_Info);
    848 #endif
    849     if(status != STATUS_OK)
    850     {
    851         goto exit;
    852     }
    853     while(!feof(Os_info->fp) &&
    854             (Os_info->bytes_read < Os_info->fls_size))
    855     {
    856         len_byte = 0x00;
    857         offset = 0;
    858 #if(NXP_LDR_SVC_VER_2 == TRUE)
    859         /*Check if the certificate/ is verified or not*/
    860         if(status != STATUS_OK)
    861         {
    862             goto exit;
    863         }
    864 #endif
    865         memset(temp_buf, 0, sizeof(temp_buf));
    866         ALOGE("%s; Start of line processing", fn);
    867         status = ALA_ReadScript(Os_info, temp_buf);
    868         if(status != STATUS_OK)
    869         {
    870             goto exit;
    871         }
    872 #if(NXP_LDR_SVC_VER_2 == TRUE)
    873         else if(status == STATUS_OK)
    874         {
    875             /*Reset the flag in case further commands exists*/
    876             reachEOFCheck = FALSE;
    877         }
    878 #endif
    879         if(temp_buf[offset] == TAG_ALA_CMD_ID)
    880         {
    881             /*
    882              * start sending the packet to Ala
    883              * */
    884             offset = offset+1;
    885             len_byte = Numof_lengthbytes(&temp_buf[offset], &wLen);
    886 #if(NXP_LDR_SVC_VER_2 == TRUE)
    887             /*If the len data not present or
    888              * len is less than or equal to 32*/
    889             if((len_byte == 0)||(wLen <= 32))
    890 #else
    891             if((len_byte == 0))
    892 #endif
    893             {
    894                 ALOGE("Invalid length zero");
    895 #if(NXP_LDR_SVC_VER_2 == TRUE)
    896                 goto exit;
    897 #else
    898                 return status;
    899 #endif
    900             }
    901             else
    902             {
    903 #if(NXP_LDR_SVC_VER_2 == TRUE)
    904                 tag40_found = STATUS_OK;
    905 #endif
    906                 offset = offset+len_byte;
    907                 pTranscv_Info->sSendlength = wLen;
    908                 memcpy(pTranscv_Info->sSendData, &temp_buf[offset], wLen);
    909             }
    910 #if(NXP_LDR_SVC_VER_2 == TRUE)
    911             status = ALA_SendtoAla(Os_info, status, pTranscv_Info, LS_Comm);
    912 #else
    913             status = ALA_SendtoAla(Os_info, status, pTranscv_Info);
    914 #endif
    915             if(status != STATUS_OK)
    916             {
    917 
    918 #if(NXP_LDR_SVC_VER_2 == TRUE)
    919                 /*When the switching of LS 6320 case*/
    920                 if(status == STATUS_FILE_NOT_FOUND)
    921                 {
    922                     /*When 6320 occurs close the existing channels*/
    923                     ALA_CloseChannel(Os_info,status,pTranscv_Info);
    924 
    925                     status = STATUS_FAILED;
    926                     status = ALA_OpenChannel(Os_info,status,pTranscv_Info);
    927                     if(status == STATUS_OK)
    928                     {
    929                         ALOGD("SUCCESS:Post Switching LS open channel");
    930                         status = STATUS_FAILED;
    931                         status = ALA_SelectAla(Os_info,status,pTranscv_Info);
    932                         if(status == STATUS_OK)
    933                         {
    934                             ALOGD("SUCCESS:Post Switching LS select");
    935                             status = STATUS_FAILED;
    936                             status = ALA_StoreData(Os_info,status,pTranscv_Info);
    937                             if(status == STATUS_OK)
    938                             {
    939                                 /*Enable certificate and signature verification*/
    940                                 tag40_found = STATUS_OK;
    941                                 lsExecuteResp[2] = 0x90;
    942                                 lsExecuteResp[3] = 0x00;
    943                                 reachEOFCheck = TRUE;
    944                                 continue;
    945                             }
    946                             ALOGE("Post Switching LS store data failure");
    947                         }
    948                         ALOGE("Post Switching LS select failure");
    949                     }
    950                     ALOGE("Post Switching LS failure");
    951                 }
    952                 ALOGE("Sending packet to ala failed");
    953                 goto exit;
    954 #else
    955                 return status;
    956 #endif
    957             }
    958         }
    959 #if(NXP_LDR_SVC_VER_2 == TRUE)
    960         else if((temp_buf[offset] == (0x7F))&&(temp_buf[offset+1] == (0x21)))
    961         {
    962             ALOGD("TAGID: Encountered again certificate tag 7F21");
    963             if(tag40_found == STATUS_OK)
    964             {
    965             ALOGD("2nd Script processing starts with reselect");
    966             status = STATUS_FAILED;
    967             status = ALA_SelectAla(Os_info,status,pTranscv_Info);
    968             if(status == STATUS_OK)
    969             {
    970                 ALOGD("2nd Script select success next store data command");
    971                 status = STATUS_FAILED;
    972                 status = ALA_StoreData(Os_info,status,pTranscv_Info);
    973                 if(status == STATUS_OK)
    974                 {
    975                     ALOGD("2nd Script store data success next certificate verification");
    976                     offset = offset+2;
    977                     len_byte = Numof_lengthbytes(&temp_buf[offset], &wLen);
    978                     status = ALA_Check_KeyIdentifier(Os_info, status, pTranscv_Info,
    979                     temp_buf, STATUS_OK, wLen+len_byte+2);
    980                     }
    981                 }
    982                 /*If the certificate and signature is verified*/
    983                 if(status == STATUS_OK)
    984                 {
    985                     /*If the certificate is verified for 6320 then new
    986                      * script starts*/
    987                     tag40_found = STATUS_FAILED;
    988                 }
    989                 /*If the certificate or signature verification failed*/
    990                 else{
    991                   goto exit;
    992                 }
    993             }
    994             /*Already certificate&Sginature verified previously skip 7f21& tag 60*/
    995             else
    996             {
    997                 memset(temp_buf, 0, sizeof(temp_buf));
    998                 status = ALA_ReadScript(Os_info, temp_buf);
    999                 if(status != STATUS_OK)
   1000                 {
   1001                     ALOGE("%s; Next Tag has to TAG 60 not found", fn);
   1002                     goto exit;
   1003                 }
   1004                 if(temp_buf[offset] == TAG_JSBL_HDR_ID)
   1005                 continue;
   1006                 else
   1007                     goto exit;
   1008             }
   1009         }
   1010 #endif
   1011         else
   1012         {
   1013             /*
   1014              * Invalid packet received in between stop processing packet
   1015              * return failed status
   1016              * */
   1017             status = STATUS_FAILED;
   1018             break;
   1019         }
   1020     }
   1021 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1022     if(Os_info->bytes_wrote == 0xAA)
   1023     {
   1024         fclose(Os_info->fResp);
   1025     }
   1026     ALA_UpdateExeStatus(LS_SUCCESS_STATUS);
   1027 #endif
   1028     wResult = fclose(Os_info->fp);
   1029     ALOGE("%s exit;End of Load Applet; status=0x%x",fn, status);
   1030     return status;
   1031 exit:
   1032     wResult = fclose(Os_info->fp);
   1033 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1034     if(Os_info->bytes_wrote == 0xAA)
   1035     {
   1036         fclose(Os_info->fResp);
   1037     }
   1038     /*Script ends with SW 6320 and reached END OF FILE*/
   1039     if(reachEOFCheck == TRUE)
   1040     {
   1041        status = STATUS_OK;
   1042        ALA_UpdateExeStatus(LS_SUCCESS_STATUS);
   1043     }
   1044 #endif
   1045     ALOGE("%s close fp and exit; status= 0x%X", fn,status);
   1046     return status;
   1047 
   1048 }
   1049 /*******************************************************************************
   1050 **
   1051 ** Function:        ALA_ProcessResp
   1052 **
   1053 ** Description:     Process the response packet received from Ala
   1054 **
   1055 ** Returns:         Success if ok.
   1056 **
   1057 *******************************************************************************/
   1058 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1059 tJBL_STATUS ALA_Check_KeyIdentifier(Ala_ImageInfo_t *Os_info, tJBL_STATUS status,
   1060    Ala_TranscieveInfo_t *pTranscv_Info, UINT8* temp_buf, tJBL_STATUS flag,
   1061    INT32 wNewLen)
   1062 #else
   1063 tJBL_STATUS ALA_Check_KeyIdentifier(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info)
   1064 #endif
   1065 {
   1066     static const char fn[] = "ALA_Check_KeyIdentifier";
   1067 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1068     UINT16 offset = 0x00, len_byte=0;
   1069 #else
   1070     UINT8 offset = 0x00, len_byte=0;
   1071 #endif
   1072     tJBL_STATUS key_found = STATUS_FAILED;
   1073     status = STATUS_FAILED;
   1074     UINT8 read_buf[1024];
   1075     bool stat = false;
   1076     INT32 wLen, recvBufferActualSize=0;
   1077     IChannel_t *mchannel = gpAla_Dwnld_Context->mchannel;
   1078 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1079     UINT8 certf_found = STATUS_FAILED;
   1080     UINT8 sign_found = STATUS_FAILED;
   1081 #endif
   1082     ALOGD("%s: enter", fn);
   1083 
   1084 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1085     while(!feof(Os_info->fp) &&
   1086             (Os_info->bytes_read < Os_info->fls_size))
   1087     {
   1088         offset = 0x00;
   1089         wLen = 0;
   1090         if(flag == STATUS_OK)
   1091         {
   1092             /*If the 7F21 TAG is already read: After TAG 40*/
   1093             memcpy(read_buf, temp_buf, wNewLen);
   1094             status = STATUS_OK;
   1095             flag   = STATUS_FAILED;
   1096         }
   1097         else
   1098         {
   1099             /*If the 7F21 TAG is not read: Before TAG 40*/
   1100             status = ALA_ReadScript(Os_info, read_buf);
   1101         }
   1102         if(status != STATUS_OK)
   1103             return status;
   1104         if(STATUS_OK == Check_Complete_7F21_Tag(Os_info,pTranscv_Info,
   1105                 read_buf, &offset))
   1106         {
   1107             ALOGD("%s: Certificate is verified", fn);
   1108             certf_found = STATUS_OK;
   1109             break;
   1110         }
   1111         /*The Loader Service Client ignores all subsequent commands starting by tag
   1112          * 7F21 or tag 60 until the first command starting by tag 40 is found*/
   1113         else if(((read_buf[offset] == TAG_ALA_CMD_ID)&&(certf_found != STATUS_OK)))
   1114         {
   1115             ALOGE("%s: NOT FOUND Root entity identifier's certificate", fn);
   1116             status = STATUS_FAILED;
   1117             return status;
   1118         }
   1119     }
   1120 #endif
   1121 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1122     memset(read_buf, 0, sizeof(read_buf));
   1123     if(certf_found == STATUS_OK)
   1124     {
   1125 #else
   1126         while(!feof(Os_info->fp))
   1127         {
   1128 #endif
   1129         offset  = 0x00;
   1130         wLen    = 0;
   1131         status  = ALA_ReadScript(Os_info, read_buf);
   1132         if(status != STATUS_OK)
   1133             return status;
   1134 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1135         else
   1136             status = STATUS_FAILED;
   1137 
   1138         if((read_buf[offset] == TAG_JSBL_HDR_ID)&&
   1139         (certf_found != STATUS_FAILED)&&(sign_found != STATUS_OK))
   1140 #else
   1141         if(read_buf[offset] == TAG_JSBL_HDR_ID &&
   1142            key_found != STATUS_OK)
   1143 #endif
   1144         {
   1145             //TODO check the SElect cmd response and return status accordingly
   1146             ALOGD("TAGID: TAG_JSBL_HDR_ID");
   1147             offset = offset+1;
   1148             len_byte = Numof_lengthbytes(&read_buf[offset], &wLen);
   1149             offset = offset + len_byte;
   1150 #if(NXP_LDR_SVC_VER_2 == FALSE)
   1151             if(read_buf[offset] == TAG_JSBL_KEY_ID)
   1152             {
   1153                 ALOGE("TAGID: TAG_JSBL_KEY_ID");
   1154                 offset = offset+1;
   1155                 wLen = read_buf[offset];
   1156                 offset = offset+1;
   1157                 key_found = memcmp(&read_buf[offset], Select_Rsp,
   1158                 Select_Rsp_Len);
   1159 
   1160                 if(key_found == STATUS_OK)
   1161                 {
   1162                     ALOGE("Key is matched");
   1163                     offset = offset + wLen;
   1164 #endif
   1165                     if(read_buf[offset] == TAG_SIGNATURE_ID)
   1166                     {
   1167 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1168                         offset = offset+1;
   1169                         len_byte = Numof_lengthbytes(&read_buf[offset], &wLen);
   1170                         offset = offset + len_byte;
   1171 #endif
   1172                         ALOGE("TAGID: TAG_SIGNATURE_ID");
   1173 
   1174 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1175                         pTranscv_Info->sSendlength = wLen+5;
   1176 
   1177                         pTranscv_Info->sSendData[0] = 0x00;
   1178                         pTranscv_Info->sSendData[1] = 0xA0;
   1179                         pTranscv_Info->sSendData[2] = 0x00;
   1180                         pTranscv_Info->sSendData[3] = 0x00;
   1181                         pTranscv_Info->sSendData[4] = wLen;
   1182 
   1183                         memcpy(&(pTranscv_Info->sSendData[5]),
   1184                         &read_buf[offset], wLen);
   1185 #else
   1186                         offset = offset+1;
   1187                         wLen = 0;
   1188                         len_byte = Numof_lengthbytes(&read_buf[offset], &wLen);
   1189                         if(len_byte == 0)
   1190                         {
   1191                             ALOGE("Invalid length zero");
   1192                             return STATUS_FAILED;
   1193                         }
   1194                         else
   1195                         {
   1196                             offset = offset+len_byte;
   1197                             pTranscv_Info->sSendlength = wLen;
   1198                             memcpy(pTranscv_Info->sSendData, &read_buf[offset],
   1199                             wLen);
   1200                         }
   1201 #endif
   1202                         ALOGE("%s: start transceive for length %ld", fn,
   1203                         pTranscv_Info->sSendlength);
   1204 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1205                         status = ALA_SendtoAla(Os_info, status, pTranscv_Info, LS_Sign);
   1206 #else
   1207                         status = ALA_SendtoAla(Os_info, status, pTranscv_Info);
   1208 #endif
   1209                         if(status != STATUS_OK)
   1210                         {
   1211                             return status;
   1212                         }
   1213 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1214                         else
   1215                         {
   1216                             sign_found = STATUS_OK;
   1217                         }
   1218 #endif
   1219                     }
   1220 #if(NXP_LDR_SVC_VER_2 == FALSE)
   1221                 }
   1222                 else
   1223                 {
   1224                     /*
   1225                      * Discard the packet and goto next line
   1226                      * */
   1227                 }
   1228             }
   1229             else
   1230             {
   1231                 ALOGE("Invalid Tag ID");
   1232                 status = STATUS_FAILED;
   1233                 break;
   1234             }
   1235 #endif
   1236         }
   1237 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1238         else if(read_buf[offset] != TAG_JSBL_HDR_ID )
   1239         {
   1240             status = STATUS_FAILED;
   1241         }
   1242 #else
   1243         else if(read_buf[offset] == TAG_ALA_CMD_ID &&
   1244                 key_found == STATUS_OK)
   1245         {
   1246             /*Key match is success and start sending the packet to Ala
   1247              * return status ok
   1248              * */
   1249             ALOGE("TAGID: TAG_ALA_CMD_ID");
   1250             offset = offset+1;
   1251             wLen = 0;
   1252             len_byte = Numof_lengthbytes(&read_buf[offset], &wLen);
   1253             if(len_byte == 0)
   1254             {
   1255                 ALOGE("Invalid length zero");
   1256                 return STATUS_FAILED;
   1257             }
   1258             else
   1259             {
   1260                 offset = offset+len_byte;
   1261                 pTranscv_Info->sSendlength = wLen;
   1262                 memcpy(pTranscv_Info->sSendData, &read_buf[offset], wLen);
   1263             }
   1264 
   1265             status = ALA_SendtoAla(Os_info, status, pTranscv_Info);
   1266             break;
   1267         }
   1268         else if(read_buf[offset] == TAG_JSBL_HDR_ID &&
   1269                 key_found == STATUS_OK)
   1270         {
   1271             /*Key match is success
   1272              * Discard the packets untill we found header T=0x40
   1273              * */
   1274         }
   1275         else
   1276         {
   1277             /*Invalid header*/
   1278             status = STATUS_FAILED;
   1279             break;
   1280         }
   1281 #endif
   1282 
   1283 #if(NXP_LDR_SVC_VER_2 == FALSE)
   1284         }
   1285 #else
   1286     }
   1287     else
   1288     {
   1289         ALOGE("%s : Exit certificate verification failed", fn);
   1290     }
   1291 #endif
   1292 
   1293     ALOGD("%s: exit: status=0x%x", fn, status);
   1294     return status;
   1295 }
   1296 /*******************************************************************************
   1297 **
   1298 ** Function:        ALA_ReadScript
   1299 **
   1300 ** Description:     Reads the current line if the script
   1301 **
   1302 ** Returns:         Success if ok.
   1303 **
   1304 *******************************************************************************/
   1305 tJBL_STATUS ALA_ReadScript(Ala_ImageInfo_t *Os_info, UINT8 *read_buf)
   1306 {
   1307     static const char fn[]="ALA_ReadScript";
   1308     INT32 wCount, wLen, wIndex = 0;
   1309     UINT8 len_byte = 0;
   1310     int wResult = 0;
   1311     tJBL_STATUS status = STATUS_FAILED;
   1312     INT32 lenOff = 1;
   1313 
   1314     ALOGD("%s: enter", fn);
   1315 
   1316     for(wCount =0; (wCount < 2 && !feof(Os_info->fp)); wCount++, wIndex++)
   1317     {
   1318         wResult = FSCANF_BYTE(Os_info->fp,"%2X",(unsigned int*)&read_buf[wIndex]);
   1319     }
   1320     if(wResult == 0)
   1321         return STATUS_FAILED;
   1322 
   1323     Os_info->bytes_read = Os_info->bytes_read + (wCount*2);
   1324 
   1325 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1326     if((read_buf[0]==0x7f) && (read_buf[1]==0x21))
   1327     {
   1328         for(wCount =0; (wCount < 1 && !feof(Os_info->fp)); wCount++, wIndex++)
   1329         {
   1330             wResult = FSCANF_BYTE(Os_info->fp,"%2X",(unsigned int*)&read_buf[wIndex]);
   1331         }
   1332         if(wResult == 0)
   1333         {
   1334             ALOGE("%s: Exit Read Script failed in 7F21 ", fn);
   1335             return STATUS_FAILED;
   1336         }
   1337         /*Read_Script from wCount*2 to wCount*1 */
   1338         Os_info->bytes_read = Os_info->bytes_read + (wCount*2);
   1339         lenOff = 2;
   1340     }
   1341     else if((read_buf[0] == 0x40)||(read_buf[0] == 0x60))
   1342     {
   1343         lenOff = 1;
   1344     }
   1345     /*If TAG is neither 7F21 nor 60 nor 40 then ABORT execution*/
   1346     else
   1347     {
   1348         ALOGE("Invalid TAG 0x%X found in the script", read_buf[0]);
   1349         return STATUS_FAILED;
   1350     }
   1351 #endif
   1352 
   1353     if(read_buf[lenOff] == 0x00)
   1354     {
   1355         ALOGE("Invalid length zero");
   1356         len_byte = 0x00;
   1357         return STATUS_FAILED;
   1358     }
   1359     else if((read_buf[lenOff] & 0x80) == 0x80)
   1360     {
   1361         len_byte = read_buf[lenOff] & 0x0F;
   1362         len_byte = len_byte +1; //1 byte added for byte 0x81
   1363 
   1364         ALOGD("%s: Length byte Read from 0x80 is 0x%x ", fn, len_byte);
   1365 
   1366         if(len_byte == 0x02)
   1367         {
   1368             for(wCount =0; (wCount < 1 && !feof(Os_info->fp)); wCount++, wIndex++)
   1369             {
   1370                 wResult = FSCANF_BYTE(Os_info->fp,"%2X",(unsigned int*)&read_buf[wIndex]);
   1371             }
   1372             if(wResult == 0)
   1373             {
   1374                 ALOGE("%s: Exit Read Script failed in length 0x02 ", fn);
   1375                 return STATUS_FAILED;
   1376             }
   1377 
   1378             wLen = read_buf[lenOff+1];
   1379             Os_info->bytes_read = Os_info->bytes_read + (wCount*2);
   1380             ALOGD("%s: Length of Read Script in len_byte= 0x02 is 0x%lx ", fn, wLen);
   1381         }
   1382         else if(len_byte == 0x03)
   1383         {
   1384             for(wCount =0; (wCount < 2 && !feof(Os_info->fp)); wCount++, wIndex++)
   1385             {
   1386                 wResult = FSCANF_BYTE(Os_info->fp,"%2X",(unsigned int*)&read_buf[wIndex]);
   1387             }
   1388             if(wResult == 0)
   1389             {
   1390                 ALOGE("%s: Exit Read Script failed in length 0x03 ", fn);
   1391                 return STATUS_FAILED;
   1392             }
   1393 
   1394             Os_info->bytes_read = Os_info->bytes_read + (wCount*2);
   1395             wLen = read_buf[lenOff+1]; //Length of the packet send to ALA
   1396             wLen = ((wLen << 8) | (read_buf[lenOff+2]));
   1397             ALOGD("%s: Length of Read Script in len_byte= 0x03 is 0x%lx ", fn, wLen);
   1398         }
   1399         else
   1400         {
   1401             /*Need to provide the support if length is more than 2 bytes*/
   1402             ALOGE("Length recived is greater than 3");
   1403             return STATUS_FAILED;
   1404         }
   1405     }
   1406     else
   1407     {
   1408         len_byte = 0x01;
   1409         wLen = read_buf[lenOff];
   1410         ALOGE("%s: Length of Read Script in len_byte= 0x01 is 0x%lx ", fn, wLen);
   1411     }
   1412 
   1413 
   1414     for(wCount =0; (wCount < wLen && !feof(Os_info->fp)); wCount++, wIndex++)
   1415     {
   1416         wResult = FSCANF_BYTE(Os_info->fp,"%2X",(unsigned int*)&read_buf[wIndex]);
   1417     }
   1418 
   1419     if(wResult == 0)
   1420     {
   1421         ALOGE("%s: Exit Read Script failed in fscanf function ", fn);
   1422         return status;
   1423     }
   1424     else
   1425     {
   1426 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1427         Os_info->bytes_read = Os_info->bytes_read + (wCount*2)+1; //not sure why 2 added
   1428 #else
   1429         Os_info->bytes_read = Os_info->bytes_read + (wCount*2)+2; //not sure why 2 added
   1430 #endif
   1431         status = STATUS_OK;
   1432     }
   1433 
   1434     ALOGD("%s: exit: status=0x%x; Num of bytes read=%d and index=%ld",
   1435     fn, status, Os_info->bytes_read,wIndex);
   1436 
   1437     return status;
   1438 }
   1439 
   1440 /*******************************************************************************
   1441 **
   1442 ** Function:        ALA_SendtoEse
   1443 **
   1444 ** Description:     It is used to send the packet to p61
   1445 **
   1446 ** Returns:         Success if ok.
   1447 **
   1448 *******************************************************************************/
   1449 tJBL_STATUS ALA_SendtoEse(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info)
   1450 {
   1451     static const char fn [] = "ALA_SendtoEse";
   1452     bool stat =false, chanl_open_cmd = false;
   1453     UINT8 xx=0;
   1454     status = STATUS_FAILED;
   1455     INT32 recvBufferActualSize=0, recv_len = 0;
   1456     IChannel_t *mchannel = gpAla_Dwnld_Context->mchannel;
   1457     ALOGD("%s: enter", fn);
   1458 #ifdef JCOP3_WR
   1459     /*
   1460      * Bufferize_load_cmds function is implemented in JCOP
   1461      * */
   1462     status = Bufferize_load_cmds(Os_info, status, pTranscv_Info);
   1463     if(status != STATUS_FAILED)
   1464     {
   1465 #endif
   1466         if(pTranscv_Info->sSendData[1] == 0x70)
   1467         {
   1468             if(pTranscv_Info->sSendData[2] == 0x00)
   1469             {
   1470                 ALOGE("Channel open");
   1471                 chanl_open_cmd = true;
   1472             }
   1473             else
   1474             {
   1475                 ALOGE("Channel close");
   1476                 for(UINT8 cnt=0; cnt < Os_info->channel_cnt; cnt++)
   1477                 {
   1478                     if(Os_info->Channel_Info[cnt].channel_id == pTranscv_Info->sSendData[3])
   1479                     {
   1480                         ALOGE("Closed channel id = 0x0%x", Os_info->Channel_Info[cnt].channel_id);
   1481                         Os_info->Channel_Info[cnt].isOpend = false;
   1482                     }
   1483                 }
   1484             }
   1485         }
   1486         pTranscv_Info->timeout = gTransceiveTimeout;
   1487         pTranscv_Info->sRecvlength = 1024;
   1488         stat = mchannel->transceive(pTranscv_Info->sSendData,
   1489                                     pTranscv_Info->sSendlength,
   1490                                     pTranscv_Info->sRecvData,
   1491                                     pTranscv_Info->sRecvlength,
   1492                                     recvBufferActualSize,
   1493                                     pTranscv_Info->timeout);
   1494         if(stat != TRUE)
   1495         {
   1496             ALOGE("%s: Transceive failed; status=0x%X", fn, stat);
   1497         }
   1498         else
   1499         {
   1500             if(chanl_open_cmd == true)
   1501             {
   1502                 if((recvBufferActualSize == 0x03) &&
   1503                    ((pTranscv_Info->sRecvData[recvBufferActualSize-2] == 0x90) &&
   1504                     (pTranscv_Info->sRecvData[recvBufferActualSize-1] == 0x00)))
   1505                 {
   1506                     ALOGE("open channel success");
   1507                     UINT8 cnt = Os_info->channel_cnt;
   1508                     Os_info->Channel_Info[cnt].channel_id = pTranscv_Info->sRecvData[recvBufferActualSize-3];
   1509                     Os_info->Channel_Info[cnt].isOpend = true;
   1510                     Os_info->channel_cnt++;
   1511                 }
   1512                 else
   1513                 {
   1514                     ALOGE("channel open faield");
   1515                 }
   1516             }
   1517             status = Process_EseResponse(pTranscv_Info, recvBufferActualSize, Os_info);
   1518         }
   1519 #ifdef JCOP3_WR
   1520     }
   1521     else if(SendBack_cmds == false)
   1522     {
   1523         /*
   1524          * Workaround for issue in JCOP
   1525          * Send the fake response back
   1526          * */
   1527         recvBufferActualSize = 0x03;
   1528         pTranscv_Info->sRecvData[0] = 0x00;
   1529         pTranscv_Info->sRecvData[1] = 0x90;
   1530         pTranscv_Info->sRecvData[2] = 0x00;
   1531         status = Process_EseResponse(pTranscv_Info, recvBufferActualSize, Os_info);
   1532     }
   1533     else
   1534     {
   1535 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1536         if(islastcmdLoad == true)
   1537         {
   1538             status = Send_Backall_Loadcmds(Os_info, status, pTranscv_Info);
   1539             SendBack_cmds = false;
   1540         }else
   1541         {
   1542             memset(Cmd_Buffer, 0, sizeof(Cmd_Buffer));
   1543             SendBack_cmds = false;
   1544             status = STATUS_FAILED;
   1545         }
   1546 #else
   1547         status = Send_Backall_Loadcmds(Os_info, status, pTranscv_Info);
   1548         SendBack_cmds = false;
   1549 #endif
   1550     }
   1551 #endif
   1552     ALOGD("%s: exit: status=0x%x", fn, status);
   1553     return status;
   1554 }
   1555 
   1556 /*******************************************************************************
   1557 **
   1558 ** Function:        ALA_SendtoAla
   1559 **
   1560 ** Description:     It is used to forward the packet to Ala
   1561 **
   1562 ** Returns:         Success if ok.
   1563 **
   1564 *******************************************************************************/
   1565 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1566 tJBL_STATUS ALA_SendtoAla(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info, Ls_TagType tType)
   1567 #else
   1568 tJBL_STATUS ALA_SendtoAla(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info)
   1569 #endif
   1570 {
   1571     static const char fn [] = "ALA_SendtoAla";
   1572     bool stat =false;
   1573     status = STATUS_FAILED;
   1574     INT32 recvBufferActualSize = 0;
   1575     IChannel_t *mchannel = gpAla_Dwnld_Context->mchannel;
   1576     ALOGD("%s: enter", fn);
   1577 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1578     pTranscv_Info->sSendData[0] = (0x80 | Os_info->Channel_Info[0].channel_id);
   1579 #else
   1580     pTranscv_Info->sSendData[0] = (pTranscv_Info->sSendData[0] | Os_info->Channel_Info[0].channel_id);
   1581 #endif
   1582     pTranscv_Info->timeout = gTransceiveTimeout;
   1583     pTranscv_Info->sRecvlength = 1024;
   1584 
   1585     stat = mchannel->transceive(pTranscv_Info->sSendData,
   1586                                 pTranscv_Info->sSendlength,
   1587                                 pTranscv_Info->sRecvData,
   1588                                 pTranscv_Info->sRecvlength,
   1589                                 recvBufferActualSize,
   1590                                 pTranscv_Info->timeout);
   1591     if(stat != TRUE)
   1592     {
   1593         ALOGE("%s: Transceive failed; status=0x%X", fn, stat);
   1594     }
   1595     else
   1596     {
   1597 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1598         status = ALA_ProcessResp(Os_info, recvBufferActualSize, pTranscv_Info, tType);
   1599 #else
   1600         status = ALA_ProcessResp(Os_info, recvBufferActualSize, pTranscv_Info);
   1601 #endif
   1602     }
   1603     ALOGD("%s: exit: status=0x%x", fn, status);
   1604     return status;
   1605 }
   1606 /*******************************************************************************
   1607 **
   1608 ** Function:        ALA_CloseChannel
   1609 **
   1610 ** Description:     Closes the previously opened logical channel
   1611 **
   1612 ** Returns:         Success if ok.
   1613 **
   1614 *******************************************************************************/
   1615 tJBL_STATUS ALA_CloseChannel(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info)
   1616 {
   1617     static const char fn [] = "ALA_CloseChannel";
   1618     status = STATUS_FAILED;
   1619     bool stat = false;
   1620     UINT8 xx =0;
   1621     INT32 recvBufferActualSize = 0;
   1622     IChannel_t *mchannel = gpAla_Dwnld_Context->mchannel;
   1623     UINT8 cnt = 0;
   1624     ALOGD("%s: enter",fn);
   1625 
   1626     if(Os_info == NULL ||
   1627        pTranscv_Info == NULL)
   1628     {
   1629         ALOGE("Invalid parameter");
   1630     }
   1631     else
   1632     {
   1633         for(cnt =0; (cnt < Os_info->channel_cnt); cnt++)
   1634         {
   1635             if(Os_info->Channel_Info[cnt].isOpend == false)
   1636                 continue;
   1637             xx = 0;
   1638             pTranscv_Info->sSendData[xx++] = Os_info->Channel_Info[cnt].channel_id;
   1639             pTranscv_Info->sSendData[xx++] = 0x70;
   1640             pTranscv_Info->sSendData[xx++] = 0x80;
   1641             pTranscv_Info->sSendData[xx++] = Os_info->Channel_Info[cnt].channel_id;
   1642             pTranscv_Info->sSendData[xx++] = 0x00;
   1643             pTranscv_Info->sSendlength = xx;
   1644             pTranscv_Info->timeout = gTransceiveTimeout;
   1645             pTranscv_Info->sRecvlength = 1024;
   1646             stat = mchannel->transceive(pTranscv_Info->sSendData,
   1647                                         pTranscv_Info->sSendlength,
   1648                                         pTranscv_Info->sRecvData,
   1649                                         pTranscv_Info->sRecvlength,
   1650                                         recvBufferActualSize,
   1651                                         pTranscv_Info->timeout);
   1652             if(stat != TRUE &&
   1653                recvBufferActualSize < 2)
   1654             {
   1655                 ALOGE("%s: Transceive failed; status=0x%X", fn, stat);
   1656             }
   1657             else if((pTranscv_Info->sRecvData[recvBufferActualSize-2] == 0x90) &&
   1658                     (pTranscv_Info->sRecvData[recvBufferActualSize-1] == 0x00))
   1659             {
   1660                 ALOGE("Close channel id = 0x0%x is success", Os_info->Channel_Info[cnt].channel_id);
   1661                 status = STATUS_OK;
   1662             }
   1663             else
   1664             {
   1665                 ALOGE("Close channel id = 0x0%x is failed", Os_info->Channel_Info[cnt].channel_id);
   1666             }
   1667         }
   1668 
   1669     }
   1670     ALOGD("%s: exit; status=0x0%x", fn, status);
   1671     return status;
   1672 }
   1673 /*******************************************************************************
   1674 **
   1675 ** Function:        ALA_ProcessResp
   1676 **
   1677 ** Description:     Process the response packet received from Ala
   1678 **
   1679 ** Returns:         Success if ok.
   1680 **
   1681 *******************************************************************************/
   1682 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1683 tJBL_STATUS ALA_ProcessResp(Ala_ImageInfo_t *image_info, INT32 recvlen, Ala_TranscieveInfo_t *trans_info, Ls_TagType tType)
   1684 #else
   1685 tJBL_STATUS ALA_ProcessResp(Ala_ImageInfo_t *image_info, INT32 recvlen, Ala_TranscieveInfo_t *trans_info)
   1686 #endif
   1687 {
   1688     static const char fn [] = "ALA_ProcessResp";
   1689     tJBL_STATUS status = STATUS_FAILED;
   1690     static INT32 temp_len = 0;
   1691     UINT8* RecvData = trans_info->sRecvData;
   1692     UINT8 xx =0;
   1693     char sw[2];
   1694 
   1695     ALOGD("%s: enter", fn);
   1696 
   1697     if(RecvData == NULL &&
   1698             recvlen == 0x00)
   1699     {
   1700         ALOGE("%s: Invalid parameter: status=0x%x", fn, status);
   1701         return status;
   1702     }
   1703     else if(recvlen >= 2)
   1704     {
   1705         sw[0] = RecvData[recvlen-2];
   1706         sw[1] = RecvData[recvlen-1];
   1707     }
   1708     else
   1709     {
   1710         ALOGE("%s: Invalid response; status=0x%x", fn, status);
   1711         return status;
   1712     }
   1713 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1714     /*Update the Global variable for storing response length*/
   1715     resp_len = recvlen;
   1716     if((sw[0] != 0x63))
   1717     {
   1718         lsExecuteResp[2] = sw[0];
   1719         lsExecuteResp[3] = sw[1];
   1720         ALOGD("%s: Process Response SW; status = 0x%x", fn, sw[0]);
   1721         ALOGD("%s: Process Response SW; status = 0x%x", fn, sw[1]);
   1722     }
   1723 #endif
   1724     if((recvlen == 0x02) &&
   1725        (sw[0] == 0x90) &&
   1726        (sw[1] == 0x00))
   1727     {
   1728 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1729         tJBL_STATUS wStatus = STATUS_FAILED;
   1730         ALOGE("%s: Before Write Response", fn);
   1731         wStatus = Write_Response_To_OutFile(image_info, RecvData, recvlen, tType);
   1732         if(wStatus != STATUS_FAILED)
   1733 #endif
   1734             status = STATUS_OK;
   1735     }
   1736     else if((recvlen > 0x02) &&
   1737             (sw[0] == 0x90) &&
   1738             (sw[1] == 0x00))
   1739     {
   1740 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1741         tJBL_STATUS wStatus = STATUS_FAILED;
   1742         ALOGE("%s: Before Write Response", fn);
   1743         wStatus = Write_Response_To_OutFile(image_info, RecvData, recvlen, tType);
   1744         if(wStatus != STATUS_FAILED)
   1745             status = STATUS_OK;
   1746 #else
   1747         if(temp_len != 0)
   1748         {
   1749             memcpy((trans_info->sTemp_recvbuf+temp_len), RecvData, (recvlen-2));
   1750             trans_info->sSendlength = temp_len + (recvlen-2);
   1751             memcpy(trans_info->sSendData, trans_info->sTemp_recvbuf, trans_info->sSendlength);
   1752             temp_len = 0;
   1753         }
   1754         else
   1755         {
   1756             memcpy(trans_info->sSendData, RecvData, (recvlen-2));
   1757             trans_info->sSendlength = recvlen-2;
   1758         }
   1759         status = ALA_SendtoEse(image_info, status, trans_info);
   1760 #endif
   1761     }
   1762 #if(NXP_LDR_SVC_VER_2 == FALSE)
   1763     else if ((recvlen > 0x02) &&
   1764              (sw[0] == 0x61))
   1765     {
   1766         memcpy((trans_info->sTemp_recvbuf+temp_len), RecvData, (recvlen-2));
   1767         temp_len = temp_len + recvlen-2;
   1768         trans_info->sSendData[xx++] = image_info->Channel_Info[0].channel_id;
   1769         trans_info->sSendData[xx++] = 0xC0;
   1770         trans_info->sSendData[xx++] = 0x00;
   1771         trans_info->sSendData[xx++] = 0x00;
   1772         trans_info->sSendData[xx++] = sw[1];
   1773         trans_info->sSendlength = xx;
   1774         status = ALA_SendtoAla(image_info, status, trans_info);
   1775     }
   1776 #endif
   1777 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1778     else if ((recvlen > 0x02) &&
   1779             (sw[0] == 0x63) &&
   1780             (sw[1] == 0x10))
   1781     {
   1782         if(temp_len != 0)
   1783         {
   1784             memcpy((trans_info->sTemp_recvbuf+temp_len), RecvData, (recvlen-2));
   1785             trans_info->sSendlength = temp_len + (recvlen-2);
   1786             memcpy(trans_info->sSendData, trans_info->sTemp_recvbuf,
   1787                     trans_info->sSendlength);
   1788             temp_len = 0;
   1789         }
   1790         else
   1791         {
   1792             memcpy(trans_info->sSendData, RecvData, (recvlen-2));
   1793             trans_info->sSendlength = recvlen-2;
   1794         }
   1795         status = ALA_SendtoEse(image_info, status, trans_info);
   1796     }
   1797     else if ((recvlen > 0x02) &&
   1798             (sw[0] == 0x63) &&
   1799             (sw[1] == 0x20))
   1800     {
   1801         UINT8 respLen = 0;
   1802         INT32 wStatus = 0;
   1803 
   1804         AID_ARRAY[0] = recvlen+3;
   1805         AID_ARRAY[1] = 00;
   1806         AID_ARRAY[2] = 0xA4;
   1807         AID_ARRAY[3] = 0x04;
   1808         AID_ARRAY[4] = 0x00;
   1809         AID_ARRAY[5] = recvlen-2;
   1810         memcpy(&AID_ARRAY[6], &RecvData[0],recvlen-2);
   1811         memcpy(&ArrayOfAIDs[2][0], &AID_ARRAY[0], recvlen+4);
   1812 
   1813         fAID_MEM = fopen(AID_MEM_PATH,"w");
   1814 
   1815         if (fAID_MEM == NULL) {
   1816             ALOGE("Error opening AID data for writing: %s",strerror(errno));
   1817             return status;
   1818         }
   1819 
   1820         /*Updating the AID_MEM with new value into AID file*/
   1821         while(respLen <= (recvlen+4))
   1822         {
   1823             wStatus = fprintf(fAID_MEM, "%2x", AID_ARRAY[respLen++]);
   1824             if(wStatus != 2)
   1825             {
   1826                 ALOGE("%s: Invalid Response during fprintf; status=0x%x",
   1827                         fn, status);
   1828                 fclose(fAID_MEM);
   1829                 break;
   1830             }
   1831         }
   1832         if(wStatus == 2)
   1833         {
   1834             status = STATUS_FILE_NOT_FOUND;
   1835         }
   1836         else
   1837         {
   1838            status = STATUS_FAILED;
   1839         }
   1840     }
   1841     else if((recvlen >= 0x02) &&(
   1842             (sw[0] != 0x90) &&
   1843             (sw[0] != 0x63)&&(sw[0] != 0x61)))
   1844     {
   1845         tJBL_STATUS wStatus = STATUS_FAILED;
   1846         wStatus = Write_Response_To_OutFile(image_info, RecvData, recvlen, tType);
   1847         //if(wStatus != STATUS_FAILED)
   1848             //status = STATUS_OK;
   1849     }
   1850 #endif
   1851     ALOGD("%s: exit: status=0x%x", fn, status);
   1852     return status;
   1853 }
   1854 /*******************************************************************************
   1855 **
   1856 ** Function:        ALA_SendtoEse
   1857 **
   1858 ** Description:     It is used to process the received response packet from p61
   1859 **
   1860 ** Returns:         Success if ok.
   1861 **
   1862 *******************************************************************************/
   1863 tJBL_STATUS Process_EseResponse(Ala_TranscieveInfo_t *pTranscv_Info, INT32 recv_len, Ala_ImageInfo_t *Os_info)
   1864 {
   1865     static const char fn[] = "Process_EseResponse";
   1866     tJBL_STATUS status = STATUS_OK;
   1867     UINT8 xx = 0;
   1868     ALOGD("%s: enter", fn);
   1869 
   1870     pTranscv_Info->sSendData[xx++] = (CLA_BYTE | Os_info->Channel_Info[0].channel_id);
   1871 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1872     pTranscv_Info->sSendData[xx++] = 0xA2;
   1873 #else
   1874     pTranscv_Info->sSendData[xx++] = 0xA0;
   1875 #endif
   1876     if(recv_len <= 0xFF)
   1877     {
   1878 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1879         pTranscv_Info->sSendData[xx++] = 0x80;
   1880 #else
   1881         pTranscv_Info->sSendData[xx++] = ONLY_BLOCK;
   1882 #endif
   1883         pTranscv_Info->sSendData[xx++] = 0x00;
   1884         pTranscv_Info->sSendData[xx++] = (UINT8)recv_len;
   1885         memcpy(&(pTranscv_Info->sSendData[xx]),pTranscv_Info->sRecvData,recv_len);
   1886         pTranscv_Info->sSendlength = xx+ recv_len;
   1887 #if(NXP_LDR_SVC_VER_2)
   1888         status = ALA_SendtoAla(Os_info, status, pTranscv_Info, LS_Comm);
   1889 #else
   1890         status = ALA_SendtoAla(Os_info, status, pTranscv_Info);
   1891 #endif
   1892     }
   1893     else
   1894     {
   1895         while(recv_len > MAX_SIZE)
   1896         {
   1897             xx = PARAM_P1_OFFSET;
   1898 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1899             pTranscv_Info->sSendData[xx++] = 0x00;
   1900 #else
   1901             pTranscv_Info->sSendData[xx++] = FIRST_BLOCK;
   1902 #endif
   1903             pTranscv_Info->sSendData[xx++] = 0x00;
   1904             pTranscv_Info->sSendData[xx++] = MAX_SIZE;
   1905             recv_len = recv_len - MAX_SIZE;
   1906             memcpy(&(pTranscv_Info->sSendData[xx]),pTranscv_Info->sRecvData,MAX_SIZE);
   1907             pTranscv_Info->sSendlength = xx+ MAX_SIZE;
   1908 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1909             /*Need not store Process eSE response's response in the out file so
   1910              * LS_Comm = 0*/
   1911             status = ALA_SendtoAla(Os_info, status, pTranscv_Info, LS_Comm);
   1912 #else
   1913             status = ALA_SendtoAla(Os_info, status, pTranscv_Info);
   1914 #endif
   1915             if(status != STATUS_OK)
   1916             {
   1917                 ALOGE("Sending packet to Ala failed: status=0x%x", status);
   1918                 return status;
   1919             }
   1920         }
   1921         xx = PARAM_P1_OFFSET;
   1922         pTranscv_Info->sSendData[xx++] = LAST_BLOCK;
   1923         pTranscv_Info->sSendData[xx++] = 0x01;
   1924         pTranscv_Info->sSendData[xx++] = recv_len;
   1925         memcpy(&(pTranscv_Info->sSendData[xx]),pTranscv_Info->sRecvData,recv_len);
   1926         pTranscv_Info->sSendlength = xx+ recv_len;
   1927 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1928             status = ALA_SendtoAla(Os_info, status, pTranscv_Info, LS_Comm);
   1929 #else
   1930             status = ALA_SendtoAla(Os_info, status, pTranscv_Info);
   1931 #endif
   1932     }
   1933     ALOGD("%s: exit: status=0x%x", fn, status);
   1934     return status;
   1935 }
   1936 /*******************************************************************************
   1937 **
   1938 ** Function:        Process_SelectRsp
   1939 **
   1940 ** Description:     It is used to process the received response for SELECT ALA cmd
   1941 **
   1942 ** Returns:         Success if ok.
   1943 **
   1944 *******************************************************************************/
   1945 tJBL_STATUS Process_SelectRsp(UINT8* Recv_data, INT32 Recv_len)
   1946 {
   1947     static const char fn[]="Process_SelectRsp";
   1948     tJBL_STATUS status = STATUS_FAILED;
   1949     int i = 0, len=0;
   1950     ALOGE("%s: enter", fn);
   1951 
   1952     if(Recv_data[i] == TAG_SELECT_ID)
   1953     {
   1954         ALOGD("TAG: TAG_SELECT_ID");
   1955         i = i +1;
   1956         len = Recv_data[i];
   1957         i = i+1;
   1958         if(Recv_data[i] == TAG_ALA_ID)
   1959         {
   1960             ALOGD("TAG: TAG_ALA_ID");
   1961             i = i+1;
   1962             len = Recv_data[i];
   1963             i = i + 1 + len; //points to next tag name A5
   1964 #if(NXP_LDR_SVC_VER_2 == TRUE)
   1965             //points to TAG 9F08 for LS application version
   1966             if((Recv_data[i] == TAG_LS_VER1)&&(Recv_data[i+1] == TAG_LS_VER2))
   1967             {
   1968                 UINT8 lsaVersionLen = 0;
   1969                 ALOGD("TAG: TAG_LS_APPLICATION_VER");
   1970 
   1971                 i = i+2;
   1972                 lsaVersionLen = Recv_data[i];
   1973                 //points to TAG 9F08 LS application version
   1974                 i = i+1;
   1975                 memcpy(lsVersionArr, &Recv_data[i],lsaVersionLen);
   1976 
   1977                 //points to Identifier of the Root Entity key set identifier
   1978                 i = i+lsaVersionLen;
   1979 
   1980                 if(Recv_data[i] == TAG_RE_KEYID)
   1981                 {
   1982                     UINT8 rootEntityLen = 0;
   1983                     i = i+1;
   1984                     rootEntityLen = Recv_data[i];
   1985 
   1986                     i = i+1;
   1987                     if(Recv_data[i] == TAG_LSRE_ID)
   1988                     {
   1989                         UINT8 tag42Len = 0;
   1990                         i = i+1;
   1991                         tag42Len = Recv_data[i];
   1992                         //copy the data including length
   1993                         memcpy(tag42Arr, &Recv_data[i], tag42Len+1);
   1994                         i = i+tag42Len+1;
   1995 
   1996                         if(Recv_data[i] == TAG_LSRE_SIGNID)
   1997                         {
   1998                             UINT8 tag45Len = Recv_data[i+1];
   1999                             memcpy(tag45Arr, &Recv_data[i+1],tag45Len+1);
   2000                             status = STATUS_OK;
   2001                         }
   2002                         else
   2003                         {
   2004                             ALOGE("Invalid Root entity for TAG 45 = 0x%x; "
   2005                             "status=0x%x", Recv_data[i], status);
   2006                             return status;
   2007                         }
   2008                     }
   2009                     else
   2010                     {
   2011                         ALOGE("Invalid Root entity for TAG 42 = 0x%x; "
   2012                         "status=0x%x", Recv_data[i], status);
   2013                         return status;
   2014                     }
   2015                 }
   2016                 else
   2017                 {
   2018                     ALOGE("Invalid Root entity key set TAG ID = 0x%x; "
   2019                     "status=0x%x", Recv_data[i], status);
   2020                     return status;
   2021                 }
   2022             }
   2023         }
   2024         else
   2025         {
   2026             ALOGE("Invalid Loader Service AID TAG ID = 0x%x; status=0x%x",
   2027             Recv_data[i], status);
   2028             return status;
   2029         }
   2030     }
   2031     else
   2032     {
   2033         ALOGE("Invalid FCI TAG = 0x%x; status=0x%x", Recv_data[i], status);
   2034         return status;
   2035     }
   2036 #else
   2037             if(Recv_data[i] == TAG_PRO_DATA_ID)
   2038             {
   2039                 ALOGE("TAG: TAG_PRO_DATA_ID");
   2040                 i = i+1;
   2041                 len = Recv_data[i];
   2042                 i = i + 1; //points to next tag name 61
   2043             }
   2044         }
   2045     }
   2046     else
   2047     {
   2048         /*
   2049          * Invalid start of TAG Name found
   2050          * */
   2051         ALOGE("Invalid TAG ID = 0x%x; status=0x%x", Recv_data[i], status);
   2052         return status;
   2053     }
   2054 
   2055     if((i < Recv_len) &&
   2056        (Recv_data[i] == TAG_JSBL_KEY_ID))
   2057     {
   2058         /*
   2059          * Valid Key is found
   2060          * Copy the data into Select_Rsp
   2061          * */
   2062         ALOGE("Valid key id is found");
   2063         i = i +1;
   2064         len = Recv_data[i];
   2065         if(len != 0x00)
   2066         {
   2067             i = i+1;
   2068             memcpy(Select_Rsp, &Recv_data[i], len);
   2069             Select_Rsp_Len = len;
   2070             status = STATUS_OK;
   2071         }
   2072         /*
   2073          * Identifier of the certificate storing
   2074          * JSBL encryption key
   2075          * */
   2076         i = i + len;
   2077         if(Recv_data[i] == TAG_JSBL_CER_ID)
   2078         {
   2079             i = i+1;
   2080             len = Recv_data[i];
   2081             if(len != 0x00)
   2082             {
   2083                 i = i+1;
   2084                 Jsbl_keylen = len;
   2085                 memcpy(Jsbl_RefKey, &Recv_data[i], len);
   2086             }
   2087         }
   2088     }
   2089 #endif
   2090     ALOGE("%s: Exiting status = 0x%x", fn, status);
   2091     return status;
   2092 }
   2093 
   2094 
   2095 #ifdef JCOP3_WR
   2096 tJBL_STATUS Bufferize_load_cmds(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info)
   2097 {
   2098     static const char fn[] = "Bufferize_load_cmds";
   2099     UINT8 Param_P2;
   2100     status = STATUS_FAILED;
   2101 
   2102     if(cmd_count == 0x00)
   2103     {
   2104         if((pTranscv_Info->sSendData[1] == INSTAL_LOAD_ID) &&
   2105            (pTranscv_Info->sSendData[2] == PARAM_P1_OFFSET) &&
   2106            (pTranscv_Info->sSendData[3] == 0x00))
   2107         {
   2108             ALOGE("BUffer: install for load");
   2109             pBuffer[0] = pTranscv_Info->sSendlength;
   2110             memcpy(&pBuffer[1], &(pTranscv_Info->sSendData[0]), pTranscv_Info->sSendlength);
   2111             pBuffer = pBuffer + pTranscv_Info->sSendlength + 1;
   2112             cmd_count++;
   2113         }
   2114         else
   2115         {
   2116             /*
   2117              * Do not buffer this cmd
   2118              * Send this command to eSE
   2119              * */
   2120             status = STATUS_OK;
   2121         }
   2122 
   2123     }
   2124     else
   2125     {
   2126         Param_P2 = cmd_count -1;
   2127         if((pTranscv_Info->sSendData[1] == LOAD_CMD_ID) &&
   2128            (pTranscv_Info->sSendData[2] == LOAD_MORE_BLOCKS) &&
   2129            (pTranscv_Info->sSendData[3] == Param_P2))
   2130         {
   2131             ALOGE("BUffer: load");
   2132             pBuffer[0] = pTranscv_Info->sSendlength;
   2133             memcpy(&pBuffer[1], &(pTranscv_Info->sSendData[0]), pTranscv_Info->sSendlength);
   2134             pBuffer = pBuffer + pTranscv_Info->sSendlength + 1;
   2135             cmd_count++;
   2136         }
   2137         else if((pTranscv_Info->sSendData[1] == LOAD_CMD_ID) &&
   2138                 (pTranscv_Info->sSendData[2] == LOAD_LAST_BLOCK) &&
   2139                 (pTranscv_Info->sSendData[3] == Param_P2))
   2140         {
   2141             ALOGE("BUffer: last load");
   2142             SendBack_cmds = true;
   2143             pBuffer[0] = pTranscv_Info->sSendlength;
   2144             memcpy(&pBuffer[1], &(pTranscv_Info->sSendData[0]), pTranscv_Info->sSendlength);
   2145             pBuffer = pBuffer + pTranscv_Info->sSendlength + 1;
   2146             cmd_count++;
   2147             islastcmdLoad = true;
   2148         }
   2149         else
   2150         {
   2151             ALOGE("BUffer: Not a load cmd");
   2152             SendBack_cmds = true;
   2153             pBuffer[0] = pTranscv_Info->sSendlength;
   2154             memcpy(&pBuffer[1], &(pTranscv_Info->sSendData[0]), pTranscv_Info->sSendlength);
   2155             pBuffer = pBuffer + pTranscv_Info->sSendlength + 1;
   2156             islastcmdLoad = false;
   2157             cmd_count++;
   2158         }
   2159     }
   2160     ALOGE("%s: exit; status=0x%x", fn, status);
   2161     return status;
   2162 }
   2163 
   2164 tJBL_STATUS Send_Backall_Loadcmds(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info)
   2165 {
   2166     static const char fn [] = "Send_Backall_Loadcmds";
   2167     bool stat =false;
   2168     UINT8 xx=0;
   2169     status = STATUS_FAILED;
   2170     INT32 recvBufferActualSize=0, recv_len = 0;
   2171     IChannel_t *mchannel = gpAla_Dwnld_Context->mchannel;
   2172     ALOGD("%s: enter", fn);
   2173     pBuffer = Cmd_Buffer; // Points to start of first cmd to send
   2174     if(cmd_count == 0x00)
   2175     {
   2176         ALOGE("No cmds to stored to send to eSE");
   2177     }
   2178     else
   2179     {
   2180         while(cmd_count-- > 0)
   2181         {
   2182             pTranscv_Info->sSendlength = pBuffer[0];
   2183             memcpy(pTranscv_Info->sSendData, &pBuffer[1], pTranscv_Info->sSendlength);
   2184             pBuffer = pBuffer + 1 + pTranscv_Info->sSendlength;
   2185 
   2186             stat = mchannel->transceive(pTranscv_Info->sSendData,
   2187                                         pTranscv_Info->sSendlength,
   2188                                         pTranscv_Info->sRecvData,
   2189                                         pTranscv_Info->sRecvlength,
   2190                                         recvBufferActualSize,
   2191                                         pTranscv_Info->timeout);
   2192 
   2193             if(stat != TRUE ||
   2194                (recvBufferActualSize < 2))
   2195             {
   2196                 ALOGE("%s: Transceive failed; status=0x%X", fn, stat);
   2197             }
   2198             else if(cmd_count == 0x00) //Last command in the buffer
   2199             {
   2200 
   2201                 if (islastcmdLoad == false)
   2202                 {
   2203                     status = Process_EseResponse(pTranscv_Info, recvBufferActualSize, Os_info);
   2204                 }
   2205                 else if((recvBufferActualSize == 0x02) &&
   2206                         (pTranscv_Info->sRecvData[recvBufferActualSize-2] == 0x90) &&
   2207                         (pTranscv_Info->sRecvData[recvBufferActualSize-1] == 0x00))
   2208                 {
   2209                     recvBufferActualSize = 0x03;
   2210                     pTranscv_Info->sRecvData[0] = 0x00;
   2211                     pTranscv_Info->sRecvData[1] = 0x90;
   2212                     pTranscv_Info->sRecvData[2] = 0x00;
   2213                     status = Process_EseResponse(pTranscv_Info, recvBufferActualSize, Os_info);
   2214                 }
   2215                 else
   2216                 {
   2217                     status = Process_EseResponse(pTranscv_Info, recvBufferActualSize, Os_info);
   2218                 }
   2219             }
   2220             else if((recvBufferActualSize == 0x02) &&
   2221                     (pTranscv_Info->sRecvData[0] == 0x90) &&
   2222                     (pTranscv_Info->sRecvData[1] == 0x00))
   2223             {
   2224                 /*Do not do anything
   2225                  * send next command in the buffer*/
   2226             }
   2227             else if((recvBufferActualSize == 0x03) &&
   2228                     (pTranscv_Info->sRecvData[0] == 0x00) &&
   2229                     (pTranscv_Info->sRecvData[1] == 0x90) &&
   2230                     (pTranscv_Info->sRecvData[2] == 0x00))
   2231             {
   2232                 /*Do not do anything
   2233                  * Send next cmd in the buffer*/
   2234             }
   2235             else if((pTranscv_Info->sRecvData[recvBufferActualSize-2] != 0x90) &&
   2236                     (pTranscv_Info->sRecvData[recvBufferActualSize-1] != 0x00))
   2237             {
   2238                 /*Error condition hence exiting the loop*/
   2239                 status = Process_EseResponse(pTranscv_Info, recvBufferActualSize, Os_info);
   2240                 /*If the sending of Load fails reset the count*/
   2241                 cmd_count=0;
   2242                 break;
   2243             }
   2244         }
   2245     }
   2246     memset(Cmd_Buffer, 0, sizeof(Cmd_Buffer));
   2247     pBuffer = Cmd_Buffer; //point back to start of line
   2248     cmd_count = 0x00;
   2249     ALOGD("%s: exit: status=0x%x", fn, status);
   2250     return status;
   2251 }
   2252 #endif
   2253 /*******************************************************************************
   2254 **
   2255 ** Function:        Numof_lengthbytes
   2256 **
   2257 ** Description:     Checks the number of length bytes and assigns
   2258 **                  length value to wLen.
   2259 **
   2260 ** Returns:         Number of Length bytes
   2261 **
   2262 *******************************************************************************/
   2263 UINT8 Numof_lengthbytes(UINT8 *read_buf, INT32 *pLen)
   2264 {
   2265     static const char fn[]= "Numof_lengthbytes";
   2266     UINT8 len_byte=0, i=0;
   2267     INT32 wLen = 0;
   2268     ALOGE("%s:enter", fn);
   2269 
   2270     if(read_buf[i] == 0x00)
   2271     {
   2272         ALOGE("Invalid length zero");
   2273         len_byte = 0x00;
   2274     }
   2275     else if((read_buf[i] & 0x80) == 0x80)
   2276     {
   2277         len_byte = read_buf[i] & 0x0F;
   2278         len_byte = len_byte +1; //1 byte added for byte 0x81
   2279     }
   2280     else
   2281     {
   2282         len_byte = 0x01;
   2283     }
   2284     /*
   2285      * To get the length of the value field
   2286      * */
   2287     switch(len_byte)
   2288     {
   2289     case 0:
   2290         wLen = read_buf[0];
   2291         break;
   2292     case 1:
   2293         /*1st byte is the length*/
   2294         wLen = read_buf[0];
   2295         break;
   2296     case 2:
   2297         /*2nd byte is the length*/
   2298         wLen = read_buf[1];
   2299         break;
   2300     case 3:
   2301         /*1st and 2nd bytes are length*/
   2302         wLen = read_buf[1];
   2303         wLen = ((wLen << 8) | (read_buf[2]));
   2304         break;
   2305     case 4:
   2306         /*3bytes are the length*/
   2307         wLen = read_buf[1];
   2308         wLen = ((wLen << 16) | (read_buf[2] << 8));
   2309         wLen = (wLen | (read_buf[3]));
   2310         break;
   2311     default:
   2312         ALOGE("default case");
   2313         break;
   2314     }
   2315 
   2316     *pLen = wLen;
   2317     ALOGE("%s:exit; len_bytes=0x0%x, Length=%ld", fn, len_byte, *pLen);
   2318     return len_byte;
   2319 }
   2320 #if(NXP_LDR_SVC_VER_2 == TRUE)
   2321 /*******************************************************************************
   2322 **
   2323 ** Function:        Write_Response_To_OutFile
   2324 **
   2325 ** Description:     Write the response to Out file
   2326 **                  with length recvlen from buffer RecvData.
   2327 **
   2328 ** Returns:         Success if OK
   2329 **
   2330 *******************************************************************************/
   2331 tJBL_STATUS Write_Response_To_OutFile(Ala_ImageInfo_t *image_info, UINT8* RecvData,
   2332     INT32 recvlen, Ls_TagType tType)
   2333 {
   2334     INT32 respLen       = 0;
   2335     tJBL_STATUS wStatus = STATUS_FAILED;
   2336     static const char fn [] = "Write_Response_to_OutFile";
   2337     INT32 status = 0;
   2338     UINT8 tagBuffer[12] = {0x61,0,0,0,0,0,0,0,0,0,0,0};
   2339     INT32 tag44Len = 0;
   2340     INT32 tag61Len = 0;
   2341     UINT8 tag43Len = 1;
   2342     UINT8 tag43off = 0;
   2343     UINT8 tag44off = 0;
   2344     UINT8 ucTag44[3] = {0x00,0x00,0x00};
   2345     UINT8 tagLen = 0;
   2346     UINT8 tempLen = 0;
   2347     /*If the Response out file is NULL or Other than LS commands*/
   2348     if((image_info->bytes_wrote == 0x55)||(tType == LS_Default))
   2349     {
   2350         return STATUS_OK;
   2351     }
   2352     /*Certificate TAG occupies 2 bytes*/
   2353     if(tType == LS_Cert)
   2354     {
   2355         tag43Len = 2;
   2356     }
   2357     ALOGE("%s: Enter", fn);
   2358 
   2359     /* |TAG | LEN(BERTLV)|                                VAL                         |
   2360      * | 61 |      XX    |  TAG | LEN |     VAL    | TAG | LEN(BERTLV) |      VAL     |
   2361      *                   |  43  | 1/2 | 7F21/60/40 | 44  | apduRespLen | apduResponse |
   2362      **/
   2363     if(recvlen < 0x80)
   2364     {
   2365         tag44Len = 1;
   2366         ucTag44[0] = recvlen;
   2367         tag61Len = recvlen + 4 + tag43Len;
   2368 
   2369         if(tag61Len&0x80)
   2370         {
   2371             tagBuffer[1] = 0x81;
   2372             tagBuffer[2] = tag61Len;
   2373             tag43off = 3;
   2374             tag44off = 5+tag43Len;
   2375             tagLen = tag44off+2;
   2376         }
   2377         else
   2378         {
   2379             tagBuffer[1] = tag61Len;
   2380             tag43off = 2;
   2381             tag44off = 4+tag43Len;
   2382             tagLen = tag44off+2;
   2383         }
   2384     }
   2385     else if((recvlen >= 0x80)&&(recvlen <= 0xFF))
   2386     {
   2387         ucTag44[0] = 0x81;
   2388         ucTag44[1] = recvlen;
   2389         tag61Len = recvlen + 5 + tag43Len;
   2390         tag44Len = 2;
   2391 
   2392         if((tag61Len&0xFF00) != 0)
   2393         {
   2394             tagBuffer[1] = 0x82;
   2395             tagBuffer[2] = (tag61Len & 0xFF00)>>8;
   2396             tagBuffer[3] = (tag61Len & 0xFF);
   2397             tag43off = 4;
   2398             tag44off = 6+tag43Len;
   2399             tagLen = tag44off+3;
   2400         }
   2401         else
   2402         {
   2403             tagBuffer[1] = 0x81;
   2404             tagBuffer[2] = (tag61Len & 0xFF);
   2405             tag43off = 3;
   2406             tag44off = 5+tag43Len;
   2407             tagLen = tag44off+3;
   2408         }
   2409     }
   2410     else if((recvlen > 0xFF) &&(recvlen <= 0xFFFF))
   2411     {
   2412         ucTag44[0] = 0x82;
   2413         ucTag44[1] = (recvlen&0xFF00)>>8;
   2414         ucTag44[2] = (recvlen&0xFF);
   2415         tag44Len = 3;
   2416 
   2417         tag61Len = recvlen + 6 + tag43Len;
   2418 
   2419         if((tag61Len&0xFF00) != 0)
   2420         {
   2421             tagBuffer[1] = 0x82;
   2422             tagBuffer[2] = (tag61Len & 0xFF00)>>8;
   2423             tagBuffer[3] = (tag61Len & 0xFF);
   2424             tag43off = 4;
   2425             tag44off = 6+tag43Len;
   2426             tagLen = tag44off+4;
   2427         }
   2428     }
   2429     tagBuffer[tag43off] = 0x43;
   2430     tagBuffer[tag43off+1] = tag43Len;
   2431     tagBuffer[tag44off] = 0x44;
   2432     memcpy(&tagBuffer[tag44off+1], &ucTag44[0],tag44Len);
   2433 
   2434 
   2435     if(tType == LS_Cert)
   2436     {
   2437         tagBuffer[tag43off+2] = 0x7F;
   2438         tagBuffer[tag43off+3] = 0x21;
   2439     }
   2440     else if(tType == LS_Sign)
   2441     {
   2442         tagBuffer[tag43off+2] = 0x60;
   2443     }
   2444     else if(tType == LS_Comm)
   2445     {
   2446         tagBuffer[tag43off+2] = 0x40;
   2447     }
   2448     else
   2449     {
   2450        /*Do nothing*/
   2451     }
   2452     while(tempLen < tagLen)
   2453     {
   2454         status = fprintf(image_info->fResp, "%02X", tagBuffer[tempLen++]);
   2455         if(status != 2)
   2456         {
   2457             ALOGE("%s: Invalid Response during fprintf; status=0x%lx", fn, (status));
   2458             wStatus = STATUS_FAILED;
   2459             break;
   2460         }
   2461     }
   2462     /*Updating the response data into out script*/
   2463     while(respLen < recvlen)
   2464     {
   2465         status = fprintf(image_info->fResp, "%02X", RecvData[respLen++]);
   2466         if(status != 2)
   2467         {
   2468             ALOGE("%s: Invalid Response during fprintf; status=0x%lx", fn, (status));
   2469             wStatus = STATUS_FAILED;
   2470             break;
   2471         }
   2472     }
   2473     if((status == 2))
   2474     {
   2475         fprintf(image_info->fResp, "%s\n", "");
   2476         ALOGE("%s: SUCCESS Response written to script out file; status=0x%lx", fn, (status));
   2477         wStatus = STATUS_OK;
   2478     }
   2479     return wStatus;
   2480 }
   2481 
   2482 /*******************************************************************************
   2483 **
   2484 ** Function:        Check_Certificate_Tag
   2485 **
   2486 ** Description:     Check certificate Tag presence in script
   2487 **                  by 7F21 .
   2488 **
   2489 ** Returns:         Success if Tag found
   2490 **
   2491 *******************************************************************************/
   2492 tJBL_STATUS Check_Certificate_Tag(UINT8 *read_buf, UINT16 *offset1)
   2493 {
   2494     tJBL_STATUS status = STATUS_FAILED;
   2495     UINT16 len_byte = 0;
   2496     INT32 wLen, recvBufferActualSize=0;
   2497     UINT16 offset = *offset1;
   2498 
   2499     if(((read_buf[offset]<<8|read_buf[offset+1]) == TAG_CERTIFICATE))
   2500     {
   2501         ALOGD("TAGID: TAG_CERTIFICATE");
   2502         offset = offset+2;
   2503         len_byte = Numof_lengthbytes(&read_buf[offset], &wLen);
   2504         offset = offset + len_byte;
   2505         *offset1 = offset;
   2506         if(wLen <= MAX_CERT_LEN)
   2507         status = STATUS_OK;
   2508     }
   2509     return status;
   2510 }
   2511 
   2512 /*******************************************************************************
   2513 **
   2514 ** Function:        Check_SerialNo_Tag
   2515 **
   2516 ** Description:     Check Serial number Tag presence in script
   2517 **                  by 0x93 .
   2518 **
   2519 ** Returns:         Success if Tag found
   2520 **
   2521 *******************************************************************************/
   2522 tJBL_STATUS Check_SerialNo_Tag(UINT8 *read_buf, UINT16 *offset1)
   2523 {
   2524     tJBL_STATUS status = STATUS_FAILED;
   2525     UINT16 offset = *offset1;
   2526     static const char fn[] = "Check_SerialNo_Tag";
   2527 
   2528     if((read_buf[offset] == TAG_SERIAL_NO))
   2529     {
   2530         ALOGD("TAGID: TAG_SERIAL_NO");
   2531         UINT8 serNoLen = read_buf[offset+1];
   2532         offset = offset + serNoLen + 2;
   2533         *offset1 = offset;
   2534         ALOGD("%s: TAG_LSROOT_ENTITY is %x", fn, read_buf[offset]);
   2535         status = STATUS_OK;
   2536     }
   2537     return status;
   2538 }
   2539 
   2540 /*******************************************************************************
   2541 **
   2542 ** Function:        Check_LSRootID_Tag
   2543 **
   2544 ** Description:     Check LS root ID tag presence in script and compare with
   2545 **                  select response root ID value.
   2546 **
   2547 ** Returns:         Success if Tag found
   2548 **
   2549 *******************************************************************************/
   2550 tJBL_STATUS Check_LSRootID_Tag(UINT8 *read_buf, UINT16 *offset1)
   2551 {
   2552     tJBL_STATUS status = STATUS_FAILED;
   2553     UINT16 offset      = *offset1;
   2554 
   2555     if(read_buf[offset] == TAG_LSRE_ID)
   2556     {
   2557         ALOGD("TAGID: TAG_LSROOT_ENTITY");
   2558         if(tag42Arr[0] == read_buf[offset+1])
   2559         {
   2560             UINT8 tag42Len = read_buf[offset+1];
   2561             offset = offset+2;
   2562             status = memcmp(&read_buf[offset],&tag42Arr[1],tag42Arr[0]);
   2563             ALOGD("ALA_Check_KeyIdentifier : TAG 42 verified");
   2564 
   2565             if(status == STATUS_OK)
   2566             {
   2567                 ALOGD("ALA_Check_KeyIdentifier : Loader service root entity "
   2568                 "ID is matched");
   2569                 offset = offset+tag42Len;
   2570                 *offset1 = offset;
   2571                 }
   2572         }
   2573     }
   2574     return status;
   2575 }
   2576 
   2577 /*******************************************************************************
   2578 **
   2579 ** Function:        Check_CertHoldID_Tag
   2580 **
   2581 ** Description:     Check certificate holder ID tag presence in script.
   2582 **
   2583 ** Returns:         Success if Tag found
   2584 **
   2585 *******************************************************************************/
   2586 tJBL_STATUS Check_CertHoldID_Tag(UINT8 *read_buf, UINT16 *offset1)
   2587 {
   2588     tJBL_STATUS status = STATUS_FAILED;
   2589     UINT16 offset      = *offset1;
   2590 
   2591     if((read_buf[offset]<<8|read_buf[offset+1]) == TAG_CERTFHOLD_ID)
   2592     {
   2593         UINT8 certfHoldIDLen = 0;
   2594         ALOGD("TAGID: TAG_CERTFHOLD_ID");
   2595         certfHoldIDLen = read_buf[offset+2];
   2596         offset = offset+certfHoldIDLen+3;
   2597         if(read_buf[offset] == TAG_KEY_USAGE)
   2598         {
   2599             UINT8 keyusgLen = 0;
   2600             ALOGD("TAGID: TAG_KEY_USAGE");
   2601             keyusgLen = read_buf[offset+1];
   2602             offset = offset+keyusgLen+2;
   2603             *offset1 = offset;
   2604             status = STATUS_OK;
   2605         }
   2606     }
   2607     return status;
   2608 }
   2609 
   2610 /*******************************************************************************
   2611 **
   2612 ** Function:        Check_Date_Tag
   2613 **
   2614 ** Description:     Check date tags presence in script.
   2615 **
   2616 ** Returns:         Success if Tag found
   2617 **
   2618 *******************************************************************************/
   2619 tJBL_STATUS Check_Date_Tag(UINT8 *read_buf, UINT16 *offset1)
   2620 {
   2621     tJBL_STATUS status = STATUS_OK;
   2622     UINT16 offset      = *offset1;
   2623 
   2624     if((read_buf[offset]<<8|read_buf[offset+1]) == TAG_EFF_DATE)
   2625     {
   2626         UINT8 effDateLen = read_buf[offset+2];
   2627         offset = offset+3+effDateLen;
   2628         ALOGD("TAGID: TAG_EFF_DATE");
   2629         if((read_buf[offset]<<8|read_buf[offset+1]) == TAG_EXP_DATE)
   2630         {
   2631             UINT8 effExpLen = read_buf[offset+2];
   2632             offset = offset+3+effExpLen;
   2633             ALOGD("TAGID: TAG_EXP_DATE");
   2634             status = STATUS_OK;
   2635         }else if(read_buf[offset] == TAG_LSRE_SIGNID)
   2636         {
   2637             status = STATUS_OK;
   2638         }
   2639     }
   2640     else if((read_buf[offset]<<8|read_buf[offset+1]) == TAG_EXP_DATE)
   2641     {
   2642         UINT8 effExpLen = read_buf[offset+2];
   2643         offset = offset+3+effExpLen;
   2644         ALOGD("TAGID: TAG_EXP_DATE");
   2645         status = STATUS_OK;
   2646     }else if(read_buf[offset] == TAG_LSRE_SIGNID)
   2647     {
   2648         status = STATUS_OK;
   2649     }
   2650     else
   2651     {
   2652     /*STATUS_FAILED*/
   2653     }
   2654     *offset1 = offset;
   2655     return status;
   2656 }
   2657 
   2658 
   2659 /*******************************************************************************
   2660 **
   2661 ** Function:        Check_45_Tag
   2662 **
   2663 ** Description:     Check 45 tags presence in script and compare the value
   2664 **                  with select response tag 45 value
   2665 **
   2666 ** Returns:         Success if Tag found
   2667 **
   2668 *******************************************************************************/
   2669 tJBL_STATUS Check_45_Tag(UINT8 *read_buf, UINT16 *offset1, UINT8 *tag45Len)
   2670 {
   2671     tJBL_STATUS status = STATUS_FAILED;
   2672     UINT16 offset      = *offset1;
   2673     if(read_buf[offset] == TAG_LSRE_SIGNID)
   2674     {
   2675         *tag45Len = read_buf[offset+1];
   2676         offset = offset+2;
   2677         if(tag45Arr[0] == *tag45Len)
   2678         {
   2679             status = memcmp(&read_buf[offset],&tag45Arr[1],tag45Arr[0]);
   2680             if(status == STATUS_OK)
   2681             {
   2682                 ALOGD("ALA_Check_KeyIdentifier : TAG 45 verified");
   2683                 *offset1 = offset;
   2684             }
   2685         }
   2686     }
   2687     return status;
   2688 }
   2689 
   2690 /*******************************************************************************
   2691 **
   2692 ** Function:        Certificate_Verification
   2693 **
   2694 ** Description:     Perform the certificate verification by forwarding it to
   2695 **                  LS applet.
   2696 **
   2697 ** Returns:         Success if certificate is verified
   2698 **
   2699 *******************************************************************************/
   2700 tJBL_STATUS Certificate_Verification(Ala_ImageInfo_t *Os_info,
   2701 Ala_TranscieveInfo_t *pTranscv_Info, UINT8 *read_buf, UINT16 *offset1,
   2702 UINT8 *tag45Len)
   2703 {
   2704     tJBL_STATUS status = STATUS_FAILED;
   2705     UINT16 offset      = *offset1;
   2706     INT32 wCertfLen = (read_buf[2]<<8|read_buf[3]);
   2707     tJBL_STATUS certf_found = STATUS_FAILED;
   2708     static const char fn[] = "Certificate_Verification";
   2709     UINT8 tag_len_byte = Numof_lengthbytes(&read_buf[2], &wCertfLen);
   2710 
   2711     pTranscv_Info->sSendData[0] = 0x80;
   2712     pTranscv_Info->sSendData[1] = 0xA0;
   2713     pTranscv_Info->sSendData[2] = 0x01;
   2714     pTranscv_Info->sSendData[3] = 0x00;
   2715     /*If the certificate is less than 255 bytes*/
   2716     if(wCertfLen <= 251)
   2717     {
   2718         UINT8 tag7f49Off = 0;
   2719         UINT8 u7f49Len = 0;
   2720         UINT8 tag5f37Len = 0;
   2721         ALOGD("Certificate is greater than 255");
   2722         offset = offset+*tag45Len;
   2723         ALOGD("%s: Before TAG_CCM_PERMISSION = %x",fn, read_buf[offset]);
   2724         if(read_buf[offset] == TAG_CCM_PERMISSION)
   2725         {
   2726             INT32 tag53Len = 0;
   2727             UINT8 len_byte = 0;
   2728             offset =offset+1;
   2729             len_byte = Numof_lengthbytes(&read_buf[offset], &tag53Len);
   2730             offset = offset+tag53Len+len_byte;
   2731             ALOGD("%s: Verified TAG TAG_CCM_PERMISSION = 0x53",fn);
   2732             if((UINT16)(read_buf[offset]<<8|read_buf[offset+1]) == TAG_SIG_RNS_COMP)
   2733             {
   2734                 tag7f49Off = offset;
   2735                 u7f49Len   = read_buf[offset+2];
   2736                 offset     = offset+3+u7f49Len;
   2737                 if(u7f49Len != 64)
   2738                 {
   2739                     return STATUS_FAILED;
   2740                 }
   2741                 if((UINT16)(read_buf[offset]<<8|read_buf[offset+1]) == 0x7f49)
   2742                 {
   2743                     tag5f37Len = read_buf[offset+2];
   2744                     if(read_buf[offset+3] != 0x86 || (read_buf[offset+4] != 65))
   2745                     {
   2746                         return STATUS_FAILED;
   2747                     }
   2748                 }
   2749                 else
   2750                 {
   2751                     return STATUS_FAILED;
   2752                 }
   2753              }
   2754              else
   2755              {
   2756                  return STATUS_FAILED;
   2757              }
   2758         }
   2759         else
   2760         {
   2761             return STATUS_FAILED;
   2762         }
   2763         pTranscv_Info->sSendData[4] = wCertfLen+2+tag_len_byte;
   2764         pTranscv_Info->sSendlength  = wCertfLen+7+tag_len_byte;
   2765         memcpy(&(pTranscv_Info->sSendData[5]), &read_buf[0], wCertfLen+2+tag_len_byte);
   2766 
   2767         ALOGD("%s: start transceive for length %ld", fn, pTranscv_Info->
   2768             sSendlength);
   2769         status = ALA_SendtoAla(Os_info, status, pTranscv_Info,LS_Cert);
   2770         if(status != STATUS_OK)
   2771         {
   2772             return status;
   2773         }
   2774         else
   2775         {
   2776             certf_found = STATUS_OK;
   2777             ALOGD("Certificate is verified");
   2778             return status;
   2779         }
   2780     }
   2781     /*If the certificate is more than 255 bytes*/
   2782     else
   2783     {
   2784         UINT8 tag7f49Off = 0;
   2785         UINT8 u7f49Len = 0;
   2786         UINT8 tag5f37Len = 0;
   2787         ALOGD("Certificate is greater than 255");
   2788         offset = offset+*tag45Len;
   2789         ALOGD("%s: Before TAG_CCM_PERMISSION = %x",fn, read_buf[offset]);
   2790         if(read_buf[offset] == TAG_CCM_PERMISSION)
   2791         {
   2792             INT32 tag53Len = 0;
   2793             UINT8 len_byte = 0;
   2794             offset =offset+1;
   2795             len_byte = Numof_lengthbytes(&read_buf[offset], &tag53Len);
   2796             offset = offset+tag53Len+len_byte;
   2797             ALOGD("%s: Verified TAG TAG_CCM_PERMISSION = 0x53",fn);
   2798             if((UINT16)(read_buf[offset]<<8|read_buf[offset+1]) == TAG_SIG_RNS_COMP)
   2799             {
   2800                 tag7f49Off = offset;
   2801                 u7f49Len   = read_buf[offset+2];
   2802                 offset     = offset+3+u7f49Len;
   2803                 if(u7f49Len != 64)
   2804                 {
   2805                     return STATUS_FAILED;
   2806                 }
   2807                 if((UINT16)(read_buf[offset]<<8|read_buf[offset+1]) == 0x7f49)
   2808                 {
   2809                     tag5f37Len = read_buf[offset+2];
   2810                     if(read_buf[offset+3] != 0x86 || (read_buf[offset+4] != 65))
   2811                     {
   2812                         return STATUS_FAILED;
   2813                     }
   2814                 }
   2815                 else
   2816                 {
   2817                     return STATUS_FAILED;
   2818                 }
   2819                 pTranscv_Info->sSendData[4] = tag7f49Off;
   2820                 memcpy(&(pTranscv_Info->sSendData[5]), &read_buf[0], tag7f49Off);
   2821                 pTranscv_Info->sSendlength = tag7f49Off+5;
   2822                 ALOGD("%s: start transceive for length %ld", fn,
   2823                 pTranscv_Info->sSendlength);
   2824 
   2825                 status = ALA_SendtoAla(Os_info, status, pTranscv_Info,LS_Default);
   2826                 if(status != STATUS_OK)
   2827                 {
   2828 
   2829                     UINT8* RecvData = pTranscv_Info->sRecvData;
   2830                     Write_Response_To_OutFile(Os_info, RecvData,
   2831                     resp_len, LS_Cert);
   2832                     return status;
   2833                 }
   2834 
   2835                 pTranscv_Info->sSendData[2] = 0x00;
   2836                 pTranscv_Info->sSendData[4] = u7f49Len+tag5f37Len+6;
   2837                 memcpy(&(pTranscv_Info->sSendData[5]), &read_buf[tag7f49Off],
   2838                     u7f49Len+tag5f37Len+6);
   2839                 pTranscv_Info->sSendlength = u7f49Len+tag5f37Len+11;
   2840                 ALOGD("%s: start transceive for length %ld", fn,
   2841                     pTranscv_Info->sSendlength);
   2842 
   2843                 status = ALA_SendtoAla(Os_info, status, pTranscv_Info,LS_Cert);
   2844                 if(status != STATUS_OK)
   2845                 {
   2846                     return status;
   2847                 }
   2848                 else
   2849                 {
   2850                     ALOGD("Certificate is verified");
   2851                     certf_found = STATUS_OK;
   2852                     return status;
   2853 
   2854                 }
   2855             }
   2856             else
   2857             {
   2858                 return STATUS_FAILED;
   2859             }
   2860         }
   2861         else
   2862         {
   2863             return STATUS_FAILED;
   2864         }
   2865     }
   2866 return status;
   2867 }
   2868 
   2869 /*******************************************************************************
   2870 **
   2871 ** Function:        Check_Complete_7F21_Tag
   2872 **
   2873 ** Description:     Traverses the 7F21 tag for verification of each sub tag with
   2874 **                  in the 7F21 tag.
   2875 **
   2876 ** Returns:         Success if all tags are verified
   2877 **
   2878 *******************************************************************************/
   2879 tJBL_STATUS Check_Complete_7F21_Tag(Ala_ImageInfo_t *Os_info,
   2880        Ala_TranscieveInfo_t *pTranscv_Info, UINT8 *read_buf, UINT16 *offset)
   2881 {
   2882     static const char fn[] = "Check_Complete_7F21_Tag";
   2883     UINT8 tag45Len = 0;
   2884 
   2885     if(STATUS_OK == Check_Certificate_Tag(read_buf, offset))
   2886     {
   2887         if(STATUS_OK == Check_SerialNo_Tag(read_buf, offset))
   2888         {
   2889            if(STATUS_OK == Check_LSRootID_Tag(read_buf, offset))
   2890            {
   2891                if(STATUS_OK == Check_CertHoldID_Tag(read_buf, offset))
   2892                {
   2893                    if(STATUS_OK == Check_Date_Tag(read_buf, offset))
   2894                    {
   2895                        UINT8 tag45Len = 0;
   2896                        if(STATUS_OK == Check_45_Tag(read_buf, offset,
   2897                        &tag45Len))
   2898                        {
   2899                            if(STATUS_OK == Certificate_Verification(
   2900                            Os_info, pTranscv_Info, read_buf, offset,
   2901                            &tag45Len))
   2902                            {
   2903                                return STATUS_OK;
   2904                            }
   2905                        }else{
   2906                        ALOGE("%s: FAILED in Check_45_Tag", fn);}
   2907                    }else{
   2908                    ALOGE("%s: FAILED in Check_Date_Tag", fn);}
   2909                }else{
   2910                ALOGE("%s: FAILED in Check_CertHoldID_Tag", fn);}
   2911            }else{
   2912            ALOGE("%s: FAILED in Check_LSRootID_Tag", fn);}
   2913         }else{
   2914         ALOGE("%s: FAILED in Check_SerialNo_Tag", fn);}
   2915     }
   2916     else
   2917     {
   2918         ALOGE("%s: FAILED in Check_Certificate_Tag", fn);
   2919     }
   2920 return STATUS_FAILED;
   2921 }
   2922 
   2923 BOOLEAN ALA_UpdateExeStatus(UINT16 status)
   2924 {
   2925     fLS_STATUS = fopen(LS_STATUS_PATH, "w+");
   2926     ALOGD("enter: ALA_UpdateExeStatus");
   2927     if(fLS_STATUS == NULL)
   2928     {
   2929         ALOGE("Error opening LS Status file for backup: %s",strerror(errno));
   2930         return FALSE;
   2931     }
   2932     if((fprintf(fLS_STATUS, "%04x",status)) != 4)
   2933     {
   2934         ALOGE("Error updating LS Status backup: %s",strerror(errno));
   2935         fclose(fLS_STATUS);
   2936         return FALSE;
   2937     }
   2938     ALOGD("exit: ALA_UpdateExeStatus");
   2939     fclose(fLS_STATUS);
   2940     return TRUE;
   2941 }
   2942 
   2943 /*******************************************************************************
   2944 **
   2945 ** Function:        ALA_getAppletLsStatus
   2946 **
   2947 ** Description:     Interface to fetch Loader service Applet status to JNI, Services
   2948 **
   2949 ** Returns:         SUCCESS/FAILURE
   2950 **
   2951 *******************************************************************************/
   2952 tJBL_STATUS ALA_getAppletLsStatus(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info)
   2953 {
   2954     static const char fn[] = "ALA_getAppletLsStatus";
   2955     bool stat = false;
   2956     INT32 recvBufferActualSize = 0;
   2957     IChannel_t *mchannel = gpAla_Dwnld_Context->mchannel;
   2958 
   2959     ALOGD("%s: enter", fn);
   2960 
   2961     if(Os_info == NULL ||
   2962        pTranscv_Info == NULL)
   2963     {
   2964         ALOGD("%s: Invalid parameter", fn);
   2965     }
   2966     else
   2967     {
   2968         pTranscv_Info->sSendData[0] = STORE_DATA_CLA | Os_info->Channel_Info[0].channel_id;
   2969         pTranscv_Info->timeout = gTransceiveTimeout;
   2970         pTranscv_Info->sSendlength = (INT32)sizeof(GetData);
   2971         pTranscv_Info->sRecvlength = 1024;//(INT32)sizeof(INT32);
   2972 
   2973 
   2974         memcpy(&(pTranscv_Info->sSendData[1]), &GetData[1],
   2975                 ((sizeof(GetData))-1));
   2976         ALOGD("%s: Calling Secure Element Transceive with GET DATA apdu", fn);
   2977 
   2978         stat = mchannel->transceive (pTranscv_Info->sSendData,
   2979                                 pTranscv_Info->sSendlength,
   2980                                 pTranscv_Info->sRecvData,
   2981                                 pTranscv_Info->sRecvlength,
   2982                                 recvBufferActualSize,
   2983                                 pTranscv_Info->timeout);
   2984         if((stat != TRUE) &&
   2985            (recvBufferActualSize == 0x00))
   2986         {
   2987             status = STATUS_FAILED;
   2988             ALOGE("%s: SE transceive failed status = 0x%X", fn, status);
   2989         }
   2990         else if((pTranscv_Info->sRecvData[recvBufferActualSize-2] == 0x90) &&
   2991                 (pTranscv_Info->sRecvData[recvBufferActualSize-1] == 0x00))
   2992         {
   2993             ALOGE("STORE CMD is successful");
   2994             if((pTranscv_Info->sRecvData[0] == 0x46 )&& (pTranscv_Info->sRecvData[1] == 0x01 ))
   2995             {
   2996                if((pTranscv_Info->sRecvData[2] == 0x01))
   2997                {
   2998                    lsGetStatusArr[0]=0x63;lsGetStatusArr[1]=0x40;
   2999                    ALOGE("%s: Script execution status FAILED", fn);
   3000                }
   3001                else if((pTranscv_Info->sRecvData[2] == 0x00))
   3002                {
   3003                    lsGetStatusArr[0]=0x90;lsGetStatusArr[1]=0x00;
   3004                    ALOGE("%s: Script execution status SUCCESS", fn);
   3005                }
   3006                else
   3007                {
   3008                    lsGetStatusArr[0]=0x90;lsGetStatusArr[1]=0x00;
   3009                    ALOGE("%s: Script execution status UNKNOWN", fn);
   3010                }
   3011             }
   3012             else
   3013             {
   3014                 lsGetStatusArr[0]=0x90;lsGetStatusArr[1]=0x00;
   3015                 ALOGE("%s: Script execution status UNKNOWN", fn);
   3016             }
   3017             status = STATUS_SUCCESS;
   3018         }
   3019         else
   3020         {
   3021             status = STATUS_FAILED;
   3022         }
   3023 
   3024     ALOGE("%s: exit; status=0x%x", fn, status);
   3025     }
   3026     return status;
   3027 }
   3028 
   3029 /*******************************************************************************
   3030 **
   3031 ** Function:        Get_LsStatus
   3032 **
   3033 ** Description:     Interface to fetch Loader service client status to JNI, Services
   3034 **
   3035 ** Returns:         SUCCESS/FAILURE
   3036 **
   3037 *******************************************************************************/
   3038 tJBL_STATUS Get_LsStatus(UINT8 *pStatus)
   3039 {
   3040     tJBL_STATUS status = STATUS_FAILED;
   3041     UINT8 lsStatus[2]    = {0x63,0x40};
   3042     UINT8 loopcnt = 0;
   3043     fLS_STATUS = fopen(LS_STATUS_PATH, "r");
   3044     if(fLS_STATUS == NULL)
   3045     {
   3046         ALOGE("Error opening LS Status file for backup: %s",strerror(errno));
   3047         return status;
   3048     }
   3049     for(loopcnt=0;loopcnt<2;loopcnt++)
   3050     {
   3051         if((FSCANF_BYTE(fLS_STATUS, "%2x", &lsStatus[loopcnt])) == 0)
   3052         {
   3053             ALOGE("Error updating LS Status backup: %s",strerror(errno));
   3054             fclose(fLS_STATUS);
   3055             return status;
   3056         }
   3057     }
   3058     ALOGD("enter: ALA_getLsStatus 0x%X 0x%X",lsStatus[0],lsStatus[1] );
   3059     memcpy(pStatus, lsStatus, 2);
   3060     fclose(fLS_STATUS);
   3061     return STATUS_OK;
   3062 }
   3063 
   3064 
   3065 #endif
   3066