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