blob: c46853ce6b1fd8f15047247e2247304785c16159 [file] [log] [blame]
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001/******************************************************************************
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 ******************************************************************************/
Ruchi Kandoid03c06e2017-01-26 15:32:03 -080018#include "_OverrideLog.h"
19
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080020#include <errno.h>
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080021#include <fcntl.h>
22#include <malloc.h>
23#include <sys/stat.h>
24#include <sys/types.h>
Elliott Hughes82c3eed2015-01-29 21:43:04 -080025#include "CrcChecksum.h"
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080026#include "buildcfg.h"
27#include "config.h"
28#include "nfa_nv_ci.h"
29#include "nfa_nv_co.h"
30#include "nfc_hal_nv_co.h"
31#include "nfc_hal_target.h"
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080032extern char bcm_nfc_location[];
Martijn Coenen5c65c3a2013-03-27 13:23:36 -070033static const char* sNfaStorageBin = "/nfaStorage.bin";
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080034
35/*******************************************************************************
36**
37** Function nfa_mem_co_alloc
38**
39** Description allocate a buffer from platform's memory pool
40**
41** Returns:
42** pointer to buffer if successful
43** NULL otherwise
44**
45*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080046extern void* nfa_mem_co_alloc(uint32_t num_bytes) { return malloc(num_bytes); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080047
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*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080058extern void nfa_mem_co_free(void* pBuffer) { free(pBuffer); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080059
60/*******************************************************************************
61**
62** Function nfa_nv_co_read
63**
64** Description This function is called by NFA to read in data from the
65** previously opened file.
66**
67** Parameters pBuffer - buffer to read the data into.
68** nbytes - number of bytes to read into the buffer.
69**
70** Returns void
71**
72** Note: Upon completion of the request, nfa_nv_ci_read() is
73** called with the buffer of data, along with the number
74** of bytes read into the buffer, and a status. The
Ruchi Kandoi552f2b72017-01-28 16:22:55 -080075** call-in function should only be called when ALL
76** requested bytes have been read, the end of file has
77** been detected, or an error has occurred.
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080078**
79*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080080extern void nfa_nv_co_read(uint8_t* pBuffer, uint16_t nbytes, uint8_t block) {
81 char filename[256], filename2[256];
Martijn Coenen5c65c3a2013-03-27 13:23:36 -070082
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080083 memset(filename, 0, sizeof(filename));
84 memset(filename2, 0, sizeof(filename2));
85 strcpy(filename2, bcm_nfc_location);
86 strncat(filename2, sNfaStorageBin, sizeof(filename2) - strlen(filename2) - 1);
87 if (strlen(filename2) > 200) {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -070088 LOG(ERROR) << StringPrintf("%s: filename too long", __func__);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080089 return;
90 }
91 sprintf(filename, "%s%u", filename2, block);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080092
Ruchi Kandoi6767aec2017-09-26 09:46:26 -070093 DLOG_IF(INFO, nfc_debug_enabled)
94 << StringPrintf("%s: buffer len=%u; file=%s", __func__, nbytes, filename);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080095 int fileStream = open(filename, O_RDONLY);
96 if (fileStream >= 0) {
97 unsigned short checksum = 0;
98 size_t actualReadCrc = read(fileStream, &checksum, sizeof(checksum));
99 size_t actualReadData = read(fileStream, pBuffer, nbytes);
100 close(fileStream);
101 if (actualReadData > 0) {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700102 DLOG_IF(INFO, nfc_debug_enabled)
103 << StringPrintf("%s: data size=%zu", __func__, actualReadData);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800104 nfa_nv_ci_read(actualReadData, NFA_NV_CO_OK, block);
105 } else {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700106 LOG(ERROR) << StringPrintf("%s: fail to read", __func__);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800107 nfa_nv_ci_read(0, NFA_NV_CO_FAIL, block);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800108 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800109 } else {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700110 DLOG_IF(INFO, nfc_debug_enabled)
111 << StringPrintf("%s: fail to open", __func__);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800112 nfa_nv_ci_read(0, NFA_NV_CO_FAIL, block);
113 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800114}
115
116/*******************************************************************************
117**
118** Function nfa_nv_co_write
119**
120** Description This function is called by io to send file data to the
121** phone.
122**
123** Parameters pBuffer - buffer to read the data from.
124** nbytes - number of bytes to write out to the file.
125**
126** Returns void
127**
128** Note: Upon completion of the request, nfa_nv_ci_write() is
129** called with the file descriptor and the status. The
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800130** call-in function should only be called when ALL
131** requested bytes have been written, or an error has
132** been detected,
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800133**
134*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800135extern void nfa_nv_co_write(const uint8_t* pBuffer, uint16_t nbytes,
136 uint8_t block) {
137 char filename[256], filename2[256];
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700138
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800139 memset(filename, 0, sizeof(filename));
140 memset(filename2, 0, sizeof(filename2));
141 strcpy(filename2, bcm_nfc_location);
142 strncat(filename2, sNfaStorageBin, sizeof(filename2) - strlen(filename2) - 1);
143 if (strlen(filename2) > 200) {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700144 LOG(ERROR) << StringPrintf("%s: filename too long", __func__);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800145 return;
146 }
147 sprintf(filename, "%s%u", filename2, block);
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700148 DLOG_IF(INFO, nfc_debug_enabled)
149 << StringPrintf("%s: bytes=%u; file=%s", __func__, nbytes, filename);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800150
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800151 int fileStream = 0;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800152
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800153 fileStream = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
154 if (fileStream >= 0) {
155 unsigned short checksum = crcChecksumCompute(pBuffer, nbytes);
156 size_t actualWrittenCrc = write(fileStream, &checksum, sizeof(checksum));
157 size_t actualWrittenData = write(fileStream, pBuffer, nbytes);
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700158 DLOG_IF(INFO, nfc_debug_enabled)
159 << StringPrintf("%s: %zu bytes written", __func__, actualWrittenData);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800160 if ((actualWrittenData == nbytes) &&
161 (actualWrittenCrc == sizeof(checksum))) {
162 nfa_nv_ci_write(NFA_NV_CO_OK);
163 } else {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700164 LOG(ERROR) << StringPrintf("%s: fail to write", __func__);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800165 nfa_nv_ci_write(NFA_NV_CO_FAIL);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800166 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800167 close(fileStream);
168 } else {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700169 LOG(ERROR) << StringPrintf("%s: fail to open, error = %d", __func__, errno);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800170 nfa_nv_ci_write(NFA_NV_CO_FAIL);
171 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800172}
173
174/*******************************************************************************
175**
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700176** Function delete_stack_non_volatile_store
177**
178** Description Delete all the content of the stack's storage location.
179**
Evan Chuedbfba92013-04-10 13:57:34 -0400180** Parameters forceDelete: unconditionally delete the storage.
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700181**
182** Returns none
183**
184*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800185void delete_stack_non_volatile_store(bool forceDelete) {
186 static bool firstTime = true;
187 char filename[256], filename2[256];
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700188
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800189 if ((firstTime == false) && (forceDelete == false)) return;
190 firstTime = false;
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700191
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700192 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", __func__);
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700193
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800194 memset(filename, 0, sizeof(filename));
195 memset(filename2, 0, sizeof(filename2));
196 strcpy(filename2, bcm_nfc_location);
197 strncat(filename2, sNfaStorageBin, sizeof(filename2) - strlen(filename2) - 1);
198 if (strlen(filename2) > 200) {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700199 LOG(ERROR) << StringPrintf("%s: filename too long", __func__);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800200 return;
201 }
202 sprintf(filename, "%s%u", filename2, DH_NV_BLOCK);
203 remove(filename);
204 sprintf(filename, "%s%u", filename2, HC_F3_NV_BLOCK);
205 remove(filename);
206 sprintf(filename, "%s%u", filename2, HC_F4_NV_BLOCK);
207 remove(filename);
208 sprintf(filename, "%s%u", filename2, HC_F2_NV_BLOCK);
209 remove(filename);
210 sprintf(filename, "%s%u", filename2, HC_F5_NV_BLOCK);
211 remove(filename);
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700212}
213
214/*******************************************************************************
215**
Evan Chuedbfba92013-04-10 13:57:34 -0400216** Function verify_stack_non_volatile_store
217**
218** Description Verify the content of all non-volatile store.
219**
220** Parameters none
221**
222** Returns none
223**
224*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800225void verify_stack_non_volatile_store() {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700226 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", __func__);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800227 char filename[256], filename2[256];
228 bool isValid = false;
Evan Chuedbfba92013-04-10 13:57:34 -0400229
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800230 memset(filename, 0, sizeof(filename));
231 memset(filename2, 0, sizeof(filename2));
232 strcpy(filename2, bcm_nfc_location);
233 strncat(filename2, sNfaStorageBin, sizeof(filename2) - strlen(filename2) - 1);
234 if (strlen(filename2) > 200) {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700235 LOG(ERROR) << StringPrintf("%s: filename too long", __func__);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800236 return;
237 }
Evan Chuedbfba92013-04-10 13:57:34 -0400238
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800239 sprintf(filename, "%s%u", filename2, DH_NV_BLOCK);
240 if (crcChecksumVerifyIntegrity(filename)) {
241 sprintf(filename, "%s%u", filename2, HC_F3_NV_BLOCK);
242 if (crcChecksumVerifyIntegrity(filename)) {
243 sprintf(filename, "%s%u", filename2, HC_F4_NV_BLOCK);
244 if (crcChecksumVerifyIntegrity(filename)) {
245 sprintf(filename, "%s%u", filename2, HC_F2_NV_BLOCK);
246 if (crcChecksumVerifyIntegrity(filename)) {
247 sprintf(filename, "%s%u", filename2, HC_F5_NV_BLOCK);
248 if (crcChecksumVerifyIntegrity(filename)) isValid = true;
Evan Chuedbfba92013-04-10 13:57:34 -0400249 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800250 }
Evan Chuedbfba92013-04-10 13:57:34 -0400251 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800252 }
Evan Chuedbfba92013-04-10 13:57:34 -0400253
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800254 if (isValid == false) delete_stack_non_volatile_store(true);
Evan Chuedbfba92013-04-10 13:57:34 -0400255}