2001-01-19 02:46:50 +00:00
|
|
|
/*
|
2003-02-08 04:17:25 +00:00
|
|
|
* avrdude - A Downloader/Uploader for AVR device programmers
|
2004-12-22 01:52:45 +00:00
|
|
|
* Copyright (C) 2000-2004 Brian S. Dean <bsd@bsdhome.com>
|
2001-01-19 02:46:50 +00:00
|
|
|
*
|
2003-02-06 19:08:33 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2001-01-19 02:46:50 +00:00
|
|
|
*
|
2003-02-06 19:08:33 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2001-01-19 02:46:50 +00:00
|
|
|
*
|
2003-02-06 19:08:33 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2012-11-20 14:03:50 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2001-01-19 02:46:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2003-02-14 20:34:03 +00:00
|
|
|
#include "ac_cfg.h"
|
|
|
|
|
2001-01-26 17:22:40 +00:00
|
|
|
#include <limits.h>
|
2001-01-19 02:46:50 +00:00
|
|
|
#include <stdio.h>
|
2001-01-26 17:22:40 +00:00
|
|
|
#include <stdlib.h>
|
2001-01-19 02:46:50 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
2012-02-06 19:41:36 +00:00
|
|
|
#include <stdint.h>
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2012-02-02 16:52:45 +00:00
|
|
|
#ifdef HAVE_LIBELF
|
2012-07-18 12:42:47 +00:00
|
|
|
#ifdef HAVE_LIBELF_H
|
2012-02-02 16:52:45 +00:00
|
|
|
#include <libelf.h>
|
2012-07-18 12:42:47 +00:00
|
|
|
#elif defined(HAVE_LIBELF_LIBELF_H)
|
|
|
|
#include <libelf/libelf.h>
|
|
|
|
#endif
|
2020-03-11 09:41:15 +00:00
|
|
|
#ifndef EM_AVR32
|
2022-01-07 10:31:16 +00:00
|
|
|
# define EM_AVR32 0x18ad /* unofficial */
|
2020-03-11 09:41:15 +00:00
|
|
|
#endif
|
2012-02-02 16:52:45 +00:00
|
|
|
#endif
|
|
|
|
|
2007-01-24 21:07:54 +00:00
|
|
|
#include "avrdude.h"
|
2014-05-19 10:01:59 +00:00
|
|
|
#include "libavrdude.h"
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
|
2001-01-26 17:22:40 +00:00
|
|
|
#define IHEX_MAXDATA 256
|
|
|
|
|
2001-09-19 17:04:25 +00:00
|
|
|
#define MAX_LINE_LEN 256 /* max line length for ASCII format input files */
|
|
|
|
|
|
|
|
|
2001-01-26 17:22:40 +00:00
|
|
|
struct ihexrec {
|
|
|
|
unsigned char reclen;
|
2003-02-20 03:53:49 +00:00
|
|
|
unsigned int loadofs;
|
2001-01-26 17:22:40 +00:00
|
|
|
unsigned char rectyp;
|
|
|
|
unsigned char data[IHEX_MAXDATA];
|
|
|
|
unsigned char cksum;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int b2ihex(const unsigned char *inbuf, int bufsize,
|
2001-01-19 02:46:50 +00:00
|
|
|
int recsize, int startaddr,
|
2022-10-29 09:45:27 +00:00
|
|
|
const char *outfile, FILE *outf, FILEFMT ffmt);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int ihex2b(const char *infile, FILE *inf,
|
|
|
|
const AVRMEM *mem, int bufsize, unsigned int fileoffset,
|
2022-07-16 22:40:36 +00:00
|
|
|
FILEFMT ffmt);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int b2srec(const unsigned char *inbuf, int bufsize,
|
|
|
|
int recsize, int startaddr,
|
|
|
|
const char *outfile, FILE *outf);
|
2003-02-20 03:53:49 +00:00
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int srec2b(const char *infile, FILE *inf,
|
|
|
|
const AVRMEM *mem, int bufsize, unsigned int fileoffset);
|
2003-02-20 03:53:49 +00:00
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int ihex_readrec(struct ihexrec *ihex, char *rec);
|
2003-02-20 03:53:49 +00:00
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int srec_readrec(struct ihexrec *srec, char *rec);
|
2003-02-20 03:53:49 +00:00
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int fileio_rbin(struct fioparms *fio,
|
|
|
|
const char *filename, FILE *f, const AVRMEM *mem, int size);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int fileio_ihex(struct fioparms *fio,
|
|
|
|
const char *filename, FILE *f, const AVRMEM *mem, int size,
|
|
|
|
FILEFMT ffmt);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int fileio_srec(struct fioparms *fio,
|
|
|
|
const char *filename, FILE *f, const AVRMEM *mem, int size);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2012-02-02 16:52:45 +00:00
|
|
|
#ifdef HAVE_LIBELF
|
2022-10-29 09:45:27 +00:00
|
|
|
static int elf2b(const char *infile, FILE *inf,
|
|
|
|
const AVRMEM *mem, const AVRPART *p,
|
2012-02-02 16:52:45 +00:00
|
|
|
int bufsize, unsigned int fileoffset);
|
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int fileio_elf(struct fioparms *fio,
|
|
|
|
const char *filename, FILE *f, const AVRMEM *mem,
|
|
|
|
const AVRPART *p, int size);
|
2012-02-02 16:52:45 +00:00
|
|
|
#endif
|
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int fileio_num(struct fioparms *fio,
|
|
|
|
const char *filename, FILE *f, const AVRMEM *mem, int size,
|
|
|
|
FILEFMT fmt);
|
2006-09-08 21:28:24 +00:00
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2022-08-05 16:38:59 +00:00
|
|
|
char * fileio_fmtstr(FILEFMT format)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
switch (format) {
|
|
|
|
case FMT_AUTO : return "auto-detect"; break;
|
|
|
|
case FMT_SREC : return "Motorola S-Record"; break;
|
|
|
|
case FMT_IHEX : return "Intel Hex"; break;
|
2022-07-16 22:40:36 +00:00
|
|
|
case FMT_IHXC : return "Intel Hex with comments"; break;
|
2001-01-19 02:46:50 +00:00
|
|
|
case FMT_RBIN : return "raw binary"; break;
|
2012-02-02 16:52:45 +00:00
|
|
|
case FMT_ELF : return "ELF"; break;
|
2001-01-19 02:46:50 +00:00
|
|
|
default : return "invalid format"; break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int b2ihex(const unsigned char *inbuf, int bufsize,
|
2001-10-14 02:53:21 +00:00
|
|
|
int recsize, int startaddr,
|
2022-10-29 09:45:27 +00:00
|
|
|
const char *outfile, FILE *outf, FILEFMT ffmt)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2022-10-29 09:45:27 +00:00
|
|
|
const unsigned char *buf;
|
2001-01-19 02:46:50 +00:00
|
|
|
unsigned int nextaddr;
|
2001-10-14 02:53:21 +00:00
|
|
|
int n, nbytes, n_64k;
|
2001-01-19 02:46:50 +00:00
|
|
|
int i;
|
|
|
|
unsigned char cksum;
|
|
|
|
|
|
|
|
if (recsize > 255) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("recsize=%d, must be < 256\n", recsize);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-10-14 02:53:21 +00:00
|
|
|
n_64k = 0;
|
2001-01-19 02:46:50 +00:00
|
|
|
nextaddr = startaddr;
|
2001-01-20 16:34:28 +00:00
|
|
|
buf = inbuf;
|
|
|
|
nbytes = 0;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
while (bufsize) {
|
|
|
|
n = recsize;
|
|
|
|
if (n > bufsize)
|
|
|
|
n = bufsize;
|
|
|
|
|
2001-10-14 02:53:21 +00:00
|
|
|
if ((nextaddr + n) > 0x10000)
|
|
|
|
n = 0x10000 - nextaddr;
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
if (n) {
|
|
|
|
cksum = 0;
|
2001-10-13 03:13:13 +00:00
|
|
|
fprintf(outf, ":%02X%04X00", n, nextaddr);
|
2001-01-19 02:46:50 +00:00
|
|
|
cksum += n + ((nextaddr >> 8) & 0x0ff) + (nextaddr & 0x0ff);
|
|
|
|
for (i=0; i<n; i++) {
|
2001-10-13 03:13:13 +00:00
|
|
|
fprintf(outf, "%02X", buf[i]);
|
2001-01-19 02:46:50 +00:00
|
|
|
cksum += buf[i];
|
|
|
|
}
|
|
|
|
cksum = -cksum;
|
2022-07-16 22:40:36 +00:00
|
|
|
fprintf(outf, "%02X", cksum);
|
|
|
|
|
|
|
|
if(ffmt == FMT_IHXC) { /* Print comment with address and ASCII dump */
|
|
|
|
for(i=n; i<recsize; i++)
|
|
|
|
fprintf(outf, " ");
|
|
|
|
fprintf(outf, " // %05x> ", n_64k*0x10000 + nextaddr);
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
unsigned char c = buf[i] & 0x7f;
|
|
|
|
/* Print space as _ so that line is one word */
|
|
|
|
putc(c == ' '? '_': c < ' ' || c == 0x7f? '.': c, outf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
putc('\n', outf);
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
nextaddr += n;
|
2001-01-20 16:34:28 +00:00
|
|
|
nbytes += n;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
2001-10-14 02:53:21 +00:00
|
|
|
if (nextaddr >= 0x10000) {
|
|
|
|
int lo, hi;
|
|
|
|
/* output an extended address record */
|
|
|
|
n_64k++;
|
|
|
|
lo = n_64k & 0xff;
|
|
|
|
hi = (n_64k >> 8) & 0xff;
|
|
|
|
cksum = 0;
|
|
|
|
fprintf(outf, ":02000004%02X%02X", hi, lo);
|
|
|
|
cksum += 2 + 0 + 4 + hi + lo;
|
|
|
|
cksum = -cksum;
|
|
|
|
fprintf(outf, "%02X\n", cksum);
|
|
|
|
nextaddr = 0;
|
|
|
|
}
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
/* advance to next 'recsize' bytes */
|
|
|
|
buf += n;
|
|
|
|
bufsize -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------
|
|
|
|
add the end of record data line
|
|
|
|
-----------------------------------------------------------------*/
|
|
|
|
cksum = 0;
|
|
|
|
n = 0;
|
|
|
|
nextaddr = 0;
|
2001-10-13 03:13:13 +00:00
|
|
|
fprintf(outf, ":%02X%04X01", n, nextaddr);
|
2001-01-20 15:38:55 +00:00
|
|
|
cksum += n + ((nextaddr >> 8) & 0x0ff) + (nextaddr & 0x0ff) + 1;
|
2001-01-19 02:46:50 +00:00
|
|
|
cksum = -cksum;
|
2001-10-13 03:13:13 +00:00
|
|
|
fprintf(outf, "%02X\n", cksum);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-01-20 16:34:28 +00:00
|
|
|
return nbytes;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int ihex_readrec(struct ihexrec * ihex, char * rec)
|
2001-01-26 17:22:40 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
char buf[8];
|
|
|
|
int offset, len;
|
|
|
|
char * e;
|
|
|
|
unsigned char cksum;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
len = strlen(rec);
|
|
|
|
offset = 1;
|
|
|
|
cksum = 0;
|
|
|
|
|
|
|
|
/* reclen */
|
|
|
|
if (offset + 2 > len)
|
|
|
|
return -1;
|
|
|
|
for (i=0; i<2; i++)
|
|
|
|
buf[i] = rec[offset++];
|
|
|
|
buf[i] = 0;
|
|
|
|
ihex->reclen = strtoul(buf, &e, 16);
|
|
|
|
if (e == buf || *e != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* load offset */
|
|
|
|
if (offset + 4 > len)
|
|
|
|
return -1;
|
|
|
|
for (i=0; i<4; i++)
|
|
|
|
buf[i] = rec[offset++];
|
|
|
|
buf[i] = 0;
|
|
|
|
ihex->loadofs = strtoul(buf, &e, 16);
|
|
|
|
if (e == buf || *e != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* record type */
|
|
|
|
if (offset + 2 > len)
|
|
|
|
return -1;
|
|
|
|
for (i=0; i<2; i++)
|
|
|
|
buf[i] = rec[offset++];
|
|
|
|
buf[i] = 0;
|
|
|
|
ihex->rectyp = strtoul(buf, &e, 16);
|
|
|
|
if (e == buf || *e != 0)
|
|
|
|
return -1;
|
|
|
|
|
2001-10-13 03:13:13 +00:00
|
|
|
cksum = ihex->reclen + ((ihex->loadofs >> 8) & 0x0ff) +
|
2001-01-26 17:22:40 +00:00
|
|
|
(ihex->loadofs & 0x0ff) + ihex->rectyp;
|
|
|
|
|
|
|
|
/* data */
|
|
|
|
for (j=0; j<ihex->reclen; j++) {
|
|
|
|
if (offset + 2 > len)
|
|
|
|
return -1;
|
|
|
|
for (i=0; i<2; i++)
|
|
|
|
buf[i] = rec[offset++];
|
|
|
|
buf[i] = 0;
|
|
|
|
ihex->data[j] = strtoul(buf, &e, 16);
|
|
|
|
if (e == buf || *e != 0)
|
|
|
|
return -1;
|
|
|
|
cksum += ihex->data[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cksum */
|
|
|
|
if (offset + 2 > len)
|
|
|
|
return -1;
|
|
|
|
for (i=0; i<2; i++)
|
|
|
|
buf[i] = rec[offset++];
|
|
|
|
buf[i] = 0;
|
|
|
|
ihex->cksum = strtoul(buf, &e, 16);
|
|
|
|
if (e == buf || *e != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
rc = -cksum & 0x000000ff;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-01-26 17:47:33 +00:00
|
|
|
/*
|
|
|
|
* Intel Hex to binary buffer
|
|
|
|
*
|
2022-01-07 10:31:16 +00:00
|
|
|
* Given an open file 'inf' which contains Intel Hex formatted data,
|
2001-01-26 17:47:33 +00:00
|
|
|
* parse the file and lay it out within the memory buffer pointed to
|
2022-01-07 10:31:16 +00:00
|
|
|
* by outbuf. The size of outbuf, 'bufsize' is honored; if data would
|
2001-01-26 17:47:33 +00:00
|
|
|
* fall outsize of the memory buffer outbuf, an error is generated.
|
|
|
|
*
|
|
|
|
* Return the maximum memory address within 'outbuf' that was written.
|
|
|
|
* If an error occurs, return -1.
|
|
|
|
*
|
|
|
|
* */
|
2022-10-29 09:45:27 +00:00
|
|
|
static int ihex2b(const char *infile, FILE *inf,
|
|
|
|
const AVRMEM *mem, int bufsize, unsigned int fileoffset,
|
2022-07-16 22:40:36 +00:00
|
|
|
FILEFMT ffmt)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2001-01-26 17:22:40 +00:00
|
|
|
char buffer [ MAX_LINE_LEN ];
|
2011-10-10 13:42:08 +00:00
|
|
|
unsigned int nextaddr, baseaddr, maxaddr;
|
2001-01-26 17:22:40 +00:00
|
|
|
int i;
|
2001-01-19 02:46:50 +00:00
|
|
|
int lineno;
|
2001-01-26 17:22:40 +00:00
|
|
|
int len;
|
|
|
|
struct ihexrec ihex;
|
|
|
|
int rc;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2011-10-10 13:42:08 +00:00
|
|
|
lineno = 0;
|
|
|
|
baseaddr = 0;
|
|
|
|
maxaddr = 0;
|
|
|
|
nextaddr = 0;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
while (fgets((char *)buffer,MAX_LINE_LEN,inf)!=NULL) {
|
|
|
|
lineno++;
|
2001-01-26 17:22:40 +00:00
|
|
|
len = strlen(buffer);
|
|
|
|
if (buffer[len-1] == '\n')
|
|
|
|
buffer[--len] = 0;
|
2001-01-19 02:46:50 +00:00
|
|
|
if (buffer[0] != ':')
|
|
|
|
continue;
|
2001-01-26 17:22:40 +00:00
|
|
|
rc = ihex_readrec(&ihex, buffer);
|
|
|
|
if (rc < 0) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("invalid record at line %d of %s\n", lineno, infile);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2001-01-26 17:22:40 +00:00
|
|
|
else if (rc != ihex.cksum) {
|
2022-07-16 22:40:36 +00:00
|
|
|
if(ffmt == FMT_IHEX) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("checksum mismatch at line %d of %s\n", lineno, infile);
|
|
|
|
imsg_error("checksum=0x%02x, computed checksum=0x%02x\n", ihex.cksum, rc);
|
2022-07-16 22:40:36 +00:00
|
|
|
return -1;
|
|
|
|
} else { /* Just warn with more permissive format FMT_IHXC */
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_notice("checksum mismatch at line %d of %s\n", lineno, infile);
|
|
|
|
imsg_notice("checksum=0x%02x, computed checksum=0x%02x\n", ihex.cksum, rc);
|
2022-07-16 22:40:36 +00:00
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
2001-01-26 17:22:40 +00:00
|
|
|
switch (ihex.rectyp) {
|
|
|
|
case 0: /* data record */
|
2011-10-10 13:42:08 +00:00
|
|
|
if (fileoffset != 0 && baseaddr < fileoffset) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("address 0x%04x out of range (below fileoffset 0x%x) at line %d of %s\n",
|
|
|
|
baseaddr, fileoffset, lineno, infile);
|
2011-10-10 13:42:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
nextaddr = ihex.loadofs + baseaddr - fileoffset;
|
2022-08-05 17:04:46 +00:00
|
|
|
if (nextaddr + ihex.reclen > (unsigned) bufsize) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("address 0x%04x out of range at line %d of %s\n",
|
|
|
|
nextaddr+ihex.reclen, lineno, infile);
|
2001-01-26 17:22:40 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (i=0; i<ihex.reclen; i++) {
|
2011-10-10 13:42:08 +00:00
|
|
|
mem->buf[nextaddr+i] = ihex.data[i];
|
|
|
|
mem->tags[nextaddr+i] = TAG_ALLOCATED;
|
2001-01-26 17:22:40 +00:00
|
|
|
}
|
2001-01-26 17:47:33 +00:00
|
|
|
if (nextaddr+ihex.reclen > maxaddr)
|
|
|
|
maxaddr = nextaddr+ihex.reclen;
|
2001-01-26 17:22:40 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* end of file record */
|
2011-10-10 13:42:08 +00:00
|
|
|
return maxaddr;
|
2001-01-26 17:22:40 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* extended segment address record */
|
2010-01-18 11:05:36 +00:00
|
|
|
baseaddr = (ihex.data[0] << 8 | ihex.data[1]) << 4;
|
2001-01-26 17:22:40 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: /* start segment address record */
|
|
|
|
/* we don't do anything with the start address */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4: /* extended linear address record */
|
2010-01-18 11:05:36 +00:00
|
|
|
baseaddr = (ihex.data[0] << 8 | ihex.data[1]) << 16;
|
2001-01-26 17:22:40 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 5: /* start linear address record */
|
|
|
|
/* we don't do anything with the start address */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("do not know how to deal with rectype=%d "
|
|
|
|
"at line %d of %s\n", ihex.rectyp, lineno, infile);
|
2001-01-26 17:22:40 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
2001-01-20 16:34:28 +00:00
|
|
|
} /* while */
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2013-09-02 18:18:15 +00:00
|
|
|
if (maxaddr == 0) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("no valid record found in Intel Hex file %s\n", infile);
|
2001-01-26 17:22:40 +00:00
|
|
|
|
2013-09-02 18:18:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_warning("no end of file record found for Intel Hex file %s\n", infile);
|
2013-09-02 18:18:15 +00:00
|
|
|
|
|
|
|
return maxaddr;
|
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int b2srec(const unsigned char *inbuf, int bufsize,
|
2003-02-20 03:53:49 +00:00
|
|
|
int recsize, int startaddr,
|
2022-10-29 09:45:27 +00:00
|
|
|
const char *outfile, FILE *outf)
|
2003-02-20 03:53:49 +00:00
|
|
|
{
|
2022-10-29 09:45:27 +00:00
|
|
|
const unsigned char *buf;
|
2003-05-22 00:46:59 +00:00
|
|
|
unsigned int nextaddr;
|
2003-02-20 03:53:49 +00:00
|
|
|
int n, nbytes, addr_width;
|
|
|
|
unsigned char cksum;
|
|
|
|
|
|
|
|
char * tmpl=0;
|
|
|
|
|
|
|
|
if (recsize > 255) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("recsize=%d, must be < 256\n", recsize);
|
2003-02-20 03:53:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2003-05-22 00:46:59 +00:00
|
|
|
|
|
|
|
nextaddr = startaddr;
|
2003-02-20 03:53:49 +00:00
|
|
|
buf = inbuf;
|
2003-05-22 00:46:59 +00:00
|
|
|
nbytes = 0;
|
2003-02-20 03:53:49 +00:00
|
|
|
|
|
|
|
addr_width = 0;
|
|
|
|
|
|
|
|
while (bufsize) {
|
|
|
|
|
|
|
|
n = recsize;
|
|
|
|
|
|
|
|
if (n > bufsize)
|
|
|
|
n = bufsize;
|
|
|
|
|
|
|
|
if (n) {
|
|
|
|
cksum = 0;
|
|
|
|
if (nextaddr + n <= 0xffff) {
|
|
|
|
addr_width = 2;
|
|
|
|
tmpl="S1%02X%04X";
|
|
|
|
}
|
|
|
|
else if (nextaddr + n <= 0xffffff) {
|
|
|
|
addr_width = 3;
|
|
|
|
tmpl="S2%02X%06X";
|
|
|
|
}
|
|
|
|
else if (nextaddr + n <= 0xffffffff) {
|
|
|
|
addr_width = 4;
|
|
|
|
tmpl="S3%02X%08X";
|
|
|
|
}
|
|
|
|
else {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("address=%d, out of range\n", nextaddr);
|
2003-02-20 03:53:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-05-18 08:41:46 +00:00
|
|
|
fprintf(outf, tmpl, n + addr_width + 1, nextaddr);
|
2003-02-20 03:53:49 +00:00
|
|
|
|
2003-05-22 00:46:59 +00:00
|
|
|
cksum += n + addr_width + 1;
|
2003-02-20 03:53:49 +00:00
|
|
|
|
2022-08-05 17:04:46 +00:00
|
|
|
for (int i=addr_width; i>0; i--)
|
2003-05-22 00:46:59 +00:00
|
|
|
cksum += (nextaddr >> (i-1) * 8) & 0xff;
|
2003-02-20 03:53:49 +00:00
|
|
|
|
2022-08-05 17:04:46 +00:00
|
|
|
for (unsigned i=nextaddr; i<nextaddr + n; i++) {
|
2003-05-22 00:46:59 +00:00
|
|
|
fprintf(outf, "%02X", buf[i]);
|
|
|
|
cksum += buf[i];
|
2003-02-20 03:53:49 +00:00
|
|
|
}
|
|
|
|
|
2003-05-22 00:46:59 +00:00
|
|
|
cksum = 0xff - cksum;
|
|
|
|
fprintf(outf, "%02X\n", cksum);
|
|
|
|
|
2003-02-20 03:53:49 +00:00
|
|
|
nextaddr += n;
|
2003-05-22 00:46:59 +00:00
|
|
|
nbytes +=n;
|
2003-02-20 03:53:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* advance to next 'recsize' bytes */
|
|
|
|
bufsize -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------
|
|
|
|
add the end of record data line
|
|
|
|
-----------------------------------------------------------------*/
|
|
|
|
cksum = 0;
|
|
|
|
n = 0;
|
|
|
|
nextaddr = 0;
|
|
|
|
|
|
|
|
if (startaddr <= 0xffff) {
|
|
|
|
addr_width = 2;
|
|
|
|
tmpl="S9%02X%04X";
|
|
|
|
}
|
|
|
|
else if (startaddr <= 0xffffff) {
|
|
|
|
addr_width = 3;
|
|
|
|
tmpl="S9%02X%06X";
|
|
|
|
}
|
2022-08-05 17:04:46 +00:00
|
|
|
else if ((unsigned) startaddr <= 0xffffffff) {
|
2003-02-20 03:53:49 +00:00
|
|
|
addr_width = 4;
|
|
|
|
tmpl="S9%02X%08X";
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(outf, tmpl, n + addr_width + 1, nextaddr);
|
|
|
|
|
|
|
|
cksum += n + addr_width +1;
|
2022-08-05 17:04:46 +00:00
|
|
|
for (int i=addr_width; i>0; i--)
|
2003-02-20 03:53:49 +00:00
|
|
|
cksum += (nextaddr >> (i - 1) * 8) & 0xff;
|
|
|
|
cksum = 0xff - cksum;
|
|
|
|
fprintf(outf, "%02X\n", cksum);
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int srec_readrec(struct ihexrec * srec, char * rec)
|
2003-02-20 03:53:49 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
char buf[8];
|
|
|
|
int offset, len, addr_width;
|
|
|
|
char * e;
|
|
|
|
unsigned char cksum;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
len = strlen(rec);
|
|
|
|
offset = 1;
|
|
|
|
cksum = 0;
|
|
|
|
addr_width = 2;
|
|
|
|
|
|
|
|
/* record type */
|
|
|
|
if (offset + 1 > len)
|
|
|
|
return -1;
|
|
|
|
srec->rectyp = rec[offset++];
|
|
|
|
if (srec->rectyp == 0x32 || srec->rectyp == 0x38)
|
2022-10-29 09:45:27 +00:00
|
|
|
addr_width = 3; /* S2,S8-record */
|
2003-02-20 03:53:49 +00:00
|
|
|
else if (srec->rectyp == 0x33 || srec->rectyp == 0x37)
|
2022-10-29 09:45:27 +00:00
|
|
|
addr_width = 4; /* S3,S7-record */
|
2003-02-20 03:53:49 +00:00
|
|
|
|
|
|
|
/* reclen */
|
|
|
|
if (offset + 2 > len)
|
|
|
|
return -1;
|
|
|
|
for (i=0; i<2; i++)
|
|
|
|
buf[i] = rec[offset++];
|
|
|
|
buf[i] = 0;
|
|
|
|
srec->reclen = strtoul(buf, &e, 16);
|
|
|
|
cksum += srec->reclen;
|
|
|
|
srec->reclen -= (addr_width+1);
|
|
|
|
if (e == buf || *e != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* load offset */
|
|
|
|
if (offset + addr_width > len)
|
|
|
|
return -1;
|
|
|
|
for (i=0; i<addr_width*2; i++)
|
|
|
|
buf[i] = rec[offset++];
|
|
|
|
buf[i] = 0;
|
|
|
|
srec->loadofs = strtoull(buf, &e, 16);
|
|
|
|
if (e == buf || *e != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i=addr_width; i>0; i--)
|
|
|
|
cksum += (srec->loadofs >> (i - 1) * 8) & 0xff;
|
|
|
|
|
|
|
|
/* data */
|
|
|
|
for (j=0; j<srec->reclen; j++) {
|
|
|
|
if (offset+2 > len)
|
|
|
|
return -1;
|
|
|
|
for (i=0; i<2; i++)
|
|
|
|
buf[i] = rec[offset++];
|
|
|
|
buf[i] = 0;
|
|
|
|
srec->data[j] = strtoul(buf, &e, 16);
|
|
|
|
if (e == buf || *e != 0)
|
|
|
|
return -1;
|
|
|
|
cksum += srec->data[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cksum */
|
|
|
|
if (offset + 2 > len)
|
|
|
|
return -1;
|
|
|
|
for (i=0; i<2; i++)
|
|
|
|
buf[i] = rec[offset++];
|
|
|
|
buf[i] = 0;
|
|
|
|
srec->cksum = strtoul(buf, &e, 16);
|
|
|
|
if (e == buf || *e != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
rc = 0xff - cksum;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int srec2b(const char *infile, FILE * inf,
|
|
|
|
const AVRMEM *mem, int bufsize, unsigned int fileoffset)
|
2003-02-20 03:53:49 +00:00
|
|
|
{
|
|
|
|
char buffer [ MAX_LINE_LEN ];
|
2011-10-10 13:42:08 +00:00
|
|
|
unsigned int nextaddr, maxaddr;
|
2003-02-20 03:53:49 +00:00
|
|
|
int i;
|
|
|
|
int lineno;
|
|
|
|
int len;
|
|
|
|
struct ihexrec srec;
|
|
|
|
int rc;
|
2022-08-05 17:04:46 +00:00
|
|
|
unsigned int reccount;
|
2003-02-20 03:53:49 +00:00
|
|
|
unsigned char datarec;
|
|
|
|
|
2022-10-17 14:44:55 +00:00
|
|
|
char * msg = "";
|
2003-02-20 03:53:49 +00:00
|
|
|
|
|
|
|
lineno = 0;
|
|
|
|
maxaddr = 0;
|
|
|
|
reccount = 0;
|
|
|
|
|
|
|
|
while (fgets((char *)buffer,MAX_LINE_LEN,inf)!=NULL) {
|
|
|
|
lineno++;
|
|
|
|
len = strlen(buffer);
|
|
|
|
if (buffer[len-1] == '\n')
|
|
|
|
buffer[--len] = 0;
|
|
|
|
if (buffer[0] != 0x53)
|
|
|
|
continue;
|
|
|
|
rc = srec_readrec(&srec, buffer);
|
|
|
|
|
|
|
|
if (rc < 0) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("invalid record at line %d of %s\n", lineno, infile);
|
2003-02-20 03:53:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (rc != srec.cksum) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("checksum mismatch at line %d of %s\n", lineno, infile);
|
|
|
|
imsg_error("checksum=0x%02x, computed checksum=0x%02x\n", srec.cksum, rc);
|
2003-02-20 03:53:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-05-18 08:41:46 +00:00
|
|
|
datarec=0;
|
2003-02-20 03:53:49 +00:00
|
|
|
switch (srec.rectyp) {
|
|
|
|
case 0x30: /* S0 - header record*/
|
|
|
|
/* skip */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x31: /* S1 - 16 bit address data record */
|
|
|
|
datarec=1;
|
2022-10-17 14:44:55 +00:00
|
|
|
msg="address 0x%04x out of range %sat line %d of %s\n";
|
2003-02-20 03:53:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x32: /* S2 - 24 bit address data record */
|
|
|
|
datarec=1;
|
2022-10-17 14:44:55 +00:00
|
|
|
msg="address 0x%06x out of range %sat line %d of %s\n";
|
2003-02-20 03:53:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x33: /* S3 - 32 bit address data record */
|
|
|
|
datarec=1;
|
2022-10-17 14:44:55 +00:00
|
|
|
msg="address 0x%08x out of range %sat line %d of %s\n";
|
2003-02-20 03:53:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x34: /* S4 - symbol record (LSI extension) */
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("not supported record at line %d of %s\n", lineno, infile);
|
2003-02-20 03:53:49 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
case 0x35: /* S5 - count of S1,S2 and S3 records previously tx'd */
|
|
|
|
if (srec.loadofs != reccount){
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("count of transmitted data records mismatch at line %d of %s\n", lineno, infile);
|
|
|
|
imsg_error("transmitted data records= %d, expected value= %d\n", reccount, srec.loadofs);
|
2003-02-20 03:53:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x37: /* S7 Record - end record for 32 bit address data */
|
|
|
|
case 0x38: /* S8 Record - end record for 24 bit address data */
|
|
|
|
case 0x39: /* S9 Record - end record for 16 bit address data */
|
|
|
|
return maxaddr;
|
|
|
|
|
|
|
|
default:
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("do not know how to deal with rectype S%d at line %d of %s\n",
|
|
|
|
srec.rectyp, lineno, infile);
|
2003-02-20 03:53:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (datarec == 1) {
|
2011-10-10 13:42:08 +00:00
|
|
|
nextaddr = srec.loadofs;
|
|
|
|
if (nextaddr < fileoffset) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error(msg, nextaddr, "(below fileoffset) ", lineno, infile);
|
2011-10-10 13:42:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
nextaddr -= fileoffset;
|
2022-08-05 17:04:46 +00:00
|
|
|
if (nextaddr + srec.reclen > (unsigned) bufsize) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error(msg, nextaddr+srec.reclen, "", lineno, infile);
|
2003-02-20 03:53:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-09-14 21:49:42 +00:00
|
|
|
for (i=0; i<srec.reclen; i++) {
|
|
|
|
mem->buf[nextaddr+i] = srec.data[i];
|
|
|
|
mem->tags[nextaddr+i] = TAG_ALLOCATED;
|
|
|
|
}
|
2003-02-20 03:53:49 +00:00
|
|
|
if (nextaddr+srec.reclen > maxaddr)
|
|
|
|
maxaddr = nextaddr+srec.reclen;
|
2014-05-18 08:41:46 +00:00
|
|
|
reccount++;
|
2003-02-20 03:53:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-18 08:41:46 +00:00
|
|
|
}
|
2003-02-20 03:53:49 +00:00
|
|
|
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_warning("no end of file record found for Motorola S-Records file %s\n", infile);
|
2003-02-20 03:53:49 +00:00
|
|
|
|
|
|
|
return maxaddr;
|
|
|
|
}
|
|
|
|
|
2012-02-02 16:52:45 +00:00
|
|
|
#ifdef HAVE_LIBELF
|
2012-04-18 15:47:57 +00:00
|
|
|
/*
|
|
|
|
* Determine whether the ELF file section pointed to by `sh' fits
|
|
|
|
* completely into the program header segment pointed to by `ph'.
|
|
|
|
*
|
|
|
|
* Assumes the section has been checked already before to actually
|
|
|
|
* contain data (SHF_ALLOC, SHT_PROGBITS, sh_size > 0).
|
|
|
|
*
|
|
|
|
* Sometimes, program header segments might be larger than the actual
|
|
|
|
* file sections. On VM architectures, this is used to allow mmapping
|
|
|
|
* the entire ELF file "as is" (including things like the program
|
|
|
|
* header table itself).
|
|
|
|
*/
|
|
|
|
static inline
|
|
|
|
int is_section_in_segment(Elf32_Shdr *sh, Elf32_Phdr *ph)
|
|
|
|
{
|
|
|
|
if (sh->sh_offset < ph->p_offset)
|
|
|
|
return 0;
|
|
|
|
if (sh->sh_offset + sh->sh_size > ph->p_offset + ph->p_filesz)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-02-02 16:52:45 +00:00
|
|
|
/*
|
|
|
|
* Return the ELF section descriptor that corresponds to program
|
|
|
|
* header `ph'. The program header is expected to be of p_type
|
|
|
|
* PT_LOAD, and to have a nonzero p_filesz. (PT_LOAD sections with a
|
|
|
|
* zero p_filesz are typically RAM sections that are not initialized
|
|
|
|
* by file data, e.g. ".bss".)
|
|
|
|
*/
|
|
|
|
static Elf_Scn *elf_get_scn(Elf *e, Elf32_Phdr *ph, Elf32_Shdr **shptr)
|
|
|
|
{
|
|
|
|
Elf_Scn *s = NULL;
|
|
|
|
|
|
|
|
while ((s = elf_nextscn(e, s)) != NULL) {
|
|
|
|
Elf32_Shdr *sh;
|
|
|
|
size_t ndx = elf_ndxscn(s);
|
|
|
|
if ((sh = elf32_getshdr(s)) == NULL) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("unable to read section #%u header: %s\n", (unsigned int)ndx, elf_errmsg(-1));
|
2012-02-02 16:52:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((sh->sh_flags & SHF_ALLOC) == 0 ||
|
|
|
|
sh->sh_type != SHT_PROGBITS)
|
|
|
|
/* we are only interested in PROGBITS, ALLOC sections */
|
|
|
|
continue;
|
2012-04-18 15:47:57 +00:00
|
|
|
if (sh->sh_size == 0)
|
|
|
|
/* we are not interested in empty sections */
|
|
|
|
continue;
|
|
|
|
if (is_section_in_segment(sh, ph)) {
|
2012-02-02 16:52:45 +00:00
|
|
|
/* yeah, we found it */
|
|
|
|
*shptr = sh;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("cannot find a matching section for program header entry @p_vaddr 0x%x\n", ph->p_vaddr);
|
2012-02-02 16:52:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int elf_mem_limits(const AVRMEM *mem, const AVRPART *p,
|
2012-02-02 16:52:45 +00:00
|
|
|
unsigned int *lowbound,
|
|
|
|
unsigned int *highbound,
|
|
|
|
unsigned int *fileoff)
|
|
|
|
{
|
|
|
|
int rv = 0;
|
|
|
|
|
2022-08-30 15:33:42 +00:00
|
|
|
if (p->prog_modes & PM_aWire) { // AVR32
|
2012-02-02 16:52:45 +00:00
|
|
|
if (strcmp(mem->desc, "flash") == 0) {
|
|
|
|
*lowbound = 0x80000000;
|
|
|
|
*highbound = 0xffffffff;
|
|
|
|
*fileoff = 0;
|
|
|
|
} else {
|
|
|
|
rv = -1;
|
|
|
|
}
|
|
|
|
} else {
|
2012-04-24 15:41:02 +00:00
|
|
|
if (strcmp(mem->desc, "flash") == 0 ||
|
|
|
|
strcmp(mem->desc, "boot") == 0 ||
|
|
|
|
strcmp(mem->desc, "application") == 0 ||
|
|
|
|
strcmp(mem->desc, "apptable") == 0) {
|
2012-02-02 16:52:45 +00:00
|
|
|
*lowbound = 0;
|
|
|
|
*highbound = 0x7ffff; /* max 8 MiB */
|
|
|
|
*fileoff = 0;
|
|
|
|
} else if (strcmp(mem->desc, "eeprom") == 0) {
|
|
|
|
*lowbound = 0x810000;
|
|
|
|
*highbound = 0x81ffff; /* max 64 KiB */
|
|
|
|
*fileoff = 0;
|
|
|
|
} else if (strcmp(mem->desc, "lfuse") == 0) {
|
|
|
|
*lowbound = 0x820000;
|
|
|
|
*highbound = 0x82ffff;
|
|
|
|
*fileoff = 0;
|
|
|
|
} else if (strcmp(mem->desc, "hfuse") == 0) {
|
|
|
|
*lowbound = 0x820000;
|
|
|
|
*highbound = 0x82ffff;
|
|
|
|
*fileoff = 1;
|
|
|
|
} else if (strcmp(mem->desc, "efuse") == 0) {
|
|
|
|
*lowbound = 0x820000;
|
|
|
|
*highbound = 0x82ffff;
|
|
|
|
*fileoff = 2;
|
|
|
|
} else if (strncmp(mem->desc, "fuse", 4) == 0 &&
|
|
|
|
(mem->desc[4] >= '0' && mem->desc[4] <= '9')) {
|
|
|
|
/* Xmega fuseN */
|
|
|
|
*lowbound = 0x820000;
|
|
|
|
*highbound = 0x82ffff;
|
|
|
|
*fileoff = mem->desc[4] - '0';
|
2014-10-15 20:01:12 +00:00
|
|
|
} else if (strncmp(mem->desc, "lock", 4) == 0) {
|
2012-02-02 16:52:45 +00:00
|
|
|
*lowbound = 0x830000;
|
|
|
|
*highbound = 0x83ffff;
|
|
|
|
*fileoff = 0;
|
|
|
|
} else {
|
|
|
|
rv = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int elf2b(const char *infile, FILE *inf,
|
|
|
|
const AVRMEM *mem, const AVRPART *p,
|
2012-02-02 16:52:45 +00:00
|
|
|
int bufsize, unsigned int fileoffset)
|
|
|
|
{
|
|
|
|
Elf *e;
|
|
|
|
int rv = -1;
|
|
|
|
unsigned int low, high, foff;
|
|
|
|
|
|
|
|
if (elf_mem_limits(mem, p, &low, &high, &foff) != 0) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("cannot handle %s memory region from ELF file\n", mem->desc);
|
2012-02-02 16:52:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-24 15:41:02 +00:00
|
|
|
/*
|
|
|
|
* The Xmega memory regions for "boot", "application", and
|
|
|
|
* "apptable" are actually sub-regions of "flash". Refine the
|
|
|
|
* applicable limits. This allows to select only the appropriate
|
|
|
|
* sections out of an ELF file that contains section data for more
|
|
|
|
* than one sub-segment.
|
|
|
|
*/
|
2022-08-30 15:33:42 +00:00
|
|
|
if ((p->prog_modes & PM_PDI) != 0 &&
|
2012-04-24 15:41:02 +00:00
|
|
|
(strcmp(mem->desc, "boot") == 0 ||
|
|
|
|
strcmp(mem->desc, "application") == 0 ||
|
|
|
|
strcmp(mem->desc, "apptable") == 0)) {
|
|
|
|
AVRMEM *flashmem = avr_locate_mem(p, "flash");
|
|
|
|
if (flashmem == NULL) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("no flash memory region found, cannot compute bounds of %s sub-region\n", mem->desc);
|
2012-04-24 15:41:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* The config file offsets are PDI offsets, rebase to 0. */
|
|
|
|
low = mem->offset - flashmem->offset;
|
|
|
|
high = low + mem->size - 1;
|
|
|
|
}
|
|
|
|
|
2012-02-02 16:52:45 +00:00
|
|
|
if (elf_version(EV_CURRENT) == EV_NONE) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("ELF library initialization failed: %s\n", elf_errmsg(-1));
|
2012-02-02 16:52:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((e = elf_begin(fileno(inf), ELF_C_READ, NULL)) == NULL) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("cannot open %s as an ELF file: %s\n", infile, elf_errmsg(-1));
|
2012-02-02 16:52:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (elf_kind(e) != ELF_K_ELF) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("cannot use %s as an ELF input file\n", infile);
|
2012-02-02 16:52:45 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t i, isize;
|
|
|
|
const char *id = elf_getident(e, &isize);
|
|
|
|
|
|
|
|
if (id == NULL) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("unable to read ident area of %s: %s\n", infile, elf_errmsg(-1));
|
2012-02-02 16:52:45 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *endianname;
|
|
|
|
unsigned char endianess;
|
2022-08-30 15:33:42 +00:00
|
|
|
if (p->prog_modes & PM_aWire) { // AVR32
|
2012-02-02 16:52:45 +00:00
|
|
|
endianess = ELFDATA2MSB;
|
|
|
|
endianname = "little";
|
|
|
|
} else {
|
|
|
|
endianess = ELFDATA2LSB;
|
|
|
|
endianname = "big";
|
|
|
|
}
|
|
|
|
if (id[EI_CLASS] != ELFCLASS32 ||
|
|
|
|
id[EI_DATA] != endianess) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("ELF file %s is not a 32-bit, %s-endian file that was expected\n",
|
|
|
|
infile, endianname);
|
2012-02-02 16:52:45 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
Elf32_Ehdr *eh;
|
|
|
|
if ((eh = elf32_getehdr(e)) == NULL) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("unable to read ehdr of %s: %s\n", infile, elf_errmsg(-1));
|
2012-02-02 16:52:45 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eh->e_type != ET_EXEC) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("ELF file %s is not an executable file\n", infile);
|
2012-02-02 16:52:45 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *mname;
|
|
|
|
uint16_t machine;
|
2022-08-30 15:33:42 +00:00
|
|
|
if (p->prog_modes & PM_aWire) {
|
2012-02-02 16:52:45 +00:00
|
|
|
machine = EM_AVR32;
|
|
|
|
mname = "AVR32";
|
|
|
|
} else {
|
|
|
|
machine = EM_AVR;
|
|
|
|
mname = "AVR";
|
|
|
|
}
|
|
|
|
if (eh->e_machine != machine) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("ELF file %s is not for machine %s\n", infile, mname);
|
2012-02-02 16:52:45 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (eh->e_phnum == 0xffff /* PN_XNUM */) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("ELF file %s uses extended program header numbers which are not expected\n", infile);
|
2012-02-02 16:52:45 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
Elf32_Phdr *ph;
|
|
|
|
if ((ph = elf32_getphdr(e)) == NULL) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("unable to read program header table of %s: %s\n", infile, elf_errmsg(-1));
|
2012-02-02 16:52:45 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2012-04-24 15:41:02 +00:00
|
|
|
size_t sndx;
|
2013-07-11 21:43:11 +00:00
|
|
|
if (elf_getshdrstrndx(e, &sndx) != 0) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("unable to obtain section name string table: %s\n", elf_errmsg(-1));
|
2012-04-24 15:41:02 +00:00
|
|
|
sndx = 0;
|
|
|
|
}
|
|
|
|
|
2012-02-02 16:52:45 +00:00
|
|
|
/*
|
|
|
|
* Walk the program header table, pick up entries that are of type
|
|
|
|
* PT_LOAD, and have a non-zero p_filesz.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < eh->e_phnum; i++) {
|
|
|
|
if (ph[i].p_type != PT_LOAD ||
|
|
|
|
ph[i].p_filesz == 0)
|
|
|
|
continue;
|
|
|
|
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_notice2("considering PT_LOAD program header entry #%d:\n"
|
|
|
|
" p_vaddr 0x%x, p_paddr 0x%x, p_filesz %d\n", (int) i, ph[i].p_vaddr, ph[i].p_paddr, ph[i].p_filesz);
|
2012-02-02 16:52:45 +00:00
|
|
|
|
|
|
|
Elf32_Shdr *sh;
|
|
|
|
Elf_Scn *s = elf_get_scn(e, ph + i, &sh);
|
|
|
|
if (s == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((sh->sh_flags & SHF_ALLOC) && sh->sh_size) {
|
2012-04-24 15:41:02 +00:00
|
|
|
const char *sname;
|
|
|
|
|
|
|
|
if (sndx != 0) {
|
|
|
|
sname = elf_strptr(e, sndx, sh->sh_name);
|
|
|
|
} else {
|
|
|
|
sname = "*unknown*";
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int lma;
|
|
|
|
lma = ph[i].p_paddr + sh->sh_offset - ph[i].p_offset;
|
|
|
|
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_notice2("found section %s, LMA 0x%x, sh_size %u\n", sname, lma, sh->sh_size);
|
2012-04-24 15:41:02 +00:00
|
|
|
|
|
|
|
if (lma >= low &&
|
|
|
|
lma + sh->sh_size < high) {
|
|
|
|
/* OK */
|
|
|
|
} else {
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_notice2(" => skipping, inappropriate for %s memory region\n", mem->desc);
|
2012-04-24 15:41:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* 1-byte sized memory regions are special: they are used for fuse
|
|
|
|
* bits, where multiple regions (in the config file) map to a
|
|
|
|
* single, larger region in the ELF file (e.g. "lfuse", "hfuse",
|
|
|
|
* and "efuse" all map to ".fuse"). We silently accept a larger
|
|
|
|
* ELF file region for these, and extract the actual byte to write
|
|
|
|
* from it, using the "foff" offset obtained above.
|
|
|
|
*/
|
2022-08-05 17:04:46 +00:00
|
|
|
if (mem->size != 1 && sh->sh_size > (unsigned) mem->size) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("section %s does not fit into %s memory:\n"
|
|
|
|
" 0x%x + %u > %u\n", sname, mem->desc, lma, sh->sh_size, mem->size);
|
2012-04-24 15:41:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-02-02 16:52:45 +00:00
|
|
|
Elf_Data *d = NULL;
|
|
|
|
while ((d = elf_getdata(s, d)) != NULL) {
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_notice2(" Data block: d_buf %p, d_off 0x%x, d_size %ld\n",
|
|
|
|
d->d_buf, (unsigned int)d->d_off, (long) d->d_size);
|
2012-02-02 16:52:45 +00:00
|
|
|
if (mem->size == 1) {
|
|
|
|
if (d->d_off != 0) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("unexpected data block at offset != 0\n");
|
2012-02-02 16:52:45 +00:00
|
|
|
} else if (foff >= d->d_size) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("ELF file section does not contain byte at offset %d\n", foff);
|
2012-02-02 16:52:45 +00:00
|
|
|
} else {
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_notice2(" Extracting one byte from file offset %d\n",
|
2014-06-13 20:07:40 +00:00
|
|
|
foff);
|
2012-02-02 16:52:45 +00:00
|
|
|
mem->buf[0] = ((unsigned char *)d->d_buf)[foff];
|
|
|
|
mem->tags[0] = TAG_ALLOCATED;
|
|
|
|
rv = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unsigned int idx;
|
|
|
|
|
2012-04-24 15:41:02 +00:00
|
|
|
idx = lma - low + d->d_off;
|
2012-02-02 16:52:45 +00:00
|
|
|
if ((int)(idx + d->d_size) > rv)
|
|
|
|
rv = idx + d->d_size;
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_debug(" Writing %ld bytes to mem offset 0x%x\n",
|
|
|
|
(long) d->d_size, idx);
|
2012-02-02 16:52:45 +00:00
|
|
|
memcpy(mem->buf + idx, d->d_buf, d->d_size);
|
|
|
|
memset(mem->tags + idx, TAG_ALLOCATED, d->d_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
(void)elf_end(e);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_LIBELF */
|
|
|
|
|
2006-09-08 21:28:24 +00:00
|
|
|
/*
|
|
|
|
* Simple itoa() implementation. Caller needs to allocate enough
|
|
|
|
* space in buf. Only positive integers are handled.
|
|
|
|
*/
|
|
|
|
static char *itoa_simple(int n, char *buf, int base)
|
|
|
|
{
|
|
|
|
div_t q;
|
|
|
|
char c, *cp, *cp2;
|
|
|
|
|
|
|
|
cp = buf;
|
|
|
|
/*
|
|
|
|
* Divide by base until the number disappeared, but ensure at least
|
|
|
|
* one digit will be emitted.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
q = div(n, base);
|
|
|
|
n = q.quot;
|
|
|
|
if (q.rem >= 10)
|
|
|
|
c = q.rem - 10 + 'a';
|
|
|
|
else
|
|
|
|
c = q.rem + '0';
|
|
|
|
*cp++ = c;
|
|
|
|
} while (q.quot != 0);
|
|
|
|
|
|
|
|
/* Terminate the string. */
|
|
|
|
*cp-- = '\0';
|
|
|
|
|
|
|
|
/* Now revert the result string. */
|
|
|
|
cp2 = buf;
|
|
|
|
while (cp > cp2) {
|
|
|
|
c = *cp;
|
|
|
|
*cp-- = *cp2;
|
|
|
|
*cp2++ = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int fileio_rbin(struct fioparms *fio,
|
|
|
|
const char *filename, FILE *f, const AVRMEM *mem, int size) {
|
2001-01-19 02:46:50 +00:00
|
|
|
int rc;
|
2011-09-14 21:49:42 +00:00
|
|
|
unsigned char *buf = mem->buf;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
switch (fio->op) {
|
|
|
|
case FIO_READ:
|
|
|
|
rc = fread(buf, 1, size, f);
|
2011-09-14 21:49:42 +00:00
|
|
|
if (rc > 0)
|
|
|
|
memset(mem->tags, TAG_ALLOCATED, rc);
|
2001-01-19 02:46:50 +00:00
|
|
|
break;
|
|
|
|
case FIO_WRITE:
|
|
|
|
rc = fwrite(buf, 1, size, f);
|
|
|
|
break;
|
|
|
|
default:
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("invalid fileio operation=%d\n", fio->op);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-01-20 16:34:28 +00:00
|
|
|
if (rc < 0 || (fio->op == FIO_WRITE && rc < size)) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_ext_error("%s error %s %s: %s; %s %d of the expected %d bytes\n",
|
|
|
|
fio->iodesc, fio->dir, filename, strerror(errno), fio->rw, rc, size);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int fileio_imm(struct fioparms *fio,
|
|
|
|
const char *fname, FILE *f, const AVRMEM *mem, int size)
|
2003-04-19 18:41:35 +00:00
|
|
|
{
|
2003-08-28 23:02:25 +00:00
|
|
|
int rc = 0;
|
2022-10-29 09:45:27 +00:00
|
|
|
char *e, *p, *filename;
|
2003-04-19 18:41:35 +00:00
|
|
|
unsigned long b;
|
|
|
|
int loc;
|
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
filename = cfg_strdup(__func__, fname);
|
|
|
|
|
2003-04-19 18:41:35 +00:00
|
|
|
switch (fio->op) {
|
|
|
|
case FIO_READ:
|
|
|
|
loc = 0;
|
|
|
|
p = strtok(filename, " ,");
|
|
|
|
while (p != NULL && loc < size) {
|
|
|
|
b = strtoul(p, &e, 0);
|
2022-10-29 09:45:27 +00:00
|
|
|
/* check for binary formatted (0b10101001) strings */
|
|
|
|
b = (strncmp (p, "0b", 2))?
|
|
|
|
strtoul (p, &e, 0):
|
|
|
|
strtoul (p + 2, &e, 2);
|
2003-04-19 18:41:35 +00:00
|
|
|
if (*e != 0) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("invalid byte value (%s) specified for immediate mode\n", p);
|
2022-10-29 09:45:27 +00:00
|
|
|
free(filename);
|
2003-04-19 18:41:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-09-14 21:49:42 +00:00
|
|
|
mem->buf[loc] = b;
|
|
|
|
mem->tags[loc++] = TAG_ALLOCATED;
|
2003-04-19 18:41:35 +00:00
|
|
|
p = strtok(NULL, " ,");
|
|
|
|
rc = loc;
|
|
|
|
}
|
|
|
|
break;
|
2022-07-23 20:47:38 +00:00
|
|
|
|
|
|
|
case FIO_WRITE:
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("invalid file format 'immediate' for output\n");
|
2022-10-29 09:45:27 +00:00
|
|
|
free(filename);
|
2022-07-23 20:47:38 +00:00
|
|
|
return -1;
|
|
|
|
|
2003-04-19 18:41:35 +00:00
|
|
|
default:
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("invalid operation=%d\n", fio->op);
|
2022-10-29 09:45:27 +00:00
|
|
|
free(filename);
|
2003-04-19 18:41:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc < 0 || (fio->op == FIO_WRITE && rc < size)) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_ext_error("%s error %s %s: %s; %s %d of the expected %d bytes\n",
|
|
|
|
fio->iodesc, fio->dir, filename, strerror(errno), fio->rw, rc, size);
|
2022-10-29 09:45:27 +00:00
|
|
|
free(filename);
|
2003-04-19 18:41:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
free(filename);
|
2003-04-19 18:41:35 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int fileio_ihex(struct fioparms *fio,
|
|
|
|
const char *filename, FILE *f, const AVRMEM *mem, int size,
|
|
|
|
FILEFMT ffmt)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
switch (fio->op) {
|
|
|
|
case FIO_WRITE:
|
2022-07-16 22:40:36 +00:00
|
|
|
rc = b2ihex(mem->buf, size, 32, fio->fileoffset, filename, f, ffmt);
|
2001-01-20 16:34:28 +00:00
|
|
|
if (rc < 0) {
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FIO_READ:
|
2022-07-16 22:40:36 +00:00
|
|
|
rc = ihex2b(filename, f, mem, size, fio->fileoffset, ffmt);
|
2001-01-20 16:34:28 +00:00
|
|
|
if (rc < 0)
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("invalid Intel Hex file I/O operation=%d\n", fio->op);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-01-20 16:34:28 +00:00
|
|
|
return rc;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int fileio_srec(struct fioparms *fio,
|
|
|
|
const char *filename, FILE *f, const AVRMEM *mem, int size)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2003-02-20 03:53:49 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
switch (fio->op) {
|
|
|
|
case FIO_WRITE:
|
2011-10-10 13:42:08 +00:00
|
|
|
rc = b2srec(mem->buf, size, 32, fio->fileoffset, filename, f);
|
2003-02-20 03:53:49 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FIO_READ:
|
2011-10-10 13:42:08 +00:00
|
|
|
rc = srec2b(filename, f, mem, size, fio->fileoffset);
|
2003-02-20 03:53:49 +00:00
|
|
|
if (rc < 0)
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("invalid Motorola S-Records file I/O operation=%d\n", fio->op);
|
2003-02-20 03:53:49 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-02 16:52:45 +00:00
|
|
|
#ifdef HAVE_LIBELF
|
2022-10-29 09:45:27 +00:00
|
|
|
static int fileio_elf(struct fioparms *fio,
|
|
|
|
const char *filename, FILE *f, const AVRMEM *mem,
|
|
|
|
const AVRPART *p, int size)
|
2012-02-02 16:52:45 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
switch (fio->op) {
|
|
|
|
case FIO_WRITE:
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("write operation not supported for ELF\n");
|
2012-02-02 16:52:45 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FIO_READ:
|
|
|
|
rc = elf2b(filename, f, mem, p, size, fio->fileoffset);
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
default:
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("invalid ELF file I/O operation=%d\n", fio->op);
|
2012-02-02 16:52:45 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
static int fileio_num(struct fioparms *fio,
|
|
|
|
const char *filename, FILE *f, const AVRMEM *mem, int size,
|
|
|
|
FILEFMT fmt)
|
2006-09-08 21:28:24 +00:00
|
|
|
{
|
|
|
|
const char *prefix;
|
2022-07-23 20:47:38 +00:00
|
|
|
const char *name;
|
2006-09-08 21:28:24 +00:00
|
|
|
char cbuf[20];
|
|
|
|
int base, i, num;
|
|
|
|
|
|
|
|
switch (fmt) {
|
|
|
|
case FMT_HEX:
|
2022-07-23 20:47:38 +00:00
|
|
|
name = "hex";
|
2006-09-08 21:28:24 +00:00
|
|
|
prefix = "0x";
|
|
|
|
base = 16;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
case FMT_DEC:
|
2022-07-23 20:47:38 +00:00
|
|
|
name = "decimal";
|
2006-09-08 21:28:24 +00:00
|
|
|
prefix = "";
|
|
|
|
base = 10;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FMT_OCT:
|
2022-07-23 20:47:38 +00:00
|
|
|
name = "octal";
|
2006-09-08 21:28:24 +00:00
|
|
|
prefix = "0";
|
|
|
|
base = 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FMT_BIN:
|
2022-07-23 20:47:38 +00:00
|
|
|
name = "binary";
|
2006-09-08 21:28:24 +00:00
|
|
|
prefix = "0b";
|
|
|
|
base = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (fio->op) {
|
|
|
|
case FIO_WRITE:
|
|
|
|
break;
|
2022-07-23 20:47:38 +00:00
|
|
|
|
|
|
|
case FIO_READ:
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("invalid file format '%s' for input\n", name);
|
2022-07-23 20:47:38 +00:00
|
|
|
return -1;
|
|
|
|
|
2006-09-08 21:28:24 +00:00
|
|
|
default:
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("invalid operation=%d\n", fio->op);
|
2006-09-08 21:28:24 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
if (i > 0) {
|
|
|
|
if (putc(',', f) == EOF)
|
2022-10-29 09:45:27 +00:00
|
|
|
goto writeerr;
|
2006-09-08 21:28:24 +00:00
|
|
|
}
|
2011-09-14 21:49:42 +00:00
|
|
|
num = (unsigned int)(mem->buf[i]);
|
2006-09-08 21:28:24 +00:00
|
|
|
/*
|
|
|
|
* For a base of 8 and a value < 8 to convert, don't write the
|
|
|
|
* prefix. The conversion will be indistinguishable from a
|
|
|
|
* decimal one then.
|
|
|
|
*/
|
|
|
|
if (prefix[0] != '\0' && !(base == 8 && num < 8)) {
|
|
|
|
if (fputs(prefix, f) == EOF)
|
2022-10-29 09:45:27 +00:00
|
|
|
goto writeerr;
|
2006-09-08 21:28:24 +00:00
|
|
|
}
|
|
|
|
itoa_simple(num, cbuf, base);
|
|
|
|
if (fputs(cbuf, f) == EOF)
|
|
|
|
goto writeerr;
|
|
|
|
}
|
|
|
|
if (putc('\n', f) == EOF)
|
|
|
|
goto writeerr;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
writeerr:
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_ext_error("unable to write to %s: %s\n", filename, strerror(errno));
|
2006-09-08 21:28:24 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
int fileio_setparms(int op, struct fioparms *fp, const AVRPART *p, const AVRMEM * m) {
|
2001-01-19 02:46:50 +00:00
|
|
|
fp->op = op;
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case FIO_READ:
|
|
|
|
fp->mode = "r";
|
|
|
|
fp->iodesc = "input";
|
|
|
|
fp->dir = "from";
|
|
|
|
fp->rw = "read";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FIO_WRITE:
|
|
|
|
fp->mode = "w";
|
|
|
|
fp->iodesc = "output";
|
|
|
|
fp->dir = "to";
|
|
|
|
fp->rw = "wrote";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("invalid I/O operation %d\n", op);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-10-10 13:42:08 +00:00
|
|
|
/*
|
|
|
|
* AVR32 devices maintain their load offset within the file itself,
|
|
|
|
* but AVRDUDE maintains all memory images 0-based.
|
|
|
|
*/
|
2022-08-30 15:33:42 +00:00
|
|
|
fp->fileoffset = p->prog_modes & PM_aWire? m->offset: 0;
|
2011-10-10 13:42:08 +00:00
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-08-05 16:38:59 +00:00
|
|
|
int fileio_fmt_autodetect(const char * fname)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
FILE * f;
|
|
|
|
unsigned char buf[MAX_LINE_LEN];
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
int found;
|
2012-02-02 16:52:45 +00:00
|
|
|
int first = 1;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2022-01-07 12:15:55 +00:00
|
|
|
#if defined(WIN32)
|
2001-01-19 02:46:50 +00:00
|
|
|
f = fopen(fname, "r");
|
2013-12-05 16:27:54 +00:00
|
|
|
#else
|
|
|
|
f = fopen(fname, "rb");
|
|
|
|
#endif
|
2001-01-19 02:46:50 +00:00
|
|
|
if (f == NULL) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_ext_error("unable to open %s: %s\n", fname, strerror(errno));
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets((char *)buf, MAX_LINE_LEN, f)!=NULL) {
|
2012-02-02 16:52:45 +00:00
|
|
|
/* check for ELF file */
|
|
|
|
if (first &&
|
|
|
|
(buf[0] == 0177 && buf[1] == 'E' &&
|
|
|
|
buf[2] == 'L' && buf[3] == 'F')) {
|
|
|
|
fclose(f);
|
|
|
|
return FMT_ELF;
|
|
|
|
}
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2009-02-19 10:49:55 +00:00
|
|
|
if (found) {
|
|
|
|
fclose(f);
|
2001-01-19 02:46:50 +00:00
|
|
|
return FMT_RBIN;
|
2009-02-19 10:49:55 +00:00
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
2009-02-19 10:49:55 +00:00
|
|
|
if (found) {
|
|
|
|
fclose(f);
|
2001-01-19 02:46:50 +00:00
|
|
|
return FMT_IHEX;
|
2009-02-19 10:49:55 +00:00
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
2009-02-19 10:49:55 +00:00
|
|
|
if (found) {
|
|
|
|
fclose(f);
|
2001-01-19 02:46:50 +00:00
|
|
|
return FMT_SREC;
|
2009-02-19 10:49:55 +00:00
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
2012-02-02 16:52:45 +00:00
|
|
|
|
|
|
|
first = 0;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
2009-02-19 10:49:55 +00:00
|
|
|
fclose(f);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-10-29 09:45:27 +00:00
|
|
|
int fileio(int oprwv, const char *filename, FILEFMT format,
|
|
|
|
const AVRPART *p, const char *memtype, int size)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2022-04-28 16:53:10 +00:00
|
|
|
int op, rc;
|
2001-01-19 02:46:50 +00:00
|
|
|
FILE * f;
|
2022-10-29 09:45:27 +00:00
|
|
|
const char *fname;
|
2001-01-19 02:46:50 +00:00
|
|
|
struct fioparms fio;
|
2001-11-21 02:46:55 +00:00
|
|
|
AVRMEM * mem;
|
2003-05-07 00:14:03 +00:00
|
|
|
int using_stdio;
|
2001-11-21 02:46:55 +00:00
|
|
|
|
2022-04-28 16:53:10 +00:00
|
|
|
op = oprwv == FIO_READ_FOR_VERIFY? FIO_READ: oprwv;
|
2001-11-21 02:46:55 +00:00
|
|
|
mem = avr_locate_mem(p, memtype);
|
|
|
|
if (mem == NULL) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("memory type %s not configured for device %s\n", memtype, p->desc);
|
2001-11-21 02:46:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2011-10-10 13:42:08 +00:00
|
|
|
rc = fileio_setparms(op, &fio, p, mem);
|
2001-01-19 02:46:50 +00:00
|
|
|
if (rc < 0)
|
|
|
|
return -1;
|
|
|
|
|
2022-08-05 17:04:46 +00:00
|
|
|
if (size < 0 || fio.op == FIO_READ)
|
2003-04-19 18:41:35 +00:00
|
|
|
size = mem->size;
|
|
|
|
|
|
|
|
if (fio.op == FIO_READ) {
|
|
|
|
/* 0xff fill unspecified memory */
|
2011-09-14 21:49:42 +00:00
|
|
|
memset(mem->buf, 0xff, size);
|
2003-04-19 18:41:35 +00:00
|
|
|
}
|
2011-09-14 21:49:42 +00:00
|
|
|
memset(mem->tags, 0, size);
|
2003-04-19 18:41:35 +00:00
|
|
|
|
2003-05-07 00:14:03 +00:00
|
|
|
using_stdio = 0;
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
if (strcmp(filename, "-")==0) {
|
|
|
|
if (fio.op == FIO_READ) {
|
|
|
|
fname = "<stdin>";
|
|
|
|
f = stdin;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fname = "<stdout>";
|
|
|
|
f = stdout;
|
|
|
|
}
|
2003-05-07 00:14:03 +00:00
|
|
|
using_stdio = 1;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
fname = filename;
|
2003-04-19 18:41:35 +00:00
|
|
|
f = NULL;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (format == FMT_AUTO) {
|
2014-07-16 20:02:01 +00:00
|
|
|
int format_detect;
|
|
|
|
|
2003-05-07 00:14:03 +00:00
|
|
|
if (using_stdio) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("cannot auto detect file format when using stdin/out\n");
|
|
|
|
imsg_error("please specify a file format and try again\n");
|
2013-09-11 09:21:37 +00:00
|
|
|
return -1;
|
2003-05-07 00:14:03 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 16:38:59 +00:00
|
|
|
format_detect = fileio_fmt_autodetect(fname);
|
2014-07-16 20:02:01 +00:00
|
|
|
if (format_detect < 0) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("cannot determine file format for %s, specify explicitly\n", fname);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-07-16 20:02:01 +00:00
|
|
|
format = format_detect;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2022-10-17 14:44:55 +00:00
|
|
|
if (quell_progress < 2)
|
|
|
|
pmsg_notice("%s file %s auto detected as %s\n",
|
|
|
|
fio.iodesc, fname, fileio_fmtstr(format));
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 12:15:55 +00:00
|
|
|
#if defined(WIN32)
|
2013-12-05 16:27:54 +00:00
|
|
|
/* Open Raw Binary and ELF format in binary mode on Windows.*/
|
|
|
|
if(format == FMT_RBIN || format == FMT_ELF)
|
2011-08-12 15:53:53 +00:00
|
|
|
{
|
|
|
|
if(fio.op == FIO_READ)
|
|
|
|
{
|
|
|
|
fio.mode = "rb";
|
|
|
|
}
|
|
|
|
if(fio.op == FIO_WRITE)
|
|
|
|
{
|
|
|
|
fio.mode = "wb";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-04-19 18:41:35 +00:00
|
|
|
if (format != FMT_IMM) {
|
2003-05-07 00:14:03 +00:00
|
|
|
if (!using_stdio) {
|
|
|
|
f = fopen(fname, fio.mode);
|
|
|
|
if (f == NULL) {
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_ext_error("cannot open %s file %s: %s\n", fio.iodesc, fname, strerror(errno));
|
2003-05-07 00:14:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2003-04-19 18:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
switch (format) {
|
|
|
|
case FMT_IHEX:
|
2022-07-16 22:40:36 +00:00
|
|
|
case FMT_IHXC:
|
|
|
|
rc = fileio_ihex(&fio, fname, f, mem, size, format);
|
2001-01-19 02:46:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FMT_SREC:
|
2011-09-14 21:49:42 +00:00
|
|
|
rc = fileio_srec(&fio, fname, f, mem, size);
|
2001-01-19 02:46:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FMT_RBIN:
|
2011-09-14 21:49:42 +00:00
|
|
|
rc = fileio_rbin(&fio, fname, f, mem, size);
|
2001-01-19 02:46:50 +00:00
|
|
|
break;
|
|
|
|
|
2012-02-02 16:52:45 +00:00
|
|
|
case FMT_ELF:
|
|
|
|
#ifdef HAVE_LIBELF
|
|
|
|
rc = fileio_elf(&fio, fname, f, mem, p, size);
|
|
|
|
#else
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("cannot handle ELF file %s, ELF file support was not compiled in\n", fname);
|
2012-02-02 16:52:45 +00:00
|
|
|
rc = -1;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
2003-04-19 18:41:35 +00:00
|
|
|
case FMT_IMM:
|
2011-09-14 21:49:42 +00:00
|
|
|
rc = fileio_imm(&fio, fname, f, mem, size);
|
2003-04-19 18:41:35 +00:00
|
|
|
break;
|
|
|
|
|
2006-09-08 21:28:24 +00:00
|
|
|
case FMT_HEX:
|
|
|
|
case FMT_DEC:
|
|
|
|
case FMT_OCT:
|
|
|
|
case FMT_BIN:
|
2011-09-14 21:49:42 +00:00
|
|
|
rc = fileio_num(&fio, fname, f, mem, size, format);
|
2006-09-08 21:28:24 +00:00
|
|
|
break;
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
default:
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("invalid %s file format: %d\n", fio.iodesc, format);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-04-28 16:53:10 +00:00
|
|
|
/* on reading flash other than for verify set the size to location of highest non-0xff byte */
|
|
|
|
if (rc > 0 && oprwv == FIO_READ) {
|
2022-04-15 19:48:46 +00:00
|
|
|
int hiaddr = avr_mem_hiaddr(mem);
|
2022-04-15 19:46:40 +00:00
|
|
|
|
2022-04-15 19:48:46 +00:00
|
|
|
if(hiaddr < rc) /* if trailing-0xff not disabled */
|
|
|
|
rc = hiaddr;
|
2003-05-22 02:33:17 +00:00
|
|
|
}
|
2022-04-15 19:48:46 +00:00
|
|
|
|
2009-03-22 21:28:46 +00:00
|
|
|
if (format != FMT_IMM && !using_stdio) {
|
2009-02-19 10:49:55 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
2010-01-18 20:40:15 +00:00
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|