2007-01-24 22:43:46 +00:00
|
|
|
/*
|
|
|
|
* avrdude - A Downloader/Uploader for AVR device programmers
|
|
|
|
* Copyright (C) 2000-2005 Brian S. Dean <bsd@bsdhome.com>
|
|
|
|
* Copyright (C) 2007 Joerg Wunsch
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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/>.
|
2007-01-24 22:43:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2022-01-10 13:51:48 +00:00
|
|
|
#include "ac_cfg.h"
|
2007-01-24 22:43:46 +00:00
|
|
|
#include "avrdude.h"
|
2014-05-19 10:01:59 +00:00
|
|
|
#include "libavrdude.h"
|
2007-01-24 22:43:46 +00:00
|
|
|
|
|
|
|
UPDATE * parse_op(char * s)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
char * p, * cp, c;
|
|
|
|
UPDATE * upd;
|
|
|
|
int i;
|
|
|
|
size_t fnlen;
|
|
|
|
|
|
|
|
upd = (UPDATE *)malloc(sizeof(UPDATE));
|
|
|
|
if (upd == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: out of memory\n", progname);
|
2007-01-24 22:43:46 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
p = s;
|
|
|
|
while ((i < (sizeof(buf)-1) && *p && (*p != ':')))
|
|
|
|
buf[i++] = *p++;
|
2009-04-28 18:35:14 +00:00
|
|
|
buf[i] = 0;
|
2007-01-24 22:43:46 +00:00
|
|
|
|
|
|
|
if (*p != ':') {
|
2012-04-25 16:32:23 +00:00
|
|
|
upd->memtype = NULL; /* default memtype, "flash", or "application" */
|
2007-01-24 22:43:46 +00:00
|
|
|
upd->op = DEVICE_WRITE;
|
|
|
|
upd->filename = (char *)malloc(strlen(buf) + 1);
|
2012-04-25 16:32:23 +00:00
|
|
|
if (upd->filename == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: out of memory\n", progname);
|
2012-04-25 16:32:23 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2007-01-24 22:43:46 +00:00
|
|
|
strcpy(upd->filename, buf);
|
|
|
|
upd->format = FMT_AUTO;
|
|
|
|
return upd;
|
|
|
|
}
|
|
|
|
|
|
|
|
upd->memtype = (char *)malloc(strlen(buf)+1);
|
|
|
|
if (upd->memtype == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: out of memory\n", progname);
|
2007-01-24 22:43:46 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
strcpy(upd->memtype, buf);
|
|
|
|
|
|
|
|
p++;
|
|
|
|
if (*p == 'r') {
|
|
|
|
upd->op = DEVICE_READ;
|
|
|
|
}
|
|
|
|
else if (*p == 'w') {
|
|
|
|
upd->op = DEVICE_WRITE;
|
|
|
|
}
|
|
|
|
else if (*p == 'v') {
|
|
|
|
upd->op = DEVICE_VERIFY;
|
|
|
|
}
|
|
|
|
else {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: invalid I/O mode '%c' in update specification\n",
|
2007-01-24 22:43:46 +00:00
|
|
|
progname, *p);
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, " allowed values are:\n"
|
2014-05-18 08:41:46 +00:00
|
|
|
" r = read device\n"
|
|
|
|
" w = write device\n"
|
|
|
|
" v = verify device\n");
|
2007-01-24 22:43:46 +00:00
|
|
|
free(upd->memtype);
|
|
|
|
free(upd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p != ':') {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: invalid update specification\n", progname);
|
2007-01-24 22:43:46 +00:00
|
|
|
free(upd->memtype);
|
|
|
|
free(upd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now, parse the filename component. Instead of looking for the
|
|
|
|
* leftmost possible colon delimiter, we look for the rightmost one.
|
|
|
|
* If we found one, we do have a trailing :format specifier, and
|
|
|
|
* process it. Otherwise, the remainder of the string is our file
|
|
|
|
* name component. That way, the file name itself is allowed to
|
|
|
|
* contain a colon itself (e. g. C:/some/file.hex), except the
|
|
|
|
* optional format specifier becomes mandatory then.
|
|
|
|
*/
|
|
|
|
cp = p;
|
|
|
|
p = strrchr(cp, ':');
|
|
|
|
if (p == NULL) {
|
2018-01-21 22:45:45 +00:00
|
|
|
// missing format, default to "AUTO" for write and verify,
|
|
|
|
// and to binary for read operations:
|
|
|
|
upd->format = upd->op == DEVICE_READ? FMT_RBIN: FMT_AUTO;
|
2007-01-24 22:43:46 +00:00
|
|
|
fnlen = strlen(cp);
|
|
|
|
upd->filename = (char *)malloc(fnlen + 1);
|
|
|
|
} else {
|
|
|
|
fnlen = p - cp;
|
|
|
|
upd->filename = (char *)malloc(fnlen +1);
|
|
|
|
c = *++p;
|
|
|
|
if (c && p[1])
|
|
|
|
/* More than one char - force failure below. */
|
|
|
|
c = '?';
|
|
|
|
switch (c) {
|
|
|
|
case 'a': upd->format = FMT_AUTO; break;
|
|
|
|
case 's': upd->format = FMT_SREC; break;
|
|
|
|
case 'i': upd->format = FMT_IHEX; break;
|
|
|
|
case 'r': upd->format = FMT_RBIN; break;
|
2012-02-02 16:52:45 +00:00
|
|
|
case 'e': upd->format = FMT_ELF; break;
|
2007-01-24 22:43:46 +00:00
|
|
|
case 'm': upd->format = FMT_IMM; break;
|
|
|
|
case 'b': upd->format = FMT_BIN; break;
|
|
|
|
case 'd': upd->format = FMT_DEC; break;
|
|
|
|
case 'h': upd->format = FMT_HEX; break;
|
|
|
|
case 'o': upd->format = FMT_OCT; break;
|
|
|
|
default:
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: invalid file format '%s' in update specifier\n",
|
2007-01-24 22:43:46 +00:00
|
|
|
progname, p);
|
|
|
|
free(upd->memtype);
|
|
|
|
free(upd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (upd->filename == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: out of memory\n", progname);
|
2007-01-24 22:43:46 +00:00
|
|
|
free(upd->memtype);
|
|
|
|
free(upd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(upd->filename, cp, fnlen);
|
|
|
|
upd->filename[fnlen] = 0;
|
|
|
|
|
|
|
|
return upd;
|
|
|
|
}
|
|
|
|
|
|
|
|
UPDATE * dup_update(UPDATE * upd)
|
|
|
|
{
|
|
|
|
UPDATE * u;
|
|
|
|
|
|
|
|
u = (UPDATE *)malloc(sizeof(UPDATE));
|
|
|
|
if (u == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: out of memory\n", progname);
|
2007-01-24 22:43:46 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(u, upd, sizeof(UPDATE));
|
|
|
|
|
2012-04-25 16:32:23 +00:00
|
|
|
if (upd->memtype != NULL)
|
|
|
|
u->memtype = strdup(upd->memtype);
|
|
|
|
else
|
|
|
|
u->memtype = NULL;
|
2007-01-24 22:43:46 +00:00
|
|
|
u->filename = strdup(upd->filename);
|
|
|
|
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
|
|
|
UPDATE * new_update(int op, char * memtype, int filefmt, char * filename)
|
|
|
|
{
|
|
|
|
UPDATE * u;
|
|
|
|
|
|
|
|
u = (UPDATE *)malloc(sizeof(UPDATE));
|
|
|
|
if (u == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: out of memory\n", progname);
|
2007-01-24 22:43:46 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
u->memtype = strdup(memtype);
|
|
|
|
u->filename = strdup(filename);
|
|
|
|
u->op = op;
|
|
|
|
u->format = filefmt;
|
|
|
|
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
2012-01-17 20:56:37 +00:00
|
|
|
void free_update(UPDATE * u)
|
|
|
|
{
|
|
|
|
if (u != NULL) {
|
|
|
|
if(u->memtype != NULL) {
|
|
|
|
free(u->memtype);
|
|
|
|
u->memtype = NULL;
|
|
|
|
}
|
|
|
|
if(u->filename != NULL) {
|
|
|
|
free(u->filename);
|
|
|
|
u->filename = NULL;
|
|
|
|
}
|
|
|
|
free(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-04 10:02:30 +00:00
|
|
|
int do_op(PROGRAMMER * pgm, struct avrpart * p, UPDATE * upd, enum updateflags flags)
|
2007-01-24 22:43:46 +00:00
|
|
|
{
|
|
|
|
struct avrpart * v;
|
|
|
|
AVRMEM * mem;
|
|
|
|
int size, vsize;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
mem = avr_locate_mem(p, upd->memtype);
|
|
|
|
if (mem == NULL) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "\"%s\" memory type not defined for part \"%s\"\n",
|
2007-01-24 22:43:46 +00:00
|
|
|
upd->memtype, p->desc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-02-10 21:07:51 +00:00
|
|
|
AVRMEM_ALIAS * alias_mem = avr_find_memalias(p, mem);
|
|
|
|
char alias_mem_desc[AVR_DESCLEN + 1] = "";
|
|
|
|
if(alias_mem) {
|
|
|
|
strcat(alias_mem_desc, "/");
|
|
|
|
strcat(alias_mem_desc, alias_mem->desc);
|
|
|
|
}
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
if (upd->op == DEVICE_READ) {
|
|
|
|
/*
|
|
|
|
* read out the specified device memory and write it to a file
|
|
|
|
*/
|
2021-11-07 16:22:00 +00:00
|
|
|
if (upd->format == FMT_IMM) {
|
|
|
|
avrdude_message(MSG_INFO,
|
|
|
|
"%s: Invalid file format 'immediate' for output\n",
|
|
|
|
progname, upd->filename);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-01-24 22:43:46 +00:00
|
|
|
if (quell_progress < 2) {
|
2022-02-10 21:07:51 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: reading %s%s memory:\n",
|
|
|
|
progname, mem->desc, alias_mem_desc);
|
2007-01-24 22:43:46 +00:00
|
|
|
}
|
|
|
|
report_progress(0,1,"Reading");
|
2012-05-04 10:02:30 +00:00
|
|
|
rc = avr_read(pgm, p, upd->memtype, 0);
|
2007-01-24 22:43:46 +00:00
|
|
|
if (rc < 0) {
|
2022-02-10 21:07:51 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: failed to read all of %s%s memory, rc=%d\n",
|
|
|
|
progname, mem->desc, alias_mem_desc, rc);
|
2007-01-24 22:43:46 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
report_progress(1,1,NULL);
|
|
|
|
size = rc;
|
|
|
|
|
|
|
|
if (quell_progress < 2) {
|
2015-04-09 19:50:30 +00:00
|
|
|
if (rc == 0)
|
|
|
|
avrdude_message(MSG_INFO, "%s: Flash is empty, resulting file has no contents.\n",
|
|
|
|
progname);
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: writing output file \"%s\"\n",
|
2014-05-18 08:41:46 +00:00
|
|
|
progname,
|
|
|
|
strcmp(upd->filename, "-")==0 ? "<stdout>" : upd->filename);
|
2007-01-24 22:43:46 +00:00
|
|
|
}
|
|
|
|
rc = fileio(FIO_WRITE, upd->filename, upd->format, p, upd->memtype, size);
|
|
|
|
if (rc < 0) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: write to file '%s' failed\n",
|
2007-01-24 22:43:46 +00:00
|
|
|
progname, upd->filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (upd->op == DEVICE_WRITE) {
|
|
|
|
/*
|
|
|
|
* write the selected device memory using data from a file; first
|
|
|
|
* read the data from the specified file
|
|
|
|
*/
|
|
|
|
if (quell_progress < 2) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: reading input file \"%s\"\n",
|
2014-05-18 08:41:46 +00:00
|
|
|
progname,
|
|
|
|
strcmp(upd->filename, "-")==0 ? "<stdin>" : upd->filename);
|
2007-01-24 22:43:46 +00:00
|
|
|
}
|
|
|
|
rc = fileio(FIO_READ, upd->filename, upd->format, p, upd->memtype, -1);
|
|
|
|
if (rc < 0) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: read from file '%s' failed\n",
|
2007-01-24 22:43:46 +00:00
|
|
|
progname, upd->filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
size = rc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write the buffer contents to the selected memory type
|
|
|
|
*/
|
|
|
|
if (quell_progress < 2) {
|
2022-02-10 21:07:51 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: writing %s%s (%d bytes):\n",
|
|
|
|
progname, mem->desc, alias_mem_desc, size);
|
2007-01-24 22:43:46 +00:00
|
|
|
}
|
|
|
|
|
2012-05-04 10:02:30 +00:00
|
|
|
if (!(flags & UF_NOWRITE)) {
|
2007-01-24 22:43:46 +00:00
|
|
|
report_progress(0,1,"Writing");
|
2012-05-04 10:02:30 +00:00
|
|
|
rc = avr_write(pgm, p, upd->memtype, size, (flags & UF_AUTO_ERASE) != 0);
|
2007-01-24 22:43:46 +00:00
|
|
|
report_progress(1,1,NULL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* test mode, don't actually write to the chip, output the buffer
|
|
|
|
* to stdout in intel hex instead
|
|
|
|
*/
|
|
|
|
rc = fileio(FIO_WRITE, "-", FMT_IHEX, p, upd->memtype, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc < 0) {
|
2022-02-10 21:07:51 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: failed to write %s%s memory, rc=%d\n",
|
|
|
|
progname, mem->desc, alias_mem_desc, rc);
|
2007-01-24 22:43:46 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vsize = rc;
|
|
|
|
|
|
|
|
if (quell_progress < 2) {
|
2022-02-10 21:07:51 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: %d bytes of %s%s written\n", progname,
|
|
|
|
vsize, mem->desc, alias_mem_desc);
|
2007-01-24 22:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (upd->op == DEVICE_VERIFY) {
|
|
|
|
/*
|
|
|
|
* verify that the in memory file (p->mem[AVR_M_FLASH|AVR_M_EEPROM])
|
|
|
|
* is the same as what is on the chip
|
|
|
|
*/
|
|
|
|
pgm->vfy_led(pgm, ON);
|
|
|
|
|
|
|
|
if (quell_progress < 2) {
|
2022-02-10 21:07:51 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: verifying %s%s memory against %s:\n",
|
|
|
|
progname, mem->desc, alias_mem_desc, upd->filename);
|
2007-01-24 22:43:46 +00:00
|
|
|
|
2022-02-10 21:07:51 +00:00
|
|
|
avrdude_message(MSG_NOTICE2, "%s: load data %s%s data from input file %s:\n",
|
|
|
|
progname, mem->desc, alias_mem_desc, upd->filename);
|
2007-01-24 22:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = fileio(FIO_READ, upd->filename, upd->format, p, upd->memtype, -1);
|
|
|
|
if (rc < 0) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: read from file '%s' failed\n",
|
2007-01-24 22:43:46 +00:00
|
|
|
progname, upd->filename);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-09-14 21:49:42 +00:00
|
|
|
v = avr_dup_part(p);
|
2007-01-24 22:43:46 +00:00
|
|
|
size = rc;
|
|
|
|
if (quell_progress < 2) {
|
2022-01-20 08:36:40 +00:00
|
|
|
avrdude_message(MSG_NOTICE2, "%s: input file %s contains %d bytes\n",
|
2007-01-24 22:43:46 +00:00
|
|
|
progname, upd->filename, size);
|
2022-02-10 21:07:51 +00:00
|
|
|
avrdude_message(MSG_NOTICE2, "%s: reading on-chip %s%s data:\n",
|
|
|
|
progname, mem->desc, alias_mem_desc);
|
2007-01-24 22:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
report_progress (0,1,"Reading");
|
2012-05-04 10:02:30 +00:00
|
|
|
rc = avr_read(pgm, p, upd->memtype, v);
|
2007-01-24 22:43:46 +00:00
|
|
|
if (rc < 0) {
|
2022-02-10 21:07:51 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: failed to read all of %s%s memory, rc=%d\n",
|
|
|
|
progname, mem->desc, alias_mem_desc, rc);
|
2007-01-24 22:43:46 +00:00
|
|
|
pgm->err_led(pgm, ON);
|
2020-03-14 22:34:45 +00:00
|
|
|
avr_free_part(v);
|
2007-01-24 22:43:46 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
report_progress (1,1,NULL);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (quell_progress < 2) {
|
2022-01-20 08:36:40 +00:00
|
|
|
avrdude_message(MSG_NOTICE2, "%s: verifying ...\n", progname);
|
2007-01-24 22:43:46 +00:00
|
|
|
}
|
|
|
|
rc = avr_verify(p, v, upd->memtype, size);
|
|
|
|
if (rc < 0) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: verification error; content mismatch\n",
|
2007-01-24 22:43:46 +00:00
|
|
|
progname);
|
|
|
|
pgm->err_led(pgm, ON);
|
2020-03-14 22:34:45 +00:00
|
|
|
avr_free_part(v);
|
2007-01-24 22:43:46 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (quell_progress < 2) {
|
2022-02-10 21:07:51 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: %d bytes of %s%s verified\n",
|
|
|
|
progname, rc, mem->desc, alias_mem_desc);
|
2007-01-24 22:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pgm->vfy_led(pgm, OFF);
|
2020-03-14 22:34:45 +00:00
|
|
|
avr_free_part(v);
|
2007-01-24 22:43:46 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: invalid update operation (%d) requested\n",
|
2007-01-24 22:43:46 +00:00
|
|
|
progname, upd->op);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|