From 8fd38708614c7f78b580115f7150c19100882de0 Mon Sep 17 00:00:00 2001 From: Rene Liebscher Date: Wed, 16 Jul 2014 20:02:01 +0000 Subject: [PATCH] bug #42662 clang warnings under FreeBSD 10.x * avrftdi.c: remove warnings * buspirate.c: (Dito.) * dfu.c: (Dito.) * fileio.c: (Dito.) * libavrdude.h: (Dito.) * pickit2.c: (Dito.) * safemode.c: (Dito.) * ser_avrdoper.c: (Dito.) * ser_posix.c: (Dito.) * ser_win32.c: (Dito.) * stk500v2.c: (Dito.) * usb_libusb.c: (Dito.) * usbasp.c: (Dito.) * config_gram.y: fix problem when using parent part with usbpid lists (existing list was extended not overwritten) git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1328 81a1dc3b-b13d-400b-aceb-764788c761c2 --- ChangeLog | 26 +++++++-- avrftdi.c | 4 +- buspirate.c | 143 ++++++++++++++++++++++++------------------------- config_gram.y | 5 ++ dfu.c | 3 +- fileio.c | 7 ++- libavrdude.h | 2 +- pickit2.c | 46 ++++++++-------- safemode.c | 90 +++++++++++++++---------------- ser_avrdoper.c | 4 +- ser_posix.c | 4 +- ser_win32.c | 4 +- stk500v2.c | 2 +- usb_libusb.c | 24 +++++++-- usbasp.c | 6 +-- 15 files changed, 207 insertions(+), 163 deletions(-) diff --git a/ChangeLog b/ChangeLog index be042d46..564149ba 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,27 @@ +2014-07-16 Rene Liebscher + + bug #42662 clang warnings under FreeBSD 10.x + * avrftdi.c: remove warnings + * buspirate.c: (Dito.) + * dfu.c: (Dito.) + * fileio.c: (Dito.) + * libavrdude.h: (Dito.) + * pickit2.c: (Dito.) + * safemode.c: (Dito.) + * ser_avrdoper.c: (Dito.) + * ser_posix.c: (Dito.) + * ser_win32.c: (Dito.) + * stk500v2.c: (Dito.) + * usb_libusb.c: (Dito.) + * usbasp.c: (Dito.) + + * config_gram.y: fix problem when using parent part with usbpid lists + (existing list was extended not overwritten) + 2014-07-11 Axel Wachtler - - * avrftdi.c: rollback to vfprintf, fixed error from -r1305, (patch #8463) - + + * avrftdi.c: rollback to vfprintf, fixed error from -r1305, (patch #8463) + 2014-06-23 Rene Liebscher * linux_ppdev.h: added missing msg level for avrdude_message diff --git a/avrftdi.c b/avrftdi.c index f8b39f8b..71c7eef5 100644 --- a/avrftdi.c +++ b/avrftdi.c @@ -190,7 +190,7 @@ static void buf_dump(const unsigned char *buf, int len, char *desc, */ static int set_frequency(avrftdi_t* ftdi, uint32_t freq) { - uint32_t divisor; + int32_t divisor; uint8_t buf[3]; /* divisor on 6000000 / freq - 1 */ @@ -436,7 +436,7 @@ static int avrftdi_transmit_mpsse(avrftdi_t* pdata, unsigned char mode, const un { size_t transfer_size = (remaining > blocksize) ? blocksize : remaining; - E(ftdi_write_data(pdata->ftdic, &buf[written], transfer_size) != transfer_size, pdata->ftdic); + E(ftdi_write_data(pdata->ftdic, (unsigned char*)&buf[written], transfer_size) != transfer_size, pdata->ftdic); #if 0 if(remaining < blocksize) E(ftdi_write_data(pdata->ftdic, &si, sizeof(si)) != sizeof(si), pdata->ftdic); diff --git a/buspirate.c b/buspirate.c index 0dfe04a5..ac3e616c 100644 --- a/buspirate.c +++ b/buspirate.c @@ -91,59 +91,56 @@ buspirate_uses_ascii(struct programmer_t *pgm) /* ====== Serial talker functions - binmode ====== */ -static void dump_mem(char *buf, size_t len) +static void dump_mem(const int msglvl, const unsigned char *buf, size_t len) { size_t i; for (i = 0; i 1) { - avrdude_message(MSG_INFO, "%s: buspirate_send_bin():\n", progname); - dump_mem(data, len); - } + avrdude_message(MSG_DEBUG, "%s: buspirate_send_bin():\n", progname); + dump_mem(MSG_DEBUG, data, len); - rc = serial_send(&pgm->fd, (unsigned char *)data, len); + rc = serial_send(&pgm->fd, data, len); return rc; } -static int buspirate_recv_bin(struct programmer_t *pgm, char *buf, size_t len) +static int buspirate_recv_bin(struct programmer_t *pgm, unsigned char *buf, size_t len) { int rc; - rc = serial_recv(&pgm->fd, (unsigned char *)buf, len); + rc = serial_recv(&pgm->fd, buf, len); if (rc < 0) return EOF; - if (verbose > 1) { - avrdude_message(MSG_INFO, "%s: buspirate_recv_bin():\n", progname); - dump_mem(buf, len); - } + + avrdude_message(MSG_DEBUG, "%s: buspirate_recv_bin():\n", progname); + dump_mem(MSG_DEBUG, buf, len); return len; } static int buspirate_expect_bin(struct programmer_t *pgm, - char *send_data, size_t send_len, - char *expect_data, size_t expect_len) + unsigned char *send_data, size_t send_len, + unsigned char *expect_data, size_t expect_len) { - char *recv_buf = alloca(expect_len); - if (!pgm->flag & BP_FLAG_IN_BINMODE) { - avrdude_message(MSG_INFO, "BusPirate: Internal error: buspirate_send_bin() called from ascii mode"); + unsigned char *recv_buf = alloca(expect_len); + if ((pgm->flag & BP_FLAG_IN_BINMODE) == 0) { + avrdude_message(MSG_INFO, "BusPirate: Internal error: buspirate_send_bin() called from ascii mode\n"); return -1; } @@ -155,7 +152,7 @@ static int buspirate_expect_bin(struct programmer_t *pgm, } static int buspirate_expect_bin_byte(struct programmer_t *pgm, - char send_byte, char expect_byte) + unsigned char send_byte, unsigned char expect_byte) { return buspirate_expect_bin(pgm, &send_byte, 1, &expect_byte, 1); } @@ -168,7 +165,7 @@ static int buspirate_getc(struct programmer_t *pgm) unsigned char ch = 0; if (pgm->flag & BP_FLAG_IN_BINMODE) { - avrdude_message(MSG_INFO, "BusPirate: Internal error: buspirate_getc() called from binmode"); + avrdude_message(MSG_INFO, "BusPirate: Internal error: buspirate_getc() called from binmode\n"); return EOF; } @@ -206,7 +203,7 @@ static char *buspirate_readline_noexit(struct programmer_t *pgm, char *buf, size serial_recv_timeout = PDATA(pgm)->serial_recv_timeout; } serial_recv_timeout = orig_serial_recv_timeout; - avrdude_message(MSG_NOTICE, "%s: buspirate_readline(): %s%s", + avrdude_message(MSG_DEBUG, "%s: buspirate_readline(): %s%s", progname, buf, buf[strlen(buf) - 1] == '\n' ? "" : "\n"); if (! buf[0]) @@ -227,18 +224,18 @@ static char *buspirate_readline(struct programmer_t *pgm, char *buf, size_t len) } return ret; } -static int buspirate_send(struct programmer_t *pgm, char *str) +static int buspirate_send(struct programmer_t *pgm, const char *str) { int rc; - avrdude_message(MSG_NOTICE, "%s: buspirate_send(): %s", progname, str); + avrdude_message(MSG_DEBUG, "%s: buspirate_send(): %s", progname, str); if (pgm->flag & BP_FLAG_IN_BINMODE) { - avrdude_message(MSG_INFO, "BusPirate: Internal error: buspirate_send() called from binmode"); + avrdude_message(MSG_INFO, "BusPirate: Internal error: buspirate_send() called from binmode\n"); return -1; } - rc = serial_send(&pgm->fd, (unsigned char *)str, strlen(str)); + rc = serial_send(&pgm->fd, (const unsigned char*)str, strlen(str)); if (rc) return rc; while (strcmp(buspirate_readline(pgm, NULL, 0), str) != 0) @@ -248,11 +245,12 @@ static int buspirate_send(struct programmer_t *pgm, char *str) return 0; } -static int buspirate_is_prompt(char *str) +static int buspirate_is_prompt(const char *str) { + int strlen_str = strlen(str); /* Prompt ends with '>' or '> ' * all other input probably ends with '\n' */ - return (str[strlen(str) - 1] == '>' || str[strlen(str) - 2] == '>'); + return (str[strlen_str - 1] == '>' || str[strlen_str - 2] == '>'); } static int buspirate_expect(struct programmer_t *pgm, char *send, @@ -300,7 +298,7 @@ buspirate_parseextparms(struct programmer_t *pgm, LISTID extparms) int serial_recv_timeout; for (ln = lfirst(extparms); ln; ln = lnext(ln)) { - extended_param = ldata(ln); + extended_param = ldata(ln); if (strcmp(extended_param, "ascii") == 0) { pgm->flag |= BP_FLAG_XPARM_FORCE_ASCII; continue; @@ -342,7 +340,7 @@ buspirate_parseextparms(struct programmer_t *pgm, LISTID extparms) continue; } - if (sscanf(extended_param, "reset=%s", reset) == 1) { + if (sscanf(extended_param, "reset=%9s", reset) == 1) { char *resetpin; while ((resetpin = strtok(preset, ","))) { preset = NULL; /* for subsequent strtok() calls */ @@ -437,7 +435,7 @@ static void buspirate_close(struct programmer_t *pgm) static void buspirate_reset_from_binmode(struct programmer_t *pgm) { - char buf[10]; + unsigned char buf[10]; buf[0] = 0x00; /* BinMode: revert to HiZ */ buspirate_send_bin(pgm, buf, 1); @@ -451,7 +449,7 @@ static void buspirate_reset_from_binmode(struct programmer_t *pgm) int rc; rc = buspirate_recv_bin(pgm, buf, sizeof(buf) - 1); - if (buspirate_is_prompt(buf)) { + if (buspirate_is_prompt((const char*)buf)) { pgm->flag &= ~BP_FLAG_IN_BINMODE; break; } @@ -480,7 +478,7 @@ static int buspirate_start_mode_bin(struct programmer_t *pgm) submode = &(const struct submode){ .name = "Raw-wire", .enter = 0x05, - .entered_format = "RAW%d", + .entered_format = "RAW%1d", .config = 0x8C, }; pgm->flag |= BP_FLAG_NOPAGEDWRITE; @@ -489,7 +487,7 @@ static int buspirate_start_mode_bin(struct programmer_t *pgm) submode = &(const struct submode){ .name = "SPI", .enter = 0x01, - .entered_format = "SPI%d", + .entered_format = "SPI%1d", /* 1000wxyz - SPI config, w=HiZ(0)/3.3v(1), x=CLK idle, y=CLK edge, z=SMP sample * we want: 3.3V(1), idle low(0), data change on @@ -499,9 +497,8 @@ static int buspirate_start_mode_bin(struct programmer_t *pgm) .config = 0x8A, }; } - - char buf[20] = { '\0' }; - unsigned int ver = 0; + + unsigned char buf[20] = { '\0' }; /* == Switch to binmode - send 20x '\0' == */ buspirate_send_bin(pgm, buf, sizeof(buf)); @@ -509,7 +506,7 @@ static int buspirate_start_mode_bin(struct programmer_t *pgm) /* Expecting 'BBIOx' reply */ memset(buf, 0, sizeof(buf)); buspirate_recv_bin(pgm, buf, 5); - if (sscanf(buf, "BBIO%d", &PDATA(pgm)->binmode_version) != 1) { + if (sscanf((const char*)buf, "BBIO%1d", &PDATA(pgm)->binmode_version) != 1) { avrdude_message(MSG_INFO, "Binary mode not confirmed: '%s'\n", buf); buspirate_reset_from_binmode(pgm); return -1; @@ -524,7 +521,7 @@ static int buspirate_start_mode_bin(struct programmer_t *pgm) buspirate_send_bin(pgm, buf, 1); memset(buf, 0, sizeof(buf)); buspirate_recv_bin(pgm, buf, 4); - if (sscanf(buf, submode->entered_format, + if (sscanf((const char*)buf, submode->entered_format, &PDATA(pgm)->submode_version) != 1) { avrdude_message(MSG_INFO, "%s mode not confirmed: '%s'\n", submode->name, buf); @@ -539,8 +536,8 @@ static int buspirate_start_mode_bin(struct programmer_t *pgm) pgm->paged_write = NULL; } else { /* Check for write-then-read without !CS/CS and disable paged_write if absent: */ - strncpy(buf, "\x5\x0\x0\x0\x0", 5); - buspirate_send_bin(pgm, buf, 5); + static const unsigned char buf2[] = {5,0,0,0,0}; + buspirate_send_bin(pgm, buf2, sizeof(buf2)); buspirate_recv_bin(pgm, buf, 1); if (buf[0] != 0x01) { @@ -585,8 +582,9 @@ static int buspirate_start_mode_bin(struct programmer_t *pgm) if (rv < 0) return -1; if (rv) { - strncpy(buf, "\x1\x0\x0", 3); - buspirate_send_bin(pgm, buf, 1); + unsigned int ver = 0; + static const unsigned char buf2[] = {1}; + buspirate_send_bin(pgm, buf2, sizeof(buf2)); buspirate_recv_bin(pgm, buf, 3); ver = buf[1] << 8 | buf[2]; avrdude_message(MSG_NOTICE, "AVR Extended Commands version %d\n", ver); @@ -604,12 +602,14 @@ static int buspirate_start_spi_mode_ascii(struct programmer_t *pgm) { int spi_cmd = -1; int cmd; - char *rcvd, mode[11], buf[5]; + char *rcvd; + char buf[5]; + char mode[11]; buspirate_send(pgm, "m\n"); while(1) { rcvd = buspirate_readline(pgm, NULL, 0); - if (spi_cmd == -1 && sscanf(rcvd, "%d. %10s", &cmd, mode)) { + if (spi_cmd == -1 && sscanf(rcvd, "%2d. %10s", &cmd, mode)) { if (strcmp(mode, "SPI") == 0) spi_cmd = cmd; } @@ -632,12 +632,12 @@ static int buspirate_start_spi_mode_ascii(struct programmer_t *pgm) /* BP firmware 2.1 defaults to Open-drain output. * That doesn't work on my board, even with pull-up * resistors. Select 3.3V output mode instead. */ - sscanf(rcvd, " %d.", &cmd); + sscanf(rcvd, " %2d.", &cmd); snprintf(buf, sizeof(buf), "%d\n", cmd); } if (buspirate_is_prompt(rcvd)) { if (strncmp(rcvd, "SPI>", 4) == 0) { - if (verbose) avrdude_message(MSG_INFO, "BusPirate is now configured for SPI\n"); + avrdude_message(MSG_INFO, "BusPirate is now configured for SPI\n"); break; } /* Not yet 'SPI>' prompt */ @@ -653,8 +653,8 @@ static int buspirate_start_spi_mode_ascii(struct programmer_t *pgm) static void buspirate_enable(struct programmer_t *pgm) { - unsigned char *reset_str = "#\n"; - unsigned char *accept_str = "y\n"; + static const char *reset_str = "#\n"; + static const char *accept_str = "y\n"; char *rcvd; int rc, print_banner = 0; @@ -667,7 +667,7 @@ static void buspirate_enable(struct programmer_t *pgm) avrdude_message(MSG_INFO, "Attempting to initiate BusPirate binary mode...\n"); /* Send two CRs to ensure we're not in a sub-menu of the UI if we're in ASCII mode: */ - buspirate_send_bin(pgm, "\n\n", 2); + buspirate_send_bin(pgm, (const unsigned char*)"\n\n", 2); /* Clear input buffer: */ serial_drain(&pgm->fd, 0); @@ -683,7 +683,7 @@ static void buspirate_enable(struct programmer_t *pgm) /* Call buspirate_send_bin() instead of buspirate_send() * because we don't know if BP is in text or bin mode */ - rc = buspirate_send_bin(pgm, reset_str, strlen(reset_str)); + rc = buspirate_send_bin(pgm, (const unsigned char*)reset_str, strlen(reset_str)); if (rc) { avrdude_message(MSG_INFO, "BusPirate is not responding. Serial port error: %d\n", rc); return; @@ -696,18 +696,18 @@ static void buspirate_enable(struct programmer_t *pgm) return; } if (strncmp(rcvd, "Are you sure?", 13) == 0) { - buspirate_send_bin(pgm, accept_str, strlen(accept_str)); + buspirate_send_bin(pgm, (const unsigned char*)accept_str, strlen(accept_str)); } if (strncmp(rcvd, "RESET", 5) == 0) { print_banner = 1; continue; } if (buspirate_is_prompt(rcvd)) { - puts("**"); + avrdude_message(MSG_DEBUG, "**\n"); break; } if (print_banner) - avrdude_message(MSG_INFO, "** %s", rcvd); + avrdude_message(MSG_DEBUG, "** %s", rcvd); } if (!(pgm->flag & BP_FLAG_IN_BINMODE)) { @@ -797,8 +797,8 @@ static int buspirate_cmd_bin(struct programmer_t *pgm, if (rv == 0) return -1; - buspirate_send_bin(pgm, (char *)cmd, 4); - buspirate_recv_bin(pgm, (char *)res, 4); + buspirate_send_bin(pgm, cmd, 4); + buspirate_recv_bin(pgm, res, 4); return 0; } @@ -817,7 +817,7 @@ static int buspirate_cmd_ascii(struct programmer_t *pgm, while (i < 4) { rcvd = buspirate_readline(pgm, NULL, 0); /* WRITE: 0xAC READ: 0x04 */ - if (sscanf(rcvd, "WRITE: 0x%x READ: 0x%x", &spi_write, &spi_read) == 2) { + if (sscanf(rcvd, "WRITE: 0x%2x READ: 0x%2x", &spi_write, &spi_read) == 2) { res[i++] = spi_read; } if (buspirate_is_prompt(rcvd)) @@ -873,7 +873,8 @@ static int buspirate_paged_load( } // send command to read data - strncpy(commandbuf, "\x6\x2", 2); + commandbuf[0] = 6; + commandbuf[1] = 2; // send start address (in WORDS, not bytes!) commandbuf[2] = (address >> 1 >> 24) & 0xff; @@ -896,7 +897,7 @@ static int buspirate_paged_load( return -1; } - for (addr = 0; addr < n_bytes; addr++) { + for (addr = 0; addr < n_bytes; addr++) { buspirate_recv_bin(pgm, &m->buf[addr+address], 1); } @@ -914,8 +915,8 @@ static int buspirate_paged_write(struct programmer_t *pgm, int addr = base_addr; int n_page_writes; int this_page_size; - char cmd_buf[4096] = {'\0'}; - char send_byte, recv_byte; + unsigned char cmd_buf[4096] = {'\0'}; + unsigned char send_byte, recv_byte; if (!(pgm->flag & BP_FLAG_IN_BINMODE)) { /* Return if we are not in binary mode. */ @@ -1130,7 +1131,7 @@ void buspirate_initpgm(struct programmer_t *pgm) static void buspirate_bb_enable(struct programmer_t *pgm) { - char buf[20] = { '\0' }; + unsigned char buf[20] = { '\0' }; if (bitbang_check_prerequisites(pgm) < 0) return; /* XXX should treat as error */ @@ -1138,7 +1139,7 @@ static void buspirate_bb_enable(struct programmer_t *pgm) avrdude_message(MSG_INFO, "Attempting to initiate BusPirate bitbang binary mode...\n"); /* Send two CRs to ensure we're not in a sub-menu of the UI if we're in ASCII mode: */ - buspirate_send_bin(pgm, "\n\n", 2); + buspirate_send_bin(pgm, (const unsigned char*)"\n\n", 2); /* Clear input buffer: */ serial_drain(&pgm->fd, 0); @@ -1149,7 +1150,7 @@ static void buspirate_bb_enable(struct programmer_t *pgm) /* Expecting 'BBIOx' reply */ memset(buf, 0, sizeof(buf)); buspirate_recv_bin(pgm, buf, 5); - if (sscanf(buf, "BBIO%d", &PDATA(pgm)->binmode_version) != 1) { + if (sscanf((char*)buf, "BBIO%1d", &PDATA(pgm)->binmode_version) != 1) { avrdude_message(MSG_INFO, "Binary mode not confirmed: '%s'\n", buf); buspirate_reset_from_binmode(pgm); return; @@ -1219,8 +1220,7 @@ static int buspirate_bb_getpin(struct programmer_t *pgm, int pinfunc) if (buf[0] & (1 << (pin - 1))) value ^= 1; - if (verbose > 1) - printf("get pin %d = %d\n", pin, value); + avrdude_message(MSG_DEBUG, "get pin %d = %d\n", pin, value); return value; } @@ -1237,8 +1237,7 @@ static int buspirate_bb_setpin_internal(struct programmer_t *pgm, int pin, int v if ((pin < 1 || pin > 5) && (pin != 7)) // 7 is POWER return -1; - if (verbose > 1) - printf("set pin %d = %d\n", pin, value); + avrdude_message(MSG_DEBUG, "set pin %d = %d\n", pin, value); if (value) PDATA(pgm)->pin_val |= (1 << (pin - 1)); diff --git a/config_gram.y b/config_gram.y index 3b41ca11..0aa95a8e 100644 --- a/config_gram.y +++ b/config_gram.y @@ -555,6 +555,11 @@ prog_parm_usb: usb_pid_list: TKN_NUMBER { + { + /* overwrite pids, so clear the existing entries */ + ldestroy_cb(current_prog->usbpid, free); + current_prog->usbpid = lcreat(NULL, 0); + } { int *ip = malloc(sizeof(int)); if (ip) { diff --git a/dfu.c b/dfu.c index 3953b6a1..8e014e3c 100644 --- a/dfu.c +++ b/dfu.c @@ -139,7 +139,8 @@ struct dfu_dev * dfu_open(char *port_spec) if (dfu == NULL) { avrdude_message(MSG_INFO, "%s: out of memory\n", progname); - return 0; + free(bus_name); + return NULL; } dfu->bus_name = bus_name; diff --git a/fileio.c b/fileio.c index cc68a4b3..adfde915 100644 --- a/fileio.c +++ b/fileio.c @@ -1493,6 +1493,8 @@ int fileio(int op, char * filename, FILEFMT format, } if (format == FMT_AUTO) { + int format_detect; + if (using_stdio) { avrdude_message(MSG_INFO, "%s: can't auto detect file format when using stdin/out.\n" "%s Please specify a file format and try again.\n", @@ -1500,12 +1502,13 @@ int fileio(int op, char * filename, FILEFMT format, return -1; } - format = fmt_autodetect(fname); - if (format < 0) { + format_detect = fmt_autodetect(fname); + if (format_detect < 0) { avrdude_message(MSG_INFO, "%s: can't determine file format for %s, specify explicitly\n", progname, fname); return -1; } + format = format_detect; if (quell_progress < 2) { avrdude_message(MSG_INFO, "%s: %s file %s auto detected as %s\n", diff --git a/libavrdude.h b/libavrdude.h index 7edcad50..b7ab381b 100644 --- a/libavrdude.h +++ b/libavrdude.h @@ -545,7 +545,7 @@ struct serial_device int (*setspeed)(union filedescriptor *fd, long baud); void (*close)(union filedescriptor *fd); - int (*send)(union filedescriptor *fd, unsigned char * buf, size_t buflen); + int (*send)(union filedescriptor *fd, const unsigned char * buf, size_t buflen); int (*recv)(union filedescriptor *fd, unsigned char * buf, size_t buflen); int (*drain)(union filedescriptor *fd, int display); diff --git a/pickit2.c b/pickit2.c index b5c8a04c..bbf8b9ad 100644 --- a/pickit2.c +++ b/pickit2.c @@ -75,13 +75,13 @@ #endif #if 0 -#define DEBUG(...) do { avrdude_message(MSG_INFO, __VA_ARGS__); } while(0) +#define DEBUG(...) do { avrdude_message(MSG_DEBUG, __VA_ARGS__); } while(0) #else #define DEBUG(...) ((void)0) #endif #if 0 -#define DEBUGRECV(...) do { avrdude_message(MSG_INFO, __VA_ARGS__); } while(0) +#define DEBUGRECV(...) do { avrdude_message(MSG_DEBUG, __VA_ARGS__); } while(0) #else #define DEBUGRECV(...) ((void)0) #endif @@ -108,7 +108,7 @@ static int usb_open_device(struct usb_dev_handle **dev, int vid, int pid); #define USB_ERROR_IO 5 #endif // WIN32NATIVE -static int pickit2_write_report(PROGRAMMER *pgm, unsigned char report[65]); +static int pickit2_write_report(PROGRAMMER *pgm, const unsigned char report[65]); static int pickit2_read_report(PROGRAMMER *pgm, unsigned char report[65]); #ifndef MIN @@ -198,14 +198,14 @@ static int pickit2_open(PROGRAMMER * pgm, char * port) else { // get the device description while we're at it - short buff[PGM_DESCLEN], i; - HidD_GetProductString(PDATA(pgm)->usb_handle, buff, PGM_DESCLEN); + short buff[PGM_DESCLEN-1], i; + HidD_GetProductString(PDATA(pgm)->usb_handle, buff, PGM_DESCLEN-1); - // convert from wide chars + // convert from wide chars, but do not overwrite trailing '\0' memset(&(pgm->type), 0, PGM_DESCLEN); - for (i = 0; i < PGM_DESCLEN && buff[i]; i++) + for (i = 0; i < (PGM_DESCLEN-1) && buff[i]; i++) { - pgm->type[i] = (char)buff[i]; + pgm->type[i] = (char)buff[i]; // TODO what about little/big endian??? } } #else @@ -255,17 +255,18 @@ static int pickit2_initialize(PROGRAMMER * pgm, AVRPART * p) pgm->set_sck_period(pgm, pgm->bitclock); /* connect to target device -- we'll just ask for the firmware version */ - char report[65] = {0, CMD_GET_VERSION, CMD_END_OF_BUFFER}; + static const unsigned char report[65] = {0, CMD_GET_VERSION, CMD_END_OF_BUFFER}; if ((errorCode = pickit2_write_report(pgm, report)) > 0) { - memset(report, 0, sizeof(report)); + unsigned char report[65] = {0}; + //memset(report, 0, sizeof(report)); if ((errorCode = pickit2_read_report(pgm, report)) >= 4) { avrdude_message(MSG_NOTICE, "%s: %s firmware version %d.%d.%d\n", progname, pgm->desc, (int)report[1], (int)report[2], (int)report[3]); // set the pins, apply reset, // TO DO: apply vtarget (if requested though -x option) - char report[65] = + unsigned char report[65] = { 0, CMD_SET_VDD_4(5), CMD_SET_VPP_4(5), @@ -321,7 +322,7 @@ static int pickit2_initialize(PROGRAMMER * pgm, AVRPART * p) static void pickit2_disable(PROGRAMMER * pgm) { /* make sure all pins are floating & all voltages are off */ - uint8_t report[65] = + static const unsigned char report[65] = { 0, CMD_EXEC_SCRIPT_2(8), SCR_SET_PINS_2(1,1,0,0), @@ -430,16 +431,15 @@ static int pickit2_program_enable(struct programmer_t * pgm, AVRPART * p) avr_set_bits(p->op[AVR_OP_PGM_ENABLE], cmd); pgm->cmd(pgm, cmd, res); - if (verbose) { int i; - avrdude_message(MSG_NOTICE, "program_enable(): sending command. Resp = "); + avrdude_message(MSG_DEBUG, "program_enable(): sending command. Resp = "); for (i = 0; i < 4; i++) { - avrdude_message(MSG_NOTICE, "%x ", (int)res[i]); + avrdude_message(MSG_DEBUG, "%x ", (int)res[i]); } - avrdude_message(MSG_NOTICE, "\n"); + avrdude_message(MSG_DEBUG, "\n"); } // check for sync character @@ -837,7 +837,7 @@ static HANDLE open_hid(unsigned short vid, unsigned short pid) LONG Result; // were global, now just local scrap - int Length = 0; + DWORD Length = 0; PSP_DEVICE_INTERFACE_DETAIL_DATA detailData = NULL; HANDLE DeviceHandle=NULL; GUID HidGuid; @@ -1087,7 +1087,7 @@ static int usb_read_interrupt(PROGRAMMER *pgm, void *buff, int size, int timeout } // simple write with timeout -static int usb_write_interrupt(PROGRAMMER *pgm, void *buff, int size, int timeout) +static int usb_write_interrupt(PROGRAMMER *pgm, const void *buff, int size, int timeout) { OVERLAPPED ovr; DWORD bytesWritten = 0; @@ -1112,9 +1112,9 @@ static int usb_write_interrupt(PROGRAMMER *pgm, void *buff, int size, int timeou return bytesWritten > 0 ? bytesWritten : -1; } -static int pickit2_write_report(PROGRAMMER * pgm, unsigned char report[65]) +static int pickit2_write_report(PROGRAMMER * pgm, const unsigned char report[65]) { - return usb_write_interrupt(pgm, report, 65, PDATA(pgm)->transaction_timeout); + return usb_write_interrupt(pgm, report, 65, PDATA(pgm)->transaction_timeout); // XXX } static int pickit2_read_report(PROGRAMMER * pgm, unsigned char report[65]) @@ -1183,16 +1183,16 @@ static int usb_open_device(struct usb_dev_handle **device, int vendor, int produ return -1; } -static int pickit2_write_report(PROGRAMMER * pgm, unsigned char report[65]) +static int pickit2_write_report(PROGRAMMER * pgm, const unsigned char report[65]) { // endpoint 1 OUT?? - return usb_interrupt_write(PDATA(pgm)->usb_handle, USB_ENDPOINT_OUT | 1, report+1, 64, PDATA(pgm)->transaction_timeout); + return usb_interrupt_write(PDATA(pgm)->usb_handle, USB_ENDPOINT_OUT | 1, (const char*)(report+1), 64, PDATA(pgm)->transaction_timeout); } static int pickit2_read_report(PROGRAMMER * pgm, unsigned char report[65]) { // endpoint 1 IN?? - return usb_interrupt_read(PDATA(pgm)->usb_handle, USB_ENDPOINT_IN | 1, report+1, 64, PDATA(pgm)->transaction_timeout); + return usb_interrupt_read(PDATA(pgm)->usb_handle, USB_ENDPOINT_IN | 1, (char*)(report+1), 64, PDATA(pgm)->transaction_timeout); } #endif // WIN323NATIVE diff --git a/safemode.c b/safemode.c index 190ee969..b530a5fc 100644 --- a/safemode.c +++ b/safemode.c @@ -27,10 +27,10 @@ #include "libavrdude.h" /* This value from ac_cfg.h */ -/* +/* * Writes the specified fuse in fusename (can be "lfuse", "hfuse", or * "efuse") and verifies it. Will try up to tries amount of times - * before giving up + * before giving up */ int safemode_writefuse (unsigned char fuse, char * fusename, PROGRAMMER * pgm, AVRPART * p, int tries) @@ -38,13 +38,13 @@ int safemode_writefuse (unsigned char fuse, char * fusename, PROGRAMMER * pgm, AVRMEM * m; unsigned char fuseread; int returnvalue = -1; - + m = avr_locate_mem(p, fusename); if (m == NULL) { return -1; } - - /* Keep trying to write then read back the fuse values */ + + /* Keep trying to write then read back the fuse values */ while (tries > 0) { if (avr_write_byte(pgm, p, m, 0, fuse) != 0) { @@ -54,11 +54,11 @@ int safemode_writefuse (unsigned char fuse, char * fusename, PROGRAMMER * pgm, { continue; } - + /* Report information to user if needed */ avrdude_message(MSG_NOTICE, "%s: safemode: Wrote %s to %x, read as %x. %d attempts left\n", progname, fusename, fuse, fuseread, tries-1); - + /* If fuse wrote OK, no need to keep going */ if (fuse == fuseread) { tries = 0; @@ -66,17 +66,17 @@ int safemode_writefuse (unsigned char fuse, char * fusename, PROGRAMMER * pgm, } tries--; } - + return returnvalue; } -/* +/* * Reads the fuses three times, checking that all readings are the - * same. This will ensure that the before values aren't in error! + * same. This will ensure that the before values aren't in error! */ -int safemode_readfuses (unsigned char * lfuse, unsigned char * hfuse, - unsigned char * efuse, unsigned char * fuse, - PROGRAMMER * pgm, AVRPART * p) +int safemode_readfuses (unsigned char * lfuse, unsigned char * hfuse, + unsigned char * efuse, unsigned char * fuse, + PROGRAMMER * pgm, AVRPART * p) { unsigned char value; @@ -87,14 +87,14 @@ int safemode_readfuses (unsigned char * lfuse, unsigned char * hfuse, unsigned char safemode_efuse; unsigned char safemode_fuse; AVRMEM * m; - + safemode_lfuse = *lfuse; safemode_hfuse = *hfuse; safemode_efuse = *efuse; safemode_fuse = *fuse; - /* Read fuse three times */ + /* Read fuse three times */ fusegood = 2; /* If AVR device doesn't support this fuse, don't want to generate a verify error */ m = avr_locate_mem(p, "fuse"); @@ -121,25 +121,25 @@ int safemode_readfuses (unsigned char * lfuse, unsigned char * hfuse, fusegood = 1; /* Fuse read OK three times */ } } - } + } - //Programmer does not allow fuse reading.... no point trying anymore + //Programmer does not allow fuse reading.... no point trying anymore if (allowfuseread == 0) - { - return -5; - } + { + return -5; + } - if (fusegood == 0) { + if (fusegood == 0) { avrdude_message(MSG_INFO, "%s: safemode: Verify error - unable to read fuse properly. " "Programmer may not be reliable.\n", progname); return -1; } - else if ((fusegood == 1)) { + else if (fusegood == 1) { avrdude_message(MSG_NOTICE, "%s: safemode: fuse reads as %X\n", progname, safemode_fuse); } - /* Read lfuse three times */ + /* Read lfuse three times */ fusegood = 2; /* If AVR device doesn't support this fuse, don't want to generate a verify error */ m = avr_locate_mem(p, "lfuse"); @@ -167,14 +167,14 @@ int safemode_readfuses (unsigned char * lfuse, unsigned char * hfuse, } } - //Programmer does not allow fuse reading.... no point trying anymore + //Programmer does not allow fuse reading.... no point trying anymore if (allowfuseread == 0) - { - return -5; - } + { + return -5; + } - if (fusegood == 0) { + if (fusegood == 0) { avrdude_message(MSG_INFO, "%s: safemode: Verify error - unable to read lfuse properly. " "Programmer may not be reliable.\n", progname); return -1; @@ -183,7 +183,7 @@ int safemode_readfuses (unsigned char * lfuse, unsigned char * hfuse, avrdude_message(MSG_DEBUG, "%s: safemode: lfuse reads as %X\n", progname, safemode_lfuse); } - /* Read hfuse three times */ + /* Read hfuse three times */ fusegood = 2; /* If AVR device doesn't support this fuse, don't want to generate a verify error */ m = avr_locate_mem(p, "hfuse"); @@ -211,11 +211,11 @@ int safemode_readfuses (unsigned char * lfuse, unsigned char * hfuse, } } - //Programmer does not allow fuse reading.... no point trying anymore + //Programmer does not allow fuse reading.... no point trying anymore if (allowfuseread == 0) - { - return -5; - } + { + return -5; + } if (fusegood == 0) { avrdude_message(MSG_INFO, "%s: safemode: Verify error - unable to read hfuse properly. " @@ -226,7 +226,7 @@ int safemode_readfuses (unsigned char * lfuse, unsigned char * hfuse, avrdude_message(MSG_NOTICE, "%s: safemode: hfuse reads as %X\n", progname, safemode_hfuse); } - /* Read efuse three times */ + /* Read efuse three times */ fusegood = 2; /* If AVR device doesn't support this fuse, don't want to generate a verify error */ m = avr_locate_mem(p, "efuse"); @@ -253,14 +253,14 @@ int safemode_readfuses (unsigned char * lfuse, unsigned char * hfuse, } } } - - //Programmer does not allow fuse reading.... no point trying anymore + + //Programmer does not allow fuse reading.... no point trying anymore if (allowfuseread == 0) - { - return -5; - } - - if (fusegood == 0) { + { + return -5; + } + + if (fusegood == 0) { avrdude_message(MSG_INFO, "%s: safemode: Verify error - unable to read efuse properly. " "Programmer may not be reliable.\n", progname); return -3; @@ -294,11 +294,11 @@ int safemode_memfuses (int save, unsigned char * lfuse, unsigned char * hfuse, static unsigned char safemode_hfuse = 0xff; static unsigned char safemode_efuse = 0xff; static unsigned char safemode_fuse = 0xff; - + switch (save) { - + /* Save the fuses as safemode setting */ - case 1: + case 1: safemode_lfuse = *lfuse; safemode_hfuse = *hfuse; safemode_efuse = *efuse; @@ -313,6 +313,6 @@ int safemode_memfuses (int save, unsigned char * lfuse, unsigned char * hfuse, *fuse = safemode_fuse; break; } - + return 0; } diff --git a/ser_avrdoper.c b/ser_avrdoper.c index a74d6b06..71080b7c 100644 --- a/ser_avrdoper.c +++ b/ser_avrdoper.c @@ -443,7 +443,7 @@ static int usbGetReport(union filedescriptor *fdp, int reportType, int reportNum /* ------------------------------------------------------------------------- */ -static void dumpBlock(char *prefix, unsigned char *buf, int len) +static void dumpBlock(const char *prefix, const unsigned char *buf, int len) { int i; @@ -542,7 +542,7 @@ static int chooseDataSize(int len) return i - 1; } -static int avrdoper_send(union filedescriptor *fdp, unsigned char *buf, size_t buflen) +static int avrdoper_send(union filedescriptor *fdp, const unsigned char *buf, size_t buflen) { if(verbose > 3) dumpBlock("Send", buf, buflen); diff --git a/ser_posix.c b/ser_posix.c index 31119f95..beeb9bdc 100644 --- a/ser_posix.c +++ b/ser_posix.c @@ -309,10 +309,10 @@ static void ser_close(union filedescriptor *fd) } -static int ser_send(union filedescriptor *fd, unsigned char * buf, size_t buflen) +static int ser_send(union filedescriptor *fd, const unsigned char * buf, size_t buflen) { int rc; - unsigned char * p = buf; + const unsigned char * p = buf; size_t len = buflen; if (!len) diff --git a/ser_win32.c b/ser_win32.c index 2d2da4a6..ed5c6bb7 100644 --- a/ser_win32.c +++ b/ser_win32.c @@ -220,12 +220,12 @@ static int ser_set_dtr_rts(union filedescriptor *fd, int is_on) } -static int ser_send(union filedescriptor *fd, unsigned char * buf, size_t buflen) +static int ser_send(union filedescriptor *fd, const unsigned char * buf, size_t buflen) { size_t len = buflen; unsigned char c='\0'; DWORD written; - unsigned char * b = buf; + const unsigned char * b = buf; HANDLE hComPort=(HANDLE)fd->pfd; diff --git a/stk500v2.c b/stk500v2.c index a26abfa0..6c986c0f 100644 --- a/stk500v2.c +++ b/stk500v2.c @@ -3194,7 +3194,7 @@ f_to_kHz_MHz(double f, const char **unit) static void stk500v2_print_parms1(PROGRAMMER * pgm, const char * p) { - unsigned char vtarget, vadjust, osc_pscale, osc_cmatch, sck_duration; + unsigned char vtarget, vadjust, osc_pscale, osc_cmatch, sck_duration =0; //XXX 0 is not correct, check caller unsigned int sck_stk600, clock_conf, dac, oct, varef; unsigned char vtarget_jtag[4]; int prescale; diff --git a/usb_libusb.c b/usb_libusb.c index ef7d8c7f..eac99296 100644 --- a/usb_libusb.c +++ b/usb_libusb.c @@ -321,12 +321,12 @@ static void usbdev_close(union filedescriptor *fd) } -static int usbdev_send(union filedescriptor *fd, unsigned char *bp, size_t mlen) +static int usbdev_send(union filedescriptor *fd, const unsigned char *bp, size_t mlen) { usb_dev_handle *udev = (usb_dev_handle *)fd->usb.handle; int rv; int i = mlen; - unsigned char * p = bp; + const unsigned char * p = bp; int tx_size; if (udev == NULL) @@ -514,14 +514,30 @@ static int usbdev_recv_frame(union filedescriptor *fd, unsigned char *buf, size_ memcpy (buf, usbbuf, rv); buf += rv; } + else + { + return -1; // buffer overflow + } n += rv; nbytes -= rv; } while (nbytes > 0 && rv == fd->usb.max_xfer); - if (nbytes < 0) - return -1; +/* + this ends when the buffer is completly filled (nbytes=0) or was too small (nbytes< 0) + or a short packet is found. + however we cannot say for nbytes=0 that there was really a packet completed, + we had to check the last rv value than for a short packet, + but what happens if the packet does not end with a short packet? + and what if the buffer is filled without the packet was completed? + + preconditions: + expected packet is not a multiple of usb.max_xfer. (prevents further waiting) + + expected packet is shorter than the provided buffer (so it cannot filled completely) + or buffer size is not a multiple of usb.max_xfer. (so it can clearly detected if the buffer was overflown.) +*/ printout: if (verbose > 3) diff --git a/usbasp.c b/usbasp.c index 96146a51..df2bb841 100644 --- a/usbasp.c +++ b/usbasp.c @@ -247,7 +247,7 @@ static int usbasp_transmit(PROGRAMMER * pgm, functionid & 0xff, ((send[1] << 8) | send[0]) & 0xffff, ((send[3] << 8) | send[2]) & 0xffff, - (char *)buffer, + buffer, buffersize & 0xffff, 5000); if(nbytes < 0){ @@ -321,7 +321,7 @@ static int usbOpenDevice(libusb_device_handle **device, int vendor, errorCode = 0; /* now check whether the names match: */ /* if vendorName not given ignore it (any vendor matches) */ - r = libusb_get_string_descriptor_ascii(handle, descriptor.iManufacturer & 0xff, string, sizeof(string)); + r = libusb_get_string_descriptor_ascii(handle, descriptor.iManufacturer & 0xff, (unsigned char*)string, sizeof(string)); if (r < 0) { if ((vendorName != NULL) && (vendorName[0] != 0)) { errorCode = USB_ERROR_IO; @@ -335,7 +335,7 @@ static int usbOpenDevice(libusb_device_handle **device, int vendor, errorCode = USB_ERROR_NOTFOUND; } /* if productName not given ignore it (any product matches) */ - r = libusb_get_string_descriptor_ascii(handle, descriptor.iProduct & 0xff, string, sizeof(string)); + r = libusb_get_string_descriptor_ascii(handle, descriptor.iProduct & 0xff, (unsigned char*)string, sizeof(string)); if (r < 0) { if ((productName != NULL) && (productName[0] != 0)) { errorCode = USB_ERROR_IO;