2 * Tool for USB communication with Mega Everdrive
3 * Copyright (c) 2013,2014 notaz
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #include <sys/types.h>
37 static int setup(int fd)
42 memset(&tty, 0, sizeof(tty));
44 ret = tcgetattr(fd, &tty);
51 tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
52 | INLCR | IGNCR | ICRNL | IXON);
53 tty.c_oflag &= ~OPOST;
54 tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
55 tty.c_cflag &= ~(CSIZE | PARENB);
59 //tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
61 ret = tcsetattr(fd, TCSANOW, &tty);
70 static int write_to_cart(int fd, const void *data, size_t size)
74 ret = write(fd, data, size);
84 static int read_from_cart(int fd, void *data, size_t size)
88 ret = read(fd, data, size);
89 if (ret != (int)size) {
98 #define send_cmd(fd, cmd) \
99 write_to_cart(fd, cmd, strlen(cmd))
101 static int read_check_byte(int fd, char expect)
106 ret = read_from_cart(fd, &r, 1);
108 fprintf(stderr, "missing response, need '%c'\n", expect);
113 fprintf(stderr, "unexpected response: '%c', need '%c'\n",
121 static int write_with_check(int fd, const void *data, size_t size, char chk)
125 ret = write_to_cart(fd, data, size);
129 ret = read_check_byte(fd, chk);
132 fprintf(stderr, "data sent: '%16s'\n",
141 #define send_cmd_check_k(fd, cmd) \
142 write_with_check(fd, cmd, strlen(cmd), 'k')
144 static int send_file(int fd, const char *fname, const char *cmd)
155 f = fopen(fname, "rb");
158 fprintf(stderr, "fopen %s: ", fname);
162 fseek(f, 0, SEEK_END);
164 fseek(f, 0, SEEK_SET);
165 if (size > 0xf00000) {
166 fprintf(stderr, "size too large: %zd\n", size);
173 blocksz = sizeof(buf);
175 blocks = (size + blocksz - 1) / blocksz;
178 ret = write_with_check(fd, &blocks, 1, 'k');
182 // at this point, 'd' will arrive even if we don't
183 // write any more data, what's the point of that?
184 // probably a timeout?
186 for (i = 0; i < blocks; i++) {
187 ret = fread(buf, 1, blocksz, f);
188 if (i != blocks - 1 && ret != blocksz) {
189 fprintf(stderr, "failed to read block %d/%d\n",
193 memset(buf + ret, 0, blocksz - ret);
195 write_to_cart(fd, buf, blocksz);
198 ret = read_check_byte(fd, 'd');
210 static void usage(const char *argv0)
213 "%s [-d <ttydevice>] [-f <file>] command(s)\n"
214 "known commands for ED OS:\n"
215 " *g - upload game\n"
216 " *w - upload and start OS\n"
217 " *o - upload and start OS\n"
218 " *f - upload (and start?) FPGA firmware\n"
219 " *s - start last ROM (same as pressing start in menu)\n"
220 " *r<x> - run SDRAM contents as mapper x:\n"
221 " s - sms, m - md, o - OS app, c - cd BIOS, M - md10m\n"
222 "upload commands must be followed by -f <file>\n"
227 static void invarg(int argc, char *argv[], int arg)
230 fprintf(stderr, "invalid arg %d: \"%s\"\n", arg, argv[arg]);
232 fprintf(stderr, "missing required argument %d\n", arg);
236 int main(int argc, char *argv[])
238 const char *portname = "/dev/ttyUSB0";
239 const char *pending_cmd = NULL;
244 if (argc < 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
247 if (!strcmp(argv[arg], "-d")) {
249 if (argv[arg] != NULL)
250 portname = argv[arg];
252 invarg(argc, argv, arg);
256 fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
258 fprintf(stderr, "open %s: ", portname);
265 send_cmd_check_k(fd, " *T");
267 for (; arg < argc; arg++) {
268 if (!strcmp(argv[arg], "-f")) {
270 if (argv[arg] == NULL)
271 invarg(argc, argv, argc);
272 if (pending_cmd == NULL)
273 // assume game upload
276 ret = send_file(fd, argv[arg], pending_cmd);
283 if (argv[arg + 1] && !strcmp(argv[arg + 1], "-f")) {
284 /* we'll avoid sending command if there are
285 * problems with specified file */
286 pending_cmd = argv[arg];
290 ret = send_cmd_check_k(fd, argv[arg]);