Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
update.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include "avrdude.h"
#include "libavrdude.h"
+ Include dependency graph for update.c:

Go to the source code of this file.

Functions

UPDATEparse_op (char *s)
 
UPDATEdup_update (UPDATE *upd)
 
UPDATEnew_update (int op, char *memtype, int filefmt, char *filename, unsigned section)
 
void free_update (UPDATE *u)
 
int do_op (PROGRAMMER *pgm, struct avrpart *p, UPDATE *upd, enum updateflags flags)
 

Function Documentation

◆ do_op()

int do_op ( PROGRAMMER pgm,
struct avrpart p,
UPDATE upd,
enum updateflags  flags 
)
238{
239 struct avrpart * v;
240 AVRMEM * mem;
241 int size, vsize;
242 int rc;
243
244 mem = avr_locate_mem(p, upd->memtype);
245 if (mem == NULL) {
246 avrdude_message(MSG_INFO, "\"%s\" memory type not defined for part \"%s\"\n",
247 upd->memtype, p->desc);
248 return -1;
249 }
250
251 if (upd->op == DEVICE_READ) {
252 /*
253 * read out the specified device memory and write it to a file
254 */
255 if (quell_progress < 2) {
256 avrdude_message(MSG_INFO, "%s: reading %s memory:\n",
257 progname, mem->desc);
258 }
259 report_progress(0,1,"Reading");
260 rc = avr_read(pgm, p, upd->memtype, 0);
261 if (rc < 0) {
262 avrdude_message(MSG_INFO, "%s: failed to read all of %s memory, rc=%d\n",
263 progname, mem->desc, rc);
264 return -1;
265 }
266 report_progress(1,1,NULL);
267 size = rc;
268
269 if (quell_progress < 2) {
270 if (rc == 0)
271 avrdude_message(MSG_INFO, "%s: Flash is empty, resulting file has no contents.\n",
272 progname);
273 avrdude_message(MSG_INFO, "%s: writing output file \"%s\"\n",
274 progname,
275 strcmp(upd->filename, "-")==0 ? "<stdout>" : upd->filename);
276 }
277 rc = fileio(FIO_WRITE, upd->filename, upd->format, p, upd->memtype, size, 0);
278 if (rc < 0) {
279 avrdude_message(MSG_INFO, "%s: write to file '%s' failed\n",
280 progname, upd->filename);
281 return -1;
282 }
283 }
284 else if (upd->op == DEVICE_WRITE) {
285 /*
286 * write the selected device memory using data from a file; first
287 * read the data from the specified file
288 */
289 if (quell_progress < 2) {
290 avrdude_message(MSG_INFO, "%s: reading input file \"%s\"\n",
291 progname,
292 strcmp(upd->filename, "-")==0 ? "<stdin>" : upd->filename);
293 }
294 rc = fileio(FIO_READ, upd->filename, upd->format, p, upd->memtype, -1, upd->section);
295 if (rc < 0) {
296 avrdude_message(MSG_INFO, "%s: read from file '%s' failed\n",
297 progname, upd->filename);
298 return -1;
299 }
300 size = rc;
301
302 /*
303 * write the buffer contents to the selected memory type
304 */
305 if (quell_progress < 2) {
306 avrdude_message(MSG_INFO, "%s: writing %s (%d bytes):\n",
307 progname, mem->desc, size);
308 }
309
310 //Prusa3D bootloader progress on lcd
311 if (strcmp(pgm->type, "Wiring") == 0)
312 {
313 if (pgm->set_upload_size != 0)
314 pgm->set_upload_size(pgm, size);
315 }
316
317 if (!(flags & UF_NOWRITE)) {
318 report_progress(0,1,"Writing");
319 rc = avr_write(pgm, p, upd->memtype, size, (flags & UF_AUTO_ERASE) != 0);
320 report_progress(1,1,NULL);
321 }
322 else {
323 // /*
324 // * test mode, don't actually write to the chip, output the buffer
325 // * to stdout in intel hex instead
326 // */
327 // rc = fileio(FIO_WRITE, "-", FMT_IHEX, p, upd->memtype, size, 0);
328 }
329
330 if (rc < 0) {
331 avrdude_message(MSG_INFO, "%s: failed to write %s memory, rc=%d\n",
332 progname, mem->desc, rc);
333 return -1;
334 }
335
336 vsize = rc;
337
338 if (quell_progress < 2) {
339 avrdude_message(MSG_INFO, "%s: %d bytes of %s written\n", progname,
340 vsize, mem->desc);
341 }
342
343 }
344 else if (upd->op == DEVICE_VERIFY) {
345 /*
346 * verify that the in memory file (p->mem[AVR_M_FLASH|AVR_M_EEPROM])
347 * is the same as what is on the chip
348 */
349 pgm->vfy_led(pgm, ON);
350
351 if (quell_progress < 2) {
352 avrdude_message(MSG_INFO, "%s: verifying %s memory against %s:\n",
353 progname, mem->desc, upd->filename);
354
355 avrdude_message(MSG_INFO, "%s: load data %s data from input file %s:\n",
356 progname, mem->desc, upd->filename);
357 }
358
359 rc = fileio(FIO_READ, upd->filename, upd->format, p, upd->memtype, -1, upd->section);
360 if (rc < 0) {
361 avrdude_message(MSG_INFO, "%s: read from file '%s' failed\n",
362 progname, upd->filename);
363 return -1;
364 }
365 v = avr_dup_part(p);
366 size = rc;
367 if (quell_progress < 2) {
368 avrdude_message(MSG_INFO, "%s: input file %s contains %d bytes\n",
369 progname, upd->filename, size);
370 avrdude_message(MSG_INFO, "%s: reading on-chip %s data:\n",
371 progname, mem->desc);
372 }
373
374 report_progress (0,1,"Reading");
375 rc = avr_read(pgm, p, upd->memtype, v);
376 if (rc < 0) {
377 avrdude_message(MSG_INFO, "%s: failed to read all of %s memory, rc=%d\n",
378 progname, mem->desc, rc);
379 pgm->err_led(pgm, ON);
380 return -1;
381 }
382 report_progress (1,1,NULL);
383
384
385
386 if (quell_progress < 2) {
387 avrdude_message(MSG_INFO, "%s: verifying ...\n", progname);
388 }
389 rc = avr_verify(p, v, upd->memtype, size);
390 if (rc < 0) {
391 avrdude_message(MSG_INFO, "%s: verification error; content mismatch\n",
392 progname);
393 pgm->err_led(pgm, ON);
394 return -1;
395 }
396
397 if (quell_progress < 2) {
398 avrdude_message(MSG_INFO, "%s: %d bytes of %s verified\n",
399 progname, rc, mem->desc);
400 }
401
402 pgm->vfy_led(pgm, OFF);
403 }
404 else {
405 avrdude_message(MSG_INFO, "%s: invalid update operation (%d) requested\n",
406 progname, upd->op);
407 return -1;
408 }
409
410 return 0;
411}
int avr_write(PROGRAMMER *pgm, AVRPART *p, char *memtype, int size, int auto_erase)
Definition avr.c:831
void report_progress(int completed, int total, char *hdr)
Definition avr.c:1224
int avr_verify(AVRPART *p, AVRPART *v, char *memtype, int size)
Definition avr.c:1079
int avr_read(PROGRAMMER *pgm, AVRPART *p, char *memtype, AVRPART *v)
Definition avr.c:312
#define MSG_INFO
Definition avrdude.h:51
char * progname
Definition main.c:61
int avrdude_message(const int msglvl, const char *format,...)
Definition main.c:93
int quell_progress
Definition main.c:199
AVRPART * avr_dup_part(AVRPART *d)
Definition avrpart.c:473
AVRMEM * avr_locate_mem(AVRPART *p, char *desc)
Definition avrpart.c:354
int fileio(int op, char *filename, FILEFMT format, struct avrpart *p, char *memtype, int size, unsigned section)
Definition fileio.c:1512
int op
Definition libavrdude.h:870
char * filename
Definition libavrdude.h:872
#define OFF
Definition libavrdude.h:585
@ FIO_WRITE
Definition libavrdude.h:810
@ FIO_READ
Definition libavrdude.h:809
unsigned section
Definition libavrdude.h:871
char * memtype
Definition libavrdude.h:869
@ UF_AUTO_ERASE
Definition libavrdude.h:864
@ UF_NOWRITE
Definition libavrdude.h:863
int format
Definition libavrdude.h:873
char desc[AVR_DESCLEN]
Definition libavrdude.h:218
@ DEVICE_VERIFY
Definition libavrdude.h:858
@ DEVICE_WRITE
Definition libavrdude.h:857
@ DEVICE_READ
Definition libavrdude.h:856
#define ON
Definition libavrdude.h:584
unsigned flags
Definition libavrdude.h:230
LISTID mem
Definition libavrdude.h:277
Definition libavrdude.h:283
Definition libavrdude.h:217
static PROGRAMMER * pgm
Definition main.c:192
constexpr auto size(const C &c) -> decltype(c.size())
Definition span.hpp:183
int(* vfy_led)(struct programmer_t *pgm, int value)
Definition libavrdude.h:642
void(* set_upload_size)(struct programmer_t *pgm, int size)
Definition libavrdude.h:686
int(* err_led)(struct programmer_t *pgm, int value)
Definition libavrdude.h:640
char type[PGM_TYPELEN]
Definition libavrdude.h:619

References avr_dup_part(), avr_locate_mem(), avr_read(), avr_verify(), avr_write(), avrdude_message(), avrpart::desc, DEVICE_READ, DEVICE_VERIFY, DEVICE_WRITE, programmer_t::err_led, fileio(), update_t::filename, FIO_READ, FIO_WRITE, avrpart::flags, update_t::format, avrpart::mem, update_t::memtype, MSG_INFO, OFF, ON, update_t::op, pgm, progname, quell_progress, report_progress(), update_t::section, programmer_t::set_upload_size, programmer_t::type, UF_AUTO_ERASE, UF_NOWRITE, and programmer_t::vfy_led.

Referenced by avrdude_main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dup_update()

UPDATE * dup_update ( UPDATE upd)
180{
181 UPDATE * u;
182
183 u = (UPDATE *)malloc(sizeof(UPDATE));
184 if (u == NULL) {
185 // avrdude_message(MSG_INFO, "%s: out of memory\n", progname);
186 // exit(1);
187 avrdude_oom("dup_update: out of memory\n");
188 }
189
190 memcpy(u, upd, sizeof(UPDATE));
191
192 if (upd->memtype != NULL)
193 u->memtype = strdup(upd->memtype);
194 else
195 u->memtype = NULL;
196 u->filename = strdup(upd->filename);
197
198 return u;
199}
void avrdude_oom(const char *context)
Definition main.c:169
void * malloc(YYSIZE_T)
Definition libavrdude.h:868

References avrdude_oom(), update_t::filename, malloc(), and update_t::memtype.

Referenced by avrdude_main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ free_update()

void free_update ( UPDATE u)
222{
223 if (u != NULL) {
224 if(u->memtype != NULL) {
225 free(u->memtype);
226 u->memtype = NULL;
227 }
228 if(u->filename != NULL) {
229 free(u->filename);
230 u->filename = NULL;
231 }
232 free(u);
233 }
234}
void free(void *)

References update_t::filename, free(), and update_t::memtype.

Referenced by cleanup_main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ new_update()

UPDATE * new_update ( int  op,
char *  memtype,
int  filefmt,
char *  filename,
unsigned  section 
)
202{
203 UPDATE * u;
204
205 u = (UPDATE *)malloc(sizeof(UPDATE));
206 if (u == NULL) {
207 // avrdude_message(MSG_INFO, "%s: out of memory\n", progname);
208 // exit(1);
209 avrdude_oom("new_update: out of memory\n");
210 }
211
212 u->memtype = strdup(memtype);
213 u->filename = strdup(filename);
214 u->op = op;
215 u->format = filefmt;
216 u->section = section;
217
218 return u;
219}

References avrdude_oom(), update_t::filename, update_t::format, malloc(), update_t::memtype, update_t::op, and update_t::section.

Referenced by Slic3r::PresetUpdater::priv::get_config_updates().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_op()

UPDATE * parse_op ( char *  s)
32{
33 char buf[1024];
34 char * p, * cp, c;
35 UPDATE * upd;
36 int i;
37 size_t fnlen;
38
39 upd = (UPDATE *)malloc(sizeof(UPDATE));
40 if (upd == NULL) {
41 // avrdude_message(MSG_INFO, "%s: out of memory\n", progname);
42 // exit(1);
43 avrdude_oom("parse_op: out of memory\n");
44 }
45
46 i = 0;
47 p = s;
48 while ((i < (sizeof(buf)-1) && *p && (*p != ':')))
49 buf[i++] = *p++;
50 buf[i] = 0;
51
52 if (*p != ':') {
53 upd->memtype = NULL; /* default memtype, "flash", or "application" */
54 upd->op = DEVICE_WRITE;
55 upd->filename = (char *)malloc(strlen(buf) + 1);
56 if (upd->filename == NULL) {
57 // avrdude_message(MSG_INFO, "%s: out of memory\n", progname);
58 // exit(1);
59 avrdude_oom("parse_op: out of memory\n");
60 }
61 strcpy(upd->filename, buf);
62 upd->format = FMT_AUTO;
63 return upd;
64 }
65
66 upd->memtype = (char *)malloc(strlen(buf)+1);
67 if (upd->memtype == NULL) {
68 // avrdude_message(MSG_INFO, "%s: out of memory\n", progname);
69 // exit(1);
70 avrdude_oom("parse_op: out of memory\n");
71 }
72 strcpy(upd->memtype, buf);
73
74 p++;
75 if (*p == 'r') {
76 upd->op = DEVICE_READ;
77 }
78 else if (*p == 'w') {
79 upd->op = DEVICE_WRITE;
80 }
81 else if (*p == 'v') {
82 upd->op = DEVICE_VERIFY;
83 }
84 else {
85 avrdude_message(MSG_INFO, "%s: invalid I/O mode '%c' in update specification\n",
86 progname, *p);
87 avrdude_message(MSG_INFO, " allowed values are:\n"
88 " r = read device\n"
89 " w = write device\n"
90 " v = verify device\n");
91 free(upd->memtype);
92 free(upd);
93 return NULL;
94 }
95
96 p++;
97
98 if (*p != ':') {
99 avrdude_message(MSG_INFO, "%s: invalid update specification\n", progname);
100 free(upd->memtype);
101 free(upd);
102 return NULL;
103 }
104
105 p++;
106
107 // Extension: Parse file section number
108 unsigned section = 0;
109
110 for (; *p != ':'; p++) {
111 if (*p >= '0' && *p <= '9') {
112 section *= 10;
113 section += *p - 0x30;
114 } else {
115 avrdude_message(MSG_INFO, "%s: invalid update specification: <section> is not a number\n", progname);
116 free(upd->memtype);
117 free(upd);
118 return NULL;
119 }
120 }
121
122 upd->section = section;
123 p++;
124
125 /*
126 * Now, parse the filename component. Instead of looking for the
127 * leftmost possible colon delimiter, we look for the rightmost one.
128 * If we found one, we do have a trailing :format specifier, and
129 * process it. Otherwise, the remainder of the string is our file
130 * name component. That way, the file name itself is allowed to
131 * contain a colon itself (e. g. C:/some/file.hex), except the
132 * optional format specifier becomes mandatory then.
133 */
134 cp = p;
135 p = strrchr(cp, ':');
136 if (p == NULL) {
137 upd->format = FMT_AUTO;
138 fnlen = strlen(cp);
139 upd->filename = (char *)malloc(fnlen + 1);
140 } else {
141 fnlen = p - cp;
142 upd->filename = (char *)malloc(fnlen +1);
143 c = *++p;
144 if (c && p[1])
145 /* More than one char - force failure below. */
146 c = '?';
147 switch (c) {
148 case 'a': upd->format = FMT_AUTO; break;
149 case 's': upd->format = FMT_SREC; break;
150 case 'i': upd->format = FMT_IHEX; break;
151 case 'r': upd->format = FMT_RBIN; break;
152 case 'e': upd->format = FMT_ELF; break;
153 case 'm': upd->format = FMT_IMM; break;
154 case 'b': upd->format = FMT_BIN; break;
155 case 'd': upd->format = FMT_DEC; break;
156 case 'h': upd->format = FMT_HEX; break;
157 case 'o': upd->format = FMT_OCT; break;
158 default:
159 avrdude_message(MSG_INFO, "%s: invalid file format '%s' in update specifier\n",
160 progname, p);
161 free(upd->memtype);
162 free(upd);
163 return NULL;
164 }
165 }
166
167 if (upd->filename == NULL) {
168 avrdude_message(MSG_INFO, "%s: out of memory\n", progname);
169 free(upd->memtype);
170 free(upd);
171 return NULL;
172 }
173 memcpy(upd->filename, cp, fnlen);
174 upd->filename[fnlen] = 0;
175
176 return upd;
177}
@ FMT_AUTO
Definition libavrdude.h:787
@ FMT_RBIN
Definition libavrdude.h:790
@ FMT_HEX
Definition libavrdude.h:792
@ FMT_BIN
Definition libavrdude.h:795
@ FMT_OCT
Definition libavrdude.h:794
@ FMT_IHEX
Definition libavrdude.h:789
@ FMT_ELF
Definition libavrdude.h:796
@ FMT_IMM
Definition libavrdude.h:791
@ FMT_DEC
Definition libavrdude.h:793
@ FMT_SREC
Definition libavrdude.h:788

References avrdude_message(), avrdude_oom(), DEVICE_READ, DEVICE_VERIFY, DEVICE_WRITE, update_t::filename, FMT_AUTO, FMT_BIN, FMT_DEC, FMT_ELF, FMT_HEX, FMT_IHEX, FMT_IMM, FMT_OCT, FMT_RBIN, FMT_SREC, update_t::format, free(), malloc(), update_t::memtype, MSG_INFO, update_t::op, progname, and update_t::section.

Referenced by avrdude_main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: