blob: a93810934ac90ec670792b979163d9a693b97d92 [file] [log] [blame]
Lukasz Majewskif22b11c2012-08-06 14:41:07 +02001/*
2 * dfu.c -- DFU back-end routines
3 *
4 * Copyright (C) 2012 Samsung Electronics
5 * author: Lukasz Majewski <l.majewski@samsung.com>
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskif22b11c2012-08-06 14:41:07 +02008 */
9
10#include <common.h>
Heiko Schochere7e75c72013-06-12 06:05:51 +020011#include <errno.h>
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020012#include <malloc.h>
13#include <mmc.h>
14#include <fat.h>
15#include <dfu.h>
16#include <linux/list.h>
17#include <linux/compiler.h>
18
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020019static bool dfu_reset_request;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020020static LIST_HEAD(dfu_list);
21static int dfu_alt_num;
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +010022static int alt_num_cnt;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020023
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020024bool dfu_reset(void)
25{
26 return dfu_reset_request;
27}
28
29void dfu_trigger_reset()
30{
31 dfu_reset_request = true;
32}
33
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020034static int dfu_find_alt_num(const char *s)
35{
36 int i = 0;
37
38 for (; *s; s++)
39 if (*s == ';')
40 i++;
41
42 return ++i;
43}
44
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020045int dfu_init_env_entities(char *interface, int dev)
46{
47 const char *str_env;
48 char *env_bkp;
49 int ret;
50
51 str_env = getenv("dfu_alt_info");
52 if (!str_env) {
53 error("\"dfu_alt_info\" env variable not defined!\n");
54 return -EINVAL;
55 }
56
57 env_bkp = strdup(str_env);
58 ret = dfu_config_entities(env_bkp, interface, dev);
59 if (ret) {
60 error("DFU entities configuration failed!\n");
61 return ret;
62 }
63
64 free(env_bkp);
65 return 0;
66}
67
Heiko Schochere7e75c72013-06-12 06:05:51 +020068static unsigned char *dfu_buf;
69static unsigned long dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
70
Lukasz Majewskid4278262013-10-08 14:30:39 +020071unsigned char *dfu_free_buf(void)
Heiko Schochere7e75c72013-06-12 06:05:51 +020072{
73 free(dfu_buf);
74 dfu_buf = NULL;
75 return dfu_buf;
76}
77
Lukasz Majewski4fb12782013-12-09 16:20:13 +010078unsigned long dfu_get_buf_size(void)
79{
80 return dfu_buf_size;
81}
82
Lukasz Majewskid4278262013-10-08 14:30:39 +020083unsigned char *dfu_get_buf(void)
Heiko Schochere7e75c72013-06-12 06:05:51 +020084{
85 char *s;
86
87 if (dfu_buf != NULL)
88 return dfu_buf;
89
90 s = getenv("dfu_bufsiz");
91 dfu_buf_size = s ? (unsigned long)simple_strtol(s, NULL, 16) :
92 CONFIG_SYS_DFU_DATA_BUF_SIZE;
93
94 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
95 if (dfu_buf == NULL)
96 printf("%s: Could not memalign 0x%lx bytes\n",
97 __func__, dfu_buf_size);
98
99 return dfu_buf;
100}
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200101
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000102static int dfu_write_buffer_drain(struct dfu_entity *dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200103{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000104 long w_size;
105 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200106
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000107 /* flush size? */
108 w_size = dfu->i_buf - dfu->i_buf_start;
109 if (w_size == 0)
110 return 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200111
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000112 /* update CRC32 */
113 dfu->crc = crc32(dfu->crc, dfu->i_buf_start, w_size);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200114
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000115 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
116 if (ret)
117 debug("%s: Write error!\n", __func__);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200118
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000119 /* point back */
120 dfu->i_buf = dfu->i_buf_start;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200121
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000122 /* update offset */
123 dfu->offset += w_size;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200124
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000125 puts("#");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200126
127 return ret;
128}
129
Heiko Schochera2199af2014-03-18 08:09:55 +0100130int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
131{
132 int ret = 0;
133
Przemyslaw Marczak1aa4bdc2014-05-12 12:05:33 +0200134 ret = dfu_write_buffer_drain(dfu);
135 if (ret)
136 return ret;
137
Heiko Schochera2199af2014-03-18 08:09:55 +0100138 if (dfu->flush_medium)
139 ret = dfu->flush_medium(dfu);
140
141 printf("\nDFU complete CRC32: 0x%08x\n", dfu->crc);
142
143 /* clear everything */
144 dfu_free_buf();
145 dfu->crc = 0;
146 dfu->offset = 0;
147 dfu->i_blk_seq_num = 0;
148 dfu->i_buf_start = dfu_buf;
149 dfu->i_buf_end = dfu_buf;
150 dfu->i_buf = dfu->i_buf_start;
151 dfu->inited = 0;
152
153 return ret;
154}
155
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000156int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200157{
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200158 int ret = 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000159 int tret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200160
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000161 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%x\n",
162 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
163 dfu->i_buf - dfu->i_buf_start);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200164
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000165 if (!dfu->inited) {
166 /* initial state */
167 dfu->crc = 0;
168 dfu->offset = 0;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000169 dfu->bad_skip = 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000170 dfu->i_blk_seq_num = 0;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200171 dfu->i_buf_start = dfu_get_buf();
172 if (dfu->i_buf_start == NULL)
173 return -ENOMEM;
174 dfu->i_buf_end = dfu_get_buf() + dfu_buf_size;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000175 dfu->i_buf = dfu->i_buf_start;
176
177 dfu->inited = 1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200178 }
179
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000180 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200181 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000182 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200183 return -1;
184 }
185
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000186 /* DFU 1.1 standard says:
187 * The wBlockNum field is a block sequence number. It increments each
188 * time a block is transferred, wrapping to zero from 65,535. It is used
189 * to provide useful context to the DFU loader in the device."
190 *
191 * This means that it's a 16 bit counter that roll-overs at
192 * 0xffff -> 0x0000. By having a typical 4K transfer block
193 * we roll-over at exactly 256MB. Not very fun to debug.
194 *
195 * Handling rollover, and having an inited variable,
196 * makes things work.
197 */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200198
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000199 /* handle rollover */
200 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
201
202 /* flush buffer if overflow */
203 if ((dfu->i_buf + size) > dfu->i_buf_end) {
204 tret = dfu_write_buffer_drain(dfu);
205 if (ret == 0)
206 ret = tret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200207 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000208
209 /* we should be in buffer now (if not then size too large) */
210 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Lukasz Majewskia7d2c3c2013-09-10 15:29:22 +0200211 error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
212 size, dfu->i_buf_end);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000213 return -1;
214 }
215
216 memcpy(dfu->i_buf, buf, size);
217 dfu->i_buf += size;
218
219 /* if end or if buffer full flush */
220 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
221 tret = dfu_write_buffer_drain(dfu);
222 if (ret == 0)
223 ret = tret;
224 }
225
Lukasz Majewski672ad182014-04-24 10:24:53 +0200226 return ret;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000227}
228
229static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
230{
231 long chunk;
232 int ret, readn;
233
234 readn = 0;
235 while (size > 0) {
236 /* get chunk that can be read */
237 chunk = min(size, dfu->b_left);
238 /* consume */
239 if (chunk > 0) {
240 memcpy(buf, dfu->i_buf, chunk);
241 dfu->crc = crc32(dfu->crc, buf, chunk);
242 dfu->i_buf += chunk;
243 dfu->b_left -= chunk;
Bo Shenc2617bc2013-10-16 17:26:37 +0800244 dfu->r_left -= chunk;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000245 size -= chunk;
246 buf += chunk;
247 readn += chunk;
248 }
249
250 /* all done */
251 if (size > 0) {
252 /* no more to read */
253 if (dfu->r_left == 0)
254 break;
255
256 dfu->i_buf = dfu->i_buf_start;
257 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
258
259 /* got to read, but buffer is empty */
260 if (dfu->b_left > dfu->r_left)
261 dfu->b_left = dfu->r_left;
262 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
263 &dfu->b_left);
264 if (ret != 0) {
265 debug("%s: Read error!\n", __func__);
266 return ret;
267 }
268 dfu->offset += dfu->b_left;
269 dfu->r_left -= dfu->b_left;
270
271 puts("#");
272 }
273 }
274
275 return readn;
276}
277
278int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
279{
280 int ret = 0;
281
282 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
283 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
284
285 if (!dfu->inited) {
Łukasz Majewski9df49552013-06-28 18:41:50 +0200286 dfu->i_buf_start = dfu_get_buf();
287 if (dfu->i_buf_start == NULL)
288 return -ENOMEM;
289
290 ret = dfu->read_medium(dfu, 0, dfu->i_buf_start, &dfu->r_left);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000291 if (ret != 0) {
292 debug("%s: failed to get r_left\n", __func__);
293 return ret;
294 }
295
296 debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);
297
298 dfu->i_blk_seq_num = 0;
299 dfu->crc = 0;
300 dfu->offset = 0;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200301 dfu->i_buf_end = dfu_get_buf() + dfu_buf_size;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000302 dfu->i_buf = dfu->i_buf_start;
Bo Shen31bae4c2013-10-16 17:26:38 +0800303 dfu->b_left = min(dfu_buf_size, dfu->r_left);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000304
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000305 dfu->bad_skip = 0;
306
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000307 dfu->inited = 1;
308 }
309
310 if (dfu->i_blk_seq_num != blk_seq_num) {
311 printf("%s: Wrong sequence number! [%d] [%d]\n",
312 __func__, dfu->i_blk_seq_num, blk_seq_num);
313 return -1;
314 }
315 /* handle rollover */
316 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
317
318 ret = dfu_read_buffer_fill(dfu, buf, size);
319 if (ret < 0) {
320 printf("%s: Failed to fill buffer\n", __func__);
321 return -1;
322 }
323
324 if (ret < size) {
325 debug("%s: %s CRC32: 0x%x\n", __func__, dfu->name, dfu->crc);
326 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
327
Heiko Schochere7e75c72013-06-12 06:05:51 +0200328 dfu_free_buf();
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000329 dfu->i_blk_seq_num = 0;
330 dfu->crc = 0;
331 dfu->offset = 0;
332 dfu->i_buf_start = dfu_buf;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200333 dfu->i_buf_end = dfu_buf;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000334 dfu->i_buf = dfu->i_buf_start;
335 dfu->b_left = 0;
336
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000337 dfu->bad_skip = 0;
338
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000339 dfu->inited = 0;
340 }
341
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200342 return ret;
343}
344
345static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200346 char *interface, int num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200347{
348 char *st;
349
350 debug("%s: %s interface: %s num: %d\n", __func__, s, interface, num);
351 st = strsep(&s, " ");
352 strcpy(dfu->name, st);
353
354 dfu->dev_num = num;
355 dfu->alt = alt;
356
357 /* Specific for mmc device */
358 if (strcmp(interface, "mmc") == 0) {
359 if (dfu_fill_entity_mmc(dfu, s))
360 return -1;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000361 } else if (strcmp(interface, "nand") == 0) {
362 if (dfu_fill_entity_nand(dfu, s))
363 return -1;
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530364 } else if (strcmp(interface, "ram") == 0) {
365 if (dfu_fill_entity_ram(dfu, s))
366 return -1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200367 } else {
368 printf("%s: Device %s not (yet) supported!\n",
369 __func__, interface);
370 return -1;
371 }
372
373 return 0;
374}
375
376void dfu_free_entities(void)
377{
378 struct dfu_entity *dfu, *p, *t = NULL;
379
380 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
381 list_del(&dfu->list);
382 t = dfu;
383 }
384 if (t)
385 free(t);
386 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100387
388 alt_num_cnt = 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200389}
390
391int dfu_config_entities(char *env, char *interface, int num)
392{
393 struct dfu_entity *dfu;
394 int i, ret;
395 char *s;
396
397 dfu_alt_num = dfu_find_alt_num(env);
398 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
399
400 dfu = calloc(sizeof(*dfu), dfu_alt_num);
401 if (!dfu)
402 return -1;
403 for (i = 0; i < dfu_alt_num; i++) {
404
405 s = strsep(&env, ";");
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100406 ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface, num);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200407 if (ret)
408 return -1;
409
410 list_add_tail(&dfu[i].list, &dfu_list);
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100411 alt_num_cnt++;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200412 }
413
414 return 0;
415}
416
417const char *dfu_get_dev_type(enum dfu_device_type t)
418{
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530419 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM" };
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200420 return dev_t[t];
421}
422
423const char *dfu_get_layout(enum dfu_layout l)
424{
425 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530426 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200427 return dfu_layout[l];
428}
429
430void dfu_show_entities(void)
431{
432 struct dfu_entity *dfu;
433
434 puts("DFU alt settings list:\n");
435
436 list_for_each_entry(dfu, &dfu_list, list) {
437 printf("dev: %s alt: %d name: %s layout: %s\n",
438 dfu_get_dev_type(dfu->dev_type), dfu->alt,
439 dfu->name, dfu_get_layout(dfu->layout));
440 }
441}
442
443int dfu_get_alt_number(void)
444{
445 return dfu_alt_num;
446}
447
448struct dfu_entity *dfu_get_entity(int alt)
449{
450 struct dfu_entity *dfu;
451
452 list_for_each_entry(dfu, &dfu_list, list) {
453 if (dfu->alt == alt)
454 return dfu;
455 }
456
457 return NULL;
458}
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200459
460int dfu_get_alt(char *name)
461{
462 struct dfu_entity *dfu;
463
464 list_for_each_entry(dfu, &dfu_list, list) {
465 if (!strncmp(dfu->name, name, strlen(dfu->name)))
466 return dfu->alt;
467 }
468
469 return -ENODEV;
470}