]>
Commit | Line | Data |
---|---|---|
9fb0f4d2 MG |
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[0x40]; //FIXME!!! | |
83 | uint8_t buf[0x80]; | |
84 | int fd; | |
85 | int r; | |
86 | int i; | |
87 | int cnt; | |
88 | ||
89 | hmcfgusb_set_debug(0); | |
90 | ||
91 | memset(&rdata, 0, sizeof(rdata)); | |
92 | ||
93 | dev = hmcfgusb_init(parse_hmcfgusb, &rdata); | |
94 | if (!dev) { | |
95 | fprintf(stderr, "Can't initialize HM-CFG-USB\n"); | |
96 | exit(EXIT_FAILURE); | |
97 | } | |
98 | printf("HM-CFG-USB opened!\n"); | |
99 | ||
100 | fd = open("hmusbif.enc", O_RDONLY); | |
101 | if (fd < 0) { | |
102 | perror("Can't open hmusbif.enc"); | |
103 | exit(EXIT_FAILURE); | |
104 | } | |
105 | ||
106 | cnt = 0; | |
107 | do { | |
108 | memset(buf, 0, sizeof(buf)); | |
109 | r = read(fd, buf, sizeof(buf)); | |
110 | if (r < 0) { | |
111 | perror("read"); | |
112 | exit(EXIT_FAILURE); | |
113 | } else if (r == 0) { | |
114 | break; | |
115 | } | |
116 | memset(out, 0, sizeof(out)); | |
117 | for (i = 0; i < r; i+=2) { | |
118 | out[i/2] = (ascii_to_nibble(buf[i]) & 0xf)<< 4; | |
119 | out[i/2] |= ascii_to_nibble(buf[i+1]) & 0xf; | |
120 | } | |
121 | cnt += r/2; | |
122 | printf("Flashing %d bytes...\n", cnt); | |
123 | hmcfgusb_send(dev, out, r/2, 1); | |
124 | } while (r > 0); | |
125 | ||
126 | hmcfgusb_close(dev); | |
127 | ||
128 | return EXIT_SUCCESS; | |
129 | } |