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"
14 int eyefi_debug_level = 1;
16 int eyefi_printf(const char *fmt, ...)
22 r = vprintf(fmt, args);
28 static char *eyefi_file_name(enum eyefi_file file)
31 case REQC: return "reqc";
32 case REQM: return "reqm";
33 case RSPC: return "rspc";
34 case RSPM: return "rspm";
40 char *eyefi_file_on(enum eyefi_file file, char *mnt)
42 char *filename = eyefi_file_name(file);
43 char *full = malloc(PATHNAME_MAX);
48 sprintf(&full[0], "%s/EyeFi/%s", mnt, filename);
49 debug_printf(4, "eyefile nr: %d on '%s' is: '%s'\n", file, mnt, &full[0]);
54 * This lets us get away with a static allocation
55 * for the buffer. We make it size*2 so that we're
56 * guaranteed to be able to get a "size" buffer
57 * aligned inside of the larger one.
59 static char unaligned_buf[EYEFI_BUF_SIZE*2];
60 static void *eyefi_buf;
62 void *eyefi_response(void)
67 void dumpbuf(const char *buffer, int bytesToWrite)
70 static char linebuf[500];
72 for (i=0; i < bytesToWrite; i += 16) {
73 char *tmpbuf = &linebuf[0];
74 unsigned long sum = 0;
76 #define lprintf(args...) do { \
77 tmpbuf += sprintf(tmpbuf, ## args);\
80 lprintf("[%03d]: ", i);
81 for (j=0; j < 16; j++) {
82 u8 c = ((unsigned char *)buffer)[i+j];
83 lprintf("%02x ", (unsigned int)c);
87 for (j=0; j < 16; j++) {
88 u8 c = ((unsigned char *)buffer)[i+j];
89 if (c >= 'a' && c <= 'z')
91 else if (c >= 'A' && c <= 'Z')
93 else if (c >= '0' && c <= '9')
95 else if (c >= 0x20 && c <= 127)
103 printf("%s", linebuf);
109 void read_from(enum eyefi_file);
110 void write_to(enum eyefi_file, void *, int);
112 struct card_seq_num eyefi_seq;
113 struct card_seq_num read_seq_from(enum eyefi_file file)
115 struct card_seq_num *ret;
122 * For O_DIRECT writes to files, we need
123 * to be 512 byte aligned on Linux, I think.
124 * So, just align this to something big
125 * and be done with it. FIXME :)
127 * This probably isn't necessary on chdk,
128 * since I don't think it buffers I/O at
133 unsigned long addr = (unsigned long)&unaligned_buf[EYEFI_BUF_SIZE];
134 addr &= ~(EYEFI_BUF_SIZE-1);
135 eyefi_buf = (void *)addr;
136 debug_printf(4, "buf: %p\n", eyefi_buf);
137 debug_printf(4, "unaligned: %p\n", &unaligned_buf[0]);
141 * The real manager does this so we might
144 void zero_card_files(void)
146 char zbuf[EYEFI_BUF_SIZE];
148 memset(&zbuf[0], 0, EYEFI_BUF_SIZE);
149 //write_to(REQM, zbuf, EYEFI_BUF_SIZE);
150 write_to(REQC, zbuf, EYEFI_BUF_SIZE);
151 write_to(RSPM, zbuf, EYEFI_BUF_SIZE);
152 write_to(RSPC, zbuf, EYEFI_BUF_SIZE);
163 if (eyefi_buf != NULL)
166 debug_printf(2, "Initializing card...\n");
167 mnt = locate_eyefi_mount();
173 eyefi_seq = read_seq_from(RSPC);
174 if (eyefi_seq.seq == 0)
175 eyefi_seq.seq = 0x1234;
177 debug_printf(2, "Done initializing card...\n");
178 debug_printf(2, "seq was: %04x\n", eyefi_seq.seq);
181 static char *eyefi_file(enum eyefi_file file)
184 return eyefi_file_on(file, locate_eyefi_mount());
187 void read_from(enum eyefi_file __file)
191 char *file = eyefi_file(__file);
196 fd = open(file, O_RDONLY);
198 open_error(file, fd);
201 void * map = mmap(0, EYEFI_BUF_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
202 if (ret = msync(map, EYEFI_BUF_SIZE, MS_INVALIDATE))
203 debug_printf(0, "msync error %u", ret);
205 ret = memcpy(eyefi_buf, map, EYEFI_BUF_SIZE);
207 if (ret = msync(map, EYEFI_BUF_SIZE, MS_INVALIDATE))
208 debug_printf(0, "msync error %u", ret);
209 munmap(map, EYEFI_BUF_SIZE);
211 if (eyefi_debug_level > 3)
212 dumpbuf(eyefi_buf, 128);
215 perror("bad read, retrying...");
219 debug_printf(4, "read '%s': bytes: %d\n", file, EYEFI_BUF_SIZE);
221 * There was a time when I was carefully recording how each response
222 * looked, and I counted the zeros in each response. I don't care
227 for (i=0; i < EYEFI_BUF_SIZE; i++) {
228 c = ((char *)eyefi_buf)[i];
240 void write_to(enum eyefi_file __file, void *stuff, int len)
251 file = eyefi_file(__file);
255 if (eyefi_debug_level > 3) {
256 debug_printf(3, "%s('%s', ..., %d)\n", __func__, file, len);
259 memset(eyefi_buf, 0, EYEFI_BUF_SIZE);
260 memcpy(eyefi_buf, stuff, len);
261 fd = open(file, O_RDWR|O_CREAT, 0600);
263 open_error(file, fd);
264 if (eyefi_debug_level > 3)
265 dumpbuf(eyefi_buf, 128);
266 wrote = write(fd, eyefi_buf, EYEFI_BUF_SIZE);
268 open_error(file, wrote);
271 open_error(file, ret);
273 debug_printf(3, "wrote %d bytes to '%s' (string was %d bytes)\n", wrote, file, len);
275 fprintf(stderr, "error writing to '%s': ", file);
282 #define write_struct(file, s) write_to((file), s, sizeof(*(s)))
287 * Oddly enough, the sequence number appears
288 * to be of normal endianness.
290 //u32 tmpseq = be32_to_u32(seq.seq);
291 //seq.seq = u32_to_be32(tmpseq+1);
293 write_struct(REQC, &eyefi_seq);
296 u32 eyefi_current_seq(void)
298 return eyefi_seq.seq;
301 int wait_for_response(void)
306 debug_printf(3, "waiting for response...\n");
308 for (i = 0; i < 50; i++) {
309 struct card_seq_num cardseq = read_seq_from(RSPC);
310 debug_printf(3, "read rsp code: %x, looking for: %x raw: %x\n", rsp, eyefi_current_seq(),
313 if (rsp == eyefi_current_seq()) {
317 if (eyefi_debug_level > 4) {
319 debug_printf(1, "command issued was: '%c'\n", ((char *)eyefi_buf)[0]);
324 debug_printf(1, "never saw card seq response\n");
327 debug_printf(3, "got good seq (%d), reading RSPM...\n", rsp);
329 debug_printf(3, "done reading RSPM\n");
333 char *net_test_states[] = {
336 "verifying network key",
338 "testing connection to Eye-Fi server",
342 char *net_test_state_name(u8 state)
344 int size = ARRAY_SIZE(net_test_states);
347 return net_test_states[state];
350 const char *net_types[] = {
357 const char net_type_unknown[] = "unknown";
359 const char *net_type_name(u8 type)
361 int size = ARRAY_SIZE(net_types);
362 debug_printf(3, "%s(%d): '%s' size: %d\n", __func__, type, net_types[type], size);
364 return net_type_unknown;
365 return net_types[type];
368 static char lower(char c)
370 if ((c >= 'A') && (c <= 'Z'))
378 if ((c >= '0') && (c <= '9'))
380 else if ((lc >= 'a') && (lc <= 'z'))
381 return (lc - 'a') + 10;
382 debug_printf(5, "non-hex character: '%c'/'%c'\n", c, lc);
387 * Take a string like "0ab1" and make it
388 * a series of bytes: { 0x0a, 0xb1 }
390 * @len is the strlen() of the ascii
392 * Destroys the original string.
394 char *convert_ascii_to_hex(char *ascii, int len)
398 fprintf(stderr, "%s() must be even number of bytes: %d\n",
402 for (i=0; i < len; i+=2) {
403 int high = atoh(ascii[i]);
404 int low = atoh(ascii[i+1]);
405 u8 byte = (high<<4 | low);
406 if (high < 0 || low < 0) {
407 fprintf(stderr, "unable to parse hex string: '%s'\n", ascii);
410 debug_printf(6, "high: %02x low: %02x, both: %02x\n", high, low, byte);
413 for (i=len/2; i < len; i++)
418 int make_network_key(struct network_key *key, char *essid, char *pass)
420 char tmp[WPA_KEY_BYTES+WEP_KEY_BYTES];
421 int pass_len = strlen(pass);
423 memset(key, 0, sizeof(*key));
425 strcpy(&tmp[0], pass);
426 eyefi_printf(" interpreting passphrase as ");
428 case WPA_KEY_BYTES*2:
429 eyefi_printf("hex WPA");
430 hex_pass = convert_ascii_to_hex(tmp, pass_len);
433 key->len = pass_len/2;
434 memcpy(&key->wpa.key[0], hex_pass, key->len);
436 case WEP_KEY_BYTES*2:
437 case WEP_40_KEY_BYTES*2:
438 eyefi_printf("hex WEP");
439 hex_pass = convert_ascii_to_hex(tmp, strlen(pass));
442 key->len = pass_len/2;
443 memcpy(&key->wep.key[0], hex_pass, key->len);
446 eyefi_printf("ASCII WPA");
447 pbkdf2_sha1(pass, essid, strlen(essid), 4096,
448 &key->wpa.key[0], WPA_KEY_BYTES);
449 key->len = WPA_KEY_BYTES;
452 eyefi_printf(" key (%d bytes)\n", key->len);
453 assert(key->len != 0);
457 int card_info_cmd(enum card_info_subcommand cmd)
459 struct card_info_req cir;
461 cir.subcommand = cmd;
463 write_struct(REQM, &cir);
464 return wait_for_response();
467 u32 fetch_log_length(void)
469 debug_printf(3, "%s()\n", __func__);
470 card_info_cmd(LOG_LEN);
471 struct card_info_log_len *loglen = eyefi_buf;
472 return be32_to_u32(loglen->val);
475 struct card_firmware_info *fetch_card_firmware_info(void)
477 debug_printf(2, "%s()\n", __func__);
478 card_info_cmd(FIRMWARE_INFO);
479 return (struct card_firmware_info *)eyefi_buf;
483 void wlan_disable(int do_disable)
486 * This is complete voodoo to me. I've only ever seen
487 * a single example of this, so it's hard to figure out
488 * the structure at all.
490 char new_cmd[] = {'O', 0x0a, do_disable};
491 write_to(REQM, &new_cmd[0], 3);
495 int wlan_enabled(void)
497 struct var_byte_response *rsp;
498 card_info_cmd(WLAN_ENABLED);
500 return rsp->responses[0].response;
516 void scan_print_nets(void);
525 printf("WLAN enabled: %d\n", wlan_enabled());
527 printf("WLAN enabled: %d\n", wlan_enabled());
529 printf("WLAN enabled: %d\n", wlan_enabled());
531 for (i = 10; i <= 13; i++) {
534 printf("UNKNOWN %d result:\n", i);
535 dumpbuf(eyefi_buf, 64);
536 printf("WLAN enabled: %d\n", wlan_enabled());
541 printf("UNKNOWN %d result:\n", i);
542 dumpbuf(eyefi_buf, 64);
544 printf("WLAN enabled: %d\n", wlan_enabled());
546 printf("WLAN enabled: %d\n", wlan_enabled());
547 for (i = 10; i <= 13; i++) {
550 printf("UNKNOWN %d result:\n", i);
551 dumpbuf(eyefi_buf, 64);
552 printf("WLAN enabled: %d\n", wlan_enabled());
556 printf("UNKNOWN %d result:\n", i);
557 dumpbuf(eyefi_buf, 64);
561 printf("o3 result:\n");
562 dumpbuf(eyefi_buf, 64);
564 memset(&zbuf[0], 0, EYEFI_BUF_SIZE);
568 write_to(REQM, &zbuf[0], 16384);
569 printf("o2 written\n");
570 printf("seq: %x\n", (int)eyefi_seq.seq);
573 for (i=0; i < 4; i++) {
575 printf("RSPC %d:\n", i);
576 dumpbuf(eyefi_buf, 64);
582 dumpbuf(eyefi_buf, 64);
584 memset(&zbuf[0], 0, EYEFI_BUF_SIZE);
585 write_to(RSPM, zbuf, EYEFI_BUF_SIZE);
586 write_to(REQM, zbuf, EYEFI_BUF_SIZE);
588 fdin = open("/home/dave/projects/eyefi/EYEFIFWU.BIN.2.0001", O_RDONLY);
590 fdout = open("/media/EYE-FI/EYEFIFWU.BIN", O_WRONLY|O_CREAT);
592 if (fdin <= 0 || fdout <= 0)
595 i = read(fdin, &fwbuf[0], 524288);
599 i = write(fdout, &fwbuf[0], 524288);
607 dumpbuf(eyefi_buf, 64);
610 printf("after reboot:\n");
611 dumpbuf(eyefi_buf, 64);
615 dumpbuf(eyefi_buf, 64);
619 dumpbuf(eyefi_buf, 64);
621 memset(&zbuf[0], 0, EYEFI_BUF_SIZE);
622 write_to(RSPM, zbuf, EYEFI_BUF_SIZE);
623 write_to(REQM, zbuf, EYEFI_BUF_SIZE);
627 dumpbuf(eyefi_buf, 64);
630 strcpy(tb.name, "www.sr71.net/");
631 tb.l1 = strlen(tb.name);
632 for (i = 0; i < 10; i++) {
635 write_struct(RSPM, &z);
636 write_struct(REQM, &tb);
638 printf("buffer after O %d:\n", i);
639 dumpbuf(eyefi_buf, 64);
640 printf("----------------\n");
641 write_struct(REQM, &tb);
643 printf("card info(%d):\n", i);
644 dumpbuf(eyefi_buf, 64);
645 printf("-----------\n");
649 strcpy(tb.name, "/public/eyefi/servname");
650 strcpy(tb.name, "/config/networks.xml");
651 //tb.len = strlen(tb.name);
653 for (c = 'O'; c <= 'O'; c++) {
655 write_struct(REQM, &tb);
657 printf("dumping buffer:\n");
658 dumpbuf(eyefi_buf, 64);
659 printf("buffer dump done\n");
663 struct card_info_rsp_key *fetch_card_key(void)
665 struct card_info_rsp_key *key;
667 debug_printf(2, "%s()\n", __func__);
668 card_info_cmd(CARD_KEY);
673 int issue_noarg_command(u8 cmd)
675 struct noarg_request req;
676 debug_printf(4, "%s() cmd: %d\n", __func__, cmd);
678 write_struct(REQM, &req);
679 return wait_for_response();
682 struct scanned_net_list *scan_nets(void)
684 struct scanned_net_list *scanned;
686 debug_printf(2, "%s()\n", __func__);
687 issue_noarg_command('g');
688 scanned = eyefi_response();
692 struct configured_net_list *fetch_configured_nets(void)
694 struct configured_net_list *configured;
696 debug_printf(2, "%s()\n", __func__);
697 issue_noarg_command('l');
698 configured = eyefi_buf;
702 void reboot_card(void)
704 debug_printf(2, "%s()\n", __func__);
705 debug_printf(1, "rebooting card...");
706 issue_noarg_command('b');
707 debug_printf(1, "done\n");
710 int network_action(char cmd, char *essid, char *ascii_password)
712 struct net_request nr;
713 memset(&nr, 0, sizeof(nr));
716 strcpy(&nr.essid[0], essid);
717 nr.essid_len = strlen(essid);
719 if (ascii_password) {
720 int ret = make_network_key(&nr.key, essid, ascii_password);
724 write_struct(REQM, &nr);
725 return wait_for_response();
728 void add_network(char *essid, char *ascii_password)
730 debug_printf(2, "%s('%s', '%s')\n", __func__, essid, ascii_password);
731 network_action('a', essid, ascii_password);
734 void remove_network(char *essid)
736 debug_printf(2, "%s()\n", __func__);
737 network_action('d', essid, NULL);
740 int get_log_at_offset(u32 offset)
742 struct fetch_log_cmd cmd;
744 cmd.offset = u32_to_be32(offset);
746 debug_printf(2, "getting log at offset: %08x\n", offset);
747 write_struct(REQM, &cmd);
748 return wait_for_response();
751 void add_log_piece(u8 *log, int log_len, u8 *piece, int piece_pos, int piece_size)
753 debug_printf(2, "%s(%p, %d, %p, %d, %d)\n", __func__, log, log_len, piece, piece_pos, piece_size);
754 if (piece_pos + piece_size > log_len) {
755 int overflow_by = (piece_pos + piece_size) - log_len;
756 int piece_overrun_pos = piece_size - overflow_by;
757 piece_size -= overflow_by;
758 memcpy(&log[0], &piece[piece_overrun_pos], overflow_by);
759 debug_printf(2, "writing %d bytes to logbuf[0] from piece[%d]\n",
760 overflow_by, piece_overrun_pos);
762 debug_printf(2, "writing %d bytes to logbuf[%d]\n", piece_size, piece_pos);
763 memcpy(&log[piece_pos], piece, piece_size);
766 int get_log_into(u8 *resbuf)
773 u32 __log_size = fetch_log_length();
774 int log_pieces = __log_size/EYEFI_BUF_SIZE;
779 /* There are 8 bytes of header in the first log entry
780 * to specify where the log starts and ends */
781 u32 log_size = __log_size - 8;
783 for (i = 0; i < log_pieces; i++) {
784 debug_printf(1, "fetching EyeFi card log part %d/%d...",
787 ret = get_log_at_offset(EYEFI_BUF_SIZE*i);
788 debug_printf(1, "done\n");
792 struct first_log_response *log = eyefi_buf;
793 log_end = log_size - be32_to_u32(log->log_end);
794 log_start = log_size - be32_to_u32(log->log_start);
795 debug_printf(2, "log end: 0x%04x\n", log_end);
796 debug_printf(2, "log start: 0x%04x\n", log_start);
797 log_piece = &log->data[0];
798 log_piece_size = ARRAY_SIZE(log->data);
800 struct rest_log_response *log = eyefi_buf;
801 log_piece = &log->data[0];
802 log_piece_size = ARRAY_SIZE(log->data);
804 add_log_piece(resbuf, log_size, log_piece, log_start, log_piece_size);
805 total_bytes += log_piece_size;
806 log_start += log_piece_size;
807 log_start = log_start % log_size;