4 * Copyright (C) 2008 Dave Hansen <dave@sr71.net>
6 * This software may be redistributed and/or modified under the terms of
7 * the GNU General Public License ("GPL") version 2 as published by the
8 * Free Software Foundation.
11 #include "eyefi-config.h"
13 int eyefi_debug_level = 1;
15 int eyefi_printf(const char *fmt, ...)
21 r = vprintf(fmt, args);
27 static char *eyefi_file_name(enum eyefi_file file)
30 case REQC: return "reqc";
31 case REQM: return "reqm";
32 case RSPC: return "rspc";
33 case RSPM: return "rspm";
39 char *eyefi_file_on(enum eyefi_file file, char *mnt)
41 char *filename = eyefi_file_name(file);
42 char *full = malloc(PATHNAME_MAX);
47 sprintf(&full[0], "%s/EyeFi/%s", mnt, filename);
48 debug_printf(4, "eyefile nr: %d on '%s' is: '%s'\n", file, mnt, &full[0]);
53 * This lets us get away with a static allocation
54 * for the buffer. We make it size*2 so that we're
55 * guaranteed to be able to get a "size" buffer
56 * aligned inside of the larger one.
58 static char unaligned_buf[EYEFI_BUF_SIZE*2];
59 static void *eyefi_buf;
61 void *eyefi_response(void)
66 void dumpbuf(const char *buffer, int bytesToWrite)
69 static char linebuf[500];
71 for (i=0; i < bytesToWrite; i += 16) {
72 char *tmpbuf = &linebuf[0];
73 unsigned long sum = 0;
75 #define lprintf(args...) do { \
76 tmpbuf += sprintf(tmpbuf, ## args);\
79 lprintf("[%03d]: ", i);
80 for (j=0; j < 16; j++) {
81 u8 c = ((unsigned char *)buffer)[i+j];
82 lprintf("%02x ", (unsigned int)c);
86 for (j=0; j < 16; j++) {
87 u8 c = ((unsigned char *)buffer)[i+j];
88 if (c >= 'a' && c <= 'z')
90 else if (c >= 'A' && c <= 'Z')
92 else if (c >= '0' && c <= '9')
94 else if (c >= 0x20 && c <= 127)
102 printf("%s", linebuf);
108 void read_from(enum eyefi_file);
109 void write_to(enum eyefi_file, void *, int);
111 struct card_seq_num eyefi_seq;
112 struct card_seq_num read_seq_from(enum eyefi_file file)
114 struct card_seq_num *ret;
121 * For O_DIRECT writes to files, we need
122 * to be 512 byte aligned on Linux, I think.
123 * So, just align this to something big
124 * and be done with it. FIXME :)
126 * This probably isn't necessary on chdk,
127 * since I don't think it buffers I/O at
132 unsigned long addr = (unsigned long)&unaligned_buf[EYEFI_BUF_SIZE];
133 addr &= ~(EYEFI_BUF_SIZE-1);
134 eyefi_buf = (void *)addr;
135 debug_printf(4, "buf: %p\n", eyefi_buf);
136 debug_printf(4, "unaligned: %p\n", &unaligned_buf[0]);
140 * The real manager does this so we might
143 void zero_card_files(void)
145 char zbuf[EYEFI_BUF_SIZE];
147 memset(&zbuf[0], 0, EYEFI_BUF_SIZE);
148 //write_to(REQM, zbuf, EYEFI_BUF_SIZE);
149 write_to(REQC, zbuf, EYEFI_BUF_SIZE);
150 write_to(RSPM, zbuf, EYEFI_BUF_SIZE);
151 write_to(RSPC, zbuf, EYEFI_BUF_SIZE);
162 if (eyefi_buf != NULL)
165 debug_printf(2, "Initializing card...\n");
166 mnt = locate_eyefi_mount();
172 eyefi_seq = read_seq_from(RSPC);
173 if (eyefi_seq.seq == 0)
174 eyefi_seq.seq = 0x1234;
176 debug_printf(2, "Done initializing card...\n");
177 debug_printf(2, "seq was: %04lx\n", eyefi_seq.seq);
180 static char *eyefi_file(enum eyefi_file file)
183 return eyefi_file_on(file, locate_eyefi_mount());
186 void read_from(enum eyefi_file __file)
190 char *file = eyefi_file(__file);
195 fd = open(file, O_RDONLY);
197 open_error(file, fd);
199 ret = read(fd, eyefi_buf, EYEFI_BUF_SIZE);
200 if (eyefi_debug_level > 3)
201 dumpbuf(eyefi_buf, 128);
204 perror("bad read, retrying...");
208 debug_printf(4, "read '%s': bytes: %d\n", file, ret);
210 * There was a time when I was carefully recording how each response
211 * looked, and I counted the zeros in each response. I don't care
216 for (i=0; i < EYEFI_BUF_SIZE; i++) {
217 c = ((char *)eyefi_buf)[i];
229 void write_to(enum eyefi_file __file, void *stuff, int len)
239 file = eyefi_file(__file);
243 if (eyefi_debug_level > 3) {
244 debug_printf(3, "%s('%s', ..., %d)\n", __func__, file, len);
247 memset(eyefi_buf, 0, EYEFI_BUF_SIZE);
248 memcpy(eyefi_buf, stuff, len);
249 fd = open(file, O_RDWR|O_CREAT, 0600);
251 open_error(file, fd);
252 if (eyefi_debug_level > 3)
253 dumpbuf(eyefi_buf, 128);
254 ret = write(fd, eyefi_buf, EYEFI_BUF_SIZE);
256 open_error(file, ret);
259 open_error(file, ret);
261 debug_printf(3, "wrote %d bytes to '%s' (string was %d bytes)\n", ret, file, len);
263 fprintf(stderr, "error writing to '%s': ", file);
270 #define write_struct(file, s) write_to((file), s, sizeof(*(s)))
275 * Oddly enough, the sequence number appears
276 * to be of normal endianness.
278 //u32 tmpseq = be32_to_u32(seq.seq);
279 //seq.seq = u32_to_be32(tmpseq+1);
281 write_struct(REQC, &eyefi_seq);
284 u32 eyefi_current_seq(void)
286 return eyefi_seq.seq;
289 int wait_for_response(void)
294 debug_printf(3, "waiting for response...\n");
296 for (i = 0; i < 50; i++) {
297 struct card_seq_num cardseq = read_seq_from(RSPC);
298 debug_printf(3, "read rsp code: %lx, looking for: %lx raw: %lx\n", rsp, eyefi_current_seq(),
301 if (rsp == eyefi_current_seq()) {
305 if (eyefi_debug_level > 4) {
307 debug_printf(1, "command issued was: '%c'\n", ((char *)eyefi_buf)[0]);
312 debug_printf(1, "never saw card seq response\n");
315 debug_printf(3, "got good seq (%ld), reading RSPM...\n", rsp);
317 debug_printf(3, "done reading RSPM\n");
321 char *net_test_states[] = {
324 "verifying network key",
326 "testing connection to Eye-Fi server",
330 char *net_test_state_name(u8 state)
332 int size = ARRAY_SIZE(net_test_states);
335 return net_test_states[state];
338 const char *net_types[] = {
345 const char net_type_unknown[] = "unknown";
347 const char *net_type_name(u8 type)
349 int size = ARRAY_SIZE(net_types);
350 debug_printf(3, "%s(%d): '%s' size: %d\n", __func__, type, net_types[type], size);
352 return net_type_unknown;
353 return net_types[type];
356 static char lower(char c)
358 if ((c >= 'A') && (c <= 'Z'))
366 if ((c >= '0') && (c <= '9'))
368 else if ((lc >= 'a') && (lc <= 'z'))
369 return (lc - 'a') + 10;
370 debug_printf(5, "non-hex character: '%c'/'%c'\n", c, lc);
375 * Take a string like "0ab1" and make it
376 * a series of bytes: { 0x0a, 0xb1 }
378 * @len is the strlen() of the ascii
380 * Destroys the original string.
382 char *convert_ascii_to_hex(char *ascii, int len)
386 fprintf(stderr, "%s() must be even number of bytes: %d\n",
390 for (i=0; i < len; i+=2) {
391 int high = atoh(ascii[i]);
392 int low = atoh(ascii[i+1]);
393 u8 byte = (high<<4 | low);
394 if (high < 0 || low < 0) {
395 fprintf(stderr, "unable to parse hex string: '%s'\n", ascii);
398 debug_printf(6, "high: %02x low: %02x, both: %02x\n", high, low, byte);
401 for (i=len/2; i < len; i++)
406 int make_network_key(struct network_key *key, char *essid, char *pass)
408 char tmp[WPA_KEY_BYTES+WEP_KEY_BYTES];
409 int pass_len = strlen(pass);
411 memset(key, 0, sizeof(*key));
413 strcpy(&tmp[0], pass);
414 eyefi_printf(" interpreting passphrase as ");
416 case WPA_KEY_BYTES*2:
417 eyefi_printf("hex WPA");
418 hex_pass = convert_ascii_to_hex(tmp, pass_len);
421 key->len = pass_len/2;
422 memcpy(&key->wpa.key[0], hex_pass, key->len);
424 case WEP_KEY_BYTES*2:
425 eyefi_printf("hex WEP");
426 hex_pass = convert_ascii_to_hex(tmp, strlen(pass));
429 key->len = pass_len/2;
430 memcpy(&key->wep.key[0], hex_pass, key->len);
433 eyefi_printf("ASCII WPA");
434 pbkdf2_sha1(pass, essid, strlen(essid), 4096,
435 &key->wpa.key[0], WPA_KEY_BYTES);
436 key->len = WPA_KEY_BYTES;
439 eyefi_printf(" key (%d bytes)\n", key->len);
440 assert(key->len != 0);
444 int card_info_cmd(enum card_info_subcommand cmd)
446 struct card_info_req cir;
448 cir.subcommand = cmd;
450 write_struct(REQM, &cir);
451 return wait_for_response();
454 u32 fetch_log_length(void)
456 debug_printf(3, "%s()\n", __func__);
457 card_info_cmd(LOG_LEN);
458 struct card_info_log_len *loglen = eyefi_buf;
459 return be32_to_u32(loglen->val);
462 struct card_firmware_info *fetch_card_firmware_info(void)
464 debug_printf(2, "%s()\n", __func__);
465 card_info_cmd(FIRMWARE_INFO);
466 return (struct card_firmware_info *)eyefi_buf;
492 printf("o3 result:\n");
493 dumpbuf(eyefi_buf, 64);
495 memset(&zbuf[0], 0, EYEFI_BUF_SIZE);
499 write_to(REQM, &zbuf[0], 16384);
500 printf("o2 written\n");
501 printf("seq: %x\n", (int)eyefi_seq.seq);
504 for (i=0; i < 4; i++) {
506 printf("RSPC %d:\n", i);
507 dumpbuf(eyefi_buf, 64);
513 dumpbuf(eyefi_buf, 64);
515 memset(&zbuf[0], 0, EYEFI_BUF_SIZE);
516 write_to(RSPM, zbuf, EYEFI_BUF_SIZE);
517 write_to(REQM, zbuf, EYEFI_BUF_SIZE);
519 fdin = open("/home/dave/projects/eyefi/EYEFIFWU.BIN.2.0001", O_RDONLY);
521 fdout = open("/media/EYE-FI/EYEFIFWU.BIN", O_WRONLY|O_CREAT);
523 if (fdin <= 0 || fdout <= 0)
526 i = read(fdin, &fwbuf[0], 524288);
530 i = write(fdout, &fwbuf[0], 524288);
538 dumpbuf(eyefi_buf, 64);
541 printf("after reboot:\n");
542 dumpbuf(eyefi_buf, 64);
546 dumpbuf(eyefi_buf, 64);
550 dumpbuf(eyefi_buf, 64);
552 memset(&zbuf[0], 0, EYEFI_BUF_SIZE);
553 write_to(RSPM, zbuf, EYEFI_BUF_SIZE);
554 write_to(REQM, zbuf, EYEFI_BUF_SIZE);
558 dumpbuf(eyefi_buf, 64);
561 strcpy(tb.name, "www.sr71.net/");
562 tb.l1 = strlen(tb.name);
563 for (i = 0; i < 10; i++) {
566 write_struct(RSPM, &z);
567 write_struct(REQM, &tb);
569 printf("buffer after O %d:\n", i);
570 dumpbuf(eyefi_buf, 64);
571 printf("----------------\n");
572 write_struct(REQM, &tb);
574 printf("card info(%d):\n", i);
575 dumpbuf(eyefi_buf, 64);
576 printf("-----------\n");
580 strcpy(tb.name, "/public/eyefi/servname");
581 strcpy(tb.name, "/config/networks.xml");
582 //tb.len = strlen(tb.name);
584 for (c = 'O'; c <= 'O'; c++) {
586 write_struct(REQM, &tb);
588 printf("dumping buffer:\n");
589 dumpbuf(eyefi_buf, 64);
590 printf("buffer dump done\n");
594 struct card_info_rsp_key *fetch_card_key(void)
596 struct card_info_rsp_key *key;
598 debug_printf(2, "%s()\n", __func__);
599 card_info_cmd(CARD_KEY);
604 int issue_noarg_command(u8 cmd)
606 struct noarg_request req;
607 debug_printf(4, "%s() cmd: %d\n", __func__, cmd);
609 write_struct(REQM, &req);
610 return wait_for_response();
613 struct scanned_net_list *scan_nets(void)
615 struct scanned_net_list *scanned;
617 debug_printf(2, "%s()\n", __func__);
618 issue_noarg_command('g');
619 scanned = eyefi_response();
623 struct configured_net_list *fetch_configured_nets(void)
625 struct configured_net_list *configured;
627 debug_printf(2, "%s()\n", __func__);
628 issue_noarg_command('l');
629 configured = eyefi_buf;
633 void reboot_card(void)
635 debug_printf(2, "%s()\n", __func__);
636 issue_noarg_command('b');
639 int network_action(char cmd, char *essid, char *ascii_password)
641 struct net_request nr;
642 memset(&nr, 0, sizeof(nr));
645 strcpy(&nr.essid[0], essid);
646 nr.essid_len = strlen(essid);
648 if (ascii_password) {
649 int ret = make_network_key(&nr.key, essid, ascii_password);
653 write_struct(REQM, &nr);
654 return wait_for_response();
657 void add_network(char *essid, char *ascii_password)
659 debug_printf(2, "%s('%s', '%s')\n", __func__, essid, ascii_password);
660 network_action('a', essid, ascii_password);
663 void remove_network(char *essid)
665 debug_printf(2, "%s()\n", __func__);
666 network_action('d', essid, NULL);
669 int get_log_at_offset(u32 offset)
671 struct fetch_log_cmd cmd;
673 cmd.offset = u32_to_be32(offset);
675 debug_printf(2, "getting log at offset: %08lx\n", offset);
676 write_struct(REQM, &cmd);
677 return wait_for_response();
680 void add_log_piece(u8 *log, int log_len, u8 *piece, int piece_pos, int piece_size)
682 debug_printf(2, "%s(%p, %d, %p, %d, %d)\n", __func__, log, log_len, piece, piece_pos, piece_size);
683 if (piece_pos + piece_size > log_len) {
684 int overflow_by = (piece_pos + piece_size) - log_len;
685 int piece_overrun_pos = piece_size - overflow_by;
686 piece_size -= overflow_by;
687 memcpy(&log[0], &piece[piece_overrun_pos], overflow_by);
688 debug_printf(2, "writing %d bytes to logbuf[0] from piece[%d]\n",
689 overflow_by, piece_overrun_pos);
691 debug_printf(2, "writing %d bytes to logbuf[%d]\n", piece_size, piece_pos);
692 memcpy(&log[piece_pos], piece, piece_size);
695 int get_log_into(u8 *resbuf)
702 u32 __log_size = fetch_log_length();
703 int log_pieces = __log_size/EYEFI_BUF_SIZE;
708 /* There are 8 bytes of header in the first log entry
709 * to specify where the log starts and ends */
710 u32 log_size = __log_size - 8;
712 for (i = 0; i < log_pieces; i++) {
713 debug_printf(1, "fetching EyeFi card log part %d/%d...",
716 ret = get_log_at_offset(EYEFI_BUF_SIZE*i);
717 debug_printf(1, "done\n");
721 struct first_log_response *log = eyefi_buf;
722 log_end = log_size - be32_to_u32(log->log_end);
723 log_start = log_size - be32_to_u32(log->log_start);
724 debug_printf(2, "log end: 0x%04lx\n", log_end);
725 debug_printf(2, "log start: 0x%04lx\n", log_start);
726 log_piece = &log->data[0];
727 log_piece_size = ARRAY_SIZE(log->data);
729 struct rest_log_response *log = eyefi_buf;
730 log_piece = &log->data[0];
731 log_piece_size = ARRAY_SIZE(log->data);
733 add_log_piece(resbuf, log_size, log_piece, log_start, log_piece_size);
734 total_bytes += log_piece_size;
735 log_start += log_piece_size;
736 log_start = log_start % log_size;