? libbfd_io.c
? libbfd_io.h
? obj
Index: Makefile.am
===================================================================
RCS file: /sources/avrdude/avrdude/Makefile.am,v
retrieving revision 1.38
diff -u -r1.38 Makefile.am
--- Makefile.am	25 Jan 2007 20:51:11 -0000	1.38
+++ Makefile.am	3 May 2007 16:44:49 -0000
@@ -51,7 +51,7 @@
 
 libavrdude_a_CFLAGS   = @ENABLE_WARNINGS@
 
-avrdude_LDADD  = $(top_builddir)/$(noinst_LIBRARIES) @LIBUSB@ @LIBHID@
+avrdude_LDADD  = $(top_builddir)/$(noinst_LIBRARIES) @LIBUSB@ @LIBHID@ -lbfd -liberty
 
 bin_PROGRAMS = avrdude
 
@@ -101,6 +101,8 @@
 	jtagmkII.c \
 	jtagmkII.h \
 	jtagmkII_private.h \
+    libbfd_io.c \
+    libbfd_io.h \
 	linux_ppdev.h \
 	lists.c \
 	lists.h \
Index: fileio.c
===================================================================
RCS file: /sources/avrdude/avrdude/fileio.c,v
retrieving revision 1.37
diff -u -r1.37 fileio.c
--- fileio.c	24 Jan 2007 22:43:46 -0000	1.37
+++ fileio.c	3 May 2007 16:44:53 -0000
@@ -21,6 +21,7 @@
 
 #include "ac_cfg.h"
 
+#include <bfd.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -31,6 +32,8 @@
 #include "avrdude.h"
 #include "avr.h"
 #include "fileio.h"
+//for dealing with ELF files
+#include "libbfd_io.h"
 
 
 #define IHEX_MAXDATA 256
@@ -89,6 +92,7 @@
     case FMT_SREC : return "Motorola S-Record"; break;
     case FMT_IHEX : return "Intel Hex"; break;
     case FMT_RBIN : return "raw binary"; break;
+    case FMT_ELF  : return "ELF"; break;
     default       : return "invalid format"; break;
   };
 }
@@ -904,6 +908,63 @@
   return -1;
 }
 
+static int fileio_elf(struct fioparms * fio,
+                  char * filename, FILE * f, unsigned char * buf, int size, 
+                  char * memtype)
+{
+  int rc;
+
+  rc = avrbfd_openr(filename);
+  if (rc < 0)
+     {
+     fprintf(stderr, "%s: fileio: Cannot open %s using ELF library.\n", progname, filename);
+     return -1;
+     }
+
+  //We don't have a 'flash' memory type, everything else does (eeprom, fuses)
+  if (strcmp(memtype, "flash"))
+       {
+       if (avrbfd_get_section_size(memtype) < 0)
+          {
+          fprintf(stderr, "%s: File does not contain data for memory type %s.\n", progname, memtype);
+          return -1;
+          }
+        }
+
+  switch (fio->op) {
+    case FIO_READ:
+      //Doing flash requires special care!
+      if (!strcmp(memtype, "flash"))
+            {
+            rc = avrbfd_make_flash_image(buf, size);
+            }
+      else
+            {
+            rc = avrbfd_get_section_size(memtype);
+            if (avrbfd_get_section_data(memtype, size, buf) < 0)
+               rc = -1;
+            }
+      break;
+    case FIO_WRITE:
+      fprintf(stderr, "%s: fileio: Cannot write an ELF type file yet\n", progname);
+      return -1;
+      break;
+    default:
+      fprintf(stderr, "%s: fileio: invalid operation=%d\n",
+              progname, fio->op);
+      return -1;
+  }
+
+  if (rc < 0 || (fio->op == FIO_WRITE && rc < size)) {
+    fprintf(stderr, 
+            "%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 -1;
+  }
+
+  return rc;
+}
 
 int fileio_setparms(int op, struct fioparms * fp)
 {
@@ -951,14 +1012,35 @@
     return -1;
   }
 
+  int firstline = 1; //Flag tells us this is first line
   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 */
+    /* check for ELF start of file */
     found = 0;
+    if ( firstline && 
+         (buf[0] == 0x7f) &&
+         (buf[1] == 0x45) &&
+         (buf[2] == 0x4c) &&
+         (buf[3] == 0x46) &&
+         (buf[4] == 0x01) &&
+         (buf[5] == 0x01) &&
+         (buf[6] == 0x01) &&
+         (buf[7] == 0x00) &&
+         (buf[8] == 0x00) &&
+         (buf[9] == 0x00) &&        
+         (buf[10] == 0x00) )
+         found = 1;
+
+    if (found)
+       return FMT_ELF;
+        
+    firstline = 0;
+
+    /* check for binary data */
     for (i=0; i<len; i++) {
       if (buf[i] > 127) {
         found = 1;
@@ -1124,6 +1206,10 @@
       rc = fileio_num(&fio, fname, f, buf, size, format);
       break;
 
+    case FMT_ELF:
+      rc = fileio_elf(&fio, fname, f, buf, size, memtype);
+      break;
+
     default:
       fprintf(stderr, "%s: invalid %s file format: %d\n",
               progname, fio.iodesc, format);
Index: fileio.h
===================================================================
RCS file: /sources/avrdude/avrdude/fileio.h,v
retrieving revision 1.16
diff -u -r1.16 fileio.h
--- fileio.h	24 Jan 2007 22:43:46 -0000	1.16
+++ fileio.h	3 May 2007 16:44:53 -0000
@@ -31,7 +31,8 @@
   FMT_HEX,
   FMT_DEC,
   FMT_OCT,
-  FMT_BIN
+  FMT_BIN,
+  FMT_ELF
 } FILEFMT;
 
 struct fioparms {
Index: update.c
===================================================================
RCS file: /sources/avrdude/avrdude/update.c,v
retrieving revision 1.1
diff -u -r1.1 update.c
--- update.c	24 Jan 2007 22:43:46 -0000	1.1
+++ update.c	3 May 2007 16:44:53 -0000
@@ -144,6 +144,7 @@
       case 'd': upd->format = FMT_DEC; break;
       case 'h': upd->format = FMT_HEX; break;
       case 'o': upd->format = FMT_OCT; break;
+      case 'e': upd->format = FMT_ELF; break;
       default:
         fprintf(stderr, "%s: invalid file format '%s' in update specifier\n",
                 progname, p);
