Open programmer before allocating part

So any programmer that knows exactly which part it is connected to
can set the -p part option should the user not have done so.

Also no longer exits main without closing open programmer.
This commit is contained in:
Stefan Rueger 2022-11-01 18:06:52 +00:00
parent b5699663c4
commit 02fe08aed2
No known key found for this signature in database
GPG Key ID: B0B4F1FD86B1EC55
4 changed files with 103 additions and 86 deletions

View File

@ -31,13 +31,13 @@
#define XDG_USER_CONF_FILE "avrdude/avrdude.rc" #define XDG_USER_CONF_FILE "avrdude/avrdude.rc"
#endif #endif
extern char *progname; // name of program, for messages extern char *progname; // Name of program, for messages
extern char progbuf[]; // spaces same length as progname extern char progbuf[]; // Spaces same length as progname
extern int ovsigck; // override signature check (-F) extern int ovsigck; // Override signature check (-F)
extern int verbose; // verbosity level (-v, -vv, ...) extern int verbose; // Verbosity level (-v, -vv, ...)
extern int quell_progress; // quell progress report -q, reduce effective verbosity level (-qq, -qqq) extern int quell_progress; // Quell progress report -q, reduce effective verbosity level (-qq, -qqq)
extern char *partdesc; // part id extern const char *partdesc; // Part id
int avrdude_message(int msglvl, const char *format, ...); int avrdude_message(int msglvl, const char *format, ...);
int avrdude_message2(FILE *fp, int lno, const char *file, const char *func, int msgmode, int msglvl, const char *format, ...); int avrdude_message2(FILE *fp, int lno, const char *file, const char *func, int msgmode, int msglvl, const char *format, ...);

View File

@ -799,7 +799,7 @@ static void dev_part_strct(const AVRPART *p, bool tsv, const AVRPART *base, bool
} }
void dev_output_pgm_part(int dev_opt_c, char *programmer, int dev_opt_p, char *partdesc) { void dev_output_pgm_part(int dev_opt_c, const char *programmer, int dev_opt_p, const char *partdesc) {
if(dev_opt_c == 2 && dev_opt_p == 2) { if(dev_opt_c == 2 && dev_opt_p == 2) {
char *p; char *p;

View File

@ -19,7 +19,7 @@
#ifndef developer_opts_h #ifndef developer_opts_h
#define developer_opts_h #define developer_opts_h
void dev_output_pgm_part(int dev_opt_c, char *programmer, int dev_opt_p, char *partdesc); void dev_output_pgm_part(int dev_opt_c, const char *programmer, int dev_opt_p, const char *partdesc);
void dev_output_part_defs(char *partdesc); void dev_output_part_defs(char *partdesc);
void dev_output_pgm_defs(char *programmer); void dev_output_pgm_defs(char *programmer);

View File

@ -151,10 +151,10 @@ static PROGRAMMER * pgm;
/* /*
* global options * global options
*/ */
int verbose; /* verbose output */ int verbose; // Verbose output
int quell_progress; /* un-verebose output */ int quell_progress; // Quell progress report and un-verbose output
int ovsigck; /* 1=override sig check, 0=don't */ int ovsigck; // 1 = override sig check, 0 = don't
char *partdesc; /* part id */ const char *partdesc; // Part id
/* /*
@ -398,7 +398,7 @@ static void replace_backslashes(char *s)
} }
// Return 2 if string is * or starts with */, 1 if string contains /, 0 otherwise // Return 2 if string is * or starts with */, 1 if string contains /, 0 otherwise
static int dev_opt(char *str) { static int dev_opt(const char *str) {
return return
!str? 0: !str? 0:
!strcmp(str, "*") || !strncmp(str, "*/", 2)? 2: !strcmp(str, "*") || !strncmp(str, "*/", 2)? 2:
@ -406,7 +406,7 @@ static int dev_opt(char *str) {
} }
static void exit_programmer_not_found(const char *programmer) { static void programmer_not_found(const char *programmer) {
msg_error("\n"); msg_error("\n");
if(programmer && *programmer) if(programmer && *programmer)
pmsg_error("cannot find programmer id %s\n", programmer); pmsg_error("cannot find programmer id %s\n", programmer);
@ -418,11 +418,9 @@ static void exit_programmer_not_found(const char *programmer) {
msg_error("\nValid programmers are:\n"); msg_error("\nValid programmers are:\n");
list_programmers(stderr, " ", programmers, ~0); list_programmers(stderr, " ", programmers, ~0);
msg_error("\n"); msg_error("\n");
exit(1);
} }
static void exit_part_not_found(const char *partdesc) { static void part_not_found(const char *partdesc) {
msg_error("\n"); msg_error("\n");
if(partdesc && *partdesc) if(partdesc && *partdesc)
pmsg_error("AVR part %s not found\n", partdesc); pmsg_error("AVR part %s not found\n", partdesc);
@ -432,8 +430,6 @@ static void exit_part_not_found(const char *partdesc) {
msg_error("\nValid parts are:\n"); msg_error("\nValid parts are:\n");
list_parts(stderr, " ", part_list, ~0); list_parts(stderr, " ", part_list, ~0);
msg_error("\n"); msg_error("\n");
exit(1);
} }
@ -982,8 +978,10 @@ int main(int argc, char * argv [])
if (strcmp(partdesc, "?") == 0) { if (strcmp(partdesc, "?") == 0) {
if(programmer && *programmer) { if(programmer && *programmer) {
PROGRAMMER *pgm = locate_programmer(programmers, programmer); PROGRAMMER *pgm = locate_programmer(programmers, programmer);
if(!pgm) if(!pgm) {
exit_programmer_not_found(programmer); programmer_not_found(programmer);
exit(1);
}
msg_error("\nValid parts for programmer %s are:\n", programmer); msg_error("\nValid parts for programmer %s are:\n", programmer);
list_parts(stderr, " ", part_list, pgm->prog_modes); list_parts(stderr, " ", part_list, pgm->prog_modes);
} else { } else {
@ -999,8 +997,10 @@ int main(int argc, char * argv [])
if (strcmp(programmer, "?") == 0) { if (strcmp(programmer, "?") == 0) {
if(partdesc && *partdesc) { if(partdesc && *partdesc) {
AVRPART *p = locate_part(part_list, partdesc); AVRPART *p = locate_part(part_list, partdesc);
if(!p) if(!p) {
exit_part_not_found(partdesc); part_not_found(partdesc);
exit(1);
}
msg_error("\nValid programmers for part %s are:\n", p->desc); msg_error("\nValid programmers for part %s are:\n", p->desc);
list_programmers(stderr, " ", programmers, p->prog_modes); list_programmers(stderr, " ", programmers, p->prog_modes);
} else { } else {
@ -1021,12 +1021,16 @@ int main(int argc, char * argv [])
msg_notice("\n"); msg_notice("\n");
if (!programmer || !*programmer) if (!programmer || !*programmer) {
exit_programmer_not_found(NULL); programmer_not_found(NULL);
exit(1);
}
pgm = locate_programmer(programmers, programmer); pgm = locate_programmer(programmers, programmer);
if (pgm == NULL) if (pgm == NULL) {
exit_programmer_not_found(programmer); programmer_not_found(programmer);
exit(1);
}
if (pgm->initpgm) { if (pgm->initpgm) {
pgm->initpgm(pgm); pgm->initpgm(pgm);
@ -1077,56 +1081,6 @@ int main(int argc, char * argv [])
} }
} }
if (partdesc == NULL)
exit_part_not_found(NULL);
p = locate_part(part_list, partdesc);
if (p == NULL)
exit_part_not_found(partdesc);
if (exitspecs != NULL) {
if (pgm->parseexitspecs == NULL) {
pmsg_warning("-E option not supported by this programmer type\n");
exitspecs = NULL;
}
else if (pgm->parseexitspecs(pgm, exitspecs) < 0) {
usage();
exit(1);
}
}
if (avr_initmem(p) != 0) {
msg_error("\n");
pmsg_error("unable to initialize memories\n");
exit(1);
}
/*
* Now that we know which part we are going to program, locate any -U
* options using the default memory region, fill in the device-dependent
* default region name ("application" for Xmega parts or "flash" otherwise)
* and check for basic problems with memory names or file access with a
* view to exit before programming.
*/
int doexit = 0;
for (ln=lfirst(updates); ln; ln=lnext(ln)) {
upd = ldata(ln);
if (upd->memtype == NULL) {
const char *mtype = p->prog_modes & PM_PDI? "application": "flash";
pmsg_notice2("defaulting memtype in -U %c:%s option to \"%s\"\n",
(upd->op == DEVICE_READ)? 'r': (upd->op == DEVICE_WRITE)? 'w': 'v',
upd->filename, mtype);
upd->memtype = cfg_strdup("main()", mtype);
}
rc = update_dryrun(p, upd);
if (rc && rc != LIBAVRDUDE_SOFTFAIL)
doexit = 1;
}
if(doexit)
exit(1);
/* /*
* open the programmer * open the programmer
*/ */
@ -1140,13 +1094,6 @@ int main(int argc, char * argv [])
if (verbose) { if (verbose) {
imsg_notice("Using Port : %s\n", port); imsg_notice("Using Port : %s\n", port);
imsg_notice("Using Programmer : %s\n", programmer); imsg_notice("Using Programmer : %s\n", programmer);
if ((strcmp(pgm->type, "avr910") == 0)) {
imsg_notice("avr910_devcode (avrdude.conf) : ");
if(p->avr910_devcode)
msg_notice("0x%x\n", p->avr910_devcode);
else
msg_notice("none\n");
}
} }
if (baudrate != 0) { if (baudrate != 0) {
@ -1173,6 +1120,75 @@ int main(int argc, char * argv [])
} }
is_open = 1; is_open = 1;
if (partdesc == NULL) {
part_not_found(NULL);
exitrc = 1;
goto main_exit;
}
p = locate_part(part_list, partdesc);
if (p == NULL) {
part_not_found(partdesc);
exitrc = 1;
goto main_exit;
}
if (exitspecs != NULL) {
if (pgm->parseexitspecs == NULL) {
pmsg_warning("-E option not supported by this programmer type\n");
exitspecs = NULL;
}
else if (pgm->parseexitspecs(pgm, exitspecs) < 0) {
usage();
exitrc = 1;
goto main_exit;
}
}
if (avr_initmem(p) != 0) {
msg_error("\n");
pmsg_error("unable to initialize memories\n");
exitrc = 1;
goto main_exit;
}
if(verbose) {
if ((strcmp(pgm->type, "avr910") == 0)) {
imsg_notice("avr910_devcode (avrdude.conf) : ");
if(p->avr910_devcode)
msg_notice("0x%02x\n", (uint8_t) p->avr910_devcode);
else
msg_notice("none\n");
}
}
/*
* Now that we know which part we are going to program, locate any -U
* options using the default memory region, fill in the device-dependent
* default region name ("application" for Xmega parts or "flash" otherwise)
* and check for basic problems with memory names or file access with a
* view to exit before programming.
*/
int doexit = 0;
for (ln=lfirst(updates); ln; ln=lnext(ln)) {
upd = ldata(ln);
if (upd->memtype == NULL) {
const char *mtype = p->prog_modes & PM_PDI? "application": "flash";
pmsg_notice2("defaulting memtype in -U %c:%s option to \"%s\"\n",
(upd->op == DEVICE_READ)? 'r': (upd->op == DEVICE_WRITE)? 'w': 'v',
upd->filename, mtype);
upd->memtype = cfg_strdup("main()", mtype);
}
rc = update_dryrun(p, upd);
if (rc && rc != LIBAVRDUDE_SOFTFAIL)
doexit = 1;
}
if(doexit) {
exitrc = 1;
goto main_exit;
}
if (calibrate) { if (calibrate) {
/* /*
* perform an RC oscillator calibration * perform an RC oscillator calibration
@ -1379,7 +1395,8 @@ int main(int argc, char * argv [])
} else { } else {
msg_info("erasing chip\n"); msg_info("erasing chip\n");
exitrc = avr_chip_erase(pgm, p); exitrc = avr_chip_erase(pgm, p);
if(exitrc) goto main_exit; if(exitrc)
goto main_exit;
} }
} }