]>
Commit | Line | Data |
---|---|---|
1 | /* (not working) flasher for HM-CFG-USB | |
2 | * | |
3 | * Copyright (c) 2013 Michael Gernoth <michael@gernoth.net> | |
4 | * | |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | * of this software and associated documentation files (the "Software"), to | |
7 | * deal in the Software without restriction, including without limitation the | |
8 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
9 | * sell copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
21 | * IN THE SOFTWARE. | |
22 | */ | |
23 | ||
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
26 | #include <unistd.h> | |
27 | #include <stdint.h> | |
28 | #include <string.h> | |
29 | #include <strings.h> | |
30 | #include <poll.h> | |
31 | #include <errno.h> | |
32 | #include <sys/types.h> | |
33 | #include <sys/stat.h> | |
34 | #include <fcntl.h> | |
35 | #include <sys/time.h> | |
36 | #include <libusb-1.0/libusb.h> | |
37 | ||
38 | #include "hexdump.h" | |
39 | #include "hmcfgusb.h" | |
40 | ||
41 | struct recv_data { | |
42 | }; | |
43 | ||
44 | static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data) | |
45 | { | |
46 | if (buf_len < 1) | |
47 | return 1; | |
48 | ||
49 | switch(buf[0]) { | |
50 | case 'E': | |
51 | case 'H': | |
52 | case 'R': | |
53 | case 'I': | |
54 | break; | |
55 | default: | |
56 | hexdump(buf, buf_len, "Unknown> "); | |
57 | break; | |
58 | } | |
59 | ||
60 | return 1; | |
61 | } | |
62 | ||
63 | static uint8_t ascii_to_nibble(uint8_t a) | |
64 | { | |
65 | uint8_t c = 0x00; | |
66 | ||
67 | if ((a >= '0') && (a <= '9')) { | |
68 | c = a - '0'; | |
69 | } else if ((a >= 'A') && (a <= 'F')) { | |
70 | c = (a - 'A') + 10; | |
71 | } else if ((a >= 'a') && (a <= 'f')) { | |
72 | c = (a - 'a') + 10; | |
73 | } | |
74 | ||
75 | return c; | |
76 | } | |
77 | ||
78 | int main(int argc, char **argv) | |
79 | { | |
80 | struct hmcfgusb_dev *dev; | |
81 | struct recv_data rdata; | |
82 | uint8_t out[4096]; | |
83 | uint8_t buf[4096]; | |
84 | uint8_t *outp; | |
85 | int fd; | |
86 | int r; | |
87 | int i; | |
88 | int cnt; | |
89 | int pkt; | |
90 | ||
91 | hmcfgusb_set_debug(0); | |
92 | ||
93 | memset(&rdata, 0, sizeof(rdata)); | |
94 | ||
95 | dev = hmcfgusb_init(parse_hmcfgusb, &rdata); | |
96 | if (!dev) { | |
97 | fprintf(stderr, "Can't initialize HM-CFG-USB\n"); | |
98 | exit(EXIT_FAILURE); | |
99 | } | |
100 | printf("HM-CFG-USB opened!\n"); | |
101 | ||
102 | fd = open("hmusbif.enc", O_RDONLY); | |
103 | if (fd < 0) { | |
104 | perror("Can't open hmusbif.enc"); | |
105 | exit(EXIT_FAILURE); | |
106 | } | |
107 | ||
108 | cnt = 0; | |
109 | pkt = 0; | |
110 | do { | |
111 | int len; | |
112 | ||
113 | memset(buf, 0, sizeof(buf)); | |
114 | r = read(fd, buf, 4); | |
115 | if (r < 0) { | |
116 | perror("read"); | |
117 | exit(EXIT_FAILURE); | |
118 | } else if (r == 0) { | |
119 | break; | |
120 | } else if (r != 4) { | |
121 | printf("can't get length information!\n"); | |
122 | exit(EXIT_FAILURE); | |
123 | } | |
124 | ||
125 | len = (ascii_to_nibble(buf[0]) & 0xf)<< 4; | |
126 | len |= ascii_to_nibble(buf[1]) & 0xf; | |
127 | len <<= 8; | |
128 | len |= (ascii_to_nibble(buf[2]) & 0xf)<< 4; | |
129 | len |= ascii_to_nibble(buf[3]) & 0xf; | |
130 | ||
131 | printf("packet length: %x\n", len); | |
132 | ||
133 | r = read(fd, buf, len * 2); | |
134 | if (r < 0) { | |
135 | perror("read"); | |
136 | exit(EXIT_FAILURE); | |
137 | } else if (r < len * 2) { | |
138 | printf("short read, aborting (%d < %d)\n", r, len * 2); | |
139 | break; | |
140 | } | |
141 | ||
142 | memset(out, 0, sizeof(out)); | |
143 | outp = out; | |
144 | *outp++ = 'W'; | |
145 | *outp++ = (pkt >> 8) & 0xff; | |
146 | *outp++ = pkt & 0xff; | |
147 | *outp++ = (len >> 8) & 0xff; | |
148 | *outp++ = len & 0xff; | |
149 | for (i = 0; i < r; i+=2) { | |
150 | *outp = (ascii_to_nibble(buf[i]) & 0xf)<< 4; | |
151 | *outp |= ascii_to_nibble(buf[i+1]) & 0xf; | |
152 | outp++; | |
153 | } | |
154 | cnt += r/2; | |
155 | printf("Flashing %d bytes...\n", cnt); | |
156 | hexdump(out, outp-out, "F> "); | |
157 | //hmcfgusb_send(dev, out, r/2, 1); | |
158 | pkt++; | |
159 | } while (r > 0); | |
160 | ||
161 | hmcfgusb_close(dev); | |
162 | ||
163 | return EXIT_SUCCESS; | |
164 | } |