1
1
#include <errno.h>
2
+ #include <getopt.h>
2
3
#include <signal.h>
3
4
#include <string.h>
4
5
#include <stdio.h>
9
10
10
11
#include <libusb.h>
11
12
13
+ /* options */
14
+ typedef struct _options_t {
15
+ int verbose ;
16
+ char * file ;
17
+ } options_t ;
18
+
19
+ // defaults
20
+ options_t opts = {
21
+ .verbose = 0 ,
22
+ .file = NULL ,
23
+ };
24
+
12
25
#define EMS_EP_SEND (2 | LIBUSB_ENDPOINT_OUT)
13
26
#define EMS_EP_RECV (1 | LIBUSB_ENDPOINT_IN)
14
27
@@ -21,6 +34,7 @@ static struct libusb_device_handle *devh = NULL;
21
34
22
35
static int find_ems_device (void ) {
23
36
devh = libusb_open_device_with_vid_pid (NULL , 0x4670 , 0x9394 );
37
+
24
38
return devh ? 0 : - EIO ;
25
39
}
26
40
@@ -84,64 +98,107 @@ static int ems_write(uint32_t offset, unsigned char *buf, size_t count) {
84
98
return r ;
85
99
}
86
100
101
+ void get_options (int argc , char * * argv ) {
102
+ int c ;
103
+
104
+ while (1 ) {
105
+ int this_option_optind = optind ? optind : 1 ;
106
+ int option_index = 0 ;
107
+ static struct option long_options [] = {
108
+ {"verbose" , 0 , 0 , 'v' },
109
+ {0 , 0 , 0 , 0 }
110
+ };
111
+
112
+ c = getopt_long (
113
+ argc , argv , "v" ,
114
+ long_options , & option_index
115
+ );
116
+ if (c == -1 )
117
+ break ;
118
+
119
+ switch (c ) {
120
+ case 'v' :
121
+ opts .verbose = 1 ;
122
+ break ;
123
+ default :
124
+ break ;
125
+ }
126
+ }
127
+
128
+ // extra argument: ROM file
129
+ if (optind < argc )
130
+ opts .file = argv [optind ];
131
+ }
132
+
87
133
int main (int argc , char * * argv ) {
88
134
int r = 1 ;
89
135
int i ;
90
136
137
+ get_options (argc , argv );
138
+
91
139
r = libusb_init (NULL );
92
140
if (r < 0 ) {
93
- fprintf (stderr , "failed to initialise libusb\n" );
94
- exit (1 );
141
+ fprintf (stderr , "failed to initialise libusb\n" );
142
+ exit (1 );
95
143
}
96
144
97
145
r = find_ems_device ();
98
146
if (r < 0 ) {
99
- fprintf (stderr , "Could not find/open device\n" );
100
- goto out ;
147
+ fprintf (stderr , "Could not find/open device\n" );
148
+ goto out ;
101
149
}
102
150
103
151
r = libusb_claim_interface (devh , 0 );
104
152
if (r < 0 ) {
105
- fprintf (stderr , "usb_claim_interface error %d\n" , r );
106
- goto out ;
153
+ fprintf (stderr , "usb_claim_interface error %d\n" , r );
154
+ goto out ;
107
155
}
108
- printf ("claimed interface\n" );
109
156
110
- if (argc > 1 ) {
111
- FILE * write_file = fopen (argv [1 ], "r" );
112
- if (write_file == NULL )
113
- err (1 , "fopen" );
157
+ if (opts .verbose )
158
+ printf ("claimed interface\n" );
159
+
160
+ // file provided: write the file
161
+ if (opts .file != NULL ) {
162
+ FILE * write_file = fopen (opts .file , "r" );
163
+ if (write_file == NULL ) {
164
+ warn ("Can't open ROM file %s" , opts .file );
165
+ goto out_release ;
166
+ }
114
167
115
168
char buf [32 ];
116
169
uint32_t offset = 0 ;
117
170
118
171
while (fread (buf , sizeof (buf ), 1 , write_file ) == 1 ) {
119
172
r = ems_write (offset , buf , sizeof (buf ));
120
- if (r < 0 )
121
- errx (1 , "can't write %d" , r );
173
+ if (r < 0 ) {
174
+ warn ("can't write %d" , r );
175
+ goto out_release ;
176
+ }
122
177
123
178
offset += sizeof (buf );
124
179
}
125
180
}
126
181
182
+ // print the first 0x200 bytes in hex and ASCII
183
+ if (opts .verbose ) {
184
+ unsigned char data [0x200 ];
185
+ r = ems_read (0x0 , data , sizeof (data ));
127
186
128
- unsigned char data [0x200 ];
129
- r = ems_read (0x0 , data , sizeof (data ));
187
+ printf ("received %d\n" , r );
130
188
131
- printf ("received %d\n" , r );
132
-
133
- for (i = 0 ; i < r ; ++ i ) {
134
- printf ("%02x " , data [i ]);
135
- if ((i & 0xf ) == 0xf )
136
- printf ("\n" );
137
- }
189
+ for (i = 0 ; i < r ; ++ i ) {
190
+ printf ("%02x " , data [i ]);
191
+ if ((i & 0xf ) == 0xf )
192
+ printf ("\n" );
193
+ }
138
194
139
- for (i = 0 ; i < r ; ++ i ) {
140
- char pr = isprint (data [i ]) ? data [i ] : '.' ;
141
- printf ("%c" , pr );
195
+ for (i = 0 ; i < r ; ++ i ) {
196
+ char pr = isprint (data [i ]) ? data [i ] : '.' ;
197
+ printf ("%c" , pr );
142
198
143
- if ((i & 0xf ) == 0xf )
144
- printf ("\n" );
199
+ if ((i & 0xf ) == 0xf )
200
+ printf ("\n" );
201
+ }
145
202
}
146
203
147
204
out_release :
0 commit comments