[d09030d] | 1 | #define _XOPEN_SOURCE 700
|
---|
| 2 |
|
---|
| 3 | // C99
|
---|
| 4 | #include <stdio.h>
|
---|
| 5 | #include <stdlib.h>
|
---|
| 6 | #include <string.h>
|
---|
| 7 |
|
---|
| 8 | // POSIX/XSI
|
---|
| 9 | #include <getopt.h>
|
---|
| 10 |
|
---|
| 11 | #include "device.h"
|
---|
[16cf6ff] | 12 | #include "27c020.h"
|
---|
[d09030d] | 13 | #include "at29c040.h"
|
---|
| 14 |
|
---|
| 15 |
|
---|
| 16 | int main(int argc, char* argv[])
|
---|
| 17 | {
|
---|
| 18 | char c;
|
---|
[34c57e8] | 19 | const char* file = NULL;
|
---|
[d09030d] | 20 | char opcode = 0;
|
---|
| 21 | int port_base = 0x378;
|
---|
| 22 | Device* device = NULL;
|
---|
[34c57e8] | 23 | int error = 0;
|
---|
[d09030d] | 24 |
|
---|
[16cf6ff] | 25 | fprintf(stderr, "EPRoxygen - UNIX driver for SEEIT EPR-02 ROM programmer\n"
|
---|
[d09030d] | 26 | "Copyright 2012, Adrien Destugues <pulkomandy@pulkomandy.tk>\n"
|
---|
| 27 | "Distributed under the terms of the MIT licence\n"
|
---|
| 28 | );
|
---|
| 29 |
|
---|
[34c57e8] | 30 | while((c = getopt(argc, argv, "p:d:r:w:e")) >= 0)
|
---|
[d09030d] | 31 | {
|
---|
| 32 | switch(c)
|
---|
| 33 | {
|
---|
| 34 | case '?':
|
---|
| 35 | case ':':
|
---|
| 36 | fprintf(stderr, "Unrecognized option -%c\n", optopt);
|
---|
| 37 | // TODO print help
|
---|
[34c57e8] | 38 | error = -1;
|
---|
| 39 | goto abort;
|
---|
[d09030d] | 40 | case 'p':
|
---|
| 41 | // Set port
|
---|
| 42 | if(sscanf(optarg, "%d", &port_base) != 1)
|
---|
| 43 | {
|
---|
| 44 | fprintf(stderr, "Invalid I/O port address\n");
|
---|
[34c57e8] | 45 | error = -4;
|
---|
| 46 | goto abort;
|
---|
[d09030d] | 47 | }
|
---|
| 48 | break;
|
---|
| 49 | case 'd':
|
---|
| 50 | // TODO Set device
|
---|
[16cf6ff] | 51 | if (strcmp(optarg, "AT29C040") == 0)
|
---|
[d09030d] | 52 | {
|
---|
[16cf6ff] | 53 | device = new AT29C040();
|
---|
| 54 | } else if(strcmp(optarg, "27C020") == 0)
|
---|
| 55 | {
|
---|
| 56 | device = new x27C020();
|
---|
| 57 | } else {
|
---|
[d09030d] | 58 | fprintf(stderr,"Unknown device (only AT29C040 is supported)\n");
|
---|
[34c57e8] | 59 | error = -3;
|
---|
| 60 | goto abort;
|
---|
[d09030d] | 61 | }
|
---|
| 62 | break;
|
---|
| 63 | case 'r':
|
---|
| 64 | case 'w':
|
---|
| 65 | // Set file
|
---|
| 66 | file = optarg;
|
---|
| 67 | // Fallthrough
|
---|
| 68 | case 'e':
|
---|
| 69 | // Set operation
|
---|
| 70 | if (opcode != 0)
|
---|
| 71 | {
|
---|
| 72 | fprintf(stderr, "Multiple operations requested (only one of r,w,e is allowed)\n");
|
---|
[34c57e8] | 73 | error = -2;
|
---|
| 74 | goto abort;
|
---|
[d09030d] | 75 | }
|
---|
| 76 | opcode = c;
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[16cf6ff] | 80 | if (optind < argc)
|
---|
| 81 | {
|
---|
| 82 | fprintf(stderr, "Error parsing the command line before \"%s\"\n", argv[optind]);
|
---|
| 83 | error = -7;
|
---|
| 84 | goto abort;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[d09030d] | 87 | if (opcode == 0)
|
---|
| 88 | {
|
---|
| 89 | fprintf(stderr, "No operation specified (one of r,w,e). Aborting.\n");
|
---|
[34c57e8] | 90 | error = -5;
|
---|
| 91 | goto abort;
|
---|
[d09030d] | 92 | }
|
---|
| 93 |
|
---|
[34c57e8] | 94 | if (device == NULL)
|
---|
| 95 | {
|
---|
| 96 | fprintf(stderr, "No device specified.\n");
|
---|
| 97 | error = -6;
|
---|
| 98 | goto abort;
|
---|
| 99 | }
|
---|
[d09030d] | 100 |
|
---|
| 101 | // Configure voltage and powerpins according to device
|
---|
[34c57e8] | 102 | if(device->setup(port_base) != 0)
|
---|
| 103 | {
|
---|
| 104 | error = -7;
|
---|
| 105 | goto abort;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[16cf6ff] | 108 | fprintf(stderr,"Now insert chip into socket, then press any key...\n");
|
---|
[34c57e8] | 109 | getchar();
|
---|
[d09030d] | 110 |
|
---|
[34c57e8] | 111 | device->power();
|
---|
[d09030d] | 112 |
|
---|
| 113 | // Do action (program/erase/write)
|
---|
| 114 | switch(opcode)
|
---|
| 115 | {
|
---|
| 116 | case 'r':
|
---|
| 117 | device->read(file);
|
---|
| 118 | break;
|
---|
| 119 | case 'w':
|
---|
| 120 | device->write(file);
|
---|
| 121 | break;
|
---|
| 122 | case 'e':
|
---|
| 123 | device->erase();
|
---|
| 124 | break;
|
---|
[34c57e8] | 125 | default:
|
---|
| 126 | fprintf(stderr, "!!? invalid operation ?!\n");
|
---|
[d09030d] | 127 | }
|
---|
| 128 |
|
---|
[34c57e8] | 129 | abort:
|
---|
[16cf6ff] | 130 | // TODO in case ioperm failed, delete will try to shutdown() and give a bus error...
|
---|
[34c57e8] | 131 | delete device;
|
---|
[d09030d] | 132 |
|
---|
[34c57e8] | 133 | exit(error);
|
---|
| 134 | }
|
---|
[d09030d] | 135 |
|
---|
[34c57e8] | 136 | int Device::port;
|
---|
[d09030d] | 137 |
|
---|