diff --git a/avrprog.c b/avrprog.c
index b1a8964e..4066c5b3 100644
--- a/avrprog.c
+++ b/avrprog.c
@@ -64,6 +64,7 @@
#include
#include
#include
+#include
#define DEFAULT_PARALLEL "/dev/ppi0"
@@ -102,12 +103,12 @@ typedef enum {
AVR_FLASH_HI
} AVRMEM;
-enum {
+typedef enum {
FMT_AUTO,
FMT_SREC,
FMT_IHEX,
FMT_RBIN
-};
+} FILEFMT;
struct avrpart {
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 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",
progname, fio->iodesc, fio->dir, filename, strerror(errno),
fio->rw, rc, size);
- return -5;
+ return -1;
}
return rc;
@@ -987,20 +1000,20 @@ int fileio_ihex ( struct fioparms * fio,
case FIO_WRITE:
rc = b2ihex(buf, size, 32, 0, filename, f);
if (rc) {
- return -5;
+ return -1;
}
break;
case FIO_READ:
rc = ihex2b(filename, f, buf, size);
if (rc)
- return -5;
+ return -1;
break;
default:
fprintf(stderr, "%s: invalid Intex Hex file I/O operation=%d\n",
progname, fio->op);
- return -5;
+ return -1;
break;
}
@@ -1013,7 +1026,7 @@ int fileio_srec ( struct fioparms * fio,
{
fprintf(stderr, "%s: Motorola S-Record %s format not yet supported\n",
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 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= 10) && isdigit(buf[1])) {
+ found = 1;
+ for (i=1; i