lin
2025-03-11 6f4f7a76e03a46fefb056a4b18197f1d9e8aa939
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/******************************************************************************
 *
 *  Copyright (C) 2011-2012 Broadcom Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/
#include <android-base/stringprintf.h>
#include <base/logging.h>
#include <fcntl.h>
#include <vector>
 
#include "CrcChecksum.h"
#include "nfa_nv_ci.h"
#include "nfc_hal_nv_co.h"
 
using android::base::StringPrintf;
 
extern std::string nfc_storage_path;
extern bool nfc_debug_enabled;
 
namespace {
std::string getFilenameForBlock(const unsigned block) {
  std::string bin = "nfaStorage.bin";
  return StringPrintf("%s/%s%u", nfc_storage_path.c_str(), bin.c_str(), block);
}
}  // namespace
 
/*******************************************************************************
**
** Function         nfa_mem_co_alloc
**
** Description      allocate a buffer from platform's memory pool
**
** Returns:
**                  pointer to buffer if successful
**                  NULL otherwise
**
*******************************************************************************/
extern void* nfa_mem_co_alloc(uint32_t num_bytes) { return malloc(num_bytes); }
 
/*******************************************************************************
**
** Function         nfa_mem_co_free
**
** Description      free buffer previously allocated using nfa_mem_co_alloc
**
** Returns:
**                  Nothing
**
*******************************************************************************/
extern void nfa_mem_co_free(void* pBuffer) { free(pBuffer); }
 
/*******************************************************************************
**
** Function         nfa_nv_co_read
**
** Description      This function is called by NFA to read in data from the
**                  previously opened file.
**
** Parameters       pBuffer   - buffer to read the data into.
**                  nbytes  - number of bytes to read into the buffer.
**
** Returns          void
**
**                  Note: Upon completion of the request, nfa_nv_ci_read() is
**                        called with the buffer of data, along with the number
**                        of bytes read into the buffer, and a status.  The
**                        call-in function should only be called when ALL
**                        requested bytes have been read, the end of file has
**                        been detected, or an error has occurred.
**
*******************************************************************************/
extern void nfa_nv_co_read(uint8_t* pBuffer, uint16_t nbytes, uint8_t block) {
  std::string filename = getFilenameForBlock(block);
 
  DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
      "%s: buffer len=%u; file=%s", __func__, nbytes, filename.c_str());
  int fileStream = open(filename.c_str(), O_RDONLY);
  if (fileStream >= 0) {
    uint16_t checksum = 0;
    read(fileStream, &checksum, sizeof(checksum));
    size_t actualReadData = read(fileStream, pBuffer, nbytes);
    close(fileStream);
    if (actualReadData > 0) {
      DLOG_IF(INFO, nfc_debug_enabled)
          << StringPrintf("%s: data size=%zu", __func__, actualReadData);
      nfa_nv_ci_read(actualReadData, NFA_NV_CO_OK, block);
    } else {
      LOG(ERROR) << StringPrintf("%s: fail to read", __func__);
      nfa_nv_ci_read(0, NFA_NV_CO_FAIL, block);
    }
  } else {
    DLOG_IF(INFO, nfc_debug_enabled)
        << StringPrintf("%s: fail to open", __func__);
    nfa_nv_ci_read(0, NFA_NV_CO_FAIL, block);
  }
}
 
/*******************************************************************************
**
** Function         nfa_nv_co_write
**
** Description      This function is called by io to send file data to the
**                  phone.
**
** Parameters       pBuffer   - buffer to read the data from.
**                  nbytes  - number of bytes to write out to the file.
**
** Returns          void
**
**                  Note: Upon completion of the request, nfa_nv_ci_write() is
**                        called with the file descriptor and the status.  The
**                        call-in function should only be called when ALL
**                        requested bytes have been written, or an error has
**                        been detected,
**
*******************************************************************************/
extern void nfa_nv_co_write(const uint8_t* pBuffer, uint16_t nbytes,
                            uint8_t block) {
  std::string filename = getFilenameForBlock(block);
 
  DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
      "%s: bytes=%u; file=%s", __func__, nbytes, filename.c_str());
 
  int fileStream =
      open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  if (fileStream >= 0) {
    uint16_t checksum = crcChecksumCompute(pBuffer, nbytes);
    size_t actualWrittenCrc = write(fileStream, &checksum, sizeof(checksum));
    size_t actualWrittenData = write(fileStream, pBuffer, nbytes);
    DLOG_IF(INFO, nfc_debug_enabled)
        << StringPrintf("%s: %zu bytes written", __func__, actualWrittenData);
    if ((actualWrittenData == nbytes) &&
        (actualWrittenCrc == sizeof(checksum))) {
      nfa_nv_ci_write(NFA_NV_CO_OK);
    } else {
      LOG(ERROR) << StringPrintf("%s: fail to write", __func__);
      nfa_nv_ci_write(NFA_NV_CO_FAIL);
    }
    close(fileStream);
  } else {
    LOG(ERROR) << StringPrintf("%s: fail to open, error = %d", __func__, errno);
    nfa_nv_ci_write(NFA_NV_CO_FAIL);
  }
}
 
/*******************************************************************************
**
** Function         delete_stack_non_volatile_store
**
** Description      Delete all the content of the stack's storage location.
**
** Parameters       forceDelete: unconditionally delete the storage.
**
** Returns          none
**
*******************************************************************************/
void delete_stack_non_volatile_store(bool forceDelete) {
  static bool firstTime = true;
 
  if ((firstTime == false) && (forceDelete == false)) return;
  firstTime = false;
 
  DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", __func__);
 
  remove(getFilenameForBlock(DH_NV_BLOCK).c_str());
  remove(getFilenameForBlock(HC_F2_NV_BLOCK).c_str());
  remove(getFilenameForBlock(HC_F3_NV_BLOCK).c_str());
  remove(getFilenameForBlock(HC_F4_NV_BLOCK).c_str());
  remove(getFilenameForBlock(HC_F5_NV_BLOCK).c_str());
}
 
/*******************************************************************************
**
** Function         verify_stack_non_volatile_store
**
** Description      Verify the content of all non-volatile store.
**
** Parameters       none
**
** Returns          none
**
*******************************************************************************/
void verify_stack_non_volatile_store() {
  DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", __func__);
 
  const std::vector<unsigned> verify_blocks = {DH_NV_BLOCK, HC_F2_NV_BLOCK,
                                               HC_F3_NV_BLOCK, HC_F4_NV_BLOCK,
                                               HC_F5_NV_BLOCK};
 
  size_t verified = 0;
  for (auto block : verify_blocks) {
    if (!crcChecksumVerifyIntegrity(getFilenameForBlock(block).c_str())) break;
    ++verified;
  }
 
  if (verified != verify_blocks.size()) delete_stack_non_volatile_store(true);
}