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 = 0;
15 int eyefi_printf(const char *fmt, ...)
21 r = printf(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);
44 sprintf(&full[0], "%s/EyeFi/%s", mnt, filename);
45 debug_printf(4, "eyefile nr: %d on '%s' is: '%s'\n", file, mnt, &full[0]);
50 * This lets us get away with a static allocation
51 * for the buffer. We make it size*2 so that we're
52 * guaranteed to be able to get a "size" buffer
53 * aligned inside of the larger one.
55 static char unaligned_buf[EYEFI_BUF_SIZE*2];
56 static void *eyefi_buf;
58 void *eyefi_response(void)
63 void dumpbuf(const char *buffer, int bytesToWrite)
66 static char linebuf[500];
68 for (i=0; i < bytesToWrite; i += 16) {
69 char *tmpbuf = &linebuf[0];
70 unsigned long sum = 0;
72 #define lprintf(args...) do { \
73 tmpbuf += sprintf(tmpbuf, ## args);\
76 lprintf("[%03d]: ", i);
77 for (j=0; j < 16; j++) {
78 u8 c = ((unsigned char *)buffer)[i+j];
79 lprintf("%02x ", (unsigned int)c);
83 for (j=0; j < 16; j++) {
84 u8 c = ((unsigned char *)buffer)[i+j];
85 if (c >= 'a' && c <= 'z')
87 else if (c >= 'A' && c <= 'Z')
89 else if (c >= '0' && c <= '9')
91 else if (c >= 0x20 && c <= 127)
99 printf("%s", linebuf);
105 void read_from(enum eyefi_file);
106 void write_to(enum eyefi_file, void *, int);
108 struct card_seq_num eyefi_seq;
109 struct card_seq_num read_seq_from(enum eyefi_file file)
111 struct card_seq_num *ret;
118 * For O_DIRECT writes to files, we need
119 * to be 512 byte aligned on Linux, I think.
120 * So, just align this to something big
121 * and be done with it. FIXME :)
123 * This probably isn't necessary on chdk,
124 * since I don't think it buffers I/O at
129 unsigned long addr = (unsigned long)&unaligned_buf[EYEFI_BUF_SIZE];
130 addr &= ~(EYEFI_BUF_SIZE-1);
131 eyefi_buf = (void *)addr;
132 debug_printf(4, "buf: %p\n", eyefi_buf);
133 debug_printf(4, "unaligned: %p\n", &unaligned_buf[0]);
137 * The real manager does this so we might
140 void zero_card_files(void)
142 //write_to(REQM, eyefi_buf, EYEFI_BUF_SIZE);
143 write_to(REQC, eyefi_buf, EYEFI_BUF_SIZE);
144 write_to(RSPM, eyefi_buf, EYEFI_BUF_SIZE);
145 write_to(RSPC, eyefi_buf, EYEFI_BUF_SIZE);
156 if (eyefi_buf != NULL)
159 debug_printf(2, "Initializing card...\n");
160 mnt = locate_eyefi_mount();
166 eyefi_seq = read_seq_from(RSPC);
167 if (eyefi_seq.seq == 0)
168 eyefi_seq.seq = 0x1234;
170 debug_printf(2, "Done initializing card...\n");
171 debug_printf(2, "seq was: %04lx\n", eyefi_seq.seq);
174 static char *eyefi_file(enum eyefi_file file)
177 return eyefi_file_on(file, locate_eyefi_mount());
180 void read_from(enum eyefi_file __file)
184 char *file = eyefi_file(__file);
189 fd = open(file, O_RDONLY);
191 open_error(file, fd);
192 retcntl = fd_dont_cache(fd);
197 ret = read(fd, eyefi_buf, EYEFI_BUF_SIZE);
198 if (eyefi_debug_level > 3)
199 dumpbuf(eyefi_buf, 128);
202 perror("bad read, retrying...");
206 debug_printf(4, "read '%s': bytes: %d fcntl: %d\n", file, ret, retcntl);
208 * There was a time when I was carefully recording how each response
209 * looked, and I counted the zeros in each response. I don't care
214 for (i=0; i < EYEFI_BUF_SIZE; i++) {
215 c = ((char *)eyefi_buf)[i];
227 void write_to(enum eyefi_file __file, void *stuff, int len)
237 file = eyefi_file(__file);
241 if (eyefi_debug_level > 3) {
242 debug_printf(3, "%s('%s', ..., %d)\n", __func__, file, len);
245 memset(eyefi_buf, 0, EYEFI_BUF_SIZE);
246 memcpy(eyefi_buf, stuff, len);
247 fd = open(file, O_RDWR|O_CREAT, 0600);
249 open_error(file, fd);
250 ret = fd_dont_cache(fd);
252 open_error(file, ret);
253 if (eyefi_debug_level > 3)
254 dumpbuf(eyefi_buf, 128);
255 ret = write(fd, eyefi_buf, EYEFI_BUF_SIZE);
257 debug_printf(3, "wrote %d bytes to '%s' (string was %d bytes)\n", ret, file, len);
259 fprintf(stderr, "error writing to '%s': ", file);
266 #define write_struct(file, s) write_to((file), s, sizeof(*(s)))
271 * Oddly enough, the sequence number appears
272 * to be of normal endianness.
274 //u32 tmpseq = be32_to_u32(seq.seq);
275 //seq.seq = u32_to_be32(tmpseq+1);
277 write_struct(REQC, &eyefi_seq);
280 u32 eyefi_current_seq(void)
282 return eyefi_seq.seq;
285 int wait_for_response(void)
290 debug_printf(3, "waiting for response...\n");
292 for (i = 0; i < 10; i++) {
293 struct card_seq_num cardseq = read_seq_from(RSPC);
294 debug_printf(3, "read rsp code: %lx, looking for: %lx raw: %lx\n", rsp, eyefi_current_seq(),
297 if (rsp == eyefi_current_seq()) {
301 if (eyefi_debug_level > 4) {
303 debug_printf(1, "command issued was: '%c'\n", ((char *)eyefi_buf)[0]);
308 debug_printf(1, "never saw card seq response\n");
311 debug_printf(3, "got good seq (%ld), reading RSPM...\n", rsp);
313 debug_printf(3, "done reading RSPM\n");
317 char *net_test_states[] = {
320 "verifying network key",
322 "testing connection to Eye-Fi server",
326 char *net_test_state_name(u8 state)
328 int size = ARRAY_SIZE(net_test_states);
331 return net_test_states[state];
334 char *net_types[] = {
342 char *net_type_name(u8 type)
344 int size = ARRAY_SIZE(net_types);
347 return net_types[type];
350 static char lower(char c)
352 if ((c >= 'A') && (c <= 'Z'))
360 if ((c >= '0') && (c <= '9'))
362 else if ((c >= 'a') && (c <= 'z'))
363 return (c - 'a') + 10;
364 debug_printf(5, "non-hex character: '%c'/'%c'\n", c, lc);
369 * Take a string like "0ab1" and make it
370 * a series of bytes: { 0x0a, 0xb1 }
372 * @len is the strlen() of the ascii
374 * Destroys the original string.
376 char *convert_ascii_to_hex(char *ascii, int len)
380 fprintf(stderr, "%s() must be even number of bytes: %d\n",
384 for (i=0; i < len; i+=2) {
385 int high = atoh(ascii[i]);
386 int low = atoh(ascii[i+1]);
387 u8 byte = (high<<4 | low);
388 if (high < 0 || low < 0)
390 debug_printf(6, "high: %02x low: %02x, both: %02x\n", high, low, byte);
393 for (i=len/2; i < len; i++)
398 struct wpa_key *make_wpa_key(char *essid, char *pass)
400 struct wpa_key *key = malloc(sizeof(*key));
402 if (strlen(pass) == WPA_KEY_BYTES*2) {
404 debug_printf(2, "Interpreting password as hex WPA key\n");
405 hex_pass = convert_ascii_to_hex(pass, WPA_KEY_BYTES*2);
408 memcpy(&key->key[0], pass, WPA_KEY_BYTES);
410 debug_printf(2, "Interpreting password as ASCII WPA key\n");
411 pbkdf2_sha1(pass, essid, strlen(essid), 4096,
412 &key->key[0], WPA_KEY_BYTES);
417 int card_info_cmd(enum card_info_subcommand cmd)
419 struct card_info_req cir;
421 cir.subcommand = cmd;
423 write_struct(REQM, &cir);
424 return wait_for_response();
427 u32 fetch_log_length(void)
429 debug_printf(3, "%s()\n", __func__);
430 card_info_cmd(LOG_LEN);
431 struct card_info_log_len *loglen = eyefi_buf;
432 return be32_to_u32(loglen->val);
435 struct card_firmware_info *fetch_card_firmware_info(void)
437 debug_printf(2, "%s()\n", __func__);
438 card_info_cmd(FIRMWARE_INFO);
439 return (struct card_firmware_info *)eyefi_buf;
459 strcpy(tb.name, "www.sr71.net/");
460 tb.l1 = strlen(tb.name);
461 for (i = 0; i < 10; i++) {
464 write_struct(RSPM, &z);
465 write_struct(REQM, &tb);
467 printf("buffer after O %d:\n", i);
468 dumpbuf(eyefi_buf, 64);
469 printf("----------------\n");
470 write_struct(REQM, &tb);
472 printf("card info(%d):\n", i);
473 dumpbuf(eyefi_buf, 64);
474 printf("-----------\n");
478 strcpy(tb.name, "/public/eyefi/servname");
479 strcpy(tb.name, "/config/networks.xml");
480 //tb.len = strlen(tb.name);
482 for (c = 'O'; c <= 'O'; c++) {
484 write_struct(REQM, &tb);
486 printf("dumping buffer:\n");
487 dumpbuf(eyefi_buf, 64);
488 printf("buffer dump done\n");
492 struct card_info_rsp_key *fetch_card_key(void)
494 struct card_info_rsp_key *key;
496 debug_printf(2, "%s()\n", __func__);
497 card_info_cmd(CARD_KEY);
502 int issue_noarg_command(u8 cmd)
504 struct noarg_request req;
505 printf("%s() cmd: %d\n", __func__, cmd);
507 write_struct(REQM, &req);
508 return wait_for_response();
511 struct scanned_net_list *scan_nets(void)
513 struct scanned_net_list *scanned;
515 debug_printf(2, "%s()\n", __func__);
516 issue_noarg_command('g');
517 scanned = eyefi_response();
521 struct configured_net_list *fetch_configured_nets(void)
523 struct configured_net_list *configured;
525 debug_printf(2, "%s()\n", __func__);
526 issue_noarg_command('l');
527 configured = eyefi_buf;
531 void reboot_card(void)
533 debug_printf(2, "%s()\n", __func__);
534 issue_noarg_command('b');
537 void copy_wep_key(struct wep_key *dst, struct wep_key *src)
539 memcpy(&dst->key, &src->key, sizeof(*dst));
542 void copy_wpa_key(struct wpa_key *dst, struct wpa_key *src)
544 memcpy(&dst->key, &src->key, sizeof(*dst));
547 int network_action(char cmd, char *essid, char *wpa_ascii)
549 struct net_request nr;
550 memset(&nr, 0, sizeof(nr));
553 strcpy(&nr.essid[0], essid);
554 nr.essid_len = strlen(essid);
555 struct wpa_key *wpakey;
557 wpakey = make_wpa_key(essid, wpa_ascii);
558 nr.key.len = sizeof(*wpakey);
559 copy_wpa_key(&nr.key.wpa, wpakey);
561 write_struct(REQM, &nr);
562 return wait_for_response();
565 void add_network(char *essid, char *wpa_ascii)
567 debug_printf(2, "%s()\n", __func__);
568 network_action('a', essid, wpa_ascii);
571 void remove_network(char *essid)
573 debug_printf(2, "%s()\n", __func__);
574 network_action('d', essid, NULL);
577 int get_log_at_offset(u32 offset)
579 struct fetch_log_cmd cmd;
581 cmd.offset = u32_to_be32(offset);
583 debug_printf(2, "getting log at offset: %08lx\n", offset);
584 write_struct(REQM, &cmd);
585 return wait_for_response();
588 void add_log_piece(u8 *log, int log_len, u8 *piece, int piece_pos, int piece_size)
590 debug_printf(0, "%s(%p, %d, %p, %d, %d)\n", __func__, log, log_len, piece, piece_pos, piece_size);
591 if (piece_pos + piece_size > log_len) {
592 int overflow_by = (piece_pos + piece_size) - log_len;
593 int piece_overrun_pos = piece_size - overflow_by;
594 piece_size -= overflow_by;
595 memcpy(&log[0], &piece[piece_overrun_pos], overflow_by);
596 debug_printf(0, "writing %d bytes to logbuf[0] from piece[%d]\n",
597 overflow_by, piece_overrun_pos);
599 debug_printf(0, "writing %d bytes to logbuf[%d]\n", piece_size, piece_pos);
600 memcpy(&log[piece_pos], piece, piece_size);
603 int get_log_into(u8 *resbuf)
610 u32 __log_size = fetch_log_length();
611 int log_pieces = __log_size/EYEFI_BUF_SIZE;
616 /* There are 8 bytes of header in the first log entry
617 * to specify where the log starts and ends */
618 u32 log_size = __log_size - 8;
620 for (i = 0; i < log_pieces; i++) {
621 debug_printf(1, "fetching EyeFi card log part %d/%d...",
624 ret = get_log_at_offset(EYEFI_BUF_SIZE*i);
625 debug_printf(1, "done\n");
629 struct first_log_response *log = eyefi_buf;
630 log_end = log_size - be32_to_u32(log->log_end);
631 log_start = log_size - be32_to_u32(log->log_start);
632 debug_printf(2, "log end: 0x%04lx\n", log_end);
633 debug_printf(2, "log start: 0x%04lx\n", log_start);
634 log_piece = &log->data[0];
635 log_piece_size = ARRAY_SIZE(log->data);
637 struct rest_log_response *log = eyefi_buf;
638 log_piece = &log->data[0];
639 log_piece_size = ARRAY_SIZE(log->data);
641 add_log_piece(resbuf, log_size, log_piece, log_start, log_piece_size);
642 total_bytes += log_piece_size;
643 log_start += log_piece_size;
644 log_start = log_start % log_size;