From 31bed52080e36e9e145939b15bf386698ea64b10 Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Sun, 14 Jul 2024 17:59:40 +0100 Subject: [PATCH] Consider prog_modes in str_ccmcunames_signature() --- src/fileio.c | 7 +++---- src/libavrdude.h | 6 +++--- src/strutil.c | 51 +++++++++++++++++++++++++++++++++++------------- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/fileio.c b/src/fileio.c index 3004f1f9..4a637bca 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -352,8 +352,7 @@ static int b2ihex(const AVRPART *p, const AVRMEM *mem, const Segment *segp, Sego if(name) { fprintf(outf, " %s", name); if((str_eq(name, "sigrow") || str_eq(name, "signature")) && !nextaddr) { - char mculist[1024] = {0}; - str_mcunames_signature(buf, mculist, sizeof mculist); + const char *mculist = str_ccmcunames_signature(buf, PM_ALL); if(*mculist) fprintf(outf, " (%s)", mculist); } @@ -580,7 +579,7 @@ static int ihex2b(const char *infile, FILE *inf, const AVRPART *p, const AVRMEM if(!ovsigck && nextaddr == mulmem[MULTI_SIGROW].base && ihex.reclen >= 3) if(!avr_sig_compatible(p->signature, any->buf+nextaddr)) { pmsg_error("signature of %s incompatible with file's (%s)\n", p->desc, - str_ccmcunames_signature(any->buf+nextaddr)); + str_ccmcunames_signature(any->buf+nextaddr, PM_ALL)); imsg_error("use -F to override this check\n"); mmt_free(buffer); goto error; @@ -935,7 +934,7 @@ static int srec2b(const char *infile, FILE * inf, const AVRPART *p, if(!ovsigck && nextaddr == mulmem[MULTI_SIGROW].base && srec.reclen >= 3) if(!avr_sig_compatible(p->signature, any->buf+nextaddr)) { pmsg_error("signature of %s incompatible with file's (%s)\n", p->desc, - str_ccmcunames_signature(any->buf+nextaddr)); + str_ccmcunames_signature(any->buf+nextaddr, PM_ALL)); imsg_error("use -F to override this check\n"); mmt_free(buffer); goto error; diff --git a/src/libavrdude.h b/src/libavrdude.h index baa24b1c..9fa9041a 100644 --- a/src/libavrdude.h +++ b/src/libavrdude.h @@ -511,7 +511,7 @@ typedef struct avrmem { int initval; /* factory setting of fuses and lock bits */ int bitmask; /* bits used in fuses and lock bits */ int n_word_writes; /* TPI only: number words to write at a time */ - unsigned int offset; /* offset in IO memory (ATxmega) */ + unsigned int offset; /* offset in IO memory (ATxmega, UPDI, some classic memories) */ int min_write_delay; /* microseconds */ int max_write_delay; /* microseconds */ int pwroff_after_write; /* after this memory is written to, @@ -1524,8 +1524,8 @@ char *str_nexttok(char *buf, const char *delim, char **next); const char *str_ccfrq(double f, int n); int str_levenshtein(const char *str1, const char *str2, int swap, int subst, int add, int del); size_t str_weighted_damerau_levenshtein(const char *str1, const char *str2); -int str_mcunames_signature(const unsigned char *sigs, char *p, size_t n); -const char *str_ccmcunames_signature(const unsigned char *sigs); +int str_mcunames_signature(const unsigned char *sigs, int pm, char *p, size_t n); +const char *str_ccmcunames_signature(const unsigned char *sigs, int pm); int led_set(const PROGRAMMER *pgm, int led); int led_clr(const PROGRAMMER *pgm, int led); diff --git a/src/strutil.c b/src/strutil.c index 4d442f91..b281f411 100644 --- a/src/strutil.c +++ b/src/strutil.c @@ -1330,19 +1330,40 @@ size_t str_weighted_damerau_levenshtein(const char *s1, const char *s2) { // Puts a comma-separated list of matching MCU names into array p with n chars space -int str_mcunames_signature(const unsigned char *sigs, char *p, size_t n) { - int matching = 0; +int str_mcunames_signature(const unsigned char *sigs, int pm, char *p, size_t n) { + int matching = 0, k; + const int N = 100; + const char *matches[N]; - for(size_t i=0; i < sizeof uP_table/sizeof *uP_table; i++) { - if(0 == memcmp(sigs, uP_table[i].sigs, sizeof uP_table->sigs)) { - if(matching && n > 2) - strcpy(p, ", "), n -= 2, p += 2; - size_t len = strlen(uP_table[i].name); - if(n > len) { - strcpy(p, uP_table[i].name); - n -= len; p += len; + if(!pm || (pm & PM_ALL) == PM_ALL) // Look up uP table when unrestricted by prog modes + for(size_t i=0; i < sizeof uP_table/sizeof *uP_table; i++) + if(0 == memcmp(sigs, uP_table[i].sigs, sizeof uP_table->sigs) && matching < N) + matches[matching++] = uP_table[i].name; + + for(LNODEID lp = lfirst(part_list); lp; lp = lnext(lp)) { + AVRPART *pp = ldata(lp); + if(0 == memcmp(sigs, pp->signature, 3) && (!pm || (pp->prog_modes & pm))) { + for(k = 0; k < matching; k++) + if(str_eq(matches[k], pp->desc)) + break; + if(k == matching && matching < N) + matches[matching++] = pp->desc; + } + } + + if(n && p) { + *p = 0; + + for(int i = 0; i < matching; i++) { + size_t len = strlen(matches[i]); + if(n > len + 2) { + if(i) { + strcpy(p, ", "); + n -= 2, p += 2; + } + strcpy(p, matches[i]); + n -= len, p += len; } - matching++; } } @@ -1350,9 +1371,11 @@ int str_mcunames_signature(const unsigned char *sigs, char *p, size_t n) { } // Returns a comma-separated list of matching MCU names in closed-circuit space -const char *str_ccmcunames_signature(const unsigned char *sigs) { - char names[1024] = {0}; - (void) str_mcunames_signature(sigs, names, sizeof names); +const char *str_ccmcunames_signature(const unsigned char *sigs, int pm) { + char names[1024]; + // If no match is found, given required prog_modes, relax the match to any prog mode + if(!str_mcunames_signature(sigs, pm, names, sizeof names) && pm && (pm & PM_ALL) != PM_ALL) + (void) str_mcunames_signature(sigs, 0, names, sizeof names); return str_ccprintf("%s", names); }