Debian Codegraph @

src/scarlett4-firmware.c

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2023-2025 Geoffrey D. Bennett <[email protected]>
2 // SPDX-License-Identifier: GPL-3.0-or-later
3
4 #include "scarlett4-firmware.h"
5
6 #include <dirent.h>
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/stat.h>
12 #include <arpa/inet.h>
13 #include <glib.h>
14 #include <openssl/sha.h>
15 #include <openssl/evp.h>
16
17 const char *scarlett4_firmware_type_magic[] = {
18 [SCARLETT4_FIRMWARE_CONTAINER] = "SCARLBOX",
19 [SCARLETT4_FIRMWARE_APP] = "SCARLET4",
20 [SCARLETT4_FIRMWARE_ESP] = "SCARLESP",
21 [SCARLETT4_FIRMWARE_LEAPFROG] = "SCARLEAP"
22 };
23
24 // Hash table to store the best firmware for each PID
25 struct found_firmware {
26 char *fn;
27 struct scarlett4_firmware_container *container;
28 };
29
30 static GHashTable *best_firmware = NULL;
31
32 static int verify_sha256(
33 const unsigned char *data,
34 size_t length,
35 const unsigned char *expected_hash
36 ) {
37 unsigned char computed_hash[SHA256_DIGEST_LENGTH];
38 SHA256(data, length, computed_hash);
39 return memcmp(computed_hash, expected_hash, SHA256_DIGEST_LENGTH) == 0;
40 }
41
42 // Calculate MD5
43 static void calc_md5(
44 const unsigned char *data,
45 size_t length,
46 unsigned char *md5_out
47 ) {
48 EVP_MD_CTX *mdctx;
49 const EVP_MD *md;
50 unsigned int md_len;
51
52 mdctx = EVP_MD_CTX_new();
53 if (mdctx == NULL) {
54 fprintf(stderr, "Failed to create MD5 context\n");
55 return;
56 }
57 md = EVP_md5();
58 if (md == NULL) {
59 fprintf(stderr, "Failed to get MD5 digest\n");
60 EVP_MD_CTX_free(mdctx);
61 return;
62 }
63
64 EVP_DigestInit_ex(mdctx, md, NULL);
65 EVP_DigestUpdate(mdctx, data, length);
66 EVP_DigestFinal_ex(mdctx, md5_out, &md_len);
67
68 EVP_MD_CTX_free(mdctx);
69 }
70
71 // Convert magic string to enum
72 static int firmware_type_from_magic(const char *magic) {
73 for (int i = 0; i < SCARLETT4_FIRMWARE_TYPE_COUNT; i++)
74 if (strncmp(magic, scarlett4_firmware_type_magic[i], 8) == 0)
75 return i;
76 return -1;
77 }
78
79 static int read_magic(FILE *file, const char *fn) {
80 char magic[8];
81 if (fread(magic, sizeof(magic), 1, file) != 1) {
82 perror("Failed to read magic");
83 fprintf(stderr, "Error reading magic from %s\n", fn);
84 return -1;
85 }
86
87 return firmware_type_from_magic(magic);
88 }
89
90 // Convert from disk format to memory format
91 static struct scarlett4_firmware *firmware_header_disk_to_mem(
92 int type,
93 const struct scarlett4_firmware_header_disk *disk
94 ) {
95 struct scarlett4_firmware *firmware = calloc(1, sizeof(struct scarlett4_firmware));
96 if (!firmware) {
97 perror("Failed to allocate memory for firmware");
98 return NULL;
99 }
100
101 firmware->type = type;
102 firmware->usb_vid = ntohs(disk->usb_vid);
103 firmware->usb_pid = ntohs(disk->usb_pid);
104 for (int i = 0; i < 4; i++)
105 firmware->firmware_version[i] = ntohl(disk->firmware_version[i]);
106 firmware->firmware_length = ntohl(disk->firmware_length);
107 memcpy(firmware->sha256, disk->sha256, 32);
108
109 return firmware;
110 }
111
112 static struct scarlett4_firmware_container *firmware_container_header_disk_to_mem(
113 const struct scarlett4_firmware_container_header_disk *disk
114 ) {
115 struct scarlett4_firmware_container *container = calloc(
116 1, sizeof(struct scarlett4_firmware_container)
117 );
118 if (!container) {
119 perror("Failed to allocate memory for firmware container");
120 return NULL;
121 }
122
123 container->usb_vid = ntohs(disk->usb_vid);
124 container->usb_pid = ntohs(disk->usb_pid);
125 for (int i = 0; i < 4; i++)
126 container->firmware_version[i] = ntohl(disk->firmware_version[i]);
127 container->num_sections = ntohl(disk->num_sections);
128
129 return container;
130 }
131
132 static struct scarlett4_firmware *read_header(
133 FILE *file,
134 const char *fn,
135 int type
136 ) {
137 struct scarlett4_firmware_header_disk disk_header;
138 size_t read_count = fread(
139 &disk_header, sizeof(struct scarlett4_firmware_header_disk), 1, file
140 );
141
142 if (read_count != 1) {
143 fprintf(stderr, "Error reading firmware header\n");
144 return NULL;
145 }
146
147 struct scarlett4_firmware *firmware = firmware_header_disk_to_mem(type, &disk_header);
148
149 if (!firmware)
150 fprintf(stderr, "Invalid firmware header\n");
151
152 return firmware;
153 }
154
155 static struct scarlett4_firmware *read_header_and_data(
156 FILE *file,
157 const char *fn,
158 int type
159 ) {
160 struct scarlett4_firmware *firmware = read_header(file, fn, type);
161 if (!firmware) {
162 fprintf(stderr, "Error reading firmware header from %s\n", fn);
163 return NULL;
164 }
165
166 // Read firmware data
167 firmware->firmware_data = malloc(firmware->firmware_length);
168 if (!firmware->firmware_data) {
169 perror("Failed to allocate memory for firmware data");
170 goto error;
171 }
172 size_t read_count = fread(
173 firmware->firmware_data, 1, firmware->firmware_length, file
174 );
175 if (read_count != firmware->firmware_length) {
176 if (feof(file))
177 fprintf(stderr, "Unexpected end of file\n");
178 else
179 perror("Failed to read firmware data");
180 fprintf(stderr, "Error reading firmware data from %s\n", fn);
181 goto error;
182 }
183
184 // Verify the firmware data
185 if (!verify_sha256(
186 firmware->firmware_data,
187 firmware->firmware_length,
188 firmware->sha256
189 )) {
190 fprintf(stderr, "Corrupt firmware (failed checksum) in %s\n", fn);
191 goto error;
192 }
193
194 // Add the MD5 for ESP firmware
195 if (firmware->type == SCARLETT4_FIRMWARE_ESP) {
196 calc_md5(
197 firmware->firmware_data,
198 firmware->firmware_length,
199 firmware->md5
200 );
201 }
202
203 return firmware;
204
205 error:
206 free(firmware->firmware_data);
207 free(firmware);
208 return NULL;
209 }
210
211 static struct scarlett4_firmware *read_magic_and_header_and_data(
212 FILE *file,
213 const char *fn,
214 int section
215 ) {
216 int type = read_magic(file, fn);
217
218 if (type < 0 || type == SCARLETT4_FIRMWARE_CONTAINER) {
219 fprintf(
220 stderr,
221 "Invalid firmware type %d in section %d of %s\n",
222 type,
223 section + 1,
224 fn
225 );
226 return NULL;
227 }
228
229 return read_header_and_data(file, fn, type);
230 }
231
232 static struct scarlett4_firmware_container *read_firmware_container_header(
233 FILE *file,
234 const char *fn
235 ) {
236 struct scarlett4_firmware_container_header_disk disk_header;
237
238 // Read header
239 if (fread(
240 &disk_header,
241 sizeof(struct scarlett4_firmware_container_header_disk),
242 1,
243 file
244 ) != 1) {
245 perror("Failed to read container header");
246 fprintf(stderr, "Error reading container header from %s\n", fn);
247 return NULL;
248 }
249
250 return firmware_container_header_disk_to_mem(&disk_header);
251 }
252
253 static struct scarlett4_firmware_container *read_firmware_container(
254 FILE *file,
255 const char *fn
256 ) {
257 struct scarlett4_firmware_container *container =
258 read_firmware_container_header(file, fn);
259 if (!container) {
260 fprintf(stderr, "Error reading container header from %s\n", fn);
261 return NULL;
262 }
263
264 if (container->num_sections < 1 || container->num_sections > 3) {
265 fprintf(
266 stderr,
267 "Invalid number of sections in %s: %d\n",
268 fn,
269 container->num_sections
270 );
271 goto error;
272 }
273
274 // Allocate memory for sections
275 container->sections = calloc(
276 container->num_sections, sizeof(struct scarlett4_firmware *)
277 );
278 if (!container->sections) {
279 perror("Failed to allocate memory for firmware sections");
280 goto error;
281 }
282
283 // Read sections
284 for (uint32_t i = 0; i < container->num_sections; i++) {
285 container->sections[i] = read_magic_and_header_and_data(file, fn, i);
286 if (!container->sections[i]) {
287 fprintf(stderr, "Error reading section %d from %s\n", i + 1, fn);
288 goto error;
289 }
290 }
291
292 return container;
293
294 error:
295 scarlett4_free_firmware_container(container);
296 return NULL;
297 }
298
299 struct scarlett4_firmware_container *scarlett4_read_firmware_header(const char *fn) {
300 FILE *file = fopen(fn, "rb");
301 if (!file) {
302 perror("fopen");
303 fprintf(stderr, "Unable to open %s\n", fn);
304 return NULL;
305 }
306
307 int type = read_magic(file, fn);
308
309 struct scarlett4_firmware_container *container = NULL;
310
311 if (type == SCARLETT4_FIRMWARE_CONTAINER) {
312 container = read_firmware_container_header(file, fn);
313 fclose(file);
314 return container;
315 }
316
317 struct scarlett4_firmware *firmware = read_header(file, fn, type);
318 if (!firmware) {
319 fprintf(stderr, "Error reading firmware header from %s\n", fn);
320 fclose(file);
321 return NULL;
322 }
323
324 container = calloc(1, sizeof(struct scarlett4_firmware_container));
325 if (!container) {
326 perror("Failed to allocate memory for firmware container");
327 free(firmware);
328 fclose(file);
329 return NULL;
330 }
331
332 container->usb_pid = firmware->usb_pid;
333 container->usb_vid = firmware->usb_vid;
334 memcpy(container->firmware_version, firmware->firmware_version,
335 sizeof(container->firmware_version));
336 container->num_sections = 1;
337 container->sections = calloc(1, sizeof(struct scarlett4_firmware *));
338 container->sections[0] = firmware;
339
340 fclose(file);
341
342 return container;
343 }
344
345 struct scarlett4_firmware_container *scarlett4_read_firmware_file(const char *fn) {
346 // Open file
347 FILE *file = fopen(fn, "rb");
348 if (!file) {
349 perror("fopen");
350 fprintf(stderr, "Unable to open %s\n", fn);
351 return NULL;
352 }
353
354 // Read magic string
355 int type = read_magic(file, fn);
356
357 if (type < 0) {
358 fprintf(stderr, "Invalid firmware type\n");
359 fclose(file);
360 return NULL;
361 }
362
363 // Container?
364 if (type == SCARLETT4_FIRMWARE_CONTAINER) {
365 struct scarlett4_firmware_container *container =
366 read_firmware_container(file, fn);
367 fclose(file);
368 return container;
369 }
370
371 // Not a container; read the firmware header and data
372 struct scarlett4_firmware *firmware = read_header_and_data(file, fn, type);
373 if (!firmware) {
374 fprintf(stderr, "Error reading firmware from %s\n", fn);
375 fclose(file);
376 return NULL;
377 }
378
379 fclose(file);
380
381 // Put the firmware header and data into a container
382 struct scarlett4_firmware_container *container = calloc(
383 1, sizeof(struct scarlett4_firmware_container)
384 );
385 if (!container) {
386 perror("Failed to allocate memory for firmware container");
387 free(firmware->firmware_data);
388 free(firmware);
389 return NULL;
390 }
391
392 container->usb_pid = firmware->usb_pid;
393 container->usb_vid = firmware->usb_vid;
394 memcpy(container->firmware_version, firmware->firmware_version,
395 sizeof(container->firmware_version));
396 container->num_sections = 1;
397 container->sections = calloc(1, sizeof(struct scarlett4_firmware *));
398 container->sections[0] = firmware;
399
400 return container;
401 }
402
403 void scarlett4_free_firmware_container(struct scarlett4_firmware_container *container) {
404 if (!container)
405 return;
406
407 if (container->sections) {
408 for (uint32_t i = 0; i < container->num_sections; i++) {
409 struct scarlett4_firmware *firmware = container->sections[i];
410 if (!firmware)
411 continue;
412
413 free(firmware->firmware_data);
414 free(firmware);
415 }
416
417 free(container->sections);
418 }
419
420 free(container);
421 }
422
423 const char *scarlett4_firmware_type_to_string(enum scarlett4_firmware_type type) {
424 switch (type) {
425 case SCARLETT4_FIRMWARE_CONTAINER: return "container";
426 case SCARLETT4_FIRMWARE_APP: return "App";
427 case SCARLETT4_FIRMWARE_ESP: return "ESP";
428 case SCARLETT4_FIRMWARE_LEAPFROG: return "Leapfrog";
429 default: return "unknown";
430 }
431 }
432
433 // Compare two 4-valued firmware versions
434 // Returns: -1 if v1 < v2, 0 if equal, 1 if v1 > v2
435 static int firmware_version_cmp(const uint32_t *v1, const uint32_t *v2) {
436 for (int i = 0; i < 4; i++) {
437 if (v1[i] < v2[i])
438 return -1;
439 if (v1[i] > v2[i])
440 return 1;
441 }
442 return 0;
443 }
444
445 static void free_found_firmware(gpointer data) {
446 struct found_firmware *found = data;
447 free(found->fn);
448 scarlett4_free_firmware_container(found->container);
449 free(found);
450 }
451
452 static void init_best_firmware(void) {
453 if (best_firmware)
454 return;
455
456 best_firmware = g_hash_table_new_full(
457 g_direct_hash, g_direct_equal, NULL, free_found_firmware
458 );
459 }
460
461 // Add a firmware file to the list of found firmware files,
462 // if it's better than the one already found for the same device.
463 static void add_found_firmware(
464 char *fn,
465 struct scarlett4_firmware_container *container
466 ) {
467 gpointer key = GINT_TO_POINTER(container->usb_pid);
468 struct found_firmware *found = g_hash_table_lookup(best_firmware, key);
469
470 // Already have a firmware file for this device?
471 if (found) {
472 // Lower or equal version, ignore
473 if (firmware_version_cmp(container->firmware_version,
474 found->container->firmware_version) <= 0) {
475 free(fn);
476 scarlett4_free_firmware_container(container);
477 return;
478 }
479
480 // Higher version, replace
481 g_hash_table_remove(best_firmware, key);
482 }
483
484 found = malloc(sizeof(struct found_firmware));
485 if (!found) {
486 perror("Failed to allocate memory for firmware structure");
487 free(fn);
488 scarlett4_free_firmware_container(container);
489 return;
490 }
491
492 found->fn = fn;
493 found->container = container;
494
495 g_hash_table_insert(best_firmware, key, found);
496 }
497
498 // Look for firmware files in the given directory
499 static void enum_firmware_dir(const char *dir_name) {
500 DIR *dir = opendir(dir_name);
501
502 if (!dir) {
503 if (errno != ENOENT)
504 fprintf(stderr, "Error opening directory %s: %s\n",
505 dir_name, strerror(errno));
506 return;
507 }
508
509 struct dirent *entry;
510
511 while ((entry = readdir(dir))) {
512 char *full_fn;
513
514 // Check if the file is a .bin file
515 if (strlen(entry->d_name) < 4 ||
516 strcmp(entry->d_name + strlen(entry->d_name) - 4, ".bin") != 0)
517 continue;
518
519 // Check if the file is a regular file
520 if (entry->d_type == DT_UNKNOWN) {
521 struct stat st;
522 full_fn = g_build_filename(dir_name, entry->d_name, NULL);
523 if (stat(full_fn, &st) < 0) {
524 perror("stat");
525 g_free(full_fn);
526 continue;
527 }
528 if (!S_ISREG(st.st_mode)) {
529 g_free(full_fn);
530 continue;
531 }
532 } else if (entry->d_type != DT_REG) {
533 continue;
534 } else {
535 full_fn = g_build_filename(dir_name, entry->d_name, NULL);
536 }
537
538 // Read the firmware header
539 struct scarlett4_firmware_container *container = scarlett4_read_firmware_header(full_fn);
540 if (!container) {
541 g_free(full_fn);
542 continue;
543 }
544
545 add_found_firmware(full_fn, container);
546 }
547
548 closedir(dir);
549 }
550
551 void scarlett4_enum_firmware(void) {
552 init_best_firmware();
553
554 // Check for override directory
555 const char *override_dir = getenv("SCARLETT4_FIRMWARE_DIR");
556 if (override_dir) {
557 enum_firmware_dir(override_dir);
558 return;
559 }
560
561 enum_firmware_dir(SCARLETT4_FIRMWARE_DIR);
562 }
563
564 uint32_t *scarlett4_get_best_firmware_version(uint32_t pid) {
565 struct found_firmware *found = g_hash_table_lookup(
566 best_firmware, GINT_TO_POINTER(pid)
567 );
568 if (!found)
569 return NULL;
570 return found->container->firmware_version;
571 }
572
573 struct scarlett4_firmware_container *scarlett4_get_best_firmware(uint32_t pid) {
574 struct found_firmware *found = g_hash_table_lookup(
575 best_firmware, GINT_TO_POINTER(pid)
576 );
577 if (!found)
578 return NULL;
579
580 // Read the full firmware file (with data)
581 return scarlett4_read_firmware_file(found->fn);
582 }
583
584 // Compare two 4-valued firmware versions
585 // Returns: -1 if v1 < v2, 0 if v1 == v2, 1 if v1 > v2
586 static int version_cmp(const uint32_t *v1, const uint32_t *v2) {
587 for (int i = 0; i < 4; i++) {
588 if (v1[i] < v2[i]) return -1;
589 if (v1[i] > v2[i]) return 1;
590 }
591 return 0;
592 }
593
594 // Find a firmware section by type in a container
595 static struct scarlett4_firmware *find_section(
596 struct scarlett4_firmware_container *container,
597 enum scarlett4_firmware_type type
598 ) {
599 for (uint32_t i = 0; i < container->num_sections; i++)
600 if (container->sections[i]->type == type)
601 return container->sections[i];
602 return NULL;
603 }
604
605 int scarlett4_is_mid_upgrade(struct alsa_card *card) {
606 struct found_firmware *found = g_hash_table_lookup(
607 best_firmware, GINT_TO_POINTER(card->pid)
608 );
609 if (!found)
610 return 0;
611
612 // Read the full container (enumeration only stores metadata)
613 struct scarlett4_firmware_container *container =
614 scarlett4_read_firmware_file(found->fn);
615 if (!container)
616 return 0;
617
618 // Find leapfrog and ESP sections
619 struct scarlett4_firmware *leapfrog_fw =
620 find_section(container, SCARLETT4_FIRMWARE_LEAPFROG);
621 struct scarlett4_firmware *esp_fw =
622 find_section(container, SCARLETT4_FIRMWARE_ESP);
623
624 if (!leapfrog_fw || !esp_fw) {
625 scarlett4_free_firmware_container(container);
626 return 0;
627 }
628
629 // Check if leapfrog is loaded (device firmware == leapfrog version)
630 int leapfrog_loaded =
631 version_cmp(card->firmware_version_4, leapfrog_fw->firmware_version) == 0;
632
633 // Check if ESP needs update
634 int need_esp =
635 version_cmp(card->esp_firmware_version, esp_fw->firmware_version) != 0;
636
637 fprintf(stderr, "is_mid_upgrade: leapfrog_loaded=%d need_esp=%d\n",
638 leapfrog_loaded, need_esp);
639
640 scarlett4_free_firmware_container(container);
641
642 return leapfrog_loaded && need_esp;
643 }