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 "Ala.h"
     17 #include "AlaLib.h"
     18 #include <data_types.h>
     19 #include <cutils/log.h>
     20 #include <dirent.h>
     21 
     22 static INT16 alaHandle;
     23 extern pAla_Dwnld_Context_t gpAla_Dwnld_Context;
     24 /*static bool Ala_inUse = false;*/
     25 /*******************************************************************************
     26 **
     27 ** Function:        JCDNLD_Init
     28 **
     29 ** Description:     Initializes the JCOP library and opens the DWP communication channel
     30 **
     31 ** Returns:         TRUE if ok.
     32 **
     33 *******************************************************************************/
     34 tJBL_STATUS ALA_Init(IChannel_t *channel)
     35 {
     36     static const char fn[] = "ALA_Init";
     37     BOOLEAN stat = FALSE;
     38     alaHandle  = EE_ERROR_OPEN_FAIL;
     39     ALOGD("%s: enter", fn);
     40 
     41 /*    if (Ala_inUse == true)
     42     {
     43         return STATUS_INUSE;
     44     }*/
     45     if(channel == NULL)
     46     {
     47         return STATUS_FAILED;
     48     }
     49     /*TODO: inUse assignment should be with protection like using semaphore*/
     50     /*Ala_inUse = true;*/
     51     stat = initialize (channel);
     52     if(stat != TRUE)
     53     {
     54         ALOGE("%s: failed", fn);
     55     }
     56     else
     57     {
     58         channel = gpAla_Dwnld_Context->mchannel;
     59         if((channel != NULL) &&
     60            (channel->open) != NULL)
     61         {
     62             alaHandle = channel->open();
     63             if(alaHandle == EE_ERROR_OPEN_FAIL)
     64             {
     65                 ALOGE("%s:Open DWP communication is failed", fn);
     66                 stat = FALSE;
     67             }
     68             else
     69             {
     70                 ALOGE("%s:Open DWP communication is success", fn);
     71                 stat = TRUE;
     72             }
     73         }
     74         else
     75         {
     76             ALOGE("%s: NULL DWP channel", fn);
     77             stat = FALSE;
     78         }
     79     }
     80     return (stat == true)?STATUS_OK:STATUS_FAILED;
     81 }
     82 
     83 /*******************************************************************************
     84 **
     85 ** Function:        ALA_Start
     86 **
     87 ** Description:     Starts the ALA update over DWP
     88 **
     89 ** Returns:         SUCCESS if ok.
     90 **
     91 *******************************************************************************/
     92 #if(NXP_LDR_SVC_VER_2 == TRUE)
     93 tJBL_STATUS ALA_Start(const char *name, const char *dest, UINT8 *pdata, UINT16 len, UINT8 *respSW)
     94 #else
     95 tJBL_STATUS ALA_Start(const char *name, UINT8 *pdata, UINT16 len)
     96 #endif
     97 {
     98     static const char fn[] = "ALA_Start";
     99     tJBL_STATUS status = STATUS_FAILED;
    100     IChannel_t *channel = gpAla_Dwnld_Context->mchannel;
    101     if(name != NULL)
    102     {
    103         ALOGE("%s: name is %s", fn, name);
    104 #if(NXP_LDR_SVC_VER_2 == TRUE)
    105         ALOGE("%s: Dest is %s", fn, dest);
    106         status = Perform_ALA(name, dest, pdata, len, respSW);
    107 #else
    108         status = Perform_ALA(name, pdata, len);
    109 #endif
    110     }
    111     else
    112     {
    113         ALOGE("Invalid parameter");
    114     }
    115     ALOGE("%s: Exit; status=0x0%X", fn, status);
    116     return status;
    117 }
    118 
    119 /*******************************************************************************
    120 **
    121 ** Function:        JCDNLD_DeInit
    122 **
    123 ** Description:     Deinitializes the ALA module
    124 **
    125 ** Returns:         TRUE if ok.
    126 **
    127 *******************************************************************************/
    128 bool ALA_DeInit()
    129 {
    130     static const char fn[] = "ALA_DeInit";
    131     BOOLEAN stat = FALSE;
    132     IChannel_t* channel = gpAla_Dwnld_Context->mchannel;
    133     ALOGD("%s: enter", fn);
    134     if(channel != NULL)
    135     {
    136         if(channel->doeSE_Reset != NULL)
    137         {
    138             //channel->doeSE_Reset();
    139             if(channel->close != NULL)
    140             {
    141                 stat = channel->close(alaHandle);
    142                 if(stat != TRUE)
    143                 {
    144                     ALOGE("%s:closing DWP channel is failed", fn);
    145                 }
    146             }
    147             else
    148             {
    149                 ALOGE("%s: NULL fp DWP_close", fn);
    150                 stat = FALSE;
    151             }
    152         }
    153     }
    154     else
    155     {
    156         ALOGE("%s: NULL dwp channel", fn);
    157     }
    158     finalize();
    159     /*TODO: inUse assignment should be with protection like using semaphore*/
    160     /*Ala_inUse = false;*/
    161     return stat;
    162 }
    163 #if(NXP_LDR_SVC_VER_2 != TRUE)
    164 /*******************************************************************************
    165 **
    166 ** Function:        ALA_GetlistofApplets
    167 **
    168 ** Description:     Gets the list of applets present the pre-defined directory
    169 **
    170 ** Returns:         TRUE if ok.
    171 **
    172 *******************************************************************************/
    173 void ALA_GetlistofApplets(char *list[], UINT8* num)
    174 {
    175   static const char dir[] = "/data/ala/";
    176   struct dirent *dp;
    177   UINT8 xx =0;
    178   DIR *fd;
    179 
    180   if ((fd = opendir(dir)) == NULL)
    181   {
    182     fprintf(stderr, "listdir: can't open %s\n", dir);
    183     return;
    184   }
    185   while ((dp = readdir(fd)) != NULL)
    186   {
    187       if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
    188        continue;    /* skip self and parent */
    189 
    190       ALOGE("%s/%s\n", dir, dp->d_name);
    191       list[xx] = (char *)malloc(strlen(dp->d_name)+1);
    192       if(list[xx] != NULL)
    193       {
    194           memset((void *)list[xx],0, strlen(dp->d_name)+1);
    195           memcpy(list[xx++], dp->d_name, strlen(dp->d_name)+1);
    196       }
    197       else
    198       {
    199           ALOGE("Memory allocation failed");
    200       }
    201 
    202   }
    203   *num = xx;
    204   ALOGD("%s: number of applets found=0x0%x", __FUNCTION__, *num);
    205   closedir(fd);
    206 }
    207 
    208 /*******************************************************************************
    209 **
    210 ** Function:        ALA_GetCertificateKey
    211 **
    212 ** Description:     Get the JSBL reference key
    213 **
    214 ** Returns:         TRUE if ok.
    215 **
    216 *******************************************************************************/
    217 tJBL_STATUS ALA_GetCertificateKey(UINT8 *pKey, INT32 *pKeylen)
    218 {
    219     static const char fn[] = "ALA_GetCertificateKey";
    220     tJBL_STATUS status = STATUS_FAILED;
    221     IChannel_t *channel = gpAla_Dwnld_Context->mchannel;
    222     if(pKey != NULL)
    223     {
    224         status = GetJsbl_Certificate_Refkey(pKey, pKeylen);
    225     }
    226     else
    227     {
    228         ALOGE("Invalid parameter");
    229     }
    230     ALOGE("%s: Exit; status=0x0%X", fn, status);
    231     return status;
    232 }
    233 #else
    234 
    235 /*******************************************************************************
    236 **
    237 ** Function:        ALA_lsGetVersion
    238 **
    239 ** Description:     Get the version of Loder service client and applet
    240 **
    241 ** Returns:         TRUE if ok.
    242 **
    243 *******************************************************************************/
    244 tJBL_STATUS ALA_lsGetVersion(UINT8 *pVersion)
    245 {
    246     static const char fn[] = "ALA_lsGetVersion";
    247     tJBL_STATUS status = STATUS_FAILED;
    248     IChannel_t *channel = gpAla_Dwnld_Context->mchannel;
    249     if(pVersion!= NULL)
    250     {
    251         status = GetLs_Version(pVersion);
    252         ALOGE("%s: LS Lib lsGetVersion status =0x0%X%X", fn, *pVersion, *(pVersion+1));
    253     }
    254     else
    255     {
    256         ALOGE("Invalid parameter");
    257     }
    258     ALOGE("%s: Exit; status=0x0%X", fn, status);
    259     return status;
    260 }
    261 /*******************************************************************************
    262 **
    263 ** Function:        ALA_lsGetStatus
    264 **
    265 ** Description:     Get the version of Loder service client and applet
    266 **
    267 ** Returns:         TRUE if ok.
    268 **
    269 *******************************************************************************/
    270 tJBL_STATUS ALA_lsGetStatus(UINT8 *pVersion)
    271 {
    272     static const char fn[] = "ALA_lsGetStatus";
    273     tJBL_STATUS status = STATUS_FAILED;
    274     IChannel_t *channel = gpAla_Dwnld_Context->mchannel;
    275     if(pVersion!= NULL)
    276     {
    277         status = Get_LsStatus(pVersion);
    278         ALOGE("%s: lsGetStatus ALALIB status=0x0%X 0x0%X", fn, pVersion[0], pVersion[1]);
    279     }
    280     else
    281     {
    282         ALOGE("Invalid parameter");
    283     }
    284     ALOGE("%s: Exit; status=0x0%X", fn, status);
    285     return status;
    286 }
    287 /*******************************************************************************
    288 **
    289 ** Function:        ALA_lsGetAppletStatus
    290 **
    291 ** Description:     Get the version of Loder service client and applet
    292 **
    293 ** Returns:         TRUE if ok.
    294 **
    295 *******************************************************************************/
    296 tJBL_STATUS ALA_lsGetAppletStatus(UINT8 *pVersion)
    297 {
    298     static const char fn[] = "ALA_lsGetStatus";
    299     tJBL_STATUS status = STATUS_FAILED;
    300     IChannel_t *channel = gpAla_Dwnld_Context->mchannel;
    301     if(pVersion!= NULL)
    302     {
    303         status = Get_LsAppletStatus(pVersion);
    304         ALOGE("%s: lsGetStatus ALALIB status=0x0%X 0x0%X", fn, pVersion[0], pVersion[1]);
    305     }
    306     else
    307     {
    308         ALOGE("Invalid parameter");
    309     }
    310     ALOGE("%s: Exit; status=0x0%X", fn, status);
    311     return status;
    312 }
    313 
    314 #endif
    315