forked from Yurik72/ESPHap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.c
540 lines (456 loc) · 13.9 KB
/
storage.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
#include <string.h>
#include <ctype.h>
#include "esphap_debug.h"
#include "crypto.h"
#include "pairing.h"
#include "port.h"
//#include "storage_ex.h"
#pragma GCC diagnostic ignored "-Wunused-value"
#ifndef SPIFLASH_BASE_ADDR
#define SPIFLASH_BASE_ADDR 0x200000
#endif
#define MAGIC_OFFSET 0
#define ACCESSORY_ID_OFFSET 4
#define ACCESSORY_KEY_OFFSET 32
#define PAIRINGS_OFFSET 128
#define MAGIC_ADDR (SPIFLASH_BASE_ADDR + MAGIC_OFFSET)
#define ACCESSORY_ID_ADDR (SPIFLASH_BASE_ADDR + ACCESSORY_ID_OFFSET)
#define ACCESSORY_KEY_ADDR (SPIFLASH_BASE_ADDR + ACCESSORY_KEY_OFFSET)
#define PAIRINGS_ADDR (SPIFLASH_BASE_ADDR + PAIRINGS_OFFSET)
#ifndef MAX_PAIRINGS
#define MAX_PAIRINGS 16
#endif
byte max_pairing = MAX_PAIRINGS;
#ifndef ACCESSORY_ID_SIZE
#define ACCESSORY_ID_SIZE 17
#endif
#define ACCESSORY_KEY_SIZE 64
const char magic1[] = "HAP";
typedef struct {
char magic[sizeof(magic1)];
byte permissions;
char device_id[36];
byte device_public_key[32];
byte _reserved[7]; // align record to be 80 bytes
} pairing_data_t;
#ifdef EX_STORAGE_CHAR
#define MAGIC_ADDR_EX 0
#define ACCESSORY_ID_ADDR_EX (0 + ACCESSORY_ID_OFFSET)
#define ACCESSORY_KEY_ADDR_EX (0 + ACCESSORY_KEY_OFFSET)
#define PAIRINGS_ADDR_EX (0 + PAIRINGS_OFFSET)
char ex_storage[PAIRINGS_OFFSET + MAX_PAIRINGS * sizeof(pairing_data_t) + 1] = { 0xFF };
int init_storage_ex(char* szdata, int size) {
int actualsize = size > sizeof(ex_storage) ? sizeof(ex_storage) : size;
INFO("init_storage_ex size 0x%x", actualsize);
memcpy((void *)&ex_storage[0], szdata, actualsize);
return actualsize;
}
char * get_ex_storage() {
return ex_storage;
}
int get_ex_storage_size() {
if (max_pairing == MAX_PAIRINGS) {
return sizeof(ex_storage);
}
return PAIRINGS_OFFSET + max_pairing * sizeof(pairing_data_t) + 1;
}
typedef void(*callback_function)(void);
static callback_function callbackstorage = NULL;
void set_callback_storage(callback_function fn) {
callbackstorage = fn;
}
void on_storage_change() {
if (callbackstorage) {
callbackstorage();
}
}
void set_max_pairing(byte val) {
max_pairing = val;
}
#endif
int homekit_storage_reset() {
byte blank[sizeof(magic1)];
#ifdef EX_STORAGE_CHAR
// blabla();
// memset((void*)ex_storage,0,sizeof(ex_storage));
memcpy((void *)&ex_storage[MAGIC_ADDR_EX], blank, sizeof(blank));
on_storage_change();
#else
if (!spiflash_write(MAGIC_ADDR, blank, sizeof(blank))) {
ERROR("Failed to reset flash");
return -1;
}
#endif
return 0;
}
int homekit_storage_init() {
char magic[sizeof(magic1)];
memset(magic, 0, sizeof(magic));
#ifdef EX_STORAGE_CHAR
memcpy(magic, (void *)&ex_storage[MAGIC_ADDR_EX], sizeof(magic));
#else
if (!spiflash_read(MAGIC_ADDR, (byte *)magic, sizeof(magic))) {
ERROR("Failed to read flash magic");
}
#endif
if (strncmp(magic, magic1, sizeof(magic1))) {
INFO("Formatting flash at 0x%x", SPIFLASH_BASE_ADDR);
#ifdef EX_STORAGE_CHAR
memset((void *)&ex_storage[0], 0xff, sizeof(ex_storage));
//on_storage_change();
//INFO("storage %s",ex_storage);
#else
if (!spiflash_erase_sector(SPIFLASH_BASE_ADDR)) {
ERROR("Failed to erase flash");
return -1;
}
#endif
strncpy(magic, magic1, sizeof(magic));
#ifdef EX_STORAGE_CHAR
if (!memcpy((void *)&ex_storage[MAGIC_ADDR_EX], (void *)magic, sizeof(magic))) {
ERROR("Failed to initialize ex_buffer");
return -1;
}
//INFO("storage %s",ex_storage);
on_storage_change();
// INFO("homekit storage ex init done");
#else
if (!spiflash_write(MAGIC_ADDR, (byte *)magic, sizeof(magic))) {
ERROR("Failed to initialize flash");
return -1;
}
#endif
// INFO("homekit_storage_init done");
return 1;
}
return 0;
}
void homekit_storage_save_accessory_id(const char *accessory_id) {
#ifdef EX_STORAGE_CHAR
memcpy((void *)&ex_storage[ACCESSORY_ID_ADDR_EX], (byte *)accessory_id, strlen(accessory_id));
on_storage_change();
#else
if (!spiflash_write(ACCESSORY_ID_ADDR, (byte *)accessory_id, strlen(accessory_id))) {
ERROR("Failed to write accessory ID to flash");
}
#endif
}
static char ishex(unsigned char c) {
c = toupper(c);
return isdigit(c) || (c >= 'A' && c <= 'F');
}
char *homekit_storage_load_accessory_id() {
byte data[ACCESSORY_ID_SIZE + 1];
// INFO("homekit load_accessory_id");
#ifdef EX_STORAGE_CHAR
memcpy((void*)data, &ex_storage[ACCESSORY_ID_ADDR_EX], sizeof(data));
#else
if (!spiflash_read(ACCESSORY_ID_ADDR, data, sizeof(data))) {
ERROR("Failed to read accessory ID from flash");
return NULL;
}
#endif
if (!data[0])
return NULL;
// INFO("data 0x%s",(char*)data);
data[sizeof(data) - 1] = 0;
for (int i = 0; i < 17; i++) {
if (i % 3 == 2) {
if (data[i] != ':')
return NULL;
}
else if (!ishex(data[i]))
return NULL;
}
return strndup((char *)data, sizeof(data));
}
void homekit_storage_save_accessory_key(const ed25519_key *key) {
byte key_data[ACCESSORY_KEY_SIZE];
size_t key_data_size = sizeof(key_data);
int r = crypto_ed25519_export_key(key, key_data, &key_data_size);
if (r) {
ERROR("Failed to export accessory key (code %d)", r);
return;
}
#ifdef EX_STORAGE_CHAR
memcpy((void*)&ex_storage[ACCESSORY_KEY_ADDR_EX], key_data, key_data_size);
on_storage_change();
#else
if (!spiflash_write(ACCESSORY_KEY_ADDR, key_data, key_data_size)) {
ERROR("Failed to write accessory key to flash");
return;
}
#endif
}
ed25519_key *homekit_storage_load_accessory_key() {
byte key_data[ACCESSORY_KEY_SIZE];
#ifdef EX_STORAGE_CHAR
memcpy(key_data, (void*)&ex_storage[ACCESSORY_KEY_ADDR_EX], sizeof(key_data));
#else
if (!spiflash_read(ACCESSORY_KEY_ADDR, key_data, sizeof(key_data))) {
ERROR("Failed to read accessory key from flash");
return NULL;
}
#endif
// INFO("homekit load_accessory_key");
ed25519_key *key = crypto_ed25519_new();
int r = crypto_ed25519_import_key(key, key_data, sizeof(key_data));
if (r) {
ERROR("Failed to import accessory key (code %d)", r);
crypto_ed25519_free(key);
return NULL;
}
return key;
}
// TODO: figure out alignment issues
bool homekit_storage_can_add_pairing() {
pairing_data_t data;
for (int i = 0; i < max_pairing; i++) {
#ifdef EX_STORAGE_CHAR
memcpy((byte *)&data, (void*)&ex_storage[PAIRINGS_ADDR_EX + sizeof(data)*i], sizeof(data));
#else
spiflash_read(PAIRINGS_ADDR + sizeof(data)*i, (byte *)&data, sizeof(data));
#endif
if (strncmp(data.magic, magic1, sizeof(magic1)))
return true;
}
return false;
}
static int compact_data() {
byte *data = malloc(SPI_FLASH_SECTOR_SIZE);
#ifdef EX_STORAGE_CHAR
memcpy((byte *)data, (void*)&ex_storage[0], SPI_FLASH_SECTOR_SIZE);
#else
if (!spiflash_read(SPIFLASH_BASE_ADDR, data, SPI_FLASH_SECTOR_SIZE)) {
free(data);
ERROR("Failed to compact data: sector data read error");
return -1;
}
#endif
int next_pairing_idx = 0;
for (int i = 0; i < max_pairing; i++) {
pairing_data_t *pairing_data = (pairing_data_t *)&data[PAIRINGS_OFFSET + sizeof(pairing_data_t)*i];
if (!strncmp(pairing_data->magic, magic1, sizeof(magic1))) {
if (i != next_pairing_idx) {
memcpy(&data[PAIRINGS_ADDR + sizeof(pairing_data_t)*next_pairing_idx],
pairing_data, sizeof(*pairing_data));
}
next_pairing_idx++;
}
}
if (next_pairing_idx == max_pairing) {
// We are full, no compaction possible, do not waste flash erase cycle
return 0;
}
if (homekit_storage_reset()) {
ERROR("Failed to compact data: error resetting flash");
return -1;
}
if (homekit_storage_init() < 0) {
ERROR("Failed to compact data: error initializing flash");
return -1;
}
#ifdef EX_STORAGE_CHAR
memcpy((void*)&ex_storage[0], data, PAIRINGS_OFFSET + sizeof(pairing_data_t)*next_pairing_idx);
on_storage_change();
#else
if (!spiflash_write(SPIFLASH_BASE_ADDR, data, PAIRINGS_OFFSET + sizeof(pairing_data_t)*next_pairing_idx)) {
ERROR("Failed to compact data: error writing compacted data");
return -1;
}
#endif
return 0;
}
static int find_empty_block() {
byte data[sizeof(pairing_data_t)];
for (int i = 0; i < max_pairing; i++) {
#ifdef EX_STORAGE_CHAR
memcpy(data, (void*)&ex_storage[PAIRINGS_ADDR_EX + sizeof(data)*i], sizeof(data));
#else
spiflash_read(PAIRINGS_ADDR + sizeof(data)*i, data, sizeof(data));
#endif
bool block_empty = true;
for (int j = 0; j < sizeof(data); j++)
if (data[j] != 0xff) {
block_empty = false;
break;
}
if (block_empty)
return i;
}
return -1;
}
int homekit_storage_add_pairing(const char *device_id, const ed25519_key *device_key, byte permissions) {
int next_block_idx = find_empty_block();
if (next_block_idx == -1) {
compact_data();
next_block_idx = find_empty_block();
}
if (next_block_idx == -1) {
ERROR("Failed to write pairing info to flash: max number of pairings");
return -2;
}
pairing_data_t data;
memset(&data, 0, sizeof(data));
strncpy(data.magic, magic1, sizeof(data.magic));
data.permissions = permissions;
strncpy(data.device_id, device_id, sizeof(data.device_id));
size_t device_public_key_size = sizeof(data.device_public_key);
int r = crypto_ed25519_export_public_key(
device_key, data.device_public_key, &device_public_key_size
);
if (r) {
ERROR("Failed to export device public key (code %d)", r);
return -1;
}
#ifdef EX_STORAGE_CHAR
memcpy((void*)&ex_storage[PAIRINGS_ADDR_EX + sizeof(data)*next_block_idx], (byte *)&data, sizeof(data));
on_storage_change();
#else
if (!spiflash_write(PAIRINGS_ADDR + sizeof(data)*next_block_idx, (byte *)&data, sizeof(data))) {
ERROR("Failed to write pairing info to flash");
return -1;
}
#endif
return 0;
}
int homekit_storage_update_pairing(const char *device_id, byte permissions) {
pairing_data_t data;
for (int i = 0; i < max_pairing; i++) {
#ifdef EX_STORAGE_CHAR
memcpy((byte *)&data, (void*)&ex_storage[PAIRINGS_ADDR_EX + sizeof(data)*i], sizeof(data));
#else
spiflash_read(PAIRINGS_ADDR + sizeof(data)*i, (byte *)&data, sizeof(data));
#endif
if (strncmp(data.magic, magic1, sizeof(data.magic)))
continue;
if (!strncmp(data.device_id, device_id, sizeof(data.device_id))) {
int r;
ed25519_key *device_key = crypto_ed25519_new();
r = crypto_ed25519_import_public_key(device_key, data.device_public_key, sizeof(data.device_public_key));
if (r) {
ERROR("Failed to import device public key (code %d)", r);
crypto_ed25519_free(device_key);
return -2;
}
r = homekit_storage_add_pairing(data.device_id, device_key, permissions);
crypto_ed25519_free(device_key);
if (r) {
return -2;
}
memset(&data, 0, sizeof(data));
#ifdef EX_STORAGE_CHAR
memcpy((void*)&ex_storage[PAIRINGS_ADDR_EX + sizeof(data)*i], (byte *)&data, sizeof(data));
on_storage_change();
#else
if (!spiflash_write(PAIRINGS_ADDR + sizeof(data)*i, (byte *)&data, sizeof(data))) {
ERROR("Failed to update pairing: error erasing old record");
return -2;
}
#endif
return 0;
}
}
return -1;
}
int homekit_storage_remove_pairing(const char *device_id) {
pairing_data_t data;
for (int i = 0; i < max_pairing; i++) {
#ifdef EX_STORAGE_CHAR
memcpy((byte *)&data, (void*)&ex_storage[PAIRINGS_ADDR_EX + sizeof(data)*i], sizeof(data));
#else
spiflash_read(PAIRINGS_ADDR + sizeof(data)*i, (byte *)&data, sizeof(data));
#endif
if (strncmp(data.magic, magic1, sizeof(data.magic)))
continue;
if (!strncmp(data.device_id, device_id, sizeof(data.device_id))) {
memset(&data, 0, sizeof(data));
#ifdef EX_STORAGE_CHAR
memcpy((void*)&ex_storage[PAIRINGS_ADDR_EX + sizeof(data)*i], (byte *)&data, sizeof(data));
on_storage_change();
#else
if (!spiflash_write(PAIRINGS_ADDR + sizeof(data)*i, (byte *)&data, sizeof(data))) {
ERROR("Failed to remove pairing from flash");
return -2;
}
#endif
return 0;
}
}
return 0;
}
void homekit_storage_pairing_free(pairing_t * pairing) {
if (!pairing)
return;
if (pairing->device_id)
free(pairing->device_id);
if (pairing->device_key)
crypto_ed25519_free(pairing->device_key);
free(pairing);
}
pairing_t *homekit_storage_find_pairing(const char *device_id) {
pairing_data_t data;
for (int i = 0; i < max_pairing; i++) {
#ifdef EX_STORAGE_CHAR
memcpy((byte *)&data, (void*)&ex_storage[PAIRINGS_ADDR_EX + sizeof(data)*i], sizeof(data));
#else
spiflash_read(PAIRINGS_ADDR + sizeof(data)*i, (byte *)&data, sizeof(data));
#endif
if (strncmp(data.magic, magic1, sizeof(data.magic)))
continue;
if (!strncmp(data.device_id, device_id, sizeof(data.device_id))) {
ed25519_key *device_key = crypto_ed25519_new();
int r = crypto_ed25519_import_public_key(device_key, data.device_public_key, sizeof(data.device_public_key));
if (r) {
ERROR("Failed to import device public key (code %d)", r);
return NULL;
}
pairing_t *pairing = pairing_new();
pairing->id = i;
pairing->device_id = strndup(data.device_id, sizeof(data.device_id));
pairing->device_key = device_key;
pairing->permissions = data.permissions;
return pairing;
}
}
return NULL;
}
typedef struct {
int idx;
} pairing_iterator_t;
pairing_iterator_t *homekit_storage_pairing_iterator() {
pairing_iterator_t *it = malloc(sizeof(pairing_iterator_t));
it->idx = 0;
return it;
}
void homekit_storage_pairing_iterator_free(pairing_iterator_t *iterator) {
free(iterator);
}
pairing_t *homekit_storage_next_pairing(pairing_iterator_t *it) {
pairing_data_t data;
while (it->idx < max_pairing) {
int id = it->idx++;
#ifdef EX_STORAGE_CHAR
memcpy((byte *)&data, (void*)&ex_storage[PAIRINGS_ADDR_EX + sizeof(data)*id], sizeof(data));
#else
spiflash_read(PAIRINGS_ADDR + sizeof(data)*id, (byte *)&data, sizeof(data));
#endif
if (!strncmp(data.magic, magic1, sizeof(data.magic))) {
ed25519_key *device_key = crypto_ed25519_new();
int r = crypto_ed25519_import_public_key(device_key, data.device_public_key, sizeof(data.device_public_key));
if (r) {
ERROR("Failed to import device public key (code %d)", r);
crypto_ed25519_free(device_key);
it->idx++;
continue;
}
pairing_t *pairing = pairing_new();
pairing->id = id;
pairing->device_id = strndup(data.device_id, sizeof(data.device_id));
pairing->device_key = device_key;
pairing->permissions = data.permissions;
return pairing;
}
}
return NULL;
}