-
Notifications
You must be signed in to change notification settings - Fork 13
/
ems.c
256 lines (221 loc) · 6.89 KB
/
ems.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#define _XOPEN_SOURCE 500
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h> // FIXME this will (probably) go away with error coes
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h> /* for htonl */
#include <libusb.h>
#include "ems.h"
#include "config.h"
/* magic numbers! */
#define EMS_VID 0x4670
#define EMS_PID 0x9394
#define EMS_EP_SEND (2 | LIBUSB_ENDPOINT_OUT)
#define EMS_EP_RECV (1 | LIBUSB_ENDPOINT_IN)
enum {
CMD_READ = 0xff,
CMD_WRITE = 0x57,
CMD_READ_SRAM = 0x6d,
CMD_WRITE_SRAM = 0x4d,
};
static struct libusb_device_handle *devh = NULL;
static int claimed = 0;
/**
* Attempt to find the EMS cart by vid/pid.
*
* Returns:
* 0 success
* < 0 failure
*/
static int find_ems_device(void) {
ssize_t num_devices = 0;
libusb_device **device_list = NULL;
struct libusb_device_descriptor device_descriptor;
int i = 0;
int retval = 0;
#define INSTALLUDEVMSG "Try running as root/sudo or update udev rules " \
"(check Building and Installating instructions " \
"in the README.md file for more info).\n"
num_devices = libusb_get_device_list(NULL, &device_list);
if (num_devices >= 0) {
for (; i < num_devices; ++i) {
(void) memset(&device_descriptor, 0, sizeof(device_descriptor));
retval = libusb_get_device_descriptor(device_list[i], &device_descriptor);
if (retval == 0) {
if (device_descriptor.idVendor == EMS_VID
&& device_descriptor.idProduct == EMS_PID) {
retval = libusb_open(device_list[i], &devh);
if (retval != 0) {
/*
* According to the documentation, devh will not
* be populated on error, so it should remain
* NULL.
*/
fprintf(stderr, "Failed to open device (libusb error: %s).\n", libusb_error_name(retval));
#ifdef __linux__
if (retval == LIBUSB_ERROR_ACCESS) {
fprintf(stderr, INSTALLUDEVMSG);
}
#endif
}
break;
}
} else {
fprintf(stderr, "Failed to get device description (libusb error: %s).\n", libusb_error_name(retval));
}
}
if (i == num_devices) {
fprintf(stderr, "Could not find device, is it plugged in?\n"
"Or it's may be a permission issue. "
INSTALLUDEVMSG);
}
libusb_free_device_list(device_list, 1);
device_list = NULL;
} else {
fprintf(stderr, "Failed to get device list: %s\n", libusb_error_name((int)num_devices));
}
return devh != NULL ? 0 : -EIO;
}
/**
* Init the flasher. Inits libusb and claims the device. Aborts if libusb
* can't be initialized.
*
* TODO replace printed error with return code
*
* Returns:
* 0 Success
* < 0 Failure
*/
int ems_init(void) {
int r;
void ems_deinit(void);
// call the cleanup when we're done
atexit(ems_deinit);
// mask asyn signals for the thread created by libusb_init()
// to make sure that handlers are invoked in the main thread.
// Include only signals whose handler is redirected in catchint()
#ifdef USE_PTHREAD
sigset_t newmask, oldmask;
sigemptyset(&newmask);
sigaddset(&newmask, SIGHUP);
sigaddset(&newmask, SIGINT);
sigaddset(&newmask, SIGTERM);
pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
#endif
r = libusb_init(NULL);
if (r < 0) {
fprintf(stderr, "failed to initialize libusb\n");
exit(1); // pretty much hosed
}
#ifdef USE_PTHREAD
pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
#endif
r = find_ems_device();
if (r < 0) {
return r;
}
r = libusb_claim_interface(devh, 0);
if (r < 0) {
fprintf(stderr, "usb_claim_interface error %d\n", r);
return r;
}
claimed = 1;
return 0;
}
/**
* Cleanup / release the device. Registered with atexit.
*/
void ems_deinit(void) {
if (claimed)
libusb_release_interface(devh, 0);
libusb_close(devh);
libusb_exit(NULL);
}
/**
* Initialize a command buffer. Commands are a 1 byte command code followed by
* a 4 byte address and a 4 byte value.
*
* buf must point to a memory chunk of size >= 9 bytes
*/
static void ems_command_init(
unsigned char *buf, // buffer to init
unsigned char cmd, // command to run
uint32_t addr, // address
uint32_t val // value
) {
buf[0] = cmd;
*(uint32_t *)(buf + 1) = htonl(addr);
*(uint32_t *)(buf + 5) = htonl(val);
}
/**
* Read some bytes from the cart.
*
* Params:
* from FROM_ROM or FROM_SRAM
* offset absolute read address from the cart
* buf buffer to read into (buffer must be at least count bytes)
* count number of bytes to read
*
* Returns:
* >= 0 number of bytes read (error if != count)
* < 0 error sending command or reading data
*/
int ems_read(int from, uint32_t offset, unsigned char *buf, size_t count) {
int r, transferred;
unsigned char cmd;
unsigned char cmd_buf[9];
assert(from == FROM_ROM || from == FROM_SRAM);
cmd = from == FROM_ROM ? CMD_READ : CMD_READ_SRAM;
ems_command_init(cmd_buf, cmd, offset, count);
#ifdef DEBUG
int i;
for (i = 0; i < 9; ++i)
printf("%02x ", cmd_buf[i]);
printf("\n");
#endif
// send the read command
r = libusb_bulk_transfer(devh, EMS_EP_SEND, cmd_buf, sizeof(cmd_buf), &transferred, 0);
if (r < 0)
return r;
// read the data
r = libusb_bulk_transfer(devh, EMS_EP_RECV, buf, count, &transferred, 0);
if (r < 0)
return r;
return transferred;
}
/**
* Write to the cartridge.
*
* Params:
* to TO_ROM or TO_SRAM
* offset address to write to
* buf data to write
* count number of bytes out of buf to write
*
* Returns:
* >= 0 number of bytes written (error if != count)
* < 0 error writing data
*/
int ems_write(int to, uint32_t offset, unsigned char *buf, size_t count) {
int r, transferred;
unsigned char cmd;
unsigned char *write_buf;
assert(to == TO_ROM || to == TO_SRAM);
cmd = to == TO_ROM ? CMD_WRITE : CMD_WRITE_SRAM;
// thx libusb for having no scatter/gather io
write_buf = malloc(count + 9);
if (write_buf == NULL)
err(1, "malloc");
// set up the command buffer
ems_command_init(write_buf, cmd, offset, count);
memcpy(write_buf + 9, buf, count);
r = libusb_bulk_transfer(devh, EMS_EP_SEND, write_buf, count + 9, &transferred, 0);
if (r == 0)
r = transferred>=9?transferred-9:0; // number of bytes sent on success
free(write_buf);
return r;
}