Implement -T cmdline to mix terminal commands with -U commands

This commit is contained in:
Stefan Rueger
2023-05-31 22:23:23 +01:00
parent 98c460268e
commit 1126ff1621
5 changed files with 54 additions and 22 deletions

View File

@@ -1051,10 +1051,11 @@ enum updateflags {
typedef struct update_t {
char * memtype;
int op;
char * filename;
int format;
const char *cmdline; // -T line is stored here and takes precedence if it exists
char *memtype; // Memory name for -U
int op; // Symbolic memory operation DEVICE_... for -U
char *filename; // Filename for -U, can be -
int format; // File format FMT_...
} UPDATE;
typedef struct { // File reads for flash can exclude trailing 0xff, which are cut off
@@ -1072,15 +1073,14 @@ typedef struct { // File reads for flash can exclude trailing 0xf
extern "C" {
#endif
extern UPDATE * parse_op(char * s);
extern UPDATE * dup_update(UPDATE * upd);
extern UPDATE * new_update(int op, char * memtype, int filefmt,
char * filename);
UPDATE *parse_op(char *s);
UPDATE *dup_update(UPDATE *upd);
UPDATE *new_update(int op, char *memtype, int filefmt, char *filename);
UPDATE *cmd_update(const char *cmd);
extern void free_update(UPDATE * upd);
extern int do_op(const PROGRAMMER *pgm, const AVRPART *p, UPDATE *upd,
enum updateflags flags);
extern int memstats(const AVRPART *p, const char *memtype, int size, Filestats *fsp);
int do_op(const PROGRAMMER *pgm, const AVRPART *p, UPDATE *upd,
enum updateflags flags);
int memstats(const AVRPART *p, const char *memtype, int size, Filestats *fsp);
// Convenience functions for printing
const char *update_plural(int x);
@@ -1245,6 +1245,12 @@ unsigned long long int str_int(const char *str, int type, const char **errpp);
int str_membuf(const char *str, int type, unsigned char *buf, int size, const char **errpp);
char *str_nexttok(char *buf, const char *delim, char **next);
int terminal_mode(const PROGRAMMER *pgm, const AVRPART *p);
int terminal_mode_noninteractive(const PROGRAMMER *pgm, const AVRPART *p);
int terminal_line(const PROGRAMMER *pgm, const AVRPART *p, const char *line);
char *terminal_get_input(const char *prompt);
void terminal_setup_update_progress();
#ifdef __cplusplus
}
#endif

View File

@@ -49,7 +49,6 @@
#include "avrdude.h"
#include "libavrdude.h"
#include "config.h"
#include "term.h"
#include "developer_opts.h"
/* Get VERSION from ac_cfg.h */
@@ -632,7 +631,8 @@ int main(int argc, char * argv [])
/*
* process command line arguments
*/
while ((ch = getopt(argc,argv,"?Ab:B:c:C:DeE:Fi:l:np:OP:qstU:uvVx:yY:")) != -1) {
int memwrite = 0, memterminal = 0;
while ((ch = getopt(argc,argv,"?Ab:B:c:C:DeE:Fi:l:np:OP:qstT:U:uvVx:yY:")) != -1) {
switch (ch) {
case 'b': /* override default programmer baud rate */
@@ -772,12 +772,28 @@ int main(int argc, char * argv [])
pmsg_error("\"safemode\" feature no longer supported\n");
break;
case 'T':
upd = (UPDATE *) cfg_malloc(__func__, sizeof *upd);
upd->cmdline = optarg;
if(memwrite) { // Invalidate cache if device was written to
memwrite = 0;
ladd(updates, cmd_update("abort # Reset cache"));
}
memterminal = 1;
ladd(updates, upd);
break;
case 'U':
upd = parse_op(optarg);
if (upd == NULL) {
pmsg_error("unable to parse update operation '%s'\n", optarg);
exit(1);
}
if(memterminal) { // Flush cache before any device memory access
memterminal = 0;
ladd(updates, cmd_update("flush"));
}
memwrite |= upd->op == DEVICE_WRITE;
ladd(updates, upd);
break;
@@ -815,6 +831,9 @@ int main(int argc, char * argv [])
}
if(memterminal)
ladd(updates, cmd_update("flush"));
if (logfile != NULL) {
FILE *newstderr = freopen(logfile, "w", stderr);
if (newstderr == NULL) {
@@ -1235,7 +1254,7 @@ int main(int argc, char * argv [])
int doexit = 0;
for (ln=lfirst(updates); ln; ln=lnext(ln)) {
upd = ldata(ln);
if (upd->memtype == NULL) {
if (upd->memtype == NULL && upd->cmdline == 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',
@@ -1445,6 +1464,8 @@ int main(int argc, char * argv [])
uflags &= ~UF_AUTO_ERASE;
for (ln=lfirst(updates); ln; ln=lnext(ln)) {
upd = ldata(ln);
if(!upd->memtype)
continue;
m = avr_locate_mem(p, upd->memtype);
if (m == NULL)
continue;

View File

@@ -56,7 +56,6 @@
#include "avrdude.h"
#include "term.h"
struct command {
char *name;

View File

@@ -27,12 +27,6 @@
extern "C" {
#endif
int terminal_mode(const PROGRAMMER * pgm, const AVRPART * p);
int terminal_mode_noninteractive(const PROGRAMMER * pgm, const AVRPART * p);
int terminal_line(const PROGRAMMER *pgm, const AVRPART *p, const char *line);
char * terminal_get_input(const char *prompt);
void terminal_setup_update_progress();
#ifdef __cplusplus
}
#endif

View File

@@ -126,6 +126,12 @@ UPDATE * new_update(int op, char * memtype, int filefmt, char * filename)
return u;
}
UPDATE *cmd_update(const char *cmd) {
UPDATE *u = (UPDATE *) cfg_malloc(__func__, sizeof *u);
u->cmdline = cmd;
return u;
}
void free_update(UPDATE * u)
{
if (u != NULL) {
@@ -305,6 +311,9 @@ int update_dryrun(const AVRPART *p, UPDATE *upd) {
int known, format_detect, ret = LIBAVRDUDE_SUCCESS;
if(upd->cmdline) // Todo: parse terminal command line?
return 0;
/*
* Reject an update if memory name is not known amongst any part (suspect a typo)
* but accept when the specific part does not have it (allow unifying i/faces)
@@ -387,6 +396,9 @@ int do_op(const PROGRAMMER *pgm, const AVRPART *p, UPDATE *upd, enum updateflags
int rc;
Filestats fs, fs_patched;
if(upd->cmdline)
return terminal_line(pgm, p, upd->cmdline);
mem = avr_locate_mem(p, upd->memtype);
if (mem == NULL) {
pmsg_warning("skipping -U %s:... as memory not defined for part %s\n", upd->memtype, p->desc);