-
Notifications
You must be signed in to change notification settings - Fork 136
/
main.c
1625 lines (1415 loc) · 47.8 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* avrdude - A Downloader/Uploader for AVR device programmers
* Copyright (C) 2000-2005 Brian S. Dean <bsd@bdmicro.com>
* Copyright Joerg Wunsch <j@uriah.heep.sax.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* $Id$ */
/*
* Code to program an Atmel AVR device through one of the supported
* programmers.
*
* For parallel port connected programmers, the pin definitions can be
* changed via a config file. See the config file for instructions on
* how to add a programmer definition.
*
*/
#include "ac_cfg.h"
#include <stdio.h>
#include <stdlib.h>
#include <whereami.h>
#include <stdarg.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "avrdude.h"
#include "libavrdude.h"
#include "config.h"
#include "developer_opts.h"
/* Get VERSION from ac_cfg.h */
char * version = VERSION;
char * progname;
char progbuf[PATH_MAX]; /* temporary buffer of spaces the same
length as progname; used for lining up
multiline messages */
// Old (deprecated) message routine
int avrdude_message(int msglvl, const char *format, ...)
{
int rc = 0;
va_list ap;
if (verbose >= msglvl) {
va_start(ap, format);
rc = vfprintf(stderr, format, ap);
va_end(ap);
}
return rc;
}
static const char *avrdude_message_type(int msglvl) {
switch(msglvl) {
case MSG_EXT_ERROR: return "OS error";
case MSG_ERROR: return "error";
case MSG_WARNING: return "warning";
case MSG_INFO: return "info";
case MSG_NOTICE: return "notice";
case MSG_NOTICE2: return "notice2";
case MSG_DEBUG: return "debug";
case MSG_TRACE: return "trace";
case MSG_TRACE2: return "trace2";
default: return "unknown msglvl";
}
}
/*
* Core msg_xyz() routine
* See #define lines in avrdude.h of how it is normally called
* Side note: if format starts with \v print \n but only if *not* at beginning of line
*/
int avrdude_message2(FILE *fp, int lno, const char *file, const char *func, int msgmode, int msglvl, const char *format, ...) {
int rc = 0;
va_list ap;
static struct { // Memorise whether last print ended at beginning of line
FILE *fp;
int bol; // Are we at the beginning of a line for this fp stream?
} bols[5+1]; // Cater for up to 5 different FILE pointers plus one catch-all
size_t bi = 0; // bi is index to bols[] array
for(bi=0; bi < sizeof bols/sizeof*bols -1; bi++) { // Note the -1, so bi is valid after loop
if(!bols[bi].fp) { // First free space
bols[bi].fp = fp; // Insert fp in first free space
bols[bi].bol = 1; // Assume beginning of line on first use
}
if(bols[bi].fp == fp)
break;
}
if(msglvl <= MSG_ERROR) // Serious error? Free progress bars (if any)
report_progress(1, -1, NULL);
if(msgmode & MSG2_FLUSH) {
fflush(stdout);
fflush(stderr);
}
// Reduce effective verbosity level by number of -q above one when printing to stderr
if ((quell_progress < 2 || fp != stderr? verbose: verbose+1-quell_progress) >= msglvl) {
if(msgmode & MSG2_PROGNAME) {
if(!bols[bi].bol)
fprintf(fp, "\n");
fprintf(fp, "%s", progname);
if(verbose >= MSG_NOTICE && (msgmode & MSG2_FUNCTION))
fprintf(fp, " %s()", func);
if(verbose >= MSG_DEBUG && (msgmode & MSG2_FILELINE)) {
const char *pr = strrchr(file, '/'); // Only print basename
#if defined (WIN32)
if(!pr)
pr = strrchr(file, '\\');
#endif
pr = pr? pr+1: file;
fprintf(fp, " [%s:%d]", pr, lno);
}
if(msgmode & MSG2_TYPE)
fprintf(fp, " %s", avrdude_message_type(msglvl));
fprintf(fp, ": ");
bols[bi].bol = 0;
} else if(msgmode & MSG2_INDENT1) {
fprintf(fp, "%*s", (int) strlen(progname)+1, "");
bols[bi].bol = 0;
} else if(msgmode & MSG2_INDENT2) {
fprintf(fp, "%*s", (int) strlen(progname)+2, "");
bols[bi].bol = 0;
}
// Vertical tab at start of format string is a conditional new line
if(*format == '\v') {
format++;
if(!bols[bi].bol) {
fprintf(fp, "\n");
bols[bi].bol = 1;
}
}
// Figure out whether this print will leave us at beginning of line
// Determine required size first
va_start(ap, format);
rc = vsnprintf(NULL, 0, format, ap);
va_end(ap);
if(rc < 0) // Some errror?
return 0;
rc++; // Accommodate terminating nul
char *p = cfg_malloc(__func__, rc);
va_start(ap, format);
rc = vsnprintf(p, rc, format, ap);
va_end(ap);
if(rc < 0) {
free(p);
return 0;
}
if(*p) {
fprintf(fp, "%s", p); // Finally: print!
bols[bi].bol = p[strlen(p)-1] == '\n';
}
free(p);
}
if(msgmode & MSG2_FLUSH)
fflush(fp);
return rc;
}
struct list_walk_cookie
{
FILE *f;
const char *prefix;
};
static LISTID updates = NULL;
static LISTID extended_params = NULL;
static LISTID additional_config_files = NULL;
static PROGRAMMER * pgm;
/*
* global options
*/
int verbose; // Verbose output
int quell_progress; // Quell progress report and un-verbose output
int ovsigck; // 1 = override sig check, 0 = don't
const char *partdesc; // Part -p string
const char *pgmid; // Programmer -c string
/*
* usage message
*/
static void usage(void)
{
msg_error(
"Usage: %s [options]\n"
"Options:\n"
" -p <partno> Specify AVR device; -p ? lists all known parts\n"
" -p <wildcard>/<flags> Run developer options for matched AVR devices,\n"
" e.g., -p ATmega328P/s or /S for part definition\n"
" -b <baudrate> Override RS-232 baud rate\n"
" -B <bitclock> Specify bit clock period (us)\n"
" -C <config-file> Specify location of configuration file\n"
" -c <programmer> Specify programmer; -c ? and -c ?type list all\n"
" -c <wildcard>/<flags> Run developer options for matched programmers,\n"
" e.g., -c 'ur*'/s for programmer info/definition\n"
" -A Disable trailing-0xff removal for file/AVR read\n"
" -D Disable auto erase for flash memory; implies -A\n"
" -i <delay> ISP Clock Delay [in microseconds]\n"
" -P <port> Connection; -P ?s or -P ?sa lists serial ones\n"
" -r Reconnect to -P port after \"touching\" it; wait\n"
" 400 ms for each -r; needed for some USB boards\n"
" -F Override invalid signature or initial checks\n"
" -e Perform a chip erase\n"
" -O Perform RC oscillator calibration (see AVR053)\n"
" -t Run an interactive terminal when it is its turn\n"
" -T <terminal cmd line> Run terminal line when it is its turn\n"
" -U <memstr>:r|w|v:<filename>[:format]\n"
" Carry out memory operation when it is its turn\n"
" Multiple -t, -T and -U options can be specified\n"
" -n Do not write to the device whilst processing -U\n"
" -V Do not automatically verify during -U\n"
" -E <exitsp>[,<exitsp>] List programmer exit specifications\n"
" -x <extended_param> Pass <extended_param> to programmer, see -xhelp\n"
" -v Verbose output; -v -v for more\n"
" -q Quell progress output; -q -q for less\n"
" -l logfile Use logfile rather than stderr for diagnostics\n"
" -? Display this usage\n"
"\navrdude version %s, https://github.com/avrdudes/avrdude\n",
progname, version);
}
// Potentially shorten copy of prog description if it's the suggested mode
static void pmshorten(char *desc, const char *modes) {
struct { const char *end, *mode; } pairs[] = {
{" in parallel programming mode", "HVPP"},
{" in PP mode", "HVPP"},
{" in high-voltage serial programming mode", "HVSP"},
{" in HVSP mode", "HVSP"},
{" in ISP mode", "ISP"},
{" in debugWire mode", "debugWIRE"},
{" in AVR32 mode", "aWire"},
{" in PDI mode", "PDI"},
{" in UPDI mode", "UPDI"},
{" in JTAG mode", "JTAG"},
{" in JTAG mode", "JTAGmkI"},
{" in JTAG mode", "XMEGAJTAG"},
{" in JTAG mode", "AVR32JTAG"},
{" for bootloader", "bootloader"},
};
size_t len = strlen(desc);
for(size_t i=0; i<sizeof pairs/sizeof*pairs; i++) {
size_t elen = strlen(pairs[i].end);
if(len > elen && str_caseeq(desc+len-elen, pairs[i].end) && str_eq(modes, pairs[i].mode)) {
desc[len-elen] = 0;
break;
}
}
}
static void list_programmers(FILE *f, const char *prefix, LISTID programmers, int pm) {
LNODEID ln1;
LNODEID ln2;
PROGRAMMER *pgm;
int maxlen=0, len;
sort_programmers(programmers);
// Compute max length of programmer names
for(ln1 = lfirst(programmers); ln1; ln1 = lnext(ln1)) {
pgm = ldata(ln1);
if(!is_programmer(pgm))
continue;
for(ln2=lfirst(pgm->id); ln2; ln2=lnext(ln2))
if(!pm || !pgm->prog_modes || (pm & pgm->prog_modes)) {
const char *id = ldata(ln2);
if(*id == 0 || *id == '.')
continue;
if((len = strlen(id)) > maxlen)
maxlen = len;
}
}
for(ln1 = lfirst(programmers); ln1; ln1 = lnext(ln1)) {
pgm = ldata(ln1);
if(!is_programmer(pgm))
continue;
for(ln2=lfirst(pgm->id); ln2; ln2=lnext(ln2)) {
// List programmer if pm or prog_modes uninitialised or if they are compatible otherwise
if(!pm || !pgm->prog_modes || (pm & pgm->prog_modes)) {
const char *id = ldata(ln2);
char *desc = cfg_strdup("list_programmers()", pgm->desc);
const char *modes = avr_prog_modes(pm & pgm->prog_modes);
if(pm != ~0)
pmshorten(desc, modes);
if(*id == 0 || *id == '.')
continue;
if(verbose > 0)
fprintf(f, "%s%-*s = %-30s [%s:%d]", prefix, maxlen, id, desc, pgm->config_file, pgm->lineno);
else
fprintf(f, "%s%-*s = %-s", prefix, maxlen, id, desc);
if(pm != ~0)
fprintf(f, " via %s", modes);
fprintf(f, "\n");
free(desc);
}
}
}
}
static void list_programmer_types_callback(const char *name, const char *desc,
void *cookie)
{
struct list_walk_cookie *c = (struct list_walk_cookie *)cookie;
fprintf(c->f, "%s%-16s = %-s\n", c->prefix, name, desc);
}
static void list_programmer_types(FILE * f, const char *prefix)
{
struct list_walk_cookie c;
c.f = f;
c.prefix = prefix;
walk_programmer_types(list_programmer_types_callback, &c);
}
static void list_parts(FILE *f, const char *prefix, LISTID avrparts, int pm) {
LNODEID ln1;
AVRPART *p;
int maxlen=0, len;
sort_avrparts(avrparts);
// Compute max length of part names
for(ln1 = lfirst(avrparts); ln1; ln1 = lnext(ln1)) {
p = ldata(ln1);
// List part if pm or prog_modes uninitialised or if they are compatible otherwise
if(!pm || !p->prog_modes || (pm & p->prog_modes)) {
if(verbose < 2 && p->id[0] == '.') // hide ids starting with '.'
continue;
if((len = strlen(p->id)) > maxlen)
maxlen = len;
}
}
for(ln1 = lfirst(avrparts); ln1; ln1 = lnext(ln1)) {
p = ldata(ln1);
// List part if pm or prog_modes uninitialised or if they are compatible otherwise
if(!pm || !p->prog_modes || (pm & p->prog_modes)) {
if(verbose < 2 && p->id[0] == '.') // hide ids starting with '.'
continue;
if(verbose > 0)
fprintf(f, "%s%-*s = %-18s [%s:%d]", prefix, maxlen, p->id, p->desc, p->config_file, p->lineno);
else
fprintf(f, "%s%-*s = %s", prefix, maxlen, p->id, p->desc);
if(pm != ~0)
fprintf(f, " via %s", avr_prog_modes(pm & p->prog_modes));
fprintf(f, "\n");
if(verbose > 0)
for(LNODEID ln = lfirst(p->variants); ln; ln = lnext(ln))
fprintf(f, "%s%s- %s\n", prefix, prefix, (char *) ldata(ln));
}
}
}
static void exithook(void)
{
if (pgm->teardown)
pgm->teardown(pgm);
}
static void cleanup_main(void)
{
if (updates) {
ldestroy_cb(updates, (void(*)(void*))free_update);
updates = NULL;
}
if (extended_params) {
ldestroy(extended_params);
extended_params = NULL;
}
if (additional_config_files) {
ldestroy(additional_config_files);
additional_config_files = NULL;
}
cleanup_config();
}
static void replace_backslashes(char *s)
{
// Replace all backslashes with forward slashes
for (size_t i = 0; i < strlen(s); i++) {
if (s[i] == '\\') {
s[i] = '/';
}
}
}
// Return 2 if string is * or starts with */, 1 if string contains /, 0 otherwise
static int dev_opt(const char *str) {
return
!str? 0:
str_eq(str, "*") || str_starts(str, "*/")? 2:
!!strchr(str, '/');
}
static void programmer_not_found(const char *programmer) {
msg_error("\n");
if(programmer && *programmer)
pmsg_error("cannot find programmer id %s\n", programmer);
else {
pmsg_error("no programmer has been specified on the command line or in the\n");
imsg_error("config file(s); specify one using the -c option and try again\n");
}
msg_error("\nValid programmers are:\n");
list_programmers(stderr, " ", programmers, ~0);
msg_error("\n");
}
static void part_not_found(const char *partdesc) {
msg_error("\n");
if(partdesc && *partdesc)
pmsg_error("AVR part %s not found\n", partdesc);
else
pmsg_error("no AVR part has been specified; use -p part\n");
msg_error("\nValid parts are:\n");
list_parts(stderr, " ", part_list, ~0);
msg_error("\n");
}
#if !defined(WIN32)
// Safely concatenate dir/file into dst that has size n
static char *concatpath(char *dst, char *dir, char *file, size_t n) {
// Dir or file empty?
if(!dir || !*dir || !file || !*file)
return NULL;
size_t len = strlen(dir);
// Insufficient space?
if(len + (dir[len-1] != '/') + strlen(file) > n-1)
return NULL;
if(dst != dir)
strcpy(dst, dir);
if(dst[len-1] != '/')
strcat(dst, "/");
strcat(dst, file);
return dst;
}
#endif
/*
* main routine
*/
int main(int argc, char * argv [])
{
int rc; /* general return code checking */
int exitrc; /* exit code for main() */
int i; /* general loop counter */
int ch; /* options flag */
int len; /* length for various strings */
struct avrpart * p; /* which avr part we are programming */
AVRMEM * sig; /* signature data */
struct stat sb;
UPDATE * upd;
LNODEID * ln;
/* options / operating mode variables */
int erase; /* 1=erase chip, 0=don't */
int calibrate; /* 1=calibrate RC oscillator, 0=don't */
char * port; /* device port (/dev/xxx) */
const char *exitspecs; /* exit specs string from command line */
int explicit_c; /* 1=explicit -c on command line, 0=not specified there */
int explicit_e; /* 1=explicit -e on command line, 0=not specified there */
char sys_config[PATH_MAX]; /* system wide config file */
char usr_config[PATH_MAX]; /* per-user config file */
char executable_abspath[PATH_MAX]; /* absolute path to avrdude executable */
char executable_dirpath[PATH_MAX]; /* absolute path to folder with executable */
bool executable_abspath_found = false; /* absolute path to executable found */
bool sys_config_found = false; /* 'avrdude.conf' file found */
char * e; /* for strtod() error checking */
const char *errstr; /* for str_int() error checking */
int baudrate; /* override default programmer baud rate */
int touch_1200bps; /* "touch" serial port prior to programming */
double bitclock; /* Specify programmer bit clock (JTAG ICE) */
int ispdelay; /* Specify the delay for ISP clock */
int init_ok; /* Device initialization worked well */
int is_open; /* Device open succeeded */
int ce_delayed; /* Chip erase delayed */
char * logfile; /* Use logfile rather than stderr for diagnostics */
enum updateflags uflags = UF_AUTO_ERASE | UF_VERIFY; /* Flags for do_op() */
(void) avr_ustimestamp();
#ifdef _MSC_VER
_set_printf_count_output(1);
#endif
/*
* Set line buffering for file descriptors so we see stdout and stderr
* properly interleaved.
*/
setvbuf(stdout, (char*)NULL, _IOLBF, 0);
setvbuf(stderr, (char*)NULL, _IOLBF, 0);
sys_config[0] = '\0';
progname = strrchr(argv[0], '/');
#if defined (WIN32)
/* take care of backslash as dir sep in W32 */
if (!progname)
progname = strrchr(argv[0], '\\');
#endif /* WIN32 */
if (progname)
progname++;
else
progname = argv[0];
// Remove trailing .exe
if(str_ends(progname, ".exe")) {
progname = cfg_strdup("main()", progname); // Don't write to argv[0]
progname[strlen(progname)-4] = 0;
}
avrdude_conf_version = "";
default_programmer = "";
default_parallel = "";
default_serial = "";
default_spi = "";
default_bitclock = 0.0;
default_linuxgpio = "";
allow_subshells = 0;
init_config();
atexit(cleanup_main);
updates = lcreat(NULL, 0);
if (updates == NULL) {
pmsg_error("cannot initialize updater list\n");
exit(1);
}
extended_params = lcreat(NULL, 0);
if (extended_params == NULL) {
pmsg_error("cannot initialize extended parameter list\n");
exit(1);
}
additional_config_files = lcreat(NULL, 0);
if (additional_config_files == NULL) {
pmsg_error("cannot initialize additional config files list\n");
exit(1);
}
partdesc = NULL;
port = NULL;
erase = 0;
calibrate = 0;
p = NULL;
ovsigck = 0;
quell_progress = 0;
exitspecs = NULL;
pgm = NULL;
pgmid = "";
explicit_c = 0;
explicit_e = 0;
verbose = 0;
baudrate = 0;
touch_1200bps = 0;
bitclock = 0.0;
ispdelay = 0;
is_open = 0;
ce_delayed = 0;
logfile = NULL;
len = strlen(progname) + 2;
for (i=0; i<len; i++)
progbuf[i] = ' ';
progbuf[i] = 0;
/*
* check for no arguments
*/
if (argc == 1) {
usage();
return 0;
}
/*
* process command line arguments
*/
while ((ch = getopt(argc,argv,"?Ab:B:c:C:DeE:Fi:l:np:OP:qrstT:U:uvVx:yY:")) != -1) {
switch (ch) {
case 'b': /* override default programmer baud rate */
baudrate = str_int(optarg, STR_INT32, &errstr);
if(errstr) {
pmsg_error("invalid baud rate %s specified: %s\n", optarg, errstr);
exit(1);
}
break;
case 'B': /* specify JTAG ICE bit clock period */
bitclock = strtod(optarg, &e);
if (*e != 0) {
/* trailing unit of measure present */
int suffixlen = strlen(e);
switch (suffixlen) {
case 2:
if ((e[0] != 'h' && e[0] != 'H') || e[1] != 'z')
bitclock = 0.0;
else
/* convert from Hz to microseconds */
bitclock = 1E6 / bitclock;
break;
case 3:
if ((e[1] != 'h' && e[1] != 'H') || e[2] != 'z')
bitclock = 0.0;
else {
switch (e[0]) {
case 'M':
case 'm': /* no Millihertz here :) */
bitclock = 1.0 / bitclock;
break;
case 'k':
bitclock = 1E3 / bitclock;
break;
default:
bitclock = 0.0;
break;
}
}
break;
default:
bitclock = 0.0;
break;
}
if (bitclock == 0.0)
pmsg_error("invalid bit clock unit of measure '%s'\n", e);
}
if ((e == optarg) || bitclock == 0.0) {
pmsg_error("invalid bit clock period specified '%s'\n", optarg);
exit(1);
}
break;
case 'i': /* specify isp clock delay */
ispdelay = str_int(optarg, STR_INT32, &errstr);
if(errstr || ispdelay == 0) {
pmsg_error("invalid isp clock delay %s specified", optarg);
if(errstr)
msg_error(": %s\n", errstr);
else
msg_error("\n");
exit(1);
}
break;
case 'c': /* programmer id */
pgmid = optarg;
explicit_c = 1;
break;
case 'C': /* system wide configuration file */
if (optarg[0] == '+') {
ladd(additional_config_files, optarg+1);
} else {
strncpy(sys_config, optarg, PATH_MAX);
sys_config[PATH_MAX-1] = 0;
}
break;
case 'D': /* disable auto erase */
uflags &= ~UF_AUTO_ERASE;
/* fall through */
case 'A': /* explicit disabling of trailing-0xff removal */
disable_trailing_ff_removal();
break;
case 'e': /* perform a chip erase */
erase = 1;
explicit_e = 1;
uflags &= ~UF_AUTO_ERASE;
break;
case 'E':
exitspecs = optarg;
break;
case 'F': /* override invalid signature check */
ovsigck = 1;
break;
case 'l':
logfile = optarg;
break;
case 'n':
uflags |= UF_NOWRITE;
break;
case 'O': /* perform RC oscillator calibration */
calibrate = 1;
break;
case 'p' : /* specify AVR part */
partdesc = optarg;
break;
case 'P':
port = cfg_strdup(__func__, optarg);
break;
case 'q' : /* Quell progress output */
quell_progress++ ;
break;
case 'r' :
touch_1200bps++;
break;
case 't': /* enter terminal mode */
ladd(updates, cmd_update("interactive terminal"));
break;
case 's':
case 'u':
pmsg_error("\"safemode\" feature no longer supported\n");
break;
case 'T':
ladd(updates, cmd_update(optarg));
break;
case 'U':
upd = parse_op(optarg);
if (upd == NULL) {
pmsg_error("unable to parse update operation '%s'\n", optarg);
exit(1);
}
ladd(updates, upd);
break;
case 'v':
verbose++;
break;
case 'V':
uflags &= ~UF_VERIFY;
break;
case 'x':
ladd(extended_params, optarg);
break;
case 'y':
pmsg_error("erase cycle counter no longer supported\n");
break;
case 'Y':
pmsg_error("erase cycle counter no longer supported\n");
break;
case '?': /* help */
usage();
exit(0);
break;
default:
pmsg_error("invalid option -%c\n\n", ch);
usage();
exit(1);
break;
}
}
if (logfile != NULL) {
FILE *newstderr = freopen(logfile, "w", stderr);
if (newstderr == NULL) {
/* Help! There's no stderr to complain to anymore now. */
printf("Cannot create logfile %s: %s\n", logfile, strerror(errno));
return 1;
}
}
/* search for system configuration file unless -C conffile was given */
if (strlen(sys_config) == 0) {
/*
* EXECUTABLE ABSPATH
* ------------------
* Determine the absolute path to avrdude executable. This will be used to
* locate the 'avrdude.conf' file later.
*/
int executable_dirpath_len;
int executable_abspath_len = wai_getExecutablePath(
executable_abspath,
PATH_MAX,
&executable_dirpath_len
);
if (
(executable_abspath_len != -1) &&
(executable_abspath_len != 0) &&
(executable_dirpath_len != -1) &&
(executable_dirpath_len != 0)
) {
// All requirements satisfied, executable path was found
executable_abspath_found = true;
// Make sure the string is null terminated
executable_abspath[executable_abspath_len] = '\0';
replace_backslashes(executable_abspath);
// Define 'executable_dirpath' to be the path to the parent folder of the
// executable.
strcpy(executable_dirpath, executable_abspath);
executable_dirpath[executable_dirpath_len] = '\0';
// Debug output
msg_trace2("executable_abspath = %s\n", executable_abspath);
msg_trace2("executable_abspath_len = %i\n", executable_abspath_len);
msg_trace2("executable_dirpath = %s\n", executable_dirpath);
msg_trace2("executable_dirpath_len = %i\n", executable_dirpath_len);
}
/*
* SYSTEM CONFIG
* -------------
* Determine the location of 'avrdude.conf'. Check in this order:
* 1. <dirpath of executable>/../etc/avrdude.conf
* 2. <dirpath of executable>/avrdude.conf
* 3. CONFIG_DIR/avrdude.conf
*
* When found, write the result into the 'sys_config' variable.
*/
if (executable_abspath_found) {
// 1. Check <dirpath of executable>/../etc/avrdude.conf
strcpy(sys_config, executable_dirpath);
sys_config[PATH_MAX - 1] = '\0';
i = strlen(sys_config);
if (i && (sys_config[i - 1] != '/'))
strcat(sys_config, "/");
strcat(sys_config, "../etc/" SYSTEM_CONF_FILE);
sys_config[PATH_MAX - 1] = '\0';
if (access(sys_config, F_OK) == 0) {
sys_config_found = true;
}
else {
// 2. Check <dirpath of executable>/avrdude.conf
strcpy(sys_config, executable_dirpath);
sys_config[PATH_MAX - 1] = '\0';
i = strlen(sys_config);
if (i && (sys_config[i - 1] != '/'))
strcat(sys_config, "/");
strcat(sys_config, SYSTEM_CONF_FILE);
sys_config[PATH_MAX - 1] = '\0';
if (access(sys_config, F_OK) == 0) {
sys_config_found = true;
}
}
}
if (!sys_config_found) {
// 3. Check CONFIG_DIR/avrdude.conf
#if defined(WIN32)
win_sys_config_set(sys_config);
#else
strcpy(sys_config, CONFIG_DIR);
i = strlen(sys_config);
if (i && (sys_config[i - 1] != '/'))
strcat(sys_config, "/");
strcat(sys_config, SYSTEM_CONF_FILE);
#endif
if (access(sys_config, F_OK) == 0) {
sys_config_found = true;
}
}
}
// Debug output
msg_trace2("sys_config = %s\n", sys_config);
msg_trace2("sys_config_found = %s\n", sys_config_found ? "true" : "false");
msg_trace2("\n");
/*
* USER CONFIG
* -----------
* Determine the location of '.avrduderc'.
*/
#if defined(WIN32)
win_usr_config_set(usr_config);
#else
usr_config[0] = 0;
if(!concatpath(usr_config, getenv("XDG_CONFIG_HOME"), XDG_USER_CONF_FILE, sizeof usr_config))
concatpath(usr_config, getenv("HOME"), ".config/" XDG_USER_CONF_FILE, sizeof usr_config);
if(stat(usr_config, &sb) < 0 || (sb.st_mode & S_IFREG) == 0)
concatpath(usr_config, getenv("HOME"), USER_CONF_FILE, sizeof usr_config);
#endif
if (quell_progress == 0)
terminal_setup_update_progress();
/*
* Print out an identifying string so folks can tell what version
* they are running
*/
msg_notice("\n");
pmsg_notice("Version %s\n", version);
imsg_notice("Copyright the AVRDUDE authors;\n");
imsg_notice("see https://github.com/avrdudes/avrdude/blob/main/AUTHORS\n\n");
if(*sys_config) {
char *real_sys_config = realpath(sys_config, NULL);
if(real_sys_config) {
imsg_notice("System wide configuration file is %s\n", real_sys_config);
} else
pmsg_warning("cannot determine realpath() of config file %s: %s\n", sys_config, strerror(errno));
rc = read_config(real_sys_config);
if (rc) {
pmsg_error("unable to process system wide configuration file %s\n", real_sys_config);
exit(1);
}
free(real_sys_config);
}
if (usr_config[0] != 0) {
imsg_notice("User configuration file is %s\n", usr_config);
rc = stat(usr_config, &sb);
if ((rc < 0) || ((sb.st_mode & S_IFREG) == 0))
imsg_notice("User configuration file does not exist or is not a regular file, skipping\n");
else {
rc = read_config(usr_config);
if (rc) {
pmsg_error("unable to process user configuration file %s\n", usr_config);
exit(1);
}
}
}
if (lsize(additional_config_files) > 0) {
LNODEID ln1;
const char * p = NULL;