@@ -167,83 +167,86 @@ static uint16_t prepare_coin_specific_data_tlv(
167
167
return find_latest_coin_data (
168
168
coin_specific_data , & coin_data_len , & coin_data_addr );
169
169
}
170
+ #ifndef BTC_ONLY_BUILD
171
+ // This function is commented out as it is not used in the current
172
+ // implementation. It is kept here for reference in case it is needed in the
173
+ // future. It was intended to purge coin specific data from flash by reading all
174
+ // the unique coin data and writing them back to flash after erasing the flash.
175
+ // It is a complex operation and may not be necessary in the current context.
176
+
177
+ static void purge_coin_specific_data () {
178
+ // Store all the unique data length and address in an array
179
+ Coin_Type coin_type_arr [MAX_UNIQUE_COIN_COUNT ] = {COIN_TYPE_NEAR };
180
+ struct meta_data_t {
181
+ Coin_Specific_Data_Struct data_struct ;
182
+ uint16_t data_length ;
183
+ uint32_t data_addr ;
184
+ } meta_data_arr [MAX_UNIQUE_COIN_COUNT * MAX_WALLETS_ALLOWED ] = {0 };
185
+
186
+ for (size_t coin_type_index = 0 ; coin_type_index < MAX_UNIQUE_COIN_COUNT ;
187
+ coin_type_index ++ ) {
188
+ for (size_t wallet_id_index = 0 ; wallet_id_index < MAX_WALLETS_ALLOWED ;
189
+ wallet_id_index ++ ) {
190
+ size_t current_index =
191
+ coin_type_index * MAX_WALLETS_ALLOWED + wallet_id_index ;
192
+
193
+ meta_data_arr [current_index ].data_length = 0 ;
194
+ meta_data_arr [current_index ].data_addr = 0 ;
195
+
196
+ meta_data_arr [current_index ].data_struct .coin_type =
197
+ coin_type_arr [coin_type_index ];
198
+ memcpy (meta_data_arr [current_index ].data_struct .wallet_id ,
199
+ get_wallet_id (wallet_id_index ),
200
+ WALLET_ID_SIZE );
201
+ find_latest_coin_data (& meta_data_arr [current_index ].data_struct ,
202
+ & meta_data_arr [current_index ].data_length ,
203
+ & meta_data_arr [current_index ].data_addr );
204
+
205
+ if (meta_data_arr [current_index ].data_length > 0 &&
206
+ FLASH_COIN_SPECIFIC_BASE_ADDRESS <
207
+ meta_data_arr [current_index ].data_addr &&
208
+ meta_data_arr [current_index ].data_addr +
209
+ meta_data_arr [current_index ].data_length <
210
+ FLASH_END ) {
211
+ // Allocate a proper size array to store the data
212
+ meta_data_arr [current_index ].data_struct .coin_data =
213
+ (uint8_t * )malloc (meta_data_arr [current_index ].data_length );
214
+ ASSERT (meta_data_arr [current_index ].data_struct .coin_data != NULL );
215
+ read_cmd (meta_data_arr [current_index ].data_addr ,
216
+ (uint32_t * )meta_data_arr [current_index ].data_struct .coin_data ,
217
+ meta_data_arr [current_index ].data_length );
218
+ }
219
+ }
220
+ }
170
221
171
- // static void purge_coin_specific_data() {
172
- // // Store all the unique data length and address in an array
173
- // Coin_Type coin_type_arr[MAX_UNIQUE_COIN_COUNT] = {COIN_TYPE_NEAR};
174
- // struct meta_data_t {
175
- // Coin_Specific_Data_Struct data_struct;
176
- // uint16_t data_length;
177
- // uint32_t data_addr;
178
- // } meta_data_arr[MAX_UNIQUE_COIN_COUNT * MAX_WALLETS_ALLOWED] = {0};
179
-
180
- // for (size_t coin_type_index = 0; coin_type_index < MAX_UNIQUE_COIN_COUNT;
181
- // coin_type_index++) {
182
- // for (size_t wallet_id_index = 0; wallet_id_index < MAX_WALLETS_ALLOWED;
183
- // wallet_id_index++) {
184
- // size_t current_index =
185
- // coin_type_index * MAX_WALLETS_ALLOWED + wallet_id_index;
186
-
187
- // meta_data_arr[current_index].data_length = 0;
188
- // meta_data_arr[current_index].data_addr = 0;
189
-
190
- // meta_data_arr[current_index].data_struct.coin_type =
191
- // coin_type_arr[coin_type_index];
192
- // memcpy(meta_data_arr[current_index].data_struct.wallet_id,
193
- // get_wallet_id(wallet_id_index),
194
- // WALLET_ID_SIZE);
195
- // find_latest_coin_data(&meta_data_arr[current_index].data_struct,
196
- // &meta_data_arr[current_index].data_length,
197
- // &meta_data_arr[current_index].data_addr);
198
-
199
- // if (meta_data_arr[current_index].data_length > 0 &&
200
- // FLASH_COIN_SPECIFIC_BASE_ADDRESS <
201
- // meta_data_arr[current_index].data_addr &&
202
- // meta_data_arr[current_index].data_addr +
203
- // meta_data_arr[current_index].data_length <
204
- // FLASH_END) {
205
- // // Allocate a proper size array to store the data
206
- // meta_data_arr[current_index].data_struct.coin_data =
207
- // (uint8_t *)malloc(meta_data_arr[current_index].data_length);
208
- // ASSERT(meta_data_arr[current_index].data_struct.coin_data != NULL);
209
- // read_cmd(meta_data_arr[current_index].data_addr,
210
- // (uint32_t
211
- // *)meta_data_arr[current_index].data_struct.coin_data,
212
- // meta_data_arr[current_index].data_length);
213
- // }
214
- // }
215
- // }
216
-
217
- // // Erase everything from flash
218
- // erase_flash_coin_specific_data();
219
-
220
- // // Write the data back to flash
221
- // for (size_t i = 0; i < MAX_UNIQUE_COIN_COUNT * MAX_WALLETS_ALLOWED; i++) {
222
- // if (meta_data_arr[i].data_length > 0 &&
223
- // FLASH_COIN_SPECIFIC_BASE_ADDRESS < meta_data_arr[i].data_addr &&
224
- // meta_data_arr[i].data_addr + meta_data_arr[i].data_length <
225
- // FLASH_END) {
226
- // uint16_t data_length =
227
- // (3 + WALLET_ID_SIZE) + (3 + meta_data_arr[i].data_length);
228
- // uint16_t tlv_size = 6 + data_length;
229
- // tlv_size = GET_NEXT_MULTIPLE_OF_8(tlv_size);
230
- // uint8_t tlv[tlv_size];
231
- // memzero(tlv, sizeof(tlv));
232
-
233
- // uint16_t offset =
234
- // prepare_coin_specific_data_tlv(&meta_data_arr[i].data_struct,
235
- // meta_data_arr[i].data_length,
236
- // tlv,
237
- // data_length);
238
- // write_cmd(
239
- // FLASH_COIN_SPECIFIC_BASE_ADDRESS + offset, (uint32_t *)tlv,
240
- // tlv_size);
241
-
242
- // free(meta_data_arr[i].data_struct.coin_data);
243
- // }
244
- // }
245
- // }
246
-
222
+ // Erase everything from flash
223
+ erase_flash_coin_specific_data ();
224
+
225
+ // Write the data back to flash
226
+ for (size_t i = 0 ; i < MAX_UNIQUE_COIN_COUNT * MAX_WALLETS_ALLOWED ; i ++ ) {
227
+ if (meta_data_arr [i ].data_length > 0 &&
228
+ FLASH_COIN_SPECIFIC_BASE_ADDRESS < meta_data_arr [i ].data_addr &&
229
+ meta_data_arr [i ].data_addr + meta_data_arr [i ].data_length < FLASH_END ) {
230
+ uint16_t data_length =
231
+ (3 + WALLET_ID_SIZE ) + (3 + meta_data_arr [i ].data_length );
232
+ uint16_t tlv_size = 6 + data_length ;
233
+ tlv_size = GET_NEXT_MULTIPLE_OF_8 (tlv_size );
234
+ uint8_t tlv [tlv_size ];
235
+ memzero (tlv , sizeof (tlv ));
236
+
237
+ uint16_t offset =
238
+ prepare_coin_specific_data_tlv (& meta_data_arr [i ].data_struct ,
239
+ meta_data_arr [i ].data_length ,
240
+ tlv ,
241
+ data_length );
242
+ write_cmd (
243
+ FLASH_COIN_SPECIFIC_BASE_ADDRESS + offset , (uint32_t * )tlv , tlv_size );
244
+
245
+ free (meta_data_arr [i ].data_struct .coin_data );
246
+ }
247
+ }
248
+ }
249
+ #endif // BTC_ONLY_BUILD
247
250
static int store_coin_data (const uint8_t * tlv_data ,
248
251
uint16_t tlv_data_size ,
249
252
uint16_t offset ) {
@@ -253,7 +256,13 @@ static int store_coin_data(const uint8_t *tlv_data,
253
256
(uint32_t * )tlv_data ,
254
257
tlv_data_size );
255
258
} else {
256
- // purge_coin_specific_data();
259
+ #ifndef BTC_ONLY_BUILD
260
+ // If there is not enough space, purge the coin specific data and try again.
261
+ // This is a complex operation and may not be necessary in the current
262
+ // context. It is commented out for now, but can be uncommented if needed in
263
+ // the future
264
+ purge_coin_specific_data ();
265
+ #endif // BTC_ONLY_BUILD
257
266
uint16_t coin_data_len = 0 ;
258
267
uint32_t coin_data_addr = 0 ;
259
268
Coin_Specific_Data_Struct dummy = {0 };
0 commit comments