Debian Codegraph @

src/scarlett2-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 <glib.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <arpa/inet.h>
9 #include <openssl/sha.h>
10 #include <sys/stat.h>
11
12 #include "scarlett2-firmware.h"
13
14 // List of found firmware files
15 struct found_firmware {
16 char *fn;
17 struct scarlett2_firmware_header *firmware;
18 };
19
20 GHashTable *best_firmware = NULL;
21
22 static int verify_sha256(
23 const unsigned char *data,
24 size_t length,
25 const unsigned char *expected_hash
26 ) {
27 unsigned char computed_hash[SHA256_DIGEST_LENGTH];
28 SHA256(data, length, computed_hash);
29 return memcmp(computed_hash, expected_hash, SHA256_DIGEST_LENGTH) == 0;
30 }
31
32 static struct scarlett2_firmware_file *read_header(FILE *file) {
33 struct scarlett2_firmware_file *firmware = calloc(
34 1, sizeof(struct scarlett2_firmware_file)
35 );
36 if (!firmware) {
37 perror("Failed to allocate memory for firmware structure");
38 goto error;
39 }
40
41 size_t read_count = fread(
42 &firmware->header, sizeof(struct scarlett2_firmware_header), 1, file
43 );
44
45 if (read_count != 1) {
46 if (feof(file))
47 fprintf(stderr, "Unexpected end of file\n");
48 else
49 perror("Failed to read header");
50 goto error;
51 }
52
53 if (strncmp(firmware->header.magic, MAGIC_STRING, 8) != 0) {
54 fprintf(stderr, "Invalid magic number\n");
55 goto error;
56 }
57
58 firmware->header.usb_vid = ntohs(firmware->header.usb_vid);
59 firmware->header.usb_pid = ntohs(firmware->header.usb_pid);
60 firmware->header.firmware_version = ntohl(firmware->header.firmware_version);
61 firmware->header.firmware_length = ntohl(firmware->header.firmware_length);
62
63 return firmware;
64
65 error:
66 free(firmware);
67 return NULL;
68 }
69
70 struct scarlett2_firmware_header *scarlett2_read_firmware_header(
71 const char *fn
72 ) {
73 FILE *file = fopen(fn, "rb");
74 if (!file) {
75 perror("fopen firmware header");
76 fprintf(stderr, "Unable to open %s\n", fn);
77 return NULL;
78 }
79
80 struct scarlett2_firmware_file *firmware = read_header(file);
81 if (!firmware) {
82 fprintf(stderr, "Error reading firmware header from %s\n", fn);
83 return NULL;
84 }
85
86 fclose(file);
87
88 return realloc(firmware, sizeof(struct scarlett2_firmware_header));
89 }
90
91 struct scarlett2_firmware_file *scarlett2_read_firmware_file(const char *fn) {
92 FILE *file = fopen(fn, "rb");
93 if (!file) {
94 perror("fopen firmware file");
95 fprintf(stderr, "Unable to open %s\n", fn);
96 return NULL;
97 }
98
99 struct scarlett2_firmware_file *firmware = read_header(file);
100 if (!firmware) {
101 fprintf(stderr, "Error reading firmware header from %s\n", fn);
102 return NULL;
103 }
104
105 firmware->firmware_data = malloc(firmware->header.firmware_length);
106 if (!firmware->firmware_data) {
107 perror("Failed to allocate memory for firmware data");
108 goto error;
109 }
110
111 size_t read_count = fread(
112 firmware->firmware_data, 1, firmware->header.firmware_length, file
113 );
114
115 if (read_count != firmware->header.firmware_length) {
116 if (feof(file))
117 fprintf(stderr, "Unexpected end of file\n");
118 else
119 perror("Failed to read firmware data");
120 fprintf(stderr, "Error reading firmware data from %s\n", fn);
121 goto error;
122 }
123
124 if (!verify_sha256(
125 firmware->firmware_data,
126 firmware->header.firmware_length,
127 firmware->header.sha256
128 )) {
129 fprintf(stderr, "Corrupt firmware (failed checksum) in %s\n", fn);
130 goto error;
131 }
132
133 fclose(file);
134 return firmware;
135
136 error:
137 scarlett2_free_firmware_file(firmware);
138 fclose(file);
139 return NULL;
140 }
141
142 void scarlett2_free_firmware_header(struct scarlett2_firmware_header *firmware) {
143 if (firmware)
144 free(firmware);
145 }
146
147 void scarlett2_free_firmware_file(struct scarlett2_firmware_file *firmware) {
148 if (firmware) {
149 free(firmware->firmware_data);
150 free(firmware);
151 }
152 }
153
154 static void free_found_firmware(gpointer data) {
155 struct found_firmware *found = data;
156
157 free(found->fn);
158 scarlett2_free_firmware_header(found->firmware);
159 free(found);
160 }
161
162 static void init_best_firmware(void) {
163 if (best_firmware)
164 return;
165
166 best_firmware = g_hash_table_new_full(
167 g_direct_hash, g_direct_equal, NULL, free_found_firmware
168 );
169 }
170
171 // Add a firmware file to the list of found firmware
172 // files, if it's better than the one already found
173 // for the same device.
174 static void add_found_firmware(
175 char *fn,
176 struct scarlett2_firmware_header *firmware
177 ) {
178 gpointer key = GINT_TO_POINTER(firmware->usb_pid);
179 struct found_firmware *found = g_hash_table_lookup(best_firmware, key);
180
181 // already have a firmware file for this device?
182 if (found) {
183
184 // lower version number, ignore
185 if (firmware->firmware_version <= found->firmware->firmware_version) {
186 free(fn);
187 scarlett2_free_firmware_header(firmware);
188 return;
189 }
190
191 // higher version number, replace
192 g_hash_table_remove(best_firmware, key);
193 }
194
195 found = malloc(sizeof(struct found_firmware));
196 if (!found) {
197 perror("Failed to allocate memory for firmware structure");
198 return;
199 }
200
201 found->fn = fn;
202 found->firmware = firmware;
203
204 g_hash_table_insert(best_firmware, key, found);
205 }
206
207 // look for firmware files in the given directory
208 static void enum_firmware_dir(const char *dir_name) {
209 DIR *dir = opendir(dir_name);
210
211 if (!dir) {
212 if (errno == ENOENT) {
213 fprintf(stderr, "Firmware directory %s does not exist\n", dir_name);
214 return;
215 }
216 fprintf(
217 stderr, "Error opening directory %s: %s\n", dir_name, strerror(errno)
218 );
219 return;
220 }
221
222 struct dirent *entry;
223
224 while ((entry = readdir(dir))) {
225 char *full_fn;
226
227 // check if the file is a .bin file
228 if (strlen(entry->d_name) < 4 ||
229 strcmp(entry->d_name + strlen(entry->d_name) - 4, ".bin") != 0)
230 continue;
231
232 // check if the file is a regular file
233 if (entry->d_type == DT_UNKNOWN) {
234 struct stat st;
235 full_fn = g_build_filename(dir_name, entry->d_name, NULL);
236 if (stat(full_fn, &st) < 0) {
237 perror("stat");
238 g_free(full_fn);
239 continue;
240 }
241 if (!S_ISREG(st.st_mode)) {
242 g_free(full_fn);
243 continue;
244 }
245 } else if (entry->d_type != DT_REG) {
246 continue;
247 } else {
248 full_fn = g_build_filename(dir_name, entry->d_name, NULL);
249 }
250
251 struct scarlett2_firmware_header *firmware =
252 scarlett2_read_firmware_header(full_fn);
253
254 if (!firmware) {
255 fprintf(stderr, "Error reading firmware file %s\n", full_fn);
256 g_free(full_fn);
257 continue;
258 }
259
260 add_found_firmware(full_fn, firmware);
261 }
262
263 closedir(dir);
264 }
265
266 void scarlett2_enum_firmware(void) {
267 init_best_firmware();
268
269 const char *fw_dir = getenv("SCARLETT2_FIRMWARE_DIR");
270
271 if (!fw_dir)
272 fw_dir = SCARLETT2_FIRMWARE_DIR;
273 enum_firmware_dir(fw_dir);
274 }
275
276 uint32_t scarlett2_get_best_firmware_version(uint32_t pid) {
277 struct found_firmware *found = g_hash_table_lookup(
278 best_firmware, GINT_TO_POINTER(pid)
279 );
280 if (!found)
281 return 0;
282
283 return found->firmware->firmware_version;
284 }
285
286 struct scarlett2_firmware_file *scarlett2_get_best_firmware(uint32_t pid) {
287 struct found_firmware *found = g_hash_table_lookup(
288 best_firmware, GINT_TO_POINTER(pid)
289 );
290 if (!found)
291 return NULL;
292
293 return scarlett2_read_firmware_file(found->fn);
294 }