forked from OpenKinect/libfreenect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.c
420 lines (382 loc) · 13.4 KB
/
loader.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
/*
* This file is part of the OpenKinect Project. http://www.openkinect.org
*
* Copyright (c) 2011 individual OpenKinect contributors. See the CONTRIB file
* for details.
*
* This code is licensed to you under the terms of the Apache License, version
* 2.0, or, at your option, the terms of the GNU General Public License,
* version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
* or the following URLs:
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.gnu.org/licenses/gpl-2.0.txt
*
* If you redistribute this file in source form, modified or unmodified, you
* may:
* 1) Leave this header intact and distribute it under the same terms,
* accompanying it with the APACHE20 and GPL20 files, or
* 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
* 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
* In all cases you must keep the copyright notice intact and include a copy
* of the CONTRIB file.
*
* Binary distributions must follow the binary distribution requirements of
* either License.
*/
#include "libfreenect.h"
#include "freenect_internal.h"
#include "loader.h"
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
static void dump_bl_cmd(freenect_context* ctx, bootloader_command cmd) {
int i;
for(i = 0; i < 24; i++)
FN_INFO("%02X ", ((unsigned char*)(&cmd))[i]);
FN_INFO("\n");
}
static void dump_cemd_cmd(freenect_context* ctx, cemdloader_command cmd) {
int i;
for(i = 0; i < 24; i++)
FN_INFO("%02X ", ((unsigned char*)(&cmd))[i]);
FN_INFO("(%d more zeros)\n", (int)(sizeof(cmd)-24));
}
static int get_reply(fnusb_dev* dev) {
freenect_context* ctx = dev->parent->parent;
unsigned char dump[512];
bootloader_status_code buffer;
int res;
int transferred;
res = fnusb_bulk(dev, 0x81, dump, 512, &transferred);
if(res != 0 || transferred != sizeof(bootloader_status_code)) {
FN_ERROR("Error reading reply: %d\ttransferred: %d (expected %d)\n", res, transferred, (int)(sizeof(bootloader_status_code)));
return res;
}
memcpy(&buffer, dump, sizeof(bootloader_status_code));
if(fn_le32(buffer.magic) != 0x0a6fe000) {
FN_ERROR("Error reading reply: invalid magic %08X\n",buffer.magic);
return -1;
}
if(fn_le32(buffer.tag) != dev->parent->audio_tag) {
FN_ERROR("Error reading reply: non-matching tag number %08X (expected %08X)\n", buffer.tag, dev->parent->audio_tag);
return -1;
}
if(fn_le32(buffer.status) != 0) {
FN_ERROR("Notice reading reply: last uint32_t was nonzero: %d\n", buffer.status);
}
FN_INFO("Reading reply: ");
int i;
for(i = 0; i < transferred; ++i) {
FN_INFO("%02X ", ((unsigned char*)(&buffer))[i]);
}
FN_INFO("\n");
return res;
}
static int check_version_string(fnusb_dev* dev) {
freenect_context* ctx = dev->parent->parent;
bootloader_command bootcmd;
memset(&bootcmd, 0, sizeof(bootcmd));
bootcmd.magic = fn_le32(0x06022009);
bootcmd.tag = fn_le32(dev->parent->audio_tag);
bootcmd.bytes = fn_le32(0x60);
bootcmd.cmd = fn_le32(0);
bootcmd.addr = fn_le32(0x15);
unsigned char buffer[512];
int res;
int transferred;
FN_INFO("check_version_string(): About to send: ");
dump_bl_cmd(ctx, bootcmd);
// Send "get version string" command
res = fnusb_bulk(dev, 1, (unsigned char*)&bootcmd, sizeof(bootcmd), &transferred);
if(res != 0 || transferred != sizeof(bootcmd)) {
FN_ERROR("Error: res: %d\ttransferred: %d (expected %d)\n",res, transferred, (int)sizeof(bootcmd));
return -1;
}
// Read version string reply
res = fnusb_bulk(dev, 0x81, buffer, 512, &transferred);
if(res != 0 ) {
FN_ERROR("Error reading version string: %d\ttransferred: %d (expected %d)\n", res, transferred, 0x60);
return res;
}
FN_INFO("Read version string: ");
int i;
for(i = 0; i < transferred; ++i) {
FN_INFO("%02X ", buffer[i]);
}
FN_INFO("\n");
// Read status code reply
res = get_reply(dev);
dev->parent->audio_tag++;
return res;
}
FN_INTERNAL int upload_firmware(fnusb_dev* dev, char * filename) {
freenect_context* ctx = dev->parent->parent;
/* Search for firmware file (audios.bin) in the following places:
* $LIBFREENECT_FIRMWARE_PATH
* .
* ${HOME}/.libfreenect
* /usr/local/share/libfreenect
* /usr/share/libfreenect
* ./../Resources/ ( for OS X )
*/
//need to add a forward slash
char fw_filename[1024];
sprintf(fw_filename, "/%s", filename);
int filenamelen = strlen(fw_filename);
int i;
int searchpathcount;
FILE* fw = NULL;
for(i = 0, searchpathcount = 6; !fw && i < searchpathcount; i++) {
char* fwfile;
int needs_free = 0;
switch(i) {
case 0: {
char* envpath = getenv("LIBFREENECT_FIRMWARE_PATH");
if (!envpath)
continue;
int pathlen = strlen(envpath);
fwfile = (char *)malloc(pathlen + filenamelen + 1);
strcpy(fwfile, envpath);
strcat(fwfile, fw_filename);
needs_free = 1;
}
break;
case 1:
//fwfile = "./audios.bin";
fwfile = (char *)malloc(2048);
needs_free = 1;
sprintf(fwfile, ".%s", fw_filename);
break;
case 2: {
// Construct $HOME/.libfreenect/
char* home = getenv("HOME");
if (!home)
continue;
int homelen = strlen(home);
char* dotfolder = "/.libfreenect";
int locallen = strlen(dotfolder);
fwfile = (char*)malloc(homelen + locallen + filenamelen + 1);
strcpy(fwfile, home);
strcat(fwfile, dotfolder);
strcat(fwfile, fw_filename);
needs_free = 1;
}
break;
case 3:
//fwfile = "/usr/local/share/libfreenect/audios.bin";
fwfile = (char *)malloc(2048);
needs_free = 1;
sprintf(fwfile, "/usr/local/share/libfreenect%s", fw_filename);
break;
case 4:
//fwfile = "/usr/share/libfreenect/audios.bin";
fwfile = (char *)malloc(2048);
needs_free = 1;
sprintf(fwfile, "/usr/share/libfreenect%s", fw_filename);
break;
case 5:
//fwfile = "./../Resources/audios.bin"; //default for OS X equivilant to: "./audios.bin";
fwfile = (char *)malloc(2048);
needs_free = 1;
sprintf(fwfile, "./../Resources%s", fw_filename);
break;
default: break;
}
FN_INFO("Trying to open %s as firmware...\n", fwfile);
fw = fopen(fwfile, "rb");
if (needs_free) {
free(fwfile);
}
}
if (!fw) {
FN_ERROR("upload_firmware: failed to find firmware file.\n");
return -errno;
}
// get the number of bytes of the file
fseek(fw , 0, SEEK_END);
int fw_num_bytes = ftell(fw);
rewind(fw);
if( fw_num_bytes <= 0 ){
FN_ERROR("upload_firmware: failed to find file with any data.\n");
return -errno;
}
unsigned char * fw_bytes = (unsigned char *)malloc(fw_num_bytes);
int numRead = fread(fw_bytes, 1, fw_num_bytes, fw);
fw_num_bytes = numRead; // just in case
int retVal = upload_firmware_from_memory(dev, fw_bytes, fw_num_bytes);
fclose(fw);
fw = NULL;
return retVal;
}
FN_INTERNAL int upload_firmware_from_memory(fnusb_dev* dev, unsigned char * fw_from_mem, unsigned int fw_size_in_btyes) {
freenect_context* ctx = dev->parent->parent;
bootloader_command bootcmd;
memset(&bootcmd, 0, sizeof(bootcmd));
bootcmd.magic = fn_le32(0x06022009);
int res;
int transferred;
firmware_header fwheader;
int read = 0;
int bytesLeft = fw_size_in_btyes;
unsigned char * readPtr = &fw_from_mem[0];
if (fw_size_in_btyes < sizeof(firmware_header)) {
FN_ERROR("upload_firmware: firmware image too small, has no header?\n");
return -errno;
}
memcpy(&fwheader, readPtr, sizeof(firmware_header));
// The file is serialized as little endian.
fwheader.magic = fn_le32(fwheader.magic);
fwheader.ver_major = fn_le16(fwheader.ver_major);
fwheader.ver_minor = fn_le16(fwheader.ver_minor);
fwheader.ver_release = fn_le16(fwheader.ver_release);
fwheader.ver_patch = fn_le16(fwheader.ver_patch);
fwheader.base_addr = fn_le32(fwheader.base_addr);
fwheader.size = fn_le32(fwheader.size);
fwheader.entry_addr = fn_le32(fwheader.entry_addr);
FN_INFO("Found firmware image:\n");
FN_INFO("\tmagic %08X\n", fwheader.magic);
FN_INFO("\tversion %02d.%02d.%02d.%02d\n", fwheader.ver_major, fwheader.ver_minor, fwheader.ver_release, fwheader.ver_patch);
FN_INFO("\tbase address 0x%08x\n", fwheader.base_addr);
FN_INFO("\tsize 0x%08x\n", fwheader.size);
FN_INFO("\tentry point 0x%08x\n", fwheader.entry_addr);
uint32_t addr = fwheader.base_addr;
unsigned char page[0x4000];
int readIndex = 0;
int total_bytes_sent = 0;
do {
read = (0x4000 > fwheader.size - total_bytes_sent) ? fwheader.size - total_bytes_sent : 0x4000;
// sanity check
if( read > bytesLeft ){
read = bytesLeft;
}
if (read <= 0) {
break;
}
memcpy(page, &readPtr[readIndex], read);
readIndex += read;
bytesLeft -= read;
bootcmd.tag = fn_le32(dev->parent->audio_tag);
bootcmd.bytes = fn_le32(read);
bootcmd.cmd = fn_le32(0x03);
bootcmd.addr = fn_le32(addr);
FN_INFO("About to send: ");
dump_bl_cmd(ctx, bootcmd);
// Send it off!
res = fnusb_bulk(dev, 1, (unsigned char*)&bootcmd, sizeof(bootcmd), &transferred);
if(res != 0 || transferred != sizeof(bootcmd)) {
FN_ERROR("upload_firmware(): Error: res: %d\ttransferred: %d (expected %d)\n",res, transferred, (int)(sizeof(bootcmd)));
return -1;
}
int bytes_sent = 0;
while(bytes_sent < read) {
int to_send = (read - bytes_sent > 512 ? 512 : read - bytes_sent);
res = fnusb_bulk(dev, 1, &page[bytes_sent], to_send, &transferred);
if(res != 0 || transferred != to_send) {
FN_ERROR("upload_firmware(): Error: res: %d\ttransferred: %d (expected %d)\n",res, transferred, to_send);
return -1;
}
bytes_sent += to_send;
total_bytes_sent += to_send;
}
res = get_reply(dev);
addr += (uint32_t)read;
dev->parent->audio_tag++;
} while (read > 0);
if (total_bytes_sent != fwheader.size) {
FN_ERROR("upload_firmware: firmware image declared %d bytes, but file only contained %d bytes\n", fwheader.size, total_bytes_sent);
return -1;
}
bootcmd.tag = fn_le32(dev->parent->audio_tag);
bootcmd.bytes = fn_le32(0);
bootcmd.cmd = fn_le32(0x04);
bootcmd.addr = fn_le32(fwheader.entry_addr);
dump_bl_cmd(ctx, bootcmd);
res = fnusb_bulk(dev, 1, (unsigned char*)&bootcmd, sizeof(bootcmd), &transferred);
if(res != 0 || transferred != sizeof(bootcmd)) {
FN_ERROR("upload_firmware(): Error: res: %d\ttransferred: %d (expected %d)\n", res, transferred, (int)sizeof(bootcmd));
return -1;
}
res = get_reply(dev);
dev->parent->audio_tag++;
FN_INFO("Firmware successfully uploaded and launched. Device will disconnect and reenumerate.\n");
return 0;
}
FN_INTERNAL int upload_cemd_data(fnusb_dev* dev) {
// Now we upload the CEMD data.
freenect_context* ctx = dev->parent->parent;
cemdloader_command cemdcmd;
memset(&cemdcmd, 0, sizeof(cemdcmd));
cemdcmd.magic = fn_le32(0x06022009);
cemdcmd.tag = fn_le32(dev->parent->audio_tag);
cemdcmd.arg1 = fn_le32(0);
cemdcmd.cmd = fn_le32(0x00000133);
cemdcmd.arg2 = fn_le32(0x00064014); // This is the length of the CEMD data.
FN_INFO("Starting CEMD data upload:\n");
int res;
int transferred;
res = fnusb_bulk(dev, 1, (unsigned char*)&cemdcmd, sizeof(cemdcmd), &transferred);
if(res != 0 || transferred != sizeof(cemdcmd)) {
FN_ERROR("Error: res: %d\ttransferred: %d (expected %d)\n", res, transferred, (int)sizeof(cemdcmd));
return -1;
}
res = get_reply(dev);
dev->parent->audio_tag++;
const char* cemd_filename = "cemd_data.bin";
FILE* cf = fopen(cemd_filename, "r");
if(cf == NULL) {
FN_ERROR("upload_cemd_data: Failed to open %s: error %d", cemd_filename, errno);
return errno;
}
uint32_t addr = 0x00000000;
int read = 0;
unsigned char page[0x4000];
do {
read = fread(page, 1, 0x4000, cf);
if(read <= 0) {
break;
}
//LOG("");
cemdcmd.tag = fn_le32(dev->parent->audio_tag);
cemdcmd.arg1 = fn_le32(read);
cemdcmd.cmd = fn_le32(0x134);
cemdcmd.arg2 = fn_le32(addr);
FN_INFO("About to send: ");
dump_cemd_cmd(ctx, cemdcmd);
// Send it off!
res = fnusb_bulk(dev, 1, (unsigned char*)&cemdcmd, sizeof(cemdcmd), &transferred);
if(res != 0 || transferred != sizeof(cemdcmd)) {
FN_ERROR("Error: res: %d\ttransferred: %d (expected %d)\n",res, transferred, (int)sizeof(cemdcmd));
return -1;
}
int bytes_sent = 0;
while(bytes_sent < read) {
int to_send = (read - bytes_sent > 512 ? 512 : read - bytes_sent);
res = fnusb_bulk(dev, 1, &page[bytes_sent], to_send, &transferred);
if(res != 0 || transferred != to_send) {
FN_ERROR("Error: res: %d\ttransferred: %d (expected %d)\n",res, transferred, to_send);
return -1;
}
bytes_sent += to_send;
}
res = get_reply(dev);
addr += (uint32_t)read;
dev->parent->audio_tag++;
} while (read > 0);
fclose(cf);
cf = NULL;
cemdcmd.tag = fn_le32(dev->parent->audio_tag);
cemdcmd.arg1 = fn_le32(0); // bytes = 0
cemdcmd.cmd = fn_le32(0x135);
cemdcmd.arg2 = fn_le32(0x00064000); // mimicing the USB logs. This is the # of bytes of actual CEMD data after the 20-byte CEMD header.
FN_INFO("Finishing CEMD data upload...\n");
res = fnusb_bulk(dev, 1, (unsigned char*)&cemdcmd, sizeof(cemdcmd), &transferred);
if(res != 0 || transferred != sizeof(cemdcmd)) {
FN_ERROR("upload_cemd_data(): Error: res: %d\ttransferred: %d (expected %d)\n", res, transferred, (int)sizeof(cemdcmd));
return -1;
}
res = get_reply(dev);
dev->parent->audio_tag++;
FN_INFO("CEMD data uploaded successfully.\n");
return 0;
}