From ce21e57225190b830899486365bef61238230296 Mon Sep 17 00:00:00 2001 From: stefanrueger Date: Fri, 16 May 2025 00:35:40 +0200 Subject: [PATCH 01/10] Supress compiler warning --- src/butterfly.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/butterfly.c b/src/butterfly.c index f61a02b9..e9f97a7f 100644 --- a/src/butterfly.c +++ b/src/butterfly.c @@ -84,8 +84,8 @@ static void butterfly_teardown(PROGRAMMER *pgm) { pgm->cookie = NULL; } -static int butterfly_send(const PROGRAMMER *pgm, char *buf, size_t len) { - return serial_send(&pgm->fd, (unsigned char *) buf, len); +static int butterfly_send(const PROGRAMMER *pgm, const char *buf, size_t len) { + return serial_send(&pgm->fd, (const unsigned char *) buf, len); } static int butterfly_recv(const PROGRAMMER *pgm, char *buf, size_t len) { @@ -174,11 +174,11 @@ static int butterfly_initialize(const PROGRAMMER *pgm, const AVRPART *p) { */ msg_notice("connecting to programmer: "); if(pgm->flag & IS_BUTTERFLY_MK) { - char mk_reset_cmd[6] = { "#aR@S\r" }; + const char * const mk_reset_cmd = "#aR@S\r"; unsigned char mk_timeout = 0; msg_notice("."); - EI(butterfly_send(pgm, mk_reset_cmd, sizeof(mk_reset_cmd))); + EI(butterfly_send(pgm, mk_reset_cmd, strlen(mk_reset_cmd))); usleep(20000); do { From 1c88dc881c3f358d7a929b758354069d3118c84c Mon Sep 17 00:00:00 2001 From: stefanrueger Date: Fri, 16 May 2025 00:52:21 +0200 Subject: [PATCH 02/10] Supress overzealous compiler warning --- src/urbootlist.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/urbootlist.c b/src/urbootlist.c index d42c77eb..d7658379 100644 --- a/src/urbootlist.c +++ b/src/urbootlist.c @@ -10,7 +10,7 @@ * Meta-author Stefan Rueger * * v 1.2 - * 29.04.2025 + * 16.05.2025 * */ @@ -260553,7 +260553,7 @@ int urbootexists(const char *mcu, const char *io, const char *blt, int req_feats Urboot_template **urboottemplate(const Avrintel *up, const char *mcu, const char *io, const char *blt, int req_feats, int req_ulevel, int showall, int *np) { - size_t m, i, b, c, req_c, n = -1U; + size_t m, i, b, c, req_c, n = ~0U; // n = -1U; Urboot_template **ret = NULL; for(m=0; m Date: Sun, 18 May 2025 22:26:49 +0200 Subject: [PATCH 03/10] Suppress overzealous compiler warning --- src/urbootlist.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/urbootlist.c b/src/urbootlist.c index d7658379..f695c591 100644 --- a/src/urbootlist.c +++ b/src/urbootlist.c @@ -260398,7 +260398,7 @@ static uint16_t *ul_urtemplate(const uint64_t *bl) { hn = 0; } // Move 1 bit from Huffman bootloader bit string to a single Huffman code variable - hc = (hc<<1) | (h64 & 1), hcn++; + hc = ((uint64_t) hc << 1) | (h64 & 1), hcn++; h64 >>= 1, hn++; if(hcn > 27) { pmsg_error("unexpected problem decoding bootloader code"); From 1ab3e518574eb9204107fd3df61cd5abb9abeca9 Mon Sep 17 00:00:00 2001 From: stefanrueger Date: Sun, 18 May 2025 22:43:23 +0200 Subject: [PATCH 04/10] Clarify role of index n --- src/urbootlist.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/urbootlist.c b/src/urbootlist.c index f695c591..2a98f3c6 100644 --- a/src/urbootlist.c +++ b/src/urbootlist.c @@ -260553,7 +260553,7 @@ int urbootexists(const char *mcu, const char *io, const char *blt, int req_feats Urboot_template **urboottemplate(const Avrintel *up, const char *mcu, const char *io, const char *blt, int req_feats, int req_ulevel, int showall, int *np) { - size_t m, i, b, c, req_c, n = ~0U; // n = -1U; + size_t m, i, b, c, req_c; Urboot_template **ret = NULL; for(m=0; mtofree = ut; + int n = (*np)++; + ret[n]->tofree = ut; int size = ut[0]; // Bootloader size including 6-byte-table in top flash int usage = ut[1]; // Flash usage, ie, boot section size or multiple of flash pages From a06acf5e5f2994697848225de1510e523bfa3b53 Mon Sep 17 00:00:00 2001 From: stefanrueger Date: Sun, 18 May 2025 22:57:32 +0200 Subject: [PATCH 05/10] Simplify template indices --- src/urbootlist.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/urbootlist.c b/src/urbootlist.c index 2a98f3c6..ce17a990 100644 --- a/src/urbootlist.c +++ b/src/urbootlist.c @@ -260519,7 +260519,7 @@ static int blcmp(const void *v1, const void *v2) { } int urbootexists(const char *mcu, const char *io, const char *blt, int req_feats) { - size_t m, i, b, c; + int m, i, b, c; for(m=0; m= (int) (sizeof urfeat/sizeof*urfeat)) Return("unexpected feature request"); - req_c = urfeat[req_feats]; + int req_c = urfeat[req_feats]; // Specific feature set chosen? Then only one bootloader needs decoding and requesting int spec = !showall && (req_ulevel == 0 || req_ulevel == 4) ; ret = mmt_malloc(sizeof *ret * UL_CONFIG_N); *np = 0; - for(c = 0; c < UL_CONFIG_N; c++) { + for(int c = 0; c < UL_CONFIG_N; c++) { if(!spec || c == req_c) { const char *cfg = configs[c]; From 5a0d5358128789faaaf8a30e2dc78aa847b795c7 Mon Sep 17 00:00:00 2001 From: stefanrueger Date: Sun, 18 May 2025 23:13:37 +0200 Subject: [PATCH 06/10] Shift resetting tx.len to caller of ft245r_flush() This commit makes it easier for automated program analysis to see that the tx buffer does not overflow. It also hardens AVRDUDE against potential buffer overflow should ft245r_flush() fail and return prematurely with -1. --- src/ft245r.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ft245r.c b/src/ft245r.c index 873651cd..a4f847ce 100644 --- a/src/ft245r.c +++ b/src/ft245r.c @@ -253,7 +253,7 @@ static int ft245r_flush(const PROGRAMMER *pgm) { len -= avail; my.rx.pending += avail; } - my.tx.len = 0; + return 0; } @@ -263,8 +263,10 @@ static int ft245r_send2(const PROGRAMMER *pgm, unsigned char *buf, size_t len, b if(discard_rx_data) ++my.rx.discard; my.tx.buf[my.tx.len++] = buf[i]; - if(my.tx.len >= FT245R_MIN_FIFO_SIZE) + if(my.tx.len >= FT245R_MIN_FIFO_SIZE) { ft245r_flush(pgm); + my.tx.len = 0; + } } } return 0; @@ -280,6 +282,7 @@ static int ft245r_send_and_discard(const PROGRAMMER *pgm, unsigned char *buf, si static int ft245r_recv(const PROGRAMMER *pgm, unsigned char *buf, size_t len) { ft245r_flush(pgm); + my.tx.len = 0; ft245r_fill(pgm); #if FT245R_DEBUG @@ -333,6 +336,7 @@ static int ft245r_drain(const PROGRAMMER *pgm, int display) { // Ensure any pending writes are sent to the FTDI chip before sleeping static void ft245r_usleep(const PROGRAMMER *pgm, useconds_t usec) { ft245r_flush(pgm); + my.tx.len = 0; usleep(usec); } @@ -388,6 +392,7 @@ static int get_pin(const PROGRAMMER *pgm, int pinname) { uint8_t byte; ft245r_flush(pgm); + my.tx.len = 0; if(ftdi_read_pins(my.handle, &byte) != 0) return -1; From 98b15b497a407f4832a5ba32c2478e953408c9ec Mon Sep 17 00:00:00 2001 From: stefanrueger Date: Sun, 18 May 2025 23:20:24 +0200 Subject: [PATCH 07/10] Remove unnecessary brackets --- src/ft245r.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/ft245r.c b/src/ft245r.c index a4f847ce..1fded607 100644 --- a/src/ft245r.c +++ b/src/ft245r.c @@ -292,9 +292,8 @@ static int ft245r_recv(const PROGRAMMER *pgm, unsigned char *buf, size_t len) { while(my.rx.discard > 0) { int result = ft245r_rx_buf_fill_and_get(pgm); - if(result < 0) { + if(result < 0) return result; - } --my.rx.discard; } @@ -302,16 +301,14 @@ static int ft245r_recv(const PROGRAMMER *pgm, unsigned char *buf, size_t len) { for(size_t i = 0; i < len; ++i) { int result = ft245r_rx_buf_fill_and_get(pgm); - if(result < 0) { + if(result < 0) return result; - } buf[i] = (uint8_t) result; for(int j = 1; j < baud_multiplier; ++j) { result = ft245r_rx_buf_fill_and_get(pgm); - if(result < 0) { + if(result < 0) return result; - } } } return 0; @@ -634,9 +631,8 @@ static inline unsigned char extract_data(const PROGRAMMER *pgm, unsigned char *b buf += offset*(8*FT245R_CYCLES); for(j = 0; j < 8; j++) { - if(GET_BITS_0(buf[buf_pos], pgm, PIN_AVR_SDI)) { + if(GET_BITS_0(buf[buf_pos], pgm, PIN_AVR_SDI)) r |= bit; - } buf_pos += FT245R_CYCLES; bit >>= 1; } @@ -654,9 +650,8 @@ static inline unsigned char extract_data_out(const PROGRAMMER *pgm, unsigned cha buf += offset*(8*FT245R_CYCLES); for(j = 0; j < 8; j++) { - if(GET_BITS_0(buf[buf_pos], pgm, PIN_AVR_SDO)) { + if(GET_BITS_0(buf[buf_pos], pgm, PIN_AVR_SDO)) r |= bit; - } buf_pos += FT245R_CYCLES; bit >>= 1; } @@ -673,9 +668,8 @@ static int ft245r_cmd(const PROGRAMMER *pgm, const unsigned char *cmd, unsigned unsigned char buf[128]; buf_pos = 0; - for(i = 0; i < 4; i++) { + for(i = 0; i < 4; i++) buf_pos += set_data(pgm, buf + buf_pos, cmd[i]); - } buf[buf_pos] = 0; buf_pos++; @@ -841,9 +835,8 @@ static int ft245r_open(PROGRAMMER *pgm, const char *port) { char *endptr = NULL; devnum = strtol(startptr, &endptr, 10); - if((startptr == endptr) || (*endptr != '\0')) { + if((startptr == endptr) || (*endptr != '\0')) devnum = -1; - } pmsg_notice2("%s(): device number parsed as: %d\n", __func__, devnum); } } @@ -909,9 +902,8 @@ static int ft245r_open(PROGRAMMER *pgm, const char *port) { } rv = ft245r_set_bitclock(pgm); - if(rv) { + if(rv) goto cleanup; - } // Drain any extraneous input ft245r_drain(pgm, 0); @@ -998,9 +990,8 @@ static int do_request(const PROGRAMMER *pgm, const AVRMEM *m) { my.req_pool = p; ft245r_recv(pgm, buf, bytes); - for(j = 0; j < n; j++) { + for(j = 0; j < n; j++) m->buf[addr++] = extract_data(pgm, buf, (j*4 + 3)); - } return 1; } From 209e1461f2f3dfde0d3d12a6d15c2aea367fe6ac Mon Sep 17 00:00:00 2001 From: stefanrueger Date: Sun, 18 May 2025 23:22:47 +0200 Subject: [PATCH 08/10] Update urbootlist.c version --- src/urbootlist.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/urbootlist.c b/src/urbootlist.c index ce17a990..1b414bf3 100644 --- a/src/urbootlist.c +++ b/src/urbootlist.c @@ -9,8 +9,8 @@ * Published under GNU General Public License, version 3 (GPL-3.0) * Meta-author Stefan Rueger * - * v 1.2 - * 16.05.2025 + * v 1.21 + * 18.05.2025 * */ From 042ca89b2c383fe8bd2f09f8f57f2cd40b827699 Mon Sep 17 00:00:00 2001 From: stefanrueger Date: Sun, 18 May 2025 23:31:11 +0200 Subject: [PATCH 09/10] Suppress overzealous compiler warning --- src/urbootautogen.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/urbootautogen.c b/src/urbootautogen.c index ba6fb5e8..0420a1e0 100644 --- a/src/urbootautogen.c +++ b/src/urbootautogen.c @@ -166,18 +166,18 @@ static const struct { }; -# define _ok(c) ((c) && (uint8_t) (c) <= 0x7f) +#define _ok(c) ((c) > 0 && (c) < 0x7f) // Is s a ^[0-9]+k[0-9]+$ pattern for baud rate? static int is_baudrate_k(const char *s) { int pre=0, post=0; - while(_ok(*s) && isdigit(*s)) + while(_ok(*s) && isdigit((int) *s)) pre++, s++; if(*s != 'k') return 0; s++; - while(_ok(*s) && isdigit(*s)) + while(_ok(*s) && isdigit((int) *s)) post++, s++; return !*s && pre && post; } @@ -217,12 +217,12 @@ static int is_fcpu_m(const char *s) { if(is_fcpu_type(*s)) s++; - while(_ok(*s) && isdigit(*s)) + while(_ok(*s) && isdigit((int) *s)) pre++, s++; if(*s != 'm') return 0; s++; - while(_ok(*s) && isdigit(*s)) + while(_ok(*s) && isdigit((int) *s)) post++, s++; return !*s && pre && post; } @@ -239,11 +239,11 @@ static int is_num_unit(const char *s, const char *unit) { while(*s == '+') // Ignore leading + (used as fillers for sorting) s++; - while(_ok(*s) && isdigit(*s)) + while(_ok(*s) && isdigit((int) *s)) pre++, s++; if(*s == '.') s++; - while(_ok(*s) && isdigit(*s)) + while(_ok(*s) && isdigit((int) *s)) post++, s++; if(!pre && !post) return 0; @@ -252,12 +252,12 @@ static int is_num_unit(const char *s, const char *unit) { s++; if(*s == '-' || *s == '+') s++; - while(_ok(*s) && isdigit(*s)) + while(_ok(*s) && isdigit((int) *s)) ee++, s++; if(!ee) return 0; } - while(_ok(*s) && isspace(*s)) + while(_ok(*s) && isspace((int) *s)) s++; size_t ulen = strlen(unit); From 97039410d43bfb63abfedddaaef031b8a5b4344b Mon Sep 17 00:00:00 2001 From: stefanrueger Date: Sun, 18 May 2025 23:40:40 +0200 Subject: [PATCH 10/10] Suppress loop-not-executed warning --- src/ft245r.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ft245r.c b/src/ft245r.c index 1fded607..2cf7eff3 100644 --- a/src/ft245r.c +++ b/src/ft245r.c @@ -305,11 +305,13 @@ static int ft245r_recv(const PROGRAMMER *pgm, unsigned char *buf, size_t len) { return result; buf[i] = (uint8_t) result; +#if FT245R_BITBANG_VARIABLE_PULSE_WIDTH_WORKAROUND for(int j = 1; j < baud_multiplier; ++j) { result = ft245r_rx_buf_fill_and_get(pgm); if(result < 0) return result; } +#endif } return 0; }