From 1429550f8e02b7aa1d48e37e766a0cd5f1e13f27 Mon Sep 17 00:00:00 2001 From: joerg_wunsch Date: Wed, 13 Jan 2010 12:44:54 +0000 Subject: [PATCH] Cleanup the open/close handling to avoid accessing unallocated memory (in the atexit handler) in case of bailing out. git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@910 81a1dc3b-b13d-400b-aceb-764788c761c2 --- ChangeLog | 6 ++++++ jtagmkII_private.h | 1 + main.c | 12 ++++++++---- stk500v2.c | 35 ++++++++++++++++++++++++++--------- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7a0d6f08..c3ad4820 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2010-01-13 Joerg Wunsch + + * stk500v2.c: Cleanup the open/close handling to avoid accessing + unallocated memory (in the atexit handler) in case of bailing out. + * main.c: (Ditto.) + 2010-01-13 Joerg Wunsch * jtagmkII.c: Stylistic changes: move #defines out into diff --git a/jtagmkII_private.h b/jtagmkII_private.h index 3ef88db4..40e7327c 100644 --- a/jtagmkII_private.h +++ b/jtagmkII_private.h @@ -193,6 +193,7 @@ # define EMULATOR_MODE_HV 0x02 /* HVSP or PP mode of AVR Dragon */ # define EMULATOR_MODE_SPI 0x03 # define EMULATOR_MODE_JTAG_XMEGA 0x05 +# define EMULATOR_MODE_PDI 0x06 #define PAR_IREG 0x04 #define PAR_BAUD_RATE 0x05 # define PAR_BAUD_2400 0x01 diff --git a/main.c b/main.c index a929dae3..ea68791c 100644 --- a/main.c +++ b/main.c @@ -280,6 +280,7 @@ int main(int argc, char * argv []) int safemode; /* Enable safemode, 1=safemode on, 0=normal */ int silentsafe; /* Don't ask about fuses, 1=silent, 0=normal */ int init_ok; /* Device initialization worked well */ + int is_open; /* Device open succeeded */ unsigned char safemode_lfuse = 0xff; unsigned char safemode_hfuse = 0xff; unsigned char safemode_efuse = 0xff; @@ -350,6 +351,7 @@ int main(int argc, char * argv []) ispdelay = 0; safemode = 1; /* Safemode on by default */ silentsafe = 0; /* Ask by default */ + is_open = 0; if (isatty(STDIN_FILENO) == 0) safemode = 0; /* Turn off safemode if this isn't a terminal */ @@ -789,6 +791,7 @@ int main(int argc, char * argv []) pgm->ppidata = 0; /* clear all bits at exit */ goto main_exit; } + is_open = 1; if (calibrate) { /* @@ -1243,12 +1246,13 @@ main_exit: * program complete */ + if (is_open) { + pgm->powerdown(pgm); - pgm->powerdown(pgm); + pgm->disable(pgm); - pgm->disable(pgm); - - pgm->rdy_led(pgm, OFF); + pgm->rdy_led(pgm, OFF); + } pgm->close(pgm); diff --git a/stk500v2.c b/stk500v2.c index 6ff3ba75..bdf95d94 100644 --- a/stk500v2.c +++ b/stk500v2.c @@ -271,6 +271,7 @@ static int stk500v2_set_sck_period_mk2(PROGRAMMER * pgm, double v); static int stk600_set_sck_period(PROGRAMMER * pgm, double v); static void stk600_setup_xprog(PROGRAMMER * pgm); +static void stk600_setup_isp(PROGRAMMER * pgm); static int stk600_xprog_program_enable(PROGRAMMER * pgm, AVRPART * p); static void stk500v2_setup(PROGRAMMER * pgm) @@ -1056,13 +1057,17 @@ static int stk500hvsp_program_enable(PROGRAMMER * pgm, AVRPART * p) static int stk500v2_initialize(PROGRAMMER * pgm, AVRPART * p) { - if ((PDATA(pgm)->pgmtype == PGMTYPE_STK600 || PDATA(pgm)->pgmtype == PGMTYPE_AVRISP_MKII) != 0 + if ((PDATA(pgm)->pgmtype == PGMTYPE_STK600 || + PDATA(pgm)->pgmtype == PGMTYPE_AVRISP_MKII || + PDATA(pgm)->pgmtype == PGMTYPE_JTAGICE_MKII) != 0 && (p->flags & AVRPART_HAS_PDI) != 0) { /* * This is an ATxmega device, must use XPROG protocol for the * remaining actions. */ stk600_setup_xprog(pgm); + } else { + stk600_setup_isp(pgm); } return pgm->program_enable(pgm, p); @@ -2783,9 +2788,8 @@ static int stk500v2_jtagmkII_open(PROGRAMMER * pgm, char * port) if (jtagmkII_getsync(pgm, EMULATOR_MODE_SPI) != 0) { fprintf(stderr, "%s: failed to sync with the JTAG ICE mkII in ISP mode\n", progname); - pgm->close(pgm); /* sign off correctly */ pgm->cookie = mycookie; - exit(1); + return -1; } pgm->cookie = mycookie; @@ -2870,11 +2874,10 @@ static int stk500v2_dragon_isp_open(PROGRAMMER * pgm, char * port) mycookie = pgm->cookie; pgm->cookie = PDATA(pgm)->chained_pdata; if (jtagmkII_getsync(pgm, EMULATOR_MODE_SPI) != 0) { - fprintf(stderr, "%s: failed to sync with the JTAG ICE mkII in ISP mode\n", + fprintf(stderr, "%s: failed to sync with the AVR Dragon in ISP mode\n", progname); - pgm->close(pgm); /* sign off correctly */ pgm->cookie = mycookie; - exit(1); + return -1; } pgm->cookie = mycookie; @@ -2942,11 +2945,10 @@ static int stk500v2_dragon_hv_open(PROGRAMMER * pgm, char * port) mycookie = pgm->cookie; pgm->cookie = PDATA(pgm)->chained_pdata; if (jtagmkII_getsync(pgm, EMULATOR_MODE_HV) != 0) { - fprintf(stderr, "%s: failed to sync with the JTAG ICE mkII in HV mode\n", + fprintf(stderr, "%s: failed to sync with the AVR Dragon in HV mode\n", progname); - pgm->close(pgm); /* sign off correctly */ pgm->cookie = mycookie; - exit(1); + return -1; } pgm->cookie = mycookie; @@ -3446,6 +3448,21 @@ static void stk600_setup_xprog(PROGRAMMER * pgm) } +/* + * Modify pgm's methods for ISP operation. + */ +static void stk600_setup_isp(PROGRAMMER * pgm) +{ + pgm->program_enable = stk500v2_program_enable; + pgm->disable = stk500v2_disable; + pgm->read_byte = avr_read_byte_default; + pgm->write_byte = avr_write_byte_default; + pgm->paged_load = stk500v2_paged_load; + pgm->paged_write = stk500v2_paged_write; + pgm->chip_erase = stk500v2_chip_erase; +} + + void stk500v2_initpgm(PROGRAMMER * pgm) { strcpy(pgm->type, "STK500V2");