Add input file format auto-detection support.

git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@26 81a1dc3b-b13d-400b-aceb-764788c761c2
This commit is contained in:
Brian S. Dean 2001-01-13 21:48:10 +00:00
parent 0292bd8abe
commit e6950275d9
1 changed files with 112 additions and 21 deletions

117
avrprog.c
View File

@ -64,6 +64,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include </sys/dev/ppbus/ppi.h> #include </sys/dev/ppbus/ppi.h>
#include <limits.h> #include <limits.h>
#include <ctype.h>
#define DEFAULT_PARALLEL "/dev/ppi0" #define DEFAULT_PARALLEL "/dev/ppi0"
@ -102,12 +103,12 @@ typedef enum {
AVR_FLASH_HI AVR_FLASH_HI
} AVRMEM; } AVRMEM;
enum { typedef enum {
FMT_AUTO, FMT_AUTO,
FMT_SREC, FMT_SREC,
FMT_IHEX, FMT_IHEX,
FMT_RBIN FMT_RBIN
}; } FILEFMT;
struct avrpart { struct avrpart {
char * partdesc; /* long part name */ char * partdesc; /* long part name */
@ -800,6 +801,18 @@ void usage ( void )
} }
char * fmtstr ( FILEFMT format )
{
switch (format) {
case FMT_AUTO : return "auto-detect"; break;
case FMT_SREC : return "Motorola S-Record"; break;
case FMT_IHEX : return "Intel Hex"; break;
case FMT_RBIN : return "raw binary"; break;
default : return "invalid format"; break;
};
}
int b2ihex ( unsigned char * inbuf, int bufsize, int b2ihex ( unsigned char * inbuf, int bufsize,
int recsize, int startaddr, int recsize, int startaddr,
@ -971,7 +984,7 @@ int fileio_rbin ( struct fioparms * fio,
"%s: %s error %s %s: %s; %s %d of the expected %d bytes\n", "%s: %s error %s %s: %s; %s %d of the expected %d bytes\n",
progname, fio->iodesc, fio->dir, filename, strerror(errno), progname, fio->iodesc, fio->dir, filename, strerror(errno),
fio->rw, rc, size); fio->rw, rc, size);
return -5; return -1;
} }
return rc; return rc;
@ -987,20 +1000,20 @@ int fileio_ihex ( struct fioparms * fio,
case FIO_WRITE: case FIO_WRITE:
rc = b2ihex(buf, size, 32, 0, filename, f); rc = b2ihex(buf, size, 32, 0, filename, f);
if (rc) { if (rc) {
return -5; return -1;
} }
break; break;
case FIO_READ: case FIO_READ:
rc = ihex2b(filename, f, buf, size); rc = ihex2b(filename, f, buf, size);
if (rc) if (rc)
return -5; return -1;
break; break;
default: default:
fprintf(stderr, "%s: invalid Intex Hex file I/O operation=%d\n", fprintf(stderr, "%s: invalid Intex Hex file I/O operation=%d\n",
progname, fio->op); progname, fio->op);
return -5; return -1;
break; break;
} }
@ -1013,7 +1026,7 @@ int fileio_srec ( struct fioparms * fio,
{ {
fprintf(stderr, "%s: Motorola S-Record %s format not yet supported\n", fprintf(stderr, "%s: Motorola S-Record %s format not yet supported\n",
progname, fio->iodesc); progname, fio->iodesc);
return -5; return -1;
} }
@ -1047,7 +1060,72 @@ int fileio_setparms ( int op, struct fioparms * fp )
} }
int fileio ( int op, char * filename, int format,
int fmt_autodetect ( char * fname )
{
FILE * f;
unsigned char buf[MAX_LINE_LEN];
int i;
int len;
int found;
f = fopen(fname, "r");
if (f == NULL) {
fprintf(stderr, "%s: error opening %s: %s\n",
progname, fname, strerror(errno));
return -1;
}
while (fgets((char *)buf, MAX_LINE_LEN, f)!=NULL) {
buf[MAX_LINE_LEN-1] = 0;
len = strlen((char *)buf);
if (buf[len-1] == '\n')
buf[--len] = 0;
/* check for binary data */
found = 0;
for (i=0; i<len; i++) {
if (buf[i] > 127) {
found = 1;
break;
}
}
if (found)
return FMT_RBIN;
/* check for lines that look like intel hex */
if ((buf[0] == ':') && (len >= 11)) {
found = 1;
for (i=1; i<len; i++) {
if (!isxdigit(buf[1])) {
found = 0;
break;
}
}
if (found)
return FMT_IHEX;
}
/* check for lines that look like motorola s-record */
if ((buf[0] == 'S') && (len >= 10) && isdigit(buf[1])) {
found = 1;
for (i=1; i<len; i++) {
if (!isxdigit(buf[1])) {
found = 0;
break;
}
}
if (found)
return FMT_SREC;
}
}
return -1;
}
int fileio ( int op, char * filename, FILEFMT format,
struct avrpart * p, AVRMEM memtype ) struct avrpart * p, AVRMEM memtype )
{ {
int rc; int rc;
@ -1078,7 +1156,7 @@ int fileio ( int op, char * filename, int format,
if (f == NULL) { if (f == NULL) {
fprintf(stderr, "%s: can't open %s file %s: %s\n", fprintf(stderr, "%s: can't open %s file %s: %s\n",
progname, fio.iodesc, fname, strerror(errno)); progname, fio.iodesc, fname, strerror(errno));
return -2; return -1;
} }
} }
@ -1096,7 +1174,7 @@ int fileio ( int op, char * filename, int format,
default: default:
fprintf(stderr, "%s: invalid memory type for %s: %d\n", fprintf(stderr, "%s: invalid memory type for %s: %d\n",
progname, fio.iodesc, memtype); progname, fio.iodesc, memtype);
return -3; return -1;
} }
if (fio.op == FIO_READ) { if (fio.op == FIO_READ) {
@ -1106,6 +1184,19 @@ int fileio ( int op, char * filename, int format,
} }
} }
if (format == FMT_AUTO) {
format = fmt_autodetect(fname);
if (format < 0) {
fprintf(stderr,
"%s: can't determine file format for %s, specify explicitly\n",
progname, fname);
return -1;
}
fprintf(stderr, "%s: %s file %s auto detected as %s\n\n",
progname, fio.iodesc, fname, fmtstr(format));
}
switch (format) { switch (format) {
case FMT_IHEX: case FMT_IHEX:
rc = fileio_ihex(&fio, fname, f, buf, size); rc = fileio_ihex(&fio, fname, f, buf, size);
@ -1122,7 +1213,7 @@ int fileio ( int op, char * filename, int format,
default: default:
fprintf(stderr, "%s: invalid %s file format: %d\n", fprintf(stderr, "%s: invalid %s file format: %d\n",
progname, fio.iodesc, format); progname, fio.iodesc, format);
return -4; return -1;
} }
return rc; return rc;
@ -1168,7 +1259,7 @@ int main ( int argc, char * argv [] )
int ovsigck; /* 1=override sig check, 0=don't */ int ovsigck; /* 1=override sig check, 0=don't */
char * parallel; /* parallel port device */ char * parallel; /* parallel port device */
int interactive; /* 1=enter interactive command mode, 0=don't */ int interactive; /* 1=enter interactive command mode, 0=don't */
int filefmt; /* FMT_AUTO, FMT_IHEX, FMT_SREC, FMT_RBIN */ FILEFMT filefmt; /* FMT_AUTO, FMT_IHEX, FMT_SREC, FMT_RBIN */
readorwrite = 0; readorwrite = 0;
parallel = DEFAULT_PARALLEL; parallel = DEFAULT_PARALLEL;
@ -1517,7 +1608,7 @@ int main ( int argc, char * argv [] )
* test mode, don't actually write to the chip, output the buffer * test mode, don't actually write to the chip, output the buffer
* to stdout in intel hex instead * to stdout in intel hex instead
*/ */
fileio(FIO_WRITE, "-", FMT_IHEX, p, memtype); rc = fileio(FIO_WRITE, "-", FMT_IHEX, p, memtype);
#endif #endif
if (rc) { if (rc) {
fprintf ( stderr, "%s: failed to write flash memory, rc=%d\n", fprintf ( stderr, "%s: failed to write flash memory, rc=%d\n",