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 <android-base/stringprintf.h>
19 #include <base/logging.h>
20 #include <fcntl.h>
21 #include <vector>
22
23 #include "CrcChecksum.h"
24 #include "nfa_nv_ci.h"
25 #include "nfc_hal_nv_co.h"
26
27 using android::base::StringPrintf;
28
29 extern std::string nfc_storage_path;
30 extern bool nfc_debug_enabled;
31
32 namespace {
getFilenameForBlock(const unsigned block)33 std::string getFilenameForBlock(const unsigned block) {
34 std::string bin = "nfaStorage.bin";
35 return StringPrintf("%s/%s%u", nfc_storage_path.c_str(), bin.c_str(), block);
36 }
37 } // namespace
38
39 /*******************************************************************************
40 **
41 ** Function nfa_mem_co_alloc
42 **
43 ** Description allocate a buffer from platform's memory pool
44 **
45 ** Returns:
46 ** pointer to buffer if successful
47 ** NULL otherwise
48 **
49 *******************************************************************************/
nfa_mem_co_alloc(uint32_t num_bytes)50 extern void* nfa_mem_co_alloc(uint32_t num_bytes) { return malloc(num_bytes); }
51
52 /*******************************************************************************
53 **
54 ** Function nfa_mem_co_free
55 **
56 ** Description free buffer previously allocated using nfa_mem_co_alloc
57 **
58 ** Returns:
59 ** Nothing
60 **
61 *******************************************************************************/
nfa_mem_co_free(void * pBuffer)62 extern void nfa_mem_co_free(void* pBuffer) { free(pBuffer); }
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
80 ** requested bytes have been read, the end of file has
81 ** been detected, or an error has occurred.
82 **
83 *******************************************************************************/
nfa_nv_co_read(uint8_t * pBuffer,uint16_t nbytes,uint8_t block)84 extern void nfa_nv_co_read(uint8_t* pBuffer, uint16_t nbytes, uint8_t block) {
85 std::string filename = getFilenameForBlock(block);
86
87 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
88 "%s: buffer len=%u; file=%s", __func__, nbytes, filename.c_str());
89 int fileStream = open(filename.c_str(), O_RDONLY);
90 if (fileStream >= 0) {
91 uint16_t checksum = 0;
92 size_t checkSumRdData = read(fileStream, &checksum, sizeof(checksum));
93 if (checkSumRdData <= 0) {
94 LOG(ERROR) << StringPrintf("%s: failed to read checksum, errno = 0x%02x",
95 __func__, errno);
96 }
97 size_t actualReadData = read(fileStream, pBuffer, nbytes);
98 close(fileStream);
99 if (actualReadData > 0) {
100 DLOG_IF(INFO, nfc_debug_enabled)
101 << StringPrintf("%s: data size=%zu", __func__, actualReadData);
102 nfa_nv_ci_read(actualReadData, NFA_NV_CO_OK, block);
103 } else {
104 LOG(ERROR) << StringPrintf("%s: fail to read", __func__);
105 nfa_nv_ci_read(0, NFA_NV_CO_FAIL, block);
106 }
107 } else {
108 DLOG_IF(INFO, nfc_debug_enabled)
109 << StringPrintf("%s: fail to open", __func__);
110 nfa_nv_ci_read(0, NFA_NV_CO_FAIL, block);
111 }
112 }
113
114 /*******************************************************************************
115 **
116 ** Function nfa_nv_co_write
117 **
118 ** Description This function is called by io to send file data to the
119 ** phone.
120 **
121 ** Parameters pBuffer - buffer to read the data from.
122 ** nbytes - number of bytes to write out to the file.
123 **
124 ** Returns void
125 **
126 ** Note: Upon completion of the request, nfa_nv_ci_write() is
127 ** called with the file descriptor and the status. The
128 ** call-in function should only be called when ALL
129 ** requested bytes have been written, or an error has
130 ** been detected,
131 **
132 *******************************************************************************/
nfa_nv_co_write(const uint8_t * pBuffer,uint16_t nbytes,uint8_t block)133 extern void nfa_nv_co_write(const uint8_t* pBuffer, uint16_t nbytes,
134 uint8_t block) {
135 std::string filename = getFilenameForBlock(block);
136
137 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
138 "%s: bytes=%u; file=%s", __func__, nbytes, filename.c_str());
139
140 int fileStream =
141 open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
142 if (fileStream >= 0) {
143 uint16_t checksum = crcChecksumCompute(pBuffer, nbytes);
144 size_t actualWrittenCrc = write(fileStream, &checksum, sizeof(checksum));
145 size_t actualWrittenData = write(fileStream, pBuffer, nbytes);
146 DLOG_IF(INFO, nfc_debug_enabled)
147 << StringPrintf("%s: %zu bytes written", __func__, actualWrittenData);
148 if ((actualWrittenData == nbytes) &&
149 (actualWrittenCrc == sizeof(checksum))) {
150 nfa_nv_ci_write(NFA_NV_CO_OK);
151 } else {
152 LOG(ERROR) << StringPrintf("%s: fail to write", __func__);
153 nfa_nv_ci_write(NFA_NV_CO_FAIL);
154 }
155 close(fileStream);
156 } else {
157 LOG(ERROR) << StringPrintf("%s: fail to open, error = %d", __func__, errno);
158 nfa_nv_ci_write(NFA_NV_CO_FAIL);
159 }
160 }
161
162 /*******************************************************************************
163 **
164 ** Function delete_stack_non_volatile_store
165 **
166 ** Description Delete all the content of the stack's storage location.
167 **
168 ** Parameters forceDelete: unconditionally delete the storage.
169 **
170 ** Returns none
171 **
172 *******************************************************************************/
delete_stack_non_volatile_store(bool forceDelete)173 void delete_stack_non_volatile_store(bool forceDelete) {
174 static bool firstTime = true;
175
176 if ((firstTime == false) && (forceDelete == false)) return;
177 firstTime = false;
178
179 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", __func__);
180
181 if (remove(getFilenameForBlock(DH_NV_BLOCK).c_str())) {
182 LOG(ERROR) << StringPrintf(
183 "%s: fail to delete DH_NV_BLOCK file, errno = 0x%02X", __func__, errno);
184 }
185 if (remove(getFilenameForBlock(HC_F2_NV_BLOCK).c_str())) {
186 LOG(ERROR) << StringPrintf(
187 "%s: fail to delete HC_F2_NV_BLOCK file, errno = 0x%02X", __func__,
188 errno);
189 }
190 if (remove(getFilenameForBlock(HC_F3_NV_BLOCK).c_str())) {
191 LOG(ERROR) << StringPrintf(
192 "%s: fail to delete HC_F3_NV_BLOCK file, errno = 0x%02X", __func__,
193 errno);
194 }
195 if (remove(getFilenameForBlock(HC_F4_NV_BLOCK).c_str())) {
196 LOG(ERROR) << StringPrintf(
197 "%s: fail to delete HC_F4_NV_BLOCK file, errno = 0x%02X", __func__,
198 errno);
199 }
200 if (remove(getFilenameForBlock(HC_F5_NV_BLOCK).c_str())) {
201 LOG(ERROR) << StringPrintf(
202 "%s: fail to delete HC_F5_NV_BLOCK file, errno = 0x%02X", __func__,
203 errno);
204 }
205 }
206
207 /*******************************************************************************
208 **
209 ** Function verify_stack_non_volatile_store
210 **
211 ** Description Verify the content of all non-volatile store.
212 **
213 ** Parameters none
214 **
215 ** Returns none
216 **
217 *******************************************************************************/
verify_stack_non_volatile_store()218 void verify_stack_non_volatile_store() {
219 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", __func__);
220
221 const std::vector<unsigned> verify_blocks = {DH_NV_BLOCK, HC_F2_NV_BLOCK,
222 HC_F3_NV_BLOCK, HC_F4_NV_BLOCK,
223 HC_F5_NV_BLOCK};
224
225 size_t verified = 0;
226 for (auto block : verify_blocks) {
227 if (!crcChecksumVerifyIntegrity(getFilenameForBlock(block).c_str())) break;
228 ++verified;
229 }
230
231 if (verified != verify_blocks.size()) delete_stack_non_volatile_store(true);
232 }
233