Replace string arrays with const char * and allocated space (part 2)

This commit replaces fixed-string buffers in PROGRAMMER, AVRPART and AVRMEM
that are dealt with by the parser and grammar. Now, string assignments are
always to const char *, ie, these are read-only strings with arbitrary
length.

config_gram.y now only needs to consider one type of string assignment.

This commit also

  - Replaces the simple linear-search cache_string() function with faster
    hashed cache_string(). Either way, the returned value is likely to be
    shared, so should never be free()'d.

  - Duplicates hvupdi_support list in pgm_dup() and frees it in pgm_free()

  - Adds const qualifier to some function args in avrpart.c and pgm.c

  - Hardens some functions against being called with NULL pointers

  - Ensures _new() and _dup() functions for parts, programmers and memory
    return a suitable memory. Out of memory triggers exit in one of three
    functions, cfg_malloc(), cfg_realloc() and cfg_strdup(); there is
    rarely anything useful that AVRDUDE or, for that matter, any
    application compiled against libavrdude can do once you run out of
    memory as AVRDUDE/libavrdude rely heavily on allocation of memory.
This commit is contained in:
Stefan Rueger 2022-08-10 16:14:56 +01:00
parent 7375477f70
commit f4c5a8350d
No known key found for this signature in database
GPG Key ID: B0B4F1FD86B1EC55
13 changed files with 359 additions and 482 deletions

View File

@ -1,4 +1,3 @@
/* /*
* avrdude - A Downloader/Uploader for AVR device programmers * avrdude - A Downloader/Uploader for AVR device programmers
* Copyright (C) 2000-2004 Brian S. Dean <bsd@bsdhome.com> * Copyright (C) 2000-2004 Brian S. Dean <bsd@bsdhome.com>
@ -31,43 +30,22 @@
*** Elementary functions dealing with OPCODE structures *** Elementary functions dealing with OPCODE structures
***/ ***/
OPCODE * avr_new_opcode(void) OPCODE *avr_new_opcode(void) {
{ return (OPCODE *) cfg_malloc("avr_new_opcode()", sizeof(OPCODE));
OPCODE * m;
m = (OPCODE *)malloc(sizeof(*m));
if (m == NULL) {
avrdude_message(MSG_INFO, "avr_new_opcode(): out of memory\n");
exit(1);
} }
memset(m, 0, sizeof(*m)); static OPCODE *avr_dup_opcode(const OPCODE *op) {
if(op == NULL) // Caller wants NULL if op == NULL
return m;
}
static OPCODE * avr_dup_opcode(OPCODE * op)
{
OPCODE * m;
/* this makes life easier */
if (op == NULL) {
return NULL; return NULL;
}
m = (OPCODE *)malloc(sizeof(*m));
if (m == NULL) {
avrdude_message(MSG_INFO, "avr_dup_opcode(): out of memory\n");
exit(1);
}
OPCODE *m = (OPCODE *) cfg_malloc("avr_dup_opcode()", sizeof(*m));
memcpy(m, op, sizeof(*m)); memcpy(m, op, sizeof(*m));
return m; return m;
} }
void avr_free_opcode(OPCODE * op) void avr_free_opcode(OPCODE *op) {
{ if(op)
free(op); free(op);
} }
@ -268,11 +246,10 @@ int avr_set_input(OPCODE * op, unsigned char * cmd, unsigned char data)
/* /*
* avr_get_output() * avr_get_output()
* *
* Retreive output data bits from the command results based on the * Retrieve output data bits from the command results based on the
* opcode data. * opcode data.
*/ */
int avr_get_output(OPCODE * op, unsigned char * res, unsigned char * data) int avr_get_output(const OPCODE *op, const unsigned char *res, unsigned char *data) {
{
int i, j, bit; int i, j, bit;
unsigned char value; unsigned char value;
unsigned char mask; unsigned char mask;
@ -301,8 +278,7 @@ int avr_get_output(OPCODE * op, unsigned char * res, unsigned char * data)
* Calculate the byte number of the output data based on the * Calculate the byte number of the output data based on the
* opcode data. * opcode data.
*/ */
int avr_get_output_index(OPCODE * op) int avr_get_output_index(const OPCODE *op) {
{
int i, j; int i, j;
for (i=0; i<32; i++) { for (i=0; i<32; i++) {
@ -353,34 +329,17 @@ static char * bittype(int type)
*** Elementary functions dealing with AVRMEM structures *** Elementary functions dealing with AVRMEM structures
***/ ***/
AVRMEM * avr_new_memtype(void) AVRMEM *avr_new_memtype(void) {
{ AVRMEM *m = (AVRMEM *) cfg_malloc("avr_new_memtype()", sizeof(*m));
AVRMEM * m; m->desc = cache_string("");
m = (AVRMEM *)malloc(sizeof(*m));
if (m == NULL) {
avrdude_message(MSG_INFO, "avr_new_memtype(): out of memory\n");
exit(1);
}
memset(m, 0, sizeof(*m));
m->page_size = 1; // ensure not 0 m->page_size = 1; // ensure not 0
return m; return m;
} }
AVRMEM_ALIAS * avr_new_memalias(void) AVRMEM_ALIAS *avr_new_memalias(void) {
{ AVRMEM_ALIAS *m = (AVRMEM_ALIAS *) cfg_malloc("avr_new_memalias()", sizeof*m);
AVRMEM_ALIAS * m; m->desc = cache_string("");
m = (AVRMEM_ALIAS *)malloc(sizeof(*m));
if (m == NULL) {
avrdude_message(MSG_INFO, "avr_new_memalias(): out of memory\n");
exit(1);
}
memset(m, 0, sizeof(*m));
return m; return m;
} }
@ -389,92 +348,66 @@ AVRMEM_ALIAS * avr_new_memalias(void)
* Allocate and initialize memory buffers for each of the device's * Allocate and initialize memory buffers for each of the device's
* defined memory regions. * defined memory regions.
*/ */
int avr_initmem(AVRPART * p) int avr_initmem(const AVRPART *p) {
{ if(p == NULL || p->mem == NULL)
LNODEID ln; return -1;
AVRMEM * m;
for (ln=lfirst(p->mem); ln; ln=lnext(ln)) { for (LNODEID ln=lfirst(p->mem); ln; ln=lnext(ln)) {
m = ldata(ln); AVRMEM *m = ldata(ln);
m->buf = (unsigned char *) malloc(m->size); m->buf = (unsigned char *) cfg_malloc("avr_initmem()", m->size);
if (m->buf == NULL) { m->tags = (unsigned char *) cfg_malloc("avr_initmem()", m->size);
avrdude_message(MSG_INFO, "%s: can't alloc buffer for %s size of %d bytes\n",
progname, m->desc, m->size);
return -1;
}
m->tags = (unsigned char *) malloc(m->size);
if (m->tags == NULL) {
avrdude_message(MSG_INFO, "%s: can't alloc buffer for %s size of %d bytes\n",
progname, m->desc, m->size);
return -1;
}
} }
return 0; return 0;
} }
AVRMEM * avr_dup_mem(AVRMEM * m) AVRMEM *avr_dup_mem(const AVRMEM *m) {
{ AVRMEM *n = avr_new_memtype();
AVRMEM * n;
int i;
n = avr_new_memtype();
if(m) {
*n = *m; *n = *m;
if (m->buf != NULL) { if(m->buf) {
n->buf = (unsigned char *)malloc(n->size); n->buf = (unsigned char *) cfg_malloc("avr_dup_mem()", n->size);
if (n->buf == NULL) {
avrdude_message(MSG_INFO, "avr_dup_mem(): out of memory (memsize=%d)\n",
n->size);
exit(1);
}
memcpy(n->buf, m->buf, n->size); memcpy(n->buf, m->buf, n->size);
} }
if (m->tags != NULL) { if(m->tags) {
n->tags = (unsigned char *)malloc(n->size); n->tags = (unsigned char *) cfg_malloc("avr_dup_mem()", n->size);
if (n->tags == NULL) {
avrdude_message(MSG_INFO, "avr_dup_mem(): out of memory (memsize=%d)\n",
n->size);
exit(1);
}
memcpy(n->tags, m->tags, n->size); memcpy(n->tags, m->tags, n->size);
} }
for (i = 0; i < AVR_OP_MAX; i++) { for(int i = 0; i < AVR_OP_MAX; i++)
n->op[i] = avr_dup_opcode(n->op[i]); n->op[i] = avr_dup_opcode(n->op[i]);
} }
return n; return n;
} }
AVRMEM_ALIAS * avr_dup_memalias(AVRMEM_ALIAS * m) AVRMEM_ALIAS *avr_dup_memalias(const AVRMEM_ALIAS *m) {
{ AVRMEM_ALIAS *n = avr_new_memalias();
AVRMEM_ALIAS * n;
n = avr_new_memalias();
if(m)
*n = *m; *n = *m;
return n; return n;
} }
void avr_free_mem(AVRMEM * m) void avr_free_mem(AVRMEM * m) {
{ if(m == NULL)
if (m->buf != NULL) { return;
if(m->buf) {
free(m->buf); free(m->buf);
m->buf = NULL; m->buf = NULL;
} }
if (m->tags != NULL) { if(m->tags) {
free(m->tags); free(m->tags);
m->tags = NULL; m->tags = NULL;
} }
for(size_t i=0; i<sizeof(m->op)/sizeof(m->op[0]); i++) for(size_t i=0; i<sizeof(m->op)/sizeof(m->op[0]); i++) {
{ if(m->op[i]) {
if (m->op[i] != NULL)
{
avr_free_opcode(m->op[i]); avr_free_opcode(m->op[i]);
m->op[i] = NULL; m->op[i] = NULL;
} }
@ -482,13 +415,12 @@ void avr_free_mem(AVRMEM * m)
free(m); free(m);
} }
void avr_free_memalias(AVRMEM_ALIAS * m) void avr_free_memalias(AVRMEM_ALIAS *m) {
{ if(m)
free(m); free(m);
} }
AVRMEM_ALIAS * avr_locate_memalias(AVRPART * p, const char * desc) AVRMEM_ALIAS *avr_locate_memalias(const AVRPART *p, const char *desc) {
{
AVRMEM_ALIAS * m, * match; AVRMEM_ALIAS * m, * match;
LNODEID ln; LNODEID ln;
int matches; int matches;
@ -514,8 +446,7 @@ AVRMEM_ALIAS * avr_locate_memalias(AVRPART * p, const char * desc)
return NULL; return NULL;
} }
AVRMEM * avr_locate_mem_noalias(AVRPART * p, const char * desc) AVRMEM *avr_locate_mem_noalias(const AVRPART *p, const char *desc) {
{
AVRMEM * m, * match; AVRMEM * m, * match;
LNODEID ln; LNODEID ln;
int matches; int matches;
@ -542,8 +473,7 @@ AVRMEM * avr_locate_mem_noalias(AVRPART * p, const char * desc)
} }
AVRMEM * avr_locate_mem(AVRPART * p, const char * desc) AVRMEM *avr_locate_mem(const AVRPART *p, const char *desc) {
{
AVRMEM * m, * match; AVRMEM * m, * match;
AVRMEM_ALIAS * alias; AVRMEM_ALIAS * alias;
LNODEID ln; LNODEID ln;
@ -577,13 +507,10 @@ AVRMEM * avr_locate_mem(AVRPART * p, const char * desc)
return NULL; return NULL;
} }
AVRMEM_ALIAS * avr_find_memalias(AVRPART * p, AVRMEM * m_orig) AVRMEM_ALIAS *avr_find_memalias(const AVRPART *p, const AVRMEM *m_orig) {
{ if(p && p->mem_alias && m_orig)
AVRMEM_ALIAS * m; for(LNODEID ln=lfirst(p->mem_alias); ln; ln=lnext(ln)) {
LNODEID ln; AVRMEM_ALIAS *m = ldata(ln);
for (ln=lfirst(p->mem_alias); ln; ln=lnext(ln)) {
m = ldata(ln);
if(m->aliased_mem == m_orig) if(m->aliased_mem == m_orig)
return m; return m;
} }
@ -592,9 +519,8 @@ AVRMEM_ALIAS * avr_find_memalias(AVRPART * p, AVRMEM * m_orig)
} }
void avr_mem_display(const char * prefix, FILE * f, AVRMEM * m, AVRPART * p, void avr_mem_display(const char *prefix, FILE *f, const AVRMEM *m,
int type, int verbose) const AVRPART *p, int verbose) {
{
static unsigned int prev_mem_offset; static unsigned int prev_mem_offset;
static int prev_mem_size; static int prev_mem_size;
int i, j; int i, j;
@ -623,7 +549,7 @@ void avr_mem_display(const char * prefix, FILE * f, AVRMEM * m, AVRPART * p,
AVRMEM_ALIAS *ap = avr_find_memalias(p, m); AVRMEM_ALIAS *ap = avr_find_memalias(p, m);
/* Show alias if the current and the next memory section has the same offset /* Show alias if the current and the next memory section has the same offset
and size, we're not out of band and a family_id is present */ and size, we're not out of band and a family_id is present */
char * mem_desc_alias = ap? ap->desc: ""; const char *mem_desc_alias = ap? ap->desc: "";
fprintf(f, fprintf(f,
"%s%-11s %-8s %4d %5d %5d %4d %-6s %6d %4d %6d %5d %5d 0x%02x 0x%02x\n", "%s%-11s %-8s %4d %5d %5d %4d %-6s %6d %4d %6d %5d %5d 0x%02x 0x%02x\n",
prefix, prefix,
@ -669,73 +595,62 @@ void avr_mem_display(const char * prefix, FILE * f, AVRMEM * m, AVRPART * p,
* Elementary functions dealing with AVRPART structures * Elementary functions dealing with AVRPART structures
*/ */
AVRPART * avr_new_part(void) AVRPART *avr_new_part(void) {
{ AVRPART *p = (AVRPART *) cfg_malloc("avr_new_part()", sizeof(AVRPART));
AVRPART * p;
const char *nulp = cache_string(""); const char *nulp = cache_string("");
p = (AVRPART *)malloc(sizeof(AVRPART));
if (p == NULL) {
avrdude_message(MSG_INFO, "new_part(): out of memory\n");
exit(1);
}
memset(p, 0, sizeof(*p)); memset(p, 0, sizeof(*p));
p->id[0] = 0; // Initialise const char * and LISTID entities
p->desc[0] = 0; p->desc = nulp;
p->id = nulp;
p->parent_id = nulp;
p->family_id = nulp;
p->config_file = nulp;
p->mem = lcreat(NULL, 0);
p->mem_alias = lcreat(NULL, 0);
// Default values
p->hvupdi_variant = -1;
memset(p->signature, 0xFF, 3);
p->reset_disposition = RESET_DEDICATED; p->reset_disposition = RESET_DEDICATED;
p->retry_pulse = PIN_AVR_SCK; p->retry_pulse = PIN_AVR_SCK;
p->flags = AVRPART_SERIALOK | AVRPART_PARALLELOK | AVRPART_ENABLEPAGEPROGRAMMING; p->flags = AVRPART_SERIALOK | AVRPART_PARALLELOK | AVRPART_ENABLEPAGEPROGRAMMING;
p->parent_id = nulp;
p->config_file = nulp;
p->lineno = 0;
memset(p->signature, 0xFF, 3);
p->ctl_stack_type = CTL_STACK_NONE; p->ctl_stack_type = CTL_STACK_NONE;
p->ocdrev = -1; p->ocdrev = -1;
p->hvupdi_variant = -1; p->lineno = 0;
p->mem = lcreat(NULL, 0);
p->mem_alias = lcreat(NULL, 0);
return p; return p;
} }
AVRPART * avr_dup_part(AVRPART * d) AVRPART *avr_dup_part(const AVRPART *d) {
{ AVRPART *p = avr_new_part();
AVRPART * p;
LISTID save, save2;
LNODEID ln, ln2;
int i;
p = avr_new_part();
save = p->mem;
save2 = p->mem_alias;
if(d) {
*p = *d; *p = *d;
p->mem = save; // Duplicate the memory and alias chains
p->mem_alias = save2; p->mem = lcreat(NULL, 0);
for (ln=lfirst(d->mem); ln; ln=lnext(ln)) { p->mem_alias = lcreat(NULL, 0);
for(LNODEID ln=lfirst(d->mem); ln; ln=lnext(ln)) {
AVRMEM *m = ldata(ln); AVRMEM *m = ldata(ln);
AVRMEM *m2 = avr_dup_mem(m); AVRMEM *m2 = avr_dup_mem(m);
ladd(p->mem, m2); ladd(p->mem, m2);
// see if there is any alias for it // See if there is any alias for it
for (ln2=lfirst(d->mem_alias); ln2; ln2=lnext(ln2)) { for(LNODEID ln2=lfirst(d->mem_alias); ln2; ln2=lnext(ln2)) {
AVRMEM_ALIAS *a = ldata(ln2); AVRMEM_ALIAS *a = ldata(ln2);
if (a->aliased_mem == m) { if (a->aliased_mem == m) {
// yes, duplicate it // Yes, duplicate it, adjust the pointer and add to new list
AVRMEM_ALIAS *a2 = avr_dup_memalias(a); AVRMEM_ALIAS *a2 = avr_dup_memalias(a);
// ... adjust the pointer ...
a2->aliased_mem = m2; a2->aliased_mem = m2;
// ... and add to new list
ladd(p->mem_alias, a2); ladd(p->mem_alias, a2);
} }
} }
} }
for (i = 0; i < AVR_OP_MAX; i++) { for(int i = 0; i < AVR_OP_MAX; i++)
p->op[i] = avr_dup_opcode(p->op[i]); p->op[i] = avr_dup_opcode(p->op[i]);
} }
@ -758,37 +673,27 @@ void avr_free_part(AVRPART * d)
free(d); free(d);
} }
AVRPART * locate_part(LISTID parts, const char * partdesc) AVRPART *locate_part(const LISTID parts, const char *partdesc) {
{
LNODEID ln1;
AVRPART * p = NULL; AVRPART * p = NULL;
int found; int found = 0;
if(!parts || !partdesc) if(!parts || !partdesc)
return NULL; return NULL;
found = 0; for (LNODEID ln1=lfirst(parts); ln1 && !found; ln1=lnext(ln1)) {
for (ln1=lfirst(parts); ln1 && !found; ln1=lnext(ln1)) {
p = ldata(ln1); p = ldata(ln1);
if ((strcasecmp(partdesc, p->id) == 0) || if ((strcasecmp(partdesc, p->id) == 0) ||
(strcasecmp(partdesc, p->desc) == 0)) (strcasecmp(partdesc, p->desc) == 0))
found = 1; found = 1;
} }
if (found) return found? p: NULL;
return p;
return NULL;
} }
AVRPART * locate_part_by_avr910_devcode(LISTID parts, int devcode) AVRPART *locate_part_by_avr910_devcode(const LISTID parts, int devcode) {
{ if(parts)
LNODEID ln1; for (LNODEID ln1=lfirst(parts); ln1; ln1=lnext(ln1)) {
AVRPART * p = NULL; AVRPART * p = ldata(ln1);
for (ln1=lfirst(parts); ln1; ln1=lnext(ln1)) {
p = ldata(ln1);
if (p->avr910_devcode == devcode) if (p->avr910_devcode == devcode)
return p; return p;
} }
@ -796,23 +701,17 @@ AVRPART * locate_part_by_avr910_devcode(LISTID parts, int devcode)
return NULL; return NULL;
} }
AVRPART * locate_part_by_signature(LISTID parts, unsigned char * sig, AVRPART *locate_part_by_signature(const LISTID parts, unsigned char *sig, int sigsize) {
int sigsize) if(parts && sigsize == 3)
{ for(LNODEID ln1=lfirst(parts); ln1; ln1=lnext(ln1)) {
LNODEID ln1; AVRPART *p = ldata(ln1);
AVRPART * p = NULL;
int i; int i;
if (sigsize == 3) {
for (ln1=lfirst(parts); ln1; ln1=lnext(ln1)) {
p = ldata(ln1);
for(i=0; i<3; i++) for(i=0; i<3; i++)
if(p->signature[i] != sig[i]) if(p->signature[i] != sig[i])
break; break;
if(i == 3) if(i == 3)
return p; return p;
} }
}
return NULL; return NULL;
} }
@ -841,12 +740,11 @@ void walk_avrparts(LISTID avrparts, walk_avrparts_cb cb, void *cookie)
/* /*
* Compare function to sort the list of programmers * Compare function to sort the list of programmers
*/ */
static int sort_avrparts_compare(AVRPART * p1,AVRPART * p2) static int sort_avrparts_compare(const AVRPART *p1, const AVRPART *p2) {
{ if(p1 == NULL || p1->desc == NULL || p2 == NULL || p2->desc == NULL)
if(p1 == NULL || p2 == NULL) {
return 0; return 0;
}
return strncasecmp(p1->desc,p2->desc,AVR_DESCLEN); return strcasecmp(p1->desc, p2->desc);
} }
/* /*
@ -868,9 +766,7 @@ static char * reset_disp_str(int r)
} }
void avr_display(FILE * f, AVRPART * p, const char * prefix, int verbose) void avr_display(FILE *f, const AVRPART *p, const char *prefix, int verbose) {
{
int i;
char * buf; char * buf;
const char * px; const char * px;
LNODEID ln; LNODEID ln;
@ -905,23 +801,17 @@ void avr_display(FILE * f, AVRPART * p, const char * prefix, int verbose)
fprintf( f, "%sMemory Detail :\n\n", prefix); fprintf( f, "%sMemory Detail :\n\n", prefix);
px = prefix; px = prefix;
i = strlen(prefix) + 5; buf = (char *)cfg_malloc("avr_display()", strlen(prefix) + 5);
buf = (char *)malloc(i);
if (buf == NULL) {
/* ugh, this is not important enough to bail, just ignore it */
}
else {
strcpy(buf, prefix); strcpy(buf, prefix);
strcat(buf, " "); strcat(buf, " ");
px = buf; px = buf;
}
if (verbose <= 2) { if (verbose <= 2)
avr_mem_display(px, f, NULL, p, 0, verbose); avr_mem_display(px, f, NULL, p, verbose);
}
for (ln=lfirst(p->mem); ln; ln=lnext(ln)) { for (ln=lfirst(p->mem); ln; ln=lnext(ln)) {
m = ldata(ln); m = ldata(ln);
avr_mem_display(px, f, m, p, i, verbose); avr_mem_display(px, f, m, p, verbose);
} }
if (buf) if (buf)

View File

@ -83,13 +83,24 @@ int init_config(void)
void *cfg_malloc(const char *funcname, size_t n) { void *cfg_malloc(const char *funcname, size_t n) {
void *ret = malloc(n); void *ret = malloc(n);
if(!ret) { if(!ret) {
avrdude_message(MSG_INFO, "%s: out of memory in %s\n", progname, funcname); avrdude_message(MSG_INFO, "%s: out of memory in %s (needed %lu bytes)\n", progname, funcname, (unsigned long) n);
exit(1); exit(1);
} }
memset(ret, 0, n); memset(ret, 0, n);
return ret; return ret;
} }
void *cfg_realloc(const char *funcname, void *p, size_t n) {
void *ret;
if(!(ret = p? realloc(p, n): calloc(1, n))) {
avrdude_message(MSG_INFO, "%s: out of memory in %s (needed %lu bytes)\n", progname, funcname, (unsigned long) n);
exit(1);
}
return ret;
}
char *cfg_strdup(const char *funcname, const char *s) { char *cfg_strdup(const char *funcname, const char *s) {
char *ret = strdup(s); char *ret = strdup(s);
@ -323,35 +334,45 @@ int read_config(const char * file)
} }
// Linear-search cache for a few often-referenced strings // Adapted version of a neat empirical hash function from comp.lang.c by Daniel Bernstein
const char *cache_string(const char *file) { unsigned strhash(const char *str) {
static char **fnames; unsigned c, hash = 5381, n = 0;
static int n=0;
if(!file) while((c = (unsigned char) *str++) && n++ < 20)
return NULL; hash = 33*hash ^ c;
// Exists in cache? return hash;
for(int i=0; i<n; i++)
if(strcmp(fnames[i], file) == 0)
return fnames[i];
// Expand cache?
if(n%128 == 0) {
if(!(fnames = realloc(fnames, (n+128)*sizeof*fnames))) {
yyerror("cache_string(): out of memory");
return NULL;
}
} }
fnames[n] = cfg_strdup("cache_string()", file);
return fnames[n++]; static char **hstrings[1<<12];
// Return a copy of the argument as hashed string
const char *cache_string(const char *p) {
int h, k;
char **hs;
if(!p)
p = "(NULL)";
h = strhash(p) % (sizeof hstrings/sizeof*hstrings);
if(!(hs=hstrings[h]))
hs = hstrings[h] = (char **) cfg_realloc("cache_string()", NULL, (16+1)*sizeof**hstrings);
for(k=0; hs[k]; k++)
if(*p == *hs[k] && !strcmp(p, hs[k]))
return hs[k];
if(k && k%16 == 0)
hstrings[h] = (char **) cfg_realloc("cache_string()", hstrings[h], (k+16+1)*sizeof**hstrings);
hstrings[h][k+1]=NULL;
return hstrings[h][k] = cfg_strdup("cache_string()", p);
} }
// Captures comments during parsing // Captures comments during parsing
int capture_comment_char(int c) { void capture_comment_str(const char *p) {
return c;
} }
@ -426,6 +447,12 @@ unsigned char *cfg_unescapeu(unsigned char *d, const unsigned char *s) {
switch (*s) { switch (*s) {
case '\\': case '\\':
switch (*++s) { switch (*++s) {
case '\n': // String continuation over new line
#if '\n' != '\r'
case '\r':
#endif
--d;
break;
case 'n': case 'n':
*d = '\n'; *d = '\n';
break; break;

View File

@ -94,7 +94,7 @@ void print_token(TOKEN *tkn);
void pyytext(void); void pyytext(void);
int capture_comment_char(int c); void capture_comment_str(const char *str);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -308,10 +308,6 @@ prog_def :
prog_decl : prog_decl :
K_PROGRAMMER K_PROGRAMMER
{ current_prog = pgm_new(); { current_prog = pgm_new();
if (current_prog == NULL) {
yyerror("could not create pgm instance");
YYABORT;
}
current_prog->config_file = cache_string(cfg_infile); current_prog->config_file = cache_string(cfg_infile);
current_prog->lineno = cfg_lineno; current_prog->lineno = cfg_lineno;
} }
@ -325,11 +321,6 @@ prog_decl :
YYABORT; YYABORT;
} }
current_prog = pgm_dup(pgm); current_prog = pgm_dup(pgm);
if (current_prog == NULL) {
yyerror("could not duplicate pgm instance");
free_token($3);
YYABORT;
}
current_prog->parent_id = cache_string($3->value.string); current_prog->parent_id = cache_string($3->value.string);
current_prog->config_file = cache_string(cfg_infile); current_prog->config_file = cache_string(cfg_infile);
current_prog->lineno = cfg_lineno; current_prog->lineno = cfg_lineno;
@ -400,10 +391,6 @@ part_decl :
K_PART K_PART
{ {
current_part = avr_new_part(); current_part = avr_new_part();
if (current_part == NULL) {
yyerror("could not create part instance");
YYABORT;
}
current_part->config_file = cache_string(cfg_infile); current_part->config_file = cache_string(cfg_infile);
current_part->lineno = cfg_lineno; current_part->lineno = cfg_lineno;
} | } |
@ -417,11 +404,6 @@ part_decl :
} }
current_part = avr_dup_part(parent_part); current_part = avr_dup_part(parent_part);
if (current_part == NULL) {
yyerror("could not duplicate part instance");
free_token($3);
YYABORT;
}
current_part->parent_id = cache_string($3->value.string); current_part->parent_id = cache_string($3->value.string);
current_part->config_file = cache_string(cfg_infile); current_part->config_file = cache_string(cfg_infile);
current_part->lineno = cfg_lineno; current_part->lineno = cfg_lineno;
@ -465,8 +447,7 @@ prog_parm :
prog_parm_conntype prog_parm_conntype
| |
K_DESC TKN_EQUAL TKN_STRING { K_DESC TKN_EQUAL TKN_STRING {
strncpy(current_prog->desc, $3->value.string, PGM_DESCLEN); current_prog->desc = cache_string($3->value.string);
current_prog->desc[PGM_DESCLEN-1] = 0;
free_token($3); free_token($3);
} | } |
K_BAUDRATE TKN_EQUAL TKN_NUMBER { K_BAUDRATE TKN_EQUAL TKN_NUMBER {
@ -686,22 +667,19 @@ retry_lines :
part_parm : part_parm :
K_ID TKN_EQUAL TKN_STRING K_ID TKN_EQUAL TKN_STRING
{ {
strncpy(current_part->id, $3->value.string, AVR_IDLEN); current_part->id = cache_string($3->value.string);
current_part->id[AVR_IDLEN-1] = 0;
free_token($3); free_token($3);
} | } |
K_DESC TKN_EQUAL TKN_STRING K_DESC TKN_EQUAL TKN_STRING
{ {
strncpy(current_part->desc, $3->value.string, AVR_DESCLEN - 1); current_part->desc = cache_string($3->value.string);
current_part->desc[AVR_DESCLEN-1] = 0;
free_token($3); free_token($3);
} | } |
K_FAMILY_ID TKN_EQUAL TKN_STRING K_FAMILY_ID TKN_EQUAL TKN_STRING
{ {
strncpy(current_part->family_id, $3->value.string, AVR_FAMILYIDLEN); current_part->family_id = cache_string($3->value.string);
current_part->family_id[AVR_FAMILYIDLEN] = 0;
free_token($3); free_token($3);
} | } |
@ -1289,13 +1267,8 @@ part_parm :
{ /* select memory for extension or create if not there */ { /* select memory for extension or create if not there */
AVRMEM *mem = avr_locate_mem_noalias(current_part, $2->value.string); AVRMEM *mem = avr_locate_mem_noalias(current_part, $2->value.string);
if(!mem) { if(!mem) {
if(!(mem = avr_new_memtype())) { mem = avr_new_memtype();
yyerror("could not create mem instance"); mem->desc = cache_string($2->value.string);
free_token($2);
YYABORT;
}
strncpy(mem->desc, $2->value.string, AVR_MEMDESCLEN - 1);
mem->desc[AVR_MEMDESCLEN-1] = 0;
ladd(current_part->mem, mem); ladd(current_part->mem, mem);
} }
avr_add_mem_order($2->value.string); avr_add_mem_order($2->value.string);
@ -1339,11 +1312,6 @@ part_parm :
opnum = which_opcode($1); opnum = which_opcode($1);
if (opnum < 0) YYABORT; if (opnum < 0) YYABORT;
op = avr_new_opcode(); op = avr_new_opcode();
if (op == NULL) {
yyerror("could not create opcode instance");
free_token($1);
YYABORT;
}
if(0 != parse_cmdbits(op, opnum)) if(0 != parse_cmdbits(op, opnum))
YYABORT; YYABORT;
if (current_part->op[opnum] != NULL) { if (current_part->op[opnum] != NULL) {
@ -1500,11 +1468,6 @@ mem_spec :
opnum = which_opcode($1); opnum = which_opcode($1);
if (opnum < 0) YYABORT; if (opnum < 0) YYABORT;
op = avr_new_opcode(); op = avr_new_opcode();
if (op == NULL) {
yyerror("could not create opcode instance");
free_token($1);
YYABORT;
}
if(0 != parse_cmdbits(op, opnum)) if(0 != parse_cmdbits(op, opnum))
YYABORT; YYABORT;
if (current_mem->op[opnum] != NULL) { if (current_mem->op[opnum] != NULL) {
@ -1553,10 +1516,7 @@ mem_alias :
is_alias = true; is_alias = true;
alias = avr_new_memalias(); alias = avr_new_memalias();
alias->desc = current_mem->desc;
// alias->desc and current_mem->desc have the same length
// definition, thus no need to check for length here
strcpy(alias->desc, current_mem->desc);
alias->aliased_mem = existing_mem; alias->aliased_mem = existing_mem;
ladd(current_part->mem_alias, alias); ladd(current_part->mem_alias, alias);

View File

@ -222,7 +222,7 @@ int dev_message(int msglvl, const char *fmt, ...) {
static int dev_part_strct_entry(bool tsv, char *col0, char *col1, char *col2, const char *name, char *cont) { static int dev_part_strct_entry(bool tsv, const char *col0, const char *col1, const char *col2, const char *name, char *cont) {
const char *n = name? name: "name_error"; const char *n = name? name: "name_error";
const char *c = cont? cont: "cont_error"; const char *c = cont? cont: "cont_error";
@ -286,23 +286,22 @@ static int intcmp(int a, int b) {
// Deep copies for comparison and raw output // Deep copies for comparison and raw output
typedef struct { typedef struct {
char descbuf[32];
AVRMEM base; AVRMEM base;
OPCODE ops[AVR_OP_MAX]; OPCODE ops[AVR_OP_MAX];
} AVRMEMdeep; } AVRMEMdeep;
static int avrmem_deep_copy(AVRMEMdeep *d, AVRMEM *m) { static int avrmem_deep_copy(AVRMEMdeep *d, AVRMEM *m) {
size_t len;
d->base = *m; d->base = *m;
// Zap all bytes beyond terminating nul of desc array // Note memory desc (name, really) is limited to 31 char here
len = strlen(m->desc)+1; memset(d->descbuf, 0, sizeof d->descbuf);
if(len < sizeof m->desc) strncpy(d->descbuf, m->desc, sizeof d->descbuf-1);
memset(d->base.desc + len, 0, sizeof m->desc - len);
// Zap address values // Zap address values
d->base.buf = NULL; d->base.buf = NULL;
d->base.tags = NULL; d->base.tags = NULL;
d->base.desc = NULL;
for(int i=0; i<AVR_OP_MAX; i++) for(int i=0; i<AVR_OP_MAX; i++)
d->base.op[i] = NULL; d->base.op[i] = NULL;
@ -334,6 +333,9 @@ static int memorycmp(AVRMEM *m1, AVRMEM *m2) {
typedef struct { typedef struct {
char descbuf[64];
char idbuf[32];
char family_idbuf[16];
AVRPART base; AVRPART base;
OPCODE ops[AVR_OP_MAX]; OPCODE ops[AVR_OP_MAX];
AVRMEMdeep mems[40]; AVRMEMdeep mems[40];
@ -341,7 +343,7 @@ typedef struct {
static int avrpart_deep_copy(AVRPARTdeep *d, AVRPART *p) { static int avrpart_deep_copy(AVRPARTdeep *d, AVRPART *p) {
AVRMEM *m; AVRMEM *m;
size_t len, di; size_t di;
memset(d, 0, sizeof *d); memset(d, 0, sizeof *d);
@ -351,20 +353,21 @@ static int avrpart_deep_copy(AVRPARTdeep *d, AVRPART *p) {
d->base.config_file = NULL; d->base.config_file = NULL;
d->base.lineno = 0; d->base.lineno = 0;
// Zap all bytes beyond terminating nul of desc, id and family_id array // Copy over desc, id, and family_id
len = strlen(p->desc); memset(d->descbuf, 0, sizeof d->descbuf);
if(len < sizeof p->desc) if(d->descbuf)
memset(d->base.desc + len, 0, sizeof p->desc - len); strncpy(d->descbuf, p->desc, sizeof d->descbuf-1);
memset(d->idbuf, 0, sizeof d->idbuf);
len = strlen(p->family_id); if(d->idbuf)
if(len < sizeof p->family_id) strncpy(d->idbuf, p->id, sizeof d->idbuf-1);
memset(d->base.family_id + len, 0, sizeof p->family_id - len); memset(d->family_idbuf, 0, sizeof d->family_idbuf);
if(d->family_idbuf)
len = strlen(p->id); strncpy(d->family_idbuf, p->family_id, sizeof d->family_idbuf-1);
if(len < sizeof p->id)
memset(d->base.id + len, 0, sizeof p->id - len);
// Zap address values // Zap address values
d->base.desc = NULL;
d->base.id = NULL;
d->base.family_id = NULL;
d->base.mem = NULL; d->base.mem = NULL;
d->base.mem_alias = NULL; d->base.mem_alias = NULL;
for(int i=0; i<AVR_OP_MAX; i++) for(int i=0; i<AVR_OP_MAX; i++)
@ -397,14 +400,14 @@ static int avrpart_deep_copy(AVRPARTdeep *d, AVRPART *p) {
static char txtchar(unsigned char in) { static char txtchar(unsigned char in) {
in &= 0x7f; in &= 0x7f;
return in == ' '? '_': in > ' ' && in < 0x7f? in: '.'; return in == 0? '.': in > ' ' && in < 0x7f? in: '_';
} }
static void dev_raw_dump(const char *p, int nbytes, const char *name, const char *sub, int idx) { static void dev_raw_dump(const char *p, int nbytes, const char *name, const char *sub, int idx) {
int n = (nbytes + 31)/32; int n = (nbytes + 31)/32;
for(int i=0; i<n; i++, p += 32, nbytes -= 32) { for(int i=0; i<n; i++, p += 32, nbytes -= 32) {
dev_info("%s\t%s\t%02x%03x0: ", name, sub, idx, 2*i); dev_info("%s\t%s\t%02x.%03x0: ", name, sub, idx, 2*i);
for(int j=0; j<32; j++) { for(int j=0; j<32; j++) {
if(j && j%8 == 0) if(j && j%8 == 0)
dev_info(" "); dev_info(" ");
@ -429,7 +432,7 @@ static void dev_part_raw(AVRPART *part) {
dev_raw_dump((char *) &dp.ops, sizeof dp.ops, part->desc, "ops", 1); dev_raw_dump((char *) &dp.ops, sizeof dp.ops, part->desc, "ops", 1);
for(int i=0; i<di; i++) for(int i=0; i<di; i++)
dev_raw_dump((char *) (dp.mems+i), sizeof dp.mems[i], part->desc, dp.mems[i].base.desc, i+2); dev_raw_dump((char *) (dp.mems+i), sizeof dp.mems[i], part->desc, dp.mems[i].descbuf, i+2);
} }
@ -680,7 +683,10 @@ void dev_output_part_defs(char *partdesc) {
avr_add_mem_order(((AVRMEM_ALIAS *) ldata(lnm))->desc); avr_add_mem_order(((AVRMEM_ALIAS *) ldata(lnm))->desc);
} }
if((nprinted = dev_nprinted)) {
dev_info("\n");
nprinted = dev_nprinted; nprinted = dev_nprinted;
}
for(LNODEID ln1 = lfirst(part_list); ln1; ln1 = lnext(ln1)) { for(LNODEID ln1 = lfirst(part_list); ln1; ln1 = lnext(ln1)) {
AVRPART *p = ldata(ln1); AVRPART *p = ldata(ln1);
int flashsize, flashoffset, flashpagesize, eepromsize , eepromoffset, eeprompagesize; int flashsize, flashoffset, flashpagesize, eepromsize , eepromoffset, eeprompagesize;
@ -914,6 +920,8 @@ static void dev_pgm_raw(PROGRAMMER *pgm) {
for(idx=0, ln=lfirst(dp.hvupdi_support); ln; ln=lnext(ln)) for(idx=0, ln=lfirst(dp.hvupdi_support); ln; ln=lnext(ln))
dev_raw_dump(ldata(ln), sizeof(int), id, "hvupdi_", idx++); dev_raw_dump(ldata(ln), sizeof(int), id, "hvupdi_", idx++);
if(dp.desc)
dev_raw_dump(dp.desc, strlen(dp.desc)+1, id, "desc", 0);
// Dump cache_string values // Dump cache_string values
if(dp.usbdev && *dp.usbdev) if(dp.usbdev && *dp.usbdev)
dev_raw_dump(dp.usbdev, strlen(dp.usbdev)+1, id, "usbdev", 0); dev_raw_dump(dp.usbdev, strlen(dp.usbdev)+1, id, "usbdev", 0);
@ -925,14 +933,13 @@ static void dev_pgm_raw(PROGRAMMER *pgm) {
dev_raw_dump(dp.usbproduct, strlen(dp.usbproduct)+1, id, "usbprod", 0); dev_raw_dump(dp.usbproduct, strlen(dp.usbproduct)+1, id, "usbprod", 0);
// Zap all bytes beyond terminating nul of desc, type and port array // Zap all bytes beyond terminating nul of desc, type and port array
if((len = strlen(dp.desc)+1) < sizeof dp.desc)
memset(dp.desc + len, 0, sizeof dp.desc - len);
if((len = strlen(dp.type)+1) < sizeof dp.type) if((len = strlen(dp.type)+1) < sizeof dp.type)
memset(dp.type + len, 0, sizeof dp.type - len); memset(dp.type + len, 0, sizeof dp.type - len);
if((len = strlen(dp.port)+1) < sizeof dp.port) if((len = strlen(dp.port)+1) < sizeof dp.port)
memset(dp.port + len, 0, sizeof dp.port - len); memset(dp.port + len, 0, sizeof dp.port - len);
// Zap address values // Zap address values
dp.desc = NULL;
dp.id = NULL; dp.id = NULL;
dp.parent_id = NULL; dp.parent_id = NULL;
dp.initpgm = NULL; dp.initpgm = NULL;

View File

@ -609,7 +609,7 @@ static int jtagmkI_initialize(PROGRAMMER * pgm, AVRPART * p)
if (jtagmkI_reset(pgm) < 0) if (jtagmkI_reset(pgm) < 0)
return -1; return -1;
strcpy(hfuse.desc, "hfuse"); hfuse.desc = cache_string("hfuse");
if (jtagmkI_read_byte(pgm, p, &hfuse, 1, &b) < 0) if (jtagmkI_read_byte(pgm, p, &hfuse, 1, &b) < 0)
return -1; return -1;
if ((b & OCDEN) != 0) if ((b & OCDEN) != 0)

View File

@ -1439,7 +1439,7 @@ static int jtagmkII_initialize(PROGRAMMER * pgm, AVRPART * p)
} }
if ((pgm->flag & PGM_FL_IS_JTAG) && !(p->flags & (AVRPART_HAS_PDI | AVRPART_HAS_UPDI))) { if ((pgm->flag & PGM_FL_IS_JTAG) && !(p->flags & (AVRPART_HAS_PDI | AVRPART_HAS_UPDI))) {
strcpy(hfuse.desc, "hfuse"); hfuse.desc = cache_string("hfuse");
if (jtagmkII_read_byte(pgm, p, &hfuse, 1, &b) < 0) if (jtagmkII_read_byte(pgm, p, &hfuse, 1, &b) < 0)
return -1; return -1;
if ((b & OCDEN) != 0) if ((b & OCDEN) != 0)

View File

@ -45,8 +45,6 @@ DIGIT [0-9]
HEXDIGIT [0-9a-fA-F] HEXDIGIT [0-9a-fA-F]
SIGN [+-] SIGN [+-]
%x incl
%x comment
%option nounput %option nounput
/* Bump resources for classic lex. */ /* Bump resources for classic lex. */
@ -73,16 +71,12 @@ SIGN [+-]
0x{HEXDIGIT}+ { yylval = hexnumber(yytext); return TKN_NUMBER; } 0x{HEXDIGIT}+ { yylval = hexnumber(yytext); return TKN_NUMBER; }
# { /* The following captures all '#' style comments to end of line */ #[^\n]*\n+ { /* record and skip # comments */
BEGIN(comment); } capture_comment_str(yytext);
<comment>[^\n]*\n+ { /* eat comments */
capture_comment_char('#');
for(int i=0; yytext[i]; i++) { for(int i=0; yytext[i]; i++) {
capture_comment_char(yytext[i]);
if(yytext[i] == '\n') if(yytext[i] == '\n')
cfg_lineno++; cfg_lineno++;
} }
BEGIN(INITIAL);
} }

View File

@ -203,8 +203,6 @@ typedef struct opcode {
#define HV_UPDI_VARIANT_1 1 /* Dedicated UPDI pin, no HV (megaAVR0/AVR-Dx) */ #define HV_UPDI_VARIANT_1 1 /* Dedicated UPDI pin, no HV (megaAVR0/AVR-Dx) */
#define HV_UPDI_VARIANT_2 2 /* Shared UPDI pin, HV on _RESET (AVR-Ex) */ #define HV_UPDI_VARIANT_2 2 /* Shared UPDI pin, HV on _RESET (AVR-Ex) */
#define AVR_DESCLEN 64
#define AVR_IDLEN 32
#define AVR_FAMILYIDLEN 7 #define AVR_FAMILYIDLEN 7
#define AVR_SIBLEN 16 #define AVR_SIBLEN 16
#define CTL_STACK_SIZE 32 #define CTL_STACK_SIZE 32
@ -215,10 +213,10 @@ typedef struct opcode {
/* Any changes here, please also reflect in dev_part_strct() of developer_opts.c */ /* Any changes here, please also reflect in dev_part_strct() of developer_opts.c */
typedef struct avrpart { typedef struct avrpart {
char desc[AVR_DESCLEN]; /* long part name */ const char * desc; /* long part name */
char id[AVR_IDLEN]; /* short part name */ const char * id; /* short part name */
const char * parent_id; /* parent id if set, for -p.../s */ const char * parent_id; /* parent id if set, for -p.../s */
char family_id[AVR_FAMILYIDLEN+1]; /* family id in the SIB (avr8x) */ const char * family_id; /* family id in the SIB (avr8x) */
int hvupdi_variant; /* HV pulse on UPDI pin, no pin or RESET pin */ int hvupdi_variant; /* HV pulse on UPDI pin, no pin or RESET pin */
int stk500_devcode; /* stk500 device code */ int stk500_devcode; /* stk500 device code */
int avr910_devcode; /* avr910 device code */ int avr910_devcode; /* avr910 device code */
@ -286,7 +284,7 @@ typedef struct avrpart {
#define AVR_MEMDESCLEN 64 #define AVR_MEMDESCLEN 64
typedef struct avrmem { typedef struct avrmem {
char desc[AVR_MEMDESCLEN]; /* memory description ("flash", "eeprom", etc) */ const char *desc; /* memory description ("flash", "eeprom", etc) */
int paged; /* page addressed (e.g. ATmega flash) */ int paged; /* page addressed (e.g. ATmega flash) */
int size; /* total memory size in bytes */ int size; /* total memory size in bytes */
int page_size; /* size of memory page (if page addressed) */ int page_size; /* size of memory page (if page addressed) */
@ -312,7 +310,7 @@ typedef struct avrmem {
} AVRMEM; } AVRMEM;
typedef struct avrmem_alias { typedef struct avrmem_alias {
char desc[AVR_MEMDESCLEN]; /* alias name ("syscfg0" etc.) */ const char *desc; /* alias name ("syscfg0" etc.) */
AVRMEM *aliased_mem; AVRMEM *aliased_mem;
} AVRMEM_ALIAS; } AVRMEM_ALIAS;
@ -330,8 +328,8 @@ int avr_set_bits(OPCODE * op, unsigned char * cmd);
int avr_set_addr(OPCODE * op, unsigned char * cmd, unsigned long addr); int avr_set_addr(OPCODE * op, unsigned char * cmd, unsigned long addr);
int avr_set_addr_mem(AVRMEM *mem, int opnum, unsigned char *cmd, unsigned long addr); int avr_set_addr_mem(AVRMEM *mem, int opnum, unsigned char *cmd, unsigned long addr);
int avr_set_input(OPCODE * op, unsigned char * cmd, unsigned char data); int avr_set_input(OPCODE * op, unsigned char * cmd, unsigned char data);
int avr_get_output(OPCODE * op, unsigned char * res, unsigned char * data); int avr_get_output(const OPCODE *op, const unsigned char *res, unsigned char *data);
int avr_get_output_index(OPCODE * op); int avr_get_output_index(const OPCODE *op);
char cmdbitchar(CMDBIT cb); char cmdbitchar(CMDBIT cb);
char *cmdbitstr(CMDBIT cb); char *cmdbitstr(CMDBIT cb);
const char *opcodename(int opnum); const char *opcodename(int opnum);
@ -340,26 +338,26 @@ char *opcode2str(OPCODE *op, int opnum, int detailed);
/* Functions for AVRMEM structures */ /* Functions for AVRMEM structures */
AVRMEM * avr_new_memtype(void); AVRMEM * avr_new_memtype(void);
AVRMEM_ALIAS * avr_new_memalias(void); AVRMEM_ALIAS * avr_new_memalias(void);
int avr_initmem(AVRPART * p); int avr_initmem(const AVRPART *p);
AVRMEM * avr_dup_mem(AVRMEM * m); AVRMEM * avr_dup_mem(const AVRMEM *m);
void avr_free_mem(AVRMEM * m); void avr_free_mem(AVRMEM * m);
void avr_free_memalias(AVRMEM_ALIAS * m); void avr_free_memalias(AVRMEM_ALIAS * m);
AVRMEM * avr_locate_mem(AVRPART * p, const char * desc); AVRMEM * avr_locate_mem(const AVRPART *p, const char *desc);
AVRMEM * avr_locate_mem_noalias(AVRPART * p, const char * desc); AVRMEM * avr_locate_mem_noalias(const AVRPART *p, const char *desc);
AVRMEM_ALIAS * avr_locate_memalias(AVRPART * p, const char * desc); AVRMEM_ALIAS * avr_locate_memalias(const AVRPART *p, const char *desc);
AVRMEM_ALIAS * avr_find_memalias(AVRPART * p, AVRMEM * m_orig); AVRMEM_ALIAS * avr_find_memalias(const AVRPART *p, const AVRMEM *m_orig);
void avr_mem_display(const char * prefix, FILE * f, AVRMEM * m, AVRPART * p, void avr_mem_display(const char *prefix, FILE *f, const AVRMEM *m,
int type, int verbose); const AVRPART *p, int verbose);
/* Functions for AVRPART structures */ /* Functions for AVRPART structures */
AVRPART * avr_new_part(void); AVRPART * avr_new_part(void);
AVRPART * avr_dup_part(AVRPART * d); AVRPART * avr_dup_part(const AVRPART *d);
void avr_free_part(AVRPART * d); void avr_free_part(AVRPART * d);
AVRPART * locate_part(LISTID parts, const char * partdesc); AVRPART * locate_part(const LISTID parts, const char *partdesc);
AVRPART * locate_part_by_avr910_devcode(LISTID parts, int devcode); AVRPART * locate_part_by_avr910_devcode(const LISTID parts, int devcode);
AVRPART * locate_part_by_signature(LISTID parts, unsigned char * sig, AVRPART * locate_part_by_signature(const LISTID parts, unsigned char *sig,
int sigsize); int sigsize);
void avr_display(FILE * f, AVRPART * p, const char * prefix, int verbose); void avr_display(FILE *f, const AVRPART *p, const char *prefix, int verbose);
typedef void (*walk_avrparts_cb)(const char *name, const char *desc, typedef void (*walk_avrparts_cb)(const char *name, const char *desc,
const char *cfgname, int cfglineno, const char *cfgname, int cfglineno,
@ -653,7 +651,6 @@ extern struct serial_device usbhid_serdev;
#define ON 1 #define ON 1
#define OFF 0 #define OFF 0
#define PGM_DESCLEN 80
#define PGM_PORTLEN PATH_MAX #define PGM_PORTLEN PATH_MAX
#define PGM_TYPELEN 32 #define PGM_TYPELEN 32
@ -685,7 +682,7 @@ typedef enum {
/* Any changes here, please also reflect in dev_pgm_strct() of developer_opts.c */ /* Any changes here, please also reflect in dev_pgm_strct() of developer_opts.c */
typedef struct programmer_t { typedef struct programmer_t {
LISTID id; LISTID id;
char desc[PGM_DESCLEN]; const char *desc;
void (*initpgm)(struct programmer_t *pgm); void (*initpgm)(struct programmer_t *pgm);
const char *parent_id; // Used by developer options -c*/[sr...] const char *parent_id; // Used by developer options -c*/[sr...]
struct pindef_t pin[N_PINS]; struct pindef_t pin[N_PINS];
@ -762,6 +759,7 @@ typedef struct programmer_t {
int (*parseextparams) (struct programmer_t * pgm, LISTID xparams); int (*parseextparams) (struct programmer_t * pgm, LISTID xparams);
void (*setup) (struct programmer_t * pgm); void (*setup) (struct programmer_t * pgm);
void (*teardown) (struct programmer_t * pgm); void (*teardown) (struct programmer_t * pgm);
const char *config_file; // Config file where defined const char *config_file; // Config file where defined
int lineno; // Config file line number int lineno; // Config file line number
void *cookie; // For private use by the programmer void *cookie; // For private use by the programmer
@ -773,8 +771,8 @@ extern "C" {
#endif #endif
PROGRAMMER * pgm_new(void); PROGRAMMER * pgm_new(void);
PROGRAMMER * pgm_dup(const PROGRAMMER * const src); PROGRAMMER * pgm_dup(const PROGRAMMER *src);
void pgm_free(PROGRAMMER * const p); void pgm_free(PROGRAMMER *p);
void programmer_display(PROGRAMMER * pgm, const char * p); void programmer_display(PROGRAMMER * pgm, const char * p);
@ -783,10 +781,10 @@ void programmer_display(PROGRAMMER * pgm, const char * p);
#define SHOW_PPI_PINS ((1<<PPI_AVR_VCC)|(1<<PPI_AVR_BUFF)) #define SHOW_PPI_PINS ((1<<PPI_AVR_VCC)|(1<<PPI_AVR_BUFF))
#define SHOW_AVR_PINS ((1<<PIN_AVR_RESET)|(1<<PIN_AVR_SCK)|(1<<PIN_AVR_MOSI)|(1<<PIN_AVR_MISO)) #define SHOW_AVR_PINS ((1<<PIN_AVR_RESET)|(1<<PIN_AVR_SCK)|(1<<PIN_AVR_MOSI)|(1<<PIN_AVR_MISO))
#define SHOW_LED_PINS ((1<<PIN_LED_ERR)|(1<<PIN_LED_RDY)|(1<<PIN_LED_PGM)|(1<<PIN_LED_VFY)) #define SHOW_LED_PINS ((1<<PIN_LED_ERR)|(1<<PIN_LED_RDY)|(1<<PIN_LED_PGM)|(1<<PIN_LED_VFY))
void pgm_display_generic_mask(PROGRAMMER * pgm, const char * p, unsigned int show); void pgm_display_generic_mask(const PROGRAMMER *pgm, const char *p, unsigned int show);
void pgm_display_generic(PROGRAMMER * pgm, const char * p); void pgm_display_generic(const PROGRAMMER *pgm, const char *p);
PROGRAMMER * locate_programmer(LISTID programmers, const char * configid); PROGRAMMER *locate_programmer(const LISTID programmers, const char *configid);
typedef void (*walk_programmers_cb)(const char *name, const char *desc, typedef void (*walk_programmers_cb)(const char *name, const char *desc,
const char *cfgname, int cfglineno, const char *cfgname, int cfglineno,

View File

@ -1073,9 +1073,9 @@ int main(int argc, char * argv [])
pgm->read_sib(pgm, p, sib); pgm->read_sib(pgm, p, sib);
avrdude_message(MSG_NOTICE, "%s: System Information Block: \"%s\"\n", avrdude_message(MSG_NOTICE, "%s: System Information Block: \"%s\"\n",
progname, sib); progname, sib);
if (quell_progress < 2) { if (quell_progress < 2)
avrdude_message(MSG_INFO, "%s: Received FamilyID: \"%.*s\"\n", progname, AVR_FAMILYIDLEN, sib); avrdude_message(MSG_INFO, "%s: Received FamilyID: \"%.*s\"\n", progname, AVR_FAMILYIDLEN, sib);
}
if (strncmp(p->family_id, sib, AVR_FAMILYIDLEN)) { if (strncmp(p->family_id, sib, AVR_FAMILYIDLEN)) {
avrdude_message(MSG_INFO, "%s: Expected FamilyID: \"%s\"\n", progname, p->family_id); avrdude_message(MSG_INFO, "%s: Expected FamilyID: \"%s\"\n", progname, p->family_id);
if (!ovsigck) { if (!ovsigck) {
@ -1093,9 +1093,8 @@ int main(int argc, char * argv [])
avrdude_message(MSG_INFO, "%s: conflicting -e and -n options specified, NOT erasing chip\n", avrdude_message(MSG_INFO, "%s: conflicting -e and -n options specified, NOT erasing chip\n",
progname); progname);
} else { } else {
if (quell_progress < 2) { if (quell_progress < 2)
avrdude_message(MSG_INFO, "%s: erasing chip\n", progname); avrdude_message(MSG_INFO, "%s: erasing chip\n", progname);
}
exitrc = avr_unlock(pgm, p); exitrc = avr_unlock(pgm, p);
if(exitrc) goto main_exit; if(exitrc) goto main_exit;
goto sig_again; goto sig_again;
@ -1229,9 +1228,8 @@ int main(int argc, char * argv [])
avrdude_message(MSG_INFO, "%s: conflicting -e and -n options specified, NOT erasing chip\n", avrdude_message(MSG_INFO, "%s: conflicting -e and -n options specified, NOT erasing chip\n",
progname); progname);
} else { } else {
if (quell_progress < 2) { if (quell_progress < 2)
avrdude_message(MSG_INFO, "%s: erasing chip\n", progname); avrdude_message(MSG_INFO, "%s: erasing chip\n", progname);
}
exitrc = avr_chip_erase(pgm, p); exitrc = avr_chip_erase(pgm, p);
if(exitrc) goto main_exit; if(exitrc) goto main_exit;
} }

115
src/pgm.c
View File

@ -61,31 +61,29 @@ static void pgm_default_powerup_powerdown (struct programmer_t * pgm)
} }
PROGRAMMER * pgm_new(void) PROGRAMMER *pgm_new(void) {
{ PROGRAMMER *pgm = (PROGRAMMER *) cfg_malloc("pgm_new()", sizeof(*pgm));
int i;
PROGRAMMER * pgm;
const char *nulp = cache_string(""); const char *nulp = cache_string("");
pgm = (PROGRAMMER *) cfg_malloc("pgm_new()", sizeof(*pgm)); // Initialise const char * and LISTID entities
pgm->id = lcreat(NULL, 0); pgm->id = lcreat(NULL, 0);
pgm->usbpid = lcreat(NULL, 0); pgm->usbpid = lcreat(NULL, 0);
pgm->desc[0] = 0;
pgm->type[0] = 0;
pgm->parent_id = nulp;
pgm->config_file = nulp;
pgm->lineno = 0;
pgm->baudrate = 0;
pgm->initpgm = NULL;
pgm->hvupdi_support = lcreat(NULL, 0); pgm->hvupdi_support = lcreat(NULL, 0);
pgm->desc = nulp;
pgm->parent_id = nulp;
pgm->usbdev = nulp; pgm->usbdev = nulp;
pgm->usbsn = nulp; pgm->usbsn = nulp;
pgm->usbvendor = nulp; pgm->usbvendor = nulp;
pgm->usbproduct = nulp; pgm->usbproduct = nulp;
pgm->config_file = nulp;
for (i=0; i<N_PINS; i++) { // Default values
pgm->initpgm = NULL;
pgm->lineno = 0;
pgm->baudrate = 0;
// Clear pin array
for(int i=0; i<N_PINS; i++) {
pgm->pinno[i] = 0; pgm->pinno[i] = 0;
pin_clear_all(&(pgm->pin[i])); pin_clear_all(&(pgm->pin[i]));
} }
@ -121,50 +119,71 @@ PROGRAMMER * pgm_new(void)
* optional functions - these are checked to make sure they are * optional functions - these are checked to make sure they are
* assigned before they are called * assigned before they are called
*/ */
pgm->unlock = NULL;
pgm->cmd = NULL; pgm->cmd = NULL;
pgm->cmd_tpi = NULL; pgm->cmd_tpi = NULL;
pgm->spi = NULL; pgm->spi = NULL;
pgm->paged_write = NULL; pgm->paged_write = NULL;
pgm->paged_load = NULL; pgm->paged_load = NULL;
pgm->page_erase = NULL;
pgm->write_setup = NULL; pgm->write_setup = NULL;
pgm->read_sig_bytes = NULL; pgm->read_sig_bytes = NULL;
pgm->read_sib = NULL;
pgm->print_parms = NULL;
pgm->set_vtarget = NULL; pgm->set_vtarget = NULL;
pgm->set_varef = NULL; pgm->set_varef = NULL;
pgm->set_fosc = NULL; pgm->set_fosc = NULL;
pgm->set_sck_period = NULL;
pgm->setpin = NULL;
pgm->getpin = NULL;
pgm->highpulsepin = NULL;
pgm->parseexitspecs = NULL;
pgm->perform_osccal = NULL; pgm->perform_osccal = NULL;
pgm->parseextparams = NULL; pgm->parseextparams = NULL;
pgm->setup = NULL; pgm->setup = NULL;
pgm->teardown = NULL; pgm->teardown = NULL;
// For allocating "global" memory by the programmer
pgm->cookie = NULL;
return pgm; return pgm;
} }
void pgm_free(PROGRAMMER * const p) void pgm_free(PROGRAMMER *p) {
{ if(p) {
ldestroy_cb(p->id, free); ldestroy_cb(p->id, free);
ldestroy_cb(p->usbpid, free); ldestroy_cb(p->usbpid, free);
ldestroy_cb(p->hvupdi_support, free);
p->id = NULL; p->id = NULL;
p->usbpid = NULL; p->usbpid = NULL;
/* do not free p->parent_id, p->config_file, p->usbdev, p->usbsn, p->usbvendor or p-> usbproduct */ p->hvupdi_support = NULL;
/* p->cookie is freed by pgm_teardown */ // Never free const char *, eg, p->desc, which are set by cache_string()
// p->cookie is freed by pgm_teardown
free(p); free(p);
} }
}
PROGRAMMER * pgm_dup(const PROGRAMMER * const src) PROGRAMMER *pgm_dup(const PROGRAMMER *src) {
{ PROGRAMMER *pgm = pgm_new();
PROGRAMMER * pgm;
LNODEID ln;
pgm = (PROGRAMMER *) cfg_malloc("pgm_dup()", sizeof(*pgm)); if(src) {
memcpy(pgm, src, sizeof(*pgm)); memcpy(pgm, src, sizeof(*pgm));
pgm->id = lcreat(NULL, 0); pgm->id = lcreat(NULL, 0);
pgm->usbpid = lcreat(NULL, 0); pgm->usbpid = lcreat(NULL, 0);
for (ln = lfirst(src->usbpid); ln; ln = lnext(ln)) { pgm->hvupdi_support = lcreat(NULL, 0);
// Leave id list empty but copy usbpid and hvupdi_support over
for(LNODEID ln = lfirst(src->hvupdi_support); ln; ln = lnext(ln)) {
int *ip = cfg_malloc("pgm_dup()", sizeof(int));
*ip = *(int *) ldata(ln);
ladd(pgm->hvupdi_support, ip);
}
for(LNODEID ln = lfirst(src->usbpid); ln; ln = lnext(ln)) {
int *ip = cfg_malloc("pgm_dup()", sizeof(int)); int *ip = cfg_malloc("pgm_dup()", sizeof(int));
*ip = *(int *) ldata(ln); *ip = *(int *) ldata(ln);
ladd(pgm->usbpid, ip); ladd(pgm->usbpid, ip);
} }
}
return pgm; return pgm;
} }
@ -207,8 +226,7 @@ static void pgm_default_6 (struct programmer_t * pgm, const char * p)
} }
void programmer_display(PROGRAMMER * pgm, const char * p) void programmer_display(PROGRAMMER *pgm, const char * p) {
{
avrdude_message(MSG_INFO, "%sProgrammer Type : %s\n", p, pgm->type); avrdude_message(MSG_INFO, "%sProgrammer Type : %s\n", p, pgm->type);
avrdude_message(MSG_INFO, "%sDescription : %s\n", p, pgm->desc); avrdude_message(MSG_INFO, "%sDescription : %s\n", p, pgm->desc);
@ -216,8 +234,7 @@ void programmer_display(PROGRAMMER * pgm, const char * p)
} }
void pgm_display_generic_mask(PROGRAMMER * pgm, const char * p, unsigned int show) void pgm_display_generic_mask(const PROGRAMMER *pgm, const char *p, unsigned int show) {
{
if(show & (1<<PPI_AVR_VCC)) if(show & (1<<PPI_AVR_VCC))
avrdude_message(MSG_INFO, "%s VCC = %s\n", p, pins_to_str(&pgm->pin[PPI_AVR_VCC])); avrdude_message(MSG_INFO, "%s VCC = %s\n", p, pins_to_str(&pgm->pin[PPI_AVR_VCC]));
if(show & (1<<PPI_AVR_BUFF)) if(show & (1<<PPI_AVR_BUFF))
@ -240,33 +257,22 @@ void pgm_display_generic_mask(PROGRAMMER * pgm, const char * p, unsigned int sho
avrdude_message(MSG_INFO, "%s VFY LED = %s\n", p, pins_to_str(&pgm->pin[PIN_LED_VFY])); avrdude_message(MSG_INFO, "%s VFY LED = %s\n", p, pins_to_str(&pgm->pin[PIN_LED_VFY]));
} }
void pgm_display_generic(PROGRAMMER * pgm, const char * p) void pgm_display_generic(const PROGRAMMER *pgm, const char *p) {
{
pgm_display_generic_mask(pgm, p, SHOW_ALL_PINS); pgm_display_generic_mask(pgm, p, SHOW_ALL_PINS);
} }
PROGRAMMER * locate_programmer(LISTID programmers, const char * configid) PROGRAMMER *locate_programmer(const LISTID programmers, const char *configid) {
{
LNODEID ln1, ln2;
PROGRAMMER *p = NULL; PROGRAMMER *p = NULL;
const char * id; int found = 0;
int found;
found = 0; for(LNODEID ln1=lfirst(programmers); ln1 && !found; ln1=lnext(ln1)) {
for (ln1=lfirst(programmers); ln1 && !found; ln1=lnext(ln1)) {
p = ldata(ln1); p = ldata(ln1);
for (ln2=lfirst(p->id); ln2 && !found; ln2=lnext(ln2)) { for(LNODEID ln2=lfirst(p->id); ln2 && !found; ln2=lnext(ln2))
id = ldata(ln2); if(strcasecmp(configid, (const char *) ldata(ln2)) == 0)
if (strcasecmp(configid, id) == 0)
found = 1; found = 1;
} }
}
if (found) return found? p: NULL;
return p;
return NULL;
} }
/* /*
@ -296,16 +302,11 @@ void walk_programmers(LISTID programmers, walk_programmers_cb cb, void *cookie)
/* /*
* Compare function to sort the list of programmers * Compare function to sort the list of programmers
*/ */
static int sort_programmer_compare(PROGRAMMER * p1,PROGRAMMER * p2) static int sort_programmer_compare(const PROGRAMMER *p1, const PROGRAMMER *p2) {
{ if(p1 == NULL || p1->id == NULL || p2 == NULL || p2->id == NULL)
char* id1;
char* id2;
if(p1 == NULL || p2 == NULL) {
return 0; return 0;
}
id1 = ldata(lfirst(p1->id)); return strcasecmp(ldata(lfirst(p1->id)), ldata(lfirst(p2->id)));
id2 = ldata(lfirst(p2->id));
return strncasecmp(id1,id2,AVR_IDLEN);
} }
/* /*

View File

@ -193,16 +193,18 @@ static int pickit2_open(PROGRAMMER * pgm, char * port)
} }
else else
{ {
// get the device description while we're at it // Get the device description while we're at it and overlay it on pgm->desc
short buff[PGM_DESCLEN-1], i; short wbuf[80-1];
HidD_GetProductString(PDATA(pgm)->usb_handle, buff, PGM_DESCLEN-1); char *cbuf = cfg_malloc("pickit2_open()", sizeof wbuf/sizeof*wbuf + (pgm->desc? strlen(pgm->desc): 0) + 2);
HidD_GetProductString(PDATA(pgm)->usb_handle, wbuf, sizeof wbuf/sizeof*wbuf);
// convert from wide chars, but do not overwrite trailing '\0' if(pgm->desc && *pgm->desc)
memset(&(pgm->desc), 0, PGM_DESCLEN); strcpy(cbuf, pgm->desc);
for (i = 0; i < (PGM_DESCLEN-1) && buff[i]; i++)
{ // Convert from wide chars and overlay over initial part of desc
pgm->desc[i] = (char)buff[i]; // TODO what about little/big endian??? for (int i = 0; i < sizeof wbuf/sizeof*wbuf && wbuf[i]; i++)
} cbuf[i] = (char) wbuf[i]; // TODO what about little/big endian???
pgm->desc = cache_string(cbuf);
} }
#else #else
if (usb_open_device(&(PDATA(pgm)->usb_handle), PICKIT2_VID, PICKIT2_PID) < 0) if (usb_open_device(&(PDATA(pgm)->usb_handle), PICKIT2_VID, PICKIT2_PID) < 0)

View File

@ -307,10 +307,10 @@ int do_op(PROGRAMMER * pgm, struct avrpart * p, UPDATE * upd, enum updateflags f
} }
AVRMEM_ALIAS *alias_mem = avr_find_memalias(p, mem); AVRMEM_ALIAS *alias_mem = avr_find_memalias(p, mem);
char alias_mem_desc[AVR_DESCLEN + 1] = ""; char *alias_mem_desc = cfg_malloc("do_op()", 2 + (alias_mem && alias_mem->desc? strlen(alias_mem->desc): 0));
if(alias_mem) { if(alias_mem && alias_mem->desc && *alias_mem->desc) {
strcat(alias_mem_desc, "/"); *alias_mem_desc = '/';
strcat(alias_mem_desc, alias_mem->desc); strcpy(alias_mem_desc+1, alias_mem->desc);
} }
switch (upd->op) { switch (upd->op) {