-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_config.cpp
417 lines (339 loc) · 14.2 KB
/
device_config.cpp
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
#include <apptools/device_config.h>
#include <cstring>
#include "esp_log.h"
#include "esp_random.h"
#include "cJSON.h"
#include "esp_ota_ops.h"
#include <esp_app_format.h>
#include <sys/param.h>
#include <apptools/fs_utils.h>
#define CONFIG_FILE_PATH "/mnt/config.json"
#define MOUNT_POINT "/mnt"
static const char* TAG = "device_config";
static char buffer_s[1024];
/*
esp_err_t dump_flash_contents() {
const esp_partition_t* running = esp_ota_get_running_partition();
if (!running) {
ESP_LOGE(TAG, "Failed to get running partition");
return ESP_FAIL;
}
// Read through entire binary and dump structure
size_t offset = 0;
uint8_t buffer[32];
esp_err_t ret;
// Read and dump header
esp_image_header_t header;
ret = esp_partition_read(running, 0, &header, sizeof(header));
if (ret != ESP_OK) {
return ret;
}
ESP_LOGI(TAG, "Image Header at offset 0x%x:", offset);
ESP_LOGI(TAG, " Magic: 0x%02x", header.magic);
ESP_LOGI(TAG, " Segment Count: %d", header.segment_count);
ESP_LOGI(TAG, " SHA256: %s", header.hash_appended ? "Yes" : "No");
ESP_LOGI(TAG, " Entry: 0x%08lx", header.entry_addr);
offset += sizeof(header);
// Process each segment
size_t total_size = sizeof(header);
for (int i = 0; i < header.segment_count; i++) {
esp_image_segment_header_t seg_header;
ret = esp_partition_read(running, offset, &seg_header, sizeof(seg_header));
if (ret != ESP_OK) break;
ESP_LOGI(TAG, "Segment %d at offset 0x%x:", i, offset);
ESP_LOGI(TAG, " Load Address: 0x%08lx", seg_header.load_addr);
ESP_LOGI(TAG, " Size: %ld", seg_header.data_len);
offset += sizeof(seg_header);
total_size += sizeof(seg_header);
// Show first and last bytes of segment
if (seg_header.data_len > 0) {
// First bytes
ret = esp_partition_read(running, offset, buffer,
MIN(32, seg_header.data_len));
if (ret == ESP_OK) {
ESP_LOGI(TAG, " First bytes:");
ESP_LOG_BUFFER_HEX(TAG, buffer, MIN(32, seg_header.data_len));
}
// Last bytes if segment is large enough
if (seg_header.data_len > 32) {
ret = esp_partition_read(running,
offset + seg_header.data_len - 32,
buffer, 32);
if (ret == ESP_OK) {
ESP_LOGI(TAG, " Last bytes:");
ESP_LOG_BUFFER_HEX(TAG, buffer, 32);
}
}
}
offset += seg_header.data_len;
total_size += seg_header.data_len;
}
// Check for padding to 16-byte boundary
size_t padding = (16 - (total_size % 16)) % 16;
if (padding > 0) {
ESP_LOGI(TAG, "Padding bytes at offset 0x%x: %d bytes", offset, padding);
ret = esp_partition_read(running, offset, buffer, padding);
if (ret == ESP_OK) {
ESP_LOG_BUFFER_HEX(TAG, buffer, padding);
}
total_size += padding;
}
// Check for appended SHA256
if (header.hash_appended) {
ESP_LOGI(TAG, "SHA256 at offset 0x%x:", offset + padding);
ret = esp_partition_read(running, offset + padding, buffer, 32);
if (ret == ESP_OK) {
ESP_LOG_BUFFER_HEX(TAG, buffer, 32);
}
total_size += 32;
}
ESP_LOGI(TAG, "Total calculated size: %d", total_size);
return ESP_OK;
}
esp_err_t compute_firmware_sha256_0() {
const esp_partition_t* running = esp_ota_get_running_partition();
if (!running) {
ESP_LOGE(TAG, "Failed to get running partition");
return ESP_FAIL;
}
// Initialize SHA-256
mbedtls_sha256_context sha_ctx;
mbedtls_sha256_init(&sha_ctx);
mbedtls_sha256_starts(&sha_ctx, 0);
const size_t BUFFER_SIZE = 4096;
uint8_t* buffer = (uint8_t*)malloc(BUFFER_SIZE);
if (!buffer) {
ESP_LOGE(TAG, "Failed to allocate buffer");
return ESP_ERR_NO_MEM;
}
// Calculate total size including padding
esp_image_header_t header;
esp_err_t ret = esp_partition_read(running, 0, &header, sizeof(header));
if (ret != ESP_OK) {
free(buffer);
return ret;
}
ESP_LOGI(TAG, "Header: magic=0x%02x segments=%d has_sha256=%d",
header.magic, header.segment_count, header.hash_appended);
// Calculate size up to end of segments
size_t total_size = sizeof(header);
size_t offset = total_size;
for (int i = 0; i < header.segment_count; i++) {
esp_image_segment_header_t seg_header;
ret = esp_partition_read(running, offset, &seg_header, sizeof(seg_header));
if (ret != ESP_OK) break;
ESP_LOGI(TAG, "Segment %d: offset=0x%x size=%ld", i, offset, seg_header.data_len);
total_size += sizeof(seg_header) + seg_header.data_len;
offset += sizeof(seg_header) + seg_header.data_len;
}
// Calculate padding size (align to 16 bytes)
size_t padding_size = (16 - (total_size % 16)) % 16;
if (padding_size > 0) {
ESP_LOGI(TAG, "Padding size: %d bytes", padding_size);
total_size += padding_size;
}
// Add SHA-256 size
if (header.hash_appended) {
total_size += 32;
}
ESP_LOGI(TAG, "Total binary size: %d bytes", total_size);
// Now hash everything except the appended SHA-256
size_t bytes_to_hash = total_size - 32; // Don't include appended SHA
size_t remaining = bytes_to_hash;
offset = 0;
while (remaining > 0) {
size_t to_read = (remaining < BUFFER_SIZE) ? remaining : BUFFER_SIZE;
ret = esp_partition_read(running, offset, buffer, to_read);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to read at offset 0x%x", offset);
break;
}
// Debug output for first and last blocks
if (offset == 0) {
ESP_LOGI(TAG, "First %d bytes:", MIN(32, to_read));
ESP_LOG_BUFFER_HEX(TAG, buffer, MIN(32, to_read));
}
if (remaining <= BUFFER_SIZE) {
ESP_LOGI(TAG, "Last %d bytes:", MIN(32, to_read));
ESP_LOG_BUFFER_HEX(TAG, buffer + (to_read - MIN(32, to_read)),
MIN(32, to_read));
}
mbedtls_sha256_update(&sha_ctx, buffer, to_read);
remaining -= to_read;
offset += to_read;
}
ESP_LOGI(TAG, "Hashed %d bytes", bytes_to_hash);
// Read the appended SHA for comparison
uint8_t appended_hash[32];
ret = esp_partition_read(running, bytes_to_hash, appended_hash, 32);
if (ret == ESP_OK) {
ESP_LOGI(TAG, "Appended SHA-256:");
ESP_LOG_BUFFER_HEX(TAG, appended_hash, 32);
}
// Calculate final hash
uint8_t computed_hash[32];
mbedtls_sha256_finish(&sha_ctx, computed_hash);
mbedtls_sha256_free(&sha_ctx);
printf("\nSHA256: ");
for (int i = 0; i < 32; i++) {
printf("%02x", computed_hash[i]);
}
printf("\n");
ESP_LOGI(TAG, "Computed SHA-256:");
ESP_LOG_BUFFER_HEX(TAG, computed_hash, 32);
free(buffer);
return ESP_OK;
}
*/
static void generate_uuid(char* uuid_str) {
uint32_t random_values[4];
for (int i = 0; i < 4; i++) {
random_values[i] = esp_random();
}
snprintf(uuid_str, device_config_t::MAX_EQUIPMENT_ID_LENGTH,
"%08" PRIx32 "-%04" PRIx16 "-%04" PRIx16 "-%04" PRIx16 "-%04" PRIx16 "%08" PRIx32,
random_values[0],
(uint16_t)(random_values[1] >> 16),
((uint16_t)random_values[1] & 0xffff) | 0x4000, // Version 4
((uint16_t)(random_values[2] >> 16) & 0x3fff) | 0x8000, // Variant 1
(uint16_t)random_values[2],
random_values[3]);
}
esp_err_t device_config_set_sw_version(device_config_t* config, const char* hash) {
// FIRMWARE_VERSION comes from CMake
snprintf(config->software_revision,
device_config_t::MAX_SW_VERSION_LENGTH,
"%s;SHA256:%s",
FIRMWARE_VERSION,
hash);
return ESP_OK;
}
esp_err_t device_config_init(const char* manufacturer, const char* model, const char* hardware_revision, device_config_t* config) {
bool needs_save = false;
config->eid[0] = 0;
// Check if config file already exists
FILE* file = fopen(CONFIG_FILE_PATH, "r");
if (file != nullptr) {
ESP_LOGI(TAG, "Config file exists - checking for changes");
size_t file_size = fread(buffer_s, 1, sizeof(buffer_s), file);
ESP_LOGI(TAG, "read %d bytes", file_size);
fclose(file);
cJSON *root = cJSON_Parse(buffer_s);
if (root != nullptr) {
cJSON *eid1 = cJSON_GetObjectItemCaseSensitive(root, "equipment_id");
cJSON *eid2 = cJSON_GetObjectItemCaseSensitive(root, "eid");
if (cJSON_IsString(eid1)) {
strncpy(config->eid, eid1->valuestring, device_config_t::MAX_EQUIPMENT_ID_LENGTH - 1);
config->eid[device_config_t::MAX_EQUIPMENT_ID_LENGTH - 1] = '\0';
} else if (cJSON_IsString(eid2)) {
strncpy(config->eid, eid2->valuestring, device_config_t::MAX_EQUIPMENT_ID_LENGTH - 1);
config->eid[device_config_t::MAX_EQUIPMENT_ID_LENGTH - 1] = '\0';
}
// Read existing values to compare
cJSON *mfr = cJSON_GetObjectItemCaseSensitive(root, "manufacturer");
cJSON *mdl = cJSON_GetObjectItemCaseSensitive(root, "model");
cJSON *hw_rev = cJSON_GetObjectItemCaseSensitive(root, "hardware_revision");
needs_save = !cJSON_IsString(mfr) || strcmp(mfr->valuestring, manufacturer) != 0 ||
!cJSON_IsString(mdl) || strcmp(mdl->valuestring, model) != 0 ||
!cJSON_IsString(hw_rev) || strcmp(hw_rev->valuestring, hardware_revision) != 0;
cJSON_Delete(root);
} else {
needs_save = true;
}
} else {
needs_save = true;
}
// Generate new eid if empty
if (strlen(config->eid)==0) {
generate_uuid(config->eid);
ESP_LOGI(TAG, "generated new eid %s", config->eid);
needs_save = true;
} else {
ESP_LOGI(TAG, "eid is %s", config->eid);
}
strncpy(config->manufacturer, manufacturer, device_config_t::MAX_MANUFACTURER_LENGTH - 1);
config->manufacturer[device_config_t::MAX_MANUFACTURER_LENGTH - 1] = '\0';
strncpy(config->model, model, device_config_t::MAX_MODEL_LENGTH - 1);
config->model[device_config_t::MAX_MODEL_LENGTH - 1] = '\0';
strncpy(config->hardware_revision, hardware_revision, device_config_t::MAX_HW_VERSION_LENGTH - 1);
config->hardware_revision[device_config_t::MAX_HW_VERSION_LENGTH - 1] = '\0';
//verify_firmware_sha256(config->sw_sha256);
//dump_flash_contents();
//compute_firmware_sha256();
config->sw_sha256[60] = '\0';
compute_firmware_sha256(config->sw_sha256, device_config_t::SHA_LENGTH);
/*
const esp_app_desc_t* app_desc = esp_app_get_description();
for(int i = 0; i < 32; i++) {
sprintf(&config->sw_sha256[i*2], "%02x", app_desc->app_elf_sha256[i]);
}
*/
// Set software version with hash
device_config_set_sw_version(config, config->sw_sha256);
if (needs_save) {
ESP_LOGI(TAG, "Config changed - saving to file");
// Create JSON object
cJSON *root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "manufacturer", config->manufacturer);
cJSON_AddStringToObject(root, "model", config->model);
cJSON_AddStringToObject(root, "eid", config->eid);
cJSON_AddStringToObject(root, "hardware_revision", config->hardware_revision);
// Write JSON to file
file = fopen(CONFIG_FILE_PATH, "w");
if (file == NULL) {
ESP_LOGE(TAG, "Failed to open config file for writing");
cJSON_Delete(root);
return ESP_FAIL;
}
char *json_str = cJSON_Print(root);
fprintf(file, "%s", json_str);
fclose(file);
cJSON_free(json_str);
cJSON_Delete(root);
ESP_LOGI(TAG, "Saved new config file with UUID: %s", config->eid);
} else {
ESP_LOGI(TAG, "Config unchanged - not saving");
}
return ESP_OK;
}
esp_err_t device_config_init(device_config_t* config) {
FILE* file = fopen(CONFIG_FILE_PATH, "r");
if (file == NULL) {
ESP_LOGE(TAG, "Failed to open config file");
return ESP_FAIL;
}
size_t file_size = fread(buffer_s, 1, sizeof(buffer_s), file);
fclose(file);
cJSON *root = cJSON_Parse(buffer_s);
if (root == NULL) {
ESP_LOGE(TAG, "Failed to parse config file");
return ESP_FAIL;
}
cJSON *manufacturer = cJSON_GetObjectItemCaseSensitive(root, "manufacturer");
cJSON *model = cJSON_GetObjectItemCaseSensitive(root, "model");
cJSON *eid = cJSON_GetObjectItemCaseSensitive(root, "eid");
cJSON *hw_revision = cJSON_GetObjectItemCaseSensitive(root, "hardware_revision");
if (!cJSON_IsString(manufacturer) || !cJSON_IsString(model) || !cJSON_IsString(eid) || !cJSON_IsString(hw_revision)) {
ESP_LOGE(TAG, "Invalid config file format");
cJSON_Delete(root);
return ESP_FAIL;
}
strncpy(config->manufacturer, manufacturer->valuestring, device_config_t::MAX_MANUFACTURER_LENGTH - 1);
strncpy(config->model, model->valuestring, device_config_t::MAX_MODEL_LENGTH - 1);
strncpy(config->eid, eid->valuestring, device_config_t::MAX_EQUIPMENT_ID_LENGTH - 1);
strncpy(config->hardware_revision, hw_revision->valuestring, device_config_t::MAX_HW_VERSION_LENGTH - 1);
config->manufacturer[device_config_t::MAX_MANUFACTURER_LENGTH - 1] = '\0';
config->model[device_config_t::MAX_MODEL_LENGTH - 1] = '\0';
config->eid[device_config_t::MAX_EQUIPMENT_ID_LENGTH - 1] = '\0';
config->hardware_revision[device_config_t::MAX_HW_VERSION_LENGTH - 1] = '\0';
// Get hash from app description instead of partition
const esp_app_desc_t* app_desc = esp_app_get_description();
for(int i = 0; i < 32; i++) {
sprintf(&config->sw_sha256[i*2], "%02x", app_desc->app_elf_sha256[i]);
}
config->sw_sha256[64] = '\0';
device_config_set_sw_version(config, config->sw_sha256);
cJSON_Delete(root);
ESP_LOGI(TAG, "Loaded config: ID=%s, HW=%s", config->eid, config->hardware_revision);
return ESP_OK;
}