blob: eb9a9f10ac0ecd0431e8e66d5fea125051c559ce [file] [log] [blame]
nxpandroidc7611652015-09-23 16:42:05 +05301/******************************************************************************
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 ******************************************************************************/
nxf24591c1cbeab2018-02-21 17:32:26 +053018#include <android-base/stringprintf.h>
19#include <base/logging.h>
nxpandroidc7611652015-09-23 16:42:05 +053020#include <fcntl.h>
nxf24591c1cbeab2018-02-21 17:32:26 +053021#include <vector>
22
nxpandroid25862ca2015-11-06 18:43:02 +053023#include "CrcChecksum.h"
nxf24591c1cbeab2018-02-21 17:32:26 +053024#include "nfa_nv_ci.h"
25#include "nfc_hal_nv_co.h"
26
27using android::base::StringPrintf;
28
29extern std::string nfc_storage_path;
30extern bool nfc_debug_enabled;
31
32namespace {
33std::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
nxpandroidc7611652015-09-23 16:42:05 +053038
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*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +053050extern void* nfa_mem_co_alloc(uint32_t num_bytes) { return malloc(num_bytes); }
nxpandroidc7611652015-09-23 16:42:05 +053051
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*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +053062extern void nfa_mem_co_free(void* pBuffer) { free(pBuffer); }
nxpandroidc7611652015-09-23 16:42:05 +053063
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
nxpandroid8f6d0532017-07-12 18:25:30 +053079** 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.
nxpandroidc7611652015-09-23 16:42:05 +053082**
83*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +053084extern void nfa_nv_co_read(uint8_t* pBuffer, uint16_t nbytes, uint8_t block) {
nxf24591c1cbeab2018-02-21 17:32:26 +053085 std::string filename = getFilenameForBlock(block);
nxpandroidc7611652015-09-23 16:42:05 +053086
nxf24591c1cbeab2018-02-21 17:32:26 +053087 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);
nxpandroid8f6d0532017-07-12 18:25:30 +053090 if (fileStream >= 0) {
91 unsigned short checksum = 0;
nxf24591c1cbeab2018-02-21 17:32:26 +053092 read(fileStream, &checksum, sizeof(checksum));
nxpandroid8f6d0532017-07-12 18:25:30 +053093 size_t actualReadData = read(fileStream, pBuffer, nbytes);
94 close(fileStream);
95 if (actualReadData > 0) {
nxf24591c1cbeab2018-02-21 17:32:26 +053096 DLOG_IF(INFO, nfc_debug_enabled)
97 << StringPrintf("%s: data size=%zu", __func__, actualReadData);
nxpandroid8f6d0532017-07-12 18:25:30 +053098 nfa_nv_ci_read(actualReadData, NFA_NV_CO_OK, block);
99 } else {
nxf24591c1cbeab2018-02-21 17:32:26 +0530100 LOG(ERROR) << StringPrintf("%s: fail to read", __func__);
nxpandroid8f6d0532017-07-12 18:25:30 +0530101 nfa_nv_ci_read(0, NFA_NV_CO_FAIL, block);
nxpandroidc7611652015-09-23 16:42:05 +0530102 }
nxpandroid8f6d0532017-07-12 18:25:30 +0530103 } else {
nxf24591c1cbeab2018-02-21 17:32:26 +0530104 DLOG_IF(INFO, nfc_debug_enabled)
105 << StringPrintf("%s: fail to open", __func__);
nxpandroid8f6d0532017-07-12 18:25:30 +0530106 nfa_nv_ci_read(0, NFA_NV_CO_FAIL, block);
107 }
nxpandroidc7611652015-09-23 16:42:05 +0530108}
109
110/*******************************************************************************
111**
112** Function nfa_nv_co_write
113**
114** Description This function is called by io to send file data to the
115** phone.
116**
117** Parameters pBuffer - buffer to read the data from.
118** nbytes - number of bytes to write out to the file.
119**
120** Returns void
121**
122** Note: Upon completion of the request, nfa_nv_ci_write() is
123** called with the file descriptor and the status. The
nxpandroid8f6d0532017-07-12 18:25:30 +0530124** call-in function should only be called when ALL
125** requested bytes have been written, or an error has
126** been detected,
nxpandroidc7611652015-09-23 16:42:05 +0530127**
128*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530129extern void nfa_nv_co_write(const uint8_t* pBuffer, uint16_t nbytes,
130 uint8_t block) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530131 std::string filename = getFilenameForBlock(block);
nxpandroidc7611652015-09-23 16:42:05 +0530132
nxf24591c1cbeab2018-02-21 17:32:26 +0530133 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
134 "%s: bytes=%u; file=%s", __func__, nbytes, filename.c_str());
nxpandroidc7611652015-09-23 16:42:05 +0530135
nxf24591c1cbeab2018-02-21 17:32:26 +0530136 int fileStream =
137 open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
nxpandroid8f6d0532017-07-12 18:25:30 +0530138 if (fileStream >= 0) {
139 unsigned short checksum = crcChecksumCompute(pBuffer, nbytes);
140 size_t actualWrittenCrc = write(fileStream, &checksum, sizeof(checksum));
141 size_t actualWrittenData = write(fileStream, pBuffer, nbytes);
nxf24591c1cbeab2018-02-21 17:32:26 +0530142 DLOG_IF(INFO, nfc_debug_enabled)
143 << StringPrintf("%s: %zu bytes written", __func__, actualWrittenData);
nxpandroid8f6d0532017-07-12 18:25:30 +0530144 if ((actualWrittenData == nbytes) &&
145 (actualWrittenCrc == sizeof(checksum))) {
146 nfa_nv_ci_write(NFA_NV_CO_OK);
147 } else {
nxf24591c1cbeab2018-02-21 17:32:26 +0530148 LOG(ERROR) << StringPrintf("%s: fail to write", __func__);
nxpandroid8f6d0532017-07-12 18:25:30 +0530149 nfa_nv_ci_write(NFA_NV_CO_FAIL);
nxpandroidc7611652015-09-23 16:42:05 +0530150 }
nxpandroid8f6d0532017-07-12 18:25:30 +0530151 close(fileStream);
152 } else {
nxf24591c1cbeab2018-02-21 17:32:26 +0530153 LOG(ERROR) << StringPrintf("%s: fail to open, error = %d", __func__, errno);
nxpandroid8f6d0532017-07-12 18:25:30 +0530154 nfa_nv_ci_write(NFA_NV_CO_FAIL);
155 }
nxpandroidc7611652015-09-23 16:42:05 +0530156}
157
158/*******************************************************************************
159**
160** Function delete_stack_non_volatile_store
161**
162** Description Delete all the content of the stack's storage location.
163**
164** Parameters forceDelete: unconditionally delete the storage.
165**
166** Returns none
167**
168*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530169void delete_stack_non_volatile_store(bool forceDelete) {
170 static bool firstTime = true;
nxpandroidc7611652015-09-23 16:42:05 +0530171
nxpandroid8f6d0532017-07-12 18:25:30 +0530172 if ((firstTime == false) && (forceDelete == false)) return;
173 firstTime = false;
nxpandroidc7611652015-09-23 16:42:05 +0530174
nxf24591c1cbeab2018-02-21 17:32:26 +0530175 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", __func__);
nxpandroidc7611652015-09-23 16:42:05 +0530176
nxf24591c1cbeab2018-02-21 17:32:26 +0530177 remove(getFilenameForBlock(DH_NV_BLOCK).c_str());
178 remove(getFilenameForBlock(HC_F2_NV_BLOCK).c_str());
179 remove(getFilenameForBlock(HC_F3_NV_BLOCK).c_str());
180 remove(getFilenameForBlock(HC_F4_NV_BLOCK).c_str());
181 remove(getFilenameForBlock(HC_F5_NV_BLOCK).c_str());
nxpandroidc7611652015-09-23 16:42:05 +0530182}
183
184/*******************************************************************************
185**
186** Function verify_stack_non_volatile_store
187**
188** Description Verify the content of all non-volatile store.
189**
190** Parameters none
191**
192** Returns none
193**
194*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530195void verify_stack_non_volatile_store() {
nxf24591c1cbeab2018-02-21 17:32:26 +0530196 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", __func__);
nxpandroidc7611652015-09-23 16:42:05 +0530197
nxf24591c1cbeab2018-02-21 17:32:26 +0530198 const std::vector<unsigned> verify_blocks = {DH_NV_BLOCK, HC_F2_NV_BLOCK,
199 HC_F3_NV_BLOCK, HC_F4_NV_BLOCK,
200 HC_F5_NV_BLOCK};
201
202 size_t verified = 0;
203 for (auto block : verify_blocks) {
204 if (!crcChecksumVerifyIntegrity(getFilenameForBlock(block).c_str())) break;
205 ++verified;
nxpandroid8f6d0532017-07-12 18:25:30 +0530206 }
nxpandroidc7611652015-09-23 16:42:05 +0530207
nxf24591c1cbeab2018-02-21 17:32:26 +0530208 if (verified != verify_blocks.size()) delete_stack_non_volatile_store(true);
nxpandroidc7611652015-09-23 16:42:05 +0530209}