-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathmemory.c
638 lines (546 loc) · 19.1 KB
/
memory.c
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
The MIT License (MIT)
Copyright (c) 2015-2018 Douglas J. Bakkum
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "commander.h"
#include "memory.h"
#include "random.h"
#include "utils.h"
#include "flags.h"
#include "flash.h"
#include "hmac.h"
#include "sha2.h"
#ifndef TESTING
#include <gpio.h>
#include <delay.h>
#include <ioport.h>
#include "ataes132.h"
#include "mcu.h"
#endif
static uint8_t MEM_unlocked = DEFAULT_unlocked;
static uint8_t MEM_erased = DEFAULT_erased;
static uint8_t MEM_setup = DEFAULT_setup;
static uint32_t MEM_ext_flags = DEFAULT_ext_flags;
static uint32_t MEM_u2f_count = DEFAULT_u2f_count;
static uint16_t MEM_pin_err = DBB_ACCESS_INITIALIZE;
static uint16_t MEM_access_err = DBB_ACCESS_INITIALIZE;
__extension__ static uint8_t MEM_active_key[] = {[0 ... MEM_PAGE_LEN - 1] = 0xFF};
__extension__ static uint8_t MEM_user_entropy[] = {[0 ... MEM_PAGE_LEN - 1] = 0xFF};
__extension__ static uint8_t MEM_aeskey_stand[] = {[0 ... MEM_PAGE_LEN - 1] = 0xFF};
__extension__ static uint8_t MEM_aeskey_hidden[] = {[0 ... MEM_PAGE_LEN - 1] = 0xFF};
__extension__ static uint8_t MEM_aeskey_verify[] = {[0 ... MEM_PAGE_LEN - 1] = 0xFF};
__extension__ static uint8_t MEM_master_hww_entropy[] = {[0 ... MEM_PAGE_LEN - 1] = 0xFF};
__extension__ static uint8_t MEM_master_hww_chain[] = {[0 ... MEM_PAGE_LEN - 1] = 0xFF};
__extension__ static uint8_t MEM_master_hww[] = {[0 ... MEM_PAGE_LEN - 1] = 0xFF};
__extension__ static uint8_t MEM_hidden_hww_chain[] = {[0 ... MEM_PAGE_LEN - 1] = 0xFF};
__extension__ static uint8_t MEM_hidden_hww[] = {[0 ... MEM_PAGE_LEN - 1] = 0xFF};
__extension__ static uint8_t MEM_master_u2f[] = {[0 ... MEM_PAGE_LEN - 1] = 0xFF};
__extension__ static uint8_t MEM_name[] = {[0 ... MEM_PAGE_LEN - 1] = '0'};
__extension__ const uint8_t MEM_PAGE_ERASE[] = {[0 ... MEM_PAGE_LEN - 1] = 0xFF};
__extension__ const uint16_t MEM_PAGE_ERASE_2X[] = {[0 ... MEM_PAGE_LEN - 1] = 0xFFFF};
__extension__ const uint8_t MEM_PAGE_ERASE_FE[] = {[0 ... MEM_PAGE_LEN - 1] = 0xFE};
static uint8_t memory_eeprom(uint8_t *write_b, uint8_t *read_b, const int32_t addr,
const uint16_t len)
{
#ifndef TESTING
// read current memory
if (ataes_eeprom(len, addr, read_b, NULL) != DBB_OK) {
commander_fill_report(cmd_str(CMD_ataes), NULL, DBB_ERR_MEM_ATAES);
return DBB_ERROR;
}
#endif
if (write_b) {
#ifndef TESTING
// skip writing if memory does not change
if (read_b) {
if (!memcmp(read_b, write_b, len)) {
return DBB_OK;
}
}
if (ataes_eeprom(len, addr, read_b, write_b) != DBB_OK) {
commander_fill_report(cmd_str(CMD_ataes), NULL, DBB_ERR_MEM_ATAES);
return DBB_ERROR;
}
if (read_b) {
if (!memcmp(write_b, read_b, len)) {
return DBB_OK;
} else {
// error
if (len > 2) {
memcpy(read_b, MEM_PAGE_ERASE, len);
}
return DBB_ERROR;
}
}
#else
memcpy(read_b, write_b, len);
(void) addr;
return DBB_OK;
#endif
}
return DBB_OK;
}
// Encrypted storage
static uint8_t memory_eeprom_crypt(const uint8_t *write_b, uint8_t *read_b,
const int32_t addr)
{
int enc_len, dec_len;
char *enc, *dec, enc_r[MEM_PAGE_LEN * 4 + 1] = {0};
static uint8_t mempass[MEM_PAGE_LEN];
// Encrypt data saved to memory using an AES key obfuscated by the
// bootloader bytes.
memset(mempass, 0, sizeof(mempass));
#ifndef TESTING
uint8_t rn[FLASH_USERSIG_RN_LEN] = {0};
sha256_Raw((uint8_t *)(FLASH_BOOT_START), FLASH_BOOT_LEN, mempass);
flash_read_user_signature((uint32_t *)rn, FLASH_USERSIG_RN_LEN / sizeof(uint32_t));
if (memcmp(rn, MEM_PAGE_ERASE, FLASH_USERSIG_RN_LEN)) {
hmac_sha256(mempass, MEM_PAGE_LEN, rn, FLASH_USERSIG_RN_LEN, mempass);
}
#endif
sha256_Raw(mempass, MEM_PAGE_LEN, mempass);
sha256_Raw((const uint8_t *)(utils_uint8_to_hex(mempass, MEM_PAGE_LEN)), MEM_PAGE_LEN * 2,
mempass);
sha256_Raw(mempass, MEM_PAGE_LEN, mempass);
if (read_b) {
enc = aes_cbc_b64_encrypt((unsigned char *)utils_uint8_to_hex(read_b, MEM_PAGE_LEN),
MEM_PAGE_LEN * 2, &enc_len, mempass);
if (!enc) {
goto err;
}
snprintf(enc_r, sizeof(enc_r), "%.*s", enc_len, enc);
free(enc);
}
if (write_b) {
char enc_w[MEM_PAGE_LEN * 4 + 1] = {0};
enc = aes_cbc_b64_encrypt((unsigned char *)utils_uint8_to_hex(write_b, MEM_PAGE_LEN),
MEM_PAGE_LEN * 2, &enc_len, mempass);
if (!enc) {
goto err;
}
snprintf(enc_w, sizeof(enc_w), "%.*s", enc_len, enc);
free(enc);
if (memory_eeprom((uint8_t *)enc_w, (uint8_t *)enc_r, addr,
MEM_PAGE_LEN) == DBB_ERROR) {
goto err;
}
if (memory_eeprom((uint8_t *)enc_w + MEM_PAGE_LEN, (uint8_t *)enc_r + MEM_PAGE_LEN,
addr + MEM_PAGE_LEN, MEM_PAGE_LEN) == DBB_ERROR) {
goto err;
}
if (memory_eeprom((uint8_t *)enc_w + MEM_PAGE_LEN * 2,
(uint8_t *)enc_r + MEM_PAGE_LEN * 2, addr + MEM_PAGE_LEN * 2,
MEM_PAGE_LEN) == DBB_ERROR) {
goto err;
}
if (memory_eeprom((uint8_t *)enc_w + MEM_PAGE_LEN * 3,
(uint8_t *)enc_r + MEM_PAGE_LEN * 3, addr + MEM_PAGE_LEN * 3,
MEM_PAGE_LEN) == DBB_ERROR) {
goto err;
}
} else {
if (memory_eeprom(NULL, (uint8_t *)enc_r, addr, MEM_PAGE_LEN) == DBB_ERROR) {
goto err;
}
if (memory_eeprom(NULL, (uint8_t *)enc_r + MEM_PAGE_LEN, addr + MEM_PAGE_LEN,
MEM_PAGE_LEN) == DBB_ERROR) {
goto err;
}
if (memory_eeprom(NULL, (uint8_t *)enc_r + MEM_PAGE_LEN * 2, addr + MEM_PAGE_LEN * 2,
MEM_PAGE_LEN) == DBB_ERROR) {
goto err;
}
if (memory_eeprom(NULL, (uint8_t *)enc_r + MEM_PAGE_LEN * 3, addr + MEM_PAGE_LEN * 3,
MEM_PAGE_LEN) == DBB_ERROR) {
goto err;
}
}
dec = aes_cbc_b64_decrypt((unsigned char *)enc_r, MEM_PAGE_LEN * 4, &dec_len,
mempass);
if (!dec) {
goto err;
}
memcpy(read_b, utils_hex_to_uint8(dec), MEM_PAGE_LEN);
utils_zero(dec, dec_len);
free(dec);
utils_zero(mempass, MEM_PAGE_LEN);
utils_clear_buffers();
return DBB_OK;
err:
utils_zero(mempass, MEM_PAGE_LEN);
utils_clear_buffers();
return DBB_ERROR;
}
static void memory_write_setup(uint8_t setup)
{
memory_eeprom(&setup, &MEM_setup, MEM_SETUP_ADDR, 1);
}
static uint8_t memory_read_setup(void)
{
memory_eeprom(NULL, &MEM_setup, MEM_SETUP_ADDR, 1);
return MEM_setup;
}
static void memory_scramble_default_aeskeys(void)
{
uint8_t number[32] = {0};
random_bytes(number, sizeof(number), 0);
memcpy(MEM_aeskey_stand, number, MEM_PAGE_LEN);
memcpy(MEM_aeskey_hidden, number, MEM_PAGE_LEN);
memcpy(MEM_aeskey_verify, number, MEM_PAGE_LEN);
memcpy(MEM_active_key, number, MEM_PAGE_LEN);
}
static void memory_scramble_rn(void)
{
#ifndef TESTING
uint32_t i = 0;
uint8_t usersig[FLASH_USERSIG_SIZE];
uint8_t number[FLASH_USERSIG_RN_LEN] = {0};
random_bytes(number, FLASH_USERSIG_RN_LEN, 0);
flash_read_user_signature((uint32_t *)usersig, FLASH_USERSIG_SIZE / sizeof(uint32_t));
for (i = 0; i < FLASH_USERSIG_RN_LEN; i++) {
usersig[i] ^= number[i];
}
flash_erase_user_signature();
flash_write_user_signature((uint32_t *)usersig, FLASH_USERSIG_SIZE / sizeof(uint32_t));
#endif
}
uint8_t memory_setup(void)
{
if (memory_read_setup()) {
// One-time setup on factory install
#ifndef TESTING
// Lock Config Memory: OP MODE PARAMETER1 PARAMETER2
const uint8_t ataes_cmd[] = {0x0D, 0x02, 0x00, 0x00, 0x00, 0x00};
// Return packet [Count(1) || Return Code (1) || CRC (2)]
uint8_t ataes_ret[4] = {0};
if (ataes_process(ataes_cmd, sizeof(ataes_cmd), ataes_ret, 4) != DBB_OK) {
return DBB_ERROR;
}
#endif
uint32_t c = 0x00000000;
memory_reset_hww();
memory_reset_u2f();
memory_eeprom((uint8_t *)&c, (uint8_t *)&MEM_u2f_count, MEM_U2F_COUNT_ADDR, 4);
memory_write_setup(0x00);
} else {
memory_read_ext_flags();
memory_eeprom(NULL, &MEM_erased, MEM_ERASED_ADDR, 1);
memory_master_u2f(NULL);// Load cache so that U2F speed is fast enough
memory_read_access_err_count();// Load cache
memory_u2f_count_read();
}
memory_scramble_default_aeskeys();
return DBB_OK;
}
void memory_erase_hww_seed(void)
{
memory_master_hww_entropy(MEM_PAGE_ERASE);
memory_master_hww_chaincode(MEM_PAGE_ERASE);
memory_master_hww(MEM_PAGE_ERASE);
memory_hidden_hww_chaincode(MEM_PAGE_ERASE_FE);
memory_hidden_hww(MEM_PAGE_ERASE_FE);
memory_random_password(PASSWORD_HIDDEN);
}
void memory_reset_hww(void)
{
uint8_t u2f[MEM_PAGE_LEN];
memcpy(u2f, MEM_master_u2f, MEM_PAGE_LEN);
memory_scramble_rn();
memory_master_u2f(u2f);
memory_random_password(PASSWORD_STAND);
memory_random_password(PASSWORD_VERIFY);
memory_random_password(PASSWORD_HIDDEN);
memory_erase_hww_seed();
memory_name(DEVICE_DEFAULT_NAME);
memory_write_erased(DEFAULT_erased);
memory_write_unlocked(DEFAULT_unlocked);
memory_write_ext_flags(DEFAULT_ext_flags);
memory_access_err_count(DBB_ACCESS_INITIALIZE);
memory_pin_err_count(DBB_ACCESS_INITIALIZE);
utils_zero(u2f, sizeof(u2f));
}
void memory_reset_u2f(void)
{
// Create random master U2F key. It is independent of the HWW.
// U2F is functional on fresh device without a seeded wallet.
uint8_t number[32] = {0};
random_bytes(number, sizeof(number), 0);
memory_master_u2f(number);
utils_zero(number, sizeof(number));
}
void memory_random_password(PASSWORD_ID id)
{
uint8_t number[16] = {0};
random_bytes(number, sizeof(number), 0);
memory_write_aeskey(utils_uint8_to_hex(number, sizeof(number)), sizeof(number) * 2, id);
utils_zero(number, sizeof(number));
utils_clear_buffers();
}
void memory_clear(void)
{
#ifndef TESTING
// Zero important variables in RAM on embedded MCU.
// Do not clear for testing routines (i.e. not embedded).
memcpy(MEM_hidden_hww_chain, MEM_PAGE_ERASE, MEM_PAGE_LEN);
memcpy(MEM_hidden_hww, MEM_PAGE_ERASE, MEM_PAGE_LEN);
memcpy(MEM_master_hww_chain, MEM_PAGE_ERASE, MEM_PAGE_LEN);
memcpy(MEM_master_hww, MEM_PAGE_ERASE, MEM_PAGE_LEN);
memcpy(MEM_master_hww_entropy, MEM_PAGE_ERASE, MEM_PAGE_LEN);
#endif
}
uint8_t *memory_name(const char *name)
{
uint8_t name_b[MEM_PAGE_LEN] = {0};
if (strlens(name)) {
snprintf((char *)name_b, MEM_PAGE_LEN, "%s", name);
memory_eeprom(name_b, MEM_name, MEM_NAME_ADDR, MEM_PAGE_LEN);
} else {
memory_eeprom(NULL, MEM_name, MEM_NAME_ADDR, MEM_PAGE_LEN);
}
return MEM_name;
}
uint8_t *memory_hidden_hww(const uint8_t *master)
{
memory_eeprom_crypt(NULL, MEM_hidden_hww, MEM_HIDDEN_BIP32_ADDR);
if ((master == NULL) && !memcmp(MEM_hidden_hww, MEM_PAGE_ERASE, 32)) {
// Backward compatible with firmware <=2.2.3
return memory_master_hww_chaincode(NULL);
}
memory_eeprom_crypt(master, MEM_hidden_hww, MEM_HIDDEN_BIP32_ADDR);
return MEM_hidden_hww;
}
uint8_t *memory_hidden_hww_chaincode(const uint8_t *chain)
{
memory_eeprom_crypt(NULL, MEM_hidden_hww_chain, MEM_HIDDEN_BIP32_CHAIN_ADDR);
if ((chain == NULL) && !memcmp(MEM_hidden_hww_chain, MEM_PAGE_ERASE, 32)) {
// Backward compatible with firmware <=2.2.3
return memory_master_hww(NULL);
}
memory_eeprom_crypt(chain, MEM_hidden_hww_chain, MEM_HIDDEN_BIP32_CHAIN_ADDR);
return MEM_hidden_hww_chain;
}
uint8_t *memory_master_hww(const uint8_t *master)
{
memory_eeprom_crypt(master, MEM_master_hww, MEM_MASTER_BIP32_ADDR);
return MEM_master_hww;
}
uint8_t *memory_master_hww_chaincode(const uint8_t *chain)
{
memory_eeprom_crypt(chain, MEM_master_hww_chain, MEM_MASTER_BIP32_CHAIN_ADDR);
return MEM_master_hww_chain;
}
uint8_t *memory_master_hww_entropy(const uint8_t *master_entropy)
{
memory_eeprom_crypt(master_entropy, MEM_master_hww_entropy, MEM_MASTER_ENTROPY_ADDR);
return MEM_master_hww_entropy;
}
uint8_t *memory_master_u2f(const uint8_t *master_u2f)
{
memory_eeprom_crypt(master_u2f, MEM_master_u2f, MEM_MASTER_U2F_ADDR);
return MEM_master_u2f;
}
uint8_t *memory_report_master_u2f(void)
{
return MEM_master_u2f;
}
void memory_active_key_set(uint8_t *key)
{
if (key) {
memcpy(MEM_active_key, key, MEM_PAGE_LEN);
}
}
uint8_t *memory_active_key_get(void)
{
return MEM_active_key;
}
uint8_t memory_write_aeskey(const char *password, int len, PASSWORD_ID id)
{
int ret = 0;
uint8_t password_b[MEM_PAGE_LEN];
memset(password_b, 0, MEM_PAGE_LEN);
if (len < PASSWORD_LEN_MIN || strlens(password) < PASSWORD_LEN_MIN) {
return DBB_ERR_IO_PASSWORD_LEN;
}
sha256_Raw((const uint8_t *)password, len, password_b);
sha256_Raw(password_b, MEM_PAGE_LEN, password_b);
switch ((int)id) {
case PASSWORD_STAND:
memcpy(MEM_aeskey_stand, password_b, MEM_PAGE_LEN);
break;
case PASSWORD_HIDDEN:
memcpy(MEM_aeskey_hidden, password_b, MEM_PAGE_LEN);
break;
case PASSWORD_VERIFY:
memcpy(MEM_aeskey_verify, password_b, MEM_PAGE_LEN);
break;
default: {
/* never reached */
}
}
ret |= memory_eeprom_crypt(MEM_aeskey_stand, MEM_aeskey_stand,
MEM_AESKEY_STAND_ADDR) - DBB_OK;
ret |= memory_eeprom_crypt(MEM_aeskey_hidden, MEM_aeskey_hidden,
MEM_AESKEY_HIDDEN_ADDR) - DBB_OK;
ret |= memory_eeprom_crypt(MEM_aeskey_verify, MEM_aeskey_verify,
MEM_AESKEY_VERIFY_ADDR) - DBB_OK;
utils_zero(password_b, MEM_PAGE_LEN);
if (ret) {
return DBB_ERR_MEM_ATAES;
} else {
return DBB_OK;
}
}
void memory_read_aeskeys(void)
{
static uint8_t read = 0;
if (!read) {
memory_eeprom_crypt(NULL, MEM_aeskey_stand, MEM_AESKEY_STAND_ADDR);
memory_eeprom_crypt(NULL, MEM_aeskey_hidden, MEM_AESKEY_HIDDEN_ADDR);
memory_eeprom_crypt(NULL, MEM_aeskey_verify, MEM_AESKEY_VERIFY_ADDR);
sha256_Raw(MEM_aeskey_stand, MEM_PAGE_LEN, MEM_user_entropy);
read++;
}
}
uint8_t *memory_report_aeskey(PASSWORD_ID id)
{
switch ((int)id) {
case PASSWORD_STAND:
return MEM_aeskey_stand;
case PASSWORD_HIDDEN:
return MEM_aeskey_hidden;
case PASSWORD_VERIFY:
return MEM_aeskey_verify;
default:
return NULL;
}
}
uint8_t *memory_report_user_entropy(void)
{
return MEM_user_entropy;
}
uint8_t memory_report_setup(void)
{
return MEM_setup;
}
void memory_write_unlocked(uint8_t u)
{
memory_eeprom(&u, &MEM_unlocked, MEM_UNLOCKED_ADDR, 1);
}
uint8_t memory_read_unlocked(void)
{
memory_eeprom(NULL, &MEM_unlocked, MEM_UNLOCKED_ADDR, 1);
return MEM_unlocked;
}
void memory_write_erased(uint8_t erased)
{
memory_eeprom(&erased, &MEM_erased, MEM_ERASED_ADDR, 1);
}
uint8_t memory_read_erased(void)
{
memory_eeprom(NULL, &MEM_erased, MEM_ERASED_ADDR, 1);
return MEM_erased;
}
uint8_t memory_report_erased(void)
{
return MEM_erased;
}
uint16_t memory_access_err_count(const uint8_t access)
{
uint16_t err_count = 0xF0F0;
if (access == DBB_ACCESS_ITERATE) {
memory_eeprom(NULL, (uint8_t *)&MEM_access_err, MEM_ACCESS_ERR_ADDR, 2);
err_count = MEM_access_err + 1;
} else if (access == DBB_ACCESS_INITIALIZE) {
err_count = 0;
} else {
err_count = COMMANDER_MAX_ATTEMPTS; // corrupted input
}
// Force reset after too many failed attempts
if (err_count >= COMMANDER_MAX_ATTEMPTS) {
commander_force_reset();
} else {
memory_eeprom((uint8_t *)&err_count, (uint8_t *)&MEM_access_err, MEM_ACCESS_ERR_ADDR, 2);
}
return err_count;
}
uint16_t memory_read_access_err_count(void)
{
memory_eeprom(NULL, (uint8_t *)&MEM_access_err, MEM_ACCESS_ERR_ADDR, 2);
return MEM_access_err;
}
uint16_t memory_report_access_err_count(void)
{
return MEM_access_err;
}
uint16_t memory_pin_err_count(const uint8_t access)
{
uint16_t err_count = 0xF0F0;
if (access == DBB_ACCESS_ITERATE) {
memory_eeprom(NULL, (uint8_t *)&MEM_pin_err, MEM_PIN_ERR_ADDR, 2);
err_count = MEM_pin_err + 1;
} else if (access == DBB_ACCESS_INITIALIZE) {
err_count = 0;
} else {
err_count = COMMANDER_MAX_ATTEMPTS; // corrupted input
}
// Force reset after too many failed attempts
if (err_count >= COMMANDER_MAX_ATTEMPTS) {
commander_force_reset();
} else {
memory_eeprom((uint8_t *)&err_count, (uint8_t *)&MEM_pin_err, MEM_PIN_ERR_ADDR, 2);
}
return err_count;
}
uint16_t memory_read_pin_err_count(void)
{
memory_eeprom(NULL, (uint8_t *)&MEM_pin_err, MEM_PIN_ERR_ADDR, 2);
return MEM_pin_err;
}
uint32_t memory_u2f_count_iter(void)
{
uint32_t c;
memory_u2f_count_read();
c = MEM_u2f_count + 1;
memory_eeprom((uint8_t *)&c, (uint8_t *)&MEM_u2f_count, MEM_U2F_COUNT_ADDR, 4);
return MEM_u2f_count;
}
void memory_u2f_count_set(uint32_t c)
{
memory_eeprom((uint8_t *)&c, (uint8_t *)&MEM_u2f_count, MEM_U2F_COUNT_ADDR, 4);
}
uint32_t memory_u2f_count_read(void)
{
memory_eeprom(NULL, (uint8_t *)&MEM_u2f_count, MEM_U2F_COUNT_ADDR, 4);
return MEM_u2f_count;
}
void memory_write_ext_flags(uint32_t flags)
{
memory_eeprom((uint8_t *)&flags, (uint8_t *)&MEM_ext_flags, MEM_EXT_FLAGS_ADDR, 4);
}
uint32_t memory_read_ext_flags(void)
{
memory_eeprom(NULL, (uint8_t *)&MEM_ext_flags, MEM_EXT_FLAGS_ADDR, 4);
return MEM_ext_flags;
}
uint32_t memory_report_ext_flags(void)
{
return MEM_ext_flags;
}