blob: bc78e6c45afdef4445c56b3fcbe84b62b87cddef [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) {
88 ALOGE("%s: filename too long", __func__);
89 return;
90 }
91 sprintf(filename, "%s%u", filename2, block);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080092
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080093 ALOGD("%s: buffer len=%u; file=%s", __func__, nbytes, filename);
94 int fileStream = open(filename, O_RDONLY);
95 if (fileStream >= 0) {
96 unsigned short checksum = 0;
97 size_t actualReadCrc = read(fileStream, &checksum, sizeof(checksum));
98 size_t actualReadData = read(fileStream, pBuffer, nbytes);
99 close(fileStream);
100 if (actualReadData > 0) {
101 ALOGD("%s: data size=%zu", __func__, actualReadData);
102 nfa_nv_ci_read(actualReadData, NFA_NV_CO_OK, block);
103 } else {
104 ALOGE("%s: fail to read", __func__);
105 nfa_nv_ci_read(0, NFA_NV_CO_FAIL, block);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800106 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800107 } else {
108 ALOGD("%s: fail to open", __func__);
109 nfa_nv_ci_read(0, NFA_NV_CO_FAIL, block);
110 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800111}
112
113/*******************************************************************************
114**
115** Function nfa_nv_co_write
116**
117** Description This function is called by io to send file data to the
118** phone.
119**
120** Parameters pBuffer - buffer to read the data from.
121** nbytes - number of bytes to write out to the file.
122**
123** Returns void
124**
125** Note: Upon completion of the request, nfa_nv_ci_write() is
126** called with the file descriptor and the status. The
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800127** call-in function should only be called when ALL
128** requested bytes have been written, or an error has
129** been detected,
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800130**
131*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800132extern void nfa_nv_co_write(const uint8_t* pBuffer, uint16_t nbytes,
133 uint8_t block) {
134 char filename[256], filename2[256];
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700135
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800136 memset(filename, 0, sizeof(filename));
137 memset(filename2, 0, sizeof(filename2));
138 strcpy(filename2, bcm_nfc_location);
139 strncat(filename2, sNfaStorageBin, sizeof(filename2) - strlen(filename2) - 1);
140 if (strlen(filename2) > 200) {
141 ALOGE("%s: filename too long", __func__);
142 return;
143 }
144 sprintf(filename, "%s%u", filename2, block);
145 ALOGD("%s: bytes=%u; file=%s", __func__, nbytes, filename);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800146
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800147 int fileStream = 0;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800148
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800149 fileStream = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
150 if (fileStream >= 0) {
151 unsigned short checksum = crcChecksumCompute(pBuffer, nbytes);
152 size_t actualWrittenCrc = write(fileStream, &checksum, sizeof(checksum));
153 size_t actualWrittenData = write(fileStream, pBuffer, nbytes);
154 ALOGD("%s: %zu bytes written", __func__, actualWrittenData);
155 if ((actualWrittenData == nbytes) &&
156 (actualWrittenCrc == sizeof(checksum))) {
157 nfa_nv_ci_write(NFA_NV_CO_OK);
158 } else {
159 ALOGE("%s: fail to write", __func__);
160 nfa_nv_ci_write(NFA_NV_CO_FAIL);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800161 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800162 close(fileStream);
163 } else {
164 ALOGE("%s: fail to open, error = %d", __func__, errno);
165 nfa_nv_ci_write(NFA_NV_CO_FAIL);
166 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800167}
168
169/*******************************************************************************
170**
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700171** Function delete_stack_non_volatile_store
172**
173** Description Delete all the content of the stack's storage location.
174**
Evan Chuedbfba92013-04-10 13:57:34 -0400175** Parameters forceDelete: unconditionally delete the storage.
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700176**
177** Returns none
178**
179*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800180void delete_stack_non_volatile_store(bool forceDelete) {
181 static bool firstTime = true;
182 char filename[256], filename2[256];
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700183
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800184 if ((firstTime == false) && (forceDelete == false)) return;
185 firstTime = false;
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700186
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800187 ALOGD("%s", __func__);
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700188
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800189 memset(filename, 0, sizeof(filename));
190 memset(filename2, 0, sizeof(filename2));
191 strcpy(filename2, bcm_nfc_location);
192 strncat(filename2, sNfaStorageBin, sizeof(filename2) - strlen(filename2) - 1);
193 if (strlen(filename2) > 200) {
194 ALOGE("%s: filename too long", __func__);
195 return;
196 }
197 sprintf(filename, "%s%u", filename2, DH_NV_BLOCK);
198 remove(filename);
199 sprintf(filename, "%s%u", filename2, HC_F3_NV_BLOCK);
200 remove(filename);
201 sprintf(filename, "%s%u", filename2, HC_F4_NV_BLOCK);
202 remove(filename);
203 sprintf(filename, "%s%u", filename2, HC_F2_NV_BLOCK);
204 remove(filename);
205 sprintf(filename, "%s%u", filename2, HC_F5_NV_BLOCK);
206 remove(filename);
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700207}
208
209/*******************************************************************************
210**
Evan Chuedbfba92013-04-10 13:57:34 -0400211** Function verify_stack_non_volatile_store
212**
213** Description Verify the content of all non-volatile store.
214**
215** Parameters none
216**
217** Returns none
218**
219*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800220void verify_stack_non_volatile_store() {
221 ALOGD("%s", __func__);
222 char filename[256], filename2[256];
223 bool isValid = false;
Evan Chuedbfba92013-04-10 13:57:34 -0400224
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800225 memset(filename, 0, sizeof(filename));
226 memset(filename2, 0, sizeof(filename2));
227 strcpy(filename2, bcm_nfc_location);
228 strncat(filename2, sNfaStorageBin, sizeof(filename2) - strlen(filename2) - 1);
229 if (strlen(filename2) > 200) {
230 ALOGE("%s: filename too long", __func__);
231 return;
232 }
Evan Chuedbfba92013-04-10 13:57:34 -0400233
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800234 sprintf(filename, "%s%u", filename2, DH_NV_BLOCK);
235 if (crcChecksumVerifyIntegrity(filename)) {
236 sprintf(filename, "%s%u", filename2, HC_F3_NV_BLOCK);
237 if (crcChecksumVerifyIntegrity(filename)) {
238 sprintf(filename, "%s%u", filename2, HC_F4_NV_BLOCK);
239 if (crcChecksumVerifyIntegrity(filename)) {
240 sprintf(filename, "%s%u", filename2, HC_F2_NV_BLOCK);
241 if (crcChecksumVerifyIntegrity(filename)) {
242 sprintf(filename, "%s%u", filename2, HC_F5_NV_BLOCK);
243 if (crcChecksumVerifyIntegrity(filename)) isValid = true;
Evan Chuedbfba92013-04-10 13:57:34 -0400244 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800245 }
Evan Chuedbfba92013-04-10 13:57:34 -0400246 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800247 }
Evan Chuedbfba92013-04-10 13:57:34 -0400248
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800249 if (isValid == false) delete_stack_non_volatile_store(true);
Evan Chuedbfba92013-04-10 13:57:34 -0400250}