38 for(i = 0; i < 24; i++)
39 FN_INFO(
"%02X ", ((
unsigned char*)(&cmd))[i]);
45 for(i = 0; i < 24; i++)
46 FN_INFO(
"%02X ", ((
unsigned char*)(&cmd))[i]);
47 FN_INFO(
"(%d more zeros)\n", (
int)(
sizeof(cmd)-24));
52 unsigned char dump[512];
56 res =
fnusb_bulk(dev, 0x81, dump, 512, &transferred);
63 FN_ERROR(
"Error reading reply: invalid magic %08X\n",buffer.
magic);
71 FN_ERROR(
"Notice reading reply: last uint32_t was nonzero: %d\n", buffer.
status);
75 for(i = 0; i < transferred; ++i) {
76 FN_INFO(
"%02X ", ((
unsigned char*)(&buffer))[i]);
85 memset(&bootcmd, 0,
sizeof(bootcmd));
91 unsigned char buffer[512];
95 FN_INFO(
"check_version_string(): About to send: ");
99 res =
fnusb_bulk(dev, 1, (
unsigned char*)&bootcmd,
sizeof(bootcmd), &transferred);
100 if(res != 0 || transferred !=
sizeof(bootcmd)) {
101 FN_ERROR(
"Error: res: %d\ttransferred: %d (expected %d)\n",res, transferred, (
int)
sizeof(bootcmd));
106 res =
fnusb_bulk(dev, 0x81, buffer, 512, &transferred);
108 FN_ERROR(
"Error reading version string: %d\ttransferred: %d (expected %d)\n", res, transferred, 0x60);
111 FN_INFO(
"Read version string: ");
113 for(i = 0; i < transferred; ++i) {
137 char fw_filename[1024];
138 sprintf(fw_filename,
"/%s", filename);
140 int filenamelen = strlen(fw_filename);
145 for(i = 0, searchpathcount = 6; !fw && i < searchpathcount; i++) {
150 char* envpath = getenv(
"LIBFREENECT_FIRMWARE_PATH");
153 int pathlen = strlen(envpath);
154 fwfile = (
char *)malloc(pathlen + filenamelen + 1);
155 strcpy(fwfile, envpath);
156 strcat(fwfile, fw_filename);
162 fwfile = (
char *)malloc(2048);
164 sprintf(fwfile,
".%s", fw_filename);
168 char* home = getenv(
"HOME");
171 int homelen = strlen(home);
172 char* dotfolder =
"/.libfreenect";
173 int locallen = strlen(dotfolder);
174 fwfile = (
char*)malloc(homelen + locallen + filenamelen + 1);
175 strcpy(fwfile, home);
176 strcat(fwfile, dotfolder);
177 strcat(fwfile, fw_filename);
183 sprintf(fwfile,
"/usr/local/share/libfreenect%s", fw_filename);
188 fwfile = (
char *)malloc(2048);
190 sprintf(fwfile,
"/usr/share/libfreenect%s", fw_filename);
194 fwfile = (
char *)malloc(2048);
196 sprintf(fwfile,
"./../Resources%s", fw_filename);
200 FN_INFO(
"Trying to open %s as firmware...\n", fwfile);
201 fw = fopen(fwfile,
"rb");
207 FN_ERROR(
"upload_firmware: failed to find firmware file.\n");
212 fseek(fw , 0, SEEK_END);
213 int fw_num_bytes = ftell(fw);
216 if( fw_num_bytes <= 0 ){
217 FN_ERROR(
"upload_firmware: failed to find file with any data.\n");
221 unsigned char * fw_bytes = (
unsigned char *)malloc(fw_num_bytes);
222 int numRead = fread(fw_bytes, 1, fw_num_bytes, fw);
223 fw_num_bytes = numRead;
236 memset(&bootcmd, 0,
sizeof(bootcmd));
244 int bytesLeft = fw_size_in_btyes;
245 unsigned char * readPtr = &fw_from_mem[0];
248 FN_ERROR(
"upload_firmware: firmware image too small, has no header?\n");
263 FN_INFO(
"Found firmware image:\n");
272 unsigned char page[0x4000];
274 int total_bytes_sent = 0;
277 read = (0x4000 > fwheader.
size - total_bytes_sent) ? fwheader.
size - total_bytes_sent : 0x4000;
280 if( read > bytesLeft ){
287 memcpy(page, &readPtr[readIndex], read);
298 res =
fnusb_bulk(dev, 1, (
unsigned char*)&bootcmd,
sizeof(bootcmd), &transferred);
299 if(res != 0 || transferred !=
sizeof(bootcmd)) {
300 FN_ERROR(
"upload_firmware(): Error: res: %d\ttransferred: %d (expected %d)\n",res, transferred, (
int)(
sizeof(bootcmd)));
304 while(bytes_sent < read) {
305 int to_send = (read - bytes_sent > 512 ? 512 : read - bytes_sent);
306 res =
fnusb_bulk(dev, 1, &page[bytes_sent], to_send, &transferred);
307 if(res != 0 || transferred != to_send) {
308 FN_ERROR(
"upload_firmware(): Error: res: %d\ttransferred: %d (expected %d)\n",res, transferred, to_send);
311 bytes_sent += to_send;
312 total_bytes_sent += to_send;
319 if (total_bytes_sent != fwheader.
size) {
320 FN_ERROR(
"upload_firmware: firmware image declared %d bytes, but file only contained %d bytes\n", fwheader.
size, total_bytes_sent);
329 res =
fnusb_bulk(dev, 1, (
unsigned char*)&bootcmd,
sizeof(bootcmd), &transferred);
330 if(res != 0 || transferred !=
sizeof(bootcmd)) {
331 FN_ERROR(
"upload_firmware(): Error: res: %d\ttransferred: %d (expected %d)\n", res, transferred, (
int)
sizeof(bootcmd));
336 FN_INFO(
"Firmware successfully uploaded and launched. Device will disconnect and reenumerate.\n");
344 memset(&cemdcmd, 0,
sizeof(cemdcmd));
350 FN_INFO(
"Starting CEMD data upload:\n");
353 res =
fnusb_bulk(dev, 1, (
unsigned char*)&cemdcmd,
sizeof(cemdcmd), &transferred);
354 if(res != 0 || transferred !=
sizeof(cemdcmd)) {
355 FN_ERROR(
"Error: res: %d\ttransferred: %d (expected %d)\n", res, transferred, (
int)
sizeof(cemdcmd));
361 const char* cemd_filename =
"cemd_data.bin";
362 FILE* cf = fopen(cemd_filename,
"r");
364 FN_ERROR(
"upload_cemd_data: Failed to open %s: error %d", cemd_filename, errno);
369 unsigned char page[0x4000];
371 read = fread(page, 1, 0x4000, cf);
383 res =
fnusb_bulk(dev, 1, (
unsigned char*)&cemdcmd,
sizeof(cemdcmd), &transferred);
384 if(res != 0 || transferred !=
sizeof(cemdcmd)) {
385 FN_ERROR(
"Error: res: %d\ttransferred: %d (expected %d)\n",res, transferred, (
int)
sizeof(cemdcmd));
389 while(bytes_sent < read) {
390 int to_send = (read - bytes_sent > 512 ? 512 : read - bytes_sent);
391 res =
fnusb_bulk(dev, 1, &page[bytes_sent], to_send, &transferred);
392 if(res != 0 || transferred != to_send) {
393 FN_ERROR(
"Error: res: %d\ttransferred: %d (expected %d)\n",res, transferred, to_send);
396 bytes_sent += to_send;
409 FN_INFO(
"Finishing CEMD data upload...\n");
410 res =
fnusb_bulk(dev, 1, (
unsigned char*)&cemdcmd,
sizeof(cemdcmd), &transferred);
411 if(res != 0 || transferred !=
sizeof(cemdcmd)) {
412 FN_ERROR(
"upload_cemd_data(): Error: res: %d\ttransferred: %d (expected %d)\n", res, transferred, (
int)
sizeof(cemdcmd));
417 FN_INFO(
"CEMD data uploaded successfully.\n");
FN_INTERNAL int upload_cemd_data(fnusb_dev *dev)
FN_INTERNAL int fnusb_bulk(fnusb_dev *dev, uint8_t endpoint, uint8_t *data, int len, int *transferred)
freenect_context * parent
FN_INTERNAL int upload_firmware(fnusb_dev *dev, char *filename)
static void dump_cemd_cmd(freenect_context *ctx, cemdloader_command cmd)
static int get_reply(fnusb_dev *dev)
void dump(char type, uint32_t timestamp, void *data, int data_size)
FN_INTERNAL int upload_firmware_from_memory(fnusb_dev *dev, unsigned char *fw_from_mem, unsigned int fw_size_in_btyes)
static void dump_bl_cmd(freenect_context *ctx, bootloader_command cmd)
static int check_version_string(fnusb_dev *dev)
static freenect_context * ctx