The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 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 | ******************************************************************************/ |
Ruchi Kandoi | d03c06e | 2017-01-26 15:32:03 -0800 | [diff] [blame] | 18 | #include "_OverrideLog.h" |
| 19 | |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 20 | #include <errno.h> |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
| 22 | #include <malloc.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
Elliott Hughes | 82c3eed | 2015-01-29 21:43:04 -0800 | [diff] [blame] | 25 | #include "CrcChecksum.h" |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 26 | #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 Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 32 | extern char bcm_nfc_location[]; |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 33 | static const char* sNfaStorageBin = "/nfaStorage.bin"; |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 34 | |
| 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 Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 46 | extern void* nfa_mem_co_alloc(uint32_t num_bytes) { return malloc(num_bytes); } |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 47 | |
| 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 Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 58 | extern void nfa_mem_co_free(void* pBuffer) { free(pBuffer); } |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 59 | |
| 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 Kandoi | 552f2b7 | 2017-01-28 16:22:55 -0800 | [diff] [blame] | 75 | ** 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 Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 78 | ** |
| 79 | *******************************************************************************/ |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 80 | extern void nfa_nv_co_read(uint8_t* pBuffer, uint16_t nbytes, uint8_t block) { |
| 81 | char filename[256], filename2[256]; |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 82 | |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 83 | 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) { |
Ruchi Kandoi | 6767aec | 2017-09-26 09:46:26 -0700 | [diff] [blame^] | 88 | LOG(ERROR) << StringPrintf("%s: filename too long", __func__); |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 89 | return; |
| 90 | } |
| 91 | sprintf(filename, "%s%u", filename2, block); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 92 | |
Ruchi Kandoi | 6767aec | 2017-09-26 09:46:26 -0700 | [diff] [blame^] | 93 | DLOG_IF(INFO, nfc_debug_enabled) |
| 94 | << StringPrintf("%s: buffer len=%u; file=%s", __func__, nbytes, filename); |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 95 | int fileStream = open(filename, O_RDONLY); |
| 96 | if (fileStream >= 0) { |
| 97 | unsigned short checksum = 0; |
| 98 | size_t actualReadCrc = read(fileStream, &checksum, sizeof(checksum)); |
| 99 | size_t actualReadData = read(fileStream, pBuffer, nbytes); |
| 100 | close(fileStream); |
| 101 | if (actualReadData > 0) { |
Ruchi Kandoi | 6767aec | 2017-09-26 09:46:26 -0700 | [diff] [blame^] | 102 | DLOG_IF(INFO, nfc_debug_enabled) |
| 103 | << StringPrintf("%s: data size=%zu", __func__, actualReadData); |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 104 | nfa_nv_ci_read(actualReadData, NFA_NV_CO_OK, block); |
| 105 | } else { |
Ruchi Kandoi | 6767aec | 2017-09-26 09:46:26 -0700 | [diff] [blame^] | 106 | LOG(ERROR) << StringPrintf("%s: fail to read", __func__); |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 107 | nfa_nv_ci_read(0, NFA_NV_CO_FAIL, block); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 108 | } |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 109 | } else { |
Ruchi Kandoi | 6767aec | 2017-09-26 09:46:26 -0700 | [diff] [blame^] | 110 | DLOG_IF(INFO, nfc_debug_enabled) |
| 111 | << StringPrintf("%s: fail to open", __func__); |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 112 | nfa_nv_ci_read(0, NFA_NV_CO_FAIL, block); |
| 113 | } |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /******************************************************************************* |
| 117 | ** |
| 118 | ** Function nfa_nv_co_write |
| 119 | ** |
| 120 | ** Description This function is called by io to send file data to the |
| 121 | ** phone. |
| 122 | ** |
| 123 | ** Parameters pBuffer - buffer to read the data from. |
| 124 | ** nbytes - number of bytes to write out to the file. |
| 125 | ** |
| 126 | ** Returns void |
| 127 | ** |
| 128 | ** Note: Upon completion of the request, nfa_nv_ci_write() is |
| 129 | ** called with the file descriptor and the status. The |
Ruchi Kandoi | 552f2b7 | 2017-01-28 16:22:55 -0800 | [diff] [blame] | 130 | ** call-in function should only be called when ALL |
| 131 | ** requested bytes have been written, or an error has |
| 132 | ** been detected, |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 133 | ** |
| 134 | *******************************************************************************/ |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 135 | extern void nfa_nv_co_write(const uint8_t* pBuffer, uint16_t nbytes, |
| 136 | uint8_t block) { |
| 137 | char filename[256], filename2[256]; |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 138 | |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 139 | memset(filename, 0, sizeof(filename)); |
| 140 | memset(filename2, 0, sizeof(filename2)); |
| 141 | strcpy(filename2, bcm_nfc_location); |
| 142 | strncat(filename2, sNfaStorageBin, sizeof(filename2) - strlen(filename2) - 1); |
| 143 | if (strlen(filename2) > 200) { |
Ruchi Kandoi | 6767aec | 2017-09-26 09:46:26 -0700 | [diff] [blame^] | 144 | LOG(ERROR) << StringPrintf("%s: filename too long", __func__); |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 145 | return; |
| 146 | } |
| 147 | sprintf(filename, "%s%u", filename2, block); |
Ruchi Kandoi | 6767aec | 2017-09-26 09:46:26 -0700 | [diff] [blame^] | 148 | DLOG_IF(INFO, nfc_debug_enabled) |
| 149 | << StringPrintf("%s: bytes=%u; file=%s", __func__, nbytes, filename); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 150 | |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 151 | int fileStream = 0; |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 152 | |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 153 | fileStream = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); |
| 154 | if (fileStream >= 0) { |
| 155 | unsigned short checksum = crcChecksumCompute(pBuffer, nbytes); |
| 156 | size_t actualWrittenCrc = write(fileStream, &checksum, sizeof(checksum)); |
| 157 | size_t actualWrittenData = write(fileStream, pBuffer, nbytes); |
Ruchi Kandoi | 6767aec | 2017-09-26 09:46:26 -0700 | [diff] [blame^] | 158 | DLOG_IF(INFO, nfc_debug_enabled) |
| 159 | << StringPrintf("%s: %zu bytes written", __func__, actualWrittenData); |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 160 | if ((actualWrittenData == nbytes) && |
| 161 | (actualWrittenCrc == sizeof(checksum))) { |
| 162 | nfa_nv_ci_write(NFA_NV_CO_OK); |
| 163 | } else { |
Ruchi Kandoi | 6767aec | 2017-09-26 09:46:26 -0700 | [diff] [blame^] | 164 | LOG(ERROR) << StringPrintf("%s: fail to write", __func__); |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 165 | nfa_nv_ci_write(NFA_NV_CO_FAIL); |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 166 | } |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 167 | close(fileStream); |
| 168 | } else { |
Ruchi Kandoi | 6767aec | 2017-09-26 09:46:26 -0700 | [diff] [blame^] | 169 | LOG(ERROR) << StringPrintf("%s: fail to open, error = %d", __func__, errno); |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 170 | nfa_nv_ci_write(NFA_NV_CO_FAIL); |
| 171 | } |
The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /******************************************************************************* |
| 175 | ** |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 176 | ** Function delete_stack_non_volatile_store |
| 177 | ** |
| 178 | ** Description Delete all the content of the stack's storage location. |
| 179 | ** |
Evan Chu | edbfba9 | 2013-04-10 13:57:34 -0400 | [diff] [blame] | 180 | ** Parameters forceDelete: unconditionally delete the storage. |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 181 | ** |
| 182 | ** Returns none |
| 183 | ** |
| 184 | *******************************************************************************/ |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 185 | void delete_stack_non_volatile_store(bool forceDelete) { |
| 186 | static bool firstTime = true; |
| 187 | char filename[256], filename2[256]; |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 188 | |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 189 | if ((firstTime == false) && (forceDelete == false)) return; |
| 190 | firstTime = false; |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 191 | |
Ruchi Kandoi | 6767aec | 2017-09-26 09:46:26 -0700 | [diff] [blame^] | 192 | DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", __func__); |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 193 | |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 194 | memset(filename, 0, sizeof(filename)); |
| 195 | memset(filename2, 0, sizeof(filename2)); |
| 196 | strcpy(filename2, bcm_nfc_location); |
| 197 | strncat(filename2, sNfaStorageBin, sizeof(filename2) - strlen(filename2) - 1); |
| 198 | if (strlen(filename2) > 200) { |
Ruchi Kandoi | 6767aec | 2017-09-26 09:46:26 -0700 | [diff] [blame^] | 199 | LOG(ERROR) << StringPrintf("%s: filename too long", __func__); |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 200 | return; |
| 201 | } |
| 202 | sprintf(filename, "%s%u", filename2, DH_NV_BLOCK); |
| 203 | remove(filename); |
| 204 | sprintf(filename, "%s%u", filename2, HC_F3_NV_BLOCK); |
| 205 | remove(filename); |
| 206 | sprintf(filename, "%s%u", filename2, HC_F4_NV_BLOCK); |
| 207 | remove(filename); |
| 208 | sprintf(filename, "%s%u", filename2, HC_F2_NV_BLOCK); |
| 209 | remove(filename); |
| 210 | sprintf(filename, "%s%u", filename2, HC_F5_NV_BLOCK); |
| 211 | remove(filename); |
Martijn Coenen | 5c65c3a | 2013-03-27 13:23:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /******************************************************************************* |
| 215 | ** |
Evan Chu | edbfba9 | 2013-04-10 13:57:34 -0400 | [diff] [blame] | 216 | ** Function verify_stack_non_volatile_store |
| 217 | ** |
| 218 | ** Description Verify the content of all non-volatile store. |
| 219 | ** |
| 220 | ** Parameters none |
| 221 | ** |
| 222 | ** Returns none |
| 223 | ** |
| 224 | *******************************************************************************/ |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 225 | void verify_stack_non_volatile_store() { |
Ruchi Kandoi | 6767aec | 2017-09-26 09:46:26 -0700 | [diff] [blame^] | 226 | DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", __func__); |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 227 | char filename[256], filename2[256]; |
| 228 | bool isValid = false; |
Evan Chu | edbfba9 | 2013-04-10 13:57:34 -0400 | [diff] [blame] | 229 | |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 230 | memset(filename, 0, sizeof(filename)); |
| 231 | memset(filename2, 0, sizeof(filename2)); |
| 232 | strcpy(filename2, bcm_nfc_location); |
| 233 | strncat(filename2, sNfaStorageBin, sizeof(filename2) - strlen(filename2) - 1); |
| 234 | if (strlen(filename2) > 200) { |
Ruchi Kandoi | 6767aec | 2017-09-26 09:46:26 -0700 | [diff] [blame^] | 235 | LOG(ERROR) << StringPrintf("%s: filename too long", __func__); |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 236 | return; |
| 237 | } |
Evan Chu | edbfba9 | 2013-04-10 13:57:34 -0400 | [diff] [blame] | 238 | |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 239 | sprintf(filename, "%s%u", filename2, DH_NV_BLOCK); |
| 240 | if (crcChecksumVerifyIntegrity(filename)) { |
| 241 | sprintf(filename, "%s%u", filename2, HC_F3_NV_BLOCK); |
| 242 | if (crcChecksumVerifyIntegrity(filename)) { |
| 243 | sprintf(filename, "%s%u", filename2, HC_F4_NV_BLOCK); |
| 244 | if (crcChecksumVerifyIntegrity(filename)) { |
| 245 | sprintf(filename, "%s%u", filename2, HC_F2_NV_BLOCK); |
| 246 | if (crcChecksumVerifyIntegrity(filename)) { |
| 247 | sprintf(filename, "%s%u", filename2, HC_F5_NV_BLOCK); |
| 248 | if (crcChecksumVerifyIntegrity(filename)) isValid = true; |
Evan Chu | edbfba9 | 2013-04-10 13:57:34 -0400 | [diff] [blame] | 249 | } |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 250 | } |
Evan Chu | edbfba9 | 2013-04-10 13:57:34 -0400 | [diff] [blame] | 251 | } |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 252 | } |
Evan Chu | edbfba9 | 2013-04-10 13:57:34 -0400 | [diff] [blame] | 253 | |
Ruchi Kandoi | 6fca02d | 2017-01-30 14:28:16 -0800 | [diff] [blame] | 254 | if (isValid == false) delete_stack_non_volatile_store(true); |
Evan Chu | edbfba9 | 2013-04-10 13:57:34 -0400 | [diff] [blame] | 255 | } |