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/logging.h>
19 #include <android-base/stringprintf.h>
20 #include <fcntl.h>
21
22 #include <vector>
23
24 #include "CrcChecksum.h"
25 #include "nfa_nv_ci.h"
26 #include "nfc_hal_nv_co.h"
27
28 using android::base::StringPrintf;
29
30 extern std::string nfc_storage_path;
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 LOG(VERBOSE) << StringPrintf("%s: buffer len=%u; file=%s", __func__, nbytes,
88 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 LOG(VERBOSE) << StringPrintf("%s: data size=%zu", __func__, actualReadData);
101 nfa_nv_ci_read(actualReadData, NFA_NV_CO_OK, block);
102 } else {
103 LOG(ERROR) << StringPrintf("%s: fail to read", __func__);
104 nfa_nv_ci_read(0, NFA_NV_CO_FAIL, block);
105 }
106 } else {
107 LOG(VERBOSE) << StringPrintf("%s: fail to open", __func__);
108 nfa_nv_ci_read(0, NFA_NV_CO_FAIL, block);
109 }
110 }
111
112 /*******************************************************************************
113 **
114 ** Function nfa_nv_co_write
115 **
116 ** Description This function is called by io to send file data to the
117 ** phone.
118 **
119 ** Parameters pBuffer - buffer to read the data from.
120 ** nbytes - number of bytes to write out to the file.
121 **
122 ** Returns void
123 **
124 ** Note: Upon completion of the request, nfa_nv_ci_write() is
125 ** called with the file descriptor and the status. The
126 ** call-in function should only be called when ALL
127 ** requested bytes have been written, or an error has
128 ** been detected,
129 **
130 *******************************************************************************/
nfa_nv_co_write(const uint8_t * pBuffer,uint16_t nbytes,uint8_t block)131 extern void nfa_nv_co_write(const uint8_t* pBuffer, uint16_t nbytes,
132 uint8_t block) {
133 std::string filename = getFilenameForBlock(block);
134
135 LOG(VERBOSE) << StringPrintf("%s: bytes=%u; file=%s", __func__, nbytes,
136 filename.c_str());
137
138 int fileStream =
139 open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
140 if (fileStream >= 0) {
141 uint16_t checksum = crcChecksumCompute(pBuffer, nbytes);
142 size_t actualWrittenCrc = write(fileStream, &checksum, sizeof(checksum));
143 size_t actualWrittenData = write(fileStream, pBuffer, nbytes);
144 LOG(VERBOSE) << StringPrintf("%s: %zu bytes written", __func__,
145 actualWrittenData);
146 if ((actualWrittenData == nbytes) &&
147 (actualWrittenCrc == sizeof(checksum))) {
148 nfa_nv_ci_write(NFA_NV_CO_OK);
149 } else {
150 LOG(ERROR) << StringPrintf("%s: fail to write", __func__);
151 nfa_nv_ci_write(NFA_NV_CO_FAIL);
152 }
153 close(fileStream);
154 } else {
155 LOG(ERROR) << StringPrintf("%s: fail to open, error = %d", __func__, errno);
156 nfa_nv_ci_write(NFA_NV_CO_FAIL);
157 }
158 }
159
160 /*******************************************************************************
161 **
162 ** Function delete_stack_non_volatile_store
163 **
164 ** Description Delete all the content of the stack's storage location.
165 **
166 ** Parameters forceDelete: unconditionally delete the storage.
167 **
168 ** Returns none
169 **
170 *******************************************************************************/
delete_stack_non_volatile_store(bool forceDelete)171 void delete_stack_non_volatile_store(bool forceDelete) {
172 static bool firstTime = true;
173
174 if ((firstTime == false) && (forceDelete == false)) return;
175 firstTime = false;
176
177 LOG(VERBOSE) << StringPrintf("%s", __func__);
178
179 if (remove(getFilenameForBlock(DH_NV_BLOCK).c_str())) {
180 LOG(ERROR) << StringPrintf(
181 "%s: fail to delete DH_NV_BLOCK file, errno = 0x%02X", __func__, errno);
182 }
183 if (remove(getFilenameForBlock(HC_F2_NV_BLOCK).c_str())) {
184 LOG(ERROR) << StringPrintf(
185 "%s: fail to delete HC_F2_NV_BLOCK file, errno = 0x%02X", __func__,
186 errno);
187 }
188 if (remove(getFilenameForBlock(HC_F3_NV_BLOCK).c_str())) {
189 LOG(ERROR) << StringPrintf(
190 "%s: fail to delete HC_F3_NV_BLOCK file, errno = 0x%02X", __func__,
191 errno);
192 }
193 if (remove(getFilenameForBlock(HC_F4_NV_BLOCK).c_str())) {
194 LOG(ERROR) << StringPrintf(
195 "%s: fail to delete HC_F4_NV_BLOCK file, errno = 0x%02X", __func__,
196 errno);
197 }
198 if (remove(getFilenameForBlock(HC_F5_NV_BLOCK).c_str())) {
199 LOG(ERROR) << StringPrintf(
200 "%s: fail to delete HC_F5_NV_BLOCK file, errno = 0x%02X", __func__,
201 errno);
202 }
203 }
204
205 /*******************************************************************************
206 **
207 ** Function verify_stack_non_volatile_store
208 **
209 ** Description Verify the content of all non-volatile store.
210 **
211 ** Parameters none
212 **
213 ** Returns none
214 **
215 *******************************************************************************/
verify_stack_non_volatile_store()216 void verify_stack_non_volatile_store() {
217 LOG(VERBOSE) << StringPrintf("%s", __func__);
218
219 const std::vector<unsigned> verify_blocks = {DH_NV_BLOCK, HC_F2_NV_BLOCK,
220 HC_F3_NV_BLOCK, HC_F4_NV_BLOCK,
221 HC_F5_NV_BLOCK};
222
223 size_t verified = 0;
224 for (auto block : verify_blocks) {
225 if (!crcChecksumVerifyIntegrity(getFilenameForBlock(block).c_str())) break;
226 ++verified;
227 }
228
229 if (verified != verify_blocks.size()) delete_stack_non_volatile_store(true);
230 }
231