Home | History | Annotate | Download | only in adaptation
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2011-2012 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 #include "OverrideLog.h"
     19 #include <sys/types.h>
     20 #include <sys/stat.h>
     21 #include <malloc.h>
     22 #include <fcntl.h>
     23 #include <errno.h>
     24 #include "buildcfg.h"
     25 #include "nfa_nv_co.h"
     26 #include "config.h"
     27 #include "nfc_hal_target.h"
     28 #include "nfc_hal_nv_co.h"
     29 #include "nfa_nv_ci.h"
     30 #include "CrcChecksum.h"
     31 extern char bcm_nfc_location[];
     32 static const char* sNfaStorageBin = "/nfaStorage.bin";
     33 
     34 /*******************************************************************************
     35 **
     36 ** Function         nfa_mem_co_alloc
     37 **
     38 ** Description      allocate a buffer from platform's memory pool
     39 **
     40 ** Returns:
     41 **                  pointer to buffer if successful
     42 **                  NULL otherwise
     43 **
     44 *******************************************************************************/
     45 extern void *nfa_mem_co_alloc(UINT32 num_bytes)
     46 {
     47     return malloc(num_bytes);
     48 }
     49 
     50 
     51 /*******************************************************************************
     52 **
     53 ** Function         nfa_mem_co_free
     54 **
     55 ** Description      free buffer previously allocated using nfa_mem_co_alloc
     56 **
     57 ** Returns:
     58 **                  Nothing
     59 **
     60 *******************************************************************************/
     61 extern void nfa_mem_co_free(void *pBuffer)
     62 {
     63     free(pBuffer);
     64 }
     65 
     66 
     67 /*******************************************************************************
     68 **
     69 ** Function         nfa_nv_co_read
     70 **
     71 ** Description      This function is called by NFA to read in data from the
     72 **                  previously opened file.
     73 **
     74 ** Parameters       pBuffer   - buffer to read the data into.
     75 **                  nbytes  - number of bytes to read into the buffer.
     76 **
     77 ** Returns          void
     78 **
     79 **                  Note: Upon completion of the request, nfa_nv_ci_read() is
     80 **                        called with the buffer of data, along with the number
     81 **                        of bytes read into the buffer, and a status.  The
     82 **                        call-in function should only be called when ALL requested
     83 **                        bytes have been read, the end of file has been detected,
     84 **                        or an error has occurred.
     85 **
     86 *******************************************************************************/
     87 extern void nfa_nv_co_read(UINT8 *pBuffer, UINT16 nbytes, UINT8 block)
     88 {
     89     char filename[256], filename2[256];
     90 
     91     memset (filename, 0, sizeof(filename));
     92     memset (filename2, 0, sizeof(filename2));
     93     strcpy(filename2, bcm_nfc_location);
     94     strncat(filename2, sNfaStorageBin, sizeof(filename2)-strlen(filename2)-1);
     95     if (strlen(filename2) > 200)
     96     {
     97         ALOGE ("%s: filename too long", __FUNCTION__);
     98         return;
     99     }
    100     sprintf (filename, "%s%u", filename2, block);
    101 
    102     ALOGD ("%s: buffer len=%u; file=%s", __FUNCTION__, nbytes, filename);
    103     int fileStream = open (filename, O_RDONLY);
    104     if (fileStream >= 0)
    105     {
    106         unsigned short checksum = 0;
    107         size_t actualReadCrc = read (fileStream, &checksum, sizeof(checksum));
    108         size_t actualReadData = read (fileStream, pBuffer, nbytes);
    109         close (fileStream);
    110         if (actualReadData > 0)
    111         {
    112             ALOGD ("%s: data size=%zu", __FUNCTION__, actualReadData);
    113             nfa_nv_ci_read (actualReadData, NFA_NV_CO_OK, block);
    114         }
    115         else
    116         {
    117             ALOGE ("%s: fail to read", __FUNCTION__);
    118             nfa_nv_ci_read (0, NFA_NV_CO_FAIL, block);
    119         }
    120     }
    121     else
    122     {
    123         ALOGD ("%s: fail to open", __FUNCTION__);
    124         nfa_nv_ci_read (0, NFA_NV_CO_FAIL, block);
    125     }
    126 }
    127 
    128 /*******************************************************************************
    129 **
    130 ** Function         nfa_nv_co_write
    131 **
    132 ** Description      This function is called by io to send file data to the
    133 **                  phone.
    134 **
    135 ** Parameters       pBuffer   - buffer to read the data from.
    136 **                  nbytes  - number of bytes to write out to the file.
    137 **
    138 ** Returns          void
    139 **
    140 **                  Note: Upon completion of the request, nfa_nv_ci_write() is
    141 **                        called with the file descriptor and the status.  The
    142 **                        call-in function should only be called when ALL requested
    143 **                        bytes have been written, or an error has been detected,
    144 **
    145 *******************************************************************************/
    146 extern void nfa_nv_co_write(const UINT8 *pBuffer, UINT16 nbytes, UINT8 block)
    147 {
    148     char filename[256], filename2[256];
    149 
    150     memset (filename, 0, sizeof(filename));
    151     memset (filename2, 0, sizeof(filename2));
    152     strcpy(filename2, bcm_nfc_location);
    153     strncat(filename2, sNfaStorageBin, sizeof(filename2)-strlen(filename2)-1);
    154     if (strlen(filename2) > 200)
    155     {
    156         ALOGE ("%s: filename too long", __FUNCTION__);
    157         return;
    158     }
    159     sprintf (filename, "%s%u", filename2, block);
    160     ALOGD ("%s: bytes=%u; file=%s", __FUNCTION__, nbytes, filename);
    161 
    162     int fileStream = 0;
    163 
    164     fileStream = open (filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
    165     if (fileStream >= 0)
    166     {
    167         unsigned short checksum = crcChecksumCompute (pBuffer, nbytes);
    168         size_t actualWrittenCrc = write (fileStream, &checksum, sizeof(checksum));
    169         size_t actualWrittenData = write (fileStream, pBuffer, nbytes);
    170         ALOGD ("%s: %zu bytes written", __FUNCTION__, actualWrittenData);
    171         if ((actualWrittenData == nbytes) && (actualWrittenCrc == sizeof(checksum)))
    172         {
    173             nfa_nv_ci_write (NFA_NV_CO_OK);
    174         }
    175         else
    176         {
    177             ALOGE ("%s: fail to write", __FUNCTION__);
    178             nfa_nv_ci_write (NFA_NV_CO_FAIL);
    179         }
    180         close (fileStream);
    181     }
    182     else
    183     {
    184         ALOGE ("%s: fail to open, error = %d", __FUNCTION__, errno);
    185         nfa_nv_ci_write (NFA_NV_CO_FAIL);
    186     }
    187 }
    188 
    189 /*******************************************************************************
    190 **
    191 ** Function         delete_stack_non_volatile_store
    192 **
    193 ** Description      Delete all the content of the stack's storage location.
    194 **
    195 ** Parameters       forceDelete: unconditionally delete the storage.
    196 **
    197 ** Returns          none
    198 **
    199 *******************************************************************************/
    200 void delete_stack_non_volatile_store (BOOLEAN forceDelete)
    201 {
    202     static BOOLEAN firstTime = TRUE;
    203     char filename[256], filename2[256];
    204 
    205     if ((firstTime == FALSE) && (forceDelete == FALSE))
    206         return;
    207     firstTime = FALSE;
    208 
    209     ALOGD ("%s", __FUNCTION__);
    210 
    211     memset (filename, 0, sizeof(filename));
    212     memset (filename2, 0, sizeof(filename2));
    213     strcpy(filename2, bcm_nfc_location);
    214     strncat(filename2, sNfaStorageBin, sizeof(filename2)-strlen(filename2)-1);
    215     if (strlen(filename2) > 200)
    216     {
    217         ALOGE ("%s: filename too long", __FUNCTION__);
    218         return;
    219     }
    220     sprintf (filename, "%s%u", filename2, DH_NV_BLOCK);
    221     remove (filename);
    222     sprintf (filename, "%s%u", filename2, HC_F3_NV_BLOCK);
    223     remove (filename);
    224     sprintf (filename, "%s%u", filename2, HC_F4_NV_BLOCK);
    225     remove (filename);
    226     sprintf (filename, "%s%u", filename2, HC_F2_NV_BLOCK);
    227     remove (filename);
    228     sprintf (filename, "%s%u", filename2, HC_F5_NV_BLOCK);
    229     remove (filename);
    230 }
    231 
    232 /*******************************************************************************
    233 **
    234 ** Function         verify_stack_non_volatile_store
    235 **
    236 ** Description      Verify the content of all non-volatile store.
    237 **
    238 ** Parameters       none
    239 **
    240 ** Returns          none
    241 **
    242 *******************************************************************************/
    243 void verify_stack_non_volatile_store ()
    244 {
    245     ALOGD ("%s", __FUNCTION__);
    246     char filename[256], filename2[256];
    247     BOOLEAN isValid = FALSE;
    248 
    249     memset (filename, 0, sizeof(filename));
    250     memset (filename2, 0, sizeof(filename2));
    251     strcpy(filename2, bcm_nfc_location);
    252     strncat(filename2, sNfaStorageBin, sizeof(filename2)-strlen(filename2)-1);
    253     if (strlen(filename2) > 200)
    254     {
    255         ALOGE ("%s: filename too long", __FUNCTION__);
    256         return;
    257     }
    258 
    259     sprintf (filename, "%s%u", filename2, DH_NV_BLOCK);
    260     if (crcChecksumVerifyIntegrity (filename))
    261     {
    262         sprintf (filename, "%s%u", filename2, HC_F3_NV_BLOCK);
    263         if (crcChecksumVerifyIntegrity (filename))
    264         {
    265             sprintf (filename, "%s%u", filename2, HC_F4_NV_BLOCK);
    266             if (crcChecksumVerifyIntegrity (filename))
    267             {
    268                 sprintf (filename, "%s%u", filename2, HC_F2_NV_BLOCK);
    269                 if (crcChecksumVerifyIntegrity (filename))
    270                 {
    271                     sprintf (filename, "%s%u", filename2, HC_F5_NV_BLOCK);
    272                     if (crcChecksumVerifyIntegrity (filename))
    273                         isValid = TRUE;
    274                 }
    275             }
    276         }
    277     }
    278 
    279     if (isValid == FALSE)
    280         delete_stack_non_volatile_store (TRUE);
    281 }
    282 
    283